Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – tendra.SVN – Blame – /branches/tendra5/src/producers/common/construct/cast.c – Rev 2

Subversion Repositories tendra.SVN

Rev

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

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