OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 library rasta.kernel; | 5 library rasta.kernel; |
6 | 6 |
7 import 'dart:async' show | 7 import 'dart:async' show |
8 Future; | 8 Future; |
9 | 9 |
10 import 'dart:collection' show | 10 import 'dart:collection' show |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 87 |
88 final Set<ir.TreeNode> checkedNodes = new Set<ir.TreeNode>(); | 88 final Set<ir.TreeNode> checkedNodes = new Set<ir.TreeNode>(); |
89 | 89 |
90 final Map<LibraryElement, Map<String, int>> mixinApplicationNamesByLibrary = | 90 final Map<LibraryElement, Map<String, int>> mixinApplicationNamesByLibrary = |
91 <LibraryElement, Map<String, int>>{}; | 91 <LibraryElement, Map<String, int>>{}; |
92 | 92 |
93 /// FIFO queue of work that needs to be completed before the returned AST | 93 /// FIFO queue of work that needs to be completed before the returned AST |
94 /// nodes are correct. | 94 /// nodes are correct. |
95 final Queue<WorkItem> workQueue = new Queue<WorkItem>(); | 95 final Queue<WorkItem> workQueue = new Queue<WorkItem>(); |
96 | 96 |
| 97 // TODO(ahe): this should be pruned when unloading libraries. |
| 98 final Map<String, List<int>> lineStarts = <String, List<int>>{}; |
| 99 |
97 Kernel(this.compiler); | 100 Kernel(this.compiler); |
98 | 101 |
99 bool hasMainMethod(Uri uri) { | 102 bool hasMainMethod(Uri uri) { |
100 LibraryElement library = compiler.libraryLoader.lookupLibrary(uri); | 103 LibraryElement library = compiler.libraryLoader.lookupLibrary(uri); |
101 return library.localLookup("main") != null; | 104 return library.localLookup("main") != null; |
102 } | 105 } |
103 | 106 |
104 void addWork(Element element, WorkAction action) { | 107 void addWork(Element element, WorkAction action) { |
105 workQueue.addLast(new WorkItem(element, action)); | 108 workQueue.addLast(new WorkItem(element, action)); |
106 } | 109 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 } | 149 } |
147 irLibrary = libraryToIr(element.library); | 150 irLibrary = libraryToIr(element.library); |
148 } | 151 } |
149 return new ir.Name(name, irLibrary); | 152 return new ir.Name(name, irLibrary); |
150 } | 153 } |
151 | 154 |
152 Future<ir.Library> loadLibrary(Uri uri) async { | 155 Future<ir.Library> loadLibrary(Uri uri) async { |
153 return libraryToIr(await compiler.libraryLoader.loadLibrary(uri)); | 156 return libraryToIr(await compiler.libraryLoader.loadLibrary(uri)); |
154 } | 157 } |
155 | 158 |
| 159 String fileUri(Element element) { |
| 160 String fileUri = "${element.compilationUnit.script.resourceUri}"; |
| 161 lineStarts[fileUri] = <int>[0]; |
| 162 return fileUri; |
| 163 } |
| 164 |
| 165 void addLineStartsTo(ir.Program program) { |
| 166 program.uriToLineStarts.addAll(lineStarts); |
| 167 } |
| 168 |
156 ir.Library libraryToIr(LibraryElement library) { | 169 ir.Library libraryToIr(LibraryElement library) { |
157 library = library.declaration; | 170 library = library.declaration; |
158 return libraries.putIfAbsent(library, () { | 171 return libraries.putIfAbsent(library, () { |
159 String name = library.hasLibraryName ? library.libraryName : null; | 172 String name = library.hasLibraryName ? library.libraryName : null; |
160 ir.Library libraryNode = new ir.Library( | 173 ir.Library libraryNode = new ir.Library( |
161 library.canonicalUri, name: name, classes: null, procedures: null, | 174 library.canonicalUri, name: name, classes: null, procedures: null, |
162 fields: null); | 175 fields: null); |
| 176 libraryNode.fileUri = fileUri(library); |
163 addWork(library, () { | 177 addWork(library, () { |
164 Queue<ir.Class> classes = new Queue<ir.Class>(); | 178 Queue<ir.Class> classes = new Queue<ir.Class>(); |
165 Queue<ir.Member> members = new Queue<ir.Member>(); | 179 Queue<ir.Member> members = new Queue<ir.Member>(); |
166 library.implementation.forEachLocalMember((Element e) { | 180 library.implementation.forEachLocalMember((Element e) { |
167 if (e.isClass) { | 181 if (e.isClass) { |
168 classes.addFirst(classToIr(e)); | 182 classes.addFirst(classToIr(e)); |
169 } else if (e.isFunction || e.isAccessor) { | 183 } else if (e.isFunction || e.isAccessor) { |
170 members.addFirst(functionToIr(e)); | 184 members.addFirst(functionToIr(e)); |
171 } else if (e.isField) { | 185 } else if (e.isField) { |
172 members.addFirst(fieldToIr(e)); | 186 members.addFirst(fieldToIr(e)); |
(...skipping 28 matching lines...) Expand all Loading... |
201 | 215 |
202 ir.Class classToIr(ClassElement cls) { | 216 ir.Class classToIr(ClassElement cls) { |
203 cls = cls.declaration; | 217 cls = cls.declaration; |
204 return classes.putIfAbsent(cls, () { | 218 return classes.putIfAbsent(cls, () { |
205 cls.ensureResolved(compiler.resolution); | 219 cls.ensureResolved(compiler.resolution); |
206 compiler.enqueuer.resolution.emptyDeferredTaskQueue(); | 220 compiler.enqueuer.resolution.emptyDeferredTaskQueue(); |
207 String name = computeName(cls); | 221 String name = computeName(cls); |
208 ir.Class classNode = new ir.Class( | 222 ir.Class classNode = new ir.Class( |
209 name: name, isAbstract: cls.isAbstract, | 223 name: name, isAbstract: cls.isAbstract, |
210 typeParameters: null, implementedTypes: null, constructors: null, | 224 typeParameters: null, implementedTypes: null, constructors: null, |
211 procedures: null, fields: null); | 225 procedures: null, fields: null, fileUri: fileUri(cls)); |
212 addWork(cls, () { | 226 addWork(cls, () { |
213 if (cls.supertype != null) { | 227 if (cls.supertype != null) { |
214 classNode.supertype = interfaceTypeToIr(cls.supertype); | 228 classNode.supertype = interfaceTypeToIr(cls.supertype); |
215 } | 229 } |
216 classNode.parent = libraryToIr(cls.library); | 230 classNode.parent = libraryToIr(cls.library); |
217 if (cls.isUnnamedMixinApplication) { | 231 if (cls.isUnnamedMixinApplication) { |
218 classNode.enclosingLibrary.addClass(classNode); | 232 classNode.enclosingLibrary.addClass(classNode); |
219 } | 233 } |
220 cls.implementation.forEachMember( | 234 cls.implementation.forEachMember( |
221 (ClassElement enclosingClass, Element member) { | 235 (ClassElement enclosingClass, Element member) { |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 if (function.isGenerativeConstructor) { | 383 if (function.isGenerativeConstructor) { |
370 member = constructor = new ir.Constructor( | 384 member = constructor = new ir.Constructor( |
371 null, name: name, isConst: function.isConst, | 385 null, name: name, isConst: function.isConst, |
372 isExternal: isNative || function.isExternal, initializers: null); | 386 isExternal: isNative || function.isExternal, initializers: null); |
373 } else { | 387 } else { |
374 member = procedure = new ir.Procedure( | 388 member = procedure = new ir.Procedure( |
375 name, null, null, isAbstract: function.isAbstract, | 389 name, null, null, isAbstract: function.isAbstract, |
376 isStatic: function.isStatic || function.isTopLevel | 390 isStatic: function.isStatic || function.isTopLevel |
377 || function.isFactoryConstructor, | 391 || function.isFactoryConstructor, |
378 isExternal: isNative || function.isExternal, | 392 isExternal: isNative || function.isExternal, |
| 393 fileUri: fileUri(function), |
379 isConst: false); // TODO(ahe): When is this true? | 394 isConst: false); // TODO(ahe): When is this true? |
380 } | 395 } |
381 addWork(function, () { | 396 addWork(function, () { |
382 setParent(member, function); | 397 setParent(member, function); |
383 KernelVisitor visitor = | 398 KernelVisitor visitor = |
384 new KernelVisitor(function, function.treeElements, this); | 399 new KernelVisitor(function, function.treeElements, this); |
385 beginFactoryScope(function); | 400 beginFactoryScope(function); |
386 IrFunction irFunction = visitor.buildFunction(); | 401 IrFunction irFunction = visitor.buildFunction(); |
387 // TODO(ahe): Add addFunction/set function to [ir.Procedure]. | 402 // TODO(ahe): Add addFunction/set function to [ir.Procedure]. |
388 irFunction.node.parent = member; | 403 irFunction.node.parent = member; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 return fields.putIfAbsent(field, () { | 464 return fields.putIfAbsent(field, () { |
450 compiler.analyzeElement(field); | 465 compiler.analyzeElement(field); |
451 compiler.enqueuer.resolution.emptyDeferredTaskQueue(); | 466 compiler.enqueuer.resolution.emptyDeferredTaskQueue(); |
452 field = field.implementation; | 467 field = field.implementation; |
453 ir.DartType type = field.isMalformed | 468 ir.DartType type = field.isMalformed |
454 ? const ir.InvalidType() | 469 ? const ir.InvalidType() |
455 : typeToIr(field.type); | 470 : typeToIr(field.type); |
456 ir.Field fieldNode = new ir.Field( | 471 ir.Field fieldNode = new ir.Field( |
457 irName(field.memberName.text, field), type: type, initializer: null, | 472 irName(field.memberName.text, field), type: type, initializer: null, |
458 isFinal: field.isFinal, isStatic: field.isStatic || field.isTopLevel, | 473 isFinal: field.isFinal, isStatic: field.isStatic || field.isTopLevel, |
459 isConst: field.isConst); | 474 isConst: field.isConst, fileUri: fileUri(field)); |
460 addWork(field, () { | 475 addWork(field, () { |
461 setParent(fieldNode, field); | 476 setParent(fieldNode, field); |
462 if (!field.isMalformed && !field.isInstanceMember && | 477 if (!field.isMalformed && !field.isInstanceMember && |
463 field.initializer != null) { | 478 field.initializer != null) { |
464 KernelVisitor visitor = | 479 KernelVisitor visitor = |
465 new KernelVisitor(field, field.treeElements, this); | 480 new KernelVisitor(field, field.treeElements, this); |
466 fieldNode.initializer = visitor.buildInitializer() | 481 fieldNode.initializer = visitor.buildInitializer() |
467 ..parent = fieldNode; | 482 ..parent = fieldNode; |
468 } | 483 } |
469 }); | 484 }); |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
687 } | 702 } |
688 | 703 |
689 class ConstructorTarget { | 704 class ConstructorTarget { |
690 final ConstructorElement element; | 705 final ConstructorElement element; |
691 final DartType type; | 706 final DartType type; |
692 | 707 |
693 ConstructorTarget(this.element, this.type); | 708 ConstructorTarget(this.element, this.type); |
694 | 709 |
695 String toString() => "ConstructorTarget($element, $type)"; | 710 String toString() => "ConstructorTarget($element, $type)"; |
696 } | 711 } |
OLD | NEW |