Search in sources :

Example 26 with KtExpression

use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.

the class FunctionBodyTranslator method setDefaultValueForArguments.

@NotNull
public static List<JsStatement> setDefaultValueForArguments(@NotNull FunctionDescriptor descriptor, @NotNull TranslationContext functionBodyContext) {
    List<ValueParameterDescriptor> valueParameters = descriptor.getValueParameters();
    List<JsStatement> result = new ArrayList<JsStatement>(valueParameters.size());
    for (ValueParameterDescriptor valueParameter : valueParameters) {
        if (!valueParameter.declaresDefaultValue())
            continue;
        JsNameRef jsNameRef = functionBodyContext.getNameForDescriptor(valueParameter).makeRef();
        KtExpression defaultArgument = getDefaultArgument(valueParameter);
        JsBlock defaultArgBlock = new JsBlock();
        JsExpression defaultValue = Translation.translateAsExpression(defaultArgument, functionBodyContext, defaultArgBlock);
        JsStatement assignStatement = assignment(jsNameRef, defaultValue).makeStmt();
        JsStatement thenStatement = JsAstUtils.mergeStatementInBlockIfNeeded(assignStatement, defaultArgBlock);
        JsBinaryOperation checkArgIsUndefined = equality(jsNameRef, Namer.getUndefinedExpression());
        JsIf jsIf = JsAstUtils.newJsIf(checkArgIsUndefined, thenStatement);
        result.add(jsIf);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) KtExpression(org.jetbrains.kotlin.psi.KtExpression) ValueParameterDescriptor(org.jetbrains.kotlin.descriptors.ValueParameterDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with KtExpression

use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.

the class CallBasedArgumentGenerator method generateExpression.

@Override
protected void generateExpression(int i, @NotNull ExpressionValueArgument argument) {
    ValueParameterDescriptor parameter = valueParameters.get(i);
    Type type = valueParameterTypes.get(i);
    ValueArgument valueArgument = argument.getValueArgument();
    assert valueArgument != null;
    KtExpression argumentExpression = valueArgument.getArgumentExpression();
    assert argumentExpression != null : valueArgument.asElement().getText();
    callGenerator.genValueAndPut(parameter, argumentExpression, type, i);
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) ValueArgument(org.jetbrains.kotlin.psi.ValueArgument) KtExpression(org.jetbrains.kotlin.psi.KtExpression) ValueParameterDescriptor(org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)

Example 28 with KtExpression

use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.

the class KotlinTypeCheckerTest method assertErrorType.

private void assertErrorType(String expression) {
    Project project = getProject();
    KtExpression ktExpression = KtPsiFactoryKt.KtPsiFactory(project).createExpression(expression);
    KotlinType type = expressionTypingServices.safeGetType(scopeWithImports, ktExpression, TypeUtils.NO_EXPECTED_TYPE, DataFlowInfoFactory.EMPTY, KotlinTestUtils.DUMMY_TRACE);
    assertTrue("Error type expected but " + type + " returned", type.isError());
}
Also used : Project(com.intellij.openapi.project.Project) KtExpression(org.jetbrains.kotlin.psi.KtExpression)

Aggregations

KtExpression (org.jetbrains.kotlin.psi.KtExpression)28 NotNull (org.jetbrains.annotations.NotNull)13 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)6 KotlinType (org.jetbrains.kotlin.types.KotlinType)5 Nullable (org.jetbrains.annotations.Nullable)4 ValueArgument (org.jetbrains.kotlin.psi.ValueArgument)4 Project (com.intellij.openapi.project.Project)3 TextRange (com.intellij.openapi.util.TextRange)3 ValueParameterDescriptor (org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)3 HashMap (java.util.HashMap)2 FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)2 KtElement (org.jetbrains.kotlin.psi.KtElement)2 KtParenthesizedExpression (org.jetbrains.kotlin.psi.KtParenthesizedExpression)2 KtTypeReference (org.jetbrains.kotlin.psi.KtTypeReference)2 ExpressionValueArgument (org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument)2 ResolvedValueArgument (org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument)2 Type (org.jetbrains.org.objectweb.asm.Type)2 PsiCodeFragment (com.intellij.psi.PsiCodeFragment)1 IElementType (com.intellij.psi.tree.IElementType)1 ParameterTableModelItemBase (com.intellij.refactoring.changeSignature.ParameterTableModelItemBase)1