| 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 /** | 5 /** |
| 6 * This library contains the infrastructure to parse and integrate patch files. | 6 * This library contains the infrastructure to parse and integrate patch files. |
| 7 * | 7 * |
| 8 * Three types of elements can be patched: [LibraryElement], [ClassElement], | 8 * Three types of elements can be patched: [LibraryElement], [ClassElement], |
| 9 * [FunctionElement]. Patches are introduced in patch libraries which are loaded | 9 * [FunctionElement]. Patches are introduced in patch libraries which are loaded |
| 10 * together with the corresponding origin library. Which libraries that are | 10 * together with the corresponding origin library. Which libraries that are |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 import 'diagnostics/messages.dart' show | 126 import 'diagnostics/messages.dart' show |
| 127 MessageKind; | 127 MessageKind; |
| 128 import 'elements/elements.dart'; | 128 import 'elements/elements.dart'; |
| 129 import 'elements/modelx.dart' show | 129 import 'elements/modelx.dart' show |
| 130 BaseFunctionElementX, | 130 BaseFunctionElementX, |
| 131 ClassElementX, | 131 ClassElementX, |
| 132 GetterElementX, | 132 GetterElementX, |
| 133 LibraryElementX, | 133 LibraryElementX, |
| 134 MetadataAnnotationX, | 134 MetadataAnnotationX, |
| 135 SetterElementX; | 135 SetterElementX; |
| 136 import 'js_backend/js_backend.dart' show |
| 137 JavaScriptBackend; |
| 136 import 'library_loader.dart' show | 138 import 'library_loader.dart' show |
| 137 LibraryLoader; | 139 LibraryLoader; |
| 138 import 'parser/listener.dart' show | 140 import 'parser/listener.dart' show |
| 139 Listener, | 141 Listener, |
| 140 ParserError; | 142 ParserError; |
| 141 import 'parser/element_listener.dart' show | 143 import 'parser/element_listener.dart' show |
| 142 ElementListener; | 144 ElementListener; |
| 143 import 'parser/member_listener.dart' show | 145 import 'parser/member_listener.dart' show |
| 144 MemberListener; | 146 MemberListener; |
| 145 import 'parser/partial_elements.dart' show | 147 import 'parser/partial_elements.dart' show |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 } | 298 } |
| 297 | 299 |
| 298 void patchElement(Compiler compiler, | 300 void patchElement(Compiler compiler, |
| 299 Element origin, | 301 Element origin, |
| 300 Element patch) { | 302 Element patch) { |
| 301 if (origin == null) { | 303 if (origin == null) { |
| 302 compiler.reportErrorMessage( | 304 compiler.reportErrorMessage( |
| 303 patch, MessageKind.PATCH_NON_EXISTING, {'name': patch.name}); | 305 patch, MessageKind.PATCH_NON_EXISTING, {'name': patch.name}); |
| 304 return; | 306 return; |
| 305 } | 307 } |
| 308 |
| 306 if (!(origin.isClass || | 309 if (!(origin.isClass || |
| 307 origin.isConstructor || | 310 origin.isConstructor || |
| 308 origin.isFunction || | 311 origin.isFunction || |
| 309 origin.isAbstractField)) { | 312 origin.isAbstractField)) { |
| 310 // TODO(ahe): Remove this error when the parser rejects all bad modifiers. | 313 // TODO(ahe): Remove this error when the parser rejects all bad modifiers. |
| 311 compiler.reportErrorMessage(origin, MessageKind.PATCH_NONPATCHABLE); | 314 compiler.reportErrorMessage(origin, MessageKind.PATCH_NONPATCHABLE); |
| 312 return; | 315 return; |
| 313 } | 316 } |
| 314 if (patch.isClass) { | 317 if (patch.isClass) { |
| 315 tryPatchClass(compiler, origin, patch); | 318 tryPatchClass(compiler, origin, patch); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 checkNativeAnnotation(compiler, patch); | 361 checkNativeAnnotation(compiler, patch); |
| 359 } | 362 } |
| 360 | 363 |
| 361 /// Check whether [cls] has a `@Native(...)` annotation, and if so, set its | 364 /// Check whether [cls] has a `@Native(...)` annotation, and if so, set its |
| 362 /// native name from the annotation. | 365 /// native name from the annotation. |
| 363 checkNativeAnnotation(Compiler compiler, ClassElement cls) { | 366 checkNativeAnnotation(Compiler compiler, ClassElement cls) { |
| 364 EagerAnnotationHandler.checkAnnotation(compiler, cls, | 367 EagerAnnotationHandler.checkAnnotation(compiler, cls, |
| 365 const NativeAnnotationHandler()); | 368 const NativeAnnotationHandler()); |
| 366 } | 369 } |
| 367 | 370 |
| 371 checkJsInteropAnnotation(Compiler compiler, element) { |
| 372 EagerAnnotationHandler.checkAnnotation(compiler, element, |
| 373 const JsInteropAnnotationHandler()); |
| 374 } |
| 375 |
| 376 |
| 368 /// Abstract interface for pre-resolution detection of metadata. | 377 /// Abstract interface for pre-resolution detection of metadata. |
| 369 /// | 378 /// |
| 370 /// The detection is handled in two steps: | 379 /// The detection is handled in two steps: |
| 371 /// - match the annotation syntactically and assume that the annotation is valid | 380 /// - match the annotation syntactically and assume that the annotation is valid |
| 372 /// if it looks correct, | 381 /// if it looks correct, |
| 373 /// - setup a deferred action to check that the annotation has a valid constant | 382 /// - setup a deferred action to check that the annotation has a valid constant |
| 374 /// value and report an internal error if not. | 383 /// value and report an internal error if not. |
| 375 abstract class EagerAnnotationHandler<T> { | 384 abstract class EagerAnnotationHandler<T> { |
| 376 /// Checks that [annotation] looks like a matching annotation and optionally | 385 /// Checks that [annotation] looks like a matching annotation and optionally |
| 377 /// applies actions on [element]. Returns a non-null annotation marker if the | 386 /// applies actions on [element]. Returns a non-null annotation marker if the |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 Element element, | 454 Element element, |
| 446 MetadataAnnotation annotation, | 455 MetadataAnnotation annotation, |
| 447 ConstantValue constant) { | 456 ConstantValue constant) { |
| 448 if (constant.getType(compiler.coreTypes).element != | 457 if (constant.getType(compiler.coreTypes).element != |
| 449 compiler.nativeAnnotationClass) { | 458 compiler.nativeAnnotationClass) { |
| 450 compiler.internalError(annotation, 'Invalid @Native(...) annotation.'); | 459 compiler.internalError(annotation, 'Invalid @Native(...) annotation.'); |
| 451 } | 460 } |
| 452 } | 461 } |
| 453 } | 462 } |
| 454 | 463 |
| 464 /// Annotation handler for pre-resolution detection of `@Js(...)` |
| 465 /// annotations. |
| 466 class JsInteropAnnotationHandler implements EagerAnnotationHandler<bool> { |
| 467 const JsInteropAnnotationHandler(); |
| 468 |
| 469 bool hasJsNameAnnotation(MetadataAnnotation annotation) => |
| 470 annotation.beginToken != null && annotation.beginToken.next.value == 'Js'; |
| 471 |
| 472 bool apply(Compiler compiler, |
| 473 Element element, |
| 474 MetadataAnnotation annotation) { |
| 475 bool hasJsInterop = hasJsNameAnnotation(annotation); |
| 476 if (hasJsInterop) { |
| 477 element.markAsJsInterop(); |
| 478 } |
| 479 // Due to semantics of apply in the baseclass we have to return null to |
| 480 // indicate that no match was found. |
| 481 return hasJsInterop ? true : null; |
| 482 } |
| 483 |
| 484 @override |
| 485 void validate(Compiler compiler, |
| 486 Element element, |
| 487 MetadataAnnotation annotation, |
| 488 ConstantValue constant) { |
| 489 JavaScriptBackend backend = compiler.backend; |
| 490 if (constant.getType(compiler.coreTypes).element != |
| 491 backend.jsAnnotationClass) { |
| 492 compiler.internalError(annotation, 'Invalid @Js(...) annotation.'); |
| 493 } |
| 494 } |
| 495 } |
| 496 |
| 455 /// Annotation handler for pre-resolution detection of `@patch` annotations. | 497 /// Annotation handler for pre-resolution detection of `@patch` annotations. |
| 456 class PatchAnnotationHandler implements EagerAnnotationHandler<PatchVersion> { | 498 class PatchAnnotationHandler implements EagerAnnotationHandler<PatchVersion> { |
| 457 const PatchAnnotationHandler(); | 499 const PatchAnnotationHandler(); |
| 458 | 500 |
| 459 PatchVersion getPatchVersion(MetadataAnnotation annotation) { | 501 PatchVersion getPatchVersion(MetadataAnnotation annotation) { |
| 460 if (annotation.beginToken != null) { | 502 if (annotation.beginToken != null) { |
| 461 if (annotation.beginToken.next.value == 'patch') { | 503 if (annotation.beginToken.next.value == 'patch') { |
| 462 return const PatchVersion(null); | 504 return const PatchVersion(null); |
| 463 } else if (annotation.beginToken.next.value == 'patch_full') { | 505 } else if (annotation.beginToken.next.value == 'patch_full') { |
| 464 return const PatchVersion('full'); | 506 return const PatchVersion('full'); |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 631 | 673 |
| 632 class PatchVersion { | 674 class PatchVersion { |
| 633 final String tag; | 675 final String tag; |
| 634 | 676 |
| 635 const PatchVersion(this.tag); | 677 const PatchVersion(this.tag); |
| 636 | 678 |
| 637 bool isActive(String patchTag) => tag == null || tag == patchTag; | 679 bool isActive(String patchTag) => tag == null || tag == patchTag; |
| 638 | 680 |
| 639 String toString() => 'PatchVersion($tag)'; | 681 String toString() => 'PatchVersion($tag)'; |
| 640 } | 682 } |
| OLD | NEW |