OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 js_backend.backend.annotations; | 5 library js_backend.backend.annotations; |
6 | 6 |
7 import '../common.dart'; | 7 import '../common_elements.dart' show CommonElements, ElementEnvironment; |
8 import '../common_elements.dart' show CommonElements; | |
9 import '../compiler.dart' show Compiler; | |
10 import '../constants/values.dart'; | 8 import '../constants/values.dart'; |
11 import '../elements/elements.dart'; | 9 import '../elements/entities.dart'; |
12 | 10 |
13 /// Handling of special annotations for tests. | 11 /// Handling of special annotations for tests. |
14 class OptimizerHintsForTests { | 12 class OptimizerHintsForTests { |
15 final Compiler _compiler; | 13 final ElementEnvironment _elementEnvironment; |
| 14 final CommonElements _commonElements; |
16 | 15 |
17 OptimizerHintsForTests(this._compiler); | 16 OptimizerHintsForTests(this._elementEnvironment, this._commonElements); |
18 | |
19 CommonElements get _commonElements => _compiler.commonElements; | |
20 | 17 |
21 /// Returns `true` if inlining is disabled for [element]. | 18 /// Returns `true` if inlining is disabled for [element]. |
22 bool noInline(Element element) { | 19 bool noInline(MemberEntity element) { |
23 if (_hasAnnotation(element, _commonElements.expectNoInlineClass)) { | 20 if (_hasAnnotation(element, _commonElements.expectNoInlineClass)) { |
24 // TODO(floitsch): restrict to elements from the test directory. | 21 // TODO(floitsch): restrict to elements from the test directory. |
25 return true; | 22 return true; |
26 } | 23 } |
27 return _hasAnnotation(element, _commonElements.noInlineClass); | 24 return _hasAnnotation(element, _commonElements.noInlineClass); |
28 } | 25 } |
29 | 26 |
30 /// Returns `true` if parameter and returns types should be trusted for | 27 /// Returns `true` if parameter and returns types should be trusted for |
31 /// [element]. | 28 /// [element]. |
32 bool trustTypeAnnotations(Element element) { | 29 bool trustTypeAnnotations(MemberEntity element) { |
33 return _hasAnnotation( | 30 return _hasAnnotation( |
34 element, _commonElements.expectTrustTypeAnnotationsClass); | 31 element, _commonElements.expectTrustTypeAnnotationsClass); |
35 } | 32 } |
36 | 33 |
37 /// Returns `true` if inference of parameter types is disabled for [element]. | 34 /// Returns `true` if inference of parameter types is disabled for [element]. |
38 bool assumeDynamic(Element element) { | 35 bool assumeDynamic(MemberEntity element) { |
39 return _hasAnnotation(element, _commonElements.expectAssumeDynamicClass); | 36 return _hasAnnotation(element, _commonElements.expectAssumeDynamicClass); |
40 } | 37 } |
41 | 38 |
42 /// Returns `true` if [element] is annotated with [annotationClass]. | 39 /// Returns `true` if [element] is annotated with [annotationClass]. |
43 bool _hasAnnotation(Element element, ClassElement annotationClass) { | 40 bool _hasAnnotation(MemberEntity element, ClassEntity annotationClass) { |
44 if (annotationClass == null) return false; | 41 if (annotationClass == null) return false; |
45 return _compiler.reporter.withCurrentElement(element, () { | 42 for (ConstantValue value |
46 for (MetadataAnnotation metadata in element.metadata) { | 43 in _elementEnvironment.getMemberMetadata(element)) { |
47 assert(invariant(metadata, metadata.constant != null, | 44 if (value.isConstructedObject) { |
48 message: "Unevaluated metadata constant.")); | 45 ConstructedConstantValue constructedConstant = value; |
49 ConstantValue value = | 46 if (constructedConstant.type.element == annotationClass) { |
50 _compiler.constants.getConstantValue(metadata.constant); | 47 return true; |
51 if (value.isConstructedObject) { | |
52 ConstructedConstantValue constructedConstant = value; | |
53 if (constructedConstant.type.element == annotationClass) { | |
54 return true; | |
55 } | |
56 } | 48 } |
57 } | 49 } |
58 return false; | 50 } |
59 }); | 51 return false; |
60 } | 52 } |
61 } | 53 } |
OLD | NEW |