Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – tendra.SVN – Blame – /branches/tendra5-amd64/src/producers/common/construct/inttype.c – Rev 6

Subversion Repositories tendra.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
6 7u83 2
 * Copyright (c) 2002-2006 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997, 1998
6 7u83 33
 
2 7u83 34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
6 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
6 7u83 45
 
2 7u83 46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
6 7u83 49
 
2 7u83 50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
6 7u83 53
 
2 7u83 54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
#include "config.h"
62
#include <limits.h>
63
#include "c_types.h"
64
#include "exp_ops.h"
65
#include "etype_ops.h"
66
#include "ftype_ops.h"
67
#include "id_ops.h"
68
#include "itype_ops.h"
69
#include "tok_ops.h"
70
#include "type_ops.h"
71
#include "error.h"
72
#include "catalog.h"
73
#include "basetype.h"
74
#include "constant.h"
75
#include "convert.h"
76
#include "chktype.h"
77
#include "dump.h"
78
#include "exception.h"
79
#include "hash.h"
80
#include "interface.h"
81
#include "inttype.h"
82
#include "mangle.h"
83
#include "print.h"
84
#include "syntax.h"
85
#include "tok.h"
86
#include "tokdef.h"
87
#include "token.h"
88
 
89
 
90
/*
91
    FUNDAMENTAL TYPE INFORMATION
92
 
93
    These values give the fundamental information about the built-in
94
    types, the sizes of the various types, whether char is signed, and
95
    whether the signed ranges are full (for example [-128,127]) or
96
    symmetric (for example [-127,127]).
97
*/
98
 
6 7u83 99
BASE_INFO basetype_info[ORDER_ntype] = {
100
	{ btype_sint, 16, UINT_MAX, btype_signed, 1, NULL_type },
101
	{ btype_char, 8, UINT_MAX, btype_none, 1, NULL_type },
102
	{ btype_schar, 8, UINT_MAX, btype_signed, 1, NULL_type },
103
	{ btype_uchar, 8, UINT_MAX, btype_unsigned, 1, NULL_type },
104
	{ btype_sshort, 16, UINT_MAX, btype_signed, 1, NULL_type },
105
	{ btype_ushort, 16, UINT_MAX, btype_unsigned, 1, NULL_type },
106
	{ btype_sint, 16, UINT_MAX, btype_signed, 1, NULL_type },
107
	{ btype_uint, 16, UINT_MAX, btype_unsigned, 1, NULL_type },
108
	{ btype_slong, 32, UINT_MAX, btype_signed, 1, NULL_type },
109
	{ btype_ulong, 32, UINT_MAX, btype_unsigned, 1, NULL_type },
110
	{ btype_sllong, 32, UINT_MAX, btype_signed, 0, NULL_type },
111
	{ btype_ullong, 32, UINT_MAX, btype_unsigned, 0, NULL_type },
112
	{ btype_float, 0, 0, btype_none, 1, NULL_type },
113
	{ btype_double, 0, 0, btype_none, 1, NULL_type },
114
	{ btype_ldouble, 0, 0, btype_none, 1, NULL_type },
115
	{ btype_void, 0, 0, btype_none, 1, NULL_type },
116
	{ btype_bottom, 0, 0, btype_none, 1, NULL_type },
117
	{ btype_bool, 1, 1, btype_unsigned, LANGUAGE_CPP, NULL_type },
118
	{ btype_ptrdiff_t, 16, UINT_MAX, btype_signed, 0, NULL_type },
119
	{ btype_size_t, 16, UINT_MAX, btype_unsigned, 0, NULL_type },
120
	{ btype_wchar_t, 8, UINT_MAX, btype_none, LANGUAGE_CPP, NULL_type },
121
	{ btype_ellipsis, 0, UINT_MAX, btype_none, 0, NULL_type }
122
};
2 7u83 123
 
124
 
125
/*
126
    STANDARD LISTS OF TYPES
127
 
128
    These variables give various standard lists of types.
129
*/
130
 
6 7u83 131
LIST(TYPE) all_int_types = NULL_list(TYPE);
132
LIST(TYPE) all_prom_types = NULL_list(TYPE);
133
LIST(TYPE) all_llong_types = NULL_list(TYPE);
2 7u83 134
 
135
 
136
/*
137
    TABLE OF BASIC TYPE CONVERSIONS
138
 
139
    This table gives the severity levels for conversions between the
140
    various built-in types (the source types are listed along the right
141
    hand side, and the destination types along the top).  The first row
142
    and column are just copies of the second.  The values are as follows
143
    (from safest to most unsafe):
144
 
145
 
146
	1 = almost certainly safe			SAFE
147
	2 = safe on real machines			SAFE
148
	3 = safe on 32-bit machines			DEPENDS
149
	4 = safe on 64-bit machines			DEPENDS
150
	5 = same size, possibly different sign		UNSAFE
151
	6 = same size, different sign conversion	UNSAFE
152
	7 = signed to unsigned conversion		UNSAFE
153
	8 = always unsafe				UNSAFE
154
	9 = illegal type in conversion			UNSAFE
155
 
156
    min_builtin_cast determines the maximum value which is considered
157
    probably safe by the program.  safe_builtin_cast gives the maximum
158
    value which is considered possibly safe.  max_builtin_cast gives the
159
    minimum threshold value for error reporting.
160
*/
161
 
6 7u83 162
unsigned char builtin_casts[ORDER_ntype][ORDER_ntype] = {
2 7u83 163
	 /* CH CH SC UC SS US SI UI SL UL SX UX FL DB LD VD BT BL PD SZ WC EL */
164
 /* CH */ { 0, 0, 5, 5, 1, 7, 1, 7, 1, 7, 1, 7, 8, 8, 8, 9, 9, 8, 1, 7, 0, 0 },
165
 /* CH */ { 0, 0, 5, 5, 1, 7, 1, 7, 1, 7, 1, 7, 8, 8, 8, 9, 9, 8, 1, 7, 0, 0 },
166
 /* SC */ { 5, 5, 0, 6, 0, 7, 0, 7, 0, 7, 0, 7, 8, 8, 8, 9, 9, 8, 1, 7, 8, 0 },
167
 /* UC */ { 5, 5, 6, 0, 2, 0, 2, 0, 2, 0, 2, 0, 8, 8, 8, 9, 9, 8, 2, 1, 8, 0 },
168
 /* SS */ { 8, 8, 8, 8, 0, 6, 0, 7, 0, 7, 0, 7, 8, 8, 8, 9, 9, 8, 2, 7, 8, 0 },
169
 /* US */ { 8, 8, 8, 8, 6, 0, 3, 0, 2, 0, 2, 0, 8, 8, 8, 9, 9, 8, 3, 2, 8, 0 },
170
 /* SI */ { 8, 8, 8, 8, 8, 8, 0, 6, 0, 7, 0, 7, 8, 8, 8, 9, 9, 8, 2, 7, 8, 0 },
171
 /* UI */ { 8, 8, 8, 8, 8, 8, 6, 0, 4, 0, 4, 0, 8, 8, 8, 9, 9, 8, 7, 3, 8, 0 },
172
 /* SL */ { 8, 8, 8, 8, 8, 8, 8, 8, 0, 6, 0, 7, 8, 8, 8, 9, 9, 8, 4, 7, 8, 0 },
173
 /* UL */ { 8, 8, 8, 8, 8, 8, 8, 8, 6, 0, 4, 0, 8, 8, 8, 9, 9, 8, 8, 4, 8, 0 },
174
 /* SX */ { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 0, 6, 8, 8, 8, 9, 9, 8, 4, 7, 8, 0 },
175
 /* UX */ { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 6, 0, 8, 8, 8, 9, 9, 8, 8, 4, 8, 0 },
176
 /* FL */ { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 0, 0, 0, 9, 9, 8, 8, 8, 8, 0 },
177
 /* DB */ { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 0, 0, 9, 9, 8, 8, 8, 8, 0 },
178
 /* LD */ { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 0, 9, 9, 8, 8, 8, 8, 0 },
179
 /* VD */ { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 0, 0, 9, 9, 9, 9, 0 },
180
 /* BT */ { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 0, 0, 9, 9, 9, 9, 0 },
181
 /* BL */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 9, 0, 0, 0, 0, 0 },
182
 /* PD */ { 8, 8, 8, 8, 8, 8, 3, 7, 0, 7, 0, 7, 8, 8, 8, 9, 9, 8, 0, 8, 8, 0 },
183
 /* SZ */ { 8, 8, 8, 8, 8, 8, 8, 3, 7, 0, 7, 0, 8, 8, 8, 9, 9, 8, 8, 0, 8, 0 },
184
 /* WC */ { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 8, 8, 8, 0, 0 },
185
 /* EL */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
6 7u83 186
};
2 7u83 187
 
6 7u83 188
int min_builtin_cast = 2;
189
int safe_builtin_cast = 4;
190
int max_builtin_cast = 3;
2 7u83 191
 
192
 
193
/*
194
    COPY BUILT-IN CASTS
195
 
196
    This routine copies the built-in cast values for type m into type n.
197
*/
198
 
6 7u83 199
static void
200
copy_builtin_cast(BUILTIN_TYPE n, BUILTIN_TYPE m)
2 7u83 201
{
6 7u83 202
	unsigned long i;
203
	for (i = 1; i < ORDER_ntype; i++) {
204
		builtin_casts[n][i] = builtin_casts[m][i];
205
		builtin_casts[i][n] = builtin_casts[i][m];
206
	}
207
	builtin_casts[n][n] = 0;
208
	builtin_casts[n][m] = 0;
209
	builtin_casts[m][n] = 0;
210
	builtin_casts[m][m] = 0;
211
	return;
2 7u83 212
}
213
 
214
 
215
/*
216
    SET EXACT TYPE RANGES
217
 
218
    This routine recalculates the table of built-in casts on the assumption
219
    that the type sizes given in basetype_info are exact.  The definition
220
    of what constitutes a safe conversion is adjusted accordingly.  Note
221
    that the conversions between char and signed/unsigned char are handled
222
    separately by set_char_type.
223
*/
224
 
6 7u83 225
void
226
set_exact_types(void)
2 7u83 227
{
6 7u83 228
	BUILTIN_TYPE n, m;
229
	BASE_INFO *p = basetype_info;
230
	for (n = ntype_char; n < ntype_ellipsis; n++) {
231
		for (m = ntype_char; m < ntype_ellipsis; m++) {
232
			unsigned char c = builtin_casts[n][m];
233
			if (c >= 1 && c <= 4) {
234
				unsigned bn = p[n].min_bits;
235
				unsigned bm = p[m].min_bits;
236
				BASE_TYPE sn = p[n].sign;
237
				BASE_TYPE sm = p[m].sign;
238
				if (sn == sm) {
239
					/* Same sign */
240
					if (bn <= bm) {
241
						c = 1;
242
					}
243
				} else if (sn & btype_unsigned) {
244
					/* n is unsigned */
245
					if (bn < bm) {
246
						c = 1;
247
					}
248
				} else if (sm & btype_signed) {
249
					/* m is signed */
250
					if (bn < bm) {
251
						c = 1;
252
					}
253
				}
254
				builtin_casts[n][m] = c;
255
			}
2 7u83 256
		}
257
	}
6 7u83 258
	min_builtin_cast = 1;
259
	safe_builtin_cast = 1;
260
	max_builtin_cast = 2;
261
	return;
2 7u83 262
}
263
 
264
 
265
/*
266
    SET THE SIGN OF CHAR
267
 
268
    This routine sets the type of char to be bt, which can be btype_signed,
269
    btype_unsigned, or btype_none.
270
*/
271
 
6 7u83 272
void
273
set_char_sign(BASE_TYPE bt)
2 7u83 274
{
6 7u83 275
	BASE_INFO *p = basetype_info;
276
	BUILTIN_TYPE nt = ntype_none;
277
	if (bt & btype_signed) {
278
		nt = ntype_schar;
279
		bt = p[nt].sign;
280
	} else if (bt & btype_unsigned) {
281
		nt = ntype_uchar;
282
		bt = p[nt].sign;
283
	}
284
	if (p[ntype_char].sign != bt) {
285
		p[ntype_char].sign = bt;
286
		builtin_casts[ntype_char][ntype_schar] = 5;
287
		builtin_casts[ntype_char][ntype_uchar] = 5;
288
		builtin_casts[ntype_schar][ntype_char] = 5;
289
		builtin_casts[ntype_uchar][ntype_char] = 5;
290
		builtin_casts[ntype_schar][ntype_wchar_t] = 8;
291
		builtin_casts[ntype_uchar][ntype_wchar_t] = 8;
292
		copy_builtin_cast(ntype_char, nt);
293
		builtin_casts[ntype_char][ntype_wchar_t] = 0;
294
		builtin_casts[nt][ntype_wchar_t] = 0;
295
	}
296
	return;
2 7u83 297
}
298
 
299
 
300
/*
301
    DEFINE A BUILT-IN TYPE
302
 
303
    This routine defines the built-in type indicated by bt to be t.
304
*/
305
 
6 7u83 306
void
307
set_builtin_type(BASE_TYPE bt, TYPE t)
2 7u83 308
{
6 7u83 309
	/* Built-in integral types */
310
	INT_TYPE is, it;
311
	TYPE s = make_base_type(bt);
312
	if (!IS_type_integer(t)) {
313
		report(crt_loc, ERR_pragma_builtin_type(t));
314
		return;
2 7u83 315
	}
6 7u83 316
	is = DEREF_itype(type_integer_rep(s));
317
	it = DEREF_itype(type_integer_rep(t));
318
	if (!EQ_itype(is, it)) {
319
		TYPE r;
320
		int key;
321
		BUILTIN_TYPE ns = ntype_none;
322
		if (bt == btype_ptrdiff_t) {
323
			ns = ntype_ptrdiff_t;
324
		} else if (bt == btype_size_t) {
325
			ns = ntype_size_t;
326
		} else if (bt == btype_wchar_t) {
327
			ns = ntype_wchar_t;
328
		}
329
		key = basetype_info[ns].key;
330
		r = basetype_info[ns].set;
331
		basetype_info[ns].set = t;
332
		if (!IS_NULL_type(r)) {
333
			/* Check compatibility */
334
			ERROR err = NULL_err;
335
			IGNORE check_compatible(r, t, 0, &err, 0);
336
			if (!IS_NULL_err(err)) {
337
				report(crt_loc, err);
338
			}
339
		}
340
		if (!key) {
341
			/* Set integral type */
342
			TYPE p = DEREF_type(itype_prom(it));
343
			BUILTIN_TYPE nu = DEREF_ntype(itype_unprom(it));
344
			LIST(TYPE)cases = DEREF_list(itype_cases(it));
345
			COPY_type(itype_prom(is), p);
346
			COPY_ntype(itype_unprom(is), nu);
347
			COPY_list(itype_cases(is), cases);
348
			if (IS_itype_basic(it)) {
349
				BUILTIN_TYPE nt =
350
				    DEREF_ntype(itype_basic_no(it));
351
				copy_builtin_cast(ns, nt);
352
			}
353
		}
2 7u83 354
	}
6 7u83 355
	return;
2 7u83 356
}
357
 
358
 
359
/*
360
    SET THE IMPLEMENTATION OF LONG LONG
361
 
362
    This routine sets the implementation of 'long long' to be the
363
    real 'long long' type if big is true and 'long' otherwise.
364
*/
365
 
6 7u83 366
void
367
set_long_long_type(int big)
2 7u83 368
{
6 7u83 369
	if (big) {
370
		base_token[ntype_sllong].tok = TOK_signed_llong;;
371
		base_token[ntype_sllong].no = ARITH_sllong;;
372
		base_token[ntype_ullong].tok = TOK_unsigned_llong;;
373
		base_token[ntype_ullong].no = ARITH_ullong;;
374
	} else {
375
		base_token[ntype_sllong].tok = TOK_signed_long;;
376
		base_token[ntype_sllong].no = ARITH_slong;;
377
		base_token[ntype_ullong].tok = TOK_unsigned_long;;
378
		base_token[ntype_ullong].no = ARITH_ulong;;
379
	}
380
	return;
2 7u83 381
}
382
 
383
 
384
/*
385
    IS A TYPE A BUILT-IN TYPE?
386
 
387
    This routine checks whether the type t is a qualified version of a
388
    built-in type (including the semantic type if sem is true).  If so
389
    it returns the corresponding built-in type number.
390
*/
391
 
6 7u83 392
BUILTIN_TYPE
393
is_builtin_type(TYPE t, int sem)
2 7u83 394
{
6 7u83 395
	BUILTIN_TYPE nt = ntype_none;
396
	if (!IS_NULL_type(t)) {
397
		switch (TAG_type(t)) {
398
		case type_integer_tag: {
399
			INT_TYPE it = DEREF_itype(type_integer_rep(t));
400
			if (IS_itype_basic(it)) {
401
				nt = DEREF_ntype(itype_basic_no(it));
402
				if (sem) {
403
					/* Check semantic type */
404
					INT_TYPE is;
405
					is = DEREF_itype(type_integer_sem(t));
406
					if (IS_itype_basic(is)) {
407
						BUILTIN_TYPE ns;
408
						ns = DEREF_ntype(itype_basic_no(is));
409
						if (ns != nt)nt = ntype_none;
410
					} else {
411
						nt = ntype_none;
412
					}
413
				}
2 7u83 414
			}
6 7u83 415
			break;
2 7u83 416
		}
6 7u83 417
		case type_floating_tag: {
418
			FLOAT_TYPE ft = DEREF_ftype(type_floating_rep(t));
419
			if (IS_ftype_basic(ft)) {
420
				nt = DEREF_ntype(ftype_basic_no(ft));
421
			}
422
			break;
2 7u83 423
		}
6 7u83 424
		case type_top_tag:
425
			nt = ntype_void;
426
			break;
427
		case type_bottom_tag:
428
			nt = ntype_bottom;
429
			break;
430
		case type_pre_tag: {
431
			BASE_TYPE bt = DEREF_btype(type_pre_rep(t));
432
			if (bt == btype_ellipsis) {
433
				nt = ntype_ellipsis;
434
			}
435
			break;
436
		}
437
		}
2 7u83 438
	}
6 7u83 439
	return (nt);
2 7u83 440
}
441
 
442
 
443
/*
444
    EXPAND A BUILT-IN INTEGRAL TYPE
445
 
446
    This routine expands the integral type it by replacing built-in types
447
    such as size_t by their definition.
448
*/
449
 
6 7u83 450
INT_TYPE
451
expand_itype(INT_TYPE it)
2 7u83 452
{
6 7u83 453
	if (!IS_NULL_itype(it) && IS_itype_basic(it)) {
454
		BUILTIN_TYPE nt = DEREF_ntype(itype_basic_no(it));
455
		TYPE t = basetype_info[nt].set;
456
		if (!IS_NULL_type(t) && !basetype_info[nt].key) {
457
			it = DEREF_itype(type_integer_rep(t));
458
		}
2 7u83 459
	}
6 7u83 460
	return (it);
2 7u83 461
}
462
 
463
 
464
/*
465
    CHECK BUILT-IN TYPE DEFINITIONS
466
 
467
    This routine defines any tokens for the built-in types from their
468
    set values.
469
*/
470
 
6 7u83 471
void
472
term_itypes(void)
2 7u83 473
{
6 7u83 474
	IDENTIFIER id = get_special(TOK_ptrdiff_t, 1);
475
	if (!IS_NULL_id(id)) {
476
		TYPE t = basetype_info[ntype_ptrdiff_t].set;
477
		if (!IS_NULL_type(t)) {
478
			IGNORE define_type_token(id, t, 0);
479
		}
2 7u83 480
	}
6 7u83 481
	id = get_special(TOK_size_t, 1);
482
	if (!IS_NULL_id(id)) {
483
		TYPE t = basetype_info[ntype_size_t].set;
484
		if (!IS_NULL_type(t)) {
485
			IGNORE define_type_token(id, t, 0);
486
		} else {
487
			TOKEN tok = DEREF_tok(id_token_sort(id));
488
			if (IS_tok_type(tok)) {
489
				/* Allow deduction of __size_t from size_t */
490
				t = DEREF_type(tok_type_value(tok));
491
				basetype_info[ntype_size_t].set = t;
492
			}
493
		}
2 7u83 494
	}
6 7u83 495
	id = get_special(TOK_size_t_2, 1);
496
	if (!IS_NULL_id(id)) {
497
		TYPE t = basetype_info[ntype_size_t].set;
498
		if (!IS_NULL_type(t)) {
499
			t = promote_type(t);
500
			IGNORE define_type_token(id, t, 0);
501
		}
2 7u83 502
	}
6 7u83 503
	id = get_special(TOK_wchar_t, 1);
504
	if (!IS_NULL_id(id)) {
505
		TYPE t = basetype_info[ntype_wchar_t].set;
506
		if (!IS_NULL_type(t)) {
507
			IGNORE define_type_token(id, t, 0);
508
		}
2 7u83 509
	}
6 7u83 510
	return;
2 7u83 511
}
512
 
513
 
514
/*
515
    SET A PROMOTED TYPE
516
 
517
    This routine sets the promotion of type t to be s.  Note that this
518
    implies that s is its own promotion and that the conversion from t to
519
    s is deemed to be safe.
520
*/
521
 
6 7u83 522
void
523
set_promote_type(TYPE t, TYPE s, BUILTIN_TYPE ns)
2 7u83 524
{
6 7u83 525
	INT_TYPE it, is;
526
	BUILTIN_TYPE nt;
527
	if (!IS_type_integer(t)) {
528
		ENUM_TYPE et;
529
		if (!IS_type_enumerate(t)) {
530
			report(crt_loc, ERR_pragma_promote_type(t));
531
			return;
532
		}
533
		et = DEREF_etype(type_enumerate_defn(t));
534
		t = DEREF_type(etype_rep(et));
2 7u83 535
	}
6 7u83 536
	if (!IS_type_integer(s)) {
537
		ENUM_TYPE es;
538
		if (!IS_type_enumerate(s)) {
539
			report(crt_loc, ERR_pragma_promote_type(s));
540
			return;
541
		}
542
		es = DEREF_etype(type_enumerate_defn(s));
543
		s = DEREF_type(etype_rep(es));
2 7u83 544
	}
545
 
6 7u83 546
	/* Check previous definition */
547
	is = DEREF_itype(type_integer_rep(s));
548
	it = DEREF_itype(type_integer_rep(t));
549
	nt = DEREF_ntype(itype_unprom(it));
550
	if (nt != ntype_none) {
551
		ERROR err = NULL_err;
552
		TYPE p = DEREF_type(itype_prom(it));
553
		IGNORE check_compatible(p, s, 0, &err, 0);
554
		if (!IS_NULL_err(err)) {
555
			ERROR err2 = ERR_pragma_promote_compat(t);
556
			err = concat_error(err, err2);
557
			report(crt_loc, err);
558
		}
559
		if (nt != ntype_ellipsis) {
560
			ns = ntype_none;
561
		}
2 7u83 562
	}
563
 
6 7u83 564
	/* Set promoted type */
565
	if (ns != ntype_none) {
566
		LIST(TYPE) ps;
567
		TYPE p = make_itype(is, it);
568
		COPY_type(itype_prom(it), p);
569
		if (nt == ntype_none) {
570
			COPY_ntype(itype_unprom(it), ns);
571
		}
572
		ps = DEREF_list(itype_cases(is));
573
		if (EQ_list(ps, all_int_types)) {
574
			/* Restrict cases for is */
575
			COPY_list(itype_cases(is), all_prom_types);
576
		}
577
		if (IS_itype_basic(it)) {
578
			BUILTIN_TYPE n = DEREF_ntype(itype_basic_no(it));
579
			if (IS_itype_basic(is)) {
580
				/* Set conversion rank */
581
				BUILTIN_TYPE m =
582
				    DEREF_ntype(itype_basic_no(is));
583
				builtin_casts[n][m] = 0;
584
			}
585
			ns = n;
586
		}
587
		if (do_dump) {
588
			dump_promote(it, is);
589
		}
2 7u83 590
	}
6 7u83 591
 
592
	/* Set s to be its own promotion */
593
	if (!eq_itype(it, is)) {
594
		set_promote_type(s, s, ns);
2 7u83 595
	}
6 7u83 596
	return;
2 7u83 597
}
598
 
599
 
600
/*
601
    SET THE PROMOTION COMPUTATION TOKEN
602
 
603
    This routine sets the token used to calculate promoted types to be id.
604
*/
605
 
6 7u83 606
void
607
compute_promote_type(IDENTIFIER id)
2 7u83 608
{
6 7u83 609
	IDENTIFIER tid = resolve_token(id, "ZZ", 0);
610
	if (!IS_NULL_id(tid)) {
611
		set_special(TOK_promote, tid);
612
	}
613
	return;
2 7u83 614
}
615
 
616
 
617
/*
618
    ARRAY OF ALL INTEGRAL AND FLOATING POINT TYPES
619
 
620
    This array is used to hold all the integral and floating point types,
621
    with all the cases of representation and semantics.
622
*/
623
 
6 7u83 624
static TYPE all_itypes[ORDER_ntype][ORDER_ntype];
2 7u83 625
 
626
 
627
/*
628
    CONSTRUCT AN INTEGRAL TYPE
629
 
630
    This routine constructs an integral type with representation it and
631
    semantics is.  If the semantic type is the null type then the semantics
632
    are the same as the representation.
633
*/
634
 
6 7u83 635
TYPE
636
make_itype(INT_TYPE it, INT_TYPE is)
2 7u83 637
{
6 7u83 638
	TYPE r;
639
	if (IS_NULL_itype(is)) {
640
		is = it;
2 7u83 641
	}
6 7u83 642
	if (IS_itype_basic(it) && IS_itype_basic(is)) {
643
		BUILTIN_TYPE n = DEREF_ntype(itype_basic_no(it));
644
		BUILTIN_TYPE m = DEREF_ntype(itype_basic_no(is));
645
		r = all_itypes[n][m];
646
		if (IS_NULL_type(r)) {
647
			MAKE_type_integer(cv_none, it, is, r);
648
			all_itypes[n][m] = r;
649
		}
650
	} else {
651
		MAKE_type_integer(cv_none, it, is, r);
652
	}
653
	return (r);
2 7u83 654
}
655
 
656
 
657
/*
658
    CONSTRUCT A FLOATING POINT TYPE
659
 
660
    This routine constructs a floating point type with representation ft
661
    and semantics fs.
662
*/
663
 
6 7u83 664
TYPE
665
make_ftype(FLOAT_TYPE ft, FLOAT_TYPE fs)
2 7u83 666
{
6 7u83 667
	TYPE r;
668
	if (IS_NULL_ftype(fs)) {
669
		fs = ft;
2 7u83 670
	}
6 7u83 671
	if (IS_ftype_basic(ft)) {
672
		BUILTIN_TYPE n = DEREF_ntype(ftype_basic_no(ft));
673
		BUILTIN_TYPE m = DEREF_ntype(ftype_basic_no(fs));
674
		r = all_itypes[n][m];
675
		if (IS_NULL_type(r)) {
676
			MAKE_type_floating(cv_none, ft, fs, r);
677
			all_itypes[n][m] = r;
678
		}
679
	} else {
680
		MAKE_type_floating(cv_none, ft, fs, r);
681
	}
682
	return (r);
2 7u83 683
}
684
 
685
 
686
/*
687
    CREATE AN INTEGRAL PROMOTION TYPE
688
 
689
    This routine sets the promotion type of the integral type it to be
690
    ip.  If ip is the null type then a promotion type is created.  An
691
    integral type corresponding to it is returned.
692
*/
693
 
6 7u83 694
TYPE
695
promote_itype(INT_TYPE it, INT_TYPE ip)
2 7u83 696
{
6 7u83 697
	TYPE p;
698
	TYPE t = make_itype(it, it);
699
	if (IS_NULL_itype(ip)) {
700
		MAKE_itype_promote(NULL_type, all_prom_types, it, ip);
701
	} else {
702
		COPY_ntype(itype_unprom(ip), ntype_ellipsis);
703
		COPY_ntype(itype_unprom(it), ntype_ellipsis);
704
	}
705
	if (EQ_itype(it, ip)) {
706
		p = t;
707
	} else {
708
		p = make_itype(ip, it);
709
	}
710
	COPY_type(itype_prom(ip), p);
711
	COPY_type(itype_prom(it), p);
712
	return (t);
2 7u83 713
}
714
 
715
 
716
/*
717
    ARITHMETIC TYPES
718
 
719
    The only difficult case in the basic arithmetic types is the combination
720
    of 'signed long' and 'unsigned int'.  The variable arith_slong_uint is
721
    used to hold this value.  If 'long long' is allowed then combining
722
    'signed long long' with 'unsigned long' or 'unsigned int' is also
723
    target dependent.
724
*/
725
 
6 7u83 726
static INT_TYPE arith_slong_uint;
727
static INT_TYPE arith_sllong_uint;
728
static INT_TYPE arith_sllong_ulong;
2 7u83 729
 
730
 
731
/*
732
    FIND AN ARITHMETIC INTEGRAL TYPE
733
 
734
    This routine finds the type to be used for arithmetic involving operands
735
    of promoted integral types t and s.  The operands a and b are passed
736
    in to enable the semantic type to be determined.  Note that for base
737
    integral types, because:
738
 
739
		ntype_sint < ntype_uint < ... < ntype_ullong
740
 
741
    the arithmetic type is, except in the cases listed above, the maximum
742
    of the two base type values.
743
 
744
		       | SI  UI  SL  UL  SX  UX
745
		    ---+-----------------------
746
		    SI | SI  UI  SL  UL  SX  UX
747
		    UI | UI  UI  ??  UL  ??  UX
748
		    SL | SL  ??  SL  UL  SX  UX
749
		    UL | UL  UL  UL  UL  ??  UX
750
		    SX | SX  ??  SX  ??  SX  UX
751
		    UX | UX  UX  UX  UX  UX  UX
752
*/
753
 
6 7u83 754
TYPE
755
arith_itype(TYPE t, TYPE s, EXP a, EXP b)
2 7u83 756
{
6 7u83 757
	TYPE r;
758
	if (IS_type_integer(t) && IS_type_integer(s)) {
759
		INT_TYPE ir;
760
		INT_TYPE it = DEREF_itype(type_integer_rep(t));
761
		INT_TYPE is = DEREF_itype(type_integer_rep(s));
2 7u83 762
 
6 7u83 763
		/* Find semantic type of result */
764
		INT_TYPE mr = NULL_itype;
765
		INT_TYPE mt = DEREF_itype(type_integer_sem(t));
766
		INT_TYPE ms = DEREF_itype(type_integer_sem(s));
767
		if (EQ_itype(mt, ms)) {
768
			/* Same semantic types */
769
			mr = mt;
2 7u83 770
		} else {
6 7u83 771
			/* Allow for variable semantics of constants */
772
			if (!IS_NULL_exp(a) && IS_exp_int_lit(a)) {
773
				if (!IS_NULL_exp(b) && IS_exp_int_lit(b)) {
774
					/* Leave to constant evaluation
775
					 * routines */
776
					/* EMPTY */
777
				} else {
778
					NAT n = DEREF_nat(exp_int_lit_nat(a));
779
					COPY_itype(type_integer_rep(s), ms);
780
					if (check_nat_range(s, n) == 0) {
781
						mr = ms;
782
					}
783
					COPY_itype(type_integer_rep(s), is);
784
				}
785
			} else if (!IS_NULL_exp(b) && IS_exp_int_lit(b)) {
786
				NAT n = DEREF_nat(exp_int_lit_nat(b));
787
				COPY_itype(type_integer_rep(t), mt);
788
				if (check_nat_range(t, n) == 0) {
789
					mr = mt;
790
				}
791
				COPY_itype(type_integer_rep(t), it);
792
			}
2 7u83 793
		}
794
 
6 7u83 795
		/* Find representation type of result */
796
		if (EQ_itype(it, is)) {
797
			r = make_itype(it, mr);
798
			return (r);
2 7u83 799
		}
6 7u83 800
		if (IS_itype_basic(it)) {
801
			BUILTIN_TYPE nt = DEREF_ntype(itype_basic_no(it));
802
			if (IS_itype_basic(is)) {
803
				BUILTIN_TYPE bt = nt;
804
				BUILTIN_TYPE bs =
805
				    DEREF_ntype(itype_basic_no(is));
806
				if (bs > bt) {
807
					bt = bs;
808
					bs = nt;
809
					ir = is;
810
				} else {
811
					ir = it;
812
				}
813
				if (bt <= ntype_ullong) {
814
					if (bt == ntype_sllong) {
815
						if (bs == ntype_ulong) {
816
							ir = arith_sllong_ulong;
817
						}
818
						if (bs == ntype_uint) {
819
							ir = arith_sllong_uint;
820
						}
821
					} else if (bt == ntype_slong) {
822
						if (bs == ntype_uint) {
823
							ir = arith_slong_uint;
824
						}
825
					}
826
					r = make_itype(ir, mr);
827
					return (r);
828
				}
829
			}
830
			if (nt == ntype_ullong) {
831
				r = make_itype(it, mr);
832
				return (r);
833
			}
834
			if (nt == ntype_ulong &&
835
			    !basetype_info[ntype_sllong].key) {
836
				r = make_itype(it, mr);
837
				return (r);
838
			}
839
			if (nt == ntype_sint) {
840
				r = make_itype(is, mr);
841
				return (r);
842
			}
2 7u83 843
		}
6 7u83 844
		if (IS_itype_basic(is)) {
845
			BUILTIN_TYPE ns = DEREF_ntype(itype_basic_no(is));
846
			if (ns == ntype_ullong) {
847
				r = make_itype(is, mr);
848
				return (r);
849
			}
850
			if (ns == ntype_ulong &&
851
			    !basetype_info[ntype_sllong].key) {
852
				r = make_itype(is, mr);
853
				return (r);
854
			}
855
			if (ns == ntype_sint) {
856
				r = make_itype(it, mr);
857
				return (r);
858
			}
859
		}
860
 
861
		/* Construct an arithmetic type */
862
		MAKE_itype_arith(NULL_type, all_prom_types, it, is, ir);
863
		r = promote_itype(ir, ir);
864
		if (!IS_NULL_itype(mr)) {
865
			r = make_itype(ir, mr);
866
		}
867
	} else {
868
		/* This shouldn't happen */
869
		r = t;
2 7u83 870
	}
6 7u83 871
	return (r);
2 7u83 872
}
873
 
874
 
875
/*
876
    CREATE A FLOATING POINT PROMOTION TYPE
877
 
878
    This routine sets the argument promotion type of the floating point
879
    type ft to be fp.  If fp is the null type then an argument promotion
880
    type is created.  A floating point type corresponding to ft is
881
    returned.
882
*/
883
 
6 7u83 884
TYPE
885
promote_ftype(FLOAT_TYPE ft, FLOAT_TYPE fp)
2 7u83 886
{
6 7u83 887
	TYPE t, p;
888
	init_float(ft);
889
	t = make_ftype(ft, ft);
890
	if (IS_NULL_ftype(fp)) {
891
		MAKE_ftype_arg_promote(NULL_type, ft, fp);
892
		init_float(fp);
893
	}
894
	if (EQ_ftype(ft, fp)) {
895
		p = t;
896
	} else {
897
		p = make_ftype(fp, ft);
898
	}
899
	COPY_type(ftype_arg_prom(ft), p);
900
	COPY_type(ftype_arg_prom(fp), p);
901
	return (t);
2 7u83 902
}
903
 
904
 
905
/*
906
    FIND AN ARITHMETIC FLOATING POINT TYPE
907
 
908
    This routine finds the type to be used for arithmetic involving operands
909
    of floating point types t and s.
910
*/
911
 
6 7u83 912
TYPE
913
arith_ftype(TYPE t, TYPE s)
2 7u83 914
{
6 7u83 915
	TYPE r;
916
	if (IS_type_floating(t) && IS_type_floating(s)) {
917
		FLOAT_TYPE fr;
918
		FLOAT_TYPE ft = DEREF_ftype(type_floating_rep(t));
919
		FLOAT_TYPE fs = DEREF_ftype(type_floating_rep(s));
2 7u83 920
 
6 7u83 921
		/* Find the arithmetic type */
922
		if (EQ_ftype(ft, fs)) {
923
			return (t);
924
		} else if (IS_ftype_basic(ft)) {
925
			BUILTIN_TYPE nt = DEREF_ntype(ftype_basic_no(ft));
926
			if (IS_ftype_basic(fs)) {
927
				BUILTIN_TYPE ns =
928
				    DEREF_ntype(ftype_basic_no(fs));
929
				if (nt == ntype_ldouble) {
930
					return (t);
931
				}
932
				if (ns == ntype_ldouble) {
933
					return (s);
934
				}
935
				if (nt == ntype_double) {
936
					return (t);
937
				}
938
				if (ns == ntype_double) {
939
					return (s);
940
				}
941
				return (t);
942
			}
943
			if (nt == ntype_ldouble) {
944
				return (t);
945
			}
946
			if (nt == ntype_float) {
947
				return (s);
948
			}
949
		} else if (IS_ftype_basic(fs)) {
950
			BUILTIN_TYPE ns = DEREF_ntype(ftype_basic_no(fs));
951
			if (ns == ntype_ldouble) {
952
				return (s);
953
			}
954
			if (ns == ntype_float) {
955
				return (t);
956
			}
957
		}
958
 
959
		/* Construct an arithmetic type */
960
		MAKE_ftype_arith(NULL_type, ft, fs, fr);
961
		r = promote_ftype(fr, NULL_ftype);
962
	} else {
963
		/* This shouldn't happen */
964
		r = t;
2 7u83 965
	}
6 7u83 966
	return (r);
2 7u83 967
}
968
 
969
 
970
/*
971
    FIND A KEYWORD TYPE
972
 
973
    This routine finds the type corresponding to the keyword with lexical
974
    token number tok.
975
*/
976
 
6 7u83 977
BASE_TYPE
978
key_type(int tok)
2 7u83 979
{
6 7u83 980
	BASE_TYPE bs = btype_none;
981
	switch (tok) {
982
	case lex_bool:
983
		bs = btype_bool;
984
		break;
985
	case lex_ptrdiff_Ht:
986
		bs = btype_ptrdiff_t;
987
		break;
988
	case lex_size_Ht:
989
		bs = btype_size_t;
990
		break;
991
	case lex_wchar_Ht:
992
		bs = btype_wchar_t;
993
		break;
994
	}
995
	return (bs);
2 7u83 996
}
997
 
998
 
999
/*
1000
    CREATE A TOKENISED INTEGRAL TYPE
1001
 
1002
    This routine creates a tokenised integral type from the token id
1003
    and the token arguments args.
1004
*/
1005
 
6 7u83 1006
TYPE
1007
apply_itype_token(IDENTIFIER id, LIST(TOKEN) args)
2 7u83 1008
{
6 7u83 1009
	TYPE t;
1010
	INT_TYPE it;
2 7u83 1011
 
6 7u83 1012
	/* Check for previous instance */
1013
	if (IS_NULL_list(args)) {
1014
		if (IS_id_token(id)) {
1015
			IDENTIFIER tid = DEREF_id(id_token_alt(id));
1016
			if (IS_id_type_alias(tid)) {
1017
				t = DEREF_type(id_type_alias_defn(tid));
1018
				t = copy_typedef(tid, t, cv_none);
1019
				COPY_id(type_name(t), tid);
1020
				return (t);
1021
			}
1022
		}
2 7u83 1023
	}
1024
 
6 7u83 1025
	/* Create new instance */
1026
	MAKE_itype_token(NULL_type, all_int_types, id, args, it);
1027
	t = promote_itype(it, NULL_itype);
2 7u83 1028
 
6 7u83 1029
	/* Allow for special tokens */
1030
	if (IS_id_token(id)) {
1031
		int tok = builtin_token(id);
1032
		switch (tok) {
1033
		case TOK_ptrdiff_t:
1034
			t = type_ptrdiff_t;
1035
			break;
1036
		case TOK_size_t:
1037
			t = type_size_t;
1038
			break;
1039
		case TOK_size_t_2:
1040
			t = promote_type(type_size_t);
1041
			break;
1042
		case TOK_wchar_t:
1043
			t = type_wchar_t;
1044
			break;
1045
		}
2 7u83 1046
	}
6 7u83 1047
	return (t);
2 7u83 1048
}
1049
 
1050
 
1051
/*
1052
    CREATE A TOKENISED FLOATING POINT TYPE
1053
 
1054
    This routine creates a tokenised floating point type from the token
1055
    id and the token arguments args.
1056
*/
1057
 
6 7u83 1058
TYPE
1059
apply_ftype_token(IDENTIFIER id, LIST(TOKEN) args)
2 7u83 1060
{
6 7u83 1061
	TYPE t;
1062
	FLOAT_TYPE ft;
2 7u83 1063
 
6 7u83 1064
	/* Check for previous instance */
1065
	if (IS_NULL_list(args)) {
1066
		if (IS_id_token(id)) {
1067
			IDENTIFIER tid = DEREF_id(id_token_alt(id));
1068
			if (IS_id_type_alias(tid)) {
1069
				t = DEREF_type(id_type_alias_defn(tid));
1070
				t = copy_typedef(tid, t, cv_none);
1071
				COPY_id(type_name(t), tid);
1072
				return (t);
1073
			}
1074
		}
2 7u83 1075
	}
1076
 
6 7u83 1077
	/* Create new instance */
1078
	MAKE_ftype_token(NULL_type, id, args, ft);
1079
	t = promote_ftype(ft, NULL_ftype);
1080
	return (t);
2 7u83 1081
}
1082
 
1083
 
1084
/*
1085
    FIND THE SIGN OF A TYPE
1086
 
1087
    This routine returns the sign of the type specified by bt.
1088
*/
1089
 
6 7u83 1090
static BASE_TYPE
1091
find_itype_sign(BASE_TYPE bt)
2 7u83 1092
{
6 7u83 1093
	BASE_TYPE sign;
1094
	if (bt & btype_unsigned) {
1095
		sign = btype_unsigned;
1096
	} else if (bt & btype_signed) {
1097
		sign = basetype_info[ntype_sint].sign;
1098
	} else {
1099
		sign = btype_none;
1100
	}
1101
	return (sign);
2 7u83 1102
}
1103
 
1104
 
1105
/*
1106
    FIND THE SIZE OF AN INTEGRAL TYPE
1107
 
1108
    This routine determines the minimum number of bits in the integral
1109
    type it.  It also returns information on the the maximum number of
1110
    bits and the sign of the type in the given pointer arguments.
1111
*/
1112
 
6 7u83 1113
static unsigned
1114
find_itype_size(INT_TYPE it, unsigned *mbits, BASE_TYPE *sign)
2 7u83 1115
{
6 7u83 1116
	unsigned sz;
1117
	it = expand_itype(it);
1118
	switch (TAG_itype(it)) {
1119
	case itype_basic_tag: {
1120
		/* Built-in types */
1121
		BUILTIN_TYPE n = DEREF_ntype(itype_basic_no(it));
1122
		sz = basetype_info[n].min_bits;
1123
		*mbits = basetype_info[n].max_bits;
1124
		*sign = basetype_info[n].sign;
1125
		break;
2 7u83 1126
	}
6 7u83 1127
	case itype_bitfield_tag: {
1128
		/* Bitfield types */
1129
		NAT n = DEREF_nat(itype_bitfield_size(it));
1130
		BASE_TYPE rep = DEREF_btype(itype_bitfield_rep(it));
1131
		sz = (unsigned)get_nat_value(n);
1132
		*mbits = sz;
1133
		*sign = find_itype_sign(rep);
1134
		break;
2 7u83 1135
	}
6 7u83 1136
	case itype_promote_tag: {
1137
		/* Promotion types */
1138
		unsigned si;
1139
		it = DEREF_itype(itype_promote_arg(it));
1140
		sz = find_itype_size(it, mbits, sign);
1141
		si = basetype_info[ntype_sint].min_bits;
1142
		if (sz < si) {
1143
			sz = si;
1144
			*mbits = basetype_info[ntype_ellipsis].max_bits;
1145
			*sign = btype_none;
1146
		}
1147
		break;
2 7u83 1148
	}
6 7u83 1149
	case itype_arith_tag: {
1150
		/* Arithmetic types */
1151
		INT_TYPE is = DEREF_itype(itype_arith_arg1(it));
1152
		unsigned ssz = find_itype_size(is, mbits, sign);
1153
		is = DEREF_itype(itype_arith_arg2(it));
1154
		sz = find_itype_size(is, mbits, sign);
1155
		if (sz < ssz) {
1156
			sz = ssz;
1157
		}
1158
		*mbits = basetype_info[ntype_ellipsis].max_bits;
1159
		*sign = btype_none;
1160
		break;
2 7u83 1161
	}
6 7u83 1162
	case itype_literal_tag: {
1163
		/* Literal types */
1164
		sz = basetype_info[ntype_sint].min_bits;
1165
		*mbits = basetype_info[ntype_ellipsis].max_bits;
1166
		*sign = btype_none;
1167
		break;
2 7u83 1168
	}
6 7u83 1169
	case itype_token_tag: {
1170
		/* Tokenised types */
1171
		BASE_TYPE bt = btype_none;
1172
		IDENTIFIER tid = DEREF_id(itype_token_tok(it));
1173
		TOKEN tok = DEREF_tok(id_token_sort(tid));
1174
		if (IS_tok_proc(tok)) {
1175
			tok = DEREF_tok(tok_proc_res(tok));
1176
		}
1177
		if (IS_tok_type(tok)) {
1178
			bt = DEREF_btype(tok_type_kind(tok));
1179
			bt = find_itype_sign(bt);
1180
		}
1181
		sz = basetype_info[ntype_ellipsis].min_bits;
1182
		*mbits = basetype_info[ntype_ellipsis].max_bits;
1183
		*sign = bt;
1184
		break;
2 7u83 1185
	}
6 7u83 1186
	default:
1187
		/* Other types */
1188
		sz = basetype_info[ntype_ellipsis].min_bits;
1189
		*mbits = basetype_info[ntype_ellipsis].max_bits;
1190
		*sign = btype_none;
1191
		break;
2 7u83 1192
	}
6 7u83 1193
	return (sz);
2 7u83 1194
}
1195
 
1196
 
1197
/*
1198
    FIND THE SIZE OF A TYPE
1199
 
1200
    This routine is identical to find_itype_size except that it works for
1201
    all integer, enumeration and bitfield types.
1202
*/
1203
 
6 7u83 1204
unsigned
1205
find_type_size(TYPE t, unsigned *mbits, BASE_TYPE *sign)
2 7u83 1206
{
6 7u83 1207
	switch (TAG_type(t)) {
1208
	case type_integer_tag: {
1209
		/* Integral types */
1210
		INT_TYPE it = DEREF_itype(type_integer_rep(t));
1211
		return (find_itype_size(it, mbits, sign));
2 7u83 1212
	}
6 7u83 1213
	case type_enumerate_tag: {
1214
		/* Enumeration types */
1215
		ENUM_TYPE et = DEREF_etype(type_enumerate_defn(t));
1216
		t = DEREF_type(etype_rep(et));
1217
		return (find_type_size(t, mbits, sign));
2 7u83 1218
	}
6 7u83 1219
	case type_bitfield_tag: {
1220
		/* Bitfield types */
1221
		INT_TYPE it = DEREF_itype(type_bitfield_defn(t));
1222
		return (find_itype_size(it, mbits, sign));
2 7u83 1223
	}
6 7u83 1224
	}
1225
	*mbits = basetype_info[ntype_ellipsis].max_bits;
1226
	*sign = btype_none;
1227
	return (0);
2 7u83 1228
}
1229
 
1230
 
1231
/*
1232
    CREATE AN ARITHMETIC INTEGRAL TYPE
1233
 
1234
    This routine constructs an non-trivial arithmetic integral type.
1235
    nu gives the first type (for example, 'unsigned int'), ns gives the
1236
    second type (for example, 'signed long'), and nt gives the default
1237
    arithmetic type (for example, 'unsigned long').
1238
*/
1239
 
6 7u83 1240
static INT_TYPE
1241
make_arith(BUILTIN_TYPE nu, BUILTIN_TYPE ns, BUILTIN_TYPE nt)
2 7u83 1242
{
6 7u83 1243
	INT_TYPE it;
1244
	int c = builtin_cast(nu, ns);
1245
	if (c <= min_builtin_cast) {
1246
		/* u definitely fits inside s */
1247
		TYPE s = type_builtin[ns];
1248
		it = DEREF_itype(type_integer_rep(s));
1249
	} else if (c <= safe_builtin_cast) {
1250
		/* u possibly fits inside s */
1251
		TYPE u = type_builtin[nu];
1252
		TYPE s = type_builtin[ns];
1253
		TYPE t = type_builtin[nt];
1254
		LIST(TYPE)pt = NULL_list(TYPE);
1255
		INT_TYPE ir = DEREF_itype(type_integer_rep(u));
1256
		INT_TYPE is = DEREF_itype(type_integer_rep(s));
1257
		CONS_type(t, pt, pt);
1258
		CONS_type(s, pt, pt);
1259
		pt = uniq_type_set(pt);
1260
		MAKE_itype_arith(NULL_type, pt, ir, is, it);
1261
		IGNORE promote_itype(it, it);
1262
	} else {
1263
		/* u definitely does not fit inside s */
1264
		TYPE t = type_builtin[nt];
1265
		it = DEREF_itype(type_integer_rep(t));
1266
	}
1267
	return (it);
2 7u83 1268
}
1269
 
1270
 
1271
/*
1272
    INITIALISE INTEGRAL TYPES
1273
 
1274
    This routine initialises the integral and floating-point types.
1275
*/
1276
 
6 7u83 1277
void
1278
init_itypes(int init)
2 7u83 1279
{
6 7u83 1280
	int c;
1281
	INT_TYPE it;
1282
	BUILTIN_TYPE n;
1283
	LIST(TYPE) qt;
1284
	unsigned long i, j;
1285
	BUILTIN_TYPE ntype_max = ntype_ulong;
2 7u83 1286
 
6 7u83 1287
	/* Initialise type tables */
1288
	if (init) {
1289
		for (i = 0; i < ORDER_ntype; i++) {
1290
			for (j = 0; j < ORDER_ntype; j++) {
1291
				all_itypes[i][j] = NULL_type;
1292
			}
1293
		}
2 7u83 1294
	}
1295
 
6 7u83 1296
	/* Initialise all the types */
1297
	for (n = ntype_none; n <= ntype_ellipsis; n++) {
1298
		TYPE t = NULL_type;
1299
		BUILTIN_TYPE m = n;
1300
		BASE_TYPE rep = basetype_info[n].rep;
1301
		switch (n) {
1302
		case ntype_none: {
1303
			/* Allow for inferred types */
1304
			m = ntype_sint;
1305
			goto default_lab;
2 7u83 1306
		}
6 7u83 1307
		default:
1308
default_lab: {
1309
		     /* Create an integral type */
1310
		     LIST(TYPE) pt = NULL_list(TYPE);
1311
		     MAKE_itype_basic(t, pt, rep, m, it);
1312
		     if (init) {
1313
			     MAKE_type_integer(cv_none, it, it, t);
1314
			     CONS_type(t, pt, pt);
1315
			     COPY_list(itype_cases(it), pt);
1316
			     all_itypes[n][n] = t;
1317
			     type_builtin[n] = t;
1318
		     } else {
1319
			     t = type_builtin[n];
1320
			     COPY_itype(type_integer_rep(t), it);
1321
			     COPY_itype(type_integer_sem(t), it);
1322
		     }
1323
		     break;
1324
	     }
1325
		case ntype_float:
1326
		case ntype_double:
1327
		case ntype_ldouble: {
1328
			/* Create a floating type */
1329
			FLOAT_TYPE ft;
1330
			MAKE_ftype_basic(NULL_type, rep, n, ft);
1331
			init_float(ft);
1332
			if (init) {
1333
				MAKE_type_floating(cv_none, ft, ft, t);
1334
				all_itypes[n][n] = t;
1335
				type_builtin[n] = t;
1336
			} else {
1337
				t = type_builtin[n];
1338
				COPY_ftype(type_floating_rep(t), ft);
1339
				COPY_ftype(type_floating_sem(t), ft);
1340
			}
1341
			break;
2 7u83 1342
		}
6 7u83 1343
		case ntype_void:
1344
			if (init) {
1345
				MAKE_type_top(cv_none, t);
1346
				type_builtin[n] = t;
1347
			}
1348
			break;
1349
		case ntype_bottom:
1350
			if (init) {
1351
				MAKE_type_bottom(cv_none, t);
1352
				type_builtin[n] = t;
1353
			}
1354
			break;
1355
		case ntype_ellipsis:
1356
			if (init) {
1357
				MAKE_type_pre(cv_none, rep, qual_none, t);
1358
				type_builtin[n] = t;
1359
			}
1360
			break;
2 7u83 1361
		}
1362
	}
1363
 
6 7u83 1364
	/* Set up list of all integral types */
1365
	qt = NULL_list(TYPE);
1366
	for (n = ntype_max; n >= ntype_char; n--) {
1367
		CONS_type(type_builtin[n], qt, qt);
1368
	}
1369
	all_int_types = uniq_type_set(qt);
2 7u83 1370
 
6 7u83 1371
	/* Set up list of all promoted types */
1372
	qt = NULL_list(TYPE);
1373
	for (n = ntype_max; n >= ntype_sint; n--) {
1374
		CONS_type(type_builtin[n], qt, qt);
1375
	}
1376
	all_prom_types = uniq_type_set(qt);
2 7u83 1377
 
6 7u83 1378
	/* Set up list of all promoted types (including long long) */
1379
	qt = NULL_list(TYPE);
1380
	for (n = ntype_ullong; n >= ntype_sint; n--) {
1381
		CONS_type(type_builtin[n], qt, qt);
1382
	}
1383
	all_llong_types = uniq_type_set(qt);
2 7u83 1384
 
6 7u83 1385
	/* Set up non-built-in types (also see init_tok) */
1386
	if (basetype_info[ntype_bool].key) {
1387
		base_token[ntype_bool].alt = ARITH_bool;
1388
	} else {
1389
		/* 'bool' is equal to 'int' for most purposes */
1390
		CONST char **nms = ntype_name ; /* SCO cc gets const wrong */
1391
		it = DEREF_itype(type_integer_rep(type_bool));
1392
		qt = NULL_list(TYPE);
1393
		CONS_type(type_sint, qt, qt);
1394
		COPY_list(itype_cases(it), qt);
1395
		base_token[ntype_bool].alt = ARITH_none;
1396
		nms[ntype_bool] = nms[ntype_sint];
1397
		mangle_ntype[ntype_bool][0] = MANGLE_int;
1398
	}
1399
	if (basetype_info[ntype_ptrdiff_t].key) {
1400
		base_token[ntype_ptrdiff_t].alt = ARITH_ptrdiff_t;
1401
	} else {
1402
		/* ptrdiff_t will be either 'int' or 'long' */
1403
		it = DEREF_itype(type_integer_rep(type_ptrdiff_t));
1404
		qt = NULL_list(TYPE);
1405
		CONS_type(type_slong, qt, qt);
1406
		CONS_type(type_sint, qt, qt);
1407
		COPY_list(itype_cases(it), qt);
1408
		base_token[ntype_ptrdiff_t].alt = ARITH_none;
1409
	}
1410
	if (basetype_info[ntype_size_t].key) {
1411
		base_token[ntype_size_t].alt = ARITH_size_t;
1412
	} else {
1413
		/* size_t will be either 'unsigned' or 'unsigned long' */
1414
		it = DEREF_itype(type_integer_rep(type_size_t));
1415
		qt = NULL_list(TYPE);
1416
		CONS_type(type_ulong, qt, qt);
1417
		CONS_type(type_uint, qt, qt);
1418
		COPY_list(itype_cases(it), qt);
1419
		base_token[ntype_size_t].alt = ARITH_none;
1420
	}
1421
	if (basetype_info[ntype_wchar_t].key) {
1422
		base_token[ntype_wchar_t].alt = ARITH_wchar_t;
1423
	} else {
1424
		/* wchar_t can be any type */
1425
		it = DEREF_itype(type_integer_rep(type_wchar_t));
1426
		qt = all_int_types;
1427
		COPY_list(itype_cases(it), qt);
1428
		base_token[ntype_wchar_t].alt = ARITH_none;
1429
	}
2 7u83 1430
 
6 7u83 1431
	/* Calculate all promotion types */
1432
	for (i = 0; i < ORDER_ntype; i++) {
1433
		TYPE t = type_builtin[i];
1434
		if (!IS_NULL_type(t)) {
1435
			switch (TAG_type(t)) {
1436
			case type_integer_tag: {
1437
				/* Calculate promotion for integral type */
1438
				INT_TYPE ip;
1439
				BUILTIN_TYPE m;
1440
				TYPE p = NULL_type;
1441
				LIST(TYPE)pt = NULL_list(TYPE);
1442
				it = DEREF_itype(type_integer_rep(t));
1443
				n = DEREF_ntype(itype_basic_no(it));
2 7u83 1444
 
6 7u83 1445
				/* int, unsigned etc. promote to themselves */
1446
				if (n >= ntype_sint && n <= ntype_ullong) {
1447
					if (do_dump && i) {
1448
						dump_promote(it, it);
1449
					}
1450
					COPY_type(itype_prom(it), t);
1451
					COPY_ntype(itype_unprom(it), n);
1452
					break;
1453
				}
2 7u83 1454
 
6 7u83 1455
				/* ptrdiff_t and size_t promote to themselves */
1456
				if (n == ntype_ptrdiff_t || n == ntype_size_t) {
1457
					if (do_dump) {
1458
						dump_promote(it, it);
1459
					}
1460
					COPY_type(itype_prom(it), t);
1461
					break;
1462
				}
2 7u83 1463
 
6 7u83 1464
				/* Construct promotion type for wchar_t */
1465
				if (n == ntype_wchar_t) {
1466
					IGNORE promote_itype(it, NULL_itype);
1467
					break;
1468
				}
2 7u83 1469
 
6 7u83 1470
				/* Find promotion type */
1471
				for (m = ntype_sint; m <= ntype_ulong; m++) {
1472
					c = builtin_cast(n, m);
1473
					if (c <= safe_builtin_cast) {
1474
						/* Possibly fits */
1475
						p = type_builtin[m];
1476
						CONS_type(p, pt, pt);
1477
					}
1478
					if (c <= min_builtin_cast) {
1479
						/* Definitely fits */
1480
						break;
1481
					}
1482
				}
1483
				if (LENGTH_list(pt) == 1) {
1484
					/* Unique promotion type */
1485
					p = DEREF_type(HEAD_list(pt));
1486
					ip = DEREF_itype(type_integer_rep(p));
1487
					p = make_itype(ip, it);
1488
					DESTROY_list(pt, SIZE_type);
1489
					if (do_dump) {
1490
						dump_promote(it, ip);
1491
					}
1492
				} else {
1493
					/* Construct promotion type */
1494
					pt = REVERSE_list(pt);
1495
					pt = uniq_type_set(pt);
1496
					MAKE_itype_promote(p, pt, it, ip);
1497
					p = make_itype(ip, it);
1498
					COPY_type(itype_prom(ip), p);
1499
				}
1500
				COPY_type(itype_prom(it), p);
1501
				break;
1502
			}
2 7u83 1503
 
6 7u83 1504
			case type_floating_tag: {
1505
				/* Calculate promotion for floating-point
1506
				 * type */
1507
				FLOAT_TYPE ft;
1508
				ft = DEREF_ftype(type_floating_rep(t));
1509
				n = DEREF_ntype(ftype_basic_no(ft));
1510
				if (n == ntype_float) {
1511
					FLOAT_TYPE fp;
1512
					TYPE p = type_double;
1513
					fp = DEREF_ftype(type_floating_rep(p));
1514
					p = make_ftype(fp, ft);
1515
					COPY_type(ftype_arg_prom(ft), p);
1516
				} else {
1517
					COPY_type(ftype_arg_prom(ft), t);
1518
				}
1519
				break;
2 7u83 1520
			}
1521
			}
1522
		}
1523
	}
1524
 
6 7u83 1525
	/* Calculate the arithmetic types */
1526
	it = make_arith(ntype_uint, ntype_slong, ntype_ulong);
1527
	arith_slong_uint = it;
1528
	it = make_arith(ntype_uint, ntype_sllong, ntype_ullong);
1529
	arith_sllong_uint = it;
1530
	it = make_arith(ntype_ulong, ntype_sllong, ntype_ullong);
1531
	arith_sllong_ulong = it;
1532
	return;
2 7u83 1533
}