@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
 protected void checkFunction(
     JetNamedFunction function, SimpleFunctionDescriptor functionDescriptor) {
   reportErrorIfHasIllegalModifier(function);
   DeclarationDescriptor containingDescriptor = functionDescriptor.getContainingDeclaration();
   boolean hasAbstractModifier = function.hasModifier(JetTokens.ABSTRACT_KEYWORD);
   checkDeclaredTypeInPublicMember(function, functionDescriptor);
   if (containingDescriptor instanceof ClassDescriptor) {
     ClassDescriptor classDescriptor = (ClassDescriptor) containingDescriptor;
     boolean inTrait = classDescriptor.getKind() == ClassKind.INTERFACE;
     if (hasAbstractModifier && !classCanHaveAbstractMembers(classDescriptor)) {
       trace.report(
           ABSTRACT_FUNCTION_IN_NON_ABSTRACT_CLASS.on(
               function, functionDescriptor.getName().asString(), classDescriptor));
     }
     if (hasAbstractModifier && inTrait) {
       trace.report(ABSTRACT_MODIFIER_IN_TRAIT.on(function));
     }
     boolean hasBody = function.hasBody();
     if (hasBody && hasAbstractModifier) {
       trace.report(ABSTRACT_FUNCTION_WITH_BODY.on(function, functionDescriptor));
     }
     if (!hasBody && function.hasModifier(JetTokens.FINAL_KEYWORD) && inTrait) {
       trace.report(FINAL_FUNCTION_WITH_NO_BODY.on(function, functionDescriptor));
     }
     if (!hasBody && !hasAbstractModifier && !inTrait) {
       trace.report(NON_ABSTRACT_FUNCTION_WITH_NO_BODY.on(function, functionDescriptor));
     }
     return;
   }
   modifiersChecker.reportIllegalModalityModifiers(function);
   if (!function.hasBody() && !hasAbstractModifier) {
     trace.report(NON_MEMBER_FUNCTION_NO_BODY.on(function, functionDescriptor));
   }
 }
Esempio n. 4
0
 private static boolean isIllegalNestedClass(@NotNull DeclarationDescriptor descriptor) {
   if (!(descriptor instanceof ClassDescriptor)) return false;
   DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
   if (!(containingDeclaration instanceof ClassDescriptor)) return false;
   ClassDescriptor containingClass = (ClassDescriptor) containingDeclaration;
   return containingClass.isInner()
       || containingClass.getContainingDeclaration() instanceof FunctionDescriptor;
 }
Esempio n. 5
0
 @Nullable
 public static ReceiverParameterDescriptor getDispatchReceiverParameterIfNeeded(
     @NotNull DeclarationDescriptor containingDeclaration) {
   if (containingDeclaration instanceof ClassDescriptor) {
     ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration;
     return classDescriptor.getThisAsReceiverParameter();
   }
   return null;
 }
Esempio n. 6
0
 public static boolean isDirectSubclass(
     @NotNull ClassDescriptor subClass, @NotNull ClassDescriptor superClass) {
   for (KotlinType superType : subClass.getTypeConstructor().getSupertypes()) {
     if (isSameClass(superType, superClass.getOriginal())) {
       return true;
     }
   }
   return false;
 }
  @NotNull
  private static FunctionDescriptor standardFunction(
      ClassDescriptor classDescriptor, String name, Project project, KotlinType... parameterTypes) {
    ModuleDescriptorImpl emptyModule = KotlinTestUtils.createEmptyModule();
    ContainerForTests container = InjectionKt.createContainerForTests(project, emptyModule);
    emptyModule.setDependencies(emptyModule);
    emptyModule.initialize(PackageFragmentProvider.Empty.INSTANCE);

    LexicalScopeImpl lexicalScope =
        new LexicalScopeImpl(
            ImportingScope.Empty.INSTANCE,
            classDescriptor,
            false,
            classDescriptor.getThisAsReceiverParameter(),
            LexicalScopeKind.SYNTHETIC);

    ExpressionTypingContext context =
        ExpressionTypingContext.newContext(
            new BindingTraceContext(),
            lexicalScope,
            DataFlowInfoFactory.EMPTY,
            TypeUtils.NO_EXPECTED_TYPE);

    OverloadResolutionResults<FunctionDescriptor> functions =
        container
            .getFakeCallResolver()
            .resolveFakeCall(
                context,
                null,
                Name.identifier(name),
                null,
                null,
                FakeCallKind.OTHER,
                parameterTypes);

    for (ResolvedCall<? extends FunctionDescriptor> resolvedCall : functions.getResultingCalls()) {
      List<ValueParameterDescriptor> unsubstitutedValueParameters =
          resolvedCall.getResultingDescriptor().getValueParameters();
      for (int i = 0, unsubstitutedValueParametersSize = unsubstitutedValueParameters.size();
          i < unsubstitutedValueParametersSize;
          i++) {
        ValueParameterDescriptor unsubstitutedValueParameter = unsubstitutedValueParameters.get(i);
        if (unsubstitutedValueParameter.getType().equals(parameterTypes[i])) {
          return resolvedCall.getResultingDescriptor();
        }
      }
    }
    throw new IllegalArgumentException(
        "Not found: kotlin::"
            + classDescriptor.getName()
            + "."
            + name
            + "("
            + Arrays.toString(parameterTypes)
            + ")");
  }
Esempio n. 8
0
 @NotNull
 public static KotlinType getSuperClassType(@NotNull ClassDescriptor classDescriptor) {
   Collection<KotlinType> superclassTypes = classDescriptor.getTypeConstructor().getSupertypes();
   for (KotlinType type : superclassTypes) {
     ClassDescriptor superClassDescriptor = getClassDescriptorForType(type);
     if (superClassDescriptor.getKind() != ClassKind.INTERFACE) {
       return type;
     }
   }
   return getBuiltIns(classDescriptor).getAnyType();
 }
Esempio n. 9
0
 @Nullable
 public static ClassDescriptor getSuperClassDescriptor(@NotNull ClassDescriptor classDescriptor) {
   Collection<KotlinType> superclassTypes = classDescriptor.getTypeConstructor().getSupertypes();
   for (KotlinType type : superclassTypes) {
     ClassDescriptor superClassDescriptor = getClassDescriptorForType(type);
     if (superClassDescriptor.getKind() != ClassKind.INTERFACE) {
       return superClassDescriptor;
     }
   }
   return null;
 }
  private static void addResultsForClass(
      @NotNull @Mutable Set<LookupResult> results,
      @NotNull JetSimpleNameExpression selector,
      @NotNull LookupMode lookupMode,
      @NotNull ClassDescriptor descriptor) {
    JetScope scope =
        lookupMode == LookupMode.ONLY_CLASSES_AND_PACKAGES
            ? descriptor.getUnsubstitutedInnerClassesScope()
            : descriptor.getDefaultType().getMemberScope();
    results.add(lookupSimpleNameReference(selector, scope, lookupMode, false));

    results.add(lookupSimpleNameReference(selector, descriptor.getStaticScope(), lookupMode, true));
  }
 @NotNull
 @Override
 public String getText() {
   if (functionsToAdd.size() == 1) {
     FunctionDescriptor newFunction = functionsToAdd.get(0);
     ClassDescriptor supertype = (ClassDescriptor) newFunction.getContainingDeclaration();
     return JetBundle.message(
         "add.function.to.type.action.single",
         IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.render(newFunction),
         supertype.getName().toString());
   } else {
     return JetBundle.message("add.function.to.supertype.action.multiple");
   }
 }
 private static FunctionDescriptor generateFunctionSignatureForType(
     FunctionDescriptor functionDescriptor, ClassDescriptor typeDescriptor) {
   // TODO: support for generics.
   Modality modality = typeDescriptor.getModality();
   if (typeDescriptor.getKind() == ClassKind.INTERFACE) {
     modality = Modality.OPEN;
   }
   return functionDescriptor.copy(
       typeDescriptor,
       modality,
       functionDescriptor.getVisibility(),
       CallableMemberDescriptor.Kind.DECLARATION,
       /* copyOverrides = */ false);
 }
Esempio n. 13
0
 @NotNull
 public static FunctionDescriptor getErasedInvokeFunction(
     @NotNull FunctionDescriptor elementDescriptor) {
   int arity = elementDescriptor.getValueParameters().size();
   ClassDescriptor elementClass =
       elementDescriptor.getExtensionReceiverParameter() == null
           ? getBuiltIns(elementDescriptor).getFunction(arity)
           : getBuiltIns(elementDescriptor).getExtensionFunction(arity);
   return elementClass
       .getDefaultType()
       .getMemberScope()
       .getFunctions(OperatorConventions.INVOKE)
       .iterator()
       .next();
 }
Esempio n. 14
0
  @NotNull
  public JetType getFunctionType(
      @NotNull Annotations annotations,
      @Nullable JetType receiverType,
      @NotNull List<JetType> parameterTypes,
      @NotNull JetType returnType) {
    List<TypeProjection> arguments =
        getFunctionTypeArgumentProjections(receiverType, parameterTypes, returnType);
    int size = parameterTypes.size();
    ClassDescriptor classDescriptor =
        receiverType == null ? getFunction(size) : getExtensionFunction(size);
    TypeConstructor constructor = classDescriptor.getTypeConstructor();

    return new JetTypeImpl(
        annotations, constructor, false, arguments, classDescriptor.getMemberScope(arguments));
  }
 private static List<ClassDescriptor> getSupertypes(ClassDescriptor classDescriptor) {
   List<JetType> supertypes =
       Lists.newArrayList(TypeUtils.getAllSupertypes(classDescriptor.getDefaultType()));
   Collections.sort(
       supertypes,
       new Comparator<JetType>() {
         @Override
         public int compare(JetType o1, JetType o2) {
           if (o1.equals(o2)) {
             return 0;
           }
           if (JetTypeChecker.DEFAULT.isSubtypeOf(o1, o2)) {
             return -1;
           }
           if (JetTypeChecker.DEFAULT.isSubtypeOf(o2, o1)) {
             return 1;
           }
           return o1.toString().compareTo(o2.toString());
         }
       });
   List<ClassDescriptor> supertypesDescriptors = Lists.newArrayList();
   for (JetType supertype : supertypes) {
     supertypesDescriptors.add(DescriptorUtils.getClassDescriptorForType(supertype));
   }
   return supertypesDescriptors;
 }
Esempio n. 16
0
    private void reportCyclicInheritanceHierarchyError(
        @NotNull BindingTrace trace,
        @NotNull ClassDescriptor classDescriptor,
        @NotNull ClassDescriptor superclass) {
      PsiElement psiElement = DescriptorToSourceUtils.getSourceFromDescriptor(classDescriptor);

      PsiElement elementToMark = null;
      if (psiElement instanceof KtClassOrObject) {
        KtClassOrObject classOrObject = (KtClassOrObject) psiElement;
        for (KtSuperTypeListEntry delegationSpecifier : classOrObject.getSuperTypeListEntries()) {
          KtTypeReference typeReference = delegationSpecifier.getTypeReference();
          if (typeReference == null) continue;
          KotlinType supertype = trace.get(TYPE, typeReference);
          if (supertype != null && supertype.getConstructor() == superclass.getTypeConstructor()) {
            elementToMark = typeReference;
          }
        }
      }
      if (elementToMark == null && psiElement instanceof PsiNameIdentifierOwner) {
        PsiNameIdentifierOwner namedElement = (PsiNameIdentifierOwner) psiElement;
        PsiElement nameIdentifier = namedElement.getNameIdentifier();
        if (nameIdentifier != null) {
          elementToMark = nameIdentifier;
        }
      }
      if (elementToMark != null) {
        trace.report(CYCLIC_INHERITANCE_HIERARCHY.on(elementToMark));
      }
    }
Esempio n. 17
0
  @Override
  protected void generateBody() {
    List<KtObjectDeclaration> companions = new ArrayList<KtObjectDeclaration>();
    if (kind != OwnerKind.DEFAULT_IMPLS) {
      // generate nested classes first and only then generate class body. It necessary to access to
      // nested CodegenContexts
      for (KtDeclaration declaration : myClass.getDeclarations()) {
        if (shouldProcessFirst(declaration)) {
          // Generate companions after class body generation (need to record all synthetic
          // accessors)
          if (declaration instanceof KtObjectDeclaration
              && ((KtObjectDeclaration) declaration).isCompanion()) {
            companions.add((KtObjectDeclaration) declaration);
            CodegenUtilKt.populateCompanionBackingFieldNamesToOuterContextIfNeeded(
                (KtObjectDeclaration) declaration, context, state);
          } else {
            generateDeclaration(declaration);
          }
        }
      }
    }

    for (KtDeclaration declaration : myClass.getDeclarations()) {
      if (!shouldProcessFirst(declaration)) {
        generateDeclaration(declaration);
      }
    }

    generatePrimaryConstructorProperties();
    generateConstructors();
    generateDefaultImplsIfNeeded();

    for (KtObjectDeclaration companion : companions) {
      generateDeclaration(companion);
    }

    if (!DescriptorUtils.isInterface(descriptor)) {
      for (DeclarationDescriptor memberDescriptor :
          DescriptorUtils.getAllDescriptors(descriptor.getDefaultType().getMemberScope())) {
        if (memberDescriptor instanceof CallableMemberDescriptor) {
          CallableMemberDescriptor member = (CallableMemberDescriptor) memberDescriptor;
          if (!member.getKind().isReal() && ImplKt.findInterfaceImplementation(member) == null) {
            if (member instanceof FunctionDescriptor) {
              functionCodegen.generateBridges((FunctionDescriptor) member);
            } else if (member instanceof PropertyDescriptor) {
              PropertyGetterDescriptor getter = ((PropertyDescriptor) member).getGetter();
              if (getter != null) {
                functionCodegen.generateBridges(getter);
              }
              PropertySetterDescriptor setter = ((PropertyDescriptor) member).getSetter();
              if (setter != null) {
                functionCodegen.generateBridges(setter);
              }
            }
          }
        }
      }
    }
  }
  private static List<FunctionDescriptor> generateFunctionsToAdd(JetNamedFunction functionElement) {
    FunctionDescriptor functionDescriptor =
        (FunctionDescriptor) ResolvePackage.resolveToDescriptor(functionElement);

    DeclarationDescriptor containingDeclaration = functionDescriptor.getContainingDeclaration();
    if (!(containingDeclaration instanceof ClassDescriptor)) return Collections.emptyList();

    List<FunctionDescriptor> functions = Lists.newArrayList();
    ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration;
    // TODO: filter out impossible supertypes (for example when argument's type isn't visible in a
    // superclass).
    for (ClassDescriptor supertypeDescriptor : getSupertypes(classDescriptor)) {
      if (KotlinBuiltIns.isAnyOrNullableAny(supertypeDescriptor.getDefaultType())) continue;
      functions.add(generateFunctionSignatureForType(functionDescriptor, supertypeDescriptor));
    }
    return functions;
  }
Esempio n. 19
0
 @Nullable
 private static ClassDescriptor getEnumEntry(
     @NotNull ClassDescriptor enumDescriptor, @NotNull String entryName) {
   ClassifierDescriptor result =
       enumDescriptor
           .getUnsubstitutedInnerClassesScope()
           .getContributedClassifier(Name.identifier(entryName), NoLookupLocation.FROM_BUILTINS);
   return result instanceof ClassDescriptor ? (ClassDescriptor) result : null;
 }
Esempio n. 20
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. 21
0
  private void checkEnumEntry(
      @NotNull JetEnumEntry enumEntry, @NotNull ClassDescriptor classDescriptor) {
    DeclarationDescriptor declaration = classDescriptor.getContainingDeclaration();
    assert DescriptorUtils.isEnumClass(declaration)
        : "Enum entry should be declared in enum class: " + classDescriptor;
    ClassDescriptor enumClass = (ClassDescriptor) declaration;

    if (enumEntryUsesDeprecatedSuperConstructor(enumEntry)) {
      trace.report(
          Errors.ENUM_ENTRY_USES_DEPRECATED_SUPER_CONSTRUCTOR.on(enumEntry, classDescriptor));
    }
    String neededDelimiter = enumEntryExpectedDelimiter(enumEntry);
    if (!neededDelimiter.isEmpty()) {
      trace.report(
          Errors.ENUM_ENTRY_USES_DEPRECATED_OR_NO_DELIMITER.on(
              enumEntry, classDescriptor, neededDelimiter));
    }
    if (enumEntryAfterEnumMember(enumEntry)) {
      trace.report(Errors.ENUM_ENTRY_AFTER_ENUM_MEMBER.on(enumEntry, classDescriptor));
    }

    List<JetDelegationSpecifier> delegationSpecifiers = enumEntry.getDelegationSpecifiers();
    ConstructorDescriptor constructor = enumClass.getUnsubstitutedPrimaryConstructor();
    if ((constructor == null || !constructor.getValueParameters().isEmpty())
        && delegationSpecifiers.isEmpty()) {
      trace.report(ENUM_ENTRY_SHOULD_BE_INITIALIZED.on(enumEntry, enumClass));
    }

    for (JetDelegationSpecifier delegationSpecifier : delegationSpecifiers) {
      JetTypeReference typeReference = delegationSpecifier.getTypeReference();
      if (typeReference != null) {
        JetType type = trace.getBindingContext().get(TYPE, typeReference);
        if (type != null) {
          JetType enumType = enumClass.getDefaultType();
          if (!type.getConstructor().equals(enumType.getConstructor())) {
            trace.report(ENUM_ENTRY_ILLEGAL_TYPE.on(typeReference, enumClass));
          }
        }
      }
    }
  }
Esempio n. 22
0
  @NotNull
  private static InnerModifierCheckResult checkIllegalInner(
      @NotNull DeclarationDescriptor descriptor) {
    if (!(descriptor instanceof ClassDescriptor)) return InnerModifierCheckResult.ILLEGAL_POSITION;
    ClassDescriptor classDescriptor = (ClassDescriptor) descriptor;

    if (classDescriptor.getKind() != ClassKind.CLASS)
      return InnerModifierCheckResult.ILLEGAL_POSITION;

    DeclarationDescriptor containingDeclaration = classDescriptor.getContainingDeclaration();
    if (!(containingDeclaration instanceof ClassDescriptor))
      return InnerModifierCheckResult.ILLEGAL_POSITION;

    if (DescriptorUtils.isTrait(containingDeclaration)) {
      return InnerModifierCheckResult.IN_TRAIT;
    } else if (DescriptorUtils.isObject(containingDeclaration)) {
      return InnerModifierCheckResult.IN_OBJECT;
    } else {
      return InnerModifierCheckResult.ALLOWED;
    }
  }
Esempio n. 23
0
  @NotNull
  public static KotlinType substituteProjectionsForParameters(
      @NotNull ClassDescriptor clazz, @NotNull List<TypeProjection> projections) {
    List<TypeParameterDescriptor> clazzTypeParameters = clazz.getTypeConstructor().getParameters();
    if (clazzTypeParameters.size() != projections.size()) {
      throw new IllegalArgumentException(
          "type parameter counts do not match: " + clazz + ", " + projections);
    }

    Map<TypeConstructor, TypeProjection> substitutions =
        org.jetbrains.kotlin.utils.CollectionsKt.newHashMapWithExpectedSize(
            clazzTypeParameters.size());

    for (int i = 0; i < clazzTypeParameters.size(); ++i) {
      TypeConstructor typeConstructor = clazzTypeParameters.get(i).getTypeConstructor();
      substitutions.put(typeConstructor, projections.get(i));
    }

    return TypeSubstitutor.create(substitutions)
        .substitute(clazz.getDefaultType(), Variance.INVARIANT);
  }
Esempio n. 24
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()]);
  }
Esempio n. 25
0
  @Nullable
  public static ValueParameterDescriptor getAnnotationParameterByName(
      @NotNull Name name, @NotNull ClassDescriptor annotationClass) {
    Collection<ConstructorDescriptor> constructors = annotationClass.getConstructors();

    for (ValueParameterDescriptor parameter : constructors.iterator().next().getValueParameters()) {
      if (parameter.getName().equals(name)) {
        return parameter;
      }
    }

    return null;
  }
Esempio n. 26
0
 @NotNull
 public static List<ClassDescriptor> getSuperclassDescriptors(
     @NotNull ClassDescriptor classDescriptor) {
   Collection<KotlinType> superclassTypes = classDescriptor.getTypeConstructor().getSupertypes();
   List<ClassDescriptor> superClassDescriptors = new ArrayList<ClassDescriptor>();
   for (KotlinType type : superclassTypes) {
     ClassDescriptor result = getClassDescriptorForType(type);
     if (!isAny(result)) {
       superClassDescriptors.add(result);
     }
   }
   return superClassDescriptors;
 }
  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);
      }
    }
  }
Esempio n. 28
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. 29
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. 30
0
  @NotNull
  public static SimpleFunctionDescriptor createEnumValuesMethod(
      @NotNull ClassDescriptor enumClass) {
    AnnotationsImpl annotations =
        AnnotationsImpl.createWithNoTarget(
            AnnotationUtilKt.createDeprecatedAnnotation(
                getBuiltIns(enumClass), "Use 'values' property instead", "this.values"));

    SimpleFunctionDescriptorImpl values =
        SimpleFunctionDescriptorImpl.create(
            enumClass,
            annotations,
            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);
  }