OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 dart2js.mirrors_used; | 5 library dart2js.mirrors_used; |
6 | 6 |
7 import 'compile_time_constants.dart' show | 7 import 'compile_time_constants.dart' show |
8 ConstantCompiler; | 8 ConstantCompiler; |
9 import 'constants/expressions.dart'; | 9 import 'constants/expressions.dart'; |
10 import 'constants/values.dart' show | 10 import 'constants/values.dart' show |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 | 130 |
131 /// Call-back from the resolver to analyze MirorsUsed annotations. The result | 131 /// Call-back from the resolver to analyze MirorsUsed annotations. The result |
132 /// is stored in [analyzer] and later used to compute | 132 /// is stored in [analyzer] and later used to compute |
133 /// [:analyzer.mergedMirrorUsage:]. | 133 /// [:analyzer.mergedMirrorUsage:]. |
134 void validate(NewExpression node, TreeElements mapping) { | 134 void validate(NewExpression node, TreeElements mapping) { |
135 for (Node argument in node.send.arguments) { | 135 for (Node argument in node.send.arguments) { |
136 NamedArgument named = argument.asNamedArgument(); | 136 NamedArgument named = argument.asNamedArgument(); |
137 if (named == null) continue; | 137 if (named == null) continue; |
138 ConstantCompiler constantCompiler = compiler.resolver.constantCompiler; | 138 ConstantCompiler constantCompiler = compiler.resolver.constantCompiler; |
139 ConstantValue value = | 139 ConstantValue value = |
140 constantCompiler.compileNode(named.expression, mapping).value; | 140 constantCompiler.getConstantValue( |
| 141 constantCompiler.compileNode(named.expression, mapping)); |
141 | 142 |
142 MirrorUsageBuilder builder = | 143 MirrorUsageBuilder builder = |
143 new MirrorUsageBuilder( | 144 new MirrorUsageBuilder( |
144 analyzer, mapping.analyzedElement.library, named.expression, | 145 analyzer, mapping.analyzedElement.library, named.expression, |
145 value, mapping); | 146 value, mapping); |
146 | 147 |
147 if (named.name.source == 'symbols') { | 148 if (named.name.source == 'symbols') { |
148 analyzer.cachedStrings[value] = | 149 analyzer.cachedStrings[value] = |
149 builder.convertConstantToUsageList(value, onlyStrings: true); | 150 builder.convertConstantToUsageList(value, onlyStrings: true); |
150 } else if (named.name.source == 'targets') { | 151 } else if (named.name.source == 'targets') { |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 /// annotations are represented as [MirrorUsage]. | 260 /// annotations are represented as [MirrorUsage]. |
260 List<MirrorUsage> mirrorsUsedOnLibraryTag(LibraryElement library, | 261 List<MirrorUsage> mirrorsUsedOnLibraryTag(LibraryElement library, |
261 Import tag) { | 262 Import tag) { |
262 LibraryElement importedLibrary = library.getLibraryFromTag(tag); | 263 LibraryElement importedLibrary = library.getLibraryFromTag(tag); |
263 if (importedLibrary != compiler.mirrorsLibrary) { | 264 if (importedLibrary != compiler.mirrorsLibrary) { |
264 return null; | 265 return null; |
265 } | 266 } |
266 List<MirrorUsage> result = <MirrorUsage>[]; | 267 List<MirrorUsage> result = <MirrorUsage>[]; |
267 for (MetadataAnnotation metadata in tag.metadata) { | 268 for (MetadataAnnotation metadata in tag.metadata) { |
268 metadata.ensureResolved(compiler); | 269 metadata.ensureResolved(compiler); |
269 Element element = | 270 ConstantValue value = |
270 metadata.constant.value.getType(compiler.coreTypes).element; | 271 compiler.constants.getConstantValue(metadata.constant); |
| 272 Element element = value.getType(compiler.coreTypes).element; |
271 if (element == compiler.mirrorsUsedClass) { | 273 if (element == compiler.mirrorsUsedClass) { |
272 result.add(buildUsage(metadata.constant.value)); | 274 result.add(buildUsage(value)); |
273 } | 275 } |
274 } | 276 } |
275 return result; | 277 return result; |
276 } | 278 } |
277 | 279 |
278 /// Merge all [MirrorUsage] instances accross all libraries. | 280 /// Merge all [MirrorUsage] instances accross all libraries. |
279 MirrorUsage mergeUsages(Map<LibraryElement, List<MirrorUsage>> usageMap) { | 281 MirrorUsage mergeUsages(Map<LibraryElement, List<MirrorUsage>> usageMap) { |
280 Set<MirrorUsage> usagesToMerge = new Set<MirrorUsage>(); | 282 Set<MirrorUsage> usagesToMerge = new Set<MirrorUsage>(); |
281 usageMap.forEach((LibraryElement library, List<MirrorUsage> usages) { | 283 usageMap.forEach((LibraryElement library, List<MirrorUsage> usages) { |
282 librariesWithUsage.add(library); | 284 librariesWithUsage.add(library); |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 } | 563 } |
562 return scope.localLookup(name); | 564 return scope.localLookup(name); |
563 } | 565 } |
564 return null; | 566 return null; |
565 } | 567 } |
566 | 568 |
567 /// Attempt to find a [Spannable] corresponding to constant. | 569 /// Attempt to find a [Spannable] corresponding to constant. |
568 Spannable positionOf(ConstantValue constant) { | 570 Spannable positionOf(ConstantValue constant) { |
569 Node node; | 571 Node node; |
570 elements.forEachConstantNode((Node n, ConstantExpression c) { | 572 elements.forEachConstantNode((Node n, ConstantExpression c) { |
571 if (node == null && c.value == constant) { | 573 if (node == null && compiler.constants.getConstantValue(c) == constant) { |
572 node = n; | 574 node = n; |
573 } | 575 } |
574 }); | 576 }); |
575 if (node == null) { | 577 if (node == null) { |
576 // TODO(ahe): Returning [spannable] here leads to confusing error | 578 // TODO(ahe): Returning [spannable] here leads to confusing error |
577 // messages. For example, consider: | 579 // messages. For example, consider: |
578 // @MirrorsUsed(targets: fisk) | 580 // @MirrorsUsed(targets: fisk) |
579 // import 'dart:mirrors'; | 581 // import 'dart:mirrors'; |
580 // | 582 // |
581 // const fisk = const [main]; | 583 // const fisk = const [main]; |
582 // | 584 // |
583 // main() {} | 585 // main() {} |
584 // | 586 // |
585 // The message is: | 587 // The message is: |
586 // example.dart:1:23: Hint: Can't use 'fisk' here because ... | 588 // example.dart:1:23: Hint: Can't use 'fisk' here because ... |
587 // Did you forget to add quotes? | 589 // Did you forget to add quotes? |
588 // @MirrorsUsed(targets: fisk) | 590 // @MirrorsUsed(targets: fisk) |
589 // ^^^^ | 591 // ^^^^ |
590 // | 592 // |
591 // Instead of saying 'fisk' should pretty print the problematic constant | 593 // Instead of saying 'fisk' should pretty print the problematic constant |
592 // value. | 594 // value. |
593 return spannable; | 595 return spannable; |
594 } | 596 } |
595 return node; | 597 return node; |
596 } | 598 } |
597 } | 599 } |
OLD | NEW |