예제 #1
0
  private void detectAndDisconnectLoops(@NotNull TopDownAnalysisContext c) {
    // Loop detection and disconnection
    List<Runnable> tasks = new ArrayList<Runnable>();
    for (final MutableClassDescriptorLite klass : c.getClassesTopologicalOrder()) {
      for (final JetType supertype : klass.getSupertypes()) {
        ClassifierDescriptor supertypeDescriptor =
            supertype.getConstructor().getDeclarationDescriptor();
        if (supertypeDescriptor instanceof MutableClassDescriptorLite) {
          MutableClassDescriptorLite superclass = (MutableClassDescriptorLite) supertypeDescriptor;
          if (isReachable(superclass, klass, new HashSet<ClassDescriptor>())) {
            tasks.add(
                new Runnable() {
                  @Override
                  public void run() {
                    klass.getSupertypes().remove(supertype);
                  }
                });
            reportCyclicInheritanceHierarchyError(trace, klass, superclass);
          }
        }
      }
    }

    for (Runnable task : tasks) {
      task.run();
    }
  }
예제 #2
0
    @NotNull
    @Override
    public List<TypeParameterDescriptor> getParameters() {
      if (parameters == null) {
        JetClassLikeInfo classInfo = declarationProvider.getOwnerInfo();
        List<JetTypeParameter> typeParameters = classInfo.getTypeParameters();
        parameters = new ArrayList<TypeParameterDescriptor>(typeParameters.size());

        for (int i = 0; i < typeParameters.size(); i++) {
          parameters.add(
              new LazyTypeParameterDescriptor(
                  resolveSession, LazyClassDescriptor.this, typeParameters.get(i), i));
        }
      }
      return parameters;
    }
예제 #3
0
 private void checkDefaultParameterValues(
     List<JetParameter> valueParameters,
     List<ValueParameterDescriptor> valueParameterDescriptors,
     JetScope declaringScope) {
   for (int i = 0; i < valueParameters.size(); i++) {
     ValueParameterDescriptor valueParameterDescriptor = valueParameterDescriptors.get(i);
     if (valueParameterDescriptor.hasDefaultValue()) {
       JetParameter jetParameter = valueParameters.get(i);
       JetExpression defaultValue = jetParameter.getDefaultValue();
       if (defaultValue != null) {
         expressionTypingServices.getType(
             declaringScope, defaultValue, valueParameterDescriptor.getType(), trace);
       }
     }
   }
 }
예제 #4
0
 private void findAndDisconnectLoopsInTypeHierarchy(List<JetType> supertypes) {
   for (Iterator<JetType> iterator = supertypes.iterator(); iterator.hasNext(); ) {
     JetType supertype = iterator.next();
     if (isReachable(supertype.getConstructor(), this, new HashSet<TypeConstructor>())) {
       iterator.remove();
     }
   }
 }
예제 #5
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);
      }
    }
  }
예제 #6
0
  private static Collection<ClassDescriptor> getClassOrObjectDescriptorsByFqName(
      NamespaceDescriptor packageDescriptor, FqName path, boolean includeObjectDeclarations) {
    if (path.isRoot()) {
      return Collections.emptyList();
    }

    Collection<JetScope> scopes = Arrays.asList(packageDescriptor.getMemberScope());

    List<Name> names = path.pathSegments();
    if (names.size() > 1) {
      for (Name subName : path.pathSegments().subList(0, names.size() - 1)) {
        Collection<JetScope> tempScopes = Lists.newArrayList();
        for (JetScope scope : scopes) {
          ClassifierDescriptor classifier = scope.getClassifier(subName);
          if (classifier instanceof ClassDescriptorBase) {
            ClassDescriptorBase classDescriptor = (ClassDescriptorBase) classifier;
            tempScopes.add(classDescriptor.getUnsubstitutedInnerClassesScope());
          }
        }
        scopes = tempScopes;
      }
    }

    Name shortName = path.shortName();
    Collection<ClassDescriptor> resultClassifierDescriptors = Lists.newArrayList();
    for (JetScope scope : scopes) {
      ClassifierDescriptor classifier = scope.getClassifier(shortName);
      if (classifier instanceof ClassDescriptor) {
        resultClassifierDescriptors.add((ClassDescriptor) classifier);
      }
      if (includeObjectDeclarations) {
        ClassDescriptor objectDescriptor = scope.getObjectDescriptor(shortName);
        if (objectDescriptor != null) {
          resultClassifierDescriptors.add(objectDescriptor);
        }
      }
    }

    return resultClassifierDescriptors;
  }
예제 #7
0
  /** Return class names form jet sources in given scope which should be visible as Java classes. */
  @NotNull
  @Override
  public PsiClass[] getClassesByName(
      @NotNull @NonNls String name, @NotNull GlobalSearchScope scope) {
    List<PsiClass> result = new ArrayList<PsiClass>();

    IDELightClassGenerationSupport lightClassGenerationSupport =
        IDELightClassGenerationSupport.getInstanceForIDE(project);
    MultiMap<String, FqName> packageClasses =
        lightClassGenerationSupport.getAllPackageClasses(scope);

    // .namespace classes can not be indexed, since they have no explicit declarations
    Collection<FqName> fqNames = packageClasses.get(name);
    if (!fqNames.isEmpty()) {
      for (FqName fqName : fqNames) {
        PsiClass psiClass =
            JavaElementFinder.getInstance(project).findClass(fqName.getFqName(), scope);
        if (psiClass != null) {
          result.add(psiClass);
        }
      }
    }

    // Quick check for classes from getAllClassNames()
    Collection<JetClassOrObject> classOrObjects =
        JetShortClassNameIndex.getInstance().get(name, project, scope);
    if (classOrObjects.isEmpty()) {
      return result.toArray(new PsiClass[result.size()]);
    }

    for (JetClassOrObject classOrObject : classOrObjects) {
      FqName fqName = JetPsiUtil.getFQName(classOrObject);
      if (fqName != null) {
        assert fqName.shortName().getName().equals(name)
            : "A declaration obtained from index has non-matching name:\n"
                + "in index: "
                + name
                + "\n"
                + "declared: "
                + fqName.shortName()
                + "("
                + fqName
                + ")";
        PsiClass psiClass =
            JavaElementFinder.getInstance(project).findClass(fqName.getFqName(), scope);
        if (psiClass != null) {
          result.add(psiClass);
        }
      }
    }

    return result.toArray(new PsiClass[result.size()]);
  }
예제 #8
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);
  }