Search in sources :

Example 1 with TranslationContext

use of org.jetbrains.kotlin.js.translate.context.TranslationContext 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 2 with TranslationContext

use of org.jetbrains.kotlin.js.translate.context.TranslationContext in project kotlin by JetBrains.

the class WhenTranslator method translateOrCondition.

@NotNull
private JsExpression translateOrCondition(@NotNull JsExpression leftExpression, @NotNull KtWhenCondition condition, @NotNull TranslationContext context) {
    TranslationContext rightContext = context.innerBlock();
    JsExpression rightExpression = translateCondition(condition, rightContext);
    context.moveVarsFrom(rightContext);
    if (rightContext.currentBlockIsEmpty()) {
        return new JsBinaryOperation(JsBinaryOperator.OR, leftExpression, rightExpression);
    } else {
        assert rightExpression instanceof JsNameRef : "expected JsNameRef, but: " + rightExpression;
        JsNameRef result = (JsNameRef) rightExpression;
        JsIf ifStatement = JsAstUtils.newJsIf(leftExpression, JsAstUtils.assignment(result, JsLiteral.TRUE).makeStmt(), rightContext.getCurrentBlock());
        context.addStatementToCurrentBlock(ifStatement);
        return result;
    }
}
Also used : TranslationContext(org.jetbrains.kotlin.js.translate.context.TranslationContext) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with TranslationContext

use of org.jetbrains.kotlin.js.translate.context.TranslationContext in project kotlin by JetBrains.

the class WhenTranslator method translateRangeCondition.

@NotNull
private JsExpression translateRangeCondition(@NotNull KtWhenConditionInRange condition, @NotNull TranslationContext context) {
    KtExpression patternExpression = condition.getRangeExpression();
    assert patternExpression != null : "Expression pattern should have an expression: " + PsiUtilsKt.getTextWithLocation(condition);
    JsExpression expressionToMatch = getExpressionToMatch();
    assert expressionToMatch != null : "Range pattern is only available for 'when (C) { in ... }'  expressions: " + PsiUtilsKt.getTextWithLocation(condition);
    Map<KtExpression, JsExpression> subjectAliases = new HashMap<KtExpression, JsExpression>();
    subjectAliases.put(whenExpression.getSubjectExpression(), expressionToMatch);
    TranslationContext callContext = context.innerContextWithAliasesForExpressions(subjectAliases);
    boolean negated = condition.getOperationReference().getReferencedNameElementType() == KtTokens.NOT_IN;
    return new InOperationTranslator(callContext, expressionToMatch, condition.getRangeExpression(), condition.getOperationReference(), negated).translate();
}
Also used : HashMap(java.util.HashMap) TranslationContext(org.jetbrains.kotlin.js.translate.context.TranslationContext) InOperationTranslator(org.jetbrains.kotlin.js.translate.operation.InOperationTranslator) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with TranslationContext

use of org.jetbrains.kotlin.js.translate.context.TranslationContext in project kotlin by JetBrains.

the class Translation method doGenerateAst.

@NotNull
private static TranslationContext doGenerateAst(@NotNull BindingTrace bindingTrace, @NotNull Collection<KtFile> files, @NotNull MainCallParameters mainCallParameters, @NotNull ModuleDescriptor moduleDescriptor, @NotNull JsConfig config) {
    StaticContext staticContext = StaticContext.generateStaticContext(bindingTrace, config, moduleDescriptor);
    JsProgram program = staticContext.getProgram();
    JsName rootPackageName = program.getRootScope().declareName(Namer.getRootPackageName());
    JsFunction rootFunction = staticContext.getRootFunction();
    JsBlock rootBlock = rootFunction.getBody();
    List<JsStatement> statements = rootBlock.getStatements();
    program.getScope().declareName("_");
    TranslationContext context = TranslationContext.rootContext(staticContext, rootFunction);
    PackageDeclarationTranslator.translateFiles(files, context);
    staticContext.postProcess();
    statements.add(0, program.getStringLiteral("use strict").makeStmt());
    if (!staticContext.isBuiltinModule()) {
        defineModule(context, statements, config.getModuleId());
    }
    mayBeGenerateTests(files, config, rootBlock, context);
    rootFunction.getParameters().add(new JsParameter((rootPackageName)));
    // Invoke function passing modules as arguments
    // This should help minifier tool to recognize references to these modules as local variables and make them shorter.
    List<StaticContext.ImportedModule> importedModuleList = new ArrayList<StaticContext.ImportedModule>();
    for (StaticContext.ImportedModule importedModule : staticContext.getImportedModules()) {
        rootFunction.getParameters().add(new JsParameter(importedModule.getInternalName()));
        importedModuleList.add(importedModule);
    }
    if (mainCallParameters.shouldBeGenerated()) {
        JsStatement statement = generateCallToMain(context, files, mainCallParameters.arguments());
        if (statement != null) {
            statements.add(statement);
        }
    }
    statements.add(new JsReturn(rootPackageName.makeRef()));
    JsBlock block = program.getGlobalBlock();
    block.getStatements().addAll(wrapIfNecessary(config.getModuleId(), rootFunction, importedModuleList, program, config.getModuleKind()));
    return context;
}
Also used : ArrayList(java.util.ArrayList) TranslationContext(org.jetbrains.kotlin.js.translate.context.TranslationContext) StaticContext(org.jetbrains.kotlin.js.translate.context.StaticContext) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with TranslationContext

use of org.jetbrains.kotlin.js.translate.context.TranslationContext in project kotlin by JetBrains.

the class Translation method translateExpression.

@NotNull
public static JsNode translateExpression(@NotNull KtExpression expression, @NotNull TranslationContext context, @NotNull JsBlock block) {
    JsExpression aliasForExpression = context.aliasingContext().getAliasForExpression(expression);
    if (aliasForExpression != null) {
        return aliasForExpression;
    }
    CompileTimeConstant<?> compileTimeValue = ConstantExpressionEvaluator.getConstant(expression, context.bindingContext());
    if (compileTimeValue != null) {
        KotlinType type = context.bindingContext().getType(expression);
        if (type != null) {
            if (KotlinBuiltIns.isLong(type) || (KotlinBuiltIns.isInt(type) && expression instanceof KtUnaryExpression)) {
                JsExpression constantResult = translateConstant(compileTimeValue, expression, context);
                if (constantResult != null)
                    return constantResult;
            }
        }
    }
    TranslationContext innerContext = context.innerBlock();
    JsNode result = doTranslateExpression(expression, innerContext);
    context.moveVarsFrom(innerContext);
    block.getStatements().addAll(innerContext.dynamicContext().jsBlock().getStatements());
    return result;
}
Also used : KtUnaryExpression(org.jetbrains.kotlin.psi.KtUnaryExpression) KotlinType(org.jetbrains.kotlin.types.KotlinType) TranslationContext(org.jetbrains.kotlin.js.translate.context.TranslationContext) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

TranslationContext (org.jetbrains.kotlin.js.translate.context.TranslationContext)9 NotNull (org.jetbrains.annotations.NotNull)8 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)2 StaticContext (org.jetbrains.kotlin.js.translate.context.StaticContext)2 LinkedHashMap (java.util.LinkedHashMap)1 FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)1 ModuleDescriptor (org.jetbrains.kotlin.descriptors.ModuleDescriptor)1 JsBlock (org.jetbrains.kotlin.js.backend.ast.JsBlock)1 JsProgram (org.jetbrains.kotlin.js.backend.ast.JsProgram)1 CoroutineTransformer (org.jetbrains.kotlin.js.coroutine.CoroutineTransformer)1 InOperationTranslator (org.jetbrains.kotlin.js.translate.operation.InOperationTranslator)1 KtExpression (org.jetbrains.kotlin.psi.KtExpression)1 KtUnaryExpression (org.jetbrains.kotlin.psi.KtUnaryExpression)1 BindingTrace (org.jetbrains.kotlin.resolve.BindingTrace)1 Diagnostics (org.jetbrains.kotlin.resolve.diagnostics.Diagnostics)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1