Ejemplo n.º 1
0
    public void serialize(FunctionDescriptor fun) {
      serialize(fun.getModality());
      sb.append(" ");

      if (!fun.getAnnotations().isEmpty()) {
        new Serializer(sb).serializeSeparated(fun.getAnnotations(), " ");
        sb.append(" ");
      }

      if (!fun.getOverriddenDescriptors().isEmpty()) {
        sb.append("override /*" + fun.getOverriddenDescriptors().size() + "*/ ");
      }

      if (fun instanceof ConstructorDescriptor) {
        sb.append("/*constructor*/ ");
      }
      sb.append("fun ");
      if (!fun.getTypeParameters().isEmpty()) {
        sb.append("<");
        new Serializer(sb).serializeCommaSeparated(fun.getTypeParameters());
        sb.append(">");
      }

      if (fun.getReceiverParameter().exists()) {
        new TypeSerializer(sb).serialize(fun.getReceiverParameter());
        sb.append(".");
      }

      sb.append(fun.getName());
      sb.append("(");
      new TypeSerializer(sb).serializeCommaSeparated(fun.getValueParameters());
      sb.append("): ");
      new TypeSerializer(sb).serialize(fun.getReturnType());
    }
Ejemplo n.º 2
0
  @NotNull
  public Collection<JetType> getSupertypesForClosure(@NotNull FunctionDescriptor descriptor) {
    ReceiverParameterDescriptor receiverParameter = descriptor.getReceiverParameter();

    List<TypeProjection> typeArguments = new ArrayList<TypeProjection>(2);

    ClassDescriptor classDescriptor;
    if (receiverParameter != null) {
      classDescriptor = extensionFunctionImpl;
      typeArguments.add(new TypeProjectionImpl(receiverParameter.getType()));
    } else {
      classDescriptor = functionImpl;
    }

    //noinspection ConstantConditions
    typeArguments.add(new TypeProjectionImpl(descriptor.getReturnType()));

    JetType functionImplType =
        new JetTypeImpl(
            classDescriptor.getDefaultType().getAnnotations(),
            classDescriptor.getTypeConstructor(),
            false,
            typeArguments,
            classDescriptor.getMemberScope(typeArguments));

    JetType functionType =
        KotlinBuiltIns.getInstance()
            .getFunctionType(
                Annotations.EMPTY,
                receiverParameter == null ? null : receiverParameter.getType(),
                DescriptorUtils.getValueParametersTypes(descriptor.getValueParameters()),
                descriptor.getReturnType());

    return Arrays.asList(functionImplType, functionType);
  }
Ejemplo n.º 3
0
  /* FUNCTIONS */
  private void renderFunction(
      @NotNull FunctionDescriptor function, @NotNull StringBuilder builder) {
    if (!startFromName) {
      renderAnnotations(function, builder);
      renderVisibility(function.getVisibility(), builder);
      renderModalityForCallable(function, builder);
      renderOverride(function, builder);
      renderMemberKind(function, builder);

      builder.append(renderKeyword("fun")).append(" ");
      renderTypeParameters(function.getTypeParameters(), builder, true);

      ReceiverParameterDescriptor receiver = function.getReceiverParameter();
      if (receiver != null) {
        builder.append(escape(renderType(receiver.getType()))).append(".");
      }
    }

    renderName(function, builder);
    renderValueParameters(function, builder);
    JetType returnType = function.getReturnType();
    if (unitReturnType || !KotlinBuiltIns.getInstance().isUnit(returnType)) {
      builder.append(": ").append(returnType == null ? "[NULL]" : escape(renderType(returnType)));
    }
    renderWhereSuffix(function.getTypeParameters(), builder);
  }
Ejemplo n.º 4
0
  private ClassDescriptor recordClassForFunction(FunctionDescriptor funDescriptor) {
    ClassDescriptor classDescriptor;
    int arity = funDescriptor.getValueParameters().size();

    classDescriptor =
        new ClassDescriptorImpl(
            funDescriptor.getContainingDeclaration(),
            Collections.<AnnotationDescriptor>emptyList(),
            Modality.FINAL,
            Name.special("<closure>"));
    ((ClassDescriptorImpl) classDescriptor)
        .initialize(
            false,
            Collections.<TypeParameterDescriptor>emptyList(),
            Collections.singleton(
                (funDescriptor.getReceiverParameter().exists()
                        ? JetStandardClasses.getReceiverFunction(arity)
                        : JetStandardClasses.getFunction(arity))
                    .getDefaultType()),
            JetScope.EMPTY,
            Collections.<ConstructorDescriptor>emptySet(),
            null);

    assert PsiCodegenPredictor.checkPredictedClassNameForFun(
        bindingContext, funDescriptor, classDescriptor);
    bindingTrace.record(CLASS_FOR_FUNCTION, funDescriptor, classDescriptor);
    return classDescriptor;
  }
 @NotNull
 public static JetScope getFunctionInnerScope(
     @NotNull JetScope outerScope,
     @NotNull FunctionDescriptor descriptor,
     @NotNull BindingTrace trace) {
   WritableScope parameterScope =
       new WritableScopeImpl(
           outerScope,
           descriptor,
           new TraceBasedRedeclarationHandler(trace),
           "Function inner scope");
   ReceiverParameterDescriptor receiver = descriptor.getReceiverParameter();
   if (receiver != null) {
     parameterScope.setImplicitReceiver(receiver);
   }
   for (TypeParameterDescriptor typeParameter : descriptor.getTypeParameters()) {
     parameterScope.addTypeParameterDescriptor(typeParameter);
   }
   for (ValueParameterDescriptor valueParameterDescriptor : descriptor.getValueParameters()) {
     parameterScope.addVariableDescriptor(valueParameterDescriptor);
   }
   parameterScope.addLabeledDeclaration(descriptor);
   parameterScope.changeLockLevel(WritableScope.LockLevel.READING);
   return parameterScope;
 }
Ejemplo n.º 6
0
  public static SimpleFunctionDescriptor createInvoke(FunctionDescriptor fd) {
    int arity = fd.getValueParameters().size();
    SimpleFunctionDescriptorImpl invokeDescriptor =
        new SimpleFunctionDescriptorImpl(
            fd.getExpectedThisObject().exists()
                ? JetStandardClasses.getReceiverFunction(arity)
                : JetStandardClasses.getFunction(arity),
            Collections.<AnnotationDescriptor>emptyList(),
            Name.identifier("invoke"),
            CallableMemberDescriptor.Kind.DECLARATION);

    invokeDescriptor.initialize(
        fd.getReceiverParameter().exists() ? fd.getReceiverParameter().getType() : null,
        fd.getExpectedThisObject(),
        Collections.<TypeParameterDescriptorImpl>emptyList(),
        fd.getValueParameters(),
        fd.getReturnType(),
        Modality.FINAL,
        Visibilities.PUBLIC,
        /*isInline = */ false);
    return invokeDescriptor;
  }
Ejemplo n.º 7
0
  @NotNull
  public Collection<JetType> getSupertypesForCallableReference(
      @NotNull FunctionDescriptor descriptor) {
    ReceiverParameterDescriptor receiverParameter = descriptor.getReceiverParameter();
    ReceiverParameterDescriptor expectedThisObject = descriptor.getExpectedThisObject();

    List<TypeProjection> typeArguments = new ArrayList<TypeProjection>(2);

    ClassDescriptor classDescriptor;
    JetType receiverType;
    if (receiverParameter != null) {
      classDescriptor = kExtensionFunctionImpl;
      receiverType = receiverParameter.getType();
      typeArguments.add(new TypeProjectionImpl(receiverType));
    } else if (expectedThisObject != null) {
      classDescriptor = kMemberFunctionImpl;
      receiverType = expectedThisObject.getType();
      typeArguments.add(new TypeProjectionImpl(receiverType));
    } else {
      classDescriptor = kFunctionImpl;
      receiverType = null;
    }

    //noinspection ConstantConditions
    typeArguments.add(new TypeProjectionImpl(descriptor.getReturnType()));

    JetType kFunctionImplType =
        new JetTypeImpl(
            classDescriptor.getDefaultType().getAnnotations(),
            classDescriptor.getTypeConstructor(),
            false,
            typeArguments,
            classDescriptor.getMemberScope(typeArguments));

    JetType kFunctionType =
        reflectionTypes.getKFunctionType(
            Annotations.EMPTY,
            receiverType,
            DescriptorUtils.getValueParametersTypes(descriptor.getValueParameters()),
            descriptor.getReturnType(),
            receiverParameter != null);

    return Arrays.asList(kFunctionImplType, kFunctionType);
  }
Ejemplo n.º 8
0
    @Override
    public Void visitFunctionDescriptor(FunctionDescriptor descriptor, StringBuilder builder) {
      renderVisibility(descriptor.getVisibility(), builder);
      renderModality(descriptor.getModality(), builder);
      builder.append(renderKeyword("fun")).append(" ");
      if (renderTypeParameters(descriptor.getTypeParameters(), builder)) {
        builder.append(" ");
      }

      ReceiverDescriptor receiver = descriptor.getReceiverParameter();
      if (receiver.exists()) {
        builder.append(escape(renderType(receiver.getType()))).append(".");
      }

      renderName(descriptor, builder);
      renderValueParameters(descriptor, builder);
      builder.append(" : ").append(escape(renderType(descriptor.getReturnType())));
      renderWhereSuffix(descriptor, builder);
      return null;
    }
Ejemplo n.º 9
0
  public static boolean containsErrorType(@NotNull FunctionDescriptor function) {
    if (containsErrorType(function.getReturnType())) {
      return true;
    }
    ReceiverParameterDescriptor receiverParameter = function.getReceiverParameter();
    if (receiverParameter != null && containsErrorType(receiverParameter.getType())) {
      return true;
    }
    for (ValueParameterDescriptor parameter : function.getValueParameters()) {
      if (containsErrorType(parameter.getType())) {
        return true;
      }
    }
    for (TypeParameterDescriptor parameter : function.getTypeParameters()) {
      for (JetType upperBound : parameter.getUpperBounds()) {
        if (containsErrorType(upperBound)) {
          return true;
        }
      }
    }

    return false;
  }
  public AccessorForFunctionDescriptor(
      @NotNull FunctionDescriptor descriptor,
      @NotNull DeclarationDescriptor containingDeclaration,
      int index) {
    super(
        containingDeclaration,
        Annotations.EMPTY,
        Name.identifier(
            (descriptor instanceof ConstructorDescriptor ? "$init" : descriptor.getName())
                + "$b$"
                + index),
        Kind.DECLARATION);

    initialize(
        DescriptorUtils.getReceiverParameterType(descriptor.getReceiverParameter()),
        descriptor instanceof ConstructorDescriptor
            ? NO_RECEIVER_PARAMETER
            : descriptor.getExpectedThisObject(),
        Collections.<TypeParameterDescriptor>emptyList(),
        copyValueParameters(descriptor),
        descriptor.getReturnType(),
        Modality.FINAL,
        Visibilities.INTERNAL);
  }
Ejemplo n.º 11
0
  @NotNull
  public static LookupElement createLookupElement(
      @NotNull KotlinCodeAnalyzer analyzer,
      @NotNull DeclarationDescriptor descriptor,
      @Nullable PsiElement declaration) {
    if (declaration != null) {
      MutableLookupElement javaLookupElement = createJavaLookupElementIfPossible(declaration);
      if (javaLookupElement != null) {
        InsertHandler<LookupElement> customHandler = getInsertHandler(descriptor);
        if (customHandler != null) {
          return javaLookupElement.setInsertHandler(getInsertHandler(descriptor));
        } else {
          return javaLookupElement;
        }
      }
    }

    LookupElementBuilder element =
        LookupElementBuilder.create(
            new JetLookupObject(descriptor, analyzer, declaration), descriptor.getName().getName());

    String presentableText = descriptor.getName().getName();
    String typeText = "";
    String tailText = "";
    boolean tailTextGrayed = true;

    if (descriptor instanceof FunctionDescriptor) {
      FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor;
      JetType returnType = functionDescriptor.getReturnType();
      typeText = DescriptorRenderer.TEXT.renderType(returnType);
      presentableText += DescriptorRenderer.TEXT.renderFunctionParameters(functionDescriptor);

      boolean extensionFunction = functionDescriptor.getReceiverParameter() != null;
      DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
      if (containingDeclaration != null && extensionFunction) {
        tailText +=
            " for "
                + DescriptorRenderer.TEXT.renderType(
                    functionDescriptor.getReceiverParameter().getType());
        tailText += " in " + DescriptorUtils.getFQName(containingDeclaration);
      }
    } else if (descriptor instanceof VariableDescriptor) {
      JetType outType = ((VariableDescriptor) descriptor).getType();
      typeText = DescriptorRenderer.TEXT.renderType(outType);
    } else if (descriptor instanceof ClassDescriptor) {
      DeclarationDescriptor declaredIn = descriptor.getContainingDeclaration();
      assert declaredIn != null;
      tailText = " (" + DescriptorUtils.getFQName(declaredIn) + ")";
      tailTextGrayed = true;
    } else {
      typeText = DescriptorRenderer.TEXT.render(descriptor);
    }

    element = element.withInsertHandler(getInsertHandler(descriptor));
    element =
        element
            .withTailText(tailText, tailTextGrayed)
            .withTypeText(typeText)
            .withPresentableText(presentableText);
    element =
        element.withIcon(
            JetDescriptorIconProvider.getIcon(descriptor, Iconable.ICON_FLAG_VISIBILITY));
    element = element.withStrikeoutness(KotlinBuiltIns.getInstance().isDeprecated(descriptor));
    return element;
  }