OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library trydart.poi.scope_information_visitor; | 5 library trydart.poi.scope_information_visitor; |
6 | 6 |
7 import 'package:compiler/src/elements/modelx.dart' as modelx; | 7 import 'package:compiler/src/elements/modelx.dart' as modelx; |
8 | 8 |
9 import 'package:compiler/src/elements/modelx.dart' show | 9 import 'package:compiler/src/elements/modelx.dart' show |
10 CompilationUnitElementX, | 10 CompilationUnitElementX, |
11 FieldElementX; | 11 FieldElementX; |
12 | 12 |
13 import 'package:compiler/src/elements/visitor.dart' show | 13 import 'package:compiler/src/elements/visitor.dart' show |
14 ElementVisitor; | 14 BaseElementVisitor; |
15 | 15 |
16 import 'package:compiler/src/dart2jslib.dart' show | 16 import 'package:compiler/src/dart2jslib.dart' show |
17 Compiler; | 17 Compiler; |
18 | 18 |
19 import 'package:compiler/src/elements/elements.dart' show | 19 import 'package:compiler/src/elements/elements.dart' show |
20 AbstractFieldElement, | 20 AbstractFieldElement, |
21 ClassElement, | 21 ClassElement, |
22 CompilationUnitElement, | 22 CompilationUnitElement, |
23 Element, | 23 Element, |
24 ElementCategory, | 24 ElementCategory, |
25 FunctionElement, | 25 FunctionElement, |
26 LibraryElement, | 26 LibraryElement, |
27 ScopeContainerElement; | 27 ScopeContainerElement; |
28 | 28 |
29 import 'package:compiler/src/dart_types.dart' show | 29 import 'package:compiler/src/dart_types.dart' show |
30 DartType; | 30 DartType; |
31 | 31 |
32 /** | 32 /** |
33 * Serializes scope information about an element. This is accomplished by | 33 * Serializes scope information about an element. This is accomplished by |
34 * calling the [serialize] method on each element. Some elements need special | 34 * calling the [serialize] method on each element. Some elements need special |
35 * treatment, as their enclosing scope must also be serialized. | 35 * treatment, as their enclosing scope must also be serialized. |
36 */ | 36 */ |
37 class ScopeInformationVisitor extends ElementVisitor/* <void> */ { | 37 class ScopeInformationVisitor extends BaseElementVisitor/* <void> */ { |
38 // TODO(ahe): Include function parameters and local variables. | 38 // TODO(ahe): Include function parameters and local variables. |
39 | 39 |
40 final Compiler compiler; | 40 final Compiler compiler; |
41 final Element currentElement; | 41 final Element currentElement; |
42 final int position; | 42 final int position; |
43 final StringBuffer buffer = new StringBuffer(); | 43 final StringBuffer buffer = new StringBuffer(); |
44 int indentationLevel = 0; | 44 int indentationLevel = 0; |
45 ClassElement currentClass; | 45 ClassElement currentClass; |
46 | 46 |
47 bool sortMembers = false; | 47 bool sortMembers = false; |
48 | 48 |
49 bool ignoreImports = false; | 49 bool ignoreImports = false; |
50 | 50 |
51 ScopeInformationVisitor(this.compiler, this.currentElement, this.position); | 51 ScopeInformationVisitor(this.compiler, this.currentElement, this.position); |
52 | 52 |
53 String get indentation => ' ' * indentationLevel; | 53 String get indentation => ' ' * indentationLevel; |
54 | 54 |
55 StringBuffer get indented => buffer..write(indentation); | 55 StringBuffer get indented => buffer..write(indentation); |
56 | 56 |
57 void visitElement(Element e) { | 57 void visitElement(Element e, _) { |
58 serialize(e, omitEnclosing: false); | 58 serialize(e, omitEnclosing: false); |
59 } | 59 } |
60 | 60 |
61 void visitLibraryElement(LibraryElement e) { | 61 void visitLibraryElement(LibraryElement e, _) { |
62 bool isFirst = true; | 62 bool isFirst = true; |
63 forEach(Element member) { | 63 forEach(Element member) { |
64 if (!isFirst) { | 64 if (!isFirst) { |
65 buffer.write(','); | 65 buffer.write(','); |
66 } | 66 } |
67 buffer.write('\n'); | 67 buffer.write('\n'); |
68 indented; | 68 indented; |
69 serialize(member); | 69 serialize(member); |
70 isFirst = false; | 70 isFirst = false; |
71 } | 71 } |
(...skipping 24 matching lines...) Expand all Loading... |
96 buffer.write('\n'); | 96 buffer.write('\n'); |
97 indentationLevel--; | 97 indentationLevel--; |
98 indented.write('}'); | 98 indented.write('}'); |
99 }, | 99 }, |
100 serializeMembers: () { | 100 serializeMembers: () { |
101 isFirst = true; | 101 isFirst = true; |
102 sortElements(localScope(e).values).forEach(forEach); | 102 sortElements(localScope(e).values).forEach(forEach); |
103 }); | 103 }); |
104 } | 104 } |
105 | 105 |
106 void visitClassElement(ClassElement e) { | 106 void visitClassElement(ClassElement e, _) { |
107 currentClass = e; | 107 currentClass = e; |
108 serializeClassSide(e, isStatic: true); | 108 serializeClassSide(e, isStatic: true); |
109 } | 109 } |
110 | 110 |
111 /// Serializes one of the "sides" a class. The sides of a class are "instance | 111 /// Serializes one of the "sides" a class. The sides of a class are "instance |
112 /// side" and "class side". These terms are from Smalltalk. The instance side | 112 /// side" and "class side". These terms are from Smalltalk. The instance side |
113 /// is all the local instance members of the class (the members of the | 113 /// is all the local instance members of the class (the members of the |
114 /// mixin), and the class side is the equivalent for static members and | 114 /// mixin), and the class side is the equivalent for static members and |
115 /// constructors. | 115 /// constructors. |
116 /// The scope chain is ordered so that the "class side" is searched before | 116 /// The scope chain is ordered so that the "class side" is searched before |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 buffer.write(','); | 161 buffer.write(','); |
162 } | 162 } |
163 buffer.write('\n'); | 163 buffer.write('\n'); |
164 indented; | 164 indented; |
165 serialize(member); | 165 serialize(member); |
166 isFirst = false; | 166 isFirst = false; |
167 }); | 167 }); |
168 }); | 168 }); |
169 } | 169 } |
170 | 170 |
171 void visitScopeContainerElement(ScopeContainerElement e) { | 171 void visitScopeContainerElement(ScopeContainerElement e, _) { |
172 bool isFirst = true; | 172 bool isFirst = true; |
173 serialize(e, omitEnclosing: false, serializeMembers: () { | 173 serialize(e, omitEnclosing: false, serializeMembers: () { |
174 localMembersSorted(e).forEach((Element member) { | 174 localMembersSorted(e).forEach((Element member) { |
175 if (!isFirst) { | 175 if (!isFirst) { |
176 buffer.write(','); | 176 buffer.write(','); |
177 } | 177 } |
178 buffer.write('\n'); | 178 buffer.write('\n'); |
179 indented; | 179 indented; |
180 serialize(member); | 180 serialize(member); |
181 isFirst = false; | 181 isFirst = false; |
182 }); | 182 }); |
183 }); | 183 }); |
184 } | 184 } |
185 | 185 |
186 void visitCompilationUnitElement(CompilationUnitElement e) { | 186 void visitCompilationUnitElement(CompilationUnitElement e, _) { |
187 e.enclosingElement.accept(this); | 187 e.enclosingElement.accept(this, _); |
188 } | 188 } |
189 | 189 |
190 void visitAbstractFieldElement(AbstractFieldElement e) { | 190 void visitAbstractFieldElement(AbstractFieldElement e, _) { |
191 throw new UnsupportedError('AbstractFieldElement cannot be serialized.'); | 191 throw new UnsupportedError('AbstractFieldElement cannot be serialized.'); |
192 } | 192 } |
193 | 193 |
194 void serialize( | 194 void serialize( |
195 Element element, | 195 Element element, |
196 {bool omitEnclosing: true, | 196 {bool omitEnclosing: true, |
197 void serializeMembers(), | 197 void serializeMembers(), |
198 void serializeEnclosing(), | 198 void serializeEnclosing(), |
199 String kind, | 199 String kind, |
200 String name}) { | 200 String name}) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 indentationLevel--; | 266 indentationLevel--; |
267 buffer.write('\n'); | 267 buffer.write('\n'); |
268 indented.write(']'); | 268 indented.write(']'); |
269 } | 269 } |
270 if (!omitEnclosing) { | 270 if (!omitEnclosing) { |
271 buffer.write(',\n'); | 271 buffer.write(',\n'); |
272 indented.write('"enclosing": '); | 272 indented.write('"enclosing": '); |
273 if (serializeEnclosing != null) { | 273 if (serializeEnclosing != null) { |
274 serializeEnclosing(); | 274 serializeEnclosing(); |
275 } else { | 275 } else { |
276 element.enclosingElement.accept(this); | 276 element.enclosingElement.accept(this, null); |
277 } | 277 } |
278 } | 278 } |
279 indentationLevel--; | 279 indentationLevel--; |
280 buffer.write('\n'); | 280 buffer.write('\n'); |
281 indented.write('}'); | 281 indented.write('}'); |
282 } | 282 } |
283 | 283 |
284 List<Element> localMembersSorted(ScopeContainerElement element) { | 284 List<Element> localMembersSorted(ScopeContainerElement element) { |
285 List<Element> result = <Element>[]; | 285 List<Element> result = <Element>[]; |
286 element.forEachLocalMember((Element member) { | 286 element.forEachLocalMember((Element member) { |
287 result.add(member); | 287 result.add(member); |
288 }); | 288 }); |
289 return sortElements(result); | 289 return sortElements(result); |
290 } | 290 } |
291 | 291 |
292 List<Element> sortElements(Iterable<Element> elements) { | 292 List<Element> sortElements(Iterable<Element> elements) { |
293 List<Element> result = new List<Element>.from(elements); | 293 List<Element> result = new List<Element>.from(elements); |
294 if (sortMembers) { | 294 if (sortMembers) { |
295 result.sort((Element a, Element b) => a.name.compareTo(b.name)); | 295 result.sort((Element a, Element b) => a.name.compareTo(b.name)); |
296 } | 296 } |
297 return result; | 297 return result; |
298 } | 298 } |
299 } | 299 } |
300 | 300 |
301 modelx.ScopeX localScope(modelx.LibraryElementX element) => element.localScope; | 301 modelx.ScopeX localScope(modelx.LibraryElementX element) => element.localScope; |
302 | 302 |
303 modelx.ImportScope importScope(modelx.LibraryElementX element) { | 303 modelx.ImportScope importScope(modelx.LibraryElementX element) { |
304 return element.importScope; | 304 return element.importScope; |
305 } | 305 } |
OLD | NEW |