From d8d4b95dd7bfcca07dc8edcc9c1b3b469fa67310 Mon Sep 17 00:00:00 2001 From: Pavel Talanov Date: Fri, 25 Nov 2011 19:31:43 +0400 Subject: [PATCH] Refactoring --- .idea/workspace.xml | 461 ++++++++++++------ .../declaration/PropertyTranslator.java | 18 +- .../expression/ExpressionVisitor.java | 28 +- .../translate/general/TranslationContext.java | 7 - .../translate/general/TranslatorVisitor.java | 14 - .../ClassInitializerTranslator.java | 11 +- .../translate/utils/TranslationUtils.java | 43 ++ .../org/jetbrains/k2js/utils/DiGraphNode.java | 1 + 8 files changed, 357 insertions(+), 226 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index da3a9e8925f..c9df3632798 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -8,12 +8,14 @@ - - - - - + + + + + + + @@ -182,72 +184,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -257,19 +193,93 @@ - - + + - + - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -296,22 +306,22 @@ @@ -537,6 +547,54 @@ @@ -1159,44 +1295,23 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - + @@ -1208,51 +1323,9 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -1264,23 +1337,87 @@ - + - + + + + + + + + - - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/translator/src/org/jetbrains/k2js/translate/declaration/PropertyTranslator.java b/translator/src/org/jetbrains/k2js/translate/declaration/PropertyTranslator.java index b964b327045..fb4f5d50dc5 100644 --- a/translator/src/org/jetbrains/k2js/translate/declaration/PropertyTranslator.java +++ b/translator/src/org/jetbrains/k2js/translate/declaration/PropertyTranslator.java @@ -14,12 +14,14 @@ import org.jetbrains.k2js.translate.general.AbstractTranslator; import org.jetbrains.k2js.translate.general.Translation; import org.jetbrains.k2js.translate.general.TranslationContext; import org.jetbrains.k2js.translate.utils.BindingUtils; -import org.jetbrains.k2js.translate.utils.TranslationUtils; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import static org.jetbrains.k2js.translate.utils.TranslationUtils.assignmentToBackingFieldFromParameter; +import static org.jetbrains.k2js.translate.utils.TranslationUtils.backingFieldReference; + /** * @author Talanov Pavel */ @@ -103,17 +105,13 @@ public final class PropertyTranslator extends AbstractTranslator { @NotNull private JsFunction generateDefaultGetterFunction(@NotNull PropertyGetterDescriptor descriptor) { - JsReturn returnExpression = new JsReturn(backingFieldReference()); + JsReturn returnExpression = new JsReturn(backingFieldReference(context(), property)); JsFunction getterFunction = JsFunction.getAnonymousFunctionWithScope(context().getScopeForDescriptor(descriptor)); getterFunction.setBody(AstUtil.convertToBlock(returnExpression)); return getterFunction; } - private JsNameRef backingFieldReference() { - return TranslationUtils.backingFieldReference(context(), property); - } - @NotNull private JsPropertyInitializer generateDefaultSetter() { PropertySetterDescriptor setterDescriptor = property.getSetter(); @@ -128,7 +126,7 @@ public final class PropertyTranslator extends AbstractTranslator { context().getScopeForDescriptor(propertySetterDescriptor)); JsParameter defaultParameter = new JsParameter(propertyAccessContext(propertySetterDescriptor).enclosingScope().declareTemporary()); - JsBinaryOperation assignment = assignmentToBackingFieldFromParameter(defaultParameter); + JsStatement assignment = assignmentToBackingFieldFromParameter(context(), property, defaultParameter); result.setParameters(Arrays.asList(defaultParameter)); result.setBody(AstUtil.convertToBlock(assignment)); return result; @@ -139,12 +137,6 @@ public final class PropertyTranslator extends AbstractTranslator { return context().newPropertyAccess(propertySetterDescriptor); } - //TODO: similar code assignment to backing field 1 - @NotNull - private JsBinaryOperation assignmentToBackingFieldFromParameter(@NotNull JsParameter parameter) { - return AstUtil.newAssignment(backingFieldReference(), parameter.getName().makeRef()); - } - @NotNull private JsPropertyInitializer translateCustomAccessor(@NotNull JetPropertyAccessor expression) { JsFunction methodBody = translateCustomAccessorBody(expression); diff --git a/translator/src/org/jetbrains/k2js/translate/expression/ExpressionVisitor.java b/translator/src/org/jetbrains/k2js/translate/expression/ExpressionVisitor.java index d8a0a22015b..fb1249931cd 100644 --- a/translator/src/org/jetbrains/k2js/translate/expression/ExpressionVisitor.java +++ b/translator/src/org/jetbrains/k2js/translate/expression/ExpressionVisitor.java @@ -4,11 +4,8 @@ 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; -import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor; import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.resolve.BindingContext; -import org.jetbrains.jet.lang.resolve.calls.ResolvedCall; import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant; import org.jetbrains.jet.lang.resolve.constants.NullValue; import org.jetbrains.k2js.translate.general.Translation; @@ -18,10 +15,11 @@ import org.jetbrains.k2js.translate.operation.BinaryOperationTranslator; import org.jetbrains.k2js.translate.operation.UnaryOperationTranslator; import org.jetbrains.k2js.translate.reference.PropertyAccessTranslator; import org.jetbrains.k2js.translate.utils.BindingUtils; -import org.jetbrains.k2js.translate.utils.TranslationUtils; import java.util.List; +import static org.jetbrains.k2js.translate.utils.TranslationUtils.*; + /** * @author Talanov Pavel */ @@ -102,7 +100,7 @@ public final class ExpressionVisitor extends TranslatorVisitor { @NotNull // assume it is a local variable declaration public JsNode visitProperty(@NotNull JetProperty expression, @NotNull TranslationContext context) { - JsName jsPropertyName = context.declareLocalName(TranslationUtils.getPropertyName(expression)); + JsName jsPropertyName = context.declareLocalName(getPropertyName(expression)); JsExpression jsInitExpression = translateInitializerForProperty(expression, context); return AstUtil.newVar(jsPropertyName, jsInitExpression); } @@ -111,8 +109,8 @@ public final class ExpressionVisitor extends TranslatorVisitor { @NotNull public JsNode visitCallExpression(JetCallExpression expression, TranslationContext context) { JsExpression callee = translateCallee(expression, context); - List arguments = TranslationUtils.translateArgumentList(expression.getValueArguments(), context); - if (isConstructorInvocation(expression, context)) { + List arguments = translateArgumentList(expression.getValueArguments(), context); + if (isConstructorInvocation(context, expression)) { JsNew constructorCall = new JsNew(callee); constructorCall.setArguments(arguments); return constructorCall; @@ -120,19 +118,6 @@ public final class ExpressionVisitor extends TranslatorVisitor { return AstUtil.newInvocation(callee, arguments); } - //TODO: move to util - private boolean isConstructorInvocation(@NotNull JetCallExpression expression, - @NotNull TranslationContext context) { - JetExpression calleeExpression = expression.getCalleeExpression(); - assert calleeExpression != null : "JetCallExpression should have not null callee"; - ResolvedCall resolvedCall = BindingUtils.getResolvedCall(context.bindingContext(), calleeExpression); - if (resolvedCall == null) { - return false; - } - CallableDescriptor descriptor = resolvedCall.getCandidateDescriptor(); - return (descriptor instanceof ConstructorDescriptor); - } - @NotNull private JsExpression translateCallee(@NotNull JetCallExpression expression, @NotNull TranslationContext context) { JetExpression callee = expression.getCalleeExpression(); @@ -262,7 +247,6 @@ public final class ExpressionVisitor extends TranslatorVisitor { return null; } - //TODO: refactor and possibly move somewhere @Override @NotNull public JsNode visitDotQualifiedExpression(@NotNull JetDotQualifiedExpression expression, @@ -351,7 +335,7 @@ public final class ExpressionVisitor extends TranslatorVisitor { JsExpression receiver = translateReceiver(expression, context); JsNullLiteral nullLiteral = context.program().getNullLiteral(); JsExpression thenExpression = translateQualifiedExpression(expression, context); - return new JsConditional(TranslationUtils.notNullCheck(context, receiver), + return new JsConditional(notNullCheck(context, receiver), thenExpression, nullLiteral); } diff --git a/translator/src/org/jetbrains/k2js/translate/general/TranslationContext.java b/translator/src/org/jetbrains/k2js/translate/general/TranslationContext.java index 1b8b7249b99..a12e6a58c05 100644 --- a/translator/src/org/jetbrains/k2js/translate/general/TranslationContext.java +++ b/translator/src/org/jetbrains/k2js/translate/general/TranslationContext.java @@ -118,13 +118,6 @@ public final class TranslationContext { return new TranslationContext(namespaceName, program, bindingContext, newScopes, declarations, block); } - //TODO: consider deleting -// @NotNull -// public TranslationContext newFunctionLiteral(@NotNull JsScope correspondingScope) { -// Scopes newScopes = new Scopes(correspondingScope, scopes.classScope, scopes.namespaceScope); -// return new TranslationContext(namespaceName, program, bindingContext, newScopes, declarations, block); -// } - // Note: Should be used if and only if scope has no corresponding descriptor @NotNull public TranslationContext newEnclosingScope(@NotNull JsScope enclosingScope) { diff --git a/translator/src/org/jetbrains/k2js/translate/general/TranslatorVisitor.java b/translator/src/org/jetbrains/k2js/translate/general/TranslatorVisitor.java index 9efecf47cb7..88af4c2edc1 100644 --- a/translator/src/org/jetbrains/k2js/translate/general/TranslatorVisitor.java +++ b/translator/src/org/jetbrains/k2js/translate/general/TranslatorVisitor.java @@ -1,11 +1,7 @@ package org.jetbrains.k2js.translate.general; -import com.google.dart.compiler.backend.js.ast.JsExpression; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.psi.JetElement; -import org.jetbrains.jet.lang.psi.JetExpression; -import org.jetbrains.jet.lang.psi.JetProperty; import org.jetbrains.jet.lang.psi.JetVisitor; /** @@ -21,14 +17,4 @@ public class TranslatorVisitor extends JetVisitor { throw new RuntimeException("Unsupported expression encountered:" + expression.toString()); } - @Nullable - protected JsExpression translateInitializerForProperty(@NotNull JetProperty declaration, - @NotNull TranslationContext context) { - JsExpression jsInitExpression = null; - JetExpression initializer = declaration.getInitializer(); - if (initializer != null) { - jsInitExpression = Translation.translateAsExpression(initializer, context); - } - return jsInitExpression; - } } diff --git a/translator/src/org/jetbrains/k2js/translate/initializer/ClassInitializerTranslator.java b/translator/src/org/jetbrains/k2js/translate/initializer/ClassInitializerTranslator.java index 33dd6323053..650f15154e1 100644 --- a/translator/src/org/jetbrains/k2js/translate/initializer/ClassInitializerTranslator.java +++ b/translator/src/org/jetbrains/k2js/translate/initializer/ClassInitializerTranslator.java @@ -99,16 +99,11 @@ public final class ClassInitializerTranslator extends AbstractInitializerTransla PropertyDescriptor propertyDescriptor = BindingUtils.getPropertyDescriptorForConstructorParameter(context().bindingContext(), jetParameter); if (propertyDescriptor != null) { - initializerStatements.add(assignmentToBackingFieldFromParameter(propertyDescriptor, jsParameter)); + initializerStatements.add + (TranslationUtils.assignmentToBackingFieldFromParameter + (context(), propertyDescriptor, jsParameter)); } } - //TODO: similar code assignment to backing field 2 - @NotNull - private JsStatement assignmentToBackingFieldFromParameter(@NotNull PropertyDescriptor descriptor, - @NotNull JsParameter parameter) { - JsNameRef backingFieldReference = TranslationUtils.backingFieldReference(context(), descriptor); - return AstUtil.newAssignmentStatement(backingFieldReference, parameter.getName().makeRef()); - } } diff --git a/translator/src/org/jetbrains/k2js/translate/utils/TranslationUtils.java b/translator/src/org/jetbrains/k2js/translate/utils/TranslationUtils.java index 26ec095bc35..59fbc40aed7 100644 --- a/translator/src/org/jetbrains/k2js/translate/utils/TranslationUtils.java +++ b/translator/src/org/jetbrains/k2js/translate/utils/TranslationUtils.java @@ -4,10 +4,14 @@ 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; +import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor; import org.jetbrains.jet.lang.descriptors.PropertyDescriptor; +import org.jetbrains.jet.lang.psi.JetCallExpression; import org.jetbrains.jet.lang.psi.JetExpression; import org.jetbrains.jet.lang.psi.JetProperty; import org.jetbrains.jet.lang.psi.ValueArgument; +import org.jetbrains.jet.lang.resolve.calls.ResolvedCall; import org.jetbrains.k2js.translate.general.Translation; import org.jetbrains.k2js.translate.general.TranslationContext; import org.jetbrains.k2js.translate.reference.ReferenceProvider; @@ -95,4 +99,43 @@ public final class TranslationUtils { String backingFieldName = Namer.getKotlinBackingFieldName(propertyName); return context.enclosingScope().findExistingName(backingFieldName); } + + @Nullable + public static JsExpression translateInitializerForProperty(@NotNull JetProperty declaration, + @NotNull TranslationContext context) { + JsExpression jsInitExpression = null; + JetExpression initializer = declaration.getInitializer(); + if (initializer != null) { + jsInitExpression = Translation.translateAsExpression(initializer, context); + } + return jsInitExpression; + } + + public static boolean isConstructorInvocation(@NotNull TranslationContext context, + @NotNull JetCallExpression expression) { + JetExpression calleeExpression = expression.getCalleeExpression(); + assert calleeExpression != null : "JetCallExpression should have not null callee"; + ResolvedCall resolvedCall = BindingUtils.getResolvedCall(context.bindingContext(), calleeExpression); + if (resolvedCall == null) { + return false; + } + CallableDescriptor descriptor = resolvedCall.getCandidateDescriptor(); + return (descriptor instanceof ConstructorDescriptor); + } + + @NotNull + public static JsStatement assignmentToBackingFieldFromParameter(@NotNull TranslationContext context, + @NotNull PropertyDescriptor descriptor, + @NotNull JsParameter parameter) { + JsNameRef backingFieldReference = backingFieldReference(context, descriptor); + return AstUtil.newAssignmentStatement(backingFieldReference, parameter.getName().makeRef()); + } + + @NotNull + public static JsStatement assignmentToBackingFieldFromParameter(@NotNull TranslationContext context, + @NotNull JetProperty property, + @NotNull JsParameter parameter) { + return assignmentToBackingFieldFromParameter + (context, BindingUtils.getPropertyDescriptor(context.bindingContext(), property), parameter); + } } diff --git a/translator/src/org/jetbrains/k2js/utils/DiGraphNode.java b/translator/src/org/jetbrains/k2js/utils/DiGraphNode.java index fdee94213b7..b599a6cac0e 100644 --- a/translator/src/org/jetbrains/k2js/utils/DiGraphNode.java +++ b/translator/src/org/jetbrains/k2js/utils/DiGraphNode.java @@ -1,5 +1,6 @@ package org.jetbrains.k2js.utils; +//TODO: find other implementation or steal it! /* * Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.