-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathsource.dart
1579 lines (1426 loc) · 52.4 KB
/
source.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
part of 'serialization.dart';
/// Interface handling [DataSourceReader] low-level data deserialization.
///
/// Each implementation of [DataSource] should have a corresponding
/// [DataSink] for which it deserializes data.
abstract class DataSource {
/// Deserialization of a section begin tag.
void begin(String tag);
/// Deserialization of a section end tag.
void end(String tag);
/// Deserialization of a string value.
String readString();
/// Deserialization of a non-negative integer value.
int readInt();
/// Deserialization of a non-negative 32 bit integer value. The value might
/// not be compacted as with [readInt].
int readUint32();
/// Deserialization of an enum value in [values].
E readEnum<E extends Enum>(List<E> values);
/// Returns the offset for a deferred entity and skips it in the read queue.
/// The offset can later be passed to [readAtOffset] to get the value. This
/// block can be written with either [DataSink.writeDeferred] or the
/// combination of [DataSink.startDeferred] and [DataSink.endDeferred].
int readDeferred();
/// Eagerly reads and returns the value for a deferred entity.
E readDeferredAsEager<E>(E Function() reader);
/// Calls [reader] to read a value at the provided offset in the underlying
/// data stream. Use with [readDeferred] to read a deferred value.
E readAtOffset<E>(int offset, E Function() reader);
/// The length of the underlying data source.
int get length;
/// The current offset being read from.
int get currentOffset;
/// Returns a string representation of the current state of the data source
/// useful for debugging in consistencies between serialization and
/// deserialization.
String get errorContext;
}
/// Deserialization reader
///
/// To be used with [DataSinkWriter] to read and write serialized data.
/// Deserialization format is deferred to provided [DataSource].
class DataSourceReader {
// The active [DataSource] to read data from. This can be the base DataSource
// for this reader or can be set to access data in a different serialized
// input in the case of deferred indexed data.
DataSource _sourceReader;
static final List<ir.DartType> emptyListOfDartTypes =
List<ir.DartType>.empty();
final bool useDeferredStrategy;
final bool useDataKinds;
final ValueInterner? interner;
final SerializationIndices importedIndices;
ComponentLookup? _componentLookup;
AbstractValueDomain? _abstractValueDomain;
SourceLookup? _sourceLookup;
late final IndexedSource<String> _stringIndex;
late final IndexedSource<Uri> _uriIndex;
late final IndexedSource<MemberData> _memberNodeIndex;
late final IndexedSource<ImportEntity> _importIndex;
late final IndexedSource<ConstantValue> _constantIndex;
final Map<Type, IndexedSource> _generalCaches = {};
ir.Member? _currentMemberContext;
MemberData? _currentMemberData;
int get currentOffset => _sourceReader.currentOffset;
int get length => _sourceReader.length;
/// Defines the beginning of this block in the address space created by all
/// instances of [DataSourceReader].
///
/// The amount by which the offsets for indexed values read by this reader are
/// shifted. That is the length of all the sources read before this one.
///
/// See [UnorderedIndexedSource] for more info.
late int startOffset;
DataSourceReader(
this._sourceReader,
CompilerOptions options,
this.importedIndices, {
this.useDataKinds = false,
this.interner,
this.useDeferredStrategy = false,
}) {
startOffset = importedIndices.registerSource(this);
_stringIndex = importedIndices.getIndexedSource<String>();
_uriIndex = importedIndices.getIndexedSource<Uri>();
_importIndex = importedIndices.getIndexedSource<ImportEntity>();
_memberNodeIndex = importedIndices.getIndexedSource<MemberData>();
_constantIndex = importedIndices.getIndexedSource<ConstantValue>();
}
/// Registers that the section [tag] starts.
///
/// This is used for debugging to verify that sections are correctly aligned
/// between serialization and deserialization.
void begin(String tag) {
if (useDataKinds) _sourceReader.begin(tag);
}
/// Registers that the section [tag] ends.
///
/// This is used for debugging to verify that sections are correctly aligned
/// between serialization and deserialization.
void end(String tag) {
if (useDataKinds) _sourceReader.end(tag);
}
/// Registers a [ComponentLookup] object with this data source to support
/// deserialization of references to kernel nodes.
void registerComponentLookup(ComponentLookup componentLookup) {
assert(_componentLookup == null);
_componentLookup = componentLookup;
}
ComponentLookup get componentLookup {
return _componentLookup!;
}
void registerSourceLookup(SourceLookup sourceLookup) {
assert(_sourceLookup == null);
_sourceLookup = sourceLookup;
}
SourceLookup get sourceLookup => _sourceLookup!;
/// Registers a [AbstractValueDomain] with this data source to support
/// deserialization of abstract values.
void registerAbstractValueDomain(AbstractValueDomain domain) {
_abstractValueDomain = domain;
}
/// Evaluates [f] with [DataSource] for the provided [source] as the
/// temporary [DataSource] for this object. Allows deferred data to be read
/// from a file other than the one currently being read from.
E readWithSource<E>(DataSourceReader source, E Function() f) {
final lastSource = _sourceReader;
final lastComponentLookup = _componentLookup;
final lastStartOffset = startOffset;
final lastAbstractValueDomain = _abstractValueDomain;
_sourceReader = source._sourceReader;
_componentLookup = source._componentLookup;
startOffset = source.startOffset;
_abstractValueDomain = source._abstractValueDomain;
final value = f();
_sourceReader = lastSource;
_componentLookup = lastComponentLookup;
startOffset = lastStartOffset;
_abstractValueDomain = lastAbstractValueDomain;
return value;
}
E readWithOffset<E>(int offset, E Function() f) {
return _sourceReader.readAtOffset(offset, f);
}
Deferrable<E> readDeferrable<E>(
E Function(DataSourceReader source) f, {
bool cacheData = true,
}) {
return useDeferredStrategy
? Deferrable<E>.deferred(
this,
f,
_sourceReader.readDeferred(),
cacheData: cacheData,
)
: Deferrable<E>.eager(_sourceReader.readDeferredAsEager(() => f(this)));
}
Deferrable<E> readDeferrableWithArg<E, A>(
E Function(DataSourceReader source, A arg) f,
A arg, {
bool cacheData = true,
}) {
return useDeferredStrategy
? Deferrable.deferredWithArg<E, A>(
this,
f,
arg,
_sourceReader.readDeferred(),
cacheData: cacheData,
)
: Deferrable<E>.eager(
_sourceReader.readDeferredAsEager(() => f(this, arg)),
);
}
/// Invoke [f] in the context of [member]. This sets up support for
/// deserialization of `ir.TreeNode`s using the `readTreeNode*InContext`
/// methods.
T inMemberContext<T>(ir.Member? context, T Function() f) {
ir.Member? oldMemberContext = _currentMemberContext;
MemberData? oldMemberData = _currentMemberData;
_currentMemberContext = context;
_currentMemberData = null;
T result = f();
_currentMemberData = oldMemberData;
_currentMemberContext = oldMemberContext;
return result;
}
MemberData get currentMemberData {
assert(
_currentMemberContext != null,
"DataSink has no current member context.",
);
return _currentMemberData ??= _getMemberData(_currentMemberContext!);
}
/// Reads a reference to an [E] value from this data source. If the value has
/// not yet been deserialized, [f] is called to deserialize the value itself.
E readIndexed<E extends Object>(E Function() f) {
E? value = readIndexedOrNull(f);
if (value == null) throw StateError("Unexpected 'null' for $E");
return value;
}
/// Reads a reference to an [E] value from this data source. [f] is called to
/// deserialize the value at the relevant index. Use [readIndexed] if the value
/// should be cached and all reads of the index should return the same object.
E readIndexedNoCache<E extends Object>(E Function() f) {
E? value = readIndexedOrNullNoCache(f);
if (value == null) throw StateError("Unexpected 'null' for $E");
return value;
}
/// Reads a reference to a nullable [E] value from this data source. If the
/// value has not yet been deserialized, [f] is called to deserialize the
/// value itself.
E? readIndexedOrNull<E extends Object>(E Function() f) {
IndexedSource<E> source =
(_generalCaches[E] ??= importedIndices.getIndexedSource<E>())
as IndexedSource<E>;
return source.read(this, f);
}
/// Reads a reference to a nullable [E] value from this data source. [f] is
/// called to deserialize the value at the relevant index. Use
/// [readIndexedOrNull] if the value should be cached and all reads of the
/// index should return the same object.
E? readIndexedOrNullNoCache<E extends Object>(E Function() f) {
IndexedSource<E> source =
(_generalCaches[E] ??= importedIndices.getIndexedSource<E>())
as IndexedSource<E>;
return source.readWithoutCache(this, f);
}
/// Reads a potentially `null` [E] value from this data source, calling [f] to
/// read the non-null value from the data source.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeValueOrNull].
E? readValueOrNull<E>(E Function() f) {
bool hasValue = readBool();
if (hasValue) {
return f();
}
return null;
}
/// Reads a list of [E] values from this data source.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeList].
List<E> readList<E>(E Function() f) {
return readListOrNull<E>(f) ?? List<E>.empty();
}
/// Reads a list of [E] values from this data source.
/// `null` is returned instead of an empty list.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeList].
List<E>? readListOrNull<E>(E Function() f) {
int count = readInt();
if (count == 0) return null;
return List<E>.generate(count, (_) => f(), growable: false);
}
bool readBool() {
_checkDataKind(DataKind.bool);
return _readBool();
}
/// Reads a boolean value from this data source.
bool _readBool() {
int value = _sourceReader.readInt();
assert(value == 0 || value == 1);
return value == 1;
}
/// Reads a non-negative 30 bit integer value from this data source.
int readInt() {
_checkDataKind(DataKind.uint30);
return _sourceReader.readInt();
}
/// Reads a non-negative 32 bit integer value from this data source. The value
/// might not be compacted as with [readInt].
int readUint32() {
_checkDataKind(DataKind.uint32);
return _sourceReader.readUint32();
}
/// Reads a potentially `null` non-negative integer value from this data
/// source.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeIntOrNull].
int? readIntOrNull() {
bool hasValue = readBool();
if (hasValue) {
return readInt();
}
return null;
}
/// Reads a string value from this data source.
String readString() {
_checkDataKind(DataKind.string);
return _readString();
}
String _readString() {
// Cannot use a tear-off for `_sourceReader.readString` because the data
// source may be different at the time of reading.
return _stringIndex.read(this, () => _sourceReader.readString())!;
}
/// Reads a potentially `null` string value from this data source.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeStringOrNull].
String? readStringOrNull() {
bool hasValue = readBool();
if (hasValue) {
return readString();
}
return null;
}
/// Reads a list of string values from this data source.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeStrings].
List<String> readStrings() {
return readStringsOrNull() ?? const <String>[];
}
/// Reads a list of string values from this data source. If the list would be
/// empty returns `null` instead.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeStrings].
List<String>? readStringsOrNull() {
int count = readInt();
if (count == 0) return null;
return List.generate(count, (_) => readString(), growable: false);
}
/// Reads a map from [Name] values to [V] values from this data source,
/// calling [f] to read each value from the data source.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeNameMap].
Map<Name, V> readNameMap<V>(V Function() f) {
int count = readInt();
Map<Name, V> map = {};
for (int i = 0; i < count; i++) {
Name key = readMemberName();
V value = f();
map[key] = value;
}
return map;
}
/// Reads a map from string values to [V] values from this data source,
/// calling [f] to read each value from the data source.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeStringMap].
Map<String, V> readStringMap<V>(V Function() f) {
int count = readInt();
if (count == 0) return {};
Map<String, V> map = {};
for (int i = 0; i < count; i++) {
String key = readString();
V value = f();
map[key] = value;
}
return map;
}
/// Reads an enum value from the list of enum [values] from this data source.
///
/// The [values] argument is intended to be the static `.values` field on
/// enum classes, for instance:
///
/// enum Foo { bar, baz }
/// ...
/// Foo foo = source.readEnum(Foo.values);
///
E readEnum<E extends Enum>(List<E> values) {
_checkDataKind(DataKind.enumValue);
return _sourceReader.readEnum(values);
}
/// Reads a URI value from this data source.
Uri readUri() {
_checkDataKind(DataKind.uri);
return _readUri();
}
Uri _readUri() {
return _uriIndex.read(this, _doReadUri)!;
}
Uri _doReadUri() {
return Uri.parse(_readString());
}
/// Reads a reference to a kernel library node from this data source.
ir.Library readLibraryNode() {
_checkDataKind(DataKind.libraryNode);
return _readLibraryData().node;
}
LibraryData _readLibraryData() {
Uri canonicalUri = _readUri();
return componentLookup.getLibraryDataByUri(canonicalUri);
}
/// Reads a reference to a kernel class node from this data source.
ir.Class readClassNode() {
_checkDataKind(DataKind.classNode);
return _readClassData().node;
}
ClassData _readClassData() {
LibraryData library = _readLibraryData();
String name = _readString();
return library.lookupClassByName(name)!;
}
/// Reads a reference to a kernel extension type declaration node from this
/// data source.
ir.ExtensionTypeDeclaration readExtensionTypeDeclarationNode() {
_checkDataKind(DataKind.extensionTypeDeclarationNode);
return _readExtensionTypeDeclarationNode();
}
ir.ExtensionTypeDeclaration _readExtensionTypeDeclarationNode() {
LibraryData library = _readLibraryData();
String name = _readString();
return library.lookupExtensionTypeDeclaration(name)!;
}
/// Reads a reference to a kernel typedef node from this data source.
ir.Typedef readTypedefNode() {
_checkDataKind(DataKind.typedefNode);
return _readTypedefNode();
}
ir.Typedef _readTypedefNode() {
LibraryData library = _readLibraryData();
String name = _readString();
return library.lookupTypedef(name)!;
}
/// Reads a reference to a kernel member node from this data source.
ir.Member readMemberNode() {
_checkDataKind(DataKind.memberNode);
return _readMemberData().node;
}
MemberData _readMemberData() {
return _memberNodeIndex.read(this, _readMemberDataInternal)!;
}
MemberData _readMemberDataInternal() {
MemberContextKind kind = _sourceReader.readEnum(MemberContextKind.values);
switch (kind) {
case MemberContextKind.cls:
ClassData cls = _readClassData();
String name = _readString();
return cls.lookupMemberDataByName(name)!;
case MemberContextKind.library:
LibraryData library = _readLibraryData();
String name = _readString();
return library.lookupMemberDataByName(name)!;
}
}
/// Reads a list of references to kernel member nodes from this data source.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeMemberNodes].
List<E> readMemberNodes<E extends ir.Member>() {
return readMemberNodesOrNull<E>() ?? List.empty();
}
/// Reads a list of references to kernel member nodes from this data source.
/// `null` is returned instead of an empty list.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeMemberNodes].
List<E>? readMemberNodesOrNull<E extends ir.Member>() {
int count = readInt();
if (count == 0) return null;
return List<E>.generate(
count,
(_) => readMemberNode() as E,
growable: false,
);
}
/// Reads a map from kernel member nodes to [V] values from this data source,
/// calling [f] to read each value from the data source.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeMemberNodeMap].
Map<K, V> readMemberNodeMap<K extends ir.Member, V>(V Function() f) {
return readMemberNodeMapOrNull<K, V>(f) ?? {};
}
/// Reads a map from kernel member nodes to [V] values from this data source,
/// calling [f] to read each value from the data source. `null` is returned
/// instead of an empty map.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeMemberNodeMap].
Map<K, V>? readMemberNodeMapOrNull<K extends ir.Member, V>(V Function() f) {
int count = readInt();
if (count == 0) return null;
Map<K, V> map = {};
for (int i = 0; i < count; i++) {
final node = readMemberNode() as K;
V value = f();
map[node] = value;
}
return map;
}
/// Reads a [Name] from this data source.
Name readMemberName() {
String text = readString();
Uri? uri = readValueOrNull(readUri);
bool setter = readBool();
return Name(text, uri, isSetter: setter);
}
/// Reads a reference to a kernel tree node from this data source.
ir.TreeNode readTreeNode() {
_checkDataKind(DataKind.treeNode);
return _readTreeNode(null);
}
ir.TreeNode _readTreeNode(MemberData? memberData) {
_TreeNodeKind kind = _sourceReader.readEnum(_TreeNodeKind.values);
switch (kind) {
case _TreeNodeKind.cls:
return _readClassData().node;
case _TreeNodeKind.member:
return _readMemberData().node;
case _TreeNodeKind.functionDeclarationVariable:
final functionDeclaration =
_readTreeNode(memberData) as ir.FunctionDeclaration;
return functionDeclaration.variable;
case _TreeNodeKind.functionNode:
return _readFunctionNode(memberData);
case _TreeNodeKind.typeParameter:
return _readTypeParameter(memberData);
case _TreeNodeKind.constant:
memberData ??= _readMemberData();
final expression = _readTreeNode(memberData) as ir.ConstantExpression;
ir.Constant constant = memberData.getConstantByIndex(
expression,
_sourceReader.readInt(),
);
return ConstantReference(expression, constant);
case _TreeNodeKind.node:
memberData ??= _readMemberData();
int index = _sourceReader.readInt();
ir.TreeNode treeNode = memberData.getTreeNodeByIndex(index);
return treeNode;
}
}
/// Reads a reference to a potentially `null` kernel tree node from this data
/// source.
ir.TreeNode? readTreeNodeOrNull() {
bool hasValue = readBool();
if (hasValue) {
return readTreeNode();
}
return null;
}
/// Reads a list of references to kernel tree nodes from this data source.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeTreeNodes].
List<E> readTreeNodes<E extends ir.TreeNode>() {
return readTreeNodesOrNull<E>() ?? List.empty();
}
/// Reads a list of references to kernel tree nodes from this data source.
/// `null` is returned instead of an empty list.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeTreeNodes].
List<E>? readTreeNodesOrNull<E extends ir.TreeNode>() {
int count = readInt();
if (count == 0) return null;
return List<E>.generate(count, (i) => readTreeNode() as E, growable: false);
}
/// Reads a map from kernel tree nodes to [V] values from this data source,
/// calling [f] to read each value from the data source.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeTreeNodeMap].
Map<K, V> readTreeNodeMap<K extends ir.TreeNode, V>(V Function() f) {
return readTreeNodeMapOrNull(f) ?? <K, V>{};
}
Map<K, V>? readTreeNodeMapOrNull<K extends ir.TreeNode, V>(V Function() f) {
int count = readInt();
if (count == 0) return null;
Map<K, V> map = {};
for (int i = 0; i < count; i++) {
final node = readTreeNode() as K;
V value = f();
map[node] = value;
}
return map;
}
/// Reads a reference to a kernel tree node in the known [context] from this
/// data source.
ir.TreeNode readTreeNodeInContext() {
return readTreeNodeInContextInternal(currentMemberData);
}
ir.TreeNode readTreeNodeInContextInternal(MemberData memberData) {
_checkDataKind(DataKind.treeNode);
return _readTreeNode(memberData);
}
/// Reads a reference to a potentially `null` kernel tree node in the known
/// [context] from this data source.
ir.TreeNode? readTreeNodeOrNullInContext() {
bool hasValue = readBool();
if (hasValue) {
return readTreeNodeInContextInternal(currentMemberData);
}
return null;
}
/// Reads a map from kernel tree nodes to [V] values in the known [context]
/// from this data source, calling [f] to read each value from the data
/// source.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeTreeNodeMapInContext].
Map<K, V> readTreeNodeMapInContext<K extends ir.TreeNode, V>(V Function() f) {
return readTreeNodeMapInContextOrNull<K, V>(f) ?? {};
}
/// Reads a map from kernel tree nodes to [V] values in the known [context]
/// from this data source, calling [f] to read each value from the data
/// source. `null` is returned for an empty map.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeTreeNodeMapInContext].
Map<K, V>? readTreeNodeMapInContextOrNull<K extends ir.TreeNode, V>(
V Function() f,
) {
int count = readInt();
if (count == 0) return null;
Map<K, V> map = {};
for (int i = 0; i < count; i++) {
final node = readTreeNodeInContextInternal(currentMemberData) as K;
V value = f();
map[node] = value;
}
return map;
}
/// Reads a reference to a kernel type parameter node from this data source.
ir.TypeParameter readTypeParameterNode() {
_checkDataKind(DataKind.typeParameterNode);
return _readTypeParameter(null);
}
ir.TypeParameter _readTypeParameter(MemberData? memberData) {
_TypeParameterKind kind = _sourceReader.readEnum(_TypeParameterKind.values);
switch (kind) {
case _TypeParameterKind.cls:
ir.Class cls = _readClassData().node;
return cls.typeParameters[_sourceReader.readInt()];
case _TypeParameterKind.functionNode:
ir.FunctionNode functionNode = _readFunctionNode(memberData);
return functionNode.typeParameters[_sourceReader.readInt()];
}
}
/// Reads a list of references to kernel type parameter nodes from this data
/// source.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeTypeParameterNodes].
List<ir.TypeParameter> readTypeParameterNodes() {
int count = readInt();
return List<ir.TypeParameter>.generate(
count,
(index) => readTypeParameterNode(),
growable: false,
);
}
/// Reads a type from this data source.
DartType readDartType() {
_checkDataKind(DataKind.dartType);
final type = DartType.readFromDataSource(this, []);
return interner?.internDartType(type) ?? type;
}
/// Reads a nullable type from this data source.
DartType? readDartTypeOrNull() {
_checkDataKind(DataKind.dartType);
return DartType.readFromDataSourceOrNull(this, []);
}
/// Reads a list of types from this data source.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeDartTypes].
List<DartType> readDartTypes() {
// Share the list when empty.
return readDartTypesOrNull() ?? const [];
}
/// Reads a list of types from this data source. Returns `null` instead of an
/// empty list.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeDartTypes].
List<DartType>? readDartTypesOrNull() {
int count = readInt();
if (count == 0) return null;
return List.generate(count, (_) => readDartType(), growable: false);
}
/// Reads a kernel type node from this data source. If [allowNull], the
/// returned type is allowed to be `null`.
ir.DartType readDartTypeNode() {
_checkDataKind(DataKind.dartTypeNode);
ir.DartType? type = _readDartTypeNodeOrNull();
if (type == null) throw UnsupportedError('Unexpected `null` DartTypeNode');
return type;
}
/// Reads a kernel type node from this data source. The returned type is
/// allowed to be `null`.
ir.DartType? readDartTypeNodeOrNull() {
_checkDataKind(DataKind.dartTypeNode);
return _readDartTypeNodeOrNull();
}
ir.DartType? _readDartTypeNodeOrNull() {
final type = _readDartTypeNode([]);
return interner?.internDartTypeNode(type) ?? type;
}
ir.DartType? _readDartTypeNode(
List<ir.StructuralParameter> functionTypeVariables,
) {
DartTypeNodeKind kind = readEnum(DartTypeNodeKind.values);
switch (kind) {
case DartTypeNodeKind.none:
return null;
case DartTypeNodeKind.voidType:
return const ir.VoidType();
case DartTypeNodeKind.invalidType:
return const ir.InvalidType();
case DartTypeNodeKind.neverType:
ir.Nullability nullability = readEnum(ir.Nullability.values);
return ir.NeverType.fromNullability(nullability);
case DartTypeNodeKind.typeParameterType:
ir.TypeParameter typeParameter = readTypeParameterNode();
ir.Nullability typeParameterTypeNullability = readEnum(
ir.Nullability.values,
);
ir.DartType? promotedBound = _readDartTypeNode(functionTypeVariables);
ir.TypeParameterType typeParameterType = ir.TypeParameterType(
typeParameter,
typeParameterTypeNullability,
);
if (promotedBound == null) {
return typeParameterType;
} else {
return ir.IntersectionType(typeParameterType, promotedBound);
}
case DartTypeNodeKind.functionTypeVariable:
int index = readInt();
assert(0 <= index && index < functionTypeVariables.length);
ir.Nullability typeParameterTypeNullability = readEnum(
ir.Nullability.values,
);
ir.StructuralParameterType typeParameterType =
ir.StructuralParameterType(
functionTypeVariables[index],
typeParameterTypeNullability,
);
return typeParameterType;
case DartTypeNodeKind.functionType:
begin(functionTypeNodeTag);
int typeParameterCount = readInt();
List<ir.StructuralParameter> typeParameters =
List<ir.StructuralParameter>.generate(
typeParameterCount,
(int index) => ir.StructuralParameter(),
growable: false,
);
functionTypeVariables = List<ir.StructuralParameter>.from(
functionTypeVariables,
)..addAll(typeParameters);
for (int index = 0; index < typeParameterCount; index++) {
typeParameters[index].name = readString();
typeParameters[index].bound =
_readDartTypeNode(functionTypeVariables)!;
typeParameters[index].defaultType =
_readDartTypeNode(functionTypeVariables)!;
}
ir.DartType returnType = _readDartTypeNode(functionTypeVariables)!;
ir.Nullability nullability = readEnum(ir.Nullability.values);
int requiredParameterCount = readInt();
List<ir.DartType> positionalParameters = _readDartTypeNodes(
functionTypeVariables,
);
final namedParameters = _readNamedTypeNodes(functionTypeVariables);
end(functionTypeNodeTag);
return ir.FunctionType(
positionalParameters,
returnType,
nullability,
namedParameters: namedParameters,
typeParameters: typeParameters,
requiredParameterCount: requiredParameterCount,
);
case DartTypeNodeKind.interfaceType:
ir.Class cls = readClassNode();
ir.Nullability nullability = readEnum(ir.Nullability.values);
List<ir.DartType> typeArguments = _readDartTypeNodes(
functionTypeVariables,
);
return ir.InterfaceType(cls, nullability, typeArguments);
case DartTypeNodeKind.recordType:
ir.Nullability nullability = readEnum(ir.Nullability.values);
List<ir.DartType> positional = _readDartTypeNodes(
functionTypeVariables,
);
List<ir.NamedType> named = _readNamedTypeNodes(functionTypeVariables);
return ir.RecordType(positional, named, nullability);
case DartTypeNodeKind.extensionType:
ir.ExtensionTypeDeclaration extensionTypeDeclaration =
readExtensionTypeDeclarationNode();
ir.Nullability nullability = readEnum(ir.Nullability.values);
List<ir.DartType> typeArguments = _readDartTypeNodes(
functionTypeVariables,
);
return ir.ExtensionType(
extensionTypeDeclaration,
nullability,
typeArguments,
);
case DartTypeNodeKind.typedef:
ir.Typedef typedef = readTypedefNode();
ir.Nullability nullability = readEnum(ir.Nullability.values);
List<ir.DartType> typeArguments = _readDartTypeNodes(
functionTypeVariables,
);
return ir.TypedefType(typedef, nullability, typeArguments);
case DartTypeNodeKind.dynamicType:
return const ir.DynamicType();
case DartTypeNodeKind.futureOrType:
ir.Nullability nullability = readEnum(ir.Nullability.values);
ir.DartType typeArgument = _readDartTypeNode(functionTypeVariables)!;
return ir.FutureOrType(typeArgument, nullability);
case DartTypeNodeKind.nullType:
return const ir.NullType();
}
}
List<ir.NamedType> _readNamedTypeNodes(
List<ir.StructuralParameter> functionTypeVariables,
) {
int count = readInt();
if (count == 0) return const [];
return List<ir.NamedType>.generate(count, (index) {
String name = readString();
bool isRequired = readBool();
ir.DartType type = _readDartTypeNode(functionTypeVariables)!;
return ir.NamedType(name, type, isRequired: isRequired);
}, growable: false);
}
/// Reads a list of kernel type nodes from this data source.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeDartTypeNodes].
List<ir.DartType> readDartTypeNodes() {
return readDartTypeNodesOrNull() ?? const [];
}
/// Reads a list of kernel type nodes from this data source. `null` is
/// returned instead of an empty list.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeDartTypeNodes].
List<ir.DartType>? readDartTypeNodesOrNull() {
int count = readInt();
if (count == 0) return null;
return List<ir.DartType>.generate(
count,
(index) => readDartTypeNode(),
growable: false,
);
}
List<ir.DartType> _readDartTypeNodes(
List<ir.StructuralParameter> functionTypeVariables,
) {
int count = readInt();
if (count == 0) return emptyListOfDartTypes;
return List<ir.DartType>.generate(
count,
(index) => _readDartTypeNode(functionTypeVariables)!,
growable: false,
);
}
/// Reads a source span from this data source.
SourceSpan readSourceSpan() {
_checkDataKind(DataKind.sourceSpan);
Uri uri = _readUri();
int begin = _sourceReader.readInt();
int end = _sourceReader.readInt();
return SourceSpan(uri, begin, end);
}
/// Reads a reference to a library entity from this data source.
LibraryEntity readLibrary() {
return readIndexed<LibraryEntity>(() => JLibrary.readFromDataSource(this));
}
/// Reads a reference to a potentially `null` library entity from this data
/// source.
LibraryEntity? readLibraryOrNull() {
bool hasValue = readBool();
if (hasValue) {
return readLibrary();
}
return null;
}
/// Reads a library from library entities to [V] values from this data source,
/// calling [f] to read each value from the data source.
///
/// This is a convenience method to be used together with
/// [DataSinkWriter.writeLibraryMap].
Map<K, V> readLibraryMap<K extends LibraryEntity, V>(V Function() f) {
return readLibraryMapOrNull<K, V>(f) ?? {};
}