| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 dart2js.serialization.elements; | 5 library dart2js.serialization.elements; |
| 6 | 6 |
| 7 import '../common.dart'; | 7 import '../common.dart'; |
| 8 import '../constants/constructors.dart'; | 8 import '../constants/constructors.dart'; |
| 9 import '../constants/expressions.dart'; | 9 import '../constants/expressions.dart'; |
| 10 import '../dart_types.dart'; | 10 import '../dart_types.dart'; |
| 11 import '../elements/elements.dart'; | 11 import '../elements/elements.dart'; |
| 12 import 'constant_serialization.dart'; | 12 import 'constant_serialization.dart'; |
| 13 import 'keys.dart'; | 13 import 'keys.dart'; |
| 14 import 'modelz.dart'; | 14 import 'modelz.dart'; |
| 15 import 'serialization.dart'; | 15 import 'serialization.dart'; |
| 16 | 16 |
| 17 /// Enum kinds used for encoding [Element]s. | 17 /// Enum kinds used for encoding [Element]s. |
| 18 enum SerializedElementKind { | 18 enum SerializedElementKind { |
| 19 LIBRARY, | 19 LIBRARY, |
| 20 COMPILATION_UNIT, | 20 COMPILATION_UNIT, |
| 21 CLASS, | 21 CLASS, |
| 22 ENUM, | 22 ENUM, |
| 23 NAMED_MIXIN_APPLICATION, | 23 NAMED_MIXIN_APPLICATION, |
| 24 GENERATIVE_CONSTRUCTOR, | 24 GENERATIVE_CONSTRUCTOR, |
| 25 FACTORY_CONSTRUCTOR, | 25 FACTORY_CONSTRUCTOR, |
| 26 TOPLEVEL_FIELD, | 26 TOPLEVEL_FIELD, |
| 27 STATIC_FIELD, | 27 STATIC_FIELD, |
| 28 INSTANCE_FIELD, | 28 INSTANCE_FIELD, |
| 29 ENUM_CONSTANT, |
| 29 TOPLEVEL_FUNCTION, | 30 TOPLEVEL_FUNCTION, |
| 30 TOPLEVEL_GETTER, | 31 TOPLEVEL_GETTER, |
| 31 TOPLEVEL_SETTER, | 32 TOPLEVEL_SETTER, |
| 32 STATIC_FUNCTION, | 33 STATIC_FUNCTION, |
| 33 STATIC_GETTER, | 34 STATIC_GETTER, |
| 34 STATIC_SETTER, | 35 STATIC_SETTER, |
| 35 INSTANCE_FUNCTION, | 36 INSTANCE_FUNCTION, |
| 36 INSTANCE_GETTER, | 37 INSTANCE_GETTER, |
| 37 INSTANCE_SETTER, | 38 INSTANCE_SETTER, |
| 38 LOCAL_FUNCTION, | 39 LOCAL_FUNCTION, |
| 39 TYPEDEF, | 40 TYPEDEF, |
| 40 TYPEVARIABLE, | 41 TYPEVARIABLE, |
| 41 PARAMETER, | 42 PARAMETER, |
| 42 INITIALIZING_FORMAL, | 43 INITIALIZING_FORMAL, |
| 43 IMPORT, | 44 IMPORT, |
| 44 EXPORT, | 45 EXPORT, |
| 45 PREFIX, | 46 PREFIX, |
| 47 LOCAL_VARIABLE, |
| 46 EXTERNAL_LIBRARY, | 48 EXTERNAL_LIBRARY, |
| 47 EXTERNAL_LIBRARY_MEMBER, | 49 EXTERNAL_LIBRARY_MEMBER, |
| 48 EXTERNAL_STATIC_MEMBER, | 50 EXTERNAL_STATIC_MEMBER, |
| 49 EXTERNAL_CONSTRUCTOR, | 51 EXTERNAL_CONSTRUCTOR, |
| 50 } | 52 } |
| 51 | 53 |
| 52 /// Set of serializers used to serialize different kinds of elements by | 54 /// Set of serializers used to serialize different kinds of elements by |
| 53 /// encoding into them into [ObjectEncoder]s. | 55 /// encoding into them into [ObjectEncoder]s. |
| 54 /// | 56 /// |
| 55 /// This class is called from the [Serializer] when an [Element] needs | 57 /// This class is called from the [Serializer] when an [Element] needs |
| 56 /// serialization. The [ObjectEncoder] ensures that any [Element], [DartType], | 58 /// serialization. The [ObjectEncoder] ensures that any [Element], [DartType], |
| 57 /// and [ConstantExpression] that the serialized [Element] depends upon are also | 59 /// and [ConstantExpression] that the serialized [Element] depends upon are also |
| 58 /// serialized. | 60 /// serialized. |
| 59 const List<ElementSerializer> ELEMENT_SERIALIZERS = const [ | 61 const List<ElementSerializer> ELEMENT_SERIALIZERS = const [ |
| 60 const LibrarySerializer(), | 62 const LibrarySerializer(), |
| 61 const CompilationUnitSerializer(), | 63 const CompilationUnitSerializer(), |
| 62 const ClassSerializer(), | 64 const ClassSerializer(), |
| 63 const ConstructorSerializer(), | 65 const ConstructorSerializer(), |
| 64 const FieldSerializer(), | 66 const FieldSerializer(), |
| 65 const FunctionSerializer(), | 67 const FunctionSerializer(), |
| 66 const TypedefSerializer(), | 68 const TypedefSerializer(), |
| 67 const TypeVariableSerializer(), | 69 const TypeVariableSerializer(), |
| 68 const ParameterSerializer(), | 70 const ParameterSerializer(), |
| 69 const ImportSerializer(), | 71 const ImportSerializer(), |
| 70 const ExportSerializer(), | 72 const ExportSerializer(), |
| 71 const PrefixSerializer(), | 73 const PrefixSerializer(), |
| 74 const LocalVariableSerializer(), |
| 72 ]; | 75 ]; |
| 73 | 76 |
| 74 /// Interface for a function that can serialize a set of element kinds. | 77 /// Interface for a function that can serialize a set of element kinds. |
| 75 abstract class ElementSerializer { | 78 abstract class ElementSerializer { |
| 76 /// Returns the [SerializedElementKind] for [element] if this serializer | 79 /// Returns the [SerializedElementKind] for [element] if this serializer |
| 77 /// supports serialization of [element] or `null` otherwise. | 80 /// supports serialization of [element] or `null` otherwise. |
| 78 SerializedElementKind getSerializedKind(Element element); | 81 SerializedElementKind getSerializedKind(Element element); |
| 79 | 82 |
| 80 /// Serializes [element] into the [encoder] using the [kind] computed | 83 /// Serializes [element] into the [encoder] using the [kind] computed |
| 81 /// by [getSerializedKind]. | 84 /// by [getSerializedKind]. |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 } | 358 } |
| 356 } | 359 } |
| 357 } | 360 } |
| 358 | 361 |
| 359 class FieldSerializer implements ElementSerializer { | 362 class FieldSerializer implements ElementSerializer { |
| 360 const FieldSerializer(); | 363 const FieldSerializer(); |
| 361 | 364 |
| 362 SerializedElementKind getSerializedKind(Element element) { | 365 SerializedElementKind getSerializedKind(Element element) { |
| 363 if (element.isField) { | 366 if (element.isField) { |
| 364 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_FIELD; | 367 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_FIELD; |
| 365 if (element.isStatic) return SerializedElementKind.STATIC_FIELD; | 368 if (element.isStatic) { |
| 369 if (element is EnumConstantElement) { |
| 370 return SerializedElementKind.ENUM_CONSTANT; |
| 371 } |
| 372 return SerializedElementKind.STATIC_FIELD; |
| 373 } |
| 366 if (element.isInstanceMember) return SerializedElementKind.INSTANCE_FIELD; | 374 if (element.isInstanceMember) return SerializedElementKind.INSTANCE_FIELD; |
| 367 } | 375 } |
| 368 return null; | 376 return null; |
| 369 } | 377 } |
| 370 | 378 |
| 371 void serialize(FieldElement element, | 379 void serialize(FieldElement element, |
| 372 ObjectEncoder encoder, | 380 ObjectEncoder encoder, |
| 373 SerializedElementKind kind) { | 381 SerializedElementKind kind) { |
| 374 encoder.setString(Key.NAME, element.name); | 382 encoder.setString(Key.NAME, element.name); |
| 375 SerializerUtil.serializePosition(element, encoder); | 383 SerializerUtil.serializePosition(element, encoder); |
| 376 encoder.setType(Key.TYPE, element.type); | 384 encoder.setType(Key.TYPE, element.type); |
| 377 encoder.setBool(Key.IS_FINAL, element.isFinal); | 385 encoder.setBool(Key.IS_FINAL, element.isFinal); |
| 378 encoder.setBool(Key.IS_CONST, element.isConst); | 386 encoder.setBool(Key.IS_CONST, element.isConst); |
| 379 if (element.isConst) { | 387 if (element.isConst) { |
| 380 ConstantExpression constant = element.constant; | 388 ConstantExpression constant = element.constant; |
| 381 encoder.setConstant(Key.CONSTANT, constant); | 389 encoder.setConstant(Key.CONSTANT, constant); |
| 382 } | 390 } |
| 383 if (kind != SerializedElementKind.TOPLEVEL_FIELD) { | 391 if (kind != SerializedElementKind.TOPLEVEL_FIELD) { |
| 384 encoder.setElement(Key.CLASS, element.enclosingClass); | 392 encoder.setElement(Key.CLASS, element.enclosingClass); |
| 385 } else { | 393 } else { |
| 386 encoder.setElement(Key.LIBRARY, element.library); | 394 encoder.setElement(Key.LIBRARY, element.library); |
| 387 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); | 395 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); |
| 388 } | 396 } |
| 397 if (element is EnumConstantElement) { |
| 398 EnumConstantElement enumConstant = element; |
| 399 encoder.setInt(Key.INDEX, enumConstant.index); |
| 400 } |
| 389 } | 401 } |
| 390 } | 402 } |
| 391 | 403 |
| 392 class FunctionSerializer implements ElementSerializer { | 404 class FunctionSerializer implements ElementSerializer { |
| 393 const FunctionSerializer(); | 405 const FunctionSerializer(); |
| 394 | 406 |
| 395 SerializedElementKind getSerializedKind(Element element) { | 407 SerializedElementKind getSerializedKind(Element element) { |
| 396 if (element.isFunction) { | 408 if (element.isFunction) { |
| 397 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_FUNCTION; | 409 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_FUNCTION; |
| 398 if (element.isStatic) return SerializedElementKind.STATIC_FUNCTION; | 410 if (element.isStatic) return SerializedElementKind.STATIC_FUNCTION; |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 if (element.isOptional) { | 525 if (element.isOptional) { |
| 514 encoder.setConstant(Key.CONSTANT, element.constant); | 526 encoder.setConstant(Key.CONSTANT, element.constant); |
| 515 } | 527 } |
| 516 if (element.isInitializingFormal) { | 528 if (element.isInitializingFormal) { |
| 517 InitializingFormalElement initializingFormal = element; | 529 InitializingFormalElement initializingFormal = element; |
| 518 encoder.setElement(Key.FIELD, initializingFormal.fieldElement); | 530 encoder.setElement(Key.FIELD, initializingFormal.fieldElement); |
| 519 } | 531 } |
| 520 } | 532 } |
| 521 } | 533 } |
| 522 | 534 |
| 535 |
| 536 class LocalVariableSerializer implements ElementSerializer { |
| 537 const LocalVariableSerializer(); |
| 538 |
| 539 SerializedElementKind getSerializedKind(Element element) { |
| 540 if (element.isVariable) { |
| 541 return SerializedElementKind.LOCAL_VARIABLE; |
| 542 } |
| 543 return null; |
| 544 } |
| 545 |
| 546 void serialize(LocalVariableElement element, |
| 547 ObjectEncoder encoder, |
| 548 SerializedElementKind kind) { |
| 549 encoder.setString(Key.NAME, element.name); |
| 550 SerializerUtil.serializePosition(element, encoder); |
| 551 encoder.setType(Key.TYPE, element.type); |
| 552 encoder.setBool(Key.IS_FINAL, element.isFinal); |
| 553 encoder.setBool(Key.IS_CONST, element.isConst); |
| 554 if (element.isConst) { |
| 555 ConstantExpression constant = element.constant; |
| 556 encoder.setConstant(Key.CONSTANT, constant); |
| 557 } |
| 558 encoder.setElement( |
| 559 Key.EXECUTABLE_CONTEXT, element.executableContext); |
| 560 } |
| 561 } |
| 562 |
| 563 |
| 523 class ImportSerializer implements ElementSerializer { | 564 class ImportSerializer implements ElementSerializer { |
| 524 const ImportSerializer(); | 565 const ImportSerializer(); |
| 525 | 566 |
| 526 SerializedElementKind getSerializedKind(Element element) { | 567 SerializedElementKind getSerializedKind(Element element) { |
| 527 if (element.isImport) { | 568 if (element.isImport) { |
| 528 return SerializedElementKind.IMPORT; | 569 return SerializedElementKind.IMPORT; |
| 529 } | 570 } |
| 530 return null; | 571 return null; |
| 531 } | 572 } |
| 532 | 573 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 case SerializedElementKind.CLASS: | 652 case SerializedElementKind.CLASS: |
| 612 return new ClassElementZ(decoder); | 653 return new ClassElementZ(decoder); |
| 613 case SerializedElementKind.ENUM: | 654 case SerializedElementKind.ENUM: |
| 614 return new EnumClassElementZ(decoder); | 655 return new EnumClassElementZ(decoder); |
| 615 case SerializedElementKind.NAMED_MIXIN_APPLICATION: | 656 case SerializedElementKind.NAMED_MIXIN_APPLICATION: |
| 616 return new NamedMixinApplicationElementZ(decoder); | 657 return new NamedMixinApplicationElementZ(decoder); |
| 617 case SerializedElementKind.TOPLEVEL_FIELD: | 658 case SerializedElementKind.TOPLEVEL_FIELD: |
| 618 return new TopLevelFieldElementZ(decoder); | 659 return new TopLevelFieldElementZ(decoder); |
| 619 case SerializedElementKind.STATIC_FIELD: | 660 case SerializedElementKind.STATIC_FIELD: |
| 620 return new StaticFieldElementZ(decoder); | 661 return new StaticFieldElementZ(decoder); |
| 662 case SerializedElementKind.ENUM_CONSTANT: |
| 663 return new EnumConstantElementZ(decoder); |
| 621 case SerializedElementKind.INSTANCE_FIELD: | 664 case SerializedElementKind.INSTANCE_FIELD: |
| 622 return new InstanceFieldElementZ(decoder); | 665 return new InstanceFieldElementZ(decoder); |
| 623 case SerializedElementKind.GENERATIVE_CONSTRUCTOR: | 666 case SerializedElementKind.GENERATIVE_CONSTRUCTOR: |
| 624 return new GenerativeConstructorElementZ(decoder); | 667 return new GenerativeConstructorElementZ(decoder); |
| 625 case SerializedElementKind.FACTORY_CONSTRUCTOR: | 668 case SerializedElementKind.FACTORY_CONSTRUCTOR: |
| 626 return new FactoryConstructorElementZ(decoder); | 669 return new FactoryConstructorElementZ(decoder); |
| 627 case SerializedElementKind.TOPLEVEL_FUNCTION: | 670 case SerializedElementKind.TOPLEVEL_FUNCTION: |
| 628 return new TopLevelFunctionElementZ(decoder); | 671 return new TopLevelFunctionElementZ(decoder); |
| 629 case SerializedElementKind.STATIC_FUNCTION: | 672 case SerializedElementKind.STATIC_FUNCTION: |
| 630 return new StaticFunctionElementZ(decoder); | 673 return new StaticFunctionElementZ(decoder); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 651 case SerializedElementKind.PARAMETER: | 694 case SerializedElementKind.PARAMETER: |
| 652 return new ParameterElementZ(decoder); | 695 return new ParameterElementZ(decoder); |
| 653 case SerializedElementKind.INITIALIZING_FORMAL: | 696 case SerializedElementKind.INITIALIZING_FORMAL: |
| 654 return new InitializingFormalElementZ(decoder); | 697 return new InitializingFormalElementZ(decoder); |
| 655 case SerializedElementKind.IMPORT: | 698 case SerializedElementKind.IMPORT: |
| 656 return new ImportElementZ(decoder); | 699 return new ImportElementZ(decoder); |
| 657 case SerializedElementKind.EXPORT: | 700 case SerializedElementKind.EXPORT: |
| 658 return new ExportElementZ(decoder); | 701 return new ExportElementZ(decoder); |
| 659 case SerializedElementKind.PREFIX: | 702 case SerializedElementKind.PREFIX: |
| 660 return new PrefixElementZ(decoder); | 703 return new PrefixElementZ(decoder); |
| 704 case SerializedElementKind.LOCAL_VARIABLE: |
| 705 return new LocalVariableElementZ(decoder); |
| 661 case SerializedElementKind.EXTERNAL_LIBRARY: | 706 case SerializedElementKind.EXTERNAL_LIBRARY: |
| 662 case SerializedElementKind.EXTERNAL_LIBRARY_MEMBER: | 707 case SerializedElementKind.EXTERNAL_LIBRARY_MEMBER: |
| 663 case SerializedElementKind.EXTERNAL_STATIC_MEMBER: | 708 case SerializedElementKind.EXTERNAL_STATIC_MEMBER: |
| 664 case SerializedElementKind.EXTERNAL_CONSTRUCTOR: | 709 case SerializedElementKind.EXTERNAL_CONSTRUCTOR: |
| 665 break; | 710 break; |
| 666 } | 711 } |
| 667 throw new UnsupportedError("Unexpected element kind '${elementKind}."); | 712 throw new UnsupportedError("Unexpected element kind '${elementKind}."); |
| 668 } | 713 } |
| 669 } | 714 } |
| OLD | NEW |