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 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, |
|
ahe
2012/09/18 11:25:54
Nice.
| |
| 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 // Invariant: Key elements are declarations. | |
| 58 final Map<Element, TreeElements> resolvedElements; | 60 final Map<Element, TreeElements> resolvedElements; |
| 59 final RecompilationQueue recompilationCandidates; | 61 final RecompilationQueue recompilationCandidates; |
| 60 | 62 |
| 61 bool queueIsClosed = false; | 63 bool queueIsClosed = false; |
| 62 EnqueueTask task; | 64 EnqueueTask task; |
| 63 | 65 |
| 64 Enqueuer(this.compiler, | 66 Enqueuer(this.name, this.compiler, |
| 65 ItemCompilationContext itemCompilationContextCreator()) | 67 ItemCompilationContext itemCompilationContextCreator()) |
| 66 : this.itemCompilationContextCreator = itemCompilationContextCreator, | 68 : this.itemCompilationContextCreator = itemCompilationContextCreator, |
| 67 instanceMembersByName = new Map<String, Link<Element>>(), | 69 instanceMembersByName = new Map<String, Link<Element>>(), |
| 68 seenClasses = new Set<ClassElement>(), | 70 seenClasses = new Set<ClassElement>(), |
| 69 universe = new Universe(), | 71 universe = new Universe(), |
| 70 queue = new Queue<WorkItem>(), | 72 queue = new Queue<WorkItem>(), |
| 71 resolvedElements = new Map<Element, TreeElements>(), | 73 resolvedElements = new Map<Element, TreeElements>(), |
| 72 recompilationCandidates = | 74 recompilationCandidates = |
| 73 new RecompilationQueue(itemCompilationContextCreator); | 75 new RecompilationQueue(itemCompilationContextCreator); |
| 74 | 76 |
| 75 bool get isResolutionQueue => compiler.enqueuer.resolution === this; | 77 bool get isResolutionQueue => compiler.enqueuer.resolution === this; |
| 76 | 78 |
| 77 TreeElements getCachedElements(Element element) { | 79 TreeElements getCachedElements(Element element) { |
| 78 // TODO(ngeoffray): Get rid of this check. | 80 // TODO(ngeoffray): Get rid of this check. |
| 79 if (element.enclosingElement.isClosure()) { | 81 if (element.enclosingElement.isClosure()) { |
| 80 closureMapping.ClosureClassElement cls = element.enclosingElement; | 82 closureMapping.ClosureClassElement cls = element.enclosingElement; |
| 81 element = cls.methodElement; | 83 element = cls.methodElement; |
| 82 } | 84 } |
| 83 Element owner = element.getOutermostEnclosingMemberOrTopLevel(); | 85 Element owner = element.getOutermostEnclosingMemberOrTopLevel(); |
| 84 return compiler.enqueuer.resolution.resolvedElements[owner]; | 86 return compiler.enqueuer.resolution.resolvedElements[owner.declaration]; |
| 85 } | 87 } |
| 86 | 88 |
| 87 String lookupCode(Element element) => | 89 /** |
| 88 universe.generatedCode[element].toString(); | 90 * Invariant: [element] must be the declaration element. |
|
ahe
2012/09/18 11:25:54
Not documentation.
Johnni Winther
2012/09/20 08:12:23
Done.
| |
| 91 */ | |
| 92 String lookupCode(Element element) { | |
| 93 assert(element.isDeclaration); | |
| 94 return universe.generatedCode[element].toString(); | |
|
ahe
2012/09/18 11:25:54
Why are you calling toString here?
Johnni Winther
2012/09/20 08:12:23
I have no idea. It was there before this CL.
| |
| 95 } | |
| 89 | 96 |
| 97 /** | |
| 98 * Invariant: [element] must be the declaration element. | |
|
ahe
2012/09/18 11:25:54
Not documentation.
Johnni Winther
2012/09/20 08:12:23
Done.
| |
| 99 */ | |
| 90 void addToWorkList(Element element, [TreeElements elements]) { | 100 void addToWorkList(Element element, [TreeElements elements]) { |
| 101 assert(element.isDeclaration); | |
| 91 if (element.isForeign()) return; | 102 if (element.isForeign()) return; |
| 92 if (compiler.phase == Compiler.PHASE_RECOMPILING) return; | 103 if (compiler.phase == Compiler.PHASE_RECOMPILING) return; |
| 93 if (queueIsClosed) { | 104 if (queueIsClosed) { |
| 94 if (isResolutionQueue && getCachedElements(element) !== null) return; | 105 if (isResolutionQueue && getCachedElements(element) !== null) return; |
| 106 print('$element: isMember=${element.isMember()} isInstanceMember=${element .isInstanceMember()}'); | |
|
ngeoffray
2012/09/17 12:46:24
Remove debugging code.
Johnni Winther
2012/09/20 08:12:23
Done.
| |
| 95 compiler.internalErrorOnElement(element, "Work list is closed."); | 107 compiler.internalErrorOnElement(element, "Work list is closed."); |
| 96 } | 108 } |
| 97 if (!isResolutionQueue && | 109 if (!isResolutionQueue && |
| 98 element.kind === ElementKind.GENERATIVE_CONSTRUCTOR) { | 110 element.kind === ElementKind.GENERATIVE_CONSTRUCTOR) { |
| 99 registerInstantiatedClass(element.getEnclosingClass()); | 111 registerInstantiatedClass(element.getEnclosingClass()); |
| 100 } | 112 } |
| 101 if (elements === null) { | 113 if (elements === null) { |
| 102 elements = getCachedElements(element); | 114 elements = getCachedElements(element); |
| 103 } | 115 } |
| 104 if (isResolutionQueue) { | 116 if (isResolutionQueue) { |
| 105 compiler.world.registerUsedElement(element); | 117 compiler.world.registerUsedElement(element); |
| 106 } | 118 } |
| 107 | 119 |
| 108 queue.add(new WorkItem(element, elements, itemCompilationContextCreator())); | 120 queue.add(new WorkItem(element, elements, itemCompilationContextCreator())); |
| 109 | 121 |
| 110 // Enable isolate support if we start using something from the | 122 // Enable isolate support if we start using something from the |
| 111 // isolate library. | 123 // isolate library. |
| 112 LibraryElement library = element.getLibrary(); | 124 LibraryElement library = element.getLibrary(); |
| 113 if (!compiler.hasIsolateSupport() | 125 if (!compiler.hasIsolateSupport() |
| 114 && library.uri.toString() == 'dart:isolate') { | 126 && library.uri.toString() == 'dart:isolate') { |
| 115 compiler.enableIsolateSupport(library); | 127 compiler.enableIsolateSupport(library); |
| 116 } | 128 } |
| 117 } | 129 } |
| 118 | 130 |
| 131 /** | |
| 132 * Invariant: [element] must be the declaration element. | |
| 133 */ | |
| 119 void eagerRecompile(Element element) { | 134 void eagerRecompile(Element element) { |
| 135 assert(element.isDeclaration); | |
| 120 universe.generatedCode.remove(element); | 136 universe.generatedCode.remove(element); |
| 121 universe.generatedBailoutCode.remove(element); | 137 universe.generatedBailoutCode.remove(element); |
| 122 addToWorkList(element); | 138 addToWorkList(element); |
| 123 } | 139 } |
| 124 | 140 |
| 125 void registerRecompilationCandidate(Element element, | 141 void registerRecompilationCandidate(Element element, |
| 126 [TreeElements elements]) { | 142 [TreeElements elements]) { |
| 127 if (queueIsClosed) { | 143 if (queueIsClosed) { |
| 128 compiler.internalErrorOnElement(element, "Work list is closed."); | 144 compiler.internalErrorOnElement(element, "$name work list is closed."); |
| 129 } | 145 } |
| 130 recompilationCandidates.add(element, elements); | 146 recompilationCandidates.add(element, elements); |
| 131 } | 147 } |
| 132 | 148 |
| 133 void registerInstantiatedClass(ClassElement cls) { | 149 void registerInstantiatedClass(ClassElement cls) { |
| 134 if (cls.isInterface()) { | 150 if (cls.isInterface()) { |
| 135 compiler.internalErrorOnElement( | 151 compiler.internalErrorOnElement( |
| 136 // Use the current element, as this is where cls is referenced from. | 152 // Use the current element, as this is where cls is referenced from. |
| 137 compiler.currentElement, | 153 compiler.currentElement, |
| 138 'Expected a class, but $cls is an interface.'); | 154 'Expected a class, but $cls is an interface.'); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 153 } | 169 } |
| 154 }); | 170 }); |
| 155 return true; | 171 return true; |
| 156 } | 172 } |
| 157 | 173 |
| 158 void processInstantiatedClass(ClassElement cls) { | 174 void processInstantiatedClass(ClassElement cls) { |
| 159 cls.localMembers.forEach(processInstantiatedClassMember); | 175 cls.localMembers.forEach(processInstantiatedClassMember); |
| 160 } | 176 } |
| 161 | 177 |
| 162 void processInstantiatedClassMember(Element member) { | 178 void processInstantiatedClassMember(Element member) { |
| 179 assert(member.isDeclaration); | |
| 163 if (universe.generatedCode.containsKey(member)) return; | 180 if (universe.generatedCode.containsKey(member)) return; |
| 164 if (resolvedElements[member] !== null) return; | 181 if (resolvedElements[member] !== null) return; |
| 165 if (!member.isInstanceMember()) return; | 182 if (!member.isInstanceMember()) return; |
| 166 if (member.isField()) return; | 183 if (member.isField()) return; |
| 167 | 184 |
| 168 String memberName = member.name.slowToString(); | 185 String memberName = member.name.slowToString(); |
| 169 Link<Element> members = instanceMembersByName.putIfAbsent( | 186 Link<Element> members = instanceMembersByName.putIfAbsent( |
| 170 memberName, () => const EmptyLink<Element>()); | 187 memberName, () => const EmptyLink<Element>()); |
| 171 instanceMembersByName[memberName] = members.prepend(member); | 188 instanceMembersByName[memberName] = members.prepend(member); |
| 172 | 189 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 288 void handleUnseenSelector(SourceString methodName, Selector selector) { | 305 void handleUnseenSelector(SourceString methodName, Selector selector) { |
| 289 processInstanceMembers(methodName, (Element member) { | 306 processInstanceMembers(methodName, (Element member) { |
| 290 if (selector.applies(member, compiler)) { | 307 if (selector.applies(member, compiler)) { |
| 291 addToWorkList(member); | 308 addToWorkList(member); |
| 292 return true; | 309 return true; |
| 293 } | 310 } |
| 294 return false; | 311 return false; |
| 295 }); | 312 }); |
| 296 } | 313 } |
| 297 | 314 |
| 315 /** | |
| 316 * Invariant: [element] must be the declaration element. | |
|
ahe
2012/09/18 11:25:54
Not documentation.
Johnni Winther
2012/09/20 08:12:23
Done.
| |
| 317 */ | |
| 298 void registerStaticUse(Element element) { | 318 void registerStaticUse(Element element) { |
| 299 if (element !== null) addToWorkList(element); | 319 if (element == null) return; |
| 320 assert(element.isDeclaration); | |
| 321 addToWorkList(element); | |
| 300 } | 322 } |
| 301 | 323 |
| 302 void registerGetOfStaticFunction(FunctionElement element) { | 324 void registerGetOfStaticFunction(FunctionElement element) { |
| 303 registerStaticUse(element); | 325 registerStaticUse(element); |
| 304 universe.staticFunctionsNeedingGetter.add(element); | 326 universe.staticFunctionsNeedingGetter.add(element); |
| 305 } | 327 } |
| 306 | 328 |
| 307 void registerDynamicInvocation(SourceString methodName, Selector selector) { | 329 void registerDynamicInvocation(SourceString methodName, Selector selector) { |
| 308 assert(selector !== null); | 330 assert(selector !== null); |
| 309 registerInvocation(methodName, selector); | 331 registerInvocation(methodName, selector); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 345 | 367 |
| 346 void registerIsCheck(DartType type) { | 368 void registerIsCheck(DartType type) { |
| 347 universe.isChecks.add(type); | 369 universe.isChecks.add(type); |
| 348 } | 370 } |
| 349 | 371 |
| 350 void forEach(f(WorkItem work)) { | 372 void forEach(f(WorkItem work)) { |
| 351 while (!queue.isEmpty()) { | 373 while (!queue.isEmpty()) { |
| 352 f(queue.removeLast()); // TODO(kasperl): Why isn't this removeFirst? | 374 f(queue.removeLast()); // TODO(kasperl): Why isn't this removeFirst? |
| 353 } | 375 } |
| 354 } | 376 } |
| 377 | |
| 378 String toString() => name; | |
|
ahe
2012/09/18 11:25:54
I prefer this: 'Enqueuer($name)'
This makes debug
Johnni Winther
2012/09/20 08:12:23
Done.
| |
| 355 } | 379 } |
| OLD | NEW |