Search in sources :

Example 1 with TransientReceiver

use of org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver in project kotlin by JetBrains.

the class DescriptorResolver method resolveValueParameterDescriptor.

@NotNull
public ValueParameterDescriptorImpl resolveValueParameterDescriptor(@NotNull final LexicalScope scope, @NotNull final FunctionDescriptor owner, @NotNull KtParameter valueParameter, int index, @NotNull final KotlinType type, @NotNull final BindingTrace trace) {
    KotlinType varargElementType = null;
    KotlinType variableType = type;
    if (valueParameter.hasModifier(VARARG_KEYWORD)) {
        varargElementType = type;
        variableType = getVarargParameterType(type);
    }
    KtModifierList modifierList = valueParameter.getModifierList();
    Annotations allAnnotations = annotationResolver.resolveAnnotationsWithoutArguments(scope, valueParameter.getModifierList(), trace);
    Annotations valueParameterAnnotations = Annotations.Companion.getEMPTY();
    if (modifierList != null) {
        if (valueParameter.hasValOrVar()) {
            AnnotationSplitter annotationSplitter = AnnotationSplitter.create(storageManager, allAnnotations, SetsKt.setOf(CONSTRUCTOR_PARAMETER));
            valueParameterAnnotations = annotationSplitter.getAnnotationsForTarget(CONSTRUCTOR_PARAMETER);
        } else {
            valueParameterAnnotations = allAnnotations;
        }
    }
    final KtDestructuringDeclaration destructuringDeclaration = valueParameter.getDestructuringDeclaration();
    Function0<List<VariableDescriptor>> destructuringVariables;
    if (destructuringDeclaration != null) {
        if (!languageVersionSettings.supportsFeature(LanguageFeature.DestructuringLambdaParameters)) {
            trace.report(Errors.UNSUPPORTED_FEATURE.on(valueParameter, TuplesKt.to(LanguageFeature.DestructuringLambdaParameters, languageVersionSettings)));
        }
        destructuringVariables = new Function0<List<VariableDescriptor>>() {

            @Override
            public List<VariableDescriptor> invoke() {
                assert owner.getDispatchReceiverParameter() == null : "Destructuring declarations are only be parsed for lambdas, and they must not have a dispatch receiver";
                LexicalScope scopeForDestructuring = ScopeUtilsKt.createScopeForDestructuring(scope, owner.getExtensionReceiverParameter());
                List<VariableDescriptor> result = destructuringDeclarationResolver.resolveLocalVariablesFromDestructuringDeclaration(scope, destructuringDeclaration, new TransientReceiver(type), /* initializer = */
                null, ExpressionTypingContext.newContext(trace, scopeForDestructuring, DataFlowInfoFactory.EMPTY, TypeUtils.NO_EXPECTED_TYPE));
                modifiersChecker.withTrace(trace).checkModifiersForDestructuringDeclaration(destructuringDeclaration);
                return result;
            }
        };
    } else {
        destructuringVariables = null;
    }
    Name parameterName;
    if (destructuringDeclaration == null) {
        // NB: val/var for parameter is only allowed in primary constructors where single underscore names are still prohibited.
        // The problem with val/var is that when lazy resolve try to find their descriptor, it searches through the member scope
        // of containing class where, it can not find a descriptor with special name.
        // Thus, to preserve behavior, we don't use a special name for val/var.
        parameterName = !valueParameter.hasValOrVar() && UnderscoreUtilKt.isSingleUnderscore(valueParameter) ? Name.special("<anonymous parameter " + index + ">") : KtPsiUtil.safeName(valueParameter.getName());
    } else {
        parameterName = Name.special("<name for destructuring parameter " + index + ">");
    }
    ValueParameterDescriptorImpl valueParameterDescriptor = ValueParameterDescriptorImpl.createWithDestructuringDeclarations(owner, null, index, valueParameterAnnotations, parameterName, variableType, valueParameter.hasDefaultValue(), valueParameter.hasModifier(CROSSINLINE_KEYWORD), valueParameter.hasModifier(NOINLINE_KEYWORD), varargElementType, KotlinSourceElementKt.toSourceElement(valueParameter), destructuringVariables);
    trace.record(BindingContext.VALUE_PARAMETER, valueParameter, valueParameterDescriptor);
    return valueParameterDescriptor;
}
Also used : CompositeAnnotations(org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations) Annotations(org.jetbrains.kotlin.descriptors.annotations.Annotations) TransientReceiver(org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver) AnnotationSplitter(org.jetbrains.kotlin.descriptors.annotations.AnnotationSplitter) FqName(org.jetbrains.kotlin.name.FqName) Name(org.jetbrains.kotlin.name.Name) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with TransientReceiver

use of org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver in project kotlin by JetBrains.

the class ForLoopConventionsChecker method checkConventionForIterator.

@Nullable
private KotlinType checkConventionForIterator(@NotNull ExpressionTypingContext context, @NotNull KtExpression loopRangeExpression, @NotNull KotlinType iteratorType, @NotNull Name name, @NotNull DiagnosticFactory1<KtExpression, KotlinType> ambiguity, @NotNull DiagnosticFactory1<KtExpression, KotlinType> missing, @NotNull DiagnosticFactory1<KtExpression, KotlinType> noneApplicable, @NotNull WritableSlice<KtExpression, ResolvedCall<FunctionDescriptor>> resolvedCallKey) {
    OverloadResolutionResults<FunctionDescriptor> nextResolutionResults = fakeCallResolver.resolveFakeCall(context, new TransientReceiver(iteratorType), name, loopRangeExpression, loopRangeExpression, FakeCallKind.OTHER, Collections.<KtExpression>emptyList());
    if (nextResolutionResults.isAmbiguity()) {
        context.trace.report(ambiguity.on(loopRangeExpression, iteratorType));
    } else if (nextResolutionResults.isNothing()) {
        context.trace.report(missing.on(loopRangeExpression, iteratorType));
    } else if (!nextResolutionResults.isSuccess()) {
        context.trace.report(noneApplicable.on(loopRangeExpression, iteratorType));
    } else {
        assert nextResolutionResults.isSuccess();
        ResolvedCall<FunctionDescriptor> resolvedCall = nextResolutionResults.getResultingCall();
        context.trace.record(resolvedCallKey, loopRangeExpression, resolvedCall);
        FunctionDescriptor functionDescriptor = resolvedCall.getResultingDescriptor();
        checkIfOperatorModifierPresent(loopRangeExpression, functionDescriptor, context.trace);
        return functionDescriptor.getReturnType();
    }
    return null;
}
Also used : TransientReceiver(org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver) FunctionDescriptor(org.jetbrains.kotlin.descriptors.FunctionDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with TransientReceiver

use of org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver in project kotlin by JetBrains.

the class ControlStructureTypingVisitor method visitForExpression.

public KotlinTypeInfo visitForExpression(KtForExpression 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, components.languageVersionSettings));
    KtExpression loopRange = expression.getLoopRange();
    KotlinType expectedParameterType = null;
    KotlinTypeInfo loopRangeInfo;
    if (loopRange != null) {
        ExpressionReceiver loopRangeReceiver = getExpressionReceiver(facade, loopRange, context.replaceScope(context.scope));
        loopRangeInfo = facade.getTypeInfo(loopRange, context);
        if (loopRangeReceiver != null) {
            expectedParameterType = components.forLoopConventionsChecker.checkIterableConvention(loopRangeReceiver, context);
        }
    } else {
        loopRangeInfo = TypeInfoFactoryKt.noTypeInfo(context);
    }
    LexicalWritableScope loopScope = newWritableScopeImpl(context, LexicalScopeKind.FOR, components.overloadChecker);
    KtParameter loopParameter = expression.getLoopParameter();
    if (loopParameter != null) {
        VariableDescriptor variableDescriptor = createLoopParameterDescriptor(loopParameter, expectedParameterType, context);
        ModifiersChecker.ModifiersCheckingProcedure modifiersCheckingProcedure = components.modifiersChecker.withTrace(context.trace);
        modifiersCheckingProcedure.checkModifiersForLocalDeclaration(loopParameter, variableDescriptor);
        components.identifierChecker.checkDeclaration(loopParameter, context.trace);
        loopScope.addVariableDescriptor(variableDescriptor);
        KtDestructuringDeclaration multiParameter = loopParameter.getDestructuringDeclaration();
        if (multiParameter != null) {
            KotlinType elementType = expectedParameterType == null ? ErrorUtils.createErrorType("Loop range has no type") : expectedParameterType;
            TransientReceiver iteratorNextAsReceiver = new TransientReceiver(elementType);
            components.annotationResolver.resolveAnnotationsWithArguments(loopScope, loopParameter.getModifierList(), context.trace);
            components.destructuringDeclarationResolver.defineLocalVariablesFromDestructuringDeclaration(loopScope, multiParameter, iteratorNextAsReceiver, loopRange, context);
            modifiersCheckingProcedure.checkModifiersForDestructuringDeclaration(multiParameter);
            components.identifierChecker.checkDeclaration(multiParameter, context.trace);
        }
    }
    KtExpression body = expression.getBody();
    KotlinTypeInfo bodyTypeInfo;
    if (body != null) {
        bodyTypeInfo = components.expressionTypingServices.getBlockReturnedTypeWithWritableScope(loopScope, Collections.singletonList(body), CoercionStrategy.NO_COERCION, context.replaceDataFlowInfo(loopRangeInfo.getDataFlowInfo()));
    } else {
        bodyTypeInfo = loopRangeInfo;
    }
    return components.dataFlowAnalyzer.checkType(bodyTypeInfo.replaceType(components.builtIns.getUnitType()), expression, contextWithExpectedType).replaceDataFlowInfo(loopRangeInfo.getDataFlowInfo());
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) ExpressionReceiver(org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver) LexicalWritableScope(org.jetbrains.kotlin.resolve.scopes.LexicalWritableScope) ModifiersChecker(org.jetbrains.kotlin.resolve.ModifiersChecker) TransientReceiver(org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver)

Example 4 with TransientReceiver

use of org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver in project kotlin by JetBrains.

the class KotlinTypeCheckerTest method assertType.

private void assertType(String contextType, String expression, String expectedType) {
    KotlinType thisType = makeType(contextType);
    ReceiverParameterDescriptorImpl receiverParameterDescriptor = new ReceiverParameterDescriptorImpl(scopeWithImports.getOwnerDescriptor(), new TransientReceiver(thisType));
    LexicalScope scope = new LexicalScopeImpl(scopeWithImports, scopeWithImports.getOwnerDescriptor(), false, receiverParameterDescriptor, LexicalScopeKind.SYNTHETIC);
    assertType(scope, expression, expectedType);
}
Also used : LexicalScope(org.jetbrains.kotlin.resolve.scopes.LexicalScope) LexicalScopeImpl(org.jetbrains.kotlin.resolve.scopes.LexicalScopeImpl) TransientReceiver(org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver) ReceiverParameterDescriptorImpl(org.jetbrains.kotlin.descriptors.impl.ReceiverParameterDescriptorImpl)

Aggregations

TransientReceiver (org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver)4 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)1 AnnotationSplitter (org.jetbrains.kotlin.descriptors.annotations.AnnotationSplitter)1 Annotations (org.jetbrains.kotlin.descriptors.annotations.Annotations)1 CompositeAnnotations (org.jetbrains.kotlin.descriptors.annotations.CompositeAnnotations)1 ReceiverParameterDescriptorImpl (org.jetbrains.kotlin.descriptors.impl.ReceiverParameterDescriptorImpl)1 FqName (org.jetbrains.kotlin.name.FqName)1 Name (org.jetbrains.kotlin.name.Name)1 ModifiersChecker (org.jetbrains.kotlin.resolve.ModifiersChecker)1 LexicalScope (org.jetbrains.kotlin.resolve.scopes.LexicalScope)1 LexicalScopeImpl (org.jetbrains.kotlin.resolve.scopes.LexicalScopeImpl)1 LexicalWritableScope (org.jetbrains.kotlin.resolve.scopes.LexicalWritableScope)1 ExpressionReceiver (org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1