Ejemplo n.º 1
0
 @NotNull
 private static ClassKind getClassKind(@NotNull JetClass jetClass) {
   if (jetClass.isTrait()) return ClassKind.TRAIT;
   if (jetClass.isAnnotation()) return ClassKind.ANNOTATION_CLASS;
   if (jetClass.isEnum()) return ClassKind.ENUM_CLASS;
   return ClassKind.CLASS;
 }
Ejemplo n.º 2
0
    @NotNull
    private MutableClassDescriptor createClassDescriptorForClass(
        @NotNull JetClass klass, @NotNull DeclarationDescriptor containingDeclaration) {
      ClassKind kind = getClassKind(klass);
      // Kind check is needed in order to not consider enums as inner in any case
      // (otherwise it would be impossible to create a class object in the enum)
      boolean isInner = kind == ClassKind.CLASS && klass.isInner();
      MutableClassDescriptor mutableClassDescriptor =
          new MutableClassDescriptor(
              containingDeclaration,
              outerScope,
              kind,
              isInner,
              JetPsiUtil.safeName(klass.getName()));
      c.getClasses().put(klass, mutableClassDescriptor);
      trace.record(
          FQNAME_TO_CLASS_DESCRIPTOR, JetPsiUtil.getUnsafeFQName(klass), mutableClassDescriptor);

      createClassObjectForEnumClass(mutableClassDescriptor);

      JetScope classScope = mutableClassDescriptor.getScopeForMemberDeclarationResolution();

      prepareForDeferredCall(classScope, mutableClassDescriptor, klass);

      return mutableClassDescriptor;
    }
Ejemplo n.º 3
0
  private void resolvePropertyDeclarationBodies(@NotNull BodiesResolveContext c) {

    // Member properties
    Set<JetProperty> processed = Sets.newHashSet();
    for (Map.Entry<JetClassOrObject, ClassDescriptorWithResolutionScopes> entry :
        c.getDeclaredClasses().entrySet()) {
      if (!(entry.getKey() instanceof JetClass)) continue;
      JetClass jetClass = (JetClass) entry.getKey();
      ClassDescriptorWithResolutionScopes classDescriptor = entry.getValue();

      for (JetProperty property : jetClass.getProperties()) {
        PropertyDescriptor propertyDescriptor = c.getProperties().get(property);
        assert propertyDescriptor != null;

        resolveProperty(
            c,
            classDescriptor.getScopeForMemberDeclarationResolution(),
            property,
            propertyDescriptor);
        processed.add(property);
      }
    }

    // Top-level properties & properties of objects
    for (Map.Entry<JetProperty, PropertyDescriptor> entry : c.getProperties().entrySet()) {
      JetProperty property = entry.getKey();
      if (processed.contains(property)) continue;

      PropertyDescriptor propertyDescriptor = entry.getValue();

      resolveProperty(c, null, property, propertyDescriptor);
    }
  }
Ejemplo n.º 4
0
 private void checkSealedModifiers(JetClass aClass) {
   if (aClass.hasModifier(JetTokens.OPEN_KEYWORD)) {
     trace.report(OPEN_MODIFIER_IN_SEALED.on(aClass));
   }
   if (aClass.hasModifier(JetTokens.FINAL_KEYWORD)) {
     trace.report(FINAL_MODIFIER_IN_SEALED.on(aClass));
   }
   if (aClass.hasModifier(JetTokens.ABSTRACT_KEYWORD)) {
     trace.report(ABSTRACT_MODIFIER_IN_SEALED.on(aClass));
   }
 }
Ejemplo n.º 5
0
 private void checkEnumModifiers(JetClass aClass) {
   if (aClass.hasModifier(JetTokens.OPEN_KEYWORD)) {
     trace.report(OPEN_MODIFIER_IN_ENUM.on(aClass));
   }
   if (aClass.hasModifier(JetTokens.ABSTRACT_KEYWORD)) {
     trace.report(ABSTRACT_MODIFIER_IN_ENUM.on(aClass));
   }
   if (aClass.hasModifier(JetTokens.SEALED_KEYWORD)) {
     trace.report(SEALED_MODIFIER_IN_ENUM.on(aClass));
   }
 }
Ejemplo n.º 6
0
 private void checkNestedClassAllowed(
     @NotNull JetModifierListOwner modifierListOwner,
     @NotNull DeclarationDescriptor descriptor) {
   if (modifierListOwner.hasModifier(INNER_KEYWORD)) return;
   if (modifierListOwner instanceof JetClass && !(modifierListOwner instanceof JetEnumEntry)) {
     JetClass aClass = (JetClass) modifierListOwner;
     boolean localEnumError = aClass.isLocal() && aClass.isEnum();
     if (!localEnumError && isIllegalNestedClass(descriptor)) {
       trace.report(NESTED_CLASS_NOT_ALLOWED.on(aClass));
     }
   }
 }
Ejemplo n.º 7
0
  private void resolvePropertyDeclarationBodies() {

    // Member properties
    Set<JetProperty> processed = Sets.newHashSet();
    for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) {
      JetClass jetClass = entry.getKey();
      if (!context.completeAnalysisNeeded(jetClass)) continue;
      MutableClassDescriptor classDescriptor = entry.getValue();

      for (JetProperty property : jetClass.getProperties()) {
        final PropertyDescriptor propertyDescriptor = this.context.getProperties().get(property);
        assert propertyDescriptor != null;

        computeDeferredType(propertyDescriptor.getReturnType());

        JetExpression initializer = property.getInitializer();
        if (initializer != null) {
          ConstructorDescriptor primaryConstructor =
              classDescriptor.getUnsubstitutedPrimaryConstructor();
          if (primaryConstructor != null) {
            JetScope declaringScopeForPropertyInitializer =
                this.context.getDeclaringScopes().get(property);
            resolvePropertyInitializer(
                property, propertyDescriptor, initializer, declaringScopeForPropertyInitializer);
          }
        }

        resolvePropertyAccessors(property, propertyDescriptor);
        processed.add(property);
      }
    }

    // Top-level properties & properties of objects
    for (Map.Entry<JetProperty, PropertyDescriptor> entry :
        this.context.getProperties().entrySet()) {
      JetProperty property = entry.getKey();
      if (!context.completeAnalysisNeeded(property)) continue;
      if (processed.contains(property)) continue;

      final PropertyDescriptor propertyDescriptor = entry.getValue();

      computeDeferredType(propertyDescriptor.getReturnType());

      JetScope declaringScope = this.context.getDeclaringScopes().get(property);

      JetExpression initializer = property.getInitializer();
      if (initializer != null) {
        resolvePropertyInitializer(property, propertyDescriptor, initializer, declaringScope);
      }

      resolvePropertyAccessors(property, propertyDescriptor);
    }
  }
Ejemplo n.º 8
0
 private void resolvePrimaryConstructorParameters() {
   for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) {
     JetClass klass = entry.getKey();
     MutableClassDescriptor classDescriptor = entry.getValue();
     ConstructorDescriptor unsubstitutedPrimaryConstructor =
         classDescriptor.getUnsubstitutedPrimaryConstructor();
     if (unsubstitutedPrimaryConstructor != null) {
       checkDefaultParameterValues(
           klass.getPrimaryConstructorParameters(),
           unsubstitutedPrimaryConstructor.getValueParameters(),
           classDescriptor.getScopeForInitializers());
     }
   }
 }
Ejemplo n.º 9
0
 private void checkValOnAnnotationParameter(JetClass aClass) {
   for (JetParameter parameter : aClass.getPrimaryConstructorParameters()) {
     if (!parameter.hasValOrVar()) {
       trace.report(MISSING_VAL_ON_ANNOTATION_PARAMETER.on(parameter));
     }
   }
 }
Ejemplo n.º 10
0
  private void processPrimaryConstructor(
      @NotNull TopDownAnalysisContext c,
      @NotNull MutableClassDescriptor classDescriptor,
      @NotNull JetClass klass) {
    // TODO : not all the parameters are real properties
    JetScope memberScope = classDescriptor.getScopeForClassHeaderResolution();
    ConstructorDescriptor constructorDescriptor =
        descriptorResolver.resolvePrimaryConstructorDescriptor(
            memberScope, classDescriptor, klass, trace);
    if (constructorDescriptor != null) {
      List<ValueParameterDescriptor> valueParameterDescriptors =
          constructorDescriptor.getValueParameters();
      List<JetParameter> primaryConstructorParameters = klass.getPrimaryConstructorParameters();
      assert valueParameterDescriptors.size() == primaryConstructorParameters.size();
      List<ValueParameterDescriptor> notProperties = new ArrayList<ValueParameterDescriptor>();
      for (ValueParameterDescriptor valueParameterDescriptor : valueParameterDescriptors) {
        JetParameter parameter =
            primaryConstructorParameters.get(valueParameterDescriptor.getIndex());
        if (parameter.getValOrVarNode() != null) {
          PropertyDescriptor propertyDescriptor =
              descriptorResolver.resolvePrimaryConstructorParameterToAProperty(
                  classDescriptor, valueParameterDescriptor, memberScope, parameter, trace);
          classDescriptor.getBuilder().addPropertyDescriptor(propertyDescriptor);
          c.getPrimaryConstructorParameterProperties().put(parameter, propertyDescriptor);
        } else {
          notProperties.add(valueParameterDescriptor);
        }
      }

      if (classDescriptor.getKind() != ClassKind.TRAIT) {
        classDescriptor.setPrimaryConstructor(constructorDescriptor);
        classDescriptor.addConstructorParametersToInitializersScope(notProperties);
      }
    }
  }
Ejemplo n.º 11
0
  private void checkTypesInClassHeader(@NotNull JetClassOrObject classOrObject) {
    for (JetDelegationSpecifier delegationSpecifier : classOrObject.getDelegationSpecifiers()) {
      checkBoundsForTypeInClassHeader(delegationSpecifier.getTypeReference());
    }

    if (!(classOrObject instanceof JetClass)) return;
    JetClass jetClass = (JetClass) classOrObject;

    for (JetTypeParameter jetTypeParameter : jetClass.getTypeParameters()) {
      checkBoundsForTypeInClassHeader(jetTypeParameter.getExtendsBound());
      checkFinalUpperBounds(jetTypeParameter.getExtendsBound());
    }

    for (JetTypeConstraint constraint : jetClass.getTypeConstraints()) {
      checkBoundsForTypeInClassHeader(constraint.getBoundTypeReference());
      checkFinalUpperBounds(constraint.getBoundTypeReference());
    }
  }
Ejemplo n.º 12
0
  private void checkClass(
      BodiesResolveContext c,
      JetClass aClass,
      ClassDescriptorWithResolutionScopes classDescriptor) {
    checkOpenMembers(classDescriptor);
    checkTypeParameters(aClass);

    if (aClass.isInterface()) {
      ASTNode traitKeyword = aClass.getNode().findChildByType(JetTokens.TRAIT_KEYWORD);
      if (traitKeyword != null) {
        trace.report(Errors.DEPRECATED_TRAIT_KEYWORD.on(traitKeyword.getPsi()));
      }

      checkTraitModifiers(aClass);
      checkConstructorInTrait(aClass);
    } else if (aClass.isAnnotation()) {
      checkAnnotationClassWithBody(aClass);
      checkValOnAnnotationParameter(aClass);
    } else if (aClass.isEnum()) {
      checkEnumModifiers(aClass);
      if (aClass.isLocal()) {
        trace.report(LOCAL_ENUM_NOT_ALLOWED.on(aClass, classDescriptor));
      }
    } else if (aClass.hasModifier(JetTokens.SEALED_KEYWORD)) {
      checkSealedModifiers(aClass);
    } else if (aClass instanceof JetEnumEntry) {
      checkEnumEntry((JetEnumEntry) aClass, classDescriptor);
    }
  }
Ejemplo n.º 13
0
  public void resolveConstructorParameterDefaultValuesAndAnnotations(
      @NotNull DataFlowInfo outerDataFlowInfo,
      @NotNull BindingTrace trace,
      @NotNull JetClass klass,
      @NotNull ConstructorDescriptor constructorDescriptor,
      @NotNull LexicalScope declaringScope) {
    List<JetParameter> valueParameters = klass.getPrimaryConstructorParameters();
    List<ValueParameterDescriptor> valueParameterDescriptors =
        constructorDescriptor.getValueParameters();

    LexicalScope scope =
        getPrimaryConstructorParametersScope(declaringScope, constructorDescriptor);

    valueParameterResolver.resolveValueParameters(
        valueParameters, valueParameterDescriptors, scope, outerDataFlowInfo, trace);
  }
Ejemplo n.º 14
0
 private void checkTraitModifiers(JetClass aClass) {
   reportErrorIfHasIllegalModifier(aClass);
   JetModifierList modifierList = aClass.getModifierList();
   if (modifierList == null) return;
   if (modifierList.hasModifier(JetTokens.FINAL_KEYWORD)) {
     trace.report(Errors.TRAIT_CAN_NOT_BE_FINAL.on(aClass));
   }
   if (modifierList.hasModifier(JetTokens.SEALED_KEYWORD)) {
     trace.report(Errors.TRAIT_CAN_NOT_BE_SEALED.on(aClass));
   }
   if (modifierList.hasModifier(JetTokens.ABSTRACT_KEYWORD)) {
     trace.report(Errors.ABSTRACT_MODIFIER_IN_TRAIT.on(aClass));
   }
   if (modifierList.hasModifier(JetTokens.OPEN_KEYWORD)) {
     trace.report(Errors.OPEN_MODIFIER_IN_TRAIT.on(aClass));
   }
 }
Ejemplo n.º 15
0
 private void checkConstructorInTrait(JetClass klass) {
   JetPrimaryConstructor primaryConstructor = klass.getPrimaryConstructor();
   if (primaryConstructor != null) {
     trace.report(CONSTRUCTOR_IN_TRAIT.on(primaryConstructor));
   }
 }