1 /**
2  * Provides an implicit conversion table for basic types.
3  *
4  * Used to determine integer promotions and common types.
5  *
6  * Specification: $(LINK2 https://dlang.org/spec/type.html#integer-promotions, Integer Promotions),
7  *   $(LINK2 https://dlang.org/spec/type.html#usual-arithmetic-conversions, Usual Arithmetic Conversions).
8  *
9  * Copyright:   Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved
10  * Authors:     $(LINK2 http://www.digitalmars.com, Walter Bright)
11  * License:     $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
12  * Source:      $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/impcnvtab.d, _impcnvtab.d)
13  * Documentation:  https://dlang.org/phobos/dmd_impcnvtab.html
14  * Coverage:    https://codecov.io/gh/dlang/dmd/src/master/src/dmd/impcnvtab.d
15  */
16 
17 module dmd.impcnvtab;
18 
19 import dmd.astenums;
20 import dmd.mtype;
21 
22 pure @nogc nothrow @safe:
23 
24 /*************************************************
25  * If ty1 and ty2 are basic types, return the TY that both can
26  * be implicitly converted to.
27  * Params:
28  *      ty1 = first operand type
29  *      ty2 = second operand type
30  * Returns:
31  *      ty = common type, else Terror
32  */
implicitConvCommonTy(TY ty1,TY ty2)33 TY implicitConvCommonTy(TY ty1, TY ty2)
34 {
35     return impCnvTab.impcnvResultTab[ty1][ty2];
36 }
37 
38 /*************************************************
39  * If ty1 and ty2 are basic types, return the TY that ty1 can
40  * be implicitly converted to to bring them to a common ty.
41  * It's symmetric, i.e. the operands can be swapped.
42  * Params:
43  *      ty1 = first operand type
44  *      ty2 = second operand type
45  * Returns:
46  *      ty = what ty1 should be converted to, else Terror
47  */
implicitConvTy1(TY ty1,TY ty2)48 TY implicitConvTy1(TY ty1, TY ty2)
49 {
50     return impCnvTab.impcnvType1Tab[ty1][ty2];
51 }
52 
53 /******************************************************************************/
54 
55 private:
56 
57 struct ImpCnvTab
58 {
59     TY[TMAX][TMAX] impcnvResultTab;
60     TY[TMAX][TMAX] impcnvType1Tab;
61 }
62 
63 enum ImpCnvTab impCnvTab = generateImpCnvTab();
64 
generateImpCnvTab()65 ImpCnvTab generateImpCnvTab()
66 {
67     ImpCnvTab impCnvTab;
68 
69     // Set conversion tables
70     foreach (i; 0 .. cast(size_t)TMAX)
71     {
72         foreach (j; 0 .. cast(size_t)TMAX)
73         {
74             impCnvTab.impcnvResultTab[i][j] = Terror;
75             impCnvTab.impcnvType1Tab[i][j] = Terror;
76         }
77     }
78 
79     void X(TY t1, TY t2, TY nt1, TY nt2, TY rt)
80     {
81         impCnvTab.impcnvResultTab[t1][t2] = rt;
82         impCnvTab.impcnvResultTab[t2][t1] = rt;
83 
84         impCnvTab.impcnvType1Tab[t1][t2] = nt1;
85         impCnvTab.impcnvType1Tab[t2][t1] = nt2;
86     }
87 
88     /* ======================= */
89 
90     X(Tbool,Tbool,   Tbool,Tbool,    Tbool);
91     X(Tbool,Tint8,   Tint32,Tint32,  Tint32);
92     X(Tbool,Tuns8,   Tint32,Tint32,  Tint32);
93     X(Tbool,Tint16,  Tint32,Tint32,  Tint32);
94     X(Tbool,Tuns16,  Tint32,Tint32,  Tint32);
95     X(Tbool,Tint32,  Tint32,Tint32,  Tint32);
96     X(Tbool,Tuns32,  Tuns32,Tuns32,  Tuns32);
97     X(Tbool,Tint64,  Tint64,Tint64,  Tint64);
98     X(Tbool,Tuns64,  Tuns64,Tuns64,  Tuns64);
99     X(Tbool,Tint128, Tint128,Tint128, Tint128);
100     X(Tbool,Tuns128, Tuns128,Tuns128, Tuns128);
101 
102     X(Tbool,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
103     X(Tbool,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
104     X(Tbool,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
105     X(Tbool,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
106     X(Tbool,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
107     X(Tbool,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
108     X(Tbool,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
109     X(Tbool,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
110     X(Tbool,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
111 
112     /* ======================= */
113 
114     X(Tint8,Tint8,   Tint32,Tint32,  Tint32);
115     X(Tint8,Tuns8,   Tint32,Tint32,  Tint32);
116     X(Tint8,Tint16,  Tint32,Tint32,  Tint32);
117     X(Tint8,Tuns16,  Tint32,Tint32,  Tint32);
118     X(Tint8,Tint32,  Tint32,Tint32,  Tint32);
119     X(Tint8,Tuns32,  Tuns32,Tuns32,  Tuns32);
120     X(Tint8,Tint64,  Tint64,Tint64,  Tint64);
121     X(Tint8,Tuns64,  Tuns64,Tuns64,  Tuns64);
122     X(Tint8,Tint128, Tint128,Tint128, Tint128);
123     X(Tint8,Tuns128, Tuns128,Tuns128, Tuns128);
124 
125     X(Tint8,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
126     X(Tint8,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
127     X(Tint8,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
128     X(Tint8,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
129     X(Tint8,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
130     X(Tint8,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
131     X(Tint8,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
132     X(Tint8,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
133     X(Tint8,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
134 
135     /* ======================= */
136 
137     X(Tuns8,Tuns8,   Tint32,Tint32,  Tint32);
138     X(Tuns8,Tint16,  Tint32,Tint32,  Tint32);
139     X(Tuns8,Tuns16,  Tint32,Tint32,  Tint32);
140     X(Tuns8,Tint32,  Tint32,Tint32,  Tint32);
141     X(Tuns8,Tuns32,  Tuns32,Tuns32,  Tuns32);
142     X(Tuns8,Tint64,  Tint64,Tint64,  Tint64);
143     X(Tuns8,Tuns64,  Tuns64,Tuns64,  Tuns64);
144     X(Tuns8,Tint128,  Tint128,Tint128,  Tint128);
145     X(Tuns8,Tuns128,  Tuns128,Tuns128,  Tuns128);
146 
147     X(Tuns8,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
148     X(Tuns8,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
149     X(Tuns8,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
150     X(Tuns8,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
151     X(Tuns8,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
152     X(Tuns8,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
153     X(Tuns8,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
154     X(Tuns8,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
155     X(Tuns8,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
156 
157     /* ======================= */
158 
159     X(Tint16,Tint16,  Tint32,Tint32,  Tint32);
160     X(Tint16,Tuns16,  Tint32,Tint32,  Tint32);
161     X(Tint16,Tint32,  Tint32,Tint32,  Tint32);
162     X(Tint16,Tuns32,  Tuns32,Tuns32,  Tuns32);
163     X(Tint16,Tint64,  Tint64,Tint64,  Tint64);
164     X(Tint16,Tuns64,  Tuns64,Tuns64,  Tuns64);
165     X(Tint16,Tint128,  Tint128,Tint128,  Tint128);
166     X(Tint16,Tuns128,  Tuns128,Tuns128,  Tuns128);
167 
168     X(Tint16,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
169     X(Tint16,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
170     X(Tint16,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
171     X(Tint16,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
172     X(Tint16,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
173     X(Tint16,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
174     X(Tint16,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
175     X(Tint16,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
176     X(Tint16,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
177 
178     /* ======================= */
179 
180     X(Tuns16,Tuns16,  Tint32,Tint32,  Tint32);
181     X(Tuns16,Tint32,  Tint32,Tint32,  Tint32);
182     X(Tuns16,Tuns32,  Tuns32,Tuns32,  Tuns32);
183     X(Tuns16,Tint64,  Tint64,Tint64,  Tint64);
184     X(Tuns16,Tuns64,  Tuns64,Tuns64,  Tuns64);
185     X(Tuns16,Tint128, Tint128,Tint128,  Tint128);
186     X(Tuns16,Tuns128, Tuns128,Tuns128,  Tuns128);
187 
188     X(Tuns16,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
189     X(Tuns16,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
190     X(Tuns16,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
191     X(Tuns16,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
192     X(Tuns16,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
193     X(Tuns16,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
194     X(Tuns16,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
195     X(Tuns16,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
196     X(Tuns16,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
197 
198     /* ======================= */
199 
200     X(Tint32,Tint32,  Tint32,Tint32,  Tint32);
201     X(Tint32,Tuns32,  Tuns32,Tuns32,  Tuns32);
202     X(Tint32,Tint64,  Tint64,Tint64,  Tint64);
203     X(Tint32,Tuns64,  Tuns64,Tuns64,  Tuns64);
204     X(Tint32,Tint128, Tint128,Tint128,  Tint128);
205     X(Tint32,Tuns128, Tuns128,Tuns128,  Tuns128);
206 
207     X(Tint32,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
208     X(Tint32,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
209     X(Tint32,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
210     X(Tint32,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
211     X(Tint32,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
212     X(Tint32,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
213     X(Tint32,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
214     X(Tint32,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
215     X(Tint32,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
216 
217     /* ======================= */
218 
219     X(Tuns32,Tuns32,  Tuns32,Tuns32,  Tuns32);
220     X(Tuns32,Tint64,  Tint64,Tint64,  Tint64);
221     X(Tuns32,Tuns64,  Tuns64,Tuns64,  Tuns64);
222     X(Tuns32,Tint128,  Tint128,Tint128,  Tint128);
223     X(Tuns32,Tuns128,  Tuns128,Tuns128,  Tuns128);
224 
225     X(Tuns32,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
226     X(Tuns32,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
227     X(Tuns32,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
228     X(Tuns32,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
229     X(Tuns32,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
230     X(Tuns32,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
231     X(Tuns32,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
232     X(Tuns32,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
233     X(Tuns32,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
234 
235     /* ======================= */
236 
237     X(Tint64,Tint64,  Tint64,Tint64,  Tint64);
238     X(Tint64,Tuns64,  Tuns64,Tuns64,  Tuns64);
239     X(Tint64,Tint128,  Tint128,Tint128,  Tint128);
240     X(Tint64,Tuns128,  Tuns128,Tuns128,  Tuns128);
241 
242     X(Tint64,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
243     X(Tint64,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
244     X(Tint64,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
245     X(Tint64,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
246     X(Tint64,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
247     X(Tint64,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
248     X(Tint64,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
249     X(Tint64,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
250     X(Tint64,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
251 
252     /* ======================= */
253 
254     X(Tuns64,Tuns64,  Tuns64,Tuns64,  Tuns64);
255     X(Tuns64,Tint128,  Tint128,Tint128,  Tint128);
256     X(Tuns64,Tuns128,  Tuns128,Tuns128,  Tuns128);
257 
258     X(Tuns64,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
259     X(Tuns64,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
260     X(Tuns64,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
261     X(Tuns64,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
262     X(Tuns64,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
263     X(Tuns64,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
264     X(Tuns64,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
265     X(Tuns64,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
266     X(Tuns64,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
267 
268     /* ======================= */
269 
270     X(Tint128,Tint128,  Tint128,Tint128,  Tint128);
271     X(Tint128,Tuns128,  Tuns128,Tuns128,  Tuns128);
272 
273     X(Tint128,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
274     X(Tint128,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
275     X(Tint128,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
276     X(Tint128,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
277     X(Tint128,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
278     X(Tint128,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
279     X(Tint128,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
280     X(Tint128,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
281     X(Tint128,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
282 
283     /* ======================= */
284 
285     X(Tuns128,Tuns128,  Tuns128,Tuns128,  Tuns128);
286 
287     X(Tuns128,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
288     X(Tuns128,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
289     X(Tuns128,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
290     X(Tuns128,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
291     X(Tuns128,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
292     X(Tuns128,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
293     X(Tuns128,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
294     X(Tuns128,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
295     X(Tuns128,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
296 
297     /* ======================= */
298 
299     X(Tfloat32,Tfloat32,  Tfloat32,Tfloat32, Tfloat32);
300     X(Tfloat32,Tfloat64,  Tfloat64,Tfloat64, Tfloat64);
301     X(Tfloat32,Tfloat80,  Tfloat80,Tfloat80, Tfloat80);
302 
303     X(Tfloat32,Timaginary32,  Tfloat32,Timaginary32, Tfloat32);
304     X(Tfloat32,Timaginary64,  Tfloat64,Timaginary64, Tfloat64);
305     X(Tfloat32,Timaginary80,  Tfloat80,Timaginary80, Tfloat80);
306 
307     X(Tfloat32,Tcomplex32,  Tfloat32,Tcomplex32, Tcomplex32);
308     X(Tfloat32,Tcomplex64,  Tfloat64,Tcomplex64, Tcomplex64);
309     X(Tfloat32,Tcomplex80,  Tfloat80,Tcomplex80, Tcomplex80);
310 
311     /* ======================= */
312 
313     X(Tfloat64,Tfloat64,  Tfloat64,Tfloat64, Tfloat64);
314     X(Tfloat64,Tfloat80,  Tfloat80,Tfloat80, Tfloat80);
315 
316     X(Tfloat64,Timaginary32,  Tfloat64,Timaginary64, Tfloat64);
317     X(Tfloat64,Timaginary64,  Tfloat64,Timaginary64, Tfloat64);
318     X(Tfloat64,Timaginary80,  Tfloat80,Timaginary80, Tfloat80);
319 
320     X(Tfloat64,Tcomplex32,  Tfloat64,Tcomplex64, Tcomplex64);
321     X(Tfloat64,Tcomplex64,  Tfloat64,Tcomplex64, Tcomplex64);
322     X(Tfloat64,Tcomplex80,  Tfloat80,Tcomplex80, Tcomplex80);
323 
324     /* ======================= */
325 
326     X(Tfloat80,Tfloat80,  Tfloat80,Tfloat80, Tfloat80);
327 
328     X(Tfloat80,Timaginary32,  Tfloat80,Timaginary80, Tfloat80);
329     X(Tfloat80,Timaginary64,  Tfloat80,Timaginary80, Tfloat80);
330     X(Tfloat80,Timaginary80,  Tfloat80,Timaginary80, Tfloat80);
331 
332     X(Tfloat80,Tcomplex32,  Tfloat80,Tcomplex80, Tcomplex80);
333     X(Tfloat80,Tcomplex64,  Tfloat80,Tcomplex80, Tcomplex80);
334     X(Tfloat80,Tcomplex80,  Tfloat80,Tcomplex80, Tcomplex80);
335 
336     /* ======================= */
337 
338     X(Timaginary32,Timaginary32,  Timaginary32,Timaginary32, Timaginary32);
339     X(Timaginary32,Timaginary64,  Timaginary64,Timaginary64, Timaginary64);
340     X(Timaginary32,Timaginary80,  Timaginary80,Timaginary80, Timaginary80);
341 
342     X(Timaginary32,Tcomplex32,  Timaginary32,Tcomplex32, Tcomplex32);
343     X(Timaginary32,Tcomplex64,  Timaginary64,Tcomplex64, Tcomplex64);
344     X(Timaginary32,Tcomplex80,  Timaginary80,Tcomplex80, Tcomplex80);
345 
346     /* ======================= */
347 
348     X(Timaginary64,Timaginary64,  Timaginary64,Timaginary64, Timaginary64);
349     X(Timaginary64,Timaginary80,  Timaginary80,Timaginary80, Timaginary80);
350 
351     X(Timaginary64,Tcomplex32,  Timaginary64,Tcomplex64, Tcomplex64);
352     X(Timaginary64,Tcomplex64,  Timaginary64,Tcomplex64, Tcomplex64);
353     X(Timaginary64,Tcomplex80,  Timaginary80,Tcomplex80, Tcomplex80);
354 
355     /* ======================= */
356 
357     X(Timaginary80,Timaginary80,  Timaginary80,Timaginary80, Timaginary80);
358 
359     X(Timaginary80,Tcomplex32,  Timaginary80,Tcomplex80, Tcomplex80);
360     X(Timaginary80,Tcomplex64,  Timaginary80,Tcomplex80, Tcomplex80);
361     X(Timaginary80,Tcomplex80,  Timaginary80,Tcomplex80, Tcomplex80);
362 
363     /* ======================= */
364 
365     X(Tcomplex32,Tcomplex32,  Tcomplex32,Tcomplex32, Tcomplex32);
366     X(Tcomplex32,Tcomplex64,  Tcomplex64,Tcomplex64, Tcomplex64);
367     X(Tcomplex32,Tcomplex80,  Tcomplex80,Tcomplex80, Tcomplex80);
368 
369     /* ======================= */
370 
371     X(Tcomplex64,Tcomplex64,  Tcomplex64,Tcomplex64, Tcomplex64);
372     X(Tcomplex64,Tcomplex80,  Tcomplex80,Tcomplex80, Tcomplex80);
373 
374     /* ======================= */
375 
376     X(Tcomplex80,Tcomplex80,  Tcomplex80,Tcomplex80, Tcomplex80);
377 
378     return impCnvTab;
379 }
380