@NotNull
 public static SimpleFunctionDescriptor createEnumValueOfMethod(
     @NotNull ClassDescriptor enumClass) {
   SimpleFunctionDescriptorImpl valueOf =
       SimpleFunctionDescriptorImpl.create(
           enumClass,
           Annotations.Companion.getEMPTY(),
           DescriptorUtils.ENUM_VALUE_OF,
           CallableMemberDescriptor.Kind.SYNTHESIZED,
           enumClass.getSource());
   ValueParameterDescriptor parameterDescriptor =
       new ValueParameterDescriptorImpl(
           valueOf,
           null,
           0,
           Annotations.Companion.getEMPTY(),
           Name.identifier("value"),
           getBuiltIns(enumClass).getStringType(),
           /* declaresDefaultValue = */ false,
           /* isCrossinline = */ false,
           /* isNoinline = */ false,
           null,
           enumClass.getSource());
   return valueOf.initialize(
       null,
       null,
       Collections.<TypeParameterDescriptor>emptyList(),
       Collections.singletonList(parameterDescriptor),
       enumClass.getDefaultType(),
       Modality.FINAL,
       Visibilities.PUBLIC);
 }
  @NotNull
  public static PropertyDescriptor createEnumValuesProperty(@NotNull ClassDescriptor enumClass) {
    PropertyDescriptorImpl values =
        PropertyDescriptorImpl.create(
            enumClass,
            Annotations.Companion.getEMPTY(),
            Modality.FINAL,
            Visibilities.PUBLIC, /* isVar */
            false,
            DescriptorUtils.ENUM_VALUES,
            CallableMemberDescriptor.Kind.SYNTHESIZED,
            enumClass.getSource(),
            /* lateInit = */ false,
            /* isConst = */ false);

    KotlinType type =
        getBuiltIns(enumClass).getArrayType(Variance.INVARIANT, enumClass.getDefaultType());

    PropertyGetterDescriptorImpl getter =
        createDefaultGetter(values, Annotations.Companion.getEMPTY());

    values.initialize(getter, null);
    getter.initialize(type);
    values.setType(type, Collections.<TypeParameterDescriptor>emptyList(), null, (KotlinType) null);

    return values;
  }
Esempio n. 3
0
 @NotNull
 public static List<ValueParameterDescriptor> getValueParameters(
     @NotNull FunctionDescriptor functionDescriptor, @NotNull KotlinType type) {
   assert isFunctionOrExtensionFunctionType(type);
   List<TypeProjection> parameterTypes = getParameterTypeProjectionsFromFunctionType(type);
   List<ValueParameterDescriptor> valueParameters =
       new ArrayList<ValueParameterDescriptor>(parameterTypes.size());
   for (int i = 0; i < parameterTypes.size(); i++) {
     TypeProjection parameterType = parameterTypes.get(i);
     ValueParameterDescriptorImpl valueParameterDescriptor =
         new ValueParameterDescriptorImpl(
             functionDescriptor,
             null,
             i,
             Annotations.Companion.getEMPTY(),
             Name.identifier("p" + (i + 1)),
             parameterType.getType(),
             /* declaresDefaultValue = */ false,
             /* isCrossinline = */ false,
             /* isNoinline = */ false,
             null,
             SourceElement.NO_SOURCE);
     valueParameters.add(valueParameterDescriptor);
   }
   return valueParameters;
 }
Esempio n. 4
0
 @NotNull
 public KotlinType getEnumType(@NotNull KotlinType argument) {
   Variance projectionType = Variance.INVARIANT;
   List<TypeProjectionImpl> types =
       Collections.singletonList(new TypeProjectionImpl(projectionType, argument));
   return KotlinTypeImpl.create(Annotations.Companion.getEMPTY(), getEnum(), false, types);
 }
Esempio n. 5
0
 @Nullable
 public static AnnotationDescriptor getAnnotationByFqName(
     @NotNull Annotations annotations, @NotNull FqName name) {
   AnnotationWithTarget annotationWithTarget =
       Annotations.Companion.findAnyAnnotation(annotations, name);
   return annotationWithTarget == null ? null : annotationWithTarget.getAnnotation();
 }
Esempio n. 6
0
 public DefaultConstructorDescriptor(
     @NotNull ClassDescriptor containingClass, @NotNull SourceElement source) {
   super(
       containingClass, null, Annotations.Companion.getEMPTY(), true, Kind.DECLARATION, source);
   initialize(
       Collections.<ValueParameterDescriptor>emptyList(),
       getDefaultConstructorVisibility(containingClass));
 }
Esempio n. 7
0
 private TypeParameterDescriptor createTypeVariable(String name) {
   return TypeParameterDescriptorImpl.createWithDefaultBound(
       builtIns.getBuiltInsModule(),
       Annotations.Companion.getEMPTY(),
       false,
       Variance.INVARIANT,
       Name.identifier(name),
       0);
 }
Esempio n. 8
0
  @NotNull
  public Collection<KotlinType> getSupertypesForClosure(@NotNull FunctionDescriptor descriptor) {
    ReceiverParameterDescriptor receiverParameter = descriptor.getExtensionReceiverParameter();

    //noinspection ConstantConditions
    KotlinType functionType =
        DescriptorUtilsKt.getBuiltIns(descriptor)
            .getFunctionType(
                Annotations.Companion.getEMPTY(),
                receiverParameter == null ? null : receiverParameter.getType(),
                ExpressionTypingUtils.getValueParametersTypes(descriptor.getValueParameters()),
                descriptor.getReturnType());

    return Arrays.asList(lambda.getDefaultType(), functionType);
  }
Esempio n. 9
0
 @NotNull
 public static KotlinType makeUnsubstitutedType(
     ClassDescriptor classDescriptor, MemberScope unsubstitutedMemberScope) {
   if (ErrorUtils.isError(classDescriptor)) {
     return ErrorUtils.createErrorType("Unsubstituted type for " + classDescriptor);
   }
   TypeConstructor typeConstructor = classDescriptor.getTypeConstructor();
   List<TypeProjection> arguments = getDefaultTypeProjections(typeConstructor.getParameters());
   return KotlinTypeImpl.create(
       Annotations.Companion.getEMPTY(),
       typeConstructor,
       false,
       arguments,
       unsubstitutedMemberScope);
 }
Esempio n. 10
0
  private static boolean containsAnnotation(
      DeclarationDescriptor descriptor, FqName annotationClassFqName) {
    DeclarationDescriptor original = descriptor.getOriginal();
    Annotations annotations = original.getAnnotations();

    if (annotations.findAnnotation(annotationClassFqName) != null) return true;

    AnnotationUseSiteTarget associatedUseSiteTarget =
        AnnotationUseSiteTarget.Companion.getAssociatedUseSiteTarget(descriptor);
    if (associatedUseSiteTarget != null) {
      if (Annotations.Companion.findUseSiteTargetedAnnotation(
              annotations, associatedUseSiteTarget, annotationClassFqName)
          != null) {
        return true;
      }
    }

    return false;
  }
Esempio n. 11
0
 @NotNull
 public static SimpleFunctionDescriptor createEnumValuesMethod(
     @NotNull ClassDescriptor enumClass) {
   SimpleFunctionDescriptorImpl values =
       SimpleFunctionDescriptorImpl.create(
           enumClass,
           Annotations.Companion.getEMPTY(),
           DescriptorUtils.ENUM_VALUES,
           CallableMemberDescriptor.Kind.SYNTHESIZED,
           enumClass.getSource());
   return values.initialize(
       null,
       null,
       Collections.<TypeParameterDescriptor>emptyList(),
       Collections.<ValueParameterDescriptor>emptyList(),
       getBuiltIns(enumClass).getArrayType(Variance.INVARIANT, enumClass.getDefaultType()),
       Modality.FINAL,
       Visibilities.PUBLIC);
 }
Esempio n. 12
0
  @NotNull
  public Collection<KotlinType> getSupertypesForFunctionReference(
      @NotNull FunctionDescriptor descriptor) {
    ReceiverParameterDescriptor extensionReceiver = descriptor.getExtensionReceiverParameter();
    ReceiverParameterDescriptor dispatchReceiver = descriptor.getDispatchReceiverParameter();

    KotlinType receiverType =
        extensionReceiver != null
            ? extensionReceiver.getType()
            : dispatchReceiver != null ? dispatchReceiver.getType() : null;

    //noinspection ConstantConditions
    KotlinType functionType =
        DescriptorUtilsKt.getBuiltIns(descriptor)
            .getFunctionType(
                Annotations.Companion.getEMPTY(),
                receiverType,
                ExpressionTypingUtils.getValueParametersTypes(descriptor.getValueParameters()),
                descriptor.getReturnType());

    return Arrays.asList(functionReference.getDefaultType(), functionType);
  }
Esempio n. 13
0
  @NotNull
  private DeclarationDescriptor createInitializerScopeParent() {
    ConstructorDescriptor primaryConstructor = getUnsubstitutedPrimaryConstructor();
    if (primaryConstructor != null) return primaryConstructor;

    return new FunctionDescriptorImpl(
        LazyClassDescriptor.this,
        null,
        Annotations.Companion.getEMPTY(),
        Name.special("<init-blocks>"),
        CallableMemberDescriptor.Kind.SYNTHESIZED,
        SourceElement.NO_SOURCE) {
      {
        initialize(
            null,
            null,
            Collections.<TypeParameterDescriptor>emptyList(),
            Collections.<ValueParameterDescriptor>emptyList(),
            null,
            Modality.FINAL,
            Visibilities.PRIVATE);
      }

      @NotNull
      @Override
      protected FunctionDescriptorImpl createSubstitutedCopy(
          @NotNull DeclarationDescriptor newOwner,
          @Nullable FunctionDescriptor original,
          @NotNull Kind kind,
          @Nullable Name newName,
          @NotNull Annotations annotations,
          @NotNull SourceElement source) {
        throw new UnsupportedOperationException();
      }
    };
  }
Esempio n. 14
0
 @NotNull
 @Override
 public Annotations getAnnotations() {
   return Annotations.Companion.getEMPTY(); // TODO
 }
Esempio n. 15
0
  public LazyClassDescriptor(
      @NotNull final LazyClassContext c,
      @NotNull DeclarationDescriptor containingDeclaration,
      @NotNull Name name,
      @NotNull final KtClassLikeInfo classLikeInfo) {
    super(
        c.getStorageManager(),
        containingDeclaration,
        name,
        KotlinSourceElementKt.toSourceElement(classLikeInfo.getCorrespondingClassOrObject()));
    this.c = c;

    KtClassOrObject classOrObject = classLikeInfo.getCorrespondingClassOrObject();
    if (classOrObject != null) {
      this.c.getTrace().record(BindingContext.CLASS, classOrObject, this);
    }
    this.c
        .getTrace()
        .record(BindingContext.FQNAME_TO_CLASS_DESCRIPTOR, DescriptorUtils.getFqName(this), this);

    this.declarationProvider =
        c.getDeclarationProviderFactory().getClassMemberDeclarationProvider(classLikeInfo);

    StorageManager storageManager = c.getStorageManager();

    this.unsubstitutedMemberScope = createMemberScope(c, this.declarationProvider);
    this.kind = classLikeInfo.getClassKind();
    this.staticScope =
        kind == ClassKind.ENUM_CLASS
            ? new StaticScopeForKotlinEnum(storageManager, this)
            : MemberScope.Empty.INSTANCE;

    this.typeConstructor = new LazyClassTypeConstructor();

    this.isCompanionObject =
        classLikeInfo instanceof KtObjectInfo && ((KtObjectInfo) classLikeInfo).isCompanionObject();

    KtModifierList modifierList = classLikeInfo.getModifierList();
    if (kind.isSingleton()) {
      this.modality = Modality.FINAL;
    } else {
      Modality defaultModality = kind == ClassKind.INTERFACE ? Modality.ABSTRACT : Modality.FINAL;
      this.modality =
          resolveModalityFromModifiers(modifierList, defaultModality, /* allowSealed = */ true);
    }

    boolean isLocal = classOrObject != null && KtPsiUtil.isLocal(classOrObject);
    Visibility defaultVisibility;
    if (kind == ClassKind.ENUM_ENTRY || (kind == ClassKind.OBJECT && isCompanionObject)) {
      defaultVisibility = Visibilities.PUBLIC;
    } else {
      defaultVisibility = Visibilities.DEFAULT_VISIBILITY;
    }
    this.visibility =
        isLocal
            ? Visibilities.LOCAL
            : resolveVisibilityFromModifiers(modifierList, defaultVisibility);

    this.isInner = isInnerClass(modifierList) && !ModifiersChecker.isIllegalInner(this);
    this.isData = modifierList != null && modifierList.hasModifier(KtTokens.DATA_KEYWORD);

    // Annotation entries are taken from both own annotations (if any) and object literal
    // annotations (if any)
    List<KtAnnotationEntry> annotationEntries = new ArrayList<KtAnnotationEntry>();
    if (classOrObject != null && classOrObject.getParent() instanceof KtObjectLiteralExpression) {
      // TODO: it would be better to have separate ObjectLiteralDescriptor without so much magic
      annotationEntries.addAll(
          KtPsiUtilKt.getAnnotationEntries((KtObjectLiteralExpression) classOrObject.getParent()));
    }
    if (modifierList != null) {
      annotationEntries.addAll(modifierList.getAnnotationEntries());
    }
    if (!annotationEntries.isEmpty()) {
      this.annotations =
          new LazyAnnotations(
              new LazyAnnotationsContext(c.getAnnotationResolver(), storageManager, c.getTrace()) {
                @NotNull
                @Override
                public LexicalScope getScope() {
                  return getOuterScope();
                }
              },
              annotationEntries);
    } else {
      this.annotations = Annotations.Companion.getEMPTY();
    }

    List<KtAnnotationEntry> jetDanglingAnnotations = classLikeInfo.getDanglingAnnotations();
    if (jetDanglingAnnotations.isEmpty()) {
      this.danglingAnnotations = Annotations.Companion.getEMPTY();
    } else {
      this.danglingAnnotations =
          new LazyAnnotations(
              new LazyAnnotationsContext(c.getAnnotationResolver(), storageManager, c.getTrace()) {
                @NotNull
                @Override
                public LexicalScope getScope() {
                  return getScopeForMemberDeclarationResolution();
                }
              },
              jetDanglingAnnotations);
    }

    this.companionObjectDescriptor =
        storageManager.createNullableLazyValue(
            new Function0<LazyClassDescriptor>() {
              @Override
              public LazyClassDescriptor invoke() {
                return computeCompanionObjectDescriptor(getCompanionObjectIfAllowed());
              }
            });
    this.extraCompanionObjectDescriptors =
        storageManager.createMemoizedFunction(
            new Function1<KtObjectDeclaration, ClassDescriptor>() {
              @Override
              public ClassDescriptor invoke(KtObjectDeclaration companionObject) {
                return computeCompanionObjectDescriptor(companionObject);
              }
            });
    this.forceResolveAllContents =
        storageManager.createRecursionTolerantNullableLazyValue(
            new Function0<Void>() {
              @Override
              public Void invoke() {
                doForceResolveAllContents();
                return null;
              }
            },
            null);

    this.resolutionScopesSupport =
        new ClassResolutionScopesSupport(
            this,
            storageManager,
            new Function0<LexicalScope>() {
              @Override
              public LexicalScope invoke() {
                return getOuterScope();
              }
            });

    this.parameters =
        c.getStorageManager()
            .createLazyValue(
                new Function0<List<TypeParameterDescriptor>>() {
                  @Override
                  public List<TypeParameterDescriptor> invoke() {
                    KtClassLikeInfo classInfo = declarationProvider.getOwnerInfo();
                    KtTypeParameterList typeParameterList = classInfo.getTypeParameterList();
                    if (typeParameterList == null) return Collections.emptyList();

                    if (classInfo.getClassKind() == ClassKind.ENUM_CLASS) {
                      c.getTrace().report(TYPE_PARAMETERS_IN_ENUM.on(typeParameterList));
                    }

                    List<KtTypeParameter> typeParameters = typeParameterList.getParameters();
                    if (typeParameters.isEmpty()) return Collections.emptyList();

                    List<TypeParameterDescriptor> parameters =
                        new ArrayList<TypeParameterDescriptor>(typeParameters.size());

                    for (int i = 0; i < typeParameters.size(); i++) {
                      parameters.add(
                          new LazyTypeParameterDescriptor(
                              c, LazyClassDescriptor.this, typeParameters.get(i), i));
                    }

                    return parameters;
                  }
                });

    this.scopeForInitializerResolution =
        storageManager.createLazyValue(
            new Function0<LexicalScope>() {
              @Override
              public LexicalScope invoke() {
                return ClassResolutionScopesSupportKt.scopeForInitializerResolution(
                    LazyClassDescriptor.this,
                    createInitializerScopeParent(),
                    classLikeInfo.getPrimaryConstructorParameters());
              }
            });
  }