예제 #1
0
  public LambdaInfo(
      @NotNull KtExpression expression,
      @NotNull KotlinTypeMapper typeMapper,
      boolean isCrossInline,
      boolean isBoundCallableReference) {
    this.isCrossInline = isCrossInline;
    this.expression =
        expression instanceof KtLambdaExpression
            ? ((KtLambdaExpression) expression).getFunctionLiteral()
            : expression;

    this.typeMapper = typeMapper;
    this.isBoundCallableReference = isBoundCallableReference;
    BindingContext bindingContext = typeMapper.getBindingContext();
    FunctionDescriptor function = bindingContext.get(BindingContext.FUNCTION, this.expression);
    if (function == null && expression instanceof KtCallableReferenceExpression) {
      VariableDescriptor variableDescriptor =
          bindingContext.get(BindingContext.VARIABLE, this.expression);
      assert variableDescriptor instanceof VariableDescriptorWithAccessors
          : "Reference expression not resolved to variable descriptor with accessors: "
              + expression.getText();
      classDescriptor =
          CodegenBinding.anonymousClassForCallable(bindingContext, variableDescriptor);
      closureClassType = typeMapper.mapClass(classDescriptor);
      SimpleFunctionDescriptor getFunction =
          PropertyReferenceCodegen.findGetFunction(variableDescriptor);
      functionDescriptor =
          PropertyReferenceCodegen.createFakeOpenDescriptor(getFunction, classDescriptor);
      ResolvedCall<?> resolvedCall =
          CallUtilKt.getResolvedCallWithAssert(
              ((KtCallableReferenceExpression) expression).getCallableReference(), bindingContext);
      propertyReferenceInfo =
          new PropertyReferenceInfo(
              (VariableDescriptor) resolvedCall.getResultingDescriptor(), getFunction);
    } else {
      propertyReferenceInfo = null;
      functionDescriptor = function;
      assert functionDescriptor != null
          : "Function is not resolved to descriptor: " + expression.getText();
      classDescriptor = anonymousClassForCallable(bindingContext, functionDescriptor);
      closureClassType = asmTypeForAnonymousClass(bindingContext, functionDescriptor);
    }

    closure = bindingContext.get(CLOSURE, classDescriptor);
    assert closure != null : "Closure for lambda should be not null " + expression.getText();

    labels = InlineCodegen.getDeclarationLabels(expression, functionDescriptor);
  }
  @NotNull
  @Override
  public LightClassConstructionContext getContextForClassOrObject(
      @NotNull JetClassOrObject classOrObject) {
    ResolveSessionForBodies session =
        KotlinCacheService.getInstance(classOrObject.getProject())
            .getLazyResolveSession(classOrObject);

    if (classOrObject.isLocal()) {
      BindingContext bindingContext = session.resolveToElement(classOrObject, BodyResolveMode.FULL);
      ClassDescriptor descriptor = bindingContext.get(BindingContext.CLASS, classOrObject);

      if (descriptor == null) {
        LOG.warn(
            "No class descriptor in context for class: "
                + PsiUtilPackage.getElementTextWithContext(classOrObject));
        return new LightClassConstructionContext(bindingContext, session.getModuleDescriptor());
      }

      ForceResolveUtil.forceResolveAllContents(descriptor);

      return new LightClassConstructionContext(bindingContext, session.getModuleDescriptor());
    }

    ForceResolveUtil.forceResolveAllContents(session.getClassDescriptor(classOrObject));
    return new LightClassConstructionContext(
        session.getBindingContext(), session.getModuleDescriptor());
  }
 private void generateCopyFunctionForDataClasses(List<JetParameter> constructorParameters) {
   FunctionDescriptor copyFunction =
       bindingContext.get(BindingContext.DATA_CLASS_COPY_FUNCTION, classDescriptor);
   if (copyFunction != null) {
     generateCopyFunction(copyFunction, constructorParameters);
   }
 }
예제 #4
0
  public void gen(@NotNull KtNamedFunction function) {
    SimpleFunctionDescriptor functionDescriptor =
        bindingContext.get(BindingContext.FUNCTION, function);
    assert functionDescriptor != null
        : "No descriptor for function "
            + function.getText()
            + "\n"
            + "in "
            + function.getContainingFile().getVirtualFile();

    if (owner.getContextKind() != OwnerKind.DEFAULT_IMPLS || function.hasBody()) {
      generateMethod(
          JvmDeclarationOriginKt.OtherOrigin(function, functionDescriptor),
          functionDescriptor,
          new FunctionGenerationStrategy.FunctionDefault(state, functionDescriptor, function));
    }

    generateDefaultIfNeeded(
        owner.intoFunction(functionDescriptor),
        functionDescriptor,
        owner.getContextKind(),
        DefaultParameterValueLoader.DEFAULT,
        function);

    generateOverloadsWithDefaultValues(function, functionDescriptor, functionDescriptor);
  }
  @NotNull
  private static KotlinType getPropertyType(@NotNull KtProperty property) {
    BindingContext bindingContext = ResolutionUtils.analyze(property, BodyResolveMode.PARTIAL);

    VariableDescriptor propertyDescriptor = bindingContext.get(BindingContext.VARIABLE, property);
    assert propertyDescriptor != null
        : "Couldn't resolve property to property descriptor " + property.getText();
    return propertyDescriptor.getType();
  }
 private List<PropertyDescriptor> getDataProperties() {
   List<PropertyDescriptor> result = Lists.newArrayList();
   for (JetParameter parameter : getPrimaryConstructorParameters()) {
     if (parameter.hasValOrVar()) {
       result.add(bindingContext.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter));
     }
   }
   return result;
 }
  private void generateComponentFunctionsForDataClasses() {
    ConstructorDescriptor constructor = classDescriptor.getUnsubstitutedPrimaryConstructor();
    // primary constructor should exist for data classes
    // but when generating light-classes still need to check we have one
    if (constructor == null) return;

    for (ValueParameterDescriptor parameter : constructor.getValueParameters()) {
      FunctionDescriptor function =
          bindingContext.get(BindingContext.DATA_CLASS_COMPONENT_FUNCTION, parameter);
      if (function != null) {
        generateComponentFunction(function, parameter);
      }
    }
  }
  private static Map<DeclarationDescriptor, List<String>> getActualLoadErrors(
      @NotNull BindingContext bindingContext) {
    Map<DeclarationDescriptor, List<String>> result =
        new HashMap<DeclarationDescriptor, List<String>>();

    Collection<DeclarationDescriptor> descriptors =
        bindingContext.getKeys(JavaBindingContext.LOAD_FROM_JAVA_SIGNATURE_ERRORS);
    for (DeclarationDescriptor descriptor : descriptors) {
      List<String> errors =
          bindingContext.get(JavaBindingContext.LOAD_FROM_JAVA_SIGNATURE_ERRORS, descriptor);
      result.put(descriptor, errors);
    }

    return result;
  }
  private static Boolean isResolvedToDescriptor(
      JetValueArgumentList argumentList,
      FunctionDescriptor functionDescriptor,
      BindingContext bindingContext) {
    JetSimpleNameExpression callNameExpression = getCallSimpleNameExpression(argumentList);
    if (callNameExpression != null) {
      DeclarationDescriptor declarationDescriptor =
          bindingContext.get(BindingContext.REFERENCE_TARGET, callNameExpression);
      if (declarationDescriptor != null) {
        if (declarationDescriptor == functionDescriptor) {
          return true;
        }
      }
    }

    return false;
  }
예제 #10
0
  @Nullable
  private static PsiNamedElement[] getSupertypes(Expression[] params, ExpressionContext context) {
    if (params.length != 0) return null;

    Project project = context.getProject();
    PsiDocumentManager.getInstance(project).commitAllDocuments();

    PsiFile psiFile =
        PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument());
    if (!(psiFile instanceof JetFile)) return null;

    JetExpression expression =
        PsiTreeUtil.getParentOfType(
            psiFile.findElementAt(context.getStartOffset()), JetExpression.class);
    if (expression == null) return null;

    BindingContext bc = ResolutionUtils.analyze(expression, BodyResolveMode.FULL);
    JetScope scope = bc.get(BindingContext.RESOLUTION_SCOPE, expression);
    if (scope == null) return null;

    List<PsiNamedElement> result = new ArrayList<PsiNamedElement>();

    for (DeclarationDescriptor descriptor :
        scope.getDescriptors(
            DescriptorKindFilter.NON_SINGLETON_CLASSIFIERS,
            JetScope.Companion.getALL_NAME_FILTER())) {
      if (!(descriptor instanceof ClassDescriptor)) continue;
      ClassDescriptor classDescriptor = (ClassDescriptor) descriptor;
      if (!classDescriptor.getModality().isOverridable()) continue;
      ClassKind kind = classDescriptor.getKind();
      if (kind == ClassKind.INTERFACE || kind == ClassKind.CLASS) {
        PsiElement declaration = DescriptorToSourceUtils.descriptorToDeclaration(descriptor);
        if (declaration != null) {
          result.add((PsiNamedElement) declaration);
        }
      }
    }

    return result.toArray(new PsiNamedElement[result.size()]);
  }
  private static JetValueArgumentList findCall(CreateParameterInfoContext context) {
    // todo: calls to this constructors, when we will have auxiliary constructors
    PsiFile file = context.getFile();
    if (!(file instanceof JetFile)) {
      return null;
    }

    JetValueArgumentList argumentList =
        PsiTreeUtil.getParentOfType(
            file.findElementAt(context.getOffset()), JetValueArgumentList.class);
    if (argumentList == null) {
      return null;
    }

    final JetSimpleNameExpression callNameExpression = getCallSimpleNameExpression(argumentList);
    if (callNameExpression == null) {
      return null;
    }

    PsiReference[] references = callNameExpression.getReferences();
    if (references.length == 0) {
      return null;
    }

    ResolutionFacade resolutionFacade =
        ResolvePackage.getResolutionFacade(callNameExpression.getContainingJetFile());
    final BindingContext bindingContext =
        resolutionFacade.analyze(callNameExpression, BodyResolveMode.FULL);
    ModuleDescriptor moduleDescriptor = resolutionFacade.findModuleDescriptor(callNameExpression);

    JetScope scope = bindingContext.get(BindingContext.RESOLUTION_SCOPE, callNameExpression);
    final DeclarationDescriptor placeDescriptor;
    if (scope != null) {
      placeDescriptor = scope.getContainingDeclaration();
    } else {
      placeDescriptor = null;
    }
    Function1<DeclarationDescriptor, Boolean> visibilityFilter =
        new Function1<DeclarationDescriptor, Boolean>() {
          @Override
          public Boolean invoke(DeclarationDescriptor descriptor) {
            if (placeDescriptor == null) return true;
            if (!(descriptor instanceof DeclarationDescriptorWithVisibility)) return true;
            return CorePackage.isVisible(
                (DeclarationDescriptorWithVisibility) descriptor,
                placeDescriptor,
                bindingContext,
                callNameExpression);
          }
        };

    final Name refName = callNameExpression.getReferencedNameAsName();

    Function1<Name, Boolean> nameFilter =
        new Function1<Name, Boolean>() {
          @Override
          public Boolean invoke(Name name) {
            return name.equals(refName);
          }
        };
    Collection<DeclarationDescriptor> variants =
        new ReferenceVariantsHelper(
                bindingContext, moduleDescriptor, file.getProject(), visibilityFilter)
            .getReferenceVariants(
                callNameExpression,
                new DescriptorKindFilter(
                    DescriptorKindFilter.FUNCTIONS_MASK | DescriptorKindFilter.CLASSIFIERS_MASK,
                    Collections.<DescriptorKindExclude>emptyList()),
                nameFilter,
                false,
                false);

    Collection<Pair<? extends DeclarationDescriptor, ResolutionFacade>> itemsToShow =
        new ArrayList<Pair<? extends DeclarationDescriptor, ResolutionFacade>>();
    for (DeclarationDescriptor variant : variants) {
      if (variant instanceof FunctionDescriptor) {
        // todo: renamed functions?
        itemsToShow.add(Pair.create((FunctionDescriptor) variant, resolutionFacade));
      } else if (variant instanceof ClassDescriptor) {
        // todo: renamed classes?
        for (ConstructorDescriptor constructorDescriptor :
            ((ClassDescriptor) variant).getConstructors()) {
          itemsToShow.add(Pair.create(constructorDescriptor, resolutionFacade));
        }
      }
    }

    context.setItemsToShow(ArrayUtil.toObjectArray(itemsToShow));
    return argumentList;
  }