Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(455)

Unified Diff: lib/compiler/implementation/elements/elements.dart

Issue 10824275: Revert "Refactor Library/CompilationUnit and how we define local Scope." (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/compiler/implementation/compiler.dart ('k') | lib/compiler/implementation/enqueue.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/compiler/implementation/elements/elements.dart
diff --git a/lib/compiler/implementation/elements/elements.dart b/lib/compiler/implementation/elements/elements.dart
index e45f581338b55bfb3592daf0db7c70d23da1709d..2344018ca6a1811693d957a920efa8c961bbed8d 100644
--- a/lib/compiler/implementation/elements/elements.dart
+++ b/lib/compiler/implementation/elements/elements.dart
@@ -4,8 +4,6 @@
#library('elements');
-#import('dart:uri');
-
#import('../tree/tree.dart');
#import('../scanner/scannerlib.dart');
#import('../leg.dart'); // TODO(karlklose): we only need type.
@@ -257,43 +255,10 @@ class Element implements Hashable {
}
class ContainerElement extends Element {
- Link<Element> localMembers = const EmptyLink<Element>();
-
- ContainerElement(name, kind, enclosingElement)
- : super(name, kind, enclosingElement);
-
- void addMember(Element element, DiagnosticListener listener) {
- localMembers = localMembers.prepend(element);
- }
-}
-
-class ScopeContainerElement extends ContainerElement {
- final Map<SourceString, Element> localScope;
-
- ScopeContainerElement(name, kind, enclosingElement)
- : super(name, kind, enclosingElement),
- localScope = new Map<SourceString, Element>();
-
- void addMember(Element element, DiagnosticListener listener) {
- super.addMember(element, listener);
- addToScope(element, listener);
- }
-
- void addToScope(Element element, DiagnosticListener listener) {
- if (element.isAccessor()) {
- addGetterOrSetter(element, localScope[element.name], listener);
- } else {
- Element existing = localScope.putIfAbsent(element.name, () => element);
- if (existing !== element) {
- listener.cancel('duplicate definition', token: element.position());
- listener.cancel('existing definition', token: existing.position());
- }
- }
- }
+ ContainerElement(name, kind, enclosingElement) :
+ super(name, kind, enclosingElement);
- Element localLookup(SourceString elementName) {
- return localScope[elementName];
- }
+ abstract void addMember(Element element, DiagnosticListener listener);
void addGetterOrSetter(Element element,
Element existing,
@@ -333,10 +298,9 @@ class ScopeContainerElement extends ContainerElement {
}
}
-
-
class CompilationUnitElement extends ContainerElement {
final Script script;
+ Link<Element> topLevelElements = const EmptyLink<Element>();
CompilationUnitElement(Script script, Element enclosing)
: this.script = script,
@@ -344,30 +308,41 @@ class CompilationUnitElement extends ContainerElement {
ElementKind.COMPILATION_UNIT,
enclosing);
+ CompilationUnitElement.library(Script script)
+ : this.script = script,
+ super(new SourceString(script.name), ElementKind.LIBRARY, null);
+
void addMember(Element element, DiagnosticListener listener) {
- // Keep a list of top level members.
- super.addMember(element, listener);
- // Provide the member to the library to build scope.
- getLibrary().addMember(element, listener);
+ LibraryElement library = enclosingElement;
+ library.addMember(element, listener);
+ topLevelElements = topLevelElements.prepend(element);
+ }
+
+ void define(Element element, DiagnosticListener listener) {
+ LibraryElement library = enclosingElement;
+ library.define(element, listener);
+ }
+
+ void addTag(ScriptTag tag, DiagnosticListener listener) {
+ listener.cancel("script tags not allowed here", node: tag);
}
}
-class LibraryElement extends ScopeContainerElement {
- CompilationUnitElement entryCompilationUnit;
- Link<CompilationUnitElement> compilationUnits =
- const EmptyLink<CompilationUnitElement>();
+class LibraryElement extends CompilationUnitElement {
+ // TODO(ahe): Library element should not be a subclass of
+ // CompilationUnitElement.
+ Link<CompilationUnitElement> compilationUnits =
+ const EmptyLink<CompilationUnitElement>();
Link<ScriptTag> tags = const EmptyLink<ScriptTag>();
ScriptTag libraryTag;
+ Map<SourceString, Element> elements;
bool canUseNative = false;
LibraryElement patch = null;
LibraryElement(Script script)
- : super(new SourceString(script.name), ElementKind.LIBRARY, null) {
- entryCompilationUnit = new CompilationUnitElement(script, this);
- }
-
- Uri get uri() => entryCompilationUnit.script.uri;
+ : elements = new Map<SourceString, Element>(),
+ super.library(script);
bool get isPatched() => patch !== null;
@@ -379,27 +354,45 @@ class LibraryElement extends ScopeContainerElement {
tags = tags.prepend(tag);
}
+ void addMember(Element element, DiagnosticListener listener) {
+ topLevelElements = topLevelElements.prepend(element);
+ define(element, listener);
+ }
+
+ void define(Element element, DiagnosticListener listener) {
+ if (element.kind == ElementKind.GETTER
+ || element.kind == ElementKind.SETTER) {
+ addGetterOrSetter(element, elements[element.name], listener);
+ } else {
+ Element existing = elements.putIfAbsent(element.name, () => element);
+ if (existing !== element) {
+ listener.cancel('duplicate definition', token: element.position());
+ listener.cancel('existing definition', token: existing.position());
+ }
+ }
+ }
+
/** Look up a top-level element in this library. The element could
* potentially have been imported from another library. Returns
* null if no such element exist. */
Element find(SourceString elementName) {
- return localScope[elementName];
+ return elements[elementName];
}
/** Look up a top-level element in this library, but only look for
* non-imported elements. Returns null if no such element exist. */
Element findLocal(SourceString elementName) {
- Element result = localScope[elementName];
+ Element result = elements[elementName];
if (result === null || result.getLibrary() != this) return null;
return result;
}
void forEachExport(f(Element element)) {
- localMembers.forEach((Element e) {
- if (e.kind !== ElementKind.PREFIX
- && e.kind !== ElementKind.FOREIGN
- && !e.name.isPrivate()) {
- f(e);
+ elements.forEach((SourceString _, Element e) {
+ if (this === e.getLibrary()
+ && e.kind !== ElementKind.PREFIX
+ && e.kind !== ElementKind.FOREIGN) {
+ if (!e.name.isPrivate()) f(e);
}
});
}
@@ -416,28 +409,26 @@ class LibraryElement extends ScopeContainerElement {
return libraryTag.argument.dartString.slowToString();
} else {
// Use the file name as script name.
- String path = uri.path;
+ var path = script.uri.path;
return path.substring(path.lastIndexOf('/') + 1);
}
}
- CompilationUnitElement getCompilationUnit() => entryCompilationUnit;
-
Scope buildEnclosingScope() => new TopScope(this);
}
class PrefixElement extends Element {
Map<SourceString, Element> imported;
Token firstPosition;
- final CompilationUnitElement patchCompilationUnit;
+ final CompilationUnitElement patchSource;
PrefixElement(SourceString prefix, Element enclosing, this.firstPosition,
- [this.patchCompilationUnit])
+ [this.patchSource])
: imported = new Map<SourceString, Element>(),
super(prefix, ElementKind.PREFIX, enclosing);
CompilationUnitElement getCompilationUnit() {
- if (patchCompilationUnit !== null) return patchCompilationUnit;
+ if (patchSource !== null) return patchSource;
return super.getCompilationUnit();
}
@@ -888,12 +879,15 @@ abstract class TypeDeclarationElement implements Element {
}
}
-class ClassElement extends ScopeContainerElement
+class ClassElement extends ContainerElement
implements TypeDeclarationElement {
final int id;
InterfaceType type;
Type supertype;
Type defaultClass;
+ Link<Element> members = const EmptyLink<Element>();
+ Map<SourceString, Element> localMembers;
+ Map<SourceString, Element> constructors;
Link<Type> interfaces = const EmptyLink<Type>();
bool isResolved = false;
bool isBeingResolved = false;
@@ -905,7 +899,22 @@ class ClassElement extends ScopeContainerElement
ClassElement patch = null;
ClassElement(SourceString name, CompilationUnitElement enclosing, this.id)
- : super(name, ElementKind.CLASS, enclosing);
+ : localMembers = new Map<SourceString, Element>(),
+ constructors = new Map<SourceString, Element>(),
+ super(name, ElementKind.CLASS, enclosing);
+
+ void addMember(Element element, DiagnosticListener listener) {
+ members = members.prepend(element);
+ if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR ||
+ element.modifiers.isFactory()) {
+ constructors[element.name] = element;
+ } else if (element.kind == ElementKind.GETTER
+ || element.kind == ElementKind.SETTER) {
+ addGetterOrSetter(element, localMembers[element.name], listener);
+ } else {
+ localMembers[element.name] = element;
+ }
+ }
InterfaceType computeType(compiler) {
if (type == null) {
@@ -924,18 +933,10 @@ class ClassElement extends ScopeContainerElement
return this;
}
- /**
- * Lookup local members in the class. This will ignore constructors.
- */
Element lookupLocalMember(SourceString memberName) {
- var result = localLookup(memberName);
- if (result !== null && result.isConstructor()) return null;
- return result;
+ return localMembers[memberName];
}
- /**
- * Lookup super members for the class. This will ignore constructors.
- */
Element lookupSuperMember(SourceString memberName) {
bool isPrivate = memberName.isPrivate();
for (ClassElement s = superclass; s != null; s = s.superclass) {
@@ -957,7 +958,7 @@ class ClassElement extends ScopeContainerElement
* rules, where library scope comes before superclass scope.
*/
Element lookupMember(SourceString memberName) {
- Element localMember = lookupLocalMember(memberName);
+ Element localMember = localMembers[memberName];
return localMember === null ? lookupSuperMember(memberName) : localMember;
}
@@ -1002,31 +1003,13 @@ class ClassElement extends ScopeContainerElement
} else {
normalizedName = className;
}
- Element result = localLookup(normalizedName);
- if (result === null || !result.isConstructor()) {
- result = noMatch !== null ? noMatch(result) : null;
+ Element result = constructors[normalizedName];
+ if (result === null && noMatch !== null) {
+ result = noMatch(lookupLocalMember(constructorName));
}
return result;
}
- bool get hasConstructor() {
- // Search in scope to be sure we search patched constructors.
- for (var element in localScope.getValues()) {
- if (element.isConstructor()) return true;
- }
- return false;
- }
-
- Collection<Element> get constructors() {
- // TODO(ajohnsen): See if we can avoid this method at some point.
- List<Element> result = <Element>[];
- // Search in scope to be sure we search patched constructors.
- localScope.forEach((_, Element value) {
- if (value.isConstructor()) result.add(value);
- });
- return result;
- }
-
/**
* Returns the super class, if any.
*
@@ -1051,7 +1034,7 @@ class ClassElement extends ScopeContainerElement
do {
if (seen.contains(classElement)) return;
seen.add(classElement);
- for (Element element in classElement.localMembers) {
+ for (Element element in classElement.members) {
f(classElement, element);
}
if (includeBackendMembers) {
« no previous file with comments | « lib/compiler/implementation/compiler.dart ('k') | lib/compiler/implementation/enqueue.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698