| 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 BailoutException { | 5 class BailoutException { |
| 6 final String reason; | 6 final String reason; |
| 7 | 7 |
| 8 const BailoutException(this.reason); | 8 const BailoutException(this.reason); |
| 9 } | 9 } |
| 10 | 10 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 final LIBS_TO_IGNORE = [ | 63 final LIBS_TO_IGNORE = [ |
| 64 compiler.jsHelperLibrary, | 64 compiler.jsHelperLibrary, |
| 65 compiler.interceptorsLibrary, | 65 compiler.interceptorsLibrary, |
| 66 ]; | 66 ]; |
| 67 bool shouldOutput(Element element) => | 67 bool shouldOutput(Element element) => |
| 68 element.kind !== ElementKind.VOID && | 68 element.kind !== ElementKind.VOID && |
| 69 LIBS_TO_IGNORE.indexOf(element.getLibrary()) == -1 && | 69 LIBS_TO_IGNORE.indexOf(element.getLibrary()) == -1 && |
| 70 !isDartCoreLib(compiler, element.getLibrary()); | 70 !isDartCoreLib(compiler, element.getLibrary()); |
| 71 | 71 |
| 72 try { | 72 try { |
| 73 Set<TypedefElement> typedefs = new Set<TypedefElement>(); |
| 73 PlaceholderCollector collector = new PlaceholderCollector(compiler); | 74 PlaceholderCollector collector = new PlaceholderCollector(compiler); |
| 74 resolvedElements.forEach((element, treeElements) { | 75 resolvedElements.forEach((element, treeElements) { |
| 75 if (!shouldOutput(element)) return; | 76 if (!shouldOutput(element)) return; |
| 77 if (element is AbstractFieldElement) return; |
| 76 collector.collect(element, treeElements); | 78 collector.collect(element, treeElements); |
| 79 new ReferencedElementCollector( |
| 80 compiler, element, treeElements, typedefs) |
| 81 .collect(); |
| 77 }); | 82 }); |
| 78 | 83 |
| 79 ConflictingRenamer renamer = | 84 ConflictingRenamer renamer = |
| 80 new ConflictingRenamer(compiler, collector.placeholders); | 85 new ConflictingRenamer(compiler, collector.placeholders); |
| 81 Emitter emitter = new Emitter(compiler, renamer); | 86 Emitter emitter = new Emitter(compiler, renamer); |
| 82 resolvedElements.forEach((element, treeElements) { | 87 resolvedElements.forEach((element, treeElements) { |
| 83 if (!shouldOutput(element)) return; | 88 if (!shouldOutput(element)) return; |
| 84 if (element.isMember()) { | 89 if (element.isMember()) { |
| 85 ClassElement enclosingClass = element.getEnclosingClass(); | 90 ClassElement enclosingClass = element.getEnclosingClass(); |
| 86 assert(enclosingClass.isClass()); | 91 assert(enclosingClass.isClass()); |
| 87 assert(enclosingClass.isTopLevel()); | 92 assert(enclosingClass.isTopLevel()); |
| 88 addMemberToClass(element, enclosingClass); | 93 addMemberToClass(element, enclosingClass); |
| 89 return; | 94 return; |
| 90 } | 95 } |
| 91 if (!element.isTopLevel()) { | 96 if (!element.isTopLevel()) { |
| 92 bailout('Cannot process non top-level $element'); | 97 bailout('Cannot process non top-level $element'); |
| 93 } | 98 } |
| 94 | 99 |
| 95 emitter.outputElement(element); | 100 emitter.outputElement(element); |
| 96 }); | 101 }); |
| 97 | 102 |
| 103 typedefs.forEach(emitter.outputElement); |
| 104 |
| 98 // Now output resolved classes with inner elements we met before. | 105 // Now output resolved classes with inner elements we met before. |
| 99 resolvedClassMembers.forEach(emitter.outputClass); | 106 resolvedClassMembers.forEach(emitter.outputClass); |
| 100 compiler.assembledCode = emitter.toString(); | 107 compiler.assembledCode = emitter.toString(); |
| 101 } catch (BailoutException e) { | 108 } catch (BailoutException e) { |
| 102 compiler.assembledCode = ''' | 109 compiler.assembledCode = ''' |
| 103 main() { | 110 main() { |
| 104 final bailout_reason = "${e.reason}"; | 111 final bailout_reason = "${e.reason}"; |
| 105 } | 112 } |
| 106 '''; | 113 '''; |
| 107 } | 114 } |
| 108 } | 115 } |
| 109 | 116 |
| 110 log(String message) => compiler.log('[DartBackend] $message'); | 117 log(String message) => compiler.log('[DartBackend] $message'); |
| 111 } | 118 } |
| 112 | 119 |
| 113 /** | 120 /** |
| 114 * Checks if [:libraryElement:] is a core lib, that is a library | 121 * Checks if [:libraryElement:] is a core lib, that is a library |
| 115 * provided by the implementation like dart:core, dart:coreimpl, etc. | 122 * provided by the implementation like dart:core, dart:coreimpl, etc. |
| 116 */ | 123 */ |
| 117 bool isDartCoreLib(Compiler compiler, LibraryElement libraryElement) { | 124 bool isDartCoreLib(Compiler compiler, LibraryElement libraryElement) { |
| 118 final libraries = compiler.libraries; | 125 final libraries = compiler.libraries; |
| 119 for (final uri in libraries.getKeys()) { | 126 for (final uri in libraries.getKeys()) { |
| 120 if (libraryElement === libraries[uri]) { | 127 if (libraryElement === libraries[uri]) { |
| 121 if (uri.startsWith('dart:')) return true; | 128 if (uri.startsWith('dart:')) return true; |
| 122 } | 129 } |
| 123 } | 130 } |
| 124 return false; | 131 return false; |
| 125 } | 132 } |
| 133 |
| 134 /** |
| 135 * Some elements are not recorded by resolver now, |
| 136 * for example, typedefs or classes which are only |
| 137 * used in signatures, as/is operators or in super clauses |
| 138 * (just to name a few). Retraverse AST to pick those up. |
| 139 */ |
| 140 class ReferencedElementCollector extends AbstractVisitor { |
| 141 final Compiler compiler; |
| 142 final Element element; |
| 143 final TreeElements treeElements; |
| 144 final Set<TypedefElement> typedefs; |
| 145 |
| 146 ReferencedElementCollector( |
| 147 this.compiler, |
| 148 this.element, this.treeElements, |
| 149 this.typedefs); |
| 150 |
| 151 visitNode(Node node) { node.visitChildren(this); } |
| 152 |
| 153 visitTypeAnnotation(TypeAnnotation typeAnnotation) { |
| 154 Element element = treeElements[typeAnnotation]; |
| 155 if (element !== null) { |
| 156 if (element.isTypedef()) typedefs.add(element); |
| 157 } |
| 158 typeAnnotation.visitChildren(this); |
| 159 } |
| 160 |
| 161 void collect() { |
| 162 element.parseNode(compiler).accept(this); |
| 163 } |
| 164 } |
| OLD | NEW |