1
+ from collections import defaultdict
2
+
1
3
import src .ir .ast as ast
2
4
import src .ir .typescript_ast as ts_ast
3
5
import src .ir .builtins as bt
@@ -96,7 +98,7 @@ def update_add_node_to_parent(self):
96
98
97
99
def get_dynamic_types (self , gen_object ):
98
100
return [
99
- # union_types.get_union_type(gen_object),
101
+ union_types .get_union_type (gen_object ),
100
102
]
101
103
102
104
def get_constant_candidates (self , constants ):
@@ -430,6 +432,9 @@ def __init__(self, types, name="UnionType", primitive=False):
430
432
def get_types (self ):
431
433
return self .types
432
434
435
+ def is_combound (self ):
436
+ return True
437
+
433
438
@two_way_subtyping
434
439
def is_subtype (self , other ):
435
440
if isinstance (other , UnionType ):
@@ -439,9 +444,53 @@ def is_subtype(self, other):
439
444
def dynamic_subtyping (self , other ):
440
445
return other in set (self .types )
441
446
447
+ def substitute_type_args (self , type_map ,
448
+ cond = lambda t : t .has_type_variables ()):
449
+ new_types = []
450
+ for t in self .types :
451
+ new_t = (t .substitute_type_args (type_map , cond )
452
+ if t .has_type_variables ()
453
+ else t )
454
+ new_types .append (new_t )
455
+ return UnionType (new_types )
456
+
442
457
def has_type_variables (self ):
443
458
return any (t .has_type_variables () for t in self .types )
444
459
460
+ def get_type_variables (self , factory ):
461
+ # This function actually returns a dict of the enclosing type variables
462
+ # along with the set of their bounds.
463
+ type_vars = defaultdict (set )
464
+ for t in self .types :
465
+ if t .is_type_var ():
466
+ type_vars [t ].add (
467
+ t .get_bound_rec (factory ))
468
+ elif t .is_combound () or t .is_wildcard ():
469
+ for k , v in t .get_type_variables (factory ).items ():
470
+ type_vars [k ].update (v )
471
+ else :
472
+ continue
473
+ return type_vars
474
+
475
+ def to_variance_free (self , type_var_map = None ):
476
+ new_types = []
477
+ for t in self .types :
478
+ new_types .append (t .to_variance_free (type_var_map )
479
+ if t .is_combound ()
480
+ else t )
481
+ return UnionType (new_types )
482
+
483
+ def to_type_variable_free (self , factory ):
484
+ # We translate a union type that contains
485
+ # type variables into a parameterized type that is
486
+ # type variable free.
487
+ new_types = []
488
+ for t in self .types :
489
+ new_types .append (t .to_type_variable_free (factory )
490
+ if t .is_combound ()
491
+ else t )
492
+ return UnionType (new_types )
493
+
445
494
def get_name (self ):
446
495
return self .name
447
496
0 commit comments