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.types; | 5 library dart2js.serialization.types; |
6 | 6 |
7 import '../dart_types.dart'; | 7 import '../dart_types.dart'; |
8 import 'serialization.dart'; | 8 import 'serialization.dart'; |
9 import 'keys.dart'; | 9 import 'keys.dart'; |
10 | 10 |
11 /// Visitor that serializes a [DartType] by encoding it into an [ObjectEncoder]. | 11 /// Visitor that serializes a [DartType] by encoding it into an [ObjectEncoder]. |
12 /// | 12 /// |
13 /// This class is called from the [Serializer] when a [DartType] needs | 13 /// This class is called from the [Serializer] when a [DartType] needs |
14 /// serialization. The [ObjectEncoder] ensures that any [Element], and other | 14 /// serialization. The [ObjectEncoder] ensures that any [Element], and other |
15 /// [DartType] that the serialized [DartType] depends upon are also serialized. | 15 /// [DartType] that the serialized [DartType] depends upon are also serialized. |
16 class TypeSerializer extends DartTypeVisitor<dynamic, ObjectEncoder> { | 16 class TypeSerializer extends DartTypeVisitor<dynamic, ObjectEncoder> { |
17 const TypeSerializer(); | 17 const TypeSerializer(); |
18 | 18 |
19 void visitType(DartType type, ObjectEncoder encoder) { | 19 void visitType(DartType type, ObjectEncoder encoder) { |
20 throw new UnsupportedError('Unsupported type: $type'); | 20 throw new UnsupportedError('Unsupported type: $type'); |
21 } | 21 } |
22 | 22 |
23 void visitVoidType(VoidType type, ObjectEncoder encoder) {} | 23 void visitVoidType(VoidType type, ObjectEncoder encoder) {} |
24 | 24 |
25 void visitTypeVariableType(TypeVariableType type, | 25 void visitTypeVariableType(TypeVariableType type, ObjectEncoder encoder) { |
26 ObjectEncoder encoder) { | |
27 encoder.setElement(Key.ELEMENT, type.element); | 26 encoder.setElement(Key.ELEMENT, type.element); |
28 } | 27 } |
29 | 28 |
30 void visitFunctionType(FunctionType type, | 29 void visitFunctionType(FunctionType type, ObjectEncoder encoder) { |
31 ObjectEncoder encoder) { | |
32 // TODO(johnniwinther): Support encoding of `type.element`. | 30 // TODO(johnniwinther): Support encoding of `type.element`. |
33 encoder.setType(Key.RETURN_TYPE, type.returnType); | 31 encoder.setType(Key.RETURN_TYPE, type.returnType); |
34 encoder.setTypes(Key.PARAMETER_TYPES, type.parameterTypes); | 32 encoder.setTypes(Key.PARAMETER_TYPES, type.parameterTypes); |
35 encoder.setTypes( | 33 encoder.setTypes(Key.OPTIONAL_PARAMETER_TYPES, type.optionalParameterTypes); |
36 Key.OPTIONAL_PARAMETER_TYPES, type.optionalParameterTypes); | |
37 encoder.setStrings(Key.NAMED_PARAMETERS, type.namedParameters); | 34 encoder.setStrings(Key.NAMED_PARAMETERS, type.namedParameters); |
38 encoder.setTypes(Key.NAMED_PARAMETER_TYPES, type.namedParameterTypes); | 35 encoder.setTypes(Key.NAMED_PARAMETER_TYPES, type.namedParameterTypes); |
39 } | 36 } |
40 | 37 |
41 void visitMalformedType(MalformedType type, ObjectEncoder encoder) { | 38 void visitMalformedType(MalformedType type, ObjectEncoder encoder) {} |
42 | |
43 } | |
44 | 39 |
45 void visitInterfaceType(InterfaceType type, ObjectEncoder encoder) { | 40 void visitInterfaceType(InterfaceType type, ObjectEncoder encoder) { |
46 encoder.setElement(Key.ELEMENT, type.element); | 41 encoder.setElement(Key.ELEMENT, type.element); |
47 encoder.setTypes(Key.TYPE_ARGUMENTS, type.typeArguments); | 42 encoder.setTypes(Key.TYPE_ARGUMENTS, type.typeArguments); |
48 } | 43 } |
49 | 44 |
50 void visitTypedefType(TypedefType type, ObjectEncoder encoder) { | 45 void visitTypedefType(TypedefType type, ObjectEncoder encoder) { |
51 encoder.setElement(Key.ELEMENT, type.element); | 46 encoder.setElement(Key.ELEMENT, type.element); |
52 encoder.setTypes(Key.TYPE_ARGUMENTS, type.typeArguments); | 47 encoder.setTypes(Key.TYPE_ARGUMENTS, type.typeArguments); |
53 } | 48 } |
54 | 49 |
55 void visitDynamicType(DynamicType type, ObjectEncoder encoder) {} | 50 void visitDynamicType(DynamicType type, ObjectEncoder encoder) {} |
56 } | 51 } |
57 | 52 |
58 | |
59 /// Utility class for deserializing [DartType]s. | 53 /// Utility class for deserializing [DartType]s. |
60 /// | 54 /// |
61 /// This is used by the [Deserializer]. | 55 /// This is used by the [Deserializer]. |
62 class TypeDeserializer { | 56 class TypeDeserializer { |
63 | |
64 /// Deserializes a [DartType] from an [ObjectDecoder]. | 57 /// Deserializes a [DartType] from an [ObjectDecoder]. |
65 /// | 58 /// |
66 /// The class is called from the [Deserializer] when a [DartType] needs | 59 /// The class is called from the [Deserializer] when a [DartType] needs |
67 /// deserialization. The [ObjectDecoder] ensures that any [Element], other | 60 /// deserialization. The [ObjectDecoder] ensures that any [Element], other |
68 /// [DartType] that the deserialized [DartType] depends upon are available. | 61 /// [DartType] that the deserialized [DartType] depends upon are available. |
69 static DartType deserialize(ObjectDecoder decoder) { | 62 static DartType deserialize(ObjectDecoder decoder) { |
70 TypeKind typeKind = decoder.getEnum(Key.KIND, TypeKind.values); | 63 TypeKind typeKind = decoder.getEnum(Key.KIND, TypeKind.values); |
71 switch (typeKind) { | 64 switch (typeKind) { |
72 case TypeKind.INTERFACE: | 65 case TypeKind.INTERFACE: |
73 return new InterfaceType( | 66 return new InterfaceType(decoder.getElement(Key.ELEMENT), |
74 decoder.getElement(Key.ELEMENT), | |
75 decoder.getTypes(Key.TYPE_ARGUMENTS, isOptional: true)); | 67 decoder.getTypes(Key.TYPE_ARGUMENTS, isOptional: true)); |
76 case TypeKind.FUNCTION: | 68 case TypeKind.FUNCTION: |
77 // TODO(johnniwinther): Support decoding of `type.element`. | 69 // TODO(johnniwinther): Support decoding of `type.element`. |
78 return new FunctionType.synthesized( | 70 return new FunctionType.synthesized( |
79 decoder.getType(Key.RETURN_TYPE), | 71 decoder.getType(Key.RETURN_TYPE), |
80 decoder.getTypes(Key.PARAMETER_TYPES, isOptional: true), | 72 decoder.getTypes(Key.PARAMETER_TYPES, isOptional: true), |
81 decoder.getTypes( | 73 decoder.getTypes(Key.OPTIONAL_PARAMETER_TYPES, isOptional: true), |
82 Key.OPTIONAL_PARAMETER_TYPES, isOptional: true), | 74 decoder.getStrings(Key.NAMED_PARAMETERS, isOptional: true), |
83 decoder.getStrings( | 75 decoder.getTypes(Key.NAMED_PARAMETER_TYPES, isOptional: true)); |
84 Key.NAMED_PARAMETERS, isOptional: true), | |
85 decoder.getTypes( | |
86 Key.NAMED_PARAMETER_TYPES, isOptional: true)); | |
87 case TypeKind.TYPE_VARIABLE: | 76 case TypeKind.TYPE_VARIABLE: |
88 return new TypeVariableType( | 77 return new TypeVariableType(decoder.getElement(Key.ELEMENT)); |
89 decoder.getElement(Key.ELEMENT)); | |
90 case TypeKind.TYPEDEF: | 78 case TypeKind.TYPEDEF: |
91 return new TypedefType( | 79 return new TypedefType(decoder.getElement(Key.ELEMENT), |
92 decoder.getElement(Key.ELEMENT), | |
93 decoder.getTypes(Key.TYPE_ARGUMENTS, isOptional: true)); | 80 decoder.getTypes(Key.TYPE_ARGUMENTS, isOptional: true)); |
94 case TypeKind.STATEMENT: | 81 case TypeKind.STATEMENT: |
95 case TypeKind.MALFORMED_TYPE: | 82 case TypeKind.MALFORMED_TYPE: |
96 throw new UnsupportedError("Unexpected type kind '${typeKind}."); | 83 throw new UnsupportedError("Unexpected type kind '${typeKind}."); |
97 case TypeKind.DYNAMIC: | 84 case TypeKind.DYNAMIC: |
98 return const DynamicType(); | 85 return const DynamicType(); |
99 case TypeKind.VOID: | 86 case TypeKind.VOID: |
100 return const VoidType(); | 87 return const VoidType(); |
101 } | 88 } |
102 } | 89 } |
103 } | 90 } |
104 | |
OLD | NEW |