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 import 'package:async_helper/async_helper.dart'; | 5 import 'package:async_helper/async_helper.dart'; |
6 import 'package:compiler/src/constants/values.dart' | 6 import 'package:compiler/src/constants/values.dart' |
7 show PrimitiveConstantValue; | 7 show PrimitiveConstantValue; |
8 import 'package:expect/expect.dart'; | 8 import 'package:expect/expect.dart'; |
9 import 'compiler_helper.dart'; | 9 import 'compiler_helper.dart'; |
10 import 'package:compiler/src/parser/partial_elements.dart' show | 10 import 'package:compiler/src/parser/partial_elements.dart' show |
11 PartialMetadataAnnotation; | 11 PartialMetadataAnnotation; |
| 12 import 'package:compiler/src/diagnostics/diagnostic_listener.dart' show |
| 13 DiagnosticReporter; |
12 | 14 |
13 void checkPosition(Spannable spannable, Node node, String source, compiler) { | 15 void checkPosition(Spannable spannable, |
14 SourceSpan span = compiler.spanFromSpannable(spannable); | 16 Node node, |
| 17 String source, |
| 18 DiagnosticReporter reporter) { |
| 19 SourceSpan span = reporter.spanFromSpannable(spannable); |
15 Expect.isTrue(span.begin < span.end, | 20 Expect.isTrue(span.begin < span.end, |
16 'begin = ${span.begin}; end = ${span.end}'); | 21 'begin = ${span.begin}; end = ${span.end}'); |
17 Expect.isTrue(span.end < source.length, | 22 Expect.isTrue(span.end < source.length, |
18 'end = ${span.end}; length = ${source.length}'); | 23 'end = ${span.end}; length = ${source.length}'); |
19 String yield = source.substring(span.begin, span.end); | 24 String yield = source.substring(span.begin, span.end); |
20 | 25 |
21 // TODO(ahe): The node does not include "@". Fix that. | 26 // TODO(ahe): The node does not include "@". Fix that. |
22 Expect.stringEquals('@$node', yield); | 27 Expect.stringEquals('@$node', yield); |
23 } | 28 } |
24 | 29 |
25 void checkAnnotation(String name, String declaration, | 30 void checkAnnotation(String name, String declaration, |
26 {bool isTopLevelOnly: false}) { | 31 {bool isTopLevelOnly: false}) { |
27 // Ensure that a compile-time constant can be resolved from an | 32 // Ensure that a compile-time constant can be resolved from an |
28 // annotation. | 33 // annotation. |
29 var source1 = """const native = 'xyz'; | 34 var source1 = """const native = 'xyz'; |
30 @native | 35 @native |
31 $declaration | 36 $declaration |
32 main() {}"""; | 37 main() {}"""; |
33 | 38 |
34 compileAndCheck(source1, name, (compiler, element) { | 39 compileAndCheck(source1, name, (compiler, element) { |
35 compiler.enqueuer.resolution.queueIsClosed = false; | 40 compiler.enqueuer.resolution.queueIsClosed = false; |
36 Expect.equals(1, element.metadata.length, | 41 Expect.equals(1, element.metadata.length, |
37 'Unexpected metadata count on $element.'); | 42 'Unexpected metadata count on $element.'); |
38 PartialMetadataAnnotation annotation = element.metadata.first; | 43 PartialMetadataAnnotation annotation = element.metadata.first; |
39 annotation.ensureResolved(compiler.resolution); | 44 annotation.ensureResolved(compiler.resolution); |
40 PrimitiveConstantValue value = | 45 PrimitiveConstantValue value = |
41 compiler.constants.getConstantValue(annotation.constant); | 46 compiler.constants.getConstantValue(annotation.constant); |
42 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); | 47 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); |
43 | 48 |
44 checkPosition(annotation, annotation.cachedNode, source1, compiler); | 49 checkPosition( |
| 50 annotation, annotation.cachedNode, source1, compiler.reporter); |
45 }); | 51 }); |
46 | 52 |
47 // Ensure that each repeated annotation has a unique instance of | 53 // Ensure that each repeated annotation has a unique instance of |
48 // [MetadataAnnotation]. | 54 // [MetadataAnnotation]. |
49 var source2 = """const native = 'xyz'; | 55 var source2 = """const native = 'xyz'; |
50 @native @native | 56 @native @native |
51 $declaration | 57 $declaration |
52 main() {}"""; | 58 main() {}"""; |
53 | 59 |
54 compileAndCheck(source2, name, (compiler, element) { | 60 compileAndCheck(source2, name, (compiler, element) { |
55 compiler.enqueuer.resolution.queueIsClosed = false; | 61 compiler.enqueuer.resolution.queueIsClosed = false; |
56 Expect.equals(2, element.metadata.length); | 62 Expect.equals(2, element.metadata.length); |
57 PartialMetadataAnnotation annotation1 = element.metadata.elementAt(0); | 63 PartialMetadataAnnotation annotation1 = element.metadata.elementAt(0); |
58 PartialMetadataAnnotation annotation2 = element.metadata.elementAt(1); | 64 PartialMetadataAnnotation annotation2 = element.metadata.elementAt(1); |
59 annotation1.ensureResolved(compiler.resolution); | 65 annotation1.ensureResolved(compiler.resolution); |
60 annotation2.ensureResolved(compiler.resolution); | 66 annotation2.ensureResolved(compiler.resolution); |
61 Expect.isFalse(identical(annotation1, annotation2), | 67 Expect.isFalse(identical(annotation1, annotation2), |
62 'expected unique instances'); | 68 'expected unique instances'); |
63 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); | 69 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); |
64 PrimitiveConstantValue value1 = | 70 PrimitiveConstantValue value1 = |
65 compiler.constants.getConstantValue(annotation1.constant); | 71 compiler.constants.getConstantValue(annotation1.constant); |
66 PrimitiveConstantValue value2 = | 72 PrimitiveConstantValue value2 = |
67 compiler.constants.getConstantValue(annotation2.constant); | 73 compiler.constants.getConstantValue(annotation2.constant); |
68 Expect.identical(value1, value2, 'expected same compile-time constant'); | 74 Expect.identical(value1, value2, 'expected same compile-time constant'); |
69 Expect.stringEquals('xyz', value1.primitiveValue.slowToString()); | 75 Expect.stringEquals('xyz', value1.primitiveValue.slowToString()); |
70 Expect.stringEquals('xyz', value2.primitiveValue.slowToString()); | 76 Expect.stringEquals('xyz', value2.primitiveValue.slowToString()); |
71 | 77 |
72 checkPosition(annotation1, annotation1.cachedNode, source2, compiler); | 78 checkPosition( |
73 checkPosition(annotation2, annotation2.cachedNode, source2, compiler); | 79 annotation1, annotation1.cachedNode, source2, compiler.reporter); |
| 80 checkPosition( |
| 81 annotation2, annotation2.cachedNode, source2, compiler.reporter); |
74 }); | 82 }); |
75 | 83 |
76 if (isTopLevelOnly) return; | 84 if (isTopLevelOnly) return; |
77 | 85 |
78 // Ensure that a compile-time constant can be resolved from an | 86 // Ensure that a compile-time constant can be resolved from an |
79 // annotation. | 87 // annotation. |
80 var source3 = """const native = 'xyz'; | 88 var source3 = """const native = 'xyz'; |
81 class Foo { | 89 class Foo { |
82 @native | 90 @native |
83 $declaration | 91 $declaration |
84 } | 92 } |
85 main() {}"""; | 93 main() {}"""; |
86 | 94 |
87 compileAndCheck(source3, 'Foo', (compiler, element) { | 95 compileAndCheck(source3, 'Foo', (compiler, element) { |
88 compiler.enqueuer.resolution.queueIsClosed = false; | 96 compiler.enqueuer.resolution.queueIsClosed = false; |
89 Expect.equals(0, element.metadata.length); | 97 Expect.equals(0, element.metadata.length); |
90 element.ensureResolved(compiler.resolution); | 98 element.ensureResolved(compiler.resolution); |
91 Expect.equals(0, element.metadata.length); | 99 Expect.equals(0, element.metadata.length); |
92 element = element.lookupLocalMember(name); | 100 element = element.lookupLocalMember(name); |
93 Expect.equals(1, element.metadata.length); | 101 Expect.equals(1, element.metadata.length); |
94 PartialMetadataAnnotation annotation = element.metadata.first; | 102 PartialMetadataAnnotation annotation = element.metadata.first; |
95 annotation.ensureResolved(compiler.resolution); | 103 annotation.ensureResolved(compiler.resolution); |
96 PrimitiveConstantValue value = | 104 PrimitiveConstantValue value = |
97 compiler.constants.getConstantValue(annotation.constant); | 105 compiler.constants.getConstantValue(annotation.constant); |
98 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); | 106 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); |
99 | 107 |
100 checkPosition(annotation, annotation.cachedNode, source3, compiler); | 108 checkPosition( |
| 109 annotation, annotation.cachedNode, source3, compiler.reporter); |
101 }); | 110 }); |
102 | 111 |
103 // Ensure that each repeated annotation has a unique instance of | 112 // Ensure that each repeated annotation has a unique instance of |
104 // [MetadataAnnotation]. | 113 // [MetadataAnnotation]. |
105 var source4 = """const native = 'xyz'; | 114 var source4 = """const native = 'xyz'; |
106 class Foo { | 115 class Foo { |
107 @native @native | 116 @native @native |
108 $declaration | 117 $declaration |
109 } | 118 } |
110 main() {}"""; | 119 main() {}"""; |
(...skipping 13 matching lines...) Expand all Loading... |
124 'expected unique instances'); | 133 'expected unique instances'); |
125 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); | 134 Expect.notEquals(annotation1, annotation2, 'expected unequal instances'); |
126 PrimitiveConstantValue value1 = | 135 PrimitiveConstantValue value1 = |
127 compiler.constants.getConstantValue(annotation1.constant); | 136 compiler.constants.getConstantValue(annotation1.constant); |
128 PrimitiveConstantValue value2 = | 137 PrimitiveConstantValue value2 = |
129 compiler.constants.getConstantValue(annotation2.constant); | 138 compiler.constants.getConstantValue(annotation2.constant); |
130 Expect.identical(value1, value2, 'expected same compile-time constant'); | 139 Expect.identical(value1, value2, 'expected same compile-time constant'); |
131 Expect.stringEquals('xyz', value1.primitiveValue.slowToString()); | 140 Expect.stringEquals('xyz', value1.primitiveValue.slowToString()); |
132 Expect.stringEquals('xyz', value2.primitiveValue.slowToString()); | 141 Expect.stringEquals('xyz', value2.primitiveValue.slowToString()); |
133 | 142 |
134 checkPosition(annotation1, annotation1.cachedNode, source4, compiler); | 143 checkPosition( |
135 checkPosition(annotation1, annotation2.cachedNode, source4, compiler); | 144 annotation1, annotation1.cachedNode, source4, compiler.reporter); |
| 145 checkPosition( |
| 146 annotation1, annotation2.cachedNode, source4, compiler.reporter); |
136 }); | 147 }); |
137 } | 148 } |
138 | 149 |
139 void testClassMetadata() { | 150 void testClassMetadata() { |
140 checkAnnotation('Foo', 'class Foo {}', isTopLevelOnly: true); | 151 checkAnnotation('Foo', 'class Foo {}', isTopLevelOnly: true); |
141 } | 152 } |
142 | 153 |
143 void testTopLevelMethodMetadata() { | 154 void testTopLevelMethodMetadata() { |
144 checkAnnotation('foo', 'foo() {}'); | 155 checkAnnotation('foo', 'foo() {}'); |
145 } | 156 } |
(...skipping 26 matching lines...) Expand all Loading... |
172 | 183 |
173 List<MetadataAnnotation> metadata = extractMetadata(element); | 184 List<MetadataAnnotation> metadata = extractMetadata(element); |
174 Expect.equals(1, metadata.length); | 185 Expect.equals(1, metadata.length); |
175 | 186 |
176 PartialMetadataAnnotation annotation = metadata.first; | 187 PartialMetadataAnnotation annotation = metadata.first; |
177 annotation.ensureResolved(compiler.resolution); | 188 annotation.ensureResolved(compiler.resolution); |
178 PrimitiveConstantValue value = | 189 PrimitiveConstantValue value = |
179 compiler.constants.getConstantValue(annotation.constant); | 190 compiler.constants.getConstantValue(annotation.constant); |
180 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); | 191 Expect.stringEquals('xyz', value.primitiveValue.slowToString()); |
181 | 192 |
182 checkPosition(annotation, annotation.cachedNode, source, compiler); | 193 checkPosition( |
| 194 annotation, annotation.cachedNode, source, compiler.reporter); |
183 })); | 195 })); |
184 } | 196 } |
185 | 197 |
186 var source; | 198 var source; |
187 | 199 |
188 source = """@native | 200 source = """@native |
189 library foo; | 201 library foo; |
190 const native = 'xyz'; | 202 const native = 'xyz'; |
191 main() {}"""; | 203 main() {}"""; |
192 compileAndCheckLibrary(source, (e) => e.libraryTag.metadata); | 204 compileAndCheckLibrary(source, (e) => e.libraryTag.metadata); |
(...skipping 23 matching lines...) Expand all Loading... |
216 compileAndCheckLibrary(source, | 228 compileAndCheckLibrary(source, |
217 (e) => e.compilationUnits.first.partTag.metadata); | 229 (e) => e.compilationUnits.first.partTag.metadata); |
218 } | 230 } |
219 | 231 |
220 void main() { | 232 void main() { |
221 testClassMetadata(); | 233 testClassMetadata(); |
222 testTopLevelMethodMetadata(); | 234 testTopLevelMethodMetadata(); |
223 testTopLevelFieldMetadata(); | 235 testTopLevelFieldMetadata(); |
224 testLibraryTags(); | 236 testLibraryTags(); |
225 } | 237 } |
OLD | NEW |