Subversion Repositories tendra.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
7 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
7 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:-
7 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
7 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;
7 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;
7 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 "c_types.h"
63
#include "exp_ops.h"
64
#include "ftype_ops.h"
65
#include "id_ops.h"
66
#include "itype_ops.h"
67
#include "nat_ops.h"
68
#include "tok_ops.h"
69
#include "type_ops.h"
70
#include "error.h"
71
#include "catalog.h"
72
#include "option.h"
73
#include "basetype.h"
74
#include "class.h"
75
#include "constant.h"
76
#include "convert.h"
77
#include "chktype.h"
78
#include "function.h"
79
#include "identifier.h"
80
#include "inttype.h"
81
#include "lex.h"
82
#include "predict.h"
83
#include "preproc.h"
84
#include "redeclare.h"
85
#include "syntax.h"
86
#include "template.h"
87
#include "token.h"
88
 
89
 
90
/*
91
    CREATE A QUALIFIED TYPE
92
 
93
    This routine creates a type consisting of t qualified by the const-
94
    volatile qualifiers cv.  Note that the existing qualifiers for t are
95
    not taken into account in the result.  If t already has qualifier cv
96
    and force is false then t itself is returned, otherwise a copy of t
97
    with the appropriate qualifier is constructed.
98
*/
99
 
7 7u83 100
TYPE
101
qualify_type(TYPE t, CV_SPEC cv, int force)
2 7u83 102
{
7 7u83 103
	TYPE r;
104
	unsigned tag;
105
	IDENTIFIER tid;
2 7u83 106
 
7 7u83 107
	/* Just return t if it is correctly qualified */
108
	CV_SPEC qual = DEREF_cv(type_qual(t));
109
	if (qual == cv && !force) {
110
		return (t);
111
	}
2 7u83 112
 
7 7u83 113
	/* Copy the type otherwise */
114
	tag = TAG_type(t);
115
	ASSERT(ORDER_type == 18);
116
	switch (tag) {
117
	case type_pre_tag: {
118
		BASE_TYPE rep;
119
		QUALIFIER idtype;
120
		DECONS_type_pre(qual, tid, rep, idtype, t);
121
		MAKE_type_pre(cv, rep, idtype, r);
122
		break;
2 7u83 123
	}
7 7u83 124
	case type_integer_tag: {
125
		INT_TYPE rep;
126
		INT_TYPE sem;
127
		DECONS_type_integer(qual, tid, rep, sem, t);
128
		if (cv == cv_none && IS_NULL_id(tid) && !force) {
129
			r = make_itype(rep, sem);
130
			break;
131
		}
132
		MAKE_type_integer(cv, rep, sem, r);
133
		break;
2 7u83 134
	}
7 7u83 135
	case type_floating_tag: {
136
		FLOAT_TYPE rep;
137
		FLOAT_TYPE sem;
138
		DECONS_type_floating(qual, tid, rep, sem, t);
139
		if (cv == cv_none && IS_NULL_id(tid) && !force) {
140
			if (IS_ftype_basic(rep)) {
141
				BUILTIN_TYPE n =
142
				    DEREF_ntype(ftype_basic_no(rep));
143
				r = type_builtin[n];
144
				break;
145
			}
2 7u83 146
		}
7 7u83 147
		MAKE_type_floating(cv, rep, sem, r);
148
		break;
2 7u83 149
	}
7 7u83 150
	case type_top_tag: {
151
		tid = DEREF_id(type_name(t));
152
		if (cv == cv_none && IS_NULL_id(tid) && !force) {
153
			r = type_void;
154
			break;
155
		}
156
		MAKE_type_top(cv, r);
157
		break;
2 7u83 158
	}
7 7u83 159
	case type_bottom_tag: {
160
		tid = DEREF_id(type_name(t));
161
		if (cv == cv_none && IS_NULL_id(tid) && !force) {
162
			r = type_bottom;
163
			break;
164
		}
165
		MAKE_type_bottom(cv, r);
166
		break;
2 7u83 167
	}
7 7u83 168
	case type_ptr_tag: {
169
		TYPE sub;
170
		DECONS_type_ptr(qual, tid, sub, t);
171
		MAKE_type_ptr(cv, sub, r);
172
		break;
2 7u83 173
	}
7 7u83 174
	case type_ref_tag: {
175
		TYPE sub;
176
		CV_SPEC cv1 = (cv & cv_qual);
177
		DECONS_type_ref(qual, tid, sub, t);
178
		if (cv1) {
179
			/* Pass cv-qualifiers to base type */
180
			CV_SPEC cv2 = DEREF_cv(type_qual(sub));
181
			sub = qualify_type(sub,(cv1 | cv2), force);
182
			cv &= ~cv_qual;
183
		}
184
		MAKE_type_ref(cv, sub, r);
185
		break;
2 7u83 186
	}
187
#if LANGUAGE_CPP
7 7u83 188
	case type_ptr_mem_tag: {
189
		CLASS_TYPE of;
190
		TYPE sub;
191
		DECONS_type_ptr_mem(qual, tid, of, sub, t);
192
		MAKE_type_ptr_mem(cv, of, sub, r);
193
		break;
2 7u83 194
	}
195
#endif
7 7u83 196
	case type_func_tag: {
197
		int ell;
198
		TYPE ret;
199
		CV_SPEC mqual;
200
		NAMESPACE pars;
201
		LIST(TYPE)ptypes;
202
		LIST(TYPE)mtypes;
203
		LIST(TYPE)except;
204
		LIST(IDENTIFIER)pids;
205
		DECONS_type_func(qual, tid, ret, ptypes, ell, mqual, mtypes,
206
				 pars, pids, except, t);
207
		cv &= ~cv_qual;
208
		MAKE_type_func(cv, ret, ptypes, ell, mqual, mtypes, pars, pids,
209
			       except, r);
210
		break;
2 7u83 211
	}
7 7u83 212
	case type_array_tag: {
213
		TYPE sub;
214
		NAT size;
215
		CV_SPEC cv1 = (cv & cv_qual);
216
		DECONS_type_array(qual, tid, sub, size, t);
217
		if (cv1) {
218
			/* Pass cv-qualifiers to base type */
219
			CV_SPEC cv2 = DEREF_cv(type_qual(sub));
220
			sub = qualify_type(sub,(cv1 | cv2), force);
221
			cv &= ~cv_qual;
222
		}
223
		MAKE_type_array(cv, sub, size, r);
224
		break;
2 7u83 225
	}
7 7u83 226
	case type_bitfield_tag: {
227
		INT_TYPE defn;
228
		DECONS_type_bitfield(qual, tid, defn, t);
229
		MAKE_type_bitfield(cv, defn, r);
230
		break;
2 7u83 231
	}
7 7u83 232
	case type_compound_tag: {
233
		CLASS_TYPE defn;
234
		DECONS_type_compound(qual, tid, defn, t);
235
		MAKE_type_compound(cv, defn, r);
236
		break;
2 7u83 237
	}
7 7u83 238
	case type_enumerate_tag: {
239
		ENUM_TYPE defn;
240
		DECONS_type_enumerate(qual, tid, defn, t);
241
		MAKE_type_enumerate(cv, defn, r);
242
		break;
2 7u83 243
	}
7 7u83 244
	case type_token_tag: {
245
		INSTANCE app;
246
		IDENTIFIER tok;
247
		LIST(TOKEN)args;
248
		DECONS_type_token(qual, tid, tok, args, app, t);
249
		MAKE_type_token(cv, tok, args, r);
250
		COPY_inst(type_token_app(r), app);
251
		break;
2 7u83 252
	}
7 7u83 253
	case type_templ_tag: {
254
		int fix;
255
		TYPE defn;
256
		TOKEN sort;
257
		DECONS_type_templ(qual, tid, sort, defn, fix, t);
258
		MAKE_type_templ(cv, sort, defn, fix, r);
259
		break;
2 7u83 260
	}
7 7u83 261
	case type_instance_tag: {
262
		IDENTIFIER id;
263
		DECL_SPEC acc;
264
		DECONS_type_instance(qual, tid, id, acc, t);
265
		MAKE_type_instance(cv, id, acc, r);
266
		break;
2 7u83 267
	}
7 7u83 268
	case type_dummy_tag: {
269
		int tok;
270
		DECONS_type_dummy(qual, tid, tok, t);
271
		MAKE_type_dummy(cv, tok, r);
272
		break;
2 7u83 273
	}
7 7u83 274
	case type_error_tag: {
275
		tid = DEREF_id(type_name(t));
276
		cv &= cv_lvalue;
277
		MAKE_type_error(cv, r);
278
		break;
2 7u83 279
	}
280
	default : {
7 7u83 281
		FAIL(Invalid type);
282
		tid = NULL_id;
283
		r = t;
284
		break;
2 7u83 285
	}
7 7u83 286
	}
287
	COPY_id(type_name(r), tid);
288
	UNUSED(qual);
289
	return (r);
2 7u83 290
}
291
 
292
 
293
/*
294
    COPY A TYPEDEF TYPE
295
 
296
    This routine copies the typedef definition type t (corresponding to the
297
    typedef-name id) qualified by the const-volatile qualifiers cv.  Note
298
    that the qualifiers apply to the base type of an array rather than the
299
    array itself.  For example, in:
300
 
301
		typedef int array [3] ;
302
		const array var ;
303
 
304
    the type of var is 'const int [3]'.  Also any qualifiers applied to
305
    reference and function types are ignored.
306
*/
307
 
7 7u83 308
TYPE
309
copy_typedef(IDENTIFIER id, TYPE t, CV_SPEC cv)
2 7u83 310
{
7 7u83 311
	CV_SPEC qual = DEREF_cv(type_qual(t));
312
	CV_SPEC qual_new = (qual | cv);
313
	CV_SPEC qual_join = (qual & cv);
314
	if (qual_join && !IS_NULL_id(id)) {
315
		/* Warn about duplicate qualifiers */
316
		report(crt_loc, ERR_dcl_type_type_cv(id, qual_join));
2 7u83 317
	}
318
 
7 7u83 319
	/* Check types */
320
	switch (TAG_type(t)) {
321
	case type_ptr_tag: {
322
		/* Copy pointer types */
323
		TYPE s = DEREF_type(type_ptr_sub(t));
324
		TYPE r = copy_typedef(id, s, cv_none);
325
		if (!EQ_type(r, s) || qual_new != qual) {
326
			IDENTIFIER tid = DEREF_id(type_name(t));
327
			MAKE_type_ptr(qual_new, r, t);
328
			COPY_id(type_name(t), tid);
329
		}
330
		break;
2 7u83 331
	}
7 7u83 332
	case type_ref_tag: {
333
		/* Ignore qualifiers for references */
334
		TYPE s, r;
335
		if (cv && !IS_NULL_id(id)) {
336
			report(crt_loc, ERR_dcl_ref_cv_type(cv, id));
337
		}
338
		s = DEREF_type(type_ref_sub(t));
339
		r = copy_typedef(id, s, cv_none);
340
		if (!EQ_type(r, s)) {
341
			IDENTIFIER tid = DEREF_id(type_name(t));
342
			MAKE_type_ref(qual, r, t);
343
			COPY_id(type_name(t), tid);
344
		}
345
		break;
346
	}
2 7u83 347
#if LANGUAGE_CPP
7 7u83 348
	case type_ptr_mem_tag: {
349
		/* Copy pointer member types */
350
		TYPE s = DEREF_type(type_ptr_mem_sub(t));
351
		TYPE r = copy_typedef(id, s, cv_none);
352
		if (!EQ_type(r, s) || qual_new != qual) {
353
			IDENTIFIER tid = DEREF_id(type_name(t));
354
			CLASS_TYPE of = DEREF_ctype(type_ptr_mem_of(t));
355
			MAKE_type_ptr_mem(qual_new, of, r, t);
356
			COPY_id(type_name(t), tid);
357
		}
358
		break;
2 7u83 359
	}
360
#endif
7 7u83 361
	case type_func_tag: {
362
		/* Ignore qualifiers for functions */
363
		int ell;
364
		TYPE ret;
365
		CV_SPEC mqual;
366
		IDENTIFIER tid;
367
		NAMESPACE pars;
368
		LIST(TYPE)except;
369
		LIST(TYPE)ptypes;
370
		LIST(TYPE)mtypes;
371
		LIST(IDENTIFIER)pids;
372
		LIST(TYPE)mtypes_new;
373
		LIST(TYPE)ptypes_new = NULL_list(TYPE);
374
		LIST(IDENTIFIER)pids_new = NULL_list(IDENTIFIER);
2 7u83 375
 
7 7u83 376
		/* Warn about qualifiers */
377
		if (cv && !IS_NULL_id(id)) {
378
			report(crt_loc, ERR_dcl_fct_qual(cv, id));
379
		}
2 7u83 380
 
7 7u83 381
		/* Decompose old function type */
382
		DECONS_type_func(qual, tid, ret, ptypes, ell, mqual,
383
				 mtypes, pars, pids, except, t);
2 7u83 384
 
7 7u83 385
		/* Copy appropriate components */
386
		ret = copy_typedef(id, ret, cv_none);
387
		while (!IS_NULL_list(pids)) {
388
			/* Redeclare each parameter */
389
			TYPE s;
390
			IDENTIFIER pid = DEREF_id(HEAD_list(pids));
391
			pid = copy_id(pid, 1);
392
			CONS_id(pid, pids_new, pids_new);
393
			s = DEREF_type(id_parameter_type(pid));
394
			s = qualify_type(s, cv_none, 0);
395
			CONS_type(s, ptypes_new, ptypes_new);
396
			pids = TAIL_list(pids);
397
		}
398
		pids_new = REVERSE_list(pids_new);
399
		ptypes_new = REVERSE_list(ptypes_new);
400
		if (EQ_list(ptypes, mtypes)) {
401
			mtypes_new = ptypes_new;
402
		} else {
403
			TYPE r = DEREF_type(HEAD_list(mtypes));
404
			CONS_type(r, ptypes_new, mtypes_new);
405
		}
2 7u83 406
 
7 7u83 407
		/* Construct new function type */
408
		MAKE_type_func(qual, ret, ptypes_new, ell, mqual,
409
			       mtypes_new, pars, pids_new, except, t);
410
		COPY_id(type_name(t), tid);
411
		break;
2 7u83 412
	}
7 7u83 413
	case type_array_tag: {
414
		/* Qualify the base type of an array */
415
		NAT n = DEREF_nat(type_array_size(t));
416
		TYPE s = DEREF_type(type_array_sub(t));
417
		TYPE r = copy_typedef(id, s, cv);
418
		IDENTIFIER tid = DEREF_id(type_name(t));
419
		MAKE_type_array(qual, r, n, t);
420
		COPY_id(type_name(t), tid);
421
		break;
2 7u83 422
	}
7 7u83 423
	case type_token_tag: {
424
		/* Tokenised types */
425
		IDENTIFIER tok = DEREF_id(type_token_tok(t));
426
		DECL_SPEC ds = DEREF_dspec(id_storage(tok));
427
		if (!(ds & dspec_defn))goto default_lab;
428
		t = expand_type(t, 0);
429
		t = qualify_type(t, qual_new, 0);
430
		break;
2 7u83 431
	}
7 7u83 432
	case type_templ_tag: {
433
		/* Template types */
434
		TYPE s = DEREF_type(type_templ_defn(t));
435
		TOKEN tok = DEREF_tok(type_templ_sort(t));
436
		IDENTIFIER tid = DEREF_id(type_name(t));
437
		MAKE_type_templ(qual_new, tok, s, 1, t);
438
		COPY_id(type_name(t), tid);
439
		break;
2 7u83 440
	}
7 7u83 441
	default:
442
default_lab:
443
		/* Other types */
444
		if (qual_new != qual) {
445
			t = qualify_type(t, qual_new, 0);
446
		}
447
		break;
2 7u83 448
	}
7 7u83 449
	return (t);
2 7u83 450
}
451
 
452
 
453
/*
454
    CREATE AN LVALUE TYPE
455
 
456
    This routine creates a type which is an lvalue qualified version of t
457
    (also preserving any other type qualifiers).
458
*/
459
 
7 7u83 460
TYPE
461
lvalue_type(TYPE t)
2 7u83 462
{
7 7u83 463
	CV_SPEC qual = DEREF_cv(type_qual(t));
464
	if (qual & cv_lvalue) {
465
		return (t);
466
	}
467
	qual |= cv_lvalue;
468
	return (qualify_type(t, qual, 0));
2 7u83 469
}
470
 
471
 
472
/*
473
    CREATE AN RVALUE TYPE
474
 
475
    This routine creates a type which is identical to t except that it
476
    is an rvalue rather than an lvalue.
477
*/
478
 
7 7u83 479
TYPE
480
rvalue_type(TYPE t)
2 7u83 481
{
7 7u83 482
	CV_SPEC qual = DEREF_cv(type_qual(t));
483
	if (!(qual & cv_lvalue)) {
484
		return (t);
485
	}
486
	qual &= ~cv_lvalue;
487
	return (qualify_type(t, qual, 0));
2 7u83 488
}
489
 
490
 
491
/*
492
    COMBINE TWO PRE-TYPES
493
 
494
    The pre-types are a construction mechanism for allowing built-in types
495
    such as 'unsigned long int' to be built up.  Each pre-type is associated
496
    with a bitmask giving the basic keywords used in defining the type.
497
    This routine is the main constructor function, which combines two
498
    pre-types and returns the result.
499
*/
500
 
7 7u83 501
BASE_TYPE
502
join_pre_types(BASE_TYPE b1, BASE_TYPE b2)
2 7u83 503
{
7 7u83 504
	BASE_TYPE bt = (b1 | b2);
505
	BASE_TYPE bs = (b1 & b2);
506
	if (bs) {
507
		if (bs == btype_long) {
508
			if (!(bt & btype_long2)) {
509
				/* Allow 'long long' */
510
				bt |= btype_long2;
511
				return (bt);
512
			}
513
		}
514
		report(crt_loc, ERR_dcl_type_simple_dup(bs));
2 7u83 515
	}
7 7u83 516
	return (bt);
2 7u83 517
}
518
 
519
 
520
/*
521
    INJECT A TYPE INTO A PRE-TYPE
522
 
523
    This routine injects the type t into the pre-type p, returning the
524
    result.  p will be built up of pointers, arrays etc. but its base
525
    type will be null.  The routine injects t into the base type position.
526
    It also checks for illegal type constructions.  Note that p itself
527
    is built up using inject_pre_type, so all such illegal constructions
528
    will be detected.  Some of the error recovery for such constructions
529
    is mildly interesting.  Also note that bitfields are handled by a
530
    separate routine, make_bitfield_type.
531
*/
532
 
7 7u83 533
TYPE
534
inject_pre_type(TYPE p, TYPE t, int chk)
2 7u83 535
{
7 7u83 536
	TYPE q = p;
537
	unsigned long n = 0;
538
	if (IS_NULL_type(q)) {
539
		return (t);
540
	}
541
	if (IS_NULL_type(t)) {
542
		return (q);
543
	}
2 7u83 544
 
7 7u83 545
	/* Map 'ptr template' to 'template ptr' etc */
546
	if (IS_type_templ(t) && !IS_type_templ(q)) {
547
		TYPE s;
548
		int fix;
549
		TOKEN tok;
550
		CV_SPEC cv;
551
		IDENTIFIER tid;
552
		DECONS_type_templ(cv, tid, tok, s, fix, t);
553
		if (!fix) {
554
			s = inject_pre_type(q, s, chk);
555
			MAKE_type_templ(cv, tok, s, fix, t);
556
			COPY_id(type_name(t), tid);
557
			return (t);
558
		}
2 7u83 559
	}
560
 
7 7u83 561
	/* Scan down to incomplete component */
562
	do {
563
		switch (TAG_type(q)) {
564
		case type_ptr_tag: {
565
			/* Pointer types, t * */
566
			TYPE s = DEREF_type(type_ptr_sub(q));
567
			if (IS_NULL_type(s)) {
568
				switch (TAG_type(t)) {
569
				case type_ref_tag: {
570
					/* Can't have pointer to reference */
571
					report(crt_loc, ERR_dcl_ref_ptr());
572
					t = DEREF_type(type_ref_sub(t));
573
					if (!IS_NULL_type(t)) {
574
						t = rvalue_type(t);
575
					}
576
					break;
577
				}
578
				case type_bitfield_tag: {
579
					/* Can't have pointer to bitfield */
580
					report(crt_loc, ERR_class_bit_ptr());
581
					t = find_bitfield_type(t);
582
					break;
583
				}
584
				case type_func_tag: {
585
					/* Adjust function type */
586
					member_func_type(NULL_ctype, null_tag,
587
							 t);
588
					check_weak_func(t, 0);
589
					break;
590
				}
591
				}
592
				COPY_type(type_ptr_sub(q), t);
593
				if (chk) {
594
					s = t;
595
				}
2 7u83 596
			}
7 7u83 597
			q = s;
598
			break;
2 7u83 599
		}
7 7u83 600
		case type_ref_tag: {
601
			/* Reference types, t & */
602
			TYPE s = DEREF_type(type_ref_sub(q));
603
			if (IS_NULL_type(s)) {
604
				switch (TAG_type(t)) {
605
				case type_top_tag:
606
				case type_bottom_tag: {
607
					/* Can't have reference to void */
608
					report(crt_loc, ERR_dcl_ref_void(t));
609
					MODIFY_type_ptr_etc(type_ptr_tag, q);
610
					break;
611
				}
612
				case type_ref_tag: {
613
					/* Can't have reference to reference */
614
					report(crt_loc, ERR_dcl_ref_ref());
615
					t = DEREF_type(type_ref_sub(t));
616
					break;
617
				}
618
				case type_bitfield_tag: {
619
					/* Can't have reference to bitfield */
620
					report(crt_loc, ERR_class_bit_ref());
621
					t = find_bitfield_type(t);
622
					break;
623
				}
624
				case type_func_tag: {
625
					/* Adjust function type */
626
					member_func_type(NULL_ctype, null_tag,
627
							 t);
628
					check_weak_func(t, 0);
629
					break;
630
				}
631
				}
632
				/* All references are 'lvalue t &' */
633
				if (!IS_NULL_type(t)) {
634
					t = lvalue_type(t);
635
				}
636
				COPY_type(type_ptr_etc_sub(q), t);
637
				if (chk) {
638
					s = t;
639
				}
2 7u83 640
			}
7 7u83 641
			q = s;
642
			break;
2 7u83 643
		}
644
#if LANGUAGE_CPP
7 7u83 645
		case type_ptr_mem_tag: {
646
			/* Pointer to member types, t c::* */
647
			TYPE s = DEREF_type(type_ptr_mem_sub(q));
648
			if (IS_NULL_type(s)) {
649
				switch (TAG_type(t)) {
650
				case type_top_tag:
651
				case type_bottom_tag: {
652
					/* Can't have pointer to void member */
653
					report(crt_loc, ERR_dcl_mptr_void(t));
654
					break;
655
				}
656
				case type_ref_tag: {
657
					/* Can't have pointer to reference
658
					 * member */
659
					report(crt_loc, ERR_dcl_mptr_ref());
660
					t = DEREF_type(type_ref_sub(t));
661
					if (!IS_NULL_type(t))t = rvalue_type(t);
662
					break;
663
				}
664
				case type_bitfield_tag: {
665
					/* Can't have pointer to bitfield
666
					 * member */
667
					report(crt_loc, ERR_class_bit_mptr());
668
					t = find_bitfield_type(t);
669
					break;
670
				}
671
				case type_func_tag: {
672
					/* Adjust function type */
673
					CLASS_TYPE ct;
674
					ct = DEREF_ctype(type_ptr_mem_of(q));
675
					member_func_type(ct, id_mem_func_tag, t);
676
					check_weak_func(t, 0);
677
					break;
678
				}
679
				}
680
				COPY_type(type_ptr_mem_sub(q), t);
681
				if (chk) {
682
					s = t;
683
				}
2 7u83 684
			}
7 7u83 685
			q = s;
686
			break;
687
		}
688
#endif
689
		case type_func_tag: {
690
			/* Function types, t ( args ) */
691
			TYPE s = DEREF_type(type_func_ret(q));
692
			if (IS_NULL_type(s)) {
693
				ERROR err = NULL_err;
694
				switch (TAG_type(t)) {
695
				case type_array_tag: {
696
					/* Can't have function returning
697
					 * array */
698
					report(crt_loc, ERR_dcl_fct_array());
699
					t = DEREF_type(type_array_sub(t));
700
					MAKE_type_ptr(cv_none, t, t);
701
					break;
702
				}
703
				case type_bitfield_tag: {
704
					/* Can't have function returning
705
					 * bitfield */
706
					report(crt_loc, ERR_dcl_fct_bitf());
707
					t = find_bitfield_type(t);
708
					break;
709
				}
710
				case type_func_tag: {
711
					/* Can't have function returning
712
					 * function */
713
					report(crt_loc, ERR_dcl_fct_func());
714
					member_func_type(NULL_ctype, null_tag,
715
							 t);
716
					check_weak_func(t, 0);
717
					MAKE_type_ptr(cv_none, t, t);
718
					break;
719
				}
720
				case type_compound_tag: {
721
					/* Can't have function returning
722
					 * abstract */
723
					ERROR err1 = check_abstract(t);
724
					if (!IS_NULL_err(err1)) {
725
						ERROR err2 =
726
						    ERR_class_abstract_ret();
727
						err1 = concat_error(err1, err2);
728
						report(crt_loc, err1);
729
					}
730
					break;
731
				}
732
				}
733
				t = check_ret_type(t, &err, 0);
734
				if (!IS_NULL_err(err)) {
735
					err = concat_error(err,
736
							   ERR_dcl_fct_ret());
737
					report(crt_loc, err);
738
				}
739
				object_type(t, null_tag);
740
				COPY_type(type_func_ret(q), t);
741
				if (chk) {
742
					s = t;
743
				}
2 7u83 744
			}
7 7u83 745
			q = s;
746
			break;
2 7u83 747
		}
748
 
7 7u83 749
		case type_array_tag: {
750
			/* Array types, t [n] */
751
			TYPE s = DEREF_type(type_array_sub(q));
752
			if (IS_NULL_type(s)) {
753
				switch (TAG_type(t)) {
754
				case type_top_tag:
755
				case type_bottom_tag: {
756
					/* Can't have array of void */
757
					report(crt_loc, ERR_dcl_array_void(t));
758
					break;
759
				}
760
				case type_ref_tag: {
761
					/* Can't have array of references */
762
					report(crt_loc, ERR_dcl_ref_array());
763
					t = DEREF_type(type_ref_sub(t));
764
					if (!IS_NULL_type(t))t = rvalue_type(t);
765
					break;
766
				}
767
				case type_func_tag: {
768
					/* Can't have array of functions */
769
					report(crt_loc, ERR_dcl_array_func());
770
					member_func_type(NULL_ctype, null_tag,
771
							 t);
772
					check_weak_func(t, 0);
773
					MAKE_type_ptr(cv_none, t, t);
774
					break;
775
				}
776
				case type_bitfield_tag: {
777
					/* Can't have array of bitfields */
778
					report(crt_loc, ERR_dcl_array_bitf());
779
					t = find_bitfield_type(t);
780
					break;
781
				}
782
				case type_array_tag: {
783
					/* Check arrays of arrays */
784
					NAT m = DEREF_nat(type_array_size(t));
785
					if (IS_NULL_nat(m)) {
786
						ERROR err =
787
						    ERR_dcl_array_array();
788
						report(crt_loc, err);
789
					}
790
					break;
791
				}
792
				case type_compound_tag:
793
				case type_enumerate_tag: {
794
					ERROR err = check_complete(t);
795
					if (!IS_NULL_err(err)) {
796
						/* Can't have array of
797
						 * incomplete */
798
						ERROR err2 =
799
						    ERR_dcl_array_incompl();
800
						err = concat_error(err, err2);
801
						report(crt_loc, err);
802
					}
803
					err = check_abstract(t);
804
					if (!IS_NULL_err(err)) {
805
						/* Can't have array of
806
						 * abstract */
807
						ERROR err2 =
808
						    ERR_dcl_array_abstract();
809
						err = concat_error(err, err2);
810
						report(crt_loc, err);
811
					}
812
					break;
813
				}
814
				}
815
				COPY_type(type_array_sub(q), t);
816
				if (chk) {
817
					s = t;
818
				}
2 7u83 819
			}
7 7u83 820
			q = s;
821
			break;
2 7u83 822
		}
823
 
7 7u83 824
		case type_templ_tag: {
825
			/* Template types, template < ... > t */
826
			TYPE s = DEREF_type(type_templ_defn(q));
827
			if (IS_NULL_type(s)) {
828
				COPY_type(type_templ_defn(q), t);
829
				if (chk) {
830
					s = t;
831
				}
2 7u83 832
			}
7 7u83 833
			q = s;
834
			n--;
835
			break;
2 7u83 836
		}
837
 
7 7u83 838
		default: {
839
			/* Only the types above should appear */
840
			q = NULL_type;
841
			n--;
842
			break;
2 7u83 843
		}
7 7u83 844
		}
845
		n++;
846
	} while (!IS_NULL_type(q));
847
	if (chk) {
848
		IGNORE check_value(OPT_VAL_declarator_max, n);
2 7u83 849
	}
7 7u83 850
	return (p);
2 7u83 851
}
852
 
853
 
854
/*
855
    CHECK FOR INFERRED TYPES
856
 
857
    This routine checks whether the type t is formed from the inferred
858
    type type_inferred.  It returns INFERRED_EMPTY if there were no
859
    specifiers or qualifiers in the description of p,  INFERRED_QUAL if
860
    the only type specifiers were const or volatile, INFERRED_SPEC for
861
    other inferred types, and INFERRED_NOT for non-inferred types.
862
*/
863
 
7 7u83 864
int
865
is_type_inferred(TYPE t)
2 7u83 866
{
7 7u83 867
	if (EQ_type(t, type_inferred)) {
868
		return (INFERRED_EMPTY);
869
	}
870
	while (!IS_NULL_type(t)) {
871
		switch (TAG_type(t)) {
872
		case type_integer_tag: {
873
			/* Check integer types */
874
			if (EQ_type(t, type_inferred)) {
875
				return (INFERRED_SPEC);
876
			} else {
877
				INT_TYPE it = DEREF_itype(type_integer_rep(t));
878
				t = DEREF_type(itype_prom(it));
879
				if (EQ_type(t, type_inferred)) {
880
					/* Qualified inferred type */
881
					return (INFERRED_QUAL);
882
				}
883
			}
884
			return (INFERRED_NOT);
2 7u83 885
		}
7 7u83 886
		case type_ptr_tag:
887
		case type_ref_tag: {
888
			/* Check pointer and reference types */
889
			t = DEREF_type(type_ptr_etc_sub(t));
890
			break;
891
		}
2 7u83 892
#if LANGUAGE_CPP
7 7u83 893
		case type_ptr_mem_tag: {
894
			/* Check pointer to member types */
895
			t = DEREF_type(type_ptr_mem_sub(t));
896
			break;
897
		}
2 7u83 898
#endif
7 7u83 899
		case type_func_tag: {
900
			/* Check function return types only */
901
			t = DEREF_type(type_func_ret(t));
902
			break;
903
		}
904
		case type_array_tag: {
905
			/* Check array types */
906
			t = DEREF_type(type_array_sub(t));
907
			break;
908
		}
909
		case type_bitfield_tag: {
910
			/* Check bitfield types */
911
			t = find_bitfield_type(t);
912
			break;
913
		}
914
		case type_templ_tag: {
915
			/* Check template types */
916
			t = DEREF_type(type_templ_defn(t));
917
			break;
918
		}
919
		default : {
920
			/* Other types are not inferred */
921
			return (INFERRED_NOT);
922
		}
923
		}
2 7u83 924
	}
7 7u83 925
	return (INFERRED_QUAL);
2 7u83 926
}
927
 
928
 
929
/*
930
    CLEAN AN INFERRED TYPE
931
 
932
    This routine returns an equivalent type to the inferred type t, but
933
    with any inferred components replaced by 'signed int'.
934
*/
935
 
7 7u83 936
TYPE
937
clean_inferred_type(TYPE t)
2 7u83 938
{
7 7u83 939
	TYPE sub;
940
	CV_SPEC qual;
941
	IDENTIFIER tid;
942
	switch (TAG_type(t)) {
943
	case type_integer_tag: {
944
		if (EQ_type(t, type_inferred)) {
945
			/* Simple inferred type */
946
			t = type_sint;
947
		} else {
948
			INT_TYPE it = DEREF_itype(type_integer_rep(t));
949
			TYPE pt = DEREF_type(itype_prom(it));
950
			if (EQ_type(pt, type_inferred)) {
951
				/* Qualified inferred type */
952
				qual = DEREF_cv(type_qual(t));
953
				t = qualify_type(type_sint, qual, 0);
954
			}
2 7u83 955
		}
7 7u83 956
		break;
2 7u83 957
	}
7 7u83 958
	case type_ptr_tag: {
959
		DECONS_type_ptr(qual, tid, sub, t);
960
		sub = clean_inferred_type(sub);
961
		MAKE_type_ptr(qual, sub, t);
962
		COPY_id(type_name(t), tid);
963
		break;
2 7u83 964
	}
7 7u83 965
	case type_ref_tag: {
966
		DECONS_type_ref(qual, tid, sub, t);
967
		sub = clean_inferred_type(sub);
968
		MAKE_type_ref(qual, sub, t);
969
		COPY_id(type_name(t), tid);
970
		break;
2 7u83 971
	}
972
#if LANGUAGE_CPP
7 7u83 973
	case type_ptr_mem_tag: {
974
		CLASS_TYPE of;
975
		DECONS_type_ptr_mem(qual, tid, of, sub, t);
976
		sub = clean_inferred_type(sub);
977
		MAKE_type_ptr_mem(qual, of, sub, t);
978
		COPY_id(type_name(t), tid);
979
		break;
2 7u83 980
	}
981
#endif
7 7u83 982
	case type_func_tag: {
983
		int ell;
984
		CV_SPEC mqual;
985
		NAMESPACE pars;
986
		LIST(TYPE)ptypes;
987
		LIST(TYPE)mtypes;
988
		LIST(TYPE)except;
989
		LIST(IDENTIFIER)pids;
990
		DECONS_type_func(qual, tid, sub, ptypes, ell, mqual,
991
				 mtypes, pars, pids, except, t);
992
		sub = clean_inferred_type(sub);
993
		MAKE_type_func(qual, sub, ptypes, ell, mqual,
994
			       mtypes, pars, pids, except, t);
995
		COPY_id(type_name(t), tid);
996
		break;
2 7u83 997
	}
7 7u83 998
	case type_array_tag: {
999
		NAT size;
1000
		DECONS_type_array(qual, tid, sub, size, t);
1001
		sub = clean_inferred_type(sub);
1002
		MAKE_type_array(qual, sub, size, t);
1003
		COPY_id(type_name(t), tid);
1004
		break;
2 7u83 1005
	}
7 7u83 1006
	case type_bitfield_tag: {
1007
		INT_TYPE bf;
1008
		DECONS_type_bitfield(qual, tid, bf, t);
1009
		sub = DEREF_type(itype_bitfield_sub(bf));
1010
		sub = clean_inferred_type(sub);
1011
		COPY_type(itype_bitfield_sub(bf), sub);
1012
		MAKE_type_bitfield(qual, bf, t);
1013
		COPY_id(type_name(t), tid);
1014
		break;
2 7u83 1015
	}
7 7u83 1016
	case type_templ_tag: {
1017
		int fix;
1018
		TOKEN sort;
1019
		DECONS_type_templ(qual, tid, sort, sub, fix, t);
1020
		sub = clean_inferred_type(sub);
1021
		MAKE_type_templ(qual, sort, sub, fix, t);
1022
		COPY_id(type_name(t), tid);
1023
		break;
2 7u83 1024
	}
7 7u83 1025
	}
1026
	return (t);
2 7u83 1027
}
1028
 
1029
 
1030
/*
1031
    REPORT AN INFERRED TYPE
1032
 
1033
    This routine returns an error suitable for the inferred type t.
1034
    infer gives the value of is_type_inferred on t.
1035
*/
1036
 
7 7u83 1037
ERROR
1038
report_inferred_type(TYPE t, int infer)
2 7u83 1039
{
7 7u83 1040
	ERROR err = NULL_err;
1041
	switch (infer) {
1042
	case INFERRED_EMPTY:
1043
	case INFERRED_SPEC:
1044
		err = ERR_dcl_type_none();
1045
		break;
1046
	case INFERRED_QUAL:
1047
		err = ERR_dcl_type_qual();
1048
		break;
2 7u83 1049
	}
7 7u83 1050
	err = concat_error(err, ERR_dcl_type_infer(t));
1051
	return (err);
2 7u83 1052
}
1053
 
1054
 
1055
/*
1056
    BASE TYPE SHIFT
1057
 
1058
    This macro is used to shift a base type specifier into a more sensible
1059
    range for use in a switch statement.
1060
*/
1061
 
7 7u83 1062
#define BTYPE(B)	((B) >> 3)
2 7u83 1063
 
1064
 
1065
/*
1066
    FIND A BASE TYPE
1067
 
1068
    This routine determines the type given by the base type specifiers bt.
1069
*/
1070
 
7 7u83 1071
TYPE
1072
make_base_type(BASE_TYPE bt)
2 7u83 1073
{
7 7u83 1074
	TYPE t;
1075
	BASE_TYPE bm = bt;
1076
	BASE_TYPE bo = btype_none;
2 7u83 1077
 
7 7u83 1078
	if (bm & btype_other) {
1079
		/* Deal with non-integral cases */
1080
		if (bm & btype_void) {
1081
			t = type_void;
1082
			bm = btype_void;
1083
		} else if (bm & btype_double) {
1084
			if (bm & btype_long) {
1085
				t = type_ldouble;
1086
				bm = btype_ldouble;
1087
			} else {
1088
				t = type_double;
1089
				bm = btype_double;
1090
			}
1091
		} else if (bm & btype_float) {
1092
			if (bm & btype_long) {
1093
				if (bm & btype_long2) {
1094
					/* Map 'long long float' to 'long
1095
					 * double' */
1096
					bm = (btype_llong | btype_float);
1097
					bo = btype_ldouble;
1098
					t = type_ldouble;
1099
				} else {
1100
					/* Map 'long float' to 'double' */
1101
					bm = (btype_long | btype_float);
1102
					bo = btype_double;
1103
					t = type_double;
1104
				}
1105
			} else {
1106
				t = type_float;
1107
				bm = btype_float;
1108
			}
1109
		} else if (bm & btype_bool) {
1110
			t = type_bool;
1111
			bm = btype_bool;
1112
		} else if (bm & btype_wchar_t) {
1113
			t = type_wchar_t;
1114
			bm = btype_wchar_t;
1115
		} else if (bm & btype_bottom) {
1116
			t = type_bottom;
1117
			bm = btype_bottom;
1118
		} else if (bm & btype_size_t) {
1119
			t = type_size_t;
1120
			bm = btype_size_t;
1121
		} else if (bm & btype_ptrdiff_t) {
1122
			t = type_ptrdiff_t;
1123
			bm = btype_ptrdiff_t;
2 7u83 1124
		} else {
7 7u83 1125
			FAIL(Unknown type specifier);
1126
			bm = btype_sint;
1127
			t = type_sint;
2 7u83 1128
		}
1129
	} else {
7 7u83 1130
		do {
1131
			/* Deal with obvious integral cases */
1132
			switch (BTYPE(bm)) {
1133
			case BTYPE(btype_char): {
1134
				bm = btype_char;
1135
				t = type_char;
1136
				break;
1137
			}
1138
			case BTYPE(btype_signed | btype_char): {
1139
				bm = btype_schar;
1140
				t = type_schar;
1141
				break;
1142
			}
1143
			case BTYPE(btype_unsigned | btype_char): {
1144
				bm = btype_uchar;
1145
				t = type_uchar;
1146
				break;
1147
			}
1148
			case BTYPE(btype_short):
1149
			case BTYPE(btype_short | btype_int):
1150
			case BTYPE(btype_signed | btype_short):
1151
			case BTYPE(btype_signed | btype_short | btype_int): {
1152
				bm = btype_sshort;
1153
				t = type_sshort;
1154
				break;
1155
			}
1156
			case BTYPE(btype_unsigned | btype_short):
1157
			case BTYPE(btype_unsigned | btype_short | btype_int): {
1158
				bm = btype_ushort;
1159
				t = type_ushort;
1160
				break;
1161
			}
1162
			case BTYPE(btype_int):
1163
			case BTYPE(btype_none):
1164
			case BTYPE(btype_signed):
1165
			case BTYPE(btype_signed | btype_int): {
1166
				bm = btype_sint;
1167
				t = type_sint;
1168
				break;
1169
			}
1170
			case BTYPE(btype_unsigned):
1171
			case BTYPE(btype_unsigned | btype_int): {
1172
				bm = btype_uint;
1173
				t = type_uint;
1174
				break;
1175
			}
1176
			case BTYPE(btype_long):
1177
			case BTYPE(btype_long | btype_int):
1178
			case BTYPE(btype_signed | btype_long):
1179
			case BTYPE(btype_signed | btype_long | btype_int): {
1180
				bm = btype_slong;
1181
				t = type_slong;
1182
				break;
1183
			}
1184
			case BTYPE(btype_unsigned | btype_long):
1185
			case BTYPE(btype_unsigned | btype_long | btype_int): {
1186
				bm = btype_ulong;
1187
				t = type_ulong;
1188
				break;
1189
			}
1190
			case BTYPE(btype_llong):
1191
			case BTYPE(btype_llong | btype_int):
1192
			case BTYPE(btype_signed | btype_llong):
1193
			case BTYPE(btype_signed | btype_llong | btype_int): {
1194
				/* Allow for 'signed long long' */
1195
				if (basetype_info[ntype_sllong].key) {
1196
					report(crt_loc,
1197
					       ERR_dcl_type_simple_llong(bm));
1198
					bm = btype_sllong;
1199
					t = type_sllong;
1200
				} else {
1201
					bm = btype_slong;
1202
					t = type_slong;
1203
				}
1204
				break;
1205
			}
1206
			case BTYPE(btype_unsigned | btype_llong):
1207
			case BTYPE(btype_unsigned | btype_llong | btype_int): {
1208
				/* Allow for 'unsigned long long' */
1209
				if (basetype_info[ntype_sllong].key) {
1210
					report(crt_loc,
1211
					       ERR_dcl_type_simple_llong(bm));
1212
					bm = btype_ullong;
1213
					t = type_ullong;
1214
				} else {
1215
					bm = btype_ulong;
1216
					t = type_ulong;
1217
				}
1218
				break;
1219
			}
1220
			case BTYPE(btype_long | btype_char): {
1221
				/* Map 'long char' to 'wchar_t' */
1222
				bm = (btype_long | btype_char);
1223
				bo = btype_wchar_t;
1224
				t = type_wchar_t;
1225
				break;
1226
			}
1227
			default : {
1228
				/* Invalid type specifier */
1229
				bm = btype_none;
1230
				if (bt & btype_signed) {
1231
					bm |= btype_signed;
1232
				} else if (bt & btype_unsigned) {
1233
					bm |= btype_unsigned;
1234
				}
1235
				if (bt & btype_char) {
1236
					bm |= btype_char;
1237
				} else if (bt & btype_short) {
1238
					bm |= btype_short;
1239
				} else if (bt & btype_long2) {
1240
					bm |= btype_llong;
1241
				} else if (bt & btype_long) {
1242
					bm |= btype_long;
1243
				}
1244
				t = NULL_type;
1245
				break;
1246
			}
1247
			}
1248
		} while (IS_NULL_type(t));
2 7u83 1249
	}
1250
 
7 7u83 1251
	/* Check result */
1252
	if (bt & ~bm) {
1253
		/* Report excess type specifiers */
1254
		if (bo == btype_none) {
1255
			bo = (bt & bm);
2 7u83 1256
		}
7 7u83 1257
		report(crt_loc, ERR_dcl_type_simple_bad(bt, bo));
1258
	} else if (bo) {
1259
		/* Report non-standard type specifiers */
1260
		report(crt_loc, ERR_dcl_type_simple_bad(bt, bo));
1261
	}
1262
	return (t);
2 7u83 1263
}
1264
 
1265
 
1266
/*
1267
    SHOULD A TYPE NAME BE MARKED AS USED?
1268
 
1269
    This flag is used by complete_pre_type to decide whether a type
1270
    name should be marked as having been used.  This is not the case,
1271
    for example, in a class definition.
1272
*/
1273
 
7 7u83 1274
static int use_type_name = 1;
2 7u83 1275
 
1276
 
1277
/*
1278
    COMPLETE A PRE-TYPE
1279
 
1280
    This routine turns a pre-type comprising the base type qualifiers bt
1281
    and the type specifier t, used during the type construction routines,
1282
    into a genuine type.  The original type is destroyed.  The routine
1283
    also assigns the const-volatile qualifier cv to the output type.
1284
*/
1285
 
7 7u83 1286
TYPE
1287
complete_pre_type(BASE_TYPE bt, TYPE t, CV_SPEC cv, int infer)
2 7u83 1288
{
7 7u83 1289
	/* Deal with named types */
1290
	if (!IS_NULL_type(t)) {
1291
		if (bt) {
1292
			report(crt_loc, ERR_dcl_type_simple_undecl(bt, t));
2 7u83 1293
		}
7 7u83 1294
		if (IS_type_pre(t)) {
1295
			CV_SPEC qual;
1296
			QUALIFIER it;
1297
			IDENTIFIER id;
1298
			DESTROY_type_pre(destroy, qual, id, bt, it, t);
1299
			qual |= cv;
1300
			switch (bt) {
1301
			case btype_class:
1302
			case btype_struct:
1303
			case btype_union:
1304
			case btype_enum: {
1305
				/* Deal with elaborated types */
1306
				DECL_SPEC mode = dspec_used;
1307
				if (it != qual_none) {
1308
					mode = dspec_alias;
1309
				}
1310
				id = find_elaborate_type(id, bt, NULL_type,
1311
							 mode);
1312
				break;
1313
			}
1314
			}
1315
			if (IS_id_class_name_etc(id)) {
1316
				t = DEREF_type(id_class_name_etc_defn(id));
1317
				if (IS_type_templ(t)) {
1318
					t = deduce_type_template(id,
1319
								 use_type_name);
1320
				}
1321
				t = copy_typedef(id, t, qual);
1322
				COPY_id(type_name(t), id);
1323
				if (use_type_name) {
1324
					use_id(id, 0);
1325
				}
1326
			} else {
1327
				report(crt_loc, ERR_dcl_type_simple_undef(id));
1328
				t = type_error;
1329
			}
1330
		} else {
1331
			t = copy_typedef(NULL_id, t, cv);
2 7u83 1332
		}
7 7u83 1333
		return (t);
2 7u83 1334
	}
1335
 
7 7u83 1336
	/* Deal with type specifiers */
1337
	if (bt == btype_none) {
1338
		t = type_inferred;
1339
	} else {
1340
		t = make_base_type(bt);
2 7u83 1341
	}
7 7u83 1342
	if (cv) {
1343
		t = qualify_type(t, cv, 0);
1344
	}
1345
	if (infer) {
1346
		/* Check for inferred types */
1347
		int i = is_type_inferred(t);
1348
		if (i != INFERRED_NOT) {
1349
			ERROR err;
1350
			t = clean_inferred_type(t);
1351
			err = report_inferred_type(t, i);
1352
			report(crt_loc, err);
1353
		}
1354
	}
1355
	return (t);
2 7u83 1356
}
1357
 
1358
 
1359
/*
1360
    COMPLETE A NON-ELABORATED PRE-TYPE
1361
 
1362
    This routine is equivalent to complete_pre_type except that it leaves
1363
    elaborated type declarations (such as 'struct s') as pre-types.  It is
1364
    used in declarations without declarators where such declarations
1365
    resolve differently than in the main case.
1366
*/
1367
 
7 7u83 1368
TYPE
1369
empty_complete_pre_type(BASE_TYPE bt, TYPE t, CV_SPEC cv, int infer)
2 7u83 1370
{
7 7u83 1371
	/* Check for elaborated type declarations */
1372
	if (!IS_NULL_type(t) && IS_type_pre(t)) {
1373
		BASE_TYPE key = DEREF_btype(type_pre_rep(t));
1374
		switch (key) {
1375
		case btype_class:
1376
		case btype_struct:
1377
		case btype_union:
1378
		case btype_enum: {
1379
			if (bt) {
1380
				ERROR err = ERR_dcl_type_simple_undecl(bt, t);
1381
				report(crt_loc, err);
1382
			}
1383
			COPY_cv(type_qual(t), cv);
1384
			COPY_btype(type_pre_rep(t), key);
1385
			return (t);
2 7u83 1386
		}
7 7u83 1387
		}
2 7u83 1388
	}
1389
 
7 7u83 1390
	/* Otherwise obtain the completed type */
1391
	use_type_name = 0;
1392
	t = complete_pre_type(bt, t, cv, infer);
1393
	use_type_name = 1;
1394
	return (t);
2 7u83 1395
}
1396
 
1397
 
1398
/*
1399
    FIND UNDERLYING BITFIELD TYPE
1400
 
1401
    This routine returns the type underlying the bitfield type t.
1402
*/
1403
 
7 7u83 1404
TYPE
1405
find_bitfield_type(TYPE t)
2 7u83 1406
{
7 7u83 1407
	INT_TYPE it = DEREF_itype(type_bitfield_defn(t));
1408
	t = DEREF_type(itype_bitfield_sub(it));
1409
	return (t);
2 7u83 1410
}
1411
 
1412
 
1413
/*
1414
    FIND A BITFIELD REPRESENTATION
1415
 
1416
    This routine finds the bitfield representation for a bitfield originally
1417
    declared with representation bt which expands to the type t.
1418
*/
1419
 
7 7u83 1420
BASE_TYPE
1421
get_bitfield_rep(TYPE t, BASE_TYPE bt)
2 7u83 1422
{
7 7u83 1423
	int depth = 0;
1424
	IDENTIFIER id = DEREF_id(type_name(t));
1425
	while (!IS_NULL_id(id) && depth < 100) {
1426
		/* Scan down to original typedef definition */
1427
		bt = DEREF_btype(id_class_name_etc_rep(id));
1428
		t = DEREF_type(id_class_name_etc_defn(id));
1429
		id = DEREF_id(type_name(t));
1430
		depth++;
2 7u83 1431
	}
7 7u83 1432
	if (IS_type_integer(t)) {
1433
		INT_TYPE it = DEREF_itype(type_integer_rep(t));
1434
		if (IS_itype_basic(it)) {
1435
			BASE_TYPE br = DEREF_btype(itype_basic_rep(it));
1436
			if (bt & br) {
1437
				if (!(br & btype_char)) {
1438
					br &= ~btype_signed;
1439
				}
1440
				if (bt & btype_signed) {
1441
					br |= btype_signed;
1442
				}
1443
			}
1444
			bt = br;
1445
		}
1446
	}
1447
	return (bt);
2 7u83 1448
}
1449
 
1450
 
1451
/*
1452
    CHECK A BITFIELD TYPE
1453
 
1454
    This routine creates a bitfield type with base type t and width n.
1455
    Note that in 'int : 3', t will be 'signed int' whereas the sign of
1456
    the bitfield itself is implementation dependent.  For this reason
1457
    the pre-type which gave rise to t, bt (in this case 'int'), is also
1458
    given.  The zero argument is true if zero sized bitfields are
1459
    allowed.
1460
*/
1461
 
7 7u83 1462
TYPE
1463
check_bitfield_type(CV_SPEC cv, TYPE t, BASE_TYPE bt, NAT n, int zero)
2 7u83 1464
{
7 7u83 1465
	INT_TYPE bf;
1466
	TYPE p = NULL_type;
1467
	DECL_SPEC info = dspec_none;
1468
	CV_SPEC qual = DEREF_cv(type_qual(t));
1469
	if (qual) {
1470
		/* Move any cv-qualifiers to top level */
1471
		t = qualify_type(t, cv_none, 0);
1472
		cv |= qual;
2 7u83 1473
	}
7 7u83 1474
	switch (TAG_type(t)) {
1475
	case type_integer_tag:
1476
	case type_enumerate_tag: {
1477
		/* Integral types are allowed */
1478
		int ok = 1;
1479
		bt = get_bitfield_rep(t, bt);
1480
		if (bt & btype_int) {
1481
			if (bt & (btype_short | btype_long)) {
1482
				ok = 0;
1483
			}
1484
		} else {
1485
			ok = 0;
1486
		}
1487
		if (!ok) {
1488
			/* Only 'int' types allowed in C */
1489
			report(crt_loc, ERR_class_bit_base_int(t));
1490
		}
1491
		if (!(bt & (btype_signed | btype_unsigned))) {
1492
			/* No sign given in type specifier */
1493
			report(crt_loc, ERR_class_bit_sign(bt));
1494
		}
1495
		break;
2 7u83 1496
	}
7 7u83 1497
	case type_token_tag: {
1498
		/* Allow template parameter types */
1499
		if (is_templ_type(t)) {
1500
			IDENTIFIER id = DEREF_id(type_token_tok(t));
1501
			LIST(TOKEN)args = DEREF_list(type_token_args(t));
1502
			t = apply_itype_token(id, args);
1503
			bt = (btype_named | btype_template);
1504
			break;
1505
		}
1506
		report(crt_loc, ERR_class_bit_base(t));
1507
		return (t);
2 7u83 1508
	}
7 7u83 1509
	case type_error_tag: {
1510
		/* Ignore error types */
1511
		return (t);
2 7u83 1512
	}
7 7u83 1513
	default: {
1514
		/* Other types are not allowed */
1515
		report(crt_loc, ERR_class_bit_base(t));
1516
		return (t);
1517
	}
1518
	}
2 7u83 1519
 
7 7u83 1520
	/* Check the range of n */
1521
	if (IS_NULL_nat(n) || is_zero_nat(n)) {
1522
		/* Only anonymous bitfields can have size zero */
1523
		if (!zero) {
1524
			report(crt_loc, ERR_class_bit_dim_zero());
1525
		}
1526
		info |= dspec_pure;
1527
		n = small_nat[0];
1528
		p = type_sint;
1529
	} else {
1530
		if (is_negative_nat(n)) {
1531
			/* Bitfield size cannot be negative */
1532
			report(crt_loc, ERR_class_bit_dim_neg(n));
1533
			n = negate_nat(n);
1534
		}
1535
		if (check_type_size(t, n) > 0) {
1536
			report(crt_loc, ERR_class_bit_dim_big(n, t));
1537
		}
1538
		if (IS_nat_small(n)) {
1539
			unsigned sz = DEREF_unsigned(nat_small_value(n));
1540
			unsigned si = basetype_info[ntype_sint].min_bits;
1541
			if (sz < si) {
1542
				/* Fits into 'signed int' */
1543
				p = type_sint;
1544
			} else if (sz == si && (bt & btype_unsigned)) {
1545
				/* Fits into 'unsigned int' */
1546
				p = type_uint;
1547
			}
1548
		}
2 7u83 1549
	}
7 7u83 1550
	if (IS_NULL_type(p)) {
1551
		/* NOT YET IMPLEMENTED */
1552
		p = promote_type(t);
2 7u83 1553
	}
7 7u83 1554
 
1555
	/* Construct bitfield type */
1556
	if (zero) {
1557
		info |= dspec_ignore;
2 7u83 1558
	}
7 7u83 1559
	MAKE_itype_bitfield(p, NULL_list(TYPE), t, bt, n, info, bf);
1560
	MAKE_type_bitfield(cv, bf, t);
1561
	return (t);
2 7u83 1562
}
1563
 
1564
 
1565
/*
1566
    CREATE A BITFIELD TYPE
1567
 
1568
    This routine is identical to check_bitfield_type except that it
1569
    takes a constant-expression e rather than the integer constant n.
1570
*/
1571
 
7 7u83 1572
TYPE
1573
make_bitfield_type(TYPE t, BASE_TYPE bt, EXP e, int zero)
2 7u83 1574
{
7 7u83 1575
	ERROR err = NULL_err;
1576
	NAT n = make_nat_exp(e, &err);
1577
	if (!IS_NULL_err(err)) {
1578
		err = concat_error(err, ERR_class_bit_dim_const());
1579
		report(crt_loc, err);
1580
	}
1581
	t = check_bitfield_type(cv_none, t, bt, n, zero);
1582
	return (t);
2 7u83 1583
}
1584
 
1585
 
1586
/*
1587
    CHECK AN ARRAY BOUND
1588
 
1589
    This routine checks the array bound n.  n can be null, indicating an
1590
    unbounded array, or a positive constant.
1591
*/
1592
 
7 7u83 1593
NAT
1594
check_array_dim(NAT n)
2 7u83 1595
{
7 7u83 1596
	if (!IS_NULL_nat(n)) {
1597
		if (is_zero_nat(n)) {
1598
			report(crt_loc, ERR_dcl_array_dim_zero());
1599
		} else if (is_negative_nat(n)) {
1600
			report(crt_loc, ERR_dcl_array_dim_neg(n));
1601
			n = negate_nat(n);
1602
		}
2 7u83 1603
	}
7 7u83 1604
	return (n);
2 7u83 1605
}
1606
 
1607
 
1608
/*
1609
    CREATE AN ARRAY BOUND
1610
 
1611
    This routine turns the integer constant expression e into an array
1612
    bound.  e may also be the null expression, indicating an unbounded
1613
    array.
1614
*/
1615
 
7 7u83 1616
NAT
1617
make_array_dim(EXP e)
2 7u83 1618
{
7 7u83 1619
	if (!IS_NULL_exp(e)) {
1620
		ERROR err = NULL_err;
1621
		NAT n = make_nat_exp(e, &err);
1622
		if (!IS_NULL_err(err)) {
1623
			err = concat_error(err, ERR_dcl_array_dim_const());
1624
			report(crt_loc, err);
1625
		}
1626
		n = check_array_dim(n);
1627
		return (n);
2 7u83 1628
	}
7 7u83 1629
	return (NULL_nat);
2 7u83 1630
}
1631
 
1632
 
1633
/*
1634
    CHECK AN INTEGER TYPE
1635
 
1636
    This routine checks whether t is an integral type containing the base
1637
    type specifiers m.  It is used to spot signed types, unsigned types,
1638
    character types and so on.
1639
*/
1640
 
7 7u83 1641
int
1642
check_int_type(TYPE t, BASE_TYPE m)
2 7u83 1643
{
7 7u83 1644
	if (IS_type_integer(t)) {
1645
		INT_TYPE it = DEREF_itype(type_integer_rep(t));
1646
		switch (TAG_itype(it)) {
1647
		case itype_basic_tag: {
1648
			BASE_TYPE bt = DEREF_btype(itype_basic_rep(it));
1649
			if ((bt & m) == m) {
1650
				return (1);
1651
			}
1652
			break;
2 7u83 1653
		}
7 7u83 1654
		case itype_token_tag: {
1655
			/* Tokenised types */
1656
			IDENTIFIER tid = DEREF_id(itype_token_tok(it));
1657
			TOKEN tok = DEREF_tok(id_token_sort(tid));
1658
			if (IS_tok_proc(tok)) {
1659
				tok = DEREF_tok(tok_proc_res(tok));
1660
			}
1661
			if (IS_tok_type(tok)) {
1662
				BASE_TYPE bt = DEREF_btype(tok_type_kind(tok));
1663
				if ((bt & m) == m) {
1664
					return (1);
1665
				}
1666
			}
1667
			break;
2 7u83 1668
		}
7 7u83 1669
		}
2 7u83 1670
	}
7 7u83 1671
	return (0);
2 7u83 1672
}
1673
 
1674
 
1675
/*
1676
    LISTS OF COMPATIBLE TYPES
1677
 
1678
    These lists are used to record the compatible types set by the
1679
    routines below.
1680
*/
1681
 
7 7u83 1682
static LIST(TYPE) arg1_types = NULL_list(TYPE);
1683
static LIST(TYPE) arg2_types = NULL_list(TYPE);
1684
static LIST(TYPE) ell_types = NULL_list(TYPE);
2 7u83 1685
 
1686
 
1687
/*
1688
    SET TWO TYPES TO BE COMPATIBLE
1689
 
1690
    This routine sets the types t and s to be compatible with option
1691
    level opt.
1692
*/
1693
 
7 7u83 1694
void
1695
set_compatible_type(TYPE t, TYPE s, unsigned opt)
2 7u83 1696
{
7 7u83 1697
	TYPE pt = type_char_star;
1698
	TYPE ps = type_void_star;
1699
	if (eq_type(t, pt) && eq_type(s, ps)) {
1700
		set_option(OPT_gen_ptr_char, opt);
1701
	} else {
1702
		report(preproc_loc, ERR_pragma_compat_type());
1703
	}
1704
	return;
2 7u83 1705
}
1706
 
1707
 
1708
/*
1709
    FIND CANONICAL ARGUMENT TYPE
1710
 
1711
    This routine finds the canonical argument type for the type t by
1712
    chasing down the lists of compatible argument types.  It returns
1713
    the null type if t is not compatible with a type from this list.
1714
*/
1715
 
7 7u83 1716
static
1717
TYPE find_arg_type(TYPE t)
2 7u83 1718
{
7 7u83 1719
	LIST(TYPE)p = arg1_types;
1720
	LIST(TYPE)q = arg2_types;
1721
	while (!IS_NULL_list(p)) {
1722
		TYPE r = DEREF_type(HEAD_list(p));
1723
		r = type_composite(t, r, 0, 1, KILL_err, 0);
1724
		if (!IS_NULL_type(r)) {
1725
			r = DEREF_type(HEAD_list(q));
1726
			return (r);
1727
		}
1728
		q = TAIL_list(q);
1729
		p = TAIL_list(p);
2 7u83 1730
	}
7 7u83 1731
	return (NULL_type);
2 7u83 1732
}
1733
 
1734
 
1735
/*
1736
    SET TWO TYPES TO BE ARGUMENT COMPATIBLE
1737
 
1738
    This routine sets the types t and s to be compatible as function
1739
    parameters.
1740
*/
1741
 
7 7u83 1742
void
1743
accept_argument(TYPE t, TYPE s)
2 7u83 1744
{
7 7u83 1745
	TYPE pt, ps;
1746
	LIST(TYPE)p;
1747
	t = qualify_type(t, cv_none, 0);
1748
	t = make_param_type(t, CONTEXT_PARAMETER);
1749
	s = qualify_type(s, cv_none, 0);
1750
	s = make_param_type(s, CONTEXT_PARAMETER);
1751
	pt = find_arg_type(t);
1752
	ps = find_arg_type(s);
1753
	if (IS_NULL_type(ps)) {
1754
		ps = s;
2 7u83 1755
	}
7 7u83 1756
	if (!IS_NULL_type(pt)) {
1757
		/* Already have entry for t */
1758
		if (!eq_type(ps, pt)) {
1759
			report(preproc_loc, ERR_pragma_arg_dup(t));
1760
		}
1761
		return;
1762
	}
1763
	pt = type_composite(ps, t, 0, 1, KILL_err, 0);
1764
	if (!IS_NULL_type(pt)) {
1765
		report(preproc_loc, ERR_pragma_arg_cycle());
1766
		return;
1767
	}
1768
	p = arg2_types;
1769
	CONS_type(t, arg1_types, arg1_types);
1770
	CONS_type(ps, p, arg2_types);
1771
	while (!IS_NULL_list(p)) {
1772
		pt = DEREF_type(HEAD_list(p));
1773
		pt = type_composite(pt, t, 0, 1, KILL_err, 0);
1774
		if (!IS_NULL_type(pt)) {
1775
			COPY_type(HEAD_list(p), ps);
1776
		}
1777
		p = TAIL_list(p);
1778
	}
1779
	return;
2 7u83 1780
}
1781
 
1782
 
1783
/*
1784
    SET A TYPE TO BE ELLIPSIS COMPATIBLE
1785
 
1786
    This routine sets the type t to be compatible with an ellipsis in
1787
    function parameters.
1788
*/
1789
 
7 7u83 1790
void
1791
accept_ellipsis(TYPE t)
2 7u83 1792
{
7 7u83 1793
	TYPE r;
1794
	t = qualify_type(t, cv_none, 0);
1795
	t = make_param_type(t, CONTEXT_PARAMETER);
1796
	r = eq_ellipsis(t);
1797
	if (IS_NULL_type(r)) {
1798
		CONS_type(t, ell_types, ell_types);
1799
	}
1800
	return;
2 7u83 1801
}
1802
 
1803
 
1804
/*
1805
    ARE TWO TYPES ARGUMENT COMPATIBLE?
1806
 
1807
    This routine checks whether the types t and s are compatible as
1808
    function parameters.  If eq is true then compatibility is only
1809
    checked if one of the types appears in the list of types.  The
1810
    routine returns the composite type.
1811
*/
1812
 
7 7u83 1813
TYPE
1814
eq_argument(TYPE t, TYPE s, int eq)
2 7u83 1815
{
7 7u83 1816
	TYPE pt = find_arg_type(t);
1817
	TYPE ps = find_arg_type(s);
1818
	if (EQ_type(pt, ps)) {
1819
		if (!IS_NULL_type(pt)) {
1820
			return (pt);
1821
		}
1822
		if (eq) {
1823
			return (NULL_type);
1824
		}
1825
	}
1826
	if (IS_NULL_type(pt)) {
1827
		pt = t;
1828
	}
1829
	if (IS_NULL_type(ps)) {
1830
		ps = s;
1831
	}
1832
	pt = type_composite(pt, ps, 0, 1, KILL_err, 1);
1833
	return (pt);
2 7u83 1834
}
1835
 
1836
 
1837
/*
1838
    IS A TYPE ELLIPSIS COMPATIBLE?
1839
 
1840
    This routine checks whether the type t is compatible with an ellipsis
1841
    in function parameters.
1842
*/
1843
 
7 7u83 1844
TYPE
1845
eq_ellipsis(TYPE t)
2 7u83 1846
{
7 7u83 1847
	LIST(TYPE)p = ell_types;
1848
	while (!IS_NULL_list(p)) {
1849
		TYPE r = DEREF_type(HEAD_list(p));
1850
		TYPE s = eq_argument(t, r, 0);
1851
		if (!IS_NULL_type(s)) {
1852
			return (s);
1853
		}
1854
		p = TAIL_list(p);
1855
	}
1856
	return (NULL_type);
2 7u83 1857
}
1858
 
1859
 
1860
/*
1861
    BASIC TYPES
1862
 
1863
    These variables stand for the basic types which arise naturally in
1864
    C and C++.  This includes the basic integer and floating-point types,
1865
    bool, size_t and related construct types, void and bottom (the type
1866
    of an expression which does not return), plus a few useful composite
1867
    types.
1868
*/
1869
 
7 7u83 1870
TYPE type_builtin[ORDER_ntype];
1871
TYPE ptr_type_builtin[ORDER_ntype];
1872
TYPE type_func_void;
1873
TYPE type_temp_func;
1874
TYPE type_ellipsis;
1875
TYPE type_error;
2 7u83 1876
 
1877
 
1878
/*
1879
    INITIALISE BASIC TYPES
1880
 
1881
    This routine initialises the basic types above.
1882
*/
1883
 
7 7u83 1884
void
1885
init_types(void)
2 7u83 1886
{
7 7u83 1887
	unsigned long n;
1888
	init_itypes(1);
1889
	for (n = 0; n < ORDER_ntype; n++) {
1890
		MAKE_type_ptr(cv_none, type_builtin[n], ptr_type_builtin[n]);
1891
	}
1892
	MAKE_type_func(cv_lvalue, type_void, NULL_list(TYPE), 0, cv_lang,
1893
		       NULL_list(TYPE), NULL_nspace, NULL_list(IDENTIFIER),
1894
		       NULL_list(TYPE), type_func_void);
1895
	MAKE_type_func(cv_none, type_void, NULL_list(TYPE), 0, cv_lang,
1896
		       NULL_list(TYPE), NULL_nspace, NULL_list(IDENTIFIER),
1897
		       NULL_list(TYPE), type_temp_func);
1898
	MAKE_type_error(cv_none, type_error);
1899
	type_ellipsis = NULL_type;
1900
	return;
2 7u83 1901
}