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 "ctype_ops.h"
64
#include "etype_ops.h"
65
#include "ftype_ops.h"
66
#include "hashid_ops.h"
67
#include "id_ops.h"
68
#include "inst_ops.h"
69
#include "itype_ops.h"
70
#include "tok_ops.h"
71
#include "type_ops.h"
72
#include "error.h"
73
#include "catalog.h"
74
#include "option.h"
75
#include "basetype.h"
76
#include "check.h"
77
#include "chktype.h"
78
#include "class.h"
79
#include "constant.h"
80
#include "convert.h"
81
#include "function.h"
82
#include "instance.h"
83
#include "inttype.h"
84
#include "literal.h"
85
#include "merge.h"
86
#include "namespace.h"
87
#include "predict.h"
88
#include "printf.h"
89
#include "template.h"
90
#include "tok.h"
91
#include "tokdef.h"
92
#include "token.h"
93
 
94
 
95
/*
96
    FIND A TYPE TAG
97
 
98
    This routine finds the tag of the type t, ignoring any template
99
    qualifiers.
100
*/
101
 
7 7u83 102
unsigned
103
type_tag(TYPE t)
2 7u83 104
{
7 7u83 105
	if (!IS_NULL_type(t)) {
106
		unsigned tag = TAG_type(t);
107
		if (tag == type_templ_tag) {
108
			TYPE s = DEREF_type(type_templ_defn(t));
109
			tag = type_tag(s);
110
		}
111
		return (tag);
2 7u83 112
	}
7 7u83 113
	return (null_tag);
2 7u83 114
}
115
 
116
 
117
/*
118
    FIND THE CATEGORY OF A TYPE
119
 
120
    This routine returns the type category associated with the type pointed
121
    to by pt.  If this is a reference type then the result is the category
122
    of the referenced type (which is always an lvalue).  If it is a tokenised
123
    type then the token is expanded and returned via pt.
124
*/
125
 
7 7u83 126
unsigned
127
type_category(TYPE *pt)
2 7u83 128
{
7 7u83 129
	TYPE t = *pt;
130
	unsigned res = CTYPE_NONE;
131
	if (!IS_NULL_type(t)) {
132
		CV_SPEC qual = DEREF_cv(type_qual(t));
133
		switch (TAG_type(t)) {
134
		case type_integer_tag:
135
			res = CTYPE_INTEGER;
136
			break;
137
		case type_floating_tag:
138
			res = CTYPE_FLOAT;
139
			break;
140
		case type_top_tag:
141
			res = CTYPE_VOID;
142
			break;
143
		case type_bottom_tag:
144
			res = CTYPE_VOID;
145
			break;
146
		case type_ptr_tag:
147
			res = CTYPE_PTR;
148
			break;
149
		case type_ptr_mem_tag:
150
			res = CTYPE_PTR_MEM;
151
			break;
152
		case type_bitfield_tag:
153
			res = CTYPE_BITF;
154
			break;
155
		case type_compound_tag:
156
			res = CTYPE_CLASS;
157
			break;
158
		case type_enumerate_tag:
159
			res = CTYPE_ENUM;
160
			break;
161
		case type_error_tag:
162
			res = CTYPE_ERROR;
163
			break;
164
		case type_func_tag:
165
		case type_array_tag: {
166
			/* Allow for lvalue conversions */
167
			if (qual & cv_lvalue) {
168
				res = CTYPE_PTR;
169
			}
170
			break;
171
		}
2 7u83 172
 
7 7u83 173
		case type_ref_tag: {
174
			/* Deal with reference types */
175
			TYPE r = DEREF_type(type_ref_sub(t));
176
			TYPE s = r;
177
			res = type_category(&r);
178
			if (!EQ_type(r, s)) {
179
				MAKE_type_ref(qual, r, r);
180
				*pt = r;
181
			}
182
			break;
2 7u83 183
		}
184
 
7 7u83 185
		case type_token_tag: {
186
			/* Deal with tokenised types */
187
			IDENTIFIER id = DEREF_id(type_token_tok(t));
188
			DECL_SPEC ds = DEREF_dspec(id_storage(id));
189
			if (ds & dspec_temp) {
190
				/* Check for recursive tokens */
191
				report(crt_loc, ERR_token_recursive(id));
192
			} else {
193
				/* Expand token definition */
194
				TYPE r = expand_type(t, 0);
195
				if (!EQ_type(r, t)) {
196
					COPY_dspec(id_storage(id),
197
						   (ds | dspec_temp));
198
					res = type_category(&r);
199
					COPY_dspec(id_storage(id), ds);
200
					*pt = r;
201
				} else {
202
					if (is_templ_param(id) &&
203
					    in_template_decl) {
204
						res |= CTYPE_TEMPL;
205
					}
206
				}
2 7u83 207
			}
7 7u83 208
			res |= CTYPE_TOKEN;
209
			break;
2 7u83 210
		}
7 7u83 211
		}
212
		if (qual & cv_lvalue) {
213
			res |= CTYPE_LVALUE;
214
		}
2 7u83 215
	}
7 7u83 216
	return (res);
2 7u83 217
}
218
 
219
 
220
/*
221
    ARE TWO INTEGRAL TYPES EQUAL?
222
 
223
    This routine checks whether the integral types s and t are equal.
224
*/
225
 
7 7u83 226
int
227
eq_itype(INT_TYPE s, INT_TYPE t)
2 7u83 228
{
7 7u83 229
	int eq = 0;
230
	unsigned ns, nt;
231
	if (EQ_itype(s, t)) {
232
		return (1);
233
	}
234
	if (IS_NULL_itype(s)) {
235
		return (0);
236
	}
237
	if (IS_NULL_itype(t)) {
238
		return (0);
239
	}
240
	s = expand_itype(s);
241
	t = expand_itype(t);
242
	if (EQ_itype(s, t)) {
243
		return (1);
244
	}
245
	ns = TAG_itype(s);
246
	nt = TAG_itype(t);
247
	if (ns == nt) {
248
		ASSERT(ORDER_itype == 6);
249
		switch (ns) {
250
		case itype_basic_tag: {
251
			/* Built-in types */
252
			BUILTIN_TYPE bs = DEREF_ntype(itype_basic_no(s));
253
			BUILTIN_TYPE bt = DEREF_ntype(itype_basic_no(t));
254
			if (bs == bt) {
255
				eq = 1;
256
			}
257
			break;
2 7u83 258
		}
7 7u83 259
		case itype_bitfield_tag: {
260
			/* Bitfield types */
261
			BASE_TYPE bs = DEREF_btype(itype_bitfield_rep(s));
262
			BASE_TYPE bt = DEREF_btype(itype_bitfield_rep(t));
263
			if (bs == bt) {
264
				NAT ms = DEREF_nat(itype_bitfield_size(s));
265
				NAT mt = DEREF_nat(itype_bitfield_size(t));
266
				if (EQ_nat(ms, mt) || eq_nat(ms, mt)) {
267
					TYPE ps =
268
					    DEREF_type(itype_bitfield_sub(s));
269
					TYPE pt =
270
					    DEREF_type(itype_bitfield_sub(t));
271
					eq = eq_type(ps, pt);
272
				}
273
			}
274
			break;
2 7u83 275
		}
7 7u83 276
		case itype_promote_tag: {
277
			/* Promotion types */
278
			s = DEREF_itype(itype_promote_arg(s));
279
			t = DEREF_itype(itype_promote_arg(t));
280
			eq = eq_itype(s, t);
281
			break;
2 7u83 282
		}
7 7u83 283
		case itype_arith_tag: {
284
			/* Arithmetic types */
285
			INT_TYPE s1 = DEREF_itype(itype_arith_arg1(s));
286
			INT_TYPE s2 = DEREF_itype(itype_arith_arg2(s));
287
			INT_TYPE t1 = DEREF_itype(itype_arith_arg1(t));
288
			INT_TYPE t2 = DEREF_itype(itype_arith_arg2(t));
289
			if (eq_itype(s1, t1)) {
290
				eq = eq_itype(s2, t2);
291
			} else if (eq_itype(s1, t2)) {
292
				eq = eq_itype(s2, t1);
293
			}
294
			break;
295
		}
296
		case itype_literal_tag: {
297
			/* Literal types */
298
			int bs = DEREF_int(itype_literal_spec(s));
299
			int bt = DEREF_int(itype_literal_spec(t));
300
			IDENTIFIER is = DEREF_id(itype_literal_tok(s));
301
			IDENTIFIER it = DEREF_id(itype_literal_tok(t));
302
			NAT ms = DEREF_nat(itype_literal_nat(s));
303
			NAT mt = DEREF_nat(itype_literal_nat(t));
304
			if (bs == bt && EQ_id(is, it) && eq_nat(ms, mt)) {
305
				eq = 1;
306
			}
307
			break;
308
		}
309
		case itype_token_tag: {
310
			/* Token applications */
311
			IDENTIFIER is = DEREF_id(itype_token_tok(s));
312
			IDENTIFIER it = DEREF_id(itype_token_tok(t));
313
			LIST(TOKEN)ps = DEREF_list(itype_token_args(s));
314
			LIST(TOKEN)pt = DEREF_list(itype_token_args(t));
315
			eq = eq_token_args(is, it, ps, pt);
316
			break;
317
		}
318
		}
2 7u83 319
	}
7 7u83 320
	return (eq);
2 7u83 321
}
322
 
323
 
324
/*
325
    ARE TWO FLOATING POINT TYPES EQUAL?
326
 
327
    This routine checks whether the floating point types s and t are equal.
328
*/
329
 
7 7u83 330
int
331
eq_ftype(FLOAT_TYPE s, FLOAT_TYPE t)
2 7u83 332
{
7 7u83 333
	int eq = 0;
334
	unsigned ns, nt;
335
	if (EQ_ftype(s, t)) {
336
		return (1);
337
	}
338
	if (IS_NULL_ftype(s)) {
339
		return (0);
340
	}
341
	if (IS_NULL_ftype(t)) {
342
		return (0);
343
	}
344
	ns = TAG_ftype(s);
345
	nt = TAG_ftype(t);
346
	if (ns == nt) {
347
		ASSERT(ORDER_ftype == 4);
348
		switch (ns) {
349
		case ftype_basic_tag: {
350
			/* Built-in types */
351
			BUILTIN_TYPE bs = DEREF_ntype(ftype_basic_no(s));
352
			BUILTIN_TYPE bt = DEREF_ntype(ftype_basic_no(t));
353
			if (bs == bt) {
354
				eq = 1;
355
			}
356
			break;
2 7u83 357
		}
7 7u83 358
		case ftype_arg_promote_tag: {
359
			/* Argument promotion types */
360
			FLOAT_TYPE s1 = DEREF_ftype(ftype_arg_promote_arg(s));
361
			FLOAT_TYPE t1 = DEREF_ftype(ftype_arg_promote_arg(t));
362
			eq = eq_ftype(s1, t1);
363
			break;
364
		}
365
		case ftype_arith_tag: {
366
			/* Arithmetic types */
367
			FLOAT_TYPE s1 = DEREF_ftype(ftype_arith_arg1(s));
368
			FLOAT_TYPE s2 = DEREF_ftype(ftype_arith_arg2(s));
369
			FLOAT_TYPE t1 = DEREF_ftype(ftype_arith_arg1(t));
370
			FLOAT_TYPE t2 = DEREF_ftype(ftype_arith_arg2(t));
371
			if (eq_ftype(s1, t1)) {
372
				eq = eq_ftype(s2, t2);
373
			} else if (eq_ftype(s1, t2)) {
374
				eq = eq_ftype(s2, t1);
375
			}
376
			break;
377
		}
378
		case ftype_token_tag: {
379
			/* Token applications */
380
			IDENTIFIER is = DEREF_id(ftype_token_tok(s));
381
			IDENTIFIER it = DEREF_id(ftype_token_tok(t));
382
			LIST(TOKEN)ps = DEREF_list(ftype_token_args(s));
383
			LIST(TOKEN)pt = DEREF_list(ftype_token_args(t));
384
			eq = eq_token_args(is, it, ps, pt);
385
			break;
386
		}
387
		}
2 7u83 388
	}
7 7u83 389
	return (eq);
2 7u83 390
}
391
 
392
 
393
/*
394
    FIND THE CV-QUALIFIER FOR A TYPE
395
 
396
    This routine finds the cv-qualifier for the type t.  In most cases this
397
    is trivial, but for arrays the qualifier is that of the subtype.
398
*/
399
 
7 7u83 400
CV_SPEC
401
find_cv_qual(TYPE t)
2 7u83 402
{
7 7u83 403
	CV_SPEC qt = DEREF_cv(type_qual(t));
404
	while (IS_type_array(t)) {
405
		CV_SPEC qs;
406
		t = DEREF_type(type_array_sub(t));
407
		qs = DEREF_cv(type_qual(t));
408
		qt |= qs;
409
	}
410
	return (qt);
2 7u83 411
}
412
 
413
 
414
/*
415
    IS ONE TYPE MORE CV-QUALIFIED THAN ANOTHER?
416
 
417
    This routine returns cv_none if the type s is more cv-qualified than the
418
    type t.  That is to say, if t is const then so is s, and if t is volatile
419
    then so is s.  Otherwise it returns those cv-qualifiers for which s
420
    fails to be more qualified than t.
421
*/
422
 
7 7u83 423
CV_SPEC
424
cv_compare(TYPE s, TYPE t)
2 7u83 425
{
7 7u83 426
	CV_SPEC qs = find_cv_qual(s);
427
	CV_SPEC qt = find_cv_qual(t);
428
	qs &= cv_qual;
429
	qt &= cv_qual;
430
	return (qt & ~qs);
2 7u83 431
}
432
 
433
 
434
/*
435
    ARE TWO FUNCTION LINKAGE SPECIFIERS THE SAME?
436
 
437
    This routine compares the function linkage specifiers for the function
438
    types s and t.
439
*/
440
 
7 7u83 441
static int
442
eq_func_lang(TYPE s, TYPE t)
2 7u83 443
{
7 7u83 444
	CV_SPEC qs = DEREF_cv(type_func_mqual(s));
445
	CV_SPEC qt = DEREF_cv(type_func_mqual(t));
446
	if (qs != qt) {
447
		CV_SPEC ps = (qs & cv_language);
448
		CV_SPEC pt = (qt & cv_language);
449
		if (ps != pt) {
450
			if (ps == cv_none) {
451
				if (force_tokdef) {
452
					ps = pt;
453
					COPY_cv(type_func_mqual(s), (qs | ps));
454
				} else {
455
					ps = cv_lang;
456
				}
457
			}
458
			if (pt == cv_none) {
459
				if (force_tokdef) {
460
					pt = ps;
461
					COPY_cv(type_func_mqual(t), (qt | pt));
462
				} else {
463
					pt = cv_lang;
464
				}
465
			}
466
			if (ps != pt) {
467
				return (0);
468
			}
2 7u83 469
		}
470
	}
7 7u83 471
	return (1);
2 7u83 472
}
473
 
474
 
475
/*
476
    ARE TWO FUNCTION TYPES EQUAL?
477
 
478
    This routine checks whether the function types s and t are equal.
479
    Member function qualifiers are only considered if mq is true.
480
    If rf is true then any parameter of type 'X' is considered to match
481
    one of type 'X &'.  The routine returns 3 if the types are precisely
482
    equal, 2 if they differ only in the linkage specifier, 1 if they
483
    differ only in the return type or in one of these reference
484
    equalities, and 0 otherwise.
485
*/
486
 
7 7u83 487
int
488
eq_func_type(TYPE s, TYPE t, int mq, int rf)
2 7u83 489
{
7 7u83 490
	int eq = 3;
491
	int es, et;
492
	unsigned ns, nt;
493
	LIST(TYPE)ls, lt;
2 7u83 494
 
7 7u83 495
	/* Check for obvious equality */
496
	if (EQ_type(s, t)) {
497
		return (3);
2 7u83 498
	}
7 7u83 499
	ns = TAG_type(s);
500
	nt = TAG_type(t);
501
	if (ns != type_func_tag || nt != type_func_tag) {
502
		if (ns == type_templ_tag && nt == type_templ_tag) {
503
			/* Allow for template functions */
504
			eq = eq_template(s, t, 1, mq, rf);
505
			return (eq);
506
		} else {
507
			/* Otherwise just check type equality */
508
			eq = eq_type(s, t);
509
			if (eq == 1) {
510
				return (3);
511
			}
512
			return (0);
513
		}
514
	}
2 7u83 515
 
7 7u83 516
	/* Check number of parameters */
517
	es = DEREF_int(type_func_ellipsis(s));
518
	et = DEREF_int(type_func_ellipsis(t));
519
	ls = DEREF_list(type_func_ptypes(s));
520
	lt = DEREF_list(type_func_ptypes(t));
521
	if (es != et || LENGTH_list(ls)!= LENGTH_list(lt)) {
522
		return (0);
523
	}
2 7u83 524
 
7 7u83 525
	/* Check parameter types */
526
	while (!IS_NULL_list(ls)) {
527
		/* Check next parameter */
528
		TYPE as = DEREF_type(HEAD_list(ls));
529
		TYPE at = DEREF_type(HEAD_list(lt));
530
		if (es & FUNC_PARAMS) {
531
			/* Compare unpromoted types */
532
			as = unpromote_type(as);
533
			at = unpromote_type(at);
2 7u83 534
		}
7 7u83 535
		if (rf) {
536
			/* Allow for references */
537
			if (IS_type_ref(as)) {
538
				if (!IS_type_ref(at)) {
539
					as = DEREF_type(type_ref_sub(as));
540
					eq = 1;
541
				}
542
			} else if (IS_type_ref(at)) {
543
				at = DEREF_type(type_ref_sub(at));
544
				eq = 1;
545
			}
546
		}
547
		if (eq_type(as, at) != 1) {
548
			return (0);
549
		}
550
		if (force_tokdef) {
551
			/* Preserve printf and scanf types */
552
			if (is_printf_type(as)) {
553
				IDENTIFIER id = DEREF_id(type_name(as));
554
				COPY_id(type_name(at), id);
555
			} else if (is_printf_type(at)) {
556
				IDENTIFIER id = DEREF_id(type_name(at));
557
				COPY_id(type_name(as), id);
558
			}
559
		}
560
		ls = TAIL_list(ls);
561
		lt = TAIL_list(lt);
2 7u83 562
	}
563
 
7 7u83 564
	/* Check return type */
565
	if (eq == 3) {
566
		TYPE rs = DEREF_type(type_func_ret(s));
567
		TYPE rt = DEREF_type(type_func_ret(t));
568
		if (eq_type(rt, rs)!= 1) {
569
			if (IS_type_top_etc(rs)) {
570
				/* Check for 'void' and 'bottom' */
571
				TYPE r = type_composite(rs, rt, 0, 1, KILL_err,
572
							0);
573
				if (IS_NULL_type(r)) {
574
					eq = 1;
575
				}
576
			} else {
577
				eq = 1;
578
			}
579
		}
2 7u83 580
	}
581
 
7 7u83 582
	/* Check member qualifiers */
583
	if (eq) {
584
		CV_SPEC qs = DEREF_cv(type_func_mqual(s));
585
		CV_SPEC qt = DEREF_cv(type_func_mqual(t));
586
		if (qs != qt) {
587
			if (mq && (qs & cv_qual)!= (qt & cv_qual)) {
588
				eq = 0;
589
			} else if (!eq_func_lang(s, t)) {
590
				/* Linkage specifiers don't match */
591
				if (eq == 3 && option(OPT_func_linkage)) {
592
					eq = 2;
593
				}
594
			}
595
		}
2 7u83 596
	}
7 7u83 597
	return (eq);
2 7u83 598
}
599
 
600
 
601
/*
602
    CHECK EQUALITY OF NESTED TEMPLATE CLASS
603
 
604
    This routine checks whether the instance s of a nested class or
605
    enumeration type of a template class equals the type tid.
606
*/
607
 
7 7u83 608
static int
609
eq_instance(TYPE s, IDENTIFIER tid)
2 7u83 610
{
7 7u83 611
	if (IS_type_instance(s)) {
612
		IDENTIFIER sid = DEREF_id(type_instance_id(s));
613
		if (EQ_id(sid, tid)) {
614
			CLASS_TYPE cs, ct;
615
			sid = DEREF_id(type_name(s));
616
			if (EQ_id(sid, tid)) {
617
				return (1);
618
			}
619
			cs = parent_class(sid);
620
			ct = parent_class(tid);
621
			if (IS_NULL_ctype(cs)) {
622
				return (0);
623
			}
624
			if (IS_NULL_ctype(ct)) {
625
				return (0);
626
			}
627
			return (eq_ctype(cs, ct));
628
		}
2 7u83 629
	}
7 7u83 630
	return (0);
2 7u83 631
}
632
 
633
 
634
/*
635
    CHECK EQUALITY OF CLASS TYPES
636
 
637
    This routine checks for equality of the class types cs and ct.
638
*/
639
 
7 7u83 640
int
641
eq_ctype(CLASS_TYPE cs, CLASS_TYPE ct)
2 7u83 642
{
7 7u83 643
	if (EQ_ctype(cs, ct)) {
644
		/* Simple class equality */
645
		return (1);
646
	}
647
	if (!IS_NULL_ctype(cs) && !IS_NULL_ctype(ct)) {
648
		TYPE s = DEREF_type(ctype_form(cs));
649
		TYPE t = DEREF_type(ctype_form(ct));
650
		if (!IS_NULL_type(s) || !IS_NULL_type(t)) {
651
			/* Allow for template classes */
652
			unsigned ns, nt;
653
			if (IS_NULL_type(s)) {
654
				s = make_class_type(cs);
655
			}
656
			if (IS_NULL_type(t)) {
657
				t = make_class_type(ct);
658
			}
659
			ns = TAG_type(s);
660
			nt = TAG_type(t);
661
			if (ns != nt) {
662
				if (ns == type_instance_tag) {
663
					IDENTIFIER tid =
664
					    DEREF_id(ctype_name(ct));
665
					if (eq_instance(s, tid)) {
666
						return (1);
667
					}
668
				}
669
				if (nt == type_instance_tag) {
670
					IDENTIFIER sid =
671
					    DEREF_id(ctype_name(cs));
672
					if (eq_instance(t, sid)) {
673
						return (1);
674
					}
675
				}
676
			}
677
			return (eq_type(s, t));
2 7u83 678
		}
7 7u83 679
		if (force_merge) {
680
			/* Allow for merging of type names */
681
			IDENTIFIER sid = DEREF_id(ctype_name(cs));
682
			IDENTIFIER tid = DEREF_id(ctype_name(ct));
683
			return (merge_type(sid, tid));
2 7u83 684
		}
685
	}
7 7u83 686
	return (0);
2 7u83 687
}
688
 
689
 
690
/*
691
    CHECK EQUALITY OF ENUMERATION TYPES
692
 
693
    This routine checks for equality of the enumeration types es and et.
694
*/
695
 
7 7u83 696
int
697
eq_etype(ENUM_TYPE es, ENUM_TYPE et)
2 7u83 698
{
7 7u83 699
	if (EQ_etype(es, et)) {
700
		/* Simple equality */
701
		return (1);
2 7u83 702
	}
7 7u83 703
	if (!IS_NULL_etype(es) && !IS_NULL_etype(et)) {
704
		TYPE s = DEREF_type(etype_form(es));
705
		TYPE t = DEREF_type(etype_form(et));
706
		if (!IS_NULL_type(s) && !IS_NULL_type(t)) {
707
			return (eq_type(s, t));
708
		}
709
		if (!IS_NULL_type(s)) {
710
			IDENTIFIER tid = DEREF_id(etype_name(et));
711
			return (eq_instance(s, tid));
712
		}
713
		if (!IS_NULL_type(t)) {
714
			IDENTIFIER sid = DEREF_id(etype_name(es));
715
			return (eq_instance(t, sid));
716
		}
717
		if (force_merge) {
718
			/* Allow for merging of type names */
719
			IDENTIFIER sid = DEREF_id(etype_name(es));
720
			IDENTIFIER tid = DEREF_id(etype_name(et));
721
			return (merge_type(sid, tid));
722
		}
2 7u83 723
	}
7 7u83 724
	return (0);
2 7u83 725
}
726
 
727
 
728
/*
729
    CHECK TYPE EQUALITY
730
 
731
    This is an auxiliary routine used by eq_type_qual which checks the
732
    types s and t for equality ignoring qualifiers according to the
733
    value of qu.  If either s or t is a template type and force_template
734
    is true then 1 is returned if the types are precisely equal, 2 is
735
    returned if t is a specialisation of s, 3 if s is a specialisation
736
    of t, 4 if each is a specialisation of the other (but they are not
737
    equal) and 0 otherwise.
738
*/
739
 
7 7u83 740
static int
741
eq_type_aux(TYPE s, TYPE t, int qu)
2 7u83 742
{
7 7u83 743
	/* Check for obvious equality */
744
	unsigned ns, nt;
745
	if (EQ_type(s, t)) {
746
		return (1);
2 7u83 747
	}
7 7u83 748
	if (IS_NULL_type(s)) {
749
		return (0);
2 7u83 750
	}
7 7u83 751
	if (IS_NULL_type(t)) {
752
		return (0);
753
	}
2 7u83 754
 
7 7u83 755
	/* Tags should be equal */
756
	ns = TAG_type(s);
757
	nt = TAG_type(t);
758
	if (ns != nt) {
759
		if (ns == type_templ_tag && force_template) {
760
			/* Allow for template types */
761
			if (deduce_template(s, t, qu)) {
762
				return (2);
763
			}
764
		}
765
		if (nt == type_templ_tag && force_template) {
766
			/* Allow for template types */
767
			if (deduce_template(t, s, qu)) {
768
				return (3);
769
			}
770
		}
771
		return (0);
2 7u83 772
	}
773
 
7 7u83 774
	/* Qualifiers should be equal */
775
	if (qu == 0) {
776
		CV_SPEC qs = DEREF_cv(type_qual(s));
777
		CV_SPEC qt = DEREF_cv(type_qual(t));
778
		if (qs != qt) {
779
			/* Try again allowing for lvalues */
780
			qs &= cv_qual;
781
			qt &= cv_qual;
782
			if (qs != qt) {
783
				return (0);
784
			}
785
		}
786
	}
2 7u83 787
 
7 7u83 788
	/* Check on type components */
789
	ASSERT(ORDER_type == 18);
790
	switch (ns) {
791
 
792
	case type_integer_tag: {
793
		/* Check integer types */
794
		INT_TYPE is = DEREF_itype(type_integer_rep(s));
795
		INT_TYPE it = DEREF_itype(type_integer_rep(t));
796
		if (EQ_itype(is, it)) {
797
			return (1);
798
		}
799
		return (eq_itype(is, it));
2 7u83 800
	}
801
 
7 7u83 802
	case type_floating_tag: {
803
		/* Check floating types */
804
		FLOAT_TYPE fs = DEREF_ftype(type_floating_rep(s));
805
		FLOAT_TYPE ft = DEREF_ftype(type_floating_rep(t));
806
		if (EQ_ftype(fs, ft)) {
807
			return (1);
808
		}
809
		return (eq_ftype(fs, ft));
2 7u83 810
	}
811
 
7 7u83 812
	case type_ptr_tag: {
813
		/* Check pointer sub-types */
814
		s = DEREF_type(type_ptr_sub(s));
815
		t = DEREF_type(type_ptr_sub(t));
816
		if (qu == 1) {
817
			qu = 0;
818
		}
819
		return (eq_type_qual(s, t, qu));
2 7u83 820
	}
821
 
7 7u83 822
	case type_ref_tag: {
823
		/* Check reference sub-types */
824
		s = DEREF_type(type_ref_sub(s));
825
		t = DEREF_type(type_ref_sub(t));
826
		return (eq_type_qual(s, t, qu));
2 7u83 827
	}
828
 
829
#if LANGUAGE_CPP
7 7u83 830
	case type_ptr_mem_tag: {
831
		/* Check pointer to member class types */
832
		CLASS_TYPE cs = DEREF_ctype(type_ptr_mem_of(s));
833
		CLASS_TYPE ct = DEREF_ctype(type_ptr_mem_of(t));
834
		if (!eq_ctype(cs, ct)) {
835
			return (0);
836
		}
2 7u83 837
 
7 7u83 838
		/* Check pointer to member sub-types */
839
		s = DEREF_type(type_ptr_mem_sub(s));
840
		t = DEREF_type(type_ptr_mem_sub(t));
841
		if (qu == 1) {
842
			qu = 0;
843
		}
844
		return (eq_type_qual(s, t, qu));
2 7u83 845
	}
846
#endif
847
 
7 7u83 848
	case type_func_tag: {
849
		/* Check function types */
850
		int ret = eq_func_type(s, t, 1, 0);
851
		if (ret == 3) {
852
			return (1);
853
		}
854
		return (0);
2 7u83 855
	}
856
 
7 7u83 857
	case type_array_tag: {
858
		/* Check array bounds */
859
		NAT ms = DEREF_nat(type_array_size(s));
860
		NAT mt = DEREF_nat(type_array_size(t));
861
		if (!EQ_nat(ms, mt) && !eq_nat(ms, mt)) {
862
			return (0);
863
		}
2 7u83 864
 
7 7u83 865
		/* Check array sub-types */
866
		s = DEREF_type(type_array_sub(s));
867
		t = DEREF_type(type_array_sub(t));
868
		return (eq_type_qual(s, t, qu));
2 7u83 869
	}
870
 
7 7u83 871
	case type_bitfield_tag: {
872
		/* Check bitfield types */
873
		INT_TYPE bs = DEREF_itype(type_bitfield_defn(s));
874
		INT_TYPE bt = DEREF_itype(type_bitfield_defn(t));
875
		return (eq_itype(bs, bt));
2 7u83 876
	}
877
 
7 7u83 878
	case type_compound_tag: {
879
		/* Check class definitions */
880
		CLASS_TYPE cs = DEREF_ctype(type_compound_defn(s));
881
		CLASS_TYPE ct = DEREF_ctype(type_compound_defn(t));
882
		return (eq_ctype(cs, ct));
2 7u83 883
	}
884
 
7 7u83 885
	case type_enumerate_tag: {
886
		/* Check enumeration definitions */
887
		ENUM_TYPE es = DEREF_etype(type_enumerate_defn(s));
888
		ENUM_TYPE et = DEREF_etype(type_enumerate_defn(t));
889
		return (eq_etype(es, et));
2 7u83 890
	}
891
 
7 7u83 892
	case type_token_tag: {
893
		/* Check token applications */
894
		IDENTIFIER is, it;
895
		LIST(TOKEN)ps, pt;
896
		INSTANCE as = DEREF_inst(type_token_app(s));
897
		INSTANCE at = DEREF_inst(type_token_app(t));
898
		if (!IS_NULL_inst(as) && !IS_NULL_inst(at)) {
899
			/* Check for equality of template instances */
900
			if (EQ_inst(as, at)) {
901
				return (1);
902
			}
903
			as = DEREF_inst(inst_alias(as));
904
			at = DEREF_inst(inst_alias(at));
905
			if (EQ_inst(as, at)) {
906
				return (1);
907
			}
908
		}
909
		is = DEREF_id(type_token_tok(s));
910
		it = DEREF_id(type_token_tok(t));
911
		ps = DEREF_list(type_token_args(s));
912
		pt = DEREF_list(type_token_args(t));
913
		return (eq_token_args(is, it, ps, pt));
2 7u83 914
	}
915
 
7 7u83 916
	case type_templ_tag: {
917
		int ret = eq_template(s, t, 1, 1, 0);
918
		if (ret == 3) {
919
			/* Precise template equality */
920
			return (1);
921
		}
922
		if (force_template) {
923
			/* Check for template specialisations */
924
			int ds, dt;
925
			TYPE ps = DEREF_type(type_templ_defn(s));
926
			TYPE pt = DEREF_type(type_templ_defn(t));
927
			if (qu == 1) {
928
				qu = 0;
929
			}
930
			ds = deduce_template(s, pt, qu);
931
			dt = deduce_template(t, ps, qu);
932
			if (ds) {
933
				return (dt ? 4 : 2);
934
			}
935
			if (dt) {
936
				return (3);
937
			}
938
		}
939
		return (0);
2 7u83 940
	}
941
 
7 7u83 942
	case type_pre_tag: {
943
		/* Check pre-types */
944
		BASE_TYPE bs = DEREF_btype(type_pre_rep(s));
945
		BASE_TYPE bt = DEREF_btype(type_pre_rep(t));
946
		IDENTIFIER is = DEREF_id(type_name(s));
947
		IDENTIFIER it = DEREF_id(type_name(t));
948
		if (!IS_NULL_id(is)) {
949
			is = DEREF_id(id_alias(is));
950
		}
951
		if (!IS_NULL_id(it)) {
952
			it = DEREF_id(id_alias(it));
953
		}
954
		return (bs == bt && EQ_id(is, it));
2 7u83 955
	}
956
 
7 7u83 957
	case type_instance_tag: {
958
		/* Check instance types */
959
		IDENTIFIER is = DEREF_id(type_instance_id(s));
960
		IDENTIFIER it = DEREF_id(type_instance_id(t));
961
		if (EQ_id(is, it)) {
962
			/* Derived from same member */
963
			CLASS_TYPE cs, ct;
964
			is = DEREF_id(type_name(s));
965
			it = DEREF_id(type_name(t));
966
			if (EQ_id(is, it)) {
967
				return (1);
968
			}
969
			cs = parent_class(is);
970
			ct = parent_class(it);
971
			if (IS_NULL_ctype(cs)) {
972
				return (0);
973
			}
974
			if (IS_NULL_ctype(ct)) {
975
				return (0);
976
			}
977
			return (eq_ctype(cs, ct));
978
		}
979
		return (0);
2 7u83 980
	}
981
 
7 7u83 982
	case type_dummy_tag: {
983
		/* Check dummy types */
984
		int is = DEREF_int(type_dummy_tok(s));
985
		int it = DEREF_int(type_dummy_tok(t));
986
		return (is == it);
2 7u83 987
	}
7 7u83 988
	}
989
	/* Simple types compare equal */
990
	return (1);
2 7u83 991
}
992
 
993
 
994
/*
995
    UNIFY TWO TYPES
996
 
997
    This routine unifies the types s and t by defining tokens if necessary.
998
    cv gives the type qualifiers which are in t but not in s.  It returns
999
    true if a value is assigned to a token.
1000
*/
1001
 
7 7u83 1002
int
1003
unify_type(TYPE s, TYPE t, CV_SPEC cv, int qual)
2 7u83 1004
{
7 7u83 1005
	IDENTIFIER id;
1006
	LIST(TOKEN)args;
1007
	unsigned tag = TAG_type(s);
1008
	switch (tag) {
1009
	case type_integer_tag: {
1010
		/* Integral types */
1011
		INT_TYPE is = DEREF_itype(type_integer_rep(s));
1012
		switch (TAG_itype(is)) {
1013
		case itype_basic_tag: {
1014
			/* Built-in integral types */
1015
			BUILTIN_TYPE n = DEREF_ntype(itype_basic_no(is));
1016
			id = get_special(base_token[n].tok, 0);
1017
			if (IS_NULL_id(id)) {
1018
				return (0);
1019
			}
1020
			args = NULL_list(TOKEN);
1021
			break;
2 7u83 1022
		}
7 7u83 1023
		case itype_token_tag: {
1024
			/* Tokenised integral types */
1025
			id = DEREF_id(itype_token_tok(is));
1026
			args = DEREF_list(itype_token_args(is));
1027
			break;
2 7u83 1028
		}
7 7u83 1029
		default:
1030
			/* Other integral types */
1031
			return (0);
2 7u83 1032
		}
7 7u83 1033
		break;
2 7u83 1034
	}
7 7u83 1035
	case type_floating_tag: {
1036
		/* Floating types */
1037
		FLOAT_TYPE fs = DEREF_ftype(type_floating_rep(s));
1038
		if (IS_ftype_token(fs)) {
1039
			id = DEREF_id(ftype_token_tok(fs));
1040
			args = DEREF_list(ftype_token_args(fs));
1041
			break;
1042
		}
1043
		return (0);
2 7u83 1044
	}
7 7u83 1045
	case type_compound_tag: {
1046
		/* Class types */
1047
		CLASS_TYPE cs = DEREF_ctype(type_compound_defn(s));
1048
		CLASS_INFO ci = DEREF_cinfo(ctype_info(cs));
1049
		if (ci & cinfo_token) {
1050
			TYPE r = DEREF_type(ctype_form(cs));
1051
			if (!IS_NULL_type(r) && IS_type_token(r)) {
1052
				id = DEREF_id(type_token_tok(r));
1053
				args = DEREF_list(type_token_args(r));
1054
				break;
1055
			}
2 7u83 1056
		}
7 7u83 1057
		return (0);
2 7u83 1058
	}
7 7u83 1059
	case type_token_tag: {
1060
		/* Tokenised types */
1061
		id = DEREF_id(type_token_tok(s));
1062
		args = DEREF_list(type_token_args(s));
1063
		break;
2 7u83 1064
	}
7 7u83 1065
	default:
1066
		/* Other types */
1067
		return (0);
2 7u83 1068
	}
7 7u83 1069
	if (defining_token(id)) {
1070
		TOKEN sort;
1071
		if (IS_NULL_list(args)) {
1072
			t = qualify_type(t, cv, 0);
1073
			return (define_type_token(id, t, qual));
1074
		}
1075
		sort = DEREF_tok(id_token_sort(id));
1076
		if (IS_tok_class(sort) && IS_type_compound(t)) {
1077
			/* Check for template template parameters */
1078
			CLASS_TYPE ct = DEREF_ctype(type_compound_defn(t));
1079
			TYPE r = DEREF_type(ctype_form(ct));
1080
			if (!IS_NULL_type(r) && IS_type_token(r)) {
1081
				IDENTIFIER tid = DEREF_id(type_token_tok(r));
1082
				if (IS_id_class_name(tid)) {
1083
					LIST(TOKEN)targs;
1084
					targs = DEREF_list(type_token_args(r));
1085
					if (eq_token_args(tid, tid, args, targs)) {
1086
						return (define_templ_token(id, tid));
1087
					}
1088
				}
1089
			}
1090
		}
2 7u83 1091
	}
7 7u83 1092
	if (expand_tokdef) {
1093
		/* Expand token definitions */
1094
		TOKEN sort = find_tokdef(id);
1095
		if (!IS_NULL_tok(sort) && IS_tok_type(sort)) {
1096
			TYPE r = DEREF_type(tok_type_value(sort));
1097
			if (!IS_NULL_type(r) && eq_type(r, t)) {
1098
				return (1);
1099
			}
2 7u83 1100
		}
1101
	}
7 7u83 1102
	return (0);
2 7u83 1103
}
1104
 
1105
 
1106
/*
1107
    CAN TWO TYPES BE UNIFIED?
1108
 
1109
    This routine checks whether the type s can be unified with t or vice
1110
    versa using token definitions.
1111
*/
1112
 
7 7u83 1113
static int
1114
unify_types(TYPE s, TYPE t, int qu)
2 7u83 1115
{
7 7u83 1116
	if (force_tokdef || force_template || expand_tokdef) {
1117
		CV_SPEC qs, qt;
1118
		CV_SPEC rs, rt;
1119
		if (IS_NULL_type(s)) {
1120
			return (0);
1121
		}
1122
		if (IS_NULL_type(t)) {
1123
			return (0);
1124
		}
1125
		qs = DEREF_cv(type_qual(s));
1126
		qt = DEREF_cv(type_qual(t));
1127
		qs &= cv_qual;
1128
		qt &= cv_qual;
1129
		rs = (qs & ~qt);
1130
		rt = (qt & ~qs);
1131
		if (rs == cv_none || qu) {
1132
			if (unify_type(s, t, rt, 0)) {
1133
				return (1);
1134
			}
1135
		}
1136
		if (rt == cv_none || qu) {
1137
			if (unify_type(t, s, rs, 0)) {
1138
				return (1);
1139
			}
1140
		}
2 7u83 1141
	}
7 7u83 1142
	return (0);
2 7u83 1143
}
1144
 
1145
 
1146
/*
1147
    ARE TWO TYPES EQUAL?
1148
 
1149
    This routine checks whether the types s and t are equal (excluding
1150
    lvalue qualifiers).  If qu is 1 then the top level qualifiers
1151
    are completely ignored, if it is 2 all qualifiers are ignored.
1152
    The return values are as in eq_type_aux.  The routine is usually
1153
    accessed through the macros eq_type and eq_type_unqual.
1154
*/
1155
 
7 7u83 1156
int
1157
eq_type_qual(TYPE s, TYPE t, int qu)
2 7u83 1158
{
7 7u83 1159
	int eq;
1160
	if (EQ_type(s, t)) {
1161
		return (1);
1162
	}
1163
	eq = eq_type_aux(s, t, qu);
1164
	if (eq == 0) {
1165
		eq = unify_types(s, t, qu);
1166
	}
1167
	return (eq);
2 7u83 1168
}
1169
 
1170
 
1171
/*
1172
    IS THE OFFSET OF TWO TYPES EQUAL?
1173
 
1174
    This routine checks whether the types s and t are offset equivalent.
1175
    For example, 'int' is offset equivalent to 'unsigned int' because it
1176
    has the same size and alignment requirements.  Note that the value 6
1177
    is used in the builtin_casts table to indicate integral types which
1178
    are equivalent in this way.
1179
*/
1180
 
7 7u83 1181
int
1182
eq_type_offset(TYPE s, TYPE t)
2 7u83 1183
{
7 7u83 1184
	unsigned ns = TAG_type(s);
1185
	unsigned nt = TAG_type(t);
1186
	if (ns != nt) {
1187
		return (0);
2 7u83 1188
	}
7 7u83 1189
	if (ns == type_integer_tag) {
1190
		INT_TYPE is = DEREF_itype(type_integer_rep(s));
1191
		INT_TYPE it = DEREF_itype(type_integer_rep(t));
1192
		if (IS_itype_basic(is) && IS_itype_basic(it)) {
1193
			BUILTIN_TYPE bs = DEREF_ntype(itype_basic_no(is));
1194
			BUILTIN_TYPE bt = DEREF_ntype(itype_basic_no(it));
1195
			if (bs == bt) {
1196
				return (1);
1197
			}
1198
			if (builtin_cast(bs, bt) == 6) {
1199
				return (1);
1200
			}
1201
			return (0);
1202
		}
1203
	}
1204
	return (eq_type_unqual(s, t));
2 7u83 1205
}
1206
 
1207
 
1208
/*
1209
    FIND THE COMPOSITE OF TWO FUNCTION TYPES
1210
 
1211
    This routine finds the composite type (in the C sense) of the function
1212
    types s and t.  eq gives the result of a previous call to eq_func_type.
1213
    If the types are compatible then a new composite function type, based
1214
    on s, is returned.  Otherwise the null type is returned.
1215
*/
1216
 
1217
#if LANGUAGE_C
1218
 
7 7u83 1219
static TYPE
1220
func_composite(TYPE s, TYPE t, int eq, ERROR *err, int mk)
2 7u83 1221
{
7 7u83 1222
	TYPE rs, rt;
1223
	CV_SPEC qs, qt;
1224
	TYPE mt = NULL_type;
1225
	int es = DEREF_int(type_func_ellipsis(s));
1226
	NAMESPACE ns = DEREF_nspace(type_func_pars(s));
1227
	LIST(TYPE)ps = DEREF_list(type_func_ptypes(s));
1228
	LIST(TYPE)pt = DEREF_list(type_func_mtypes(s));
1229
	LIST(IDENTIFIER)pids = DEREF_list(type_func_pids(s));
2 7u83 1230
 
7 7u83 1231
	/* Check parameter types */
1232
	if (!EQ_list(ps, pt)) {
1233
		mt = DEREF_type(HEAD_list(pt));
2 7u83 1234
	}
7 7u83 1235
	if (eq == 0) {
1236
		int et = DEREF_int(type_func_ellipsis(t));
1237
		if (es & FUNC_NO_PARAMS) {
1238
			/* s has no parameter information - swap types */
1239
			if (et != FUNC_NO_PARAMS) {
1240
				ps = DEREF_list(type_func_ptypes(t));
1241
				ns = DEREF_nspace(type_func_pars(t));
1242
				pids = DEREF_list(type_func_pids(t));
1243
				es = et;
1244
				et = FUNC_NO_PARAMS;
1245
			}
2 7u83 1246
		}
7 7u83 1247
		if (et & FUNC_NO_PARAMS) {
1248
			/* One type has no parameter information */
1249
			if (es & FUNC_ELLIPSIS) {
1250
				OPTION opt = option(OPT_ellipsis_extra);
1251
				add_error(err, ERR_dcl_fct_compat_ellipsis());
1252
				if (opt == OPTION_DISALLOW) {
1253
					return (NULL_type);
1254
				}
1255
			}
1256
			if (!(es & FUNC_WEAK)) {
1257
				pt = ps;
1258
				while (!IS_NULL_list(pt)) {
1259
					TYPE at = DEREF_type(HEAD_list(pt));
1260
					if (!is_arg_promote(at)) {
1261
						OPTION opt = option(OPT_func_incompat);
1262
						add_error(err, ERR_dcl_fct_compat_prom(at));
1263
						if (opt == OPTION_DISALLOW) {
1264
							return (NULL_type);
1265
						}
1266
					}
1267
					pt = TAIL_list(pt);
1268
				}
1269
			}
2 7u83 1270
 
1271
		} else {
7 7u83 1272
			/* Both types have parameter information */
1273
			int prom = 0;
1274
			int force = force_tokdef;
1275
			LIST(TYPE)pr = NULL_list(TYPE);
1276
			pt = DEREF_list(type_func_ptypes(t));
1277
			if (es & FUNC_ELLIPSIS) {
1278
				if (et & FUNC_ELLIPSIS) {
1279
					/* Both functions have ellipsis */
1280
					/* EMPTY */
1281
				} else {
1282
					/* One function has ellipsis */
1283
					OPTION opt = option(OPT_ellipsis_extra);
1284
					add_error(err, ERR_dcl_fct_compat_ellipsis());
1285
					if (opt == OPTION_DISALLOW) {
1286
						return (NULL_type);
1287
					}
1288
					et |= FUNC_ELLIPSIS;
1289
				}
1290
			} else {
1291
				if (et & FUNC_ELLIPSIS) {
1292
					/* One function has ellipsis */
1293
					OPTION opt = option(OPT_ellipsis_extra);
1294
					add_error(err, ERR_dcl_fct_compat_ellipsis());
1295
					if (opt == OPTION_DISALLOW) {
1296
						return (NULL_type);
1297
					}
1298
					es |= FUNC_ELLIPSIS;
1299
				} else {
1300
					/* Neither function has ellipsis */
1301
					if (LENGTH_list(ps)!= LENGTH_list(pt)) {
1302
						return (NULL_type);
1303
					}
1304
				}
2 7u83 1305
			}
7 7u83 1306
			if (es & FUNC_PARAMS) {
1307
				prom++;
2 7u83 1308
			}
7 7u83 1309
			if (et & FUNC_PARAMS) {
1310
				prom++;
1311
			}
1312
			while (!IS_NULL_list(ps) && !IS_NULL_list(pt)) {
1313
				TYPE ar;
1314
				TYPE as = DEREF_type(HEAD_list(ps));
1315
				TYPE at = DEREF_type(HEAD_list(pt));
1316
				if (prom == 2) {
1317
					/* Compare unpromoted types */
1318
					as = unpromote_type(as);
1319
					at = unpromote_type(at);
1320
				}
1321
				ar = type_composite(as, at, 0, 1, err, mk);
1322
				if (IS_NULL_type(ar)) {
1323
					/* Check for specified compatible types */
1324
					ar = eq_argument(as, at, 1);
1325
					if (IS_NULL_type(ar) && prom == 1) {
1326
						if (es & FUNC_PARAMS) {
1327
							as = unpromote_type(as);
1328
						}
1329
						if (et & FUNC_PARAMS) {
1330
							at = unpromote_type(at);
1331
						}
1332
						ar = eq_argument(as, at, 0);
1333
						if (!IS_NULL_type(ar)) {
1334
							OPTION opt = option(OPT_func_incompat);
1335
							ERROR err2 = ERR_dcl_fct_compat_prom(as);
1336
							add_error(err, err2);
1337
							if (opt == OPTION_DISALLOW) {
1338
								ar = NULL_type;
1339
							}
1340
						}
1341
					}
1342
					if (IS_NULL_type(ar)) {
1343
						DESTROY_list(pr, SIZE_type);
1344
						return (NULL_type);
1345
					}
1346
				}
1347
				if (mk) {
1348
					if (force) {
1349
						/* Preserve printf and scanf types */
1350
						if (is_printf_type(as)) {
1351
							IDENTIFIER id = DEREF_id(type_name(as));
1352
							COPY_id(type_name(ar), id);
1353
						} else if (is_printf_type(at)) {
1354
							IDENTIFIER id = DEREF_id(type_name(at));
1355
							COPY_id(type_name(ar), id);
1356
						}
1357
					}
1358
					if (prom == 2 && !is_arg_promote(ar)) {
1359
						/* Promote type */
1360
						ar = arg_promote_type(ar, err);
1361
					}
1362
					CONS_type(ar, pr, pr);
1363
				}
1364
				pt = TAIL_list(pt);
1365
				ps = TAIL_list(ps);
1366
			}
1367
			if (!EQ_list(ps, pt)) {
1368
				if (IS_NULL_list(ps)) {
1369
					ps = pt;
1370
				}
1371
				while (!IS_NULL_list(ps)) {
1372
					TYPE as = DEREF_type(HEAD_list(ps));
1373
					as = eq_ellipsis(as);
1374
					if (IS_NULL_type(as)) {
1375
						DESTROY_list(pr, SIZE_type);
1376
						return (NULL_type);
1377
					}
1378
					if (mk) {
1379
						CONS_type(as, pr, pr);
1380
					}
1381
					ps = TAIL_list(ps);
1382
				}
1383
			}
1384
			if (es == et) {
1385
				/* Same kinds of function */
1386
				ps = REVERSE_list(pr);
1387
			} else {
1388
				/* Different kinds of function */
1389
				int use_s = 1;
1390
				if (et & FUNC_WEAK) {
1391
					if (es & FUNC_WEAK) {
1392
						if (et & FUNC_PARAMS) {
1393
							use_s = 0;
1394
						}
1395
					}
1396
				} else {
1397
					if (es & FUNC_WEAK)use_s = 0;
1398
				}
1399
				if (use_s) {
1400
					ps = DEREF_list(type_func_ptypes(s));
1401
				} else {
1402
					ps = DEREF_list(type_func_ptypes(t));
1403
					es = et;
1404
				}
1405
				DESTROY_list(pr, SIZE_type);
1406
			}
2 7u83 1407
		}
7 7u83 1408
	}
1409
 
1410
	/* Check return type */
1411
	rs = DEREF_type(type_func_ret(s));
1412
	rt = DEREF_type(type_func_ret(t));
1413
	rs = type_composite(rs, rt, 0, 1, err, mk);
1414
	if (IS_NULL_type(rs)) {
1415
		return (NULL_type);
1416
	}
1417
 
1418
	/* Check member qualifiers */
1419
	qs = DEREF_cv(type_func_mqual(s));
1420
	qt = DEREF_cv(type_func_mqual(t));
1421
	if (qs != qt) {
1422
		qs &= cv_qual;
1423
		qt &= cv_qual;
1424
		if (qs != qt) {
1425
			return (NULL_type);
2 7u83 1426
		}
7 7u83 1427
		if (!eq_func_lang(s, t)) {
1428
			return (NULL_type);
2 7u83 1429
		}
7 7u83 1430
		qs = DEREF_cv(type_func_mqual(s));
1431
	}
1432
 
1433
	/* Construct composite type */
1434
	if (mk) {
1435
		CV_SPEC cs = DEREF_cv(type_qual(s));
1436
		CV_SPEC ct = DEREF_cv(type_qual(t));
1437
		LIST(TYPE)ex = DEREF_list(type_func_except(s));
1438
		cs |= ct;
1439
		pt = ps;
1440
		if (!IS_NULL_type(mt)) {
1441
			CONS_type(mt, pt, pt);
2 7u83 1442
		}
7 7u83 1443
		MAKE_type_func(cs, rs, ps, es, qs, pt, ns, pids, ex, s);
2 7u83 1444
	}
7 7u83 1445
	return (s);
2 7u83 1446
}
1447
 
1448
#endif
1449
 
1450
 
1451
/*
1452
    FIND THE COMPOSITE OF TWO TYPES
1453
 
1454
    This routine finds the composite type (in the C sense) of s and t.
1455
    In C++ we only need to worry about compatible bound and unbound array
1456
    types, since all functions will be declared with prototypes.  The
1457
    routine returns the null type if s and t are not compatible.  Otherwise
1458
    it tries to returns either s or, as a second choice, t, whenever
1459
    possible to avoid new types having to be created.  Indeed if mk is
1460
    false a new type is never created - this can be used whenever
1461
    compatibility is being checked but the composite type is not used.
1462
    The result is an lvalue if either s or t is.  If qual is nonzero to
1463
    indicate that differing qualifiers are allowed.  In C++ the qualifiers
1464
    are allowed at any level; in C qual gives the maximum depth.  Type
1465
    qualifiers at the top level are handled by adding an error to err.
1466
*/
1467
 
7 7u83 1468
TYPE
1469
type_composite(TYPE s, TYPE t, int qual, int depth, ERROR *err, int mk)
2 7u83 1470
{
7 7u83 1471
	TYPE r = s;
1472
	int eq = 1;
1473
	int checked = 0;
1474
	unsigned ns, nt;
1475
	CV_SPEC qr, qs, qt;
2 7u83 1476
 
7 7u83 1477
	/* Check for obvious equality */
1478
	if (EQ_type(s, t)) {
1479
		return (s);
1480
	}
1481
	if (IS_NULL_type(s)) {
1482
		return (NULL_type);
1483
	}
1484
	if (IS_NULL_type(t)) {
1485
		return (NULL_type);
1486
	}
2 7u83 1487
 
7 7u83 1488
	/* Compare type qualifiers */
1489
	ns = TAG_type(s);
1490
	nt = TAG_type(t);
1491
	qs = DEREF_cv(type_qual(s));
1492
	qt = DEREF_cv(type_qual(t));
1493
	qr = (qs | qt);
1494
	if (qs != qt && qual <= 0) {
1495
		/* Qualifiers should be equal up to lvalues */
1496
		CV_SPEC rs = (qs & cv_qual);
1497
		CV_SPEC rt = (qt & cv_qual);
1498
		if (rs != rt) {
1499
			OPTION opt;
1500
			if (unify_types(s, t, 0)) {
1501
				/* Can happen with token definitions */
1502
				if (mk) {
1503
					r = qualify_type(r, qr, 0);
1504
				}
1505
				return (r);
1506
			}
1507
			opt = option(OPT_type_qual_incompat);
1508
			if (opt == OPTION_DISALLOW) {
1509
				goto return_lab;
1510
			}
1511
			add_error(err, ERR_basic_link_qual(rs, rt));
1512
		}
2 7u83 1513
	}
1514
#if LANGUAGE_C
7 7u83 1515
	qual--;
2 7u83 1516
#endif
1517
 
7 7u83 1518
	/* Check on type components */
1519
	if (ns == nt) {
1520
		switch (ns) {
1521
		case type_ptr_tag:
1522
		case type_ref_tag: {
1523
			/* Check pointer sub-types */
1524
			TYPE pr;
1525
			TYPE ps = DEREF_type(type_ptr_etc_sub(s));
1526
			TYPE pt = DEREF_type(type_ptr_etc_sub(t));
1527
			pr = type_composite(ps, pt, qual, depth + 1, err, mk);
1528
			if (IS_NULL_type(pr)) {
1529
				/* Check for generic pointer types */
1530
				OPTION opt = option(OPT_gen_ptr_char);
1531
				if (opt == OPTION_DISALLOW) {
1532
					return (NULL_type);
1533
				}
1534
				if (IS_type_top_etc(ps)) {
1535
					if (eq_type_unqual(pt, type_char)) {
1536
						CV_SPEC cv = DEREF_cv(type_qual(pt));
1537
						pt = qualify_type(ps, cv, 0);
1538
						add_error(err, ERR_conv_ptr_gen(t));
1539
					} else {
1540
						return (NULL_type);
1541
					}
1542
				} else if (IS_type_top_etc(pt)) {
1543
					if (eq_type_unqual(ps, type_char)) {
1544
						CV_SPEC cv = DEREF_cv(type_qual(ps));
1545
						ps = qualify_type(pt, cv, 0);
1546
						add_error(err, ERR_conv_ptr_gen(s));
1547
					} else {
1548
						return (NULL_type);
1549
					}
1550
				} else {
1551
					return (NULL_type);
1552
				}
1553
				pr = type_composite(ps, pt, qual, depth + 1, err, mk);
1554
				if (IS_NULL_type(pr)) {
1555
					return (NULL_type);
1556
				}
2 7u83 1557
			}
7 7u83 1558
			if (mk) {
1559
				if (EQ_type(pr, ps) && qr == qs) {
1560
					return (s);
1561
				}
1562
				if (EQ_type(pr, pt) && qr == qt) {
1563
					return (t);
1564
				}
1565
				MAKE_type_ptr_etc(ns, qr, pr, r);
2 7u83 1566
			}
7 7u83 1567
			return (r);
2 7u83 1568
		}
1569
 
1570
#if LANGUAGE_CPP
7 7u83 1571
		case type_ptr_mem_tag: {
1572
			/* Check pointer to member class types */
1573
			TYPE ps, pt, pr;
1574
			CLASS_TYPE cs = DEREF_ctype(type_ptr_mem_of(s));
1575
			CLASS_TYPE ct = DEREF_ctype(type_ptr_mem_of(t));
1576
			if (!eq_ctype(cs, ct)) {
1577
				return (NULL_type);
1578
			}
2 7u83 1579
 
7 7u83 1580
			/* Check pointer to member sub-types */
1581
			ps = DEREF_type(type_ptr_mem_sub(s));
1582
			pt = DEREF_type(type_ptr_mem_sub(t));
1583
			pr = type_composite(ps, pt, qual, depth + 1, err, mk);
1584
			if (IS_NULL_type(pr)) {
1585
				return (NULL_type);
1586
			}
1587
			if (mk) {
1588
				if (EQ_type(pr, ps) && qr == qt) {
1589
					return (s);
1590
				}
1591
				if (EQ_type(pr, pt) && qr == qs) {
1592
					return (t);
1593
				}
1594
				MAKE_type_ptr_mem(qr, cs, pr, r);
1595
			}
1596
			return (r);
2 7u83 1597
		}
1598
#endif
1599
 
7 7u83 1600
		case type_array_tag: {
1601
			/* Check array sub-types */
1602
			TYPE pr;
1603
			NAT ms, mt, mr;
1604
			TYPE ps = DEREF_type(type_array_sub(s));
1605
			TYPE pt = DEREF_type(type_array_sub(t));
1606
			pr = type_composite(ps, pt, qual, depth + 1, err, mk);
1607
			if (IS_NULL_type(pr)) {
1608
				return (NULL_type);
1609
			}
2 7u83 1610
 
7 7u83 1611
			/* Check array bounds */
1612
			ms = DEREF_nat(type_array_size(s));
1613
			mt = DEREF_nat(type_array_size(t));
1614
			if (EQ_nat(ms, mt) || eq_nat(ms, mt)) {
1615
				/* Equal bounds */
1616
				if (EQ_type(pr, ps) && qr == qs) {
1617
					return (s);
1618
				}
1619
				if (EQ_type(pr, pt) && qr == qt) {
1620
					return (t);
1621
				}
1622
				mr = ms;
1623
			} else if (IS_NULL_nat(ms)) {
1624
				/* s unbounded, t bounded */
2 7u83 1625
#if LANGUAGE_CPP
7 7u83 1626
				if (depth) {
1627
					return (NULL_type);
1628
				}
2 7u83 1629
#endif
7 7u83 1630
				if (EQ_type(pr, pt) && qr == qt) {
1631
					return (t);
1632
				}
1633
				mr = mt;
1634
			} else if (IS_NULL_nat(mt)) {
1635
				/* s bounded, t unbounded */
2 7u83 1636
#if LANGUAGE_CPP
7 7u83 1637
				if (depth) {
1638
					return (NULL_type);
1639
				}
2 7u83 1640
#endif
7 7u83 1641
				if (EQ_type(pr, ps) && qr == qs) {
1642
					return (s);
1643
				}
1644
				mr = ms;
1645
			} else {
1646
				/* Unequal bounds - check for error
1647
				 * propagation */
1648
				if (is_error_nat(ms)) {
1649
					mr = mt;
1650
				} else if (is_error_nat(mt)) {
1651
					mr = ms;
1652
				} else {
1653
					return (NULL_type);
1654
				}
1655
			}
1656
			if (mk) {
1657
				MAKE_type_array(qr, pr, mr, r);
1658
			}
1659
			return (r);
2 7u83 1660
		}
1661
 
7 7u83 1662
		case type_func_tag: {
1663
			/* Check function types */
1664
			int ret = eq_func_type(s, t, 1, 0);
2 7u83 1665
#if LANGUAGE_C
7 7u83 1666
			if (ret < 2) {
1667
				r = func_composite(s, t, ret, err, mk);
1668
				return (r);
1669
			}
2 7u83 1670
#else
7 7u83 1671
			if (ret < 2) {
1672
				eq = 0;
1673
			} else if (depth && ret == 2) {
1674
				/* Differ in language specifiers */
1675
				eq = 0;
1676
			}
1677
#endif
1678
			checked = 1;
1679
			break;
2 7u83 1680
		}
1681
 
1682
#if LANGUAGE_C
7 7u83 1683
		case type_enumerate_tag: {
1684
			/* Check C enumeration types */
1685
			ENUM_TYPE es = DEREF_etype(type_enumerate_defn(s));
1686
			ENUM_TYPE et = DEREF_etype(type_enumerate_defn(t));
1687
			TYPE ps = DEREF_type(etype_rep(es));
1688
			TYPE pt = DEREF_type(etype_rep(et));
1689
			eq = eq_type_unqual(ps, pt);
1690
			checked = 1;
1691
			break;
1692
		}
2 7u83 1693
#endif
1694
		}
7 7u83 1695
	} else {
1696
		switch (ns) {
1697
		case type_top_tag: {
1698
			/* Allow for 'void' and 'bottom' */
1699
			if (nt == type_bottom_tag) {
1700
				checked = 1;
1701
				r = t;
1702
			}
1703
			break;
2 7u83 1704
		}
7 7u83 1705
		case type_bottom_tag: {
1706
			/* Allow for 'void' and 'bottom' */
1707
			if (nt == type_top_tag) {
1708
				checked = 1;
1709
			}
1710
			break;
1711
		}
2 7u83 1712
#if LANGUAGE_C
7 7u83 1713
		case type_integer_tag: {
1714
			if (nt == type_enumerate_tag) {
1715
				/* Check C enumeration types */
1716
				TYPE pt;
1717
				ENUM_TYPE et;
1718
				et = DEREF_etype(type_enumerate_defn(t));
1719
				pt = DEREF_type(etype_rep(et));
1720
				eq = eq_type_unqual(s, pt);
1721
				checked = 1;
1722
				r = t;
1723
			}
1724
			break;
2 7u83 1725
		}
7 7u83 1726
		case type_enumerate_tag: {
1727
			if (nt == type_integer_tag) {
1728
				/* Check C enumeration types */
1729
				TYPE ps;
1730
				ENUM_TYPE es;
1731
				es = DEREF_etype(type_enumerate_defn(s));
1732
				ps = DEREF_type(etype_rep(es));
1733
				eq = eq_type_unqual(ps, t);
1734
				checked = 1;
1735
			}
1736
			break;
2 7u83 1737
		}
1738
#endif
7 7u83 1739
		}
2 7u83 1740
	}
1741
 
7 7u83 1742
	/* In other cases compatibility is equality */
1743
	if (eq) {
1744
		if (!checked) {
1745
			eq = eq_type_unqual(s, t);
2 7u83 1746
		}
7 7u83 1747
		if (eq == 1) {
1748
			if (mk) {
1749
				if (ns == nt) {
1750
					if (qr == qs) {
1751
						return (s);
1752
					}
1753
					if (qr == qt) {
1754
						return (t);
1755
					}
1756
				}
1757
				r = qualify_type(r, qr, 0);
1758
			}
1759
			return (r);
1760
		}
2 7u83 1761
	}
7 7u83 1762
return_lab:
1763
	if (ns == type_error_tag) {
1764
		return (t);
1765
	}
1766
	if (nt == type_error_tag) {
1767
		return (s);
1768
	}
1769
	return (NULL_type);
2 7u83 1770
}
1771
 
1772
 
1773
/*
1774
    ARE TWO TYPES COMPATIBLE?
1775
 
1776
    This routine checks whether the types s and t are compatible, returning
1777
    the composite type if they are (or either s or t if mk is false).  If
1778
    the types are not compatible s is returned and an error is added to the
1779
    end of err.  qual is as in type_composite.
1780
*/
1781
 
7 7u83 1782
TYPE
1783
check_compatible(TYPE s, TYPE t, int qual, ERROR *err, int mk)
2 7u83 1784
{
7 7u83 1785
	TYPE r;
1786
	force_tokdef++;
1787
	r = type_composite(s, t, qual, 0, err, mk);
1788
	if (IS_NULL_type(r)) {
1789
		add_error(err, ERR_basic_link_incompat(s, t));
1790
		r = s;
1791
	}
1792
	force_tokdef--;
1793
	return (r);
2 7u83 1794
}
1795
 
1796
 
1797
/*
1798
    IS A TYPE AN OBJECT TYPE?
1799
 
1800
    C++ types are partitioned into object types, reference types and
1801
    function types.  Object types are further partitioned into complete
1802
    and incomplete types.  This routine checks whether the type t is
1803
    an object type.  It returns an error if it isn't.
1804
*/
1805
 
7 7u83 1806
ERROR
1807
check_object(TYPE t)
2 7u83 1808
{
7 7u83 1809
	ERROR err = NULL_err;
1810
	switch (TAG_type(t)) {
1811
	case type_func_tag: {
1812
		/* Function types are not object types */
1813
		err = ERR_basic_types_obj_func(t);
1814
		break;
2 7u83 1815
	}
7 7u83 1816
	case type_ref_tag: {
1817
		/* Reference types are not object types */
1818
		err = ERR_basic_types_obj_ref(t);
1819
		break;
2 7u83 1820
	}
7 7u83 1821
	case type_templ_tag: {
1822
		/* Check template types */
1823
		TYPE s = DEREF_type(type_templ_defn(t));
1824
		err = check_object(s);
1825
		break;
2 7u83 1826
	}
7 7u83 1827
	}
1828
	return (err);
2 7u83 1829
}
1830
 
1831
 
1832
/*
1833
    IS A TYPE AN ABSTRACT CLASS?
1834
 
1835
    This routine checks whether the type t is an abstract class type.
1836
    It returns an error if it is.  It also checks for arrays of abstract
1837
    types, although it shouldn't be possible to construct these.
1838
*/
1839
 
7 7u83 1840
ERROR
1841
check_abstract(TYPE t)
2 7u83 1842
{
7 7u83 1843
	ERROR err = NULL_err;
1844
	switch (TAG_type(t)) {
1845
	case type_compound_tag: {
1846
		/* Check for abstract classes */
1847
		CLASS_TYPE ct = DEREF_ctype(type_compound_defn(t));
1848
		CLASS_INFO ci = DEREF_cinfo(ctype_info(ct));
1849
		if (ci & cinfo_abstract) {
1850
			err = class_info(ct, cinfo_abstract, 1);
1851
		}
1852
		break;
2 7u83 1853
	}
7 7u83 1854
	case type_array_tag: {
1855
		/* Check for abstract arrays */
1856
		TYPE s = DEREF_type(type_array_sub(t));
1857
		err = check_abstract(s);
1858
		break;
2 7u83 1859
	}
7 7u83 1860
	case type_templ_tag: {
1861
		/* Check template types */
1862
		TYPE s = DEREF_type(type_templ_defn(t));
1863
		err = check_abstract(s);
1864
		break;
2 7u83 1865
	}
7 7u83 1866
	}
1867
	return (err);
2 7u83 1868
}
1869
 
1870
 
1871
/*
1872
    IS A TYPE INCOMPLETE?
1873
 
1874
    This routine checks whether the type t is an incomplete object type.
1875
    It returns an error if it is.
1876
*/
1877
 
7 7u83 1878
ERROR
1879
check_incomplete(TYPE t)
2 7u83 1880
{
7 7u83 1881
	ERROR err = NULL_err;
1882
	switch (TAG_type(t)) {
1883
	case type_top_tag:
1884
	case type_bottom_tag: {
1885
		/* void and bottom are incomplete */
1886
		err = ERR_basic_types_incompl(t);
1887
		break;
2 7u83 1888
	}
7 7u83 1889
	case type_array_tag: {
1890
		/* Check for incomplete arrays */
1891
		NAT n = DEREF_nat(type_array_size(t));
1892
		if (IS_NULL_nat(n)) {
1893
			err = ERR_basic_types_incompl(t);
1894
		} else {
1895
			TYPE s = DEREF_type(type_array_sub(t));
1896
			err = check_incomplete(s);
1897
		}
1898
		break;
2 7u83 1899
	}
7 7u83 1900
	case type_compound_tag: {
1901
		/* Check for incomplete classes */
1902
		CLASS_TYPE ct = DEREF_ctype(type_compound_defn(t));
1903
		CLASS_INFO ci = DEREF_cinfo(ctype_info(ct));
1904
		if (!(ci & cinfo_defined)) {
1905
			complete_class(ct, 1);
1906
			ci = DEREF_cinfo(ctype_info(ct));
1907
		}
1908
		if (!(ci & cinfo_complete)) {
1909
			err = ERR_basic_types_incompl(t);
1910
		}
1911
		break;
2 7u83 1912
	}
7 7u83 1913
	case type_enumerate_tag: {
1914
		/* Check for incomplete enumerations */
1915
		ENUM_TYPE et = DEREF_etype(type_enumerate_defn(t));
1916
		CLASS_INFO ei = DEREF_cinfo(etype_info(et));
1917
		if (!(ei & cinfo_complete)) {
1918
			err = ERR_basic_types_incompl(t);
1919
		}
1920
		break;
2 7u83 1921
	}
7 7u83 1922
	case type_templ_tag: {
1923
		/* Check template types */
1924
		TYPE s = DEREF_type(type_templ_defn(t));
1925
		err = check_incomplete(s);
1926
		break;
2 7u83 1927
	}
7 7u83 1928
	}
1929
	return (err);
2 7u83 1930
}
1931
 
1932
 
1933
/*
1934
    IS A TYPE A COMPLETE OBJECT TYPE?
1935
 
1936
    This routine checks whether the type t is a complete object type.  It
1937
    returns an error if it isn't.
1938
*/
1939
 
7 7u83 1940
ERROR
1941
check_complete(TYPE t)
2 7u83 1942
{
7 7u83 1943
	ERROR err = check_object(t);
1944
	if (IS_NULL_err(err)) {
1945
		err = check_incomplete(t);
1946
		if (!IS_NULL_err(err)) {
1947
			err = concat_error(err, ERR_basic_types_obj_incompl());
1948
		}
2 7u83 1949
	}
7 7u83 1950
	return (err);
2 7u83 1951
}
1952
 
1953
 
1954
/*
1955
    IS A TYPE A POINTER TO A COMPLETE OBJECT TYPE?
1956
 
1957
    This routine checks whether the type t is a pointer to a complete
1958
    object type, adding an error to err.  It returns the type pointed to.
1959
*/
1960
 
7 7u83 1961
TYPE
1962
check_pointer(TYPE t, ERROR *err)
2 7u83 1963
{
7 7u83 1964
	TYPE s;
1965
	switch (TAG_type(t)) {
1966
	case type_ptr_tag: {
1967
		/* Pointer type */
1968
		s = DEREF_type(type_ptr_sub(t));
1969
		break;
2 7u83 1970
	}
7 7u83 1971
	case type_array_tag: {
1972
		/* Allow for array-to-pointer conversion */
1973
		s = DEREF_type(type_array_sub(t));
1974
		break;
2 7u83 1975
	}
7 7u83 1976
	case type_func_tag: {
1977
		/* Allow for function-to-pointer conversion */
1978
		s = t;
1979
		break;
2 7u83 1980
	}
7 7u83 1981
	case type_ref_tag: {
1982
		/* Reference type */
1983
		t = DEREF_type(type_ref_sub(t));
1984
		s = check_pointer(t, err);
1985
		return (s);
2 7u83 1986
	}
7 7u83 1987
	default:
1988
		/* Shouldn't happen */
1989
		return (t);
2 7u83 1990
	}
7 7u83 1991
	if (err != KILL_err) {
1992
		switch (TAG_type(s)) {
1993
		case type_top_tag:
1994
		case type_bottom_tag: {
1995
			add_error(err, ERR_basic_types_obj_void(t));
1996
			break;
1997
		}
1998
		default:
1999
			add_error(err, check_complete(s));
2000
			break;
2001
		}
2 7u83 2002
	}
7 7u83 2003
	return (s);
2 7u83 2004
}
2005
 
2006
 
2007
/*
2008
    IS A TYPE MODIFIABLE?
2009
 
2010
    This routine checks whether the expression a of type t represents a
2011
    modifiable lvalue.  If it is then the null error is returned, otherwise
2012
    a sequence of errors giving the reasons why t is not modifiable is
2013
    returned.
2014
*/
2015
 
7 7u83 2016
ERROR
2017
check_modifiable(TYPE t, EXP a)
2 7u83 2018
{
7 7u83 2019
	ERROR err;
2020
	unsigned tag = TAG_type(t);
2021
	CV_SPEC qual = DEREF_cv(type_qual(t));
2022
	if (qual & cv_lvalue) {
2023
		while (tag == type_templ_tag) {
2024
			t = DEREF_type(type_templ_defn(t));
2025
			tag = TAG_type(t);
2 7u83 2026
		}
7 7u83 2027
		if (tag == type_func_tag) {
2028
			/* Function types are not modifiable */
2029
			err = ERR_basic_lval_mod_func();
2030
		} else if (tag == type_array_tag) {
2031
			/* Array types are not modifiable */
2032
			err = ERR_basic_lval_mod_array();
2033
		} else {
2034
			err = check_complete(t);
2035
			if (!IS_NULL_err(err)) {
2036
				/* Incomplete types are not modifiable */
2037
				err = concat_error(err, ERR_basic_lval_mod_incompl());
2038
			} else if (qual & cv_const) {
2039
				/* const objects are not modifiable */
2040
				err = ERR_basic_lval_mod_const();
2041
			} else if (tag == type_compound_tag) {
2042
				CLASS_TYPE ct = DEREF_ctype(type_compound_defn(t));
2043
				CLASS_INFO ci = DEREF_cinfo(ctype_info(ct));
2044
				if (ci & cinfo_const) {
2045
					/* Objects with const members are not modifiable */
2046
					err = ERR_basic_lval_mod_member(t);
2047
				}
2048
			} else if (tag == type_integer_tag) {
2049
				if (option(OPT_const_string) && cv_string == cv_none) {
2050
					EXP b = NULL_exp;
2051
					DECL_SPEC ds = find_exp_linkage(a, &b, 1);
2052
					if (ds & dspec_pure) {
2053
						/* String literals are not modifiable */
2054
						err = ERR_conv_array_str_mod();
2055
					}
2056
				}
2057
			}
2 7u83 2058
		}
2059
	} else {
7 7u83 2060
		/* rvalues are not modifiable */
2061
		if (tag == type_error_tag) {
2062
			err = NULL_err;
2063
		} else {
2064
			err = ERR_basic_lval_not();
2065
			err = concat_error(err, ERR_basic_lval_mod_rvalue());
2066
		}
2 7u83 2067
	}
7 7u83 2068
	return (err);
2 7u83 2069
}
2070
 
2071
 
2072
/*
2073
    DOES A TYPE HAVE LINKAGE?
2074
 
2075
    This routine returns true if the type t has external linkage and is
2076
    not an anonymous type.  Not all the cases are fully checked yet.
2077
*/
2078
 
7 7u83 2079
int
2080
is_global_type(TYPE t)
2 7u83 2081
{
7 7u83 2082
	if (!IS_NULL_type(t)) {
2083
		ASSERT(ORDER_type == 18);
2084
		switch (TAG_type(t)) {
2085
		case type_ptr_tag: {
2086
			TYPE s = DEREF_type(type_ptr_sub(t));
2087
			return (is_global_type(s));
2088
		}
2089
		case type_ref_tag: {
2090
			TYPE s = DEREF_type(type_ref_sub(t));
2091
			return (is_global_type(s));
2092
		}
2 7u83 2093
#if LANGUAGE_CPP
7 7u83 2094
		case type_ptr_mem_tag: {
2095
			TYPE s = DEREF_type(type_ptr_mem_sub(t));
2096
			CLASS_TYPE ct = DEREF_ctype(type_ptr_mem_of(t));
2097
			IDENTIFIER id = DEREF_id(ctype_name(ct));
2098
			HASHID nm = DEREF_hashid(id_name(id));
2099
			DECL_SPEC ds = DEREF_dspec(id_storage(id));
2100
			if (!(ds & dspec_extern)) {
2101
				return (0);
2102
			}
2103
			if (IS_hashid_anon(nm)) {
2104
				return (0);
2105
			}
2106
			return (is_global_type(s));
2107
		}
2 7u83 2108
#endif
7 7u83 2109
		case type_func_tag: {
2110
			TYPE r = DEREF_type(type_func_ret(t));
2111
			LIST(TYPE)p = DEREF_list(type_func_ptypes(t));
2112
			if (!is_global_type(r)) {
2113
				return (0);
2114
			}
2115
			while (!IS_NULL_list(p)) {
2116
				TYPE s = DEREF_type(HEAD_list(p));
2117
				if (!is_global_type(s)) {
2118
					return (0);
2119
				}
2120
				p = TAIL_list(p);
2121
			}
2122
			break;
2 7u83 2123
		}
7 7u83 2124
		case type_array_tag: {
2125
			TYPE s = DEREF_type(type_array_sub(t));
2126
			return (is_global_type(s));
2127
		}
2128
		case type_compound_tag: {
2129
			CLASS_TYPE ct = DEREF_ctype(type_compound_defn(t));
2130
			IDENTIFIER id = DEREF_id(ctype_name(ct));
2131
			DECL_SPEC ds = DEREF_dspec(id_storage(id));
2132
			HASHID nm = DEREF_hashid(id_name(id));
2133
			if (!(ds & dspec_extern)) {
2134
				return (0);
2135
			}
2136
			if (IS_hashid_anon(nm)) {
2137
				return (0);
2138
			}
2139
			break;
2140
		}
2141
		case type_enumerate_tag: {
2142
			ENUM_TYPE et = DEREF_etype(type_enumerate_defn(t));
2143
			IDENTIFIER id = DEREF_id(etype_name(et));
2144
			DECL_SPEC ds = DEREF_dspec(id_storage(id));
2145
			HASHID nm = DEREF_hashid(id_name(id));
2146
			if (!(ds & dspec_extern)) {
2147
				return (0);
2148
			}
2149
			if (IS_hashid_anon(nm)) {
2150
				return (0);
2151
			}
2152
			break;
2153
		}
2154
		case type_templ_tag: {
2155
			TYPE s = DEREF_type(type_templ_defn(t));
2156
			return (is_global_type(s));
2157
		}
2158
		}
2 7u83 2159
	}
7 7u83 2160
	return (1);
2 7u83 2161
}