public void addConstructorParametersToInitializersScope(
     @NotNull Collection<? extends VariableDescriptor> variables) {
   WritableScope scope = getWritableScopeForInitializers();
   for (VariableDescriptor variable : variables) {
     scope.addVariableDescriptor(variable);
   }
 }
  public MutableClassDescriptor(
      @NotNull DeclarationDescriptor containingDeclaration,
      @NotNull JetScope outerScope,
      @NotNull ClassKind kind,
      boolean isInner,
      @NotNull Name name,
      @NotNull SourceElement source) {
    super(LockBasedStorageManager.NO_LOCKS, containingDeclaration, name, source);
    this.kind = kind;
    this.isInner = isInner;

    RedeclarationHandler redeclarationHandler = RedeclarationHandler.DO_NOTHING;

    setScopeForMemberLookup(
        new WritableScopeImpl(JetScope.EMPTY, this, redeclarationHandler, "MemberLookup")
            .changeLockLevel(WritableScope.LockLevel.BOTH));
    this.scopeForSupertypeResolution =
        new WritableScopeImpl(outerScope, this, redeclarationHandler, "SupertypeResolution")
            .changeLockLevel(WritableScope.LockLevel.BOTH);
    this.scopeForMemberResolution =
        new WritableScopeImpl(
                scopeForSupertypeResolution, this, redeclarationHandler, "MemberResolution")
            .changeLockLevel(WritableScope.LockLevel.BOTH);

    if (kind == ClassKind.TRAIT) {
      setUpScopeForInitializers(this);
    }

    scopeForMemberResolution.importScope(staticScope);
    scopeForMemberResolution.addLabeledDeclaration(this);
  }
 public void lockScopes() {
   getScopeForMemberLookupAsWritableScope().changeLockLevel(WritableScope.LockLevel.READING);
   if (classObjectDescriptor != null) {
     classObjectDescriptor.lockScopes();
   }
   scopeForSupertypeResolution.changeLockLevel(WritableScope.LockLevel.READING);
   scopeForMemberResolution.changeLockLevel(WritableScope.LockLevel.READING);
   getWritableScopeForInitializers().changeLockLevel(WritableScope.LockLevel.READING);
 }
 public void setTypeParameterDescriptors(@NotNull List<TypeParameterDescriptor> typeParameters) {
   if (this.typeParameters != null) {
     throw new IllegalStateException("Type parameters are already set for " + getName());
   }
   this.typeParameters = new ArrayList<TypeParameterDescriptor>(typeParameters);
   for (TypeParameterDescriptor typeParameterDescriptor : typeParameters) {
     scopeForSupertypeResolution.addTypeParameterDescriptor(typeParameterDescriptor);
   }
   scopeForSupertypeResolution.changeLockLevel(WritableScope.LockLevel.READING);
 }
 @Override
 public JetTypeInfo visitClass(JetClass klass, ExpressionTypingContext context) {
   TopDownAnalyzer.processClassOrObject(
       context.expressionTypingServices.getProject(),
       context.trace,
       scope,
       scope.getContainingDeclaration(),
       klass);
   ClassDescriptor classDescriptor =
       context.trace.getBindingContext().get(BindingContext.CLASS, klass);
   if (classDescriptor != null) {
     scope.addClassifierDescriptor(classDescriptor);
   }
   return DataFlowUtils.checkStatementType(klass, context, context.dataFlowInfo);
 }
Beispiel #6
0
  @Nullable
  private ScriptDescriptor doAnalyze(
      @NotNull JetFile psiFile, @NotNull MessageCollector messageCollector) {
    WritableScope scope =
        new WritableScopeImpl(
            JetScope.EMPTY,
            module,
            new TraceBasedRedeclarationHandler(trace),
            "Root scope in analyzePackage");

    scope.changeLockLevel(WritableScope.LockLevel.BOTH);

    // Import a scope that contains all top-level packages that come from dependencies
    // This makes the packages visible at all, does not import themselves
    scope.importScope(module.getPackage(FqName.ROOT).getMemberScope());

    if (lastLineScope != null) {
      scope.importScope(lastLineScope);
    }

    scope.changeLockLevel(WritableScope.LockLevel.READING);

    // dummy builder is used because "root" is module descriptor,
    // packages added to module explicitly in
    injector
        .getTopDownAnalyzer()
        .doProcess(
            topDownAnalysisContext,
            scope,
            new PackageLikeBuilderDummy(),
            Collections.singletonList(psiFile));

    boolean hasErrors =
        AnalyzerWithCompilerReport.reportDiagnostics(trace.getBindingContext(), messageCollector);
    if (hasErrors) {
      return null;
    }

    ScriptDescriptor scriptDescriptor =
        topDownAnalysisContext.getScripts().get(psiFile.getScript());
    lastLineScope = trace.get(BindingContext.SCRIPT_SCOPE, scriptDescriptor);
    if (lastLineScope == null) {
      throw new IllegalStateException("last line scope is not initialized");
    }

    return scriptDescriptor;
  }
  @Override
  public JetTypeInfo visitProperty(JetProperty property, ExpressionTypingContext context) {
    JetTypeReference receiverTypeRef = property.getReceiverTypeRef();
    if (receiverTypeRef != null) {
      context.trace.report(LOCAL_EXTENSION_PROPERTY.on(receiverTypeRef));
    }

    JetPropertyAccessor getter = property.getGetter();
    if (getter != null) {
      context.trace.report(LOCAL_VARIABLE_WITH_GETTER.on(getter));
    }

    JetPropertyAccessor setter = property.getSetter();
    if (setter != null) {
      context.trace.report(LOCAL_VARIABLE_WITH_SETTER.on(setter));
    }

    VariableDescriptor propertyDescriptor =
        context
            .expressionTypingServices
            .getDescriptorResolver()
            .resolveLocalVariableDescriptor(
                scope.getContainingDeclaration(),
                scope,
                property,
                context.dataFlowInfo,
                context.trace);
    JetExpression initializer = property.getInitializer();
    DataFlowInfo dataFlowInfo = context.dataFlowInfo;
    if (initializer != null) {
      JetType outType = propertyDescriptor.getType();
      JetTypeInfo typeInfo =
          facade.getTypeInfo(initializer, context.replaceExpectedType(outType).replaceScope(scope));
      dataFlowInfo = typeInfo.getDataFlowInfo();
    }

    {
      VariableDescriptor olderVariable = scope.getLocalVariable(propertyDescriptor.getName());
      ExpressionTypingUtils.checkVariableShadowing(context, propertyDescriptor, olderVariable);
    }

    scope.addVariableDescriptor(propertyDescriptor);
    ModifiersChecker.create(context.trace).checkModifiersForLocalDeclaration(property);
    return DataFlowUtils.checkStatementType(property, context, dataFlowInfo);
  }
Beispiel #8
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;
  }
 @Override
 public void extendNamespaceScope(
     @NotNull BindingTrace trace,
     @NotNull NamespaceDescriptor namespaceDescriptor,
     @NotNull WritableScope namespaceMemberScope) {
   namespaceMemberScope.importScope(
       javaSemanticServices
           .getDescriptorResolver()
           .createJavaPackageScope(
               DescriptorUtils.getFQName(namespaceDescriptor).toSafe(), namespaceDescriptor));
   delegateConfiguration.extendNamespaceScope(trace, namespaceDescriptor, namespaceMemberScope);
 }
 @NotNull
 public static JetScope getFunctionInnerScope(
     @NotNull JetScope outerScope,
     @NotNull FunctionDescriptor descriptor,
     @NotNull BindingTrace trace) {
   WritableScope parameterScope =
       new WritableScopeImpl(
           outerScope,
           descriptor,
           new TraceBasedRedeclarationHandler(trace),
           "Function inner scope");
   ReceiverParameterDescriptor receiver = descriptor.getReceiverParameter();
   if (receiver != null) {
     parameterScope.setImplicitReceiver(receiver);
   }
   for (TypeParameterDescriptor typeParameter : descriptor.getTypeParameters()) {
     parameterScope.addTypeParameterDescriptor(typeParameter);
   }
   for (ValueParameterDescriptor valueParameterDescriptor : descriptor.getValueParameters()) {
     parameterScope.addVariableDescriptor(valueParameterDescriptor);
   }
   parameterScope.addLabeledDeclaration(descriptor);
   parameterScope.changeLockLevel(WritableScope.LockLevel.READING);
   return parameterScope;
 }
 @Override
 public JetTypeInfo visitObjectDeclaration(
     JetObjectDeclaration declaration, ExpressionTypingContext context) {
   TopDownAnalyzer.processClassOrObject(
       context.expressionTypingServices.getProject(),
       context.trace,
       scope,
       scope.getContainingDeclaration(),
       declaration);
   ClassDescriptor classDescriptor =
       context.trace.getBindingContext().get(BindingContext.CLASS, declaration);
   if (classDescriptor != null) {
     VariableDescriptor variableDescriptor =
         context
             .expressionTypingServices
             .getDescriptorResolver()
             .resolveObjectDeclaration(
                 scope.getContainingDeclaration(), declaration, classDescriptor, context.trace);
     scope.addVariableDescriptor(variableDescriptor);
   }
   return DataFlowUtils.checkStatementType(declaration, context, context.dataFlowInfo);
 }
 public void createTypeConstructor() {
   assert typeConstructor == null : typeConstructor;
   this.typeConstructor =
       TypeConstructorImpl.createForClass(
           this,
           Annotations.EMPTY, // TODO : pass annotations from the class?
           !getModality().isOverridable(),
           getName().asString(),
           typeParameters,
           supertypes);
   for (FunctionDescriptor functionDescriptor : getConstructors()) {
     ((ConstructorDescriptorImpl) functionDescriptor).setReturnType(getDefaultType());
   }
   scopeForMemberResolution.setImplicitReceiver(getThisAsReceiverParameter());
 }
  @Override
  public JetTypeInfo visitNamedFunction(
      JetNamedFunction function, ExpressionTypingContext context) {
    SimpleFunctionDescriptor functionDescriptor =
        context
            .expressionTypingServices
            .getDescriptorResolver()
            .resolveFunctionDescriptor(
                scope.getContainingDeclaration(), scope, function, context.trace);

    scope.addFunctionDescriptor(functionDescriptor);
    JetScope functionInnerScope =
        FunctionDescriptorUtil.getFunctionInnerScope(
            context.scope, functionDescriptor, context.trace);
    context.expressionTypingServices.checkFunctionReturnType(
        functionInnerScope,
        function,
        functionDescriptor,
        context.dataFlowInfo,
        null,
        context.trace);
    ModifiersChecker.create(context.trace).checkModifiersForLocalDeclaration(function);
    return DataFlowUtils.checkStatementType(function, context, context.dataFlowInfo);
  }