| 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.dart'; |
| 8 import '../common_elements.dart' show ElementEnvironment; | 8 import '../common_elements.dart' show CommonElements; |
| 9 import '../compiler.dart' show Compiler; | 9 import '../compiler.dart' show Compiler; |
| 10 import '../constants/values.dart'; | 10 import '../constants/values.dart'; |
| 11 import '../elements/elements.dart'; | 11 import '../elements/elements.dart'; |
| 12 import '../elements/entities.dart'; | |
| 13 import 'backend.dart'; | |
| 14 | 12 |
| 15 /// Handling of special annotations for tests. | 13 /// Handling of special annotations for tests. |
| 16 class Annotations { | 14 class OptimizerHintsForTests { |
| 17 static final Uri PACKAGE_EXPECT = | 15 final Compiler _compiler; |
| 18 new Uri(scheme: 'package', path: 'expect/expect.dart'); | |
| 19 | 16 |
| 20 final Compiler compiler; | 17 OptimizerHintsForTests(this._compiler); |
| 21 | 18 |
| 22 ClassElement expectNoInlineClass; | 19 CommonElements get _commonElements => _compiler.commonElements; |
| 23 ClassElement expectTrustTypeAnnotationsClass; | |
| 24 ClassElement expectAssumeDynamicClass; | |
| 25 | |
| 26 JavaScriptBackend get backend => compiler.backend; | |
| 27 | |
| 28 DiagnosticReporter get reporter => compiler.reporter; | |
| 29 | |
| 30 ElementEnvironment get _elementEnvironment => compiler.elementEnvironment; | |
| 31 | |
| 32 Annotations(this.compiler); | |
| 33 | |
| 34 void onLibraryLoaded(LibraryEntity library) { | |
| 35 if (library.canonicalUri == PACKAGE_EXPECT) { | |
| 36 expectNoInlineClass = | |
| 37 _elementEnvironment.lookupClass(library, 'NoInline'); | |
| 38 expectTrustTypeAnnotationsClass = | |
| 39 _elementEnvironment.lookupClass(library, 'TrustTypeAnnotations'); | |
| 40 expectAssumeDynamicClass = | |
| 41 _elementEnvironment.lookupClass(library, 'AssumeDynamic'); | |
| 42 if (expectNoInlineClass == null || | |
| 43 expectTrustTypeAnnotationsClass == null || | |
| 44 expectAssumeDynamicClass == null) { | |
| 45 // This is not the package you're looking for. | |
| 46 expectNoInlineClass = null; | |
| 47 expectTrustTypeAnnotationsClass = null; | |
| 48 expectAssumeDynamicClass = null; | |
| 49 } | |
| 50 } | |
| 51 } | |
| 52 | 20 |
| 53 /// Returns `true` if inlining is disabled for [element]. | 21 /// Returns `true` if inlining is disabled for [element]. |
| 54 bool noInline(Element element) { | 22 bool noInline(Element element) { |
| 55 if (_hasAnnotation(element, expectNoInlineClass)) { | 23 if (_hasAnnotation(element, _commonElements.expectNoInlineClass)) { |
| 56 // TODO(floitsch): restrict to elements from the test directory. | 24 // TODO(floitsch): restrict to elements from the test directory. |
| 57 return true; | 25 return true; |
| 58 } | 26 } |
| 59 return _hasAnnotation(element, compiler.commonElements.noInlineClass); | 27 return _hasAnnotation(element, _commonElements.noInlineClass); |
| 60 } | 28 } |
| 61 | 29 |
| 62 /// Returns `true` if parameter and returns types should be trusted for | 30 /// Returns `true` if parameter and returns types should be trusted for |
| 63 /// [element]. | 31 /// [element]. |
| 64 bool trustTypeAnnotations(Element element) { | 32 bool trustTypeAnnotations(Element element) { |
| 65 return _hasAnnotation(element, expectTrustTypeAnnotationsClass); | 33 return _hasAnnotation( |
| 34 element, _commonElements.expectTrustTypeAnnotationsClass); |
| 66 } | 35 } |
| 67 | 36 |
| 68 /// Returns `true` if inference of parameter types is disabled for [element]. | 37 /// Returns `true` if inference of parameter types is disabled for [element]. |
| 69 bool assumeDynamic(Element element) { | 38 bool assumeDynamic(Element element) { |
| 70 return _hasAnnotation(element, expectAssumeDynamicClass); | 39 return _hasAnnotation(element, _commonElements.expectAssumeDynamicClass); |
| 71 } | 40 } |
| 72 | 41 |
| 73 /// Returns `true` if [element] is annotated with [annotationClass]. | 42 /// Returns `true` if [element] is annotated with [annotationClass]. |
| 74 bool _hasAnnotation(Element element, ClassElement annotationClass) { | 43 bool _hasAnnotation(Element element, ClassElement annotationClass) { |
| 75 if (annotationClass == null) return false; | 44 if (annotationClass == null) return false; |
| 76 return reporter.withCurrentElement(element, () { | 45 return _compiler.reporter.withCurrentElement(element, () { |
| 77 for (MetadataAnnotation metadata in element.metadata) { | 46 for (MetadataAnnotation metadata in element.metadata) { |
| 78 assert(invariant(metadata, metadata.constant != null, | 47 assert(invariant(metadata, metadata.constant != null, |
| 79 message: "Unevaluated metadata constant.")); | 48 message: "Unevaluated metadata constant.")); |
| 80 ConstantValue value = | 49 ConstantValue value = |
| 81 compiler.constants.getConstantValue(metadata.constant); | 50 _compiler.constants.getConstantValue(metadata.constant); |
| 82 if (value.isConstructedObject) { | 51 if (value.isConstructedObject) { |
| 83 ConstructedConstantValue constructedConstant = value; | 52 ConstructedConstantValue constructedConstant = value; |
| 84 if (constructedConstant.type.element == annotationClass) { | 53 if (constructedConstant.type.element == annotationClass) { |
| 85 return true; | 54 return true; |
| 86 } | 55 } |
| 87 } | 56 } |
| 88 } | 57 } |
| 89 return false; | 58 return false; |
| 90 }); | 59 }); |
| 91 } | 60 } |
| 92 } | 61 } |
| OLD | NEW |