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, 1998
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 "nat_ops.h"
67
#include "off_ops.h"
68
#include "type_ops.h"
69
#include "error.h"
70
#include "catalog.h"
71
#include "option.h"
72
#include "basetype.h"
73
#include "cast.h"
74
#include "check.h"
75
#include "chktype.h"
76
#include "class.h"
77
#include "compile.h"
78
#include "constant.h"
79
#include "construct.h"
80
#include "convert.h"
81
#include "derive.h"
82
#include "expression.h"
83
#include "identifier.h"
84
#include "initialise.h"
85
#include "literal.h"
86
#include "member.h"
87
#include "operator.h"
88
#include "overload.h"
89
#include "predict.h"
90
#include "quality.h"
91
#include "statement.h"
92
#include "syntax.h"
93
#include "template.h"
94
 
95
 
96
/*
97
    CONSTRUCT AN ERROR EXPRESSION
98
 
99
    This routine creates an error expression.  The result is an lvalue
100
    if lv is true.
101
*/
102
 
7 7u83 103
EXP
104
make_error_exp(int lv)
2 7u83 105
{
7 7u83 106
	EXP a;
107
	TYPE t = type_error;
108
	if (lv) {
109
		t = lvalue_type(t);
110
	}
111
	MAKE_exp_value(t, a);
112
	return (a);
2 7u83 113
}
114
 
115
 
116
/*
117
    CONSTRUCT A NULL POINTER CONSTANT
118
 
119
    This routine checks converts the integral expression a into a null
120
    pointer (or pointer to member) constant of type t.  Basically this
121
    consists of testing whether a evaluates to zero, however writing
122
    anything other than a plain literal '0' for the null pointer (for
123
    example '1 - 1') is considered bad practice, so there is a test
124
    for this.  The null expression is returns if a is non-zero.
125
*/
126
 
7 7u83 127
EXP
128
make_null_ptr(EXP a, TYPE t)
2 7u83 129
{
7 7u83 130
	EXP e = NULL_exp;
131
	if (IS_NULL_exp(a)) {
132
		/* Allow null expressions */
133
		MAKE_exp_null(t, e);
2 7u83 134
	} else {
7 7u83 135
		if (is_zero_exp(a)) {
136
			if (!is_literal(a)) {
137
				/* Report complex literals */
138
				report(crt_loc, ERR_conv_ptr_null_complex());
139
			}
140
			MAKE_exp_null(t, e);
141
		} else {
142
			if (in_template_decl && IS_exp_int_lit(a)) {
143
				if (depends_on_exp(a, any_templ_param, 0)) {
144
					/* Check for template parameters */
145
					report(crt_loc,
146
					       ERR_conv_ptr_null_complex());
147
					MAKE_exp_op(t, lex_pointer, a,
148
						    NULL_exp, e);
149
				}
150
			}
2 7u83 151
		}
152
	}
7 7u83 153
	return (e);
2 7u83 154
}
155
 
156
 
157
/*
158
    CONSTRUCT A PARENTHESISED EXPRESSION
159
 
160
    This routine constructs the expression '( a )'.  Note that parentheses
161
    are only needed in order to perform analysis for odd precedence in
162
    expressions.  It is otherwise just an identity operation.
163
*/
164
 
7 7u83 165
EXP
166
make_paren_exp(EXP a)
2 7u83 167
{
7 7u83 168
	EXP e;
169
	TYPE ta = DEREF_type(exp_type(a));
170
	if (IS_exp_int_lit(a)) {
171
		/* Deal with integer constant expressions */
172
		unsigned etag = DEREF_unsigned(exp_int_lit_etag(a));
173
		switch (etag) {
174
		case exp_int_lit_tag:
175
		case exp_null_tag:
176
		case exp_identifier_tag: {
177
			/* Don't bother with literals and enumerators */
178
			e = a;
179
			break;
180
		}
181
		default: {
182
			/* Mark other values as parenthesised */
183
			NAT n = DEREF_nat(exp_int_lit_nat(a));
184
			MAKE_exp_int_lit(ta, n, exp_paren_tag, e);
185
			break;
186
		}
187
		}
188
	} else {
189
		MAKE_exp_paren(ta, a, e);
2 7u83 190
	}
7 7u83 191
	return (e);
2 7u83 192
}
193
 
194
 
195
/*
196
    CONSTRUCT A TYPE OFFSET
197
 
198
    This routine constructs an offset of a times the offset of the type t.
199
    This is negated if neg is true.
200
*/
201
 
7 7u83 202
OFFSET
203
make_off_mult(TYPE t, EXP a, int neg)
2 7u83 204
{
7 7u83 205
	OFFSET off;
206
	if (IS_type_top_etc(t)) {
207
		/* Map 'void *' to 'char *' */
208
		t = type_char;
2 7u83 209
	}
7 7u83 210
	if (IS_exp_int_lit(a)) {
211
		/* Constant offsets */
212
		int neg1 = neg;
213
		NAT n = DEREF_nat(exp_int_lit_nat(a));
214
		if (IS_nat_neg(n)) {
215
			n = DEREF_nat(nat_neg_arg(n));
216
			neg1 = !neg1;
2 7u83 217
		}
7 7u83 218
		if (IS_nat_small(n)) {
219
			unsigned v = DEREF_unsigned(nat_small_value(n));
220
			if (v < 100) {
221
				if (v == 0) {
222
					MAKE_off_zero(t, off);
223
				} else if (v == 1) {
224
					MAKE_off_type(t, off);
225
				} else {
226
					MAKE_type_array(cv_none, t, n, t);
227
					MAKE_off_type(t, off);
228
				}
229
				if (neg1) {
230
					MAKE_off_negate(off, off);
231
				}
232
				return (off);
233
			}
234
		}
2 7u83 235
	}
7 7u83 236
	MAKE_off_type(t, off);
237
	MAKE_off_mult(off, a, off);
238
	if (neg) {
239
		MAKE_off_negate(off, off);
240
	}
241
	return (off);
2 7u83 242
}
243
 
244
 
245
/*
246
    CREATE AN ADD-TO-POINTER EXPRESSION
247
 
248
    This routine creates a pointer of type t by adding the offset off to
249
    the pointer expression a.  For pointers of type 'void *' conversions
250
    are performed to and from 'char *'.
251
*/
252
 
7 7u83 253
EXP
254
make_add_ptr(TYPE t, EXP a, OFFSET off)
2 7u83 255
{
7 7u83 256
	EXP e;
257
	if (IS_type_ptr(t)) {
258
		TYPE s = DEREF_type(type_ptr_sub(t));
259
		if (IS_type_top_etc(s)) {
260
			TYPE p = type_char_star;
261
			MAKE_exp_cast(p,(CONV_PTR_VOID | CONV_REVERSE), a, e);
262
			e = make_add_ptr(p, e, off);
263
			MAKE_exp_cast(t, CONV_PTR_VOID, e, e);
264
			return (e);
265
		}
2 7u83 266
	}
7 7u83 267
	MAKE_exp_add_ptr(t, a, off, 0, e);
268
	return (e);
2 7u83 269
}
270
 
271
 
272
/*
273
    CONSTRUCT AN INDEX EXPRESSION
274
 
275
    This routine constructs the expression 'a [b]'.  The result is an
276
    lvalue.  Note that if a is immediately derived from an array and b is
277
    an integer constant then bounds checks are applied to the operation.
278
    However once an array has been converted into a pointer any associated
279
    bounds information is lost.
280
*/
281
 
7 7u83 282
EXP
283
make_index_exp(EXP a, EXP b)
2 7u83 284
{
7 7u83 285
	TYPE t;
286
	EXP e, p;
287
	int z = 0;
288
	OFFSET off;
289
	TYPE ta, tb;
290
	unsigned ca, cb;
291
	ERROR err = NULL_err;
292
	TYPE sa = DEREF_type(exp_type(a));
293
	TYPE sb = DEREF_type(exp_type(b));
2 7u83 294
 
7 7u83 295
	/* Do reference conversions */
296
	a = convert_reference(a, REF_NORMAL);
297
	b = convert_reference(b, REF_NORMAL);
2 7u83 298
 
7 7u83 299
	/* Find the operand types */
300
	ta = DEREF_type(exp_type(a));
301
	ca = type_category(&ta);
302
	tb = DEREF_type(exp_type(b));
303
	cb = type_category(&tb);
2 7u83 304
 
7 7u83 305
	/* Check for overloading */
2 7u83 306
#if LANGUAGE_CPP
7 7u83 307
	if (IS_TYPE_OVERLOAD(ca) || IS_TYPE_OVERLOAD(cb)) {
308
		if (overload_depth == 0) {
309
			e = binary_overload(lex_array_Hop, a, b);
310
			return (e);
311
		}
2 7u83 312
	}
313
#endif
314
 
7 7u83 315
	/* Do lvalue conversions */
316
	if (IS_TYPE_ADDRESS(ca)) {
317
		a = convert_lvalue(a);
318
		ta = DEREF_type(exp_type(a));
319
		ca = type_category(&ta);
320
	}
321
	if (IS_TYPE_ADDRESS(cb)) {
322
		b = convert_lvalue(b);
323
		tb = DEREF_type(exp_type(b));
324
		cb = type_category(&tb);
325
	}
2 7u83 326
 
7 7u83 327
	/* Swap operands if the second is a pointer */
328
	if (IS_TYPE_PTR(cb)) {
329
		/* Alright because order of evaluation is undefined */
330
		EXP c = a;
331
		TYPE tc = ta;
332
		unsigned cc = ca;
333
		a = b;
334
		b = c;
335
		ta = tb;
336
		tb = tc;
337
		ca = cb;
338
		cb = cc;
339
		sa = sb;
340
	}
2 7u83 341
 
7 7u83 342
	/* The first operand should now be a pointer */
343
	if (!IS_TYPE_PTR(ca)) {
344
		if (!IS_TYPE_ERROR(ca) && !IS_TYPE_ERROR(cb)) {
345
			report(crt_loc, ERR_expr_sub_ptr_op(ta, tb));
346
		}
347
		return (make_error_exp(0));
2 7u83 348
	}
349
 
7 7u83 350
	/* The second operand should be integral */
351
	if (!IS_TYPE_INT(cb)) {
352
		if (!IS_TYPE_ERROR(cb)) {
353
			report(crt_loc, ERR_expr_sub_int_op(tb));
354
		}
355
		/* Continue with zero index */
356
		z = 1;
2 7u83 357
	}
358
 
7 7u83 359
	/* Check index value */
360
	if (IS_exp_int_lit(b)) {
361
		if (IS_type_array(sa)) {
362
			check_bounds(lex_array_Hop, sa, b);
363
		}
364
		z = is_zero_exp(b);
365
	} else {
366
		if (eq_type_unqual(tb, type_char)) {
367
			report(crt_loc, ERR_expr_sub_char_op(tb));
368
		}
2 7u83 369
	}
7 7u83 370
 
371
	/* The pointer must be to a complete object type */
372
	t = check_pointer(ta, &err);
373
	if (!IS_NULL_err(err)) {
374
		err = concat_error(err, ERR_expr_sub_incompl());
375
		report(crt_loc, err);
2 7u83 376
	}
377
 
7 7u83 378
	/* Construct pointer to the result */
379
	if (z) {
380
		/* Zero offset */
381
		p = a;
382
	} else {
383
		/* Non-zero offset */
384
		if (IS_TYPE_BITF(cb)) {
385
			b = convert_bitfield(b);
386
		}
387
		off = make_off_mult(t, b, 0);
388
		p = make_add_ptr(ta, a, off);
389
	}
2 7u83 390
 
7 7u83 391
	/* The result is an lvalue */
392
	t = lvalue_type(t);
2 7u83 393
 
7 7u83 394
	/* Construct the result */
395
	MAKE_exp_indir(t, p, e);
396
	COPY_int(exp_indir_index(e), 1);
397
	return (e);
2 7u83 398
}
399
 
400
 
401
/*
402
    CONSTRUCT A INDIRECTION EXPRESSION
403
 
404
    This routine constructs the indirection expression '*a'.  The result
405
    is an lvalue.
406
*/
407
 
7 7u83 408
EXP
409
make_indir_exp(EXP a)
2 7u83 410
{
7 7u83 411
	EXP e;
412
	TYPE ta;
413
	unsigned ca;
2 7u83 414
 
7 7u83 415
	/* Do reference conversion */
416
	a = convert_reference(a, REF_NORMAL);
2 7u83 417
 
7 7u83 418
	/* Find operand type */
419
	ta = DEREF_type(exp_type(a));
420
	ca = type_category(&ta);
2 7u83 421
 
7 7u83 422
	/* Check for overloading */
2 7u83 423
#if LANGUAGE_CPP
7 7u83 424
	if (IS_TYPE_OVERLOAD(ca)) {
425
		if (overload_depth == 0) {
426
			e = unary_overload(lex_star, a);
427
			return (e);
428
		}
2 7u83 429
	}
430
#endif
431
 
7 7u83 432
	/* Do lvalue conversion */
433
	if (IS_TYPE_ADDRESS(ca)) {
434
		a = convert_lvalue(a);
435
		ta = DEREF_type(exp_type(a));
436
		ca = type_category(&ta);
2 7u83 437
	}
438
 
7 7u83 439
	/* Operand can be pointer ... */
440
	if (IS_TYPE_PTR(ca)) {
441
		TYPE t = check_pointer(ta, KILL_err);
442
		if (IS_type_top_etc(t)) {
443
			/* The pointer cannot be 'void *' */
444
			report(crt_loc, ERR_expr_unary_op_indir_void(ta));
445
		}
446
		if (IS_exp_null(a)) {
447
			/* Check for obvious null pointers */
448
			report(crt_loc, ERR_expr_unary_op_indir_null(lex_star));
449
		}
2 7u83 450
 
7 7u83 451
		/* The result is an lvalue */
452
		t = lvalue_type(t);
2 7u83 453
 
7 7u83 454
		/* Construct the result */
455
		MAKE_exp_indir(t, a, e);
456
		return (e);
457
	}
458
 
459
	/* ... and nothing else */
460
	if (!IS_TYPE_ERROR(ca)) {
461
		report(crt_loc, ERR_expr_unary_op_indir_op(ta));
462
	}
463
	return (make_error_exp(1));
2 7u83 464
}
465
 
466
 
467
/*
468
    CONSTRUCT A REFERENCE TO AN OBJECT
469
 
470
    This routine constructs a pointer to the expression a which designates
471
    an object.  Any errors arising are added to err.
472
*/
473
 
7 7u83 474
EXP
475
make_ref_object(EXP a, ERROR *err)
2 7u83 476
{
7 7u83 477
	EXP e;
478
	TYPE p;
479
	TYPE ta = DEREF_type(exp_type(a));
480
	unsigned ca = type_category(&ta);
481
	if (IS_TYPE_ERROR(ca)) {
482
		/* Error progagation */
483
		e = make_error_exp(0);
484
		return (e);
2 7u83 485
	}
7 7u83 486
	if (!IS_TYPE_LVALUE(ca)) {
487
		/* Operand should be an lvalue */
488
		add_error(err, ERR_expr_unary_op_ref_lvalue());
2 7u83 489
	}
7 7u83 490
	if (IS_TYPE_BITF(ca)) {
491
		/* Can't apply to a bitfield */
492
		add_error(err, ERR_expr_unary_op_ref_bitf());
493
		ta = find_bitfield_type(ta);
494
	}
495
	if (option(OPT_addr_register) && used_register) {
496
		/* Can't apply to a register variable in C */
497
		EXP b = NULL_exp;
498
		DECL_SPEC ds = find_exp_linkage(a, &b, 1);
499
		if ((ds & dspec_register) && !(ds & dspec_temp)) {
500
			if (IS_exp_identifier(b)) {
501
				IDENTIFIER id = DEREF_id(exp_identifier_id(b));
502
				add_error(err,
503
					  ERR_expr_unary_op_ref_register(id));
504
			}
505
		}
506
	}
507
	if (IS_type_top_etc(ta)) {
508
		/* Can't apply to void */
509
		add_error(err, ERR_expr_unary_op_ref_void(ta));
510
	} else {
511
		/* Check for incomplete types */
512
		ERROR err2 = check_incomplete(ta);
513
		if (!IS_NULL_err(err2)) {
514
			add_error(err, err2);
515
			add_error(err, ERR_expr_unary_op_ref_incompl());
516
		}
517
	}
2 7u83 518
 
7 7u83 519
	/* Construct the result */
520
	ta = rvalue_type(ta);
521
	MAKE_type_ptr(cv_none, ta, p);
522
	MAKE_exp_address(p, a, e);
523
	return (e);
2 7u83 524
}
525
 
526
 
527
/*
528
    CONSTRUCT A REFERENCE TO A MEMBER
529
 
530
    This routine constructs a pointer to the expression a which designates
531
    a class member.  Note that a can represent an overloaded member function
532
    in which case the actual result type can only be determined after
533
    overload resolution.  Also the type of an inherited member is that
534
    of its base class rather than its derived class.
535
*/
536
 
537
#if LANGUAGE_CPP
538
 
7 7u83 539
static EXP
540
make_ref_member(EXP a, int paren, int res)
2 7u83 541
{
7 7u83 542
	EXP e;
543
	TYPE p;
544
	DECL_SPEC ds;
2 7u83 545
 
7 7u83 546
	/* Find the base class */
547
	IDENTIFIER id = DEREF_id(exp_member_id(a));
548
	IDENTIFIER uid = DEREF_id(id_alias(id));
549
	CLASS_TYPE tc = parent_class(uid);
2 7u83 550
 
7 7u83 551
	/* Find the result type */
552
	TYPE ta = DEREF_type(exp_type(a));
553
	ta = rvalue_type(ta);
554
	if (IS_type_bitfield(ta)) {
555
		/* Can't apply to a bitfield */
556
		report(crt_loc, ERR_expr_unary_op_ref_bitf());
557
		ta = find_bitfield_type(ta);
558
	}
559
	MAKE_type_ptr_mem(cv_none, tc, ta, p);
2 7u83 560
 
7 7u83 561
	/* Construct the result */
562
	if (!EQ_id(id, uid)) {
563
		QUALIFIER qual = DEREF_qual(exp_member_qual(a));
564
		MAKE_exp_member(ta, uid, qual, a);
2 7u83 565
	}
7 7u83 566
	MAKE_exp_address_mem(p, a, paren, e);
567
	if (res) {
568
		/* Mark identifier as used */
569
		if (res == 2) {
570
			use_id(id, suppress_usage);
571
		} else {
572
			reuse_id(id, suppress_usage);
573
		}
574
		ds = DEREF_dspec(id_storage(id));
575
		if (ds & dspec_trivial) {
576
			/* Can take the address of a trivial function */
577
			CONS_id(id, pending_funcs, pending_funcs);
578
		}
2 7u83 579
	}
7 7u83 580
	return (e);
2 7u83 581
}
582
 
583
#endif
584
 
585
 
586
/*
587
    CONSTRUCT A REFERENCE EXPRESSION
588
 
589
    This routine constructs the expression '&a' for constructing a pointer
590
    to a or a pointer member to a.  The res argument is true to indicate
591
    that any overloaded functions in a have been resolved.
592
*/
593
 
7 7u83 594
EXP
595
make_ref_exp(EXP a, int res)
2 7u83 596
{
7 7u83 597
	EXP e;
598
	ERROR err = NULL_err;
2 7u83 599
 
600
#if LANGUAGE_CPP
7 7u83 601
	TYPE ta;
602
	unsigned ca;
603
	int paren = IS_exp_paren(a);
2 7u83 604
 
7 7u83 605
	/* Perform reference conversions */
606
	a = convert_reference(a, REF_ADDRESS);
2 7u83 607
 
7 7u83 608
	/* Check for members */
609
	if (IS_exp_member(a)) {
610
		int is_mem = 1;
611
		IDENTIFIER id = DEREF_id(exp_member_id(a));
612
		QUALIFIER idtype = DEREF_qual(exp_member_qual(a));
613
		idtype &= ~qual_mark;
614
		if (!(idtype & qual_explicit)) {
615
			EXP b = make_this_field(id);
616
			if (!IS_NULL_exp(b)) {
617
				a = convert_reference(b, REF_ADDRESS);
618
				is_mem = 0;
619
			}
2 7u83 620
		}
7 7u83 621
		if (is_mem) {
622
			if (IS_id_member(id))res = 2;
623
			if (res) {
624
				if (idtype == qual_none) {
625
					/* Identifiers must be qualified */
626
					report(crt_loc,
627
					       ERR_expr_unary_op_ref_unqual());
628
				} else {
629
					if (idtype != qual_nested) {
630
						/* Shouldn't have fully
631
						 * qualified identifier */
632
						report(crt_loc, ERR_expr_unary_op_ref_full());
633
					}
634
					if (paren) {
635
						/* Identifier can't be
636
						 * parenthesised */
637
						report(crt_loc, ERR_expr_unary_op_ref_paren());
638
						paren = 0;
639
					}
640
				}
641
			}
642
			e = make_ref_member(a, paren, res);
643
			return (e);
644
		}
2 7u83 645
	}
646
 
7 7u83 647
	/* Get operand type */
648
	ta = DEREF_type(exp_type(a));
649
	ca = type_category(&ta);
650
	if (IS_TYPE_OVERLOAD(ca)) {
651
		/* Check for overloading */
652
		if (overload_depth == 0) {
653
			e = unary_overload(lex_and_H1, a);
654
			return (e);
655
		}
656
		if (IS_type_compound(ta)) {
657
			/* Mark class types */
658
			CLASS_TYPE ct = DEREF_ctype(type_compound_defn(ta));
659
			CLASS_USAGE cu = DEREF_cusage(ctype_usage(ct));
660
			cu |= cusage_address;
661
			COPY_cusage(ctype_usage(ct), cu);
662
		}
2 7u83 663
	}
664
 
665
#else
666
 
7 7u83 667
	/* Perform reference conversions */
668
	a = convert_reference(a, REF_ADDRESS);
669
	UNUSED(res);
2 7u83 670
 
671
#endif
672
 
7 7u83 673
	/* Construct the result */
674
	e = make_ref_object(a, &err);
675
	if (!IS_NULL_err(err)) {
676
		report(crt_loc, err);
677
	}
678
	return (e);
2 7u83 679
}
680
 
681
 
682
/*
683
    CONSTRUCT A UNARY ARITHMETIC EXPRESSION
684
 
685
    This routine constructs the unary arithmetic expression 'op a'.  For
686
    '+a' the expression constructed is '( a )' rather than 'a'.  This is
687
    to prevent expressions like 'a << +( b + c )' confusing the dubious
688
    parenthesis checks.
689
*/
690
 
7 7u83 691
EXP
692
make_uminus_exp(int op, EXP a)
2 7u83 693
{
7 7u83 694
	TYPE ta;
695
	unsigned ca;
2 7u83 696
 
7 7u83 697
	/* Find operation information */
698
	unsigned cb = CTYPE_ARITH;
699
	unsigned tag = exp_negate_tag;
700
	switch (op) {
701
	case lex_plus: {
702
		tag = exp_paren_tag;
2 7u83 703
#if LANGUAGE_CPP
7 7u83 704
		cb = CTYPE_SCALAR;
2 7u83 705
#endif
7 7u83 706
		break;
2 7u83 707
	}
7 7u83 708
	case lex_compl_H1: {
709
		tag = exp_compl_tag;
710
		cb = CTYPE_INT;
711
		break;
2 7u83 712
	}
7 7u83 713
	case lex_abs: {
714
		tag = exp_abs_tag;
715
		break;
2 7u83 716
	}
7 7u83 717
	}
2 7u83 718
 
7 7u83 719
	/* Do reference conversion */
720
	a = convert_reference(a, REF_NORMAL);
2 7u83 721
 
7 7u83 722
	/* Find the operand type */
723
	ta = DEREF_type(exp_type(a));
724
	ca = type_category(&ta);
2 7u83 725
 
7 7u83 726
	/* Check for overloading */
2 7u83 727
#if LANGUAGE_CPP
7 7u83 728
	if (IS_TYPE_OVERLOAD(ca)) {
729
		if (overload_depth == 0) {
730
			EXP e = unary_overload(op, a);
731
			return (e);
732
		}
2 7u83 733
	}
734
#endif
735
 
7 7u83 736
	/* Do lvalue conversion */
737
	if (IS_TYPE_ADDRESS(ca)) {
738
		a = convert_lvalue(a);
739
		ta = DEREF_type(exp_type(a));
740
		ca = type_category(&ta);
741
	}
2 7u83 742
 
7 7u83 743
	/* Check operand type ... */
744
	if (ca & cb) {
745
		EXP e;
746
		TYPE pta = promote_type(ta);
747
		a = convert_promote(pta, a);
748
		if (tag == exp_paren_tag) {
749
			e = make_paren_exp(a);
750
		} else {
751
			if (IS_exp_int_lit(a)) {
752
				e = make_unary_nat(tag, a);
753
			} else {
754
				MAKE_exp_negate_etc(tag, pta, a, e);
755
			}
756
		}
757
		return (e);
2 7u83 758
	}
759
 
7 7u83 760
	/* ... and report error otherwise */
761
	if (!IS_TYPE_ERROR(ca)) {
762
		ERROR err;
763
		if (cb == CTYPE_SCALAR) {
764
			err = ERR_expr_unary_op_uplus_op(op, ta);
765
		} else if (cb == CTYPE_ARITH) {
766
			err = ERR_expr_unary_op_uminus_op(op, ta);
767
		} else {
768
			err = ERR_expr_unary_op_compl_op(op, ta);
769
		}
770
		report(crt_loc, err);
2 7u83 771
	}
7 7u83 772
	return (make_error_exp(0));
2 7u83 773
}
774
 
775
 
776
/*
777
    CONSTRUCT A LOGICAL NEGATION EXPRESSION
778
 
779
    This routine constructs the expression '!a'.
780
*/
781
 
7 7u83 782
EXP
783
make_not_exp(EXP a)
2 7u83 784
{
7 7u83 785
	TYPE ta;
786
	EXP e, b;
787
	unsigned ca;
788
	ERROR err = NULL_err;
789
	unsigned tag = TAG_exp(a);
2 7u83 790
 
7 7u83 791
	/* Do reference conversion */
792
	a = convert_reference(a, REF_NORMAL);
2 7u83 793
 
7 7u83 794
	/* Find the operand type */
795
	ta = DEREF_type(exp_type(a));
796
	ca = type_category(&ta);
2 7u83 797
 
7 7u83 798
	/* Check for overloading */
2 7u83 799
#if LANGUAGE_CPP
7 7u83 800
	if (IS_TYPE_OVERLOAD(ca)) {
801
		if (overload_depth == 0) {
802
			e = unary_overload(lex_not_H1, a);
803
			return (e);
804
		}
2 7u83 805
	}
806
#endif
807
 
7 7u83 808
	/* Do lvalue conversion */
809
	if (IS_TYPE_ADDRESS(ca)) {
810
		a = convert_lvalue(a);
811
	}
2 7u83 812
 
7 7u83 813
	/* Convert the operand to a boolean */
814
	b = convert_boolean(a, tag, &err);
815
	if (!IS_NULL_err(err)) {
816
		err = concat_error(err, ERR_expr_unary_op_not_op());
817
		report(crt_loc, err);
818
	}
2 7u83 819
 
7 7u83 820
	/* Construct the result */
821
	if (IS_exp_int_lit(b)) {
822
		e = make_unary_nat(exp_not_tag, b);
823
	} else {
824
		MAKE_exp_not(type_bool, b, e);
825
	}
826
	return (e);
2 7u83 827
}
828
 
829
 
830
/*
831
    DIVISION MODE
832
 
833
    This flag gives the mode to be used in integer division and remainder
834
    operations.  The values 0, 1 and 2 correspond to the TDF operations
835
    div0, div1 and div2 and rem0, rem1 and rem2 respectively.  The value 3
836
    indicates that the decision should be postponed to the installers.
837
*/
838
 
7 7u83 839
int division_mode = 3;
2 7u83 840
 
841
 
842
/*
843
    CHECK FOR DUBIOUS DIVISION EXPRESSIONS
844
 
845
    This routine checks the division operation 'a / b' or 'a % b' for
846
    dubious constant operands.  All the necessary operand and arithmetic
847
    type conversions have already been performed on a and b.  The routine
848
    returns 1 if both operands are integer constants and b is not zero.
849
*/
850
 
7 7u83 851
int
852
check_div_exp(int op, EXP a, EXP b)
2 7u83 853
{
7 7u83 854
	int eval = 1;
855
	int div_mode = division_mode;
856
	if (IS_exp_int_lit(b)) {
857
		/* Check the second operand */
858
		NAT n = DEREF_nat(exp_int_lit_nat(b));
859
		if (is_zero_nat(n)) {
860
			/* Report division by zero */
861
			report(crt_loc, ERR_expr_mul_div_zero(op));
862
			return (0);
863
		}
864
		if (div_mode != 1 && div_mode != 2) {
865
			if (is_negative_nat(n) && !divides_nat(a, b)) {
866
				/* Division by negative is undefined */
867
				report(crt_loc, ERR_expr_mul_div_neg(op, n));
868
			}
869
		}
870
	} else {
871
		eval = 0;
2 7u83 872
	}
7 7u83 873
	if (IS_exp_int_lit(a)) {
874
		/* Check the first operand */
875
		if (div_mode != 1 && div_mode != 2) {
876
			NAT n = DEREF_nat(exp_int_lit_nat(a));
877
			if (is_negative_nat(n) && !divides_nat(a, b)) {
878
				/* Division of negative is undefined */
879
				report(crt_loc, ERR_expr_mul_div_neg(op, n));
880
			}
881
		}
882
	} else {
883
		eval = 0;
2 7u83 884
	}
7 7u83 885
	return (eval);
2 7u83 886
}
887
 
888
 
889
/*
890
    CONSTRUCT A MULTIPLICATION OR DIVISION EXPRESSION
891
 
892
    This routine constructs the expressions 'a * b' and 'a / b'.
893
*/
894
 
7 7u83 895
EXP
896
make_mult_exp(int op, EXP a, EXP b)
2 7u83 897
{
7 7u83 898
	TYPE ta, tb;
899
	unsigned ca, cb;
2 7u83 900
 
7 7u83 901
	/* Do reference conversions */
902
	a = convert_reference(a, REF_NORMAL);
903
	b = convert_reference(b, REF_NORMAL);
2 7u83 904
 
7 7u83 905
	/* Find operand types */
906
	ta = DEREF_type(exp_type(a));
907
	tb = DEREF_type(exp_type(b));
908
	ca = type_category(&ta);
909
	cb = type_category(&tb);
2 7u83 910
 
7 7u83 911
	/* Check for overloading */
2 7u83 912
#if LANGUAGE_CPP
7 7u83 913
	if (IS_TYPE_OVERLOAD(ca) || IS_TYPE_OVERLOAD(cb)) {
914
		if (overload_depth == 0) {
915
			EXP e = binary_overload(op, a, b);
916
			return (e);
917
		}
2 7u83 918
	}
919
#endif
920
 
7 7u83 921
	/* Do lvalue conversions */
922
	if (IS_TYPE_ADDRESS(ca)) {
923
		a = convert_lvalue(a);
924
		ta = DEREF_type(exp_type(a));
925
		ca = type_category(&ta);
926
	}
927
	if (IS_TYPE_ADDRESS(cb)) {
928
		b = convert_lvalue(b);
929
		tb = DEREF_type(exp_type(b));
930
		cb = type_category(&tb);
931
	}
2 7u83 932
 
7 7u83 933
	/* Operands can be arithmetic ... */
934
	if (IS_TYPE_ARITH(ca) && IS_TYPE_ARITH(cb)) {
935
		EXP e;
936
		unsigned tag;
937
		TYPE t = arith_type(ta, tb, a, b);
938
		a = convert_arith(t, a, op, 1);
939
		b = convert_arith(t, b, op, 2);
2 7u83 940
 
7 7u83 941
		/* Check for constant operands */
942
		if (op == lex_div) {
943
			tag = exp_div_tag;
944
			if (check_div_exp(op, a, b)) {
945
				e = make_binary_nat(tag, a, b);
946
				return (e);
947
			}
948
		} else {
949
			if (op == lex_star) {
950
				tag = exp_mult_tag;
951
			} else if (op == lex_max) {
952
				tag = exp_max_tag;
953
			} else {
954
				tag = exp_min_tag;
955
			}
956
			if (IS_exp_int_lit(a) && IS_exp_int_lit(b)) {
957
				e = make_binary_nat(tag, a, b);
958
				return (e);
959
			}
960
		}
961
 
962
		/* Construct the result */
963
		MAKE_exp_plus_etc(tag, t, a, b, e);
964
		return (e);
2 7u83 965
	}
966
 
7 7u83 967
	/* ... and nothing else */
968
	if (!IS_TYPE_ERROR(ca) && !IS_TYPE_ERROR(cb)) {
969
		report(crt_loc, ERR_expr_mul_mul_op(op, ta, tb));
970
	}
971
	return (make_error_exp(0));
2 7u83 972
}
973
 
974
 
975
/*
976
    CONSTRUCT A REMAINDER EXPRESSION
977
 
978
    This routine constructs the expression 'a % b'.
979
*/
980
 
7 7u83 981
EXP
982
make_rem_exp(EXP a, EXP b)
2 7u83 983
{
7 7u83 984
	TYPE ta, tb;
985
	unsigned ca, cb;
2 7u83 986
 
7 7u83 987
	/* Do reference conversions */
988
	a = convert_reference(a, REF_NORMAL);
989
	b = convert_reference(b, REF_NORMAL);
2 7u83 990
 
7 7u83 991
	/* Find operand types */
992
	ta = DEREF_type(exp_type(a));
993
	tb = DEREF_type(exp_type(b));
994
	ca = type_category(&ta);
995
	cb = type_category(&tb);
2 7u83 996
 
7 7u83 997
	/* Check for overloading */
2 7u83 998
#if LANGUAGE_CPP
7 7u83 999
	if (IS_TYPE_OVERLOAD(ca) || IS_TYPE_OVERLOAD(cb)) {
1000
		if (overload_depth == 0) {
1001
			EXP e = binary_overload(lex_rem, a, b);
1002
			return (e);
1003
		}
2 7u83 1004
	}
1005
#endif
1006
 
7 7u83 1007
	/* Do lvalue conversions */
1008
	if (IS_TYPE_ADDRESS(ca)) {
1009
		a = convert_lvalue(a);
1010
		ta = DEREF_type(exp_type(a));
1011
		ca = type_category(&ta);
1012
	}
1013
	if (IS_TYPE_ADDRESS(cb)) {
1014
		b = convert_lvalue(b);
1015
		tb = DEREF_type(exp_type(b));
1016
		cb = type_category(&tb);
1017
	}
2 7u83 1018
 
7 7u83 1019
	/* Operands can be integral ... */
1020
	if (IS_TYPE_INT(ca) && IS_TYPE_INT(cb)) {
1021
		EXP e;
1022
		TYPE t = arith_type(ta, tb, a, b);
1023
		a = convert_arith(t, a, lex_rem, 1);
1024
		b = convert_arith(t, b, lex_rem, 2);
2 7u83 1025
 
7 7u83 1026
		/* Check for constant operands */
1027
		if (check_div_exp(lex_rem, a, b)) {
1028
			e = make_binary_nat(exp_rem_tag, a, b);
1029
			return (e);
1030
		}
1031
 
1032
		/* Construct the result */
1033
		MAKE_exp_rem(t, a, b, e);
1034
		return (e);
2 7u83 1035
	}
1036
 
7 7u83 1037
	/* ... and nothing else */
1038
	if (!IS_TYPE_ERROR(ca) && !IS_TYPE_ERROR(cb)) {
1039
		report(crt_loc, ERR_expr_mul_rem_op(lex_rem, ta, tb));
1040
	}
1041
	return (make_error_exp(0));
2 7u83 1042
}
1043
 
1044
 
1045
/*
1046
    CONSTRUCT AN ADDITION EXPRESSION
1047
 
1048
    This routine constructs the expression 'a + b'.
1049
*/
1050
 
7 7u83 1051
EXP
1052
make_plus_exp(EXP a, EXP b)
2 7u83 1053
{
7 7u83 1054
	EXP e;
1055
	TYPE t;
1056
	OFFSET off;
1057
	TYPE ta, tb;
1058
	unsigned ca, cb;
1059
	int op = lex_plus;
1060
	ERROR err = NULL_err;
1061
	TYPE sa = DEREF_type(exp_type(a));
1062
	TYPE sb = DEREF_type(exp_type(b));
2 7u83 1063
 
7 7u83 1064
	/* Do reference conversions */
1065
	a = convert_reference(a, REF_NORMAL);
1066
	b = convert_reference(b, REF_NORMAL);
2 7u83 1067
 
7 7u83 1068
	/* Find operand types */
1069
	ta = DEREF_type(exp_type(a));
1070
	tb = DEREF_type(exp_type(b));
1071
	ca = type_category(&ta);
1072
	cb = type_category(&tb);
2 7u83 1073
 
7 7u83 1074
	/* Check for overloading */
2 7u83 1075
#if LANGUAGE_CPP
7 7u83 1076
	if (IS_TYPE_OVERLOAD(ca) || IS_TYPE_OVERLOAD(cb)) {
1077
		if (overload_depth == 0) {
1078
			e = binary_overload(op, a, b);
1079
			return (e);
1080
		}
2 7u83 1081
	}
1082
#endif
1083
 
7 7u83 1084
	/* Do lvalue conversions */
1085
	if (IS_TYPE_ADDRESS(ca)) {
1086
		a = convert_lvalue(a);
1087
		ta = DEREF_type(exp_type(a));
1088
		ca = type_category(&ta);
1089
	}
1090
	if (IS_TYPE_ADDRESS(cb)) {
1091
		b = convert_lvalue(b);
1092
		tb = DEREF_type(exp_type(b));
1093
		cb = type_category(&tb);
1094
	}
2 7u83 1095
 
7 7u83 1096
	/* Operands can be arithmetic ... */
1097
	if (IS_TYPE_ARITH(ca) && IS_TYPE_ARITH(cb)) {
1098
		t = arith_type(ta, tb, a, b);
1099
		a = convert_arith(t, a, op, 1);
1100
		b = convert_arith(t, b, op, 2);
1101
		if (IS_exp_int_lit(a) && IS_exp_int_lit(b)) {
1102
			e = make_binary_nat(exp_plus_tag, a, b);
1103
		} else {
1104
			MAKE_exp_plus(t, a, b, e);
1105
		}
1106
		return (e);
2 7u83 1107
	}
1108
 
7 7u83 1109
	/* Swap operands if the second is a pointer */
1110
	if (IS_TYPE_PTR(cb)) {
1111
		/* Alright because order of evaluation is undefined */
1112
		EXP c = a;
1113
		TYPE tc = ta;
1114
		unsigned cc = ca;
1115
		a = b;
1116
		b = c;
1117
		ta = tb;
1118
		tb = tc;
1119
		ca = cb;
1120
		cb = cc;
1121
		sa = sb;
1122
	}
2 7u83 1123
 
7 7u83 1124
	/* The first operand should now be a pointer */
1125
	if (!IS_TYPE_PTR(ca)) {
1126
		if (!IS_TYPE_ERROR(ca) && !IS_TYPE_ERROR(cb)) {
1127
			report(crt_loc, ERR_expr_add_op(op, ta, tb));
1128
		}
1129
		return (make_error_exp(0));
2 7u83 1130
	}
1131
 
7 7u83 1132
	/* The second operand should be integral */
1133
	if (!IS_TYPE_INT(cb)) {
1134
		if (!IS_TYPE_ERROR(cb)) {
1135
			report(crt_loc, ERR_expr_add_op(op, ta, tb));
1136
		}
1137
		return (make_paren_exp(a));
2 7u83 1138
	}
1139
 
7 7u83 1140
	/* Do bounds checks */
1141
	if (IS_exp_int_lit(b) && IS_type_array(sa)) {
1142
		check_bounds(op, sa, b);
1143
	}
2 7u83 1144
 
7 7u83 1145
	/* The pointer must be to a complete object type */
1146
	t = check_pointer(ta, &err);
1147
	if (!IS_NULL_err(err)) {
1148
		err = concat_error(err, ERR_expr_add_incompl(op));
1149
		report(crt_loc, err);
1150
	}
2 7u83 1151
 
7 7u83 1152
	/* Construct the result */
1153
	if (IS_TYPE_BITF(cb)) {
1154
		b = convert_bitfield(b);
1155
	}
1156
	off = make_off_mult(t, b, 0);
1157
	e = make_add_ptr(ta, a, off);
1158
	return (e);
2 7u83 1159
}
1160
 
1161
 
1162
/*
1163
    CONSTRUCT A SUBTRACTION EXPRESSION
1164
 
1165
    This routine constructs the expression 'a - b'.
1166
*/
1167
 
7 7u83 1168
EXP
1169
make_minus_exp(EXP a, EXP b)
2 7u83 1170
{
7 7u83 1171
	EXP e;
1172
	TYPE ta, tb;
1173
	unsigned ca, cb;
1174
	int op = lex_minus;
1175
	TYPE sa = DEREF_type(exp_type(a));
2 7u83 1176
 
7 7u83 1177
	/* Do reference conversions */
1178
	a = convert_reference(a, REF_NORMAL);
1179
	b = convert_reference(b, REF_NORMAL);
2 7u83 1180
 
7 7u83 1181
	/* Find operand types */
1182
	ta = DEREF_type(exp_type(a));
1183
	tb = DEREF_type(exp_type(b));
1184
	ca = type_category(&ta);
1185
	cb = type_category(&tb);
2 7u83 1186
 
7 7u83 1187
	/* Check for overloading */
2 7u83 1188
#if LANGUAGE_CPP
7 7u83 1189
	if (IS_TYPE_OVERLOAD(ca) || IS_TYPE_OVERLOAD(cb)) {
1190
		if (overload_depth == 0) {
1191
			e = binary_overload(op, a, b);
1192
			return (e);
1193
		}
2 7u83 1194
	}
1195
#endif
1196
 
7 7u83 1197
	/* Do lvalue conversions */
1198
	if (IS_TYPE_ADDRESS(ca)) {
1199
		a = convert_lvalue(a);
1200
		ta = DEREF_type(exp_type(a));
1201
		ca = type_category(&ta);
1202
	}
1203
	if (IS_TYPE_ADDRESS(cb)) {
1204
		b = convert_lvalue(b);
1205
		tb = DEREF_type(exp_type(b));
1206
		cb = type_category(&tb);
1207
	}
2 7u83 1208
 
7 7u83 1209
	/* Operands can be arithmetic ... */
1210
	if (IS_TYPE_ARITH(ca) && IS_TYPE_ARITH(cb)) {
1211
		TYPE t = arith_type(ta, tb, a, b);
1212
		a = convert_arith(t, a, op, 1);
1213
		b = convert_arith(t, b, op, 2);
1214
		if (IS_exp_int_lit(a) && IS_exp_int_lit(b)) {
1215
			e = make_binary_nat(exp_minus_tag, a, b);
1216
		} else {
1217
			MAKE_exp_minus(t, a, b, e);
1218
		}
1219
		return (e);
2 7u83 1220
	}
1221
 
7 7u83 1222
	/* ... or a pointer and an integer ... */
1223
	if (IS_TYPE_PTR(ca) && IS_TYPE_INT(cb)) {
1224
		TYPE t;
1225
		OFFSET off;
1226
		ERROR err = NULL_err;
2 7u83 1227
 
7 7u83 1228
		/* Do bounds checks */
1229
		if (IS_exp_int_lit(b) && IS_type_array(sa)) {
1230
			check_bounds(op, sa, b);
1231
		}
2 7u83 1232
 
7 7u83 1233
		/* The pointer must be to a complete object type */
1234
		t = check_pointer(ta, &err);
1235
		if (!IS_NULL_err(err)) {
1236
			err = concat_error(err, ERR_expr_add_incompl(op));
1237
			report(crt_loc, err);
1238
		}
1239
 
1240
		/* Construct the result */
1241
		if (IS_TYPE_BITF(cb)) {
1242
			b = convert_bitfield(b);
1243
		}
1244
		off = make_off_mult(t, b, 1);
1245
		e = make_add_ptr(ta, a, off);
1246
		return (e);
2 7u83 1247
	}
1248
 
7 7u83 1249
	/* ... or both pointers ... */
1250
	if (IS_TYPE_PTR(ca) && IS_TYPE_PTR(cb)) {
1251
		OFFSET off;
1252
		int suspect = 0;
1253
		ERROR err = NULL_err;
1254
		TYPE tc = ptr_common_type(ta, tb, 0, &suspect);
1255
		if (suspect > 0) {
1256
			/* Should have pointers to compatible types */
1257
			err = ERR_basic_link_incompat(ta, tb);
1258
			err = concat_error(err, ERR_expr_add_ptrdiff());
1259
			report(crt_loc, err);
1260
			err = NULL_err;
1261
		}
1262
		IGNORE check_pointer(ta, &err);
1263
		if (!IS_NULL_err(err)) {
1264
			/* Can't have pointer to incomplete type */
1265
			err = concat_error(err, ERR_expr_add_incompl(op));
1266
			report(crt_loc, err);
1267
			err = NULL_err;
1268
		}
1269
		IGNORE check_pointer(tb, &err);
1270
		if (!IS_NULL_err(err)) {
1271
			/* Can't have pointer to incomplete type */
1272
			if (eq_type_unqual(ta, tb)) {
1273
				destroy_error(err, 1);
1274
			} else {
1275
				err = concat_error(err,
1276
						   ERR_expr_add_incompl(op));
1277
				report(crt_loc, err);
1278
			}
1279
		}
2 7u83 1280
 
7 7u83 1281
		/* Construct the result */
1282
		if (suspect == -1) {
1283
			MAKE_exp_op(type_ptrdiff_t, op, a, b, e);
1284
		} else {
1285
			TYPE pc = DEREF_type(type_ptr_sub(tc));
1286
			if (option(OPT_ptr_operator)) {
1287
				EXP ra = NULL_exp;
1288
				EXP rb = NULL_exp;
1289
				IGNORE find_exp_linkage(a, &ra, 1);
1290
				IGNORE find_exp_linkage(b, &rb, 1);
1291
				if (!IS_NULL_exp(ra) && !IS_NULL_exp(rb)) {
1292
					if (IS_exp_string_lit(ra) ||
1293
					    !eq_exp(ra, rb, 0)) {
1294
						report(crt_loc, ERR_expr_add_different());
1295
					}
1296
				}
1297
			}
1298
			a = convert_ptr_common(tc, a, op, 1);
1299
			b = convert_ptr_common(tc, b, op, 2);
1300
			if (IS_type_top_etc(pc)) {
1301
				/* Map 'void *' to 'char *' */
1302
				unsigned conv = (CONV_PTR_VOID | CONV_REVERSE);
1303
				tc = type_char_star;
1304
				pc = type_char;
1305
				MAKE_exp_cast(tc, conv, a, a);
1306
				MAKE_exp_cast(tc, conv, b, b);
1307
			}
1308
			MAKE_off_ptr_diff(a, b, off);
1309
			MAKE_exp_offset_size(type_ptrdiff_t, off, pc, 0, e);
1310
		}
1311
		return (e);
2 7u83 1312
	}
1313
 
7 7u83 1314
	/* ... and nothing else */
1315
	if (!IS_TYPE_ERROR(ca) && !IS_TYPE_ERROR(cb)) {
1316
		report(crt_loc, ERR_expr_add_op(op, ta, tb));
2 7u83 1317
	}
7 7u83 1318
	return (make_error_exp(0));
2 7u83 1319
}
1320
 
1321
 
1322
/*
1323
    CHECK FOR DUBIOUS SHIFT EXPRESSIONS
1324
 
1325
    This routine checks the shift operation 'a << b' or 'a >> b' for
1326
    dubious constant operands.  All the necessary operand and arithmetic
1327
    type conversions have already been performed on a and b, and the type
1328
    of a is passed in as t.  The routine returns 1 if both operands are
1329
    integer constants.
1330
*/
1331
 
7 7u83 1332
int
1333
check_shift_exp(int op, TYPE t, EXP a, EXP b)
2 7u83 1334
{
7 7u83 1335
	int ret = 1;
1336
	ERROR err = NULL_err;
2 7u83 1337
 
7 7u83 1338
	/* Check first operand */
1339
	if (IS_exp_int_lit(a)) {
1340
		NAT n = DEREF_nat(exp_int_lit_nat(a));
1341
		if (is_negative_nat(n)) {
1342
			err = ERR_expr_shift_op1_neg(op, n);
1343
		}
1344
	} else {
1345
		if (check_int_type(t, btype_signed)) {
1346
			err = ERR_expr_shift_op1_sign(op);
1347
		}
1348
		ret = 0;
2 7u83 1349
	}
7 7u83 1350
 
1351
	/* Check second operand */
1352
	if (IS_exp_int_lit(b)) {
1353
		NAT n = DEREF_nat(exp_int_lit_nat(b));
1354
		if (is_negative_nat(n)) {
1355
			err = concat_error(err, ERR_expr_shift_op2_neg(op, n));
1356
		} else if (check_type_size(t, n) >= 0) {
1357
			err = concat_error(err,
1358
					   ERR_expr_shift_op2_big(op, n, t));
1359
		}
1360
	} else {
1361
		ret = 0;
2 7u83 1362
	}
1363
 
7 7u83 1364
	/* Report any accumulated errors */
1365
	if (!IS_NULL_err(err)) {
1366
		report(crt_loc, err);
2 7u83 1367
	}
7 7u83 1368
	return (ret);
2 7u83 1369
}
1370
 
1371
 
1372
/*
1373
    CONSTRUCT A SHIFT EXPRESSION
1374
 
1375
    This routine constructs the expressions 'a << b' and 'a >> b'.  Note
1376
    that this has a lower priority than plus and minus.
1377
*/
1378
 
7 7u83 1379
EXP
1380
make_shift_exp(int op, EXP a, EXP b)
2 7u83 1381
{
7 7u83 1382
	EXP e;
1383
	TYPE ta, tb;
1384
	unsigned ca, cb;
2 7u83 1385
 
7 7u83 1386
	/* Check for non-obvious resolutions */
1387
	if (option(OPT_paren)) {
1388
		check_paren(PAREN_PLUS, op, a, b);
1389
	}
2 7u83 1390
 
7 7u83 1391
	/* Do reference conversions */
1392
	a = convert_reference(a, REF_NORMAL);
1393
	b = convert_reference(b, REF_NORMAL);
2 7u83 1394
 
7 7u83 1395
	/* Find operand types */
1396
	ta = DEREF_type(exp_type(a));
1397
	tb = DEREF_type(exp_type(b));
1398
	ca = type_category(&ta);
1399
	cb = type_category(&tb);
2 7u83 1400
 
7 7u83 1401
	/* Check for overloading */
2 7u83 1402
#if LANGUAGE_CPP
7 7u83 1403
	if (IS_TYPE_OVERLOAD(ca) || IS_TYPE_OVERLOAD(cb)) {
1404
		if (overload_depth == 0) {
1405
			e = binary_overload(op, a, b);
1406
			return (e);
1407
		}
2 7u83 1408
	}
1409
#endif
1410
 
7 7u83 1411
	/* Do lvalue conversions */
1412
	if (IS_TYPE_ADDRESS(ca)) {
1413
		a = convert_lvalue(a);
1414
		ta = DEREF_type(exp_type(a));
1415
		ca = type_category(&ta);
1416
	}
1417
	if (IS_TYPE_ADDRESS(cb)) {
1418
		b = convert_lvalue(b);
1419
		tb = DEREF_type(exp_type(b));
1420
		cb = type_category(&tb);
1421
	}
2 7u83 1422
 
7 7u83 1423
	/* Operands can be integral ... */
1424
	if (IS_TYPE_INT(ca) && IS_TYPE_INT(cb)) {
1425
		unsigned tag;
1426
		ERROR err = NULL_err;
1427
		TYPE pta = promote_type(ta);
1428
		TYPE ptb = promote_type(tb);
1429
		a = convert_promote(pta, a);
1430
		b = convert_promote(ptb, b);
1431
		tag = (op == lex_lshift ? exp_lshift_tag : exp_rshift_tag);
2 7u83 1432
 
7 7u83 1433
		/* Check for dubious shifts */
1434
		if (check_shift_exp(op, pta, a, b)) {
1435
			/* Allow for constant evaluation */
1436
			if (!IS_NULL_err(err)) {
1437
				report(crt_loc, err);
1438
			}
1439
			e = make_binary_nat(tag, a, b);
1440
			return (e);
1441
		}
1442
 
1443
		/* Construct the result */
1444
		MAKE_exp_plus_etc(tag, pta, a, b, e);
1445
		return (e);
2 7u83 1446
	}
1447
 
7 7u83 1448
	/* ... and nothing else */
1449
	if (!IS_TYPE_ERROR(ca) && !IS_TYPE_ERROR(cb)) {
1450
		report(crt_loc, ERR_expr_shift_op(op, ta, tb));
1451
	}
1452
	return (make_error_exp(0));
2 7u83 1453
}
1454
 
1455
 
1456
/*
1457
    CONSTRUCT A RELATIONAL OPERATOR
1458
 
1459
    This routine converts the lexical token op to a relational operator.
1460
*/
1461
 
7 7u83 1462
static NTEST
1463
make_ntest(int op)
2 7u83 1464
{
7 7u83 1465
	switch (op) {
1466
	case lex_eq:
1467
		return (ntest_eq);
1468
	case lex_not_Heq_H1:
1469
		return (ntest_not_eq);
1470
	case lex_less:
1471
		return (ntest_less);
1472
	case lex_less_Heq:
1473
		return (ntest_less_eq);
1474
	case lex_greater:
1475
		return (ntest_greater);
1476
	case lex_greater_Heq:
1477
		return (ntest_greater_eq);
1478
	}
1479
	return (ntest_none);
2 7u83 1480
}
1481
 
1482
 
1483
/*
1484
    CONSTRUCT A RELATIONAL EXPRESSION
1485
 
1486
    This routine constructs the expressions 'a < b', 'a > b', 'a <= b' and
1487
    'a >= b'.
1488
*/
1489
 
7 7u83 1490
EXP
1491
make_relation_exp(int op, EXP a, EXP b)
2 7u83 1492
{
7 7u83 1493
	EXP e;
1494
	NTEST tst;
1495
	TYPE ta, tb;
1496
	unsigned ca, cb;
2 7u83 1497
 
7 7u83 1498
	/* Check for dubious relations */
1499
	if (option(OPT_paren)) {
1500
		check_relation(op, a, b);
1501
	}
2 7u83 1502
 
7 7u83 1503
	/* Do reference conversions */
1504
	a = convert_reference(a, REF_NORMAL);
1505
	b = convert_reference(b, REF_NORMAL);
2 7u83 1506
 
7 7u83 1507
	/* Find operand types */
1508
	ta = DEREF_type(exp_type(a));
1509
	tb = DEREF_type(exp_type(b));
1510
	ca = type_category(&ta);
1511
	cb = type_category(&tb);
2 7u83 1512
 
7 7u83 1513
	/* Check for overloading */
2 7u83 1514
#if LANGUAGE_CPP
7 7u83 1515
	if (IS_TYPE_OVERLOAD(ca) || IS_TYPE_OVERLOAD(cb)) {
1516
		if (overload_depth == 0) {
1517
			e = binary_overload(op, a, b);
1518
			return (e);
1519
		}
2 7u83 1520
	}
1521
#endif
1522
 
7 7u83 1523
	/* Do lvalue conversions */
1524
	if (IS_TYPE_ADDRESS(ca)) {
1525
		a = convert_lvalue(a);
1526
		ta = DEREF_type(exp_type(a));
1527
		ca = type_category(&ta);
1528
	}
1529
	if (IS_TYPE_ADDRESS(cb)) {
1530
		b = convert_lvalue(b);
1531
		tb = DEREF_type(exp_type(b));
1532
		cb = type_category(&tb);
1533
	}
2 7u83 1534
 
7 7u83 1535
	/* Operands can both be arithmetic ... */
1536
	if (IS_TYPE_ARITH(ca) && IS_TYPE_ARITH(cb)) {
1537
		TYPE t = arith_type(ta, tb, a, b);
1538
		a = convert_arith(t, a, op, 1);
1539
		b = convert_arith(t, b, op, 2);
2 7u83 1540
 
7 7u83 1541
		if (IS_exp_int_lit(a)) {
1542
			/* Allow for constant evaluation */
1543
			if (IS_exp_int_lit(b)) {
1544
				tst = make_ntest(op);
1545
				e = make_compare_nat(tst, a, b);
1546
				return (e);
1547
			}
2 7u83 1548
 
7 7u83 1549
			/* Check for unsigned comparisons against zero */
1550
			if (is_zero_exp(a) &&
1551
			    check_int_type(t, btype_unsigned)) {
1552
				if (op == lex_greater) {
1553
					report(crt_loc, ERR_expr_rel_false());
1554
				} else if (op == lex_less_Heq) {
1555
					report(crt_loc, ERR_expr_rel_true());
1556
				}
1557
			}
2 7u83 1558
		}
7 7u83 1559
 
1560
		/* Check for unsigned comparisons against zero */
1561
		if (IS_exp_int_lit(b)) {
1562
			if (is_zero_exp(b) &&
1563
			    check_int_type(t, btype_unsigned)) {
1564
				if (op == lex_less) {
1565
					report(crt_loc, ERR_expr_rel_false());
1566
				} else if (op == lex_greater_Heq) {
1567
					report(crt_loc, ERR_expr_rel_true());
1568
				}
1569
			}
1570
		}
1571
 
1572
		/* Construct the result */
1573
		tst = make_ntest(op);
1574
		MAKE_exp_compare(type_bool, tst, a, b, e);
1575
		return (e);
2 7u83 1576
	}
1577
 
7 7u83 1578
	/* ... or both pointers ... */
1579
	if (IS_TYPE_PTR(ca)) {
1580
		if (IS_TYPE_PTR(cb)) {
1581
			int suspect = 0;
1582
			TYPE t = ptr_common_type(ta, tb, 1, &suspect);
1583
			if (suspect == -1) {
1584
				/* Allow for template types */
1585
				MAKE_exp_op(type_bool, op, a, b, e);
1586
				return (e);
1587
			}
1588
			if (suspect == 2) {
1589
				/* Can't bring to a common type */
1590
				ERROR err = ERR_basic_link_incompat(ta, tb);
1591
				err = concat_error(err, ERR_conv_ptr_common());
1592
				err = concat_error(err, ERR_expr_rel_ptr(op));
1593
				report(crt_loc, err);
1594
			} else if (suspect == 1) {
1595
				/* These comparisons are suspect */
1596
				report(crt_loc,
1597
				       ERR_expr_rel_ptr_void(op, ta, tb));
1598
			}
1599
			a = convert_ptr_common(t, a, op, 1);
1600
			b = convert_ptr_common(t, b, op, 2);
1601
			tst = make_ntest(op);
1602
			MAKE_exp_compare(type_bool, tst, a, b, e);
1603
			return (e);
2 7u83 1604
		}
7 7u83 1605
		if (IS_TYPE_INT(cb)) {
1606
			/* Allow zero integer as a null pointer */
1607
			b = make_null_ptr(b, ta);
1608
			if (IS_NULL_exp(b)) {
1609
				report(crt_loc,
1610
				       ERR_expr_rel_nonzero(op, ta, tb));
1611
				b = make_null_ptr(NULL_exp, ta);
1612
			} else {
1613
				report(crt_loc, ERR_expr_rel_null(op));
1614
			}
1615
			tst = make_ntest(op);
1616
			MAKE_exp_compare(type_bool, tst, a, b, e);
1617
			return (e);
1618
		}
1619
	} else if (IS_TYPE_PTR(cb)) {
1620
		if (IS_TYPE_INT(ca)) {
1621
			/* Allow zero integer as a null pointer */
1622
			a = make_null_ptr(a, tb);
1623
			if (IS_NULL_exp(a)) {
1624
				report(crt_loc,
1625
				       ERR_expr_rel_nonzero(op, tb, ta));
1626
				a = make_null_ptr(NULL_exp, tb);
1627
			} else {
1628
				report(crt_loc, ERR_expr_rel_null(op));
1629
			}
1630
			tst = make_ntest(op);
1631
			MAKE_exp_compare(type_bool, tst, a, b, e);
1632
			return (e);
1633
		}
2 7u83 1634
	}
1635
 
7 7u83 1636
	/* ... and nothing else */
1637
	if (!IS_TYPE_ERROR(ca) && !IS_TYPE_ERROR(cb)) {
1638
		report(crt_loc, ERR_expr_rel_op(op, ta, tb));
2 7u83 1639
	}
7 7u83 1640
	tst = make_ntest(op);
1641
	MAKE_exp_test(type_bool, tst, b, e);
1642
	return (e);
2 7u83 1643
}
1644
 
1645
 
1646
/*
1647
    CONSTRUCT AN EQUALITY EXPRESSION
1648
 
1649
    This routine constructs the expressions 'a == b' and 'a != b'.
1650
*/
1651
 
7 7u83 1652
EXP
1653
make_equality_exp(int op, EXP a, EXP b)
2 7u83 1654
{
7 7u83 1655
	EXP e;
1656
	NTEST tst;
1657
	TYPE ta, tb;
1658
	unsigned ca, cb;
2 7u83 1659
 
7 7u83 1660
	/* Check for dubious relations */
1661
	if (option(OPT_paren)) {
1662
		check_relation(op, a, b);
1663
	}
2 7u83 1664
 
7 7u83 1665
	/* Do reference conversions */
1666
	a = convert_reference(a, REF_NORMAL);
1667
	b = convert_reference(b, REF_NORMAL);
2 7u83 1668
 
7 7u83 1669
	/* Find operand types */
1670
	ta = DEREF_type(exp_type(a));
1671
	tb = DEREF_type(exp_type(b));
1672
	ca = type_category(&ta);
1673
	cb = type_category(&tb);
2 7u83 1674
 
7 7u83 1675
	/* Check for overloading */
2 7u83 1676
#if LANGUAGE_CPP
7 7u83 1677
	if (IS_TYPE_OVERLOAD(ca) || IS_TYPE_OVERLOAD(cb)) {
1678
		if (overload_depth == 0) {
1679
			e = binary_overload(op, a, b);
1680
			return (e);
1681
		}
2 7u83 1682
	}
1683
#endif
1684
 
7 7u83 1685
	/* Do lvalue conversions */
1686
	if (IS_TYPE_ADDRESS(ca)) {
1687
		a = convert_lvalue(a);
1688
		ta = DEREF_type(exp_type(a));
1689
		ca = type_category(&ta);
2 7u83 1690
	}
7 7u83 1691
	if (IS_TYPE_ADDRESS(cb)) {
1692
		b = convert_lvalue(b);
1693
		tb = DEREF_type(exp_type(b));
1694
		cb = type_category(&tb);
2 7u83 1695
	}
1696
 
7 7u83 1697
	/* Operands can both be arithmetic ... */
1698
	if (IS_TYPE_ARITH(ca) && IS_TYPE_ARITH(cb)) {
1699
		TYPE t = arith_type(ta, tb, a, b);
1700
		if (IS_type_floating(t)) {
1701
			report(crt_loc, ERR_expr_eq_float(op));
1702
		}
1703
		a = convert_arith(t, a, op, 1);
1704
		b = convert_arith(t, b, op, 2);
1705
		tst = make_ntest(op);
1706
		if (IS_exp_int_lit(a) && IS_exp_int_lit(b)) {
1707
			e = make_compare_nat(tst, a, b);
1708
		} else {
1709
			MAKE_exp_compare(type_bool, tst, a, b, e);
1710
		}
1711
		return (e);
2 7u83 1712
	}
7 7u83 1713
 
1714
	/* ... or both pointers ... */
1715
	if (IS_TYPE_PTR(ca)) {
1716
		if (IS_TYPE_PTR(cb)) {
1717
			int suspect = 0;
1718
			TYPE t = ptr_common_type(ta, tb, 1, &suspect);
1719
			if (suspect == -1) {
1720
				/* Allow for template types */
1721
				MAKE_exp_op(type_bool, op, a, b, e);
1722
				return (e);
1723
			}
1724
			if (suspect == 2) {
1725
				ERROR err = ERR_basic_link_incompat(ta, tb);
1726
				err = concat_error(err, ERR_conv_ptr_common());
1727
				err = concat_error(err, ERR_expr_eq_ptr(op));
1728
				report(crt_loc, err);
1729
			}
1730
			a = convert_ptr_common(t, a, op, 1);
1731
			b = convert_ptr_common(t, b, op, 2);
1732
			if (IS_exp_null(a)) {
1733
				/* Make null pointer the second argument */
1734
				EXP c = a;
1735
				a = b;
1736
				b = c;
1737
			}
1738
			tst = make_ntest(op);
1739
			MAKE_exp_compare(type_bool, tst, a, b, e);
1740
			return (e);
1741
		}
1742
		if (IS_TYPE_INT(cb)) {
1743
			/* Allow zero integer as a null pointer */
1744
			b = make_null_ptr(b, ta);
1745
			if (IS_NULL_exp(b)) {
1746
				report(crt_loc,
1747
				       ERR_expr_eq_nonzero(op, ta, tb));
1748
				b = make_null_ptr(NULL_exp, ta);
1749
			}
1750
			tst = make_ntest(op);
1751
			MAKE_exp_compare(type_bool, tst, a, b, e);
1752
			return (e);
1753
		}
1754
	} else if (IS_TYPE_PTR(cb)) {
1755
		if (IS_TYPE_INT(ca)) {
1756
			/* Allow zero integer as a null pointer */
1757
			a = make_null_ptr(a, tb);
1758
			if (IS_NULL_exp(a)) {
1759
				report(crt_loc,
1760
				       ERR_expr_eq_nonzero(op, tb, ta));
1761
				a = make_null_ptr(NULL_exp, tb);
1762
			}
1763
			tst = make_ntest(op);
1764
			MAKE_exp_compare(type_bool, tst, b, a, e);
1765
			return (e);
1766
		}
2 7u83 1767
	}
1768
 
1769
#if LANGUAGE_CPP
7 7u83 1770
	/* ... or both pointers to members ... */
1771
	if (IS_TYPE_PTR_MEM(ca)) {
1772
		if (IS_TYPE_PTR_MEM(cb)) {
1773
			int suspect = 0;
1774
			TYPE t = ptr_mem_common_type(ta, tb, &suspect);
1775
			if (suspect == -1) {
1776
				/* Allow for template types */
1777
				MAKE_exp_op(type_bool, op, a, b, e);
1778
				return (e);
1779
			}
1780
			if (suspect == 2) {
1781
				ERROR err = ERR_basic_link_incompat(ta, tb);
1782
				err = concat_error(err, ERR_conv_mem_common());
1783
				err = concat_error(err, ERR_expr_eq_mptr(op));
1784
				report(crt_loc, err);
1785
			} else {
1786
				a = convert_ptr_mem_common(t, a, op, 1);
1787
				b = convert_ptr_mem_common(t, b, op, 2);
1788
			}
1789
			if (IS_exp_null(a)) {
1790
				/* Make null pointer the second argument */
1791
				EXP c = a;
1792
				a = b;
1793
				b = c;
1794
			}
1795
			tst = make_ntest(op);
1796
			MAKE_exp_compare(type_bool, tst, a, b, e);
1797
			return (e);
1798
		}
1799
		if (IS_TYPE_INT(cb)) {
1800
			/* Allow zero integer as a null pointer member */
1801
			b = make_null_ptr(b, ta);
1802
			if (IS_NULL_exp(b)) {
1803
				report(crt_loc,
1804
				       ERR_expr_eq_nonzero(op, ta, tb));
1805
				b = make_null_ptr(NULL_exp, ta);
1806
			}
1807
			tst = make_ntest(op);
1808
			MAKE_exp_compare(type_bool, tst, a, b, e);
1809
			return (e);
1810
		}
1811
	} else if (IS_TYPE_PTR_MEM(cb)) {
1812
		if (IS_TYPE_INT(ca)) {
1813
			/* Allow zero integer as a null pointer member */
1814
			a = make_null_ptr(a, tb);
1815
			if (IS_NULL_exp(a)) {
1816
				report(crt_loc,
1817
				       ERR_expr_eq_nonzero(op, tb, ta));
1818
				a = make_null_ptr(NULL_exp, tb);
1819
			}
1820
			tst = make_ntest(op);
1821
			MAKE_exp_compare(type_bool, tst, b, a, e);
1822
			return (e);
1823
		}
2 7u83 1824
	}
1825
#endif
1826
 
7 7u83 1827
	/* ... and nothing else */
1828
	if (!IS_TYPE_ERROR(ca) && !IS_TYPE_ERROR(cb)) {
1829
		report(crt_loc, ERR_expr_eq_op(op, ta, tb));
1830
	}
1831
	tst = make_ntest(op);
1832
	MAKE_exp_compare(type_bool, tst, a, b, e);
1833
	return (e);
2 7u83 1834
}
1835
 
1836
 
1837
/*
1838
    CONSTRUCT A BIT EXPRESSION
1839
 
1840
    This routine constructs the expressions 'a & b', 'a ^ b' and 'a | b'.
1841
*/
1842
 
7 7u83 1843
static EXP
1844
make_bit_exp(int op, unsigned tag, EXP a, EXP b)
2 7u83 1845
{
7 7u83 1846
	EXP e;
1847
	TYPE ta, tb;
1848
	unsigned ca, cb;
2 7u83 1849
 
7 7u83 1850
	/* Do reference conversions */
1851
	a = convert_reference(a, REF_NORMAL);
1852
	b = convert_reference(b, REF_NORMAL);
2 7u83 1853
 
7 7u83 1854
	/* Find operand types */
1855
	ta = DEREF_type(exp_type(a));
1856
	tb = DEREF_type(exp_type(b));
1857
	ca = type_category(&ta);
1858
	cb = type_category(&tb);
2 7u83 1859
 
7 7u83 1860
	/* Check for overloading */
2 7u83 1861
#if LANGUAGE_CPP
7 7u83 1862
	if (IS_TYPE_OVERLOAD(ca) || IS_TYPE_OVERLOAD(cb)) {
1863
		if (overload_depth == 0) {
1864
			e = binary_overload(op, a, b);
1865
			return (e);
1866
		}
2 7u83 1867
	}
1868
#endif
1869
 
7 7u83 1870
	/* Do lvalue conversions */
1871
	if (IS_TYPE_ADDRESS(ca)) {
1872
		a = convert_lvalue(a);
1873
		ta = DEREF_type(exp_type(a));
1874
		ca = type_category(&ta);
1875
	}
1876
	if (IS_TYPE_ADDRESS(cb)) {
1877
		b = convert_lvalue(b);
1878
		tb = DEREF_type(exp_type(b));
1879
		cb = type_category(&tb);
1880
	}
2 7u83 1881
 
7 7u83 1882
	/* Operands can both be integral ... */
1883
	if (IS_TYPE_INT(ca) && IS_TYPE_INT(cb)) {
1884
		TYPE t = arith_type(ta, tb, a, b);
1885
		a = convert_arith(t, a, op, 1);
1886
		b = convert_arith(t, b, op, 2);
1887
		if (IS_exp_int_lit(a) && IS_exp_int_lit(b)) {
1888
			e = make_binary_nat(tag, a, b);
1889
		} else {
1890
			MAKE_exp_plus_etc(tag, t, a, b, e);
1891
		}
1892
		return (e);
2 7u83 1893
	}
1894
 
7 7u83 1895
	/* ... and nothing else */
1896
	if (!IS_TYPE_ERROR(ca) && !IS_TYPE_ERROR(cb)) {
1897
		switch (tag) {
1898
		case exp_and_tag: {
1899
			report(crt_loc, ERR_expr_bit_and_op(op, ta, tb));
1900
			break;
1901
		}
1902
		case exp_or_tag: {
1903
			report(crt_loc, ERR_expr_or_op(op, ta, tb));
1904
			break;
1905
		}
1906
		case exp_xor_tag: {
1907
			report(crt_loc, ERR_expr_xor_op(op, ta, tb));
1908
			break;
1909
		}
1910
		}
2 7u83 1911
	}
7 7u83 1912
	return (make_error_exp(0));
2 7u83 1913
}
1914
 
1915
 
1916
/*
1917
    CONSTRUCT A BITWISE AND EXPRESSION
1918
 
1919
    This routine constructs the expression 'a & b'.  Note that this
1920
    has lower priority than plus or minus and the equality operators.
1921
*/
1922
 
7 7u83 1923
EXP
1924
make_and_exp(EXP a, EXP b)
2 7u83 1925
{
7 7u83 1926
	EXP e;
1927
	int op = lex_and_H1;
1928
	if (option(OPT_paren)) {
1929
		check_paren(PAREN_EQUALITY, op, a, b);
1930
	}
1931
	e = make_bit_exp(op, exp_and_tag, a, b);
1932
	return (e);
2 7u83 1933
}
1934
 
1935
 
1936
/*
1937
    CONSTRUCT A BITWISE XOR EXPRESSION
1938
 
1939
    This routine constructs the expression 'a ^ b'.  Note that this has
1940
    a lower priority than bitwise and, plus and minus.
1941
*/
1942
 
7 7u83 1943
EXP
1944
make_xor_exp(EXP a, EXP b)
2 7u83 1945
{
7 7u83 1946
	EXP e;
1947
	int op = lex_xor_H1;
1948
	if (option(OPT_paren)) {
1949
		check_paren(PAREN_AND, op, a, b);
1950
	}
1951
	e = make_bit_exp(op, exp_xor_tag, a, b);
1952
	return (e);
2 7u83 1953
}
1954
 
1955
 
1956
/*
1957
    CONSTRUCT A BITWISE OR EXPRESSION
1958
 
1959
    This routine constructs the expression 'a | b'.  Note that this has
1960
    a lower priority than bitwise and, bitwise xor, plus and minus.
1961
*/
1962
 
7 7u83 1963
EXP
1964
make_or_exp(EXP a, EXP b)
2 7u83 1965
{
7 7u83 1966
	EXP e;
1967
	int op = lex_or_H1;
1968
	if (option(OPT_paren)) {
1969
		check_paren(PAREN_XOR, op, a, b);
1970
	}
1971
	e = make_bit_exp(op, exp_or_tag, a, b);
1972
	return (e);
2 7u83 1973
}
1974
 
1975
 
1976
/*
1977
    CONSTRUCT A LOGICAL EXPRESSION
1978
 
1979
    This routine constructs the expressions 'a && b' and 'a || b'.
1980
*/
1981
 
7 7u83 1982
static EXP
1983
make_logic_exp(int op, unsigned tag, EXP a, EXP b)
2 7u83 1984
{
7 7u83 1985
	EXP e;
1986
	TYPE ta, tb;
1987
	unsigned ca, cb;
1988
	ERROR err = NULL_err;
1989
	unsigned taga = TAG_exp(a);
1990
	unsigned tagb = TAG_exp(b);
2 7u83 1991
 
7 7u83 1992
	/* Do reference conversions */
1993
	a = convert_reference(a, REF_NORMAL);
1994
	b = convert_reference(b, REF_NORMAL);
2 7u83 1995
 
7 7u83 1996
	/* Find operand types */
1997
	ta = DEREF_type(exp_type(a));
1998
	tb = DEREF_type(exp_type(b));
1999
	ca = type_category(&ta);
2000
	cb = type_category(&tb);
2 7u83 2001
 
7 7u83 2002
	/* Check for overloading */
2 7u83 2003
#if LANGUAGE_CPP
7 7u83 2004
	if (IS_TYPE_OVERLOAD(ca) || IS_TYPE_OVERLOAD(cb)) {
2005
		if (overload_depth == 0) {
2006
			e = binary_overload(op, a, b);
2007
			return (e);
2008
		}
2 7u83 2009
	}
2010
#endif
2011
 
7 7u83 2012
	/* Do lvalue conversions */
2013
	if (IS_TYPE_ADDRESS(ca)) {
2014
		a = convert_lvalue(a);
2015
	}
2016
	if (IS_TYPE_ADDRESS(cb)) {
2017
		b = convert_lvalue(b);
2018
	}
2 7u83 2019
 
7 7u83 2020
	/* Convert first operand to a boolean */
2021
	a = convert_boolean(a, taga, &err);
2022
	if (!IS_NULL_err(err)) {
2023
		ERROR err2;
2024
		if (tag == exp_log_and_tag) {
2025
			err2 = ERR_expr_log_and_op(op);
2026
		} else {
2027
			err2 = ERR_expr_log_or_op(op);
2028
		}
2029
		err = concat_error(err, err2);
2030
		report(crt_loc, err);
2031
		err = NULL_err;
2 7u83 2032
	}
2033
 
7 7u83 2034
	/* Convert second operand to a boolean */
2035
	b = convert_boolean(b, tagb, &err);
2036
	if (!IS_NULL_err(err)) {
2037
		ERROR err2;
2038
		if (tag == exp_log_and_tag) {
2039
			err2 = ERR_expr_log_and_op(op);
2040
		} else {
2041
			err2 = ERR_expr_log_or_op(op);
2042
		}
2043
		err = concat_error(err, err2);
2044
		report(crt_loc, err);
2045
	}
2046
 
2047
	/* Construct the result */
2048
	if (IS_exp_int_lit(a) && IS_exp_int_lit(b)) {
2049
		e = make_binary_nat(tag, a, b);
2 7u83 2050
	} else {
7 7u83 2051
		MAKE_exp_plus_etc(tag, type_bool, a, b, e);
2 7u83 2052
	}
7 7u83 2053
	return (e);
2 7u83 2054
}
2055
 
2056
 
2057
/*
2058
    CONSTRUCT A LOGICAL AND EXPRESSION
2059
 
2060
    This routine constructs the expression 'a && b'.
2061
*/
2062
 
7 7u83 2063
EXP
2064
make_log_and_exp(EXP a, EXP b)
2 7u83 2065
{
7 7u83 2066
	EXP e;
2067
	e = make_logic_exp(lex_logical_Hand_H1, exp_log_and_tag, a, b);
2068
	return (e);
2 7u83 2069
}
2070
 
2071
 
2072
/*
2073
    CONSTRUCT A LOGICAL OR EXPRESSION
2074
 
2075
    This routine constructs the expression 'a || b'.  Note that this
2076
    has a lower priority than logical and.
2077
*/
2078
 
7 7u83 2079
EXP
2080
make_log_or_exp(EXP a, EXP b)
2 7u83 2081
{
7 7u83 2082
	EXP e;
2083
	if (option(OPT_paren)) {
2084
		check_logic(a, b);
2085
	}
2086
	e = make_logic_exp(lex_logical_Hor_H1, exp_log_or_tag, a, b);
2087
	return (e);
2 7u83 2088
}
2089
 
2090
 
2091
/*
2092
    CONSTRUCT A TEMPLATE DEPENDENT CONDITIONAL EXPRESSION
2093
 
2094
    This routine constructs the expression 'a ? b : c' where one of b
2095
    and c depends on a template parameter.
2096
*/
2097
 
2098
#if LANGUAGE_CPP
2099
 
7 7u83 2100
static EXP
2101
make_templ_cond(EXP a, EXP b, EXP c)
2 7u83 2102
{
7 7u83 2103
	EXP e;
2104
	TYPE t = type_templ_param;
2105
	LIST(EXP)p = NULL_list(EXP);
2106
	CONS_exp(c, p, p);
2107
	CONS_exp(b, p, p);
2108
	CONS_exp(a, p, p);
2109
	MAKE_exp_opn(t, lex_cond_Hop, p, e);
2110
	return (e);
2 7u83 2111
}
2112
 
2113
#endif
2114
 
2115
 
2116
/*
2117
    ADD A VALUE TO AN EXCEPTION EXPRESSION
2118
 
2119
    This routine is used to turn the throw expression a into an expression
2120
    of type t.  This is required so that a conditional involving a throw
2121
    expression satisfies the shape requirements of a TDF conditional
2122
    construct.
2123
*/
2124
 
7 7u83 2125
static EXP
2126
make_except_value(TYPE t, EXP a)
2 7u83 2127
{
7 7u83 2128
	if (!IS_type_top_etc(t)) {
2129
		EXP b;
2130
		MAKE_exp_value(t, b);
2131
		a = join_exp(a, b);
2132
	}
2133
	return (a);
2 7u83 2134
}
2135
 
2136
 
2137
/*
2138
    CONSTRUCT A CONDITIONAL EXPRESSION
2139
 
2140
    This routine constructs the expression 'a ? b : c'.  Although '?:'
2141
    cannot be overloaded, user-defined conversions which bring the second
2142
    and third operands to a common type are considered.  This is done
2143
    using a dummy binary operation 'b : c' given by 'NULL_exp ? b : c'.
2144
*/
2145
 
7 7u83 2146
EXP
2147
make_cond_exp(EXP a, EXP b, EXP c)
2 7u83 2148
{
7 7u83 2149
	EXP e;
2150
	TYPE t;
2151
	TYPE tb, tc;
2152
	unsigned cb, cc;
2153
	int op = lex_cond_Hop;
2 7u83 2154
 
7 7u83 2155
	/* First operand is converted to a boolean */
2156
	if (!IS_NULL_exp(a)) {
2157
		unsigned cr = crt_condition;
2158
		a = check_cond(a, &e, op);
2159
		crt_condition = cr;
2160
	}
2 7u83 2161
 
7 7u83 2162
	/* Do reference conversion on second and third operands */
2163
	b = convert_reference(b, REF_NORMAL);
2164
	c = convert_reference(c, REF_NORMAL);
2 7u83 2165
 
7 7u83 2166
	/* Find operand types */
2167
	tb = DEREF_type(exp_type(b));
2168
	cb = type_category(&tb);
2169
	tc = DEREF_type(exp_type(c));
2170
	cc = type_category(&tc);
2 7u83 2171
 
7 7u83 2172
	/* Check for template parameters */
2 7u83 2173
#if LANGUAGE_CPP
7 7u83 2174
	if (IS_TYPE_TEMPL(cb) || IS_TYPE_TEMPL(cc)) {
2175
		e = make_templ_cond(a, b, c);
2176
		return (e);
2177
	}
2 7u83 2178
#endif
2179
 
7 7u83 2180
	/* Check for throw expressions */
2181
	if (IS_exp_exception(b)) {
2182
		c = convert_lvalue(c);
2183
		t = DEREF_type(exp_type(c));
2184
		b = make_except_value(t, b);
2185
		goto return_lab;
2186
	}
2187
	if (IS_exp_exception(c)) {
2188
		b = convert_lvalue(b);
2189
		t = DEREF_type(exp_type(b));
2190
		c = make_except_value(t, c);
2191
		goto return_lab;
2192
	}
2 7u83 2193
 
7 7u83 2194
	/* Allow for overload resolution */
2 7u83 2195
#if LANGUAGE_CPP
7 7u83 2196
	if (IS_TYPE_CLASS(cb) || IS_TYPE_CLASS(cc)) {
2197
		if (overload_depth == 0) {
2198
			e = binary_overload(lex_colon, b, c);
2199
			if (!IS_NULL_exp(e)) {
2200
				/* Fill in condition */
2201
				if (IS_exp_if_stmt(e)) {
2202
					COPY_exp(exp_if_stmt_cond(e), a);
2203
				} else if (IS_exp_opn(e)) {
2204
					LIST(EXP) p =
2205
					    DEREF_list(exp_opn_args(e));
2206
					COPY_exp(HEAD_list(p), a);
2207
				}
2208
			}
2209
			return (e);
2 7u83 2210
		}
2211
	}
2212
#endif
2213
 
7 7u83 2214
	/* Check operands */
2215
	b = convert_none(b);
2216
	c = convert_none(c);
2 7u83 2217
 
7 7u83 2218
	/* Can have lvalues of the same type ... */
2219
	if (IS_TYPE_LVALUE(cb)) {
2220
		if (cb == cc && !option(OPT_cond_lvalue)) {
2221
			/* Operands are lvalues of the same category */
2222
			if (eq_type_unqual(tb, tc)) {
2223
				CV_SPEC qb = find_cv_qual(tb);
2224
				CV_SPEC qc = find_cv_qual(tc);
2225
				t = tb;
2226
				if (qb != qc) {
2227
					/* Adjust qualifiers */
2228
					report(crt_loc,
2229
					       ERR_expr_cond_qual(tb, tc));
2230
					t = qualify_type(t,(qb | qc), 0);
2231
				}
2232
				goto return_lab;
2233
			}
2 7u83 2234
		}
2235
	}
2236
 
7 7u83 2237
	/* ...otherwise do lvalue conversion ... */
2238
	if (IS_TYPE_ADDRESS(cb)) {
2239
		b = convert_lvalue(b);
2240
		tb = DEREF_type(exp_type(b));
2241
		cb = type_category(&tb);
2242
	}
2243
	if (IS_TYPE_ADDRESS(cc)) {
2244
		c = convert_lvalue(c);
2245
		tc = DEREF_type(exp_type(c));
2246
		cc = type_category(&tc);
2247
	}
2 7u83 2248
 
7 7u83 2249
	/* ... can have rvalues of the same type ... */
2250
	if (cb == cc) {
2 7u83 2251
#if LANGUAGE_C
7 7u83 2252
		if (IS_TYPE_SCALAR(cb)) {
2253
			/* Normal arithmetic conversions are applied in C */
2254
			/* EMPTY */
2255
		} else /* continues ... */
2 7u83 2256
#endif
7 7u83 2257
			if (eq_type_unqual(tb, tc)) {
2258
				CV_SPEC qb = find_cv_qual(tb);
2259
				CV_SPEC qc = find_cv_qual(tc);
2260
				t = tb;
2261
				if (qb != qc) {
2262
					/* Adjust qualifiers */
2263
					report(crt_loc,
2264
					       ERR_expr_cond_qual(tb, tc));
2265
					t = qualify_type(t,(qb | qc), 0);
2266
				}
2267
				if (IS_exp_int_lit(b) && IS_exp_int_lit(c)) {
2268
					if (!IS_NULL_exp(a) &&
2269
					    IS_exp_int_lit(a)) {
2270
						/* Allow for integral constants */
2271
						e = make_cond_nat(a, b, c);
2272
						return (e);
2273
					}
2274
				}
2275
				goto return_lab;
2276
			}
2277
		if (IS_TYPE_VOID(cb)) {
2278
			/* ... or 'void' and 'bottom' ... */
2279
			t = type_void;
2280
			goto return_lab;
2 7u83 2281
		}
2282
	}
2283
 
7 7u83 2284
	/* ... or both operands can be arithmetic ... */
2285
	if (IS_TYPE_ARITH(cb) && IS_TYPE_ARITH(cc)) {
2286
		t = arith_type(tb, tc, b, c);
2287
		b = convert_arith(t, b, op, 2);
2288
		c = convert_arith(t, c, op, 3);
2289
		if (IS_exp_int_lit(b) && IS_exp_int_lit(c)) {
2290
			if (!IS_NULL_exp(a) && IS_exp_int_lit(a)) {
2291
				/* Allow for integral constants */
2292
				e = make_cond_nat(a, b, c);
2293
				return (e);
2294
			}
2295
		}
2296
		goto return_lab;
2 7u83 2297
	}
2298
 
7 7u83 2299
	/* ... or both pointers ... */
2300
	if (IS_TYPE_PTR(cb)) {
2301
		if (IS_TYPE_PTR(cc)) {
2302
			int suspect = 0;
2303
			t = ptr_common_type(tb, tc, 1, &suspect);
2 7u83 2304
#if LANGUAGE_CPP
7 7u83 2305
			if (suspect == -1) {
2306
				/* Allow for template types */
2307
				e = make_templ_cond(a, b, c);
2308
				return (e);
2309
			}
2 7u83 2310
#endif
7 7u83 2311
			if (suspect == 2) {
2312
				ERROR err = ERR_basic_link_incompat(tb, tc);
2313
				err = concat_error(err, ERR_conv_ptr_common());
2314
				err = concat_error(err, ERR_expr_cond_ptr());
2315
				report(crt_loc, err);
2316
			}
2317
			b = convert_ptr_common(t, b, op, 2);
2318
			c = convert_ptr_common(t, c, op, 3);
2319
			goto return_lab;
2320
		}
2321
		if (IS_TYPE_INT(cc)) {
2322
			/* Allow zero integer as a null pointer */
2323
			t = tb;
2324
			c = make_null_ptr(c, t);
2325
			if (IS_NULL_exp(c)) {
2326
				report(crt_loc, ERR_expr_cond_nonzero(tb, tc));
2327
				c = make_null_ptr(NULL_exp, t);
2328
			}
2329
			goto return_lab;
2330
		}
2331
	} else if (IS_TYPE_PTR(cc)) {
2332
		if (IS_TYPE_INT(cb)) {
2333
			/* Allow zero integer as a null pointer */
2334
			t = tc;
2335
			b = make_null_ptr(b, t);
2336
			if (IS_NULL_exp(b)) {
2337
				report(crt_loc, ERR_expr_cond_nonzero(tc, tb));
2338
				b = make_null_ptr(NULL_exp, t);
2339
			}
2340
			goto return_lab;
2341
		}
2 7u83 2342
	}
2343
 
2344
#if LANGUAGE_CPP
7 7u83 2345
	/* ... or both pointers to members ... */
2346
	if (IS_TYPE_PTR_MEM(cb)) {
2347
		if (IS_TYPE_PTR_MEM(cc)) {
2348
			int suspect = 0;
2349
			t = ptr_mem_common_type(tb, tc, &suspect);
2350
			if (suspect == -1) {
2351
				/* Allow for template types */
2352
				e = make_templ_cond(a, b, c);
2353
				return (e);
2354
			}
2355
			if (suspect == 2 || suspect == 1) {
2356
				ERROR err = ERR_basic_link_incompat(tb, tc);
2357
				err = concat_error(err, ERR_conv_mem_common());
2358
				err = concat_error(err, ERR_expr_cond_mptr());
2359
				report(crt_loc, err);
2360
			} else {
2361
				b = convert_ptr_mem_common(t, b, op, 2);
2362
				c = convert_ptr_mem_common(t, c, op, 3);
2363
			}
2364
			goto return_lab;
2365
		}
2366
		if (IS_TYPE_INT(cc)) {
2367
			/* Allow zero integer as a null pointer member */
2368
			t = tb;
2369
			c = make_null_ptr(c, t);
2370
			if (IS_NULL_exp(c)) {
2371
				report(crt_loc, ERR_expr_cond_nonzero(tb, tc));
2372
				c = make_null_ptr(NULL_exp, t);
2373
			}
2374
			goto return_lab;
2375
		}
2376
	} else if (IS_TYPE_PTR_MEM(cc)) {
2377
		if (IS_TYPE_INT(cb)) {
2378
			/* Allow zero integer as a null pointer member */
2379
			t = tc;
2380
			b = make_null_ptr(b, t);
2381
			if (IS_NULL_exp(b)) {
2382
				report(crt_loc, ERR_expr_cond_nonzero(tc, tb));
2383
				b = make_null_ptr(NULL_exp, t);
2384
			}
2385
			goto return_lab;
2386
		}
2 7u83 2387
	}
7 7u83 2388
#endif
2389
 
2390
	/* ... and nothing else */
2391
	if (!IS_TYPE_ERROR(cb) && !IS_TYPE_ERROR(cc)) {
2392
		report(crt_loc, ERR_expr_cond_op(tb, tc));
2 7u83 2393
	}
7 7u83 2394
	if (IS_TYPE_VOID(cb)) {
2395
		c = make_discard_exp(c);
2396
		t = tb;
2397
		goto return_lab;
2 7u83 2398
	}
7 7u83 2399
	if (IS_TYPE_VOID(cc)) {
2400
		b = make_discard_exp(b);
2401
		t = tc;
2402
		goto return_lab;
2403
	}
2404
	e = make_error_exp(0);
2405
	return (e);
2 7u83 2406
 
7 7u83 2407
	/* Construct the result */
2408
return_lab:
2409
	MAKE_exp_if_stmt(t, a, b, c, NULL_id, e);
2410
	return (e);
2 7u83 2411
}
2412
 
2413
 
2414
/*
2415
    JOIN TWO EXPRESSIONS
2416
 
2417
    This routine joins the expressions a and b by forming a comma
2418
    expression, 'a, b'.
2419
*/
2420
 
7 7u83 2421
EXP
2422
join_exp(EXP a, EXP b)
2 7u83 2423
{
7 7u83 2424
	EXP e;
2425
	TYPE t;
2426
	LIST(EXP)p;
2427
	if (IS_NULL_exp(a)) {
2428
		return (b);
2429
	}
2430
	if (IS_NULL_exp(b)) {
2431
		return (a);
2432
	}
2433
	CONS_exp(b, NULL_list(EXP), p);
2434
	CONS_exp(a, p, p);
2435
	t = DEREF_type(exp_type(b));
2436
	MAKE_exp_comma(t, p, e);
2437
	return (e);
2 7u83 2438
}
2439
 
2440
 
2441
/*
2442
    CONSTRUCT A SIMPLE COMMA EXPRESSION
2443
 
2444
    This routine constructs the simple comma expression 'a, b'.  If started
2445
    is true and a is itself a comma expression then b is added to the end
2446
    of a.  Otherwise a new comma expression is created.  Note that discard
2447
    analysis is applied to a, and unreached code analysis to b.
2448
*/
2449
 
7 7u83 2450
static EXP
2451
make_comma_simple(EXP a, EXP b, int started)
2 7u83 2452
{
7 7u83 2453
	EXP e;
2454
	int uc;
2455
	TYPE ta, tb;
2 7u83 2456
#if LANGUAGE_CPP
7 7u83 2457
	unsigned ca, cb;
2 7u83 2458
#endif
2459
 
7 7u83 2460
	/* Do reference conversions */
2461
	a = convert_reference(a, REF_NORMAL);
2462
	b = convert_reference(b, REF_NORMAL);
2 7u83 2463
 
7 7u83 2464
	/* Find operand types */
2 7u83 2465
#if LANGUAGE_CPP
7 7u83 2466
	ta = DEREF_type(exp_type(a));
2467
	tb = DEREF_type(exp_type(b));
2468
	ca = type_category(&ta);
2469
	cb = type_category(&tb);
2 7u83 2470
 
7 7u83 2471
	/* Check for overloading */
2472
	if (IS_TYPE_OVERLOAD(ca) || IS_TYPE_OVERLOAD(cb)) {
2473
		if (overload_depth == 0) {
2474
			e = binary_overload(lex_comma, a, b);
2475
			if (!IS_NULL_exp(e)) {
2476
				return (e);
2477
			}
2478
		}
2479
		/* Continue if not overloaded */
2 7u83 2480
	}
2481
#endif
2482
 
7 7u83 2483
	/* Do discard analysis on first operand */
2484
	uc = unreached_code;
2485
	a = make_exp_stmt(a);
2486
	ta = DEREF_type(exp_type(a));
2487
	if (IS_type_bottom(ta)) {
2488
		if (!unreached_last) {
2489
			/* Report unreached code */
2490
			report(crt_loc, ERR_stmt_stmt_unreach());
2491
			unreached_last = 1;
2492
		}
2493
	}
2494
	unreached_code = uc;
2 7u83 2495
 
7 7u83 2496
	/* Check second operand */
2 7u83 2497
#if LANGUAGE_C
7 7u83 2498
	b = convert_lvalue(b);
2 7u83 2499
#endif
7 7u83 2500
	b = convert_none(b);
2501
	tb = DEREF_type(exp_type(b));
2 7u83 2502
 
7 7u83 2503
	/* Construct the result */
2504
	if (started && IS_exp_comma(a)) {
2505
		LIST(EXP)q;
2506
		LIST(EXP)p = DEREF_list(exp_comma_args(a));
2507
		CONS_exp(b, NULL_list(EXP), q);
2508
		p = APPEND_list(p, q);
2509
		COPY_list(exp_comma_args(a), p);
2510
		COPY_type(exp_type(a), tb);
2511
		e = a;
2512
	} else {
2513
		LIST(EXP)p;
2514
		CONS_exp(b, NULL_list(EXP), p);
2515
		CONS_exp(a, p, p);
2516
		MAKE_exp_comma(tb, p, e);
2517
	}
2518
	return (e);
2 7u83 2519
}
2520
 
2521
 
2522
/*
2523
    CONSTRUCT A COMMA EXPRESSION
2524
 
2525
    This routine constructs the n-ary comma expression 'p1, p2, ..., pn' for
2526
    the expression list p = ( p1, p2, ..., pn ).  Note that this groups from
2527
    left to right as '( ( ... ( p1, p2 ), ... ), pn )'.
2528
*/
2529
 
7 7u83 2530
EXP
2531
make_comma_exp(LIST(EXP)p)
2 7u83 2532
{
7 7u83 2533
	EXP e;
2534
	if (IS_NULL_list(p)) {
2535
		/* This shouldn't happen */
2536
		e = make_error_exp(LANGUAGE_CPP);
2537
	} else {
2538
		int started = 0;
2539
		DESTROY_CONS_exp(destroy, e, p, p);
2540
		while (!IS_NULL_list(p)) {
2541
			EXP a;
2542
			DESTROY_CONS_exp(destroy, a, p, p);
2543
			e = make_comma_simple(e, a, started);
2544
			started = 1;
2545
		}
2546
		if (!started) {
2547
			/* This shouldn't happen */
2548
			e = convert_reference(e, REF_NORMAL);
2549
		}
2 7u83 2550
	}
7 7u83 2551
	return (e);
2 7u83 2552
}