Esempio n. 1
0
  private void processPrimaryConstructor(MutableClassDescriptor classDescriptor, JetClass klass) {
    if (classDescriptor.getKind() == ClassKind.TRAIT) {
      JetParameterList primaryConstructorParameterList = klass.getPrimaryConstructorParameterList();
      if (primaryConstructorParameterList != null) {
        context.getTrace().report(CONSTRUCTOR_IN_TRAIT.on(primaryConstructorParameterList));
      }
      if (!klass.hasPrimaryConstructor()) return;
    }

    // TODO : not all the parameters are real properties
    JetScope memberScope = classDescriptor.getScopeForSupertypeResolution();
    ConstructorDescriptor constructorDescriptor =
        context
            .getDescriptorResolver()
            .resolvePrimaryConstructorDescriptor(memberScope, classDescriptor, klass);
    for (JetParameter parameter : klass.getPrimaryConstructorParameters()) {
      if (parameter.getValOrVarNode() != null) {
        PropertyDescriptor propertyDescriptor =
            context
                .getDescriptorResolver()
                .resolvePrimaryConstructorParameterToAProperty(
                    classDescriptor, memberScope, parameter);
        classDescriptor.addPropertyDescriptor(propertyDescriptor);
        context.getPrimaryConstructorParameterProperties().add(propertyDescriptor);
      }
    }
    if (constructorDescriptor != null) {
      classDescriptor.setPrimaryConstructor(constructorDescriptor, context.getTrace());
    }
  }
Esempio n. 2
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);
    }
  }
Esempio n. 3
0
 private void resolveAnnotationsForClassOrObject(
     AnnotationResolver annotationResolver,
     JetClassOrObject jetClass,
     MutableClassDescriptor descriptor) {
   JetModifierList modifierList = jetClass.getModifierList();
   if (modifierList != null) {
     descriptor
         .getAnnotations()
         .addAll(
             annotationResolver.resolveAnnotations(
                 descriptor.getScopeForSupertypeResolution(),
                 modifierList.getAnnotationEntries()));
   }
 }
Esempio n. 4
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());
     }
   }
 }
Esempio n. 5
0
 private void processSecondaryConstructor(
     MutableClassDescriptor classDescriptor, JetSecondaryConstructor constructor) {
   if (classDescriptor.getKind() == ClassKind.TRAIT) {
     //            context.getTrace().getErrorHandler().genericError(constructor.getNameNode(), "A
     // trait may not have a constructor");
     context.getTrace().report(CONSTRUCTOR_IN_TRAIT.on(constructor.getNameNode()));
   }
   ConstructorDescriptor constructorDescriptor =
       context
           .getDescriptorResolver()
           .resolveSecondaryConstructorDescriptor(
               classDescriptor.getScopeForMemberResolution(), classDescriptor, constructor);
   classDescriptor.addConstructor(constructorDescriptor, context.getTrace());
   context.getConstructors().put(constructor, constructorDescriptor);
   context.getDeclaringScopes().put(constructor, classDescriptor.getScopeForMemberLookup());
 }
Esempio n. 6
0
  @NotNull
  private static ClassDescriptor createClass(
      @NotNull PackageFragmentDescriptor packageFragment, @NotNull String name) {
    MutableClassDescriptor descriptor =
        new MutableClassDescriptor(
            packageFragment,
            ClassKind.CLASS,
            false,
            Name.identifier(name),
            SourceElement.NO_SOURCE);

    descriptor.setModality(Modality.FINAL);
    descriptor.setVisibility(Visibilities.PUBLIC);
    descriptor.setTypeParameterDescriptors(Collections.<TypeParameterDescriptor>emptyList());
    descriptor.createTypeConstructor();

    return descriptor;
  }
Esempio n. 7
0
 private void resolveAnonymousInitializers(
     JetClassOrObject jetClassOrObject, MutableClassDescriptor classDescriptor) {
   if (!context.completeAnalysisNeeded(jetClassOrObject)) return;
   List<JetClassInitializer> anonymousInitializers = jetClassOrObject.getAnonymousInitializers();
   if (classDescriptor.getUnsubstitutedPrimaryConstructor() != null) {
     ConstructorDescriptor primaryConstructor =
         classDescriptor.getUnsubstitutedPrimaryConstructor();
     assert primaryConstructor != null;
     final JetScope scopeForInitializers = classDescriptor.getScopeForInitializers();
     for (JetClassInitializer anonymousInitializer : anonymousInitializers) {
       expressionTypingServices.getType(
           scopeForInitializers, anonymousInitializer.getBody(), NO_EXPECTED_TYPE, trace);
     }
   } else {
     for (JetClassInitializer anonymousInitializer : anonymousInitializers) {
       trace.report(ANONYMOUS_INITIALIZER_WITHOUT_CONSTRUCTOR.on(anonymousInitializer));
     }
   }
 }
Esempio n. 8
0
  private void resolveFunctionAndPropertyHeaders() {
    for (Map.Entry<JetFile, WritableScope> entry : context.getNamespaceScopes().entrySet()) {
      JetFile namespace = entry.getKey();
      WritableScope namespaceScope = entry.getValue();
      NamespaceLike namespaceDescriptor = context.getNamespaceDescriptors().get(namespace);

      resolveFunctionAndPropertyHeaders(
          namespace.getDeclarations(),
          namespaceScope,
          namespaceScope,
          namespaceScope,
          namespaceDescriptor);
    }
    for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) {
      JetClass jetClass = entry.getKey();
      MutableClassDescriptor classDescriptor = entry.getValue();

      resolveFunctionAndPropertyHeaders(
          jetClass.getDeclarations(),
          classDescriptor.getScopeForMemberResolution(),
          classDescriptor.getScopeForInitializers(),
          classDescriptor.getScopeForMemberResolution(),
          classDescriptor);
      //            processPrimaryConstructor(classDescriptor, jetClass);
      //            for (JetSecondaryConstructor jetConstructor :
      // jetClass.getSecondaryConstructors()) {
      //                processSecondaryConstructor(classDescriptor, jetConstructor);
      //            }
    }
    for (Map.Entry<JetObjectDeclaration, MutableClassDescriptor> entry :
        context.getObjects().entrySet()) {
      JetObjectDeclaration object = entry.getKey();
      MutableClassDescriptor classDescriptor = entry.getValue();

      resolveFunctionAndPropertyHeaders(
          object.getDeclarations(),
          classDescriptor.getScopeForMemberResolution(),
          classDescriptor.getScopeForInitializers(),
          classDescriptor.getScopeForMemberResolution(),
          classDescriptor);
    }

    // TODO : Extensions
  }
Esempio n. 9
0
  private void resolveSecondaryConstructorBody(
      JetSecondaryConstructor declaration, final ConstructorDescriptor descriptor) {
    if (!context.completeAnalysisNeeded(declaration)) return;
    MutableClassDescriptor classDescriptor =
        (MutableClassDescriptor) descriptor.getContainingDeclaration();
    final JetScope scopeForSupertypeInitializers =
        FunctionDescriptorUtil.getFunctionInnerScope(
            classDescriptor.getScopeForSupertypeResolution(), descriptor, trace);
    // contains only constructor parameters
    final JetScope scopeForConstructorBody =
        FunctionDescriptorUtil.getFunctionInnerScope(
            classDescriptor.getScopeForInitializers(), descriptor, trace);
    // contains members & backing fields

    final DataFlowInfo dataFlowInfo = DataFlowInfo.EMPTY; // TODO: dataFlowInfo

    PsiElement nameElement = declaration.getNameNode().getPsi();
    if (classDescriptor.getUnsubstitutedPrimaryConstructor() == null) {
      trace.report(SECONDARY_CONSTRUCTOR_BUT_NO_PRIMARY.on(nameElement));
    } else {
      List<JetDelegationSpecifier> initializers = declaration.getInitializers();
      if (initializers.isEmpty()) {
        trace.report(SECONDARY_CONSTRUCTOR_NO_INITIALIZER_LIST.on(nameElement));
      } else {
        initializers
            .get(0)
            .accept(
                new JetVisitorVoid() {
                  @Override
                  public void visitDelegationToSuperCallSpecifier(JetDelegatorToSuperCall call) {
                    JetTypeReference typeReference = call.getTypeReference();
                    if (typeReference != null) {
                      callResolver.resolveFunctionCall(
                          trace,
                          scopeForSupertypeInitializers,
                          CallMaker.makeCall(ReceiverDescriptor.NO_RECEIVER, null, call),
                          NO_EXPECTED_TYPE,
                          dataFlowInfo);
                    }
                  }

                  @Override
                  public void visitDelegationToThisCall(JetDelegatorToThisCall call) {
                    // TODO : check that there's no recursion in this() calls
                    // TODO : check: if a this() call is present, no other initializers are allowed
                    ClassDescriptor classDescriptor = descriptor.getContainingDeclaration();

                    callResolver.resolveFunctionCall(
                        trace,
                        scopeForSupertypeInitializers,
                        CallMaker.makeCall(ReceiverDescriptor.NO_RECEIVER, null, call),
                        NO_EXPECTED_TYPE,
                        dataFlowInfo);
                    //                                call.getThisReference(),
                    //                                classDescriptor,
                    //                                classDescriptor.getDefaultType(),
                    //                                call);
                    //                        trace.getErrorHandler().genericError(call.getNode(),
                    // "this-calls are not supported");
                  }

                  @Override
                  public void visitDelegationByExpressionSpecifier(
                      JetDelegatorByExpressionSpecifier specifier) {
                    trace.report(BY_IN_SECONDARY_CONSTRUCTOR.on(specifier));
                  }

                  @Override
                  public void visitDelegationToSuperClassSpecifier(
                      JetDelegatorToSuperClass specifier) {
                    trace.report(INITIALIZER_WITH_NO_ARGUMENTS.on(specifier));
                  }

                  @Override
                  public void visitDelegationSpecifier(JetDelegationSpecifier specifier) {
                    throw new IllegalStateException();
                  }
                });
        for (int i = 1, initializersSize = initializers.size(); i < initializersSize; i++) {
          JetDelegationSpecifier initializer = initializers.get(i);
          trace.report(MANY_CALLS_TO_THIS.on(initializer));
        }
      }
    }
    JetExpression bodyExpression = declaration.getBodyExpression();
    if (bodyExpression != null) {

      expressionTypingServices.checkFunctionReturnType(
          scopeForConstructorBody,
          declaration,
          descriptor,
          JetStandardClasses.getUnitType(),
          trace);
    }

    checkDefaultParameterValues(
        declaration.getValueParameters(), descriptor.getValueParameters(), scopeForConstructorBody);
  }
Esempio n. 10
0
  private void resolveDelegationSpecifierList(
      final JetClassOrObject jetClass, final MutableClassDescriptor descriptor) {
    if (!context.completeAnalysisNeeded(jetClass)) return;
    final ConstructorDescriptor primaryConstructor =
        descriptor.getUnsubstitutedPrimaryConstructor();
    final JetScope scopeForConstructor =
        primaryConstructor == null
            ? null
            : FunctionDescriptorUtil.getFunctionInnerScope(
                descriptor.getScopeForSupertypeResolution(), primaryConstructor, trace);
    final ExpressionTypingServices typeInferrer = expressionTypingServices; // TODO : flow

    final Map<JetTypeReference, JetType> supertypes = Maps.newLinkedHashMap();
    JetVisitorVoid visitor =
        new JetVisitorVoid() {
          private void recordSupertype(JetTypeReference typeReference, JetType supertype) {
            if (supertype == null) return;
            supertypes.put(typeReference, supertype);
          }

          @Override
          public void visitDelegationByExpressionSpecifier(
              JetDelegatorByExpressionSpecifier specifier) {
            if (descriptor.getKind() == ClassKind.TRAIT) {
              trace.report(DELEGATION_IN_TRAIT.on(specifier));
            }
            JetType supertype =
                trace.getBindingContext().get(BindingContext.TYPE, specifier.getTypeReference());
            recordSupertype(specifier.getTypeReference(), supertype);
            if (supertype != null) {
              DeclarationDescriptor declarationDescriptor =
                  supertype.getConstructor().getDeclarationDescriptor();
              if (declarationDescriptor instanceof ClassDescriptor) {
                ClassDescriptor classDescriptor = (ClassDescriptor) declarationDescriptor;
                if (classDescriptor.getKind() != ClassKind.TRAIT) {
                  trace.report(DELEGATION_NOT_TO_TRAIT.on(specifier.getTypeReference()));
                }
              }
            }
            JetExpression delegateExpression = specifier.getDelegateExpression();
            if (delegateExpression != null) {
              JetScope scope =
                  scopeForConstructor == null
                      ? descriptor.getScopeForMemberResolution()
                      : scopeForConstructor;
              JetType type =
                  typeInferrer.getType(scope, delegateExpression, NO_EXPECTED_TYPE, trace);
              if (type != null
                  && supertype != null
                  && !JetTypeChecker.INSTANCE.isSubtypeOf(type, supertype)) {
                trace.report(TYPE_MISMATCH.on(delegateExpression, supertype, type));
              }
            }
          }

          @Override
          public void visitDelegationToSuperCallSpecifier(JetDelegatorToSuperCall call) {
            JetValueArgumentList valueArgumentList = call.getValueArgumentList();
            PsiElement elementToMark = valueArgumentList == null ? call : valueArgumentList;
            if (descriptor.getKind() == ClassKind.TRAIT) {
              trace.report(SUPERTYPE_INITIALIZED_IN_TRAIT.on(elementToMark));
            }
            JetTypeReference typeReference = call.getTypeReference();
            if (typeReference == null) return;
            if (descriptor.getUnsubstitutedPrimaryConstructor() == null) {
              assert descriptor.getKind() == ClassKind.TRAIT;
              return;
            }
            OverloadResolutionResults<FunctionDescriptor> results =
                callResolver.resolveFunctionCall(
                    trace,
                    scopeForConstructor,
                    CallMaker.makeCall(ReceiverDescriptor.NO_RECEIVER, null, call),
                    NO_EXPECTED_TYPE,
                    DataFlowInfo.EMPTY);
            if (results.isSuccess()) {
              JetType supertype = results.getResultingDescriptor().getReturnType();
              recordSupertype(typeReference, supertype);
              ClassDescriptor classDescriptor = TypeUtils.getClassDescriptor(supertype);
              if (classDescriptor != null) {
                if (classDescriptor.getKind() == ClassKind.TRAIT) {
                  trace.report(CONSTRUCTOR_IN_TRAIT.on(elementToMark));
                }
              }
            } else {
              recordSupertype(
                  typeReference, trace.getBindingContext().get(BindingContext.TYPE, typeReference));
            }
          }

          @Override
          public void visitDelegationToSuperClassSpecifier(JetDelegatorToSuperClass specifier) {
            JetTypeReference typeReference = specifier.getTypeReference();
            JetType supertype = trace.getBindingContext().get(BindingContext.TYPE, typeReference);
            recordSupertype(typeReference, supertype);
            if (supertype == null) return;
            ClassDescriptor classDescriptor = TypeUtils.getClassDescriptor(supertype);
            if (classDescriptor == null) return;
            if (descriptor.getKind() != ClassKind.TRAIT
                && classDescriptor.hasConstructors()
                && !ErrorUtils.isError(classDescriptor.getTypeConstructor())
                && classDescriptor.getKind() != ClassKind.TRAIT) {
              boolean hasConstructorWithoutParams = false;
              for (ConstructorDescriptor constructor : classDescriptor.getConstructors()) {
                if (constructor.getValueParameters().isEmpty()) {
                  hasConstructorWithoutParams = true;
                }
              }
              if (!hasConstructorWithoutParams) {
                trace.report(SUPERTYPE_NOT_INITIALIZED.on(specifier));
              } else {
                trace.report(SUPERTYPE_NOT_INITIALIZED_DEFAULT.on(specifier));
              }
            }
          }

          @Override
          public void visitDelegationToThisCall(JetDelegatorToThisCall thisCall) {
            throw new IllegalStateException("This-calls should be prohibited by the parser");
          }

          @Override
          public void visitJetElement(JetElement element) {
            throw new UnsupportedOperationException(element.getText() + " : " + element);
          }
        };

    for (JetDelegationSpecifier delegationSpecifier : jetClass.getDelegationSpecifiers()) {
      delegationSpecifier.accept(visitor);
    }

    Set<TypeConstructor> parentEnum = Collections.emptySet();
    if (jetClass instanceof JetEnumEntry) {
      parentEnum =
          Collections.singleton(
              ((ClassDescriptor) descriptor.getContainingDeclaration().getContainingDeclaration())
                  .getTypeConstructor());
    }

    checkSupertypeList(descriptor, supertypes, parentEnum);
  }