-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
rti.dart
1254 lines (1079 loc) · 42 KB
/
rti.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) 2019, 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.
/// This library contains support for runtime type information.
library rti;
import 'dart:_foreign_helper'
show
getInterceptor,
JS,
JS_BUILTIN,
JS_EMBEDDED_GLOBAL,
JS_GET_NAME,
RAW_DART_FUNCTION_REF;
import 'dart:_interceptors' show JSArray, JSUnmodifiableArray;
import 'dart:_js_embedded_names'
show JsBuiltin, JsGetName, RtiUniverseFieldNames, RTI_UNIVERSE;
import 'dart:_recipe_syntax';
/// An Rti object represents both a type (e.g `Map<int, String>`) and a type
/// environment (`Map<int, String>` binds `Map.K=int` and `Map.V=String`).
///
/// There is a single [Rti] class to help reduce polymorphism in the JavaScript
/// runtime. The class has a default constructor and no final fields so it can
/// be created before much of the runtime exists.
///
/// The fields are declared in an order that gets shorter minified names for the
/// more commonly used fields. (TODO: we should exploit the fact that an Rti
/// instance never appears in a dynamic context, so does not need field names to
/// be distinct from dynamic selectors).
///
class Rti {
/// JavaScript method for 'as' check. The method is called from generated code,
/// e.g. `o as T` generates something like `rtiForT._as(o)`.
@pragma('dart2js:noElision')
dynamic _as;
/// JavaScript method for type check. The method is called from generated
/// code, e.g. parameter check for `T param` generates something like
/// `rtiForT._check(param)`.
@pragma('dart2js:noElision')
dynamic _check;
/// JavaScript method for 'is' test. The method is called from generated
/// code, e.g. `o is T` generates something like `rtiForT._is(o)`.
@pragma('dart2js:noElision')
dynamic _is;
static void _setAsCheckFunction(Rti rti, fn) {
rti._as = fn;
}
static void _setTypeCheckFunction(Rti rti, fn) {
rti._check = fn;
}
static void _setIsTestFunction(Rti rti, fn) {
rti._is = fn;
}
/// Method called from generated code to evaluate a type environment recipe in
/// `this` type environment.
Rti _eval(recipe) {
// TODO(sra): Clone the fast-path of _Universe.evalInEnvironment to here.
return _rtiEval(this, _Utils.asString(recipe));
}
/// Method called from generated code to extend `this` type environment (an
/// interface or binding Rti) with function type arguments (a singleton
/// argument or tuple of arguments).
Rti _bind(typeOrTuple) => _rtiBind(this, _castToRti(typeOrTuple));
/// Method called from generated code to extend `this` type (as a singleton
/// type environment) with function type arguments (a singleton argument or
/// tuple of arguments).
Rti _bind1(Rti typeOrTuple) => _rtiBind1(this, typeOrTuple);
// Precomputed derived types. These fields are used to hold derived types that
// are computed eagerly.
// TODO(sra): Implement precomputed type optimizations.
dynamic _precomputed1;
dynamic _precomputed2;
dynamic _precomputed3;
dynamic _precomputed4;
// The Type object corresponding to this Rti.
Object _cachedRuntimeType;
static _Type _getCachedRuntimeType(Rti rti) =>
JS('_Type|Null', '#', rti._cachedRuntimeType);
static void _setCachedRuntimeType(Rti rti, _Type type) {
rti._cachedRuntimeType = type;
}
/// The kind of Rti `this` is, one of the kindXXX constants below.
///
/// We don't use an enum since we need to create Rti objects very early.
///
/// The zero initializer ensures dart2js type analysis considers [_kind] is
/// non-nullable.
Object /*int*/ _kind = 0;
static int _getKind(Rti rti) => _Utils.asInt(rti._kind);
static void _setKind(Rti rti, int kind) {
rti._kind = kind;
}
// Terminal terms.
static const kindNever = 1;
static const kindDynamic = 2;
static const kindVoid = 3; // TODO(sra): Use `dynamic` instead?
static const kindAny = 4; // Dart1-style 'dynamic' for JS-interop.
// Unary terms.
static const kindStar = 5;
static const kindQuestion = 6;
static const kindFutureOr = 7;
// More complex terms.
static const kindInterface = 8;
// A vector of type parameters from enclosing functions and closures.
static const kindBinding = 9;
static const kindFunction = 10;
static const kindGenericFunction = 11;
static const kindGenericFunctionParameter = 12;
/// Primary data associated with type.
///
/// - Minified name of interface for interface types.
/// - Underlying type for unary terms.
/// - Class part of a type environment inside a generic class, or `null` for
/// type tuple.
dynamic _primary;
static Object _getPrimary(Rti rti) => rti._primary;
static void _setPrimary(Rti rti, value) {
rti._primary = value;
}
/// Additional data associated with type.
///
/// - The type arguments of an interface type.
/// - The type arguments from enclosing functions and closures for a
/// kindBinding.
/// - TBD for kindFunction and kindGenericFunction.
dynamic _rest;
static Object _getRest(Rti rti) => rti._rest;
static void _setRest(Rti rti, value) {
rti._rest = value;
}
static String _getInterfaceName(Rti rti) {
assert(_getKind(rti) == kindInterface);
return _Utils.asString(_getPrimary(rti));
}
static JSArray _getInterfaceTypeArguments(Rti rti) {
// The array is a plain JavaScript Array, otherwise we would need the type
// `JSArray<Rti>` to exist before we could create the type `JSArray<Rti>`.
assert(_getKind(rti) == kindInterface);
return JS('JSUnmodifiableArray', '#', _getRest(rti));
}
static Rti _getBindingBase(Rti rti) {
assert(_getKind(rti) == kindBinding);
return _castToRti(_getPrimary(rti));
}
static JSArray _getBindingArguments(Rti rti) {
assert(_getKind(rti) == kindBinding);
return JS('JSUnmodifiableArray', '#', _getRest(rti));
}
static Rti _getFutureOrArgument(Rti rti) {
assert(_getKind(rti) == kindFutureOr);
return _castToRti(_getPrimary(rti));
}
/// On [Rti]s that are type environments*, derived types are cached on the
/// environment to ensure fast canonicalization. Ground-term types (i.e. not
/// dependent on class or function type parameters) are cached in the
/// universe. This field starts as `null` and the cache is created on demand.
///
/// *Any Rti can be a type environment, since we use the type for a function
/// type environment. The ambiguity between 'generic class is the environment'
/// and 'generic class is a singleton type argument' is resolved by using
/// different indexing in the recipe.
Object _evalCache;
static Object _getEvalCache(Rti rti) => rti._evalCache;
static void _setEvalCache(Rti rti, value) {
rti._evalCache = value;
}
/// On [Rti]s that are type environments*, extended environments are cached on
/// the base environment to ensure fast canonicalization.
///
/// This field starts as `null` and the cache is created on demand.
///
/// *This is valid only on kindInterface and kindBinding Rtis. The ambiguity
/// between 'generic class is the base environment' and 'generic class is a
/// singleton type argument' is resolved [TBD] (either (1) a bind1 cache, or
/// (2)using `env._eval("@<0>")._bind(args)` in place of `env._bind1(args)`).
Object _bindCache;
static Object _getBindCache(Rti rti) => rti._bindCache;
static void _setBindCache(Rti rti, value) {
rti._bindCache = value;
}
static Rti allocate() {
return new Rti();
}
Object _canonicalRecipe;
static String _getCanonicalRecipe(Rti rti) {
var s = rti._canonicalRecipe;
assert(_Utils.isString(s), 'Missing canonical recipe');
return _Utils.asString(s);
}
static void _setCanonicalRecipe(Rti rti, String s) {
rti._canonicalRecipe = s;
}
}
Object _theUniverse() => JS_EMBEDDED_GLOBAL('', RTI_UNIVERSE);
Rti _rtiEval(Rti environment, String recipe) {
return _Universe.evalInEnvironment(_theUniverse(), environment, recipe);
}
Rti _rtiBind1(Rti environment, Rti types) {
return _Universe.bind1(_theUniverse(), environment, types);
}
Rti _rtiBind(Rti environment, Rti types) {
return _Universe.bind(_theUniverse(), environment, types);
}
/// Evaluate a ground-term type.
/// Called from generated code.
Rti findType(String recipe) {
return _Universe.eval(_theUniverse(), recipe);
}
/// Returns the Rti type of [object].
/// Called from generated code.
Rti instanceType(object) {
// TODO(sra): Add specializations of this method. One possible way is to
// arrange that the interceptor has a _getType method that is injected into
// DartObject, Interceptor and JSArray. Then this method can be replaced-by
// `getInterceptor(o)._getType(o)`, allowing interceptor optimizations to
// select the specialization.
if (_Utils.instanceOf(
object,
JS_BUILTIN(
'depends:none;effects:none;', JsBuiltin.dartObjectConstructor))) {
var rti = JS('', r'#[#]', object, JS_GET_NAME(JsGetName.RTI_NAME));
if (rti != null) return _castToRti(rti);
return _instanceTypeFromConstructor(JS('', '#.constructor', object));
}
if (_Utils.isArray(object)) {
var rti = JS('', r'#[#]', object, JS_GET_NAME(JsGetName.RTI_NAME));
// TODO(sra): Do we need to protect against an Array passed between two Dart
// programs loaded into the same JavaScript isolate (e.g. via JS-interop).
// FWIW, the legacy rti has this problem too. Perhaps JSArrays should use a
// program-local `symbol` for the type field.
if (rti != null) return _castToRti(rti);
// TODO(sra): Use JS_GET_NAME to access the recipe.
throw UnimplementedError('return JSArray<any>');
}
var interceptor = getInterceptor(object);
return _instanceTypeFromConstructor(JS('', '#.constructor', interceptor));
}
Rti _instanceTypeFromConstructor(constructor) {
// TODO(sra): Cache Rti on constructor.
return findType(JS('String', '#.name', constructor));
}
Type getRuntimeType(object) {
Rti rti = instanceType(object);
return _createRuntimeType(rti);
}
/// Called from generated code.
Type _createRuntimeType(Rti rti) {
_Type type = Rti._getCachedRuntimeType(rti);
if (type != null) return type;
// TODO(https://github.com/dart-lang/language/issues/428) For NNBD transition,
// canonicalization may be needed. It might be possible to generate a
// star-free recipe from the canonical recipe and evaluate that.
type = _Type(rti);
Rti._setCachedRuntimeType(rti, type);
return type;
}
/// Called from generated code in the constant pool.
Type typeLiteral(String recipe) {
return _createRuntimeType(findType(recipe));
}
/// Implementation of [Type] based on Rti.
class _Type implements Type {
final Rti _rti;
int _hashCode;
_Type(this._rti);
int get hashCode => _hashCode ??= Rti._getCanonicalRecipe(_rti).hashCode;
@pragma('dart2js:noInline')
bool operator ==(other) {
return (other is _Type) && identical(_rti, other._rti);
}
@override
String toString() => _rtiToString(_rti, null);
}
/// Called from generated code.
bool _generalIsTestImplementation(object) {
// This static method is installed on an Rti object as a JavaScript instance
// method. The Rti object is 'this'.
Rti testRti = _castToRti(JS('', 'this'));
Rti objectRti = instanceType(object);
return isSubtype(_theUniverse(), objectRti, testRti);
}
/// Called from generated code.
_generalAsCheckImplementation(object) {
// This static method is installed on an Rti object as a JavaScript instance
// method. The Rti object is 'this'.
Rti testRti = _castToRti(JS('', 'this'));
throw UnimplementedError(
'${Error.safeToString(object)} as ${_rtiToString(testRti, null)}');
}
/// Called from generated code.
_generalTypeCheckImplementation(object) {
// This static method is installed on an Rti object as a JavaScript instance
// method. The Rti object is 'this'.
Rti testRti = _castToRti(JS('', 'this'));
throw UnimplementedError(
'${Error.safeToString(object)} as ${_rtiToString(testRti, null)}'
' (TypeError)');
}
String _rtiToString(Rti rti, List<String> genericContext) {
int kind = Rti._getKind(rti);
if (kind == Rti.kindDynamic) return 'dynamic';
if (kind == Rti.kindVoid) return 'void';
if (kind == Rti.kindNever) return 'Never';
if (kind == Rti.kindAny) return 'any';
if (kind == Rti.kindInterface) {
String name = Rti._getInterfaceName(rti);
var arguments = Rti._getInterfaceTypeArguments(rti);
if (arguments.length != 0) {
name += '<';
for (int i = 0; i < arguments.length; i++) {
if (i > 0) name += ', ';
name += _rtiToString(_castToRti(arguments[i]), genericContext);
}
name += '>';
}
return name;
}
return '?';
}
String _rtiToDebugString(Rti rti) {
String arrayToString(Object array) {
String s = '[', sep = '';
for (int i = 0; i < _Utils.arrayLength(array); i++) {
s += sep + _rtiToDebugString(_castToRti(_Utils.arrayAt(array, i)));
sep = ', ';
}
return s + ']';
}
int kind = Rti._getKind(rti);
if (kind == Rti.kindDynamic) return 'dynamic';
if (kind == Rti.kindVoid) return 'void';
if (kind == Rti.kindNever) return 'Never';
if (kind == Rti.kindAny) return 'any';
if (kind == Rti.kindInterface) {
String name = Rti._getInterfaceName(rti);
var arguments = Rti._getInterfaceTypeArguments(rti);
if (_Utils.arrayLength(arguments) == 0) {
return 'interface("$name")';
} else {
return 'interface("$name", ${arrayToString(arguments)})';
}
}
if (kind == Rti.kindBinding) {
var base = Rti._getBindingBase(rti);
var arguments = Rti._getBindingArguments(rti);
return 'binding(${_rtiToDebugString(base)}, ${arrayToString(arguments)})';
}
return 'other(kind=$kind)';
}
/// Class of static methods for the universe of Rti objects.
///
/// The universe is the manager object for the Rti instances.
///
/// The universe itself is allocated at startup before any types or Dart objects
/// can be created, so it does not have a Dart type.
class _Universe {
_Universe._() {
throw UnimplementedError('_Universe is static methods only');
}
@pragma('dart2js:noInline')
static Object create() {
// This needs to be kept in sync with `FragmentEmitter.createRtiUniverse` in
// `fragment_emitter.dart`.
return JS(
'',
'{'
'#: new Map(),'
'#: {},'
'#: [],' // shared empty array.
'}',
RtiUniverseFieldNames.evalCache,
RtiUniverseFieldNames.typeRules,
RtiUniverseFieldNames.sharedEmptyArray);
}
// Field accessors.
static evalCache(universe) =>
JS('', '#.#', universe, RtiUniverseFieldNames.evalCache);
static Object typeRules(universe) =>
JS('', '#.#', universe, RtiUniverseFieldNames.typeRules);
static Object findRule(universe, String targetType) =>
JS('', '#.#', typeRules(universe), targetType);
static void addRules(universe, rules) {
JS('', 'Object.assign(#, #)', typeRules(universe), rules);
}
static Object sharedEmptyArray(universe) =>
JS('JSArray', '#.#', universe, RtiUniverseFieldNames.sharedEmptyArray);
/// Evaluates [recipe] in the global environment.
static Rti eval(Object universe, String recipe) {
var cache = evalCache(universe);
var probe = _cacheGet(cache, recipe);
if (probe != null) return _castToRti(probe);
var rti = _parseRecipe(universe, null, recipe);
_cacheSet(cache, recipe, rti);
return rti;
}
static Rti evalInEnvironment(
Object universe, Rti environment, String recipe) {
var cache = Rti._getEvalCache(environment);
if (cache == null) {
cache = JS('', 'new Map()');
Rti._setEvalCache(environment, cache);
}
var probe = _cacheGet(cache, recipe);
if (probe != null) return _castToRti(probe);
var rti = _parseRecipe(universe, environment, recipe);
_cacheSet(cache, recipe, rti);
return rti;
}
static Rti bind(Object universe, Rti environment, Rti argumentsRti) {
var cache = Rti._getBindCache(environment);
if (cache == null) {
cache = JS('', 'new Map()');
Rti._setBindCache(environment, cache);
}
var argumentsRecipe = Rti._getCanonicalRecipe(argumentsRti);
var probe = _cacheGet(cache, argumentsRecipe);
if (probe != null) return _castToRti(probe);
var argumentsArray;
if (Rti._getKind(argumentsRti) == Rti.kindBinding) {
argumentsArray = Rti._getBindingArguments(argumentsRti);
} else {
argumentsArray = JS('', '[#]', argumentsRti);
}
var rti = _lookupBindingRti(universe, environment, argumentsArray);
_cacheSet(cache, argumentsRecipe, rti);
return rti;
}
static Rti bind1(Object universe, Rti environment, Rti argumentsRti) {
throw UnimplementedError('_Universe.bind1');
}
static Rti evalTypeVariable(Object universe, Rti environment, String name) {
throw UnimplementedError('_Universe.evalTypeVariable("$name")');
}
static _cacheGet(cache, key) => JS('', '#.get(#)', cache, key);
static void _cacheSet(cache, key, value) {
JS('', '#.set(#, #)', cache, key, value);
}
static Rti _parseRecipe(Object universe, Object environment, String recipe) {
var parser = _Parser.create(universe, environment, recipe);
Rti rti = _Parser.parse(parser);
if (rti != null) return rti;
throw UnimplementedError('_Universe._parseRecipe("$recipe")');
}
static Rti _finishRti(Object universe, Rti rti) {
// Enter fresh Rti in global table under it's canonical recipe.
String key = Rti._getCanonicalRecipe(rti);
_cacheSet(evalCache(universe), key, rti);
// Set up methods to type tests.
// TODO(sra): Install specializations.
Rti._setAsCheckFunction(
rti, RAW_DART_FUNCTION_REF(_generalAsCheckImplementation));
Rti._setTypeCheckFunction(
rti, RAW_DART_FUNCTION_REF(_generalTypeCheckImplementation));
Rti._setIsTestFunction(
rti, RAW_DART_FUNCTION_REF(_generalIsTestImplementation));
return rti;
}
// For each kind of Rti there are three methods:
//
// * `lookupXXX` which takes the component parts and returns an existing Rti
// object if it exists.
// * `canonicalRecipeOfXXX` that returns the compositional canonical recipe
// for the proposed type.
// * `createXXX` to create the type if it does not exist.
static String _canonicalRecipeOfDynamic() => Recipe.pushDynamicString;
static String _canonicalRecipeOfVoid() => Recipe.pushVoidString;
static String _canonicalRecipeOfNever() =>
Recipe.pushNeverExtensionString + Recipe.extensionOpString;
static String _canonicalRecipeOfAny() =>
Recipe.pushAnyExtensionString + Recipe.extensionOpString;
static String _canonicalRecipeOfFutureOr(Rti baseType) =>
Rti._getCanonicalRecipe(baseType) + Recipe.wrapFutureOrString;
static Rti _lookupDynamicRti(universe) {
return _lookupTerminalRti(
universe, Rti.kindDynamic, _canonicalRecipeOfDynamic());
}
static Rti _lookupVoidRti(universe) {
return _lookupTerminalRti(universe, Rti.kindVoid, _canonicalRecipeOfVoid());
}
static Rti _lookupNeverRti(universe) {
return _lookupTerminalRti(
universe, Rti.kindNever, _canonicalRecipeOfNever());
}
static Rti _lookupAnyRti(universe) {
return _lookupTerminalRti(universe, Rti.kindAny, _canonicalRecipeOfAny());
}
static Rti _lookupTerminalRti(universe, int kind, String canonicalRecipe) {
var cache = evalCache(universe);
var probe = _cacheGet(cache, canonicalRecipe);
if (probe != null) return _castToRti(probe);
return _createTerminalRti(universe, kind, canonicalRecipe);
}
static Rti _createTerminalRti(universe, int kind, String canonicalRecipe) {
var rti = Rti.allocate();
Rti._setKind(rti, kind);
Rti._setCanonicalRecipe(rti, canonicalRecipe);
return _finishRti(universe, rti);
}
static Rti _lookupFutureOrRti(universe, Rti baseType) {
String canonicalRecipe = _canonicalRecipeOfFutureOr(baseType);
var cache = evalCache(universe);
var probe = _cacheGet(cache, canonicalRecipe);
if (probe != null) return _castToRti(probe);
return _createFutureOrRti(universe, baseType, canonicalRecipe);
}
static Rti _createFutureOrRti(
universe, Rti baseType, String canonicalRecipe) {
var rti = Rti.allocate();
Rti._setKind(rti, Rti.kindFutureOr);
Rti._setPrimary(rti, baseType);
Rti._setCanonicalRecipe(rti, canonicalRecipe);
return _finishRti(universe, rti);
}
static String _canonicalRecipeJoin(Object arguments) {
String s = '', sep = '';
int length = _Utils.arrayLength(arguments);
for (int i = 0; i < length; i++) {
Rti argument = _castToRti(_Utils.arrayAt(arguments, i));
String subrecipe = Rti._getCanonicalRecipe(argument);
s += sep + subrecipe;
sep = Recipe.separatorString;
}
return s;
}
static String _canonicalRecipeOfInterface(String name, Object arguments) {
assert(_Utils.isString(name));
String s = _Utils.asString(name);
int length = _Utils.arrayLength(arguments);
if (length != 0) {
s += Recipe.startTypeArgumentsString +
_canonicalRecipeJoin(arguments) +
Recipe.endTypeArgumentsString;
}
return s;
}
static Rti _lookupInterfaceRti(
Object universe, String name, Object arguments) {
String key = _canonicalRecipeOfInterface(name, arguments);
var cache = evalCache(universe);
var probe = _cacheGet(cache, key);
if (probe != null) return _castToRti(probe);
return _createInterfaceRti(universe, name, arguments, key);
}
static Rti _createInterfaceRti(
Object universe, String name, Object typeArguments, String key) {
var rti = Rti.allocate();
Rti._setKind(rti, Rti.kindInterface);
Rti._setPrimary(rti, name);
Rti._setRest(rti, typeArguments);
Rti._setCanonicalRecipe(rti, key);
return _finishRti(universe, rti);
}
static String _canonicalRecipeOfBinding(Rti base, Object arguments) {
String s = Rti._getCanonicalRecipe(base);
s += Recipe
.toTypeString; // TODO(sra): Omit when base encoding is Rti without ToType.
s += Recipe.startTypeArgumentsString +
_canonicalRecipeJoin(arguments) +
Recipe.endTypeArgumentsString;
return s;
}
/// [arguments] becomes owned by the created Rti.
static Rti _lookupBindingRti(Object universe, Rti base, Object arguments) {
Rti newBase = base;
var newArguments = arguments;
if (Rti._getKind(base) == Rti.kindBinding) {
newBase = Rti._getBindingBase(base);
newArguments =
_Utils.arrayConcat(Rti._getBindingArguments(base), arguments);
}
String key = _canonicalRecipeOfBinding(newBase, newArguments);
var cache = evalCache(universe);
var probe = _cacheGet(cache, key);
if (probe != null) return _castToRti(probe);
return _createBindingRti(universe, newBase, newArguments, key);
}
static Rti _createBindingRti(
Object universe, Rti base, Object arguments, String key) {
var rti = Rti.allocate();
Rti._setKind(rti, Rti.kindBinding);
Rti._setPrimary(rti, base);
Rti._setRest(rti, arguments);
Rti._setCanonicalRecipe(rti, key);
return _finishRti(universe, rti);
}
}
/// Class of static methods implementing recipe parser.
///
/// The recipe is a sequence of operations on a stack machine. The operations
/// are described below using the format
///
/// operation: stack elements before --- stack elements after
///
/// integer: --- integer-value
///
/// identifier: --- string-value
///
/// identifier-with-one-period: --- type-variable-value
///
/// Period may be in any position, including first and last e.g. `.x`.
///
/// ',': ---
///
/// Ignored. Used to separate elements.
///
/// ';': item --- ToType(item)
///
/// Used to separate elements.
///
/// '@': --- dynamicType
///
/// '~': --- voidType
///
/// '?': type --- type?
///
/// '&': 0 --- NeverType
/// '&': 1 --- anyType
///
/// Escape op-code with small integer values for encoding rare operations.
///
/// '<': --- position
///
/// Saves (pushes) position register, sets position register to end of stack.
///
/// '>': name saved-position type ... type --- name<type, ..., type>
/// '>': type saved-position type ... type --- binding(type, type, ..., type)
///
/// When first element is a String: Creates interface type from string 'name'
/// and the types pushed since the position register was last set. The types
/// are converted with a ToType operation. Restores position register to
/// previous saved value.
///
/// When first element is an Rti: Creates binding Rti wrapping the first
/// element. Binding Rtis are flattened: if the first element is a binding
/// Rti, the new binding Rti has the concatentation of the first element
/// bindings and new type.
///
///
/// The ToType operation coerces an item to an Rti. This saves encoding looking
/// up simple interface names and indexed variables.
///
/// ToType(string): Creates an interface Rti for the non-generic class.
/// ToType(integer): Indexes into the environment.
/// ToType(Rti): Same Rti
///
///
/// Notes on enviroments and indexing.
///
/// To avoid creating a binding Rti for a single function type parameter, the
/// type is passed without creating a 1-tuple object. This means that the
/// interface Rti for, say, `Map<num,dynamic>` serves as two environments with
/// different shapes. It is a class environment (K=num, V=dynamic) and a simple
/// 1-tuple environment. This is supported by index '0' refering to the whole
/// type, and '1 and '2' refering to K and V positionally:
///
/// interface("Map", [num,dynamic])
/// 0 1 2
///
/// Thus the type expression `List<K>` encodes as `List<1>` and in this
/// environment evaluates to `List<num>`. `List<Map<K,V>>` could be encoded as
/// either `List<0>` or `List<Map<1,2>>` (and in this environment evaluates to
/// `List<Map<num,dynamic>>`).
///
/// When `Map<num,dynamic>` is combined with a binding `<int,bool>` (e.g. inside
/// the instance method `Map<K,V>.cast<RK,RV>()`), '0' refers to the base object
/// of the binding, and then the numbering counts the bindings followed by the
/// class parameters.
///
/// binding(interface("Map", [num,dynamic]), [int, bool])
/// 0 3 4 1 2
///
/// Any environment can be reconstructed via a recipe. The above enviroment for
/// method `cast` can be constructed as the ground term
/// `Map<num,dynamic><int,bool>`, or (somewhat pointlessly) reconstructed via
/// `0<1,2>` or `Map<3,4><1,2>`. The ability to construct an environment
/// directly rather than via `bind` calls is used in folding sequences of `eval`
/// and `bind` calls.
///
/// While a single type parameter is passed as the type, multiple type
/// parameters are passed as a tuple. Tuples are encoded as a binding with an
/// ignored base. `dynamic` can be used as the base, giving an encoding like
/// `@<int,bool>`.
///
/// Bindings flatten, so `@<int><bool><num>` is the same as `@<int,bool,num>`.
///
/// The base of a binding does not have to have type parameters. Consider
/// `CodeUnits`, which mixes in `ListMixin<int>`. The environment inside of
/// `ListMixin.fold` (from the call `x.codeUnits.fold<bool>(...)`) would be
///
/// binding(interface("CodeUnits", []), [bool])
///
/// This can be encoded as `CodeUnits;<bool>` (note the `;` to force ToType to
/// avoid creating an interface type Rti with a single class type
/// argument). Metadata about the supertypes is used to resolve the recipe
/// `ListMixin.E` to `int`.
class _Parser {
_Parser._() {
throw UnimplementedError('_Parser is static methods only');
}
/// Creates a parser object for parsing a recipe against an environment in a
/// universe.
///
/// Marked as no-inline so the object literal is not cloned by inlining.
@pragma('dart2js:noInline')
static Object create(Object universe, Object environment, String recipe) {
return JS(
'',
'{'
'u:#,' // universe
'e:#,' // environment
'r:#,' // recipe
's:[],' // stack
'p:0,' // position of sequence start.
'}',
universe,
environment,
recipe);
}
// Field accessors for the parser.
static Object universe(Object parser) => JS('', '#.u', parser);
static Rti environment(Object parser) => JS('Rti', '#.e', parser);
static String recipe(Object parser) => JS('String', '#.r', parser);
static Object stack(Object parser) => JS('', '#.s', parser);
static int position(Object parser) => JS('int', '#.p', parser);
static void setPosition(Object parser, int p) {
JS('', '#.p = #', parser, p);
}
static int charCodeAt(String s, int i) => JS('int', '#.charCodeAt(#)', s, i);
static void push(Object stack, Object value) {
JS('', '#.push(#)', stack, value);
}
static Object pop(Object stack) => JS('', '#.pop()', stack);
static Rti parse(Object parser) {
String source = _Parser.recipe(parser);
var stack = _Parser.stack(parser);
int i = 0;
while (i < source.length) {
int ch = charCodeAt(source, i);
if (Recipe.isDigit(ch)) {
i = handleDigit(i + 1, ch, source, stack);
} else if (Recipe.isIdentifierStart(ch)) {
i = handleIdentifer(parser, i, source, stack, false);
} else if (ch == Recipe.period) {
i = handleIdentifer(parser, i, source, stack, true);
} else {
i++;
switch (ch) {
case Recipe.separator:
break;
case Recipe.toType:
push(stack,
toType(universe(parser), environment(parser), pop(stack)));
break;
case Recipe.pushDynamic:
push(stack, _Universe._lookupDynamicRti(universe(parser)));
break;
case Recipe.pushVoid:
push(stack, _Universe._lookupVoidRti(universe(parser)));
break;
case Recipe.startTypeArguments:
push(stack, position(parser));
setPosition(parser, _Utils.arrayLength(stack));
break;
case Recipe.endTypeArguments:
handleTypeArguments(parser, stack);
break;
case Recipe.extensionOp:
handleExtendedOperations(parser, stack);
break;
case Recipe.wrapFutureOr:
var u = universe(parser);
push(
stack,
_Universe._lookupFutureOrRti(
u, toType(u, environment(parser), pop(stack))));
break;
default:
JS('', 'throw "Bad character " + #', ch);
}
}
}
Object item = pop(stack);
return toType(universe(parser), environment(parser), item);
}
static int handleDigit(int i, int digit, String source, Object stack) {
int value = Recipe.digitValue(digit);
for (; i < source.length; i++) {
int ch = charCodeAt(source, i);
if (!Recipe.isDigit(ch)) break;
value = value * 10 + Recipe.digitValue(ch);
}
push(stack, value);
return i;
}
static int handleIdentifer(
Object parser, int start, String source, Object stack, bool hasPeriod) {
int i = start + 1;
for (; i < source.length; i++) {
int ch = charCodeAt(source, i);
if (ch == Recipe.period) {
if (hasPeriod) break;
hasPeriod = true;
} else if (Recipe.isIdentifierStart(ch) || Recipe.isDigit(ch)) {
// Accept.
} else {
break;
}
}
String string = _Utils.substring(source, start, i);
if (hasPeriod) {
push(
stack,
_Universe.evalTypeVariable(
universe(parser), environment(parser), string));
} else {
push(stack, string);
}
return i;
}
static void handleTypeArguments(Object parser, Object stack) {
var universe = _Parser.universe(parser);
var arguments = collectArray(parser, stack);
var head = pop(stack);
if (_Utils.isString(head)) {
String name = _Utils.asString(head);
push(stack, _Universe._lookupInterfaceRti(universe, name, arguments));
} else {
Rti base = toType(universe, environment(parser), head);
push(stack, _Universe._lookupBindingRti(universe, base, arguments));
}
}
static void handleExtendedOperations(Object parser, Object stack) {
var top = pop(stack);
if (0 == top) {
push(stack, _Universe._lookupNeverRti(universe(parser)));
return;
}
if (1 == top) {
push(stack, _Universe._lookupAnyRti(universe(parser)));
return;
}
throw AssertionError('Unexpected extended operation $top');
}
static Object collectArray(Object parser, Object stack) {
var array = _Utils.arraySplice(stack, position(parser));
toTypes(_Parser.universe(parser), environment(parser), array);
setPosition(parser, _Utils.asInt(pop(stack)));
return array;
}
/// Coerce a stack item into an Rti object. Strings are converted to interface
/// types, integers are looked up in the type environment.
static Rti toType(Object universe, Rti environment, Object item) {
if (_Utils.isString(item)) {
String name = _Utils.asString(item);
// TODO(sra): Compile this out for minified code.
if ('dynamic' == name) {
return _Universe._lookupDynamicRti(universe);
}
return _Universe._lookupInterfaceRti(
universe, name, _Universe.sharedEmptyArray(universe));
} else if (_Utils.isNum(item)) {
return _Parser.indexToType(universe, environment, _Utils.asInt(item));
} else {
return _castToRti(item);
}
}
static void toTypes(Object universe, Rti environment, Object items) {
int length = _Utils.arrayLength(items);
for (int i = 0; i < length; i++) {
var item = _Utils.arrayAt(items, i);
var type = toType(universe, environment, item);
_Utils.arraySetAt(items, i, type);
}
}
static Rti indexToType(Object universe, Rti environment, int index) {
int kind = Rti._getKind(environment);