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);
  }
 @NotNull
 @Override
 public Collection<? extends JetType> getSupertypes() {
   if (supertypes == null) {
     if (resolveSession.isClassSpecial(DescriptorUtils.getFQName(LazyClassDescriptor.this))) {
       this.supertypes = Collections.emptyList();
     } else {
       JetClassOrObject classOrObject =
           declarationProvider.getOwnerInfo().getCorrespondingClassOrObject();
       if (classOrObject == null) {
         this.supertypes = Collections.emptyList();
       } else {
         List<JetType> allSupertypes =
             resolveSession
                 .getInjector()
                 .getDescriptorResolver()
                 .resolveSupertypes(
                     getScopeForClassHeaderResolution(),
                     LazyClassDescriptor.this,
                     classOrObject,
                     resolveSession.getTrace());
         List<JetType> validSupertypes =
             Lists.newArrayList(Collections2.filter(allSupertypes, VALID_SUPERTYPE));
         this.supertypes = validSupertypes;
         findAndDisconnectLoopsInTypeHierarchy(validSupertypes);
       }
     }
   }
   return supertypes;
 }
                  @Override
                  public Collection<JetType> invoke() {
                    if (resolveSession.isClassSpecial(
                        DescriptorUtils.getFQName(LazyClassDescriptor.this))) {
                      return Collections.emptyList();
                    } else {
                      JetClassOrObject classOrObject =
                          declarationProvider.getOwnerInfo().getCorrespondingClassOrObject();
                      if (classOrObject == null) {
                        return Collections.emptyList();
                      } else {
                        List<JetType> allSupertypes =
                            resolveSession
                                .getInjector()
                                .getDescriptorResolver()
                                .resolveSupertypes(
                                    getScopeForClassHeaderResolution(),
                                    LazyClassDescriptor.this,
                                    classOrObject,
                                    resolveSession.getTrace());

                        return Lists.newArrayList(
                            Collections2.filter(allSupertypes, VALID_SUPERTYPE));
                      }
                    }
                  }
Exemple #4
0
 static {
   ERROR_CLASS.initialize(
       true,
       Collections.<TypeParameterDescriptor>emptyList(),
       Collections.<JetType>emptyList(),
       createErrorScope("ERROR_CLASS"),
       ERROR_CONSTRUCTOR_GROUP,
       ERROR_CONSTRUCTOR,
       false);
 }
Exemple #5
0
 private static JetType createErrorTypeWithCustomDebugName(
     JetScope memberScope, String debugName) {
   return new ErrorTypeImpl(
       new TypeConstructorImpl(
           ERROR_CLASS,
           Collections.<AnnotationDescriptor>emptyList(),
           false,
           debugName,
           Collections.<TypeParameterDescriptorImpl>emptyList(),
           Collections.singleton(KotlinBuiltIns.getInstance().getAnyType())),
       memberScope);
 }
Exemple #6
0
 public static ConstructorDescriptor createErrorConstructor(
     int typeParameterCount, List<JetType> positionedValueParameterTypes) {
   ConstructorDescriptorImpl r =
       new ConstructorDescriptorImpl(
           ERROR_CLASS, Collections.<AnnotationDescriptor>emptyList(), false);
   r.initialize(
       Collections.<TypeParameterDescriptor>emptyList(), // TODO
       Collections.<ValueParameterDescriptor>emptyList(), // TODO
       Visibilities.INTERNAL);
   r.setReturnType(createErrorType("<ERROR RETURN TYPE>"));
   return r;
 }
Exemple #7
0
 private static SimpleFunctionDescriptor createErrorFunction(ErrorScope ownerScope) {
   ErrorSimpleFunctionDescriptorImpl function = new ErrorSimpleFunctionDescriptorImpl(ownerScope);
   function.initialize(
       null,
       ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER,
       Collections.<TypeParameterDescriptorImpl>emptyList(), // TODO
       Collections.<ValueParameterDescriptor>emptyList(), // TODO
       createErrorType("<ERROR FUNCTION RETURN TYPE>"),
       Modality.OPEN,
       Visibilities.INTERNAL,
       /*isInline = */ false);
   return function;
 }
  @NotNull
  private Collection<PsiElement> getDeclarationsByDescriptor(
      @NotNull DeclarationDescriptor declarationDescriptor) {
    Collection<PsiElement> declarations;
    if (declarationDescriptor instanceof PackageViewDescriptor) {
      final PackageViewDescriptor aPackage = (PackageViewDescriptor) declarationDescriptor;
      Collection<JetFile> files = trace.get(BindingContext.PACKAGE_TO_FILES, aPackage.getFqName());

      if (files == null) {
        return Collections
            .emptyList(); // package can be defined out of Kotlin sources, e. g. in library or Java
        // code
      }

      declarations =
          Collections2.transform(
              files,
              new Function<JetFile, PsiElement>() {
                @Override
                public PsiElement apply(@Nullable JetFile file) {
                  assert file != null : "File is null for aPackage " + aPackage;
                  return file.getPackageDirective().getNameIdentifier();
                }
              });
    } else {
      declarations =
          Collections.singletonList(
              BindingContextUtils.descriptorToDeclaration(
                  trace.getBindingContext(), declarationDescriptor));
    }
    return declarations;
  }
  @NotNull
  public static Collection<ClassDescriptor> getClassOrObjectDescriptorsByFqName(
      @NotNull ResolveSession resolveSession,
      @NotNull FqName fqName,
      boolean includeObjectDeclarations) {
    if (fqName.isRoot()) {
      return Collections.emptyList();
    }

    Collection<ClassDescriptor> classDescriptors = Lists.newArrayList();

    FqName packageFqName = fqName.parent();
    while (true) {
      NamespaceDescriptor packageDescriptor =
          resolveSession.getPackageDescriptorByFqName(packageFqName);
      if (packageDescriptor != null) {
        FqName classInPackagePath = new FqName(QualifiedNamesUtil.tail(packageFqName, fqName));
        Collection<ClassDescriptor> descriptors =
            getClassOrObjectDescriptorsByFqName(
                packageDescriptor, classInPackagePath, includeObjectDeclarations);
        classDescriptors.addAll(descriptors);
      }

      if (packageFqName.isRoot()) {
        break;
      } else {
        packageFqName = packageFqName.parent();
      }
    }

    return classDescriptors;
  }
 @NotNull
 private List<AnnotationDescriptor> resolveAnnotations() {
   JetClassLikeInfo classInfo = declarationProvider.getOwnerInfo();
   JetModifierList modifierList = classInfo.getModifierList();
   if (modifierList != null) {
     AnnotationResolver annotationResolver = resolveSession.getInjector().getAnnotationResolver();
     JetScope scopeForDeclaration =
         getScopeProvider().getResolutionScopeForDeclaration(classInfo.getScopeAnchor());
     return annotationResolver.resolveAnnotationsWithArguments(
         scopeForDeclaration, modifierList, resolveSession.getTrace());
   } else {
     return Collections.emptyList();
   }
 }
Exemple #11
0
 private static List<ValueParameterDescriptor> getValueParameters(
     FunctionDescriptor functionDescriptor, List<JetType> argumentTypes) {
   List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>();
   for (int i = 0, argumentTypesSize = argumentTypes.size(); i < argumentTypesSize; i++) {
     result.add(
         new ValueParameterDescriptorImpl(
             functionDescriptor,
             i,
             Collections.<AnnotationDescriptor>emptyList(),
             Name.special("<ERROR VALUE_PARAMETER>"),
             ERROR_PARAMETER_TYPE,
             false,
             null));
   }
   return result;
 }
 private static BodyResolver createBodyResolver(
     DelegatingBindingTrace trace,
     JetFile file,
     BodyResolveContextForLazy bodyResolveContext,
     ModuleConfiguration moduleConfiguration) {
   TopDownAnalysisParameters parameters =
       new TopDownAnalysisParameters(
           Predicates.<PsiFile>alwaysTrue(),
           false,
           true,
           Collections.<AnalyzerScriptParameter>emptyList());
   InjectorForBodyResolve bodyResolve =
       new InjectorForBodyResolve(
           file.getProject(), parameters, trace, bodyResolveContext, moduleConfiguration);
   return bodyResolve.getBodyResolver();
 }
    @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;
    }
  public static void initializeFromFunctionType(
      @NotNull FunctionDescriptorImpl functionDescriptor,
      @NotNull JetType functionType,
      @Nullable ReceiverParameterDescriptor expectedThisObject,
      @NotNull Modality modality,
      @NotNull Visibility visibility) {

    assert KotlinBuiltIns.getInstance().isFunctionOrExtensionFunctionType(functionType);
    functionDescriptor.initialize(
        KotlinBuiltIns.getInstance().getReceiverType(functionType),
        expectedThisObject,
        Collections.<TypeParameterDescriptorImpl>emptyList(),
        KotlinBuiltIns.getInstance().getValueParameters(functionDescriptor, functionType),
        KotlinBuiltIns.getInstance().getReturnTypeFromFunctionType(functionType),
        modality,
        visibility);
  }
  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();
      }
    }
  }
 @Override
 public List<AnnotationDescriptor> getAnnotations() {
   if (annotations == null) {
     JetClassLikeInfo classInfo = declarationProvider.getOwnerInfo();
     JetModifierList modifierList = classInfo.getModifierList();
     if (modifierList != null) {
       AnnotationResolver annotationResolver =
           resolveSession.getInjector().getAnnotationResolver();
       annotations =
           annotationResolver.resolveAnnotations(
               resolveSession.getResolutionScope(classInfo.getScopeAnchor()),
               modifierList,
               resolveSession.getTrace());
     } else {
       annotations = Collections.emptyList();
     }
   }
   return annotations;
 }
 protected Void visitVariableDescriptor(
     VariableDescriptor descriptor, StringBuilder builder, boolean skipValVar) {
   JetType type = descriptor.getType();
   if (descriptor instanceof ValueParameterDescriptor) {
     JetType varargElementType = ((ValueParameterDescriptor) descriptor).getVarargElementType();
     if (varargElementType != null) {
       builder.append(renderKeyword("vararg")).append(" ");
       type = varargElementType;
     }
   }
   String typeString =
       renderPropertyPrefixAndComputeTypeString(
           builder,
           skipValVar ? null : descriptor.isVar(),
           Collections.<TypeParameterDescriptor>emptyList(),
           ReceiverDescriptor.NO_RECEIVER,
           type);
   renderName(descriptor, builder);
   builder.append(" : ").append(escape(typeString));
   return null;
 }
  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;
  }
  @NotNull
  public Collection<ClassDescriptor> getTopLevelObjectsByName(
      @NotNull String name,
      @NotNull JetSimpleNameExpression expression,
      @NotNull ResolveSession resolveSession,
      @NotNull GlobalSearchScope scope) {
    BindingContext context = ResolveSessionUtils.resolveToExpression(resolveSession, expression);
    JetScope jetScope = context.get(BindingContext.RESOLUTION_SCOPE, expression);

    if (jetScope == null) {
      return Collections.emptyList();
    }

    Set<ClassDescriptor> result = Sets.newHashSet();

    Collection<JetObjectDeclaration> topObjects =
        JetTopLevelShortObjectNameIndex.getInstance().get(name, project, scope);
    for (JetObjectDeclaration objectDeclaration : topObjects) {
      FqName fqName = JetPsiUtil.getFQName(objectDeclaration);
      assert fqName != null
          : "Local object declaration in JetTopLevelShortObjectNameIndex:"
              + objectDeclaration.getText();
      result.addAll(
          ResolveSessionUtils.getClassOrObjectDescriptorsByFqName(resolveSession, fqName, true));
    }

    for (PsiClass psiClass :
        JetFromJavaDescriptorHelper.getCompiledClassesForTopLevelObjects(
            project, GlobalSearchScope.allScope(project))) {
      String qualifiedName = psiClass.getQualifiedName();
      if (qualifiedName != null) {
        FqName fqName = new FqName(qualifiedName);
        result.addAll(
            ResolveSessionUtils.getClassOrObjectDescriptorsByFqName(resolveSession, fqName, true));
      }
    }

    return result;
  }
  public JvmFunctionImplTypes(@NotNull ReflectionTypes reflectionTypes) {
    this.reflectionTypes = reflectionTypes;

    ModuleDescriptor fakeModule =
        new ModuleDescriptorImpl(
            Name.special("<fake module for functions impl>"),
            Collections.<ImportPath>emptyList(),
            JavaToKotlinClassMap.getInstance());

    PackageFragmentDescriptor kotlinJvmInternal =
        new MutablePackageFragmentDescriptor(fakeModule, new FqName("kotlin.jvm.internal"));
    PackageFragmentDescriptor kotlinReflectJvmInternal =
        new MutablePackageFragmentDescriptor(fakeModule, new FqName("kotlin.reflect.jvm.internal"));

    this.functionImpl = createClass(kotlinJvmInternal, "FunctionImpl", "out R");
    this.extensionFunctionImpl =
        createClass(kotlinJvmInternal, "ExtensionFunctionImpl", "in T", "out R");
    this.kFunctionImpl = createClass(kotlinReflectJvmInternal, "KFunctionImpl", "out R");
    this.kExtensionFunctionImpl =
        createClass(kotlinReflectJvmInternal, "KExtensionFunctionImpl", "in T", "out R");
    this.kMemberFunctionImpl =
        createClass(kotlinReflectJvmInternal, "KMemberFunctionImpl", "in T", "out R");
  }
 @Override
 public List<AnnotationDescriptor> getAnnotations() {
   return Collections.emptyList(); // TODO
 }
 @Override
 public Collection<JetType> invoke(Boolean firstTime) {
   return Collections.emptyList();
 }
  @NotNull
  public Collection<FunctionDescriptor> getTopLevelFunctionDescriptorsByName(
      @NotNull String name,
      @NotNull JetSimpleNameExpression expression,
      @NotNull ResolveSession resolveSession,
      @NotNull GlobalSearchScope scope) {
    // name parameter can differ from expression.getReferenceName() when expression contains
    // completion suffix
    Name referenceName =
        expression.getIdentifier() == null
            ? JetPsiUtil.getConventionName(expression)
            : Name.identifier(name);
    if (referenceName == null || referenceName.toString().isEmpty()) {
      return Collections.emptyList();
    }

    BindingContext context = ResolveSessionUtils.resolveToExpression(resolveSession, expression);
    JetScope jetScope = context.get(BindingContext.RESOLUTION_SCOPE, expression);

    if (jetScope == null) {
      return Collections.emptyList();
    }

    Set<FunctionDescriptor> result = Sets.newHashSet();

    Collection<PsiMethod> topLevelFunctionPrototypes =
        JetFromJavaDescriptorHelper.getTopLevelFunctionPrototypesByName(
            referenceName.getName(), project, scope);
    for (PsiMethod method : topLevelFunctionPrototypes) {
      FqName functionFQN = JetFromJavaDescriptorHelper.getJetTopLevelDeclarationFQN(method);
      if (functionFQN != null) {
        JetImportDirective importDirective =
            JetPsiFactory.createImportDirective(project, new ImportPath(functionFQN, false));
        Collection<? extends DeclarationDescriptor> declarationDescriptors =
            new QualifiedExpressionResolver()
                .analyseImportReference(
                    importDirective,
                    jetScope,
                    new BindingTraceContext(),
                    resolveSession.getModuleConfiguration());
        for (DeclarationDescriptor declarationDescriptor : declarationDescriptors) {
          if (declarationDescriptor instanceof FunctionDescriptor) {
            result.add((FunctionDescriptor) declarationDescriptor);
          }
        }
      }
    }

    Set<FqName> affectedPackages = Sets.newHashSet();
    Collection<JetNamedFunction> jetNamedFunctions =
        JetShortFunctionNameIndex.getInstance().get(referenceName.getName(), project, scope);
    for (JetNamedFunction jetNamedFunction : jetNamedFunctions) {
      PsiFile containingFile = jetNamedFunction.getContainingFile();
      if (containingFile instanceof JetFile) {
        JetFile jetFile = (JetFile) containingFile;
        String packageName = jetFile.getPackageName();
        if (packageName != null) {
          affectedPackages.add(new FqName(packageName));
        }
      }
    }

    for (FqName affectedPackage : affectedPackages) {
      NamespaceDescriptor packageDescriptor =
          resolveSession.getPackageDescriptorByFqName(affectedPackage);
      assert packageDescriptor != null
          : "There's a function in stub index with invalid package: " + affectedPackage;
      JetScope memberScope = packageDescriptor.getMemberScope();
      result.addAll(memberScope.getFunctions(referenceName));
    }

    return result;
  }
Exemple #24
0
 @NotNull
 @Override
 public Collection<DeclarationDescriptor> getOwnDeclaredDescriptors() {
   return Collections.emptyList();
 }
Exemple #25
0
 @NotNull
 @Override
 public Collection<DeclarationDescriptor> getDeclarationsByLabel(LabelName labelName) {
   return Collections.emptyList();
 }
Exemple #26
0
 @NotNull
 @Override
 public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
   return Collections.emptyList();
 }
Exemple #27
0
public class ErrorUtils {

  private static final ModuleDescriptor ERROR_MODULE =
      new ModuleDescriptor(Name.special("<ERROR MODULE>"));

  public static class ErrorScope implements JetScope {

    private final String debugMessage;

    private ErrorScope(String debugMessage) {
      this.debugMessage = debugMessage;
    }

    @Override
    public ClassifierDescriptor getClassifier(@NotNull Name name) {
      return ERROR_CLASS;
    }

    @Override
    public ClassDescriptor getObjectDescriptor(@NotNull Name name) {
      return ERROR_CLASS;
    }

    @NotNull
    @Override
    public Set<ClassDescriptor> getObjectDescriptors() {
      return Collections.emptySet();
    }

    @NotNull
    @Override
    public Set<VariableDescriptor> getProperties(@NotNull Name name) {
      return ERROR_PROPERTY_GROUP;
    }

    @Override
    public VariableDescriptor getLocalVariable(@NotNull Name name) {
      return ERROR_PROPERTY;
    }

    @Override
    public NamespaceDescriptor getNamespace(@NotNull Name name) {
      return null; // TODO : review
    }

    @NotNull
    @Override
    public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
      return Collections.emptyList();
    }

    @NotNull
    @Override
    public Set<FunctionDescriptor> getFunctions(@NotNull Name name) {
      return Collections.<FunctionDescriptor>singleton(createErrorFunction(this));
    }

    @NotNull
    @Override
    public DeclarationDescriptor getContainingDeclaration() {
      return ERROR_MODULE;
    }

    @NotNull
    @Override
    public Collection<DeclarationDescriptor> getDeclarationsByLabel(LabelName labelName) {
      return Collections.emptyList();
    }

    @Override
    public PropertyDescriptor getPropertyByFieldReference(@NotNull Name fieldName) {
      return null; // TODO : review
    }

    @NotNull
    @Override
    public Collection<DeclarationDescriptor> getAllDescriptors() {
      return Collections.emptyList();
    }

    @NotNull
    @Override
    public Collection<DeclarationDescriptor> getOwnDeclaredDescriptors() {
      return Collections.emptyList();
    }
  }

  private static final ClassDescriptorImpl ERROR_CLASS =
      new ClassDescriptorImpl(
          ERROR_MODULE,
          Collections.<AnnotationDescriptor>emptyList(),
          Modality.OPEN,
          Name.special("<ERROR CLASS>")) {
        @NotNull
        @Override
        public Collection<ConstructorDescriptor> getConstructors() {
          return ERROR_CONSTRUCTOR_GROUP;
        }

        @NotNull
        @Override
        public Modality getModality() {
          return Modality.OPEN;
        }

        @NotNull
        @Override
        public ClassDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
          return ERROR_CLASS;
        }
      };

  private static final Set<ConstructorDescriptor> ERROR_CONSTRUCTOR_GROUP =
      Collections.singleton(createErrorConstructor(0, Collections.<JetType>emptyList()));
  private static final ConstructorDescriptor ERROR_CONSTRUCTOR =
      new ConstructorDescriptorImpl(
          ERROR_CLASS, Collections.<AnnotationDescriptor>emptyList(), true);

  static {
    ERROR_CLASS.initialize(
        true,
        Collections.<TypeParameterDescriptor>emptyList(),
        Collections.<JetType>emptyList(),
        createErrorScope("ERROR_CLASS"),
        ERROR_CONSTRUCTOR_GROUP,
        ERROR_CONSTRUCTOR,
        false);
  }

  public static JetScope createErrorScope(String debugMessage) {
    return new ErrorScope(debugMessage);
  }

  private static final JetType ERROR_PROPERTY_TYPE = createErrorType("<ERROR PROPERTY TYPE>");
  private static final VariableDescriptor ERROR_PROPERTY =
      new PropertyDescriptorImpl(
          ERROR_CLASS,
          Collections.<AnnotationDescriptor>emptyList(),
          Modality.OPEN,
          Visibilities.INTERNAL,
          true,
          null,
          ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER,
          Name.special("<ERROR PROPERTY>"),
          ERROR_PROPERTY_TYPE,
          CallableMemberDescriptor.Kind.DECLARATION);
  private static final Set<VariableDescriptor> ERROR_PROPERTY_GROUP =
      Collections.singleton(ERROR_PROPERTY);

  private static SimpleFunctionDescriptor createErrorFunction(ErrorScope ownerScope) {
    ErrorSimpleFunctionDescriptorImpl function = new ErrorSimpleFunctionDescriptorImpl(ownerScope);
    function.initialize(
        null,
        ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER,
        Collections.<TypeParameterDescriptorImpl>emptyList(), // TODO
        Collections.<ValueParameterDescriptor>emptyList(), // TODO
        createErrorType("<ERROR FUNCTION RETURN TYPE>"),
        Modality.OPEN,
        Visibilities.INTERNAL,
        /*isInline = */ false);
    return function;
  }

  public static ConstructorDescriptor createErrorConstructor(
      int typeParameterCount, List<JetType> positionedValueParameterTypes) {
    ConstructorDescriptorImpl r =
        new ConstructorDescriptorImpl(
            ERROR_CLASS, Collections.<AnnotationDescriptor>emptyList(), false);
    r.initialize(
        Collections.<TypeParameterDescriptor>emptyList(), // TODO
        Collections.<ValueParameterDescriptor>emptyList(), // TODO
        Visibilities.INTERNAL);
    r.setReturnType(createErrorType("<ERROR RETURN TYPE>"));
    return r;
  }

  private static final JetType ERROR_PARAMETER_TYPE =
      createErrorType("<ERROR VALUE_PARAMETER TYPE>");

  private static List<ValueParameterDescriptor> getValueParameters(
      FunctionDescriptor functionDescriptor, List<JetType> argumentTypes) {
    List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>();
    for (int i = 0, argumentTypesSize = argumentTypes.size(); i < argumentTypesSize; i++) {
      result.add(
          new ValueParameterDescriptorImpl(
              functionDescriptor,
              i,
              Collections.<AnnotationDescriptor>emptyList(),
              Name.special("<ERROR VALUE_PARAMETER>"),
              ERROR_PARAMETER_TYPE,
              false,
              null));
    }
    return result;
  }

  @NotNull
  public static JetType createErrorType(String debugMessage) {
    return createErrorType(debugMessage, createErrorScope(debugMessage));
  }

  private static JetType createErrorType(String debugMessage, JetScope memberScope) {
    return createErrorTypeWithCustomDebugName(memberScope, "[ERROR : " + debugMessage + "]");
  }

  @NotNull
  public static JetType createErrorTypeWithCustomDebugName(String debugName) {
    return createErrorTypeWithCustomDebugName(createErrorScope(debugName), debugName);
  }

  private static JetType createErrorTypeWithCustomDebugName(
      JetScope memberScope, String debugName) {
    return new ErrorTypeImpl(
        new TypeConstructorImpl(
            ERROR_CLASS,
            Collections.<AnnotationDescriptor>emptyList(),
            false,
            debugName,
            Collections.<TypeParameterDescriptorImpl>emptyList(),
            Collections.singleton(KotlinBuiltIns.getInstance().getAnyType())),
        memberScope);
  }

  public static JetType createWrongVarianceErrorType(TypeProjection value) {
    return createErrorType(value + " is not allowed here", value.getType().getMemberScope());
  }

  public static ClassifierDescriptor getErrorClass() {
    return ERROR_CLASS;
  }

  public static boolean isError(@NotNull TypeConstructor typeConstructor) {
    return typeConstructor == ERROR_CLASS.getTypeConstructor();
  }

  public static boolean isErrorType(@NotNull JetType type) {
    return type != TypeUtils.NO_EXPECTED_TYPE
        && !(type instanceof NamespaceType)
        && ((type instanceof DeferredType
                && (((DeferredType) type).getActualType() == null
                    || isErrorType(((DeferredType) type).getActualType())))
            || type instanceof ErrorTypeImpl
            || isError(type.getConstructor()));
  }

  public static boolean containsErrorType(@Nullable JetType type) {
    if (type == null) return false;
    if (type instanceof NamespaceType) return false;
    if (isErrorType(type)) return true;
    for (TypeProjection projection : type.getArguments()) {
      if (containsErrorType(projection.getType())) return true;
    }
    return false;
  }

  public static boolean isError(@NotNull DeclarationDescriptor candidate) {
    return candidate == getErrorClass()
        || candidate.getContainingDeclaration() == getErrorClass()
        || candidate == ERROR_MODULE;
  }

  private static class ErrorTypeImpl implements JetType {

    private final TypeConstructor constructor;

    private final JetScope memberScope;

    private ErrorTypeImpl(TypeConstructor constructor, JetScope memberScope) {
      this.constructor = constructor;
      this.memberScope = memberScope;
    }

    @NotNull
    @Override
    public TypeConstructor getConstructor() {
      return constructor;
    }

    @NotNull
    @Override
    public List<TypeProjection> getArguments() {
      return Collections.emptyList();
    }

    @Override
    public boolean isNullable() {
      return false;
    }

    @NotNull
    @Override
    public JetScope getMemberScope() {
      return memberScope;
    }

    @Override
    public List<AnnotationDescriptor> getAnnotations() {
      return Collections.emptyList();
    }

    @Override
    public String toString() {
      return constructor.toString();
    }
  }

  private ErrorUtils() {}
}
Exemple #28
0
 @NotNull
 @Override
 public List<TypeProjection> getArguments() {
   return Collections.emptyList();
 }