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