Chromium Code Reviews| 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 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 297 } | 297 } |
| 298 | 298 |
| 299 void patchElement(Compiler compiler, | 299 void patchElement(Compiler compiler, |
| 300 Element origin, | 300 Element origin, |
| 301 Element patch) { | 301 Element patch) { |
| 302 if (origin == null) { | 302 if (origin == null) { |
| 303 compiler.reportError( | 303 compiler.reportError( |
| 304 patch, MessageKind.PATCH_NON_EXISTING, {'name': patch.name}); | 304 patch, MessageKind.PATCH_NON_EXISTING, {'name': patch.name}); |
| 305 return; | 305 return; |
| 306 } | 306 } |
| 307 | |
| 307 if (!(origin.isClass || | 308 if (!(origin.isClass || |
| 308 origin.isConstructor || | 309 origin.isConstructor || |
| 309 origin.isFunction || | 310 origin.isFunction || |
| 310 origin.isAbstractField)) { | 311 origin.isAbstractField)) { |
| 311 // TODO(ahe): Remove this error when the parser rejects all bad modifiers. | 312 // TODO(ahe): Remove this error when the parser rejects all bad modifiers. |
| 312 compiler.reportError(origin, MessageKind.PATCH_NONPATCHABLE); | 313 compiler.reportError(origin, MessageKind.PATCH_NONPATCHABLE); |
| 313 return; | 314 return; |
| 314 } | 315 } |
| 315 if (patch.isClass) { | 316 if (patch.isClass) { |
| 316 tryPatchClass(compiler, origin, patch); | 317 tryPatchClass(compiler, origin, patch); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 352 checkNativeAnnotation(compiler, patch); | 353 checkNativeAnnotation(compiler, patch); |
| 353 } | 354 } |
| 354 | 355 |
| 355 /// Check whether [cls] has a `@Native(...)` annotation, and if so, set its | 356 /// Check whether [cls] has a `@Native(...)` annotation, and if so, set its |
| 356 /// native name from the annotation. | 357 /// native name from the annotation. |
| 357 checkNativeAnnotation(Compiler compiler, ClassElement cls) { | 358 checkNativeAnnotation(Compiler compiler, ClassElement cls) { |
| 358 EagerAnnotationHandler.checkAnnotation(compiler, cls, | 359 EagerAnnotationHandler.checkAnnotation(compiler, cls, |
| 359 const NativeAnnotationHandler()); | 360 const NativeAnnotationHandler()); |
| 360 } | 361 } |
| 361 | 362 |
| 363 checkJsInteropAnnotation(Compiler compiler, element) { | |
| 364 EagerAnnotationHandler.checkAnnotation(compiler, element, | |
| 365 const JsInteropAnnotationHandler()); | |
| 366 } | |
| 367 | |
| 368 | |
| 362 /// Abstract interface for pre-resolution detection of metadata. | 369 /// Abstract interface for pre-resolution detection of metadata. |
| 363 /// | 370 /// |
| 364 /// The detection is handled in two steps: | 371 /// The detection is handled in two steps: |
| 365 /// - match the annotation syntactically and assume that the annotation is valid | 372 /// - match the annotation syntactically and assume that the annotation is valid |
| 366 /// if it looks correct, | 373 /// if it looks correct, |
| 367 /// - setup a deferred action to check that the annotation has a valid constant | 374 /// - setup a deferred action to check that the annotation has a valid constant |
| 368 /// value and report an internal error if not. | 375 /// value and report an internal error if not. |
| 369 abstract class EagerAnnotationHandler<T> { | 376 abstract class EagerAnnotationHandler<T> { |
| 370 /// Checks that [annotation] looks like a matching annotation and optionally | 377 /// Checks that [annotation] looks like a matching annotation and optionally |
| 371 /// applies actions on [element]. Returns a non-null annotation marker if the | 378 /// applies actions on [element]. Returns a non-null annotation marker if the |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 387 Element element, | 394 Element element, |
| 388 EagerAnnotationHandler handler) { | 395 EagerAnnotationHandler handler) { |
| 389 for (Link<MetadataAnnotation> link = element.metadata; | 396 for (Link<MetadataAnnotation> link = element.metadata; |
| 390 !link.isEmpty; | 397 !link.isEmpty; |
| 391 link = link.tail) { | 398 link = link.tail) { |
| 392 MetadataAnnotation annotation = link.head; | 399 MetadataAnnotation annotation = link.head; |
| 393 var result = handler.apply(compiler, element, annotation); | 400 var result = handler.apply(compiler, element, annotation); |
| 394 if (result != null) { | 401 if (result != null) { |
| 395 // TODO(johnniwinther): Perform this check in | 402 // TODO(johnniwinther): Perform this check in |
| 396 // [Compiler.onLibrariesLoaded]. | 403 // [Compiler.onLibrariesLoaded]. |
| 397 compiler.enqueuer.resolution.addDeferredAction(element, () { | 404 actionCallback() { |
| 398 annotation.ensureResolved(compiler); | 405 annotation.ensureResolved(compiler); |
| 399 handler.validate( | 406 handler.validate( |
| 400 compiler, element, annotation, | 407 compiler, element, annotation, |
| 401 compiler.constants.getConstantValue(annotation.constant)); | 408 compiler.constants.getConstantValue(annotation.constant)); |
| 402 }); | 409 } |
| 410 if (compiler.enqueuer.resolution.queueIsClosed) { | |
|
Siggi Cherem (dart-lang)
2015/09/18 20:34:10
this is probably breaking the current enqueuer inv
Jacob
2015/10/01 00:47:33
this is somewhat horrible but somewhat acceptable
| |
| 411 actionCallback(); | |
| 412 } else { | |
| 413 compiler.enqueuer.resolution.addDeferredAction(element, actionCallback ); | |
|
Siggi Cherem (dart-lang)
2015/09/18 20:34:10
nit: long line
Jacob
2015/10/01 00:47:33
Done.
| |
| 414 } | |
| 403 return result; | 415 return result; |
| 404 } | 416 } |
| 405 } | 417 } |
| 406 return null; | 418 return null; |
| 407 } | 419 } |
| 408 } | 420 } |
| 409 | 421 |
| 410 /// Annotation handler for pre-resolution detection of `@Native(...)` | 422 /// Annotation handler for pre-resolution detection of `@Native(...)` |
| 411 /// annotations. | 423 /// annotations. |
| 412 class NativeAnnotationHandler implements EagerAnnotationHandler<String> { | 424 class NativeAnnotationHandler implements EagerAnnotationHandler<String> { |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 442 Element element, | 454 Element element, |
| 443 MetadataAnnotation annotation, | 455 MetadataAnnotation annotation, |
| 444 ConstantValue constant) { | 456 ConstantValue constant) { |
| 445 if (constant.getType(compiler.coreTypes).element != | 457 if (constant.getType(compiler.coreTypes).element != |
| 446 compiler.nativeAnnotationClass) { | 458 compiler.nativeAnnotationClass) { |
| 447 compiler.internalError(annotation, 'Invalid @Native(...) annotation.'); | 459 compiler.internalError(annotation, 'Invalid @Native(...) annotation.'); |
| 448 } | 460 } |
| 449 } | 461 } |
| 450 } | 462 } |
| 451 | 463 |
| 464 /// Annotation handler for pre-resolution detection of `@JsName(...)` | |
| 465 /// annotations. | |
| 466 class JsInteropAnnotationHandler implements EagerAnnotationHandler<String> { | |
| 467 const JsInteropAnnotationHandler(); | |
| 468 | |
| 469 // TODO(jacobr): this method is pure technical debt. | |
| 470 String dietResolveStringToken(StringToken token) { | |
|
Siggi Cherem (dart-lang)
2015/09/18 20:34:10
could we switch to use the constant evaluation ins
Jacob
2015/10/01 00:47:33
The trouble is getJsNameAnnotation is currently ca
| |
| 471 String value = token.value; | |
| 472 if ((value.startsWith('"') && value.endsWith('"')) || | |
| 473 value.startsWith("'") && value.endsWith("'")) { | |
| 474 return value.substring(1, value.length - 1); | |
| 475 } else { | |
| 476 throw 'Invalid @JsName(...) annotation. Unexpected name: $value'; | |
| 477 } | |
| 478 } | |
| 479 | |
| 480 String getJsNameAnnotation(MetadataAnnotation annotation) { | |
| 481 if (annotation.beginToken != null && | |
| 482 annotation.beginToken.next.value == 'JsName') { | |
| 483 // Skipping '@', 'JsName', and '('. | |
| 484 Token argument = annotation.beginToken.next.next.next; | |
| 485 return (argument is StringToken) ? dietResolveStringToken(argument) : ''; | |
| 486 } | |
| 487 | |
| 488 return null; | |
| 489 } | |
| 490 | |
| 491 String apply(Compiler compiler, | |
| 492 Element element, | |
| 493 MetadataAnnotation annotation) { | |
| 494 String jsName = getJsNameAnnotation(annotation); | |
| 495 if (jsName != null) { | |
| 496 element.setJsInterop(jsName); | |
| 497 element.declaration.setJsInterop(jsName); | |
| 498 } | |
| 499 return jsName; | |
| 500 } | |
| 501 | |
| 502 @override | |
| 503 void validate(Compiler compiler, | |
| 504 Element element, | |
| 505 MetadataAnnotation annotation, | |
| 506 ConstantValue constant) { | |
| 507 } | |
| 508 } | |
| 509 | |
| 452 /// Annotation handler for pre-resolution detection of `@patch` annotations. | 510 /// Annotation handler for pre-resolution detection of `@patch` annotations. |
| 453 class PatchAnnotationHandler implements EagerAnnotationHandler<PatchVersion> { | 511 class PatchAnnotationHandler implements EagerAnnotationHandler<PatchVersion> { |
| 454 const PatchAnnotationHandler(); | 512 const PatchAnnotationHandler(); |
| 455 | 513 |
| 456 PatchVersion getPatchVersion(MetadataAnnotation annotation) { | 514 PatchVersion getPatchVersion(MetadataAnnotation annotation) { |
| 457 if (annotation.beginToken != null) { | 515 if (annotation.beginToken != null) { |
| 458 if (annotation.beginToken.next.value == 'patch') { | 516 if (annotation.beginToken.next.value == 'patch') { |
| 459 return const PatchVersion(null); | 517 return const PatchVersion(null); |
| 460 } else if (annotation.beginToken.next.value == 'patch_full') { | 518 } else if (annotation.beginToken.next.value == 'patch_full') { |
| 461 return const PatchVersion('full'); | 519 return const PatchVersion('full'); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 591 | 649 |
| 592 class PatchVersion { | 650 class PatchVersion { |
| 593 final String tag; | 651 final String tag; |
| 594 | 652 |
| 595 const PatchVersion(this.tag); | 653 const PatchVersion(this.tag); |
| 596 | 654 |
| 597 bool isActive(String patchTag) => tag == null || tag == patchTag; | 655 bool isActive(String patchTag) => tag == null || tag == patchTag; |
| 598 | 656 |
| 599 String toString() => 'PatchVersion($tag)'; | 657 String toString() => 'PatchVersion($tag)'; |
| 600 } | 658 } |
| OLD | NEW |