Search in sources :

Example 1 with ExpressionCodegenExtension

use of org.jetbrains.kotlin.codegen.extensions.ExpressionCodegenExtension in project kotlin by JetBrains.

the class ImplementationBodyCodegen method generateSyntheticParts.

@Override
protected void generateSyntheticParts() {
    generatePropertyMetadataArrayFieldIfNeeded(classAsmType);
    generateFieldForSingleton();
    generateCompanionObjectBackingFieldCopies();
    generateTraitMethods();
    generateDelegates(delegationFieldsInfo);
    if (!isInterface(descriptor) || kind == OwnerKind.DEFAULT_IMPLS) {
        generateSyntheticAccessors();
    }
    generateEnumMethods();
    generateFunctionsForDataClasses();
    new CollectionStubMethodGenerator(typeMapper, descriptor).generate(functionCodegen, v);
    generateToArray();
    if (context.closure != null)
        genClosureFields(context.closure, v, typeMapper);
    for (ExpressionCodegenExtension extension : ExpressionCodegenExtension.Companion.getInstances(state.getProject())) {
        extension.generateClassSyntheticParts(this);
    }
}
Also used : ExpressionCodegenExtension(org.jetbrains.kotlin.codegen.extensions.ExpressionCodegenExtension)

Example 2 with ExpressionCodegenExtension

use of org.jetbrains.kotlin.codegen.extensions.ExpressionCodegenExtension in project kotlin by JetBrains.

the class ExpressionCodegen method visitSimpleNameExpression.

@Override
public StackValue visitSimpleNameExpression(@NotNull KtSimpleNameExpression expression, @NotNull StackValue receiver) {
    ResolvedCall<?> resolvedCall = CallUtilKt.getResolvedCall(expression, bindingContext);
    DeclarationDescriptor descriptor;
    if (resolvedCall == null) {
        descriptor = bindingContext.get(REFERENCE_TARGET, expression);
    } else {
        if (resolvedCall instanceof VariableAsFunctionResolvedCall) {
            VariableAsFunctionResolvedCall call = (VariableAsFunctionResolvedCall) resolvedCall;
            resolvedCall = call.getVariableCall();
        }
        descriptor = resolvedCall.getResultingDescriptor();
        //Check early if KCallableNameProperty is applicable to prevent closure generation
        StackValue intrinsicResult = applyIntrinsic(descriptor, KCallableNameProperty.class, resolvedCall, receiver);
        if (intrinsicResult != null)
            return intrinsicResult;
        receiver = StackValue.receiver(resolvedCall, receiver, this, null);
        if (descriptor instanceof FakeCallableDescriptorForObject) {
            descriptor = ((FakeCallableDescriptorForObject) descriptor).getReferencedDescriptor();
        }
    }
    assert descriptor != null : "Couldn't find descriptor for '" + expression.getText() + "'";
    descriptor = descriptor.getOriginal();
    boolean isSyntheticField = descriptor instanceof SyntheticFieldDescriptor;
    if (isSyntheticField) {
        descriptor = ((SyntheticFieldDescriptor) descriptor).getPropertyDescriptor();
    }
    StackValue intrinsicResult = applyIntrinsic(descriptor, IntrinsicPropertyGetter.class, resolvedCall, receiver);
    if (intrinsicResult != null)
        return intrinsicResult;
    if (descriptor instanceof PropertyDescriptor) {
        PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor;
        Collection<ExpressionCodegenExtension> codegenExtensions = ExpressionCodegenExtension.Companion.getInstances(state.getProject());
        if (!codegenExtensions.isEmpty() && resolvedCall != null) {
            ExpressionCodegenExtension.Context context = new ExpressionCodegenExtension.Context(typeMapper, v);
            KotlinType returnType = propertyDescriptor.getReturnType();
            for (ExpressionCodegenExtension extension : codegenExtensions) {
                if (returnType != null) {
                    StackValue value = extension.applyProperty(receiver, resolvedCall, context);
                    if (value != null)
                        return value;
                }
            }
        }
        boolean directToField = isSyntheticField && contextKind() != OwnerKind.DEFAULT_IMPLS;
        ClassDescriptor superCallTarget = resolvedCall == null ? null : getSuperCallTarget(resolvedCall.getCall());
        if (directToField) {
            receiver = StackValue.receiverWithoutReceiverArgument(receiver);
        }
        return intermediateValueForProperty(propertyDescriptor, directToField, directToField, superCallTarget, false, receiver, resolvedCall);
    }
    if (descriptor instanceof TypeAliasDescriptor) {
        ClassDescriptor classDescriptor = ((TypeAliasDescriptor) descriptor).getClassDescriptor();
        if (classDescriptor == null) {
            throw new IllegalStateException("Type alias " + descriptor + " static member refernece should be rejected by type checker, " + "since there is no class corresponding to this type alias.");
        }
        descriptor = classDescriptor;
    }
    if (descriptor instanceof ClassDescriptor) {
        ClassDescriptor classDescriptor = (ClassDescriptor) descriptor;
        if (isObject(classDescriptor)) {
            return StackValue.singleton(classDescriptor, typeMapper);
        }
        if (isEnumEntry(classDescriptor)) {
            return StackValue.enumEntry(classDescriptor, typeMapper);
        }
        ClassDescriptor companionObjectDescriptor = classDescriptor.getCompanionObjectDescriptor();
        if (companionObjectDescriptor != null) {
            return StackValue.singleton(companionObjectDescriptor, typeMapper);
        }
        return StackValue.none();
    }
    StackValue localOrCaptured = findLocalOrCapturedValue(descriptor);
    if (localOrCaptured != null) {
        return localOrCaptured;
    }
    throw new UnsupportedOperationException("don't know how to generate reference " + descriptor);
}
Also used : BindingContext(org.jetbrains.kotlin.resolve.BindingContext) SyntheticFieldDescriptor(org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor) FakeCallableDescriptorForObject(org.jetbrains.kotlin.resolve.calls.util.FakeCallableDescriptorForObject) SyntheticJavaPropertyDescriptor(org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor) KotlinType(org.jetbrains.kotlin.types.KotlinType) ExpressionCodegenExtension(org.jetbrains.kotlin.codegen.extensions.ExpressionCodegenExtension)

Example 3 with ExpressionCodegenExtension

use of org.jetbrains.kotlin.codegen.extensions.ExpressionCodegenExtension in project kotlin by JetBrains.

the class ExpressionCodegen method invokeFunction.

@NotNull
public StackValue invokeFunction(@NotNull Call call, @NotNull ResolvedCall<?> resolvedCall, @NotNull StackValue receiver) {
    ResolvedCallWithRealDescriptor callWithRealDescriptor = CoroutineCodegenUtilKt.replaceSuspensionFunctionWithRealDescriptor(resolvedCall, state.getProject(), state.getBindingContext());
    if (callWithRealDescriptor != null) {
        StackValue coroutineInstanceValueForSuspensionPoint = getCoroutineInstanceValueForSuspensionPoint(resolvedCall);
        StackValue coroutineInstanceValue = coroutineInstanceValueForSuspensionPoint != null ? coroutineInstanceValueForSuspensionPoint : getContinuationParameterFromEnclosingSuspendFunction(resolvedCall);
        tempVariables.put(callWithRealDescriptor.getFakeContinuationExpression(), coroutineInstanceValue);
        return invokeFunction(callWithRealDescriptor.getResolvedCall(), receiver);
    }
    FunctionDescriptor fd = accessibleFunctionDescriptor(resolvedCall);
    ClassDescriptor superCallTarget = getSuperCallTarget(call);
    fd = context.getAccessorForSuperCallIfNeeded(fd, superCallTarget, state);
    Collection<ExpressionCodegenExtension> codegenExtensions = ExpressionCodegenExtension.Companion.getInstances(state.getProject());
    if (!codegenExtensions.isEmpty()) {
        ExpressionCodegenExtension.Context context = new ExpressionCodegenExtension.Context(typeMapper, v);
        for (ExpressionCodegenExtension extension : codegenExtensions) {
            StackValue stackValue = extension.applyFunction(receiver, resolvedCall, context);
            if (stackValue != null)
                return stackValue;
        }
    }
    Callable callable = resolveToCallable(fd, superCallTarget != null, resolvedCall);
    return callable.invokeMethodWithArguments(resolvedCall, receiver, this);
}
Also used : BindingContext(org.jetbrains.kotlin.resolve.BindingContext) ExpressionCodegenExtension(org.jetbrains.kotlin.codegen.extensions.ExpressionCodegenExtension) ResolvedCallWithRealDescriptor(org.jetbrains.kotlin.codegen.coroutines.ResolvedCallWithRealDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ExpressionCodegenExtension (org.jetbrains.kotlin.codegen.extensions.ExpressionCodegenExtension)3 BindingContext (org.jetbrains.kotlin.resolve.BindingContext)2 NotNull (org.jetbrains.annotations.NotNull)1 ResolvedCallWithRealDescriptor (org.jetbrains.kotlin.codegen.coroutines.ResolvedCallWithRealDescriptor)1 SyntheticFieldDescriptor (org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor)1 FakeCallableDescriptorForObject (org.jetbrains.kotlin.resolve.calls.util.FakeCallableDescriptorForObject)1 SyntheticJavaPropertyDescriptor (org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1