Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: pkg/compiler/lib/src/serialization/serialization.dart

Issue 2603263002: Prefix resolution_types with Resolution. (Closed)
Patch Set: Rebased Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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; 5 library dart2js.serialization;
6 6
7 import '../common.dart'; 7 import '../common.dart';
8 import '../common/resolution.dart'; 8 import '../common/resolution.dart';
9 import '../constants/expressions.dart'; 9 import '../constants/expressions.dart';
10 import '../elements/resolution_types.dart'; 10 import '../elements/resolution_types.dart';
11 import '../elements/elements.dart'; 11 import '../elements/elements.dart';
12 import '../library_loader.dart' show LibraryProvider; 12 import '../library_loader.dart' show LibraryProvider;
13 import '../util/enumset.dart'; 13 import '../util/enumset.dart';
14 import 'constant_serialization.dart'; 14 import 'constant_serialization.dart';
15 import 'element_serialization.dart'; 15 import 'element_serialization.dart';
16 import 'json_serializer.dart'; 16 import 'json_serializer.dart';
17 import 'keys.dart'; 17 import 'keys.dart';
18 import 'type_serialization.dart'; 18 import 'type_serialization.dart';
19 import 'values.dart'; 19 import 'values.dart';
20 20
21 export 'task.dart' show LibraryDeserializer; 21 export 'task.dart' show LibraryDeserializer;
22 22
23 /// An object that supports the encoding an [ObjectValue] for serialization. 23 /// An object that supports the encoding an [ObjectValue] for serialization.
24 /// 24 ///
25 /// The [ObjectEncoder] ensures that nominality and circularities of 25 /// The [ObjectEncoder] ensures that nominality and circularities of
26 /// non-primitive values like [Element], [DartType] and [ConstantExpression] are 26 /// non-primitive values like [Element], [ResolutionDartType] and
27 /// handled. 27 /// [ConstantExpression] are handled.
28 class ObjectEncoder extends AbstractEncoder<Key> { 28 class ObjectEncoder extends AbstractEncoder<Key> {
29 /// Creates an [ObjectEncoder] in the scope of [serializer] that uses [map] 29 /// Creates an [ObjectEncoder] in the scope of [serializer] that uses [map]
30 /// as its internal storage. 30 /// as its internal storage.
31 ObjectEncoder(Serializer serializer, Map<dynamic, Value> map) 31 ObjectEncoder(Serializer serializer, Map<dynamic, Value> map)
32 : super(serializer, map); 32 : super(serializer, map);
33 33
34 String get _name => 'Object'; 34 String get _name => 'Object';
35 } 35 }
36 36
37 /// An object that supports the encoding a [MapValue] for serialization. 37 /// An object that supports the encoding a [MapValue] for serialization.
38 /// 38 ///
39 /// The [MapEncoder] ensures that nominality and circularities of 39 /// The [MapEncoder] ensures that nominality and circularities of
40 /// non-primitive values like [Element], [DartType] and [ConstantExpression] are 40 /// non-primitive values like [Element], [ResolutionDartType] and
41 /// handled. 41 /// [ConstantExpression] are handled.
42 class MapEncoder extends AbstractEncoder<String> { 42 class MapEncoder extends AbstractEncoder<String> {
43 /// Creates an [MapEncoder] in the scope of [serializer] that uses [map] 43 /// Creates an [MapEncoder] in the scope of [serializer] that uses [map]
44 /// as its internal storage. 44 /// as its internal storage.
45 MapEncoder(Serializer serializer, Map<String, Value> map) 45 MapEncoder(Serializer serializer, Map<String, Value> map)
46 : super(serializer, map); 46 : super(serializer, map);
47 47
48 String get _name => 'Map'; 48 String get _name => 'Map';
49 } 49 }
50 50
51 /// An object that supports the encoding a [ListValue] containing [ObjectValue]s 51 /// An object that supports the encoding a [ListValue] containing [ObjectValue]s
52 /// or [MapValue]s. 52 /// or [MapValue]s.
53 /// 53 ///
54 /// The [ListEncoder] ensures that nominality and circularities of 54 /// The [ListEncoder] ensures that nominality and circularities of
55 /// non-primitive values like [Element], [DartType] and [ConstantExpression] are 55 /// non-primitive values like [Element], [ResolutionDartType] and
56 /// handled. 56 /// [ConstantExpression] are handled.
57 class ListEncoder { 57 class ListEncoder {
58 final Serializer _serializer; 58 final Serializer _serializer;
59 final List<Value> _list; 59 final List<Value> _list;
60 60
61 /// Creates an [ListEncoder] in the scope of [_serializer] that uses [_list] 61 /// Creates an [ListEncoder] in the scope of [_serializer] that uses [_list]
62 /// as its internal storage. 62 /// as its internal storage.
63 ListEncoder(this._serializer, this._list); 63 ListEncoder(this._serializer, this._list);
64 64
65 /// Creates an [ObjectEncoder] and adds it to the encoded list. 65 /// Creates an [ObjectEncoder] and adds it to the encoded list.
66 ObjectEncoder createObject() { 66 ObjectEncoder createObject() {
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 /// If [constants] is empty, it is skipped. 144 /// If [constants] is empty, it is skipped.
145 void setConstants(K key, Iterable<ConstantExpression> constants) { 145 void setConstants(K key, Iterable<ConstantExpression> constants) {
146 _checkKey(key); 146 _checkKey(key);
147 if (constants.isNotEmpty) { 147 if (constants.isNotEmpty) {
148 _map[key] = new ListValue( 148 _map[key] = new ListValue(
149 constants.map(_serializer.createConstantValue).toList()); 149 constants.map(_serializer.createConstantValue).toList());
150 } 150 }
151 } 151 }
152 152
153 /// Maps the [key] entry to the [type] in the encoded object. 153 /// Maps the [key] entry to the [type] in the encoded object.
154 void setType(K key, DartType type) { 154 void setType(K key, ResolutionDartType type) {
155 _checkKey(key); 155 _checkKey(key);
156 _map[key] = _serializer.createTypeValue(type); 156 _map[key] = _serializer.createTypeValue(type);
157 } 157 }
158 158
159 /// Maps the [key] entry to the [types] in the encoded object. 159 /// Maps the [key] entry to the [types] in the encoded object.
160 /// 160 ///
161 /// If [types] is empty, it is skipped. 161 /// If [types] is empty, it is skipped.
162 void setTypes(K key, Iterable<DartType> types) { 162 void setTypes(K key, Iterable<ResolutionDartType> types) {
163 _checkKey(key); 163 _checkKey(key);
164 if (types.isNotEmpty) { 164 if (types.isNotEmpty) {
165 _map[key] = 165 _map[key] =
166 new ListValue(types.map(_serializer.createTypeValue).toList()); 166 new ListValue(types.map(_serializer.createTypeValue).toList());
167 } 167 }
168 } 168 }
169 169
170 /// Maps the [key] entry to the [uri] in the encoded object using [baseUri] to 170 /// Maps the [key] entry to the [uri] in the encoded object using [baseUri] to
171 /// relatives the encoding. 171 /// relatives the encoding.
172 /// 172 ///
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 List list = _map[_getKeyValue(key)]; 405 List list = _map[_getKeyValue(key)];
406 if (list == null) { 406 if (list == null) {
407 if (isOptional) { 407 if (isOptional) {
408 return const []; 408 return const [];
409 } 409 }
410 throw new StateError("Constants value '$key' not found in $_map."); 410 throw new StateError("Constants value '$key' not found in $_map.");
411 } 411 }
412 return list.map(_deserializer.deserializeConstant).toList(); 412 return list.map(_deserializer.deserializeConstant).toList();
413 } 413 }
414 414
415 /// Returns the [DartType] value associated with [key] in the decoded object. 415 /// Returns the [ResolutionDartType] value associated with [key] in the
416 /// decoded object.
416 /// 417 ///
417 /// If no value is associated with [key], then if [isOptional] is `true`, 418 /// If no value is associated with [key], then if [isOptional] is `true`,
418 /// `null` is returned, otherwise an exception is thrown. 419 /// `null` is returned, otherwise an exception is thrown.
419 DartType getType(K key, {bool isOptional: false}) { 420 ResolutionDartType getType(K key, {bool isOptional: false}) {
420 int id = _map[_getKeyValue(key)]; 421 int id = _map[_getKeyValue(key)];
421 if (id == null) { 422 if (id == null) {
422 if (isOptional) { 423 if (isOptional) {
423 return null; 424 return null;
424 } 425 }
425 throw new StateError("Type value '$key' not found in $_map."); 426 throw new StateError("Type value '$key' not found in $_map.");
426 } 427 }
427 return _deserializer.deserializeType(id); 428 return _deserializer.deserializeType(id);
428 } 429 }
429 430
430 /// Returns the list of [DartType] values associated with [key] in the decoded 431 /// Returns the list of [ResolutionDartType] values associated with [key] in
431 /// object. 432 /// the decoded object.
432 /// 433 ///
433 /// If no value is associated with [key], then if [isOptional] is `true`, 434 /// If no value is associated with [key], then if [isOptional] is `true`,
434 /// and empty [List] is returned, otherwise an exception is thrown. 435 /// and empty [List] is returned, otherwise an exception is thrown.
435 List<DartType> getTypes(K key, {bool isOptional: false}) { 436 List<ResolutionDartType> getTypes(K key, {bool isOptional: false}) {
436 List list = _map[_getKeyValue(key)]; 437 List list = _map[_getKeyValue(key)];
437 if (list == null) { 438 if (list == null) {
438 if (isOptional) { 439 if (isOptional) {
439 return const []; 440 return const [];
440 } 441 }
441 throw new StateError("Types value '$key' not found in $_map."); 442 throw new StateError("Types value '$key' not found in $_map.");
442 } 443 }
443 return list.map(_deserializer.deserializeType).toList(); 444 return list.map(_deserializer.deserializeType).toList();
444 } 445 }
445 446
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
623 Map<Key, Value> get map => objectValue.map; 624 Map<Key, Value> get map => objectValue.map;
624 } 625 }
625 626
626 /// Function used to filter which element serialized. 627 /// Function used to filter which element serialized.
627 typedef bool ElementMatcher(Element element); 628 typedef bool ElementMatcher(Element element);
628 629
629 bool includeAllElements(Element element) => true; 630 bool includeAllElements(Element element) => true;
630 631
631 /// Serializer for the transitive closure of a collection of libraries. 632 /// Serializer for the transitive closure of a collection of libraries.
632 /// 633 ///
633 /// The serializer creates an [ObjectValue] model of the [Element], [DartType] 634 /// The serializer creates an [ObjectValue] model of the [Element],
634 /// and [ConstantExpression] values in the transitive closure of the serialized 635 /// [ResolutionDartType] and [ConstantExpression] values in the transitive
635 /// libraries. 636 /// closure of the serialized libraries.
636 /// 637 ///
637 /// The model layout of the produced [objectValue] is: 638 /// The model layout of the produced [objectValue] is:
638 /// 639 ///
639 /// { // Header object 640 /// { // Header object
640 /// Key.ELEMENTS: [ 641 /// Key.ELEMENTS: [
641 /// {...}, // [ObjectValue] of the 0th [Element]. 642 /// {...}, // [ObjectValue] of the 0th [Element].
642 /// ... 643 /// ...
643 /// {...}, // [ObjectValue] of the n-th [Element]. 644 /// {...}, // [ObjectValue] of the n-th [Element].
644 /// ], 645 /// ],
645 /// Key.TYPES: [ 646 /// Key.TYPES: [
646 /// {...}, // [ObjectValue] of the 0th [DartType]. 647 /// {...}, // [ObjectValue] of the 0th [DartType].
647 /// ... 648 /// ...
648 /// {...}, // [ObjectValue] of the n-th [DartType]. 649 /// {...}, // [ObjectValue] of the n-th [DartType].
649 /// ], 650 /// ],
650 /// Key.CONSTANTS: [ 651 /// Key.CONSTANTS: [
651 /// {...}, // [ObjectValue] of the 0th [ConstantExpression]. 652 /// {...}, // [ObjectValue] of the 0th [ConstantExpression].
652 /// ... 653 /// ...
653 /// {...}, // [ObjectValue] of the n-th [ConstantExpression]. 654 /// {...}, // [ObjectValue] of the n-th [ConstantExpression].
654 /// ], 655 /// ],
655 /// } 656 /// }
656 /// 657 ///
657 // TODO(johnniwinther): Support dependencies between serialized subcomponent. 658 // TODO(johnniwinther): Support dependencies between serialized subcomponent.
658 class Serializer { 659 class Serializer {
659 List<SerializerPlugin> plugins = <SerializerPlugin>[]; 660 List<SerializerPlugin> plugins = <SerializerPlugin>[];
660 661
661 Map<Uri, dynamic> _dependencyMap = <Uri, dynamic>{}; 662 Map<Uri, dynamic> _dependencyMap = <Uri, dynamic>{};
662 Map<Element, DataObject> _elementMap = <Element, DataObject>{}; 663 Map<Element, DataObject> _elementMap = <Element, DataObject>{};
663 Map<ConstantExpression, DataObject> _constantMap = 664 Map<ConstantExpression, DataObject> _constantMap =
664 <ConstantExpression, DataObject>{}; 665 <ConstantExpression, DataObject>{};
665 Map<DartType, DataObject> _typeMap = <DartType, DataObject>{}; 666 Map<ResolutionDartType, DataObject> _typeMap =
667 <ResolutionDartType, DataObject>{};
666 List _pendingList = []; 668 List _pendingList = [];
667 ElementMatcher shouldInclude; 669 ElementMatcher shouldInclude;
668 670
669 // TODO(johnniwinther): Replace [includeElement] with a general strategy. 671 // TODO(johnniwinther): Replace [includeElement] with a general strategy.
670 Serializer({this.shouldInclude: includeAllElements}); 672 Serializer({this.shouldInclude: includeAllElements});
671 673
672 /// Add the transitive closure of [library] to this serializer. 674 /// Add the transitive closure of [library] to this serializer.
673 void serialize(LibraryElement library) { 675 void serialize(LibraryElement library) {
674 // Call [_getElementId] for its side-effect: To create a 676 // Call [_getElementId] for its side-effect: To create a
675 // [DataObject] for [library]. If not already created, this will 677 // [DataObject] for [library]. If not already created, this will
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
841 /// queue of this serializer. 843 /// queue of this serializer.
842 ConstantValue createConstantValue(ConstantExpression constant) { 844 ConstantValue createConstantValue(ConstantExpression constant) {
843 return new ConstantValue(constant, _getConstantId(constant)); 845 return new ConstantValue(constant, _getConstantId(constant));
844 } 846 }
845 847
846 /// Returns the id [Value] for [type]. 848 /// Returns the id [Value] for [type].
847 /// 849 ///
848 /// If [type] has no [DataObject], a new [DataObject] is created and 850 /// If [type] has no [DataObject], a new [DataObject] is created and
849 /// encoding the [ObjectValue] for [type] is put into the work queue of this 851 /// encoding the [ObjectValue] for [type] is put into the work queue of this
850 /// serializer. 852 /// serializer.
851 Value _getTypeId(DartType type) { 853 Value _getTypeId(ResolutionDartType type) {
852 DataObject dataObject = _typeMap[type]; 854 DataObject dataObject = _typeMap[type];
853 if (dataObject == null) { 855 if (dataObject == null) {
854 _typeMap[type] = dataObject = new DataObject( 856 _typeMap[type] = dataObject = new DataObject(
855 new IntValue(_typeMap.length), new EnumValue(type.kind)); 857 new IntValue(_typeMap.length), new EnumValue(type.kind));
856 // Delay the serialization of the type itself to avoid loops, and to keep 858 // Delay the serialization of the type itself to avoid loops, and to keep
857 // the call stack small. 859 // the call stack small.
858 _pendingList.add(() => _encodeType(type, dataObject)); 860 _pendingList.add(() => _encodeType(type, dataObject));
859 } 861 }
860 return dataObject.id; 862 return dataObject.id;
861 } 863 }
862 864
863 /// Encodes [type] into the [ObjectValue] of [dataObject]. 865 /// Encodes [type] into the [ObjectValue] of [dataObject].
864 void _encodeType(DartType type, DataObject dataObject) { 866 void _encodeType(ResolutionDartType type, DataObject dataObject) {
865 const TypeSerializer().visit(type, new ObjectEncoder(this, dataObject.map)); 867 const TypeSerializer().visit(type, new ObjectEncoder(this, dataObject.map));
866 } 868 }
867 869
868 /// Creates the [TypeValue] for [type]. 870 /// Creates the [TypeValue] for [type].
869 /// 871 ///
870 /// If [type] has not already been serialized, it is added to the work 872 /// If [type] has not already been serialized, it is added to the work
871 /// queue of this serializer. 873 /// queue of this serializer.
872 TypeValue createTypeValue(DartType type) { 874 TypeValue createTypeValue(ResolutionDartType type) {
873 return new TypeValue(type, _getTypeId(type)); 875 return new TypeValue(type, _getTypeId(type));
874 } 876 }
875 877
876 ObjectValue get objectValue { 878 ObjectValue get objectValue {
877 _emptyWorklist(); 879 _emptyWorklist();
878 880
879 Map<Key, Value> map = <Key, Value>{}; 881 Map<Key, Value> map = <Key, Value>{};
880 map[Key.ELEMENTS] = 882 map[Key.ELEMENTS] =
881 new ListValue(_elementMap.values.map((l) => l.objectValue).toList()); 883 new ListValue(_elementMap.values.map((l) => l.objectValue).toList());
882 if (_typeMap.isNotEmpty) { 884 if (_typeMap.isNotEmpty) {
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
975 // between deserialized subcomponent. 977 // between deserialized subcomponent.
976 class Deserializer { 978 class Deserializer {
977 final DeserializationContext context; 979 final DeserializationContext context;
978 final SerializationDecoder decoder; 980 final SerializationDecoder decoder;
979 final Uri sourceUri; 981 final Uri sourceUri;
980 ObjectDecoder _headerObject; 982 ObjectDecoder _headerObject;
981 ListDecoder _elementList; 983 ListDecoder _elementList;
982 ListDecoder _typeList; 984 ListDecoder _typeList;
983 ListDecoder _constantList; 985 ListDecoder _constantList;
984 Map<int, Element> _elementMap = {}; 986 Map<int, Element> _elementMap = {};
985 Map<int, DartType> _typeMap = {}; 987 Map<int, ResolutionDartType> _typeMap = {};
986 Map<int, ConstantExpression> _constantMap = {}; 988 Map<int, ConstantExpression> _constantMap = {};
987 989
988 Deserializer.fromText( 990 Deserializer.fromText(
989 this.context, this.sourceUri, String text, this.decoder) { 991 this.context, this.sourceUri, String text, this.decoder) {
990 _headerObject = new ObjectDecoder(this, decoder.decode(text)); 992 _headerObject = new ObjectDecoder(this, decoder.decode(text));
991 } 993 }
992 994
993 /// Returns the [ListDecoder] for the [Element]s in this deserializer. 995 /// Returns the [ListDecoder] for the [Element]s in this deserializer.
994 ListDecoder get elements { 996 ListDecoder get elements {
995 if (_elementList == null) { 997 if (_elementList == null) {
996 _elementList = _headerObject.getList(Key.ELEMENTS); 998 _elementList = _headerObject.getList(Key.ELEMENTS);
997 } 999 }
998 return _elementList; 1000 return _elementList;
999 } 1001 }
1000 1002
1001 /// Returns the [ListDecoder] for the [DartType]s in this deserializer. 1003 /// Returns the [ListDecoder] for the [ResolutionDartType]s in this
1004 /// deserializer.
1002 ListDecoder get types { 1005 ListDecoder get types {
1003 if (_typeList == null) { 1006 if (_typeList == null) {
1004 _typeList = _headerObject.getList(Key.TYPES); 1007 _typeList = _headerObject.getList(Key.TYPES);
1005 } 1008 }
1006 return _typeList; 1009 return _typeList;
1007 } 1010 }
1008 1011
1009 /// Returns the [ListDecoder] for the [ConstantExpression]s in this 1012 /// Returns the [ListDecoder] for the [ConstantExpression]s in this
1010 /// deserializer. 1013 /// deserializer.
1011 ListDecoder get constants { 1014 ListDecoder get constants {
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
1098 // Call plugins even when there is no data, so they can take action in 1101 // Call plugins even when there is no data, so they can take action in
1099 // this case. 1102 // this case.
1100 for (DeserializerPlugin plugin in context.plugins) { 1103 for (DeserializerPlugin plugin in context.plugins) {
1101 plugin.onElement(element, 1104 plugin.onElement(element,
1102 (String tag) => pluginData?.getObject(tag, isOptional: true)); 1105 (String tag) => pluginData?.getObject(tag, isOptional: true));
1103 } 1106 }
1104 } 1107 }
1105 return element; 1108 return element;
1106 } 1109 }
1107 1110
1108 /// Returns the deserialized [DartType] for [id]. 1111 /// Returns the deserialized [ResolutionDartType] for [id].
1109 DartType deserializeType(int id) { 1112 ResolutionDartType deserializeType(int id) {
1110 if (id == null) throw new ArgumentError('Deserializer.getType(null)'); 1113 if (id == null) throw new ArgumentError('Deserializer.getType(null)');
1111 return _typeMap.putIfAbsent(id, () { 1114 return _typeMap.putIfAbsent(id, () {
1112 return TypeDeserializer.deserialize(types.getObject(id)); 1115 return TypeDeserializer.deserialize(types.getObject(id));
1113 }); 1116 });
1114 } 1117 }
1115 1118
1116 /// Returns the deserialized [ConstantExpression] for [id]. 1119 /// Returns the deserialized [ConstantExpression] for [id].
1117 ConstantExpression deserializeConstant(int id) { 1120 ConstantExpression deserializeConstant(int id) {
1118 if (id == null) throw new ArgumentError('Deserializer.getConstant(null)'); 1121 if (id == null) throw new ArgumentError('Deserializer.getConstant(null)');
1119 return _constantMap.putIfAbsent(id, () { 1122 return _constantMap.putIfAbsent(id, () {
(...skipping 17 matching lines...) Expand all
1137 1140
1138 /// Returns the value used to store [key] as a property in the encoding an 1141 /// Returns the value used to store [key] as a property in the encoding an
1139 /// [ObjectValue]. 1142 /// [ObjectValue].
1140 /// 1143 ///
1141 /// Different encodings have different restrictions and capabilities as how 1144 /// Different encodings have different restrictions and capabilities as how
1142 /// to store a [Key] value. For instance: A JSON encoding needs to convert 1145 /// to store a [Key] value. For instance: A JSON encoding needs to convert
1143 /// [Key] to a [String] to store it in a JSON object; a Dart encoding can 1146 /// [Key] to a [String] to store it in a JSON object; a Dart encoding can
1144 /// choose to store a [Key] as an [int] or as the [Key] itself. 1147 /// choose to store a [Key] as an [int] or as the [Key] itself.
1145 getObjectPropertyValue(Key key); 1148 getObjectPropertyValue(Key key);
1146 } 1149 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698