Subversion Repositories tendra.SVN

Rev

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

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