| 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 EnqueueTask extends CompilerTask { | 5 class EnqueueTask extends CompilerTask { |
| 6 final Enqueuer codegen; | 6 final Enqueuer codegen; |
| 7 final Enqueuer resolution; | 7 final Enqueuer resolution; |
| 8 | 8 |
| 9 String get name => 'Enqueue'; | 9 String get name => 'Enqueue'; |
| 10 | 10 |
| 11 EnqueueTask(Compiler compiler) | 11 EnqueueTask(Compiler compiler) |
| 12 : codegen = new Enqueuer(compiler, | 12 : codegen = new Enqueuer('codegen enqueuer', compiler, |
| 13 compiler.backend.createItemCompilationContext), | 13 compiler.backend.createItemCompilationContext), |
| 14 resolution = new Enqueuer(compiler, | 14 resolution = new Enqueuer('resolution enqueuer', compiler, |
| 15 compiler.backend.createItemCompilationContext), | 15 compiler.backend.createItemCompilationContext), |
| 16 super(compiler) { | 16 super(compiler) { |
| 17 codegen.task = this; | 17 codegen.task = this; |
| 18 resolution.task = this; | 18 resolution.task = this; |
| 19 } | 19 } |
| 20 } | 20 } |
| 21 | 21 |
| 22 class RecompilationQueue { | 22 class RecompilationQueue { |
| 23 final Function itemCompilationContextCreator; | 23 final Function itemCompilationContextCreator; |
| 24 final Queue<WorkItem> queue; | 24 final Queue<WorkItem> queue; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 42 | 42 |
| 43 WorkItem next() { | 43 WorkItem next() { |
| 44 WorkItem item = queue.removeLast(); | 44 WorkItem item = queue.removeLast(); |
| 45 queueElements.remove(item.element); | 45 queueElements.remove(item.element); |
| 46 processed++; | 46 processed++; |
| 47 return item; | 47 return item; |
| 48 } | 48 } |
| 49 } | 49 } |
| 50 | 50 |
| 51 class Enqueuer { | 51 class Enqueuer { |
| 52 final String name; |
| 52 final Compiler compiler; // TODO(ahe): Remove this dependency. | 53 final Compiler compiler; // TODO(ahe): Remove this dependency. |
| 53 final Function itemCompilationContextCreator; | 54 final Function itemCompilationContextCreator; |
| 54 final Map<String, Link<Element>> instanceMembersByName; | 55 final Map<String, Link<Element>> instanceMembersByName; |
| 55 final Set<ClassElement> seenClasses; | 56 final Set<ClassElement> seenClasses; |
| 56 final Universe universe; | 57 final Universe universe; |
| 57 final Queue<WorkItem> queue; | 58 final Queue<WorkItem> queue; |
| 59 |
| 60 /** |
| 61 * Map from declaration elements to the [TreeElements] object holding the |
| 62 * resolution mapping for the element implementation. |
| 63 * |
| 64 * Invariant: Key elements are declaration elements. |
| 65 */ |
| 58 final Map<Element, TreeElements> resolvedElements; | 66 final Map<Element, TreeElements> resolvedElements; |
| 59 final RecompilationQueue recompilationCandidates; | 67 final RecompilationQueue recompilationCandidates; |
| 60 | 68 |
| 61 bool queueIsClosed = false; | 69 bool queueIsClosed = false; |
| 62 EnqueueTask task; | 70 EnqueueTask task; |
| 63 | 71 |
| 64 Enqueuer(this.compiler, | 72 Enqueuer(this.name, this.compiler, |
| 65 ItemCompilationContext itemCompilationContextCreator()) | 73 ItemCompilationContext itemCompilationContextCreator()) |
| 66 : this.itemCompilationContextCreator = itemCompilationContextCreator, | 74 : this.itemCompilationContextCreator = itemCompilationContextCreator, |
| 67 instanceMembersByName = new Map<String, Link<Element>>(), | 75 instanceMembersByName = new Map<String, Link<Element>>(), |
| 68 seenClasses = new Set<ClassElement>(), | 76 seenClasses = new Set<ClassElement>(), |
| 69 universe = new Universe(), | 77 universe = new Universe(), |
| 70 queue = new Queue<WorkItem>(), | 78 queue = new Queue<WorkItem>(), |
| 71 resolvedElements = new Map<Element, TreeElements>(), | 79 resolvedElements = new Map<Element, TreeElements>(), |
| 72 recompilationCandidates = | 80 recompilationCandidates = |
| 73 new RecompilationQueue(itemCompilationContextCreator); | 81 new RecompilationQueue(itemCompilationContextCreator); |
| 74 | 82 |
| 75 bool get isResolutionQueue => compiler.enqueuer.resolution === this; | 83 bool get isResolutionQueue => compiler.enqueuer.resolution === this; |
| 76 | 84 |
| 77 TreeElements getCachedElements(Element element) { | 85 TreeElements getCachedElements(Element element) { |
| 78 // TODO(ngeoffray): Get rid of this check. | 86 // TODO(ngeoffray): Get rid of this check. |
| 79 if (element.enclosingElement.isClosure()) { | 87 if (element.enclosingElement.isClosure()) { |
| 80 closureMapping.ClosureClassElement cls = element.enclosingElement; | 88 closureMapping.ClosureClassElement cls = element.enclosingElement; |
| 81 element = cls.methodElement; | 89 element = cls.methodElement; |
| 82 } | 90 } |
| 83 Element owner = element.getOutermostEnclosingMemberOrTopLevel(); | 91 Element owner = element.getOutermostEnclosingMemberOrTopLevel(); |
| 84 return compiler.enqueuer.resolution.resolvedElements[owner]; | 92 return compiler.enqueuer.resolution.resolvedElements[owner.declaration]; |
| 85 } | 93 } |
| 86 | 94 |
| 87 String lookupCode(Element element) => | 95 /** |
| 88 universe.generatedCode[element].toString(); | 96 * Documentation wanted -- johnniwinther |
| 97 * |
| 98 * Invariant: [element] must be a declaration element. |
| 99 */ |
| 100 String lookupCode(Element element) { |
| 101 assert(invariant(element, element.isDeclaration)); |
| 102 return universe.generatedCode[element].toString(); |
| 103 } |
| 89 | 104 |
| 105 /** |
| 106 * Documentation wanted -- johnniwinther |
| 107 * |
| 108 * Invariant: [element] must be a declaration element. |
| 109 */ |
| 90 void addToWorkList(Element element, [TreeElements elements]) { | 110 void addToWorkList(Element element, [TreeElements elements]) { |
| 111 assert(invariant(element, element.isDeclaration)); |
| 91 if (element.isForeign()) return; | 112 if (element.isForeign()) return; |
| 92 if (compiler.phase == Compiler.PHASE_RECOMPILING) return; | 113 if (compiler.phase == Compiler.PHASE_RECOMPILING) return; |
| 93 if (queueIsClosed) { | 114 if (queueIsClosed) { |
| 94 if (isResolutionQueue && getCachedElements(element) !== null) return; | 115 if (isResolutionQueue && getCachedElements(element) !== null) return; |
| 95 compiler.internalErrorOnElement(element, "Work list is closed."); | 116 compiler.internalErrorOnElement(element, "Work list is closed."); |
| 96 } | 117 } |
| 97 if (!isResolutionQueue && | 118 if (!isResolutionQueue && |
| 98 element.kind === ElementKind.GENERATIVE_CONSTRUCTOR) { | 119 element.kind === ElementKind.GENERATIVE_CONSTRUCTOR) { |
| 99 registerInstantiatedClass(element.getEnclosingClass()); | 120 registerInstantiatedClass(element.getEnclosingClass()); |
| 100 } | 121 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 114 | 135 |
| 115 // Enable isolate support if we start using something from the | 136 // Enable isolate support if we start using something from the |
| 116 // isolate library. | 137 // isolate library. |
| 117 LibraryElement library = element.getLibrary(); | 138 LibraryElement library = element.getLibrary(); |
| 118 if (!compiler.hasIsolateSupport() | 139 if (!compiler.hasIsolateSupport() |
| 119 && library.uri.toString() == 'dart:isolate') { | 140 && library.uri.toString() == 'dart:isolate') { |
| 120 compiler.enableIsolateSupport(library); | 141 compiler.enableIsolateSupport(library); |
| 121 } | 142 } |
| 122 } | 143 } |
| 123 | 144 |
| 145 /** |
| 146 * Documentation wanted -- johnniwinther |
| 147 * |
| 148 * Invariant: [element] must be an declaration element. |
| 149 */ |
| 124 void eagerRecompile(Element element) { | 150 void eagerRecompile(Element element) { |
| 151 assert(invariant(element, element.isDeclaration)); |
| 125 universe.generatedCode.remove(element); | 152 universe.generatedCode.remove(element); |
| 126 universe.generatedBailoutCode.remove(element); | 153 universe.generatedBailoutCode.remove(element); |
| 127 addToWorkList(element); | 154 addToWorkList(element); |
| 128 } | 155 } |
| 129 | 156 |
| 130 void registerRecompilationCandidate(Element element, | 157 void registerRecompilationCandidate(Element element, |
| 131 [TreeElements elements]) { | 158 [TreeElements elements]) { |
| 132 if (queueIsClosed) { | 159 if (queueIsClosed) { |
| 133 compiler.internalErrorOnElement(element, "Work list is closed."); | 160 compiler.internalErrorOnElement(element, "$name work list is closed."); |
| 134 } | 161 } |
| 135 recompilationCandidates.add(element, elements); | 162 recompilationCandidates.add(element, elements); |
| 136 } | 163 } |
| 137 | 164 |
| 138 void registerInstantiatedClass(ClassElement cls) { | 165 void registerInstantiatedClass(ClassElement cls) { |
| 139 if (cls.isInterface()) { | 166 if (cls.isInterface()) { |
| 140 compiler.internalErrorOnElement( | 167 compiler.internalErrorOnElement( |
| 141 // Use the current element, as this is where cls is referenced from. | 168 // Use the current element, as this is where cls is referenced from. |
| 142 compiler.currentElement, | 169 compiler.currentElement, |
| 143 'Expected a class, but $cls is an interface.'); | 170 'Expected a class, but $cls is an interface.'); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 157 } | 184 } |
| 158 } | 185 } |
| 159 }); | 186 }); |
| 160 return true; | 187 return true; |
| 161 } | 188 } |
| 162 | 189 |
| 163 void processInstantiatedClass(ClassElement cls) { | 190 void processInstantiatedClass(ClassElement cls) { |
| 164 cls.localMembers.forEach(processInstantiatedClassMember); | 191 cls.localMembers.forEach(processInstantiatedClassMember); |
| 165 } | 192 } |
| 166 | 193 |
| 194 /** |
| 195 * Documentation wanted -- johnniwinther |
| 196 */ |
| 167 void processInstantiatedClassMember(Element member) { | 197 void processInstantiatedClassMember(Element member) { |
| 198 assert(invariant(member, member.isDeclaration)); |
| 168 if (universe.generatedCode.containsKey(member)) return; | 199 if (universe.generatedCode.containsKey(member)) return; |
| 169 if (resolvedElements[member] !== null) return; | 200 if (resolvedElements[member] !== null) return; |
| 170 if (!member.isInstanceMember()) return; | 201 if (!member.isInstanceMember()) return; |
| 171 if (member.isField()) return; | 202 if (member.isField()) return; |
| 172 | 203 |
| 173 String memberName = member.name.slowToString(); | 204 String memberName = member.name.slowToString(); |
| 174 Link<Element> members = instanceMembersByName.putIfAbsent( | 205 Link<Element> members = instanceMembersByName.putIfAbsent( |
| 175 memberName, () => const EmptyLink<Element>()); | 206 memberName, () => const EmptyLink<Element>()); |
| 176 instanceMembersByName[memberName] = members.prepend(member); | 207 instanceMembersByName[memberName] = members.prepend(member); |
| 177 | 208 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 void handleUnseenSelector(SourceString methodName, Selector selector) { | 324 void handleUnseenSelector(SourceString methodName, Selector selector) { |
| 294 processInstanceMembers(methodName, (Element member) { | 325 processInstanceMembers(methodName, (Element member) { |
| 295 if (selector.applies(member, compiler)) { | 326 if (selector.applies(member, compiler)) { |
| 296 addToWorkList(member); | 327 addToWorkList(member); |
| 297 return true; | 328 return true; |
| 298 } | 329 } |
| 299 return false; | 330 return false; |
| 300 }); | 331 }); |
| 301 } | 332 } |
| 302 | 333 |
| 334 /** |
| 335 * Documentation wanted -- johnniwinther |
| 336 * |
| 337 * Invariant: [element] must be a declaration element. |
| 338 */ |
| 303 void registerStaticUse(Element element) { | 339 void registerStaticUse(Element element) { |
| 304 if (element !== null) addToWorkList(element); | 340 if (element == null) return; |
| 341 assert(invariant(element, element.isDeclaration)); |
| 342 addToWorkList(element); |
| 305 } | 343 } |
| 306 | 344 |
| 307 void registerGetOfStaticFunction(FunctionElement element) { | 345 void registerGetOfStaticFunction(FunctionElement element) { |
| 308 registerStaticUse(element); | 346 registerStaticUse(element); |
| 309 universe.staticFunctionsNeedingGetter.add(element); | 347 universe.staticFunctionsNeedingGetter.add(element); |
| 310 } | 348 } |
| 311 | 349 |
| 312 void registerDynamicInvocation(SourceString methodName, Selector selector) { | 350 void registerDynamicInvocation(SourceString methodName, Selector selector) { |
| 313 assert(selector !== null); | 351 assert(selector !== null); |
| 314 registerInvocation(methodName, selector); | 352 registerInvocation(methodName, selector); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 | 388 |
| 351 void registerIsCheck(DartType type) { | 389 void registerIsCheck(DartType type) { |
| 352 universe.isChecks.add(type); | 390 universe.isChecks.add(type); |
| 353 } | 391 } |
| 354 | 392 |
| 355 void forEach(f(WorkItem work)) { | 393 void forEach(f(WorkItem work)) { |
| 356 while (!queue.isEmpty()) { | 394 while (!queue.isEmpty()) { |
| 357 f(queue.removeLast()); // TODO(kasperl): Why isn't this removeFirst? | 395 f(queue.removeLast()); // TODO(kasperl): Why isn't this removeFirst? |
| 358 } | 396 } |
| 359 } | 397 } |
| 398 |
| 399 String toString() => 'Enqueuer($name)'; |
| 360 } | 400 } |
| OLD | NEW |