Subversion Repositories tendra.SVN

Rev

Rev 2 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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