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

Unified Diff: lib/compiler/implementation/dart_backend/backend.dart

Issue 10855199: Refactor program traversing logic. (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 | « no previous file | tests/compiler/dart2js/unparser_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/compiler/implementation/dart_backend/backend.dart
diff --git a/lib/compiler/implementation/dart_backend/backend.dart b/lib/compiler/implementation/dart_backend/backend.dart
index 5fb537de5128adecfaea71b4f1eb2d6a6b051b98..5a9e0e770c95710c926f309d4e193e0ef1aea53e 100644
--- a/lib/compiler/implementation/dart_backend/backend.dart
+++ b/lib/compiler/implementation/dart_backend/backend.dart
@@ -45,61 +45,71 @@ class DartBackend extends Backend {
!isDartCoreLib(compiler, element.getLibrary()) &&
element is !AbstractFieldElement;
- // TODO(smok): Refactor this traverse/collect mess.
- Set<TypedefElement> typedefs = new Set<TypedefElement>();
- Set<ClassElement> classes = new Set<ClassElement>();
- Set<Element> elements = new Set<Element>();
- Map<ClassElement, Set<Element>> resolvedClassMembers =
+ final emptyTreeElements = new TreeElementMapping();
+
+ Set<Element> topLevelElements = new Set<Element>();
+ Map<ClassElement, Set<Element>> classes =
new Map<ClassElement, Set<Element>>();
+
PlaceholderCollector collector = new PlaceholderCollector(compiler);
+ var newTypedefElementCallback, newClassElementCallback;
+
+ processElement(element, treeElements) {
Roman 2012/08/16 14:23:54 How about refactoring all this stuff to a separate
Anton Muhin 2012/08/16 14:30:44 That sounds really interesting, thanks a lot, let
+ collector.collect(element, treeElements);
+ new ReferencedElementCollector(
+ compiler,
+ element, treeElements,
+ newTypedefElementCallback, newClassElementCallback).collect();
+ }
+
+ addTopLevel(element, treeElements) {
+ if (topLevelElements.contains(element)) return;
+ topLevelElements.add(element);
+ processElement(element, treeElements);
+ }
+ addClass(classElement) {
+ if (classes.containsKey(classElement)) return;
+ classes[classElement] = new Set<Element>();
+ processElement(classElement, emptyTreeElements);
+ }
+
+ newTypedefElementCallback = (TypedefElement element) {
+ if (!shouldOutput(element)) return;
+ addTopLevel(element, emptyTreeElements);
+ };
+ newClassElementCallback = (ClassElement classElement) {
+ if (!shouldOutput(classElement)) return;
+ addClass(classElement);
+ };
+
resolvedElements.forEach((element, treeElements) {
if (!shouldOutput(element)) return;
+
if (element.isMember()) {
ClassElement enclosingClass = element.getEnclosingClass();
assert(enclosingClass.isClass());
assert(enclosingClass.isTopLevel());
- resolvedClassMembers
- .putIfAbsent(enclosingClass, () => new Set<Element>())
- .add(element);
- return;
+ assert(shouldOutput(enclosingClass));
+ addClass(enclosingClass);
+ classes[enclosingClass].add(element);
+ processElement(element, treeElements);
+ } else {
+ if (!element.isTopLevel()) {
+ compiler.cancel(reason: 'Cannot process $element', element: element);
+ }
+ addTopLevel(element, treeElements);
}
- if (!element.isTopLevel()) {
- compiler.cancel(reason: 'Cannot process $element', element: element);
- }
-
- elements.add(element);
- });
- resolvedElements.forEach((element, treeElements) {
- if (!shouldOutput(element)) return;
- collector.collect(element, treeElements);
- new ReferencedElementCollector(
- compiler, element, treeElements, typedefs, classes)
- .collect();
});
- final emptyTreeElements = new TreeElementMapping();
- collectElement(element) { collector.collect(element, emptyTreeElements); }
- typedefs.forEach(collectElement);
- classes.forEach(collectElement);
- resolvedClassMembers.getKeys().forEach(collectElement);
-
Map<Node, String> renames = new Map<Node, String>();
Map<LibraryElement, String> imports = new Map<LibraryElement, String>();
renamePlaceholders(compiler, collector, renames, imports);
Emitter emitter = new Emitter(compiler, renames);
emitter.outputImports(imports);
- elements.forEach(emitter.outputElement);
- typedefs.forEach(emitter.outputElement);
- final emptySet = new Set<Element>();
- classes.forEach((classElement) {
- if (!shouldOutput(classElement)) return;
- if (resolvedClassMembers.containsKey(classElement)) return;
- emitter.outputClass(classElement, emptySet);
- });
+ topLevelElements.forEach(emitter.outputElement);
+ classes.forEach(emitter.outputClass);
- // Now output resolved classes with inner elements we met before.
- resolvedClassMembers.forEach(emitter.outputClass);
compiler.assembledCode = emitter.toString();
}
@@ -130,19 +140,13 @@ class ReferencedElementCollector extends AbstractVisitor {
final Compiler compiler;
final Element rootElement;
final TreeElements treeElements;
- final Set<TypedefElement> typedefs;
- final Set<ClassElement> classes;
+ final newTypedefElementCallback;
+ final newClassElementCallback;
ReferencedElementCollector(
this.compiler,
this.rootElement, this.treeElements,
- this.typedefs, this.classes);
-
- void collectElement(Element element) {
- new ReferencedElementCollector(
- compiler, element, new TreeElementMapping(), typedefs, classes)
- .collect();
- }
+ this.newTypedefElementCallback, this.newClassElementCallback);
visitClassNode(ClassNode node) {
super.visitClassNode(node);
@@ -161,14 +165,8 @@ class ReferencedElementCollector extends AbstractVisitor {
visitTypeAnnotation(TypeAnnotation typeAnnotation) {
final type = compiler.resolveTypeAnnotation(rootElement, typeAnnotation);
Element typeElement = type.element;
- if (typeElement.isTypedef() && !typedefs.contains(typeElement)) {
- typedefs.add(typeElement);
- collectElement(typeElement);
- }
- if (typeElement.isClass() && !classes.contains(typeElement)) {
- classes.add(typeElement);
- collectElement(typeElement);
- }
+ if (typeElement.isTypedef()) newTypedefElementCallback(typeElement);
+ if (typeElement.isClass()) newClassElementCallback(typeElement);
typeAnnotation.visitChildren(this);
}
« no previous file with comments | « no previous file | tests/compiler/dart2js/unparser_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698