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 "exp_ops.h"
35
#include "id_ops.h"
36
#include "graph_ops.h"
37
#include "nat_ops.h"
38
#include "nspace_ops.h"
39
#include "member_ops.h"
40
#include "off_ops.h"
41
#include "tok_ops.h"
42
#include "type_ops.h"
43
#include "error.h"
44
#include "catalog.h"
45
#include "allocate.h"
46
#include "basetype.h"
47
#include "capsule.h"
48
#include "cast.h"
49
#include "check.h"
50
#include "chktype.h"
51
#include "class.h"
52
#include "compile.h"
53
#include "constant.h"
54
#include "construct.h"
55
#include "convert.h"
56
#include "copy.h"
57
#include "declare.h"
58
#include "derive.h"
59
#include "destroy.h"
60
#include "dump.h"
61
#include "exception.h"
62
#include "expression.h"
63
#include "function.h"
64
#include "hash.h"
65
#include "identifier.h"
66
#include "initialise.h"
67
#include "instance.h"
68
#include "label.h"
69
#include "literal.h"
70
#include "namespace.h"
71
#include "operator.h"
72
#include "predict.h"
73
#include "redeclare.h"
74
#include "rewrite.h"
75
#include "statement.h"
76
#include "syntax.h"
77
#include "template.h"
78
#include "token.h"
79
 
80
 
81
/*
82
    IS AN EXPRESSION AN UNRESOLVED IMPLICIT CAST?
83
 
84
    This routine checks whether the expression e denotes an unresolved
85
    implicit cast expression.  If so it returns the expression being
86
    cast.  Otherwise the null expression is returned.  The copying
87
    routines work perfectly well for such implicit casts however
88
    spotting them early can help to give a context to any error message.
89
*/
90
 
91
EXP implicit_cast_exp
92
    PROTO_N ( ( e ) )
93
    PROTO_T ( EXP e )
94
{
95
    if ( !IS_NULL_exp ( e ) ) {
96
	unsigned tag = TAG_exp ( e ) ;
97
	if ( tag == exp_op_tag ) {
98
	    /* Check for implicit conversions */
99
	    int op = DEREF_int ( exp_op_lex ( e ) ) ;
100
	    if ( op == lex_implicit ) {
101
		EXP a = DEREF_exp ( exp_op_arg1 ( e ) ) ;
102
		return ( a ) ;
103
	    }
104
	}
105
	if ( tag == exp_aggregate_tag || tag == exp_nof_tag ) {
106
	    /* Check for aggregate initialisers */
107
	    return ( e ) ;
108
	}
109
    }
110
    return ( NULL_exp ) ;
111
}
112
 
113
 
114
/*
115
    COPY A VARIABLE DEFINITION
116
 
117
    This routine defines the variable or static data member id to be e.
118
*/
119
 
120
static void copy_variable
121
    PROTO_N ( ( id, e ) )
122
    PROTO_T ( IDENTIFIER id X EXP e )
123
{
124
    EXP d ;
125
    TYPE t1 = DEREF_type ( id_variable_etc_type ( id ) ) ;
126
    TYPE t2 = expand_type ( t1, 1 ) ;
127
    if ( IS_NULL_exp ( e ) ) {
128
	/* Empty initialiser */
129
	DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
130
	if ( ( ds & dspec_temp ) && ( ds & dspec_explicit ) ) {
131
	    /* Explicitly initialised temporary */
132
	    /* EMPTY */
133
	} else {
134
	    e = init_general ( t2, e, id, LANGUAGE_C ) ;
135
	}
136
    } else {
137
	EXP a = implicit_cast_exp ( e ) ;
138
	if ( !IS_NULL_exp ( a ) ) {
139
	    /* Implicit cast initialiser */
140
	    a = copy_exp ( a, t1, t2 ) ;
141
	    e = init_general ( t2, a, id, 0 ) ;
142
	} else {
143
	    /* Simple initialiser */
144
	    e = copy_exp ( e, t1, t2 ) ;
145
	    e = dynamic_init ( id, NULL_string, e ) ;
146
	}
147
	e = check_init ( e ) ;
148
    }
149
    d = destroy_general ( t2, id ) ;
150
    COPY_exp ( id_variable_etc_term ( id ), d ) ;
151
    COPY_exp ( id_variable_etc_init ( id ), e ) ;
152
    return ;
153
}
154
 
155
 
156
/*
157
    COPY A LOCAL VARIABLE
158
 
159
    This routine copies the local variable id.
160
*/
161
 
162
static IDENTIFIER copy_local
163
    PROTO_N ( ( id ) )
164
    PROTO_T ( IDENTIFIER id )
165
{
166
    IDENTIFIER cid = copy_id ( id, 2 ) ;
167
    COPY_id ( id_alias ( id ), cid ) ;
168
    DEREF_loc ( id_loc ( id ), crt_loc ) ;
169
    decl_loc = crt_loc ;
170
    if ( IS_id_variable ( cid ) ) {
171
	EXP e = DEREF_exp ( id_variable_init ( cid ) ) ;
172
	copy_variable ( cid, e ) ;
173
    }
174
    return ( cid ) ;
175
}
176
 
177
 
178
/*
179
    COPY A TYPE OFFSET
180
 
181
    This routine copies the offset for the type t.  The result must be a
182
    complete object type.
183
*/
184
 
185
static TYPE copy_type_offset
186
    PROTO_N ( ( t, op ) )
187
    PROTO_T ( TYPE t X int op )
188
{
189
    TYPE s = expand_type ( t, 1 ) ;
190
    if ( !EQ_type ( s, t ) ) {
191
	ERROR err = check_complete ( s ) ;
192
	if ( !IS_NULL_err ( err ) ) {
193
	    err = concat_error ( err, ERR_expr_add_incompl ( op ) ) ;
194
	    report ( crt_loc, err ) ;
195
	}
196
    }
197
    return ( s ) ;
198
}
199
 
200
 
201
/*
202
    COPY AN OFFSET
203
 
204
    This routine copies the offset off.
205
*/
206
 
207
OFFSET copy_offset
208
    PROTO_N ( ( off, op ) )
209
    PROTO_T ( OFFSET off X int op )
210
{
211
    if ( IS_NULL_off ( off ) ) return ( NULL_off ) ;
212
    ASSERT ( ORDER_off == 13 ) ;
213
    switch ( TAG_off ( off ) ) {
214
	case off_zero_tag : {
215
	    /* Zero offsets */
216
	    TYPE t = DEREF_type ( off_zero_type ( off ) ) ;
217
	    t = copy_type_offset ( t, op ) ;
218
	    MAKE_off_zero ( t, off ) ;
219
	    break ;
220
	}
221
	case off_type_tag : {
222
	    /* Type offsets */
223
	    TYPE t = DEREF_type ( off_type_type ( off ) ) ;
224
	    t = copy_type_offset ( t, op ) ;
225
	    MAKE_off_type ( t, off ) ;
226
	    break ;
227
	}
228
	case off_array_tag : {
229
	    /* Array type offsets */
230
	    TYPE t = DEREF_type ( off_array_type ( off ) ) ;
231
	    unsigned n = DEREF_unsigned ( off_array_arg ( off ) ) ;
232
	    t = copy_type_offset ( t, op ) ;
233
	    MAKE_off_array ( t, n, off ) ;
234
	    break ;
235
	}
236
	case off_extra_tag : {
237
	    /* Extra allocation offsets */
238
	    TYPE t = DEREF_type ( off_extra_type ( off ) ) ;
239
	    int n = DEREF_int ( off_extra_scale ( off ) ) ;
240
	    t = expand_type ( t, 1 ) ;
241
	    MAKE_off_extra ( t, n, off ) ;
242
	    break ;
243
	}
244
	case off_base_tag : {
245
	    /* Direct base class offsets */
246
	    GRAPH gr = DEREF_graph ( off_base_graph ( off ) ) ;
247
	    gr = expand_graph ( gr, 1 ) ;
248
	    if ( !IS_NULL_graph ( gr ) ) {
249
		off = DEREF_off ( graph_off ( gr ) ) ;
250
	    }
251
	    break ;
252
	}
253
	case off_deriv_tag : {
254
	    /* Indirect base class offsets */
255
	    GRAPH gr = DEREF_graph ( off_deriv_graph ( off ) ) ;
256
	    gr = expand_graph ( gr, 1 ) ;
257
	    if ( !IS_NULL_graph ( gr ) ) {
258
		off = DEREF_off ( graph_off ( gr ) ) ;
259
	    }
260
	    break ;
261
	}
262
	case off_member_tag : {
263
	    /* Member offsets */
264
	    IDENTIFIER id = DEREF_id ( off_member_id ( off ) ) ;
265
	    id = rescan_id ( id, qual_nested, 0 ) ;
266
	    if ( IS_id_member ( id ) ) {
267
		off = DEREF_off ( id_member_off ( id ) ) ;
268
	    }
269
	    break ;
270
	}
271
	case off_ptr_mem_tag : {
272
	    /* Pointer to member offsets */
273
	    EXP a = DEREF_exp ( off_ptr_mem_arg ( off ) ) ;
274
	    a = copy_exp ( a, NULL_type, NULL_type ) ;
275
	    MAKE_off_ptr_mem ( a, off ) ;
276
	    break ;
277
	}
278
	case off_negate_tag : {
279
	    /* Negated offsets */
280
	    OFFSET a = DEREF_off ( off_negate_arg ( off ) ) ;
281
	    op = ( lex_plus + lex_minus ) - op ;
282
	    a = copy_offset ( a, op ) ;
283
	    MAKE_off_negate ( a, off ) ;
284
	    break ;
285
	}
286
	case off_plus_tag : {
287
	    /* Offset additions */
288
	    OFFSET a = DEREF_off ( off_plus_arg1 ( off ) ) ;
289
	    OFFSET b = DEREF_off ( off_plus_arg2 ( off ) ) ;
290
	    a = copy_offset ( a, op ) ;
291
	    b = copy_offset ( b, op ) ;
292
	    MAKE_off_plus ( a, b, off ) ;
293
	    break ;
294
	}
295
	case off_mult_tag : {
296
	    /* Offset multiplications */
297
	    OFFSET a = DEREF_off ( off_mult_arg1 ( off ) ) ;
298
	    EXP b = DEREF_exp ( off_mult_arg2 ( off ) ) ;
299
	    a = copy_offset ( a, op ) ;
300
	    b = copy_exp ( b, NULL_type, NULL_type ) ;
301
	    MAKE_off_mult ( a, b, off ) ;
302
	    break ;
303
	}
304
	case off_ptr_diff_tag : {
305
	    /* Pointer differences */
306
	    EXP a = DEREF_exp ( off_ptr_diff_ptr1 ( off ) ) ;
307
	    EXP b = DEREF_exp ( off_ptr_diff_ptr2 ( off ) ) ;
308
	    TYPE s1 = DEREF_type ( exp_type ( a ) ) ;
309
	    TYPE s2 = expand_type ( s1, 1 ) ;
310
	    a = copy_exp ( a, s1, s2 ) ;
311
	    b = copy_exp ( b, s1, s2 ) ;
312
	    MAKE_off_ptr_diff ( a, b, off ) ;
313
	    break ;
314
	}
315
	case off_token_tag : {
316
	    /* Offset tokens */
317
	    IDENTIFIER tok = DEREF_id ( off_token_tok ( off ) ) ;
318
	    LIST ( TOKEN ) args = DEREF_list ( off_token_args ( off ) ) ;
319
	    tok = DEREF_id ( id_alias ( tok ) ) ;
320
	    args = expand_args ( args, 1, 1 ) ;
321
	    off = apply_mem_token ( tok, args ) ;
322
	    break ;
323
	}
324
    }
325
    return ( off ) ;
326
}
327
 
328
 
329
/*
330
    COPY A LIST OF OFFSETS
331
 
332
    This routine copies the list of offsets p.
333
*/
334
 
335
static LIST ( OFFSET ) copy_off_list
336
    PROTO_N ( ( p ) )
337
    PROTO_T ( LIST ( OFFSET ) p )
338
{
339
    LIST ( OFFSET ) q = NULL_list ( OFFSET ) ;
340
    while ( !IS_NULL_list ( p ) ) {
341
	OFFSET off = DEREF_off ( HEAD_list ( p ) ) ;
342
	off = copy_offset ( off, lex_plus ) ;
343
	CONS_off ( off, q, q ) ;
344
	p = TAIL_list ( p ) ;
345
    }
346
    return ( REVERSE_list ( q ) ) ;
347
}
348
 
349
 
350
/*
351
    COPY A LIST OF EXPRESSIONS
352
 
353
    This routine copies the list of expressions p.
354
*/
355
 
356
LIST ( EXP ) copy_exp_list
357
    PROTO_N ( ( p, t1, t2 ) )
358
    PROTO_T ( LIST ( EXP ) p X TYPE t1 X TYPE t2 )
359
{
360
    LIST ( EXP ) q = NULL_list ( EXP ) ;
361
    while ( !IS_NULL_list ( p ) ) {
362
	EXP e = DEREF_exp ( HEAD_list ( p ) ) ;
363
	e = copy_exp ( e, t1, t2 ) ;
364
	CONS_exp ( e, q, q ) ;
365
	p = TAIL_list ( p ) ;
366
    }
367
    return ( REVERSE_list ( q ) ) ;
368
}
369
 
370
 
371
/*
372
    COPY A FUNCTION EXPRESSION
373
 
374
    This routine copies the function expression e.  Any name look-ups
375
    are postponed until make_func_exp.
376
*/
377
 
378
EXP copy_func_exp
379
    PROTO_N ( ( e, t1, t2 ) )
380
    PROTO_T ( EXP e X TYPE t1 X TYPE t2 )
381
{
382
    if ( !IS_NULL_exp ( e ) ) {
383
	unsigned tag = TAG_exp ( e ) ;
384
	TYPE t = DEREF_type ( exp_type ( e ) ) ;
385
	if ( !EQ_type ( t, t1 ) ) {
386
	    /* Expand type if necessary */
387
	    t1 = t ;
388
	    t2 = expand_type ( t, 1 ) ;
389
	}
390
	switch ( tag ) {
391
	    case exp_identifier_tag :
392
	    case exp_member_tag :
393
	    case exp_ambiguous_tag : {
394
		/* Identifier expressions */
395
		IDENTIFIER id ;
396
		QUALIFIER qual ;
397
		id = DEREF_id ( exp_identifier_etc_id ( e ) ) ;
398
		qual = DEREF_qual ( exp_identifier_etc_qual ( e ) ) ;
399
		MAKE_exp_identifier_etc ( tag, t2, id, qual, e ) ;
400
		break ;
401
	    }
402
	    case exp_undeclared_tag : {
403
		/* Undeclared identifiers */
404
		IDENTIFIER id ;
405
		QUALIFIER qual ;
406
		id = DEREF_id ( exp_undeclared_id ( e ) ) ;
407
		qual = DEREF_qual ( exp_undeclared_qual ( e ) ) ;
408
		MAKE_exp_identifier ( type_func_void, id, qual, e ) ;
409
		break ;
410
	    }
411
	    case exp_paren_tag : {
412
		/* Parenthesised expressions */
413
		EXP a = DEREF_exp ( exp_paren_arg ( e ) ) ;
414
		a = copy_func_exp ( a, t1, t2 ) ;
415
		if ( !IS_NULL_exp ( a ) ) {
416
		    t2 = DEREF_type ( exp_type ( a ) ) ;
417
		}
418
		MAKE_exp_paren ( t2, a, e ) ;
419
		break ;
420
	    }
421
	    case exp_address_tag : {
422
		/* Address expressions */
423
		EXP a = DEREF_exp ( exp_address_arg ( e ) ) ;
424
		a = copy_func_exp ( a, t1, t2 ) ;
425
		MAKE_exp_address ( t2, a, e ) ;
426
		break ;
427
	    }
428
	    case exp_address_mem_tag : {
429
		/* Member address expressions */
430
		EXP a = DEREF_exp ( exp_address_mem_arg ( e ) ) ;
431
		int paren = DEREF_int ( exp_address_mem_paren ( e ) ) ;
432
		a = copy_func_exp ( a, t1, t2 ) ;
433
		MAKE_exp_address_mem ( t2, a, paren, e ) ;
434
		break ;
435
	    }
436
	    case exp_op_tag : {
437
		/* Check for undetermined address expressions */
438
		int op = DEREF_int ( exp_op_lex ( e ) ) ;
439
		EXP a = DEREF_exp ( exp_op_arg1 ( e ) ) ;
440
		EXP b = DEREF_exp ( exp_op_arg2 ( e ) ) ;
441
		if ( op == lex_and_H1 && IS_NULL_exp ( b ) ) {
442
		    a = copy_func_exp ( a, t1, t2 ) ;
443
		    e = make_ref_exp ( a, 0 ) ;
444
		    break ;
445
		}
446
		e = copy_exp ( e, t1, t2 ) ;
447
		break ;
448
	    }
449
	    default : {
450
		/* Other expressions */
451
		e = copy_exp ( e, t1, t2 ) ;
452
		break ;
453
	    }
454
	}
455
    }
456
    return ( e ) ;
457
}
458
 
459
 
460
/*
461
    COPY A LIST OF FUNCTION ARGUMENTS
462
 
463
    This routine copies the list of function arguments p.  id gives the
464
    function name (for error reporting purposes).
465
*/
466
 
467
static LIST ( EXP ) copy_func_args
468
    PROTO_N ( ( p, id ) )
469
    PROTO_T ( LIST ( EXP ) p X IDENTIFIER id )
470
{
471
    unsigned n = 1 ;
472
    LIST ( EXP ) q = NULL_list ( EXP ) ;
473
    while ( !IS_NULL_list ( p ) ) {
474
	EXP e = DEREF_exp ( HEAD_list ( p ) ) ;
475
	EXP a = implicit_cast_exp ( e ) ;
476
	if ( !IS_NULL_exp ( a ) ) {
477
	    /* Do implicit argument conversion */
478
	    TYPE t ;
479
	    ERROR err = NULL_err ;
480
	    a = copy_exp ( a, NULL_type, NULL_type ) ;
481
	    t = DEREF_type ( exp_type ( a ) ) ;
482
	    e = init_assign ( t, cv_none, a, &err ) ;
483
	    if ( !IS_NULL_err ( err ) ) {
484
		err = init_error ( err, 0 ) ;
485
		err = concat_error ( err, ERR_expr_call_arg ( n ) ) ;
486
		if ( !IS_NULL_id ( id ) ) {
487
		    err = concat_error ( ERR_expr_call_func ( id ), err ) ;
488
		}
489
		report ( crt_loc, err ) ;
490
	    }
491
	} else {
492
	    /* Simple argument copy */
493
	    e = copy_exp ( e, NULL_type, NULL_type ) ;
494
	}
495
	CONS_exp ( e, q, q ) ;
496
	n++ ;
497
	p = TAIL_list ( p ) ;
498
    }
499
    return ( REVERSE_list ( q ) ) ;
500
}
501
 
502
 
503
/*
504
    COPY A LABEL
505
 
506
    This routine finds the copy of the label lab in the current label
507
    namespace, copying it if necessary.  If def is true then the labelled
508
    statement is also copied.
509
*/
510
 
511
static IDENTIFIER copy_label
512
    PROTO_N ( ( lab, def ) )
513
    PROTO_T ( IDENTIFIER lab X int def )
514
{
515
    IDENTIFIER nlab = lab ;
516
    if ( !IS_NULL_id ( nlab ) ) {
517
	NAMESPACE ns = label_namespace ;
518
	if ( !IS_NULL_nspace ( ns ) ) {
519
	    /* Look up name in label namespace */
520
	    HASHID nm = DEREF_hashid ( id_name ( lab ) ) ;
521
	    MEMBER mem = search_member ( ns, nm, 1 ) ;
522
	    nlab = DEREF_id ( member_id ( mem ) ) ;
523
	    if ( IS_NULL_id ( nlab ) ) {
524
		/* Create new label */
525
		int op = DEREF_int ( id_label_op ( lab ) ) ;
526
		IDENTIFIER alab = DEREF_id ( id_alias ( lab ) ) ;
527
		DECL_SPEC ds = DEREF_dspec ( id_storage ( lab ) ) ;
528
		DEREF_loc ( id_loc ( lab ), crt_loc ) ;
529
		ds &= ~dspec_temp ;
530
		MAKE_id_label ( nm, ds, ns, crt_loc, op, nlab ) ;
531
		if ( !EQ_id ( lab, alab ) ) {
532
		    alab = copy_label ( alab, 0 ) ;
533
		    COPY_id ( id_alias ( nlab ), alab ) ;
534
		}
535
		COPY_id ( member_id ( mem ), nlab ) ;
536
	    }
537
	}
538
	if ( def ) {
539
	    EXP e = DEREF_exp ( id_label_stmt ( lab ) ) ;
540
	    if ( !IS_NULL_exp ( e ) ) {
541
		/* Copy labelled statement */
542
		TYPE t = DEREF_type ( exp_type ( e ) ) ;
543
		EXP a = DEREF_exp ( exp_label_stmt_body ( e ) ) ;
544
		IDENTIFIER elab = DEREF_id ( exp_label_stmt_next ( e ) ) ;
545
		elab = copy_label ( elab, 0 ) ;
546
		a = copy_exp ( a, type_void, type_void ) ;
547
		MAKE_exp_label_stmt ( t, nlab, a, e ) ;
548
		COPY_id ( exp_label_stmt_next ( e ), elab ) ;
549
		COPY_exp ( id_label_stmt ( nlab ), e ) ;
550
		set_parent_stmt ( a, e ) ;
551
	    }
552
	    DEREF_loc ( id_loc ( lab ), crt_loc ) ;
553
	}
554
    }
555
    return ( nlab ) ;
556
}
557
 
558
 
559
/*
560
    SET JUMP JOIN STATEMENTS
561
 
562
    This routine sets the join field for all jumps to the label lab to
563
    be e.
564
*/
565
 
566
static void set_jump_joins
567
    PROTO_N ( ( lab, e ) )
568
    PROTO_T ( IDENTIFIER lab X EXP e )
569
{
570
    EXP a = DEREF_exp ( id_label_gotos ( lab ) ) ;
571
    while ( !IS_NULL_exp ( a ) && IS_exp_goto_stmt ( a ) ) {
572
	COPY_exp ( exp_goto_stmt_join ( a ), e ) ;
573
	a = DEREF_exp ( exp_goto_stmt_next ( a ) ) ;
574
    }
575
    return ;
576
}
577
 
578
 
579
/*
580
    COPY A SWITCH STATEMENT
581
 
582
    This routine copies the switch statement e.
583
*/
584
 
585
static EXP copy_switch_stmt
586
    PROTO_N ( ( e, t1, t2 ) )
587
    PROTO_T ( EXP e X TYPE t1 X TYPE t2 )
588
{
589
    /* Decompose switch statement */
590
    int changed = 0 ;
591
    LIST ( NAT ) an = NULL_list ( NAT ) ;
592
    LIST ( IDENTIFIER ) al = NULL_list ( IDENTIFIER ) ;
593
    EXP c = DEREF_exp ( exp_switch_stmt_control ( e ) ) ;
594
    EXP a = DEREF_exp ( exp_switch_stmt_body ( e ) ) ;
595
    int exhaust = DEREF_int ( exp_switch_stmt_exhaust ( e ) ) ;
596
    IDENTIFIER blab = DEREF_id ( exp_switch_stmt_break_lab ( e ) ) ;
597
    IDENTIFIER dlab = DEREF_id ( exp_switch_stmt_default_lab ( e ) ) ;
598
    LIST ( NAT ) cn = DEREF_list ( exp_switch_stmt_cases ( e ) ) ;
599
    LIST ( IDENTIFIER ) cl = DEREF_list ( exp_switch_stmt_case_labs ( e ) ) ;
600
 
601
    /* Copy basic components */
602
    c = copy_exp ( c, type_sint, type_sint ) ;
603
    blab = copy_label ( blab, 1 ) ;
604
    a = copy_exp ( a, t1, t2 ) ;
605
    MAKE_exp_switch_stmt ( t2, c, a, exhaust, blab, e ) ;
606
    set_jump_joins ( blab, e ) ;
607
    set_parent_stmt ( a, e ) ;
608
 
609
    /* Copy cases */
610
    while ( !IS_NULL_list ( cn ) ) {
611
	ERROR err = NULL_err ;
612
	NAT n = DEREF_nat ( HEAD_list ( cn ) ) ;
613
	IDENTIFIER clab = DEREF_id ( HEAD_list ( cl ) ) ;
614
	NAT m = expand_nat ( n, 1, 0, &err ) ;
615
	if ( !EQ_nat ( n, m ) ) {
616
	    if ( !IS_NULL_err ( err ) ) {
617
		err = concat_error ( err, ERR_stmt_switch_case_const () ) ;
618
		report ( crt_loc, err ) ;
619
	    }
620
	    changed = 1 ;
621
	}
622
	clab = copy_label ( clab, 0 ) ;
623
	COPY_exp ( id_label_gotos ( clab ), e ) ;
624
	if ( changed ) {
625
	    IDENTIFIER plab = find_case ( an, al, m ) ;
626
	    if ( !IS_NULL_id ( plab ) && !is_error_nat ( m ) ) {
627
		/* New duplicate case created */
628
		LOCATION loc ;
629
		PTR ( LOCATION ) ploc = id_loc ( plab ) ;
630
		DEREF_loc ( id_loc ( clab ), loc ) ;
631
		report ( loc, ERR_stmt_switch_case_dup ( m, ploc ) ) ;
632
            }
633
	}
634
	CONS_id ( clab, al, al ) ;
635
	CONS_nat ( m, an, an ) ;
636
	cl = TAIL_list ( cl ) ;
637
	cn = TAIL_list ( cn ) ;
638
    }
639
    an = REVERSE_list ( an ) ;
640
    al = REVERSE_list ( al ) ;
641
    COPY_list ( exp_switch_stmt_cases ( e ), an ) ;
642
    COPY_list ( exp_switch_stmt_case_labs ( e ), al ) ;
643
    if ( !IS_NULL_id ( dlab ) ) {
644
	dlab = copy_label ( dlab, 0 ) ;
645
	COPY_exp ( id_label_gotos ( dlab ), e ) ;
646
	COPY_id ( exp_switch_stmt_default_lab ( e ), dlab ) ;
647
    }
648
    e = solve_switch ( e ) ;
649
    return ( e ) ;
650
}
651
 
652
 
653
/*
654
    COPY A TRY BLOCK
655
 
656
    This routine copies the try block e.
657
*/
658
 
659
#if LANGUAGE_CPP
660
 
661
static EXP copy_try_stmt
662
    PROTO_N ( ( e, t1, t2 ) )
663
    PROTO_T ( EXP e X TYPE t1 X TYPE t2 )
664
{
665
    int empty = 1 ;
666
    int changed = 0 ;
667
    EXP a = DEREF_exp ( exp_try_block_body ( e ) ) ;
668
    LIST ( EXP ) h = DEREF_list ( exp_try_block_handlers ( e ) ) ;
669
    LIST ( TYPE ) s = DEREF_list ( exp_try_block_htypes ( e ) ) ;
670
    LIST ( TYPE ) t = DEREF_list ( exp_try_block_ttypes ( e ) ) ;
671
    LIST ( LOCATION ) tl = DEREF_list ( exp_try_block_tlocs ( e ) ) ;
672
    LIST ( LOCATION ) sl = NULL_list ( LOCATION ) ;
673
    EXP b = DEREF_exp ( exp_try_block_ellipsis ( e ) ) ;
674
    int func = DEREF_int ( exp_try_block_func ( e ) ) ;
675
    e = begin_try_stmt ( func ) ;
676
    t = expand_exceptions ( t, 1, &changed ) ;
677
    while ( !IS_NULL_list ( tl ) ) {
678
	LOCATION loc ;
679
	DEREF_loc ( HEAD_list ( tl ), loc ) ;
680
	CONS_loc ( loc, sl, sl ) ;
681
	tl = TAIL_list ( tl ) ;
682
    }
683
    sl = REVERSE_list ( sl ) ;
684
    COPY_list ( exp_try_block_ttypes ( e ), t ) ;
685
    COPY_list ( exp_try_block_tlocs ( e ), sl ) ;
686
    a = copy_exp ( a, t1, t2 ) ;
687
    e = cont_try_stmt ( e, a ) ;
688
    h = copy_exp_list ( h, t1, t2 ) ;
689
    s = expand_exceptions ( s, 1, &changed ) ;
690
    b = copy_exp ( b, t1, t2 ) ;
691
    COPY_list ( exp_try_block_handlers ( e ), h ) ;
692
    COPY_list ( exp_try_block_htypes ( e ), s ) ;
693
    COPY_exp ( exp_try_block_ellipsis ( e ), b ) ;
694
    while ( !IS_NULL_list ( h ) ) {
695
	EXP c = DEREF_exp ( HEAD_list ( h ) ) ;
696
	set_parent_stmt ( c, e ) ;
697
	empty = 0 ;
698
	h = TAIL_list ( h ) ;
699
    }
700
    set_parent_stmt ( b, e ) ;
701
    if ( !IS_NULL_exp ( b ) && IS_exp_handler ( b ) ) empty = 0 ;
702
    e = end_try_stmt ( e, empty ) ;
703
    return ( e ) ;
704
}
705
 
706
#endif
707
 
708
 
709
/*
710
    COPY A BLOCK DECLARATION
711
 
712
    This routine copies the object id declared in block scope.  This is
713
    primarily to handle local classes and the like, local variables being
714
    handled by copy_local when their declaration is encountered.
715
*/
716
 
717
static void copy_local_decl
718
    PROTO_N ( ( id ) )
719
    PROTO_T ( IDENTIFIER id )
720
{
721
    switch ( TAG_id ( id ) ) {
722
	case id_variable_tag : {
723
	    /* Local variable declaration */
724
	    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
725
	    if ( ds & dspec_linkage ) {
726
		IDENTIFIER pid ;
727
		TYPE t = DEREF_type ( id_variable_type ( id ) ) ;
728
		t = expand_type ( t, 1 ) ;
729
		pid = make_object_decl ( dspec_extern, t, id, 0 ) ;
730
		pid = DEREF_id ( id_alias ( pid ) ) ;
731
		COPY_id ( id_alias ( id ), pid ) ;
732
	    }
733
	    break ;
734
	}
735
	case id_function_tag : {
736
	    /* Local function declaration */
737
	    IDENTIFIER pid ;
738
	    TYPE t = DEREF_type ( id_function_type ( id ) ) ;
739
	    IDENTIFIER over = DEREF_id ( id_function_over ( id ) ) ;
740
	    if ( !IS_NULL_id ( over ) ) {
741
		copy_local_decl ( over ) ;
742
	    }
743
	    t = expand_type ( t, 1 ) ;
744
	    pid = make_func_decl ( dspec_extern, t, id, 0 ) ;
745
	    pid = DEREF_id ( id_alias ( pid ) ) ;
746
	    COPY_id ( id_alias ( id ), pid ) ;
747
	    break ;
748
	}
749
	case id_class_name_tag : {
750
	    /* Local class */
751
	    TYPE t = DEREF_type ( id_class_name_defn ( id ) ) ;
752
	    if ( IS_type_compound ( t ) ) {
753
		/* Can't be a template class */
754
		CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
755
		CLASS_INFO ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
756
		if ( ci & cinfo_force_copy ) {
757
		    TYPE s ;
758
		    CLASS_TYPE cs ;
759
		    COPY_type ( ctype_form ( ct ), NULL_type ) ;
760
		    s = copy_class ( t, dspec_none ) ;
761
		    cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
762
		    COPY_type ( ctype_form ( ct ), s ) ;
763
		    copy_members ( cs, ct, cinfo_none, 1 ) ;
764
		}
765
	    }
766
	    break ;
767
	}
768
	case id_enumerator_tag : {
769
	    /* Check enumerator values */
770
	    IGNORE copy_id ( id, 2 ) ;
771
	    break ;
772
	}
773
    }
774
    return ;
775
}
776
 
777
 
778
/*
779
    COPY A COMPOUND STATEMENT
780
 
781
    This routine copies the compound statement e.
782
*/
783
 
784
static EXP copy_compound_stmt
785
    PROTO_N ( ( e, t1, t2 ) )
786
    PROTO_T ( EXP e X TYPE t1 X TYPE t2 )
787
{
788
    int block = DEREF_int ( exp_sequence_block ( e ) ) ;
789
    NAMESPACE pns = DEREF_nspace ( exp_sequence_decl ( e ) ) ;
790
    LIST ( EXP ) p = DEREF_list ( exp_sequence_first ( e ) ) ;
791
 
792
    /* Copy the dummy first statement */
793
    LIST ( EXP ) q ;
794
    EXP a = DEREF_exp ( HEAD_list ( p ) ) ;
795
    if ( !IS_NULL_exp ( a ) ) a = copy_exp ( a, t1, t2 ) ;
796
    CONS_exp ( a, NULL_list ( EXP ), q ) ;
797
    p = TAIL_list ( p ) ;
798
 
799
    /* Create the compound statement */
800
    MAKE_exp_sequence ( t2, q, q, NULL_nspace, block, e ) ;
801
    if ( !IS_NULL_list ( p ) ) {
802
	/* Construct namespace for copied block */
803
	MEMBER mem = NULL_member ;
804
	NAMESPACE ns = make_namespace ( crt_func_id, nspace_block_tag, 0 ) ;
805
	COPY_nspace ( exp_sequence_decl ( e ), ns ) ;
806
	push_namespace ( ns ) ;
807
 
808
	/* Copy members of block namespace */
809
	if ( !IS_NULL_nspace ( pns ) ) {
810
	    MEMBER mem2 = DEREF_member ( nspace_last ( pns ) ) ;
811
	    while ( !IS_NULL_member ( mem2 ) ) {
812
		IDENTIFIER id = DEREF_id ( member_id ( mem2 ) ) ;
813
		IDENTIFIER alt = DEREF_id ( member_alt ( mem2 ) ) ;
814
		if ( !IS_NULL_id ( id ) ) {
815
		    copy_local_decl ( id ) ;
816
		}
817
		if ( !IS_NULL_id ( alt ) && !EQ_id ( id, alt ) ) {
818
		    copy_local_decl ( alt ) ;
819
		}
820
		mem2 = DEREF_member ( member_next ( mem2 ) ) ;
821
	    }
822
	}
823
 
824
	/* Copy list of components */
825
	while ( !IS_NULL_list ( p ) ) {
826
	    MEMBER mem2 ;
827
	    LIST ( EXP ) r ;
828
	    a = DEREF_exp ( HEAD_list ( p ) ) ;
829
	    a = copy_exp ( a, t1, t2 ) ;
830
	    mem2 = DEREF_member ( nspace_last ( ns ) ) ;
831
	    if ( !EQ_member ( mem2, mem ) ) {
832
		/* Introduced new temporary variables */
833
		a = make_temp_decl ( mem2, mem, a ) ;
834
		mem = mem2 ;
835
	    }
836
	    set_parent_stmt ( a, e ) ;
837
	    CONS_exp ( a, NULL_list ( EXP ), r ) ;
838
	    COPY_list ( PTR_TAIL_list ( q ), r ) ;
839
	    q = r ;
840
	    p = TAIL_list ( p ) ;
841
	}
842
 
843
	/* Update compound statement */
844
	IGNORE pop_namespace () ;
845
	COPY_member ( nspace_prev ( ns ), mem ) ;
846
	COPY_list ( exp_sequence_last ( e ), q ) ;
847
    }
848
    return ( e ) ;
849
}
850
 
851
 
852
/*
853
    LISTS OF DUMMY EXPRESSIONS
854
 
855
    Dummy expressions are used to point to a component of a complex
856
    expression.  These lists are used to hold all the dummy expressions to
857
    be copied by copy_exp.  They are updated as they are copied.
858
*/
859
 
860
static LIST ( EXP ) input_dummy_exps = NULL_list ( EXP ) ;
861
static LIST ( EXP ) output_dummy_exps = NULL_list ( EXP ) ;
862
 
863
 
864
/*
865
    ADD A DUMMY EXPRESSION TO THE LIST
866
 
867
    This routine adds the expression e to the lists of dummy expressions.
868
*/
869
 
870
static void save_dummy_exp
871
    PROTO_N ( ( e ) )
872
    PROTO_T ( EXP e )
873
{
874
    CONS_exp ( e, input_dummy_exps, input_dummy_exps ) ;
875
    CONS_exp ( NULL_exp, output_dummy_exps, output_dummy_exps ) ;
876
    return ;
877
}
878
 
879
 
880
/*
881
    REMOVE A DUMMY EXPRESSION FROM THE LIST
882
 
883
    This routine removes an expression from the lists of dummy expressions.
884
    It returns the copied values.
885
*/
886
 
887
static EXP restore_dummy_exp
888
    PROTO_Z ()
889
{
890
    EXP a, b ;
891
    DESTROY_CONS_exp ( destroy, a, input_dummy_exps, input_dummy_exps ) ;
892
    DESTROY_CONS_exp ( destroy, b, output_dummy_exps, output_dummy_exps ) ;
893
    if ( IS_NULL_exp ( b ) && !IS_NULL_exp ( a ) ) {
894
	/* Do a straight copy if not copied already */
895
	b = copy_exp ( a, NULL_type, NULL_type ) ;
896
    }
897
    return ( b ) ;
898
}
899
 
900
 
901
/*
902
    COPY A DUMMY EXPRESSION
903
 
904
    This routine copies the dummy expression e, including updating the lists
905
    of dummy expressions.
906
*/
907
 
908
static EXP copy_dummy_exp
909
    PROTO_N ( ( e, t1, t2 ) )
910
    PROTO_T ( EXP e X TYPE t1 X TYPE t2 )
911
{
912
    EXP f ;
913
    LIST ( EXP ) p = input_dummy_exps ;
914
    LIST ( EXP ) q = output_dummy_exps ;
915
    EXP a = DEREF_exp ( exp_dummy_value ( e ) ) ;
916
    ulong_type no = DEREF_ulong ( exp_dummy_no ( e ) ) ;
917
    OFFSET off = DEREF_off ( exp_dummy_off ( e ) ) ;
918
    int virt = DEREF_int ( exp_dummy_virt ( e ) ) ;
919
    int cont = DEREF_int ( exp_dummy_cont ( e ) ) ;
920
    a = copy_exp ( a, t1, t2 ) ;
921
    off = copy_offset ( off, lex_plus ) ;
922
    MAKE_exp_dummy ( t2, a, no, off, cont, f ) ;
923
    COPY_int ( exp_dummy_virt ( f ), virt ) ;
924
    while ( !IS_NULL_list ( p ) ) {
925
	EXP b = DEREF_exp ( HEAD_list ( p ) ) ;
926
	if ( EQ_exp ( b, e ) ) {
927
	    /* Update output lists */
928
	    COPY_exp ( HEAD_list ( q ), f ) ;
929
	}
930
	q = TAIL_list ( q ) ;
931
	p = TAIL_list ( p ) ;
932
    }
933
    return ( f ) ;
934
}
935
 
936
 
937
/*
938
    COPY AN EXPRESSION
939
 
940
    This routine copies the expression e, expanding any template parameters
941
    and tokens.  It is used in the instantiation of template functions.
942
    t1 and t2 are used to prevent repeated type expansions.  t1 gives the
943
    previous expression type and t2 gives its expanded form.
944
*/
945
 
946
EXP copy_exp
947
    PROTO_N ( ( e, t1, t2 ) )
948
    PROTO_T ( EXP e X TYPE t1 X TYPE t2 )
949
{
950
    TYPE t ;
951
    unsigned tag ;
952
    if ( IS_NULL_exp ( e ) ) return ( NULL_exp ) ;
953
    t = DEREF_type ( exp_type ( e ) ) ;
954
    if ( !EQ_type ( t, t1 ) ) {
955
	/* Expand type if necessary */
956
	t1 = t ;
957
	t2 = expand_type ( t, 1 ) ;
958
    }
959
    ASSERT ( ORDER_exp == 88 ) ;
960
    tag = TAG_exp ( e ) ;
961
    switch ( tag ) {
962
	case exp_identifier_tag :
963
	case exp_member_tag :
964
	case exp_ambiguous_tag : {
965
	    /* Identifier expressions */
966
	    IDENTIFIER id = DEREF_id ( exp_identifier_etc_id ( e ) ) ;
967
	    QUALIFIER qual = DEREF_qual ( exp_identifier_etc_qual ( e ) ) ;
968
	    id = DEREF_id ( id_alias ( id ) ) ;
969
	    id = rescan_member ( id ) ;
970
	    MAKE_exp_identifier_etc ( tag, t2, id, qual, e ) ;
971
	    break ;
972
	}
973
	case exp_undeclared_tag : {
974
	    /* Undeclared identifier expressions */
975
	    IDENTIFIER id = DEREF_id ( exp_undeclared_id ( e ) ) ;
976
	    QUALIFIER qual = DEREF_qual ( exp_undeclared_qual ( e ) ) ;
977
	    id = rescan_id ( id, qual, 0 ) ;
978
	    crt_id_qualifier = qual ;
979
	    e = make_id_exp ( id ) ;
980
	    break ;
981
	}
982
	case exp_int_lit_tag : {
983
	    /* Integer literals */
984
	    ERROR err = NULL_err ;
985
	    NAT n = DEREF_nat ( exp_int_lit_nat ( e ) ) ;
986
	    unsigned etag = DEREF_unsigned ( exp_int_lit_etag ( e ) ) ;
987
	    n = expand_nat ( n, 1, 0, &err ) ;
988
	    if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
989
	    MAKE_exp_int_lit ( t2, n, etag, e ) ;
990
	    break ;
991
	}
992
	case exp_float_lit_tag : {
993
	    /* Floating point literals */
994
	    FLOAT flt = DEREF_flt ( exp_float_lit_flt ( e ) ) ;
995
	    MAKE_exp_float_lit ( t2, flt, e ) ;
996
	    break ;
997
	}
998
	case exp_char_lit_tag : {
999
	    /* Character literals */
1000
	    STRING str = DEREF_str ( exp_char_lit_str ( e ) ) ;
1001
	    int digit = DEREF_int ( exp_char_lit_digit ( e ) ) ;
1002
	    MAKE_exp_char_lit ( t2, str, digit, e ) ;
1003
	    break ;
1004
	}
1005
	case exp_string_lit_tag : {
1006
	    /* String literals */
1007
	    STRING str = DEREF_str ( exp_string_lit_str ( e ) ) ;
1008
	    MAKE_exp_string_lit ( t2, str, e ) ;
1009
	    break ;
1010
	}
1011
	case exp_value_tag : {
1012
	    /* Uninitiated expressions */
1013
	    MAKE_exp_value ( t2, e ) ;
1014
	    break ;
1015
	}
1016
	case exp_null_tag : {
1017
	    /* Null expressions */
1018
	    MAKE_exp_null ( t2, e ) ;
1019
	    break ;
1020
	}
1021
	case exp_zero_tag : {
1022
	    /* Zero expressions */
1023
	    MAKE_exp_zero ( t2, e ) ;
1024
	    break ;
1025
	}
1026
	case exp_paren_tag : {
1027
	    /* Parenthesised expressions */
1028
	    EXP a = DEREF_exp ( exp_paren_arg ( e ) ) ;
1029
	    a = copy_exp ( a, t1, t2 ) ;
1030
	    MAKE_exp_paren ( t2, a, e ) ;
1031
	    break ;
1032
	}
1033
	case exp_copy_tag : {
1034
	    /* Copy expressions */
1035
	    EXP a = DEREF_exp ( exp_copy_arg ( e ) ) ;
1036
	    a = copy_exp ( a, t1, t2 ) ;
1037
	    MAKE_exp_copy ( t2, a, e ) ;
1038
	    break ;
1039
	}
1040
	case exp_assign_tag : {
1041
	    /* Assignment expressions */
1042
	    EXP a = DEREF_exp ( exp_assign_ref ( e ) ) ;
1043
	    EXP b = DEREF_exp ( exp_assign_arg ( e ) ) ;
1044
	    a = copy_exp ( a, t1, t2 ) ;
1045
	    b = copy_exp ( b, t1, t2 ) ;
1046
	    MAKE_exp_assign ( t2, a, b, e ) ;
1047
	    break ;
1048
	}
1049
	case exp_init_tag : {
1050
	    /* Initialisation expressions */
1051
	    IDENTIFIER id = DEREF_id ( exp_init_id ( e ) ) ;
1052
	    EXP a = DEREF_exp ( exp_init_arg ( e ) ) ;
1053
	    id = DEREF_id ( id_alias ( id ) ) ;
1054
	    a = copy_exp ( a, t1, t2 ) ;
1055
	    MAKE_exp_init ( t2, id, a, e ) ;
1056
	    break ;
1057
	}
1058
	case exp_preinc_tag : {
1059
	    /* Pre-increment expressions */
1060
	    EXP a = DEREF_exp ( exp_preinc_ref ( e ) ) ;
1061
	    EXP b = DEREF_exp ( exp_preinc_op ( e ) ) ;
1062
	    int becomes = DEREF_int ( exp_preinc_becomes ( e ) ) ;
1063
	    save_dummy_exp ( a ) ;
1064
	    b = copy_exp ( b, t1, t2 ) ;
1065
	    a = restore_dummy_exp () ;
1066
	    MAKE_exp_preinc ( t2, a, b, becomes, e ) ;
1067
	    break ;
1068
	}
1069
	case exp_postinc_tag : {
1070
	    /* Post-increment expressions */
1071
	    EXP a = DEREF_exp ( exp_postinc_ref ( e ) ) ;
1072
	    EXP b = DEREF_exp ( exp_postinc_value ( e ) ) ;
1073
	    EXP c = DEREF_exp ( exp_postinc_op ( e ) ) ;
1074
	    save_dummy_exp ( a ) ;
1075
	    if ( !IS_NULL_exp ( b ) ) {
1076
		save_dummy_exp ( b ) ;
1077
		c = copy_exp ( c, t1, t2 ) ;
1078
		b = restore_dummy_exp () ;
1079
	    } else {
1080
		c = copy_exp ( c, t1, t2 ) ;
1081
	    }
1082
	    a = restore_dummy_exp () ;
1083
	    MAKE_exp_postinc ( t2, a, b, c, e ) ;
1084
	    break ;
1085
	}
1086
	case exp_indir_tag : {
1087
	    /* Indirection expressions */
1088
	    EXP a = DEREF_exp ( exp_indir_ptr ( e ) ) ;
1089
	    int i = DEREF_int ( exp_indir_index ( e ) ) ;
1090
	    a = copy_exp ( a, t1, t2 ) ;
1091
	    MAKE_exp_indir ( t2, a, e ) ;
1092
	    COPY_int ( exp_indir_index ( e ), i ) ;
1093
	    break ;
1094
	}
1095
	case exp_contents_tag : {
1096
	    /* Contents expressions */
1097
	    EXP a = DEREF_exp ( exp_contents_ptr ( e ) ) ;
1098
	    a = copy_exp ( a, t1, t2 ) ;
1099
	    MAKE_exp_contents ( t2, a, e ) ;
1100
	    break ;
1101
	}
1102
	case exp_address_tag : {
1103
	    /* Address expressions */
1104
	    EXP a = DEREF_exp ( exp_address_arg ( e ) ) ;
1105
	    a = copy_exp ( a, t1, t2 ) ;
1106
	    MAKE_exp_address ( t2, a, e ) ;
1107
	    break ;
1108
	}
1109
	case exp_address_mem_tag : {
1110
	    /* Member address expressions */
1111
	    EXP a = DEREF_exp ( exp_address_mem_arg ( e ) ) ;
1112
	    int paren = DEREF_int ( exp_address_mem_paren ( e ) ) ;
1113
	    a = copy_exp ( a, t1, t2 ) ;
1114
	    MAKE_exp_address_mem ( t2, a, paren, e ) ;
1115
	    break ;
1116
	}
1117
	case exp_func_tag : {
1118
	    /* Function expresssions */
1119
	    EXP a = DEREF_exp ( exp_func_fn ( e ) ) ;
1120
	    LIST ( EXP ) args = DEREF_list ( exp_func_args ( e ) ) ;
1121
	    unsigned extra = DEREF_unsigned ( exp_func_extra ( e ) ) ;
1122
	    a = copy_exp ( a, t1, t2 ) ;
1123
	    args = copy_func_args ( args, NULL_id ) ;
1124
	    MAKE_exp_func ( t2, a, args, e ) ;
1125
	    COPY_unsigned ( exp_func_extra ( e ), extra ) ;
1126
	    break ;
1127
	}
1128
	case exp_func_id_tag : {
1129
	    /* Function expresssions */
1130
	    IDENTIFIER id = DEREF_id ( exp_func_id_id ( e ) ) ;
1131
	    LIST ( EXP ) args = DEREF_list ( exp_func_id_args ( e ) ) ;
1132
	    EXP a = DEREF_exp ( exp_func_id_virt ( e ) ) ;
1133
	    unsigned extra = DEREF_unsigned ( exp_func_id_extra ( e ) ) ;
1134
	    id = DEREF_id ( id_alias ( id ) ) ;
1135
	    id = rescan_member ( id ) ;
1136
	    save_dummy_exp ( a ) ;
1137
	    args = copy_func_args ( args, id ) ;
1138
	    a = restore_dummy_exp () ;
1139
	    MAKE_exp_func_id ( t2, id, args, a, e ) ;
1140
	    COPY_unsigned ( exp_func_id_extra ( e ), extra ) ;
1141
	    break ;
1142
	}
1143
	case exp_call_tag : {
1144
	    /* Member call expressions */
1145
	    EXP a = DEREF_exp ( exp_call_ptr ( e ) ) ;
1146
	    EXP b = DEREF_exp ( exp_call_arg ( e ) ) ;
1147
	    GRAPH gr = DEREF_graph ( exp_call_base ( e ) ) ;
1148
	    a = copy_exp ( a, t1, t2 ) ;
1149
	    b = copy_exp ( b, t1, t2 ) ;
1150
	    gr = expand_graph ( gr, 1 ) ;
1151
	    MAKE_exp_call ( t2, a, b, gr, e ) ;
1152
	    break ;
1153
	}
1154
	case exp_negate_tag :
1155
	case exp_compl_tag :
1156
	case exp_not_tag :
1157
	case exp_abs_tag : {
1158
	    /* Unary operations */
1159
	    EXP a = DEREF_exp ( exp_negate_etc_arg ( e ) ) ;
1160
	    a = copy_exp ( a, t1, t2 ) ;
1161
	    MAKE_exp_negate_etc ( tag, t2, a, e ) ;
1162
	    break ;
1163
	}
1164
	case exp_plus_tag :
1165
	case exp_minus_tag :
1166
	case exp_mult_tag :
1167
	case exp_div_tag :
1168
	case exp_rem_tag :
1169
	case exp_and_tag :
1170
	case exp_or_tag :
1171
	case exp_xor_tag :
1172
	case exp_log_and_tag :
1173
	case exp_log_or_tag :
1174
	case exp_lshift_tag :
1175
	case exp_rshift_tag :
1176
	case exp_max_tag :
1177
	case exp_min_tag : {
1178
	    /* Binary operations */
1179
	    EXP a = DEREF_exp ( exp_plus_etc_arg1 ( e ) ) ;
1180
	    EXP b = DEREF_exp ( exp_plus_etc_arg2 ( e ) ) ;
1181
	    a = copy_exp ( a, t1, t2 ) ;
1182
	    b = copy_exp ( b, t1, t2 ) ;
1183
	    MAKE_exp_plus_etc ( tag, t2, a, b, e ) ;
1184
	    break ;
1185
	}
1186
	case exp_test_tag : {
1187
	    /* Test expressions */
1188
	    NTEST tst = DEREF_ntest ( exp_test_tst ( e ) ) ;
1189
	    EXP a = DEREF_exp ( exp_test_arg ( e ) ) ;
1190
	    a = copy_exp ( a, t1, t2 ) ;
1191
	    MAKE_exp_test ( t2, tst, a, e ) ;
1192
	    break ;
1193
	}
1194
	case exp_compare_tag : {
1195
	    /* Comparison expressions */
1196
	    NTEST tst = DEREF_ntest ( exp_compare_tst ( e ) ) ;
1197
	    EXP a = DEREF_exp ( exp_compare_arg1 ( e ) ) ;
1198
	    EXP b = DEREF_exp ( exp_compare_arg2 ( e ) ) ;
1199
	    TYPE s1 = DEREF_type ( exp_type ( a ) ) ;
1200
	    TYPE s2 = expand_type ( s1, 1 ) ;
1201
	    a = copy_exp ( a, s1, s2 ) ;
1202
	    b = copy_exp ( b, s1, s2 ) ;
1203
	    MAKE_exp_compare ( t2, tst, a, b, e ) ;
1204
	    break ;
1205
	}
1206
	case exp_cast_tag : {
1207
	    /* Cast expressions */
1208
	    unsigned conv = DEREF_unsigned ( exp_cast_conv ( e ) ) ;
1209
	    EXP a = DEREF_exp ( exp_cast_arg ( e ) ) ;
1210
	    a = copy_exp ( a, t1, t2 ) ;
1211
	    if ( conv == CONV_EXACT && IS_exp_cast ( a ) ) {
1212
		/* Exact casts are idempotent */
1213
		unsigned conv2 = DEREF_unsigned ( exp_cast_conv ( a ) ) ;
1214
		if ( conv2 == CONV_EXACT ) {
1215
		    a = DEREF_exp ( exp_cast_arg ( a ) ) ;
1216
		}
1217
	    }
1218
	    MAKE_exp_cast ( t2, conv, a, e ) ;
1219
	    break ;
1220
	}
1221
	case exp_base_cast_tag : {
1222
	    /* Base cast expressions */
1223
	    unsigned conv = DEREF_unsigned ( exp_base_cast_conv ( e ) ) ;
1224
	    EXP a = DEREF_exp ( exp_base_cast_arg ( e ) ) ;
1225
	    OFFSET off = DEREF_off ( exp_base_cast_off ( e ) ) ;
1226
	    a = copy_exp ( a, t1, t2 ) ;
1227
	    off = copy_offset ( off, lex_plus ) ;
1228
	    MAKE_exp_base_cast ( t2, conv, a, off, e ) ;
1229
	    break ;
1230
	}
1231
	case exp_dyn_cast_tag : {
1232
	    /* Dynamic cast expressions */
1233
	    EXP a = DEREF_exp ( exp_dyn_cast_arg ( e ) ) ;
1234
	    EXP b = DEREF_exp ( exp_dyn_cast_except ( e ) ) ;
1235
	    a = copy_exp ( a, t1, t2 ) ;
1236
	    b = copy_exp ( b, t1, t2 ) ;
1237
	    MAKE_exp_dyn_cast ( t2, a, b, e ) ;
1238
	    break ;
1239
	}
1240
	case exp_add_ptr_tag : {
1241
	    /* Pointer addition expressions */
1242
	    EXP a = DEREF_exp ( exp_add_ptr_ptr ( e ) ) ;
1243
	    OFFSET off = DEREF_off ( exp_add_ptr_off ( e ) ) ;
1244
	    int virt = DEREF_int ( exp_add_ptr_virt ( e ) ) ;
1245
	    a = copy_exp ( a, t1, t2 ) ;
1246
	    off = copy_offset ( off, lex_plus ) ;
1247
	    MAKE_exp_add_ptr ( t2, a, off, virt, e ) ;
1248
	    break ;
1249
	}
1250
	case exp_offset_size_tag : {
1251
	    /* Offset division expressions */
1252
	    OFFSET off = DEREF_off ( exp_offset_size_off ( e ) ) ;
1253
	    TYPE s = DEREF_type ( exp_offset_size_step ( e ) ) ;
1254
	    int pad = DEREF_int ( exp_offset_size_pad ( e ) ) ;
1255
	    off = copy_offset ( off, lex_minus ) ;
1256
	    s = copy_type_offset ( s, lex_minus ) ;
1257
	    MAKE_exp_offset_size ( t2, off, s, pad, e ) ;
1258
	    break ;
1259
	}
1260
	case exp_constr_tag : {
1261
	    /* Constructor call expressions */
1262
	    EXP a = DEREF_exp ( exp_constr_call ( e ) ) ;
1263
	    EXP b = DEREF_exp ( exp_constr_obj ( e ) ) ;
1264
	    EXP c = DEREF_exp ( exp_constr_alt ( e ) ) ;
1265
	    int info = DEREF_int ( exp_constr_info ( e ) ) ;
1266
	    save_dummy_exp ( b ) ;
1267
	    save_dummy_exp ( c ) ;
1268
	    a = copy_exp ( a, t1, t2 ) ;
1269
	    c = restore_dummy_exp () ;
1270
	    b = restore_dummy_exp () ;
1271
	    MAKE_exp_constr ( t2, a, b, c, info, e ) ;
1272
	    break ;
1273
	}
1274
	case exp_destr_tag : {
1275
	    /* Destructor call expressions */
1276
	    EXP a = DEREF_exp ( exp_destr_call ( e ) ) ;
1277
	    EXP b = DEREF_exp ( exp_destr_obj ( e ) ) ;
1278
	    save_dummy_exp ( b ) ;
1279
	    a = copy_exp ( a, t1, t2 ) ;
1280
	    b = restore_dummy_exp () ;
1281
	    MAKE_exp_destr ( t2, a, b, e ) ;
1282
	    break ;
1283
	}
1284
	case exp_alloc_tag : {
1285
	    /* Allocation call expressions */
1286
	    EXP a = DEREF_exp ( exp_alloc_call ( e ) ) ;
1287
	    EXP b = DEREF_exp ( exp_alloc_init ( e ) ) ;
1288
	    EXP c = DEREF_exp ( exp_alloc_garbage ( e ) ) ;
1289
	    EXP d = DEREF_exp ( exp_alloc_size ( e ) ) ;
1290
	    save_dummy_exp ( d ) ;
1291
	    a = copy_exp ( a, t1, t2 ) ;
1292
	    b = copy_exp ( b, t1, t2 ) ;
1293
	    c = copy_exp ( c, t1, t2 ) ;
1294
	    d = restore_dummy_exp () ;
1295
	    MAKE_exp_alloc ( t2, a, b, c, d, e ) ;
1296
	    break ;
1297
	}
1298
	case exp_dealloc_tag : {
1299
	    /* Deallocation call expressions */
1300
	    EXP a = DEREF_exp ( exp_dealloc_term ( e ) ) ;
1301
	    EXP b = DEREF_exp ( exp_dealloc_call ( e ) ) ;
1302
	    EXP c = DEREF_exp ( exp_dealloc_arg ( e ) ) ;
1303
	    EXP d = DEREF_exp ( exp_dealloc_size ( e ) ) ;
1304
	    save_dummy_exp ( c ) ;
1305
	    save_dummy_exp ( d ) ;
1306
	    a = copy_exp ( a, t1, t2 ) ;
1307
	    b = copy_exp ( b, t1, t2 ) ;
1308
	    d = restore_dummy_exp () ;
1309
	    c = restore_dummy_exp () ;
1310
	    MAKE_exp_dealloc ( t2, a, b, c, d, e ) ;
1311
	    break ;
1312
	}
1313
	case exp_rtti_tag : {
1314
	    /* Run-time type information expressions */
1315
	    EXP a = DEREF_exp ( exp_rtti_arg ( e ) ) ;
1316
	    EXP b = DEREF_exp ( exp_rtti_except ( e ) ) ;
1317
	    int op = DEREF_int ( exp_rtti_op ( e ) ) ;
1318
	    a = copy_exp ( a, t1, t2 ) ;
1319
	    b = copy_exp ( b, t1, t2 ) ;
1320
	    MAKE_exp_rtti ( t2, a, b, op, e ) ;
1321
	    break ;
1322
	}
1323
	case exp_rtti_type_tag : {
1324
	    /* Run-time type information expressions */
1325
	    TYPE s = DEREF_type ( exp_rtti_type_arg ( e ) ) ;
1326
	    int op = DEREF_int ( exp_rtti_type_op ( e ) ) ;
1327
	    s = expand_type ( s, 1 ) ;
1328
	    MAKE_exp_rtti_type ( t2, s, op, e ) ;
1329
	    break ;
1330
	}
1331
	case exp_rtti_no_tag : {
1332
	    /* Run-time type information expressions */
1333
	    TYPE s = DEREF_type ( exp_rtti_no_arg ( e ) ) ;
1334
	    s = expand_type ( s, 1 ) ;
1335
	    MAKE_exp_rtti_no ( t2, s, e ) ;
1336
	    break ;
1337
	}
1338
	case exp_dynamic_tag : {
1339
	    /* Dynamic initialiser expressions */
1340
	    EXP a = DEREF_exp ( exp_dynamic_arg ( e ) ) ;
1341
	    a = copy_exp ( a, t1, t2 ) ;
1342
	    MAKE_exp_dynamic ( t2, a, e ) ;
1343
	    break ;
1344
	}
1345
	case exp_aggregate_tag : {
1346
	    /* Aggregate initialisers */
1347
	    LIST ( EXP ) args = DEREF_list ( exp_aggregate_args ( e ) ) ;
1348
	    LIST ( OFFSET ) offs = DEREF_list ( exp_aggregate_offs ( e ) ) ;
1349
	    args = copy_exp_list ( args, t1, t2 ) ;
1350
	    offs = copy_off_list ( offs ) ;
1351
	    MAKE_exp_aggregate ( t2, args, offs, e ) ;
1352
	    break ;
1353
	}
1354
	case exp_initialiser_tag : {
1355
	    /* Constructor initialisers */
1356
	    int kind = DEREF_int ( exp_initialiser_kind ( e ) ) ;
1357
	    if ( kind ) {
1358
		/* Copy ctor-initialiser */
1359
		e = copy_ctor ( e, kind ) ;
1360
	    } else {
1361
		unsigned nv, nb ;
1362
		LIST ( EXP ) args ;
1363
		LIST ( OFFSET ) offs ;
1364
		args = DEREF_list ( exp_initialiser_args ( e ) ) ;
1365
		offs = DEREF_list ( exp_initialiser_offs ( e ) ) ;
1366
		nv = DEREF_unsigned ( exp_initialiser_virt ( e ) ) ;
1367
		nb = DEREF_unsigned ( exp_initialiser_base ( e ) ) ;
1368
		args = copy_exp_list ( args, t1, t2 ) ;
1369
		offs = copy_off_list ( offs ) ;
1370
		MAKE_exp_initialiser ( t2, args, offs, kind, nv, nb, e ) ;
1371
	    }
1372
	    break ;
1373
	}
1374
	case exp_nof_tag : {
1375
	    /* Array initialisers */
1376
	    ERROR err = NULL_err ;
1377
	    EXP a = DEREF_exp ( exp_nof_start ( e ) ) ;
1378
	    EXP b = DEREF_exp ( exp_nof_pad ( e ) ) ;
1379
	    EXP c = DEREF_exp ( exp_nof_end ( e ) ) ;
1380
	    NAT n = DEREF_nat ( exp_nof_size ( e ) ) ;
1381
	    NAT m = expand_nat ( n, 1, 0, &err ) ;
1382
	    a = copy_exp ( a, t1, t2 ) ;
1383
	    if ( !EQ_nat ( n, m ) ) {
1384
		if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
1385
		if ( is_zero_nat ( m ) ) {
1386
		    /* No extra elements */
1387
		    e = a ;
1388
		    break ;
1389
		}
1390
		if ( is_negative_nat ( m ) ) {
1391
		    /* Number of extra elements has gone negative */
1392
		    report ( crt_loc, ERR_dcl_init_aggr_excess ( t2 ) ) ;
1393
		    e = a ;
1394
		    break ;
1395
		}
1396
	    }
1397
	    b = copy_exp ( b, t1, t2 ) ;
1398
	    c = copy_exp ( c, t1, t2 ) ;
1399
	    MAKE_exp_nof ( t2, a, m, b, c, e ) ;
1400
	    break ;
1401
	}
1402
	case exp_comma_tag : {
1403
	    /* Comma expressions */
1404
	    LIST ( EXP ) args = DEREF_list ( exp_comma_args ( e ) ) ;
1405
	    args = copy_exp_list ( args, t1, t2 ) ;
1406
	    MAKE_exp_comma ( t2, args, e ) ;
1407
	    break ;
1408
	}
1409
	case exp_set_tag : {
1410
	    /* Variable set expressions */
1411
	    EXP a = DEREF_exp ( exp_set_arg ( e ) ) ;
1412
	    a = copy_exp ( a, t1, t2 ) ;
1413
	    MAKE_exp_set ( t2, a, e ) ;
1414
	    break ;
1415
	}
1416
	case exp_unused_tag : {
1417
	    /* Variable unset expressions */
1418
	    EXP a = DEREF_exp ( exp_unused_arg ( e ) ) ;
1419
	    a = copy_exp ( a, t1, t2 ) ;
1420
	    MAKE_exp_unused ( t2, a, e ) ;
1421
	    break ;
1422
	}
1423
	case exp_reach_tag : {
1424
	    /* Reached statements */
1425
	    EXP a = DEREF_exp ( exp_reach_body ( e ) ) ;
1426
	    a = copy_exp ( a, t1, t2 ) ;
1427
	    MAKE_exp_reach ( t2, a, e ) ;
1428
	    set_parent_stmt ( a, e ) ;
1429
	    break ;
1430
	}
1431
	case exp_unreach_tag : {
1432
	    /* Unreached statements */
1433
	    EXP a = DEREF_exp ( exp_unreach_body ( e ) ) ;
1434
	    a = copy_exp ( a, t1, t2 ) ;
1435
	    MAKE_exp_unreach ( t2, a, e ) ;
1436
	    set_parent_stmt ( a, e ) ;
1437
	    break ;
1438
	}
1439
	case exp_sequence_tag : {
1440
	    /* Block statements */
1441
	    e = copy_compound_stmt ( e, t1, t2 ) ;
1442
	    break ;
1443
	}
1444
	case exp_solve_stmt_tag : {
1445
	    /* Solve statements */
1446
	    EXP a = DEREF_exp ( exp_solve_stmt_body ( e ) ) ;
1447
	    a = copy_exp ( a, t1, t2 ) ;
1448
	    MAKE_exp_solve_stmt ( t2, a, e ) ;
1449
	    CONS_exp ( e, all_solve_stmts, all_solve_stmts ) ;
1450
	    set_parent_stmt ( a, e ) ;
1451
	    break ;
1452
	}
1453
	case exp_decl_stmt_tag : {
1454
	    /* Declaration statements */
1455
	    IDENTIFIER id = DEREF_id ( exp_decl_stmt_id ( e ) ) ;
1456
	    IDENTIFIER aid = DEREF_id ( id_alias ( id ) ) ;
1457
	    IDENTIFIER cid = copy_local ( id ) ;
1458
	    EXP a = DEREF_exp ( exp_decl_stmt_body ( e ) ) ;
1459
	    a = copy_exp ( a, t1, t2 ) ;
1460
	    MAKE_exp_decl_stmt ( t2, cid, a, e ) ;
1461
	    set_parent_stmt ( a, e ) ;
1462
	    COPY_id ( id_alias ( id ), aid ) ;
1463
	    break ;
1464
	}
1465
	case exp_if_stmt_tag : {
1466
	    /* Conditional statements */
1467
	    EXP c = DEREF_exp ( exp_if_stmt_cond ( e ) ) ;
1468
	    EXP a = DEREF_exp ( exp_if_stmt_true_code ( e ) ) ;
1469
	    EXP b = DEREF_exp ( exp_if_stmt_false_code ( e ) ) ;
1470
	    IDENTIFIER lab = DEREF_id ( exp_if_stmt_label ( e ) ) ;
1471
	    c = copy_exp ( c, type_bool, type_bool ) ;
1472
	    lab = copy_label ( lab, 1 ) ;
1473
	    a = copy_exp ( a, t1, t2 ) ;
1474
	    b = copy_exp ( b, t1, t2 ) ;
1475
	    MAKE_exp_if_stmt ( t2, c, a, b, lab, e ) ;
1476
	    set_parent_stmt ( a, e ) ;
1477
	    set_parent_stmt ( b, e ) ;
1478
	    break ;
1479
	}
1480
	case exp_while_stmt_tag : {
1481
	    /* While statements */
1482
	    LIST ( IDENTIFIER ) pids ;
1483
	    EXP c = DEREF_exp ( exp_while_stmt_cond ( e ) ) ;
1484
	    EXP a = DEREF_exp ( exp_while_stmt_body ( e ) ) ;
1485
	    IDENTIFIER blab = DEREF_id ( exp_while_stmt_break_lab ( e ) ) ;
1486
	    IDENTIFIER clab = DEREF_id ( exp_while_stmt_cont_lab ( e ) ) ;
1487
	    IDENTIFIER llab = DEREF_id ( exp_while_stmt_loop_lab ( e ) ) ;
1488
	    c = copy_exp ( c, type_bool, type_bool ) ;
1489
	    blab = copy_label ( blab, 1 ) ;
1490
	    clab = copy_label ( clab, 1 ) ;
1491
	    llab = copy_label ( llab, 1 ) ;
1492
	    pids = DEREF_list ( exp_while_stmt_cond_id ( e ) ) ;
1493
	    a = copy_exp ( a, t1, t2 ) ;
1494
	    MAKE_exp_while_stmt ( t2, c, blab, clab, llab, e ) ;
1495
	    COPY_exp ( exp_while_stmt_body ( e ), a ) ;
1496
	    set_jump_joins ( blab, e ) ;
1497
	    set_jump_joins ( clab, e ) ;
1498
	    set_parent_stmt ( a, e ) ;
1499
	    if ( !IS_NULL_list ( pids ) ) {
1500
		LIST ( IDENTIFIER ) qids = NULL_list ( IDENTIFIER ) ;
1501
		while ( !IS_NULL_list ( pids ) ) {
1502
		    IDENTIFIER id = DEREF_id ( HEAD_list ( pids ) ) ;
1503
		    id = DEREF_id ( id_alias ( id ) ) ;
1504
		    CONS_id ( id, qids, qids ) ;
1505
		    pids = TAIL_list ( pids ) ;
1506
		}
1507
		qids = REVERSE_list ( qids ) ;
1508
		COPY_list ( exp_while_stmt_cond_id ( e ), qids ) ;
1509
	    }
1510
	    break ;
1511
	}
1512
	case exp_do_stmt_tag : {
1513
	    /* Do statements */
1514
	    EXP c = DEREF_exp ( exp_do_stmt_cond ( e ) ) ;
1515
	    EXP a = DEREF_exp ( exp_do_stmt_body ( e ) ) ;
1516
	    IDENTIFIER blab = DEREF_id ( exp_do_stmt_break_lab ( e ) ) ;
1517
	    IDENTIFIER clab = DEREF_id ( exp_do_stmt_cont_lab ( e ) ) ;
1518
	    IDENTIFIER llab = DEREF_id ( exp_do_stmt_loop_lab ( e ) ) ;
1519
	    blab = copy_label ( blab, 1 ) ;
1520
	    clab = copy_label ( clab, 1 ) ;
1521
	    llab = copy_label ( llab, 1 ) ;
1522
	    a = copy_exp ( a, t1, t2 ) ;
1523
	    c = copy_exp ( c, type_bool, type_bool ) ;
1524
	    MAKE_exp_do_stmt ( t2, c, blab, clab, llab, e ) ;
1525
	    COPY_exp ( exp_do_stmt_body ( e ), a ) ;
1526
	    set_jump_joins ( blab, e ) ;
1527
	    set_jump_joins ( clab, e ) ;
1528
	    set_parent_stmt ( a, e ) ;
1529
	    break ;
1530
	}
1531
	case exp_switch_stmt_tag : {
1532
	    /* Switch statements */
1533
	    e = copy_switch_stmt ( e, t1, t2 ) ;
1534
	    break ;
1535
	}
1536
	case exp_hash_if_tag : {
1537
	    /* Target dependent conditional statements */
1538
	    EXP c = DEREF_exp ( exp_hash_if_cond ( e ) ) ;
1539
	    EXP a = DEREF_exp ( exp_hash_if_true_code ( e ) ) ;
1540
	    EXP b = DEREF_exp ( exp_hash_if_false_code ( e ) ) ;
1541
	    c = copy_exp ( c, type_bool, type_bool ) ;
1542
	    a = copy_exp ( a, t1, t2 ) ;
1543
	    b = copy_exp ( b, t1, t2 ) ;
1544
	    MAKE_exp_hash_if ( t2, c, a, b, e ) ;
1545
	    set_parent_stmt ( a, e ) ;
1546
	    set_parent_stmt ( b, e ) ;
1547
	    break ;
1548
	}
1549
	case exp_return_stmt_tag : {
1550
	    /* Return statements */
1551
	    EXP a = DEREF_exp ( exp_return_stmt_value ( e ) ) ;
1552
	    EXP b = implicit_cast_exp ( a ) ;
1553
	    if ( !IS_NULL_exp ( b ) ) {
1554
		IDENTIFIER lab = NULL_id ;
1555
		b = copy_exp ( b, t1, t2 ) ;
1556
		a = find_return_exp ( b, &lab, lex_return ) ;
1557
	    } else {
1558
		a = copy_exp ( a, t1, t2 ) ;
1559
	    }
1560
	    MAKE_exp_return_stmt ( t2, a, e ) ;
1561
	    break ;
1562
	}
1563
	case exp_goto_stmt_tag : {
1564
	    /* Goto statements */
1565
	    IDENTIFIER lab = DEREF_id ( exp_goto_stmt_label ( e ) ) ;
1566
	    lab = copy_label ( lab, 0 ) ;
1567
	    e = make_jump_stmt ( lab, NULL_exp ) ;
1568
	    break ;
1569
	}
1570
	case exp_label_stmt_tag : {
1571
	    /* Labelled statements */
1572
	    IDENTIFIER lab = DEREF_id ( exp_label_stmt_label ( e ) ) ;
1573
	    lab = copy_label ( lab, 1 ) ;
1574
	    e = DEREF_exp ( id_label_stmt ( lab ) ) ;
1575
	    break ;
1576
	}
1577
#if LANGUAGE_CPP
1578
	case exp_try_block_tag : {
1579
	    /* Try blocks */
1580
	    e = copy_try_stmt ( e, t1, t2 ) ;
1581
	    break ;
1582
	}
1583
	case exp_handler_tag : {
1584
	    /* Exception handlers */
1585
	    IDENTIFIER id = DEREF_id ( exp_handler_except ( e ) ) ;
1586
	    IDENTIFIER aid = DEREF_id ( id_alias ( id ) ) ;
1587
	    IDENTIFIER cid = copy_local ( id ) ;
1588
	    EXP a = DEREF_exp ( exp_handler_body ( e ) ) ;
1589
	    a = copy_exp ( a, t1, t2 ) ;
1590
	    MAKE_exp_handler ( t2, cid, a, e ) ;
1591
	    set_parent_stmt ( a, e ) ;
1592
	    COPY_id ( id_alias ( id ), aid ) ;
1593
	    break ;
1594
	}
1595
	case exp_exception_tag : {
1596
	    /* Exception expressions */
1597
	    EXP a = DEREF_exp ( exp_exception_arg ( e ) ) ;
1598
	    EXP b = DEREF_exp ( exp_exception_size ( e ) ) ;
1599
	    EXP c = DEREF_exp ( exp_exception_destr ( e ) ) ;
1600
	    int expl = DEREF_int ( exp_exception_expl ( e ) ) ;
1601
	    a = copy_exp ( a, t1, t2 ) ;
1602
	    b = copy_exp ( b, t1, t2 ) ;
1603
	    c = copy_exp ( c, t1, t2 ) ;
1604
	    MAKE_exp_exception ( t2, a, b, c, expl, e ) ;
1605
	    break ;
1606
	}
1607
	case exp_thrown_tag : {
1608
	    /* Thrown expressions */
1609
	    int d = DEREF_int ( exp_thrown_done ( e ) ) ;
1610
	    MAKE_exp_thrown ( t2, d, e ) ;
1611
	    break ;
1612
	}
1613
#endif
1614
	case exp_op_tag : {
1615
	    /* Undetermined expressions */
1616
	    int op = DEREF_int ( exp_op_lex ( e ) ) ;
1617
	    EXP a = DEREF_exp ( exp_op_arg1 ( e ) ) ;
1618
	    EXP b = DEREF_exp ( exp_op_arg2 ( e ) ) ;
1619
	    if ( IS_NULL_exp ( b ) ) {
1620
		e = apply_unary ( op, a, t1, t2, 1 ) ;
1621
	    } else {
1622
		e = apply_binary ( op, a, b, t1, t2, 1 ) ;
1623
	    }
1624
	    break ;
1625
	}
1626
	case exp_opn_tag : {
1627
	    /* Undetermined expressions */
1628
	    int op = DEREF_int ( exp_opn_lex ( e ) ) ;
1629
	    LIST ( EXP ) args = DEREF_list ( exp_opn_args ( e ) ) ;
1630
	    e = apply_nary ( op, args, t1, t2, 1 ) ;
1631
	    break ;
1632
	}
1633
	case exp_assembler_tag : {
1634
	    /* Assembler expressions */
1635
	    STRING op = DEREF_str ( exp_assembler_op ( e ) ) ;
1636
	    LIST ( EXP ) args = DEREF_list ( exp_assembler_args ( e ) ) ;
1637
	    args = copy_exp_list ( args, t1, t2 ) ;
1638
	    MAKE_exp_assembler ( t2, op, args, e ) ;
1639
	    break ;
1640
	}
1641
	case exp_uncompiled_tag : {
1642
	    /* Uncompiled expressions */
1643
	    PPTOKEN *p = DEREF_pptok ( exp_uncompiled_defn ( e ) ) ;
1644
	    DEREF_loc ( exp_uncompiled_start ( e ), crt_loc ) ;
1645
	    MAKE_exp_uncompiled ( t2, crt_loc, p, e ) ;
1646
	    break ;
1647
	}
1648
	case exp_location_tag : {
1649
	    /* Location expressions */
1650
	    EXP a = DEREF_exp ( exp_location_arg ( e ) ) ;
1651
	    a = copy_exp ( a, t1, t2 ) ;
1652
	    DEREF_loc ( exp_location_end ( e ), crt_loc ) ;
1653
	    MAKE_exp_location ( t2, crt_loc, a, e ) ;
1654
	    break ;
1655
	}
1656
	case exp_fail_tag : {
1657
	    /* Installer error expressions */
1658
	    string msg = DEREF_string ( exp_fail_msg ( e ) ) ;
1659
	    MAKE_exp_fail ( t2, msg, e ) ;
1660
	    break ;
1661
	}
1662
	case exp_dummy_tag : {
1663
	    /* Dummy expressions */
1664
	    e = copy_dummy_exp ( e, t1, t2 ) ;
1665
	    break ;
1666
	}
1667
	case exp_token_tag : {
1668
	    /* Expression tokens */
1669
	    e = expand_exp ( e, 1, 0 ) ;
1670
	    break ;
1671
	}
1672
    }
1673
    return ( e ) ;
1674
}
1675
 
1676
 
1677
/*
1678
    EVALUATE A CONSTANT EXPRESSION
1679
 
1680
    This routine evaluates the integer constant expression e, expanding
1681
    any template parameters and tokens.  If ch is true then any character
1682
    literals are replaced by their ASCII values.
1683
*/
1684
 
1685
EXP eval_exp
1686
    PROTO_N ( ( e, ch ) )
1687
    PROTO_T ( EXP e X int ch )
1688
{
1689
    unsigned tag ;
1690
    if ( IS_NULL_exp ( e ) ) return ( NULL_exp ) ;
1691
    ASSERT ( ORDER_exp == 88 ) ;
1692
    tag = TAG_exp ( e ) ;
1693
    switch ( tag ) {
1694
	case exp_int_lit_tag : {
1695
	    /* Integer literals */
1696
	    ERROR err = NULL_err ;
1697
	    NAT n1 = DEREF_nat ( exp_int_lit_nat ( e ) ) ;
1698
	    NAT n2 = expand_nat ( n1, 1, ch, &err ) ;
1699
	    if ( !EQ_nat ( n1, n2 ) ) {
1700
		TYPE t ;
1701
		unsigned etag = DEREF_unsigned ( exp_int_lit_etag ( e ) ) ;
1702
		if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
1703
		if ( IS_nat_calc ( n2 ) ) {
1704
		    if ( etag == exp_identifier_tag ) break ;
1705
		    e = DEREF_exp ( nat_calc_value ( n2 ) ) ;
1706
		    if ( IS_exp_int_lit ( e ) ) break ;
1707
		}
1708
		t = DEREF_type ( exp_type ( e ) ) ;
1709
		t = expand_type ( t, 1 ) ;
1710
		MAKE_exp_int_lit ( t, n2, etag, e ) ;
1711
	    }
1712
	    break ;
1713
	}
1714
	case exp_char_lit_tag : {
1715
	    /* Character literals */
1716
	    if ( ch ) {
1717
		TYPE t = DEREF_type ( exp_type ( e ) ) ;
1718
		STRING s = DEREF_str ( exp_char_lit_str ( e ) ) ;
1719
		NAT n = eval_char_lit ( s ) ;
1720
		if ( check_nat_range ( t, n ) != 0 ) {
1721
		    /* n may not fit into t */
1722
		    TYPE u = find_char_type ( n ) ;
1723
		    MAKE_exp_int_lit ( u, n, tag, e ) ;
1724
		    MAKE_exp_cast ( t, CONV_INT_INT, e, e ) ;
1725
		    MAKE_nat_calc ( e, n ) ;
1726
		    tag = exp_cast_tag ;
1727
		}
1728
		MAKE_exp_int_lit ( t, n, tag, e ) ;
1729
	    } else {
1730
		e = copy_exp ( e, NULL_type, NULL_type ) ;
1731
	    }
1732
	    break ;
1733
	}
1734
	case exp_paren_tag : {
1735
	    /* Parenthesised expressions */
1736
	    EXP a = DEREF_exp ( exp_paren_arg ( e ) ) ;
1737
	    e = eval_exp ( a, ch ) ;
1738
	    break ;
1739
	}
1740
	case exp_negate_tag :
1741
	case exp_compl_tag :
1742
	case exp_not_tag :
1743
	case exp_abs_tag : {
1744
	    /* Unary operations */
1745
	    EXP a1 = DEREF_exp ( exp_negate_etc_arg ( e ) ) ;
1746
	    EXP a2 = eval_exp ( a1, ch ) ;
1747
	    if ( !EQ_exp ( a1, a2 ) ) {
1748
		int op = op_token ( e, lex_unknown ) ;
1749
		e = apply_unary ( op, a2, NULL_type, NULL_type, 0 ) ;
1750
	    }
1751
	    break ;
1752
	}
1753
	case exp_plus_tag :
1754
	case exp_minus_tag :
1755
	case exp_mult_tag :
1756
	case exp_div_tag :
1757
	case exp_rem_tag :
1758
	case exp_and_tag :
1759
	case exp_or_tag :
1760
	case exp_xor_tag :
1761
	case exp_log_and_tag :
1762
	case exp_log_or_tag :
1763
	case exp_lshift_tag :
1764
	case exp_rshift_tag :
1765
	case exp_max_tag :
1766
	case exp_min_tag : {
1767
	    /* Binary operations */
1768
	    EXP a1 = DEREF_exp ( exp_plus_etc_arg1 ( e ) ) ;
1769
	    EXP b1 = DEREF_exp ( exp_plus_etc_arg2 ( e ) ) ;
1770
	    EXP a2 = eval_exp ( a1, ch ) ;
1771
	    EXP b2 = eval_exp ( b1, ch ) ;
1772
	    if ( !EQ_exp ( a1, a2 ) || !EQ_exp ( b1, b2 ) ) {
1773
		int op = op_token ( e, lex_unknown ) ;
1774
		e = apply_binary ( op, a2, b2, NULL_type, NULL_type, 0 ) ;
1775
	    }
1776
	    break ;
1777
	}
1778
	case exp_test_tag : {
1779
	    /* Test expressions */
1780
	    EXP a1 = DEREF_exp ( exp_test_arg ( e ) ) ;
1781
	    EXP a2 = eval_exp ( a1, ch ) ;
1782
	    if ( !EQ_exp ( a1, a2 ) ) {
1783
		int op = op_token ( e, lex_unknown ) ;
1784
		TYPE t2 = DEREF_type ( exp_type ( a2 ) ) ;
1785
		EXP b2 = make_null_exp ( t2 ) ;
1786
		e = apply_binary ( op, a2, b2, NULL_type, NULL_type, 0 ) ;
1787
	    }
1788
	    break ;
1789
	}
1790
	case exp_compare_tag : {
1791
	    /* Comparison expressions */
1792
	    EXP a1 = DEREF_exp ( exp_compare_arg1 ( e ) ) ;
1793
	    EXP b1 = DEREF_exp ( exp_compare_arg2 ( e ) ) ;
1794
	    EXP a2 = eval_exp ( a1, ch ) ;
1795
	    EXP b2 = eval_exp ( b1, ch ) ;
1796
	    if ( !EQ_exp ( a1, a2 ) || !EQ_exp ( b1, b2 ) ) {
1797
		int op = op_token ( e, lex_unknown ) ;
1798
		e = apply_binary ( op, a2, b2, NULL_type, NULL_type, 0 ) ;
1799
	    }
1800
	    break ;
1801
	}
1802
	case exp_cast_tag : {
1803
	    /* Cast expressions */
1804
	    unsigned conv = DEREF_unsigned ( exp_cast_conv ( e ) ) ;
1805
	    EXP a1 = DEREF_exp ( exp_cast_arg ( e ) ) ;
1806
	    EXP a2 = eval_exp ( a1, ch ) ;
1807
	    if ( !EQ_exp ( a1, a2 ) || conv == CONV_ENUM ) {
1808
		ERROR err = NULL_err ;
1809
		TYPE t = DEREF_type ( exp_type ( e ) ) ;
1810
		t = expand_type ( t, 1 ) ;
1811
		a2 = convert_reference ( a2, REF_ASSIGN ) ;
1812
		e = cast_exp ( t, a2, &err, CAST_STATIC ) ;
1813
		if ( !IS_NULL_err ( err ) ) {
1814
		    err = concat_warning ( err, ERR_expr_cast_expl_bad () ) ;
1815
		    report ( crt_loc, err ) ;
1816
		}
1817
	    }
1818
	    break ;
1819
	}
1820
	case exp_offset_size_tag : {
1821
	    /* Offset division expressions */
1822
	    OFFSET off = DEREF_off ( exp_offset_size_off ( e ) ) ;
1823
	    TYPE s = DEREF_type ( exp_offset_size_step ( e ) ) ;
1824
	    if ( IS_off_type ( off ) && EQ_type ( s, type_char ) ) {
1825
		TYPE t1 = DEREF_type ( off_type_type ( off ) ) ;
1826
		TYPE t2 = expand_type ( t1, 1 ) ;
1827
		if ( !EQ_type ( t1, t2 ) ) {
1828
		    /* Evaluate sizeof expressions */
1829
		    e = make_sizeof_exp ( t2, NULL_exp, 0, lex_sizeof ) ;
1830
		}
1831
	    }
1832
	    break ;
1833
	}
1834
	case exp_if_stmt_tag : {
1835
	    /* Conditional statements */
1836
	    EXP c1 = DEREF_exp ( exp_if_stmt_cond ( e ) ) ;
1837
	    EXP a1 = DEREF_exp ( exp_if_stmt_true_code ( e ) ) ;
1838
	    EXP b1 = DEREF_exp ( exp_if_stmt_false_code ( e ) ) ;
1839
	    EXP c2 = eval_exp ( c1, ch ) ;
1840
	    EXP a2 = eval_exp ( a1, ch ) ;
1841
	    EXP b2 = eval_exp ( b1, ch ) ;
1842
	    if ( !EQ_exp ( c1, c2 ) ||
1843
		 !EQ_exp ( a1, a2 ) || !EQ_exp ( b1, b2 ) ) {
1844
		e = make_cond_exp ( c2, a2, b2 ) ;
1845
	    }
1846
	    break ;
1847
	}
1848
	default : {
1849
	    /* Other expressions */
1850
	    EXP f = copy_exp ( e, NULL_type, NULL_type ) ;
1851
	    if ( !EQ_exp ( f, e ) && !eq_exp_exact ( f, e ) ) e = f ;
1852
	    break ;
1853
	}
1854
    }
1855
    return ( e ) ;
1856
}
1857
 
1858
 
1859
/*
1860
    COPY AN OBJECT FUNCTION DEFINITION
1861
 
1862
    This defines the object id to be a copy of the expression e.  It
1863
    is used in the instantiation of template functions and static data
1864
    members of template classes.
1865
*/
1866
 
1867
void copy_object
1868
    PROTO_N ( ( id, e ) )
1869
    PROTO_T ( IDENTIFIER id X EXP e )
1870
{
1871
    if ( !IS_NULL_exp ( e ) ) {
1872
	int r = record_location ;
1873
	record_location = 0 ;
1874
	begin_declarator ( id, qual_none, NULL_nspace, 0 ) ;
1875
	switch ( TAG_id ( id ) ) {
1876
	    case id_function_tag :
1877
	    case id_mem_func_tag :
1878
	    case id_stat_mem_func_tag : {
1879
		/* Functions */
1880
		TYPE fn = DEREF_type ( id_function_etc_type ( id ) ) ;
1881
		in_function_defn++ ;
1882
		really_in_function_defn++ ;
1883
		IGNORE begin_templ_scope ( fn ) ;
1884
		begin_function ( id ) ;
1885
		e = copy_exp ( e, type_bottom, type_bottom ) ;
1886
		IGNORE pop_namespace () ;
1887
		unreached_code = 1 ;
1888
		IGNORE end_function ( id, e ) ;
1889
		end_templ_scope ( fn ) ;
1890
		really_in_function_defn-- ;
1891
		in_function_defn-- ;
1892
		break ;
1893
	    }
1894
	    case id_stat_member_tag : {
1895
		/* Static data members */
1896
		copy_variable ( id, e ) ;
1897
		if ( !in_template_decl ) {
1898
		    compile_variable ( id, 0 ) ;
1899
		}
1900
		if ( do_dump ) dump_declare ( id, &crt_loc, 1 ) ;
1901
		break ;
1902
	    }
1903
	    case id_class_name_tag : {
1904
		/* Nested template classes */
1905
		CLASS_TYPE ct, cs ;
1906
		TYPE t = DEREF_type ( id_class_name_defn ( id ) ) ;
1907
		TYPE s = DEREF_type ( exp_type ( e ) ) ;
1908
		while ( IS_type_templ ( t ) ) {
1909
		    t = DEREF_type ( type_templ_defn ( t ) ) ;
1910
		}
1911
		ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
1912
		while ( IS_type_templ ( s ) ) {
1913
		    s = DEREF_type ( type_templ_defn ( s ) ) ;
1914
		}
1915
		cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
1916
		copy_members ( ct, cs, cinfo_none, 1 ) ;
1917
		break ;
1918
	    }
1919
	}
1920
	end_declarator ( id, 0 ) ;
1921
	record_location = r ;
1922
    }
1923
    return ;
1924
}
1925
 
1926
 
1927
/*
1928
    CHECK FOR NON-CLASS NAMESPACES
1929
 
1930
    Qualifiers of the form 'T::' where T is a template parameter can lead
1931
    to repeated errors if T is bound to a non-class type.  This routine
1932
    is used to keep track of those instances which have been reported
1933
    to avoid duplication.
1934
*/
1935
 
1936
static int reported_nspace
1937
    PROTO_N ( ( ns, t ) )
1938
    PROTO_T ( NAMESPACE ns X TYPE t )
1939
{
1940
    static LIST ( TYPE ) types = NULL_list ( TYPE ) ;
1941
    static LIST ( NAMESPACE ) nspaces = NULL_list ( NAMESPACE ) ;
1942
    LIST ( TYPE ) p = types ;
1943
    LIST ( NAMESPACE ) q = nspaces ;
1944
    while ( !IS_NULL_list ( q ) ) {
1945
	NAMESPACE pns = DEREF_nspace ( HEAD_list ( q ) ) ;
1946
	if ( EQ_nspace ( pns, ns ) ) {
1947
	    TYPE pt = DEREF_type ( HEAD_list ( p ) ) ;
1948
	    if ( eq_type ( pt, t ) ) return ( 1 ) ;
1949
	}
1950
	p = TAIL_list ( p ) ;
1951
	q = TAIL_list ( q ) ;
1952
    }
1953
    CONS_type ( t, types, types ) ;
1954
    CONS_nspace ( ns, nspaces, nspaces ) ;
1955
    return ( 0 ) ;
1956
}
1957
 
1958
 
1959
/*
1960
    EXPAND A NAMESPACE
1961
 
1962
    This routine expands the namespace ns by replacing any class namespace
1963
    by the namespace of the expanded class.
1964
*/
1965
 
1966
NAMESPACE rescan_nspace
1967
    PROTO_N ( ( ns ) )
1968
    PROTO_T ( NAMESPACE ns )
1969
{
1970
    if ( !IS_NULL_nspace ( ns ) && IS_nspace_ctype ( ns ) ) {
1971
	TYPE s ;
1972
	IDENTIFIER tid = DEREF_id ( nspace_name ( ns ) ) ;
1973
	TYPE t = DEREF_type ( id_class_name_etc_defn ( tid ) ) ;
1974
	while ( IS_type_templ ( t ) ) {
1975
	    t = DEREF_type ( type_templ_defn ( t ) ) ;
1976
	}
1977
	s = expand_type ( t, 1 ) ;
1978
	if ( !EQ_type ( t, s ) ) {
1979
	    unsigned tag = TAG_type ( s ) ;
1980
	    while ( tag == type_templ_tag ) {
1981
		s = DEREF_type ( type_templ_defn ( s ) ) ;
1982
		tag = TAG_type ( s ) ;
1983
	    }
1984
	    if ( tag == type_compound_tag ) {
1985
		/* Expands to class type */
1986
		CLASS_TYPE cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
1987
		complete_class ( cs, 1 ) ;
1988
		ns = DEREF_nspace ( ctype_member ( cs ) ) ;
1989
	    } else if ( tag == type_token_tag && is_templ_type ( s ) ) {
1990
		/* Allow template parameters */
1991
		IDENTIFIER id = DEREF_id ( type_token_tok ( s ) ) ;
1992
		CLASS_TYPE cs = find_class ( id ) ;
1993
		if ( !IS_NULL_ctype ( cs ) ) {
1994
		    ns = DEREF_nspace ( ctype_member ( cs ) ) ;
1995
		}
1996
	    } else {
1997
		if ( tag != type_error_tag && !reported_nspace ( ns, s ) ) {
1998
		    /* Other types are not allowed */
1999
		    report ( crt_loc, ERR_temp_res_nspace ( ns, s ) ) ;
2000
		}
2001
	    }
2002
	}
2003
    }
2004
    return ( ns ) ;
2005
}
2006
 
2007
 
2008
/*
2009
    RESCAN AN IDENTIFIER NAME
2010
 
2011
    This routine looks up the identifier id again in the current context.
2012
    The name is looked up as a type-name if type is true.  The routine is
2013
    used in the resolution of dependent names in template instantiations.
2014
*/
2015
 
2016
IDENTIFIER rescan_id
2017
    PROTO_N ( ( id, qual, type ) )
2018
    PROTO_T ( IDENTIFIER id X QUALIFIER qual X int type )
2019
{
2020
    DECL_SPEC ds ;
2021
    int member = 0 ;
2022
    IDENTIFIER rid = NULL_id ;
2023
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
2024
    NAMESPACE ns = DEREF_nspace ( id_parent ( id ) ) ;
2025
 
2026
    /* Allow for pseudo-template instances */
2027
    if ( IS_id_undef ( id ) ) {
2028
	TYPE form = DEREF_type ( id_undef_form ( id ) ) ;
2029
	if ( !IS_NULL_type ( form ) && IS_type_token ( form ) ) {
2030
	    int force = 0 ;
2031
	    IDENTIFIER tid = DEREF_id ( type_token_tok ( form ) ) ;
2032
	    LIST ( TOKEN ) args = DEREF_list ( type_token_args ( form ) ) ;
2033
	    tid = rescan_id ( tid, qual, type ) ;
2034
	    if ( IS_id_undef ( tid ) ) force = 1 ;
2035
	    args = expand_args ( args, 1, 1 ) ;
2036
	    rid = apply_template ( tid, args, 0, force ) ;
2037
	    return ( rid ) ;
2038
	}
2039
    }
2040
 
2041
    /* Allow for template instances */
2042
    ds = DEREF_dspec ( id_storage ( id ) ) ;
2043
    if ( ds & dspec_instance ) {
2044
	if ( IS_id_function_etc ( id ) ) {
2045
	    /* Template functions */
2046
	    TYPE form = DEREF_type ( id_function_etc_form ( id ) ) ;
2047
	    if ( !IS_NULL_type ( form ) && IS_type_token ( form ) ) {
2048
		IDENTIFIER tid = DEREF_id ( type_token_tok ( form ) ) ;
2049
		if ( IS_id_function_etc ( tid ) ) {
2050
		    LIST ( TOKEN ) args ;
2051
		    tid = rescan_id ( tid, qual, type ) ;
2052
		    args = DEREF_list ( type_token_args ( form ) ) ;
2053
		    args = expand_args ( args, 1, 1 ) ;
2054
		    rid = apply_template ( tid, args, 0, 0 ) ;
2055
		    return ( rid ) ;
2056
		}
2057
	    }
2058
	}
2059
    }
2060
 
2061
    /* Look up identifier */
2062
    nm = expand_name ( nm, NULL_ctype ) ;
2063
    if ( !IS_NULL_nspace ( ns ) ) {
2064
	if ( qual != qual_none ) {
2065
	    member = 1 ;
2066
	} else if ( IS_nspace_ctype ( ns ) && !IS_id_undef ( id ) ) {
2067
	    member = 1 ;
2068
	}
2069
    }
2070
    if ( member ) {
2071
	/* Expand namespace */
2072
	NAMESPACE cns = rescan_nspace ( ns ) ;
2073
	if ( IS_nspace_ctype ( cns ) ) {
2074
	    rid = search_field ( cns, nm, 0, type ) ;
2075
	} else {
2076
	    MEMBER mem = search_member ( cns, nm, 0 ) ;
2077
	    if ( !IS_NULL_member ( mem ) ) {
2078
		if ( type ) {
2079
		    rid = type_member ( mem, type ) ;
2080
		} else {
2081
		    rid = DEREF_id ( member_id ( mem ) ) ;
2082
		}
2083
	    }
2084
	}
2085
 
2086
	/* Check for undeclared identifiers */
2087
	if ( IS_NULL_id ( rid ) ) {
2088
	    MAKE_id_undef ( nm, dspec_none, cns, crt_loc, rid ) ;
2089
	}
2090
 
2091
    } else {
2092
	/* Simple name look-up */
2093
	rid = find_id ( nm ) ;
2094
    }
2095
    return ( rid ) ;
2096
}
2097
 
2098
 
2099
/*
2100
    RESCAN A FUNCTION IDENTIFIER NAME
2101
 
2102
    This routine is a special case of rescan_id which is used to look up
2103
    the names of functions in template instantiations.  If the look-ups
2104
    in the contexts of both the template definition and the template
2105
    instantiation are both functions then the result is an ambiguous
2106
    identifier consisting of both sets of overloaded functions.
2107
    Overload resolution is then used to select within these two sets.
2108
*/
2109
 
2110
IDENTIFIER rescan_func_id
2111
    PROTO_N ( ( id, qual ) )
2112
    PROTO_T ( IDENTIFIER id X QUALIFIER qual )
2113
{
2114
    IDENTIFIER pid = rescan_id ( id, qual, 0 ) ;
2115
    IDENTIFIER qid = rescan_member ( id ) ;
2116
    NAMESPACE qns = DEREF_nspace ( id_parent ( qid ) ) ;
2117
    if ( !IS_NULL_nspace ( qns ) && IS_nspace_block ( qns ) ) {
2118
	qid = pid ;
2119
    }
2120
    /* QUERY: should qid be rescanned in context? */
2121
    if ( !EQ_id ( pid, qid ) && !IS_id_undef ( qid ) ) {
2122
	int eq = 0 ;
2123
	if ( IS_id_function_etc ( pid ) && IS_id_function_etc ( qid ) ) {
2124
	    /* Check for overloaded functions */
2125
	    IDENTIFIER rid = pid ;
2126
	    while ( !IS_NULL_id ( rid ) ) {
2127
		if ( EQ_id ( rid, qid ) ) {
2128
		    eq = 1 ;
2129
		    break ;
2130
		}
2131
		rid = DEREF_id ( id_function_etc_over ( rid ) ) ;
2132
	    }
2133
	}
2134
	if ( !eq ) {
2135
	    /* Create ambiguous identifier */
2136
	    DECL_SPEC ds ;
2137
	    LOCATION loc ;
2138
	    LIST ( IDENTIFIER ) pids ;
2139
	    HASHID nm = DEREF_hashid ( id_name ( pid ) ) ;
2140
	    NAMESPACE ns = DEREF_nspace ( id_parent ( pid ) ) ;
2141
	    DEREF_loc ( id_loc ( pid ), loc ) ;
2142
	    CONS_id ( pid, NULL_list ( IDENTIFIER ), pids ) ;
2143
	    CONS_id ( qid, pids, pids ) ;
2144
	    ds = find_ambig_dspec ( pids ) ;
2145
	    MAKE_id_ambig ( nm, ds, ns, loc, pids, 1, pid ) ;
2146
	}
2147
    }
2148
    return ( pid ) ;
2149
}
2150
 
2151
 
2152
/*
2153
    RESCAN A MEMBER NAME
2154
 
2155
    This routine rescans the identifier id.  If id is a member of a template
2156
    class then the corresponding member of the expanded class is returned.
2157
    id is also marked as having been used and is instantiated if necessary.
2158
*/
2159
 
2160
IDENTIFIER rescan_member
2161
    PROTO_N ( ( id ) )
2162
    PROTO_T ( IDENTIFIER id )
2163
{
2164
    IDENTIFIER lid ;
2165
    NAMESPACE ns = DEREF_nspace ( id_parent ( id ) ) ;
2166
    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
2167
 
2168
    /* Check for template functions */
2169
    if ( ds & dspec_instance ) {
2170
	if ( IS_id_function_etc ( id ) ) {
2171
	    /* Template functions */
2172
	    TYPE form = DEREF_type ( id_function_etc_form ( id ) ) ;
2173
	    if ( !IS_NULL_type ( form ) && IS_type_token ( form ) ) {
2174
		IDENTIFIER tid = DEREF_id ( type_token_tok ( form ) ) ;
2175
		if ( IS_id_function_etc ( tid ) ) {
2176
		    LIST ( TOKEN ) args ;
2177
		    tid = rescan_member ( tid ) ;
2178
		    args = DEREF_list ( type_token_args ( form ) ) ;
2179
		    args = expand_args ( args, 1, 1 ) ;
2180
		    id = apply_template ( tid, args, 0, 0 ) ;
2181
		    ns = NULL_nspace ;
2182
		}
2183
	    }
2184
	}
2185
    }
2186
 
2187
    /* Check for template class members */
2188
    if ( !IS_NULL_nspace ( ns ) && IS_nspace_ctype ( ns ) ) {
2189
	NAMESPACE pns = rescan_nspace ( ns ) ;
2190
	if ( !EQ_nspace ( ns, pns ) ) {
2191
	    IDENTIFIER pid = DEREF_id ( nspace_name ( pns ) ) ;
2192
	    lid = find_copied ( pid, id, 1 ) ;
2193
	    if ( !EQ_id ( lid, id ) ) {
2194
		define_template ( lid, 0 ) ;
2195
		id = lid ;
2196
		ds = DEREF_dspec ( id_storage ( id ) ) ;
2197
	    }
2198
	}
2199
    }
2200
 
2201
    /* Handle implicitly declared parameters */
2202
    if ( ( ds & dspec_implicit ) && ( ds & dspec_auto ) ) {
2203
	HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
2204
	id = find_id ( nm ) ;
2205
	ds = DEREF_dspec ( id_storage ( id ) ) ;
2206
    }
2207
 
2208
    /* Mark as used */
2209
    ds |= dspec_used ;
2210
    COPY_dspec ( id_storage ( id ), ds ) ;
2211
    lid = DEREF_id ( id_alias ( id ) ) ;
2212
    if ( !EQ_id ( lid, id ) ) {
2213
	ds = DEREF_dspec ( id_storage ( lid ) ) ;
2214
	ds |= dspec_used ;
2215
	COPY_dspec ( id_storage ( lid ), ds ) ;
2216
    }
2217
    return ( id ) ;
2218
}