Use other version of JS ast by develar: migrate code to using new ast.

Adapted from https://github.com/develar/kotlin/commit/d3521123a6e4d551b30743b68b6bb23b9678553b.
This commit is contained in:
Pavel V. Talanov
2012-08-07 13:38:34 +04:00
parent 17b85cbeed
commit 0a92843245
42 changed files with 238 additions and 425 deletions
@@ -16,8 +16,7 @@
package org.jetbrains.k2js.test.semantics;
import closurecompiler.internal.com.google.common.collect.Lists;
import com.google.dart.compiler.util.Maps;
import com.google.common.collect.Lists;
import junit.framework.Test;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.config.Config;
@@ -29,6 +28,7 @@ import org.jetbrains.k2js.test.config.TestConfigWithUnitTests;
import org.jetbrains.k2js.test.rhino.RhinoSystemOutputChecker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -101,6 +101,6 @@ public abstract class JsUnitTestBase extends MultipleFilesTranslationTest {
@Override
protected Map<String, Object> getRhinoTestVariables() throws Exception {
return Maps.<String, Object>create("jsTestReporter", JS_UNIT_TEST_REPORTER);
return Collections.<String, Object>singletonMap("jsTestReporter", JS_UNIT_TEST_REPORTER);
}
}
@@ -18,20 +18,19 @@ package org.jetbrains.k2js.generate;
import com.google.dart.compiler.backend.js.JsSourceGenerationVisitor;
import com.google.dart.compiler.backend.js.ast.JsProgram;
import com.google.dart.compiler.util.DefaultTextOutput;
import com.google.dart.compiler.util.TextOutputImpl;
import org.jetbrains.annotations.NotNull;
/**
* @author Pavel.Talanov
*/
public final class CodeGenerator {
private CodeGenerator() {
}
@NotNull
public static String generateProgramToString(@NotNull JsProgram program) {
DefaultTextOutput output = new DefaultTextOutput(false);
TextOutputImpl output = new TextOutputImpl();
JsSourceGenerationVisitor sourceGenerator = new JsSourceGenerationVisitor(output);
program.traverse(sourceGenerator, null);
return output.toString();
@@ -26,22 +26,22 @@ import static org.jetbrains.k2js.translate.utils.JsAstUtils.newVar;
public final class DynamicContext {
@NotNull
public static DynamicContext rootContext(@NotNull NamingScope rootScope, @NotNull JsBlock globalBlock) {
public static DynamicContext rootContext(@NotNull JsScope rootScope, @NotNull JsBlock globalBlock) {
return new DynamicContext(rootScope, globalBlock);
}
@NotNull
public static DynamicContext newContext(@NotNull NamingScope scope, @NotNull JsBlock block) {
public static DynamicContext newContext(@NotNull JsScope scope, @NotNull JsBlock block) {
return new DynamicContext(scope, block);
}
@NotNull
private final NamingScope currentScope;
private final JsScope currentScope;
@NotNull
private final JsBlock currentBlock;
private DynamicContext(@NotNull NamingScope scope, @NotNull JsBlock block) {
private DynamicContext(@NotNull JsScope scope, @NotNull JsBlock block) {
this.currentScope = scope;
this.currentBlock = block;
}
@@ -60,12 +60,7 @@ public final class DynamicContext {
}
@NotNull
public JsScope jsScope() {
return currentScope.jsScope();
}
@NotNull
public NamingScope getScope() {
public JsScope getScope() {
return currentScope;
}
@@ -17,20 +17,19 @@
package org.jetbrains.k2js.translate.context;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.setQualifier;
/**
* @author Pavel Talanov
* <p/>
* Encapuslates different types of constants and naming conventions.
*/
public final class Namer {
public static final String CALLEE_NAME = "$fun";
private static final String INITIALIZE_METHOD_NAME = "initialize";
private static final String CLASS_OBJECT_NAME = "createClass";
@@ -58,7 +57,7 @@ public final class Namer {
@NotNull
public static JsNameRef initializeMethodReference() {
return AstUtil.newQualifiedNameRef(INITIALIZE_METHOD_NAME);
return new JsNameRef(INITIALIZE_METHOD_NAME);
}
@NotNull
@@ -125,12 +124,6 @@ public final class Namer {
@NotNull
private final JsName isTypeName;
@NotNull
private final JsPropertyInitializer writablePropertyDescriptorField;
@NotNull
private final JsPropertyInitializer enumerablePropertyDescriptorField;
private Namer(@NotNull JsScope rootScope) {
kotlinName = rootScope.declareName(KOTLIN_OBJECT_NAME);
kotlinScope = new JsScope(rootScope, "Kotlin standard object");
@@ -142,10 +135,6 @@ public final class Namer {
objectName = kotlinScope.declareName(OBJECT_OBJECT_NAME);
isTypeName = kotlinScope.declareName("isType");
JsProgram program = rootScope.getProgram();
writablePropertyDescriptorField = new JsPropertyInitializer(program.getStringLiteral("writable"), program.getTrueLiteral());
enumerablePropertyDescriptorField = new JsPropertyInitializer(program.getStringLiteral("enumerable"), program.getTrueLiteral());
}
@NotNull
@@ -170,14 +159,12 @@ public final class Namer {
@NotNull
public JsExpression throwNPEFunctionCall() {
JsNameRef reference = AstUtil.newQualifiedNameRef(THROW_NPE_FUN_NAME);
JsInvocation invocation = AstUtil.newInvocation(reference);
return kotlin(invocation);
return new JsInvocation(new JsNameRef(THROW_NPE_FUN_NAME, kotlinObject()));
}
@NotNull
private JsExpression kotlin(@NotNull JsName name) {
return kotlin(name.makeRef());
private JsNameRef kotlin(@NotNull JsName name) {
return new JsNameRef(name, kotlinObject());
}
@NotNull
@@ -185,12 +172,6 @@ public final class Namer {
return kotlin(kotlinScope.declareName(name));
}
@NotNull
private JsExpression kotlin(@NotNull JsExpression reference) {
setQualifier(reference, kotlinObject());
return reference;
}
@NotNull
public JsNameRef kotlinObject() {
return kotlinName.makeRef();
@@ -201,16 +182,6 @@ public final class Namer {
return kotlin(isTypeName);
}
@NotNull
public JsPropertyInitializer writablePropertyDescriptorField() {
return writablePropertyDescriptorField;
}
@NotNull
public JsPropertyInitializer enumerablePropertyDescriptorField() {
return enumerablePropertyDescriptorField;
}
@NotNull
/*package*/ JsScope getKotlinScope() {
return kotlinScope;
@@ -225,4 +196,17 @@ public final class Namer {
return descriptor.getName().getName();
}
}
@NotNull
public JsInvocation classCreateInvocation(@NotNull ClassDescriptor descriptor) {
switch (descriptor.getKind()) {
case TRAIT:
return new JsInvocation(traitCreationMethodReference());
case OBJECT:
return new JsInvocation(objectCreationMethodReference());
default:
return new JsInvocation(classCreationMethodReference());
}
}
}
@@ -1,94 +0,0 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.k2js.translate.context;
import com.google.dart.compiler.backend.js.ast.JsName;
import com.google.dart.compiler.backend.js.ast.JsScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.translate.utils.JsAstUtils;
/**
* @author Pavel Talanov
* <p/>
* Basically a wrapper around JsScope.
*/
public final class NamingScope {
@NotNull
public static NamingScope rootScope(@NotNull JsScope rootScope) {
return new NamingScope(rootScope);
}
@NotNull
private final JsScope scope;
private NamingScope(@NotNull JsScope correspondingScope) {
this.scope = correspondingScope;
}
@NotNull
public NamingScope innerScope(@NotNull String scopeName) {
JsScope innerJsScope = new JsScope(jsScope(), scopeName);
return innerScope(innerJsScope);
}
@SuppressWarnings("MethodMayBeStatic")
@NotNull
public NamingScope innerScope(@NotNull JsScope correspondingScope) {
return new NamingScope(correspondingScope);
}
@NotNull
/*package*/ JsName declareUnobfuscatableName(@NotNull String name) {
JsName declaredName = scope.declareName(name);
declaredName.setObfuscatable(false);
return declaredName;
}
@NotNull
/*package*/ JsName declareObfuscatableName(@NotNull String name) {
return scope.declareName(obfuscateName(name));
}
//TODO: temporary solution
@NotNull
private String obfuscateName(@NotNull String name) {
int obfuscate = 0;
String result = name;
while (true) {
JsName existingNameWithSameIdent = this.scope.findExistingName(result);
boolean isDuplicate = (existingNameWithSameIdent != null) && JsAstUtils.ownsName(this.scope, existingNameWithSameIdent);
if (!isDuplicate) break;
result = name + "$" + obfuscate;
obfuscate++;
}
return result;
}
@NotNull
public JsName declareTemporary() {
return scope.declareTemporary();
}
@NotNull
public JsScope jsScope() {
return scope;
}
}
@@ -132,7 +132,6 @@ public final class StandardClasses {
private void declareTopLevelObjectInScope(@NotNull JsScope scope, @NotNull Map<FqNameUnsafe, JsName> map,
@NotNull FqNameUnsafe fullQualifiedName, @NotNull String name) {
JsName declaredName = scope.declareName(name);
declaredName.setObfuscatable(false);
map.put(fullQualifiedName, declaredName);
scopeMap.put(fullQualifiedName, new JsScope(scope, "scope for " + name));
}
@@ -147,9 +146,7 @@ public final class StandardClasses {
JsScope classScope = scopeMap.get(fullQualifiedClassName);
assert classScope != null;
FqNameUnsafe fullQualifiedMethodName = fullQualifiedClassName.child(Name.guess(shortMethodName));
JsName declaredName = classScope.declareName(javascriptName);
declaredName.setObfuscatable(false);
standardObjects.put(fullQualifiedMethodName, declaredName);
standardObjects.put(fullQualifiedMethodName, classScope.declareName(javascriptName));
}
private void declareMethods(@NotNull FqNameUnsafe classFQName,
@@ -46,12 +46,11 @@ public final class StaticContext {
public static StaticContext generateStaticContext(@NotNull BindingContext bindingContext, @NotNull EcmaVersion ecmaVersion) {
JsProgram program = new JsProgram("main");
JsRootScope jsRootScope = program.getRootScope();
Namer namer = Namer.newInstance(jsRootScope);
NamingScope scope = NamingScope.rootScope(jsRootScope);
JsRootScope rootScope = program.getRootScope();
Namer namer = Namer.newInstance(rootScope);
Intrinsics intrinsics = new Intrinsics();
StandardClasses standardClasses = StandardClasses.bindImplementations(namer.getKotlinScope());
return new StaticContext(program, bindingContext, namer, intrinsics, standardClasses, scope, ecmaVersion);
return new StaticContext(program, bindingContext, namer, intrinsics, standardClasses, rootScope, ecmaVersion);
}
@NotNull
@@ -69,18 +68,18 @@ public final class StaticContext {
private final StandardClasses standardClasses;
@NotNull
private final NamingScope rootScope;
private final JsScope rootScope;
@NotNull
private final Generator<JsName> names = new NameGenerator();
@NotNull
private final Generator<NamingScope> scopes = new ScopeGenerator();
private final Generator<JsScope> scopes = new ScopeGenerator();
@NotNull
private final Generator<JsNameRef> qualifiers = new QualifierGenerator();
@NotNull
private final Generator<Boolean> qualifierIsNull = new QualifierIsNullGenerator();
@NotNull
private final Map<NamingScope, JsFunction> scopeToFunction = Maps.newHashMap();
private final Map<JsScope, JsFunction> scopeToFunction = Maps.newHashMap();
@NotNull
private final EcmaVersion ecmaVersion;
@@ -88,7 +87,7 @@ public final class StaticContext {
//TODO: too many parameters in constructor
private StaticContext(@NotNull JsProgram program, @NotNull BindingContext bindingContext,
@NotNull Namer namer, @NotNull Intrinsics intrinsics,
@NotNull StandardClasses standardClasses, @NotNull NamingScope rootScope, @NotNull EcmaVersion ecmaVersion) {
@NotNull StandardClasses standardClasses, @NotNull JsScope rootScope, @NotNull EcmaVersion ecmaVersion) {
this.program = program;
this.bindingContext = bindingContext;
this.namer = namer;
@@ -128,22 +127,22 @@ public final class StaticContext {
}
@NotNull
public NamingScope getRootScope() {
public JsScope getRootScope() {
return rootScope;
}
@NotNull
public NamingScope getScopeForDescriptor(@NotNull DeclarationDescriptor descriptor) {
NamingScope namingScope = scopes.get(descriptor.getOriginal());
public JsScope getScopeForDescriptor(@NotNull DeclarationDescriptor descriptor) {
JsScope namingScope = scopes.get(descriptor.getOriginal());
assert namingScope != null : "Must have a scope for descriptor";
return namingScope;
}
@NotNull
public JsFunction getFunctionWithScope(@NotNull CallableDescriptor descriptor) {
NamingScope scope = getScopeForDescriptor(descriptor);
JsScope scope = getScopeForDescriptor(descriptor);
JsFunction function = scopeToFunction.get(scope);
assert scope.jsScope().equals(function.getScope()) : "Inconsistency.";
assert scope.equals(function.getScope()) : "Inconsistency.";
return function;
}
@@ -156,11 +155,11 @@ public final class StaticContext {
private final class NameGenerator extends Generator<JsName> {
private JsName declareName(DeclarationDescriptor descriptor, String name) {
NamingScope scope = getEnclosingScope(descriptor);
JsScope scope = getEnclosingScope(descriptor);
// ecma 5 property name never declares as obfuscatable:
// 1) property cannot be overloaded, so, name collision is not possible
// 2) main reason: if property doesn't have any custom accessor, value holder will have the same name as accessor, so, the same name will be declared more than once
return isEcma5() ? scope.declareUnobfuscatableName(name) : scope.declareObfuscatableName(name);
return isEcma5() ? scope.declareName(name) : scope.declareFreshName(name);
}
public NameGenerator() {
@@ -183,18 +182,18 @@ public final class StaticContext {
}
String name = Namer.generateNamespaceName(descriptor);
return getRootScope().declareUnobfuscatableName(name);
return getRootScope().declareName(name);
}
};
Rule<JsName> memberDeclarationsInsideParentsScope = new Rule<JsName>() {
@Override
@Nullable
public JsName apply(@NotNull DeclarationDescriptor descriptor) {
NamingScope namingScope = getEnclosingScope(descriptor);
JsScope namingScope = getEnclosingScope(descriptor);
if (descriptor instanceof ClassDescriptor) {
return namingScope.declareUnobfuscatableName(descriptor.getName().getName());
return namingScope.declareName(descriptor.getName().getName());
}
return namingScope.declareObfuscatableName(descriptor.getName().getName());
return namingScope.declareFreshName(descriptor.getName().getName());
}
};
Rule<JsName> constructorHasTheSameNameAsTheClass = new Rule<JsName>() {
@@ -232,7 +231,7 @@ public final class StaticContext {
}
String name = getNameForAnnotatedObject(descriptor, annotation);
name = (name != null) ? name : descriptor.getName().getName();
return getEnclosingScope(descriptor).declareUnobfuscatableName(name);
return getEnclosingScope(descriptor).declareName(name);
}
return null;
}
@@ -263,7 +262,7 @@ public final class StaticContext {
return null;
}
if (((FunctionDescriptor) descriptor).getValueParameters().isEmpty()) {
return getEnclosingScope(descriptor).declareUnobfuscatableName("toString");
return getEnclosingScope(descriptor).declareName("toString");
}
return null;
}
@@ -296,18 +295,18 @@ public final class StaticContext {
}
@NotNull
private NamingScope getEnclosingScope(@NotNull DeclarationDescriptor descriptor) {
private JsScope getEnclosingScope(@NotNull DeclarationDescriptor descriptor) {
DeclarationDescriptor containingDeclaration = getContainingDeclaration(descriptor);
return getScopeForDescriptor(containingDeclaration.getOriginal());
}
private final class ScopeGenerator extends Generator<NamingScope> {
private final class ScopeGenerator extends Generator<JsScope> {
public ScopeGenerator() {
Rule<NamingScope> generateNewScopesForClassesWithNoAncestors = new Rule<NamingScope>() {
Rule<JsScope> generateNewScopesForClassesWithNoAncestors = new Rule<JsScope>() {
@Override
public NamingScope apply(@NotNull DeclarationDescriptor descriptor) {
public JsScope apply(@NotNull DeclarationDescriptor descriptor) {
if (!(descriptor instanceof ClassDescriptor)) {
return null;
}
@@ -317,9 +316,9 @@ public final class StaticContext {
return null;
}
};
Rule<NamingScope> generateInnerScopesForDerivedClasses = new Rule<NamingScope>() {
Rule<JsScope> generateInnerScopesForDerivedClasses = new Rule<JsScope>() {
@Override
public NamingScope apply(@NotNull DeclarationDescriptor descriptor) {
public JsScope apply(@NotNull DeclarationDescriptor descriptor) {
if (!(descriptor instanceof ClassDescriptor)) {
return null;
}
@@ -330,9 +329,9 @@ public final class StaticContext {
return getScopeForDescriptor(superclass).innerScope("Scope for class " + descriptor.getName());
}
};
Rule<NamingScope> generateNewScopesForNamespaceDescriptors = new Rule<NamingScope>() {
Rule<JsScope> generateNewScopesForNamespaceDescriptors = new Rule<JsScope>() {
@Override
public NamingScope apply(@NotNull DeclarationDescriptor descriptor) {
public JsScope apply(@NotNull DeclarationDescriptor descriptor) {
if (!(descriptor instanceof NamespaceDescriptor)) {
return null;
}
@@ -340,22 +339,22 @@ public final class StaticContext {
}
};
//TODO: never get there
Rule<NamingScope> generateInnerScopesForMembers = new Rule<NamingScope>() {
Rule<JsScope> generateInnerScopesForMembers = new Rule<JsScope>() {
@Override
public NamingScope apply(@NotNull DeclarationDescriptor descriptor) {
NamingScope enclosingScope = getEnclosingScope(descriptor);
public JsScope apply(@NotNull DeclarationDescriptor descriptor) {
JsScope enclosingScope = getEnclosingScope(descriptor);
return enclosingScope.innerScope("Scope for member " + descriptor.getName());
}
};
Rule<NamingScope> createFunctionObjectsForCallableDescriptors = new Rule<NamingScope>() {
Rule<JsScope> createFunctionObjectsForCallableDescriptors = new Rule<JsScope>() {
@Override
public NamingScope apply(@NotNull DeclarationDescriptor descriptor) {
public JsScope apply(@NotNull DeclarationDescriptor descriptor) {
if (!(descriptor instanceof CallableDescriptor)) {
return null;
}
NamingScope enclosingScope = getEnclosingScope(descriptor);
JsFunction correspondingFunction = JsAstUtils.createFunctionWithEmptyBody(enclosingScope.jsScope());
NamingScope newScope = enclosingScope.innerScope(correspondingFunction.getScope());
JsScope enclosingScope = getEnclosingScope(descriptor);
JsFunction correspondingFunction = JsAstUtils.createFunctionWithEmptyBody(enclosingScope);
JsScope newScope = correspondingFunction.getScope();
assert (!scopeToFunction.containsKey(newScope)) : "Scope to function value overridden for " + descriptor;
scopeToFunction.put(newScope, correspondingFunction);
return newScope;
@@ -19,8 +19,8 @@ package org.jetbrains.k2js.translate.context;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsName;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.translate.utils.JsAstUtils;
/**
* @author Pavel Talanov
@@ -37,7 +37,7 @@ public final class TemporaryVariable {
/*package*/ TemporaryVariable(@NotNull JsName temporaryName, @NotNull JsExpression initExpression) {
this.variableName = temporaryName;
this.initExpression = initExpression;
this.assignmentExpression = AstUtil.newAssignment(variableName.makeRef(), this.initExpression);
this.assignmentExpression = JsAstUtils.assignment(variableName.makeRef(), this.initExpression);
}
@NotNull
@@ -36,8 +36,7 @@ import static org.jetbrains.k2js.translate.utils.BindingUtils.getDescriptorForEl
* <p/>
* All the info about the state of the translation process.
*/
public final class TranslationContext {
public class TranslationContext {
@NotNull
private final DynamicContext dynamicContext;
@NotNull
@@ -71,8 +70,12 @@ public final class TranslationContext {
aliasingContext = context;
}
public DynamicContext dynamicContext() {
return dynamicContext;
}
@NotNull
public TranslationContext contextWithScope(@NotNull NamingScope newScope, @NotNull JsBlock block) {
public TranslationContext contextWithScope(@NotNull JsScope newScope, @NotNull JsBlock block) {
return new TranslationContext(staticContext, DynamicContext.newContext(newScope, block), aliasingContext);
}
@@ -89,7 +92,7 @@ public final class TranslationContext {
//TODO: consider passing a function here
@NotNull
public TranslationContext innerContextWithGivenScopeAndBlock(@NotNull JsScope scope, @NotNull JsBlock block) {
return contextWithScope(dynamicContext.getScope().innerScope(scope), block);
return contextWithScope(scope, block);
}
@NotNull
@@ -128,7 +131,7 @@ public final class TranslationContext {
}
@NotNull
public NamingScope getScopeForDescriptor(@NotNull DeclarationDescriptor descriptor) {
public JsScope getScopeForDescriptor(@NotNull DeclarationDescriptor descriptor) {
return staticContext.getScopeForDescriptor(descriptor);
}
@@ -173,8 +176,8 @@ public final class TranslationContext {
}
@NotNull
public JsScope jsScope() {
return dynamicContext.jsScope();
public JsScope scope() {
return dynamicContext.getScope();
}
@NotNull
@@ -17,7 +17,6 @@
package org.jetbrains.k2js.translate.declaration;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import gnu.trove.THashMap;
import gnu.trove.TLinkable;
import gnu.trove.TLinkableAdaptor;
@@ -35,7 +34,6 @@ import org.jetbrains.k2js.translate.utils.JsAstUtils;
import java.util.ArrayList;
import java.util.List;
import static com.google.dart.compiler.backend.js.ast.JsVars.JsVar;
import static org.jetbrains.k2js.translate.general.Translation.translateClassDeclaration;
import static org.jetbrains.k2js.translate.utils.BindingUtils.getClassDescriptor;
import static org.jetbrains.k2js.translate.utils.ErrorReportingUtils.message;
@@ -59,13 +57,13 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
private final JsFunction dummyFunction;
private final JsName declarationsObject;
private final JsVar classesVar;
private final JsVars.JsVar classesVar;
public ClassDeclarationTranslator(@NotNull TranslationContext context) {
super(context);
dummyFunction = new JsFunction(context.jsScope());
declarationsObject = context().jsScope().declareName(Namer.nameForClassesVariable());
dummyFunction = new JsFunction(context.scope());
declarationsObject = context().scope().declareName(Namer.nameForClassesVariable());
classesVar = new JsVars.JsVar(declarationsObject);
}
@@ -131,7 +129,7 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
generateFinalClassDeclarations(vars, propertyInitializers);
if (vars.isEmpty()) {
classesVar.setInitExpr(valueLiteral);
classesVar.setInitExpression(valueLiteral);
return;
}
@@ -139,7 +137,7 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
result.add(vars);
result.add(new JsReturn(valueLiteral));
dummyFunction.setBody(newBlock(result));
classesVar.setInitExpr(AstUtil.newInvocation(dummyFunction));
classesVar.setInitExpression(new JsInvocation(dummyFunction));
}
private void generateOpenClassDeclarations(@NotNull JsVars vars, @NotNull List<JsPropertyInitializer> propertyInitializers) {
@@ -175,7 +173,7 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
}
else {
assert item.label.getName() != null;
vars.add(new JsVar(item.label.getName(), definition));
vars.add(new JsVars.JsVar(item.label.getName(), definition));
value = item.label;
}
@@ -209,7 +207,7 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
qualifiedLabelRef.setQualifier(declarationsObject.makeRef());
JsExpression value;
if (context().isEcma5()) {
value = JsAstUtils.createDataDescriptor(qualifiedLabelRef, false, context());
value = JsAstUtils.createDataDescriptor(qualifiedLabelRef);
}
else {
value = qualifiedLabelRef;
@@ -17,7 +17,6 @@
package org.jetbrains.k2js.translate.declaration;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
@@ -39,7 +38,8 @@ import java.util.List;
import static com.google.dart.compiler.util.AstUtil.newSequence;
import static org.jetbrains.k2js.translate.utils.BindingUtils.getClassDescriptor;
import static org.jetbrains.k2js.translate.utils.BindingUtils.getPropertyDescriptorForConstructorParameter;
import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.*;
import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.findAncestorClass;
import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.getContainingClass;
import static org.jetbrains.k2js.translate.utils.PsiUtils.getPrimaryConstructorParameters;
import static org.jetbrains.k2js.translate.utils.TranslationUtils.getQualifiedReference;
import static org.jetbrains.k2js.translate.utils.TranslationUtils.getThisObject;
@@ -123,13 +123,13 @@ public final class ClassTranslator extends AbstractTranslator {
@NotNull
private JsInvocation classCreateMethodInvocation() {
if (isObject()) {
return AstUtil.newInvocation(context().namer().objectCreationMethodReference());
return new JsInvocation(context().namer().objectCreationMethodReference());
}
else if (isTrait() && !context().isEcma5()) {
return AstUtil.newInvocation(context().namer().traitCreationMethodReference());
return new JsInvocation(context().namer().traitCreationMethodReference());
}
else {
return AstUtil.newInvocation(context().namer().classCreationMethodReference());
return new JsInvocation(context().namer().classCreationMethodReference());
}
}
@@ -156,7 +156,7 @@ public final class ClassTranslator extends AbstractTranslator {
}
}
else if (context().isEcma5()) {
jsClassDeclaration.getArguments().add(context().program().getNullLiteral());
jsClassDeclaration.getArguments().add(JsLiteral.NULL);
}
propertyList.addAll(translatePropertiesAsConstructorParameters(classDeclarationContext));
@@ -172,7 +172,7 @@ public final class ClassTranslator extends AbstractTranslator {
List<JsExpression> expressions = jsClassDeclaration.getArguments();
if (context().isEcma5()) {
if (superClassReferences.isEmpty()) {
jsClassDeclaration.getArguments().add(context().program().getNullLiteral());
jsClassDeclaration.getArguments().add(JsLiteral.NULL);
return;
}
else if (superClassReferences.size() > 1) {
@@ -89,7 +89,7 @@ public final class DeclarationBodyVisitor extends TranslatorVisitor<List<JsPrope
if (context.isEcma5()) {
FunctionDescriptor descriptor = getFunctionDescriptor(context.bindingContext(), expression);
JsExpression methodBodyExpression = methodAsPropertyInitializer.getValueExpr();
methodAsPropertyInitializer.setValueExpr(JsAstUtils.createPropertyDataDescriptor(descriptor, methodBodyExpression, context));
methodAsPropertyInitializer.setValueExpr(JsAstUtils.createPropertyDataDescriptor(descriptor, methodBodyExpression));
}
return Collections.singletonList(methodAsPropertyInitializer);
}
@@ -17,11 +17,7 @@
package org.jetbrains.k2js.translate.declaration;
import com.google.common.collect.Lists;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import com.google.dart.compiler.backend.js.ast.JsObjectLiteral;
import com.google.dart.compiler.backend.js.ast.JsStatement;
import com.google.dart.compiler.util.AstUtil;
import com.google.dart.compiler.backend.js.ast.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.psi.JetFile;
@@ -87,10 +83,10 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
private void declarationStatements(@NotNull List<NamespaceTranslator> namespaceTranslators,
@NotNull List<JsStatement> statements) {
JsObjectLiteral objectLiteral = new JsObjectLiteral();
JsNameRef packageMapNameRef = context().jsScope().declareName("_").makeRef();
JsNameRef packageMapNameRef = context().scope().declareName("_").makeRef();
JsExpression packageMapValue;
if (context().isNotEcma3()) {
packageMapValue = AstUtil.newInvocation(JsAstUtils.CREATE_OBJECT, context().program().getNullLiteral(), objectLiteral);
packageMapValue = new JsInvocation(JsAstUtils.CREATE_OBJECT, JsLiteral.NULL, objectLiteral);
}
else {
packageMapValue = objectLiteral;
@@ -18,7 +18,6 @@ package org.jetbrains.k2js.translate.declaration;
import com.google.common.collect.Lists;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
@@ -81,7 +80,7 @@ public final class NamespaceTranslator extends AbstractTranslator {
JsExpression value = getNamespaceDeclaration();
if (context().isNotEcma3()) {
value = JsAstUtils.createDataDescriptor(value, false, context());
value = JsAstUtils.createDataDescriptor(value);
}
list.add(new JsPropertyInitializer(namespaceName.makeRef(), value));
@@ -113,7 +112,7 @@ public final class NamespaceTranslator extends AbstractTranslator {
@NotNull
private JsInvocation namespaceCreateMethodInvocation() {
return AstUtil.newInvocation(context().namer().packageDefinitionMethodReference());
return new JsInvocation(context().namer().packageDefinitionMethodReference());
}
private void addIfNeed(@NotNull List<JsPropertyInitializer> declarations, @NotNull List<JsExpression> expressions) {
@@ -122,7 +121,7 @@ public final class NamespaceTranslator extends AbstractTranslator {
expressions.add(newObjectLiteral(declarations));
}
else if (context().isNotEcma3()) {
expressions.add(context().program().getNullLiteral());
expressions.add(JsLiteral.NULL);
}
}
@@ -165,7 +165,7 @@ public final class PropertyTranslator extends AbstractTranslator {
private JsFunction generateDefaultSetterFunction(@NotNull PropertySetterDescriptor propertySetterDescriptor) {
JsFunction result = context().getFunctionObject(propertySetterDescriptor);
JsParameter defaultParameter =
new JsParameter(propertyAccessContext(propertySetterDescriptor).jsScope().declareTemporary());
new JsParameter(propertyAccessContext(propertySetterDescriptor).scope().declareTemporary());
JsStatement assignment = assignmentToBackingField(context(), property, defaultParameter.getName().makeRef()).makeStmt();
setParameters(result, defaultParameter);
result.getBody().getStatements().add(assignment);
@@ -51,48 +51,36 @@ import static org.jetbrains.k2js.translate.utils.mutator.LastExpressionMutator.m
* @author Pavel Talanov
*/
public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
@Override
@NotNull
public JsNode visitConstantExpression(@NotNull JetConstantExpression expression,
@NotNull TranslationContext context) {
CompileTimeConstant<?> compileTimeValue =
context.bindingContext().get(BindingContext.COMPILE_TIME_VALUE, expression);
CompileTimeConstant<?> compileTimeValue = context.bindingContext().get(BindingContext.COMPILE_TIME_VALUE, expression);
assert compileTimeValue != null;
if (compileTimeValue instanceof NullValue) {
return context.program().getNullLiteral();
return JsLiteral.NULL;
}
Object value = compileTimeValue.getValue();
if (value instanceof Integer) {
return context.program().getNumberLiteral((Integer) value);
if (value instanceof Integer || value instanceof Short || value instanceof Byte) {
return context.program().getNumberLiteral(((Number) value).intValue());
}
if (value instanceof Boolean) {
return context.program().getBooleanLiteral((Boolean) value);
else if (value instanceof Number) {
return context.program().getNumberLiteral(((Number) value).doubleValue());
}
else if (value instanceof Boolean) {
return JsLiteral.getBoolean((Boolean) value);
}
//TODO: test
if (value instanceof Float) {
return context.program().getNumberLiteral((Float) value);
}
if (value instanceof Double) {
return context.program().getNumberLiteral((Double) value);
}
if (value instanceof String) {
return context.program().getStringLiteral((String) value);
}
if (value instanceof Character) {
return context.program().getStringLiteral(value.toString());
}
if (value instanceof Byte) {
return context.program().getNumberLiteral((Byte) value);
}
if (value instanceof Short) {
return context.program().getNumberLiteral((Short) value);
}
if (value instanceof Long) {
throw new IllegalStateException(message(expression, "Unsupported long constant"));
}
throw new IllegalStateException(message(expression, "Unsupported constant expression"));
throw new AssertionError(message(expression, "Unsupported constant expression"));
}
@Override
@@ -164,7 +152,7 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
if (BindingUtils.isStatement(context.bindingContext(), expression)) {
return ifStatement;
}
TemporaryVariable result = context.declareTemporary(context.program().getNullLiteral());
TemporaryVariable result = context.declareTemporary(JsLiteral.NULL);
AssignToExpressionMutator saveResultToTemporaryMutator =
new AssignToExpressionMutator(result.reference());
JsNode mutatedIfStatement = mutateLastExpression(ifStatement, saveResultToTemporaryMutator);
@@ -185,9 +173,9 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
private JsIf translateAsIfStatement(@NotNull JetIfExpression expression,
@NotNull TranslationContext context) {
JsIf result = new JsIf();
result.setIfExpr(translateConditionExpression(expression.getCondition(), context));
result.setThenStmt(translateNullableExpressionAsNotNullStatement(expression.getThen(), context));
result.setElseStmt(translateElseAsStatement(expression, context));
result.setIfExpression(translateConditionExpression(expression.getCondition(), context));
result.setThenStatement(translateNullableExpressionAsNotNullStatement(expression.getThen(), context));
result.setElseStatement(translateElseAsStatement(expression, context));
return result;
}
@@ -111,7 +111,7 @@ public final class FunctionTranslator extends AbstractTranslator {
@NotNull
private TranslationContext getFunctionBodyContextForExtensionFunction() {
TranslationContext contextWithFunctionBodyBlock = getContextWithFunctionBodyBlock();
extensionFunctionReceiverName = contextWithFunctionBodyBlock.jsScope().declareName(Namer.getReceiverParameterName());
extensionFunctionReceiverName = contextWithFunctionBodyBlock.scope().declareName(Namer.getReceiverParameterName());
DeclarationDescriptor expectedReceiverDescriptor = getExpectedReceiverDescriptor(descriptor);
assert expectedReceiverDescriptor != null;
return contextWithFunctionBodyBlock.innerContextWithThisAliased(expectedReceiverDescriptor, extensionFunctionReceiverName);
@@ -175,8 +175,8 @@ public final class FunctionTranslator extends AbstractTranslator {
@NotNull
private JsExpression wrapInClosureCaptureExpression(@NotNull JsExpression wrappedExpression,
@NotNull ClosureContext closureContext) {
JsFunction dummyFunction = new JsFunction(context().jsScope());
JsInvocation dummyFunctionInvocation = AstUtil.newInvocation(dummyFunction);
JsFunction dummyFunction = new JsFunction(context().scope());
JsInvocation dummyFunctionInvocation = new JsInvocation(dummyFunction);
for (VariableDescriptor variableDescriptor : closureContext.getDescriptors()) {
dummyFunction.getParameters().add(new JsParameter(context().getNameForDescriptor(variableDescriptor)));
dummyFunctionInvocation.getArguments().add(ReferenceTranslator.translateAsLocalNameReference(variableDescriptor, context()));
@@ -185,7 +185,7 @@ public final class FunctionTranslator extends AbstractTranslator {
dummyFunctionInvocation.getArguments().add(aliasForContainingClassThis.initExpression());
}
}
dummyFunction.setBody(AstUtil.newBlock(new JsReturn(wrappedExpression)));
dummyFunction.setBody(new JsBlock(new JsReturn(wrappedExpression)));
return dummyFunctionInvocation;
}
@@ -17,7 +17,6 @@
package org.jetbrains.k2js.translate.expression;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
@@ -85,7 +84,7 @@ public final class PatternTranslator extends AbstractTranslator {
@NotNull
private JsExpression translateAsIsCheck(@NotNull JsExpression expressionToMatch,
@NotNull JetTypePattern pattern) {
JsInvocation isCheck = AstUtil.newInvocation(context().namer().isOperationReference(),
JsInvocation isCheck = new JsInvocation(context().namer().isOperationReference(),
expressionToMatch, getClassReference(pattern));
if (isNullable(pattern)) {
return addNullCheck(expressionToMatch, isCheck);
@@ -137,7 +136,7 @@ public final class PatternTranslator extends AbstractTranslator {
if (context().isEcma5()) {
if (expressionToMatchAgainst instanceof JsNumberLiteral ||
expressionToMatchAgainst instanceof JsStringLiteral ||
expressionToMatchAgainst instanceof JsBooleanLiteral) {
expressionToMatchAgainst instanceof JsLiteral.JsBooleanLiteral) {
JsNameRef valueOf = new JsNameRef("valueOf");
valueOf.setQualifier(expressionToMatch);
return and(valueOf, eq);
@@ -17,6 +17,7 @@
package org.jetbrains.k2js.translate.expression;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsInvocation;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
@@ -83,7 +84,7 @@ public final class StringTemplateTranslator extends AbstractTranslator {
JsExpression translatedExpression = Translation.translateAsExpression(entryExpression, context());
JsNameRef toString = AstUtil.newQualifiedNameRef("toString");
setQualifier(toString, translatedExpression);
append(AstUtil.newInvocation(toString));
append(new JsInvocation(toString));
}
@Override
@@ -91,7 +91,7 @@ public final class TryTranslator extends AbstractTranslator {
assert catchParameter != null : "Valid catch must have a parameter.";
JsName parameterName = context().getNameForElement(catchParameter);
JsCatch result = new JsCatch(context().jsScope(), parameterName.getIdent());
JsCatch result = new JsCatch(context().scope(), parameterName.getIdent());
result.setBody(translateCatchBody(catchClause));
return result;
}
@@ -16,9 +16,8 @@
package org.jetbrains.k2js.translate.expression;
import closurecompiler.internal.com.google.common.collect.Lists;
import com.google.common.collect.Lists;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.*;
@@ -62,7 +61,7 @@ public final class WhenTranslator extends AbstractTranslator {
JsExpression expressionToMatch = translateExpressionToMatch(whenExpression);
this.expressionToMatch = expressionToMatch != null ? context.declareTemporary(expressionToMatch) : null;
this.dummyCounter = context.declareTemporary(program().getNumberLiteral(0));
this.result = context.declareTemporary(program().getNullLiteral());
this.result = context.declareTemporary(JsLiteral.NULL);
}
@NotNull
@@ -92,11 +91,7 @@ public final class WhenTranslator extends AbstractTranslator {
@NotNull
private JsFor generateDummyFor() {
JsFor result = new JsFor();
result.setInitExpr(generateInitExpressions());
result.setIncrExpr(generateIncrementStatement());
result.setCondition(generateConditionStatement());
return result;
return new JsFor(generateInitExpressions(), generateIncrementStatement(), generateConditionStatement());
}
@NotNull
@@ -182,7 +177,7 @@ public final class WhenTranslator extends AbstractTranslator {
@NotNull
private static JsStatement addDummyBreakIfNeed(@NotNull JsStatement statement) {
return statement instanceof JsReturn ? statement : AstUtil.newBlock(statement, new JsBreak());
return statement instanceof JsReturn ? statement : new JsBlock(statement, new JsBreak());
}
@NotNull
@@ -18,7 +18,6 @@ package org.jetbrains.k2js.translate.expression.foreach;
import com.google.common.collect.Lists;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetForExpression;
@@ -92,7 +91,7 @@ public final class ArrayForTranslator extends ForTranslator {
JsArrayAccess arrayAccess = new JsArrayAccess(loopRange.reference(), index.reference());
JsStatement currentVar = newVar(parameterName, arrayAccess);
JsStatement realBody = translateOriginalBodyExpression();
return AstUtil.newBlock(currentVar, realBody);
return new JsBlock(currentVar, realBody);
}
@NotNull
@@ -17,7 +17,6 @@
package org.jetbrains.k2js.translate.expression.foreach;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
@@ -42,7 +41,7 @@ public final class IteratorForTranslator extends ForTranslator {
@NotNull
public static JsStatement doTranslate(@NotNull JetForExpression expression,
@NotNull TranslationContext context) {
@NotNull TranslationContext context) {
return (new IteratorForTranslator(expression, context).translate());
}
@@ -58,7 +57,7 @@ public final class IteratorForTranslator extends ForTranslator {
private JsBlock translate() {
JsBlock bodyBlock = generateCycleBody();
JsWhile cycle = new JsWhile(hasNextMethodInvocation(), bodyBlock);
return AstUtil.newBlock(iterator.assignmentExpression().makeStmt(), cycle);
return new JsBlock(iterator.assignmentExpression().makeStmt(), cycle);
}
//TODO: check whether complex logic with blocks is needed
@@ -94,7 +93,7 @@ public final class IteratorForTranslator extends ForTranslator {
@NotNull
private JsExpression translateMethodInvocation(@Nullable JsExpression receiver,
@NotNull CallableDescriptor descriptor) {
@NotNull CallableDescriptor descriptor) {
return CallBuilder.build(context())
.receiver(receiver)
.descriptor(descriptor)
@@ -18,7 +18,6 @@ package org.jetbrains.k2js.translate.expression.foreach;
import com.google.common.collect.Lists;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetForExpression;
@@ -81,15 +80,12 @@ public final class RangeForTranslator extends ForTranslator {
List<JsStatement> blockStatements = Lists.newArrayList();
blockStatements.add(temporariesInitialization(rangeExpression, incrVar, start, end).makeStmt());
blockStatements.add(generateForExpression());
return newBlock(blockStatements);
return new JsBlock(blockStatements);
}
@NotNull
private JsFor generateForExpression() {
JsFor result = new JsFor();
result.setInitVars(initExpression());
result.setCondition(getCondition());
result.setIncrExpr(getIncrExpression());
JsFor result = new JsFor(initExpression(), getCondition(), getIncrExpression());
result.setBody(translateOriginalBodyExpression());
return result;
}
@@ -109,15 +105,8 @@ public final class RangeForTranslator extends ForTranslator {
return addAssign(parameterName.makeRef(), incrVar.reference());
}
@NotNull
private JsExpression getField(@NotNull String fieldName) {
JsNameRef nameRef = AstUtil.newQualifiedNameRef(fieldName);
setQualifier(nameRef, rangeExpression.reference());
return nameRef;
}
@NotNull
private JsExpression callFunction(@NotNull String funName) {
return AstUtil.newInvocation(getField(funName));
return new JsInvocation(new JsNameRef(funName, rangeExpression.reference()));
}
}
@@ -16,8 +16,6 @@
package org.jetbrains.k2js.translate.general;
import com.google.dart.compiler.backend.js.JsNamer;
import com.google.dart.compiler.backend.js.JsPrettyNamer;
import com.google.dart.compiler.backend.js.ast.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -186,7 +184,6 @@ public final class Translation {
}
}
mayBeGenerateTests(files, config, rootBlock, context);
performSimpleNameMangling(context.program());
return context.program();
}
@@ -200,11 +197,6 @@ public final class Translation {
}
}
private static void performSimpleNameMangling(@NotNull JsProgram program) {
JsNamer namer = new JsPrettyNamer();
namer.exec(program);
}
//TODO: determine whether should throw exception
@Nullable
private static JsStatement generateCallToMain(@NotNull TranslationContext context, @NotNull Collection<JetFile> files,
@@ -229,8 +221,7 @@ public final class Translation {
private static void setArguments(@NotNull TranslationContext context, @NotNull List<String> arguments,
@NotNull JsInvocation translatedCall) {
JsArrayLiteral arrayLiteral = new JsArrayLiteral();
arrayLiteral.getExpressions().addAll(toStringLiteralList(arguments, context.program()));
JsArrayLiteral arrayLiteral = new JsArrayLiteral(toStringLiteralList(arguments, context.program()));
JsAstUtils.setArguments(translatedCall, Collections.<JsExpression>singletonList(arrayLiteral));
}
}
@@ -17,7 +17,6 @@
package org.jetbrains.k2js.translate.initializer;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor;
@@ -82,17 +81,15 @@ public final class ClassInitializerTranslator extends AbstractTranslator {
//TODO: can be problematic to maintain
if (context().isEcma5()) {
JsName ref = context().jsScope().declareName("$initializer");
JsName ref = context().scope().declareName("$initializer");
initializer.setName(ref);
JsInvocation call = new JsInvocation();
call.setQualifier(AstUtil.newNameRef(AstUtil.newNameRef(ref.makeRef(), "baseInitializer"), "call"));
call.getArguments().add(new JsThisRef());
JsInvocation call = new JsInvocation(new JsNameRef("call", new JsNameRef("baseInitializer", ref.makeRef())));
call.getArguments().add(JsLiteral.THIS);
call.getArguments().addAll(arguments);
initializerStatements.add(call.makeStmt());
}
else {
JsName superMethodName = context().jsScope().declareName(Namer.superMethodName());
superMethodName.setObfuscatable(false);
JsName superMethodName = context().scope().declareName(Namer.superMethodName());
initializerStatements.add(convertToStatement(newInvocation(thisQualifiedReference(superMethodName), arguments)));
}
}
@@ -38,8 +38,7 @@ public final class InitializerUtils {
@NotNull
public static JsPropertyInitializer generateInitializeMethod(@NotNull JsFunction initializerFunction) {
JsPropertyInitializer initializer = new JsPropertyInitializer();
initializer.setLabelExpr(Namer.initializeMethodReference());
JsPropertyInitializer initializer = new JsPropertyInitializer(Namer.initializeMethodReference());
initializer.setValueExpr(initializerFunction);
return initializer;
}
@@ -17,7 +17,6 @@
package org.jetbrains.k2js.translate.initializer;
import com.google.dart.compiler.backend.js.ast.JsFunction;
import com.google.dart.compiler.backend.js.ast.JsPropertyInitializer;
import com.google.dart.compiler.backend.js.ast.JsStatement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
@@ -43,7 +42,7 @@ public final class NamespaceInitializerTranslator {
@NotNull
public JsFunction generateInitializeMethod() {
JsFunction result = JsAstUtils.createFunctionWithEmptyBody(namespaceContext.jsScope());
JsFunction result = JsAstUtils.createFunctionWithEmptyBody(namespaceContext.scope());
TranslationContext namespaceInitializerContext
= namespaceContext.innerContextWithGivenScopeAndBlock(result.getScope(), result.getBody());
@@ -19,7 +19,6 @@ package org.jetbrains.k2js.translate.intrinsic.functions.factories;
import com.google.common.collect.Lists;
import com.google.dart.compiler.backend.js.ast.JsArrayAccess;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.name.Name;
@@ -29,6 +28,7 @@ import org.jetbrains.k2js.translate.intrinsic.functions.basic.BuiltInPropertyInt
import org.jetbrains.k2js.translate.intrinsic.functions.basic.CallStandardMethodIntrinsic;
import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic;
import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NamePredicate;
import org.jetbrains.k2js.translate.utils.JsAstUtils;
import java.util.List;
@@ -76,8 +76,8 @@ public final class ArrayFIF extends CompositeFIF {
assert arguments.size() == 2 : "Array set expression must have two arguments.";
JsExpression indexExpression = arguments.get(0);
JsExpression value = arguments.get(1);
JsArrayAccess arrayAccess = AstUtil.newArrayAccess(receiver, indexExpression);
return AstUtil.newAssignment(arrayAccess, value);
JsArrayAccess arrayAccess = new JsArrayAccess(receiver, indexExpression);
return JsAstUtils.assignment(arrayAccess, value);
}
};
@@ -20,12 +20,11 @@ import com.google.common.collect.Sets;
import com.google.dart.compiler.backend.js.ast.JsBinaryOperation;
import com.google.dart.compiler.backend.js.ast.JsBinaryOperator;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.util.AstUtil;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
import org.jetbrains.k2js.translate.context.TemporaryVariable;
import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic;
import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NamePredicate;
@@ -35,6 +34,7 @@ import java.util.Set;
import static org.jetbrains.jet.lang.types.expressions.OperatorConventions.*;
import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.assignment;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.subtract;
/**
@@ -86,9 +86,10 @@ public final class NumberConversionFIF extends CompositeFIF {
@NotNull TranslationContext context) {
assert receiver != null;
assert arguments.isEmpty();
TemporaryVariable toConvert = context.declareTemporary(receiver);
JsBinaryOperation fractional = new JsBinaryOperation(JsBinaryOperator.MOD, toConvert.reference(), context.program().getNumberLiteral(1));
return AstUtil.newSequence(toConvert.assignmentExpression(), subtract(toConvert.reference(), fractional));
JsNameRef toConvertReference = context.declareTemporary(null).reference();
JsBinaryOperation fractional =
new JsBinaryOperation(JsBinaryOperator.MOD, toConvertReference, context.program().getNumberLiteral(1));
return subtract(assignment(toConvertReference, receiver), fractional);
}
};
@NotNull
@@ -57,10 +57,9 @@ public enum PrimitiveBinaryOperationFIF implements FunctionIntrinsicFactory {
JsBinaryOperation rangeSize = sum(subtract(rangeEnd, rangeStart),
context.program().getNumberLiteral(1));
JsNameRef expr = AstUtil.newQualifiedNameRef("Kotlin.NumberRange");
HasArguments numberRangeConstructorInvocation = context.isEcma5() ? AstUtil.newInvocation(expr) : new JsNew(expr);
HasArguments numberRangeConstructorInvocation = context.isEcma5() ? new JsInvocation(expr) : new JsNew(expr);
//TODO: add tests and correct expression for reversed ranges.
JsBooleanLiteral isRangeReversed = context.program().getFalseLiteral();
setArguments(numberRangeConstructorInvocation, rangeStart, rangeSize, isRangeReversed);
setArguments(numberRangeConstructorInvocation, rangeStart, rangeSize, /*range is not reversed*/JsLiteral.FALSE);
return (JsExpression) numberRangeConstructorInvocation;
}
};
@@ -18,8 +18,8 @@ package org.jetbrains.k2js.translate.intrinsic.functions.factories;
import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import com.google.dart.compiler.backend.js.ast.JsArrayAccess;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
@@ -78,8 +78,8 @@ public enum TupleGetterFIF implements FunctionIntrinsicFactory {
@NotNull List<JsExpression> arguments,
@NotNull TranslationContext context) {
assert arguments.isEmpty() : "Tuple access expression should not have any arguments.";
return AstUtil.newArrayAccess(receiver, context.program().getNumberLiteral(elementIndex));
return new JsArrayAccess(receiver, context.program().getNumberLiteral(elementIndex));
}
};
}
}
}
@@ -16,10 +16,7 @@
package org.jetbrains.k2js.translate.operation;
import com.google.dart.compiler.backend.js.ast.JsBinaryOperation;
import com.google.dart.compiler.backend.js.ast.JsBinaryOperator;
import com.google.dart.compiler.backend.js.ast.JsConditional;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
@@ -121,7 +118,7 @@ public final class BinaryOperationTranslator extends AbstractTranslator {
private JsExpression translateAsElvisOperator(@NotNull JetBinaryExpression expression) {
JsExpression translatedLeft = translateLeftExpression(context(), expression);
JsExpression translatedRight = translateRightExpression(context(), expression);
JsBinaryOperation leftIsNotNull = JsAstUtils.inequality(translatedLeft, program().getNullLiteral());
JsBinaryOperation leftIsNotNull = JsAstUtils.inequality(translatedLeft, JsLiteral.NULL);
return new JsConditional(leftIsNotNull, translatedLeft, translatedRight);
}
@@ -18,7 +18,6 @@ package org.jetbrains.k2js.translate.reference;
import com.google.common.collect.Lists;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
@@ -102,7 +101,7 @@ public final class CallTranslator extends AbstractTranslator {
@NotNull
private JsExpression invokeCall() {
JsInvocation callMethodInvocation = generateCallMethodInvocation();
List<JsExpression> parameters = Lists.<JsExpression>newArrayList(context().program().getNullLiteral());
List<JsExpression> parameters = Lists.<JsExpression>newArrayList(JsLiteral.NULL);
parameters.addAll(arguments);
setArguments(callMethodInvocation, parameters);
return callMethodInvocation;
@@ -155,7 +154,7 @@ public final class CallTranslator extends AbstractTranslator {
@NotNull
private JsExpression createConstructorCallExpression(@NotNull JsExpression constructorReference) {
if (context().isEcma5() && !AnnotationsUtils.isNativeObject(resolvedCall.getCandidateDescriptor())) {
return AstUtil.newInvocation(constructorReference);
return new JsInvocation(constructorReference);
}
else {
return new JsNew(constructorReference);
@@ -43,9 +43,8 @@ public enum CallType {
assert receiver != null;
TemporaryVariable temporaryVariable = context.declareTemporary(receiver);
JsBinaryOperation notNullCheck = TranslationUtils.notNullCheck(context, temporaryVariable.reference());
JsNullLiteral nullLiteral = context.program().getNullLiteral();
JsExpression methodCall = constructor.construct(temporaryVariable.reference());
JsConditional callMethodIfNotNullElseNull = new JsConditional(notNullCheck, methodCall, nullLiteral);
JsConditional callMethodIfNotNullElseNull = new JsConditional(notNullCheck, methodCall, JsNullLiteral.NULL);
return newSequence(temporaryVariable.assignmentExpression(), callMethodIfNotNullElseNull);
}
},
@@ -17,10 +17,7 @@
package org.jetbrains.k2js.translate.reference;
import com.google.common.collect.Maps;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsName;
import com.google.dart.compiler.backend.js.ast.JsNode;
import com.google.dart.compiler.backend.js.ast.JsReturn;
import com.google.dart.compiler.backend.js.ast.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
@@ -79,7 +76,7 @@ public final class InlinedCallExpressionTranslator extends AbstractCallExpressio
TranslationContext contextWithAllParametersAliased = createContextForInlining();
JsNode translatedBody = translateFunctionBody(getFunctionDescriptor(), getFunctionBody(), contextWithAllParametersAliased);
//TODO: declare uninitialized temporary
TemporaryVariable temporaryVariable = contextWithAllParametersAliased.declareTemporary(program().getNullLiteral());
TemporaryVariable temporaryVariable = contextWithAllParametersAliased.declareTemporary(JsLiteral.NULL);
JsNode mutatedBody = LastExpressionMutator.mutateLastExpression(translatedBody, new InlineFunctionMutator(temporaryVariable));
context().addStatementToCurrentBlock(JsAstUtils.convertToBlock(mutatedBody));
return temporaryVariable.reference();
@@ -17,14 +17,13 @@
package org.jetbrains.k2js.translate.reference;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
import org.jetbrains.k2js.translate.context.TemporaryVariable;
import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.general.AbstractTranslator;
import org.jetbrains.k2js.translate.utils.JsAstUtils;
import java.util.Collections;
import java.util.List;
@@ -61,8 +60,7 @@ public final class ReferenceAccessTranslator extends AbstractTranslator implemen
@Override
@NotNull
public JsExpression translateAsSet(@NotNull JsExpression toSetTo) {
assert reference instanceof JsNameRef;
return AstUtil.newAssignment((JsNameRef) reference, toSetTo);
return JsAstUtils.assignment(reference, toSetTo);
}
@NotNull
@@ -16,14 +16,9 @@
package org.jetbrains.k2js.translate.test;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsFunction;
import com.google.dart.compiler.backend.js.ast.JsStringLiteral;
import com.google.dart.compiler.backend.js.ast.*;
import org.jetbrains.annotations.NotNull;
import static com.google.dart.compiler.util.AstUtil.newBlock;
import static com.google.dart.compiler.util.AstUtil.newInvocation;
/**
* @author Pavel Talanov
*/
@@ -32,9 +27,9 @@ public abstract class CommonUnitTester extends JSTester {
@Override
public void constructTestMethodInvocation(@NotNull JsExpression functionToTestCall,
@NotNull JsStringLiteral testName) {
JsFunction functionToTest = new JsFunction(getContext().jsScope());
functionToTest.setBody(newBlock(functionToTestCall.makeStmt()));
getBlock().getStatements().add(newInvocation(getTestMethodRef(), testName, functionToTest).makeStmt());
JsFunction functionToTest = new JsFunction(getContext().scope());
functionToTest.setBody(new JsBlock(functionToTestCall.makeStmt()));
getBlock().getStatements().add(new JsInvocation(getTestMethodRef(), testName, functionToTest).makeStmt());
}
@NotNull
@@ -17,8 +17,8 @@
package org.jetbrains.k2js.translate.utils;
import com.google.common.collect.Lists;
import com.google.dart.compiler.Source;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.common.SourceInfo;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -75,9 +75,9 @@ public final class ErrorReportingUtils {
List<JsExpression> list = Lists.newArrayList(expression);
list.addAll(arguments);
for (JsExpression value : arguments) {
Source source = value.getSource();
if (source != null) {
return "at " + source + " " + value.getSourceLine() + ":" + value.getSourceColumn();
SourceInfo info = value.getSourceInfo();
if (info != null) {
return "at " + info.getSource().getUri() + " " + info.getLine() + ":" + info.getColumn();
}
}
return "at unknown location";
@@ -18,7 +18,7 @@ package org.jetbrains.k2js.translate.utils;
import com.google.common.collect.Lists;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import com.intellij.util.SmartList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
@@ -26,7 +26,10 @@ import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
import org.jetbrains.k2js.translate.context.TranslationContext;
import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* @author Pavel Talanov
@@ -36,6 +39,10 @@ public final class JsAstUtils {
public static final JsNameRef CREATE_OBJECT = new JsNameRef("create");
private static final JsNameRef EMPTY_REF = new JsNameRef("");
private static final JsNameRef VALUE = new JsNameRef("value");
private static final JsPropertyInitializer WRITABLE = new JsPropertyInitializer(new JsNameRef("writable"), JsLiteral.TRUE);
private static final JsPropertyInitializer ENUMERABLE = new JsPropertyInitializer(new JsNameRef("enumerable"), JsLiteral.TRUE);
static {
JsNameRef globalObjectReference = new JsNameRef("Object");
DEFINE_PROPERTY.setQualifier(globalObjectReference);
@@ -78,7 +85,7 @@ public final class JsAstUtils {
public static JsNameRef thisQualifiedReference(@NotNull JsName name) {
JsNameRef result = name.makeRef();
result.setQualifier(new JsThisRef());
result.setQualifier(JsLiteral.THIS);
return result;
}
@@ -172,26 +179,13 @@ public final class JsAstUtils {
@NotNull
public static JsFor generateForExpression(@NotNull JsVars initExpression,
@NotNull JsExpression condition,
@NotNull JsExpression incrExpression,
@NotNull JsExpression incrementExpression,
@NotNull JsStatement body) {
JsFor result = new JsFor();
result.setInitVars(initExpression);
result.setCondition(condition);
result.setIncrExpr(incrExpression);
JsFor result = new JsFor(initExpression, condition, incrementExpression);
result.setBody(body);
return result;
}
public static boolean ownsName(@NotNull JsScope scope, @NotNull JsName name) {
Iterator<JsName> nameIterator = scope.getAllNames();
while (nameIterator.hasNext()) {
if (nameIterator.next() == name) {
return true;
}
}
return false;
}
@NotNull
public static JsObjectLiteral newObjectLiteral(@NotNull List<JsPropertyInitializer> propertyList) {
JsObjectLiteral jsObjectLiteral = new JsObjectLiteral();
@@ -201,13 +195,7 @@ public final class JsAstUtils {
@NotNull
public static JsVars newVar(@NotNull JsName name, @Nullable JsExpression expr) {
JsVars.JsVar var = new JsVars.JsVar(name);
if (expr != null) {
var.setInitExpr(expr);
}
JsVars vars = new JsVars();
vars.add(var);
return vars;
return new JsVars(new JsVars.JsVar(name, expr));
}
public static void addVarDeclaration(@NotNull JsBlock block, @NotNull JsVars vars) {
@@ -222,12 +210,6 @@ public final class JsAstUtils {
statementList.addAll(statements);
}
public static void setArguments(@NotNull JsInvocation invocation, @NotNull List<JsExpression> newArgs) {
List<JsExpression> arguments = invocation.getArguments();
assert arguments.isEmpty() : "Arguments already set.";
arguments.addAll(newArgs);
}
public static void setArguments(@NotNull HasArguments invocation, @NotNull List<JsExpression> newArgs) {
List<JsExpression> arguments = invocation.getArguments();
assert arguments.isEmpty() : "Arguments already set.";
@@ -280,7 +262,11 @@ public final class JsAstUtils {
@NotNull
public static List<JsExpression> toStringLiteralList(@NotNull List<String> strings, @NotNull JsProgram program) {
ArrayList<JsExpression> result = Lists.newArrayList();
if (strings.isEmpty()) {
return Collections.emptyList();
}
List<JsExpression> result = new SmartList<JsExpression>();
for (String str : strings) {
result.add(program.getStringLiteral(str));
}
@@ -291,36 +277,55 @@ public final class JsAstUtils {
public static JsInvocation definePropertyDataDescriptor(@NotNull PropertyDescriptor descriptor,
@NotNull JsExpression value,
@NotNull TranslationContext context) {
return AstUtil.newInvocation(DEFINE_PROPERTY, new JsThisRef(),
context.program().getStringLiteral(context.getNameForDescriptor(descriptor).getIdent()),
createPropertyDataDescriptor(descriptor.isVar(), descriptor, value, context));
return defineProperty(context.getNameForDescriptor(descriptor).getIdent(), createPropertyDataDescriptor(descriptor, value),
context);
}
@NotNull
public static JsInvocation defineProperty(@NotNull String name,
@NotNull JsObjectLiteral value,
@NotNull TranslationContext context) {
JsInvocation invocation = new JsInvocation(DEFINE_PROPERTY);
invocation.getArguments().add(JsLiteral.THIS);
invocation.getArguments().add(context.program().getStringLiteral(name));
invocation.getArguments().add(value);
return invocation;
}
@NotNull
public static JsObjectLiteral createPropertyDataDescriptor(@NotNull FunctionDescriptor descriptor,
@NotNull JsExpression value,
@NotNull TranslationContext context) {
return createPropertyDataDescriptor(descriptor.getModality().isOverridable(), descriptor, value, context);
@NotNull JsExpression value) {
return createPropertyDataDescriptor(descriptor.getModality().isOverridable(), descriptor, value);
}
@NotNull
public static JsObjectLiteral createDataDescriptor(@NotNull JsExpression value, boolean writable, @NotNull TranslationContext context) {
public static JsObjectLiteral createDataDescriptor(@NotNull JsExpression value) {
return createDataDescriptor(value, false);
}
@NotNull
public static JsObjectLiteral createDataDescriptor(@NotNull JsExpression value, boolean writable) {
JsObjectLiteral dataDescriptor = new JsObjectLiteral();
dataDescriptor.getPropertyInitializers().add(new JsPropertyInitializer(context.program().getStringLiteral("value"), value));
dataDescriptor.getPropertyInitializers().add(new JsPropertyInitializer(VALUE, value));
if (writable) {
dataDescriptor.getPropertyInitializers().add(context.namer().writablePropertyDescriptorField());
dataDescriptor.getPropertyInitializers().add(WRITABLE);
}
return dataDescriptor;
}
@NotNull
public static JsObjectLiteral createPropertyDataDescriptor(@NotNull PropertyDescriptor descriptor,
@NotNull JsExpression value) {
return createPropertyDataDescriptor(descriptor.isVar(), descriptor, value);
}
@NotNull
private static JsObjectLiteral createPropertyDataDescriptor(boolean writable,
@NotNull DeclarationDescriptor descriptor,
@NotNull JsExpression value,
@NotNull TranslationContext context) {
JsObjectLiteral dataDescriptor = createDataDescriptor(value, writable, context);
@NotNull JsExpression value) {
JsObjectLiteral dataDescriptor = createDataDescriptor(value, writable);
if (AnnotationsUtils.isEnumerable(descriptor)) {
dataDescriptor.getPropertyInitializers().add(context.namer().enumerablePropertyDescriptorField());
dataDescriptor.getPropertyInitializers().add(ENUMERABLE);
}
return dataDescriptor;
}
@@ -336,12 +341,7 @@ public final class JsAstUtils {
@NotNull
public static JsFunction createPackage(@NotNull List<JsStatement> to, @NotNull JsScope scope) {
JsFunction packageBlockFunction = createFunctionWithEmptyBody(scope);
JsInvocation packageBlockFunctionInvocation = encloseFunction(packageBlockFunction);
JsInvocation packageBlock = new JsInvocation();
packageBlock.setQualifier(packageBlockFunctionInvocation);
to.add(packageBlock.makeStmt());
to.add(new JsInvocation(EMPTY_REF, new JsInvocation(packageBlockFunction)).makeStmt());
return packageBlockFunction;
}
}
@@ -30,7 +30,6 @@ import org.jetbrains.k2js.translate.general.Translation;
import java.util.ArrayList;
import java.util.List;
import static com.google.dart.compiler.util.AstUtil.newAssignment;
import static org.jetbrains.k2js.translate.utils.BindingUtils.getFunctionDescriptorForOperationExpression;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.*;
import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.getDeclarationDescriptorForReceiver;
@@ -62,15 +61,14 @@ public final class TranslationUtils {
@NotNull
private static JsPropertyInitializer translateExtensionFunctionAsEcma5PropertyDescriptor(@NotNull JsFunction function,
@NotNull FunctionDescriptor descriptor, @NotNull TranslationContext context) {
JsObjectLiteral meta = JsAstUtils.createDataDescriptor(function, descriptor.getModality().isOverridable(), context);
JsObjectLiteral meta = JsAstUtils.createDataDescriptor(function, descriptor.getModality().isOverridable());
return new JsPropertyInitializer(context.getNameForDescriptor(descriptor).makeRef(), meta);
}
@NotNull
public static JsBinaryOperation notNullCheck(@NotNull TranslationContext context,
@NotNull JsExpression expressionToCheck) {
JsNullLiteral nullLiteral = context.program().getNullLiteral();
JsBinaryOperation notNull = inequality(expressionToCheck, nullLiteral);
JsBinaryOperation notNull = inequality(expressionToCheck, JsLiteral.NULL);
JsBinaryOperation notUndefined = inequality(expressionToCheck, UNDEFINED_LITERAL);
return and(notNull, notUndefined);
}
@@ -78,8 +76,7 @@ public final class TranslationUtils {
@NotNull
public static JsBinaryOperation isNullCheck(@NotNull TranslationContext context,
@NotNull JsExpression expressionToCheck) {
JsNullLiteral nullLiteral = context.program().getNullLiteral();
JsBinaryOperation isNull = equality(expressionToCheck, nullLiteral);
JsBinaryOperation isNull = equality(expressionToCheck, JsLiteral.NULL);
JsBinaryOperation isUndefined = equality(expressionToCheck, UNDEFINED_LITERAL);
return or(isNull, isUndefined);
}
@@ -105,7 +102,7 @@ public final class TranslationUtils {
public static JsNameRef backingFieldReference(@NotNull TranslationContext context,
@NotNull PropertyDescriptor descriptor) {
JsName backingFieldName = context.getNameForDescriptor(descriptor);
return qualified(backingFieldName, new JsThisRef());
return qualified(backingFieldName, JsLiteral.THIS);
}
@NotNull
@@ -113,7 +110,7 @@ public final class TranslationUtils {
@NotNull PropertyDescriptor descriptor,
@NotNull JsExpression assignTo) {
JsNameRef backingFieldReference = backingFieldReference(context, descriptor);
return newAssignment(backingFieldReference, assignTo);
return JsAstUtils.assignment(backingFieldReference, assignTo);
}
@Nullable
@@ -158,7 +155,7 @@ public final class TranslationUtils {
return alias.makeRef();
}
}
return new JsThisRef();
return JsLiteral.THIS;
}
@NotNull
@@ -215,8 +212,8 @@ public final class TranslationUtils {
public static void defineModule(@NotNull TranslationContext context, @NotNull List<JsStatement> statements,
String moduleId) {
statements.add(AstUtil.newInvocation(context.namer().kotlin("defineModule"),
statements.add(new JsInvocation(context.namer().kotlin("defineModule"),
context.program().getStringLiteral(moduleId),
context.jsScope().declareName("_").makeRef()).makeStmt());
context.scope().declareName("_").makeRef()).makeStmt());
}
}
@@ -27,9 +27,8 @@ import static org.jetbrains.k2js.translate.utils.JsAstUtils.convertToStatement;
* @author Pavel Talanov
*/
public final class LastExpressionMutator {
public static JsNode mutateLastExpression(@NotNull JsNode node, @NotNull Mutator mutator) {
return (new LastExpressionMutator(mutator)).apply(node);
public static JsStatement mutateLastExpression(@NotNull JsNode node, @NotNull Mutator mutator) {
return convertToStatement(new LastExpressionMutator(mutator).apply(node));
}
@NotNull
@@ -62,10 +61,10 @@ public final class LastExpressionMutator {
@NotNull
private JsNode applyToIf(@NotNull JsIf node) {
node.setThenStmt(convertToStatement(apply(node.getThenStmt())));
JsStatement elseStmt = node.getElseStmt();
node.setThenStatement(convertToStatement(apply(node.getThenStatement())));
JsStatement elseStmt = node.getElseStatement();
if (elseStmt != null) {
node.setElseStmt(convertToStatement(apply(elseStmt)));
node.setElseStatement(convertToStatement(apply(elseStmt)));
}
return node;
}