Search in sources :

Example 21 with KtExpression

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

the class VarargValueArgument method toString.

@Override
public String toString() {
    StringBuilder builder = new StringBuilder("vararg:{");
    for (Iterator<ValueArgument> iterator = arguments.iterator(); iterator.hasNext(); ) {
        ValueArgument valueArgument = iterator.next();
        KtExpression expression = valueArgument.getArgumentExpression();
        builder.append(expression == null ? "no expression" : expression.getText());
        if (iterator.hasNext()) {
            builder.append(", ");
        }
    }
    return builder.append("}").toString();
}
Also used : ValueArgument(org.jetbrains.kotlin.psi.ValueArgument) KtExpression(org.jetbrains.kotlin.psi.KtExpression)

Example 22 with KtExpression

use of org.jetbrains.kotlin.psi.KtExpression 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)

Example 23 with KtExpression

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

the class CallExpressionTranslator method translateJsCode.

@NotNull
private JsNode translateJsCode() {
    List<? extends ValueArgument> arguments = expression.getValueArguments();
    KtExpression argumentExpression = arguments.get(0).getArgumentExpression();
    assert argumentExpression != null;
    List<JsStatement> statements = parseJsCode(argumentExpression);
    int size = statements.size();
    JsNode node;
    if (size == 0) {
        node = JsLiteral.NULL;
    } else if (size > 1) {
        node = new JsBlock(statements);
    } else {
        JsStatement resultStatement = statements.get(0);
        if (resultStatement instanceof JsExpressionStatement) {
            node = ((JsExpressionStatement) resultStatement).getExpression();
        } else {
            node = resultStatement;
        }
    }
    LexicalScope lexicalScope = context().bindingContext().get(BindingContextSlicesJsKt.LEXICAL_SCOPE_FOR_JS, resolvedCall);
    Map<JsName, JsExpression> replacements = new HashMap<JsName, JsExpression>();
    if (lexicalScope != null) {
        Set<JsName> references = CollectUtilsKt.collectUsedNames(node);
        references.removeAll(CollectUtilsKt.collectDefinedNames(node));
        for (JsName name : references) {
            VariableDescriptor variable = getVariableByName(lexicalScope, Name.identifier(name.getIdent()));
            if (variable != null) {
                replacements.put(name, ReferenceTranslator.translateAsValueReference(variable, context()));
            }
        }
        if (!replacements.isEmpty()) {
            node = RewriteUtilsKt.replaceNames(node, replacements);
        }
    }
    return node;
}
Also used : LexicalScope(org.jetbrains.kotlin.resolve.scopes.LexicalScope) KtExpression(org.jetbrains.kotlin.psi.KtExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with KtExpression

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

the class PatternTranslator method translateCastExpression.

@NotNull
public JsExpression translateCastExpression(@NotNull KtBinaryExpressionWithTypeRHS expression) {
    assert isCastExpression(expression) : "Expected cast expression, got " + expression;
    KtExpression left = expression.getLeft();
    JsExpression expressionToCast = Translation.translateAsExpression(left, context());
    KtTypeReference typeReference = expression.getRight();
    assert typeReference != null : "Cast expression must have type reference";
    KotlinType leftType = context().bindingContext().getType(left);
    if (leftType != null && KotlinBuiltIns.isChar(leftType)) {
        expressionToCast = JsAstUtils.charToBoxedChar(expressionToCast);
    }
    TemporaryVariable temporary = context().declareTemporary(expressionToCast);
    JsExpression isCheck = translateIsCheck(temporary.assignmentExpression(), typeReference);
    if (isCheck == null)
        return expressionToCast;
    JsExpression onFail;
    if (isSafeCast(expression)) {
        onFail = JsLiteral.NULL;
    } else {
        JsExpression throwCCEFunRef = Namer.throwClassCastExceptionFunRef();
        onFail = new JsInvocation(throwCCEFunRef);
    }
    JsExpression result = new JsConditional(isCheck, temporary.reference(), onFail);
    KotlinType expressionType = context().bindingContext().getType(expression);
    if (expressionType != null && KotlinBuiltIns.isCharOrNullableChar(expressionType)) {
        result = JsAstUtils.boxedCharToChar(result);
    }
    return result;
}
Also used : TemporaryVariable(org.jetbrains.kotlin.js.translate.context.TemporaryVariable) JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) JsInvocation(org.jetbrains.kotlin.js.backend.ast.JsInvocation) JsConditional(org.jetbrains.kotlin.js.backend.ast.JsConditional) KotlinType(org.jetbrains.kotlin.types.KotlinType) KtExpression(org.jetbrains.kotlin.psi.KtExpression) KtTypeReference(org.jetbrains.kotlin.psi.KtTypeReference) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with KtExpression

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

the class OverloadedAssignmentTranslator method reassignment.

@NotNull
private JsExpression reassignment() {
    KtExpression left = expression.getLeft();
    assert left != null;
    AccessTranslator accessTranslator = AccessTranslationUtils.getAccessTranslator(left, context()).getCached();
    JsExpression newValue = overloadedMethodInvocation(accessTranslator);
    return accessTranslator.translateAsSet(newValue);
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) KtExpression(org.jetbrains.kotlin.psi.KtExpression) AccessTranslator(org.jetbrains.kotlin.js.translate.reference.AccessTranslator) NotNull(org.jetbrains.annotations.NotNull)

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