public static void registerClassNameForScript(
      BindingTrace bindingTrace,
      @NotNull ScriptDescriptor scriptDescriptor,
      @NotNull JvmClassName className) {
    bindingTrace.record(SCRIPT_NAMES, className);

    ClassDescriptorImpl classDescriptor =
        new ClassDescriptorImpl(
            scriptDescriptor,
            Collections.<AnnotationDescriptor>emptyList(),
            Modality.FINAL,
            Name.special("<script-" + className + ">"));
    classDescriptor.initialize(
        false,
        Collections.<TypeParameterDescriptor>emptyList(),
        Collections.singletonList(KotlinBuiltIns.getInstance().getAnyType()),
        JetScope.EMPTY,
        Collections.<ConstructorDescriptor>emptySet(),
        null,
        false);

    recordClosure(bindingTrace, null, classDescriptor, null, className, false);

    assert PsiCodegenPredictor.checkPredictedClassNameForFun(
        bindingTrace.getBindingContext(), scriptDescriptor, classDescriptor);
    bindingTrace.record(CLASS_FOR_SCRIPT, scriptDescriptor, classDescriptor);
  }
예제 #2
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();
      }
    }
  }
예제 #3
0
    @NotNull
    private MutableClassDescriptor createSyntheticClassObject(
        @NotNull ClassDescriptor classDescriptor) {
      MutableClassDescriptor classObject =
          new MutableClassDescriptor(
              classDescriptor,
              outerScope,
              ClassKind.CLASS_OBJECT,
              false,
              getClassObjectName(classDescriptor.getName()));

      classObject.setModality(Modality.FINAL);
      classObject.setVisibility(DescriptorUtils.getSyntheticClassObjectVisibility());
      classObject.setTypeParameterDescriptors(Collections.<TypeParameterDescriptor>emptyList());
      createPrimaryConstructorForObject(null, classObject);
      return classObject;
    }
  @NotNull
  public static Collection<JetFile> allFilesInNamespaces(
      BindingContext bindingContext, Collection<JetFile> files) {
    // todo: we use Set and add given files but ignoring other scripts because something non-clear
    // kept in binding
    // for scripts especially in case of REPL

    HashSet<FqName> names = new HashSet<FqName>();
    for (JetFile file : files) {
      if (!file.isScript()) {
        names.add(JetPsiUtil.getFQName(file));
      }
    }

    HashSet<JetFile> answer = new HashSet<JetFile>();
    answer.addAll(files);

    for (FqName name : names) {
      NamespaceDescriptor namespaceDescriptor =
          bindingContext.get(BindingContext.FQNAME_TO_NAMESPACE_DESCRIPTOR, name);
      Collection<JetFile> jetFiles = bindingContext.get(NAMESPACE_TO_FILES, namespaceDescriptor);
      if (jetFiles != null) answer.addAll(jetFiles);
    }

    List<JetFile> sortedAnswer = new ArrayList<JetFile>(answer);
    Collections.sort(
        sortedAnswer,
        new Comparator<JetFile>() {
          @NotNull
          private String path(JetFile file) {
            VirtualFile virtualFile = file.getVirtualFile();
            assert virtualFile != null : "VirtualFile is null for JetFile: " + file.getName();
            return virtualFile.getPath();
          }

          @Override
          public int compare(JetFile first, JetFile second) {
            return path(first).compareTo(path(second));
          }
        });

    return sortedAnswer;
  }