Ejemplo n.º 1
0
 @NotNull
 public static JetExpression createStubExpressionOfNecessaryType(
     @NotNull Project project, @NotNull JetType type, @NotNull BindingTrace trace) {
   JetExpression expression = JetPsiFactory.createExpression(project, "$e");
   trace.record(PROCESSED, expression);
   trace.record(EXPRESSION_TYPE, expression, type);
   return expression;
 }
Ejemplo n.º 2
0
  /**
   * This method tries to resolve descriptors ambiguity between class descriptor and package
   * descriptor for the same class. It's ok choose class for expression reference resolution.
   *
   * @return <code>true</code> if method has successfully resolved ambiguity
   */
  private static boolean resolveClassPackageAmbiguity(
      @NotNull Collection<DeclarationDescriptor> filteredDescriptors,
      @NotNull JetSimpleNameExpression referenceExpression,
      @NotNull JetScope resolutionScope,
      @NotNull BindingTrace trace,
      @NotNull JetScope scopeToCheckVisibility) {
    if (filteredDescriptors.size() == 2) {
      PackageViewDescriptor packageView = null;
      ClassDescriptor classDescriptor = null;

      for (DeclarationDescriptor filteredDescriptor : filteredDescriptors) {
        if (filteredDescriptor instanceof PackageViewDescriptor) {
          packageView = (PackageViewDescriptor) filteredDescriptor;
        } else if (filteredDescriptor instanceof ClassDescriptor) {
          classDescriptor = (ClassDescriptor) filteredDescriptor;
        }
      }

      if (packageView != null && classDescriptor != null) {
        if (packageView.getFqName().equalsTo(DescriptorUtils.getFqName(classDescriptor))) {
          trace.record(BindingContext.REFERENCE_TARGET, referenceExpression, classDescriptor);
          trace.record(BindingContext.RESOLUTION_SCOPE, referenceExpression, resolutionScope);
          checkVisibility(classDescriptor, trace, referenceExpression, scopeToCheckVisibility);
          return true;
        }
      }
    }

    return false;
  }
Ejemplo n.º 3
0
 private static void recordConstructorDelegationCall(
     @NotNull BindingTrace trace,
     @NotNull ConstructorDescriptor constructor,
     @NotNull ResolvedCall<?> call) {
   //noinspection unchecked
   trace.record(
       CONSTRUCTOR_RESOLVED_DELEGATION_CALL,
       constructor,
       (ResolvedCall<ConstructorDescriptor>) call);
 }
Ejemplo n.º 4
0
  public static void recordFunctionDeclarationToDescriptor(
      @NotNull BindingTrace trace,
      @NotNull PsiElement psiElement,
      @NotNull SimpleFunctionDescriptor function) {

    if (function.getKind() != DECLARATION) {
      throw new IllegalArgumentException(
          "function of kind " + function.getKind() + " cannot have declaration");
    }

    trace.record(BindingContext.FUNCTION, psiElement, function);
  }
Ejemplo n.º 5
0
 @Nullable
 public static JetType updateRecordedType(
     @Nullable JetType type,
     @NotNull JetExpression expression,
     @NotNull BindingTrace trace,
     boolean shouldBeMadeNullable) {
   if (type == null) return null;
   if (shouldBeMadeNullable) {
     type = TypeUtils.makeNullable(type);
   }
   trace.record(BindingContext.EXPRESSION_TYPE, expression, type);
   return type;
 }
Ejemplo n.º 6
0
 public static void reportAmbiguousLabel(
     @NotNull BindingTrace trace,
     @NotNull JetSimpleNameExpression targetLabel,
     @NotNull Collection<DeclarationDescriptor> declarationsByLabel) {
   Collection<PsiElement> targets = Lists.newArrayList();
   for (DeclarationDescriptor descriptor : declarationsByLabel) {
     PsiElement element = DescriptorToSourceUtils.descriptorToDeclaration(descriptor);
     assert element != null : "Label can only point to something in the same lexical scope";
     targets.add(element);
   }
   if (!targets.isEmpty()) {
     trace.record(AMBIGUOUS_LABEL_TARGET, targetLabel, targets);
   }
   trace.report(AMBIGUOUS_LABEL.on(targetLabel));
 }
Ejemplo n.º 7
0
  private static void storeResolutionResult(
      @NotNull Collection<DeclarationDescriptor> descriptors,
      @NotNull Collection<DeclarationDescriptor> canBeImportedDescriptors,
      @NotNull JetSimpleNameExpression referenceExpression,
      @NotNull Collection<JetScope> possibleResolutionScopes,
      @NotNull BindingTrace trace,
      @NotNull JetScope scopeToCheckVisibility) {
    assert canBeImportedDescriptors.size() <= descriptors.size();
    assert !possibleResolutionScopes.isEmpty();
    // todo completion here needs all possible resolution scopes, if there are many
    JetScope resolutionScope = possibleResolutionScopes.iterator().next();

    // A special case - will fill all trace information
    if (resolveClassPackageAmbiguity(
        canBeImportedDescriptors,
        referenceExpression,
        resolutionScope,
        trace,
        scopeToCheckVisibility)) {
      return;
    }

    // Simple case of no descriptors
    if (descriptors.isEmpty()) {
      trace.record(BindingContext.RESOLUTION_SCOPE, referenceExpression, resolutionScope);
      trace.report(UNRESOLVED_REFERENCE.on(referenceExpression, referenceExpression));
      return;
    }

    // Decide if expression has resolved reference
    DeclarationDescriptor descriptor = null;
    if (descriptors.size() == 1) {
      descriptor = descriptors.iterator().next();
      assert canBeImportedDescriptors.size() <= 1;
    } else if (canBeImportedDescriptors.size() == 1) {
      descriptor = canBeImportedDescriptors.iterator().next();
    }
    if (descriptor != null) {
      trace.record(
          BindingContext.REFERENCE_TARGET, referenceExpression, descriptors.iterator().next());
      trace.record(BindingContext.RESOLUTION_SCOPE, referenceExpression, resolutionScope);

      if (descriptor instanceof DeclarationDescriptorWithVisibility) {
        checkVisibility(
            (DeclarationDescriptorWithVisibility) descriptor,
            trace,
            referenceExpression,
            scopeToCheckVisibility);
      }
    }

    // Check for more information and additional errors
    if (canBeImportedDescriptors.isEmpty()) {
      assert descriptors.size() >= 1;
      trace.report(CANNOT_BE_IMPORTED.on(referenceExpression, descriptors.iterator().next()));
      return;
    }
    if (canBeImportedDescriptors.size() > 1) {
      trace.record(BindingContext.AMBIGUOUS_REFERENCE_TARGET, referenceExpression, descriptors);
    }
  }