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-2022 by The D Language Foundation, All Rights Reserved
10 * Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
11 * License: $(LINK2 https://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 TY[TMAX] typeTYs =
68 [
69 Tarray,
70 Tsarray,
71 Taarray,
72 Tpointer,
73 Treference,
74 Tfunction,
75 Tident,
76 Tclass,
77 Tstruct,
78 Tenum,
79 Tdelegate,
80 Tnone,
81 Tvoid,
82 Tint8,
83 Tuns8,
84 Tint16,
85 Tuns16,
86 Tint32,
87 Tuns32,
88 Tint64,
89 Tuns64,
90 Tfloat32,
91 Tfloat64,
92 Tfloat80,
93 Timaginary32,
94 Timaginary64,
95 Timaginary80,
96 Tcomplex32,
97 Tcomplex64,
98 Tcomplex80,
99 Tbool,
100 Tchar,
101 Twchar,
102 Tdchar,
103 Terror,
104 Tinstance,
105 Ttypeof,
106 Ttuple,
107 Tslice,
108 Treturn,
109 Tnull,
110 Tvector,
111 Tint128,
112 Tuns128,
113 Ttraits,
114 Tmixin,
115 Tnoreturn,
116 Ttag,
117 ];
118 ImpCnvTab impCnvTab;
119
120 // Set conversion tables
121 foreach (i; 0 .. cast(size_t)TMAX)
122 {
123 foreach (j; 0 .. cast(size_t)TMAX)
124 {
125 impCnvTab.impcnvResultTab[i][j] = Terror;
126 impCnvTab.impcnvType1Tab[i][j] = Terror;
127 }
128 }
129
130 void X(TY t1, TY t2, TY nt1, TY nt2, TY rt)
131 {
132 impCnvTab.impcnvResultTab[t1][t2] = rt;
133 impCnvTab.impcnvResultTab[t2][t1] = rt;
134
135 impCnvTab.impcnvType1Tab[t1][t2] = nt1;
136 impCnvTab.impcnvType1Tab[t2][t1] = nt2;
137 }
138
139 /* ======================= */
140
141 X(Tbool,Tbool, Tbool,Tbool, Tbool);
142 X(Tbool,Tint8, Tint32,Tint32, Tint32);
143 X(Tbool,Tuns8, Tint32,Tint32, Tint32);
144 X(Tbool,Tint16, Tint32,Tint32, Tint32);
145 X(Tbool,Tuns16, Tint32,Tint32, Tint32);
146 X(Tbool,Tint32, Tint32,Tint32, Tint32);
147 X(Tbool,Tuns32, Tuns32,Tuns32, Tuns32);
148 X(Tbool,Tint64, Tint64,Tint64, Tint64);
149 X(Tbool,Tuns64, Tuns64,Tuns64, Tuns64);
150 X(Tbool,Tint128, Tint128,Tint128, Tint128);
151 X(Tbool,Tuns128, Tuns128,Tuns128, Tuns128);
152
153 X(Tbool,Tfloat32, Tfloat32,Tfloat32, Tfloat32);
154 X(Tbool,Tfloat64, Tfloat64,Tfloat64, Tfloat64);
155 X(Tbool,Tfloat80, Tfloat80,Tfloat80, Tfloat80);
156 X(Tbool,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
157 X(Tbool,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
158 X(Tbool,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
159 X(Tbool,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32);
160 X(Tbool,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64);
161 X(Tbool,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80);
162
163 /* ======================= */
164
165 X(Tint8,Tint8, Tint32,Tint32, Tint32);
166 X(Tint8,Tuns8, Tint32,Tint32, Tint32);
167 X(Tint8,Tint16, Tint32,Tint32, Tint32);
168 X(Tint8,Tuns16, Tint32,Tint32, Tint32);
169 X(Tint8,Tint32, Tint32,Tint32, Tint32);
170 X(Tint8,Tuns32, Tuns32,Tuns32, Tuns32);
171 X(Tint8,Tint64, Tint64,Tint64, Tint64);
172 X(Tint8,Tuns64, Tuns64,Tuns64, Tuns64);
173 X(Tint8,Tint128, Tint128,Tint128, Tint128);
174 X(Tint8,Tuns128, Tuns128,Tuns128, Tuns128);
175
176 X(Tint8,Tfloat32, Tfloat32,Tfloat32, Tfloat32);
177 X(Tint8,Tfloat64, Tfloat64,Tfloat64, Tfloat64);
178 X(Tint8,Tfloat80, Tfloat80,Tfloat80, Tfloat80);
179 X(Tint8,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
180 X(Tint8,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
181 X(Tint8,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
182 X(Tint8,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32);
183 X(Tint8,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64);
184 X(Tint8,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80);
185
186 /* ======================= */
187
188 X(Tuns8,Tuns8, Tint32,Tint32, Tint32);
189 X(Tuns8,Tint16, Tint32,Tint32, Tint32);
190 X(Tuns8,Tuns16, Tint32,Tint32, Tint32);
191 X(Tuns8,Tint32, Tint32,Tint32, Tint32);
192 X(Tuns8,Tuns32, Tuns32,Tuns32, Tuns32);
193 X(Tuns8,Tint64, Tint64,Tint64, Tint64);
194 X(Tuns8,Tuns64, Tuns64,Tuns64, Tuns64);
195 X(Tuns8,Tint128, Tint128,Tint128, Tint128);
196 X(Tuns8,Tuns128, Tuns128,Tuns128, Tuns128);
197
198 X(Tuns8,Tfloat32, Tfloat32,Tfloat32, Tfloat32);
199 X(Tuns8,Tfloat64, Tfloat64,Tfloat64, Tfloat64);
200 X(Tuns8,Tfloat80, Tfloat80,Tfloat80, Tfloat80);
201 X(Tuns8,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
202 X(Tuns8,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
203 X(Tuns8,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
204 X(Tuns8,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32);
205 X(Tuns8,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64);
206 X(Tuns8,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80);
207
208 /* ======================= */
209
210 X(Tint16,Tint16, Tint32,Tint32, Tint32);
211 X(Tint16,Tuns16, Tint32,Tint32, Tint32);
212 X(Tint16,Tint32, Tint32,Tint32, Tint32);
213 X(Tint16,Tuns32, Tuns32,Tuns32, Tuns32);
214 X(Tint16,Tint64, Tint64,Tint64, Tint64);
215 X(Tint16,Tuns64, Tuns64,Tuns64, Tuns64);
216 X(Tint16,Tint128, Tint128,Tint128, Tint128);
217 X(Tint16,Tuns128, Tuns128,Tuns128, Tuns128);
218
219 X(Tint16,Tfloat32, Tfloat32,Tfloat32, Tfloat32);
220 X(Tint16,Tfloat64, Tfloat64,Tfloat64, Tfloat64);
221 X(Tint16,Tfloat80, Tfloat80,Tfloat80, Tfloat80);
222 X(Tint16,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
223 X(Tint16,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
224 X(Tint16,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
225 X(Tint16,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32);
226 X(Tint16,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64);
227 X(Tint16,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80);
228
229 /* ======================= */
230
231 X(Tuns16,Tuns16, Tint32,Tint32, Tint32);
232 X(Tuns16,Tint32, Tint32,Tint32, Tint32);
233 X(Tuns16,Tuns32, Tuns32,Tuns32, Tuns32);
234 X(Tuns16,Tint64, Tint64,Tint64, Tint64);
235 X(Tuns16,Tuns64, Tuns64,Tuns64, Tuns64);
236 X(Tuns16,Tint128, Tint128,Tint128, Tint128);
237 X(Tuns16,Tuns128, Tuns128,Tuns128, Tuns128);
238
239 X(Tuns16,Tfloat32, Tfloat32,Tfloat32, Tfloat32);
240 X(Tuns16,Tfloat64, Tfloat64,Tfloat64, Tfloat64);
241 X(Tuns16,Tfloat80, Tfloat80,Tfloat80, Tfloat80);
242 X(Tuns16,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
243 X(Tuns16,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
244 X(Tuns16,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
245 X(Tuns16,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32);
246 X(Tuns16,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64);
247 X(Tuns16,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80);
248
249 /* ======================= */
250
251 X(Tint32,Tint32, Tint32,Tint32, Tint32);
252 X(Tint32,Tuns32, Tuns32,Tuns32, Tuns32);
253 X(Tint32,Tint64, Tint64,Tint64, Tint64);
254 X(Tint32,Tuns64, Tuns64,Tuns64, Tuns64);
255 X(Tint32,Tint128, Tint128,Tint128, Tint128);
256 X(Tint32,Tuns128, Tuns128,Tuns128, Tuns128);
257
258 X(Tint32,Tfloat32, Tfloat32,Tfloat32, Tfloat32);
259 X(Tint32,Tfloat64, Tfloat64,Tfloat64, Tfloat64);
260 X(Tint32,Tfloat80, Tfloat80,Tfloat80, Tfloat80);
261 X(Tint32,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
262 X(Tint32,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
263 X(Tint32,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
264 X(Tint32,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32);
265 X(Tint32,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64);
266 X(Tint32,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80);
267
268 /* ======================= */
269
270 X(Tuns32,Tuns32, Tuns32,Tuns32, Tuns32);
271 X(Tuns32,Tint64, Tint64,Tint64, Tint64);
272 X(Tuns32,Tuns64, Tuns64,Tuns64, Tuns64);
273 X(Tuns32,Tint128, Tint128,Tint128, Tint128);
274 X(Tuns32,Tuns128, Tuns128,Tuns128, Tuns128);
275
276 X(Tuns32,Tfloat32, Tfloat32,Tfloat32, Tfloat32);
277 X(Tuns32,Tfloat64, Tfloat64,Tfloat64, Tfloat64);
278 X(Tuns32,Tfloat80, Tfloat80,Tfloat80, Tfloat80);
279 X(Tuns32,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
280 X(Tuns32,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
281 X(Tuns32,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
282 X(Tuns32,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32);
283 X(Tuns32,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64);
284 X(Tuns32,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80);
285
286 /* ======================= */
287
288 X(Tint64,Tint64, Tint64,Tint64, Tint64);
289 X(Tint64,Tuns64, Tuns64,Tuns64, Tuns64);
290 X(Tint64,Tint128, Tint128,Tint128, Tint128);
291 X(Tint64,Tuns128, Tuns128,Tuns128, Tuns128);
292
293 X(Tint64,Tfloat32, Tfloat32,Tfloat32, Tfloat32);
294 X(Tint64,Tfloat64, Tfloat64,Tfloat64, Tfloat64);
295 X(Tint64,Tfloat80, Tfloat80,Tfloat80, Tfloat80);
296 X(Tint64,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
297 X(Tint64,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
298 X(Tint64,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
299 X(Tint64,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32);
300 X(Tint64,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64);
301 X(Tint64,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80);
302
303 /* ======================= */
304
305 X(Tuns64,Tuns64, Tuns64,Tuns64, Tuns64);
306 X(Tuns64,Tint128, Tint128,Tint128, Tint128);
307 X(Tuns64,Tuns128, Tuns128,Tuns128, Tuns128);
308
309 X(Tuns64,Tfloat32, Tfloat32,Tfloat32, Tfloat32);
310 X(Tuns64,Tfloat64, Tfloat64,Tfloat64, Tfloat64);
311 X(Tuns64,Tfloat80, Tfloat80,Tfloat80, Tfloat80);
312 X(Tuns64,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
313 X(Tuns64,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
314 X(Tuns64,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
315 X(Tuns64,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32);
316 X(Tuns64,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64);
317 X(Tuns64,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80);
318
319 /* ======================= */
320
321 X(Tint128,Tint128, Tint128,Tint128, Tint128);
322 X(Tint128,Tuns128, Tuns128,Tuns128, Tuns128);
323
324 X(Tint128,Tfloat32, Tfloat32,Tfloat32, Tfloat32);
325 X(Tint128,Tfloat64, Tfloat64,Tfloat64, Tfloat64);
326 X(Tint128,Tfloat80, Tfloat80,Tfloat80, Tfloat80);
327 X(Tint128,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
328 X(Tint128,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
329 X(Tint128,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
330 X(Tint128,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32);
331 X(Tint128,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64);
332 X(Tint128,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80);
333
334 /* ======================= */
335
336 X(Tuns128,Tuns128, Tuns128,Tuns128, Tuns128);
337
338 X(Tuns128,Tfloat32, Tfloat32,Tfloat32, Tfloat32);
339 X(Tuns128,Tfloat64, Tfloat64,Tfloat64, Tfloat64);
340 X(Tuns128,Tfloat80, Tfloat80,Tfloat80, Tfloat80);
341 X(Tuns128,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
342 X(Tuns128,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
343 X(Tuns128,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
344 X(Tuns128,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32);
345 X(Tuns128,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64);
346 X(Tuns128,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80);
347
348 /* ======================= */
349
350 X(Tfloat32,Tfloat32, Tfloat32,Tfloat32, Tfloat32);
351 X(Tfloat32,Tfloat64, Tfloat64,Tfloat64, Tfloat64);
352 X(Tfloat32,Tfloat80, Tfloat80,Tfloat80, Tfloat80);
353
354 X(Tfloat32,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
355 X(Tfloat32,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
356 X(Tfloat32,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
357
358 X(Tfloat32,Tcomplex32, Tfloat32,Tcomplex32, Tcomplex32);
359 X(Tfloat32,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64);
360 X(Tfloat32,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80);
361
362 /* ======================= */
363
364 X(Tfloat64,Tfloat64, Tfloat64,Tfloat64, Tfloat64);
365 X(Tfloat64,Tfloat80, Tfloat80,Tfloat80, Tfloat80);
366
367 X(Tfloat64,Timaginary32, Tfloat64,Timaginary64, Tfloat64);
368 X(Tfloat64,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
369 X(Tfloat64,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
370
371 X(Tfloat64,Tcomplex32, Tfloat64,Tcomplex64, Tcomplex64);
372 X(Tfloat64,Tcomplex64, Tfloat64,Tcomplex64, Tcomplex64);
373 X(Tfloat64,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80);
374
375 /* ======================= */
376
377 X(Tfloat80,Tfloat80, Tfloat80,Tfloat80, Tfloat80);
378
379 X(Tfloat80,Timaginary32, Tfloat80,Timaginary80, Tfloat80);
380 X(Tfloat80,Timaginary64, Tfloat80,Timaginary80, Tfloat80);
381 X(Tfloat80,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
382
383 X(Tfloat80,Tcomplex32, Tfloat80,Tcomplex80, Tcomplex80);
384 X(Tfloat80,Tcomplex64, Tfloat80,Tcomplex80, Tcomplex80);
385 X(Tfloat80,Tcomplex80, Tfloat80,Tcomplex80, Tcomplex80);
386
387 /* ======================= */
388
389 X(Timaginary32,Timaginary32, Timaginary32,Timaginary32, Timaginary32);
390 X(Timaginary32,Timaginary64, Timaginary64,Timaginary64, Timaginary64);
391 X(Timaginary32,Timaginary80, Timaginary80,Timaginary80, Timaginary80);
392
393 X(Timaginary32,Tcomplex32, Timaginary32,Tcomplex32, Tcomplex32);
394 X(Timaginary32,Tcomplex64, Timaginary64,Tcomplex64, Tcomplex64);
395 X(Timaginary32,Tcomplex80, Timaginary80,Tcomplex80, Tcomplex80);
396
397 /* ======================= */
398
399 X(Timaginary64,Timaginary64, Timaginary64,Timaginary64, Timaginary64);
400 X(Timaginary64,Timaginary80, Timaginary80,Timaginary80, Timaginary80);
401
402 X(Timaginary64,Tcomplex32, Timaginary64,Tcomplex64, Tcomplex64);
403 X(Timaginary64,Tcomplex64, Timaginary64,Tcomplex64, Tcomplex64);
404 X(Timaginary64,Tcomplex80, Timaginary80,Tcomplex80, Tcomplex80);
405
406 /* ======================= */
407
408 X(Timaginary80,Timaginary80, Timaginary80,Timaginary80, Timaginary80);
409
410 X(Timaginary80,Tcomplex32, Timaginary80,Tcomplex80, Tcomplex80);
411 X(Timaginary80,Tcomplex64, Timaginary80,Tcomplex80, Tcomplex80);
412 X(Timaginary80,Tcomplex80, Timaginary80,Tcomplex80, Tcomplex80);
413
414 /* ======================= */
415
416 X(Tcomplex32,Tcomplex32, Tcomplex32,Tcomplex32, Tcomplex32);
417 X(Tcomplex32,Tcomplex64, Tcomplex64,Tcomplex64, Tcomplex64);
418 X(Tcomplex32,Tcomplex80, Tcomplex80,Tcomplex80, Tcomplex80);
419
420 /* ======================= */
421
422 X(Tcomplex64,Tcomplex64, Tcomplex64,Tcomplex64, Tcomplex64);
423 X(Tcomplex64,Tcomplex80, Tcomplex80,Tcomplex80, Tcomplex80);
424
425 /* ======================= */
426
427 X(Tcomplex80,Tcomplex80, Tcomplex80,Tcomplex80, Tcomplex80);
428
429 // "No type is implicitly convertible to noreturn, but noreturn is implicitly convertible to every other type"
430 foreach(convertToTy; typeTYs)
431 X(Tnoreturn, convertToTy, convertToTy, convertToTy, convertToTy);
432
433 return impCnvTab;
434 }
435