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-2020 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.mtype;
20 
21 immutable ENUMTY[TMAX][TMAX] impcnvResult = impCnvTab.impcnvResultTab;
22 immutable ENUMTY[TMAX][TMAX] impcnvType1 = impCnvTab.impcnvType1Tab;
23 immutable ENUMTY[TMAX][TMAX] impcnvType2 = impCnvTab.impcnvType2Tab;
24 
25 private:
26 
27 struct ImpCnvTab
28 {
29     ENUMTY[TMAX][TMAX] impcnvResultTab;
30     ENUMTY[TMAX][TMAX] impcnvType1Tab;
31     ENUMTY[TMAX][TMAX] impcnvType2Tab;
32 }
33 
34 enum ImpCnvTab impCnvTab = generateImpCnvTab();
35 
36 ImpCnvTab generateImpCnvTab()
37 {
38     ImpCnvTab impCnvTab;
39 
40     // Set conversion tables
41     foreach (i; 0 .. cast(size_t)TMAX)
42     {
43         foreach (j; 0 .. cast(size_t)TMAX)
44         {
45             impCnvTab.impcnvResultTab[i][j] = Terror;
46             impCnvTab.impcnvType1Tab[i][j] = Terror;
47             impCnvTab.impcnvType2Tab[i][j] = Terror;
48         }
49     }
50 
51     void X(ENUMTY t1, ENUMTY t2, ENUMTY nt1, ENUMTY nt2, ENUMTY rt)
52     {
53         impCnvTab.impcnvResultTab[t1][t2] = rt;
54         impCnvTab.impcnvType1Tab[t1][t2] = nt1;
55         impCnvTab.impcnvType2Tab[t1][t2] = nt2;
56     }
57 
58     /* ======================= */
59 
60     X(Tbool,Tbool,   Tbool,Tbool,    Tbool);
61     X(Tbool,Tint8,   Tint32,Tint32,  Tint32);
62     X(Tbool,Tuns8,   Tint32,Tint32,  Tint32);
63     X(Tbool,Tint16,  Tint32,Tint32,  Tint32);
64     X(Tbool,Tuns16,  Tint32,Tint32,  Tint32);
65     X(Tbool,Tint32,  Tint32,Tint32,  Tint32);
66     X(Tbool,Tuns32,  Tuns32,Tuns32,  Tuns32);
67     X(Tbool,Tint64,  Tint64,Tint64,  Tint64);
68     X(Tbool,Tuns64,  Tuns64,Tuns64,  Tuns64);
69     X(Tbool,Tint128, Tint128,Tint128, Tint128);
70     X(Tbool,Tuns128, Tuns128,Tuns128, Tuns128);
71 
72     X(Tbool,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
73     X(Tbool,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
74     X(Tbool,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
75     X(Tbool,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
76     X(Tbool,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
77     X(Tbool,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
78     X(Tbool,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
79     X(Tbool,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
80     X(Tbool,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
81 
82     /* ======================= */
83 
84     X(Tint8,Tint8,   Tint32,Tint32,  Tint32);
85     X(Tint8,Tuns8,   Tint32,Tint32,  Tint32);
86     X(Tint8,Tint16,  Tint32,Tint32,  Tint32);
87     X(Tint8,Tuns16,  Tint32,Tint32,  Tint32);
88     X(Tint8,Tint32,  Tint32,Tint32,  Tint32);
89     X(Tint8,Tuns32,  Tuns32,Tuns32,  Tuns32);
90     X(Tint8,Tint64,  Tint64,Tint64,  Tint64);
91     X(Tint8,Tuns64,  Tuns64,Tuns64,  Tuns64);
92     X(Tint8,Tint128, Tint128,Tint128, Tint128);
93     X(Tint8,Tuns128, Tuns128,Tuns128, Tuns128);
94 
95     X(Tint8,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
96     X(Tint8,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
97     X(Tint8,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
98     X(Tint8,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
99     X(Tint8,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
100     X(Tint8,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
101     X(Tint8,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
102     X(Tint8,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
103     X(Tint8,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
104 
105     /* ======================= */
106 
107     X(Tuns8,Tuns8,   Tint32,Tint32,  Tint32);
108     X(Tuns8,Tint16,  Tint32,Tint32,  Tint32);
109     X(Tuns8,Tuns16,  Tint32,Tint32,  Tint32);
110     X(Tuns8,Tint32,  Tint32,Tint32,  Tint32);
111     X(Tuns8,Tuns32,  Tuns32,Tuns32,  Tuns32);
112     X(Tuns8,Tint64,  Tint64,Tint64,  Tint64);
113     X(Tuns8,Tuns64,  Tuns64,Tuns64,  Tuns64);
114     X(Tuns8,Tint128,  Tint128,Tint128,  Tint128);
115     X(Tuns8,Tuns128,  Tuns128,Tuns128,  Tuns128);
116 
117     X(Tuns8,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
118     X(Tuns8,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
119     X(Tuns8,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
120     X(Tuns8,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
121     X(Tuns8,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
122     X(Tuns8,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
123     X(Tuns8,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
124     X(Tuns8,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
125     X(Tuns8,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
126 
127     /* ======================= */
128 
129     X(Tint16,Tint16,  Tint32,Tint32,  Tint32);
130     X(Tint16,Tuns16,  Tint32,Tint32,  Tint32);
131     X(Tint16,Tint32,  Tint32,Tint32,  Tint32);
132     X(Tint16,Tuns32,  Tuns32,Tuns32,  Tuns32);
133     X(Tint16,Tint64,  Tint64,Tint64,  Tint64);
134     X(Tint16,Tuns64,  Tuns64,Tuns64,  Tuns64);
135     X(Tint16,Tint128,  Tint128,Tint128,  Tint128);
136     X(Tint16,Tuns128,  Tuns128,Tuns128,  Tuns128);
137 
138     X(Tint16,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
139     X(Tint16,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
140     X(Tint16,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
141     X(Tint16,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
142     X(Tint16,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
143     X(Tint16,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
144     X(Tint16,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
145     X(Tint16,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
146     X(Tint16,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
147 
148     /* ======================= */
149 
150     X(Tuns16,Tuns16,  Tint32,Tint32,  Tint32);
151     X(Tuns16,Tint32,  Tint32,Tint32,  Tint32);
152     X(Tuns16,Tuns32,  Tuns32,Tuns32,  Tuns32);
153     X(Tuns16,Tint64,  Tint64,Tint64,  Tint64);
154     X(Tuns16,Tuns64,  Tuns64,Tuns64,  Tuns64);
155     X(Tuns16,Tint128, Tint128,Tint128,  Tint128);
156     X(Tuns16,Tuns128, Tuns128,Tuns128,  Tuns128);
157 
158     X(Tuns16,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
159     X(Tuns16,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
160     X(Tuns16,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
161     X(Tuns16,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
162     X(Tuns16,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
163     X(Tuns16,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
164     X(Tuns16,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
165     X(Tuns16,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
166     X(Tuns16,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
167 
168     /* ======================= */
169 
170     X(Tint32,Tint32,  Tint32,Tint32,  Tint32);
171     X(Tint32,Tuns32,  Tuns32,Tuns32,  Tuns32);
172     X(Tint32,Tint64,  Tint64,Tint64,  Tint64);
173     X(Tint32,Tuns64,  Tuns64,Tuns64,  Tuns64);
174     X(Tint32,Tint128, Tint128,Tint128,  Tint128);
175     X(Tint32,Tuns128, Tuns128,Tuns128,  Tuns128);
176 
177     X(Tint32,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
178     X(Tint32,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
179     X(Tint32,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
180     X(Tint32,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
181     X(Tint32,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
182     X(Tint32,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
183     X(Tint32,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
184     X(Tint32,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
185     X(Tint32,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
186 
187     /* ======================= */
188 
189     X(Tuns32,Tuns32,  Tuns32,Tuns32,  Tuns32);
190     X(Tuns32,Tint64,  Tint64,Tint64,  Tint64);
191     X(Tuns32,Tuns64,  Tuns64,Tuns64,  Tuns64);
192     X(Tuns32,Tint128,  Tint128,Tint128,  Tint128);
193     X(Tuns32,Tuns128,  Tuns128,Tuns128,  Tuns128);
194 
195     X(Tuns32,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
196     X(Tuns32,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
197     X(Tuns32,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
198     X(Tuns32,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
199     X(Tuns32,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
200     X(Tuns32,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
201     X(Tuns32,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
202     X(Tuns32,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
203     X(Tuns32,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
204 
205     /* ======================= */
206 
207     X(Tint64,Tint64,  Tint64,Tint64,  Tint64);
208     X(Tint64,Tuns64,  Tuns64,Tuns64,  Tuns64);
209     X(Tint64,Tint128,  Tint128,Tint128,  Tint128);
210     X(Tint64,Tuns128,  Tuns128,Tuns128,  Tuns128);
211 
212     X(Tint64,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
213     X(Tint64,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
214     X(Tint64,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
215     X(Tint64,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
216     X(Tint64,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
217     X(Tint64,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
218     X(Tint64,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
219     X(Tint64,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
220     X(Tint64,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
221 
222     /* ======================= */
223 
224     X(Tuns64,Tuns64,  Tuns64,Tuns64,  Tuns64);
225     X(Tuns64,Tint128,  Tint128,Tint128,  Tint128);
226     X(Tuns64,Tuns128,  Tuns128,Tuns128,  Tuns128);
227 
228     X(Tuns64,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
229     X(Tuns64,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
230     X(Tuns64,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
231     X(Tuns64,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
232     X(Tuns64,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
233     X(Tuns64,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
234     X(Tuns64,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
235     X(Tuns64,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
236     X(Tuns64,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
237 
238     /* ======================= */
239 
240     X(Tint128,Tint128,  Tint128,Tint128,  Tint128);
241     X(Tint128,Tuns128,  Tuns128,Tuns128,  Tuns128);
242 
243     X(Tint128,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
244     X(Tint128,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
245     X(Tint128,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
246     X(Tint128,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
247     X(Tint128,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
248     X(Tint128,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
249     X(Tint128,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
250     X(Tint128,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
251     X(Tint128,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
252 
253     /* ======================= */
254 
255     X(Tuns128,Tuns128,  Tuns128,Tuns128,  Tuns128);
256 
257     X(Tuns128,Tfloat32,     Tfloat32,Tfloat32,     Tfloat32);
258     X(Tuns128,Tfloat64,     Tfloat64,Tfloat64,     Tfloat64);
259     X(Tuns128,Tfloat80,     Tfloat80,Tfloat80,     Tfloat80);
260     X(Tuns128,Timaginary32, Tfloat32,Timaginary32, Tfloat32);
261     X(Tuns128,Timaginary64, Tfloat64,Timaginary64, Tfloat64);
262     X(Tuns128,Timaginary80, Tfloat80,Timaginary80, Tfloat80);
263     X(Tuns128,Tcomplex32,   Tfloat32,Tcomplex32,   Tcomplex32);
264     X(Tuns128,Tcomplex64,   Tfloat64,Tcomplex64,   Tcomplex64);
265     X(Tuns128,Tcomplex80,   Tfloat80,Tcomplex80,   Tcomplex80);
266 
267     /* ======================= */
268 
269     X(Tfloat32,Tfloat32,  Tfloat32,Tfloat32, Tfloat32);
270     X(Tfloat32,Tfloat64,  Tfloat64,Tfloat64, Tfloat64);
271     X(Tfloat32,Tfloat80,  Tfloat80,Tfloat80, Tfloat80);
272 
273     X(Tfloat32,Timaginary32,  Tfloat32,Timaginary32, Tfloat32);
274     X(Tfloat32,Timaginary64,  Tfloat64,Timaginary64, Tfloat64);
275     X(Tfloat32,Timaginary80,  Tfloat80,Timaginary80, Tfloat80);
276 
277     X(Tfloat32,Tcomplex32,  Tfloat32,Tcomplex32, Tcomplex32);
278     X(Tfloat32,Tcomplex64,  Tfloat64,Tcomplex64, Tcomplex64);
279     X(Tfloat32,Tcomplex80,  Tfloat80,Tcomplex80, Tcomplex80);
280 
281     /* ======================= */
282 
283     X(Tfloat64,Tfloat64,  Tfloat64,Tfloat64, Tfloat64);
284     X(Tfloat64,Tfloat80,  Tfloat80,Tfloat80, Tfloat80);
285 
286     X(Tfloat64,Timaginary32,  Tfloat64,Timaginary64, Tfloat64);
287     X(Tfloat64,Timaginary64,  Tfloat64,Timaginary64, Tfloat64);
288     X(Tfloat64,Timaginary80,  Tfloat80,Timaginary80, Tfloat80);
289 
290     X(Tfloat64,Tcomplex32,  Tfloat64,Tcomplex64, Tcomplex64);
291     X(Tfloat64,Tcomplex64,  Tfloat64,Tcomplex64, Tcomplex64);
292     X(Tfloat64,Tcomplex80,  Tfloat80,Tcomplex80, Tcomplex80);
293 
294     /* ======================= */
295 
296     X(Tfloat80,Tfloat80,  Tfloat80,Tfloat80, Tfloat80);
297 
298     X(Tfloat80,Timaginary32,  Tfloat80,Timaginary80, Tfloat80);
299     X(Tfloat80,Timaginary64,  Tfloat80,Timaginary80, Tfloat80);
300     X(Tfloat80,Timaginary80,  Tfloat80,Timaginary80, Tfloat80);
301 
302     X(Tfloat80,Tcomplex32,  Tfloat80,Tcomplex80, Tcomplex80);
303     X(Tfloat80,Tcomplex64,  Tfloat80,Tcomplex80, Tcomplex80);
304     X(Tfloat80,Tcomplex80,  Tfloat80,Tcomplex80, Tcomplex80);
305 
306     /* ======================= */
307 
308     X(Timaginary32,Timaginary32,  Timaginary32,Timaginary32, Timaginary32);
309     X(Timaginary32,Timaginary64,  Timaginary64,Timaginary64, Timaginary64);
310     X(Timaginary32,Timaginary80,  Timaginary80,Timaginary80, Timaginary80);
311 
312     X(Timaginary32,Tcomplex32,  Timaginary32,Tcomplex32, Tcomplex32);
313     X(Timaginary32,Tcomplex64,  Timaginary64,Tcomplex64, Tcomplex64);
314     X(Timaginary32,Tcomplex80,  Timaginary80,Tcomplex80, Tcomplex80);
315 
316     /* ======================= */
317 
318     X(Timaginary64,Timaginary64,  Timaginary64,Timaginary64, Timaginary64);
319     X(Timaginary64,Timaginary80,  Timaginary80,Timaginary80, Timaginary80);
320 
321     X(Timaginary64,Tcomplex32,  Timaginary64,Tcomplex64, Tcomplex64);
322     X(Timaginary64,Tcomplex64,  Timaginary64,Tcomplex64, Tcomplex64);
323     X(Timaginary64,Tcomplex80,  Timaginary80,Tcomplex80, Tcomplex80);
324 
325     /* ======================= */
326 
327     X(Timaginary80,Timaginary80,  Timaginary80,Timaginary80, Timaginary80);
328 
329     X(Timaginary80,Tcomplex32,  Timaginary80,Tcomplex80, Tcomplex80);
330     X(Timaginary80,Tcomplex64,  Timaginary80,Tcomplex80, Tcomplex80);
331     X(Timaginary80,Tcomplex80,  Timaginary80,Tcomplex80, Tcomplex80);
332 
333     /* ======================= */
334 
335     X(Tcomplex32,Tcomplex32,  Tcomplex32,Tcomplex32, Tcomplex32);
336     X(Tcomplex32,Tcomplex64,  Tcomplex64,Tcomplex64, Tcomplex64);
337     X(Tcomplex32,Tcomplex80,  Tcomplex80,Tcomplex80, Tcomplex80);
338 
339     /* ======================= */
340 
341     X(Tcomplex64,Tcomplex64,  Tcomplex64,Tcomplex64, Tcomplex64);
342     X(Tcomplex64,Tcomplex80,  Tcomplex80,Tcomplex80, Tcomplex80);
343 
344     /* ======================= */
345 
346     X(Tcomplex80,Tcomplex80,  Tcomplex80,Tcomplex80, Tcomplex80);
347 
348     foreach (i; 0 .. cast(size_t)TMAX)
349     {
350         foreach (j; 0 .. cast(size_t)TMAX)
351         {
352             if (impCnvTab.impcnvResultTab[i][j] == Terror)
353             {
354                 impCnvTab.impcnvResultTab[i][j] = impCnvTab.impcnvResultTab[j][i];
355                 impCnvTab.impcnvType1Tab[i][j] = impCnvTab.impcnvType2Tab[j][i];
356                 impCnvTab.impcnvType2Tab[i][j] = impCnvTab.impcnvType1Tab[j][i];
357             }
358         }
359     }
360 
361     return impCnvTab;
362 }