Search in sources :

Example 6 with FunctionDescriptor

use of org.jetbrains.kotlin.descriptors.FunctionDescriptor 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 7 with FunctionDescriptor

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

the class JetTestFunctionDetector method getTestFunctions.

@NotNull
private static List<FunctionDescriptor> getTestFunctions(@NotNull BindingContext bindingContext, @NotNull List<KtDeclaration> declarations) {
    List<FunctionDescriptor> answer = Lists.newArrayList();
    for (KtDeclaration declaration : declarations) {
        MemberScope scope = null;
        if (declaration instanceof KtClass) {
            KtClass klass = (KtClass) declaration;
            ClassDescriptor classDescriptor = BindingUtils.getClassDescriptor(bindingContext, klass);
            if (classDescriptor.getModality() != Modality.ABSTRACT) {
                scope = classDescriptor.getDefaultType().getMemberScope();
            }
        }
        if (scope != null) {
            Collection<DeclarationDescriptor> allDescriptors = DescriptorUtils.getAllDescriptors(scope);
            List<FunctionDescriptor> testFunctions = ContainerUtil.mapNotNull(allDescriptors, new Function<DeclarationDescriptor, FunctionDescriptor>() {

                @Override
                public FunctionDescriptor fun(DeclarationDescriptor descriptor) {
                    if (descriptor instanceof FunctionDescriptor) {
                        FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor;
                        if (isTest(functionDescriptor))
                            return functionDescriptor;
                    }
                    return null;
                }
            });
            answer.addAll(testFunctions);
        }
    }
    return answer;
}
Also used : KtDeclaration(org.jetbrains.kotlin.psi.KtDeclaration) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) KtClass(org.jetbrains.kotlin.psi.KtClass) DeclarationDescriptor(org.jetbrains.kotlin.descriptors.DeclarationDescriptor) FunctionDescriptor(org.jetbrains.kotlin.descriptors.FunctionDescriptor) MemberScope(org.jetbrains.kotlin.resolve.scopes.MemberScope) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with FunctionDescriptor

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

the class Translation method generateCallToMain.

//TODO: determine whether should throw exception
@Nullable
private static JsStatement generateCallToMain(@NotNull TranslationContext context, @NotNull Collection<KtFile> files, @NotNull List<String> arguments) {
    MainFunctionDetector mainFunctionDetector = new MainFunctionDetector(context.bindingContext());
    KtNamedFunction mainFunction = mainFunctionDetector.getMainFunction(files);
    if (mainFunction == null) {
        return null;
    }
    FunctionDescriptor functionDescriptor = getFunctionDescriptor(context.bindingContext(), mainFunction);
    JsArrayLiteral argument = new JsArrayLiteral(toStringLiteralList(arguments, context.program()));
    return CallTranslator.INSTANCE.buildCall(context, functionDescriptor, Collections.singletonList(argument), null).makeStmt();
}
Also used : MainFunctionDetector(org.jetbrains.kotlin.idea.MainFunctionDetector) KtNamedFunction(org.jetbrains.kotlin.psi.KtNamedFunction) FunctionDescriptor(org.jetbrains.kotlin.descriptors.FunctionDescriptor) BindingUtils.getFunctionDescriptor(org.jetbrains.kotlin.js.translate.utils.BindingUtils.getFunctionDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with FunctionDescriptor

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

the class ArrayAccessTranslator method translateAsMethodCall.

@NotNull
private JsExpression translateAsMethodCall(@NotNull JsExpression arrayExpression, @Nullable JsExpression toSetTo) {
    boolean isGetter = toSetTo == null;
    TranslationContext context = context();
    ResolvedCall<FunctionDescriptor> resolvedCall = BindingUtils.getResolvedCallForArrayAccess(bindingContext(), expression, isGetter);
    if (!isGetter) {
        context = contextWithValueParameterAliasInArrayGetAccess(toSetTo);
    }
    return CallTranslator.translate(context, resolvedCall, arrayExpression);
}
Also used : TranslationContext(org.jetbrains.kotlin.js.translate.context.TranslationContext) FunctionDescriptor(org.jetbrains.kotlin.descriptors.FunctionDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with FunctionDescriptor

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

the class ArrayAccessTranslator method contextWithValueParameterAliasInArrayGetAccess.

// this is hack for a[b]++ -> a.set(b, a.get(b) + 1). Frontend generate fake expression for a.get(b) + 1.
@NotNull
private TranslationContext contextWithValueParameterAliasInArrayGetAccess(@NotNull JsExpression toSetTo) {
    ResolvedCall<FunctionDescriptor> resolvedCall = BindingUtils.getResolvedCallForArrayAccess(bindingContext(), expression, /*isGetter = */
    false);
    List<ResolvedValueArgument> arguments = resolvedCall.getValueArgumentsByIndex();
    if (arguments == null) {
        throw new IllegalStateException("Failed to arrange value arguments by index: " + resolvedCall.getResultingDescriptor());
    }
    ResolvedValueArgument lastArgument = arguments.get(arguments.size() - 1);
    assert lastArgument instanceof ExpressionValueArgument : "Last argument of array-like setter must be ExpressionValueArgument: " + lastArgument;
    ValueArgument valueArgument = ((ExpressionValueArgument) lastArgument).getValueArgument();
    assert valueArgument != null;
    KtExpression element = valueArgument.getArgumentExpression();
    return context().innerContextWithAliasesForExpressions(Collections.singletonMap(element, toSetTo));
}
Also used : ResolvedValueArgument(org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument) ExpressionValueArgument(org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument) ValueArgument(org.jetbrains.kotlin.psi.ValueArgument) ExpressionValueArgument(org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument) ResolvedValueArgument(org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument) KtExpression(org.jetbrains.kotlin.psi.KtExpression) FunctionDescriptor(org.jetbrains.kotlin.descriptors.FunctionDescriptor) NotNull(org.jetbrains.annotations.NotNull)

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