Search in sources :

Example 1 with WritableSlice

use of org.jetbrains.kotlin.util.slicedMap.WritableSlice in project kotlin by JetBrains.

the class BasicExpressionTypingVisitor method visitObjectLiteralExpression.

@Override
public KotlinTypeInfo visitObjectLiteralExpression(@NotNull final KtObjectLiteralExpression expression, final ExpressionTypingContext context) {
    final KotlinType[] result = new KotlinType[1];
    TemporaryBindingTrace temporaryTrace = TemporaryBindingTrace.create(context.trace, "trace to resolve object literal expression", expression);
    ObservableBindingTrace.RecordHandler<PsiElement, ClassDescriptor> handler = new ObservableBindingTrace.RecordHandler<PsiElement, ClassDescriptor>() {

        @Override
        public void handleRecord(WritableSlice<PsiElement, ClassDescriptor> slice, PsiElement declaration, final ClassDescriptor descriptor) {
            if (slice == CLASS && declaration == expression.getObjectDeclaration()) {
                KotlinType defaultType = components.wrappedTypeFactory.createRecursionIntolerantDeferredType(context.trace, new Function0<KotlinType>() {

                    @Override
                    public KotlinType invoke() {
                        return descriptor.getDefaultType();
                    }
                });
                result[0] = defaultType;
            }
        }
    };
    ObservableBindingTrace traceAdapter = new ObservableBindingTrace(temporaryTrace);
    traceAdapter.addHandler(CLASS, handler);
    // don't need to add classifier of object literal to any scope
    components.localClassifierAnalyzer.processClassOrObject(// don't need to add classifier of object literal to any scope
    null, context.replaceBindingTrace(traceAdapter).replaceContextDependency(INDEPENDENT), context.scope.getOwnerDescriptor(), expression.getObjectDeclaration());
    temporaryTrace.commit();
    DataFlowInfo resultFlowInfo = context.dataFlowInfo;
    for (KtSuperTypeListEntry specifier : expression.getObjectDeclaration().getSuperTypeListEntries()) {
        if (specifier instanceof KtSuperTypeCallEntry) {
            KtSuperTypeCallEntry delegator = (KtSuperTypeCallEntry) specifier;
            KotlinTypeInfo delegatorTypeInfo = context.trace.get(EXPRESSION_TYPE_INFO, delegator.getCalleeExpression());
            if (delegatorTypeInfo != null) {
                resultFlowInfo = resultFlowInfo.and(delegatorTypeInfo.getDataFlowInfo());
            }
        }
    }
    // Breaks are not possible inside constructor arguments, so jumpPossible or jumpFlowInfo are not necessary here
    KotlinTypeInfo resultTypeInfo = components.dataFlowAnalyzer.checkType(TypeInfoFactoryKt.createTypeInfo(result[0], resultFlowInfo), expression, context);
    // We have to record it here,
    // otherwise ExpressionTypingVisitorDispatcher records wrong information
    context.trace.record(EXPRESSION_TYPE_INFO, expression, resultTypeInfo);
    context.trace.record(PROCESSED, expression);
    return resultTypeInfo;
}
Also used : WritableSlice(org.jetbrains.kotlin.util.slicedMap.WritableSlice) PsiElement(com.intellij.psi.PsiElement) DataFlowInfo(org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo)

Aggregations

PsiElement (com.intellij.psi.PsiElement)1 DataFlowInfo (org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo)1 WritableSlice (org.jetbrains.kotlin.util.slicedMap.WritableSlice)1