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 #library('mirrors.util'); | 5 #library('mirrors.util'); |
| 6 | 6 |
| 7 // TODO(rnystrom): Use "package:" URL (#4968). | 7 // TODO(rnystrom): Use "package:" URL (#4968). |
| 8 #import('mirrors.dart'); | 8 #import('mirrors.dart'); |
| 9 #import('../../../lib/compiler/implementation/util/characters.dart'); | 9 #import('../../../lib/compiler/implementation/util/characters.dart'); |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 for (InterfaceMirror otherType in library.types.getValues()) { | 23 for (InterfaceMirror otherType in library.types.getValues()) { |
| 24 var superClass = otherType.superclass; | 24 var superClass = otherType.superclass; |
| 25 if (superClass !== null) { | 25 if (superClass !== null) { |
| 26 superClass = superClass.declaration; | 26 superClass = superClass.declaration; |
| 27 if (type.library === superClass.library) { | 27 if (type.library === superClass.library) { |
| 28 if (superClass == type) { | 28 if (superClass == type) { |
| 29 subtypes.add(otherType); | 29 subtypes.add(otherType); |
| 30 } | 30 } |
| 31 } | 31 } |
| 32 } | 32 } |
| 33 final superInterfaces = otherType.interfaces.getValues(); | 33 final superInterfaces = otherType.interfaces; |
| 34 for (InterfaceMirror superInterface in superInterfaces) { | 34 for (InterfaceMirror superInterface in superInterfaces) { |
| 35 superInterface = superInterface.declaration; | 35 superInterface = superInterface.declaration; |
| 36 if (type.library === superInterface.library) { | 36 if (type.library === superInterface.library) { |
| 37 if (superInterface == type) { | 37 if (superInterface == type) { |
| 38 subtypes.add(otherType); | 38 subtypes.add(otherType); |
| 39 } | 39 } |
| 40 } | 40 } |
| 41 } | 41 } |
| 42 } | 42 } |
| 43 }); | 43 }); |
| 44 return subtypes; | 44 return subtypes; |
| 45 } | 45 } |
| 46 | 46 |
| 47 /** | |
| 48 * Finds the mirror in [map] by the simple name [name]. If [constructorName] or | |
| 49 * [operatorName] is provided, a constructor/operator method by that name is | |
| 50 * returned. | |
| 51 */ | |
| 52 Mirror findMirror(Map<Object,Mirror> map, String name, | |
| 53 [String constructorName, String operatorName]) { | |
| 54 var foundMirror = null; | |
| 55 map.forEach((_, Mirror mirror) { | |
| 56 if (mirror.simpleName == name) { | |
| 57 if (constructorName !== null) { | |
| 58 if (mirror is MethodMirror && | |
| 59 constructorName == mirror.constructorName) { | |
| 60 foundMirror = mirror; | |
| 61 } | |
| 62 } else if (operatorName !== null) { | |
| 63 if (mirror is MethodMirror && | |
| 64 operatorName == mirror.operatorName) { | |
| 65 foundMirror = mirror; | |
| 66 } | |
| 67 } else { | |
| 68 foundMirror = mirror; | |
| 69 } | |
| 70 } | |
| 71 }); | |
| 72 return foundMirror; | |
| 73 } | |
| 74 | |
| 75 LibraryMirror findLibrary(MemberMirror member) { | 47 LibraryMirror findLibrary(MemberMirror member) { |
| 76 ObjectMirror owner = member.surroundingDeclaration; | 48 ObjectMirror owner = member.surroundingDeclaration; |
| 77 if (owner is LibraryMirror) { | 49 if (owner is LibraryMirror) { |
| 78 return owner; | 50 return owner; |
| 79 } else if (owner is TypeMirror) { | 51 } else if (owner is TypeMirror) { |
| 80 return owner.library; | 52 return owner.library; |
| 81 } | 53 } |
| 82 throw new Exception('Unexpected owner: ${owner}'); | 54 throw new Exception('Unexpected owner: ${owner}'); |
| 83 } | 55 } |
| 84 | 56 |
| 85 | 57 |
| 86 /** | 58 /** |
| 87 * Returns the column of the start of a location. | 59 * Returns the column of the start of a location. |
| 88 */ | 60 */ |
| 89 int getLocationColumn(Location location) { | 61 int getLocationColumn(Location location) { |
| 90 String text = location.source.text; | 62 String text = location.source.text; |
| 91 int index = location.start-1; | 63 int index = location.start-1; |
| 92 var column = 0; | 64 var column = 0; |
| 93 while (0 <= index && index < text.length) { | 65 while (0 <= index && index < text.length) { |
| 94 var charCode = text.charCodeAt(index); | 66 var charCode = text.charCodeAt(index); |
| 95 if (charCode == $CR || charCode == $LF) { | 67 if (charCode == $CR || charCode == $LF) { |
| 96 break; | 68 break; |
| 97 } | 69 } |
| 98 index--; | 70 index--; |
| 99 column++; | 71 column++; |
| 100 } | 72 } |
| 101 return column; | 73 return column; |
| 102 } | 74 } |
| 75 | |
| 76 class HierarchyIterable implements Iterable<InterfaceMirror> { | |
|
Lasse Reichstein Nielsen
2012/10/04 07:41:19
Consider whether Iterable is necessary in the name
Johnni Winther
2012/10/04 12:46:21
I can't think of a sensible name without Iterable.
| |
| 77 final bool includeType; | |
| 78 final InterfaceMirror type; | |
| 79 | |
| 80 HierarchyIterable(this.type, {bool includeType}) | |
| 81 : this.includeType = includeType; | |
| 82 | |
| 83 Iterator<InterfaceMirror> iterator() => | |
| 84 new HierarchyIterator(type, includeType: includeType); | |
| 85 } | |
| 86 | |
| 87 /** | |
| 88 * [HierarchyIterator] iterates through the class hierarchy of the provided | |
| 89 * type. | |
| 90 * | |
| 91 * First is the superclass relation is traversed, next the superinterface | |
| 92 * relation and finally is [Object] visited. | |
|
Lasse Reichstein Nielsen
2012/10/04 07:41:19
Why do you mention Object explicitly? It is in the
Johnni Winther
2012/10/04 12:46:21
Done.
| |
| 93 */ | |
| 94 class HierarchyIterator implements Iterator<InterfaceMirror> { | |
| 95 final Queue<InterfaceMirror> queue = new Queue<InterfaceMirror>(); | |
| 96 InterfaceMirror object; | |
| 97 | |
| 98 HierarchyIterator(InterfaceMirror type, {bool includeType}) { | |
| 99 if (includeType) { | |
| 100 queue.add(type); | |
| 101 } else { | |
| 102 push(type); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 InterfaceMirror push(InterfaceMirror type) { | |
| 107 if (type.superclass !== null) { | |
| 108 if (type.superclass.isObject) { | |
| 109 object = type.superclass; | |
| 110 } else { | |
| 111 queue.addFirst(type.superclass); | |
| 112 } | |
| 113 } | |
| 114 queue.addAll(type.interfaces); | |
| 115 return type; | |
| 116 } | |
| 117 | |
| 118 InterfaceMirror next() { | |
| 119 InterfaceMirror type; | |
| 120 if (queue.isEmpty()) { | |
| 121 if (object === null) { | |
| 122 throw new NoMoreElementsException(); | |
| 123 } | |
| 124 type = object; | |
| 125 object = null; | |
| 126 return type; | |
| 127 } else { | |
| 128 return push(queue.removeFirst()); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 bool hasNext() => !queue.isEmpty() || object !== null; | |
| 133 } | |
| OLD | NEW |