@@ -94,10 +94,29 @@ def update_add_node_to_parent(self):
94
94
ts_ast .TypeAliasDeclaration : add_type_alias ,
95
95
}
96
96
97
- def get_constant_candidates (self ):
97
+ def get_constant_candidates (self , gen_object , constants ):
98
+ """ Updates the constant candidates of the generator
99
+ with the type-constant pairs for language-specific features.
100
+
101
+ Args:
102
+ gen_object: The generator instance
103
+ constants: The dictionary of constant candidates
104
+ at the time of the method call
105
+ Returns:
106
+ A dictionary where the keys are strings of type names and
107
+ values are functions that return the appropriate constant
108
+ node for the type.
109
+
110
+ The constants dictionary is updated at the generator-side
111
+ with the method's returned key-value pairs.
112
+
113
+ This method is called at src.ir.generator.get_generators()
114
+
115
+ """
98
116
return {
99
117
"NumberLiteralType" : lambda etype : ast .IntegerConstant (etype .literal , NumberLiteralType ),
100
118
"StringLiteralType" : lambda etype : ast .StringConstant (etype .literal ),
119
+ "UnionType" : lambda etype : union_types .get_union_constant (etype , constants ),
101
120
}
102
121
103
122
@@ -471,6 +490,21 @@ def get_union_type(self, gen_object):
471
490
return ut .random .choice (self .unions )
472
491
return self .gen_union_type ()
473
492
493
+ def get_union_constant (self , utype , constants ):
494
+ type_candidates = [t for t in utype .types if t .name in constants ]
495
+ """ A union type can have types like 'Object' or 'undefined'
496
+ as part of its union, which however do not have a respective
497
+ constant equivalent.
498
+
499
+ Hence, we only consider types that we can generate a constant
500
+ from. If there is none, we revert to a bottom constant.
501
+
502
+ """
503
+ if len (type_candidates ) == 0 :
504
+ return ast .BottomConstant (utype .types [0 ])
505
+ t = ut .random .choice (type_candidates )
506
+ return constants [t .name ](t )
507
+
474
508
475
509
class ArrayType (tp .TypeConstructor , ObjectType ):
476
510
def __init__ (self , name = "Array" ):
0 commit comments