1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // CommonType.cc --
|
---|
8 | //
|
---|
9 | // Author : Richard C. Bilson
|
---|
10 | // Created On : Sun May 17 06:59:27 2015
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Thu Feb 14 17:10:10 2019
|
---|
13 | // Update Count : 24
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "CommonType.hpp"
|
---|
17 |
|
---|
18 | #include <cassert> // for strict_dynamic_cast
|
---|
19 | #include <map> // for _Rb_tree_const_iterator
|
---|
20 | #include <utility> // for pair
|
---|
21 |
|
---|
22 | #include "AST/Decl.hpp"
|
---|
23 | #include "AST/Pass.hpp"
|
---|
24 | #include "AST/Type.hpp"
|
---|
25 | #include "Unify.h" // for unifyExact, WidenMode
|
---|
26 | #include "typeops.h" // for isFtype
|
---|
27 | #include "Tuples/Tuples.h"
|
---|
28 |
|
---|
29 | // #define DEBUG
|
---|
30 | #ifdef DEBUG
|
---|
31 | #define PRINT(x) x
|
---|
32 | #else
|
---|
33 | #define PRINT(x)
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | namespace ResolvExpr {
|
---|
37 |
|
---|
38 | namespace {
|
---|
39 |
|
---|
40 | // GENERATED START, DO NOT EDIT
|
---|
41 | // GENERATED BY BasicTypes-gen.cc
|
---|
42 | #define BT ast::BasicKind::
|
---|
43 | static const ast::BasicKind commonTypes[BT NUMBER_OF_BASIC_TYPES][BT NUMBER_OF_BASIC_TYPES] = { // nearest common ancestor
|
---|
44 | /* B C SC UC SI SUI
|
---|
45 | I UI LI LUI LLI LLUI
|
---|
46 | IB UIB _FH _FH _F _FC
|
---|
47 | F FC _FX _FXC FD _FDC
|
---|
48 | D DC F80X _FDXC F80 _FB
|
---|
49 | _FLDC FB LD LDC _FBX _FLDXC
|
---|
50 | */
|
---|
51 | {
|
---|
52 | /* B */ BT Bool, BT Char, BT SignedChar, BT UnsignedChar, BT ShortSignedInt, BT ShortUnsignedInt,
|
---|
53 | BT SignedInt, BT UnsignedInt, BT LongSignedInt, BT LongUnsignedInt, BT LongLongSignedInt, BT LongLongUnsignedInt,
|
---|
54 | BT SignedInt128, BT UnsignedInt128, BT uFloat16, BT uFloat16Complex, BT uFloat32, BT uFloat32Complex,
|
---|
55 | BT Float, BT FloatComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
56 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
57 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
58 | },
|
---|
59 | {
|
---|
60 | /* C */ BT Char, BT Char, BT SignedChar, BT UnsignedChar, BT ShortSignedInt, BT ShortUnsignedInt,
|
---|
61 | BT SignedInt, BT UnsignedInt, BT LongSignedInt, BT LongUnsignedInt, BT LongLongSignedInt, BT LongLongUnsignedInt,
|
---|
62 | BT SignedInt128, BT UnsignedInt128, BT uFloat16, BT uFloat16Complex, BT uFloat32, BT uFloat32Complex,
|
---|
63 | BT Float, BT FloatComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
64 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
65 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
66 | },
|
---|
67 | {
|
---|
68 | /* SC */ BT SignedChar, BT SignedChar, BT SignedChar, BT UnsignedChar, BT ShortSignedInt, BT ShortUnsignedInt,
|
---|
69 | BT SignedInt, BT UnsignedInt, BT LongSignedInt, BT LongUnsignedInt, BT LongLongSignedInt, BT LongLongUnsignedInt,
|
---|
70 | BT SignedInt128, BT UnsignedInt128, BT uFloat16, BT uFloat16Complex, BT uFloat32, BT uFloat32Complex,
|
---|
71 | BT Float, BT FloatComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
72 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
73 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
74 | },
|
---|
75 | {
|
---|
76 | /* UC */ BT UnsignedChar, BT UnsignedChar, BT UnsignedChar, BT UnsignedChar, BT ShortSignedInt, BT ShortUnsignedInt,
|
---|
77 | BT SignedInt, BT UnsignedInt, BT LongSignedInt, BT LongUnsignedInt, BT LongLongSignedInt, BT LongLongUnsignedInt,
|
---|
78 | BT SignedInt128, BT UnsignedInt128, BT uFloat16, BT uFloat16Complex, BT uFloat32, BT uFloat32Complex,
|
---|
79 | BT Float, BT FloatComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
80 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
81 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
82 | },
|
---|
83 | {
|
---|
84 | /* SI */ BT ShortSignedInt, BT ShortSignedInt, BT ShortSignedInt, BT ShortSignedInt, BT ShortSignedInt, BT ShortUnsignedInt,
|
---|
85 | BT SignedInt, BT UnsignedInt, BT LongSignedInt, BT LongUnsignedInt, BT LongLongSignedInt, BT LongLongUnsignedInt,
|
---|
86 | BT SignedInt128, BT UnsignedInt128, BT uFloat16, BT uFloat16Complex, BT uFloat32, BT uFloat32Complex,
|
---|
87 | BT Float, BT FloatComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
88 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
89 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
90 | },
|
---|
91 | {
|
---|
92 | /* SUI */ BT ShortUnsignedInt, BT ShortUnsignedInt, BT ShortUnsignedInt, BT ShortUnsignedInt, BT ShortUnsignedInt, BT ShortUnsignedInt,
|
---|
93 | BT SignedInt, BT UnsignedInt, BT LongSignedInt, BT LongUnsignedInt, BT LongLongSignedInt, BT LongLongUnsignedInt,
|
---|
94 | BT SignedInt128, BT UnsignedInt128, BT uFloat16, BT uFloat16Complex, BT uFloat32, BT uFloat32Complex,
|
---|
95 | BT Float, BT FloatComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
96 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
97 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
98 | },
|
---|
99 | {
|
---|
100 | /* I */ BT SignedInt, BT SignedInt, BT SignedInt, BT SignedInt, BT SignedInt, BT SignedInt,
|
---|
101 | BT SignedInt, BT UnsignedInt, BT LongSignedInt, BT LongUnsignedInt, BT LongLongSignedInt, BT LongLongUnsignedInt,
|
---|
102 | BT SignedInt128, BT UnsignedInt128, BT uFloat16, BT uFloat16Complex, BT uFloat32, BT uFloat32Complex,
|
---|
103 | BT Float, BT FloatComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
104 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
105 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
106 | },
|
---|
107 | {
|
---|
108 | /* UI */ BT UnsignedInt, BT UnsignedInt, BT UnsignedInt, BT UnsignedInt, BT UnsignedInt, BT UnsignedInt,
|
---|
109 | BT UnsignedInt, BT UnsignedInt, BT LongSignedInt, BT LongUnsignedInt, BT LongLongSignedInt, BT LongLongUnsignedInt,
|
---|
110 | BT SignedInt128, BT UnsignedInt128, BT uFloat16, BT uFloat16Complex, BT uFloat32, BT uFloat32Complex,
|
---|
111 | BT Float, BT FloatComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
112 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
113 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
114 | },
|
---|
115 | {
|
---|
116 | /* LI */ BT LongSignedInt, BT LongSignedInt, BT LongSignedInt, BT LongSignedInt, BT LongSignedInt, BT LongSignedInt,
|
---|
117 | BT LongSignedInt, BT LongSignedInt, BT LongSignedInt, BT LongUnsignedInt, BT LongLongSignedInt, BT LongLongUnsignedInt,
|
---|
118 | BT SignedInt128, BT UnsignedInt128, BT uFloat16, BT uFloat16Complex, BT uFloat32, BT uFloat32Complex,
|
---|
119 | BT Float, BT FloatComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
120 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
121 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
122 | },
|
---|
123 | {
|
---|
124 | /* LUI */ BT LongUnsignedInt, BT LongUnsignedInt, BT LongUnsignedInt, BT LongUnsignedInt, BT LongUnsignedInt, BT LongUnsignedInt,
|
---|
125 | BT LongUnsignedInt, BT LongUnsignedInt, BT LongUnsignedInt, BT LongUnsignedInt, BT LongLongSignedInt, BT LongLongUnsignedInt,
|
---|
126 | BT SignedInt128, BT UnsignedInt128, BT uFloat16, BT uFloat16Complex, BT uFloat32, BT uFloat32Complex,
|
---|
127 | BT Float, BT FloatComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
128 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
129 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
130 | },
|
---|
131 | {
|
---|
132 | /* LLI */ BT LongLongSignedInt, BT LongLongSignedInt, BT LongLongSignedInt, BT LongLongSignedInt, BT LongLongSignedInt, BT LongLongSignedInt,
|
---|
133 | BT LongLongSignedInt, BT LongLongSignedInt, BT LongLongSignedInt, BT LongLongSignedInt, BT LongLongSignedInt, BT LongLongUnsignedInt,
|
---|
134 | BT SignedInt128, BT UnsignedInt128, BT uFloat16, BT uFloat16Complex, BT uFloat32, BT uFloat32Complex,
|
---|
135 | BT Float, BT FloatComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
136 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
137 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
138 | },
|
---|
139 | {
|
---|
140 | /* LLUI */ BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt,
|
---|
141 | BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt, BT LongLongUnsignedInt,
|
---|
142 | BT SignedInt128, BT UnsignedInt128, BT uFloat16, BT uFloat16Complex, BT uFloat32, BT uFloat32Complex,
|
---|
143 | BT Float, BT FloatComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
144 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
145 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
146 | },
|
---|
147 | {
|
---|
148 | /* IB */ BT SignedInt128, BT SignedInt128, BT SignedInt128, BT SignedInt128, BT SignedInt128, BT SignedInt128,
|
---|
149 | BT SignedInt128, BT SignedInt128, BT SignedInt128, BT SignedInt128, BT SignedInt128, BT SignedInt128,
|
---|
150 | BT SignedInt128, BT UnsignedInt128, BT uFloat16, BT uFloat16Complex, BT uFloat32, BT uFloat32Complex,
|
---|
151 | BT Float, BT FloatComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
152 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
153 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
154 | },
|
---|
155 | {
|
---|
156 | /* UIB */ BT UnsignedInt128, BT UnsignedInt128, BT UnsignedInt128, BT UnsignedInt128, BT UnsignedInt128, BT UnsignedInt128,
|
---|
157 | BT UnsignedInt128, BT UnsignedInt128, BT UnsignedInt128, BT UnsignedInt128, BT UnsignedInt128, BT UnsignedInt128,
|
---|
158 | BT UnsignedInt128, BT UnsignedInt128, BT uFloat16, BT uFloat16Complex, BT uFloat32, BT uFloat32Complex,
|
---|
159 | BT Float, BT FloatComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
160 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
161 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
162 | },
|
---|
163 | {
|
---|
164 | /* _FH */ BT uFloat16, BT uFloat16, BT uFloat16, BT uFloat16, BT uFloat16, BT uFloat16,
|
---|
165 | BT uFloat16, BT uFloat16, BT uFloat16, BT uFloat16, BT uFloat16, BT uFloat16,
|
---|
166 | BT uFloat16, BT uFloat16, BT uFloat16, BT uFloat16Complex, BT uFloat32, BT uFloat32Complex,
|
---|
167 | BT Float, BT FloatComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
168 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
169 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
170 | },
|
---|
171 | {
|
---|
172 | /* _FH */ BT uFloat16Complex, BT uFloat16Complex, BT uFloat16Complex, BT uFloat16Complex, BT uFloat16Complex, BT uFloat16Complex,
|
---|
173 | BT uFloat16Complex, BT uFloat16Complex, BT uFloat16Complex, BT uFloat16Complex, BT uFloat16Complex, BT uFloat16Complex,
|
---|
174 | BT uFloat16Complex, BT uFloat16Complex, BT uFloat16Complex, BT uFloat16Complex, BT uFloat32Complex, BT uFloat32Complex,
|
---|
175 | BT FloatComplex, BT FloatComplex, BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat64Complex, BT uFloat64Complex,
|
---|
176 | BT DoubleComplex, BT DoubleComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat128Complex,
|
---|
177 | BT uFloat128Complex, BT uFloat128Complex, BT LongDoubleComplex, BT LongDoubleComplex, BT uFloat128xComplex, BT uFloat128xComplex,
|
---|
178 | },
|
---|
179 | {
|
---|
180 | /* _F */ BT uFloat32, BT uFloat32, BT uFloat32, BT uFloat32, BT uFloat32, BT uFloat32,
|
---|
181 | BT uFloat32, BT uFloat32, BT uFloat32, BT uFloat32, BT uFloat32, BT uFloat32,
|
---|
182 | BT uFloat32, BT uFloat32, BT uFloat32, BT uFloat32Complex, BT uFloat32, BT uFloat32Complex,
|
---|
183 | BT Float, BT FloatComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
184 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
185 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
186 | },
|
---|
187 | {
|
---|
188 | /* _FC */ BT uFloat32Complex, BT uFloat32Complex, BT uFloat32Complex, BT uFloat32Complex, BT uFloat32Complex, BT uFloat32Complex,
|
---|
189 | BT uFloat32Complex, BT uFloat32Complex, BT uFloat32Complex, BT uFloat32Complex, BT uFloat32Complex, BT uFloat32Complex,
|
---|
190 | BT uFloat32Complex, BT uFloat32Complex, BT uFloat32Complex, BT uFloat32Complex, BT uFloat32Complex, BT uFloat32Complex,
|
---|
191 | BT FloatComplex, BT FloatComplex, BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat64Complex, BT uFloat64Complex,
|
---|
192 | BT DoubleComplex, BT DoubleComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat128Complex,
|
---|
193 | BT uFloat128Complex, BT uFloat128Complex, BT LongDoubleComplex, BT LongDoubleComplex, BT uFloat128xComplex, BT uFloat128xComplex,
|
---|
194 | },
|
---|
195 | {
|
---|
196 | /* F */ BT Float, BT Float, BT Float, BT Float, BT Float, BT Float,
|
---|
197 | BT Float, BT Float, BT Float, BT Float, BT Float, BT Float,
|
---|
198 | BT Float, BT Float, BT Float, BT FloatComplex, BT Float, BT FloatComplex,
|
---|
199 | BT Float, BT FloatComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
200 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
201 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
202 | },
|
---|
203 | {
|
---|
204 | /* FC */ BT FloatComplex, BT FloatComplex, BT FloatComplex, BT FloatComplex, BT FloatComplex, BT FloatComplex,
|
---|
205 | BT FloatComplex, BT FloatComplex, BT FloatComplex, BT FloatComplex, BT FloatComplex, BT FloatComplex,
|
---|
206 | BT FloatComplex, BT FloatComplex, BT FloatComplex, BT FloatComplex, BT FloatComplex, BT FloatComplex,
|
---|
207 | BT FloatComplex, BT FloatComplex, BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat64Complex, BT uFloat64Complex,
|
---|
208 | BT DoubleComplex, BT DoubleComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat128Complex,
|
---|
209 | BT uFloat128Complex, BT uFloat128Complex, BT LongDoubleComplex, BT LongDoubleComplex, BT uFloat128xComplex, BT uFloat128xComplex,
|
---|
210 | },
|
---|
211 | {
|
---|
212 | /* _FX */ BT uFloat32x, BT uFloat32x, BT uFloat32x, BT uFloat32x, BT uFloat32x, BT uFloat32x,
|
---|
213 | BT uFloat32x, BT uFloat32x, BT uFloat32x, BT uFloat32x, BT uFloat32x, BT uFloat32x,
|
---|
214 | BT uFloat32x, BT uFloat32x, BT uFloat32x, BT uFloat32xComplex, BT uFloat32x, BT uFloat32xComplex,
|
---|
215 | BT uFloat32x, BT uFloat32xComplex, BT uFloat32x, BT uFloat32xComplex, BT uFloat64, BT uFloat64Complex,
|
---|
216 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
217 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
218 | },
|
---|
219 | {
|
---|
220 | /* _FXC */ BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat32xComplex,
|
---|
221 | BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat32xComplex,
|
---|
222 | BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat32xComplex,
|
---|
223 | BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat32xComplex, BT uFloat64Complex, BT uFloat64Complex,
|
---|
224 | BT DoubleComplex, BT DoubleComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat128Complex,
|
---|
225 | BT uFloat128Complex, BT uFloat128Complex, BT LongDoubleComplex, BT LongDoubleComplex, BT uFloat128xComplex, BT uFloat128xComplex,
|
---|
226 | },
|
---|
227 | {
|
---|
228 | /* FD */ BT uFloat64, BT uFloat64, BT uFloat64, BT uFloat64, BT uFloat64, BT uFloat64,
|
---|
229 | BT uFloat64, BT uFloat64, BT uFloat64, BT uFloat64, BT uFloat64, BT uFloat64,
|
---|
230 | BT uFloat64, BT uFloat64, BT uFloat64, BT uFloat64Complex, BT uFloat64, BT uFloat64Complex,
|
---|
231 | BT uFloat64, BT uFloat64Complex, BT uFloat64, BT uFloat64Complex, BT uFloat64, BT uFloat64Complex,
|
---|
232 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
233 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
234 | },
|
---|
235 | {
|
---|
236 | /* _FDC */ BT uFloat64Complex, BT uFloat64Complex, BT uFloat64Complex, BT uFloat64Complex, BT uFloat64Complex, BT uFloat64Complex,
|
---|
237 | BT uFloat64Complex, BT uFloat64Complex, BT uFloat64Complex, BT uFloat64Complex, BT uFloat64Complex, BT uFloat64Complex,
|
---|
238 | BT uFloat64Complex, BT uFloat64Complex, BT uFloat64Complex, BT uFloat64Complex, BT uFloat64Complex, BT uFloat64Complex,
|
---|
239 | BT uFloat64Complex, BT uFloat64Complex, BT uFloat64Complex, BT uFloat64Complex, BT uFloat64Complex, BT uFloat64Complex,
|
---|
240 | BT DoubleComplex, BT DoubleComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat128Complex,
|
---|
241 | BT uFloat128Complex, BT uFloat128Complex, BT LongDoubleComplex, BT LongDoubleComplex, BT uFloat128xComplex, BT uFloat128xComplex,
|
---|
242 | },
|
---|
243 | {
|
---|
244 | /* D */ BT Double, BT Double, BT Double, BT Double, BT Double, BT Double,
|
---|
245 | BT Double, BT Double, BT Double, BT Double, BT Double, BT Double,
|
---|
246 | BT Double, BT Double, BT Double, BT DoubleComplex, BT Double, BT DoubleComplex,
|
---|
247 | BT Double, BT DoubleComplex, BT Double, BT DoubleComplex, BT Double, BT DoubleComplex,
|
---|
248 | BT Double, BT DoubleComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
249 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
250 | },
|
---|
251 | {
|
---|
252 | /* DC */ BT DoubleComplex, BT DoubleComplex, BT DoubleComplex, BT DoubleComplex, BT DoubleComplex, BT DoubleComplex,
|
---|
253 | BT DoubleComplex, BT DoubleComplex, BT DoubleComplex, BT DoubleComplex, BT DoubleComplex, BT DoubleComplex,
|
---|
254 | BT DoubleComplex, BT DoubleComplex, BT DoubleComplex, BT DoubleComplex, BT DoubleComplex, BT DoubleComplex,
|
---|
255 | BT DoubleComplex, BT DoubleComplex, BT DoubleComplex, BT DoubleComplex, BT DoubleComplex, BT DoubleComplex,
|
---|
256 | BT DoubleComplex, BT DoubleComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat128Complex,
|
---|
257 | BT uFloat128Complex, BT uFloat128Complex, BT LongDoubleComplex, BT LongDoubleComplex, BT uFloat128xComplex, BT uFloat128xComplex,
|
---|
258 | },
|
---|
259 | {
|
---|
260 | /* F80X */ BT uFloat64x, BT uFloat64x, BT uFloat64x, BT uFloat64x, BT uFloat64x, BT uFloat64x,
|
---|
261 | BT uFloat64x, BT uFloat64x, BT uFloat64x, BT uFloat64x, BT uFloat64x, BT uFloat64x,
|
---|
262 | BT uFloat64x, BT uFloat64x, BT uFloat64x, BT uFloat64xComplex, BT uFloat64x, BT uFloat64xComplex,
|
---|
263 | BT uFloat64x, BT uFloat64xComplex, BT uFloat64x, BT uFloat64xComplex, BT uFloat64x, BT uFloat64xComplex,
|
---|
264 | BT uFloat64x, BT uFloat64xComplex, BT uFloat64x, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
265 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
266 | },
|
---|
267 | {
|
---|
268 | /* _FDXC */ BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex,
|
---|
269 | BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex,
|
---|
270 | BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex,
|
---|
271 | BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex,
|
---|
272 | BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat64xComplex, BT uFloat128Complex,
|
---|
273 | BT uFloat128Complex, BT uFloat128Complex, BT LongDoubleComplex, BT LongDoubleComplex, BT uFloat128xComplex, BT uFloat128xComplex,
|
---|
274 | },
|
---|
275 | {
|
---|
276 | /* F80 */ BT uuFloat80, BT uuFloat80, BT uuFloat80, BT uuFloat80, BT uuFloat80, BT uuFloat80,
|
---|
277 | BT uuFloat80, BT uuFloat80, BT uuFloat80, BT uuFloat80, BT uuFloat80, BT uuFloat80,
|
---|
278 | BT uuFloat80, BT uuFloat80, BT uuFloat80, BT uFloat64xComplex, BT uuFloat80, BT uFloat64xComplex,
|
---|
279 | BT uuFloat80, BT uFloat64xComplex, BT uuFloat80, BT uFloat64xComplex, BT uuFloat80, BT uFloat64xComplex,
|
---|
280 | BT uuFloat80, BT uFloat64xComplex, BT uuFloat80, BT uFloat64xComplex, BT uuFloat80, BT uFloat128,
|
---|
281 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
282 | },
|
---|
283 | {
|
---|
284 | /* _FB */ BT uFloat128, BT uFloat128, BT uFloat128, BT uFloat128, BT uFloat128, BT uFloat128,
|
---|
285 | BT uFloat128, BT uFloat128, BT uFloat128, BT uFloat128, BT uFloat128, BT uFloat128,
|
---|
286 | BT uFloat128, BT uFloat128, BT uFloat128, BT uFloat128Complex, BT uFloat128, BT uFloat128Complex,
|
---|
287 | BT uFloat128, BT uFloat128Complex, BT uFloat128, BT uFloat128Complex, BT uFloat128, BT uFloat128Complex,
|
---|
288 | BT uFloat128, BT uFloat128Complex, BT uFloat128, BT uFloat128Complex, BT uFloat128, BT uFloat128,
|
---|
289 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
290 | },
|
---|
291 | {
|
---|
292 | /* _FLDC */ BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex,
|
---|
293 | BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex,
|
---|
294 | BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex,
|
---|
295 | BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex,
|
---|
296 | BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex, BT uFloat128Complex,
|
---|
297 | BT uFloat128Complex, BT uFloat128Complex, BT LongDoubleComplex, BT LongDoubleComplex, BT uFloat128xComplex, BT uFloat128xComplex,
|
---|
298 | },
|
---|
299 | {
|
---|
300 | /* FB */ BT uuFloat128, BT uuFloat128, BT uuFloat128, BT uuFloat128, BT uuFloat128, BT uuFloat128,
|
---|
301 | BT uuFloat128, BT uuFloat128, BT uuFloat128, BT uuFloat128, BT uuFloat128, BT uuFloat128,
|
---|
302 | BT uuFloat128, BT uuFloat128, BT uuFloat128, BT uFloat128Complex, BT uuFloat128, BT uFloat128Complex,
|
---|
303 | BT uuFloat128, BT uFloat128Complex, BT uuFloat128, BT uFloat128Complex, BT uuFloat128, BT uFloat128Complex,
|
---|
304 | BT uuFloat128, BT uFloat128Complex, BT uuFloat128, BT uFloat128Complex, BT uuFloat128, BT uuFloat128,
|
---|
305 | BT uFloat128Complex, BT uuFloat128, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
306 | },
|
---|
307 | {
|
---|
308 | /* LD */ BT LongDouble, BT LongDouble, BT LongDouble, BT LongDouble, BT LongDouble, BT LongDouble,
|
---|
309 | BT LongDouble, BT LongDouble, BT LongDouble, BT LongDouble, BT LongDouble, BT LongDouble,
|
---|
310 | BT LongDouble, BT LongDouble, BT LongDouble, BT LongDoubleComplex, BT LongDouble, BT LongDoubleComplex,
|
---|
311 | BT LongDouble, BT LongDoubleComplex, BT LongDouble, BT LongDoubleComplex, BT LongDouble, BT LongDoubleComplex,
|
---|
312 | BT LongDouble, BT LongDoubleComplex, BT LongDouble, BT LongDoubleComplex, BT LongDouble, BT LongDouble,
|
---|
313 | BT LongDoubleComplex, BT LongDouble, BT LongDouble, BT LongDoubleComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
314 | },
|
---|
315 | {
|
---|
316 | /* LDC */ BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex,
|
---|
317 | BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex,
|
---|
318 | BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex,
|
---|
319 | BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex,
|
---|
320 | BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex,
|
---|
321 | BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex, BT LongDoubleComplex, BT uFloat128xComplex, BT uFloat128xComplex,
|
---|
322 | },
|
---|
323 | {
|
---|
324 | /* _FBX */ BT uFloat128x, BT uFloat128x, BT uFloat128x, BT uFloat128x, BT uFloat128x, BT uFloat128x,
|
---|
325 | BT uFloat128x, BT uFloat128x, BT uFloat128x, BT uFloat128x, BT uFloat128x, BT uFloat128x,
|
---|
326 | BT uFloat128x, BT uFloat128x, BT uFloat128x, BT uFloat128xComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
327 | BT uFloat128x, BT uFloat128xComplex, BT uFloat128x, BT uFloat128xComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
328 | BT uFloat128x, BT uFloat128xComplex, BT uFloat128x, BT uFloat128xComplex, BT uFloat128x, BT uFloat128x,
|
---|
329 | BT uFloat128xComplex, BT uFloat128x, BT uFloat128x, BT uFloat128xComplex, BT uFloat128x, BT uFloat128xComplex,
|
---|
330 | },
|
---|
331 | {
|
---|
332 | /* _FLDXC */ BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex,
|
---|
333 | BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex,
|
---|
334 | BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex,
|
---|
335 | BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex,
|
---|
336 | BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex,
|
---|
337 | BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex, BT uFloat128xComplex,
|
---|
338 | },
|
---|
339 | }; // commonTypes
|
---|
340 | #undef BT
|
---|
341 | // GENERATED END
|
---|
342 | static_assert(
|
---|
343 | sizeof(commonTypes)/sizeof(commonTypes[0][0]) == ast::BasicKind::NUMBER_OF_BASIC_TYPES * ast::BasicKind::NUMBER_OF_BASIC_TYPES,
|
---|
344 | "Each basic type kind should have a corresponding row in the combined type matrix"
|
---|
345 | );
|
---|
346 |
|
---|
347 | class CommonType final : public ast::WithShortCircuiting {
|
---|
348 | const ast::Type * type2;
|
---|
349 | WidenMode widen;
|
---|
350 | ast::TypeEnvironment & tenv;
|
---|
351 | const ast::OpenVarSet & open;
|
---|
352 | ast::AssertionSet & need;
|
---|
353 | ast::AssertionSet & have;
|
---|
354 | public:
|
---|
355 | static size_t traceId;
|
---|
356 | ast::ptr< ast::Type > result;
|
---|
357 |
|
---|
358 | CommonType(
|
---|
359 | const ast::Type * t2, WidenMode w,
|
---|
360 | ast::TypeEnvironment & env, const ast::OpenVarSet & o,
|
---|
361 | ast::AssertionSet & need, ast::AssertionSet & have )
|
---|
362 | : type2( t2 ), widen( w ), tenv( env ), open( o ), need (need), have (have) ,result() {}
|
---|
363 |
|
---|
364 | void previsit( const ast::Node * ) { visit_children = false; }
|
---|
365 |
|
---|
366 | void postvisit( const ast::VoidType * ) {}
|
---|
367 |
|
---|
368 | void postvisit( const ast::BasicType * basic ) {
|
---|
369 | if ( auto basic2 = dynamic_cast< const ast::BasicType * >( type2 ) ) {
|
---|
370 | ast::BasicKind kind;
|
---|
371 | if (basic->kind != basic2->kind && !widen.first && !widen.second) return;
|
---|
372 | else if (!widen.first) kind = basic->kind; // widen.second
|
---|
373 | else if (!widen.second) kind = basic2->kind;
|
---|
374 | else kind = commonTypes[ basic->kind ][ basic2->kind ];
|
---|
375 | // xxx - what does qualifiers even do here??
|
---|
376 | if ( (basic->qualifiers >= basic2->qualifiers || widen.first)
|
---|
377 | && (basic->qualifiers <= basic2->qualifiers || widen.second) ) {
|
---|
378 | result = new ast::BasicType{ kind, basic->qualifiers | basic2->qualifiers };
|
---|
379 | }
|
---|
380 | } else if (
|
---|
381 | dynamic_cast< const ast::ZeroType * >( type2 )
|
---|
382 | || dynamic_cast< const ast::OneType * >( type2 )
|
---|
383 | ) {
|
---|
384 | if (widen.second) {
|
---|
385 | result = new ast::BasicType{ basic->kind, basic->qualifiers | type2->qualifiers };
|
---|
386 | }
|
---|
387 | } else if ( const ast::EnumInstType * enumInst = dynamic_cast< const ast::EnumInstType * >( type2 ) ) {
|
---|
388 | const ast::EnumDecl* enumDecl = enumInst->base;
|
---|
389 | if ( !enumDecl->base ) {
|
---|
390 | ast::BasicKind kind = commonTypes[ basic->kind ][ ast::BasicKind::SignedInt ];
|
---|
391 | if (
|
---|
392 | ( ( kind == basic->kind && basic->qualifiers >= type2->qualifiers )
|
---|
393 | || widen.first )
|
---|
394 | && ( ( kind != basic->kind && basic->qualifiers <= type2->qualifiers )
|
---|
395 | || widen.second )
|
---|
396 | ) {
|
---|
397 | result = new ast::BasicType{ kind, basic->qualifiers | type2->qualifiers };
|
---|
398 | }
|
---|
399 | }
|
---|
400 | } else if ( auto type2AsAttr = dynamic_cast< const ast::EnumAttrType * >( type2 ) ) {
|
---|
401 | if ( type2AsAttr->attr == ast::EnumAttribute::Posn ) {
|
---|
402 | ast::BasicKind kind = commonTypes[ basic->kind ][ ast::BasicKind::SignedInt ];
|
---|
403 | if (
|
---|
404 | ( ( kind == basic->kind && basic->qualifiers >= type2->qualifiers )
|
---|
405 | || widen.first )
|
---|
406 | && ( ( kind != basic->kind && basic->qualifiers <= type2->qualifiers )
|
---|
407 | || widen.second )
|
---|
408 | ) {
|
---|
409 | result = new ast::BasicType{ kind, basic->qualifiers | type2->qualifiers };
|
---|
410 | }
|
---|
411 | }
|
---|
412 | }
|
---|
413 | }
|
---|
414 |
|
---|
415 | private:
|
---|
416 | template< typename Pointer >
|
---|
417 | void getCommonWithVoidPointer( const Pointer * voidPtr, const Pointer * oPtr ) {
|
---|
418 | const ast::Type * base = oPtr->base;
|
---|
419 | if ( auto var = dynamic_cast< const ast::TypeInstType * >( base ) ) {
|
---|
420 | auto entry = open.find( *var );
|
---|
421 | if ( entry != open.end() ) {
|
---|
422 | ast::AssertionSet need, have;
|
---|
423 | if ( ! tenv.bindVar(
|
---|
424 | var, voidPtr->base, entry->second, need, have, open, widen )
|
---|
425 | ) return;
|
---|
426 | }
|
---|
427 | }
|
---|
428 | result = voidPtr;
|
---|
429 | add_qualifiers( result, oPtr->qualifiers );
|
---|
430 | }
|
---|
431 |
|
---|
432 | public:
|
---|
433 | void postvisit( const ast::PointerType * pointer ) {
|
---|
434 | if ( auto pointer2 = dynamic_cast< const ast::PointerType * >( type2 ) ) {
|
---|
435 | if (
|
---|
436 | widen.first
|
---|
437 | && pointer2->base.as< ast::VoidType >()
|
---|
438 | && ! ast::isFtype( pointer->base )
|
---|
439 | ) {
|
---|
440 | getCommonWithVoidPointer( pointer2, pointer );
|
---|
441 | } else if (
|
---|
442 | widen.second
|
---|
443 | && pointer->base.as< ast::VoidType >()
|
---|
444 | && ! ast::isFtype( pointer2->base )
|
---|
445 | ) {
|
---|
446 | getCommonWithVoidPointer( pointer, pointer2 );
|
---|
447 | } else if (
|
---|
448 | ( pointer->base->qualifiers >= pointer2->base->qualifiers || widen.first )
|
---|
449 | && ( pointer->base->qualifiers <= pointer2->base->qualifiers || widen.second )
|
---|
450 | ) {
|
---|
451 | ast::CV::Qualifiers q1 = pointer->base->qualifiers;
|
---|
452 | ast::CV::Qualifiers q2 = pointer2->base->qualifiers;
|
---|
453 |
|
---|
454 | // force t{1,2} to be cloned if their qualifiers must be stripped, so that
|
---|
455 | // pointer{,2}->base are unchanged
|
---|
456 | ast::ptr< ast::Type > t1{ pointer->base }, t2{ pointer2->base };
|
---|
457 | reset_qualifiers( t1 );
|
---|
458 | reset_qualifiers( t2 );
|
---|
459 |
|
---|
460 | ast::OpenVarSet newOpen{ open };
|
---|
461 | if ( unifyExact( t1, t2, tenv, have, need, newOpen, noWiden() ) ) {
|
---|
462 | result = pointer;
|
---|
463 | if ( q1.val != q2.val ) {
|
---|
464 | // reset result->base->qualifiers to be union of two base qualifiers
|
---|
465 | strict_dynamic_cast< ast::PointerType * >(
|
---|
466 | result.get_and_mutate()
|
---|
467 | )->base.get_and_mutate()->qualifiers = q1 | q2;
|
---|
468 | }
|
---|
469 | } else if ( isFtype (t1) && isFtype (t2) ) {
|
---|
470 | auto f1 = t1.as<ast::FunctionType>();
|
---|
471 | if (!f1) return;
|
---|
472 | auto f2 = t2.strict_as<ast::FunctionType>();
|
---|
473 |
|
---|
474 | assertf(f1->returns.size() <= 1, "Function return should not be a list");
|
---|
475 | assertf(f2->returns.size() <= 1, "Function return should not be a list");
|
---|
476 |
|
---|
477 | if (
|
---|
478 | ( f1->params.size() != f2->params.size() || f1->returns.size() != f2->returns.size() )
|
---|
479 | && ! f1->isTtype()
|
---|
480 | && ! f2->isTtype()
|
---|
481 | ) return;
|
---|
482 |
|
---|
483 | auto params1 = flattenList( f1->params, tenv );
|
---|
484 | auto params2 = flattenList( f2->params, tenv );
|
---|
485 |
|
---|
486 | auto crnt1 = params1.begin();
|
---|
487 | auto crnt2 = params2.begin();
|
---|
488 | auto end1 = params1.end();
|
---|
489 | auto end2 = params2.end();
|
---|
490 |
|
---|
491 | while (crnt1 != end1 && crnt2 != end2 ) {
|
---|
492 | const ast::Type * arg1 = *crnt1;
|
---|
493 | const ast::Type * arg2 = *crnt2;
|
---|
494 |
|
---|
495 | bool isTuple1 = Tuples::isTtype( t1 );
|
---|
496 | bool isTuple2 = Tuples::isTtype( t2 );
|
---|
497 |
|
---|
498 | // assumes here that ttype *must* be last parameter
|
---|
499 | if ( isTuple1 && ! isTuple2 ) {
|
---|
500 | // combine remainder of list2, then unify
|
---|
501 | if (unifyExact(
|
---|
502 | arg1, tupleFromTypes( crnt2, end2 ), tenv, need, have, open,
|
---|
503 | noWiden() )) {
|
---|
504 | break;
|
---|
505 | } else return;
|
---|
506 | } else if ( ! isTuple1 && isTuple2 ) {
|
---|
507 | // combine remainder of list1, then unify
|
---|
508 | if (unifyExact(
|
---|
509 | tupleFromTypes( crnt1, end1 ), arg2, tenv, need, have, open,
|
---|
510 | noWiden() )) {
|
---|
511 | break;
|
---|
512 | } else return;
|
---|
513 | }
|
---|
514 |
|
---|
515 | // allow qualifiers of pointer and reference base to become more specific
|
---|
516 | if (auto ref1 = dynamic_cast<const ast::ReferenceType *> (arg1)) {
|
---|
517 | if (auto ref2 = dynamic_cast<const ast::ReferenceType *> (arg2)) {
|
---|
518 | ast::ptr<ast::Type> base1 = ref1->base;
|
---|
519 | ast::ptr<ast::Type> base2 = ref2->base;
|
---|
520 |
|
---|
521 | // xxx - assume LHS is always the target type
|
---|
522 |
|
---|
523 | if ( ! ((widen.second && ref2->qualifiers.is_mutex)
|
---|
524 | || (ref1->qualifiers.is_mutex == ref2->qualifiers.is_mutex ))) return;
|
---|
525 |
|
---|
526 | if ( (widen.second && base1->qualifiers <= base2->qualifiers ) || (base2->qualifiers == base1->qualifiers) ) {
|
---|
527 |
|
---|
528 | reset_qualifiers(base1);
|
---|
529 | reset_qualifiers(base2);
|
---|
530 |
|
---|
531 | if ( !unifyExact(
|
---|
532 | base1, base2, tenv, need, have, open, noWiden() )
|
---|
533 | ) return;
|
---|
534 | }
|
---|
535 | } else return;
|
---|
536 | } else if (auto ptr1 = dynamic_cast<const ast::PointerType *> (arg1)) {
|
---|
537 | if (auto ptr2 = dynamic_cast<const ast::PointerType *> (arg2)) {
|
---|
538 | ast::ptr<ast::Type> base1 = ptr1->base;
|
---|
539 | ast::ptr<ast::Type> base2 = ptr2->base;
|
---|
540 |
|
---|
541 | // xxx - assume LHS is always the target type
|
---|
542 | // a function accepting const can always be called by non-const arg
|
---|
543 |
|
---|
544 | if ( (widen.second && base1->qualifiers <= base2->qualifiers ) || (base2->qualifiers == base1->qualifiers) ) {
|
---|
545 |
|
---|
546 | reset_qualifiers(base1);
|
---|
547 | reset_qualifiers(base2);
|
---|
548 |
|
---|
549 | if ( ! unifyExact(
|
---|
550 | base1, base2, tenv, need, have, open, noWiden() )
|
---|
551 | ) return;
|
---|
552 | }
|
---|
553 | } else return;
|
---|
554 | } else if (! unifyExact(
|
---|
555 | arg1, arg2, tenv, need, have, open, noWiden() )) {
|
---|
556 | return;
|
---|
557 | }
|
---|
558 | ++crnt1; ++crnt2;
|
---|
559 | }
|
---|
560 | if ( crnt1 != end1 ) {
|
---|
561 | // try unifying empty tuple with ttype
|
---|
562 | const ast::Type * t1 = *crnt1;
|
---|
563 | if (! Tuples::isTtype( t1 ) ) return;
|
---|
564 | if (! unifyExact(
|
---|
565 | t1, tupleFromTypes( crnt2, end2 ), tenv, need, have, open,
|
---|
566 | noWiden() )) return;
|
---|
567 | } else if ( crnt2 != end2 ) {
|
---|
568 | // try unifying empty tuple with ttype
|
---|
569 | const ast::Type * t2 = *crnt2;
|
---|
570 | if ( !Tuples::isTtype( t2 ) ) return;
|
---|
571 | if ( !unifyExact(
|
---|
572 | tupleFromTypes( crnt1, end1 ), t2, tenv, need, have, open,
|
---|
573 | noWiden() )) return;
|
---|
574 | }
|
---|
575 | if ((f1->returns.size() == 0 && f2->returns.size() == 0)
|
---|
576 | || (f1->returns.size() == 1 && f2->returns.size() == 1 && unifyExact(f1->returns[0], f2->returns[0], tenv, need, have, open, noWiden()))) {
|
---|
577 | result = pointer;
|
---|
578 |
|
---|
579 | for (auto & assn : f1->assertions) {
|
---|
580 | auto i = need.find(assn);
|
---|
581 | if (i != need.end()) i->second.isUsed = true;
|
---|
582 | auto j = have.find(assn);
|
---|
583 | if (j != have.end()) j->second.isUsed = true;
|
---|
584 | }
|
---|
585 |
|
---|
586 | for (auto & assn : f2->assertions) {
|
---|
587 | auto i = need.find(assn);
|
---|
588 | if (i != need.end()) i->second.isUsed = true;
|
---|
589 | auto j = have.find(assn);
|
---|
590 | if (j != have.end()) j->second.isUsed = true;
|
---|
591 | }
|
---|
592 | }
|
---|
593 | } // if ftype
|
---|
594 | }
|
---|
595 | } else if ( widen.second && dynamic_cast< const ast::ZeroType * >( type2 ) ) {
|
---|
596 | result = pointer;
|
---|
597 | add_qualifiers( result, type2->qualifiers );
|
---|
598 | }
|
---|
599 | }
|
---|
600 |
|
---|
601 | void postvisit( const ast::ArrayType * ) {}
|
---|
602 |
|
---|
603 | void postvisit( const ast::ReferenceType * ref ) {
|
---|
604 | if ( auto ref2 = dynamic_cast< const ast::ReferenceType * >( type2 ) ) {
|
---|
605 | if (
|
---|
606 | widen.first && ref2->base.as< ast::VoidType >() && ! ast::isFtype( ref->base )
|
---|
607 | ) {
|
---|
608 | getCommonWithVoidPointer( ref2, ref );
|
---|
609 | } else if (
|
---|
610 | widen.second && ref->base.as< ast::VoidType>() && ! ast::isFtype( ref2->base )
|
---|
611 | ) {
|
---|
612 | getCommonWithVoidPointer( ref, ref2 );
|
---|
613 | } else if (
|
---|
614 | ( ref->base->qualifiers >= ref2->base->qualifiers || widen.first )
|
---|
615 | && ( ref->base->qualifiers <= ref2->base->qualifiers || widen.second )
|
---|
616 | ) {
|
---|
617 | ast::CV::Qualifiers q1 = ref->base->qualifiers, q2 = ref2->base->qualifiers;
|
---|
618 |
|
---|
619 | // force t{1,2} to be cloned if their qualifiers must be stripped, so that
|
---|
620 | // ref{,2}->base are unchanged
|
---|
621 | ast::ptr< ast::Type > t1{ ref->base }, t2{ ref2->base };
|
---|
622 | reset_qualifiers( t1 );
|
---|
623 | reset_qualifiers( t2 );
|
---|
624 |
|
---|
625 | ast::OpenVarSet newOpen{ open };
|
---|
626 | if ( unifyExact( t1, t2, tenv, have, need, newOpen, noWiden() ) ) {
|
---|
627 | result = ref;
|
---|
628 | if ( q1.val != q2.val ) {
|
---|
629 | // reset result->base->qualifiers to be union of two base qualifiers
|
---|
630 | strict_dynamic_cast< ast::ReferenceType * >(
|
---|
631 | result.get_and_mutate()
|
---|
632 | )->base.get_and_mutate()->qualifiers = q1 | q2;
|
---|
633 | }
|
---|
634 | }
|
---|
635 | }
|
---|
636 | } else if ( widen.second && dynamic_cast< const ast::ZeroType * >( type2 ) ) {
|
---|
637 | result = ref;
|
---|
638 | add_qualifiers( result, type2->qualifiers );
|
---|
639 | } else {
|
---|
640 | if (!dynamic_cast<const ast::EnumInstType *>(type2))
|
---|
641 | result = commonType( type2, ref, tenv, need, have, open, widen );
|
---|
642 | }
|
---|
643 | }
|
---|
644 |
|
---|
645 | void postvisit( const ast::FunctionType * ) {}
|
---|
646 |
|
---|
647 | void postvisit( const ast::StructInstType * ) {}
|
---|
648 |
|
---|
649 | void postvisit( const ast::UnionInstType * ) {}
|
---|
650 |
|
---|
651 | void postvisit( const ast::EnumInstType * enumInst ) {
|
---|
652 | if ( enumInst->base && !enumInst->base->base ) {
|
---|
653 | auto basicType = new ast::BasicType( ast::BasicKind::UnsignedInt );
|
---|
654 | result = commonType( basicType, type2, tenv, need, have, open, widen);
|
---|
655 | }
|
---|
656 | }
|
---|
657 |
|
---|
658 | void postvisit( const ast::EnumAttrType * ) {}
|
---|
659 |
|
---|
660 | void postvisit( const ast::TraitInstType * ) {}
|
---|
661 |
|
---|
662 | void postvisit( const ast::TypeInstType * ) {}
|
---|
663 |
|
---|
664 | void postvisit( const ast::TupleType * ) {}
|
---|
665 |
|
---|
666 | void postvisit( const ast::VarArgsType * ) {}
|
---|
667 |
|
---|
668 | void postvisit( const ast::ZeroType * zero ) {
|
---|
669 | if ( !widen.first ) return;
|
---|
670 | if ( dynamic_cast< const ast::BasicType * >( type2 )
|
---|
671 | || dynamic_cast< const ast::PointerType * >( type2 ) ) {
|
---|
672 | if ( widen.second || zero->qualifiers <= type2->qualifiers ) {
|
---|
673 | result = type2;
|
---|
674 | add_qualifiers( result, zero->qualifiers );
|
---|
675 | }
|
---|
676 | } else if ( widen.second && dynamic_cast< const ast::OneType * >( type2 ) ) {
|
---|
677 | result = new ast::BasicType{
|
---|
678 | ast::BasicKind::SignedInt, zero->qualifiers | type2->qualifiers };
|
---|
679 | } else if ( const ast::EnumInstType * enumInst = dynamic_cast< const ast::EnumInstType * >( type2 ) ) {
|
---|
680 | const ast::EnumDecl * enumDecl = enumInst->base;
|
---|
681 | if ( !enumDecl->base ) {
|
---|
682 | if ( widen.second || zero->qualifiers <= type2->qualifiers ) {
|
---|
683 | result = type2;
|
---|
684 | add_qualifiers( result, zero->qualifiers );
|
---|
685 | }
|
---|
686 | }
|
---|
687 | }
|
---|
688 | }
|
---|
689 |
|
---|
690 | void postvisit( const ast::OneType * one ) {
|
---|
691 | if ( !widen.first ) return;
|
---|
692 | if ( dynamic_cast< const ast::BasicType * >( type2 ) ) {
|
---|
693 | if ( widen.second || one->qualifiers <= type2->qualifiers ) {
|
---|
694 | result = type2;
|
---|
695 | add_qualifiers( result, one->qualifiers );
|
---|
696 | }
|
---|
697 | } else if ( widen.second && dynamic_cast< const ast::ZeroType * >( type2 ) ) {
|
---|
698 | result = new ast::BasicType{
|
---|
699 | ast::BasicKind::SignedInt, one->qualifiers | type2->qualifiers };
|
---|
700 | } else if ( const ast::EnumInstType * enumInst = dynamic_cast< const ast::EnumInstType * >( type2 ) ) {
|
---|
701 | const ast::EnumDecl * enumDecl = enumInst->base;
|
---|
702 | if ( !enumDecl->base ) {
|
---|
703 | if ( widen.second || one->qualifiers <= type2->qualifiers ) {
|
---|
704 | result = type2;
|
---|
705 | add_qualifiers( result, one->qualifiers );
|
---|
706 | }
|
---|
707 | }
|
---|
708 | }
|
---|
709 | }
|
---|
710 | };
|
---|
711 |
|
---|
712 | // size_t CommonType::traceId = Stats::Heap::new_stacktrace_id("CommonType");
|
---|
713 |
|
---|
714 | ast::ptr< ast::Type > handleReference(
|
---|
715 | const ast::ptr< ast::Type > & t1, const ast::ptr< ast::Type > & t2, WidenMode widen,
|
---|
716 | ast::TypeEnvironment & env,
|
---|
717 | const ast::OpenVarSet & open
|
---|
718 | ) {
|
---|
719 | ast::ptr<ast::Type> common;
|
---|
720 | ast::AssertionSet have, need;
|
---|
721 | ast::OpenVarSet newOpen( open );
|
---|
722 |
|
---|
723 | // need unify to bind type variables
|
---|
724 | if ( unify( t1, t2, env, have, need, newOpen, common ) ) {
|
---|
725 | ast::CV::Qualifiers q1 = t1->qualifiers, q2 = t2->qualifiers;
|
---|
726 | PRINT(
|
---|
727 | std::cerr << "unify success: " << widenFirst << " " << widenSecond << std::endl;
|
---|
728 | )
|
---|
729 | if ( ( widen.first || q2 <= q1 ) && ( widen.second || q1 <= q2 ) ) {
|
---|
730 | PRINT(
|
---|
731 | std::cerr << "widen okay" << std::endl;
|
---|
732 | )
|
---|
733 | add_qualifiers( common, q1 | q2 );
|
---|
734 | return common;
|
---|
735 | }
|
---|
736 | }
|
---|
737 |
|
---|
738 | PRINT(
|
---|
739 | std::cerr << "exact unify failed: " << t1 << " " << t2 << std::endl;
|
---|
740 | )
|
---|
741 | return { nullptr };
|
---|
742 | }
|
---|
743 |
|
---|
744 | } // namespace
|
---|
745 |
|
---|
746 | ast::ptr< ast::Type > commonType(
|
---|
747 | const ast::ptr< ast::Type > & type1, const ast::ptr< ast::Type > & type2,
|
---|
748 | ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have,
|
---|
749 | const ast::OpenVarSet & open, WidenMode widen
|
---|
750 | ) {
|
---|
751 | unsigned depth1 = type1->referenceDepth();
|
---|
752 | unsigned depth2 = type2->referenceDepth();
|
---|
753 |
|
---|
754 | if ( depth1 != depth2 ) { // implies depth1 > 0 || depth2 > 0
|
---|
755 | PRINT(
|
---|
756 | std::cerr << "reference depth diff: " << (depth1-depth2) << std::endl;
|
---|
757 | )
|
---|
758 | ast::ptr< ast::Type > result;
|
---|
759 | const ast::ReferenceType * ref1 = type1.as< ast::ReferenceType >();
|
---|
760 | const ast::ReferenceType * ref2 = type2.as< ast::ReferenceType >();
|
---|
761 |
|
---|
762 | if ( depth1 > depth2 ) {
|
---|
763 | assert( ref1 );
|
---|
764 | result = handleReference( ref1->base, type2, widen, env, open );
|
---|
765 | } else { // Implies depth1 < depth2
|
---|
766 | assert( ref2 );
|
---|
767 | result = handleReference( type1, ref2->base, widen, env, open );
|
---|
768 | }
|
---|
769 |
|
---|
770 | if ( result && ref1 ) {
|
---|
771 | // formal is reference, so result should be reference
|
---|
772 | PRINT(
|
---|
773 | std::cerr << "formal is reference; result should be reference" << std::endl;
|
---|
774 | )
|
---|
775 | result = new ast::ReferenceType{ result, ref1->qualifiers };
|
---|
776 | }
|
---|
777 |
|
---|
778 | PRINT(
|
---|
779 | std::cerr << "common type of reference [" << type1 << "] and [" << type2 << "] is "
|
---|
780 | "[" << result << "]" << std::endl;
|
---|
781 | )
|
---|
782 | return result;
|
---|
783 | }
|
---|
784 | // otherwise both are reference types of the same depth and this is handled by the visitor
|
---|
785 | return ast::Pass<CommonType>::read( type1.get(),
|
---|
786 | type2, widen, env, open, need, have );
|
---|
787 | }
|
---|
788 |
|
---|
789 | } // namespace ResolvExpr
|
---|
790 |
|
---|
791 | // Local Variables: //
|
---|
792 | // tab-width: 4 //
|
---|
793 | // mode: c++ //
|
---|
794 | // compile-command: "make install" //
|
---|
795 | // End: //
|
---|