OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, 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 mirror_system_helper; | |
6 | |
7 import 'dart:async'; | |
8 import 'package:compiler/src/mirrors/source_mirrors.dart'; | |
9 import 'package:compiler/src/mirrors/dart2js_mirrors.dart'; | |
10 import 'mock_compiler.dart'; | |
11 | |
12 export 'package:compiler/src/mirrors/source_mirrors.dart'; | |
13 export 'package:compiler/src/mirrors/mirrors_util.dart'; | |
14 | |
15 const String SOURCE = 'source'; | |
16 final Uri SOURCE_URI = new Uri(scheme: SOURCE, path: SOURCE); | |
17 | |
18 // TODO(johnniwinther): Move this to a mirrors helper library. | |
19 Future<MirrorSystem> createMirrorSystem(String source) { | |
20 MockCompiler compiler = new MockCompiler.internal( | |
21 analyzeOnly: true, | |
22 analyzeAll: true, | |
23 preserveComments: true); | |
24 compiler.registerSource(SOURCE_URI, source); | |
25 compiler.librariesToAnalyzeWhenRun = <Uri>[SOURCE_URI]; | |
26 return compiler.run(null).then((_) { | |
27 return new Dart2JsMirrorSystem(compiler); | |
28 }); | |
29 } | |
30 | |
31 /** | |
32 * Returns [:true:] if [type] is an instance of [:decl:] with type arguments | |
33 * equal to [typeArgument]. | |
34 */ | |
35 bool isInstance(ClassMirror decl, List<TypeMirror> typeArguments, | |
36 ClassMirror type) { | |
37 if (type.isOriginalDeclaration) return false; | |
38 if (!isSameDeclaration(decl, type)) return false; | |
39 return areEqualsTypes(typeArguments, type.typeArguments); | |
40 } | |
41 | |
42 /** | |
43 * Returns [:true:] if [type] is the same type as [expected]. This method | |
44 * equates a non-generic declaration with its instantiation. | |
45 */ | |
46 bool isEqualType(TypeMirror expected, TypeMirror type) { | |
47 if (expected == type) return true; | |
48 if (expected is ClassMirror && type is ClassMirror) { | |
49 if (!isSameDeclaration(expected, type)) return false; | |
50 if (expected.isOriginalDeclaration || expected.typeArguments.isEmpty) { | |
51 return type.isOriginalDeclaration || type.typeArguments.isEmpty; | |
52 } | |
53 return areEqualsTypes(expected.typeArguments, type.typeArguments); | |
54 } | |
55 return true; | |
56 } | |
57 | |
58 /** | |
59 * Returns [:true:] if [types] are equals to [expected] using the equalitry | |
60 * defined by [isEqualType]. | |
61 */ | |
62 bool areEqualsTypes(List<TypeMirror> expected, List<TypeMirror> types) { | |
63 return checkSameList(expected, types, isEqualType); | |
64 } | |
65 | |
66 /** | |
67 * Returns [:true:] if an instance of [type] with type arguments equal to | |
68 * [typeArguments] is found in [types]. | |
69 */ | |
70 bool containsType(ClassMirror decl, List<TypeMirror> typeArguments, | |
71 Iterable<TypeMirror> types) { | |
72 return types.any((type) => isInstance(decl, typeArguments, type)); | |
73 } | |
74 | |
75 /** | |
76 * Returns the declaration of [type]. | |
77 */ | |
78 TypeMirror toDeclaration(TypeMirror type) { | |
79 return type is ClassMirror ? type.originalDeclaration : type; | |
80 } | |
81 | |
82 /** | |
83 * Returns [:true:] if [type] is of the same declaration as [expected]. | |
84 */ | |
85 bool isSameDeclaration(TypeMirror expected, TypeMirror type) { | |
86 return toDeclaration(expected) == toDeclaration(type); | |
87 } | |
88 | |
89 /** | |
90 * Returns [:true:] if a type of the declaration of [expected] is in [types]. | |
91 */ | |
92 bool containsDeclaration(TypeMirror expected, Iterable<TypeMirror> types) { | |
93 for (var type in types) { | |
94 if (isSameDeclaration(expected, type)) { | |
95 return true; | |
96 } | |
97 } | |
98 return false; | |
99 } | |
100 | |
101 /** | |
102 * Returns [:true:] if declarations of [expected] are the same as those of | |
103 * [types], taking order into account. | |
104 */ | |
105 bool isSameDeclarationList(Iterable<TypeMirror> expected, | |
106 Iterable<TypeMirror> types) { | |
107 return checkSameList(expected, types, isSameDeclaration); | |
108 } | |
109 | |
110 /** | |
111 * Returns [:true:] if declarations of [expected] are the same as those of | |
112 * [iterable], not taking order into account. | |
113 */ | |
114 bool isSameDeclarationSet(Iterable<TypeMirror> expected, | |
115 Iterable<TypeMirror> types) { | |
116 Set<TypeMirror> expectedSet = expected.map(toDeclaration).toSet(); | |
117 Set<TypeMirror> typesSet = types.map(toDeclaration).toSet(); | |
118 return expectedSet.length == typesSet.length && | |
119 expectedSet.containsAll(typesSet); | |
120 } | |
121 | |
122 /** | |
123 * Utility method for checking whether [expected] and [iterable] contains the | |
124 * same elements with respect to the checking function [check], takin order | |
125 * into account. | |
126 */ | |
127 bool checkSameList(Iterable<TypeMirror> expected, | |
128 Iterable<TypeMirror> types, | |
129 bool check(TypeMirror a, TypeMirror b)) { | |
130 if (expected.length != types.length) return false; | |
131 Iterator<TypeMirror> expectedIterator = expected.iterator; | |
132 Iterator<TypeMirror> typesIterator = types.iterator; | |
133 while (expectedIterator.moveNext() && typesIterator.moveNext()) { | |
134 if (!check(expectedIterator.current, typesIterator.current)) { | |
135 return false; | |
136 } | |
137 } | |
138 return true; | |
139 } | |
OLD | NEW |