Search in sources :

Example 6 with KtExpression

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

the class KotlinComponentUnwrapper method doUnwrap.

@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
    KtElement targetElement = (KtElement) element;
    KtExpression expressionToUnwrap = getExpressionToUnwrap(targetElement);
    assert expressionToUnwrap != null;
    KtElement enclosingElement = getEnclosingElement(targetElement);
    context.extractFromExpression(expressionToUnwrap, enclosingElement);
    context.delete(enclosingElement);
}
Also used : KtElement(org.jetbrains.kotlin.psi.KtElement) KtExpression(org.jetbrains.kotlin.psi.KtExpression)

Example 7 with KtExpression

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

the class PatternTranslator method translateIsExpression.

@NotNull
public JsExpression translateIsExpression(@NotNull KtIsExpression expression) {
    KtExpression left = expression.getLeftHandSide();
    JsExpression expressionToCheck = Translation.translateAsExpression(left, context());
    KotlinType leftType = context().bindingContext().getType(left);
    if (leftType != null && KotlinBuiltIns.isChar(leftType)) {
        expressionToCheck = JsAstUtils.charToBoxedChar(expressionToCheck);
    }
    KtTypeReference typeReference = expression.getTypeReference();
    assert typeReference != null;
    JsExpression result = translateIsCheck(expressionToCheck, typeReference);
    if (result == null)
        return JsLiteral.getBoolean(!expression.isNegated());
    if (expression.isNegated()) {
        return not(result);
    }
    return result;
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) KotlinType(org.jetbrains.kotlin.types.KotlinType) KtExpression(org.jetbrains.kotlin.psi.KtExpression) KtTypeReference(org.jetbrains.kotlin.psi.KtTypeReference) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with KtExpression

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

the class OverloadedAssignmentTranslator method overloadedMethodInvocation.

@NotNull
private JsExpression overloadedMethodInvocation(AccessTranslator accessTranslator) {
    JsBlock innerBlock = new JsBlock();
    TranslationContext innerContext = context().innerBlock(innerBlock);
    JsExpression oldValue = accessTranslator.translateAsGet();
    JsBlock argumentBlock = new JsBlock();
    TranslationContext argumentContext = innerContext.innerBlock(argumentBlock);
    KtExpression argumentPsi = expression.getRight();
    assert argumentPsi != null;
    JsExpression argument = Translation.translateAsExpression(argumentPsi, argumentContext);
    if (!argumentBlock.isEmpty()) {
        oldValue = innerContext.defineTemporary(oldValue);
        innerContext.addStatementsToCurrentBlockFrom(argumentBlock);
    }
    Map<KtExpression, JsExpression> aliases = new HashMap<KtExpression, JsExpression>();
    aliases.put(argumentPsi, argument);
    innerContext = innerContext.innerContextWithAliasesForExpressions(aliases);
    JsExpression result = CallTranslator.translate(innerContext, resolvedCall, oldValue);
    context().addStatementsToCurrentBlockFrom(innerBlock);
    return result;
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) HashMap(java.util.HashMap) KtExpression(org.jetbrains.kotlin.psi.KtExpression) JsBlock(org.jetbrains.kotlin.js.backend.ast.JsBlock) TranslationContext(org.jetbrains.kotlin.js.translate.context.TranslationContext) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with KtExpression

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

the class UnaryOperationTranslator method translate.

@NotNull
public static JsExpression translate(@NotNull KtUnaryExpression expression, @NotNull TranslationContext context) {
    IElementType operationToken = expression.getOperationReference().getReferencedNameElementType();
    if (operationToken == KtTokens.EXCLEXCL) {
        KtExpression baseExpression = getBaseExpression(expression);
        JsExpression translatedExpression = translateAsExpression(baseExpression, context);
        return sure(translatedExpression, context);
    }
    if (operationToken == KtTokens.MINUS) {
        KtExpression baseExpression = getBaseExpression(expression);
        if (baseExpression instanceof KtConstantExpression) {
            CompileTimeConstant<?> compileTimeValue = ConstantExpressionEvaluator.getConstant(expression, context.bindingContext());
            assert compileTimeValue != null : message(expression, "Expression is not compile time value: " + expression.getText() + " ");
            Object value = getCompileTimeValue(context.bindingContext(), expression, compileTimeValue);
            if (value instanceof Long) {
                return JsAstUtils.newLong((Long) value, context);
            }
        }
    }
    if (IncrementTranslator.isIncrement(operationToken)) {
        return IncrementTranslator.translate(expression, context);
    }
    JsExpression baseExpression = TranslationUtils.translateBaseExpression(context, expression);
    if (isExclForBinaryEqualLikeExpr(expression, baseExpression)) {
        return translateExclForBinaryEqualLikeExpr((JsBinaryOperation) baseExpression);
    }
    ResolvedCall<? extends FunctionDescriptor> resolvedCall = CallUtilKt.getFunctionResolvedCallWithAssert(expression, context.bindingContext());
    return CallTranslator.translate(context, resolvedCall, baseExpression);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) KtExpression(org.jetbrains.kotlin.psi.KtExpression) KtConstantExpression(org.jetbrains.kotlin.psi.KtConstantExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with KtExpression

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

the class ArrayAccessTranslator method getCached.

@NotNull
@Override
public AccessTranslator getCached() {
    Map<KtExpression, JsExpression> aliases = new HashMap<KtExpression, JsExpression>();
    JsExpression arrayExpression = context().cacheExpressionIfNeeded(getArrayExpression());
    aliases.put(expression.getArrayExpression(), arrayExpression);
    for (KtExpression ktExpression : expression.getIndexExpressions()) {
        JsExpression jsExpression = context().cacheExpressionIfNeeded(Translation.translateAsExpression(ktExpression, context()));
        aliases.put(ktExpression, jsExpression);
    }
    return new CachedArrayAccessTranslator(expression, context().innerContextWithAliasesForExpressions(aliases), arrayExpression);
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) HashMap(java.util.HashMap) KtExpression(org.jetbrains.kotlin.psi.KtExpression) 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