コード例 #1
0
 private void createClassObjectForEnumClass(
     @NotNull MutableClassDescriptor mutableClassDescriptor) {
   if (mutableClassDescriptor.getKind() == ClassKind.ENUM_CLASS) {
     MutableClassDescriptor classObject = createSyntheticClassObject(mutableClassDescriptor);
     mutableClassDescriptor.getBuilder().setClassObjectDescriptor(classObject);
     classObject
         .getBuilder()
         .addFunctionDescriptor(
             DescriptorResolver.createEnumClassObjectValuesMethod(classObject, trace));
     classObject
         .getBuilder()
         .addFunctionDescriptor(
             DescriptorResolver.createEnumClassObjectValueOfMethod(classObject, trace));
   }
 }
コード例 #2
0
    @NotNull
    private MutablePackageFragmentDescriptor getOrCreatePackageFragmentForFile(
        @NotNull JetFile file) {
      JetPackageDirective packageDirective = file.getPackageDirective();
      assert packageDirective != null : "scripts are not supported";

      MutablePackageFragmentDescriptor fragment =
          packageFragmentProvider.getOrCreateFragment(packageDirective.getFqName());

      ModuleDescriptor module = packageFragmentProvider.getModule();
      DescriptorResolver.resolvePackageHeader(
          packageDirective, module, TypeHierarchyResolver.this.trace);

      trace.record(BindingContext.FILE_TO_PACKAGE_FRAGMENT, file, fragment);

      // Register files corresponding to this package
      // The trace currently does not support bi-di multimaps that would handle this task nicer
      FqName fqName = fragment.getFqName();
      Collection<JetFile> files = trace.get(PACKAGE_TO_FILES, fqName);
      if (files == null) {
        files = Sets.newIdentityHashSet();
      }
      files.add(file);
      trace.record(BindingContext.PACKAGE_TO_FILES, fqName, files);
      return fragment;
    }
コード例 #3
0
 @NotNull
 private ConstructorDescriptorImpl createPrimaryConstructorForObject(
     @Nullable PsiElement object, @NotNull MutableClassDescriptor mutableClassDescriptor) {
   ConstructorDescriptorImpl constructorDescriptor =
       DescriptorResolver.createAndRecordPrimaryConstructorForObject(
           object, mutableClassDescriptor, trace);
   mutableClassDescriptor.setPrimaryConstructor(constructorDescriptor);
   return constructorDescriptor;
 }
コード例 #4
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);
    }
  }
コード例 #5
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();
      }
    }
  }