-
Notifications
You must be signed in to change notification settings - Fork 2
/
static_types.py
1223 lines (1022 loc) · 44.9 KB
/
static_types.py
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
# ecmaspeak-py/static_types.py:
# Code to handle static types for static type analysis.
#
# Copyright (C) 2023 J. Michael Dyck <[email protected]>
import re
from dataclasses import dataclass
from typing import Tuple, FrozenSet
from collections import defaultdict
import shared
from shared import stderr, spec
from Pseudocode_Parser import ANode
import records
# Before importing this module,
# you must ensure that a 'Spec' is loaded
# via shared.spec.restore().
_split_types_f = shared.open_for_output('split_types')
_subtype_memo = {}
_split_memo = {}
class Type():
def set_of_types(self):
return self.member_types if isinstance(self, UnionType) else frozenset([self])
def __or__(A, B):
if A == B: return A
u = union_of_types([A, B])
# print(A, '|', B, '=', u)
return u
def __lt__(A, B):
return A.sort_key() < B.sort_key()
# ... so that we can sort instances of classes derived from Type,
# simply by having them define a `sort_key` method.
#
# There are only a couple places where we do such sorting:
# - in the semantics for {NUM_COMPARISON},
# which generates multiple complaints due to STA's lack of smartness,
# and we want those complaints to be in a consistent order,
# to prevent spurious diffs.
# Eventually, STA may be smart enough that those complaints go away.
# - in the semantics for {DOTTING},
# where again, we want error messages to appear in a consistent order.
# -----------------------------------------------------
# @memoize()
def is_a_subtype_of_or_equal_to(A, B):
if (A,B) in _subtype_memo:
return _subtype_memo[(A,B)]
# No speed-up?
if B == T_Top_: return True
A_members = A.set_of_types()
B_members = B.set_of_types()
if T_TBD in A_members or T_TBD in B_members:
result = False
elif A_members <= B_members:
result = True
# elif A_members > B_members:
# result = False
# No, that assumes that A is 'reduced'.
# But consider A = T_Normal | T_Reference_Record
# and B = T_Normal
else:
# A is a subtype of B iff every A_member is a subtype of B.
result = all(
# and an A_member is a subtype of B
# iff it is a subtype of some B_member
any(
member_is_a_subtype_or_equal(A_member, B_member)
for B_member in B_members
)
for A_member in A_members
)
if 0:
print(
"SUBTYPING:",
A,
"is" if result else "is not",
"a subtype of",
B
)
_subtype_memo[(A,B)] = result
return result
# -----------------------------------------------------
def split_by(A, B):
# Return a pair of types that partition A
# (i.e., two disjoint types whose union is A):
# one that is a subtype of (or equal to) B,
# and one that is outside of B.
# (Either can be T_0.)
# if A == T_TBD and B == ListType(T_String): pdb.set_trace()
A_members = A.set_of_types()
B_members = B.set_of_types()
if (A,B) in _split_memo:
return _split_memo[(A,B)]
A_memtypes = A.set_of_types()
B_memtypes = B.set_of_types()
# A few cases that can be handled quickly:
if A_memtypes == B_memtypes:
inside_B = A # or B
outside_B = T_0
# elif A_memtypes <= B_memtypes:
# inside_B = A
# outside_B = T_0
#
# elif B_memtypes <= A_memtypes:
# inside_B = B
# outside_B = union_of_types(A_memtypes - B_memtypes)
#
# e.g., if A == T_Completion_Record | T_throw_
# and B == T_Completion_Record
# then the above will say
# inside_B == T_Completion_Record
# outside_B == T_throw_
#
# Now, you'd be right to point out that T_Completion_Record | T_throw_
# shouldn't exist (it should just be T_Completion_Record).
# TODO.
else:
# The general case:
inside_B = set()
outside_B = set()
def recurse(A_subtypes, B_subtypes):
for a in A_subtypes:
assert isinstance(a, Type)
# Treat T_TBD like Top
if a == T_TBD: a = T_Top_ # assert 0
if a == T_Normal and B == T_Completion_Record:
# A Completion Record is a Record, and Records are 'Normal' values,
# and so this would seem to be asking to split T_Normal
# into T_Completion_Record vs all the 'Normal' stypes other than T_Completion_Record.
# However, in practice, that's not what's wanted.
# Instead, we want to say that T_Normal and T_Completion_Record are completely disjoint.
outside_B.add(a)
elif a.is_a_subtype_of_or_equal_to(B):
inside_B.add(a)
else:
# get a list of the B_subtypes that are subtypes of a
bs_within_a = [
b
for b in B_subtypes
if b.is_a_subtype_of_or_equal_to(a)
]
if bs_within_a:
# break down `a`
if a == T_List:
if len(bs_within_a) == 1:
[bwa] = bs_within_a
if isinstance(bwa, ListType):
inside_B.add(bwa)
outside_B.add(ListType(T_other_))
# pdb.set_trace()
else:
assert 0
else:
assert 0
elif isinstance(a, ListType):
if a == ListType(T_String | T_Undefined) and bs_within_a == [ListType(T_String)]:
inside_B.add(ListType(T_String))
outside_B.add(ListType(T_String | T_Undefined))
else:
assert 0
elif isinstance(a, RecordType):
# TODO: be more general
ts_in_CR = [
T_normal_completion,
T_break_completion,
T_continue_completion,
T_return_completion,
T_throw_completion
]
if a == T_Completion_Record and set(bs_within_a) <= set(ts_in_CR):
for t in ts_in_CR:
if t in bs_within_a:
inside_B.add(t)
else:
outside_B.add(t)
else:
assert 0
else:
tnode = _tnode_for_type_[a]
a_imm_subtypes = [child.type for child in tnode.children]
recurse(a_imm_subtypes, bs_within_a)
else:
# no B_subtype is within `a`
# so `a` must be disjoint from B
outside_B.add(a)
recurse(A_memtypes, B_memtypes)
inside_B = union_of_types(inside_B)
outside_B = union_of_types(outside_B)
print("%s :: %s ===> %s /// %s" %
(A, B, outside_B, inside_B),
file=_split_types_f)
result = (inside_B, outside_B)
_split_memo[(A,B)] = result
return result
def member_is_a_subtype_or_equal(A, B):
assert not isinstance(A, UnionType); assert A != T_TBD
assert not isinstance(B, UnionType); assert B != T_TBD
if A == B: return True
if isinstance(A, HierType):
if isinstance(B, HierType):
A_tnode = _tnode_for_type_[A]
B_tnode = _tnode_for_type_[B]
if A_tnode.level < B_tnode.level:
# A is higher in the hierarchy than B
# (not necessarily an ancestor of B, but at a higher level).
return False
elif A_tnode.level == B_tnode.level:
# They're at the same level in the hierarchy.
# But we've already tested them for equality,
# so they must be siblings/cousins.
return False
elif A_tnode.level > B_tnode.level:
# A is at a lower level than B in the hierarchy.
# So it might be a subtype.
n_levels_diff = A_tnode.level - B_tnode.level
tnode = A_tnode
for i in range(n_levels_diff): tnode = tnode.parent
assert tnode.level == B_tnode.level
return (tnode is B_tnode)
else:
assert 0, "can't happen"
else:
# e.g., is Foo a subtype of List of Foo?
# I don't think there's much need to say it is.
return False
elif isinstance(A, ListType):
if isinstance(B, ListType):
return (A.element_type.is_a_subtype_of_or_equal_to(B.element_type))
elif isinstance(B, HierType):
return (T_List.is_a_subtype_of_or_equal_to(B))
elif isinstance(B, RecordType):
return False
else:
assert 0, (A, B)
elif isinstance(A, RecordType):
if isinstance(B, RecordType):
if A.schema_name == B.schema_name == '':
A_field_dict = dict(A.fields_info)
B_field_dict = dict(B.fields_info)
if set(A_field_dict.keys()) == set(B_field_dict.keys()):
for (field_name, A_field_type) in A_field_dict.items():
B_field_type = B_field_dict[field_name]
if A_field_type.is_a_subtype_of_or_equal_to(B_field_type):
pass
else:
return False
return True
else:
assert 0
elif A.schema_name == '' and B.schema_name != '':
return False
elif A.schema_name != '' and B.schema_name == '':
return False
assert A.schema_name != ''
assert B.schema_name != ''
# TODO: normalize schema names
A_record_schema = spec.RecordSchema_for_name_[A.schema_name]
B_record_schema = spec.RecordSchema_for_name_[B.schema_name]
for ars in A_record_schema.self_and_supers():
if ars is B_record_schema:
# A_record_schema is the same as B_record_schema,
# or is a descendant schema of it.
break
else:
# A's schema isn't a descendant of B's
return False
# For any field that B constrains,
# A must also constrain it, and constrain it the same or moreso.
# (A can also have additional fields, that's fine.)
# We assume that a RecordType never has a do-nothing field-constraint.
# For example, consider a Reference Record's [[Strict]] field, declared to be a Boolean.
# If A and B are both RecordTypes representing Reference Records,
# and B.fields_info consists of a 'constraint' that [[Strict]] is Boolean
# and A.fields_info is empty,
# then they denote the same type,
# but this code would incorrectly conclude that A isn't a subtype of or equal to B.
for (B_field_name, B_field_type) in B.fields_info:
# Linear search seems inefficient,
# but in practice, the two fields_info are pretty short.
for (A_field_name, A_field_type) in A.fields_info:
if A_field_name == B_field_name:
if A_field_type.is_a_subtype_of_or_equal_to(B_field_type):
# good so far
break
else:
return False
else:
# Didn't break from the for-loop,
# i.e., didn't find a matching field_name in A.
# i.e., A.fields_info doesn't constrain {B_field_name},
# so A isn't a subtype of B.
# (Does this ever happen in practice?)
return False
return True
elif isinstance(B, HierType):
if T_Record.is_a_subtype_of_or_equal_to(B):
return True
elif A.schema_name == 'Completion Record' and B == T_Completion_Record:
return True
else:
return False
elif isinstance(B, ListType):
return False
elif isinstance(B, ProcType):
return False
else:
assert 0, (A, B)
elif isinstance(A, ProcType):
if isinstance(B, ProcType):
assert len(A.param_types) == len(B.param_types)
return (
(
bpt.is_a_subtype_of_or_equal_to(apt) # contra-variance
for (apt, bpt) in zip(A.param_types, B.param_types)
)
and
A.return_type.is_a_subtype_of_or_equal_to(B.return_type)
)
elif isinstance(B, HierType):
return (T_proc_.is_a_subtype_of_or_equal_to(B))
elif isinstance(B, ListType):
return False
elif isinstance(B, RecordType):
return False
else:
assert 0, (A, B)
else:
assert 0, (A, B)
# --------------------------------------------------------------------------
@dataclass(frozen = True)
class TBDType(Type):
pass
def __str__(self): return 'TBD'
def unparse(self, parenthesuze=False): return 'TBD'
@dataclass(frozen = True)
class HierType(Type):
name: str
# The instances of this class represent static types that form a hierarchy.
# Specifically, for any two such types A and B,
# it must be the case that either:
# - A == B,
# - A is a subtype of B,
# - B is a subtype of A, or
# - A and B are disjoint.
#
# (Note that this condition *doesn't* hold for
# UnionTypes, ProcTypes, etc.)
def __post_init__(self):
assert isinstance(self.name, str)
assert re.fullmatch(r'[\w -]+', self.name), self.name
assert not self.name.startswith('a '), self.name
def sort_key(self):
return ('HierType', self.name)
def __str__(self):
if self.name.endswith('_completion'):
return self.name.removesuffix('completion')
return self.name
def unparse(self, parenthesize=False):
if self.name.startswith('PTN_'):
x = 'Parse Node for |%s|' % self.name.replace('PTN_','')
if parenthesize: x = '(%s)' % x
return x
else:
return self.__str__()
@dataclass(frozen = True)
class ListType(Type):
element_type: Type
def __post_init__(self):
assert isinstance(self.element_type, Type)
def sort_key(self):
return ('ListType', self.element_type)
def __str__(self): return "List of %s" % str(self.element_type)
def unparse(self, _=False): return "List of %s" % self.element_type.unparse(True)
@dataclass(frozen = True)
class RecordType(Type):
schema_name: str
fields_info: Tuple[Tuple[str, Type]]
def __post_init__(self):
assert isinstance(self.schema_name, str)
assert isinstance(self.fields_info, tuple)
for fi in self.fields_info:
assert isinstance(fi, tuple)
(field_name, field_type) = fi
assert isinstance(field_name, str)
assert isinstance(field_type, Type)
def sort_key(self):
return ('RecordType', self.schema_name, self.fields_info)
def type_of_field_named(self, field_name):
for (f_name, f_type) in self.fields_info:
if f_name == field_name:
return f_type
# To get here, we didn't 'return' above, which means that
# {field_name} didn't appear in {self.fields_info}
# which you might think means that (the type represented by) {self}
# only includes records that don't have a field by that name.
# On the contrary, it *does* include records with such a field,
# it just doesn't constrain the type of such a field
# (any more than {self}'s super-schemas might).
if self.schema_name == 'Completion Record':
# For some of the fields,
# the 'declared' types aren't as precise as they could be.
if field_name == '[[Type]]':
# In practice, this would have been handled by the for-loop above.
# But I suppose it could happen.
return T_tilde_break_ | T_tilde_continue_ | T_tilde_normal_ | T_tilde_return_ | T_tilde_throw_
else:
assert field_name in ['[[Value]]', '[[Target]]']
if self.fields_info == ():
# {self} is just T_Completion_Record,
# so the types of its fields are the most general
return {
'[[Value]]' : T_Normal,
'[[Target]]': T_String | T_tilde_empty_,
}[field_name]
else:
(Type_field_name, Type_field_stype) = self.fields_info[0]
assert Type_field_name == '[[Type]]'
assert isinstance(Type_field_stype, HierType)
if Type_field_stype == T_tilde_normal_:
return {
'[[Value]]' : T_Normal,
'[[Target]]': T_tilde_empty_,
}[field_name]
elif Type_field_stype in [T_tilde_return_, T_tilde_throw_]:
return {
'[[Value]]' : T_Tangible_ | T_tilde_empty_,
'[[Target]]': T_tilde_empty_,
}[field_name]
elif Type_field_stype in [T_tilde_break_, T_tilde_continue_]:
return {
'[[Value]]' : T_Tangible_ | T_tilde_empty_,
'[[Target]]': T_String | T_tilde_empty_,
}[field_name]
else:
assert 0
# ---------------------------
assert 0
# code that we might need in future:
record_schema = spec.RecordSchema_for_name_[self.schema_name]
for ars in record_schema.self_and_supers():
if field_name in ars.addl_field_decls:
field_decl = ars.addl_field_decls[field_name]
field_decl.value_description
return T_Normal
def __str__(self):
if self == T_Completion_Record: return 'Completion Record'
if self.schema_name == 'Completion Record' and len(self.fields_info) in [1,2]:
fields_d = dict(self.fields_info)
Type_field_stype = fields_d['[[Type]]']
prefix = Type_field_stype.name.replace('tilde_', '')
if len(self.fields_info) == 1:
suffix = ''
else:
Value_field_stype = fields_d['[[Value]]']
suffix = f"({Value_field_stype})"
return f"{prefix}{suffix}"
field_types_str = ', '.join(
f"{f_name}: {f_type}"
for (f_name, f_type) in self.fields_info
)
return f"RecordType({self.schema_name!r}, {field_types_str})"
def unparse(self, _=False):
return str(self)
def s_dot_field(lhs_type, field_name):
if isinstance(lhs_type, RecordType):
return lhs_type.type_of_field_named(field_name)
elif isinstance(lhs_type, UnionType):
field_types = []
for memtype in lhs_type.set_of_types():
assert isinstance(memtype, RecordType)
field_types.append(memtype.type_of_field_named(field_name))
return union_of_types(field_types)
else:
assert 0
@dataclass(frozen = True)
class ProcType(Type):
param_types: Tuple[Type]
return_type: Type
def __post_init__(self):
assert isinstance(self.param_types, tuple)
for pt in self.param_types: assert isinstance(pt, Type)
assert isinstance(self.return_type, Type)
def sort_key(self):
return ('ProcType', self.param_types, self.return_type)
def __str__(self):
if self == T_MatcherContinuation:
return "MatcherContinuation"
elif self == T_Matcher:
return "Matcher"
elif self == T_ReadModifyWrite_modification_closure:
return "ReadModifyWrite_modification_closure"
elif self == T_RegExpMatcher_:
return "RegExpMatcher_"
else:
return "{(%s) -> %s}" % (
', '.join(str(pt) for pt in self.param_types),
self.return_type
)
def unparse(self, _=False): return str(self)
@dataclass(frozen = True)
class UnionType(Type):
# A union of (non-union) types.
# Must satisfy the constraint that no member-type
# is a subtype or supertype of any other member-type.
# (XXX: Should check that in __new__.)
member_types: FrozenSet[Type]
def __post_init__(self):
assert len(self.member_types) != 1
for mt in self.member_types:
assert isinstance(mt, Type)
assert not isinstance(mt, UnionType)
def sort_key(self):
return ('UnionType', self.member_types)
def __str__(self): return "(%s)" % ' | '.join(sorted(map(str, self.member_types)))
def unparse(self, parenthesize=False):
if T_not_passed in self.member_types:
# This only makes sense for a top-level type,
# but I don't think it'll occur anywhere else.
prefix = '(optional) '
member_types = set(self.member_types)
member_types.remove(T_not_passed)
else:
prefix = ''
member_types = self.member_types
if self == T_abrupt_completion: return 'Abrupt'
x = ' | '.join(sorted(
member_type.unparse()
for member_type in member_types
))
if parenthesize: x = '(' + x + ')'
return prefix + x
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# memoize
def union_of_types(types):
if len(types) == 0: return T_0
types1 = set(types)
if len(types1) == 1: return types1.pop()
types1.discard(T_0)
if len(types1) == 1: return types1.pop()
types1.discard(T_TBD)
if len(types1) == 1: return types1.pop()
# ----------------------------
memtypes = set()
for t in types1:
if isinstance(t, UnionType):
for mt in t.member_types:
assert not isinstance(mt, UnionType)
memtypes.add(mt)
else:
memtypes.add(t)
memtypes.discard(T_TBD)
assert len(memtypes) > 0
list_memtypes = []
record_memtypes = []
proc_memtypes = []
hier_memtypes = []
for mt in memtypes:
if mt == T_List or isinstance(mt, ListType):
list_memtypes.append(mt)
elif isinstance(mt, RecordType):
record_memtypes.append(mt)
elif isinstance(mt, ProcType):
# There isn't a T_Proc, or a HierType that resolves to a ProcType.
proc_memtypes.append(mt)
elif isinstance(mt, HierType):
hier_memtypes.append(mt)
else:
assert 0, mt
result_memtypes = (
_union_of_list_memtypes(list_memtypes)
+ _union_of_record_memtypes(record_memtypes)
+ _union_of_proc_memtypes(proc_memtypes)
+ _union_of_hier_memtypes(hier_memtypes)
)
assert result_memtypes
if len(result_memtypes) == 1:
result = list(result_memtypes)[0]
else:
result = UnionType(frozenset(result_memtypes))
# print("union of", ', '.join(str(t) for t in types), " = ", result)
return result
# ------------------------------------------------------------------------------
def _union_of_list_memtypes(list_memtypes):
if len(list_memtypes) <= 1:
return list_memtypes
if T_List in list_memtypes:
# For the purposes of type-union,
# T_List is basically "List of TBD",
# and because len(list_memtypes) >= 2,
# there must be a more specfic list-type in the set,
# so ignore T_List.
list_memtypes.remove(T_List)
if len(list_memtypes) == 1:
return list_memtypes
t = ListType(
union_of_types([
mt.element_type
for mt in list_memtypes
])
)
return [t]
# ------------------------------------------------------------------------------
def _union_of_record_memtypes(record_memtypes):
if len(record_memtypes) <= 1:
return record_memtypes
memtypes_with_schema_name_ = defaultdict(list)
for memtype in record_memtypes:
assert isinstance(memtype, RecordType)
memtypes_with_schema_name_[memtype.schema_name].append(memtype)
result_memtypes = []
for (schema_name, memtypes) in memtypes_with_schema_name_.items():
assert len(memtypes) > 0
if len(memtypes) == 1:
result_memtypes.extend(memtypes)
else:
if schema_name == '':
result_memtypes.extend(memtypes)
#XXX for now
elif schema_name == 'Completion Record':
crtypes_with_Type_stype_ = defaultdict(list)
for crtype in memtypes:
if crtype.fields_info == ():
# {crtype} is a supertype (or equal to) all others in {memtypes}
result_memtypes.append(crtype)
break
else:
(Type_field_name, Type_field_stype) = crtype.fields_info[0]
assert Type_field_name == '[[Type]]'
if isinstance(Type_field_stype, HierType):
crtypes_with_Type_stype_[Type_field_stype].append(crtype)
elif isinstance(Type_field_stype, UnionType):
for sub_tfs in Type_field_stype.member_types:
assert isinstance(sub_tfs, HierType)
sub_crtype = RecordType(
crtype.schema_name,
(('[[Type]]', sub_tfs),)
+
crtype.fields_info[1:]
)
crtypes_with_Type_stype_[sub_tfs].append(sub_crtype)
else:
assert 0
else:
# no break from above loop,
# i.e. no crtype with this schema_name had an empty fields_info
# i.e. each crtype with this schema_name has a [[Type]] field
# So we consider them separately depending on the value (stype) of the [[Type]] field.
for (Type_field_stype, crtypes) in crtypes_with_Type_stype_.items():
if len(crtypes) == 1:
result_memtypes.extend(crtypes)
else:
# Okay, so we have multiple crtypes,
# all with the same schema_name and the same value for [[Type]] field.
# But they might or might not have a [[Value]] field.
# (e.g., T_throw_completion(TypeError) vs T_throw_completion)
Value_field_stypes = []
for crtype in crtypes:
assert len(crtype.fields_info) >= 1
if len(crtype.fields_info) == 1:
# {crtype} is a supertype (or equal to) all others in {crtypes}
result_memtypes.append(crtype)
break
else:
(Value_field_name, Value_field_stype) = crtype.fields_info[1]
assert Value_field_name == '[[Value]]'
Value_field_stypes.append(Value_field_stype)
else:
# no break from above loop,
# i.e. no crtype in {crtypes} had just a [[Type]] field
# i.e. each crtype in {crtypes} has both [[Type]] and [[Value]] field
result_memtypes.append(
RecordType(schema_name, (
('[[Type]]', Type_field_stype),
('[[Value]]', union_of_types(Value_field_stypes)),
)
)
)
else:
assert 0, schema_name
return result_memtypes
# ------------------------------------------------------------------------------
def _union_of_proc_memtypes(proc_memtypes):
if len(proc_memtypes) <= 1:
return proc_memtypes
assert 0
# ------------------------------------------------------------------------------
def _union_of_hier_memtypes(memtypes):
for memtype in memtypes:
assert isinstance(memtype, HierType)
if len(memtypes) <= 1:
return memtypes
memtypes_set = set(memtypes)
def recurse(tnode):
if not tnode.children: return
if tnode.type in memtypes_set:
# {tnode.type} subsumes all subtypes,
# so since {tnode.type} is in {memtypes_set}
# remove any of its subtypes that are in {memtypes_set}.
rm_all_descendants_of(tnode)
else:
# {tnode.type} isn't in memtypes_set,
# but if all of its children are,
# then we can replace them with {tnode.type}
for child_tnode in tnode.children:
recurse(child_tnode)
if all(
child_tnode.type in memtypes_set
for child_tnode in tnode.children
):
stderr("! replacing ...")
for child_tnode in tnode.children:
stderr("! ", child_tnode.type)
memtypes_set.remove(child_tnode.type)
stderr("! with", tnode.type)
memtypes_set.add(tnode.type)
def rm_all_descendants_of(tnode):
for child_tnode in tnode.children:
memtypes_set.discard(child_tnode.type)
rm_all_descendants_of(child_tnode)
recurse(troot)
return list(memtypes_set)
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# instances of HierType
_named_type_hierarchy = {
'Top_': {
'Absent': {
'not_passed': {}, # for an optional parameter
'not_in_node': {}, # for an optional child of a node
'not_set': {}, # for a metavariable that might not be initialized
'not_returned': {}, # for when control falls off the end of an operation
},
'Normal': {
'Tangible_': {
'Primitive': {
'Undefined': {},
'Null': {},
'Boolean': {},
'String': {},
'Symbol': {},
'Number': {
'FiniteNumber_' : {
'IntegralNumber_': {},
'NonIntegralFiniteNumber_' : {}
},
'NegInfinityNumber_': {},
'PosInfinityNumber_': {},
'NaN_Number_': {},
},
'BigInt': {},
},
'Object': {
'Error': {
'AggregateError': {},
'ReferenceError': {},
'SyntaxError': {},
'TypeError': {},
'RangeError': {},
'other_Error_': {},
},
# 'Proxy': {},
# 'RegExp': {},
'ArrayBuffer_object_': {},
'Array_object_': {},
'AsyncGenerator_object_': {},
'DataView_object_': {},
'FinalizationRegistry_object_': {},
'Generator_object_': {},
'Iterator_object_': {},
'IteratorResult_object_': {},
'Promise_object_': {},
'SharedArrayBuffer_object_': {},
'String_exotic_object_': {},
'TypedArray_object_': {},
'WeakMap_object_': {},
'WeakRef_object_': {},
'WeakSet_object_': {},
'function_object_': {
'ECMAScript_function_object_': {},
'built_in_function_object_': {},
# The spec allows a built-in function to be implemented
# as an ECMAScript function object or not.
# I.e., you can have an object that is *both*
# an ECMAScript function object and a built-in function object.
#
# However, the spec never focuses on such an object,
# or on an ES function that isn't a built-in function,
# or on a built-in function that isn't an ES function.
#
# So for the purposes of static analysis,
# it works to treat them as mutually exclusive.
#
# (There's editorial desire to eliminate that choice,
# or at least, eliminate the spec's mentioning/stressing that choice,
# at which point I can probably delete this comment.
'constructor_object_': {}, # XXX This is actually orthogonal to ECMAScript/built-in/Proxy/Bound/other
'Proxy_exotic_object_': {},
'bound_function_exotic_object_': {},
'other_function_object_': {},
},
'module_namespace_exotic_object_': {},
'other_Object_': {},
},
},
'Intangible_': {
'CharSet': {},
'CharSetElement': {},
'Data Block': {},
'event_pair_': {},
'IEEE_binary32_': {},
'IEEE_binary64_': {},
'List': {},
'ExtendedMathReal_': {
'MathReal_': {
'MathInteger_': {},
'MathOther_': {},
},
'MathPosInfinity_': {},
'MathNegInfinity_': {},
},
'Parse Node' : {
'PTN_ForBinding': {},
'PTN_Script': {},
'PTN_Pattern': {},
'PTN_Template_Literal': {},
},
'Private Name': {},
'Record': {
'Intrinsics Record': {},
},
# 'Reference': {}, # 2085
'Relation': {},
'Set': {},
'Shared Data Block': {},
# The name suggests a subtype of Data Block,
# but doesn't seem to be treated that way.
'SlotName_': {},
'TypedArray_element_type': {
# children are filled in via code
},
'agent_signifier_' : {},
'alg_steps': {},
'code_point_': {},
'code_unit_' : {},
'completion_kind_': {},
'execution context': {},
'grammar_symbol_': {},
'host_defined_': {},
'proc_': {},