JS backend: refactor ClassTranslator
This commit is contained in:
@@ -24,6 +24,8 @@ import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
|
|||||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
||||||
import org.jetbrains.jet.plugin.JetLanguage;
|
import org.jetbrains.jet.plugin.JetLanguage;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encapuslates different types of constants and naming conventions.
|
* Encapuslates different types of constants and naming conventions.
|
||||||
*/
|
*/
|
||||||
@@ -271,18 +273,18 @@ public final class Namer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public JsInvocation classCreateInvocation(@NotNull ClassDescriptor descriptor) {
|
public JsInvocation classCreateInvocation(@NotNull ClassDescriptor descriptor, @NotNull List<JsExpression> arguments) {
|
||||||
switch (descriptor.getKind()) {
|
switch (descriptor.getKind()) {
|
||||||
case TRAIT:
|
case TRAIT:
|
||||||
return new JsInvocation(traitCreationMethodReference());
|
return new JsInvocation(traitCreationMethodReference(), arguments);
|
||||||
|
|
||||||
case OBJECT:
|
case OBJECT:
|
||||||
case CLASS_OBJECT:
|
case CLASS_OBJECT:
|
||||||
case ENUM_ENTRY:
|
case ENUM_ENTRY:
|
||||||
return new JsInvocation(objectCreationMethodReference());
|
return new JsInvocation(objectCreationMethodReference(), arguments);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return new JsInvocation(classCreationMethodReference());
|
return new JsInvocation(classCreationMethodReference(), arguments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-24
@@ -124,23 +124,17 @@ public final class ClassTranslator extends AbstractTranslator {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public JsInvocation translate(@NotNull TranslationContext declarationContext) {
|
public JsInvocation translate(@NotNull TranslationContext declarationContext) {
|
||||||
JsInvocation createInvocation = context().namer().classCreateInvocation(descriptor);
|
return context().namer().classCreateInvocation(descriptor, getClassCreateInvocationArguments(declarationContext));
|
||||||
translate(createInvocation, declarationContext);
|
|
||||||
return createInvocation;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void translate(@NotNull JsInvocation createInvocation, @NotNull TranslationContext context) {
|
|
||||||
addSuperclassReferences(createInvocation);
|
|
||||||
addClassOwnDeclarations(createInvocation.getArguments(), context);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isTrait() {
|
private boolean isTrait() {
|
||||||
return descriptor.getKind().equals(ClassKind.TRAIT);
|
return descriptor.getKind().equals(ClassKind.TRAIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addClassOwnDeclarations(@NotNull List<JsExpression> invocationArguments, @NotNull TranslationContext declarationContext) {
|
private List<JsExpression> getClassCreateInvocationArguments(@NotNull TranslationContext declarationContext) {
|
||||||
final List<JsPropertyInitializer> properties = new SmartList<JsPropertyInitializer>();
|
List<JsExpression> invocationArguments = new ArrayList<JsExpression>();
|
||||||
|
|
||||||
|
final List<JsPropertyInitializer> properties = new SmartList<JsPropertyInitializer>();
|
||||||
final List<JsPropertyInitializer> staticProperties = new SmartList<JsPropertyInitializer>();
|
final List<JsPropertyInitializer> staticProperties = new SmartList<JsPropertyInitializer>();
|
||||||
boolean isTopLevelDeclaration = context() == declarationContext;
|
boolean isTopLevelDeclaration = context() == declarationContext;
|
||||||
final JsNameRef qualifiedReference;
|
final JsNameRef qualifiedReference;
|
||||||
@@ -170,6 +164,7 @@ public final class ClassTranslator extends AbstractTranslator {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
invocationArguments.add(getSuperclassReferences());
|
||||||
if (!isTrait()) {
|
if (!isTrait()) {
|
||||||
JsFunction initializer = new ClassInitializerTranslator(classDeclaration, declarationContext).generateInitializeMethod();
|
JsFunction initializer = new ClassInitializerTranslator(classDeclaration, declarationContext).generateInitializeMethod();
|
||||||
invocationArguments.add(initializer.getBody().getStatements().isEmpty() ? JsLiteral.NULL : initializer);
|
invocationArguments.add(initializer.getBody().getStatements().isEmpty() ? JsLiteral.NULL : initializer);
|
||||||
@@ -201,6 +196,7 @@ public final class ClassTranslator extends AbstractTranslator {
|
|||||||
invocationArguments.add(new JsDocComment(JsAstUtils.LENDS_JS_DOC_TAG, qualifiedReference));
|
invocationArguments.add(new JsDocComment(JsAstUtils.LENDS_JS_DOC_TAG, qualifiedReference));
|
||||||
invocationArguments.add(new JsObjectLiteral(staticProperties, true));
|
invocationArguments.add(new JsObjectLiteral(staticProperties, true));
|
||||||
}
|
}
|
||||||
|
return invocationArguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void mayBeAddEnumEntry(@NotNull List<JsPropertyInitializer> enumEntryList,
|
private void mayBeAddEnumEntry(@NotNull List<JsPropertyInitializer> enumEntryList,
|
||||||
@@ -218,25 +214,16 @@ public final class ClassTranslator extends AbstractTranslator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addSuperclassReferences(@NotNull JsInvocation jsClassDeclaration) {
|
private JsExpression getSuperclassReferences() {
|
||||||
List<JsExpression> superClassReferences = getSupertypesNameReferences();
|
List<JsExpression> superClassReferences = getSupertypesNameReferences();
|
||||||
if (superClassReferences.isEmpty()) {
|
if (superClassReferences.isEmpty()) {
|
||||||
jsClassDeclaration.getArguments().add(JsLiteral.NULL);
|
return JsLiteral.NULL;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
else if (superClassReferences.size() == 1) {
|
||||||
List<JsExpression> expressions;
|
return superClassReferences.get(0);
|
||||||
if (superClassReferences.size() > 1) {
|
|
||||||
JsArrayLiteral arrayLiteral = new JsArrayLiteral();
|
|
||||||
jsClassDeclaration.getArguments().add(arrayLiteral);
|
|
||||||
expressions = arrayLiteral.getExpressions();
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
expressions = jsClassDeclaration.getArguments();
|
return new JsArrayLiteral(superClassReferences);
|
||||||
}
|
|
||||||
|
|
||||||
for (JsExpression superClassReference : superClassReferences) {
|
|
||||||
expressions.add(superClassReference);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user