Exemple #1
0
  private static boolean isInsideCall(KtExpression expression) {
    KtElement parent = KtPsiUtil.getParentCallIfPresent(expression);
    if (parent instanceof KtBinaryExpression) {
      KtToken token = KtPsiUtil.getOperationToken((KtOperationExpression) parent);
      if (token == KtTokens.EQ || token == KtTokens.ANDAND || token == KtTokens.OROR) {
        // assignment
        return false;
      }
    }

    if (parent != null) {
      // UGLY HACK
      // check there is no casts
      PsiElement current = expression;
      while (current != parent) {
        if (current instanceof KtBinaryExpressionWithTypeRHS) {
          return false;
        }
        current = current.getParent();
      }
    }

    return parent != null;
  }
  public LazyClassDescriptor(
      @NotNull final LazyClassContext c,
      @NotNull DeclarationDescriptor containingDeclaration,
      @NotNull Name name,
      @NotNull final KtClassLikeInfo classLikeInfo) {
    super(
        c.getStorageManager(),
        containingDeclaration,
        name,
        KotlinSourceElementKt.toSourceElement(classLikeInfo.getCorrespondingClassOrObject()));
    this.c = c;

    KtClassOrObject classOrObject = classLikeInfo.getCorrespondingClassOrObject();
    if (classOrObject != null) {
      this.c.getTrace().record(BindingContext.CLASS, classOrObject, this);
    }
    this.c
        .getTrace()
        .record(BindingContext.FQNAME_TO_CLASS_DESCRIPTOR, DescriptorUtils.getFqName(this), this);

    this.declarationProvider =
        c.getDeclarationProviderFactory().getClassMemberDeclarationProvider(classLikeInfo);

    StorageManager storageManager = c.getStorageManager();

    this.unsubstitutedMemberScope = createMemberScope(c, this.declarationProvider);
    this.kind = classLikeInfo.getClassKind();
    this.staticScope =
        kind == ClassKind.ENUM_CLASS
            ? new StaticScopeForKotlinEnum(storageManager, this)
            : MemberScope.Empty.INSTANCE;

    this.typeConstructor = new LazyClassTypeConstructor();

    this.isCompanionObject =
        classLikeInfo instanceof KtObjectInfo && ((KtObjectInfo) classLikeInfo).isCompanionObject();

    KtModifierList modifierList = classLikeInfo.getModifierList();
    if (kind.isSingleton()) {
      this.modality = Modality.FINAL;
    } else {
      Modality defaultModality = kind == ClassKind.INTERFACE ? Modality.ABSTRACT : Modality.FINAL;
      this.modality =
          resolveModalityFromModifiers(modifierList, defaultModality, /* allowSealed = */ true);
    }

    boolean isLocal = classOrObject != null && KtPsiUtil.isLocal(classOrObject);
    Visibility defaultVisibility;
    if (kind == ClassKind.ENUM_ENTRY || (kind == ClassKind.OBJECT && isCompanionObject)) {
      defaultVisibility = Visibilities.PUBLIC;
    } else {
      defaultVisibility = Visibilities.DEFAULT_VISIBILITY;
    }
    this.visibility =
        isLocal
            ? Visibilities.LOCAL
            : resolveVisibilityFromModifiers(modifierList, defaultVisibility);

    this.isInner = isInnerClass(modifierList) && !ModifiersChecker.isIllegalInner(this);
    this.isData = modifierList != null && modifierList.hasModifier(KtTokens.DATA_KEYWORD);

    // Annotation entries are taken from both own annotations (if any) and object literal
    // annotations (if any)
    List<KtAnnotationEntry> annotationEntries = new ArrayList<KtAnnotationEntry>();
    if (classOrObject != null && classOrObject.getParent() instanceof KtObjectLiteralExpression) {
      // TODO: it would be better to have separate ObjectLiteralDescriptor without so much magic
      annotationEntries.addAll(
          KtPsiUtilKt.getAnnotationEntries((KtObjectLiteralExpression) classOrObject.getParent()));
    }
    if (modifierList != null) {
      annotationEntries.addAll(modifierList.getAnnotationEntries());
    }
    if (!annotationEntries.isEmpty()) {
      this.annotations =
          new LazyAnnotations(
              new LazyAnnotationsContext(c.getAnnotationResolver(), storageManager, c.getTrace()) {
                @NotNull
                @Override
                public LexicalScope getScope() {
                  return getOuterScope();
                }
              },
              annotationEntries);
    } else {
      this.annotations = Annotations.Companion.getEMPTY();
    }

    List<KtAnnotationEntry> jetDanglingAnnotations = classLikeInfo.getDanglingAnnotations();
    if (jetDanglingAnnotations.isEmpty()) {
      this.danglingAnnotations = Annotations.Companion.getEMPTY();
    } else {
      this.danglingAnnotations =
          new LazyAnnotations(
              new LazyAnnotationsContext(c.getAnnotationResolver(), storageManager, c.getTrace()) {
                @NotNull
                @Override
                public LexicalScope getScope() {
                  return getScopeForMemberDeclarationResolution();
                }
              },
              jetDanglingAnnotations);
    }

    this.companionObjectDescriptor =
        storageManager.createNullableLazyValue(
            new Function0<LazyClassDescriptor>() {
              @Override
              public LazyClassDescriptor invoke() {
                return computeCompanionObjectDescriptor(getCompanionObjectIfAllowed());
              }
            });
    this.extraCompanionObjectDescriptors =
        storageManager.createMemoizedFunction(
            new Function1<KtObjectDeclaration, ClassDescriptor>() {
              @Override
              public ClassDescriptor invoke(KtObjectDeclaration companionObject) {
                return computeCompanionObjectDescriptor(companionObject);
              }
            });
    this.forceResolveAllContents =
        storageManager.createRecursionTolerantNullableLazyValue(
            new Function0<Void>() {
              @Override
              public Void invoke() {
                doForceResolveAllContents();
                return null;
              }
            },
            null);

    this.resolutionScopesSupport =
        new ClassResolutionScopesSupport(
            this,
            storageManager,
            new Function0<LexicalScope>() {
              @Override
              public LexicalScope invoke() {
                return getOuterScope();
              }
            });

    this.parameters =
        c.getStorageManager()
            .createLazyValue(
                new Function0<List<TypeParameterDescriptor>>() {
                  @Override
                  public List<TypeParameterDescriptor> invoke() {
                    KtClassLikeInfo classInfo = declarationProvider.getOwnerInfo();
                    KtTypeParameterList typeParameterList = classInfo.getTypeParameterList();
                    if (typeParameterList == null) return Collections.emptyList();

                    if (classInfo.getClassKind() == ClassKind.ENUM_CLASS) {
                      c.getTrace().report(TYPE_PARAMETERS_IN_ENUM.on(typeParameterList));
                    }

                    List<KtTypeParameter> typeParameters = typeParameterList.getParameters();
                    if (typeParameters.isEmpty()) return Collections.emptyList();

                    List<TypeParameterDescriptor> parameters =
                        new ArrayList<TypeParameterDescriptor>(typeParameters.size());

                    for (int i = 0; i < typeParameters.size(); i++) {
                      parameters.add(
                          new LazyTypeParameterDescriptor(
                              c, LazyClassDescriptor.this, typeParameters.get(i), i));
                    }

                    return parameters;
                  }
                });

    this.scopeForInitializerResolution =
        storageManager.createLazyValue(
            new Function0<LexicalScope>() {
              @Override
              public LexicalScope invoke() {
                return ClassResolutionScopesSupportKt.scopeForInitializerResolution(
                    LazyClassDescriptor.this,
                    createInitializerScopeParent(),
                    classLikeInfo.getPrimaryConstructorParameters());
              }
            });
  }
  public KotlinTypeInfo visitWhileExpression(
      KtWhileExpression expression,
      ExpressionTypingContext contextWithExpectedType,
      boolean isStatement) {
    if (!isStatement)
      return components.dataFlowAnalyzer.illegalStatementType(
          expression, contextWithExpectedType, facade);

    ExpressionTypingContext context =
        contextWithExpectedType
            .replaceExpectedType(NO_EXPECTED_TYPE)
            .replaceContextDependency(INDEPENDENT);
    // Preliminary analysis
    PreliminaryLoopVisitor loopVisitor = PreliminaryLoopVisitor.visitLoop(expression);
    context =
        context.replaceDataFlowInfo(
            loopVisitor.clearDataFlowInfoForAssignedLocalVariables(context.dataFlowInfo));

    KtExpression condition = expression.getCondition();
    // Extract data flow info from condition itself without taking value into account
    DataFlowInfo dataFlowInfo = checkCondition(context.scope, condition, context);

    KtExpression body = expression.getBody();
    KotlinTypeInfo bodyTypeInfo;
    DataFlowInfo conditionInfo =
        components
            .dataFlowAnalyzer
            .extractDataFlowInfoFromCondition(condition, true, context)
            .and(dataFlowInfo);
    if (body != null) {
      LexicalWritableScope scopeToExtend =
          newWritableScopeImpl(context, "Scope extended in while's condition");
      bodyTypeInfo =
          components.expressionTypingServices.getBlockReturnedTypeWithWritableScope(
              scopeToExtend,
              Collections.singletonList(body),
              CoercionStrategy.NO_COERCION,
              context.replaceDataFlowInfo(conditionInfo));
    } else {
      bodyTypeInfo = TypeInfoFactoryKt.noTypeInfo(conditionInfo);
    }

    // Condition is false at this point only if there is no jumps outside
    if (!containsJumpOutOfLoop(expression, context)) {
      dataFlowInfo =
          components
              .dataFlowAnalyzer
              .extractDataFlowInfoFromCondition(condition, false, context)
              .and(dataFlowInfo);
    }

    // Special case: while (true)
    // In this case we must record data flow information at the nearest break / continue and
    // .and it with entrance data flow information, because while body until break is executed at
    // least once in this case
    // See KT-6284
    if (body != null && KtPsiUtil.isTrueConstant(condition)) {
      // We should take data flow info from the first jump point,
      // but without affecting changing variables
      dataFlowInfo =
          dataFlowInfo.and(
              loopVisitor.clearDataFlowInfoForAssignedLocalVariables(
                  bodyTypeInfo.getJumpFlowInfo()));
    }
    return components
        .dataFlowAnalyzer
        .checkType(
            bodyTypeInfo.replaceType(components.builtIns.getUnitType()),
            expression,
            contextWithExpectedType)
        .replaceDataFlowInfo(dataFlowInfo);
  }
 @Override
 public <D> void acceptChildren(@NotNull KtVisitor<Void, D> visitor, D data) {
   KtPsiUtil.visitChildren(this, visitor, data);
 }