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;
}
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;
}
}
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();
}
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;
}
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;
}
Aggregations