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 com.google.dart.compiler.backend.js.ast.JsRootScope;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
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.resolve.BindingContext;
|
||||||
import org.jetbrains.jet.lang.types.JetStandardLibrary;
|
import org.jetbrains.jet.lang.types.JetStandardLibrary;
|
||||||
import org.jetbrains.k2js.translate.context.declaration.DeclarationFacade;
|
import org.jetbrains.k2js.translate.context.declaration.DeclarationFacade;
|
||||||
import org.jetbrains.k2js.translate.context.declaration.Declarations;
|
import org.jetbrains.k2js.translate.context.declaration.Declarations;
|
||||||
import org.jetbrains.k2js.translate.intrinsic.Intrinsics;
|
import org.jetbrains.k2js.translate.intrinsic.Intrinsics;
|
||||||
import org.jetbrains.k2js.translate.utils.BindingUtils;
|
|
||||||
|
|
||||||
public class StaticContext {
|
public class StaticContext {
|
||||||
|
|
||||||
@@ -111,18 +109,6 @@ public class StaticContext {
|
|||||||
return standardClasses;
|
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
|
//TODO: consider using nullable return value instead
|
||||||
@NotNull
|
@NotNull
|
||||||
public JsName getGlobalName(@NotNull DeclarationDescriptor descriptor) {
|
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.intrinsic.Intrinsics;
|
||||||
import org.jetbrains.k2js.translate.utils.BindingUtils;
|
import org.jetbrains.k2js.translate.utils.BindingUtils;
|
||||||
|
|
||||||
|
import static org.jetbrains.k2js.translate.utils.DescriptorUtils.getContainingDeclaration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
*/
|
*/
|
||||||
@@ -27,13 +29,10 @@ public final class TranslationContext {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private final StaticContext staticContext;
|
private final StaticContext staticContext;
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Generator<DeclarationDescriptor, JsName> names =
|
private final Generator<JsName> names = nameGenerator();
|
||||||
nameGenerator();
|
|
||||||
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Generator<DeclarationDescriptor, NamingScope> scopes =
|
private final Generator<NamingScope> scopes = scopeGenerator();
|
||||||
new Generator<DeclarationDescriptor, NamingScope>();
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static TranslationContext rootContext(@NotNull StaticContext staticContext) {
|
public static TranslationContext rootContext(@NotNull StaticContext staticContext) {
|
||||||
@@ -104,12 +103,13 @@ public final class TranslationContext {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public NamingScope getScopeForDescriptor(@NotNull DeclarationDescriptor descriptor) {
|
public NamingScope getScopeForDescriptor(@NotNull DeclarationDescriptor descriptor) {
|
||||||
return staticContext.getScopeForDescriptor(descriptor);
|
return scopes.get(descriptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public NamingScope getScopeForElement(@NotNull JetElement element) {
|
public NamingScope getScopeForElement(@NotNull JetElement element) {
|
||||||
return staticContext.getScopeForElement(element);
|
DeclarationDescriptor descriptorForElement = BindingUtils.getDescriptorForElement(bindingContext(), element);
|
||||||
|
return getScopeForDescriptor(descriptorForElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -195,9 +195,9 @@ public final class TranslationContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private Generator<DeclarationDescriptor, JsName> nameGenerator() {
|
private Generator<JsName> nameGenerator() {
|
||||||
Generator<DeclarationDescriptor, JsName> nameGenerator = new Generator<DeclarationDescriptor, JsName>();
|
Generator<JsName> nameGenerator = new Generator<JsName>();
|
||||||
nameGenerator.addRule(new Rule<DeclarationDescriptor, JsName>() {
|
nameGenerator.addRule(new Rule<JsName>() {
|
||||||
@Override
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
public JsName apply(@NotNull DeclarationDescriptor data) {
|
public JsName apply(@NotNull DeclarationDescriptor data) {
|
||||||
@@ -207,7 +207,7 @@ public final class TranslationContext {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
nameGenerator.addRule(new Rule<DeclarationDescriptor, JsName>() {
|
nameGenerator.addRule(new Rule<JsName>() {
|
||||||
@Override
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
public JsName apply(@NotNull DeclarationDescriptor data) {
|
public JsName apply(@NotNull DeclarationDescriptor data) {
|
||||||
@@ -217,7 +217,7 @@ public final class TranslationContext {
|
|||||||
return standardClasses().getStandardObjectName(data);
|
return standardClasses().getStandardObjectName(data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
nameGenerator.addRule(new Rule<DeclarationDescriptor, JsName>() {
|
nameGenerator.addRule(new Rule<JsName>() {
|
||||||
@Override
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
public JsName apply(@NotNull DeclarationDescriptor descriptor) {
|
public JsName apply(@NotNull DeclarationDescriptor descriptor) {
|
||||||
@@ -227,7 +227,7 @@ public final class TranslationContext {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
nameGenerator.addRule(new Rule<DeclarationDescriptor, JsName>() {
|
nameGenerator.addRule(new Rule<JsName>() {
|
||||||
@Override
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
public JsName apply(@NotNull DeclarationDescriptor descriptor) {
|
public JsName apply(@NotNull DeclarationDescriptor descriptor) {
|
||||||
@@ -240,4 +240,33 @@ public final class TranslationContext {
|
|||||||
return nameGenerator;
|
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.Lists;
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -10,36 +11,36 @@ import java.util.Map;
|
|||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
*/
|
*/
|
||||||
public final class Generator<K, V> {
|
public final class Generator<V> {
|
||||||
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Map<K, V> values = Maps.newHashMap();
|
private final Map<DeclarationDescriptor, V> values = Maps.newHashMap();
|
||||||
@NotNull
|
@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);
|
rules.add(rule);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public V get(@NotNull K data) {
|
public V get(@NotNull DeclarationDescriptor descriptor) {
|
||||||
V result = values.get(data);
|
V result = values.get(descriptor);
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
return generate(data);
|
return generate(descriptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private V generate(@NotNull K data) {
|
private V generate(@NotNull DeclarationDescriptor descriptor) {
|
||||||
V result = null;
|
V result = null;
|
||||||
for (Rule<K, V> rule : rules) {
|
for (Rule<V> rule : rules) {
|
||||||
result = rule.apply(data);
|
result = rule.apply(descriptor);
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
return result;
|
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.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
*/
|
*/
|
||||||
public interface Rule<K, V> {
|
public interface Rule<V> {
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
V apply(@NotNull K data);
|
V apply(@NotNull DeclarationDescriptor descriptor);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user