JS backend: dropped hack from UsageTracker.
This commit is contained in:
@@ -41,6 +41,7 @@ import static org.jetbrains.k2js.translate.utils.AnnotationsUtils.getNameForAnno
|
||||
import static org.jetbrains.k2js.translate.utils.AnnotationsUtils.isLibraryObject;
|
||||
import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.*;
|
||||
import static org.jetbrains.k2js.translate.utils.TranslationUtils.getMangledName;
|
||||
import static org.jetbrains.k2js.translate.utils.TranslationUtils.getSuggestedName;
|
||||
|
||||
/**
|
||||
* Aggregates all the static parts of the context.
|
||||
@@ -222,14 +223,7 @@ public final class StaticContext {
|
||||
@Nullable
|
||||
public JsName apply(@NotNull DeclarationDescriptor descriptor) {
|
||||
JsScope scope = getEnclosingScope(descriptor);
|
||||
|
||||
String suggestedName = descriptor.getName().asString();
|
||||
|
||||
if (descriptor instanceof FunctionDescriptor) {
|
||||
suggestedName = getMangledName((FunctionDescriptor) descriptor);
|
||||
}
|
||||
|
||||
return scope.declareFreshName(suggestedName);
|
||||
return scope.declareFreshName(getSuggestedName(descriptor));
|
||||
}
|
||||
};
|
||||
Rule<JsName> constructorHasTheSameNameAsTheClass = new Rule<JsName>() {
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.jetbrains.jet.lang.psi.JetExpression;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
import org.jetbrains.k2js.translate.intrinsic.Intrinsics;
|
||||
import org.jetbrains.k2js.translate.utils.TranslationUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -114,7 +115,7 @@ public class TranslationContext {
|
||||
@NotNull
|
||||
public TranslationContext newFunctionBodyWithUsageTracker(@NotNull JsFunction fun, @NotNull MemberDescriptor descriptor) {
|
||||
DynamicContext dynamicContext = DynamicContext.newContext(fun.getScope(), fun.getBody());
|
||||
UsageTracker usageTracker = new UsageTracker(this.usageTracker, descriptor, fun.getScope(), this.staticContext);
|
||||
UsageTracker usageTracker = new UsageTracker(this.usageTracker, descriptor, fun.getScope());
|
||||
return new TranslationContext(this, this.staticContext, dynamicContext, this.aliasingContext.inner(), usageTracker, this.definitionPlace);
|
||||
}
|
||||
|
||||
@@ -285,8 +286,9 @@ public class TranslationContext {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JsNameRef define(String name, JsExpression expression) {
|
||||
return getDefinitionPlace().define(name, expression);
|
||||
public JsNameRef define(DeclarationDescriptor descriptor, JsExpression expression) {
|
||||
String suggestedName = TranslationUtils.getSuggestedNameForInnerDeclaration(this, descriptor);
|
||||
return getDefinitionPlace().define(suggestedName, expression);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
@@ -20,14 +20,14 @@ import org.jetbrains.jet.lang.descriptors.*
|
||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils.isAncestor
|
||||
import com.google.dart.compiler.backend.js.ast.JsName
|
||||
import com.google.dart.compiler.backend.js.ast.JsScope
|
||||
import org.jetbrains.k2js.translate.utils.TranslationUtils.getSuggestedName
|
||||
|
||||
private val CAPTURED_RECEIVER_NAME_PREFIX : String = "this$"
|
||||
|
||||
class UsageTracker(
|
||||
private val parent: UsageTracker?,
|
||||
val containingDescriptor: MemberDescriptor,
|
||||
private val scope: JsScope,
|
||||
private val staticContext: StaticContext
|
||||
private val scope: JsScope
|
||||
) {
|
||||
|
||||
private val captured = hashMapOf<CallableDescriptor, JsName>()
|
||||
@@ -63,18 +63,7 @@ class UsageTracker(
|
||||
}
|
||||
|
||||
private fun CallableDescriptor.getJsNameForCapturedDescriptor(): JsName {
|
||||
val suggestedName =
|
||||
when (this) {
|
||||
is ReceiverParameterDescriptor -> {
|
||||
this.getNameForCapturedReceiver()
|
||||
}
|
||||
else -> {
|
||||
// TODO: drop temporary HACK or add description
|
||||
val name = staticContext.getNameForDescriptor(this)
|
||||
name.getIdent()
|
||||
}
|
||||
}
|
||||
|
||||
val suggestedName = if (this is ReceiverParameterDescriptor) this.getNameForCapturedReceiver() else getSuggestedName(this)
|
||||
return scope.declareFreshName(suggestedName)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,6 @@ import org.jetbrains.k2js.translate.expression.ExpressionPackage;
|
||||
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
||||
import org.jetbrains.k2js.translate.initializer.ClassInitializerTranslator;
|
||||
import org.jetbrains.k2js.translate.utils.JsAstUtils;
|
||||
import org.jetbrains.k2js.translate.utils.TranslationUtils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@@ -224,7 +223,6 @@ public final class ClassTranslator extends AbstractTranslator {
|
||||
|
||||
fun.getBody().getStatements().add(new JsReturn(translate(funContext)));
|
||||
|
||||
String suggestedName = TranslationUtils.getSuggestedName(funContext, descriptor);
|
||||
return ExpressionPackage.withCapturedParameters(fun, funContext, outerClassContext, suggestedName);
|
||||
return ExpressionPackage.withCapturedParameters(fun, funContext, outerClassContext, descriptor);
|
||||
}
|
||||
}
|
||||
|
||||
+8
-9
@@ -26,6 +26,7 @@ import org.jetbrains.k2js.translate.utils.BindingUtils.getFunctionDescriptor
|
||||
import org.jetbrains.k2js.translate.utils.FunctionBodyTranslator.translateFunctionBody
|
||||
import org.jetbrains.k2js.translate.utils.TranslationUtils.getSuggestedName
|
||||
import org.jetbrains.k2js.translate.utils.TranslationUtils.simpleReturnFunction
|
||||
import org.jetbrains.jet.lang.descriptors.MemberDescriptor
|
||||
|
||||
class LiteralFunctionTranslator(context: TranslationContext) : AbstractTranslator(context) {
|
||||
fun translate(declaration: JetDeclarationWithBody): JsExpression {
|
||||
@@ -54,18 +55,16 @@ class LiteralFunctionTranslator(context: TranslationContext) : AbstractTranslato
|
||||
lambda.setName(tracker.getNameForCapturedDescriptor(descriptor))
|
||||
}
|
||||
|
||||
val suggestedName = getSuggestedName(functionContext, descriptor)
|
||||
|
||||
if (tracker.hasCapturedExceptContaining()) {
|
||||
val lambdaCreator = simpleReturnFunction(invokingContext.scope(), lambda)
|
||||
return lambdaCreator.withCapturedParameters(functionContext, invokingContext, suggestedName)
|
||||
return lambdaCreator.withCapturedParameters(functionContext, invokingContext, descriptor)
|
||||
}
|
||||
|
||||
return invokingContext.define(suggestedName, lambda)
|
||||
return invokingContext.define(descriptor, lambda)
|
||||
}
|
||||
}
|
||||
|
||||
fun JsFunction.withCapturedParameters(context: TranslationContext, invokingContext: TranslationContext, suggestedName: String): JsExpression {
|
||||
fun JsFunction.withCapturedParameters(context: TranslationContext, invokingContext: TranslationContext, descriptor: MemberDescriptor): JsExpression {
|
||||
|
||||
fun getParameterNameRefForInvocation(callableDescriptor: CallableDescriptor): JsExpression {
|
||||
val alias = invokingContext.getAliasForDescriptor(callableDescriptor)
|
||||
@@ -76,7 +75,7 @@ fun JsFunction.withCapturedParameters(context: TranslationContext, invokingConte
|
||||
return invokingContext.getNameForDescriptor(callableDescriptor).makeRef()
|
||||
}
|
||||
|
||||
val ref = invokingContext.define(suggestedName, this)
|
||||
val ref = invokingContext.define(descriptor, this)
|
||||
val invocation = JsInvocation(ref)
|
||||
|
||||
val invocationArguments = invocation.getArguments()!!
|
||||
@@ -84,11 +83,11 @@ fun JsFunction.withCapturedParameters(context: TranslationContext, invokingConte
|
||||
|
||||
val tracker = context.usageTracker()!!
|
||||
|
||||
for ((descriptor, name) in tracker.capturedDescriptorToJsName) {
|
||||
if (descriptor == tracker.containingDescriptor) continue
|
||||
for ((capturedDescriptor, name) in tracker.capturedDescriptorToJsName) {
|
||||
if (capturedDescriptor == tracker.containingDescriptor) continue
|
||||
|
||||
functionParameters.add(JsParameter(name))
|
||||
invocationArguments.add(getParameterNameRefForInvocation(descriptor))
|
||||
invocationArguments.add(getParameterNameRefForInvocation(capturedDescriptor))
|
||||
}
|
||||
|
||||
return invocation
|
||||
|
||||
+1
-1
@@ -131,7 +131,7 @@ public final class TopLevelFIF extends CompositeFIF {
|
||||
private static String getStableMangledBuiltInName(@NotNull ClassDescriptor descriptor, @NotNull String functionName) {
|
||||
Collection<FunctionDescriptor> functions = descriptor.getDefaultType().getMemberScope().getFunctions(Name.identifier(functionName));
|
||||
assert functions.size() == 1 : "Can't select a single function: " + functionName + " in " + descriptor;
|
||||
return TranslationUtils.getMangledName(functions.iterator().next());
|
||||
return TranslationUtils.getSuggestedName(functions.iterator().next());
|
||||
}
|
||||
|
||||
private static final FunctionIntrinsic PROPERTY_METADATA_IMPL = new FunctionIntrinsic() {
|
||||
|
||||
@@ -143,7 +143,18 @@ public final class TranslationUtils {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String getMangledName(@NotNull FunctionDescriptor descriptor) {
|
||||
public static String getSuggestedName(@NotNull DeclarationDescriptor descriptor) {
|
||||
String suggestedName = descriptor.getName().asString();
|
||||
|
||||
if (descriptor instanceof FunctionDescriptor) {
|
||||
suggestedName = getMangledName((FunctionDescriptor) descriptor);
|
||||
}
|
||||
|
||||
return suggestedName;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String getMangledName(@NotNull FunctionDescriptor descriptor) {
|
||||
if (needsStableMangling(descriptor)) {
|
||||
return getStableMangledName(descriptor);
|
||||
}
|
||||
@@ -322,16 +333,6 @@ public final class TranslationUtils {
|
||||
return jsInitExpression;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static List<JsExpression> translateExpressionList(@NotNull TranslationContext context,
|
||||
@NotNull List<JetExpression> expressions) {
|
||||
List<JsExpression> result = new ArrayList<JsExpression>();
|
||||
for (JetExpression expression : expressions) {
|
||||
result.add(Translation.translateAsExpression(expression, context));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JsExpression translateBaseExpression(@NotNull TranslationContext context,
|
||||
@NotNull JetUnaryExpression expression) {
|
||||
@@ -412,7 +413,7 @@ public final class TranslationUtils {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String getSuggestedName(TranslationContext context, DeclarationDescriptor descriptor) {
|
||||
public static String getSuggestedNameForInnerDeclaration(TranslationContext context, DeclarationDescriptor descriptor) {
|
||||
String suggestedName = "";
|
||||
DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
|
||||
if (containingDeclaration != null &&
|
||||
|
||||
Reference in New Issue
Block a user