private void checkVisibility(
      @NotNull DeclarationDescriptorWithVisibility descriptor,
      @NotNull BindingTrace trace,
      @NotNull JetSimpleNameExpression referenceExpression,
      @NotNull JetScope scopeToCheckVisibility) {

    if (!Visibilities.isVisible(descriptor, scopeToCheckVisibility.getContainingDeclaration())) {
      trace.report(
          INVISIBLE_REFERENCE.on(
              referenceExpression,
              descriptor,
              descriptor.getVisibility(),
              descriptor.getContainingDeclaration()));
    }
  }
  @Nullable
  public static ClassDescriptor getKotlinBuiltinClassDescriptor(@NotNull FqName qualifiedName) {
    if (!qualifiedName.firstSegmentIs(KotlinBuiltIns.BUILT_INS_PACKAGE_NAME)) return null;

    List<Name> segments = qualifiedName.pathSegments();
    if (segments.size() < 2) return null;

    JetScope scope = KotlinBuiltIns.getInstance().getBuiltInsPackageScope();
    for (int i = 1, size = segments.size(); i < size; i++) {
      ClassifierDescriptor classifier = scope.getClassifier(segments.get(i));
      if (classifier == null) return null;
      assert classifier instanceof ClassDescriptor
          : "Unexpected classifier in built-ins: " + classifier;
      scope = ((ClassDescriptor) classifier).getUnsubstitutedInnerClassesScope();
    }

    return (ClassDescriptor) scope.getContainingDeclaration();
  }
Example #3
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;
  }
Example #4
0
 @SuppressWarnings("ConstantConditions")
 private WritableScopeImpl addImports(JetScope scope) {
   WritableScopeImpl writableScope =
       new WritableScopeImpl(
           scope,
           scope.getContainingDeclaration(),
           RedeclarationHandler.DO_NOTHING,
           "JetTypeCheckerTest.addImports");
   InjectorForJavaDescriptorResolver injector =
       new InjectorForJavaDescriptorResolver(getProject(), new BindingTraceContext());
   JavaDescriptorResolver javaDescriptorResolver = injector.getJavaDescriptorResolver();
   writableScope.importScope(
       javaDescriptorResolver
           .resolveNamespace(FqName.ROOT, INCLUDE_KOTLIN_SOURCES)
           .getMemberScope());
   writableScope.importScope(
       javaDescriptorResolver
           .resolveNamespace(new FqName("java.lang"), IGNORE_KOTLIN_SOURCES)
           .getMemberScope());
   writableScope.changeLockLevel(WritableScope.LockLevel.BOTH);
   writableScope.importScope(builtIns.getBuiltInsScope());
   return writableScope;
 }
 private static JetValueArgumentList findCall(CreateParameterInfoContext context) {
   // todo: calls to this constructors, when we will have auxiliary constructors
   PsiFile file = context.getFile();
   if (!(file instanceof JetFile)) return null;
   PsiElement element = file.findElementAt(context.getOffset());
   while (element != null && !(element instanceof JetValueArgumentList)) {
     element = element.getParent();
   }
   if (element == null) return null;
   JetValueArgumentList argumentList = (JetValueArgumentList) element;
   JetCallElement callExpression;
   if (element.getParent() instanceof JetCallElement) {
     callExpression = (JetCallElement) element.getParent();
   } else {
     return null;
   }
   BindingContext bindingContext = AnalyzeSingleFileUtil.getContextForSingleFile((JetFile) file);
   JetExpression calleeExpression = callExpression.getCalleeExpression();
   if (calleeExpression == null) return null;
   JetSimpleNameExpression refExpression = null;
   if (calleeExpression instanceof JetSimpleNameExpression) {
     refExpression = (JetSimpleNameExpression) calleeExpression;
   } else if (calleeExpression instanceof JetConstructorCalleeExpression) {
     JetConstructorCalleeExpression constructorCalleeExpression =
         (JetConstructorCalleeExpression) calleeExpression;
     if (constructorCalleeExpression.getConstructorReferenceExpression()
         instanceof JetSimpleNameExpression) {
       refExpression =
           (JetSimpleNameExpression)
               constructorCalleeExpression.getConstructorReferenceExpression();
     }
   }
   if (refExpression != null) {
     JetScope scope = bindingContext.get(BindingContext.RESOLUTION_SCOPE, refExpression);
     DeclarationDescriptor placeDescriptor = null;
     if (scope != null) {
       placeDescriptor = scope.getContainingDeclaration();
     }
     Collection<DeclarationDescriptor> variants =
         TipsManager.getReferenceVariants(refExpression, bindingContext);
     Name refName = refExpression.getReferencedNameAsName();
     PsiReference[] references = refExpression.getReferences();
     if (references.length == 0) return null;
     ArrayList<DeclarationDescriptor> itemsToShow = new ArrayList<DeclarationDescriptor>();
     for (DeclarationDescriptor variant : variants) {
       if (variant instanceof FunctionDescriptor) {
         FunctionDescriptor functionDescriptor = (FunctionDescriptor) variant;
         if (functionDescriptor.getName().equals(refName)) {
           // todo: renamed functions?
           if (placeDescriptor != null
               && !JetVisibilityChecker.isVisible(placeDescriptor, functionDescriptor)) continue;
           itemsToShow.add(functionDescriptor);
         }
       } else if (variant instanceof ClassDescriptor) {
         ClassDescriptor classDescriptor = (ClassDescriptor) variant;
         if (classDescriptor.getName().equals(refName)) {
           // todo: renamed classes?
           for (ConstructorDescriptor constructorDescriptor : classDescriptor.getConstructors()) {
             if (placeDescriptor != null
                 && !JetVisibilityChecker.isVisible(placeDescriptor, constructorDescriptor))
               continue;
             itemsToShow.add(constructorDescriptor);
           }
         }
       }
     }
     context.setItemsToShow(ArrayUtil.toObjectArray(itemsToShow));
     return argumentList;
   }
   return null;
 }
Example #6
0
  private static <D extends CallableDescriptor, F extends D> void doComputeTasks(
      @NotNull JetScope scope,
      @NotNull ReceiverDescriptor receiver,
      @NotNull Name name,
      @NotNull ResolutionTaskHolder<D, F> result,
      @NotNull BasicResolutionContext context,
      @NotNull CallableDescriptorCollector<? extends D> callableDescriptorCollector) {

    ProgressIndicatorProvider.checkCanceled();

    AutoCastServiceImpl autoCastService =
        new AutoCastServiceImpl(context.dataFlowInfo, context.trace.getBindingContext());
    List<ReceiverDescriptor> implicitReceivers = Lists.newArrayList();
    scope.getImplicitReceiversHierarchy(implicitReceivers);
    boolean hasExplicitThisObject = context.call.getThisObject().exists();
    if (hasExplicitThisObject) {
      implicitReceivers.add(context.call.getThisObject());
    }
    if (receiver.exists()) {
      List<ReceiverDescriptor> variantsForExplicitReceiver =
          autoCastService.getVariantsForReceiver(receiver);

      Collection<ResolutionCandidate<D>> extensionFunctions =
          convertWithImpliedThis(
              scope,
              variantsForExplicitReceiver,
              callableDescriptorCollector.getNonMembersByName(scope, name));
      List<ResolutionCandidate<D>> nonlocals = Lists.newArrayList();
      List<ResolutionCandidate<D>> locals = Lists.newArrayList();
      //noinspection unchecked,RedundantTypeArguments
      TaskPrioritizer.<D>splitLexicallyLocalDescriptors(
          extensionFunctions, scope.getContainingDeclaration(), locals, nonlocals);

      Collection<ResolutionCandidate<D>> members = Lists.newArrayList();
      for (ReceiverDescriptor variant : variantsForExplicitReceiver) {
        Collection<? extends D> membersForThisVariant =
            callableDescriptorCollector.getMembersByName(variant.getType(), name);
        convertWithReceivers(
            membersForThisVariant,
            Collections.singletonList(variant),
            Collections.singletonList(NO_RECEIVER),
            members,
            hasExplicitThisObject);
      }

      result.addLocalExtensions(locals);
      result.addMembers(members);

      for (ReceiverDescriptor implicitReceiver : implicitReceivers) {
        Collection<? extends D> memberExtensions =
            callableDescriptorCollector.getNonMembersByName(
                implicitReceiver.getType().getMemberScope(), name);
        List<ReceiverDescriptor> variantsForImplicitReceiver =
            autoCastService.getVariantsForReceiver(implicitReceiver);
        result.addNonLocalExtensions(
            convertWithReceivers(
                memberExtensions,
                variantsForImplicitReceiver,
                variantsForExplicitReceiver,
                hasExplicitThisObject));
      }

      result.addNonLocalExtensions(nonlocals);
    } else {
      Collection<ResolutionCandidate<D>> functions =
          convertWithImpliedThis(
              scope,
              Collections.singletonList(receiver),
              callableDescriptorCollector.getNonExtensionsByName(scope, name));

      List<ResolutionCandidate<D>> nonlocals = Lists.newArrayList();
      List<ResolutionCandidate<D>> locals = Lists.newArrayList();
      //noinspection unchecked,RedundantTypeArguments
      TaskPrioritizer.<D>splitLexicallyLocalDescriptors(
          functions, scope.getContainingDeclaration(), locals, nonlocals);

      result.addLocalExtensions(locals);
      result.addNonLocalExtensions(nonlocals);

      for (ReceiverDescriptor implicitReceiver : implicitReceivers) {
        doComputeTasks(scope, implicitReceiver, name, result, context, callableDescriptorCollector);
      }
    }
  }
  private static JetValueArgumentList findCall(CreateParameterInfoContext context) {
    // todo: calls to this constructors, when we will have auxiliary constructors
    PsiFile file = context.getFile();
    if (!(file instanceof JetFile)) {
      return null;
    }

    JetValueArgumentList argumentList =
        PsiTreeUtil.getParentOfType(
            file.findElementAt(context.getOffset()), JetValueArgumentList.class);
    if (argumentList == null) {
      return null;
    }

    JetSimpleNameExpression callNameExpression = getCallSimpleNameExpression(argumentList);
    if (callNameExpression == null) {
      return null;
    }

    PsiReference[] references = callNameExpression.getReferences();
    if (references.length == 0) {
      return null;
    }

    CancelableResolveSession resolveSession =
        WholeProjectAnalyzerFacade.getLazyResolveResultForFile(
            (JetFile) callNameExpression.getContainingFile());
    BindingContext bindingContext = resolveSession.resolveToElement(callNameExpression);

    JetScope scope = bindingContext.get(BindingContext.RESOLUTION_SCOPE, callNameExpression);
    DeclarationDescriptor placeDescriptor = null;
    if (scope != null) {
      placeDescriptor = scope.getContainingDeclaration();
    }

    Collection<DeclarationDescriptor> variants =
        TipsManager.getReferenceVariants(callNameExpression, bindingContext);

    Name refName = callNameExpression.getReferencedNameAsName();

    Collection<Pair<? extends DeclarationDescriptor, CancelableResolveSession>> itemsToShow =
        new ArrayList<Pair<? extends DeclarationDescriptor, CancelableResolveSession>>();
    for (DeclarationDescriptor variant : variants) {
      if (variant instanceof FunctionDescriptor) {
        FunctionDescriptor functionDescriptor = (FunctionDescriptor) variant;
        if (functionDescriptor.getName().equals(refName)) {
          // todo: renamed functions?
          if (placeDescriptor != null
              && !JetVisibilityChecker.isVisible(placeDescriptor, functionDescriptor)) {
            continue;
          }
          itemsToShow.add(Pair.create(functionDescriptor, resolveSession));
        }
      } else if (variant instanceof ClassDescriptor) {
        ClassDescriptor classDescriptor = (ClassDescriptor) variant;
        if (classDescriptor.getName().equals(refName)) {
          // todo: renamed classes?
          for (ConstructorDescriptor constructorDescriptor : classDescriptor.getConstructors()) {
            if (placeDescriptor != null
                && !JetVisibilityChecker.isVisible(placeDescriptor, constructorDescriptor)) {
              continue;
            }
            itemsToShow.add(Pair.create(constructorDescriptor, resolveSession));
          }
        }
      }
    }

    context.setItemsToShow(ArrayUtil.toObjectArray(itemsToShow));
    return argumentList;
  }