예제 #1
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;
    }
예제 #2
0
  // TODO: Make it work for properties
  public Collection<DeclarationDescriptor> getJetCallableExtensions(
      @NotNull Condition<String> acceptedNameCondition,
      @NotNull JetSimpleNameExpression expression,
      @NotNull ResolveSession resolveSession,
      @NotNull GlobalSearchScope searchScope) {
    Collection<DeclarationDescriptor> resultDescriptors = new ArrayList<DeclarationDescriptor>();

    BindingContext context = ResolveSessionUtils.resolveToExpression(resolveSession, expression);
    JetExpression receiverExpression = expression.getReceiverExpression();

    if (receiverExpression != null) {
      JetType expressionType = context.get(BindingContext.EXPRESSION_TYPE, receiverExpression);
      JetScope scope = context.get(BindingContext.RESOLUTION_SCOPE, receiverExpression);

      if (expressionType != null && scope != null) {
        Collection<String> extensionFunctionsNames = getAllJetExtensionFunctionsNames(searchScope);

        Set<FqName> functionFQNs = new java.util.HashSet<FqName>();

        // Collect all possible extension function qualified names
        for (String name : extensionFunctionsNames) {
          if (acceptedNameCondition.value(name)) {
            Collection<PsiElement> extensionFunctions =
                getJetExtensionFunctionsByName(name, searchScope);

            for (PsiElement extensionFunction : extensionFunctions) {
              if (extensionFunction instanceof JetNamedFunction) {
                functionFQNs.add(JetPsiUtil.getFQName((JetNamedFunction) extensionFunction));
              } else if (extensionFunction instanceof PsiMethod) {
                FqName functionFQN =
                    JetFromJavaDescriptorHelper.getJetTopLevelDeclarationFQN(
                        (PsiMethod) extensionFunction);
                if (functionFQN != null) {
                  functionFQNs.add(functionFQN);
                }
              }
            }
          }
        }

        // Iterate through the function with attempt to resolve found functions
        for (FqName functionFQN : functionFQNs) {
          for (CallableDescriptor functionDescriptor :
              ExpressionTypingUtils.canFindSuitableCall(
                  functionFQN,
                  project,
                  receiverExpression,
                  expressionType,
                  scope,
                  resolveSession.getModuleConfiguration())) {

            resultDescriptors.add(functionDescriptor);
          }
        }
      }
    }

    return resultDescriptors;
  }
예제 #3
0
 private void prepareForDeferredCall(
     @NotNull JetScope outerScope,
     @NotNull DeclarationDescriptor descriptorForDeferredResolve,
     @NotNull JetDeclarationContainer container) {
   forDeferredResolve.add(container);
   c.normalScope.put(container, outerScope);
   c.forDeferredResolver.put(container, descriptorForDeferredResolve);
 }
 private static void recordInnerClass(
     @NotNull BindingTrace bindingTrace,
     @NotNull ClassDescriptor outer,
     @NotNull ClassDescriptor inner) {
   Collection<ClassDescriptor> innerClasses = bindingTrace.get(INNER_CLASSES, outer);
   if (innerClasses == null) {
     innerClasses = new ArrayList<ClassDescriptor>();
     bindingTrace.record(INNER_CLASSES, outer, innerClasses);
   }
   innerClasses.add(inner);
 }
예제 #5
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;
  }