Search in sources :

Example 1 with FunctionDescriptor

use of org.jetbrains.kotlin.descriptors.FunctionDescriptor in project kotlin by JetBrains.

the class DestructuringDeclarationTranslator method translate.

private JsVars translate() {
    if (initializer != null) {
        context().getCurrentBlock().getStatements().add(JsAstUtils.newVar(multiObjectName, initializer));
    }
    List<JsVars.JsVar> jsVars = new ArrayList<JsVars.JsVar>();
    JsNameRef multiObjNameRef = multiObjectName.makeRef();
    for (KtDestructuringDeclarationEntry entry : multiDeclaration.getEntries()) {
        VariableDescriptor descriptor = BindingContextUtils.getNotNull(context().bindingContext(), BindingContext.VARIABLE, entry);
        // Do not call `componentX` for destructuring entry called _
        if (descriptor.getName().isSpecial())
            continue;
        ResolvedCall<FunctionDescriptor> entryInitCall = context().bindingContext().get(BindingContext.COMPONENT_RESOLVED_CALL, entry);
        assert entryInitCall != null : "Entry init call must be not null";
        JsExpression entryInitializer = CallTranslator.translate(context(), entryInitCall, multiObjNameRef);
        FunctionDescriptor candidateDescriptor = entryInitCall.getCandidateDescriptor();
        if (CallExpressionTranslator.shouldBeInlined(candidateDescriptor, context())) {
            setInlineCallMetadata(entryInitializer, entry, entryInitCall, context());
        }
        KotlinType returnType = candidateDescriptor.getReturnType();
        if (returnType != null && KotlinBuiltIns.isCharOrNullableChar(returnType) && !KotlinBuiltIns.isCharOrNullableChar(descriptor.getType())) {
            entryInitializer = JsAstUtils.charToBoxedChar(entryInitializer);
        }
        JsName name = context().getNameForDescriptor(descriptor);
        if (isVarCapturedInClosure(context().bindingContext(), descriptor)) {
            JsNameRef alias = getCapturedVarAccessor(name.makeRef());
            entryInitializer = JsAstUtils.wrapValue(alias, entryInitializer);
        }
        jsVars.add(new JsVars.JsVar(name, entryInitializer));
    }
    return new JsVars(jsVars, true);
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) KotlinType(org.jetbrains.kotlin.types.KotlinType) ArrayList(java.util.ArrayList) FunctionDescriptor(org.jetbrains.kotlin.descriptors.FunctionDescriptor) VariableDescriptor(org.jetbrains.kotlin.descriptors.VariableDescriptor) JsName(org.jetbrains.kotlin.js.backend.ast.JsName) JsNameRef(org.jetbrains.kotlin.js.backend.ast.JsNameRef) JsVars(org.jetbrains.kotlin.js.backend.ast.JsVars) KtDestructuringDeclarationEntry(org.jetbrains.kotlin.psi.KtDestructuringDeclarationEntry)

Example 2 with FunctionDescriptor

use of org.jetbrains.kotlin.descriptors.FunctionDescriptor in project kotlin by JetBrains.

the class InOperationTranslator method translateInt.

@Nullable
private JsExpression translateInt() {
    ResolvedCall<? extends CallableDescriptor> rightCall = CallUtilKt.getResolvedCallWithAssert(right, bindingContext());
    if (!(rightCall.getResultingDescriptor() instanceof FunctionDescriptor)) {
        return null;
    }
    FunctionDescriptor callDescriptor = (FunctionDescriptor) rightCall.getResultingDescriptor();
    if (!INT_RANGE_TEST.apply(callDescriptor)) {
        return null;
    }
    if (!(rightCall.getDispatchReceiver() instanceof ExpressionReceiver)) {
        return null;
    }
    KtExpression lower = ((ExpressionReceiver) rightCall.getDispatchReceiver()).getExpression();
    KtExpression upper = rightCall.getCall().getValueArguments().get(0).getArgumentExpression();
    assert upper != null : "Parse error occurred: " + PsiUtilsKt.getTextWithLocation(right);
    return translateInt(lower, upper);
}
Also used : ExpressionReceiver(org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver) FunctionDescriptor(org.jetbrains.kotlin.descriptors.FunctionDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with FunctionDescriptor

use of org.jetbrains.kotlin.descriptors.FunctionDescriptor in project kotlin by JetBrains.

the class KotlinOverloadTest method assertOverloadabilityRelation.

private void assertOverloadabilityRelation(String funA, String funB, boolean expectedIsError) {
    FunctionDescriptor a = makeFunction(funA);
    FunctionDescriptor b = makeFunction(funB);
    boolean aOverloadableWithB = overloadChecker.isOverloadable(a, b);
    assertEquals(expectedIsError, !aOverloadableWithB);
    boolean bOverloadableWithA = overloadChecker.isOverloadable(b, a);
    assertEquals(expectedIsError, !bOverloadableWithA);
}
Also used : FunctionDescriptor(org.jetbrains.kotlin.descriptors.FunctionDescriptor)

Example 4 with FunctionDescriptor

use of org.jetbrains.kotlin.descriptors.FunctionDescriptor in project kotlin by JetBrains.

the class TailRecursionCodegen method generateTailRecursion.

public void generateTailRecursion(ResolvedCall<?> resolvedCall) {
    CallableDescriptor fd = CoroutineCodegenUtilKt.unwrapInitialDescriptorForSuspendFunction(resolvedCall.getResultingDescriptor());
    assert fd instanceof FunctionDescriptor : "Resolved call doesn't refer to the function descriptor: " + fd;
    CallableMethod callable = (CallableMethod) codegen.resolveToCallable((FunctionDescriptor) fd, false, resolvedCall);
    List<ResolvedValueArgument> arguments = resolvedCall.getValueArgumentsByIndex();
    if (arguments == null) {
        throw new IllegalStateException("Failed to arrange value arguments by index: " + fd);
    }
    if (((FunctionDescriptor) fd).isSuspend()) {
        AsmUtil.pop(v, callable.getValueParameters().get(callable.getValueParameters().size() - 1).getAsmType());
    }
    assignParameterValues(fd, callable, arguments);
    if (callable.getExtensionReceiverType() != null) {
        if (resolvedCall.getExtensionReceiver() != fd.getExtensionReceiverParameter().getValue()) {
            StackValue expression = context.getReceiverExpression(codegen.typeMapper);
            expression.store(StackValue.onStack(callable.getExtensionReceiverType()), v, true);
        } else {
            AsmUtil.pop(v, callable.getExtensionReceiverType());
        }
    }
    if (callable.getDispatchReceiverType() != null) {
        AsmUtil.pop(v, callable.getDispatchReceiverType());
    }
    v.goTo(context.getMethodStartLabel());
}
Also used : FunctionDescriptor(org.jetbrains.kotlin.descriptors.FunctionDescriptor) CallableDescriptor(org.jetbrains.kotlin.descriptors.CallableDescriptor)

Example 5 with FunctionDescriptor

use of org.jetbrains.kotlin.descriptors.FunctionDescriptor in project kotlin by JetBrains.

the class ForLoopConventionsChecker method checkIterableConvention.

@Nullable
public KotlinType checkIterableConvention(@NotNull ExpressionReceiver loopRange, @NotNull ExpressionTypingContext context) {
    KtExpression loopRangeExpression = loopRange.getExpression();
    // Make a fake call loopRange.iterator(), and try to resolve it
    OverloadResolutionResults<FunctionDescriptor> iteratorResolutionResults = fakeCallResolver.resolveFakeCall(context, loopRange, OperatorNameConventions.ITERATOR, loopRangeExpression, loopRangeExpression, FakeCallKind.ITERATOR, Collections.<KtExpression>emptyList());
    if (!iteratorResolutionResults.isSuccess())
        return null;
    ResolvedCall<FunctionDescriptor> iteratorResolvedCall = iteratorResolutionResults.getResultingCall();
    context.trace.record(LOOP_RANGE_ITERATOR_RESOLVED_CALL, loopRangeExpression, iteratorResolvedCall);
    FunctionDescriptor iteratorFunction = iteratorResolvedCall.getResultingDescriptor();
    checkIfOperatorModifierPresent(loopRangeExpression, iteratorFunction, context.trace);
    KotlinType iteratorType = iteratorFunction.getReturnType();
    //noinspection ConstantConditions
    KotlinType hasNextType = checkConventionForIterator(context, loopRangeExpression, iteratorType, OperatorNameConventions.HAS_NEXT, HAS_NEXT_FUNCTION_AMBIGUITY, HAS_NEXT_MISSING, HAS_NEXT_FUNCTION_NONE_APPLICABLE, LOOP_RANGE_HAS_NEXT_RESOLVED_CALL);
    if (hasNextType != null && !builtIns.isBooleanOrSubtype(hasNextType)) {
        context.trace.report(HAS_NEXT_FUNCTION_TYPE_MISMATCH.on(loopRangeExpression, hasNextType));
    }
    return checkConventionForIterator(context, loopRangeExpression, iteratorType, OperatorNameConventions.NEXT, NEXT_AMBIGUITY, NEXT_MISSING, NEXT_NONE_APPLICABLE, LOOP_RANGE_NEXT_RESOLVED_CALL);
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) KtExpression(org.jetbrains.kotlin.psi.KtExpression) FunctionDescriptor(org.jetbrains.kotlin.descriptors.FunctionDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)14 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)4 ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)2 DeclarationDescriptor (org.jetbrains.kotlin.descriptors.DeclarationDescriptor)2 KtExpression (org.jetbrains.kotlin.psi.KtExpression)2 KotlinType (org.jetbrains.kotlin.types.KotlinType)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 CallableDescriptor (org.jetbrains.kotlin.descriptors.CallableDescriptor)1 ModuleDescriptor (org.jetbrains.kotlin.descriptors.ModuleDescriptor)1 SimpleFunctionDescriptor (org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor)1 VariableDescriptor (org.jetbrains.kotlin.descriptors.VariableDescriptor)1 MainFunctionDetector (org.jetbrains.kotlin.idea.MainFunctionDetector)1 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)1 JsName (org.jetbrains.kotlin.js.backend.ast.JsName)1 JsNameRef (org.jetbrains.kotlin.js.backend.ast.JsNameRef)1 JsVars (org.jetbrains.kotlin.js.backend.ast.JsVars)1 TranslationContext (org.jetbrains.kotlin.js.translate.context.TranslationContext)1 LocalFunctionCollector (org.jetbrains.kotlin.js.translate.expression.LocalFunctionCollector)1