Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 class DartBackend extends Backend { | 5 class DartBackend extends Backend { |
| 6 final List<CompilerTask> tasks; | 6 final List<CompilerTask> tasks; |
| 7 final UnparseValidator unparseValidator; | 7 final UnparseValidator unparseValidator; |
| 8 | 8 |
| 9 Map<Element, TreeElements> get resolvedElements() => | 9 Map<Element, TreeElements> get resolvedElements() => |
| 10 compiler.enqueuer.resolution.resolvedElements; | 10 compiler.enqueuer.resolution.resolvedElements; |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 41 ]; | 41 ]; |
| 42 bool shouldOutput(Element element) => | 42 bool shouldOutput(Element element) => |
| 43 element.kind !== ElementKind.VOID && | 43 element.kind !== ElementKind.VOID && |
| 44 LIBS_TO_IGNORE.indexOf(element.getLibrary()) == -1 && | 44 LIBS_TO_IGNORE.indexOf(element.getLibrary()) == -1 && |
| 45 !isDartCoreLib(compiler, element.getLibrary()) && | 45 !isDartCoreLib(compiler, element.getLibrary()) && |
| 46 element is !AbstractFieldElement; | 46 element is !AbstractFieldElement; |
| 47 | 47 |
| 48 final emptyTreeElements = new TreeElementMapping(); | 48 final emptyTreeElements = new TreeElementMapping(); |
| 49 | 49 |
| 50 Set<Element> topLevelElements = new Set<Element>(); | 50 Set<Element> topLevelElements = new Set<Element>(); |
| 51 Map<ClassElement, Set<Element>> classes = | 51 Map<ClassElement, Set<Element>> classMembers = |
| 52 new Map<ClassElement, Set<Element>>(); | 52 new Map<ClassElement, Set<Element>>(); |
| 53 | 53 |
| 54 PlaceholderCollector collector = new PlaceholderCollector(compiler); | 54 PlaceholderCollector collector = new PlaceholderCollector(compiler); |
| 55 var newTypedefElementCallback, newClassElementCallback; | 55 var newTypedefElementCallback, newClassElementCallback; |
| 56 | 56 |
| 57 processElement(element, treeElements) { | 57 processElement(element, treeElements) { |
| 58 collector.collect(element, treeElements); | 58 collector.collect(element, treeElements); |
| 59 new ReferencedElementCollector( | 59 new ReferencedElementCollector( |
| 60 compiler, | 60 compiler, |
| 61 element, treeElements, | 61 element, treeElements, |
| 62 newTypedefElementCallback, newClassElementCallback).collect(); | 62 newTypedefElementCallback, newClassElementCallback).collect(); |
| 63 } | 63 } |
| 64 | 64 |
| 65 addTopLevel(element, treeElements) { | 65 addTopLevel(element, treeElements) { |
| 66 if (topLevelElements.contains(element)) return; | 66 if (topLevelElements.contains(element)) return; |
| 67 topLevelElements.add(element); | 67 topLevelElements.add(element); |
| 68 processElement(element, treeElements); | 68 processElement(element, treeElements); |
| 69 } | 69 } |
| 70 addClass(classElement) { | 70 addClass(classElement) { |
| 71 if (classes.containsKey(classElement)) return; | 71 addTopLevel(classElement, emptyTreeElements); |
| 72 classes[classElement] = new Set<Element>(); | 72 classMembers.putIfAbsent(classElement, () => new Set()); |
| 73 processElement(classElement, emptyTreeElements); | |
| 74 } | 73 } |
| 75 | 74 |
| 76 newTypedefElementCallback = (TypedefElement element) { | 75 newTypedefElementCallback = (TypedefElement element) { |
| 77 if (!shouldOutput(element)) return; | 76 if (!shouldOutput(element)) return; |
| 78 addTopLevel(element, emptyTreeElements); | 77 addTopLevel(element, emptyTreeElements); |
| 79 }; | 78 }; |
| 80 newClassElementCallback = (ClassElement classElement) { | 79 newClassElementCallback = (ClassElement classElement) { |
| 81 if (!shouldOutput(classElement)) return; | 80 if (!shouldOutput(classElement)) return; |
| 82 addClass(classElement); | 81 addClass(classElement); |
| 83 }; | 82 }; |
| 84 | 83 |
| 85 resolvedElements.forEach((element, treeElements) { | 84 resolvedElements.forEach((element, treeElements) { |
| 86 if (!shouldOutput(element)) return; | 85 if (!shouldOutput(element)) return; |
| 87 | 86 |
| 88 if (element.isMember()) { | 87 if (element.isMember()) { |
| 89 ClassElement enclosingClass = element.getEnclosingClass(); | 88 ClassElement enclosingClass = element.getEnclosingClass(); |
| 90 assert(enclosingClass.isClass()); | 89 assert(enclosingClass.isClass()); |
| 91 assert(enclosingClass.isTopLevel()); | 90 assert(enclosingClass.isTopLevel()); |
| 92 assert(shouldOutput(enclosingClass)); | 91 assert(shouldOutput(enclosingClass)); |
| 93 addClass(enclosingClass); | 92 addClass(enclosingClass); |
| 94 classes[enclosingClass].add(element); | 93 classMembers[enclosingClass].add(element); |
| 95 processElement(element, treeElements); | 94 processElement(element, treeElements); |
| 96 } else { | 95 } else { |
| 97 if (!element.isTopLevel()) { | 96 if (!element.isTopLevel()) { |
| 98 compiler.cancel(reason: 'Cannot process $element', element: element); | 97 compiler.cancel(reason: 'Cannot process $element', element: element); |
| 99 } | 98 } |
| 100 addTopLevel(element, treeElements); | 99 addTopLevel(element, treeElements); |
| 101 } | 100 } |
| 102 }); | 101 }); |
| 103 | 102 |
| 104 Map<Node, String> renames = new Map<Node, String>(); | 103 Map<Node, String> renames = new Map<Node, String>(); |
| 105 Map<LibraryElement, String> imports = new Map<LibraryElement, String>(); | 104 Map<LibraryElement, String> imports = new Map<LibraryElement, String>(); |
| 106 renamePlaceholders(compiler, collector, renames, imports); | 105 renamePlaceholders(compiler, collector, renames, imports); |
| 107 | 106 |
| 108 Emitter emitter = new Emitter(compiler, renames); | 107 // Sort elements. |
| 108 compareElements(e0, e1) { | |
|
Roman
2012/08/16 15:23:14
I miss SortedSet now
| |
| 109 compareBy(x, y, f) => f(x).compareTo(f(y)); | |
| 110 int result = compareBy(e0, e1, (e) => e.getLibrary().uri.toString()); | |
| 111 if (result != 0) return result; | |
| 112 return compareBy(e0, e1, (e) => e.position().charOffset); | |
| 113 } | |
| 114 | |
| 115 final sortedTopLevels = new List<Element>.from(topLevelElements); | |
| 116 sortedTopLevels.sort(compareElements); | |
| 117 | |
| 118 final sortedClassMembers = new Map<ClassElement, List<Element>>(); | |
| 119 classMembers.forEach((classElement, members) { | |
| 120 final sortedMembers = new List<Element>.from(members); | |
| 121 sortedMembers.sort(compareElements); | |
| 122 sortedClassMembers[classElement] = sortedMembers; | |
| 123 }); | |
| 124 | |
| 125 Emitter emitter = new Emitter(compiler, renames, sortedClassMembers); | |
| 109 emitter.outputImports(imports); | 126 emitter.outputImports(imports); |
| 110 topLevelElements.forEach(emitter.outputElement); | 127 sortedTopLevels.forEach(emitter.outputElement); |
| 111 classes.forEach(emitter.outputClass); | |
| 112 | 128 |
| 113 compiler.assembledCode = emitter.toString(); | 129 compiler.assembledCode = emitter.toString(); |
| 114 } | 130 } |
| 115 | 131 |
| 116 log(String message) => compiler.log('[DartBackend] $message'); | 132 log(String message) => compiler.log('[DartBackend] $message'); |
| 117 } | 133 } |
| 118 | 134 |
| 119 /** | 135 /** |
| 120 * Checks if [:libraryElement:] is a core lib, that is a library | 136 * Checks if [:libraryElement:] is a core lib, that is a library |
| 121 * provided by the implementation like dart:core, dart:coreimpl, etc. | 137 * provided by the implementation like dart:core, dart:coreimpl, etc. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 if (typeElement.isClass()) newClassElementCallback(typeElement); | 185 if (typeElement.isClass()) newClassElementCallback(typeElement); |
| 170 typeAnnotation.visitChildren(this); | 186 typeAnnotation.visitChildren(this); |
| 171 } | 187 } |
| 172 | 188 |
| 173 void collect() { | 189 void collect() { |
| 174 compiler.withCurrentElement(rootElement, () { | 190 compiler.withCurrentElement(rootElement, () { |
| 175 rootElement.parseNode(compiler).accept(this); | 191 rootElement.parseNode(compiler).accept(this); |
| 176 }); | 192 }); |
| 177 } | 193 } |
| 178 } | 194 } |
| OLD | NEW |