| 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 bool minify; | 7 final bool minify; |
| 8 final bool cutDeclarationTypes; | 8 final bool cutDeclarationTypes; |
| 9 | 9 |
| 10 Map<Element, TreeElements> get resolvedElements => | 10 Map<Element, TreeElements> get resolvedElements => |
| 11 compiler.enqueuer.resolution.resolvedElements; | 11 compiler.enqueuer.resolution.resolvedElements; |
| 12 | 12 |
| 13 DartBackend(Compiler compiler, this.minify, this.cutDeclarationTypes) | 13 DartBackend(Compiler compiler, this.minify, this.cutDeclarationTypes) |
| 14 : tasks = <CompilerTask>[], | 14 : tasks = <CompilerTask>[], |
| 15 super(compiler); | 15 super(compiler); |
| 16 | 16 |
| 17 void enqueueHelpers(Enqueuer world) { } | 17 void enqueueHelpers(Enqueuer world) { |
| 18 // Right now resolver doesn't always resolve interfaces needed |
| 19 // for literals, so force them. TODO(antonm): fix in the resolver. |
| 20 final LITERAL_TYPE_NAMES = const [ |
| 21 'Map', 'List', 'num', 'int', 'double', 'bool' |
| 22 ]; |
| 23 final coreLibrary = compiler.coreLibrary; |
| 24 for (final name in LITERAL_TYPE_NAMES) { |
| 25 ClassElement classElement = coreLibrary.findLocal(new SourceString(name)); |
| 26 classElement.ensureResolved(compiler); |
| 27 } |
| 28 } |
| 18 void codegen(WorkItem work) { } | 29 void codegen(WorkItem work) { } |
| 19 void processNativeClasses(Enqueuer world, | 30 void processNativeClasses(Enqueuer world, |
| 20 Collection<LibraryElement> libraries) { } | 31 Collection<LibraryElement> libraries) { } |
| 21 | 32 |
| 22 void assembleProgram() { | 33 void assembleProgram() { |
| 34 // Conservatively traverse all platform libraries and collect member names. |
| 35 // TODO(antonm): ideally we should only collect names of used members, |
| 36 // however as of today there are problems with names of some core library |
| 37 // interfaces, most probably for interfaces of literals. |
| 38 final fixedMemberNames = new Set<String>(); |
| 39 for (final library in compiler.libraries.getValues()) { |
| 40 if (!library.isPlatformLibrary) continue; |
| 41 for (final element in library.localMembers) { |
| 42 if (element is ClassElement) { |
| 43 ClassElement classElement = element; |
| 44 for (final member in classElement.localMembers) { |
| 45 final name = member.name.slowToString(); |
| 46 // Skip operator names. |
| 47 if (name.startsWith(@'operator$')) continue; |
| 48 // Fetch name of named constructors and factories if any, |
| 49 // otherwise store regular name. |
| 50 // TODO(antonm): better way to analyze the name. |
| 51 fixedMemberNames.add(name.split(@'$').last()); |
| 52 } |
| 53 } else { |
| 54 fixedMemberNames.add(element.name.slowToString()); |
| 55 } |
| 56 } |
| 57 } |
| 58 // TODO(antonm): TypeError.srcType and TypeError.dstType are defined in |
| 59 // runtime/lib/error.dart. Overall, all DartVM specific libs should be |
| 60 // accounted for. |
| 61 fixedMemberNames.add('srcType'); |
| 62 fixedMemberNames.add('dstType'); |
| 63 |
| 23 /** | 64 /** |
| 24 * Tells whether we should output given element. Corelib classes like | 65 * Tells whether we should output given element. Corelib classes like |
| 25 * Object should not be in the resulting code. | 66 * Object should not be in the resulting code. |
| 26 */ | 67 */ |
| 27 final LIBS_TO_IGNORE = [ | 68 final LIBS_TO_IGNORE = [ |
| 28 compiler.jsHelperLibrary, | 69 compiler.jsHelperLibrary, |
| 29 compiler.interceptorsLibrary, | 70 compiler.interceptorsLibrary, |
| 30 ]; | 71 ]; |
| 31 bool shouldOutput(Element element) => | 72 bool shouldOutput(Element element) => |
| 32 element.kind !== ElementKind.VOID && | 73 element.kind !== ElementKind.VOID && |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 processElement(element, treeElements); | 123 processElement(element, treeElements); |
| 83 } else { | 124 } else { |
| 84 if (!element.isTopLevel()) { | 125 if (!element.isTopLevel()) { |
| 85 compiler.cancel(reason: 'Cannot process $element', element: element); | 126 compiler.cancel(reason: 'Cannot process $element', element: element); |
| 86 } | 127 } |
| 87 addTopLevel(element, treeElements); | 128 addTopLevel(element, treeElements); |
| 88 } | 129 } |
| 89 }); | 130 }); |
| 90 | 131 |
| 91 // Create all necessary placeholders. | 132 // Create all necessary placeholders. |
| 92 PlaceholderCollector collector = new PlaceholderCollector(compiler); | 133 PlaceholderCollector collector = |
| 134 new PlaceholderCollector(compiler, fixedMemberNames); |
| 93 makePlaceholders(element) { | 135 makePlaceholders(element) { |
| 94 TreeElements treeElements = resolvedElements[element]; | 136 TreeElements treeElements = resolvedElements[element]; |
| 95 if (treeElements === null) treeElements = emptyTreeElements; | 137 if (treeElements === null) treeElements = emptyTreeElements; |
| 96 collector.collect(element, treeElements); | 138 collector.collect(element, treeElements); |
| 97 if (element is ClassElement) { | 139 if (element is ClassElement) { |
| 98 classMembers[element].forEach(makePlaceholders); | 140 classMembers[element].forEach(makePlaceholders); |
| 99 } | 141 } |
| 100 } | 142 } |
| 101 topLevelElements.forEach(makePlaceholders); | 143 topLevelElements.forEach(makePlaceholders); |
| 102 | 144 |
| 103 // Create renames. | 145 // Create renames. |
| 104 Map<Node, String> renames = new Map<Node, String>(); | 146 Map<Node, String> renames = new Map<Node, String>(); |
| 105 Map<LibraryElement, String> imports = new Map<LibraryElement, String>(); | 147 Map<LibraryElement, String> imports = new Map<LibraryElement, String>(); |
| 106 renamePlaceholders( | 148 renamePlaceholders( |
| 107 compiler, collector, renames, imports, minify, cutDeclarationTypes); | 149 compiler, collector, renames, imports, |
| 150 fixedMemberNames, minify, cutDeclarationTypes); |
| 108 | 151 |
| 109 // Sort elements. | 152 // Sort elements. |
| 110 final sortedTopLevels = sortElements(topLevelElements); | 153 final sortedTopLevels = sortElements(topLevelElements); |
| 111 final sortedClassMembers = new Map<ClassElement, List<Element>>(); | 154 final sortedClassMembers = new Map<ClassElement, List<Element>>(); |
| 112 classMembers.forEach((classElement, members) { | 155 classMembers.forEach((classElement, members) { |
| 113 sortedClassMembers[classElement] = sortElements(members); | 156 sortedClassMembers[classElement] = sortElements(members); |
| 114 }); | 157 }); |
| 115 | 158 |
| 116 final unparser = new Unparser.withRenamer((Node node) => renames[node]); | 159 final unparser = new Unparser.withRenamer((Node node) => renames[node]); |
| 117 compiler.assembledCode = emitCode( | 160 compiler.assembledCode = emitCode( |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 | 223 |
| 181 List<Element> sortElements(Collection<Element> elements) { | 224 List<Element> sortElements(Collection<Element> elements) { |
| 182 compareElements(e0, e1) { | 225 compareElements(e0, e1) { |
| 183 int result = compareBy((e) => e.getLibrary().uri.toString())(e0, e1); | 226 int result = compareBy((e) => e.getLibrary().uri.toString())(e0, e1); |
| 184 if (result != 0) return result; | 227 if (result != 0) return result; |
| 185 return compareBy((e) => e.position().charOffset)(e0, e1); | 228 return compareBy((e) => e.position().charOffset)(e0, e1); |
| 186 } | 229 } |
| 187 | 230 |
| 188 return sorted(elements, compareElements); | 231 return sorted(elements, compareElements); |
| 189 } | 232 } |
| OLD | NEW |