Ejemplo n.º 1
0
    @Override
    public void visitJetFile(@NotNull JetFile file) {
      MutablePackageFragmentDescriptor packageFragment = getOrCreatePackageFragmentForFile(file);
      c.getPackageFragments().put(file, packageFragment);
      c.addFile(file);

      PackageViewDescriptor packageView =
          packageFragment.getContainingDeclaration().getPackage(packageFragment.getFqName());
      ChainedScope rootPlusPackageScope =
          new ChainedScope(
              packageView, "Root scope for " + file, packageView.getMemberScope(), outerScope);
      WriteThroughScope packageScope =
          new WriteThroughScope(
              rootPlusPackageScope,
              packageFragment.getMemberScope(),
              new TraceBasedRedeclarationHandler(trace),
              "package in file " + file.getName());
      packageScope.changeLockLevel(WritableScope.LockLevel.BOTH);
      c.getFileScopes().put(file, packageScope);

      if (file.isScript()) {
        scriptHeaderResolver.processScriptHierarchy(c, file.getScript(), packageScope);
      }

      prepareForDeferredCall(packageScope, packageFragment, file);
    }
Ejemplo n.º 2
0
  private void resolveFunctionAndPropertyHeaders(@NotNull TopDownAnalysisContext c) {
    for (Map.Entry<JetFile, WritableScope> entry : c.getFileScopes().entrySet()) {
      JetFile file = entry.getKey();
      WritableScope fileScope = entry.getValue();
      PackageLikeBuilder packageBuilder = c.getPackageFragments().get(file).getBuilder();

      resolveFunctionAndPropertyHeaders(
          c, file.getDeclarations(), fileScope, fileScope, fileScope, packageBuilder);
    }
    for (Map.Entry<JetClassOrObject, ClassDescriptorWithResolutionScopes> entry :
        c.getDeclaredClasses().entrySet()) {
      JetClassOrObject classOrObject = entry.getKey();
      MutableClassDescriptor classDescriptor = (MutableClassDescriptor) entry.getValue();

      resolveFunctionAndPropertyHeaders(
          c,
          classOrObject.getDeclarations(),
          classDescriptor.getScopeForMemberDeclarationResolution(),
          classDescriptor.getScopeForInitializerResolution(),
          classDescriptor.getScopeForMemberDeclarationResolution(),
          classDescriptor.getBuilder());
    }

    // TODO : Extensions
  }
Ejemplo n.º 3
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());
    }
  }
Ejemplo n.º 4
0
 private void resolveAnonymousInitializers() {
   for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) {
     resolveAnonymousInitializers(entry.getKey(), entry.getValue());
   }
   for (Map.Entry<JetObjectDeclaration, MutableClassDescriptor> entry :
       context.getObjects().entrySet()) {
     resolveAnonymousInitializers(entry.getKey(), entry.getValue());
   }
 }
Ejemplo n.º 5
0
 private void resolveDelegationSpecifierLists() {
   // TODO : Make sure the same thing is not initialized twice
   for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) {
     resolveDelegationSpecifierList(entry.getKey(), entry.getValue());
   }
   for (Map.Entry<JetObjectDeclaration, MutableClassDescriptor> entry :
       context.getObjects().entrySet()) {
     resolveDelegationSpecifierList(entry.getKey(), entry.getValue());
   }
 }
Ejemplo n.º 6
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.º 7
0
 private void resolveAnnotationStubsOnClassesAndConstructors() {
   AnnotationResolver annotationResolver =
       new AnnotationResolver(context.getSemanticServices(), context.getTrace());
   for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) {
     JetClass jetClass = entry.getKey();
     MutableClassDescriptor descriptor = entry.getValue();
     resolveAnnotationsForClassOrObject(annotationResolver, jetClass, descriptor);
   }
   for (Map.Entry<JetObjectDeclaration, MutableClassDescriptor> entry :
       context.getObjects().entrySet()) {
     JetObjectDeclaration objectDeclaration = entry.getKey();
     MutableClassDescriptor descriptor = entry.getValue();
     resolveAnnotationsForClassOrObject(annotationResolver, objectDeclaration, descriptor);
   }
 }
Ejemplo n.º 8
0
  public void checkRedeclarationsInInnerClassNames(@NotNull TopDownAnalysisContext c) {
    for (ClassDescriptorWithResolutionScopes classDescriptor : c.getDeclaredClasses().values()) {
      if (classDescriptor.getKind() == ClassKind.CLASS_OBJECT) {
        // Class objects should be considered during analysing redeclarations in classes
        continue;
      }

      Collection<DeclarationDescriptor> allDescriptors =
          classDescriptor.getScopeForMemberLookup().getOwnDeclaredDescriptors();

      ClassDescriptorWithResolutionScopes classObj = classDescriptor.getClassObjectDescriptor();
      if (classObj != null) {
        Collection<DeclarationDescriptor> classObjDescriptors =
            classObj.getScopeForMemberLookup().getOwnDeclaredDescriptors();
        if (!classObjDescriptors.isEmpty()) {
          allDescriptors = Lists.newArrayList(allDescriptors);
          allDescriptors.addAll(classObjDescriptors);
        }
      }

      Multimap<Name, DeclarationDescriptor> descriptorMap = HashMultimap.create();
      for (DeclarationDescriptor desc : allDescriptors) {
        if (desc instanceof ClassDescriptor || desc instanceof PropertyDescriptor) {
          descriptorMap.put(desc.getName(), desc);
        }
      }

      reportRedeclarations(descriptorMap);
    }
  }
Ejemplo n.º 9
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.º 10
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());
 }
Ejemplo n.º 11
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();
    }
  }
Ejemplo n.º 12
0
 private List<MutableClassDescriptorLite> topologicallySortClassesAndObjects(
     @NotNull TopDownAnalysisContext c) {
   // A topsort is needed only for better diagnostics:
   //    edges that get removed to disconnect loops are more reasonable in this case
   //noinspection unchecked
   return DFS.topologicalOrder(
       (Iterable) c.getClasses().values(),
       new DFS.Neighbors<MutableClassDescriptorLite>() {
         @NotNull
         @Override
         public Iterable<MutableClassDescriptorLite> getNeighbors(
             MutableClassDescriptorLite current) {
           List<MutableClassDescriptorLite> result = Lists.newArrayList();
           for (JetType supertype : current.getSupertypes()) {
             DeclarationDescriptor declarationDescriptor =
                 supertype.getConstructor().getDeclarationDescriptor();
             if (declarationDescriptor instanceof MutableClassDescriptorLite) {
               MutableClassDescriptorLite classDescriptor =
                   (MutableClassDescriptorLite) declarationDescriptor;
               result.add(classDescriptor);
             }
           }
           return result;
         }
       });
 }
Ejemplo n.º 13
0
    @Override
    public void visitClassObject(@NotNull JetClassObject classObject) {
      JetObjectDeclaration objectDeclaration = classObject.getObjectDeclaration();

      DeclarationDescriptor container = owner.getOwnerForChildren();

      MutableClassDescriptor classObjectDescriptor =
          createClassDescriptorForSingleton(
              objectDeclaration, getClassObjectName(container.getName()), ClassKind.CLASS_OBJECT);

      PackageLikeBuilder.ClassObjectStatus status =
          isEnumEntry(container)
                  || isObject(container)
                  || c.getTopDownAnalysisParameters().isDeclaredLocally()
              ? PackageLikeBuilder.ClassObjectStatus.NOT_ALLOWED
              : owner.setClassObjectDescriptor(classObjectDescriptor);

      switch (status) {
        case DUPLICATE:
          trace.report(MANY_CLASS_OBJECTS.on(classObject));
          break;
        case NOT_ALLOWED:
          trace.report(CLASS_OBJECT_NOT_ALLOWED.on(classObject));
          break;
        case OK:
          // Everything is OK so no errors to trace.
          break;
      }
    }
Ejemplo n.º 14
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.º 15
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
  }
Ejemplo n.º 16
0
  public void process(
      @NotNull TopDownAnalysisContext c,
      @NotNull JetScope outerScope,
      @NotNull PackageLikeBuilder owner,
      @NotNull Collection<? extends PsiElement> declarations) {

    {
      // TODO: Very temp code - main goal is to remove recursion from
      // collectPackageFragmentsAndClassifiers
      Queue<JetDeclarationContainer> forDeferredResolve = new LinkedList<JetDeclarationContainer>();
      forDeferredResolve.addAll(
          collectPackageFragmentsAndClassifiers(c, outerScope, owner, declarations));

      while (!forDeferredResolve.isEmpty()) {
        JetDeclarationContainer declarationContainer = forDeferredResolve.poll();
        assert declarationContainer != null;

        DeclarationDescriptor descriptorForDeferredResolve =
            c.forDeferredResolver.get(declarationContainer);
        JetScope scope = c.normalScope.get(declarationContainer);

        // Even more temp code
        if (descriptorForDeferredResolve instanceof MutableClassDescriptorLite) {
          forDeferredResolve.addAll(
              collectPackageFragmentsAndClassifiers(
                  c,
                  scope,
                  ((MutableClassDescriptorLite) descriptorForDeferredResolve).getBuilder(),
                  declarationContainer.getDeclarations()));
        } else if (descriptorForDeferredResolve instanceof MutablePackageFragmentDescriptor) {
          forDeferredResolve.addAll(
              collectPackageFragmentsAndClassifiers(
                  c,
                  scope,
                  ((MutablePackageFragmentDescriptor) descriptorForDeferredResolve).getBuilder(),
                  declarationContainer.getDeclarations()));
        } else {
          assert false;
        }
      }
    }

    importsResolver.processTypeImports(c);

    createTypeConstructors(
        c); // create type constructors for classes and generic parameters, supertypes are not
            // filled in
    resolveTypesInClassHeaders(
        c); // Generic bounds and types in supertype lists (no expressions or constructor
            // resolution)

    c.setClassesTopologicalOrder(topologicallySortClassesAndObjects(c));

    // Detect and disconnect all loops in the hierarchy
    detectAndDisconnectLoops(c);
  }
Ejemplo n.º 17
0
  private void resolveConstructorHeaders() {
    for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) {
      JetClass jetClass = entry.getKey();
      MutableClassDescriptor classDescriptor = entry.getValue();

      processPrimaryConstructor(classDescriptor, jetClass);
      for (JetSecondaryConstructor jetConstructor : jetClass.getSecondaryConstructors()) {
        processSecondaryConstructor(classDescriptor, jetConstructor);
      }
    }
  }
Ejemplo n.º 18
0
  private void resolveAnnotationConstructors(@NotNull TopDownAnalysisContext c) {
    for (Map.Entry<JetClassOrObject, ClassDescriptorWithResolutionScopes> entry :
        c.getDeclaredClasses().entrySet()) {
      JetClassOrObject classOrObject = entry.getKey();
      MutableClassDescriptor classDescriptor = (MutableClassDescriptor) entry.getValue();

      if (classOrObject instanceof JetClass && DescriptorUtils.isAnnotationClass(classDescriptor)) {
        processPrimaryConstructor(c, classDescriptor, (JetClass) classOrObject);
      }
    }
  }
Ejemplo n.º 19
0
 private void resolveAnnotationStubsOnClassesAndConstructors(@NotNull TopDownAnalysisContext c) {
   for (Map.Entry<JetClassOrObject, ClassDescriptorWithResolutionScopes> entry :
       c.getDeclaredClasses().entrySet()) {
     JetModifierList modifierList = entry.getKey().getModifierList();
     if (modifierList != null) {
       MutableClassDescriptor descriptor = (MutableClassDescriptor) entry.getValue();
       descriptor.addAnnotations(
           annotationResolver.resolveAnnotationsWithoutArguments(
               descriptor.getScopeForClassHeaderResolution(), modifierList, trace));
     }
   }
 }
Ejemplo n.º 20
0
  private void resolveTypesInClassHeaders(@NotNull TopDownAnalysisContext c) {
    for (Map.Entry<JetClassOrObject, ClassDescriptorWithResolutionScopes> entry :
        c.getClasses().entrySet()) {
      JetClassOrObject classOrObject = entry.getKey();
      if (classOrObject instanceof JetClass) {
        ClassDescriptorWithResolutionScopes descriptor = entry.getValue();
        //noinspection unchecked
        descriptorResolver.resolveGenericBounds(
            (JetClass) classOrObject,
            descriptor,
            descriptor.getScopeForClassHeaderResolution(),
            (List) descriptor.getTypeConstructor().getParameters(),
            trace);
      }
    }

    for (Map.Entry<JetClassOrObject, ClassDescriptorWithResolutionScopes> entry :
        c.getClasses().entrySet()) {
      descriptorResolver.resolveSupertypesForMutableClassDescriptor(
          entry.getKey(), (MutableClassDescriptor) entry.getValue(), trace);
    }
  }
Ejemplo n.º 21
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.º 22
0
  private void createFunctionsForDataClasses(@NotNull TopDownAnalysisContext c) {
    for (Map.Entry<JetClassOrObject, ClassDescriptorWithResolutionScopes> entry :
        c.getDeclaredClasses().entrySet()) {
      JetClassOrObject klass = entry.getKey();
      MutableClassDescriptor classDescriptor = (MutableClassDescriptor) entry.getValue();

      if (klass instanceof JetClass
          && klass.hasPrimaryConstructor()
          && KotlinBuiltIns.getInstance().isData(classDescriptor)) {
        ConstructorDescriptor constructor =
            DescriptorUtils.getConstructorOfDataClass(classDescriptor);
        createComponentFunctions(classDescriptor, constructor);
        createCopyFunction(classDescriptor, constructor);
      }
    }
  }
Ejemplo n.º 23
0
    @NotNull
    private MutableClassDescriptor createClassDescriptorForSingleton(
        @NotNull JetClassOrObject declaration, @NotNull Name name, @NotNull ClassKind kind) {
      MutableClassDescriptor descriptor =
          new MutableClassDescriptor(owner.getOwnerForChildren(), outerScope, kind, false, name);

      prepareForDeferredCall(
          descriptor.getScopeForMemberDeclarationResolution(), descriptor, declaration);

      createPrimaryConstructorForObject(declaration, descriptor);
      trace.record(BindingContext.CLASS, declaration, descriptor);

      c.getClasses().put(declaration, descriptor);

      return descriptor;
    }
Ejemplo n.º 24
0
  private void createTypeConstructors(@NotNull TopDownAnalysisContext c) {
    for (Map.Entry<JetClassOrObject, ClassDescriptorWithResolutionScopes> entry :
        c.getClasses().entrySet()) {
      JetClassOrObject classOrObject = entry.getKey();
      MutableClassDescriptor descriptor = (MutableClassDescriptor) entry.getValue();
      if (classOrObject instanceof JetClass) {
        descriptorResolver.resolveMutableClassDescriptor(
            (JetClass) classOrObject, descriptor, trace);
      } else if (classOrObject instanceof JetObjectDeclaration) {
        descriptor.setModality(Modality.FINAL);
        descriptor.setVisibility(
            resolveVisibilityFromModifiers(classOrObject, getDefaultClassVisibility(descriptor)));
        descriptor.setTypeParameterDescriptors(Collections.<TypeParameterDescriptor>emptyList());
      }

      descriptor.createTypeConstructor();

      ClassKind kind = descriptor.getKind();
      if (kind == ClassKind.ENUM_ENTRY
          || kind == ClassKind.OBJECT
          || kind == ClassKind.ENUM_CLASS) {
        MutableClassDescriptorLite classObject = descriptor.getClassObjectDescriptor();
        assert classObject != null
            : "Enum entries and named objects should have class objects: "
                + classOrObject.getText();

        JetType supertype;
        if (kind == ClassKind.ENUM_CLASS) {
          supertype = KotlinBuiltIns.getInstance().getAnyType();
        } else {
          // This is a clever hack: each enum entry and object declaration (i.e. singleton) has a
          // synthetic class object.
          // We make this class object inherit from the singleton here, thus allowing to use the
          // singleton's class object where
          // the instance of the singleton is applicable. Effectively all members of the singleton
          // would be present in its class
          // object as fake overrides, so you can access them via standard class object notation:
          // ObjectName.memberName()
          supertype = descriptor.getDefaultType();
        }
        classObject.setSupertypes(Collections.singleton(supertype));
        classObject.createTypeConstructor();
      }
    }
  }
Ejemplo n.º 25
0
  private void checkRedeclarationsInPackages(@NotNull TopDownAnalysisContext c) {
    for (MutablePackageFragmentDescriptor packageFragment :
        Sets.newHashSet(c.getPackageFragments().values())) {
      PackageViewDescriptor packageView =
          packageFragment.getContainingDeclaration().getPackage(packageFragment.getFqName());
      JetScope packageViewScope = packageView.getMemberScope();
      Multimap<Name, DeclarationDescriptor> simpleNameDescriptors =
          packageFragment.getMemberScope().getDeclaredDescriptorsAccessibleBySimpleName();
      for (Name name : simpleNameDescriptors.keySet()) {
        // Keep only properties with no receiver
        Collection<DeclarationDescriptor> descriptors =
            Collections2.filter(
                simpleNameDescriptors.get(name),
                new Predicate<DeclarationDescriptor>() {
                  @Override
                  public boolean apply(@Nullable DeclarationDescriptor descriptor) {
                    if (descriptor instanceof PropertyDescriptor) {
                      PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor;
                      return propertyDescriptor.getReceiverParameter() == null;
                    }
                    return true;
                  }
                });
        ContainerUtil.addIfNotNull(descriptors, packageViewScope.getPackage(name));

        if (descriptors.size() > 1) {
          for (DeclarationDescriptor declarationDescriptor : descriptors) {
            for (PsiElement declaration : getDeclarationsByDescriptor(declarationDescriptor)) {
              assert declaration != null
                  : "Null declaration for descriptor: "
                      + declarationDescriptor
                      + " "
                      + (declarationDescriptor != null
                          ? DescriptorRenderer.FQ_NAMES_IN_TYPES.render(declarationDescriptor)
                          : "");
              trace.report(
                  REDECLARATION.on(declaration, declarationDescriptor.getName().asString()));
            }
          }
        }
      }
    }
  }
Ejemplo n.º 26
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));
     }
   }
 }
Ejemplo n.º 27
0
  private JetScope makeScopeForPropertyAccessor(
      @NotNull JetPropertyAccessor accessor, PropertyDescriptor propertyDescriptor) {
    JetScope declaringScope = context.getDeclaringScopes().get(accessor);

    JetScope propertyDeclarationInnerScope =
        descriptorResolver.getPropertyDeclarationInnerScope(
            declaringScope,
            propertyDescriptor,
            propertyDescriptor.getTypeParameters(),
            propertyDescriptor.getReceiverParameter(),
            trace);
    WritableScope accessorScope =
        new WritableScopeImpl(
                propertyDeclarationInnerScope,
                declaringScope.getContainingDeclaration(),
                new TraceBasedRedeclarationHandler(trace))
            .setDebugName("Accessor scope");
    accessorScope.changeLockLevel(WritableScope.LockLevel.READING);

    return accessorScope;
  }
Ejemplo n.º 28
0
  private void resolveFunctionBody(
      @NotNull BindingTrace trace,
      @NotNull JetDeclarationWithBody function,
      @NotNull FunctionDescriptor functionDescriptor,
      @NotNull JetScope declaringScope) {
    if (!context.completeAnalysisNeeded(function)) return;

    JetExpression bodyExpression = function.getBodyExpression();
    JetScope functionInnerScope =
        FunctionDescriptorUtil.getFunctionInnerScope(declaringScope, functionDescriptor, trace);
    if (bodyExpression != null) {
      expressionTypingServices.checkFunctionReturnType(
          functionInnerScope, function, functionDescriptor, trace);
    }

    List<JetParameter> valueParameters = function.getValueParameters();
    List<ValueParameterDescriptor> valueParameterDescriptors =
        functionDescriptor.getValueParameters();

    checkDefaultParameterValues(valueParameters, valueParameterDescriptors, functionInnerScope);

    assert functionDescriptor.getReturnType() != null;
  }
Ejemplo n.º 29
0
 public void process() {
   resolveConstructorHeaders();
   resolveAnnotationStubsOnClassesAndConstructors();
   resolveFunctionAndPropertyHeaders();
   context.getImportsResolver().processMembersImports();
 }
Ejemplo n.º 30
0
 public DeclarationResolver(TopDownAnalysisContext context) {
   this.context = context;
   this.annotationResolver =
       new AnnotationResolver(context.getSemanticServices(), context.getTrace());
 }