| 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 '../common/names.dart'; | 8 import '../common/names.dart'; |
| 9 import '../constants/constructors.dart'; | 9 import '../constants/constructors.dart'; |
| 10 import '../constants/expressions.dart'; | 10 import '../constants/expressions.dart'; |
| 11 import '../dart_types.dart'; | 11 import '../dart_types.dart'; |
| 12 import '../diagnostics/messages.dart'; | 12 import '../diagnostics/messages.dart'; |
| 13 import '../elements/elements.dart'; | 13 import '../elements/elements.dart'; |
| 14 import '../elements/modelx.dart' | 14 import '../elements/modelx.dart' |
| 15 show DeferredLoaderGetterElementX, ErroneousElementX; | 15 show |
| 16 DeferredLoaderGetterElementX, |
| 17 ErroneousElementX, |
| 18 WarnOnUseElementX, |
| 19 WrappedMessage; |
| 16 import 'constant_serialization.dart'; | 20 import 'constant_serialization.dart'; |
| 17 import 'keys.dart'; | 21 import 'keys.dart'; |
| 18 import 'modelz.dart'; | 22 import 'modelz.dart'; |
| 19 import 'serialization.dart'; | 23 import 'serialization.dart'; |
| 20 import 'serialization_util.dart'; | 24 import 'serialization_util.dart'; |
| 21 | 25 |
| 22 /// Enum kinds used for encoding [Element]s. | 26 /// Enum kinds used for encoding [Element]s. |
| 23 enum SerializedElementKind { | 27 enum SerializedElementKind { |
| 24 ERROR, | 28 ERROR, |
| 25 LIBRARY, | 29 LIBRARY, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 48 LOCAL_FUNCTION, | 52 LOCAL_FUNCTION, |
| 49 TYPEDEF, | 53 TYPEDEF, |
| 50 TYPEVARIABLE, | 54 TYPEVARIABLE, |
| 51 PARAMETER, | 55 PARAMETER, |
| 52 INITIALIZING_FORMAL, | 56 INITIALIZING_FORMAL, |
| 53 IMPORT, | 57 IMPORT, |
| 54 EXPORT, | 58 EXPORT, |
| 55 PREFIX, | 59 PREFIX, |
| 56 DEFERRED_LOAD_LIBRARY, | 60 DEFERRED_LOAD_LIBRARY, |
| 57 LOCAL_VARIABLE, | 61 LOCAL_VARIABLE, |
| 62 WARN_ON_USE, |
| 58 EXTERNAL_LIBRARY, | 63 EXTERNAL_LIBRARY, |
| 59 EXTERNAL_LIBRARY_MEMBER, | 64 EXTERNAL_LIBRARY_MEMBER, |
| 60 EXTERNAL_CLASS_MEMBER, | 65 EXTERNAL_CLASS_MEMBER, |
| 61 EXTERNAL_CONSTRUCTOR, | 66 EXTERNAL_CONSTRUCTOR, |
| 62 } | 67 } |
| 63 | 68 |
| 64 /// Set of serializers used to serialize different kinds of elements by | 69 /// Set of serializers used to serialize different kinds of elements by |
| 65 /// encoding into them into [ObjectEncoder]s. | 70 /// encoding into them into [ObjectEncoder]s. |
| 66 /// | 71 /// |
| 67 /// This class is called from the [Serializer] when an [Element] needs | 72 /// This class is called from the [Serializer] when an [Element] needs |
| 68 /// serialization. The [ObjectEncoder] ensures that any [Element], [DartType], | 73 /// serialization. The [ObjectEncoder] ensures that any [Element], [DartType], |
| 69 /// and [ConstantExpression] that the serialized [Element] depends upon are also | 74 /// and [ConstantExpression] that the serialized [Element] depends upon are also |
| 70 /// serialized. | 75 /// serialized. |
| 71 const List<ElementSerializer> ELEMENT_SERIALIZERS = const [ | 76 const List<ElementSerializer> ELEMENT_SERIALIZERS = const [ |
| 72 const ErrorSerializer(), | 77 const ErrorSerializer(), |
| 73 const LibrarySerializer(), | 78 const LibrarySerializer(), |
| 74 const CompilationUnitSerializer(), | 79 const CompilationUnitSerializer(), |
| 75 const PrefixSerializer(), | 80 const PrefixSerializer(), |
| 76 const DeferredLoadLibrarySerializer(), | 81 const DeferredLoadLibrarySerializer(), |
| 77 const ClassSerializer(), | 82 const ClassSerializer(), |
| 78 const ConstructorSerializer(), | 83 const ConstructorSerializer(), |
| 79 const FieldSerializer(), | 84 const FieldSerializer(), |
| 80 const FunctionSerializer(), | 85 const FunctionSerializer(), |
| 81 const TypedefSerializer(), | 86 const TypedefSerializer(), |
| 82 const TypeVariableSerializer(), | 87 const TypeVariableSerializer(), |
| 83 const ParameterSerializer(), | 88 const ParameterSerializer(), |
| 84 const ImportSerializer(), | 89 const ImportSerializer(), |
| 85 const ExportSerializer(), | 90 const ExportSerializer(), |
| 86 const LocalVariableSerializer(), | 91 const LocalVariableSerializer(), |
| 92 const WarnOnUseSerializer(), |
| 87 ]; | 93 ]; |
| 88 | 94 |
| 89 /// Interface for a function that can serialize a set of element kinds. | 95 /// Interface for a function that can serialize a set of element kinds. |
| 90 abstract class ElementSerializer { | 96 abstract class ElementSerializer { |
| 91 /// Returns the [SerializedElementKind] for [element] if this serializer | 97 /// Returns the [SerializedElementKind] for [element] if this serializer |
| 92 /// supports serialization of [element] or `null` otherwise. | 98 /// supports serialization of [element] or `null` otherwise. |
| 93 SerializedElementKind getSerializedKind(Element element); | 99 SerializedElementKind getSerializedKind(Element element); |
| 94 | 100 |
| 95 /// Serializes [element] into the [encoder] using the [kind] computed | 101 /// Serializes [element] into the [encoder] using the [kind] computed |
| 96 /// by [getSerializedKind]. | 102 /// by [getSerializedKind]. |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 return SerializedElementKind.ERROR; | 215 return SerializedElementKind.ERROR; |
| 210 } | 216 } |
| 211 return null; | 217 return null; |
| 212 } | 218 } |
| 213 | 219 |
| 214 void serialize(ErroneousElement element, ObjectEncoder encoder, | 220 void serialize(ErroneousElement element, ObjectEncoder encoder, |
| 215 SerializedElementKind kind) { | 221 SerializedElementKind kind) { |
| 216 encoder.setElement(Key.ENCLOSING, element.enclosingElement); | 222 encoder.setElement(Key.ENCLOSING, element.enclosingElement); |
| 217 encoder.setString(Key.NAME, element.name); | 223 encoder.setString(Key.NAME, element.name); |
| 218 encoder.setEnum(Key.MESSAGE_KIND, element.messageKind); | 224 encoder.setEnum(Key.MESSAGE_KIND, element.messageKind); |
| 219 if (element.messageArguments.isNotEmpty) { | 225 serializeMessageArguments(encoder, Key.ARGUMENTS, element.messageArguments); |
| 220 MapEncoder mapEncoder = encoder.createMap(Key.ARGUMENTS); | |
| 221 element.messageArguments.forEach((String key, var value) { | |
| 222 mapEncoder.setString(key, Message.convertToString(value)); | |
| 223 }); | |
| 224 } | |
| 225 } | 226 } |
| 226 } | 227 } |
| 227 | 228 |
| 228 class LibrarySerializer implements ElementSerializer { | 229 class LibrarySerializer implements ElementSerializer { |
| 229 const LibrarySerializer(); | 230 const LibrarySerializer(); |
| 230 | 231 |
| 231 SerializedElementKind getSerializedKind(Element element) { | 232 SerializedElementKind getSerializedKind(Element element) { |
| 232 if (element.isLibrary) { | 233 if (element.isLibrary) { |
| 233 return SerializedElementKind.LIBRARY; | 234 return SerializedElementKind.LIBRARY; |
| 234 } | 235 } |
| (...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 742 } | 743 } |
| 743 return null; | 744 return null; |
| 744 } | 745 } |
| 745 | 746 |
| 746 void serialize(GetterElement element, ObjectEncoder encoder, | 747 void serialize(GetterElement element, ObjectEncoder encoder, |
| 747 SerializedElementKind kind) { | 748 SerializedElementKind kind) { |
| 748 encoder.setElement(Key.PREFIX, element.enclosingElement); | 749 encoder.setElement(Key.PREFIX, element.enclosingElement); |
| 749 } | 750 } |
| 750 } | 751 } |
| 751 | 752 |
| 753 class WarnOnUseSerializer implements ElementSerializer { |
| 754 const WarnOnUseSerializer(); |
| 755 |
| 756 SerializedElementKind getSerializedKind(Element element) { |
| 757 if (element.isWarnOnUse) { |
| 758 return SerializedElementKind.WARN_ON_USE; |
| 759 } |
| 760 return null; |
| 761 } |
| 762 |
| 763 void serialize(WarnOnUseElementX element, ObjectEncoder encoder, |
| 764 SerializedElementKind kind) { |
| 765 encoder.setElement(Key.ENCLOSING, element.enclosingElement); |
| 766 encoder.setElement(Key.ELEMENT, element.wrappedElement); |
| 767 serializeWrappedMessage(encoder, Key.WARNING, element.warning); |
| 768 serializeWrappedMessage(encoder, Key.INFO, element.info); |
| 769 } |
| 770 } |
| 771 |
| 752 /// Utility class for deserializing [Element]s. | 772 /// Utility class for deserializing [Element]s. |
| 753 /// | 773 /// |
| 754 /// This is used by the [Deserializer]. | 774 /// This is used by the [Deserializer]. |
| 755 class ElementDeserializer { | 775 class ElementDeserializer { |
| 756 /// Deserializes an [Element] from an [ObjectDecoder]. | 776 /// Deserializes an [Element] from an [ObjectDecoder]. |
| 757 /// | 777 /// |
| 758 /// The class is called from the [Deserializer] when an [Element] | 778 /// The class is called from the [Deserializer] when an [Element] |
| 759 /// needs deserialization. The [ObjectDecoder] ensures that any [Element], | 779 /// needs deserialization. The [ObjectDecoder] ensures that any [Element], |
| 760 /// [DartType], and [ConstantExpression] that the deserialized [Element] | 780 /// [DartType], and [ConstantExpression] that the deserialized [Element] |
| 761 /// depends upon are available. | 781 /// depends upon are available. |
| 762 static Element deserialize( | 782 static Element deserialize( |
| 763 ObjectDecoder decoder, SerializedElementKind elementKind) { | 783 ObjectDecoder decoder, SerializedElementKind elementKind) { |
| 764 switch (elementKind) { | 784 switch (elementKind) { |
| 765 case SerializedElementKind.ERROR: | 785 case SerializedElementKind.ERROR: |
| 766 Element enclosing = decoder.getElement(Key.ENCLOSING); | 786 Element enclosing = decoder.getElement(Key.ENCLOSING); |
| 767 String name = decoder.getString(Key.NAME); | 787 String name = decoder.getString(Key.NAME); |
| 768 MessageKind messageKind = | 788 MessageKind messageKind = |
| 769 decoder.getEnum(Key.MESSAGE_KIND, MessageKind.values); | 789 decoder.getEnum(Key.MESSAGE_KIND, MessageKind.values); |
| 770 Map<String, String> arguments = <String, String>{}; | 790 Map<String, String> arguments = |
| 771 MapDecoder mapDecoder = decoder.getMap(Key.ARGUMENTS, isOptional: true); | 791 deserializeMessageArguments(decoder, Key.ARGUMENTS); |
| 772 if (mapDecoder != null) { | |
| 773 mapDecoder.forEachKey((String key) { | |
| 774 arguments[key] = mapDecoder.getString(key); | |
| 775 }); | |
| 776 } | |
| 777 return new ErroneousElementX(messageKind, arguments, name, enclosing); | 792 return new ErroneousElementX(messageKind, arguments, name, enclosing); |
| 778 case SerializedElementKind.LIBRARY: | 793 case SerializedElementKind.LIBRARY: |
| 779 return new LibraryElementZ(decoder); | 794 return new LibraryElementZ(decoder); |
| 780 case SerializedElementKind.COMPILATION_UNIT: | 795 case SerializedElementKind.COMPILATION_UNIT: |
| 781 return new CompilationUnitElementZ(decoder); | 796 return new CompilationUnitElementZ(decoder); |
| 782 case SerializedElementKind.CLASS: | 797 case SerializedElementKind.CLASS: |
| 783 return new ClassElementZ(decoder); | 798 return new ClassElementZ(decoder); |
| 784 case SerializedElementKind.ENUM: | 799 case SerializedElementKind.ENUM: |
| 785 return new EnumClassElementZ(decoder); | 800 return new EnumClassElementZ(decoder); |
| 786 case SerializedElementKind.NAMED_MIXIN_APPLICATION: | 801 case SerializedElementKind.NAMED_MIXIN_APPLICATION: |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 837 case SerializedElementKind.IMPORT: | 852 case SerializedElementKind.IMPORT: |
| 838 return new ImportElementZ(decoder); | 853 return new ImportElementZ(decoder); |
| 839 case SerializedElementKind.EXPORT: | 854 case SerializedElementKind.EXPORT: |
| 840 return new ExportElementZ(decoder); | 855 return new ExportElementZ(decoder); |
| 841 case SerializedElementKind.PREFIX: | 856 case SerializedElementKind.PREFIX: |
| 842 return new PrefixElementZ(decoder); | 857 return new PrefixElementZ(decoder); |
| 843 case SerializedElementKind.DEFERRED_LOAD_LIBRARY: | 858 case SerializedElementKind.DEFERRED_LOAD_LIBRARY: |
| 844 return new DeferredLoaderGetterElementX(decoder.getElement(Key.PREFIX)); | 859 return new DeferredLoaderGetterElementX(decoder.getElement(Key.PREFIX)); |
| 845 case SerializedElementKind.LOCAL_VARIABLE: | 860 case SerializedElementKind.LOCAL_VARIABLE: |
| 846 return new LocalVariableElementZ(decoder); | 861 return new LocalVariableElementZ(decoder); |
| 862 case SerializedElementKind.WARN_ON_USE: |
| 863 Element enclosing = decoder.getElement(Key.ENCLOSING); |
| 864 Element element = decoder.getElement(Key.ELEMENT); |
| 865 WrappedMessage warning = |
| 866 deserializeWrappedMessage(decoder, Key.WARNING); |
| 867 WrappedMessage info = deserializeWrappedMessage(decoder, Key.INFO); |
| 868 return new WarnOnUseElementX(warning, info, enclosing, element); |
| 847 case SerializedElementKind.EXTERNAL_LIBRARY: | 869 case SerializedElementKind.EXTERNAL_LIBRARY: |
| 848 case SerializedElementKind.EXTERNAL_LIBRARY_MEMBER: | 870 case SerializedElementKind.EXTERNAL_LIBRARY_MEMBER: |
| 849 case SerializedElementKind.EXTERNAL_CLASS_MEMBER: | 871 case SerializedElementKind.EXTERNAL_CLASS_MEMBER: |
| 850 case SerializedElementKind.EXTERNAL_CONSTRUCTOR: | 872 case SerializedElementKind.EXTERNAL_CONSTRUCTOR: |
| 851 break; | 873 break; |
| 852 } | 874 } |
| 853 throw new UnsupportedError("Unexpected element kind '${elementKind}."); | 875 throw new UnsupportedError("Unexpected element kind '${elementKind}."); |
| 854 } | 876 } |
| 855 } | 877 } |
| OLD | NEW |