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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 * - Builders shift between declaration and implementation depending on usages. | 109 * - Builders shift between declaration and implementation depending on usages. |
110 * - Compile-time constants use constructor implementation exclusively. | 110 * - Compile-time constants use constructor implementation exclusively. |
111 * - Work on function parameters is performed on the declaration of the function | 111 * - Work on function parameters is performed on the declaration of the function |
112 * element. | 112 * element. |
113 */ | 113 */ |
114 | 114 |
115 library patchparser; | 115 library patchparser; |
116 | 116 |
117 import 'dart:async'; | 117 import 'dart:async'; |
118 | 118 |
119 import 'constants/values.dart' show Constant; | 119 import 'constants/values.dart' show ConstantValue; |
120 import 'dart2jslib.dart' | 120 import 'dart2jslib.dart' |
121 show Compiler, | 121 show Compiler, |
122 CompilerTask, | 122 CompilerTask, |
123 DiagnosticListener, | 123 DiagnosticListener, |
124 MessageKind, | 124 MessageKind, |
125 Script; | 125 Script; |
126 import 'elements/elements.dart'; | 126 import 'elements/elements.dart'; |
127 import 'elements/modelx.dart' | 127 import 'elements/modelx.dart' |
128 show LibraryElementX, | 128 show LibraryElementX, |
129 MetadataAnnotationX, | 129 MetadataAnnotationX, |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 /// Checks that [annotation] looks like a matching annotation and optionally | 310 /// Checks that [annotation] looks like a matching annotation and optionally |
311 /// applies actions on [element]. Returns `true` if the annotation matched. | 311 /// applies actions on [element]. Returns `true` if the annotation matched. |
312 bool apply(Compiler compiler, | 312 bool apply(Compiler compiler, |
313 Element element, | 313 Element element, |
314 MetadataAnnotation annotation); | 314 MetadataAnnotation annotation); |
315 | 315 |
316 /// Checks that the annotation value is valid. | 316 /// Checks that the annotation value is valid. |
317 void validate(Compiler compiler, | 317 void validate(Compiler compiler, |
318 Element element, | 318 Element element, |
319 MetadataAnnotation annotation, | 319 MetadataAnnotation annotation, |
320 Constant constant); | 320 ConstantValue constant); |
321 | 321 |
322 | 322 |
323 /// Checks [element] for metadata matching the [handler]. Return `true` if | 323 /// Checks [element] for metadata matching the [handler]. Return `true` if |
324 /// matching metadata was found. | 324 /// matching metadata was found. |
325 static bool checkAnnotation(Compiler compiler, | 325 static bool checkAnnotation(Compiler compiler, |
326 Element element, | 326 Element element, |
327 EagerAnnotationHandler handler) { | 327 EagerAnnotationHandler handler) { |
328 for (Link<MetadataAnnotation> link = element.metadata; | 328 for (Link<MetadataAnnotation> link = element.metadata; |
329 !link.isEmpty; | 329 !link.isEmpty; |
330 link = link.tail) { | 330 link = link.tail) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 declaration.setNative(native); | 371 declaration.setNative(native); |
372 return true; | 372 return true; |
373 } | 373 } |
374 } | 374 } |
375 return false; | 375 return false; |
376 } | 376 } |
377 | 377 |
378 void validate(Compiler compiler, | 378 void validate(Compiler compiler, |
379 Element element, | 379 Element element, |
380 MetadataAnnotation annotation, | 380 MetadataAnnotation annotation, |
381 Constant constant) { | 381 ConstantValue constant) { |
382 if (constant.computeType(compiler).element != | 382 if (constant.computeType(compiler).element != |
383 compiler.nativeAnnotationClass) { | 383 compiler.nativeAnnotationClass) { |
384 compiler.internalError(annotation, 'Invalid @Native(...) annotation.'); | 384 compiler.internalError(annotation, 'Invalid @Native(...) annotation.'); |
385 } | 385 } |
386 } | 386 } |
387 } | 387 } |
388 | 388 |
389 /// Annotation handler for pre-resolution detection of `@patch` annotations. | 389 /// Annotation handler for pre-resolution detection of `@patch` annotations. |
390 class PatchAnnotationHandler implements EagerAnnotationHandler { | 390 class PatchAnnotationHandler implements EagerAnnotationHandler { |
391 const PatchAnnotationHandler(); | 391 const PatchAnnotationHandler(); |
392 | 392 |
393 bool isPatchAnnotation(MetadataAnnotation annotation) { | 393 bool isPatchAnnotation(MetadataAnnotation annotation) { |
394 return annotation.beginToken != null && | 394 return annotation.beginToken != null && |
395 annotation.beginToken.next.value == 'patch'; | 395 annotation.beginToken.next.value == 'patch'; |
396 } | 396 } |
397 | 397 |
398 bool apply(Compiler compiler, | 398 bool apply(Compiler compiler, |
399 Element element, | 399 Element element, |
400 MetadataAnnotation annotation) { | 400 MetadataAnnotation annotation) { |
401 return isPatchAnnotation(annotation); | 401 return isPatchAnnotation(annotation); |
402 } | 402 } |
403 | 403 |
404 void validate(Compiler compiler, | 404 void validate(Compiler compiler, |
405 Element element, | 405 Element element, |
406 MetadataAnnotation annotation, | 406 MetadataAnnotation annotation, |
407 Constant constant) { | 407 ConstantValue constant) { |
408 if (constant != compiler.patchConstant) { | 408 if (constant != compiler.patchConstant) { |
409 compiler.internalError(annotation, 'Invalid patch annotation.'); | 409 compiler.internalError(annotation, 'Invalid patch annotation.'); |
410 } | 410 } |
411 } | 411 } |
412 } | 412 } |
413 | 413 |
414 | 414 |
415 void tryPatchGetter(DiagnosticListener listener, | 415 void tryPatchGetter(DiagnosticListener listener, |
416 Element origin, | 416 Element origin, |
417 FunctionElement patch) { | 417 FunctionElement patch) { |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 "Trying to patch a function more than once."); | 504 "Trying to patch a function more than once."); |
505 } | 505 } |
506 origin.applyPatch(patch); | 506 origin.applyPatch(patch); |
507 } | 507 } |
508 | 508 |
509 // TODO(johnniwinther): Add unittest when patch is (real) metadata. | 509 // TODO(johnniwinther): Add unittest when patch is (real) metadata. |
510 bool isPatchElement(Compiler compiler, Element element) { | 510 bool isPatchElement(Compiler compiler, Element element) { |
511 return EagerAnnotationHandler.checkAnnotation(compiler, element, | 511 return EagerAnnotationHandler.checkAnnotation(compiler, element, |
512 const PatchAnnotationHandler()); | 512 const PatchAnnotationHandler()); |
513 } | 513 } |
OLD | NEW |