OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #library('mirrors.util'); | |
6 | |
7 #import('mirrors.dart'); | |
8 #import('../../../lib/compiler/implementation/util/characters.dart'); | |
9 | |
10 //------------------------------------------------------------------------------ | |
11 // Utility functions for using the Mirror API | |
12 //------------------------------------------------------------------------------ | |
13 | |
14 /** | |
15 * Returns an iterable over the type declarations directly inheriting from | |
16 * the declaration of this type. | |
17 */ | |
18 Iterable<InterfaceMirror> computeSubdeclarations(InterfaceMirror type) { | |
19 type = type.declaration; | |
20 var subtypes = <InterfaceMirror>[]; | |
21 type.system.libraries.forEach((_, library) { | |
22 for (InterfaceMirror otherType in library.types.getValues()) { | |
23 var superClass = otherType.superclass; | |
24 if (superClass !== null) { | |
25 superClass = superClass.declaration; | |
26 if (type.library === superClass.library) { | |
27 if (superClass == type) { | |
28 subtypes.add(otherType); | |
29 } | |
30 } | |
31 } | |
32 final superInterfaces = otherType.interfaces.getValues(); | |
33 for (InterfaceMirror superInterface in superInterfaces) { | |
34 superInterface = superInterface.declaration; | |
35 if (type.library === superInterface.library) { | |
36 if (superInterface == type) { | |
37 subtypes.add(otherType); | |
38 } | |
39 } | |
40 } | |
41 } | |
42 }); | |
43 return subtypes; | |
44 } | |
45 | |
46 /** | |
47 * Finds the mirror in [map] by the simple name [name]. If [constructorName] or | |
48 * [operatorName] is provided, a constructor/operator method by that name is | |
49 * returned. | |
50 */ | |
51 Mirror findMirror(Map<Object,Mirror> map, String name, | |
52 [String constructorName, String operatorName]) { | |
53 var foundMirror = null; | |
54 map.forEach((_, Mirror mirror) { | |
55 if (mirror.simpleName == name) { | |
56 if (constructorName !== null) { | |
57 if (mirror is MethodMirror && | |
58 constructorName == mirror.constructorName) { | |
59 foundMirror = mirror; | |
60 } | |
61 } else if (operatorName !== null) { | |
62 if (mirror is MethodMirror && | |
63 operatorName == mirror.operatorName) { | |
64 foundMirror = mirror; | |
65 } | |
66 } else { | |
67 foundMirror = mirror; | |
68 } | |
69 } | |
70 }); | |
71 return foundMirror; | |
72 } | |
73 | |
74 LibraryMirror findLibrary(MemberMirror member) { | |
75 ObjectMirror owner = member.surroundingDeclaration; | |
76 if (owner is LibraryMirror) { | |
77 return owner; | |
78 } else if (owner is TypeMirror) { | |
79 return owner.library; | |
80 } | |
81 throw new Exception('Unexpected owner: ${owner}'); | |
82 } | |
83 | |
84 | |
85 /** | |
86 * Returns the column of the start of a location. | |
87 */ | |
88 int getLocationColumn(Location location) { | |
89 String text = location.source.text; | |
90 int index = location.start-1; | |
91 var column = 0; | |
92 while (0 <= index && index < text.length) { | |
93 var charCode = text.charCodeAt(index); | |
94 if (charCode == $CR || charCode == $LF) { | |
95 break; | |
96 } | |
97 index--; | |
98 column++; | |
99 } | |
100 return column; | |
101 } | |
OLD | NEW |