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 "exp_ops.h"
36
#include "ftype_ops.h"
37
#include "graph_ops.h"
38
#include "hashid_ops.h"
39
#include "id_ops.h"
40
#include "itype_ops.h"
41
#include "nat_ops.h"
42
#include "type_ops.h"
43
#include "error.h"
44
#include "catalog.h"
45
#include "option.h"
46
#include "basetype.h"
47
#include "cast.h"
48
#include "check.h"
49
#include "chktype.h"
50
#include "constant.h"
51
#include "construct.h"
52
#include "convert.h"
53
#include "derive.h"
54
#include "expression.h"
55
#include "function.h"
56
#include "identifier.h"
57
#include "initialise.h"
58
#include "instance.h"
59
#include "inttype.h"
60
#include "literal.h"
61
#include "member.h"
62
#include "namespace.h"
63
#include "overload.h"
64
#include "predict.h"
65
#include "quality.h"
66
#include "syntax.h"
67
#include "template.h"
68
#include "tokdef.h"
69
#include "token.h"
70
 
71
 
72
/*
73
    FIND THE PROMOTION OF A TYPE
74
 
75
    This routine finds the promoted type for the type t.  For integral
76
    types the promoted type is calculated and stored when the type is
77
    first constructed.  Enumeration types and bitfield types are promoted
78
    according to their underlying types.  Other types (including floating
79
    point types) are their own promotions.
80
*/
81
 
82
TYPE promote_type
83
    PROTO_N ( ( t ) )
84
    PROTO_T ( TYPE t )
85
{
86
    switch ( TAG_type ( t ) ) {
87
	case type_integer_tag : {
88
	    /* Retrieve the promotion of an integral type */
89
	    INT_TYPE it = DEREF_itype ( type_integer_rep ( t ) ) ;
90
	    t = DEREF_type ( itype_prom ( it ) ) ;
91
	    break ;
92
	}
93
	case type_enumerate_tag : {
94
	    /* Find the underlying type of an enumeration */
95
	    ENUM_TYPE et = DEREF_etype ( type_enumerate_defn ( t ) ) ;
96
	    t = DEREF_type ( etype_rep ( et ) ) ;
97
	    t = promote_type ( t ) ;
98
	    break ;
99
	}
100
	case type_bitfield_tag : {
101
	    /* Retrieve the promotion of a bitfield type */
102
	    INT_TYPE it = DEREF_itype ( type_bitfield_defn ( t ) ) ;
103
	    t = DEREF_type ( itype_prom ( it ) ) ;
104
	    break ;
105
	}
106
    }
107
    return ( t ) ;
108
}
109
 
110
 
111
/*
112
    FIND THE ARGUMENT PROMOTION OF A TYPE
113
 
114
    This routine finds the argument promotion type for the type t.  This
115
    is identical to the normal arithmetic promotion type for integral types,
116
    but differs for floating point types (float promotes to double etc.).
117
    Any errors are added to the end of err.
118
*/
119
 
120
TYPE arg_promote_type
121
    PROTO_N ( ( t, err ) )
122
    PROTO_T ( TYPE t X ERROR *err )
123
{
124
    switch ( TAG_type ( t ) ) {
125
	case type_integer_tag :
126
	case type_enumerate_tag :
127
	case type_bitfield_tag : {
128
	    /* Promote integral types */
129
	    t = promote_type ( t ) ;
130
	    break ;
131
	}
132
	case type_floating_tag : {
133
	    /* Retrieve the promotion of a floating point type */
134
	    FLOAT_TYPE ft = DEREF_ftype ( type_floating_rep ( t ) ) ;
135
	    t = DEREF_type ( ftype_arg_prom ( ft ) ) ;
136
	    break ;
137
	}
138
	case type_top_tag :
139
	case type_bottom_tag : {
140
	    /* Can't have 'void' arguments */
141
	    add_error ( err, ERR_basic_fund_void_exp ( t ) ) ;
142
	    break ;
143
	}
144
	case type_func_tag : {
145
	    /* Apply function-to-pointer conversion */
146
	    MAKE_type_ptr ( cv_none, t, t ) ;
147
	    break ;
148
	}
149
	case type_array_tag : {
150
	    /* Apply array-to-pointer conversion */
151
	    TYPE s = DEREF_type ( type_array_sub ( t ) ) ;
152
	    MAKE_type_ptr ( cv_none, s, t ) ;
153
	    break ;
154
	}
155
	case type_compound_tag : {
156
	    /* Types with constructors are suspicious */
157
	    if ( pass_complex_type ( t ) ) {
158
		add_error ( err, ERR_expr_call_struct ( t ) ) ;
159
	    }
160
	    break ;
161
	}
162
    }
163
    return ( t ) ;
164
}
165
 
166
 
167
/*
168
    IS A TYPE EQUAL TO ITS ARGUMENT PROMOTION TYPE?
169
 
170
    This routine checks whether the integral or floating point type t is
171
    equal to its argument promotion type.
172
*/
173
 
174
int is_arg_promote
175
    PROTO_N ( ( t ) )
176
    PROTO_T ( TYPE t )
177
{
178
    int eq = 1 ;
179
    if ( !IS_NULL_type ( t ) ) {
180
	t = expand_type ( t, 1 ) ;
181
	switch ( TAG_type ( t ) ) {
182
	    case type_integer_tag :
183
	    case type_floating_tag : {
184
		TYPE s = arg_promote_type ( t, KILL_err ) ;
185
		if ( !EQ_type ( s, t ) ) {
186
		    int ft = force_tokdef ;
187
		    force_tokdef = 0 ;
188
		    eq = eq_type_unqual ( s, t ) ;
189
		    force_tokdef = ft ;
190
		}
191
		break ;
192
	    }
193
	}
194
    }
195
    return ( eq ) ;
196
}
197
 
198
 
199
/*
200
    FIND THE TYPE WHICH PROMOTES TO A GIVEN TYPE
201
 
202
    This routine is a partial inverse to arg_promote_type which finds a
203
    type whose promotion is t.
204
*/
205
 
206
TYPE unpromote_type
207
    PROTO_N ( ( t ) )
208
    PROTO_T ( TYPE t )
209
{
210
    if ( !IS_NULL_type ( t ) ) {
211
	switch ( TAG_type ( t ) ) {
212
	    case type_integer_tag : {
213
		INT_TYPE it = DEREF_itype ( type_integer_rep ( t ) ) ;
214
		INT_TYPE is = DEREF_itype ( type_integer_sem ( t ) ) ;
215
		if ( !EQ_itype ( is, it ) ) t = make_itype ( is, is ) ;
216
		break ;
217
	    }
218
	    case type_floating_tag : {
219
		FLOAT_TYPE ft = DEREF_ftype ( type_floating_rep ( t ) ) ;
220
		FLOAT_TYPE fs = DEREF_ftype ( type_floating_sem ( t ) ) ;
221
		if ( !EQ_ftype ( fs, ft ) ) t = make_ftype ( fs, fs ) ;
222
		break ;
223
	    }
224
	}
225
    }
226
    return ( t ) ;
227
}
228
 
229
 
230
/*
231
    FIND THE TYPE FOR AN ARITHMETIC OPERATION
232
 
233
    This routine performs finds the result type for an arithmetic operation
234
    involving operands of types t and s.  These will always be arithmetic
235
    types.  The operands a and b are passed in order to help determine the
236
    semantic type of the result.
237
*/
238
 
239
TYPE arith_type
240
    PROTO_N ( ( t, s, a, b ) )
241
    PROTO_T ( TYPE t X TYPE s X EXP a X EXP b )
242
{
243
    TYPE r ;
244
    if ( EQ_type ( t, s ) ) {
245
	/* Equal types, promote the result */
246
	r = promote_type ( t ) ;
247
    } else {
248
	unsigned nt = TAG_type ( t ) ;
249
	unsigned ns = TAG_type ( s ) ;
250
	if ( nt == type_floating_tag ) {
251
	    if ( ns == type_floating_tag ) {
252
		/* Two floating point types */
253
		r = arith_ftype ( t, s ) ;
254
	    } else {
255
		/* If there is one floating type, this is the result */
256
		r = t ;
257
	    }
258
	} else {
259
	    if ( ns == type_floating_tag ) {
260
		/* If there is one floating type, this is the result */
261
		r = s ;
262
	    } else {
263
		/* Two integer types, promote them both */
264
		TYPE pt = promote_type ( t ) ;
265
		TYPE ps = promote_type ( s ) ;
266
		if ( EQ_type ( pt, ps ) ) {
267
		    r = pt ;
268
		} else {
269
		    r = arith_itype ( pt, ps, a, b ) ;
270
		}
271
	    }
272
	}
273
    }
274
    return ( r ) ;
275
}
276
 
277
 
278
/*
279
    QUALIFIER DEPTH
280
 
281
    The value qualifier depth is set by check_qualifier to the depth of
282
    the deepest different cv-qualifier it encounters.  The easy cases
283
    are 0 (the types are identically qualified) and 1 (the conversion
284
    is of the form 'cv1 T *' to 'cv2 T *').  The value qualifier_diff
285
    gives the qualifiers added at this stage.
286
*/
287
 
288
int qualifier_depth = 0 ;
289
static CV_SPEC qualifier_diff = cv_none ;
290
 
291
 
292
/*
293
    CHECK QUALIFICATION CONVERSIONS
294
 
295
    This routine checks for qualification conversions from the pointer or
296
    pointer to member type s to the pointer or pointer to member type t.
297
    For the qualification conversion to be valid, the two types have to obey
298
    four rules.  They have to be similar (i.e. the same up to cv-qualifiers)
299
    - this is assumed to be true if safe is true.  Each target qualifier
300
    must be more const-qualified and more volatile-qualified than the
301
    corresponding source qualifier.  Finally if two qualifiers differ
302
    then all the previous target qualifiers must involve const.  The C
303
    rules are slightly different.  Only one level of pointers is
304
    considered and the types pointed to must be compatible.  Note that
305
    for pointer to member types it is not checked that the underlying
306
    classes are the same at the top level.  This is to allow base class
307
    pointer to member conversion to be checked elsewhere.
308
*/
309
 
310
unsigned check_qualifier
311
    PROTO_N ( ( t, s, safe ) )
312
    PROTO_T ( TYPE t X TYPE s X int safe )
313
{
314
    int j = 0 ;
315
    int all_const = 1 ;
316
    unsigned res = QUAL_EQUAL ;
317
    t = expand_type ( t, 1 ) ;
318
    s = expand_type ( s, 1 ) ;
319
    qualifier_depth = 0 ;
320
    qualifier_diff = cv_none ;
321
    while ( !EQ_type ( t, s ) ) {
322
        unsigned nt = TAG_type ( t ) ;
323
        unsigned ns = TAG_type ( s ) ;
324
	CV_SPEC qt = DEREF_cv ( type_qual ( t ) ) ;
325
	CV_SPEC qs = DEREF_cv ( type_qual ( s ) ) ;
326
 
327
	/* Check qualifiers */
328
	if ( j == 0 ) {
329
	    /* Don't bother with top level qualifiers */
330
	    /* EMPTY */
331
	} else {
332
	    if ( nt == type_array_tag ) qt = find_cv_qual ( t ) ;
333
	    if ( ns == type_array_tag ) qs = find_cv_qual ( s ) ;
334
	    qt &= cv_qual ;
335
	    qs &= cv_qual ;
336
	    if ( qt != qs ) {
337
		CV_SPEC qr = ( qs & ~qt ) ;
338
		if ( qr & cv_const ) res &= ~QUAL_CONST ;
339
		if ( qr & cv_volatile ) res &= ~QUAL_VOLATILE ;
340
		res &= ~QUAL_EXACT ;
341
		if ( !all_const ) {
342
		    /* For inequality should have all consts in t */
343
		    res &= ~QUAL_ALL_CONST ;
344
		}
345
		qualifier_depth = j ;
346
		qualifier_diff = ( qt & ~qs ) ;
347
	    }
348
	    if ( !( qt & cv_const ) ) all_const = 0 ;
349
	}
350
 
351
	/* Check next type */
352
	switch ( nt ) {
353
 
354
	    case type_ptr_tag :
355
	    case type_ref_tag : {
356
		/* Pointer types */
357
		if ( ns != nt ) goto error_lab ;
358
#if LANGUAGE_C
359
		if ( j > 0 ) goto error_lab ;
360
#endif
361
		t = DEREF_type ( type_ptr_etc_sub ( t ) ) ;
362
		s = DEREF_type ( type_ptr_etc_sub ( s ) ) ;
363
		break ;
364
	    }
365
 
366
#if LANGUAGE_CPP
367
	    case type_ptr_mem_tag : {
368
		/* Pointer to member types */
369
		CLASS_TYPE cs, ct ;
370
		if ( ns != nt ) goto error_lab ;
371
		ct = DEREF_ctype ( type_ptr_mem_of ( t ) ) ;
372
		cs = DEREF_ctype ( type_ptr_mem_of ( s ) ) ;
373
		if ( j == 0 ) {
374
		    /* Top level classes checked elsewhere */
375
		    /* EMPTY */
376
		} else if ( !eq_ctype ( ct, cs ) ) {
377
		    /* Must point to members of the same class */
378
		    if ( in_template_decl ) {
379
			/* Mark template parameter types */
380
			TYPE ft = DEREF_type ( ctype_form ( ct ) ) ;
381
			TYPE fs = DEREF_type ( ctype_form ( cs ) ) ;
382
			if ( is_templ_depend ( ft ) ) res |= QUAL_TEMPL ;
383
			if ( is_templ_depend ( fs ) ) res |= QUAL_TEMPL ;
384
		    }
385
		    res &= ~( QUAL_EXACT | QUAL_SIMILAR ) ;
386
		    return ( res ) ;
387
		}
388
		t = DEREF_type ( type_ptr_mem_sub ( t ) ) ;
389
		s = DEREF_type ( type_ptr_mem_sub ( s ) ) ;
390
		break ;
391
	    }
392
#endif
393
 
394
	    case type_func_tag : {
395
		/* Function types */
396
		int eq ;
397
		if ( ns != nt ) goto error_lab ;
398
		if ( safe ) {
399
		    eq = 3 ;
400
		} else {
401
		    eq = eq_func_type ( t, s, 1, 0 ) ;
402
		    if ( eq < 2 ) goto error_lab ;
403
		}
404
		if ( res == QUAL_EQUAL ) res |= QUAL_FUNC ;
405
		if ( eq != 3 ) {
406
		    /* Linkage specifiers don't match */
407
		    res &= ~( QUAL_EXACT | QUAL_SIMILAR ) ;
408
		}
409
		return ( res ) ;
410
	    }
411
 
412
	    case type_top_tag :
413
	    case type_bottom_tag :
414
	    case type_compound_tag : {
415
		/* Don't trust 'safe' in these cases */
416
		if ( ns == nt && eq_type_unqual ( t, s ) ) return ( res ) ;
417
		goto error_lab ;
418
	    }
419
 
420
	    default : {
421
		/* Check other types */
422
		if ( safe ) return ( res ) ;
423
		if ( ns == nt && eq_type_unqual ( t, s ) ) return ( res ) ;
424
		goto error_lab ;
425
	    }
426
 
427
	    error_lab : {
428
		/* Unequal types */
429
#if LANGUAGE_C
430
		TYPE r = type_composite ( t, s, 1, 0, KILL_err, 0 ) ;
431
		if ( !IS_NULL_type ( r ) ) {
432
		    if ( IS_type_func ( r ) ) {
433
			if ( res == QUAL_EQUAL ) res |= QUAL_FUNC ;
434
		    }
435
		    return ( res ) ;
436
		}
437
#endif
438
		if ( in_template_decl ) {
439
		    /* Mark template parameter types */
440
		    if ( is_templ_depend ( t ) ) res |= QUAL_TEMPL ;
441
		    if ( is_templ_depend ( s ) ) res |= QUAL_TEMPL ;
442
		}
443
		if ( ns == type_error_tag || nt == type_error_tag ) {
444
		    /* Mark error types */
445
		    res |= QUAL_TEMPL ;
446
		}
447
		res &= ~( QUAL_EXACT | QUAL_SIMILAR ) ;
448
		return ( res ) ;
449
	    }
450
	}
451
	j++ ;
452
    }
453
    return ( res ) ;
454
}
455
 
456
 
457
/*
458
    FIND THE TYPE FOR A POINTER OPERATION
459
 
460
    This routine finds the common type for a pointer operation involving
461
    the pointer types t and s (as used, for example, in the relational
462
    operators).  Pointer conversions and qualification conversions are
463
    applied (including base class conversions if base is true), but the
464
    result must be a qualified version of one of the arguments, so only
465
    depth 1 qualification conversions are applied.  If t and s cannot be
466
    brought to a common type then the suspect flag is set to 2 and
467
    qualified 'void *' is returned.  suspect is set to 1 if precisely
468
    one of the types is 'void *'.  The routine also operates on reference
469
    types except that it returns a null type in the 'void *' case.
470
*/
471
 
472
TYPE ptr_common_type
473
    PROTO_N ( ( t, s, base, suspect ) )
474
    PROTO_T ( TYPE t X TYPE s X int base X int *suspect )
475
{
476
    CV_SPEC qt, qs ;
477
    TYPE r = NULL_type ;
478
    unsigned tag = TAG_type ( t ) ;
479
    TYPE pt = DEREF_type ( type_ptr_etc_sub ( t ) ) ;
480
    TYPE ps = DEREF_type ( type_ptr_etc_sub ( s ) ) ;
481
    unsigned nt = TAG_type ( pt ) ;
482
    unsigned ns = TAG_type ( ps ) ;
483
 
484
    /* Find the common type */
485
    if ( nt == ns ) {
486
	if ( eq_type_unqual ( pt, ps ) ) {
487
	    /* Pointers to the same type */
488
	    r = pt ;
489
	} else if ( nt == type_compound_tag && base ) {
490
	    /* Check for base class conversions */
491
	    CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( pt ) ) ;
492
	    CLASS_TYPE cs = DEREF_ctype ( type_compound_defn ( ps ) ) ;
493
	    CLASS_TYPE cr = compare_base_class ( ct, cs, 1 ) ;
494
	    if ( EQ_ctype ( cr, ct ) ) {
495
		/* p is a base class of q */
496
		r = pt ;
497
	    } else if ( EQ_ctype ( cr, cs ) ) {
498
		/* q is a base class of p */
499
		r = ps ;
500
	    }
501
	}
502
    } else if ( nt == type_top_tag || nt == type_bottom_tag ) {
503
	/* One pointer is 'void *' */
504
	*suspect = 1 ;
505
	r = pt ;
506
    } else if ( ns == type_top_tag || ns == type_bottom_tag ) {
507
	/* One pointer is 'void *' */
508
	*suspect = 1 ;
509
	r = ps ;
510
    }
511
#if LANGUAGE_C
512
    if ( IS_NULL_type ( r ) ) {
513
	/* In C, compatible types are allowed */
514
	r = type_composite ( pt, ps, 1, 0, KILL_err, 1 ) ;
515
    }
516
#endif
517
    if ( IS_NULL_type ( r ) ) {
518
	/* Can't bring to common pointer type */
519
	if ( is_templ_type ( t ) || is_templ_type ( s ) ) {
520
	    *suspect = -1 ;
521
	} else {
522
	    *suspect = 2 ;
523
	}
524
	if ( tag == type_ref_tag ) {
525
	    /* There are no generic references */
526
	    return ( NULL_type ) ;
527
	}
528
	r = type_void ;
529
    }
530
 
531
    /* Qualify the common type appropriately */
532
    qt = find_cv_qual ( pt ) ;
533
    qs = find_cv_qual ( ps ) ;
534
    r = qualify_type ( r, ( qt | qs ), 0 ) ;
535
 
536
    /* Form the result type */
537
    if ( EQ_type ( r, pt ) ) return ( t ) ;
538
    if ( EQ_type ( r, ps ) ) return ( s ) ;
539
    MAKE_type_ptr_etc ( tag, cv_none, r, r ) ;
540
    return ( r ) ;
541
}
542
 
543
 
544
/*
545
    FIND THE TYPE FOR A POINTER MEMBER OPERATION
546
 
547
    This routine finds the common type for a pointer to member operation
548
    involving the pointer to member types t and s (as used, for example,
549
    in the equality operators).  If t and s cannot be brought to a common
550
    type then suspect is set to 2 and the error type is returned.  If
551
    the cv-qualifier of the common type is not equal to the cv-qualifier
552
    of either t or s then suspect is set to 1.
553
*/
554
 
555
TYPE ptr_mem_common_type
556
    PROTO_N ( ( t, s, suspect ) )
557
    PROTO_T ( TYPE t X TYPE s X int *suspect )
558
{
559
    /* Check for base class conversions */
560
    CLASS_TYPE ct = DEREF_ctype ( type_ptr_mem_of ( t ) ) ;
561
    CLASS_TYPE cs = DEREF_ctype ( type_ptr_mem_of ( s ) ) ;
562
    CLASS_TYPE cr = compare_base_class ( ct, cs, 1 ) ;
563
    if ( !IS_NULL_ctype ( cr ) ) {
564
	TYPE pr ;
565
	TYPE pt = DEREF_type ( type_ptr_mem_sub ( t ) ) ;
566
	TYPE ps = DEREF_type ( type_ptr_mem_sub ( s ) ) ;
567
	if ( EQ_ctype ( cr, ct ) ) {
568
	    cr = cs ;
569
	    pr = ps ;
570
	} else {
571
	    cr = ct ;
572
	    pr = pt ;
573
	}
574
 
575
	/* Check that underlying types are the same */
576
	if ( eq_type_unqual ( pt, ps ) ) {
577
	    /* Form the result type */
578
	    CV_SPEC qt = find_cv_qual ( pt ) ;
579
	    CV_SPEC qs = find_cv_qual ( ps ) ;
580
	    CV_SPEC qr = ( qt | qs ) ;
581
	    pr = qualify_type ( pr, qr, 0 ) ;
582
	    if ( qt != qs && qr != qs ) *suspect = 1 ;
583
	    if ( EQ_ctype ( cr, ct ) && EQ_type ( pr, pt ) ) return ( t ) ;
584
	    if ( EQ_ctype ( cr, cs ) && EQ_type ( pr, ps ) ) return ( s ) ;
585
	    MAKE_type_ptr_mem ( cv_none, cr, pr, pr ) ;
586
	    return ( pr ) ;
587
	}
588
    }
589
 
590
    /* Can't bring to common pointer member type */
591
    if ( is_templ_type ( t ) || is_templ_type ( s ) ) {
592
	*suspect = -1 ;
593
    } else {
594
	*suspect = 2 ;
595
    }
596
    return ( type_error ) ;
597
}
598
 
599
 
600
/*
601
    FIND A COMMON TYPE
602
 
603
    This routine finds the common type for the types t and s.  This is
604
    the other type if either type is null, the single type if they are
605
    equal, the arithmetic type if they are both arithmetic and the common
606
    pointer, reference or pointer-to-member type if these are appropriate.
607
    The null type is returned if the common type cannot be formed and
608
    suspect is set accordingly.  The rules are essentially the same as
609
    for the '?:' operation.
610
*/
611
 
612
TYPE common_type
613
    PROTO_N ( ( t, s, suspect ) )
614
    PROTO_T ( TYPE t X TYPE s X int *suspect )
615
{
616
    unsigned nt, ns ;
617
    TYPE r = NULL_type ;
618
    if ( IS_NULL_type ( t ) ) return ( s ) ;
619
    if ( IS_NULL_type ( s ) ) return ( t ) ;
620
    nt = TAG_type ( t ) ;
621
    ns = TAG_type ( s ) ;
622
    if ( nt == ns ) {
623
	switch ( nt ) {
624
	    case type_ptr_tag :
625
	    case type_ref_tag : {
626
		/* Common pointer or reference type */
627
		r = ptr_common_type ( t, s, 1, suspect ) ;
628
		if ( *suspect != 2 ) return ( r ) ;
629
		r = NULL_type ;
630
		break ;
631
	    }
632
	    case type_ptr_mem_tag : {
633
		/* Common pointer to member type */
634
		r = ptr_mem_common_type ( t, s, suspect ) ;
635
		if ( *suspect != 2 ) return ( r ) ;
636
		r = NULL_type ;
637
		break ;
638
	    }
639
	    default : {
640
		/* Other types */
641
		if ( eq_type_unqual ( t, s ) ) {
642
		    CV_SPEC qt = find_cv_qual ( t ) ;
643
		    CV_SPEC qs = find_cv_qual ( s ) ;
644
		    if ( qt != qs ) {
645
			*suspect = 1 ;
646
			t = qualify_type ( t, ( qt | qs ), 0 ) ;
647
		    }
648
		    return ( t ) ;
649
		}
650
		break ;
651
	    }
652
	}
653
    }
654
    switch ( nt ) {
655
	case type_integer_tag :
656
	case type_enumerate_tag :
657
	case type_bitfield_tag :
658
	case type_floating_tag : {
659
	    switch ( ns ) {
660
		case type_integer_tag :
661
		case type_enumerate_tag :
662
		case type_bitfield_tag :
663
		case type_floating_tag : {
664
		    /* Common arithmetic type */
665
		    r = arith_type ( t, s, NULL_exp, NULL_exp ) ;
666
		    return ( r ) ;
667
		}
668
	    }
669
	}
670
    }
671
#if LANGUAGE_C
672
    if ( IS_NULL_type ( r ) ) {
673
	r = type_composite ( t, s, 1, 0, KILL_err, 1 ) ;
674
	if ( !IS_NULL_type ( r ) ) return ( r ) ;
675
    }
676
#endif
677
    if ( nt == type_error_tag ) return ( s ) ;
678
    if ( ns == type_error_tag ) return ( t ) ;
679
    *suspect = 2 ;
680
    return ( r ) ;
681
}
682
 
683
 
684
/*
685
    CONVERT AN EXPRESSION TO ITS PROMOTED TYPE
686
 
687
    This routine converts the expression e to its promoted type, t
688
    (previously calculated using promote_type).  Note that there is no
689
    effect unless t is an integral type.
690
*/
691
 
692
EXP convert_promote
693
    PROTO_N ( ( t, e ) )
694
    PROTO_T ( TYPE t X EXP e )
695
{
696
    if ( IS_type_integer ( t ) ) {
697
	TYPE s = DEREF_type ( exp_type ( e ) ) ;
698
	if ( !EQ_type ( t, s ) ) {
699
	    /* Perform non-trivial integral promotions */
700
	    e = cast_int_int ( t, e, KILL_err, CAST_IMPLICIT, 0 ) ;
701
	}
702
    }
703
    return ( e ) ;
704
}
705
 
706
 
707
/*
708
    CONVERT AN EXPRESSION TO ITS ARITHMETIC TYPE
709
 
710
    This routine converts the expression e to an arithmetic result type, t,
711
    formed by arith_type using the type of e as its nth argument.  Note that
712
    there are three cases: integer->integer, integer->float and float->float.
713
*/
714
 
715
EXP convert_arith
716
    PROTO_N ( ( t, e, op, n ) )
717
    PROTO_T ( TYPE t X EXP e X int op X int n )
718
{
719
    TYPE s = DEREF_type ( exp_type ( e ) ) ;
720
    if ( !EQ_type ( t, s ) ) {
721
	ERROR err = NULL_err ;
722
	if ( IS_type_floating ( t ) ) {
723
	    unsigned tag = TAG_type ( s ) ;
724
	    if ( tag == type_floating_tag ) {
725
		e = cast_float_float ( t, e, &err, CAST_IMPLICIT ) ;
726
	    } else {
727
		if ( tag == type_bitfield_tag ) {
728
		    s = find_bitfield_type ( s ) ;
729
		    e = cast_int_int ( s, e, &err, CAST_IMPLICIT, -1 ) ;
730
		}
731
		e = cast_int_float ( t, e, &err, CAST_IMPLICIT ) ;
732
	    }
733
	} else {
734
	    e = cast_int_int ( t, e, &err, CAST_IMPLICIT, -1 ) ;
735
	}
736
	if ( !IS_NULL_err ( err ) ) {
737
	    unsigned m = ( unsigned ) n ;
738
	    err = concat_error ( err, ERR_expr_convert_op ( m, op ) ) ;
739
	    report ( crt_loc, err ) ;
740
	}
741
    }
742
    return ( e ) ;
743
}
744
 
745
 
746
/*
747
    CONVERT A POINTER TO A COMMON POINTER TYPE
748
 
749
    This routine converts the pointer expression e to a common pointer
750
    type, t, formed by ptr_common_type using the type of e as its nth
751
    argument.
752
*/
753
 
754
EXP convert_ptr_common
755
    PROTO_N ( ( t, e, op, n ) )
756
    PROTO_T ( TYPE t X EXP e X int op X int n )
757
{
758
    TYPE s = DEREF_type ( exp_type ( e ) ) ;
759
    if ( !EQ_type ( t, s ) ) {
760
	ERROR err = NULL_err ;
761
	e = cast_ptr_ptr ( t, e, &err, CAST_IMPLICIT, 1, 1 ) ;
762
	if ( !IS_NULL_err ( err ) ) {
763
	    unsigned m = ( unsigned ) n ;
764
	    err = concat_error ( err, ERR_expr_convert_op ( m, op ) ) ;
765
	    report ( crt_loc, err ) ;
766
	}
767
    }
768
    return ( e ) ;
769
}
770
 
771
 
772
/*
773
    CONVERT A POINTER MEMBER TO A COMMON POINTER MEMBER TYPE
774
 
775
    This routine converts the pointer member expression e to a common
776
    pointer to member type, t, formed by ptr_mem_common_type using the
777
    type of e as its nth argument.
778
*/
779
 
780
EXP convert_ptr_mem_common
781
    PROTO_N ( ( t, e, op, n ) )
782
    PROTO_T ( TYPE t X EXP e X int op X int n )
783
{
784
    TYPE s = DEREF_type ( exp_type ( e ) ) ;
785
    if ( !EQ_type ( t, s ) ) {
786
	ERROR err = NULL_err ;
787
	e = cast_ptr_mem_ptr_mem ( t, e, &err, CAST_IMPLICIT, 1, 1 ) ;
788
	if ( !IS_NULL_err ( err ) ) {
789
	    unsigned m = ( unsigned ) n ;
790
	    err = concat_error ( err, ERR_expr_convert_op ( m, op ) ) ;
791
	    report ( crt_loc, err ) ;
792
	}
793
    }
794
    return ( e ) ;
795
}
796
 
797
 
798
/*
799
    CHECK FOR ASSIGNMENT IN A BOOLEAN
800
 
801
    This routine checks whether the expression a, which is to be converted
802
    to a boolean, is an assignment.  A warning is issued for 'x = y' and
803
    'x /= y' because of possible confusion with 'x == y' and 'x != y'
804
    respectively.  If the original expression was enclosed in brackets
805
    (as indicated by tag) then no warning is issued.
806
*/
807
 
808
static void boolean_assign
809
    PROTO_N ( ( a, tag ) )
810
    PROTO_T ( EXP a X unsigned tag )
811
{
812
    if ( tag != exp_paren_tag && !suppress_quality ) {
813
	if ( IS_exp_assign ( a ) ) {
814
	    report ( crt_loc, ERR_conv_bool_assign () ) ;
815
	} else if ( IS_exp_preinc ( a ) ) {
816
	    int op = DEREF_int ( exp_preinc_becomes ( a ) ) ;
817
	    if ( op == lex_assign || op == lex_div_Heq ) {
818
		report ( crt_loc, ERR_conv_bool_assign () ) ;
819
	    }
820
	}
821
    }
822
    return ;
823
}
824
 
825
 
826
/*
827
    CONVERT AN EXPRESSION TO A BOOLEAN
828
 
829
    This routine converts the expression a to a boolean if this is possible,
830
    returning the corresponding boolean expression.  Any error arising are
831
    added to the position indicated by the err argument.  User-defined
832
    conversions are handled elsewhere.
833
*/
834
 
835
EXP convert_boolean
836
    PROTO_N ( ( a, tag, err ) )
837
    PROTO_T ( EXP a X unsigned tag X ERROR *err )
838
{
839
    EXP e ;
840
    TYPE t = DEREF_type ( exp_type ( a ) ) ;
841
    switch ( TAG_exp ( a ) ) {
842
	case exp_int_lit_tag : {
843
	    /* Check for integer literals */
844
	    e = make_test_nat ( a ) ;
845
	    return ( e ) ;
846
	}
847
	case exp_float_lit_tag : {
848
	    /* Check for floating-point literals */
849
	    FLOAT f = DEREF_flt ( exp_float_lit_flt ( a ) ) ;
850
	    NAT n = round_float_lit ( f, crt_round_mode ) ;
851
	    if ( !IS_NULL_nat ( n ) && IS_nat_small ( n ) ) {
852
		unsigned v = DEREF_unsigned ( nat_small_value ( n ) ) ;
853
		if ( v < 2 ) {
854
		    v = BOOL_VALUE ( v ) ;
855
		    e = make_bool_exp ( v, exp_float_lit_tag ) ;
856
		    return ( e ) ;
857
		}
858
	    }
859
	    MAKE_exp_test ( type_bool, ntest_not_eq, a, e ) ;
860
	    MAKE_nat_calc ( e, n ) ;
861
	    MAKE_exp_int_lit ( type_bool, n, exp_test_tag, e ) ;
862
	    return ( e ) ;
863
	}
864
	case exp_contents_tag : {
865
	    /* Check for assignment in boolean */
866
	    EXP b = DEREF_exp ( exp_contents_ptr ( a ) ) ;
867
	    switch ( TAG_exp ( b ) ) {
868
		case exp_assign_tag :
869
		case exp_preinc_tag :
870
		case exp_postinc_tag : {
871
		    boolean_assign ( b, tag ) ;
872
		    break ;
873
		}
874
	    }
875
	    break ;
876
	}
877
	case exp_assign_tag :
878
	case exp_preinc_tag :
879
	case exp_postinc_tag : {
880
	    /* Check for assignment in boolean */
881
	    boolean_assign ( a, tag ) ;
882
	    break ;
883
	}
884
    }
885
 
886
    /* Perform the conversion */
887
    switch ( TAG_type ( t ) ) {
888
	case type_integer_tag : {
889
	    /* Integral types are allowed */
890
	    if ( check_int_type ( t, btype_bool ) ) return ( a ) ;
891
	    break ;
892
	}
893
	case type_bitfield_tag : {
894
	    /* Convert bitfields to integers */
895
	    a = convert_bitfield ( a ) ;
896
	    break ;
897
	}
898
	case type_enumerate_tag :
899
	case type_floating_tag :
900
	case type_ptr_tag :
901
	case type_ptr_mem_tag : {
902
	    /* These types are allowed */
903
	    break ;
904
	}
905
	case type_error_tag : {
906
	    /* Allow for error propagation */
907
	    break ;
908
	}
909
	default : {
910
	    /* These types are not allowed */
911
	    add_error ( err, ERR_conv_bool_cast ( t ) ) ;
912
	    break ;
913
	}
914
    }
915
    MAKE_exp_test ( type_bool, ntest_not_eq, a, e ) ;
916
    return ( e ) ;
917
}
918
 
919
 
920
/*
921
    REPORT OVERLOADED FUNCTIONS
922
 
923
    This routine prints an error and returns true if e represents an
924
    overloaded function.  If it represents a non-overloaded function which
925
    has not already resolved using resolve_cast then the function is
926
    marked as used.
927
*/
928
 
929
static int is_overloaded
930
    PROTO_N ( ( e ) )
931
    PROTO_T ( EXP e )
932
{
933
    if ( !IS_NULL_exp ( e ) && IS_exp_identifier_etc ( e ) ) {
934
	IDENTIFIER id = DEREF_id ( exp_identifier_etc_id ( e ) ) ;
935
	if ( IS_id_function_etc ( id ) ) {
936
	    QUALIFIER q = DEREF_qual ( exp_identifier_etc_qual ( e ) ) ;
937
	    if ( !( q & qual_mark ) ) {
938
		/* Not already resolved */
939
		TYPE fn = DEREF_type ( id_function_etc_type ( id ) ) ;
940
		IDENTIFIER over = DEREF_id ( id_function_etc_over ( id ) ) ;
941
		if ( !IS_NULL_id ( over ) || IS_type_templ ( fn ) ) {
942
		    /* Overloaded function */
943
		    report ( crt_loc, ERR_over_over_context ( id ) ) ;
944
		    return ( 1 ) ;
945
		}
946
		use_id ( id, suppress_usage ) ;
947
	    }
948
	}
949
    }
950
    return ( 0 ) ;
951
}
952
 
953
 
954
/*
955
    PERFORM QUALIFICATION CONVERSIONS ON A TYPE
956
 
957
    This routine removes any type qualifiers from a rvalue, non-class
958
    type t.  Class types maintain their type qualifiers, lvalue types
959
    lose theirs in convert_lvalue.
960
*/
961
 
962
TYPE convert_qual_type
963
    PROTO_N ( ( t ) )
964
    PROTO_T ( TYPE t )
965
{
966
    CV_SPEC qual = DEREF_cv ( type_qual ( t ) ) ;
967
    if ( qual && !( qual & cv_lvalue ) ) {
968
#if LANGUAGE_CPP
969
	if ( IS_type_compound ( t ) ) return ( t ) ;
970
#endif
971
	t = qualify_type ( t, cv_none, 0 ) ;
972
    }
973
    return ( t ) ;
974
}
975
 
976
 
977
/*
978
    EVALUATE A CONST VARIABLE
979
 
980
    This routine evaluates the 'const' variable expression a, so that
981
    for example, if 'const int c = 5 ;' then 'c' is evaluated to '5'.
982
*/
983
 
984
EXP convert_const
985
    PROTO_N ( ( a ) )
986
    PROTO_T ( EXP a )
987
{
988
#if LANGUAGE_CPP
989
    IDENTIFIER id = DEREF_id ( exp_identifier_id ( a ) ) ;
990
    EXP e = DEREF_exp ( id_variable_etc_init ( id ) ) ;
991
    if ( !IS_NULL_exp ( e ) ) {
992
	switch ( TAG_exp ( e ) ) {
993
	    case exp_int_lit_tag : {
994
		/* Propagate simple constants */
995
		NAT n ;
996
		TYPE t ;
997
		unsigned tag ;
998
		DECONS_exp_int_lit ( t, n, tag, e ) ;
999
		MAKE_exp_int_lit ( t, n, tag, e ) ;
1000
		return ( e ) ;
1001
	    }
1002
	    case exp_null_tag : {
1003
		/* Propagate null constants */
1004
		TYPE t ;
1005
		DECONS_exp_null ( t, e ) ;
1006
		MAKE_exp_null ( t, e ) ;
1007
		return ( e ) ;
1008
	    }
1009
	}
1010
    }
1011
#endif
1012
    return ( a ) ;
1013
}
1014
 
1015
 
1016
/*
1017
    PERFORM ARRAY TO POINTER CONVERSION
1018
 
1019
    This routine performs array to pointer conversion on the array
1020
    expression a.  If a is a string literal and str is true then the
1021
    const qualifiers are removed from the string.  A warning is also
1022
    added to err if str is 2.
1023
*/
1024
 
1025
EXP convert_array
1026
    PROTO_N ( ( a, str, err ) )
1027
    PROTO_T ( EXP a X int str X ERROR *err )
1028
{
1029
    TYPE t = DEREF_type ( exp_type ( a ) ) ;
1030
    TYPE s = DEREF_type ( type_array_sub ( t ) ) ;
1031
    if ( str && IS_exp_string_lit ( a ) ) {
1032
	/* Remove const from string literals */
1033
	CV_SPEC cv = DEREF_cv ( type_qual ( s ) ) ;
1034
	if ( cv & cv_const ) {
1035
	    cv &= ~cv_const ;
1036
	    s = qualify_type ( s, cv, 0 ) ;
1037
	    if ( str == 2 ) add_error ( err, ERR_conv_array_string () ) ;
1038
	}
1039
    } else if ( option ( OPT_addr_register ) && used_register ) {
1040
	/* Can't apply to a register variable in C */
1041
	EXP b = NULL_exp ;
1042
	DECL_SPEC ds = find_exp_linkage ( a, &b, 1 ) ;
1043
	if ( ( ds & dspec_register ) && !( ds & dspec_temp ) ) {
1044
	    if ( IS_exp_identifier ( b ) ) {
1045
		IDENTIFIER id = DEREF_id ( exp_identifier_id ( b ) ) ;
1046
		add_error ( err, ERR_expr_unary_op_ref_register ( id ) ) ;
1047
	    }
1048
	}
1049
    }
1050
    MAKE_type_ptr ( cv_none, s, t ) ;
1051
    MAKE_exp_address ( t, a, a ) ;
1052
    return ( a ) ;
1053
}
1054
 
1055
 
1056
/*
1057
    PERFORM LVALUE CONVERSIONS
1058
 
1059
    This routine performs the lvalue conversions on the expression a.
1060
    If e is an lvalue, the lvalue-to-rvalue, array-to-pointer and
1061
    function-to-pointer conversions are applied to transform it into an
1062
    rvalue.  Checks for overloaded functions are also applied at this
1063
    stage to both lvalues and rvalues.
1064
*/
1065
 
1066
EXP convert_lvalue
1067
    PROTO_N ( ( a ) )
1068
    PROTO_T ( EXP a )
1069
{
1070
    EXP e = a ;
1071
    TYPE t = DEREF_type ( exp_type ( a ) ) ;
1072
    CV_SPEC qual = DEREF_cv ( type_qual ( t ) ) ;
1073
    if ( qual & cv_lvalue ) {
1074
	CV_SPEC cv = cv_none ;
1075
	switch ( TAG_type ( t ) ) {
1076
	    case type_array_tag : {
1077
		/* Array-to-pointer conversion */
1078
		ERROR err = NULL_err ;
1079
		e = convert_array ( a, 0, &err ) ;
1080
		if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
1081
		break ;
1082
	    }
1083
	    case type_func_tag : {
1084
		/* Function-to-pointer conversion */
1085
		if ( is_overloaded ( a ) ) {
1086
		    e = make_error_exp ( 0 ) ;
1087
		    break ;
1088
		}
1089
		if ( IS_exp_member ( a ) ) goto default_lab ;
1090
		MAKE_type_ptr ( cv_none, t, t ) ;
1091
		MAKE_exp_address ( t, a, e ) ;
1092
		break ;
1093
	    }
1094
#if LANGUAGE_CPP
1095
	    case type_compound_tag : {
1096
		/* Classes preserve qualifiers in lvalue conversion */
1097
		cv = ( qual & cv_qual ) ;
1098
		goto default_lab ;
1099
	    }
1100
#endif
1101
	    case type_templ_tag : {
1102
		/* Can't have template expressions */
1103
		if ( !is_overloaded ( a ) ) {
1104
		    report ( crt_loc, ERR_temp_local_not ( t ) ) ;
1105
		}
1106
		e = make_error_exp ( 0 ) ;
1107
		break ;
1108
	    }
1109
	    default :
1110
	    default_lab : {
1111
		/* Lvalue-to-rvalue conversion */
1112
		ERROR err ;
1113
		if ( qual == ( cv_const | cv_lvalue ) ) {
1114
		    /* Check for constants at this stage */
1115
		    if ( IS_exp_identifier ( a ) ) {
1116
			e = convert_const ( a ) ;
1117
			if ( !EQ_exp ( e, a ) ) return ( e ) ;
1118
		    }
1119
		}
1120
		t = qualify_type ( t, cv, 0 ) ;
1121
		err = check_incomplete ( t ) ;
1122
		if ( !IS_NULL_err ( err ) ) {
1123
		    err = concat_error ( err, ERR_conv_lval_incompl () ) ;
1124
		    report ( crt_loc, err ) ;
1125
		}
1126
		MAKE_exp_contents ( t, a, e ) ;
1127
		break ;
1128
	    }
1129
	}
1130
 
1131
    } else {
1132
	/* Check rvalues for overloaded functions */
1133
	switch ( TAG_exp ( e ) ) {
1134
	    case exp_address_tag : {
1135
		/* Address of object or function */
1136
		EXP b = DEREF_exp ( exp_address_arg ( a ) ) ;
1137
		if ( is_overloaded ( b ) ) e = make_error_exp ( 0 ) ;
1138
		break ;
1139
	    }
1140
	    case exp_address_mem_tag : {
1141
		/* Address of member or member function */
1142
		EXP b = DEREF_exp ( exp_address_mem_arg ( a ) ) ;
1143
		if ( is_overloaded ( b ) ) e = make_error_exp ( 0 ) ;
1144
		break ;
1145
	    }
1146
	    case exp_token_tag : {
1147
		/* Check for tokenised arrays */
1148
		if ( IS_type_array ( t ) ) {
1149
		    ERROR err = NULL_err ;
1150
		    e = convert_array ( a, 0, &err ) ;
1151
		    if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
1152
		}
1153
		break ;
1154
	    }
1155
	}
1156
    }
1157
    return ( e ) ;
1158
}
1159
 
1160
 
1161
/*
1162
    CHECK AMBIGUOUS IDENTIFIERS
1163
 
1164
    This routine checks whether the identifier id represents a non-member
1165
    function or an ambiguous set of such functions.  It is required because
1166
    overload resolution is used to distinguish between the ambiguous cases
1167
    for non-member functions, whereas it is an error otherwise.
1168
*/
1169
 
1170
int is_ambiguous_func
1171
    PROTO_N ( ( id ) )
1172
    PROTO_T ( IDENTIFIER id )
1173
{
1174
    switch ( TAG_id ( id ) ) {
1175
	case id_ambig_tag : {
1176
	    /* Deal with ambiguous identifiers */
1177
	    LIST ( IDENTIFIER ) p = DEREF_list ( id_ambig_ids ( id ) ) ;
1178
	    while ( !IS_NULL_list ( p ) ) {
1179
		id = DEREF_id ( HEAD_list ( p ) ) ;
1180
		if ( !is_ambiguous_func ( id ) ) return ( 0 ) ;
1181
		p = TAIL_list ( p ) ;
1182
	    }
1183
	    return ( 1 ) ;
1184
	}
1185
	case id_function_tag : {
1186
	    /* These are functions */
1187
	    return ( 1 ) ;
1188
	}
1189
    }
1190
    return ( 0 ) ;
1191
}
1192
 
1193
 
1194
/*
1195
    PERFORM REFERENCE CONVERSIONS
1196
 
1197
    This routine performs the reference conversions on the expression a.
1198
    That is, if a has type reference to t, then it is transformed into an
1199
    lvalue of type t.  Other checks are also applied to a - for example,
1200
    parentheses are removed, undeclared variables are reported, and
1201
    constants are evaluated.  The precise checks applied depend on context
1202
    which can take one of the values from convert.h.
1203
*/
1204
 
1205
EXP convert_reference
1206
    PROTO_N ( ( a, context ) )
1207
    PROTO_T ( EXP a X int context )
1208
{
1209
    /* Remove parentheses */
1210
    TYPE t ;
1211
    unsigned etag = TAG_exp ( a ) ;
1212
    if ( etag == exp_paren_tag ) {
1213
	do {
1214
	    DESTROY_exp_paren ( destroy, t, a, a ) ;
1215
	    etag = TAG_exp ( a ) ;
1216
	} while ( etag == exp_paren_tag ) ;
1217
    } else {
1218
	t = DEREF_type ( exp_type ( a ) ) ;
1219
    }
1220
 
1221
    /* Apply extra checks */
1222
    switch ( etag ) {
1223
 
1224
	case exp_member_tag : {
1225
	    /* Non-static data members and all function members */
1226
	    if ( context == REF_ADDRESS ) {
1227
		/* Suppress reference conversions */
1228
		t = type_error ;
1229
	    } else {
1230
		IDENTIFIER id = DEREF_id ( exp_member_id ( a ) ) ;
1231
		if ( context == REF_NORMAL || IS_id_member ( id ) ) {
1232
		    EXP b = make_this_field ( id ) ;
1233
		    if ( IS_NULL_exp ( b ) ) {
1234
			report ( crt_loc, ERR_expr_prim_mem ( id ) ) ;
1235
			a = make_error_exp ( 0 ) ;
1236
		    } else {
1237
			a = b ;
1238
			if ( IS_exp_call ( a ) ) goto call_lab ;
1239
		    }
1240
		    t = DEREF_type ( exp_type ( a ) ) ;
1241
		    etag = TAG_exp ( a ) ;
1242
		}
1243
	    }
1244
	    break ;
1245
	}
1246
 
1247
	case exp_ambiguous_tag : {
1248
	    /* Ambiguous identifiers */
1249
	    IDENTIFIER id = DEREF_id ( exp_ambiguous_id ( a ) ) ;
1250
	    if ( context == REF_NORMAL || !is_ambiguous_func ( id ) ) {
1251
		/* Report ambiguous identifier */
1252
		IGNORE report_ambiguous ( id, 0, 1, 0 ) ;
1253
		a = make_error_exp ( 0 ) ;
1254
		t = DEREF_type ( exp_type ( a ) ) ;
1255
	    } else {
1256
		/* Allow ambiguous functions */
1257
		t = type_func_void ;
1258
		t = lvalue_type ( t ) ;
1259
		COPY_type ( exp_type ( a ), t ) ;
1260
	    }
1261
	    break ;
1262
	}
1263
 
1264
	case exp_undeclared_tag : {
1265
	    /* Undeclared identifiers */
1266
	    if ( context == REF_FUNCTION || context == REF_ADDRESS ) {
1267
		/* Deal with undeclared functions later */
1268
		t = type_func_void ;
1269
		t = lvalue_type ( t ) ;
1270
		COPY_type ( exp_type ( a ), t ) ;
1271
	    } else {
1272
		/* Report undeclared identifiers */
1273
		IDENTIFIER id = DEREF_id ( exp_undeclared_id ( a ) ) ;
1274
		crt_id_qualifier = DEREF_qual ( exp_undeclared_qual ( a ) ) ;
1275
		a = implicit_id_exp ( id, 0 ) ;
1276
		t = DEREF_type ( exp_type ( a ) ) ;
1277
	    }
1278
	    break ;
1279
	}
1280
 
1281
	case exp_address_tag : {
1282
	    /* Address of object */
1283
	    EXP b = DEREF_exp ( exp_address_arg ( a ) ) ;
1284
	    unsigned btag = TAG_exp ( b ) ;
1285
	    if ( btag == exp_ambiguous_tag ) {
1286
		if ( context != REF_FUNCTION && context != REF_ASSIGN ) {
1287
		    /* Ambiguous function */
1288
		    b = convert_reference ( b, REF_NORMAL ) ;
1289
		    a = make_ref_exp ( b, 1 ) ;
1290
		    t = DEREF_type ( exp_type ( a ) ) ;
1291
		}
1292
	    } else if ( btag == exp_undeclared_tag ) {
1293
		if ( context != REF_FUNCTION ) {
1294
		    /* Undeclared function */
1295
		    b = convert_reference ( b, REF_NORMAL ) ;
1296
		    a = make_ref_exp ( b, 1 ) ;
1297
		    t = DEREF_type ( exp_type ( a ) ) ;
1298
		}
1299
	    } else if ( btag == exp_call_tag ) {
1300
		if ( context != REF_ASSIGN ) {
1301
		    b = DEREF_exp ( exp_call_ptr ( b ) ) ;
1302
		    if ( IS_exp_member ( b ) ) {
1303
			/* Member function selector */
1304
			if ( !is_overloaded ( b ) ) {
1305
			    report ( crt_loc, ERR_expr_ref_call () ) ;
1306
			}
1307
			a = make_error_exp ( 0 ) ;
1308
			t = DEREF_type ( exp_type ( a ) ) ;
1309
		    }
1310
		}
1311
	    }
1312
	    break ;
1313
	}
1314
 
1315
	case exp_call_tag :
1316
	call_lab : {
1317
	    /* All member function calls */
1318
	    if ( context != REF_FUNCTION ) {
1319
		EXP b = DEREF_exp ( exp_call_ptr ( a ) ) ;
1320
		unsigned btag = TAG_exp ( b ) ;
1321
		if ( btag == exp_identifier_tag ) {
1322
		    /* Single static member function */
1323
		    IDENTIFIER id = DEREF_id ( exp_identifier_id ( b ) ) ;
1324
		    use_id ( id, suppress_usage ) ;
1325
		    a = DEREF_exp ( exp_call_arg ( a ) ) ;
1326
		    a = join_exp ( a, b ) ;
1327
		    t = DEREF_type ( exp_type ( a ) ) ;
1328
		    break ;
1329
		}
1330
		if ( btag == exp_member_tag ) {
1331
		    /* Other member functions */
1332
		    if ( context != REF_NORMAL ) break ;
1333
		    if ( !is_overloaded ( b ) ) {
1334
			report ( crt_loc, ERR_expr_ref_call () ) ;
1335
		    }
1336
		} else {
1337
		    /* Pointer to member functions */
1338
		    report ( crt_loc, ERR_expr_mptr_oper_call () ) ;
1339
		}
1340
		a = make_error_exp ( 0 ) ;
1341
		t = DEREF_type ( exp_type ( a ) ) ;
1342
	    }
1343
	    break ;
1344
	}
1345
    }
1346
 
1347
    /* Check for reference conversions */
1348
    if ( IS_type_ref ( t ) ) {
1349
	/* Reference to t becomes lvalue t */
1350
	if ( etag == exp_indir_tag ) {
1351
	    /* Can't have two indirections in a row */
1352
	    MAKE_exp_contents ( t, a, a ) ;
1353
	}
1354
	t = DEREF_type ( type_ref_sub ( t ) ) ;
1355
	/* Note that t is already an lvalue */
1356
	MAKE_exp_indir ( t, a, a ) ;
1357
    }
1358
    return ( a ) ;
1359
}
1360
 
1361
 
1362
/*
1363
    PROMOTE BITFIELD EXPRESSIONS
1364
 
1365
    In certain expressions, even though an integral operand is not subject
1366
    to integer promotion, bitfield expressions need to be converted to
1367
    integral expressions by promotion.  This routine performs this conversion
1368
    for the expression a.
1369
*/
1370
 
1371
EXP convert_bitfield
1372
    PROTO_N ( ( a ) )
1373
    PROTO_T ( EXP a )
1374
{
1375
    TYPE t = DEREF_type ( exp_type ( a ) ) ;
1376
    if ( IS_type_bitfield ( t ) ) {
1377
	t = promote_type ( t ) ;
1378
	a = convert_promote ( t, a ) ;
1379
    }
1380
    return ( a ) ;
1381
}
1382
 
1383
 
1384
/*
1385
    CONVERT OPERAND WHERE NO CONTEXT IS GIVEN
1386
 
1387
    This routine performs conversions on the operand a in contexts where
1388
    there is no information to resolve overloaded functions etc.  It also
1389
    introduces temporary variables for constructor call expressions.
1390
*/
1391
 
1392
EXP convert_none
1393
    PROTO_N ( ( a ) )
1394
    PROTO_T ( EXP a )
1395
{
1396
    if ( !IS_NULL_exp ( a ) ) {
1397
	ERROR err = NULL_err ;
1398
	if ( IS_exp_constr ( a ) ) {
1399
	    TYPE t = DEREF_type ( exp_type ( a ) ) ;
1400
	    a = make_temporary ( t, a, NULL_exp, 0, &err ) ;
1401
	    a = convert_lvalue ( a ) ;
1402
	} else {
1403
	    LIST ( IDENTIFIER ) pids = NULL_list ( IDENTIFIER ) ;
1404
	    a = resolve_cast ( type_void, a, &err, 1, 0, pids ) ;
1405
	}
1406
	if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
1407
    }
1408
    return ( a ) ;
1409
}
1410
 
1411
 
1412
/*
1413
    QUALIFICATION CONVERSIONS
1414
 
1415
    The values cv_const and cv_volatile are used to represent qualification
1416
    conversions which add the corresponding qualifiers at a single level.
1417
    cv_strlit is used to represent the removal of const from a string
1418
    literal.  cv_multi is used to represent qualifications which add
1419
    qualifiers at more than one level.
1420
*/
1421
 
1422
#define cv_strlit	( ( CV_SPEC ) 0x10 )
1423
#define cv_multi	( ( CV_SPEC ) 0x20 )
1424
 
1425
 
1426
/*
1427
    CHECK FOR OVERLOADED FUNCTION CONVERSION SEQUENCES
1428
 
1429
    This routine checks for conversion sequences between the overloaded
1430
    function id and the pointer or pointer to member (of the correct class)
1431
    type t.
1432
*/
1433
 
1434
static unsigned overload_convert_seq
1435
    PROTO_N ( ( t, id, p ) )
1436
    PROTO_T ( TYPE t X IDENTIFIER id X CONVERSION *p )
1437
{
1438
    /* Check arguments */
1439
    TYPE fn ;
1440
    int eq = 0 ;
1441
    CV_SPEC cv ;
1442
    unsigned conv = CONV_EXACT ;
1443
    unsigned tag = TAG_type ( t ) ;
1444
    if ( tag == type_ptr_tag ) {
1445
	fn = DEREF_type ( type_ptr_sub ( t ) ) ;
1446
    } else {
1447
	fn = DEREF_type ( type_ptr_mem_sub ( t ) ) ;
1448
    }
1449
    if ( !IS_type_func ( fn ) ) return ( CONV_NONE ) ;
1450
    cv = DEREF_cv ( type_qual ( fn ) ) ;
1451
    cv &= cv_qual ;
1452
    if ( cv != cv_none ) conv = CONV_QUAL ;
1453
    p->qual = cv ;
1454
 
1455
    /* Check for matching overload function */
1456
    id = resolve_func ( id, fn, 1, 0, NULL_list ( IDENTIFIER ), &eq ) ;
1457
    if ( !IS_NULL_id ( id ) && eq == 3 ) {
1458
	switch ( TAG_id ( id ) ) {
1459
	    case id_mem_func_tag : {
1460
		/* A member function gives a pointer to member */
1461
		if ( tag == type_ptr_mem_tag ) return ( conv ) ;
1462
		break ;
1463
	    }
1464
	    case id_function_tag :
1465
	    case id_stat_mem_func_tag : {
1466
		/* Other functions give pointers */
1467
		if ( tag == type_ptr_tag ) return ( conv ) ;
1468
		break ;
1469
	    }
1470
	}
1471
    }
1472
    return ( CONV_NONE ) ;
1473
}
1474
 
1475
 
1476
/*
1477
    CHECK FOR REFERENCE CONVERSION SEQUENCES
1478
 
1479
    This routine checks the conversion sequence p, the destination type
1480
    of which is a reference type.  If std is true then only standard
1481
    conversions will be applied.  bind describes the form of reference
1482
    binding to take place.  The normal value is 0, implements the normal
1483
    reference binding rules.  A value of 1 is used to suppress all
1484
    rvalue reference bindings.  Values of 2 and 3 allow rvalues of
1485
    related types to be bound to any reference (not just const references),
1486
    with 2 further regarding any base class conversions as exact matches.
1487
*/
1488
 
1489
static unsigned ref_convert_seq
1490
    PROTO_N ( ( p, e, bind, std ) )
1491
    PROTO_T ( CONVERSION *p X EXP e X int bind X int std )
1492
{
1493
    unsigned conv ;
1494
    TYPE t = DEREF_type ( type_ref_sub ( p->to ) ) ;
1495
    TYPE s = p->from ;
1496
    unsigned nt = TAG_type ( t ) ;
1497
    unsigned ns = TAG_type ( s ) ;
1498
    CV_SPEC qs = DEREF_cv ( type_qual ( s ) ) ;
1499
    if ( qs & cv_lvalue ) {
1500
	CV_SPEC cv = cv_compare ( t, s ) ;
1501
	if ( cv == cv_none ) {
1502
	    /* Qualifiers are alright */
1503
	    cv = cv_compare ( s, t ) ;
1504
	    p->qual = cv ;
1505
	    if ( eq_type_unqual ( s, t ) ) {
1506
		if ( cv == cv_none ) {
1507
		    conv = CONV_EXACT ;
1508
		} else {
1509
		    conv = CONV_QUAL ;
1510
		}
1511
		return ( conv ) ;
1512
	    }
1513
	    if ( ns == type_compound_tag && nt == type_compound_tag ) {
1514
		CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
1515
		CLASS_TYPE cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
1516
		if ( eq_ctype ( ct, cs ) ) {
1517
		    /* Classes match */
1518
		    if ( cv == cv_none ) {
1519
			conv = CONV_EXACT ;
1520
		    } else {
1521
			conv = CONV_QUAL ;
1522
		    }
1523
		    return ( conv ) ;
1524
		} else {
1525
		    /* Check for base classes */
1526
		    GRAPH gr = find_base_class ( cs, ct, 0 ) ;
1527
		    if ( !IS_NULL_graph ( gr ) ) {
1528
			/* Base class conversion */
1529
			p->base = gr ;
1530
			if ( bind == 2 ) {
1531
			    /* Base class conversions match exactly */
1532
			    if ( cv == cv_none ) {
1533
				conv = CONV_EXACT ;
1534
			    } else {
1535
				conv = CONV_QUAL ;
1536
			    }
1537
			} else {
1538
			    conv = CONV_BASE ;
1539
			}
1540
			return ( conv ) ;
1541
		    }
1542
		}
1543
	    }
1544
	}
1545
    }
1546
    if ( bind == 0 ) {
1547
	/* Default - only const references allowed */
1548
	CV_SPEC qt = find_cv_qual ( t ) ;
1549
	if ( qt != ( cv_lvalue | cv_const ) ) return ( CONV_NONE ) ;
1550
	qs = find_cv_qual ( s ) ;
1551
	if ( qs & cv_volatile ) return ( CONV_NONE ) ;
1552
    } else if ( bind == 1 ) {
1553
	/* No references allowed */
1554
	return ( CONV_NONE ) ;
1555
    } else {
1556
	/* All references allowed */
1557
	std = 1 ;
1558
    }
1559
    p->to = t ;
1560
    p->from = s ;
1561
    if ( std ) {
1562
	conv = std_convert_seq ( p, e, bind, 1 ) ;
1563
    } else {
1564
	conv = convert_seq ( p, e, bind, 1 ) ;
1565
    }
1566
    if ( bind == 2 ) {
1567
	/* Base class conversions match exactly */
1568
	if ( conv == CONV_BASE ) {
1569
	    if ( p->qual == cv_none ) {
1570
		conv = CONV_EXACT ;
1571
	    } else {
1572
		conv = CONV_QUAL ;
1573
	    }
1574
	}
1575
    }
1576
    return ( conv ) ;
1577
}
1578
 
1579
 
1580
/*
1581
    CHECK FOR STANDARD CONVERSION SEQUENCES
1582
 
1583
    This routine checks whether there is a standard conversion sequence
1584
    between the types given by p.  e gives the argument being converted
1585
    (this is only used in identifying null pointer and null pointer member
1586
    conversions).  The arguments bind and ref describe how reference bindings
1587
    are to be treated (see above).  Note that conversion are applied in
1588
    the canonical sequence, lvalue transformations, promotions, conversions
1589
    and qualification adjustments.  The routine returns the value indicating
1590
    the rank of this conversion.
1591
*/
1592
 
1593
unsigned std_convert_seq
1594
    PROTO_N ( ( p, e, bind, ref ) )
1595
    PROTO_T ( CONVERSION *p X EXP e X int bind X int ref )
1596
{
1597
    CV_SPEC qs ;
1598
    int str = 0 ;
1599
    unsigned etag ;
1600
    TYPE t = p->to ;
1601
    TYPE s = p->from ;
1602
    unsigned nt, ns ;
1603
    unsigned conv = CONV_NONE ;
1604
 
1605
    /* Conversion to the null type counts as exact */
1606
    if ( IS_NULL_type ( t ) ) return ( CONV_EXACT ) ;
1607
 
1608
    /* Conversion from the error type counts as ellipsis */
1609
    ns = TAG_type ( s ) ;
1610
    if ( ns == type_error_tag ) return ( CONV_ELLIPSIS ) ;
1611
    qs = DEREF_cv ( type_qual ( s ) ) ;
1612
 
1613
    /* Reference conversion */
1614
    if ( ns == type_ref_tag ) {
1615
	s = DEREF_type ( type_ref_sub ( s ) ) ;
1616
	p->from = s ;
1617
	ns = TAG_type ( s ) ;
1618
	if ( ns == type_error_tag ) return ( CONV_ELLIPSIS ) ;
1619
	qs = DEREF_cv ( type_qual ( s ) ) ;
1620
    }
1621
 
1622
    /* Deal with conversions to reference types */
1623
    nt = TAG_type ( t ) ;
1624
    if ( nt == type_ref_tag ) {
1625
	conv = ref_convert_seq ( p, e, bind, 1 ) ;
1626
	return ( conv ) ;
1627
    }
1628
 
1629
    /* Examine expression */
1630
    etag = ( IS_NULL_exp ( e ) ? null_tag : TAG_exp ( e ) ) ;
1631
 
1632
    /* Lvalue transformations */
1633
    if ( qs & cv_lvalue ) {
1634
	if ( ns == type_func_tag ) {
1635
	    /* Function to pointer conversion */
1636
	    if ( etag != exp_member_tag ) {
1637
		TYPE ps = s ;
1638
		s = type_temp_star ;
1639
		COPY_type ( type_ptr_sub ( s ), ps ) ;
1640
		ns = type_ptr_tag ;
1641
	    }
1642
	} else if ( ns == type_array_tag ) {
1643
	    /* Array to pointer conversion */
1644
	    TYPE ps = DEREF_type ( type_array_sub ( s ) ) ;
1645
	    s = type_temp_star ;
1646
	    COPY_type ( type_ptr_sub ( s ), ps ) ;
1647
	    ns = type_ptr_tag ;
1648
	} else {
1649
	    /* lvalue to rvalue conversion */
1650
	    if ( etag == exp_identifier_tag ) {
1651
		if ( qs == ( cv_lvalue | cv_const ) ) {
1652
		    IDENTIFIER id = DEREF_id ( exp_identifier_id ( e ) ) ;
1653
		    EXP d = DEREF_exp ( id_variable_etc_init ( id ) ) ;
1654
		    if ( is_zero_exp ( d ) ) {
1655
			/* Propagate zero constants */
1656
			e = d ;
1657
		    }
1658
		}
1659
	    }
1660
	}
1661
    }
1662
 
1663
    /* Promotions and conversions */
1664
    switch ( ns ) {
1665
 
1666
	case type_integer_tag :
1667
	case type_enumerate_tag :
1668
	integral_lab : {
1669
	    /* Integral and similar arguments */
1670
	    if ( nt == ns && eq_type_unqual ( t, s ) ) {
1671
		/* Exact match */
1672
		conv = CONV_EXACT ;
1673
	    } else {
1674
		TYPE ps = promote_type ( s ) ;
1675
		if ( !EQ_type ( ps, s ) && eq_type_unqual ( t, ps ) ) {
1676
		    /* Integral promotion */
1677
		    conv = CONV_INT_PROM ;
1678
		} else if ( nt == type_integer_tag ) {
1679
		    /* Integral conversions (subsumes booleans) */
1680
		    conv = CONV_INT_INT ;
1681
		} else if ( nt == type_floating_tag ) {
1682
		    /* Floating-integer conversions */
1683
		    conv = CONV_INT_FLT ;
1684
		} else if ( is_zero_exp ( e ) ) {
1685
		    if ( nt == type_ptr_tag ) {
1686
			/* Null pointers */
1687
			conv = CONV_PTR_NULL ;
1688
		    } else if ( nt == type_ptr_mem_tag ) {
1689
			/* Null pointer members */
1690
			conv = CONV_PTR_MEM_NULL ;
1691
		    }
1692
		}
1693
	    }
1694
	    break ;
1695
	}
1696
 
1697
	case type_bitfield_tag : {
1698
	    /* Ignore any bitfield qualifiers */
1699
	    s = find_bitfield_type ( s ) ;
1700
	    ns = TAG_type ( s ) ;
1701
	    goto integral_lab ;
1702
	}
1703
 
1704
	case type_floating_tag : {
1705
	    /* Floating point arguments */
1706
	    if ( nt == type_floating_tag ) {
1707
		if ( eq_type_unqual ( t, s ) ) {
1708
		    /* Exact match */
1709
		    conv = CONV_EXACT ;
1710
		} else {
1711
		    TYPE ps = arg_promote_type ( s, KILL_err ) ;
1712
		    if ( !EQ_type ( ps, s ) && eq_type_unqual ( t, ps ) ) {
1713
			/* Floating point promotion */
1714
			conv = CONV_FLT_PROM ;
1715
		    } else {
1716
			/* Floating point conversions */
1717
			conv = CONV_FLT_FLT ;
1718
		    }
1719
		}
1720
	    } else if ( nt == type_integer_tag ) {
1721
		/* Floating-integer conversions (subsumes booleans) */
1722
		conv = CONV_FLT_INT ;
1723
	    }
1724
	    break ;
1725
	}
1726
 
1727
	case type_ptr_tag :
1728
	pointer_lab : {
1729
	    /* Pointer arguments */
1730
	    if ( nt == type_ptr_tag ) {
1731
		/* Check for qualifier conversions */
1732
		unsigned qual = check_qualifier ( t, s, 0 ) ;
1733
		if ( qualifier_depth <= 1 ) {
1734
		    CV_SPEC cv = qualifier_diff ;
1735
		    if ( str ) cv |= cv_strlit ;
1736
		    p->qual = cv ;
1737
		} else {
1738
		    p->qual = cv_multi ;
1739
		}
1740
		if ( qual == QUAL_EQUAL || qual == QUAL_EQ_FUNC ) {
1741
		    /* Exact match */
1742
		    conv = CONV_EXACT ;
1743
		    if ( str ) conv = CONV_STRING ;
1744
		} else if ( qual == QUAL_OK ) {
1745
		    /* Qualification conversion */
1746
		    conv = CONV_QUAL ;
1747
		    if ( str ) conv = CONV_STRING ;
1748
		} else if ( qual == QUAL_CV ) {
1749
		    /* Conversion preserves cv-qualifiers */
1750
		    TYPE pt = DEREF_type ( type_ptr_sub ( t ) ) ;
1751
		    TYPE ps = DEREF_type ( type_ptr_sub ( s ) ) ;
1752
		    nt = TAG_type ( pt ) ;
1753
		    ns = TAG_type ( ps ) ;
1754
		    if ( nt == type_compound_tag && ns == nt ) {
1755
			/* Pointer base class conversions */
1756
			GRAPH gr ;
1757
			CLASS_TYPE ct, cs ;
1758
			ct = DEREF_ctype ( type_compound_defn ( pt ) ) ;
1759
			cs = DEREF_ctype ( type_compound_defn ( ps ) ) ;
1760
			gr = find_base_class ( cs, ct, 0 ) ;
1761
			if ( !IS_NULL_graph ( gr ) ) {
1762
			    /* Don't worry about ambiguity */
1763
			    p->base = gr ;
1764
			    conv = CONV_PTR_BASE ;
1765
			}
1766
		    } else if ( nt == type_top_tag ) {
1767
			if ( ns != type_func_tag ) {
1768
			    /* Pointer to 'void *' conversions */
1769
			    conv = CONV_PTR_VOID ;
1770
			}
1771
		    } else if ( nt == type_bottom_tag ) {
1772
			if ( ns != type_func_tag ) {
1773
			    /* Pointer to 'void *' conversions */
1774
			    conv = CONV_PTR_BOTTOM ;
1775
			}
1776
		    }
1777
		}
1778
		if ( conv == CONV_NONE ) {
1779
		    /* Check for string literals */
1780
		    if ( etag == exp_string_lit_tag && !str ) {
1781
			if ( !( qual & QUAL_CONST ) ) {
1782
			    TYPE ps = DEREF_type ( type_ptr_sub ( s ) ) ;
1783
			    ps = qualify_type ( ps, cv_none, 0 ) ;
1784
			    s = type_temp_star ;
1785
			    COPY_type ( type_ptr_sub ( s ), ps ) ;
1786
			    str = 1 ;
1787
			    goto pointer_lab ;
1788
			}
1789
		    }
1790
 
1791
		    /* Check for overloaded functions */
1792
		    if ( etag == exp_address_tag ) {
1793
			e = DEREF_exp ( exp_address_arg ( e ) ) ;
1794
			etag = TAG_exp ( e ) ;
1795
		    }
1796
		    if ( etag == exp_identifier_tag ) {
1797
			IDENTIFIER id = DEREF_id ( exp_identifier_id ( e ) ) ;
1798
			conv = overload_convert_seq ( t, id, p ) ;
1799
		    } else if ( etag == exp_ambiguous_tag ) {
1800
			IDENTIFIER id = DEREF_id ( exp_ambiguous_id ( e ) ) ;
1801
			conv = overload_convert_seq ( t, id, p ) ;
1802
		    }
1803
		}
1804
	    } else if ( nt == type_integer_tag ) {
1805
		if ( check_int_type ( t, btype_bool ) ) {
1806
		    /* Boolean conversions */
1807
		    conv = CONV_BOOL ;
1808
		}
1809
	    }
1810
	    break ;
1811
	}
1812
 
1813
#if LANGUAGE_CPP
1814
	case type_ptr_mem_tag : {
1815
	    /* Pointer to member arguments */
1816
	    if ( nt == type_ptr_mem_tag ) {
1817
		unsigned qual ;
1818
		int ctype_match = 0 ;
1819
		CLASS_TYPE ct = DEREF_ctype ( type_ptr_mem_of ( t ) ) ;
1820
		CLASS_TYPE cs = DEREF_ctype ( type_ptr_mem_of ( s ) ) ;
1821
		if ( eq_ctype ( ct, cs ) ) {
1822
		    ctype_match = 2 ;
1823
		} else {
1824
		    GRAPH gr = find_base_class ( ct, cs, 0 ) ;
1825
		    if ( !IS_NULL_graph ( gr ) ) {
1826
			ctype_match = 1 ;
1827
			p->base = gr ;
1828
		    }
1829
		}
1830
		if ( ctype_match ) {
1831
		    if ( etag == exp_address_mem_tag ) {
1832
			/* Check overloaded functions */
1833
			IDENTIFIER id ;
1834
			e = DEREF_exp ( exp_address_mem_arg ( e ) ) ;
1835
			id = DEREF_id ( exp_member_id ( e ) ) ;
1836
			if ( IS_id_function_etc ( id ) ) {
1837
			    conv = overload_convert_seq ( t, id, p ) ;
1838
			    if ( conv != CONV_NONE && ctype_match == 1 ) {
1839
				conv = CONV_PTR_MEM_BASE ;
1840
			    }
1841
			    break ;
1842
			}
1843
		    }
1844
		    /* Check other cases */
1845
		    qual = check_qualifier ( t, s, 0 ) ;
1846
		    if ( qualifier_depth <= 1 ) {
1847
			p->qual = qualifier_diff ;
1848
		    } else {
1849
			p->qual = cv_multi ;
1850
		    }
1851
		    if ( qual == QUAL_EQUAL || qual == QUAL_EQ_FUNC ) {
1852
			/* Exact match */
1853
			if ( ctype_match == 2 ) {
1854
			    conv = CONV_EXACT ;
1855
			} else {
1856
			    conv = CONV_PTR_MEM_BASE ;
1857
			}
1858
		    } else if ( qual == QUAL_OK ) {
1859
			/* Qualification conversion */
1860
			if ( ctype_match == 2 ) {
1861
			    conv = CONV_QUAL ;
1862
			} else {
1863
			    conv = CONV_PTR_MEM_BASE ;
1864
			}
1865
		    }
1866
		}
1867
	    } else if ( nt == type_integer_tag ) {
1868
		if ( check_int_type ( t, btype_bool ) ) {
1869
		    /* Boolean conversions */
1870
		    conv = CONV_BOOL ;
1871
		}
1872
	    } else if ( nt == type_ptr_tag ) {
1873
		if ( etag == exp_address_mem_tag ) {
1874
		    /* Check overloaded functions */
1875
		    IDENTIFIER id ;
1876
		    e = DEREF_exp ( exp_address_mem_arg ( e ) ) ;
1877
		    id = DEREF_id ( exp_member_id ( e ) ) ;
1878
		    conv = overload_convert_seq ( t, id, p ) ;
1879
		}
1880
	    }
1881
	    break ;
1882
	}
1883
#endif
1884
 
1885
	case type_compound_tag : {
1886
	    /* Class arguments */
1887
	    if ( nt == type_compound_tag ) {
1888
		CV_SPEC cv = cv_compare ( t, s ) ;
1889
		if ( cv == cv_none || !ref ) {
1890
		    CLASS_TYPE ct, cs ;
1891
		    cv = cv_compare ( s, t ) ;
1892
		    p->qual = cv ;
1893
		    ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
1894
		    cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
1895
		    if ( eq_ctype ( ct, cs ) ) {
1896
			/* Class types match */
1897
			if ( cv == cv_none ) {
1898
			    conv = CONV_EXACT ;
1899
			} else {
1900
			    conv = CONV_QUAL ;
1901
			}
1902
		    } else {
1903
			/* Examine base classes */
1904
			GRAPH gr = find_base_class ( cs, ct, 0 ) ;
1905
			if ( !IS_NULL_graph ( gr ) ) {
1906
			    /* Base class conversion */
1907
			    p->base = gr ;
1908
			    conv = CONV_BASE ;
1909
			}
1910
		    }
1911
		}
1912
	    }
1913
	    break ;
1914
	}
1915
 
1916
	case type_func_tag : {
1917
	    /* Address of overloaded static member function */
1918
	    if ( nt == type_ptr_tag && etag == exp_member_tag ) {
1919
		IDENTIFIER id = DEREF_id ( exp_member_id ( e ) ) ;
1920
		conv = overload_convert_seq ( t, id, p ) ;
1921
	    }
1922
	    break ;
1923
	}
1924
 
1925
	case type_token_tag : {
1926
	    /* Exact conversion on tokenised type */
1927
	    if ( nt == ns && eq_type_unqual ( t, s ) ) {
1928
		conv = CONV_EXACT ;
1929
	    }
1930
	    break ;
1931
	}
1932
    }
1933
    return ( conv ) ;
1934
}
1935
 
1936
 
1937
/*
1938
    CHECK FOR CONVERSION SEQUENCES
1939
 
1940
    This routine checks whether there is an implicit conversion sequence
1941
    corresponding to p.  e gives the argument being converted.  It returns
1942
    the value indicating the rank of this conversion.  This is used in
1943
    overload resolution to determine the best viable function.
1944
*/
1945
 
1946
unsigned convert_seq
1947
    PROTO_N ( ( p, e, bind, ref ) )
1948
    PROTO_T ( CONVERSION *p X EXP e X int bind X int ref )
1949
{
1950
    int match = 0 ;
1951
    unsigned conv ;
1952
    TYPE t = p->to ;
1953
    TYPE s = p->from ;
1954
    unsigned nt, ns ;
1955
    CONVERSION user, best ;
1956
    best.rank = CONV_NONE ;
1957
 
1958
    /* Conversion to the null type counts as exact */
1959
    if ( IS_NULL_type ( t ) ) return ( CONV_EXACT ) ;
1960
    nt = TAG_type ( t ) ;
1961
 
1962
    /* Conversion from the error type counts as ellipsis */
1963
    ns = TAG_type ( s ) ;
1964
    if ( ns == type_error_tag ) return ( CONV_ELLIPSIS ) ;
1965
 
1966
    /* Reference conversion */
1967
    if ( ns == type_ref_tag ) {
1968
	s = DEREF_type ( type_ref_sub ( s ) ) ;
1969
	ns = TAG_type ( s ) ;
1970
	if ( ns == type_error_tag ) return ( CONV_ELLIPSIS ) ;
1971
    }
1972
 
1973
    /* Conversion to class type */
1974
    if ( nt == type_compound_tag && bind != 1 ) {
1975
	IDENTIFIER id ;
1976
	CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
1977
	complete_class ( ct, 1 ) ;
1978
	id = DEREF_id ( ctype_constr ( ct ) ) ;
1979
	if ( ns == type_compound_tag ) {
1980
	    /* Check for base class conversion */
1981
	    conv = std_convert_seq ( p, e, bind, ref ) ;
1982
	    if ( conv != CONV_NONE ) return ( conv ) ;
1983
	    p->from = s ;
1984
	    p->to = t ;
1985
	}
1986
	user.from = s ;
1987
	if ( IS_id_function_etc ( id ) ) {
1988
	    while ( !IS_NULL_id ( id ) ) {
1989
		DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
1990
		if ( !( ds & dspec_explicit ) ) {
1991
		    LIST ( TYPE ) q ;
1992
		    TYPE fn = DEREF_type ( id_function_etc_type ( id ) ) ;
1993
		    while ( IS_type_templ ( fn ) ) {
1994
			fn = DEREF_type ( type_templ_defn ( fn ) ) ;
1995
		    }
1996
		    q = DEREF_list ( type_func_ptypes ( fn ) ) ;
1997
		    if ( IS_NULL_list ( q ) ) {
1998
			/* Match with ellipsis */
1999
			int ell = DEREF_int ( type_func_ellipsis ( fn ) ) ;
2000
			if ( ell ) {
2001
			    conv = CONV_ELLIPSIS ;
2002
			} else {
2003
			    conv = CONV_NONE ;
2004
			}
2005
		    } else {
2006
			/* Match with parameter */
2007
			user.to = DEREF_type ( HEAD_list ( q ) ) ;
2008
			if ( min_no_args ( fn ) <= 2 ) {
2009
			    conv = std_convert_seq ( &user, e, bind, 0 ) ;
2010
			} else {
2011
			    conv = CONV_NONE ;
2012
			}
2013
		    }
2014
		    if ( conv != CONV_NONE ) {
2015
			/* Compare against previous conversion */
2016
			if ( !match ) {
2017
			    user.rank = conv ;
2018
			    user.usr = id ;
2019
			    user.std = conv ;
2020
			    best = user ;
2021
			}
2022
			match++ ;
2023
		    }
2024
		}
2025
		id = DEREF_id ( id_function_etc_over ( id ) ) ;
2026
	    }
2027
	}
2028
    }
2029
 
2030
    /* Conversion from class type */
2031
    if ( ns == type_compound_tag && bind != 1 ) {
2032
	/* Check for user-defined conversions */
2033
	LIST ( IDENTIFIER ) convs ;
2034
	CLASS_TYPE cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
2035
	complete_class ( cs, 1 ) ;
2036
	convs = DEREF_list ( ctype_conv ( cs ) ) ;
2037
	if ( nt == type_ref_tag ) {
2038
	    /* Check for base class conversion */
2039
	    TYPE pt = DEREF_type ( type_ref_sub ( t ) ) ;
2040
	    if ( IS_type_compound ( pt ) ) {
2041
		conv = ref_convert_seq ( p, e, bind, 1 ) ;
2042
		if ( conv != CONV_NONE ) return ( conv ) ;
2043
		p->from = s ;
2044
		p->to = t ;
2045
	    }
2046
	}
2047
	user.to = t ;
2048
	while ( !IS_NULL_list ( convs ) ) {
2049
	    IDENTIFIER id = DEREF_id ( HEAD_list ( convs ) ) ;
2050
	    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
2051
	    if ( !( ds & dspec_explicit ) ) {
2052
		TYPE fn = DEREF_type ( id_function_etc_type ( id ) ) ;
2053
		if ( IS_type_templ ( fn ) ) {
2054
		    /* Allow for template functions */
2055
		    fn = deduce_conv ( fn, t ) ;
2056
		}
2057
		if ( !IS_NULL_type ( fn ) ) {
2058
		    TYPE r = DEREF_type ( type_func_ret ( fn ) ) ;
2059
		    user.from = r ;
2060
		    if ( eq_type ( r, t ) ) {
2061
			conv = CONV_EXACT ;
2062
		    } else {
2063
			conv = std_convert_seq ( &user, e, bind, 0 ) ;
2064
		    }
2065
		    if ( conv != CONV_NONE ) {
2066
			/* Compare against previous conversion */
2067
			if ( !match ) {
2068
			    user.rank = conv ;
2069
			    user.usr = id ;
2070
			    user.std = conv ;
2071
			    best = user ;
2072
			}
2073
			match++ ;
2074
		    }
2075
		}
2076
	    }
2077
	    convs = TAIL_list ( convs ) ;
2078
	}
2079
    }
2080
 
2081
    /* User-defined conversion sequences */
2082
    if ( match ) {
2083
	*p = best ;
2084
	if ( match == 1 ) return ( CONV_USER ) ;
2085
	return ( CONV_USER_MULTI ) ;
2086
    }
2087
 
2088
    /* Deal with conversions to reference types */
2089
    if ( nt == type_ref_tag ) {
2090
	conv = ref_convert_seq ( p, e, bind, 0 ) ;
2091
	return ( conv ) ;
2092
    }
2093
 
2094
    /* Standard conversion sequences */
2095
    conv = std_convert_seq ( p, e, bind, ref ) ;
2096
    return ( conv ) ;
2097
}
2098
 
2099
 
2100
/*
2101
    COMPARE TWO BASE CLASS CONVERSIONS
2102
 
2103
    This routine compares the base classes given by p and q.  It returns 1
2104
    if p is a proper subgraph of q, 2 if q is a proper subgraph of p and
2105
 
2106
*/
2107
 
2108
static int base_compare_seq
2109
    PROTO_N ( ( p, q ) )
2110
    PROTO_T ( GRAPH p X GRAPH q )
2111
{
2112
    CLASS_TYPE ct ;
2113
    CLASS_TYPE pa, pb ;
2114
    CLASS_TYPE qa, qb ;
2115
 
2116
    /* Decompose p into pa > pb */
2117
    pb = DEREF_ctype ( graph_head ( p ) ) ;
2118
    p = DEREF_graph ( graph_top ( p ) ) ;
2119
    pa = DEREF_ctype ( graph_head ( p ) ) ;
2120
 
2121
    /* Decompose q into qa > qb */
2122
    qb = DEREF_ctype ( graph_head ( q ) ) ;
2123
    q = DEREF_graph ( graph_top ( q ) ) ;
2124
    qa = DEREF_ctype ( graph_head ( q ) ) ;
2125
 
2126
    if ( eq_ctype ( pa, qa ) ) {
2127
	/* Graph tops are equal, pa = qa */
2128
	if ( eq_ctype ( pb, qb ) ) {
2129
	    /* Graphs are equal */
2130
	    return ( 0 ) ;
2131
	}
2132
	ct = compare_base_class ( pb, qb, 0 ) ;
2133
	if ( EQ_ctype ( ct, pb ) ) {
2134
	    /* pa = qa > qb > pb */
2135
	    return ( 2 ) ;
2136
	}
2137
	if ( EQ_ctype ( ct, qb ) ) {
2138
	    /* pa = qa > pb > qb */
2139
	    return ( 1 ) ;
2140
	}
2141
    } else if ( eq_ctype ( pb, qb ) ) {
2142
	/* Graph bottoms are equal, pb = qb */
2143
	ct = compare_base_class ( pa, qa, 0 ) ;
2144
	if ( EQ_ctype ( ct, pa ) ) {
2145
	    /* qa > pa > pb = qb */
2146
	    return ( 1 ) ;
2147
	}
2148
	if ( EQ_ctype ( ct, qa ) ) {
2149
	    /* pa > qa > pb = qb */
2150
	    return ( 2 ) ;
2151
	}
2152
    }
2153
    return ( 0 ) ;
2154
}
2155
 
2156
 
2157
/*
2158
    COMPARE TWO QUALIFICATION CONVERSIONS
2159
 
2160
    This routine compares the qualification conversions given by p and q.
2161
    It returns 3 if they are identical, 1 if p is better (in that every
2162
    qualifier added by p is also added by q), 2 if q is better, and 0
2163
    otherwise.  When qualifiers are added at only one level it is just
2164
    a matter of comparing the added qualifiers.  If qualifiers are added
2165
    at more than one level a trial conversion is carried out.
2166
*/
2167
 
2168
static int qual_compare_seq
2169
    PROTO_N ( ( p, q ) )
2170
    PROTO_T ( CONVERSION *p X CONVERSION *q )
2171
{
2172
    CV_SPEC cp = p->qual ;
2173
    CV_SPEC cq = q->qual ;
2174
    if ( cp == cv_multi || cq == cv_multi ) {
2175
	/* Qualifiers at more than one level */
2176
	TYPE t ;
2177
	unsigned cmp ;
2178
	CONVERSION r ;
2179
	if ( EQ_type ( p->from, q->from ) ) {
2180
	    /* Compare to-types */
2181
	    r.from = p->to ;
2182
	    r.to = q->to ;
2183
	} else if ( EQ_type ( p->to, q->to ) ) {
2184
	    /* Compare from-types */
2185
	    r.from = q->from ;
2186
	    r.to = p->from ;
2187
	} else {
2188
	    /* This shouldn't happen */
2189
	    return ( 0 ) ;
2190
	}
2191
	cmp = std_convert_seq ( &r, NULL_exp, 0, 0 ) ;
2192
	if ( cmp == CONV_EXACT ) return ( 3 ) ;
2193
	if ( cmp != CONV_NONE ) return ( 1 ) ;
2194
	t = r.from ;
2195
	r.from = r.to ;
2196
	r.to = t ;
2197
	cmp = std_convert_seq ( &r, NULL_exp, 0, 0 ) ;
2198
	if ( cmp != CONV_NONE ) return ( 2 ) ;
2199
    } else {
2200
	/* Qualifiers at only one level */
2201
	CV_SPEC cr ;
2202
	if ( cp == cq ) return ( 3 ) ;
2203
	cr = ( cp | cq ) ;
2204
	if ( cr == cq ) return ( 1 ) ;
2205
	if ( cr == cp ) return ( 2 ) ;
2206
    }
2207
    return ( 0 ) ;
2208
}
2209
 
2210
 
2211
/*
2212
    COMPARE TWO CONVERSION SEQUENCES
2213
 
2214
    This routine compares the implicit conversion sequences given by
2215
    p1 and p2.  In all cases either the from types of the two conversions
2216
    or the to types will be equal.  The routine is used in determining
2217
    the best viable function in overload resolution, it returns 1 if the
2218
    first conversion is better, 2 if second is better, and some other
2219
    value otherwise.
2220
*/
2221
 
2222
int compare_seq
2223
    PROTO_N ( ( p1, p2 ) )
2224
    PROTO_T ( CONVERSION *p1 X CONVERSION *p2 )
2225
{
2226
    /* Compare the ranks of the conversions */
2227
    int cmp = 0 ;
2228
    unsigned a1 = p1->rank ;
2229
    unsigned a2 = p2->rank ;
2230
    unsigned b1 = CONV_RANK ( a1 ) ;
2231
    unsigned b2 = CONV_RANK ( a2 ) ;
2232
    if ( b1 > b2 ) return ( 1 ) ;
2233
    if ( b1 < b2 ) return ( 2 ) ;
2234
 
2235
    /* Check user-defined conversions */
2236
    if ( a1 == CONV_USER && a2 == CONV_USER ) {
2237
	if ( !EQ_id ( p1->usr, p2->usr ) ) return ( 0 ) ;
2238
	a1 = p1->std ;
2239
	a2 = p2->std ;
2240
	b1 = CONV_RANK ( a1 ) ;
2241
	b2 = CONV_RANK ( a2 ) ;
2242
	if ( b1 > b2 ) return ( 1 ) ;
2243
	if ( b1 < b2 ) return ( 2 ) ;
2244
    }
2245
 
2246
    /* Compare standard conversions */
2247
    switch ( a1 ) {
2248
	case CONV_PTR_BASE : {
2249
	    /* Pointer conversions */
2250
	    if ( a2 == a1 ) {
2251
		/* Compare base pointer conversions */
2252
		GRAPH g1 = p1->base ;
2253
		GRAPH g2 = p2->base ;
2254
		if ( eq_graph ( g1, g2 ) ) {
2255
		    cmp = qual_compare_seq ( p1, p2 ) ;
2256
		} else {
2257
		    cmp = base_compare_seq ( g1, g2 ) ;
2258
		}
2259
	    } else if ( a2 == CONV_PTR_VOID || a2 == CONV_PTR_BOTTOM ) {
2260
		/* Base pointer conversion is better than 'void *' */
2261
		cmp = 1 ;
2262
	    } else if ( a2 == CONV_BOOL ) {
2263
		/* Base pointer conversion is better than 'bool' */
2264
		cmp = 1 ;
2265
	    }
2266
	    break ;
2267
	}
2268
	case CONV_PTR_VOID :
2269
	case CONV_PTR_BOTTOM : {
2270
	    /* Pointer to 'void *' conversions */
2271
	    if ( a2 == CONV_PTR_VOID || a2 == CONV_PTR_BOTTOM ) {
2272
		/* Compare pointer conversions */
2273
		cmp = qual_compare_seq ( p1, p2 ) ;
2274
	    } else if ( a2 == CONV_PTR_BASE ) {
2275
		/* Base pointer conversion is better than 'void *' */
2276
		cmp = 2 ;
2277
	    } else if ( a2 == CONV_BOOL ) {
2278
		/* Pointer conversion is better than 'bool' */
2279
		cmp = 1 ;
2280
	    }
2281
	    break ;
2282
	}
2283
	case CONV_PTR_MEM_BASE : {
2284
	    /* Pointer member conversions */
2285
	    if ( a2 == a1 ) {
2286
		/* Compare base pointer member conversions */
2287
		GRAPH g1 = p1->base ;
2288
		GRAPH g2 = p2->base ;
2289
		if ( eq_graph ( g1, g2 ) ) {
2290
		    cmp = qual_compare_seq ( p1, p2 ) ;
2291
		} else {
2292
		    int bmp = base_compare_seq ( g1, g2 ) ;
2293
		    if ( bmp ) {
2294
			cmp = qual_compare_seq ( p1, p2 ) ;
2295
			switch ( cmp ) {
2296
			    case 1 : {
2297
				if ( bmp != 2 ) cmp = 0 ;
2298
				break ;
2299
			    }
2300
			    case 2 : {
2301
				if ( bmp != 1 ) cmp = 0 ;
2302
				break ;
2303
			    }
2304
			    case 3 : {
2305
				cmp = bmp ;
2306
				break ;
2307
			    }
2308
			}
2309
		    }
2310
		}
2311
	    } else if ( a2 == CONV_BOOL ) {
2312
		/* Pointer conversion is better than 'bool' */
2313
		cmp = 1 ;
2314
	    }
2315
	    break ;
2316
	}
2317
	case CONV_BASE : {
2318
	    /* Base class conversions */
2319
	    if ( a2 == a1 ) {
2320
		/* Compare base class conversions */
2321
		GRAPH g1 = p1->base ;
2322
		GRAPH g2 = p2->base ;
2323
		if ( eq_graph ( g1, g2 ) ) {
2324
		    cmp = qual_compare_seq ( p1, p2 ) ;
2325
		} else {
2326
		    cmp = base_compare_seq ( g1, g2 ) ;
2327
		}
2328
	    }
2329
	    break ;
2330
	}
2331
	case CONV_BOOL : {
2332
	    /* Boolean conversions */
2333
	    if ( a2 == CONV_PTR_BASE || a2 == CONV_PTR_MEM_BASE ||
2334
		 a2 == CONV_PTR_VOID || a2 == CONV_PTR_BOTTOM ) {
2335
		/* Pointer conversion is better than 'bool' */
2336
		cmp = 2 ;
2337
	    }
2338
	    break ;
2339
	}
2340
	case CONV_STRING : {
2341
	    /* String literal conversions */
2342
	    if ( a2 == a1 ) {
2343
		cmp = qual_compare_seq ( p1, p2 ) ;
2344
	    } else if ( a2 == CONV_QUAL ) {
2345
		/* Qualification conversion is better than string */
2346
		cmp = 2 ;
2347
	    }
2348
	    break ;
2349
	}
2350
	case CONV_QUAL : {
2351
	    /* Qualification conversions */
2352
	    if ( a2 == a1 ) {
2353
		cmp = qual_compare_seq ( p1, p2 ) ;
2354
	    } else if ( a2 == CONV_STRING ) {
2355
		/* Qualification conversion is better than string */
2356
		cmp = 1 ;
2357
	    }
2358
	    break ;
2359
	}
2360
    }
2361
    return ( cmp ) ;
2362
}