Subversion Repositories tendra.SVN

Rev

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

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