Subversion Repositories tendra.SVN

Rev

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

Rev Author Line No. Line
2 7u83 1
/*
6 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
6 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:-
6 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
6 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;
6 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;
6 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 "err_ops.h"
65
#include "exp_ops.h"
66
#include "ftype_ops.h"
67
#include "graph_ops.h"
68
#include "id_ops.h"
69
#include "itype_ops.h"
70
#include "nat_ops.h"
71
#include "tok_ops.h"
72
#include "type_ops.h"
73
#include "error.h"
74
#include "catalog.h"
75
#include "option.h"
76
#include "access.h"
77
#include "basetype.h"
78
#include "cast.h"
79
#include "check.h"
80
#include "chktype.h"
81
#include "constant.h"
82
#include "construct.h"
83
#include "convert.h"
84
#include "derive.h"
85
#include "exception.h"
86
#include "expression.h"
87
#include "function.h"
88
#include "identifier.h"
89
#include "initialise.h"
90
#include "inttype.h"
91
#include "literal.h"
92
#include "overload.h"
93
#include "predict.h"
94
#include "statement.h"
95
#include "syntax.h"
96
#include "template.h"
97
#include "tok.h"
98
#include "tokdef.h"
99
#include "token.h"
100
#include "typeid.h"
101
 
102
 
103
/*
104
    CREATE AN EXACT CAST EXPRESSION
105
 
106
    This routine introduces a dummy cast expression which converts the
107
    expression a to its own type t.  This is needed in a couple of places
108
    where it is necessary to recognise cast expressions.
109
*/
110
 
6 7u83 111
static EXP
112
cast_exact(TYPE t, EXP a)
2 7u83 113
{
6 7u83 114
	EXP e;
115
	if (IS_exp_cast(a)) {
116
		/* Exact casts are idempotent */
117
		unsigned conv = DEREF_unsigned(exp_cast_conv(a));
118
		if (conv == CONV_EXACT) {
119
			a = DEREF_exp(exp_cast_arg(a));
120
		}
2 7u83 121
	}
6 7u83 122
	MAKE_exp_cast(t, CONV_EXACT, a, e);
123
	return (e);
2 7u83 124
}
125
 
126
 
127
/*
128
    FIND THE RANK OF AN INTEGER-INTEGER CONVERSION
129
 
130
    This routine finds the rank of a conversion to the integral type t
131
    from the integral type s.  For basic types this is given by the
132
    table builtin_casts.
133
*/
134
 
6 7u83 135
static int
136
rank_int_int(TYPE t, TYPE s)
2 7u83 137
{
6 7u83 138
	int ct = 100, cr = 100;
139
	INT_TYPE is = DEREF_itype(type_integer_sem(s));
140
	INT_TYPE it = DEREF_itype(type_integer_sem(t));
141
	INT_TYPE ir = DEREF_itype(type_integer_rep(t));
2 7u83 142
 
6 7u83 143
	/* Find the semantic conversion */
144
	if (!EQ_itype(it, ir)) {
145
		if (eq_itype(it, is)) {
146
			return (0);
147
		}
148
		if (IS_itype_basic(ir) && IS_itype_basic(is)) {
149
			BUILTIN_TYPE bt = DEREF_ntype(itype_basic_no(it));
150
			BUILTIN_TYPE bs = DEREF_ntype(itype_basic_no(is));
151
			ct = builtin_cast(bs, bt);
152
		}
2 7u83 153
	}
154
 
6 7u83 155
	/* Find the representational conversion */
156
	while (IS_itype_promote(ir)) {
157
		/* Allow for integer promotion conversions */
158
		ir = DEREF_itype(itype_promote_arg(ir));
159
	}
160
	if (eq_itype(ir, is)) {
161
		return (0);
162
	}
163
	if (IS_itype_basic(ir) && IS_itype_basic(is)) {
164
		BUILTIN_TYPE br = DEREF_ntype(itype_basic_no(ir));
165
		BUILTIN_TYPE bs = DEREF_ntype(itype_basic_no(is));
166
		cr = builtin_cast(bs, br);
167
	} else {
168
		TYPE ps = promote_type(s);
169
		if (eq_type(ps, t)) {
170
			return (0);
171
		}
172
	}
2 7u83 173
 
6 7u83 174
	/* Return the better conversion */
175
	return (cr < ct ? cr : ct);
2 7u83 176
}
177
 
178
 
179
/*
180
    FIND THE RANK OF A FLOATING-FLOATING CONVERSION
181
 
182
    This routine finds the rank of a conversion to the floating-point
183
    type t from the floating-point type s.
184
*/
185
 
6 7u83 186
static int
187
rank_float_float(TYPE t, TYPE s)
2 7u83 188
{
6 7u83 189
	int ct = 0;
190
	FLOAT_TYPE fs = DEREF_ftype(type_floating_rep(s));
191
	FLOAT_TYPE ft = DEREF_ftype(type_floating_rep(t));
192
	while (IS_ftype_arg_promote(ft)) {
193
		/* Allow for floating promotion conversions */
194
		ft = DEREF_ftype(ftype_arg_promote_arg(ft));
2 7u83 195
	}
6 7u83 196
	if (!eq_ftype(ft, fs)) {
197
		if (IS_ftype_basic(ft) && IS_ftype_basic(fs)) {
198
			BUILTIN_TYPE nt = DEREF_ntype(ftype_basic_no(ft));
199
			BUILTIN_TYPE ns = DEREF_ntype(ftype_basic_no(fs));
200
			ct = builtin_cast(ns, nt);
201
		} else {
202
			TYPE ps = promote_type(s);
203
			if (eq_type(ps, t)) {
204
				return (0);
205
			}
206
		}
207
	}
208
	return (ct);
2 7u83 209
}
210
 
211
 
212
/*
213
    PERFORM AN INTEGER-INTEGER CONVERSION
214
 
215
    This, and the following routines, are used to perform the basic type
216
    conversions allowed within the language.  Each takes a destination
217
    type t and an argument expression a.  In this case both t and the type
218
    of a are integral (including enumeration and bitfield) types.  The case
219
    where t is bool is dealt with separately by convert_boolean, however a
220
    may have type bool.  rank gives the rank of the conversion, or -1 if
221
    the rank needs to be calculated using rank_int_int.
222
*/
223
 
6 7u83 224
EXP
225
cast_int_int(TYPE t, EXP a, ERROR *err, unsigned cast, int rank)
2 7u83 226
{
6 7u83 227
	EXP e;
228
	int opt;
229
	TYPE s = DEREF_type(exp_type(a));
230
	unsigned nt = TAG_type(t);
231
	unsigned ns = TAG_type(s);
2 7u83 232
 
6 7u83 233
	/* Don't force unnecessary token definitions */
234
	if (force_tokdef) {
235
		TYPE t0 = t;
236
		TYPE s0 = s;
237
		t = expand_type(t0, 1);
238
		if (!EQ_type(t, t0)) {
239
			nt = TAG_type(t);
240
			if (nt == type_floating_tag || nt == type_ptr_tag) {
241
				t = t0;
242
				nt = TAG_type(t);
243
			}
244
		}
245
		s = expand_type(s0, 1);
246
		if (!EQ_type(s, s0)) {
247
			ns = TAG_type(s);
248
			if (ns == type_floating_tag || ns == type_ptr_tag) {
249
				s = s0;
250
				ns = TAG_type(s);
251
			}
252
		}
2 7u83 253
	}
254
 
6 7u83 255
	/* Deal with bitfields */
256
	if (ns == type_bitfield_tag) {
257
		TYPE r = find_bitfield_type(s);
258
		MAKE_exp_cast(r, CONV_BITFIELD, a, a);
259
		/* NOT YET IMPLEMENTED: find rank */
260
		rank = 0;
261
		e = cast_int_int(t, a, err, cast, rank);
262
		if (EQ_exp(e, a)) {
263
			MAKE_exp_cast(t, CONV_INT_INT, e, e);
264
		}
265
		return (e);
2 7u83 266
	}
6 7u83 267
	if (nt == type_bitfield_tag) {
268
		TYPE r = find_bitfield_type(t);
269
		/* NOT YET IMPLEMENTED: find rank */
270
		rank = 0;
271
		e = cast_int_int(r, a, err, cast, rank);
272
		if (EQ_exp(e, a)) {
273
			MAKE_exp_cast(r, CONV_INT_INT, e, e);
274
		}
275
		MAKE_exp_cast(t,(CONV_BITFIELD | CONV_REVERSE), e, e);
276
		return (e);
2 7u83 277
	}
278
 
6 7u83 279
	/* Deal with identity casts */
280
	if (nt == ns) {
281
		if (EQ_type(t, s)) {
282
			if (IS_exp_int_lit(a) && cast != CAST_IMPLICIT) {
283
				NAT n = DEREF_nat(exp_int_lit_nat(a));
284
				MAKE_exp_int_lit(t, n, exp_cast_tag, a);
285
			}
286
			return (a);
2 7u83 287
		}
6 7u83 288
		if (eq_type(t, s)) {
289
			if (cast == CAST_IMPLICIT) {
290
				/* Preserve semantics for implicit casts */
291
				return (a);
292
			}
293
			if (cast != CAST_REINTERP) {
294
				/* Override semantics for other casts */
295
				if (IS_exp_int_lit(a)) {
296
					NAT n = DEREF_nat(exp_int_lit_nat(a));
297
					MAKE_exp_int_lit(t, n, exp_cast_tag, e);
298
				} else {
299
					MAKE_exp_cast(t, CONV_INT_INT, a, e);
300
				}
301
				return (e);
302
			}
303
		}
2 7u83 304
	}
305
 
6 7u83 306
	/* Find error severity level */
307
	if (cast == CAST_IMPLICIT) {
308
		opt = OPT_conv_int_int_impl;
309
	} else if (cast & CAST_STATIC) {
310
		opt = OPT_conv_int_int_expl;
311
	} else {
312
		opt = OPT_error;
313
	}
2 7u83 314
 
6 7u83 315
	/* Can't cast implicitly to enumeration type */
316
	if (nt == type_enumerate_tag) {
317
		ERROR err2;
2 7u83 318
#if LANGUAGE_C
6 7u83 319
		if (IS_exp_int_lit(a)) {
320
			/* Allow for C enumerators */
321
			unsigned tag = DEREF_unsigned(exp_int_lit_etag(a));
322
			if (tag == exp_identifier_tag) {
323
				e = make_cast_nat(t, a, err, cast);
324
				return (e);
325
			}
326
		}
2 7u83 327
#endif
6 7u83 328
		if (cast == CAST_IMPLICIT) {
329
			if (option(OPT_conv_int_enum) > option(opt)) {
330
				opt = OPT_conv_int_enum;
331
			}
332
		}
333
		if (ns == type_enumerate_tag) {
334
			err2 = ERR_expr_cast_stat_enum_enum(s, t);
335
		} else {
336
			err2 = ERR_expr_cast_stat_int_enum(s, t);
337
		}
338
		err2 = set_severity(err2, opt, 0);
339
		if (!IS_NULL_err(err2)) {
340
			e = cast_token(t, a, err, err2, cast);
341
			if (!IS_NULL_exp(e)) {
342
				return (e);
343
			}
344
		}
345
	} else if (opt == OPT_error) {
346
		ERROR err2 = ERR_conv_integral_cast(s, t);
347
		err2 = set_severity(err2, opt, 0);
348
		if (!IS_NULL_err(err2)) {
349
			e = cast_token(t, a, err, err2, cast);
350
			if (!IS_NULL_exp(e)) {
351
				return (e);
352
			}
353
		}
354
		opt = OPT_none;
2 7u83 355
	}
6 7u83 356
 
357
	/* Deal with integral constants */
358
	if (IS_exp_int_lit(a)) {
359
		e = make_cast_nat(t, a, err, cast);
360
		return (e);
2 7u83 361
	}
362
 
6 7u83 363
	/* Check integer to integer conversions */
364
	if (rank != 0 && option(opt)) {
365
		if (nt == type_integer_tag && ns == type_integer_tag) {
366
			if (rank < 0) {
367
				rank = rank_int_int(t, s);
368
			}
369
			if (rank >= max_builtin_cast) {
370
				ERROR err2 = ERR_conv_integral_cast(s, t);
371
				err2 = set_severity(err2, opt, 0);
372
				if (!IS_NULL_err(err2)) {
373
					e = cast_token(t, a, err, err2, cast);
374
					if (!IS_NULL_exp(e)) {
375
						return (e);
376
					}
377
				}
378
			}
2 7u83 379
		}
380
	}
381
 
6 7u83 382
	/* Construct the result */
383
	MAKE_exp_cast(t, CONV_INT_INT, a, e);
384
	return (e);
2 7u83 385
}
386
 
387
 
388
/*
389
    PERFORM A INTEGER-FLOAT CONVERSION
390
 
391
    This routine converts the integral expression a to the floating point
392
    type t.
393
*/
394
 
6 7u83 395
EXP
396
cast_int_float(TYPE t, EXP a, ERROR *err, unsigned cast)
2 7u83 397
{
6 7u83 398
	EXP e;
399
	int opt;
400
	TYPE s = DEREF_type(exp_type(a));
2 7u83 401
 
6 7u83 402
	/* Find error severity level */
403
	if (cast == CAST_IMPLICIT) {
404
		opt = OPT_conv_int_int_impl;
405
	} else if (cast & CAST_STATIC) {
406
		opt = OPT_conv_int_int_expl;
407
	} else {
408
		opt = OPT_error;
2 7u83 409
	}
6 7u83 410
	if (option(opt)) {
411
		ERROR err2 = ERR_conv_fpint_float(s, t);
412
		err2 = set_severity(err2, opt, 0);
413
		if (!IS_NULL_err(err2)) {
414
			e = cast_token(t, a, err, err2, cast);
415
			if (!IS_NULL_exp(e)) {
416
				return (e);
417
			}
418
		}
419
	}
2 7u83 420
 
6 7u83 421
	/* Construct the result */
422
	MAKE_exp_cast(t, CONV_INT_FLT, a, e);
423
	return (e);
2 7u83 424
}
425
 
426
 
427
/*
428
    PERFORM A FLOAT-INTEGER CONVERSION
429
 
430
    This routine converts the floating point expression a to the integral
431
    type t (which will not be bool).  Note that a floating point literal
432
    cast to an integral type is an integral constant expression.
433
*/
434
 
6 7u83 435
static EXP
436
cast_float_int(TYPE t, EXP a, ERROR *err, unsigned cast)
2 7u83 437
{
6 7u83 438
	EXP e;
439
	int opt;
440
	TYPE s = DEREF_type(exp_type(a));
2 7u83 441
 
6 7u83 442
	/* Find error severity level */
443
	if (cast == CAST_IMPLICIT) {
444
		opt = OPT_conv_int_int_impl;
445
		if (IS_type_enumerate(t)) {
446
			/* Can't have enumeration type */
447
			if (option(OPT_conv_int_enum) > option(opt)) {
448
				opt = OPT_conv_int_enum;
449
			}
450
		}
451
	} else if (cast & CAST_STATIC) {
452
		opt = OPT_conv_int_int_expl;
453
	} else {
454
		opt = OPT_error;
2 7u83 455
	}
6 7u83 456
	if (option(opt)) {
457
		ERROR err2 = ERR_conv_fpint_trunc(s, t);
458
		err2 = set_severity(err2, opt, 0);
459
		if (!IS_NULL_err(err2)) {
460
			e = cast_token(t, a, err, err2, cast);
461
			if (!IS_NULL_exp(e)) {
462
				return (e);
463
			}
464
		}
2 7u83 465
	}
466
 
6 7u83 467
	/* Construct the result */
468
	MAKE_exp_cast(t, CONV_FLT_INT, a, e);
2 7u83 469
 
6 7u83 470
	/* Deal with floating point literals */
471
	if (IS_exp_float_lit(a)) {
472
		FLOAT f = DEREF_flt(exp_float_lit_flt(a));
473
		NAT n = round_float_lit(f, crt_round_mode);
474
		if (!IS_NULL_nat(n)) {
475
			EXP c = make_int_exp(t, exp_cast_tag, n);
476
			if (!IS_NULL_exp(c)) {
477
				return (c);
478
			}
479
		}
480
		MAKE_nat_calc(e, n);
481
		MAKE_exp_int_lit(t, n, exp_cast_tag, e);
2 7u83 482
	}
6 7u83 483
	return (e);
2 7u83 484
}
485
 
486
 
487
/*
488
    PERFORM A FLOAT-FLOAT CONVERSION
489
 
490
    This routine converts the floating point expression a to the floating
491
    point type t.
492
*/
493
 
6 7u83 494
EXP
495
cast_float_float(TYPE t, EXP a, ERROR *err, unsigned cast)
2 7u83 496
{
6 7u83 497
	EXP e;
498
	int opt;
499
	TYPE s = DEREF_type(exp_type(a));
2 7u83 500
 
6 7u83 501
	/* Don't force unnecessary token definitions */
502
	if (force_tokdef) {
503
		TYPE t0 = t;
504
		TYPE s0 = s;
505
		t = expand_type(t0, 1);
506
		s = expand_type(s0, 1);
507
		if (!IS_type_floating(t)) {
508
			t = t0;
509
		}
510
		if (!IS_type_floating(s)) {
511
			s = s0;
512
		}
513
	}
2 7u83 514
 
6 7u83 515
	/* Deal with identity casts */
516
	if (eq_type(t, s)) {
517
		if (cast != CAST_REINTERP) {
518
			return (a);
519
		}
520
	}
2 7u83 521
 
6 7u83 522
	/* Find error severity level */
523
	if (cast == CAST_IMPLICIT) {
524
		opt = OPT_conv_int_int_impl;
525
	} else if (cast & CAST_STATIC) {
526
		opt = OPT_conv_int_int_expl;
527
	} else {
528
		opt = OPT_error;
2 7u83 529
	}
6 7u83 530
	if (option(opt)) {
531
		int c = rank_float_float(t, s);
532
		if (c >= max_builtin_cast || opt == OPT_error) {
533
			ERROR err2 = ERR_conv_double_cast(s, t);
534
			err2 = set_severity(err2, opt, 0);
535
			if (!IS_NULL_err(err2)) {
536
				e = cast_token(t, a, err, err2, cast);
537
				if (!IS_NULL_exp(e)) {
538
					return (e);
539
				}
540
			}
541
		}
542
	}
2 7u83 543
 
6 7u83 544
	/* Construct the result */
545
	MAKE_exp_cast(t, CONV_FLT_FLT, a, e);
546
	return (e);
2 7u83 547
}
548
 
549
 
550
/*
551
    CONSTRUCT AN UNRESOLVED CAST EXPRESSION
552
 
553
    This routine creates an expression for casting the expression a to
554
    type t using cast where either t or the type of a depends on a
555
    template parameter type.
556
*/
557
 
6 7u83 558
EXP
559
cast_templ_type(TYPE t, EXP a, unsigned cast)
2 7u83 560
{
6 7u83 561
	EXP e;
562
	int op;
563
	switch (cast) {
564
	case CAST_IMPLICIT:
565
		op = lex_implicit;
566
		break;
567
	case CAST_STATIC:
568
		op = lex_static_Hcast;
569
		break;
570
	case CAST_REINTERP:
571
		op = lex_reinterpret_Hcast;
572
		break;
573
	case CAST_CONST:
574
		op = lex_const_Hcast;
575
		break;
576
	default:
577
		op = lex_cast;
578
		break;
579
	}
580
	t = rvalue_type(t);
581
	MAKE_exp_op(t, op, a, NULL_exp, e);
582
	return (e);
2 7u83 583
}
584
 
585
 
586
/*
587
    REPORT CASTING AWAY CONST-NESS
588
 
589
    This routine adds an error to the end of err if the value qual returned
590
    by check_qualifier indicates that a particular conversion casts away
591
    const-ness (or volatile-ness).
592
*/
593
 
6 7u83 594
void
595
cast_away_const(unsigned qual, ERROR *err, unsigned cast)
2 7u83 596
{
6 7u83 597
	if (!(cast & CAST_CONST)) {
598
		CV_SPEC cv = cv_none;
599
		if (!(qual & QUAL_CONST)) {
600
			cv |= cv_const;
601
		}
602
		if (!(qual & QUAL_VOLATILE)) {
603
			cv |= cv_volatile;
604
		}
605
		if (cv == cv_none) {
606
			if (!(qual & QUAL_ALL_CONST)) {
607
				add_error(err, ERR_conv_qual_multi());
608
			}
609
		} else {
610
			add_error(err, ERR_conv_qual_cast(cv));
611
		}
2 7u83 612
	}
6 7u83 613
	return;
2 7u83 614
}
615
 
616
 
617
/*
618
    CREATE A BASE CAST EXPRESSION
619
 
620
    This routine creates a base cast expression for converting the
621
    expression a to type t using the offset off.  If off is a zero offset
622
    (indicating single inheritance) or the type of a can be statically
623
    determined then this is a simple add_ptr operation.  Otherwise a
624
    base_cast expression is used.  Note that a dummy expression is
625
    introduced to represent the argument.
626
*/
627
 
6 7u83 628
EXP
629
make_base_cast(TYPE t, EXP a, OFFSET off)
2 7u83 630
{
6 7u83 631
	EXP e;
632
	if (is_zero_offset(off) || know_type(a) == 1) {
633
		MAKE_exp_add_ptr(t, a, off, 0, e);
634
	} else {
635
		TYPE s = DEREF_type(exp_type(a));
636
		if (!IS_type_ptr(s)) {
637
			s = t;
638
		}
639
		MAKE_exp_dummy(s, a, LINK_NONE, NULL_off, 1, a);
640
		MAKE_exp_base_cast(t, CONV_PTR_BASE, a, off, e);
641
	}
642
	return (e);
2 7u83 643
}
644
 
645
 
646
/*
647
    PERFORM A POINTER-POINTER CONVERSION
648
 
649
    This routine converts the pointer expression a to the pointer type t.
650
    Pointers can be partitioned into pointer to object, pointer to function
651
    and void * for the purposes of pointer casts.  Note that even identity
652
    function casts are always performed.  This is to prevent further function
653
    overload resolution and to inherit any default arguments from t.  Also
654
    if force is true then an identity explicit cast is inserted after any
655
    base pointer cast.  This is to allow for TDF operations such as
656
    pointer_test which require exact equality of alignments.
657
*/
658
 
6 7u83 659
EXP
660
cast_ptr_ptr(TYPE t, EXP a, ERROR *err, unsigned cast, int safe, int force)
2 7u83 661
{
6 7u83 662
	EXP e;
663
	int opt;
664
	unsigned qual;
665
	OFFSET off = NULL_off;
666
	unsigned conv = CONV_NONE;
667
	TYPE s = DEREF_type(exp_type(a));
668
	TYPE pt = DEREF_type(type_ptr_sub(t));
669
	TYPE ps = DEREF_type(type_ptr_sub(s));
670
	unsigned nt = TAG_type(pt);
671
	unsigned ns = TAG_type(ps);
2 7u83 672
 
6 7u83 673
	/* Allow for tokenised types */
674
	if (nt == type_token_tag) {
675
		t = expand_type(t, 1);
676
		pt = DEREF_type(type_ptr_sub(t));
677
		nt = TAG_type(pt);
2 7u83 678
	}
6 7u83 679
	if (ns == type_token_tag) {
680
		s = expand_type(s, 1);
681
		ps = DEREF_type(type_ptr_sub(s));
682
		ns = TAG_type(ps);
2 7u83 683
	}
684
 
6 7u83 685
	/* Check for qualifier conversions */
686
	qual = check_qualifier(t, s, safe);
687
	if (qual == QUAL_EQUAL) {
688
		/* Allow for type equality */
689
		if (cast != CAST_IMPLICIT) {
690
			a = cast_exact(t, a);
2 7u83 691
		}
6 7u83 692
		return (a);
693
	}
694
	if (qual == QUAL_EQ_FUNC) {
695
		/* Allow for equality of function types */
696
		if (!(cast & CAST_REINTERP) && !eq_except(ps, pt)) {
697
			add_error(err, ERR_except_spec_assign());
2 7u83 698
		}
6 7u83 699
		e = cast_exact(t, a);
700
		return (e);
701
	}
702
	if (qual & QUAL_TEMPL) {
703
		/* Conversion depends on template parameter */
704
		e = cast_templ_type(t, a, cast);
705
		return (e);
706
	}
707
	if (!(qual & QUAL_CONST)) {
708
		/* Check for string literal conversions */
709
		if (IS_exp_address(a) && ns == type_integer_tag) {
710
			EXP b = DEREF_exp(exp_address_arg(a));
711
			if (IS_exp_string_lit(b)) {
712
				/* Remove const and try again */
713
				int str = 1;
714
				if (!(cast & CAST_CONST)) {
715
					str = 2;
716
				}
717
				a = convert_array(b, str, err);
718
				e = cast_ptr_ptr(t, a, err, cast, safe, force);
719
				return (e);
720
			}
2 7u83 721
		}
6 7u83 722
	}
723
	if (!(qual & QUAL_VOLATILE) && used_extern_volatile) {
724
		/* Check for implicitly volatile external objects */
725
		EXP pa = NULL_exp;
726
		DECL_SPEC ds = find_exp_linkage(a, &pa, 1);
727
		if (ds & dspec_implicit) {
728
			qual |= QUAL_VOLATILE;
729
		}
730
	}
731
 
732
	/* Check conversion */
733
	if (qual & QUAL_SIMILAR) {
734
		/* Simple qualification conversions */
735
		opt = OPT_none;
736
		conv = CONV_QUAL;
737
	} else {
738
		/* Other pointer conversions */
739
		ERROR ferr = NULL_err;
740
		switch (nt) {
741
		case type_top_tag:
742
		case type_bottom_tag:
743
generic_lab:
744
			if (ns == type_func_tag) {
745
				/* Conversion from 'function *' to 'void *' */
746
				ERROR err2 = ERR_expr_cast_reint_func_ptr(s, t);
747
				if (!IS_NULL_err(err2)) {
748
					e = cast_token(t, a, err, err2, cast);
749
					if (!IS_NULL_exp(e)) {
750
						return (e);
751
					}
752
				}
2 7u83 753
			}
6 7u83 754
			if (ns == type_top_tag || ns == type_bottom_tag) {
755
				/* Conversion from 'void *' to 'void *' */
756
				opt = OPT_none;
757
				if (nt == type_integer_tag) {
758
					conv = (CONV_PTR_VOID | CONV_REVERSE);
759
				} else {
760
					conv = CONV_EXACT;
761
				}
762
			} else {
763
				/* Conversion from 'object *' to 'void *' */
764
				TYPE r = NULL_type;
765
				if (ns == type_integer_tag) {
766
					/* Check for generic pointers */
767
					r = type_void_star;
768
					r = type_composite(s, r, 1, 0, &ferr,
769
							   0);
770
				}
771
				if (!IS_NULL_type(r)) {
772
					opt = OPT_none;
773
				} else if (cast == CAST_IMPLICIT) {
774
					opt = OPT_conv_ptr_ptr_void;
775
				} else if (cast == CAST_CONST) {
776
					opt = OPT_error;
777
				} else {
778
					opt = OPT_none;
779
				}
780
				if (nt != type_integer_tag)conv = CONV_PTR_VOID;
2 7u83 781
			}
6 7u83 782
			break;
783
		case type_compound_tag: {
784
			if (cast == CAST_CONST || cast == CAST_REINTERP) {
785
				goto default_lab;
2 7u83 786
			}
6 7u83 787
			if (ns == type_compound_tag) {
788
				/* Conversion from 'class *' to 'class *' */
789
				GRAPH gr;
790
				CLASS_TYPE ct, cs;
791
				ct = DEREF_ctype(type_compound_defn(pt));
792
				cs = DEREF_ctype(type_compound_defn(ps));
793
				gr = find_base_class(cs, ct, 1);
794
				if (!IS_NULL_graph(gr)) {
795
					/* Base class conversion */
796
					ERROR err2 = check_ambig_base(gr);
797
					if (!IS_NULL_err(err2)) {
798
						/* Can't be ambiguous */
799
						e = cast_token(t, a, err, err2,
800
							       cast);
801
						if (!IS_NULL_exp(e)) {
802
							return (e);
803
						}
804
						add_error(err, ERR_conv_ptr_ambiguous());
805
					}
806
					if (!(cast & CAST_BAD)) {
807
						/* Check base access */
808
						check_base_access(gr);
809
					}
810
					off = DEREF_off(graph_off(gr));
811
					conv = CONV_PTR_BASE;
812
					opt = OPT_none;
813
					break;
814
				}
815
				if (cast & CAST_STATIC) {
816
					gr = find_base_class(ct, cs, 1);
817
					if (!IS_NULL_graph(gr)) {
818
						/* Reverse base class
819
						 * conversion */
820
						ERROR err2 =
821
						    check_ambig_base(gr);
822
						if (!IS_NULL_err(err2)) {
823
							/* Can't be ambiguous */
824
							e = cast_token(t, a, err, err2, cast);
825
							if (!IS_NULL_exp(e)) {
826
								return (e);
827
							}
828
							add_error(err, ERR_conv_ptr_ambiguous());
829
						}
830
						err2 = check_virt_base(gr);
831
						if (!IS_NULL_err(err2)) {
832
							/* Can't be virtual */
833
							e = cast_token(t, a, err, err2, cast);
834
							if (!IS_NULL_exp(e)) {
835
								return (e);
836
							}
837
							add_error(err, ERR_expr_cast_stat_virt());
838
						}
839
						if (!(cast & CAST_BAD)) {
840
							/* Check base access */
841
							check_base_access(gr);
842
						}
843
						off = DEREF_off(graph_off(gr));
844
						conv = (CONV_PTR_BASE |
845
							CONV_REVERSE);
846
						opt = OPT_none;
847
						break;
848
					}
849
				}
850
			}
851
			goto default_lab;
2 7u83 852
		}
6 7u83 853
		case type_func_tag: {
854
			if (ns != type_func_tag) {
855
				/* Conversion from 'function *' to 'object *' */
856
				ERROR err2 = ERR_expr_cast_reint_func_ptr(s, t);
857
				if (!IS_NULL_err(err2)) {
858
					e = cast_token(t, a, err, err2, cast);
859
					if (!IS_NULL_exp(e)) {
860
						return (e);
861
					}
862
				}
863
				goto object_lab;
864
			}
865
			/* Conversion from 'function *' to 'function *' */
866
			if (cast & CAST_REINTERP) {
867
				opt = OPT_conv_ptr_ptr_expl;
868
			} else {
869
				opt = OPT_conv_ptr_ptr_impl;
870
			}
871
			conv = CONV_FUNC;
872
			break;
2 7u83 873
		}
6 7u83 874
		case type_integer_tag: {
875
			/* Check for generic pointers */
876
			TYPE r = type_void_star;
877
			r = type_composite(t, r, 1, 0, &ferr, 0);
878
			if (!IS_NULL_type(r)) {
879
				goto generic_lab;
880
			}
881
			goto default_lab;
2 7u83 882
		}
6 7u83 883
		default:
884
default_lab:
885
			if (ns == type_func_tag) {
886
				/* Conversion from 'function *' to 'object *' */
887
				ERROR err2 = ERR_expr_cast_reint_func_ptr(s, t);
888
				if (!IS_NULL_err(err2)) {
889
					e = cast_token(t, a, err, err2, cast);
890
					if (!IS_NULL_exp(e)) {
891
						return (e);
892
					}
893
				}
894
			}
895
			goto object_lab;
896
object_lab: {
897
			TYPE r = NULL_type;
898
			if (ns == type_integer_tag) {
899
				/* Check for generic pointers */
900
				r = type_void_star;
901
				r = type_composite(s, r, 1, 0, &ferr, 0);
902
			}
903
			if (IS_NULL_type(r)) {
904
				if (ns != type_top_tag &&
905
				    ns != type_bottom_tag) {
906
					/* Conversion from 'object *' to
907
					 * 'object *' */
908
					if (cast & CAST_REINTERP) {
909
						opt = OPT_conv_ptr_ptr_expl;
910
					} else {
911
						opt = OPT_conv_ptr_ptr_impl;
912
					}
913
					break;
914
				}
915
			}
916
			/* Conversion from 'void *' to 'object *' */
917
			if (cast == CAST_IMPLICIT) {
918
				opt = OPT_conv_ptr_void_ptr;
919
			} else if (cast == CAST_CONST) {
920
				opt = OPT_error;
921
			} else {
922
				opt = OPT_none;
923
			}
924
			if (IS_NULL_type(r)) {
925
				conv = (CONV_PTR_VOID | CONV_REVERSE);
926
			}
927
			break;
2 7u83 928
	    }
929
		}
6 7u83 930
 
931
		/* Add generic pointer errors */
932
		if (!IS_NULL_err(ferr)) {
933
			if (opt == OPT_none) {
934
				destroy_error(ferr, 1);
2 7u83 935
			} else {
6 7u83 936
				add_error(err, ferr);
2 7u83 937
			}
938
		}
6 7u83 939
 
940
		/* Check for function linkage conversions */
941
		if ((qual & QUAL_FUNC) && opt == OPT_conv_ptr_ptr_impl) {
942
			opt = OPT_func_linkage;
2 7u83 943
		}
944
	}
945
 
6 7u83 946
	/* Report any conversion errors */
947
	if (option(opt)) {
948
		ERROR err2;
949
		switch (opt) {
950
		case OPT_func_linkage: {
951
			err2 = ERR_dcl_link_conv();
952
			break;
953
		}
954
		case OPT_conv_ptr_ptr_expl:
955
		case OPT_conv_ptr_ptr_impl: {
956
			err2 = ERR_basic_link_incompat(ps, pt);
957
			err2 = concat_error(err2, ERR_conv_ptr_incompat());
958
			break;
959
		}
960
		default : {
961
			err2 = ERR_conv_ptr_cast(s, t);
962
			break;
963
		}
964
		}
965
		err2 = set_severity(err2, opt, 0);
966
		if (!IS_NULL_err(err2)) {
967
			e = cast_token(t, a, err, err2, cast);
968
			if (!IS_NULL_exp(e)) {
969
				return (e);
970
			}
971
		}
2 7u83 972
	}
6 7u83 973
	if (qual != QUAL_OK) {
974
		cast_away_const(qual, err, cast);
2 7u83 975
	}
976
 
6 7u83 977
	/* Construct the result */
978
	if (IS_exp_null(a)) {
979
		/* Deal with null pointers */
980
		e = make_null_exp(t);
981
	} else if (conv == CONV_PTR_BASE) {
982
		/* Deal with base class conversions */
983
		e = make_base_cast(t, a, off);
984
		if (force) {
985
			/* Force pointer cast */
986
			conv = (CONV_PTR_PTR | CONV_REVERSE);
987
			MAKE_exp_cast(t, conv, e, e);
988
		}
989
	} else if (conv == (CONV_PTR_BASE | CONV_REVERSE)) {
990
		/* Deal with reverse base class conversions */
991
		MAKE_exp_cast(t, CONV_PTR_PTR, a, a);
992
		if (is_zero_offset(off)) {
993
			e = a;
994
		} else {
995
			MAKE_exp_dummy(t, a, LINK_NONE, NULL_off, 1, a);
996
			MAKE_exp_base_cast(t, conv, a, off, e);
997
			if (force) {
998
				/* Force pointer cast */
999
				conv = (CONV_PTR_PTR | CONV_REVERSE);
1000
				MAKE_exp_cast(t, conv, e, e);
1001
			}
1002
		}
2 7u83 1003
	} else {
6 7u83 1004
		if (conv == CONV_NONE) {
1005
			if (eq_type_offset(pt, ps)) {
1006
				conv = CONV_PTR_PTR_ALIGN;
1007
			} else {
1008
				conv = CONV_PTR_PTR;
1009
			}
1010
		}
1011
		MAKE_exp_cast(t, conv, a, e);
2 7u83 1012
	}
6 7u83 1013
	return (e);
2 7u83 1014
}
1015
 
1016
 
1017
/*
1018
    PERFORM A INTEGER-POINTER CONVERSION
1019
 
1020
    This routine converts the integral expression a to the pointer type t.
1021
    There are two cases, depending on whether a represents a null pointer.
1022
*/
1023
 
6 7u83 1024
static EXP
1025
cast_int_ptr(TYPE t, EXP a, ERROR *err, unsigned cast, int nptr)
2 7u83 1026
{
6 7u83 1027
	EXP e;
1028
	int opt;
1029
	if (nptr && (cast == CAST_IMPLICIT || (cast & CAST_STATIC))) {
1030
		/* Deal with null pointers */
1031
		EXP b = make_null_ptr(a, t);
1032
		if (!IS_NULL_exp(b)) {
1033
			return (b);
1034
		}
2 7u83 1035
	}
6 7u83 1036
	if (cast & CAST_REINTERP) {
1037
		opt = OPT_conv_int_ptr_expl;
1038
	} else {
1039
		opt = OPT_conv_int_ptr_impl;
1040
	}
1041
	if (option(opt)) {
1042
		TYPE s = DEREF_type(exp_type(a));
1043
		ERROR err2 = ERR_conv_ptr_nonzero(s, t);
1044
		err2 = set_severity(err2, opt, 0);
1045
		if (!IS_NULL_err(err2)) {
1046
			e = cast_token(t, a, err, err2, cast);
1047
			if (!IS_NULL_exp(e)) {
1048
				return (e);
1049
			}
1050
		}
1051
	}
1052
	MAKE_exp_cast(t, CONV_INT_PTR, a, e);
1053
	return (e);
2 7u83 1054
}
1055
 
1056
 
1057
/*
1058
    PERFORM A POINTER-INTEGER CONVERSION
1059
 
1060
    This routine converts the pointer expression a to the integral type t
1061
    (which will not be bool).
1062
*/
1063
 
6 7u83 1064
static EXP
1065
cast_ptr_int(TYPE t, EXP a, ERROR *err, unsigned cast)
2 7u83 1066
{
6 7u83 1067
	EXP e;
1068
	int opt;
1069
	if (IS_exp_null(a)) {
1070
		if (cast & CAST_STATIC) {
1071
			MAKE_exp_cast(t, CONV_NULL, a, e);
1072
			return (e);
1073
		}
2 7u83 1074
	}
6 7u83 1075
	if (cast & CAST_REINTERP) {
1076
		opt = OPT_conv_int_ptr_expl;
1077
	} else {
1078
		opt = OPT_conv_int_ptr_impl;
2 7u83 1079
	}
6 7u83 1080
	if (option(opt)) {
1081
		TYPE s = DEREF_type(exp_type(a));
1082
		ERROR err2 = ERR_expr_cast_reint_ptr_int(s, t);
1083
		if (!IS_NULL_err(err2)) {
1084
			e = cast_token(t, a, err, err2, cast);
1085
			if (!IS_NULL_exp(e)) {
1086
				return (e);
1087
			}
1088
		}
1089
	}
1090
	MAKE_exp_cast(t, CONV_PTR_INT, a, e);
1091
	return (e);
2 7u83 1092
}
1093
 
1094
 
1095
/*
1096
    PERFORM A POINTER MEMBER-POINTER MEMBER CONVERSION
1097
 
1098
    This routine converts the pointer to member expression a to the pointer
1099
    to member type t.  force is as in cast_ptr_ptr.
1100
*/
1101
 
6 7u83 1102
EXP
1103
cast_ptr_mem_ptr_mem(TYPE t, EXP a, ERROR *err, unsigned cast, int safe,
1104
		     int force)
2 7u83 1105
{
6 7u83 1106
	EXP e;
1107
	int ok = 2;
1108
	unsigned conv = CONV_EXACT;
1109
	TYPE s = DEREF_type(exp_type(a));
2 7u83 1110
 
6 7u83 1111
	if (cast != CAST_REINTERP) {
1112
		/* Check for base class conversions */
1113
		OFFSET off = NULL_off;
1114
		CLASS_TYPE ct = DEREF_ctype(type_ptr_mem_of(t));
1115
		CLASS_TYPE cs = DEREF_ctype(type_ptr_mem_of(s));
1116
		if (!eq_ctype(ct, cs)) {
1117
			GRAPH gr = find_base_class(ct, cs, 1);
1118
			if (!IS_NULL_graph(gr)) {
1119
				/* cs is a base class of ct */
1120
				ERROR err2 = check_ambig_base(gr);
1121
				if (!IS_NULL_err(err2)) {
1122
					/* Can't be ambiguous */
1123
					e = cast_token(t, a, err, err2, cast);
1124
					if (!IS_NULL_exp(e)) {
1125
						return (e);
1126
					}
1127
					add_error(err, ERR_conv_mem_ambiguous());
1128
				}
1129
				err2 = check_virt_base(gr);
1130
				if (!IS_NULL_err(err2)) {
1131
					/* Can't be virtual */
1132
					e = cast_token(t, a, err, err2, cast);
1133
					if (!IS_NULL_exp(e)) {
1134
						return (e);
1135
					}
1136
					add_error(err, ERR_conv_mem_virtual());
1137
				}
1138
				if (!(cast & CAST_BAD)) {
1139
					/* Check base access */
1140
					check_base_access(gr);
1141
				}
1142
				off = DEREF_off(graph_off(gr));
1143
				conv = CONV_PTR_MEM_BASE;
1144
				ok = 1;
1145
			} else {
1146
				/* cs is not a base class of ct */
1147
				if (cast & CAST_STATIC) {
1148
					gr = find_base_class(cs, ct, 1);
1149
					if (!IS_NULL_graph(gr)) {
1150
						/* ct is a base class of cs */
1151
						ERROR err2 = check_ambig_base(gr);
1152
						if (!IS_NULL_err(err2)) {
1153
							/* Can't be ambiguous */
1154
							e = cast_token(t, a, err, err2, cast);
1155
							if (!IS_NULL_exp(e)) {
1156
								return (e);
1157
							}
1158
							add_error(err, ERR_conv_mem_ambiguous());
1159
						}
1160
						err2 = check_virt_base(gr);
1161
						if (!IS_NULL_err(err2)) {
1162
							/* Can't be virtual */
1163
							e = cast_token(t, a, err, err2, cast);
1164
							if (!IS_NULL_exp(e)) {
1165
								return (e);
1166
							}
1167
							add_error(err, ERR_conv_mem_virtual());
1168
						}
1169
						if (!(cast & CAST_BAD)) {
1170
							/* Check base access */
1171
							check_base_access(gr);
1172
						}
1173
						off = DEREF_off(graph_off(gr));
1174
						conv = (CONV_PTR_MEM_BASE | CONV_REVERSE);
1175
						ok = 1;
1176
					} else {
1177
						ok = 0;
1178
					}
1179
				} else {
1180
					ok = 0;
1181
				}
1182
			}
1183
 
1184
			if (ok == 0 && in_template_decl) {
1185
				/* Allow for template parameter types */
1186
				TYPE ft = DEREF_type(ctype_form(ct));
1187
				TYPE fs = DEREF_type(ctype_form(cs));
1188
				if (is_templ_depend(ft) ||
1189
				    is_templ_depend(fs)) {
1190
					/* Check further */
1191
					ok = -1;
1192
				}
1193
			}
2 7u83 1194
		}
6 7u83 1195
 
1196
		/* Check for qualification conversions */
1197
		if (ok) {
1198
			unsigned qual;
1199
			TYPE pt = DEREF_type(type_ptr_mem_sub(t));
1200
			TYPE ps = DEREF_type(type_ptr_mem_sub(s));
1201
			if (IS_type_token(pt)) {
1202
				t = expand_type(t, 1);
2 7u83 1203
			}
6 7u83 1204
			if (IS_type_token(ps)) {
1205
				s = expand_type(s, 1);
2 7u83 1206
			}
6 7u83 1207
			qual = check_qualifier(t, s, safe);
1208
			if (qual == QUAL_EQUAL) {
1209
				/* Type equality */
1210
				if (ok == 2) {
1211
					if (cast != CAST_IMPLICIT) {
1212
						a = cast_exact(t, a);
1213
					}
1214
					return (a);
1215
				}
1216
				qual = QUAL_OK;
1217
			} else if (qual == QUAL_EQ_FUNC) {
1218
				/* Function type equality */
1219
				if (!(cast & CAST_REINTERP) &&
1220
				    !eq_except(ps, pt)) {
1221
					/* Exception specifications don't
1222
					 * match */
1223
					add_error(err, ERR_except_spec_assign());
1224
				}
1225
				if (ok == 2) {
1226
					e = cast_exact(t, a);
1227
					return (e);
1228
				}
1229
				qual = QUAL_OK;
2 7u83 1230
			}
6 7u83 1231
			if ((qual & QUAL_TEMPL) || ok == -1) {
1232
				/* Conversion depends on template parameter */
1233
				e = cast_templ_type(t, a, cast);
1234
				return (e);
1235
			}
1236
			if (qual & QUAL_SIMILAR) {
1237
				/* Check for casting away const-ness */
1238
				if (qual != QUAL_OK) {
1239
					cast_away_const(qual, err, cast);
1240
				}
1241
				if (ok == 2) {
1242
					MAKE_exp_cast(t, CONV_QUAL, a, e);
1243
				} else {
1244
					MAKE_exp_dummy(s, a, LINK_NONE, NULL_off, 1, a);
1245
					MAKE_exp_base_cast(t, conv, a, off, e);
1246
					UNUSED(force);
1247
				}
1248
				return (e);
1249
			}
1250
			ok = 0;
2 7u83 1251
		}
1252
	}
1253
 
6 7u83 1254
	/* Check for reinterpret conversions */
1255
	conv = CONV_NONE;
1256
	if (cast & CAST_REINTERP) {
1257
		unsigned nt = TAG_type(t);
1258
		unsigned ns = TAG_type(s);
1259
		if (nt == type_func_tag) {
1260
			ok = (ns == type_func_tag ? 1 : 0);
1261
		} else {
1262
			ok = (ns == type_func_tag ? 0 : 1);
2 7u83 1263
		}
6 7u83 1264
		if (ok) {
1265
			unsigned qual = check_qualifier(t, s, safe);
1266
			if (qual != QUAL_OK) {
1267
				cast_away_const(qual, err, cast);
1268
			}
1269
			conv = CONV_PTR_MEM_PTR_MEM;
2 7u83 1270
		}
1271
	}
1272
 
6 7u83 1273
	/* Invalid cast expression */
1274
	if (!ok) {
1275
		ERROR err2 = ERR_conv_mem_cast(s, t);
1276
		if (!IS_NULL_err(err2)) {
1277
			e = cast_token(t, a, err, err2, cast);
1278
			if (!IS_NULL_exp(e)) {
1279
				return (e);
1280
			}
1281
		}
2 7u83 1282
	}
6 7u83 1283
	MAKE_exp_cast(t, conv, a, e);
1284
	return (e);
2 7u83 1285
}
1286
 
1287
 
1288
/*
1289
    PERFORM A INTEGER-POINTER MEMBER CONVERSION
1290
 
1291
    This routine converts the integral expression a to the pointer to
1292
    member type t.  The only valid case is when a is zero.
1293
*/
1294
 
6 7u83 1295
static EXP
1296
cast_int_ptr_mem(TYPE t, EXP a, ERROR *err, unsigned cast, int nptr)
2 7u83 1297
{
6 7u83 1298
	EXP e;
1299
	TYPE s;
1300
	ERROR err2;
1301
	if (nptr && (cast == CAST_IMPLICIT || (cast & CAST_STATIC))) {
1302
		/* Deal with null pointers */
1303
		EXP b = make_null_ptr(a, t);
1304
		if (!IS_NULL_exp(b)) {
1305
			return (b);
1306
		}
1307
	}
1308
	s = DEREF_type(exp_type(a));
1309
	err2 = ERR_conv_mem_nonzero(s, t);
1310
	if (!IS_NULL_err(err2)) {
1311
		e = cast_token(t, a, err, err2, cast);
1312
		if (!IS_NULL_exp(e)) {
1313
			return (e);
1314
		}
1315
	}
1316
	MAKE_exp_cast(t, CONV_NONE, a, e);
1317
	return (e);
2 7u83 1318
}
1319
 
1320
 
1321
/*
1322
    PERFORM A POINTER MEMBER-INTEGER CONVERSION
1323
 
1324
    This routine converts the pointer to member expression a to the
1325
    integral type t.  The only valid case is when a is a null pointer.
1326
    In all other cases the null expression is returned.
1327
*/
1328
 
6 7u83 1329
static EXP
1330
cast_ptr_mem_int(TYPE t, EXP a, ERROR *err, unsigned cast)
2 7u83 1331
{
6 7u83 1332
	if (IS_exp_null(a)) {
1333
		if (cast & CAST_STATIC) {
1334
			EXP e;
1335
			MAKE_exp_cast(t, CONV_NULL, a, e);
1336
			return (e);
1337
		}
2 7u83 1338
	}
6 7u83 1339
	UNUSED(err);
1340
	return (NULL_exp);
2 7u83 1341
}
1342
 
1343
 
1344
/*
1345
    PERFORM A POINTER MEMBER-POINTER CONVERSION
1346
 
1347
    This routine converts the pointer to member expression a to the
1348
    pointer type t.  The only potentially valid case is casting a pointer
1349
    to member function to a pointer to function.  In all other cases the
1350
    null expression is returned.
1351
*/
1352
 
6 7u83 1353
static EXP
1354
cast_ptr_mem_ptr(TYPE t, EXP a, ERROR *err, unsigned cast)
2 7u83 1355
{
6 7u83 1356
	TYPE s = DEREF_type(exp_type(a));
1357
	TYPE p = DEREF_type(type_ptr_mem_sub(s));
1358
	TYPE q = DEREF_type(type_ptr_sub(t));
1359
	if (IS_type_func(p) && IS_type_func(q)) {
1360
		if (cast & CAST_REINTERP) {
1361
			EXP e;
1362
			ERROR err2 = ERR_expr_cast_reint_mem_func(s, t);
1363
			if (!IS_NULL_err(err2)) {
1364
				e = cast_token(t, a, err, err2, cast);
1365
				if (!IS_NULL_exp(e)) {
1366
					return (e);
1367
				}
1368
			}
1369
			MAKE_exp_cast(t, CONV_PTR_MEM_FUNC, a, e);
1370
			return (e);
1371
		}
2 7u83 1372
	}
6 7u83 1373
	return (NULL_exp);
2 7u83 1374
}
1375
 
1376
 
1377
/*
1378
    PERFORM A CLASS-CLASS CONVERSION
1379
 
1380
    This routine performs any base class conversion of the class object
1381
    a to the class t.  ref is true if this is a reference binding.  The
1382
    null expression is returned if no such conversion is possible.
1383
*/
1384
 
6 7u83 1385
EXP
1386
cast_class_class(TYPE t, EXP a, ERROR *err, unsigned cast, int ref)
2 7u83 1387
{
6 7u83 1388
	EXP e = NULL_exp;
1389
	TYPE s = DEREF_type(exp_type(a));
1390
	CLASS_TYPE ct = DEREF_ctype(type_compound_defn(t));
1391
	CLASS_TYPE cs = DEREF_ctype(type_compound_defn(s));
1392
	if (eq_ctype(cs, ct)) {
1393
		e = a;
2 7u83 1394
	} else {
6 7u83 1395
		GRAPH gr = find_base_class(cs, ct, 1);
1396
		if (!IS_NULL_graph(gr)) {
1397
			/* Allow for base class conversions */
1398
			TYPE p;
1399
			CV_SPEC cv = DEREF_cv(type_qual(s));
1400
			OFFSET off = DEREF_off(graph_off(gr));
1401
			ERROR err2 = check_ambig_base(gr);
1402
			if (!IS_NULL_err(err2)) {
1403
				e = cast_token(t, a, err, err2, cast);
1404
				if (!IS_NULL_exp(e)) {
1405
					return (e);
1406
				}
1407
				add_error(err, ERR_dcl_init_ref_ambig());
1408
			}
1409
			check_base_access(gr);
1410
			p = rvalue_type(t);
1411
			MAKE_type_ptr(cv_none, p, p);
1412
			if (!(cv & cv_lvalue)) {
1413
				/* Introduce temporary if necessary */
1414
				a = make_temporary(s, a, NULL_exp, 0, err);
1415
			}
1416
			MAKE_exp_address(p, a, e);
1417
			e = make_base_cast(p, e, off);
1418
			t = lvalue_type(t);
1419
			MAKE_exp_indir(t, e, e);
1420
		}
2 7u83 1421
	}
6 7u83 1422
	if (!IS_NULL_exp(e)) {
1423
		if (ref) {
1424
			/* Check cv-qualifiers */
1425
			CV_SPEC cv = cv_compare(t, s);
1426
			if (cv) {
1427
				add_error(err, ERR_dcl_init_ref_qual(cv));
1428
			}
1429
		} else {
1430
			e = convert_lvalue(e);
1431
		}
1432
		if (cast != CAST_IMPLICIT && err != KILL_err) {
1433
			/* Can't have explicit cast in C */
1434
			*err = concat_error(ERR_expr_cast_expl_scalar(t), *err);
1435
		}
2 7u83 1436
	}
6 7u83 1437
	return (e);
2 7u83 1438
}
1439
 
1440
 
1441
/*
1442
    CONSTRUCT A CAST EXPRESSION
1443
 
1444
    This routine constructs a cast expression for converting the expression
1445
    a to the type t.  Any errors are added to the end of the position given
1446
    by err.
1447
*/
1448
 
6 7u83 1449
EXP
1450
cast_exp(TYPE t, EXP a, ERROR *err, unsigned cast)
2 7u83 1451
{
6 7u83 1452
	TYPE s;
1453
	CV_SPEC cv;
1454
	unsigned ns;
1455
	EXP e = NULL_exp;
1456
	int usr = LANGUAGE_CPP;
1457
	unsigned nt = TAG_type(t);
2 7u83 1458
 
6 7u83 1459
	/* Deal with tokenised types */
1460
	if (nt == type_token_tag) {
1461
		if (is_templ_type(t)) {
1462
			e = cast_templ_type(t, a, cast);
1463
			return (e);
1464
		}
1465
		t = expand_type(t, 0);
1466
		nt = TAG_type(t);
2 7u83 1467
	}
1468
 
6 7u83 1469
	/* Deal with reference conversions */
1470
	if (nt == type_ref_tag) {
1471
		/* Transform 'cast <t&> (a)' to '*cast <t*> (&a)' */
1472
		ERROR err2 = NULL_err;
1473
		TYPE p = DEREF_type(type_ref_sub(t));
1474
		if (is_templ_type(p)) {
1475
			e = cast_templ_type(t, a, cast);
1476
			return (e);
1477
		}
1478
		p = rvalue_type(p);
1479
		MAKE_type_ptr(cv_none, p, p);
1480
 
1481
		/* Construct the result */
1482
		a = make_ref_object(a, &err2);
1483
		s = DEREF_type(exp_type(a));
1484
		if (IS_type_error(s)) {
1485
			e = cast_exact(p, a);
1486
		} else {
1487
			e = cast_exp(p, a, &err2, cast);
1488
		}
1489
		if (!IS_NULL_err(err2)) {
1490
			add_error(err, err2);
1491
			add_error(err, ERR_expr_cast_ref(t, p));
1492
		}
1493
		e = cast_exact(t, e);
1494
		return (e);
2 7u83 1495
	}
1496
 
6 7u83 1497
	/* Check user-defined conversion status */
1498
	if (cast & CAST_STANDARD) {
1499
		cast &= ~CAST_STANDARD;
1500
		usr = 0;
2 7u83 1501
	}
1502
 
6 7u83 1503
	/* Deal with function overloading */
1504
	a = resolve_cast(t, a, err, 1, 0, NULL_list(IDENTIFIER));
2 7u83 1505
 
6 7u83 1506
	/* Deal with casting to void */
1507
	if (nt == type_top_tag || nt == type_bottom_tag) {
1508
		if (cast & CAST_STATIC) {
1509
			a = make_discard_exp(a);
1510
			MAKE_exp_cast(t, CONV_ELLIPSIS, a, e);
1511
			return (e);
1512
		}
2 7u83 1513
	}
1514
 
6 7u83 1515
	/* Find the operand type */
1516
	s = DEREF_type(exp_type(a));
1517
	ns = TAG_type(s);
2 7u83 1518
 
6 7u83 1519
	/* Check for template types */
1520
	if (ns == type_token_tag && is_templ_type(s)) {
1521
		e = cast_templ_type(t, a, cast);
1522
		return (e);
1523
	}
2 7u83 1524
 
6 7u83 1525
	/* Deal with user-defined conversions */
1526
	if (usr) {
1527
		if (nt == type_compound_tag) {
1528
			if (cast == CAST_IMPLICIT || (cast & CAST_STATIC)) {
1529
				ERROR err2 = check_incomplete(t);
1530
				if (!IS_NULL_err(err2)) {
1531
					/* Can't have incomplete type */
1532
					add_error(err, err2);
1533
					add_error(err, ERR_expr_cast_invalid(s, t));
1534
					e = make_null_exp(t);
1535
					return (e);
1536
				}
1537
				if (cast != CAST_IMPLICIT) {
1538
					err2 = check_abstract(t);
1539
					if (!IS_NULL_err(err2)) {
1540
						/* Can't have abstract type */
1541
						add_error(err, err2);
1542
						add_error(err, ERR_class_abstract_cast());
1543
					}
1544
				}
1545
				e = init_direct(t, a, err);
1546
				return (e);
1547
			}
2 7u83 1548
		}
6 7u83 1549
		if (ns == type_compound_tag) {
1550
			if (cast == CAST_IMPLICIT || (cast & CAST_STATIC)) {
1551
				e = convert_conv(t, a, err, cast);
1552
				return (e);
1553
			}
2 7u83 1554
		}
1555
	}
1556
 
6 7u83 1557
	/* Check for function casts */
2 7u83 1558
#if LANGUAGE_CPP
6 7u83 1559
	if (nt == type_func_tag && ns == type_func_tag) {
1560
		if (cast & CAST_STATIC) {
1561
			if (eq_type(t, s)) {
1562
				add_error(err, ERR_expr_cast_stat_func(t));
1563
				t = lvalue_type(t);
1564
				MAKE_exp_cast(t, CONV_FUNC, a, e);
1565
				return (e);
1566
			}
1567
		}
2 7u83 1568
	}
1569
#endif
1570
 
6 7u83 1571
	/* Do lvalue conversion on conversion */
1572
	a = convert_lvalue(a);
1573
	s = DEREF_type(exp_type(a));
1574
	ns = TAG_type(s);
2 7u83 1575
 
6 7u83 1576
	/* Deal with tokenised types */
1577
	if (ns == type_token_tag) {
1578
		s = expand_type(s, 0);
1579
		ns = TAG_type(s);
1580
	}
2 7u83 1581
 
6 7u83 1582
	/* Ignore any qualifiers for destination type */
1583
	cv = DEREF_cv(type_qual(t));
1584
	if (cv != cv_none) {
2 7u83 1585
#if LANGUAGE_CPP
6 7u83 1586
		if (nt != type_compound_tag) {
1587
			cv = cv_none;
1588
		}
1589
		t = qualify_type(t, cv, 0);
2 7u83 1590
#else
6 7u83 1591
		t = qualify_type(t, cv_none, 0);
2 7u83 1592
#endif
6 7u83 1593
	}
2 7u83 1594
 
6 7u83 1595
	/* Deal with casting to bool */
1596
	if (nt == type_integer_tag && check_int_type(t, btype_bool)) {
1597
		if (ns == type_compound_tag) {
1598
			/* User-defined conversions already handled */
1599
			/* EMPTY */
1600
		} else {
1601
			if (cast == CAST_IMPLICIT || (cast & CAST_STATIC)) {
1602
				e = convert_boolean(a, exp_paren_tag, err);
1603
				return (e);
1604
			}
1605
			if (cast == CAST_CONST && eq_type(s, t)) {
1606
				return (a);
1607
			}
1608
			ns = null_tag;
1609
		}
2 7u83 1610
	}
1611
 
6 7u83 1612
	/* Check simple conversions */
1613
	switch (ns) {
1614
	case type_integer_tag:
1615
	case type_enumerate_tag:
1616
integer_label:
1617
		/* Conversion from integer */
1618
		switch (nt) {
1619
		case type_integer_tag:
1620
		case type_bitfield_tag:
1621
		case type_enumerate_tag: {
1622
			e = cast_int_int(t, a, err, cast, -1);
1623
			break;
2 7u83 1624
		}
6 7u83 1625
		case type_floating_tag: {
1626
			e = cast_int_float(t, a, err, cast);
1627
			break;
2 7u83 1628
		}
6 7u83 1629
		case type_ptr_tag: {
1630
			e = cast_int_ptr(t, a, err, cast, 1);
1631
			break;
2 7u83 1632
		}
6 7u83 1633
		case type_ptr_mem_tag: {
1634
			e = cast_int_ptr_mem(t, a, err, cast, 1);
1635
			break;
2 7u83 1636
		}
1637
		}
6 7u83 1638
		break;
1639
	case type_bitfield_tag: {
1640
		/* Conversion from bitfield */
1641
		switch (nt) {
1642
		case type_integer_tag:
1643
		case type_bitfield_tag:
1644
		case type_enumerate_tag: {
1645
			e = cast_int_int(t, a, err, cast, -1);
1646
			break;
1647
		}
2 7u83 1648
		default : {
6 7u83 1649
			TYPE r = find_bitfield_type(s);
1650
			a = cast_int_int(r, a, err, cast, -1);
1651
			goto integer_label;
2 7u83 1652
		}
6 7u83 1653
		}
1654
		break;
2 7u83 1655
	}
6 7u83 1656
	case type_floating_tag: {
1657
		/* Conversion from floating */
1658
		switch (nt) {
1659
		case type_integer_tag:
1660
		case type_enumerate_tag: {
1661
			e = cast_float_int(t, a, err, cast);
1662
			break;
2 7u83 1663
		}
6 7u83 1664
		case type_bitfield_tag: {
1665
			TYPE r = find_bitfield_type(t);
1666
			a = cast_float_int(r, a, err, cast);
1667
			e = cast_int_int(t, a, err, cast, -1);
1668
			break;
2 7u83 1669
		}
6 7u83 1670
		case type_floating_tag: {
1671
			e = cast_float_float(t, a, err, cast);
1672
			break;
2 7u83 1673
		}
6 7u83 1674
		}
1675
		break;
2 7u83 1676
	}
6 7u83 1677
	case type_ptr_tag: {
1678
		/* Conversion from pointer */
1679
		switch (nt) {
1680
		case type_integer_tag:
1681
		case type_enumerate_tag: {
1682
			e = cast_ptr_int(t, a, err, cast);
1683
			break;
2 7u83 1684
		}
6 7u83 1685
		case type_bitfield_tag: {
1686
			TYPE r = find_bitfield_type(t);
1687
			a = cast_ptr_int(r, a, err, cast);
1688
			e = cast_int_int(t, a, err, cast, -1);
1689
			break;
2 7u83 1690
		}
6 7u83 1691
		case type_ptr_tag: {
1692
			e = cast_ptr_ptr(t, a, err, cast, 0, 0);
1693
			break;
2 7u83 1694
		}
6 7u83 1695
		}
1696
		break;
2 7u83 1697
	}
6 7u83 1698
	case type_ptr_mem_tag: {
1699
		/* Conversion from pointer to member */
1700
		switch (nt) {
1701
		case type_integer_tag:
1702
		case type_enumerate_tag: {
1703
			e = cast_ptr_mem_int(t, a, err, cast);
1704
			break;
2 7u83 1705
		}
6 7u83 1706
		case type_bitfield_tag: {
1707
			TYPE r = find_bitfield_type(t);
1708
			e = cast_ptr_mem_int(r, a, err, cast);
1709
			a = cast_int_int(t, a, err, cast, -1);
1710
			break;
2 7u83 1711
		}
6 7u83 1712
		case type_ptr_tag: {
1713
			e = cast_ptr_mem_ptr(t, a, err, cast);
1714
			break;
2 7u83 1715
		}
6 7u83 1716
		case type_ptr_mem_tag: {
1717
			e = cast_ptr_mem_ptr_mem(t, a, err, cast, 0, 0);
1718
			break;
2 7u83 1719
		}
6 7u83 1720
		}
1721
		break;
2 7u83 1722
	}
6 7u83 1723
	case type_compound_tag: {
1724
		if (nt == type_compound_tag && !usr) {
1725
			e = cast_class_class(t, a, err, cast, 0);
2 7u83 1726
		}
6 7u83 1727
		break;
2 7u83 1728
	}
1729
	}
6 7u83 1730
 
1731
	if (IS_NULL_exp(e)) {
1732
		if (cast != CAST_IMPLICIT) {
1733
			switch (nt) {
1734
			case type_func_tag:
1735
			case type_array_tag:
1736
			case type_token_tag:
1737
			case type_compound_tag: {
1738
				/* Cast to non-scalar type */
1739
				add_error(err, ERR_expr_cast_expl_scalar(t));
1740
				break;
1741
			}
1742
			}
2 7u83 1743
		}
6 7u83 1744
		if (ns == type_token_tag || nt == type_token_tag) {
1745
			/* Allow for tokenised types */
1746
			int ft = force_tokdef;
1747
			int fs = force_template;
1748
			if (cast != CAST_IMPLICIT) {
1749
				force_tokdef = 0;
1750
				force_template = 0;
1751
			}
1752
			if (eq_type_unqual(s, t)) {
1753
				e = a;
1754
			}
1755
			force_template = fs;
1756
			force_tokdef = ft;
1757
		}
1758
		if (IS_NULL_exp(e)) {
1759
			/* No other conversions are allowed */
1760
			if (ns == type_error_tag || nt == type_error_tag) {
1761
				e = cast_exact(t, a);
1762
			} else {
1763
				ERROR err2 = check_incomplete(t);
1764
				err2 = concat_error(err2, ERR_expr_cast_invalid(s, t));
1765
				if (!IS_NULL_err(err2)) {
1766
					e = cast_token(t, a, err, err2, cast);
1767
					if (!IS_NULL_exp(e)) {
1768
						return (e);
1769
					}
1770
				}
1771
				MAKE_exp_cast(t, CONV_NONE, a, e);
1772
			}
1773
		}
2 7u83 1774
	}
6 7u83 1775
	return (e);
2 7u83 1776
}
1777
 
1778
 
1779
/*
1780
    CONSTRUCT A SIMPLE CAST EXPRESSION
1781
 
1782
    This routine constructs the simple cast expression '( t ) a'.  n gives
1783
    the number of types defined in t.
1784
*/
1785
 
6 7u83 1786
EXP
1787
make_cast_exp(TYPE t, EXP a, int n)
2 7u83 1788
{
6 7u83 1789
	EXP e;
1790
	ERROR err = NULL_err;
1791
	unsigned conv = (unsigned)option_value(OPT_VAL_cast_explicit);
1792
	report(crt_loc, ERR_expr_cast_expl_used());
1793
	if (n) {
1794
		report(crt_loc, ERR_expr_cast_expl_typedef());
1795
	}
1796
	a = convert_reference(a, REF_ASSIGN);
1797
	e = cast_exp(t, a, &err, conv);
1798
	if (!IS_NULL_err(err)) {
1799
		err = concat_warning(err, ERR_expr_cast_expl_bad());
1800
		report(crt_loc, err);
1801
	}
1802
	return (e);
2 7u83 1803
}
1804
 
1805
 
1806
/*
1807
    CONSTRUCT A STATIC CAST EXPRESSION
1808
 
1809
    This routine constructs the static cast expression 'static_cast < t >
1810
    ( a )'.  n gives the number of types defined in t.
1811
*/
1812
 
6 7u83 1813
EXP
1814
make_static_cast_exp(TYPE t, EXP a, int n)
2 7u83 1815
{
6 7u83 1816
	EXP e;
1817
	ERROR err = NULL_err;
1818
	if (n) {
1819
		report(crt_loc, ERR_expr_cast_stat_typedef());
1820
	}
1821
	a = convert_reference(a, REF_ASSIGN);
1822
	e = cast_exp(t, a, &err, CAST_STATIC);
1823
	if (!IS_NULL_err(err)) {
1824
		err = concat_warning(err, ERR_expr_cast_stat_bad());
1825
		report(crt_loc, err);
1826
	}
1827
	return (e);
2 7u83 1828
}
1829
 
1830
 
1831
/*
1832
    CONSTRUCT A REINTERPRET CAST EXPRESSION
1833
 
1834
    This routine constructs the reinterpret cast expression 'reinterpret_cast
1835
    < t > ( a )'.  n gives the number of types defined in t.
1836
*/
1837
 
6 7u83 1838
EXP
1839
make_reinterp_cast_exp(TYPE t, EXP a, int n)
2 7u83 1840
{
6 7u83 1841
	EXP e;
1842
	ERROR err = NULL_err;
1843
	if (n) {
1844
		report(crt_loc, ERR_expr_cast_reint_typedef());
1845
	}
1846
	a = convert_reference(a, REF_ASSIGN);
1847
	e = cast_exp(t, a, &err, CAST_REINTERP);
1848
	if (!IS_NULL_err(err)) {
1849
		err = concat_warning(err, ERR_expr_cast_reint_bad());
1850
		report(crt_loc, err);
1851
	}
1852
	return (e);
2 7u83 1853
}
1854
 
1855
 
1856
/*
1857
    CONSTRUCT A CONST CAST EXPRESSION
1858
 
1859
    This routine constructs the const cast expression 'const_cast < t >
1860
    ( a )'.  n gives the number of types defined in t.
1861
*/
1862
 
6 7u83 1863
EXP
1864
make_const_cast_exp(TYPE t, EXP a, int n)
2 7u83 1865
{
6 7u83 1866
	EXP e;
1867
	ERROR err = NULL_err;
1868
	if (n) {
1869
		report(crt_loc, ERR_expr_cast_const_typedef());
1870
	}
1871
	a = convert_reference(a, REF_ASSIGN);
1872
	e = cast_exp(t, a, &err, CAST_CONST);
1873
	if (!IS_NULL_err(err)) {
1874
		err = concat_warning(err, ERR_expr_cast_const_bad());
1875
		report(crt_loc, err);
1876
	}
1877
	return (e);
2 7u83 1878
}
1879
 
1880
 
1881
/*
1882
    CONSTRUCT A NEW-STYLE CAST EXPRESSION
1883
 
1884
    This routine constructs the new-style cast expression 'op < t > ( a )'.
1885
    n gives the number of types defined in t.
1886
*/
1887
 
1888
#if LANGUAGE_CPP
1889
 
6 7u83 1890
EXP
1891
make_new_cast_exp(int op, TYPE t, EXP a, int n)
2 7u83 1892
{
6 7u83 1893
	EXP e;
1894
	switch (op) {
1895
	case lex_static_Hcast: {
1896
		e = make_static_cast_exp(t, a, n);
1897
		break;
2 7u83 1898
	}
6 7u83 1899
	case lex_reinterpret_Hcast: {
1900
		e = make_reinterp_cast_exp(t, a, n);
1901
		break;
2 7u83 1902
	}
6 7u83 1903
	case lex_const_Hcast: {
1904
		e = make_const_cast_exp(t, a, n);
1905
		break;
2 7u83 1906
	}
6 7u83 1907
	case lex_dynamic_Hcast: {
1908
		e = make_dynamic_cast_exp(t, a, n);
1909
		break;
2 7u83 1910
	}
6 7u83 1911
	default: {
1912
		e = make_cast_exp(t, a, n);
1913
		break;
2 7u83 1914
	}
6 7u83 1915
	}
1916
	return (e);
2 7u83 1917
}
1918
 
1919
#endif
1920
 
1921
 
1922
/*
1923
    CONSTRUCT A FUNCTION-STYLE CAST EXPRESSION
1924
 
1925
    This routine constructs the function-style cast expression 't ( args )'.
1926
    If args is a single argument, a, then this is identical to '( t ) a'.
1927
    The expression 't ()' can be formed for any type, otherwise t must be
1928
    a class type with a suitable constructor.
1929
*/
1930
 
6 7u83 1931
EXP
1932
make_func_cast_exp(TYPE t, LIST(EXP)args)
2 7u83 1933
{
6 7u83 1934
	EXP e;
1935
	ERROR err = NULL_err;
1936
	unsigned tag = TAG_type(t);
1937
	unsigned len = LENGTH_list(args);
2 7u83 1938
 
6 7u83 1939
	/* Do reference conversions on arguments */
1940
	args = convert_args(args);
2 7u83 1941
 
6 7u83 1942
	/* Check for template types */
1943
	if (tag == type_token_tag && is_templ_type(t)) {
1944
		MAKE_exp_opn(t, lex_cast, args, e);
1945
		return (e);
1946
	}
2 7u83 1947
 
6 7u83 1948
	/* Check for class type with more than one argument */
1949
	if (len > 1 && tag != type_compound_tag) {
1950
		report(crt_loc, ERR_expr_type_conv_many(t));
1951
		len = 1;
1952
	}
2 7u83 1953
 
6 7u83 1954
	if (len == 1) {
1955
		/* A single argument is the same as a normal cast */
1956
		unsigned conv = (unsigned)option_value(OPT_VAL_cast_explicit);
1957
		EXP a = DEREF_exp(HEAD_list(args));
1958
		DESTROY_list(args, SIZE_exp);
1959
		if (tag != type_compound_tag) {
1960
			report(crt_loc, ERR_expr_cast_expl_used());
1961
		}
1962
		e = cast_exp(t, a, &err, conv);
1963
		if (!IS_NULL_err(err)) {
1964
			err = concat_warning(err, ERR_expr_type_conv_bad());
1965
			report(crt_loc, err);
1966
		}
1967
		return (e);
2 7u83 1968
	}
6 7u83 1969
 
1970
	if (len == 0) {
1971
		/* No arguments give a zero value */
1972
		if (IS_type_top_etc(t)) {
1973
			MAKE_exp_value(t, e);
1974
		} else {
1975
			err = check_complete(t);
1976
			if (!IS_NULL_err(err)) {
1977
				/* Can't cast to an incomplete type */
1978
				err = concat_error(err, ERR_expr_type_conv_incompl());
1979
				report(crt_loc, err);
1980
			}
1981
			if (IS_type_array(t)) {
1982
				/* Can't use an array type */
1983
				report(crt_loc, ERR_expr_type_conv_array(t));
1984
			}
1985
			err = check_abstract(t);
1986
			if (!IS_NULL_err(err)) {
1987
				/* Can't cast to an abstract type */
1988
				err = concat_error(err, ERR_class_abstract_cast());
1989
				report(crt_loc, err);
1990
				err = NULL_err;
1991
			}
1992
			e = init_empty(t, cv_none, 1, &err);
1993
			if (!IS_NULL_err(err)) {
1994
				report(crt_loc, err);
1995
			}
1996
		}
1997
		return (e);
2 7u83 1998
	}
1999
 
6 7u83 2000
	/* Now check constructor conversions */
2001
	e = convert_constr(t, args, &err, CAST_STATIC);
2002
	if (!IS_NULL_err(err)) {
2003
		report(crt_loc, err);
2 7u83 2004
	}
6 7u83 2005
	return (e);
2 7u83 2006
}
2007
 
2008
 
2009
/*
2010
    LIST OF ALL CONVERSION TOKENS
2011
 
2012
    Whenever a type conversion would raise an error this list of tokens
2013
    is consulted to see if one of them could perform the conversion.
2014
*/
2015
 
6 7u83 2016
static LIST(IDENTIFIER) conv_tokens = NULL_list(IDENTIFIER);
2 7u83 2017
 
2018
 
2019
/*
2020
    ALLOW A TOKEN AS A CONVERSION
2021
 
2022
    This routine enables the token id as a conversion token.
2023
*/
2024
 
6 7u83 2025
void
2026
allow_conversion(IDENTIFIER id)
2 7u83 2027
{
6 7u83 2028
	IDENTIFIER tid = resolve_token(id, "EE", 1);
2029
	if (!IS_NULL_id(tid)) {
2030
		CONS_id(tid, conv_tokens, conv_tokens);
2031
	}
2032
	return;
2 7u83 2033
}
2034
 
2035
 
2036
/*
2037
    CHECK FOR TOKENISED CONVERSIONS
2038
 
2039
    This routine checks whether the expression a can be converted to the
2040
    type t using a conversion token.  If so it returns the appropriate
2041
    token application.  Otherwise it returns the null expression.
2042
*/
2043
 
6 7u83 2044
EXP
2045
cast_token(TYPE t, EXP a, ERROR *err, ERROR err2, unsigned cast)
2 7u83 2046
{
6 7u83 2047
	EXP e = NULL_exp;
2048
	int sev = ERROR_NONE;
2049
	if (!IS_NULL_err(err2)) {
2050
		sev = DEREF_int(err_severity(err2));
2051
	}
2052
	if (sev == ERROR_SERIOUS) {
2053
		/* Only check illegal conversions */
2054
		force_tokdef++;
2055
		if (cast == CAST_IMPLICIT || (cast & CAST_STATIC)) {
2056
			/* Scan through conversion tokens */
2057
			TYPE s = DEREF_type(exp_type(a));
2058
			LIST(IDENTIFIER)convs = conv_tokens;
2059
			while (!IS_NULL_list(convs)) {
2060
				IDENTIFIER id = DEREF_id(HEAD_list(convs));
2061
				TOKEN tok = DEREF_tok(id_token_sort(id));
2062
				if (IS_tok_func(tok)) {
2063
					/* Function tokens */
2064
					tok = func_proc_token(tok);
2065
				}
2066
				if (IS_tok_proc(tok)) {
2067
					/* Procedure tokens */
2068
					int d;
2069
					TYPE p;
2070
					TOKEN par;
2071
					IDENTIFIER pid;
2072
					LIST(IDENTIFIER)bids;
2073
					LIST(IDENTIFIER)pids;
2 7u83 2074
 
6 7u83 2075
					/* Find result type */
2076
					TOKEN res = DEREF_tok(tok_proc_res(tok));
2077
					TYPE r = DEREF_type(tok_exp_type(res));
2 7u83 2078
 
6 7u83 2079
					/* Find parameter type */
2080
					pids = DEREF_list(tok_proc_pids(tok));
2081
					pid = DEREF_id(HEAD_list(pids));
2082
					par = DEREF_tok(id_token_sort(pid));
2083
					p = DEREF_type(tok_exp_type(par));
2 7u83 2084
 
6 7u83 2085
					/* Check conversion */
2086
					bids = DEREF_list(tok_proc_bids(tok));
2087
					d = save_token_args(bids, NULL_list(TOKEN));
2088
					if (eq_type(s, p) && eq_type(t, r)) {
2089
						LIST(TOKEN)args;
2090
						ERROR err1 = ERR_token_conv(id, s, t);
2091
						IGNORE define_exp_token(pid, a, 1);
2092
						args = make_token_args(id, bids, &err1);
2093
						e = apply_exp_token(id, args, 0);
2094
						if (!IS_NULL_exp(e)) {
2095
							IDENTIFIER fid;
2096
							fid = DEREF_id(id_token_alt(id));
2097
							use_func_id(fid, 0, 0);
2098
							restore_token_args(bids, d);
2099
							destroy_error(err2, 1);
2100
							err2 = err1;
2101
							break;
2102
						}
2103
						destroy_error(err1, 1);
2104
					}
2105
					restore_token_args(bids, d);
2106
				}
2107
				convs = TAIL_list(convs);
2 7u83 2108
			}
2109
		}
6 7u83 2110
		force_tokdef--;
2 7u83 2111
	}
6 7u83 2112
	add_error(err, err2);
2113
	return (e);
2 7u83 2114
}