6 // Can nil be used as this type?
7 // It's safe for nil to be used as any type--zero is the initial
8 // value for all variables--but we want to avoid weirdness
9 // like using nil as a bool or number.
13 return op
== TypeString
||
26 // Is t1 a subtype of t2?
27 // Remember that nil (didn't type check) is different from typenil().
29 issubtype(Type
*t1
, Type
*t2
)
33 if(t1
== nil
|| t2
== nil
)
38 // TypeAny is used when there is no instance of an actual type.
39 // For example, list [] has type list TypeAny. Essentially TypeAny
40 // is a giant wildcard, taking on any type imaginable at any time.
41 if(t1
->op
== TypeAny
|| t2
->op
== TypeAny
)
44 return nillable(t2
->op
);
47 if(t1
->op
== TypeStruct
){
48 for(; t1
; t1
=t1
->super
)
53 if(t1
->op
== TypeGram
){
56 if(t1
->gram
!= t2
->gram
)
58 if(t2
->sym
&& t1
->sym
!= t2
->sym
)
64 // t2 must have all of t1's names,
65 // unless it is gram+?
66 // The lists are in sorted order.
67 if(t2
->gramx
&& !t2
->gramx
->tl
&& t2
->gramx
->hd
== N("?"))
70 for(l2
=t2
->gramx
; l2
; l2
=l2
->tl
)
71 if(l1
&& l1
->hd
== l2
->hd
)
80 // Take minimum of two types.
81 // Remember that nil (no constraint) is different from typenil().
82 // Lattice and type theory weenies call this the meet or infimum.
84 typemin(Type
*t1
, Type
*t2
, Type
**min
)
95 // A constraint t1 says that the desired type needs to be
96 // t1 or a descendant (subtype) of t1 in the type hiearchy.
97 // Since the type hierarchy is a tree, the only way to satisfy
98 // constraints t1 and t2 is if t1 is a subtype of t2 or vice versa.
100 // Actually the hierarchy is not a tree--the expression "nil"
101 // can be a string or a list or an array, which makes it a dag--
102 // but since nil is itself a minimum type, that doesn't break anything.
104 // XXX Do we have to treat the grammar types specially in typemin?
106 if(issubtype(t1
, t2
)){
110 if(issubtype(t2
, t1
)){
120 return t
->gramx
&& t
->gramx
->tl
== nil
&& t
->gramx
->hd
== N("?");
123 // Take maximum of two types. t1==nil or t2==nil means no constraint.
124 // Remember that nil is different from typenil().
125 // Lattice and type theory weenies call this the join or supremum.
127 _typemax(Type
*t1
, Type
*t2
, Type
**max
)
132 // max(nil, x) = x for x = nillable type.
133 if(t2
->op
== TypeNil
){
138 if(t1
->op
== TypeNil
){
139 if(nillable(t2
->op
)){
145 // Many axes for grammar types.
146 if(t1
->op
== TypeGram
){
147 if(t2
->op
!= TypeGram
)
149 if(t1
->gram
== nil
|| t2
->gram
== nil
|| t1
->gram
!= t2
->gram
){
150 *max
= typegram1(nil
, nil
);
154 if(t1
->sym
== t2
->sym
)
156 t1
= typegramsym(t1
, nil
, nil
);
157 t2
= typegramsym(t2
, nil
, nil
);
168 t
= typegramx(t1
, addNameL(copyNameL(t1
->gramx
), t2
->gramx
));
170 t
= typegramsym(t
, sym
->name
, sym
);
175 if(t1
->op
== TypeStruct
){
176 if(t2
->op
!= TypeStruct
)
178 for(; t1
; t1
=t1
->super
)
179 for(t
=t2
; t
; t
=t
->super
)
187 // TODO: More typemax cases go here, as needed.
188 fprint(2, "no max %T %T\n", t1
, t2
);
194 typemax(Type
*t1
, Type
*t2
, Type
**max
)
212 if(t1
->op
== TypeGram
&& t2
->op
== TypeGram
){
213 if(t1
->sym
!= t2
->sym
){
227 if((h2
= hashget(h1
, t1
)) == nil
)
228 hashput(h1
, t1
, h2
= mkhash());
234 if(_typemax(t1
, t2
, max
) >= 0){
235 hashput(h2
, t2
, *max
);
242 typegramadd(Type
*t1
, Type
*t2
)
250 if(t1
->op
!= TypeGram
|| t2
->op
!= TypeGram
){
252 werrstr("cannot add %T and %T", t1
, t2
);
255 if(t1
->gram
== nil
|| t2
->gram
== nil
|| t1
->gram
!= t2
->gram
)
258 if(typemax(t1
, t2
, &t
) < 0)
264 typegramquest(Type
*t1
)
266 return typegramxl(t1
, N("?"), nil
);