TODO fixes

This commit is contained in:
pTalanov
2012-02-29 22:05:18 +04:00
parent 92c69b6826
commit 26a8d18d9e
7 changed files with 23 additions and 25 deletions
@@ -51,11 +51,11 @@ public abstract class TranslationTest extends BaseTest {
public void translateFile(@NotNull String inputFile, public void translateFile(@NotNull String inputFile,
@NotNull String outputFile) throws Exception { @NotNull String outputFile) throws Exception {
traslateFiles(Collections.singletonList(inputFile), outputFile); translateFiles(Collections.singletonList(inputFile), outputFile);
} }
public void traslateFiles(@NotNull List<String> inputFiles, public void translateFiles(@NotNull List<String> inputFiles,
@NotNull String outputFile) throws Exception { @NotNull String outputFile) throws Exception {
K2JSTranslator translator = new K2JSTranslator(new TestConfig(getProject())); K2JSTranslator translator = new K2JSTranslator(new TestConfig(getProject()));
List<JetFile> psiFiles = createPsiFileList(inputFiles, getProject()); List<JetFile> psiFiles = createPsiFileList(inputFiles, getProject());
JsProgram program = translator.generateProgram(psiFiles); JsProgram program = translator.generateProgram(psiFiles);
@@ -158,8 +158,8 @@ public abstract class TranslationTest extends BaseTest {
fullFilePaths.add(getInputFilePath(dirName) + "/" + fileName); fullFilePaths.add(getInputFilePath(dirName) + "/" + fileName);
} }
assert dir.isDirectory(); assert dir.isDirectory();
traslateFiles(fullFilePaths, translateFiles(fullFilePaths,
getOutputFilePath(dirName + ".kt")); getOutputFilePath(dirName + ".kt"));
} }
protected List<String> generateFilenameList(String inputFile) { protected List<String> generateFilenameList(String inputFile) {
@@ -38,7 +38,6 @@ import static org.jetbrains.k2js.translate.utils.PsiUtils.getLoopRange;
*/ */
public final class RangeForTranslator extends ForTranslator { public final class RangeForTranslator extends ForTranslator {
//TODO: inspection
@NotNull @NotNull
public static JsStatement doTranslate(@NotNull JetForExpression expression, public static JsStatement doTranslate(@NotNull JetForExpression expression,
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
@@ -19,6 +19,7 @@ package org.jetbrains.k2js.translate.initializer;
import com.google.dart.compiler.backend.js.ast.*; import com.google.dart.compiler.backend.js.ast.*;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor; import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
import org.jetbrains.jet.lang.psi.JetClassOrObject; import org.jetbrains.jet.lang.psi.JetClassOrObject;
@@ -27,15 +28,15 @@ import org.jetbrains.jet.lang.psi.JetDelegatorToSuperCall;
import org.jetbrains.jet.lang.psi.JetParameter; import org.jetbrains.jet.lang.psi.JetParameter;
import org.jetbrains.k2js.translate.context.Namer; import org.jetbrains.k2js.translate.context.Namer;
import org.jetbrains.k2js.translate.context.TranslationContext; import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.utils.PsiUtils;
import org.jetbrains.k2js.translate.utils.TranslationUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.jetbrains.k2js.translate.utils.BindingUtils.*; import static org.jetbrains.k2js.translate.utils.BindingUtils.*;
import static org.jetbrains.k2js.translate.utils.JsAstUtils.*; import static org.jetbrains.k2js.translate.utils.JsAstUtils.*;
import static org.jetbrains.k2js.translate.utils.PsiUtils.getPrimaryConstructorParameters;
import static org.jetbrains.k2js.translate.utils.TranslationUtils.assignmentToBackingField; import static org.jetbrains.k2js.translate.utils.TranslationUtils.assignmentToBackingField;
import static org.jetbrains.k2js.translate.utils.TranslationUtils.translateArgumentList;
/** /**
* @author Pavel Talanov * @author Pavel Talanov
@@ -57,9 +58,10 @@ public final class ClassInitializerTranslator extends AbstractInitializerTransla
@Override @Override
@NotNull @NotNull
protected JsFunction generateInitializerFunction() { protected JsFunction generateInitializerFunction() {
//TODO: look for duplication of this code //TODO: it's inconsistent that we scope for class and function for constructor, currently have problems implementing better way
JsFunction result = new JsFunction(initializerMethodScope.jsScope()); ConstructorDescriptor primaryConstructor = getConstructor();
//NOTE: that while we translateAsLocalNameReference constructor parameters we also add property initializer statements JsFunction result = context().getFunctionObject(primaryConstructor);
//NOTE: while we translate constructor parameters we also add property initializer statements
// for properties declared as constructor parameters // for properties declared as constructor parameters
setParameters(result, translatePrimaryConstructorParameters()); setParameters(result, translatePrimaryConstructorParameters());
mayBeAddCallToSuperMethod(); mayBeAddCallToSuperMethod();
@@ -67,6 +69,13 @@ public final class ClassInitializerTranslator extends AbstractInitializerTransla
return result; return result;
} }
private ConstructorDescriptor getConstructor() {
ConstructorDescriptor primaryConstructor =
getClassDescriptor(context().bindingContext(), classDeclaration).getUnsubstitutedPrimaryConstructor();
assert primaryConstructor != null : "Traits do not have initialize methods.";
return primaryConstructor;
}
@NotNull @NotNull
private JsBlock generateInitializerMethodBody() { private JsBlock generateInitializerMethodBody() {
initializerStatements.addAll(translateClassInitializers(classDeclaration)); initializerStatements.addAll(translateClassInitializers(classDeclaration));
@@ -91,8 +100,7 @@ public final class ClassInitializerTranslator extends AbstractInitializerTransla
@NotNull @NotNull
private List<JsExpression> translateArguments(@NotNull JetDelegatorToSuperCall superCall) { private List<JsExpression> translateArguments(@NotNull JetDelegatorToSuperCall superCall) {
//TODO: use the same mechanism as in call translator return translateArgumentList(context(), superCall.getValueArguments());
return TranslationUtils.translateArgumentList(context(), superCall.getValueArguments());
} }
@Nullable @Nullable
@@ -103,13 +111,12 @@ public final class ClassInitializerTranslator extends AbstractInitializerTransla
result = (JetDelegatorToSuperCall) specifier; result = (JetDelegatorToSuperCall) specifier;
} }
} }
//assert result != null : "Class must call ancestor's constructor.";
return result; return result;
} }
@NotNull @NotNull
List<JsParameter> translatePrimaryConstructorParameters() { List<JsParameter> translatePrimaryConstructorParameters() {
List<JetParameter> parameterList = PsiUtils.getPrimaryConstructorParameters(classDeclaration); List<JetParameter> parameterList = getPrimaryConstructorParameters(classDeclaration);
List<JsParameter> result = new ArrayList<JsParameter>(); List<JsParameter> result = new ArrayList<JsParameter>();
for (JetParameter jetParameter : parameterList) { for (JetParameter jetParameter : parameterList) {
result.add(translateParameter(jetParameter)); result.add(translateParameter(jetParameter));
@@ -40,6 +40,7 @@ public final class NamespaceInitializerTranslator extends AbstractInitializerTra
@Override @Override
@NotNull @NotNull
protected JsFunction generateInitializerFunction() { protected JsFunction generateInitializerFunction() {
//NOTE: namespace has no constructor
JsFunction result = new JsFunction(initializerMethodScope.jsScope()); JsFunction result = new JsFunction(initializerMethodScope.jsScope());
result.setBody(newBlock(translateNamespaceInitializers(namespace))); result.setBody(newBlock(translateNamespaceInitializers(namespace)));
return result; return result;
@@ -30,7 +30,6 @@ public final class BuiltInPropertyIntrinsic implements Intrinsic {
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
assert receiver != null; assert receiver != null;
assert arguments.isEmpty() : "Length expression must have zero arguments."; assert arguments.isEmpty() : "Length expression must have zero arguments.";
//TODO: provide better way
JsNameRef lengthProperty = AstUtil.newQualifiedNameRef(propertyName); JsNameRef lengthProperty = AstUtil.newQualifiedNameRef(propertyName);
setQualifier(lengthProperty, receiver); setQualifier(lengthProperty, receiver);
return lengthProperty; return lengthProperty;
@@ -52,8 +52,7 @@ public final class PrimitiveRangeToIntrinsic implements Intrinsic {
JsBinaryOperation rangeSize = sum(subtract(rangeEnd, rangeStart), JsBinaryOperation rangeSize = sum(subtract(rangeEnd, rangeStart),
context.program().getNumberLiteral(1)); context.program().getNumberLiteral(1));
//TODO: provide a way not to hard code this value //TODO: provide a way not to hard code this value
JsNew numberRangeConstructorInvocation JsNew numberRangeConstructorInvocation = new JsNew(AstUtil.newQualifiedNameRef("Kotlin.NumberRange"));
= new JsNew(AstUtil.newQualifiedNameRef("Kotlin.NumberRange"));
//TODO: add tests and correct expression for reversed ranges. //TODO: add tests and correct expression for reversed ranges.
JsBooleanLiteral isRangeReversed = context.program().getFalseLiteral(); JsBooleanLiteral isRangeReversed = context.program().getFalseLiteral();
setArguments(numberRangeConstructorInvocation, rangeStart, rangeSize, isRangeReversed); setArguments(numberRangeConstructorInvocation, rangeStart, rangeSize, isRangeReversed);
@@ -77,13 +77,6 @@ public final class BindingUtils {
return getDescriptorForExpression(context, declaration, FunctionDescriptor.class); return getDescriptorForExpression(context, declaration, FunctionDescriptor.class);
} }
//TODO:
@NotNull
public static PropertyAccessorDescriptor getPropertyAccessorDescriptor(@NotNull BindingContext context,
@NotNull JetPropertyAccessor declaration) {
return getDescriptorForExpression(context, declaration, PropertyAccessorDescriptor.class);
}
@NotNull @NotNull
public static PropertyDescriptor getPropertyDescriptor(@NotNull BindingContext context, public static PropertyDescriptor getPropertyDescriptor(@NotNull BindingContext context,
@NotNull JetProperty declaration) { @NotNull JetProperty declaration) {