Subversion Repositories tendra.SVN

Rev

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

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