refactored naming scopes to initialize lazily
This commit is contained in:
@@ -6,13 +6,11 @@ import com.google.dart.compiler.backend.js.ast.JsProgram;
|
||||
import com.google.dart.compiler.backend.js.ast.JsRootScope;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetElement;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.types.JetStandardLibrary;
|
||||
import org.jetbrains.k2js.translate.context.declaration.DeclarationFacade;
|
||||
import org.jetbrains.k2js.translate.context.declaration.Declarations;
|
||||
import org.jetbrains.k2js.translate.intrinsic.Intrinsics;
|
||||
import org.jetbrains.k2js.translate.utils.BindingUtils;
|
||||
|
||||
public class StaticContext {
|
||||
|
||||
@@ -111,18 +109,6 @@ public class StaticContext {
|
||||
return standardClasses;
|
||||
}
|
||||
|
||||
//TODO: helper method outdated
|
||||
@NotNull
|
||||
public NamingScope getScopeForDescriptor(@NotNull DeclarationDescriptor descriptor) {
|
||||
return declarationFacade.getKotlinDeclarations().getScope(descriptor);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public NamingScope getScopeForElement(@NotNull JetElement element) {
|
||||
DeclarationDescriptor descriptor = BindingUtils.getDescriptorForElement(bindingContext, element);
|
||||
return getScopeForDescriptor(descriptor);
|
||||
}
|
||||
|
||||
//TODO: consider using nullable return value instead
|
||||
@NotNull
|
||||
public JsName getGlobalName(@NotNull DeclarationDescriptor descriptor) {
|
||||
|
||||
@@ -17,6 +17,8 @@ import org.jetbrains.k2js.translate.context.generator.Rule;
|
||||
import org.jetbrains.k2js.translate.intrinsic.Intrinsics;
|
||||
import org.jetbrains.k2js.translate.utils.BindingUtils;
|
||||
|
||||
import static org.jetbrains.k2js.translate.utils.DescriptorUtils.getContainingDeclaration;
|
||||
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
*/
|
||||
@@ -27,13 +29,10 @@ public final class TranslationContext {
|
||||
@NotNull
|
||||
private final StaticContext staticContext;
|
||||
@NotNull
|
||||
private final Generator<DeclarationDescriptor, JsName> names =
|
||||
nameGenerator();
|
||||
|
||||
|
||||
private final Generator<JsName> names = nameGenerator();
|
||||
@NotNull
|
||||
private final Generator<DeclarationDescriptor, NamingScope> scopes =
|
||||
new Generator<DeclarationDescriptor, NamingScope>();
|
||||
private final Generator<NamingScope> scopes = scopeGenerator();
|
||||
|
||||
|
||||
@NotNull
|
||||
public static TranslationContext rootContext(@NotNull StaticContext staticContext) {
|
||||
@@ -104,12 +103,13 @@ public final class TranslationContext {
|
||||
|
||||
@NotNull
|
||||
public NamingScope getScopeForDescriptor(@NotNull DeclarationDescriptor descriptor) {
|
||||
return staticContext.getScopeForDescriptor(descriptor);
|
||||
return scopes.get(descriptor);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public NamingScope getScopeForElement(@NotNull JetElement element) {
|
||||
return staticContext.getScopeForElement(element);
|
||||
DeclarationDescriptor descriptorForElement = BindingUtils.getDescriptorForElement(bindingContext(), element);
|
||||
return getScopeForDescriptor(descriptorForElement);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -195,9 +195,9 @@ public final class TranslationContext {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Generator<DeclarationDescriptor, JsName> nameGenerator() {
|
||||
Generator<DeclarationDescriptor, JsName> nameGenerator = new Generator<DeclarationDescriptor, JsName>();
|
||||
nameGenerator.addRule(new Rule<DeclarationDescriptor, JsName>() {
|
||||
private Generator<JsName> nameGenerator() {
|
||||
Generator<JsName> nameGenerator = new Generator<JsName>();
|
||||
nameGenerator.addRule(new Rule<JsName>() {
|
||||
@Override
|
||||
@Nullable
|
||||
public JsName apply(@NotNull DeclarationDescriptor data) {
|
||||
@@ -207,7 +207,7 @@ public final class TranslationContext {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
nameGenerator.addRule(new Rule<DeclarationDescriptor, JsName>() {
|
||||
nameGenerator.addRule(new Rule<JsName>() {
|
||||
@Override
|
||||
@Nullable
|
||||
public JsName apply(@NotNull DeclarationDescriptor data) {
|
||||
@@ -217,7 +217,7 @@ public final class TranslationContext {
|
||||
return standardClasses().getStandardObjectName(data);
|
||||
}
|
||||
});
|
||||
nameGenerator.addRule(new Rule<DeclarationDescriptor, JsName>() {
|
||||
nameGenerator.addRule(new Rule<JsName>() {
|
||||
@Override
|
||||
@Nullable
|
||||
public JsName apply(@NotNull DeclarationDescriptor descriptor) {
|
||||
@@ -227,7 +227,7 @@ public final class TranslationContext {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
nameGenerator.addRule(new Rule<DeclarationDescriptor, JsName>() {
|
||||
nameGenerator.addRule(new Rule<JsName>() {
|
||||
@Override
|
||||
@Nullable
|
||||
public JsName apply(@NotNull DeclarationDescriptor descriptor) {
|
||||
@@ -240,4 +240,33 @@ public final class TranslationContext {
|
||||
return nameGenerator;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Generator<NamingScope> scopeGenerator() {
|
||||
Generator<NamingScope> scopeGenerator = new Generator<NamingScope>();
|
||||
Rule<NamingScope> generateNewScopesForNamespaceDescriptors = new Rule<NamingScope>() {
|
||||
@Override
|
||||
public NamingScope apply(@NotNull DeclarationDescriptor descriptor) {
|
||||
if (!(descriptor instanceof NamespaceDescriptor)) {
|
||||
return null;
|
||||
}
|
||||
return staticContext.getRootScope().innerScope("Namespace " + descriptor.getName());
|
||||
}
|
||||
};
|
||||
Rule<NamingScope> generateInnerScopesForFunctions = new Rule<NamingScope>() {
|
||||
@Override
|
||||
public NamingScope apply(@NotNull DeclarationDescriptor descriptor) {
|
||||
// if (!(descriptor instanceof FunctionDescriptor)) {
|
||||
// return null;
|
||||
// }
|
||||
DeclarationDescriptor containingDeclaration = getContainingDeclaration(descriptor);
|
||||
NamingScope enclosingScope = getScopeForDescriptor(containingDeclaration);
|
||||
return enclosingScope.innerScope("scope for member " + descriptor.getName());
|
||||
}
|
||||
};
|
||||
scopeGenerator.addRule(generateNewScopesForNamespaceDescriptors);
|
||||
scopeGenerator.addRule(generateInnerScopesForFunctions);
|
||||
|
||||
return scopeGenerator;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.jetbrains.k2js.translate.context.generator;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -10,36 +11,36 @@ import java.util.Map;
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
*/
|
||||
public final class Generator<K, V> {
|
||||
public final class Generator<V> {
|
||||
|
||||
|
||||
@NotNull
|
||||
private final Map<K, V> values = Maps.newHashMap();
|
||||
private final Map<DeclarationDescriptor, V> values = Maps.newHashMap();
|
||||
@NotNull
|
||||
private final List<Rule<K, V>> rules = Lists.newArrayList();
|
||||
private final List<Rule<V>> rules = Lists.newArrayList();
|
||||
|
||||
public void addRule(@NotNull Rule<K, V> rule) {
|
||||
public void addRule(@NotNull Rule<V> rule) {
|
||||
rules.add(rule);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public V get(@NotNull K data) {
|
||||
V result = values.get(data);
|
||||
public V get(@NotNull DeclarationDescriptor descriptor) {
|
||||
V result = values.get(descriptor);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
return generate(data);
|
||||
return generate(descriptor);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private V generate(@NotNull K data) {
|
||||
private V generate(@NotNull DeclarationDescriptor descriptor) {
|
||||
V result = null;
|
||||
for (Rule<K, V> rule : rules) {
|
||||
result = rule.apply(data);
|
||||
for (Rule<V> rule : rules) {
|
||||
result = rule.apply(descriptor);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
throw new AssertionError("No rule applicable to generate result for " + data.toString());
|
||||
throw new AssertionError("No rule applicable to generate result for " + descriptor.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,13 @@ package org.jetbrains.k2js.translate.context.generator;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
*/
|
||||
public interface Rule<K, V> {
|
||||
public interface Rule<V> {
|
||||
|
||||
@Nullable
|
||||
V apply(@NotNull K data);
|
||||
V apply(@NotNull DeclarationDescriptor descriptor);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user