Subversion Repositories tendra.SVN

Rev

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

Rev Author Line No. Line
2 7u83 1
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
#include "config.h"
32
#include "c_types.h"
33
#include "ctype_ops.h"
34
#include "etype_ops.h"
35
#include "exp_ops.h"
36
#include "hashid_ops.h"
37
#include "id_ops.h"
38
#include "member_ops.h"
39
#include "nat_ops.h"
40
#include "type_ops.h"
41
#include "error.h"
42
#include "catalog.h"
43
#include "allocate.h"
44
#include "assign.h"
45
#include "basetype.h"
46
#include "cast.h"
47
#include "chktype.h"
48
#include "class.h"
49
#include "constant.h"
50
#include "construct.h"
51
#include "convert.h"
52
#include "copy.h"
53
#include "derive.h"
54
#include "dump.h"
55
#include "exception.h"
56
#include "expression.h"
57
#include "function.h"
58
#include "hash.h"
59
#include "identifier.h"
60
#include "initialise.h"
61
#include "instance.h"
62
#include "inttype.h"
63
#include "member.h"
64
#include "namespace.h"
65
#include "operator.h"
66
#include "overload.h"
67
#include "quality.h"
68
#include "statement.h"
69
#include "syntax.h"
70
#include "template.h"
71
#include "typeid.h"
72
 
73
 
74
/*
75
    FIND AN EXPRESSION RELATION
76
 
77
    This routine returns the lexical token number of associated with
78
    the test ntst.  The default value returned is lex.
79
*/
80
 
81
int ntest_token
82
    PROTO_N ( ( ntst, lex ) )
83
    PROTO_T ( NTEST ntst X int lex )
84
{
85
    switch ( ntst ) {
86
	case ntest_eq : lex = lex_eq ; break ;
87
	case ntest_not_eq : lex = lex_not_Heq_H1 ; break ;
88
	case ntest_less : lex = lex_less ; break ;
89
	case ntest_less_eq : lex = lex_less_Heq ; break ;
90
	case ntest_greater : lex = lex_greater ; break ;
91
	case ntest_greater_eq : lex = lex_greater_Heq ; break ;
92
    }
93
    return ( lex ) ;
94
}
95
 
96
 
97
/*
98
    FIND THE TOKEN NUMBER FOR AN OPERATION
99
 
100
    This routine returns the lexical token number associated with the
101
    expression e.  The default value returned is lex.
102
*/
103
 
104
int op_token
105
    PROTO_N ( ( e, lex ) )
106
    PROTO_T ( EXP e X int lex )
107
{
108
    if ( !IS_NULL_exp ( e ) ) {
109
	switch ( TAG_exp ( e ) ) {
110
	    case exp_negate_tag : lex = lex_minus ; break ;
111
	    case exp_compl_tag : lex = lex_compl_H1 ; break ;
112
	    case exp_not_tag : lex = lex_not_H1 ; break ;
113
	    case exp_abs_tag : lex = lex_abs ; break ;
114
	    case exp_plus_tag : lex = lex_plus ; break ;
115
	    case exp_minus_tag : lex = lex_minus ; break ;
116
	    case exp_mult_tag : lex = lex_star ; break ;
117
	    case exp_div_tag : lex = lex_div ; break ;
118
	    case exp_rem_tag : lex = lex_rem ; break ;
119
	    case exp_and_tag : lex = lex_and_H1 ; break ;
120
	    case exp_or_tag : lex = lex_or_H1 ; break ;
121
	    case exp_xor_tag : lex = lex_xor_H1 ; break ;
122
	    case exp_log_and_tag : lex = lex_logical_Hand_H1 ; break ;
123
	    case exp_log_or_tag : lex = lex_logical_Hor_H1 ; break ;
124
	    case exp_lshift_tag : lex = lex_lshift ; break ;
125
	    case exp_rshift_tag : lex = lex_rshift ; break ;
126
	    case exp_max_tag : lex = lex_max ; break ;
127
	    case exp_min_tag : lex = lex_min ; break ;
128
	    case exp_test_tag : {
129
		NTEST ntst = DEREF_ntest ( exp_test_tst ( e ) ) ;
130
		lex = ntest_token ( ntst, lex ) ;
131
		break ;
132
	    }
133
	    case exp_compare_tag : {
134
		NTEST ntst = DEREF_ntest ( exp_compare_tst ( e ) ) ;
135
		lex = ntest_token ( ntst, lex ) ;
136
		break ;
137
	    }
138
	    case exp_paren_tag : {
139
		EXP a = DEREF_exp ( exp_paren_arg ( e ) ) ;
140
		lex = op_token ( a, lex ) ;
141
		break ;
142
	    }
143
	    case exp_copy_tag : {
144
		EXP a = DEREF_exp ( exp_copy_arg ( e ) ) ;
145
		lex = op_token ( a, lex ) ;
146
		break ;
147
	    }
148
	}
149
    }
150
    return ( lex ) ;
151
}
152
 
153
 
154
/*
155
    APPLY A UNARY OPERATOR
156
 
157
    This routine applies the unary operator op to the argument a.  The
158
    type t2 gives the destination for any cast operator.
159
*/
160
 
161
EXP apply_unary
162
    PROTO_N ( ( op, a, t1, t2, cpy ) )
163
    PROTO_T ( int op X EXP a X TYPE t1 X TYPE t2 X int cpy )
164
{
165
    EXP e ;
166
    if ( cpy ) {
167
	/* Copy argument if necessary */
168
	switch ( op ) {
169
	    case lex_function : {
170
		a = copy_func_exp ( a, t1, t2 ) ;
171
		break ;
172
	    }
173
	    case lex_sizeof :
174
	    case lex_alignof :
175
	    case lex_typeid :
176
	    case lex_vtable : {
177
		suppress_usage++ ;
178
		a = copy_exp ( a, t1, t2 ) ;
179
		suppress_usage-- ;
180
		break ;
181
	    }
182
	    default : {
183
		a = copy_exp ( a, t1, t2 ) ;
184
		break ;
185
	    }
186
	}
187
    }
188
    suppress_quality++ ;
189
    switch ( op ) {
190
	case lex_and_H1 : {
191
	    /* Construct '&a' */
192
	    e = make_ref_exp ( a, 0 ) ;
193
	    break ;
194
	}
195
	case lex_abs :
196
	case lex_plus :
197
	case lex_minus :
198
	case lex_compl_H1 : {
199
	    /* Construct '+a', '-a', '~a' and 'abs a' */
200
	    e = make_uminus_exp ( op, a ) ;
201
	    break ;
202
	}
203
	case lex_not_H1 : {
204
	    /* Construct '!a' */
205
	    e = make_not_exp ( a ) ;
206
	    break ;
207
	}
208
	case lex_plus_Hplus :
209
	case lex_minus_Hminus : {
210
	    /* Construct '++a' and '--a' */
211
	    e = make_prefix_exp ( op, a ) ;
212
	    break ;
213
	}
214
	case lex_star : {
215
	    /* Construct '*a' */
216
	    e = make_indir_exp ( a ) ;
217
	    break ;
218
	}
219
	case lex_pointer : {
220
	    /* Construct null pointer constant */
221
	    e = make_null_ptr ( a, t2 ) ;
222
	    if ( IS_NULL_exp ( e ) ) {
223
		/* Force conversion error */
224
		e = apply_unary ( lex_implicit, a, t1, t2, 0 ) ;
225
	    }
226
	    break ;
227
	}
228
	case lex_sizeof :
229
	case lex_alignof : {
230
	    /* Construct 'sizeof ( a )' */
231
	    TYPE s ;
232
	    EXP b = a ;
233
	    suppress_usage++ ;
234
	    s = typeof_exp ( &b, 0, op ) ;
235
	    e = make_sizeof_exp ( s, b, 0, op ) ;
236
	    suppress_usage-- ;
237
	    break ;
238
	}
239
	case lex_function : {
240
	    /* Dummy function resolution operator */
241
	    ERROR err = NULL_err ;
242
	    LIST ( IDENTIFIER ) pids = NULL_list ( IDENTIFIER ) ;
243
	    a = resolve_cast ( t2, a, &err, 1, 1, pids ) ;
244
	    e = cast_exp ( t2, a, &err, CAST_IMPLICIT ) ;
245
	    if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
246
	    break ;
247
	}
248
	case lex_implicit : {
249
	    /* Dummy implicit cast operator */
250
	    ERROR err = NULL_err ;
251
	    if ( IS_exp_initialiser ( a ) ) {
252
		LIST ( EXP ) args ;
253
		args = DEREF_list ( exp_initialiser_args ( a ) ) ;
254
		e = init_constr ( t2, args, &err ) ;
255
	    } else {
256
		a = convert_reference ( a, REF_ASSIGN ) ;
257
		e = cast_exp ( t2, a, &err, CAST_IMPLICIT ) ;
258
	    }
259
	    if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
260
	    break ;
261
	}
262
	case lex_cast : {
263
	    /* Construct '( t2 ) a' */
264
	    if ( IS_NULL_exp ( a ) ) {
265
		ERROR err = NULL_err ;
266
		e = init_empty ( t2, cv_none, 1, &err ) ;
267
		if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
268
	    } else {
269
		e = make_cast_exp ( t2, a, 0 ) ;
270
	    }
271
	    break ;
272
	}
273
	case lex_if :
274
	case lex_do :
275
	case lex_for :
276
	case lex_while :
277
	case lex_cond_Hop : {
278
	    /* Dummy conditional operators */
279
	    EXP b = NULL_exp ;
280
	    e = check_cond ( a, &b, op ) ;
281
	    break ;
282
	}
283
	case lex_switch : {
284
	    /* Dummy control operator */
285
	    EXP b = NULL_exp ;
286
	    EXP c = NULL_exp ;
287
	    e = check_control ( a, &b, &c ) ;
288
	    break ;
289
	}
290
	case lex_fall :
291
	case lex_return : {
292
	    /* Dummy return operator */
293
	    IDENTIFIER lab = NULL_id ;
294
	    e = find_return_exp ( a, &lab, op ) ;
295
	    break ;
296
	}
297
	case lex_discard : {
298
	    /* Dummy expression statement operator */
299
	    e = make_exp_stmt ( a ) ;
300
	    break ;
301
	}
302
	case lex_void : {
303
	    /* Dummy discard operator */
304
	    e = make_discard_exp ( a ) ;
305
	    break ;
306
	}
307
	case lex_question : {
308
	    /* Dummy identity operator */
309
	    e = a ;
310
	    break ;
311
	}
312
#if LANGUAGE_CPP
313
	case lex_delete : {
314
	    /* Construct 'delete a' */
315
	    e = make_delete_exp ( lex_delete, 0, a ) ;
316
	    break ;
317
	}
318
	case lex_delete_Hfull : {
319
	    /* Construct '::delete a' */
320
	    e = make_delete_exp ( lex_delete, 1, a ) ;
321
	    break ;
322
	}
323
	case lex_delete_Harray : {
324
	    /* Construct 'delete [] a' */
325
	    e = make_delete_exp ( lex_delete_Harray, 0, a ) ;
326
	    break ;
327
	}
328
	case lex_delete_Harray_Hfull : {
329
	    /* Construct '::delete [] a' */
330
	    e = make_delete_exp ( lex_delete_Harray, 1, a ) ;
331
	    break ;
332
	}
333
	case lex_compute : {
334
	    /* Construct empty new-initialiser */
335
	    e = make_new_init ( t2, NULL_list ( EXP ), 0 ) ;
336
	    break ;
337
	}
338
	case lex_typeid :
339
	case lex_vtable : {
340
	    /* Construct 'typeid ( a )' */
341
	    suppress_usage++ ;
342
	    e = make_typeid_exp ( op, a, 0 ) ;
343
	    suppress_usage-- ;
344
	    break ;
345
	}
346
	case lex_static_Hcast :
347
	case lex_reinterpret_Hcast :
348
	case lex_const_Hcast :
349
	case lex_dynamic_Hcast : {
350
	    /* Construct 'op < t2 > ( a )' */
351
	    e = make_new_cast_exp ( op, t2, a, 0 ) ;
352
	    break ;
353
	}
354
#endif
355
	default : {
356
	    /* Illegal operations */
357
	    FAIL ( Unexpected unary operation ) ;
358
	    e = make_error_exp ( 0 ) ;
359
	    break ;
360
	}
361
    }
362
    suppress_quality-- ;
363
    return ( e ) ;
364
}
365
 
366
 
367
/*
368
    APPLY A BINARY OPERATOR
369
 
370
    This routine applies the binary operator op to the arguments a and b.
371
*/
372
 
373
EXP apply_binary
374
    PROTO_N ( ( op, a, b, t1, t2, cpy ) )
375
    PROTO_T ( int op X EXP a X EXP b X TYPE t1 X TYPE t2 X int cpy )
376
{
377
    EXP e ;
378
    if ( cpy ) {
379
	/* Copy arguments if necessary */
380
	a = copy_exp ( a, t1, t2 ) ;
381
	b = copy_exp ( b, t1, t2 ) ;
382
    }
383
    suppress_quality++ ;
384
    switch ( op ) {
385
	case lex_and_H1 : {
386
	    /* Construct 'a & b' */
387
	    e = make_and_exp ( a, b ) ;
388
	    break ;
389
	}
390
	case lex_and_Heq_H1 :
391
	case lex_div_Heq :
392
	case lex_lshift_Heq :
393
	case lex_minus_Heq :
394
	case lex_or_Heq_H1 :
395
	case lex_plus_Heq :
396
	case lex_rem_Heq :
397
	case lex_rshift_Heq :
398
	case lex_star_Heq :
399
	case lex_xor_Heq_H1 : {
400
	    /* Construct 'a &= b', 'a /= b' etc. */
401
	    e = make_become_exp ( op, a, b ) ;
402
	    break ;
403
	}
404
	case lex_array_Hop : {
405
	    /* Construct 'a [b]' */
406
	    e = make_index_exp ( a, b ) ;
407
	    break ;
408
	}
409
	case lex_arrow :
410
	case lex_dot : {
411
	    /* Construct 'a->b' and 'a.b' */
412
	    e = make_field_exp ( op, a, b ) ;
413
	    break ;
414
	}
415
	case lex_assign : {
416
	    /* Construct 'a = b' */
417
	    e = make_assign_exp ( a, b, LANGUAGE_C ) ;
418
	    break ;
419
	}
420
	case lex_comma : {
421
	    /* Construct 'a, b' */
422
	    LIST ( EXP ) p = NULL_list ( EXP ) ;
423
	    CONS_exp ( b, p, p ) ;
424
	    CONS_exp ( a, p, p ) ;
425
	    e = make_comma_exp ( p ) ;
426
	    break ;
427
	}
428
	case lex_eq :
429
	case lex_not_Heq_H1 : {
430
	    /* Construct 'a == b' and 'a != b' */
431
	    e = make_equality_exp ( op, a, b ) ;
432
	    break ;
433
	}
434
	case lex_greater :
435
	case lex_greater_Heq :
436
	case lex_less :
437
	case lex_less_Heq : {
438
	    /* Construct 'a > b', 'a >= b', 'a < b' and 'a <= b' */
439
	    e = make_relation_exp ( op, a, b ) ;
440
	    break ;
441
	}
442
	case lex_logical_Hand_H1 : {
443
	    /* Construct 'a && b' */
444
	    e = make_log_and_exp ( a, b ) ;
445
	    break ;
446
	}
447
	case lex_logical_Hor_H1 : {
448
	    /* Construct 'a || b' */
449
	    e = make_log_or_exp ( a, b ) ;
450
	    break ;
451
	}
452
	case lex_lshift :
453
	case lex_rshift : {
454
	    /* Construct 'a << b' and 'a >> b' */
455
	    e = make_shift_exp ( op, a, b ) ;
456
	    break ;
457
	}
458
	case lex_minus : {
459
	    /* Construct 'a - b' */
460
	    e = make_minus_exp ( a, b ) ;
461
	    break ;
462
	}
463
	case lex_plus : {
464
	    /* Construct 'a + b' */
465
	    e = make_plus_exp ( a, b ) ;
466
	    break ;
467
	}
468
	case lex_plus_Hplus :
469
	case lex_minus_Hminus : {
470
	    /* Construct 'a++' and 'a--' */
471
	    e = make_postfix_exp ( op, a ) ;
472
	    break ;
473
	}
474
	case lex_or_H1 : {
475
	    /* Construct 'a | b' */
476
	    e = make_or_exp ( a, b ) ;
477
	    break ;
478
	}
479
	case lex_rem : {
480
	    /* Construct 'a % b' */
481
	    e = make_rem_exp ( a, b ) ;
482
	    break ;
483
	}
484
	case lex_star :
485
	case lex_div :
486
	case lex_max :
487
	case lex_min : {
488
	    /* Construct 'a * b' and 'a / b' */
489
	    e = make_mult_exp ( op, a, b ) ;
490
	    break ;
491
	}
492
	case lex_xor_H1 : {
493
	    /* Construct 'a ^ b' */
494
	    e = make_xor_exp ( a, b ) ;
495
	    break ;
496
	}
497
	case lex_colon : {
498
	    /* Construct 'NULL_exp ? a : b' */
499
	    e = make_cond_exp ( NULL_exp, a, b ) ;
500
	    break ;
501
	}
502
#if LANGUAGE_CPP
503
	case lex_arrow_Hstar :
504
	case lex_dot_Hstar : {
505
	    /* Construct 'a->*b' and 'a.*b' */
506
	    e = make_member_exp ( op, a, b ) ;
507
	    break ;
508
	}
509
#endif
510
	default : {
511
	    /* Illegal operations */
512
	    FAIL ( Unexpected binary operation ) ;
513
	    e = make_error_exp ( 0 ) ;
514
	    break ;
515
	}
516
    }
517
    suppress_quality-- ;
518
    return ( e ) ;
519
}
520
 
521
 
522
/*
523
    APPLY AN N-ARY OPERATOR
524
 
525
    This routine applies the n-ary operator op to the arguments p.  The
526
    type t2 gives the destination for any cast operator.
527
*/
528
 
529
EXP apply_nary
530
    PROTO_N ( ( op, p, t1, t2, cpy ) )
531
    PROTO_T ( int op X LIST ( EXP ) p X TYPE t1 X TYPE t2 X int cpy )
532
{
533
    EXP e ;
534
    suppress_quality++ ;
535
    switch ( op ) {
536
	case lex_comma : {
537
	    /* Construct 'p0, p1, ..., pn' */
538
	    if ( cpy ) p = copy_exp_list ( p, t1, t2 ) ;
539
	    e = make_comma_exp ( p ) ;
540
	    break ;
541
	}
542
	case lex_cond_Hop : {
543
	    /* Construct 'p0 ? p1 : p2' */
544
	    EXP c, a, b ;
545
	    c = DEREF_exp ( HEAD_list ( p ) ) ;
546
	    p = TAIL_list ( p ) ;
547
	    a = DEREF_exp ( HEAD_list ( p ) ) ;
548
	    p = TAIL_list ( p ) ;
549
	    b = DEREF_exp ( HEAD_list ( p ) ) ;
550
	    if ( cpy ) {
551
		c = copy_exp ( c, type_bool, type_bool ) ;
552
		a = copy_exp ( a, t1, t2 ) ;
553
		b = copy_exp ( b, t1, t2 ) ;
554
	    }
555
	    e = make_cond_exp ( c, a, b ) ;
556
	    break ;
557
	}
558
	case lex_func_Hop : {
559
	    /* Construct 'p0 ( p1, ..., pn )' */
560
	    EXP a = DEREF_exp ( HEAD_list ( p ) ) ;
561
	    p = TAIL_list ( p ) ;
562
	    if ( cpy ) {
563
		a = copy_func_exp ( a, t1, t2 ) ;
564
		p = copy_exp_list ( p, t1, t2 ) ;
565
	    }
566
	    e = make_func_exp ( a, p, 1 ) ;
567
	    break ;
568
	}
569
	case lex_cast : {
570
	    /* Construct 't2 ( p0, ..., pn )' */
571
	    if ( cpy ) p = copy_exp_list ( p, t1, t2 ) ;
572
	    e = make_func_cast_exp ( t2, p ) ;
573
	    break ;
574
	}
575
	case lex_static_Hcast : {
576
	    /* Construct 't2 ( p0, ..., pn )' */
577
	    ERROR err = NULL_err ;
578
	    if ( cpy ) p = copy_exp_list ( p, t1, t2 ) ;
579
	    p = convert_args ( p ) ;
580
	    e = convert_constr ( t2, p, &err, CAST_STATIC ) ;
581
	    if ( !IS_NULL_err ( err ) ) report ( crt_loc, err ) ;
582
	    break ;
583
	}
584
#if LANGUAGE_CPP
585
	case lex_new :
586
	case lex_new_Hfull : {
587
	    /* Construct 'new ( p3, ..., pn ) ( t0 [ p1 ] ) ( p2 )' */
588
	    EXP a ;
589
	    TYPE s ;
590
	    int b = 0 ;
591
	    if ( op == lex_new_Hfull ) b = 1 ;
592
	    if ( cpy ) p = copy_exp_list ( p, t1, t2 ) ;
593
	    a = DEREF_exp ( HEAD_list ( p ) ) ;
594
	    p = TAIL_list ( p ) ;
595
	    s = DEREF_type ( exp_type ( a ) ) ;
596
	    a = DEREF_exp ( HEAD_list ( p ) ) ;
597
	    p = TAIL_list ( p ) ;
598
	    if ( !IS_NULL_exp ( a ) ) {
599
		/* Array dimension */
600
		NAT n ;
601
		MAKE_nat_calc ( a, n ) ;
602
		MAKE_type_array ( cv_none, s, n, s ) ;
603
	    }
604
	    a = DEREF_exp ( HEAD_list ( p ) ) ;
605
	    p = TAIL_list ( p ) ;
606
	    e = make_new_exp ( s, 0, b, p, a ) ;
607
	    break ;
608
	}
609
	case lex_compute : {
610
	    /* Construct new-initialiser, '( p0, ..., pn )' */
611
	    if ( cpy ) p = copy_exp_list ( p, t1, t2 ) ;
612
	    e = make_new_init ( t2, p, 1 ) ;
613
	    break ;
614
	}
615
#endif
616
	default : {
617
	    /* Illegal operations */
618
	    FAIL ( Unexpected nary operation ) ;
619
	    e = make_error_exp ( 0 ) ;
620
	    break ;
621
	}
622
    }
623
    suppress_quality-- ;
624
    return ( e ) ;
625
}
626
 
627
 
628
/*
629
    CONVERT AN OPERAND TO A BUILT-IN OPERATOR
630
 
631
    This routine converts the nth operand a to the built-in operator op
632
    to type t.
633
*/
634
 
635
static EXP convert_builtin
636
    PROTO_N ( ( t, a, op, n ) )
637
    PROTO_T ( TYPE t X EXP a X int op X unsigned n )
638
{
639
    ERROR err = NULL_err ;
640
    if ( IS_type_compound ( t ) ) {
641
	a = convert_class ( t, a, &err ) ;
642
    } else {
643
	a = init_assign ( t, cv_none, a, &err ) ;
644
    }
645
    if ( !IS_NULL_err ( err ) ) {
646
	err = init_error ( err, 0 ) ;
647
	err = concat_error ( err, ERR_expr_convert_op ( n, op ) ) ;
648
	report ( crt_loc, err ) ;
649
    }
650
    return ( a ) ;
651
}
652
 
653
 
654
/*
655
    APPLY A BUILT-IN OPERATOR
656
 
657
    This routine applies the built-in operator id to the arguments args.
658
*/
659
 
660
EXP apply_builtin
661
    PROTO_N ( ( id, args ) )
662
    PROTO_T ( IDENTIFIER id X LIST ( EXP ) args )
663
{
664
    EXP e ;
665
    TYPE t ;
666
    int op ;
667
    EXP a, b ;
668
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
669
    LIST ( TYPE ) ts = DEREF_list ( id_builtin_ptypes ( id ) ) ;
670
 
671
    /* Find operator */
672
    if ( IS_hashid_op ( nm ) ) {
673
	op = DEREF_int ( hashid_op_lex ( nm ) ) ;
674
    } else {
675
	op = lex_func_Hop ;
676
    }
677
 
678
    /* Shouldn't have no arguments */
679
    if ( IS_NULL_list ( args ) ) {
680
	e = make_error_exp ( 0 ) ;
681
	return ( e ) ;
682
    }
683
 
684
    /* Convert first argument */
685
    DESTROY_CONS_exp ( destroy, a, args, args ) ;
686
    t = DEREF_type ( HEAD_list ( ts ) ) ;
687
    a = convert_builtin ( t, a, op, ( unsigned ) 1 ) ;
688
 
689
    /* Allow for 'operator ()' */
690
    if ( op == lex_func_Hop ) {
691
	overload_depth++ ;
692
	e = make_func_exp ( a, args, 0 ) ;
693
	overload_depth-- ;
694
	return ( e ) ;
695
    }
696
 
697
    /* Allow for unary operators */
698
    if ( IS_NULL_list ( args ) ) {
699
	overload_depth++ ;
700
	e = apply_unary ( op, a, NULL_type, NULL_type, 0 ) ;
701
	overload_depth-- ;
702
	return ( e ) ;
703
    }
704
 
705
    /* Convert second argument */
706
    DESTROY_CONS_exp ( destroy, b, args, args ) ;
707
    t = DEREF_type ( HEAD_list ( TAIL_list ( ts ) ) ) ;
708
    b = convert_builtin ( t, b, op, ( unsigned ) 1 ) ;
709
 
710
    /* Allow for binary operators */
711
    if ( IS_NULL_list ( args ) ) {
712
	overload_depth++ ;
713
	e = apply_binary ( op, a, b, NULL_type, NULL_type, 0 ) ;
714
	overload_depth-- ;
715
	return ( e ) ;
716
    }
717
 
718
    /* Shouldn't have more than two arguments */
719
    DESTROY_list ( args, SIZE_exp ) ;
720
    e = make_error_exp ( 0 ) ;
721
    return ( e ) ;
722
}
723
 
724
 
725
/*
726
    OVERLOAD DEPTH FLAG
727
 
728
    This flag is used to keep track of the depth of overloaded operator
729
    resolutions.
730
*/
731
 
732
int overload_depth = 0 ;
733
int overload_warn = 1 ;
734
 
735
 
736
/*
737
    OPERATOR OVERLOADING ROUTINES
738
 
739
    The operator overloading routines are only included in the C++
740
    producer.
741
*/
742
 
743
#if LANGUAGE_CPP
744
 
745
 
746
/*
747
    CHECK AN OVERLOADED OPERATOR TYPE
748
 
749
    This routine checks the function type t for the overloaded operator id.
750
    The argument mem is true if id represents a non-static member function.
751
    Most operators have restrictions on the number and types of their
752
    parameters, others must be member functions.  Note that this routine
753
    only covers genuine overloading operators such as 'operator +', the
754
    allocation operators such as 'operator new' are handled by the routine
755
    check_allocator and alloc is set accordingly.
756
*/
757
 
758
TYPE check_operator
759
    PROTO_N ( ( t, id, mem, alloc ) )
760
    PROTO_T ( TYPE t X IDENTIFIER id X int mem X int *alloc )
761
{
762
    int op ;
763
    int ell ;
764
    HASHID nm ;
765
    int dargs = 0 ;
766
    unsigned n, m ;
767
    unsigned npars ;
768
    LIST ( TYPE ) ptypes ;
769
 
770
    /* Allow for template types */
771
    if ( IS_type_templ ( t ) ) {
772
	TYPE s = DEREF_type ( type_templ_defn ( t ) ) ;
773
	s = check_operator ( s, id, mem, alloc ) ;
774
	COPY_type ( type_templ_defn ( t ), s ) ;
775
	return ( t ) ;
776
    }
777
 
778
    /* Find the operator */
779
    nm = DEREF_hashid ( id_name ( id ) ) ;
780
    op = DEREF_int ( hashid_op_lex ( nm ) ) ;
781
    switch ( op ) {
782
	case lex_new :
783
	case lex_new_Harray : {
784
	    /* Check for allocation operators */
785
	    *alloc = 1 ;
786
	    t = check_allocator ( t, id, mem, 0 ) ;
787
	    return ( t ) ;
788
	}
789
	case lex_delete :
790
	case lex_delete_Harray : {
791
	    /* Check for deallocation operators */
792
	    *alloc = 2 ;
793
	    t = check_allocator ( t, id, mem, 0 ) ;
794
	    return ( t ) ;
795
	}
796
	default : {
797
	    *alloc = 0 ;
798
	    break ;
799
	}
800
    }
801
 
802
    /* Decompose function type */
803
    ptypes = DEREF_list ( type_func_ptypes ( t ) ) ;
804
    npars = LENGTH_list ( ptypes ) ;
805
    ell = DEREF_int ( type_func_ellipsis ( t ) ) ;
806
    if ( ell ) {
807
	/* Set number of parameters to dummy large value */
808
	npars = 10000 ;
809
    }
810
 
811
    /* Check function arguments */
812
    if ( mem ) {
813
	/* Member functions have implicit extra parameter */
814
	npars++ ;
815
	n = 0 ;
816
	m = 1 ;
817
    } else {
818
	/* Should have an overloadable parameter */
819
	LIST ( TYPE ) p = ptypes ;
820
	while ( !IS_NULL_list ( p ) ) {
821
	    TYPE a = DEREF_type ( HEAD_list ( p ) ) ;
822
	    unsigned ca = type_category ( &a ) ;
823
	    if ( IS_TYPE_OVERLOAD ( ca ) ) break ;
824
	    p = TAIL_list ( p ) ;
825
	}
826
	if ( IS_NULL_list ( p ) ) {
827
	    report ( crt_loc, ERR_over_oper_type ( nm ) ) ;
828
	}
829
	n = 1 ;
830
	m = 2 ;
831
    }
832
 
833
    /* Check number of arguments */
834
    switch ( op ) {
835
	case lex_compl_H1 :
836
	case lex_not_H1 :
837
	case lex_abs :
838
	case lex_alignof :
839
	case lex_sizeof :
840
	case lex_typeid :
841
	case lex_vtable : {
842
	    /* Unary operators */
843
	    if ( npars != 1 ) {
844
		ERROR err = ERR_over_unary_pars ( nm, n, n ) ;
845
		report ( crt_loc, err ) ;
846
	    }
847
	    break ;
848
	}
849
	case lex_plus :
850
	case lex_minus :
851
	case lex_star :
852
	case lex_and_H1 : {
853
	    /* Either unary or binary operators */
854
	    if ( npars != 1 && npars != 2 ) {
855
		ERROR err = ERR_over_binary_pars_p1 ( nm, n, m, m ) ;
856
		report ( crt_loc, err ) ;
857
	    }
858
	    break ;
859
	}
860
	default : {
861
	    /* Binary operators */
862
	    if ( npars != 2 ) {
863
		ERROR err = ERR_over_binary_pars_p2 ( nm, m, m ) ;
864
		report ( crt_loc, err ) ;
865
	    }
866
	    break ;
867
	}
868
	case lex_assign : {
869
	    /* Assignment */
870
	    if ( !mem ) report ( crt_loc, ERR_over_ass_mem ( nm ) ) ;
871
	    if ( npars != 2 ) {
872
		ERROR err = ERR_over_ass_pars ( nm, m, m ) ;
873
		report ( crt_loc, err ) ;
874
	    }
875
	    break ;
876
	}
877
	case lex_func_Hop : {
878
	    /* Function call (can have default arguments) */
879
	    if ( !mem ) report ( crt_loc, ERR_over_call_mem ( nm ) ) ;
880
	    dargs = 1 ;
881
	    break ;
882
	}
883
	case lex_array_Hop : {
884
	    /* Subscripting */
885
	    if ( !mem ) report ( crt_loc, ERR_over_sub_mem ( nm ) ) ;
886
	    if ( npars != 2 ) {
887
		ERROR err = ERR_over_sub_pars ( nm, m, m ) ;
888
		report ( crt_loc, err ) ;
889
	    }
890
	    break ;
891
	}
892
	case lex_arrow : {
893
	    /* Class member access */
894
	    if ( !mem ) report ( crt_loc, ERR_over_ref_mem ( nm ) ) ;
895
	    if ( npars != 1 ) {
896
		ERROR err = ERR_over_ref_pars ( nm, n, n ) ;
897
		report ( crt_loc, err ) ;
898
	    }
899
	    break ;
900
	}
901
	case lex_plus_Hplus :
902
	case lex_minus_Hminus : {
903
	    /* Increment and decrement */
904
	    if ( npars == 1 ) {
905
		/* One argument form is fine */
906
		/* EMPTY */
907
	    } else if ( npars == 2 ) {
908
		/* Two argument form needs checking */
909
		TYPE a ;
910
		TYPE b = type_sint ;
911
		LIST ( TYPE ) p = ptypes ;
912
		if ( !mem ) p = TAIL_list ( p ) ;
913
		a = DEREF_type ( HEAD_list ( p ) ) ;
914
		if ( !eq_type_unqual ( a, b ) && !is_templ_type ( a ) ) {
915
		    /* Second argument should be 'int' */
916
		    report ( crt_loc, ERR_over_inc_pars_p2 ( nm, b ) ) ;
917
		}
918
	    } else {
919
		/* Anything else is illegal */
920
		ERROR err = ERR_over_inc_pars ( nm, n, m, m ) ;
921
		report ( crt_loc, err ) ;
922
	    }
923
	    break ;
924
	}
925
	case lex_cond_Hop : {
926
	    /* Tertiary operators */
927
	    if ( npars != 3 ) {
928
		unsigned p = n + 2 ;
929
		ERROR err = ERR_over_binary_pars_p2 ( nm, p, p ) ;
930
		report ( crt_loc, err ) ;
931
	    }
932
	    break ;
933
	}
934
    }
935
 
936
    /* Check for default arguments */
937
    if ( !dargs && check_func_dargs ( t, 0, 0 ) ) {
938
	report ( crt_loc, ERR_over_oper_default ( nm ) ) ;
939
    }
940
    return ( t ) ;
941
}
942
 
943
 
944
/*
945
    LISTS OF BUILT-IN OPERATORS
946
 
947
    These values are used in the allocation and deallocation of built-in
948
    operators.
949
*/
950
 
951
static IDENTIFIER unary_free = NULL_id ;
952
static IDENTIFIER unary_all = NULL_id ;
953
static IDENTIFIER binary_free = NULL_id ;
954
static IDENTIFIER binary_all = NULL_id ;
955
static IDENTIFIER nary_free = NULL_id ;
956
static IDENTIFIER nary_all = NULL_id ;
957
 
958
 
959
/*
960
    CONSTRUCT A BUILT-IN UNARY OPERATOR
961
 
962
    This routine constructs the built-in unary operator 'r nm ( a )'.
963
*/
964
 
965
static IDENTIFIER unary_builtin
966
    PROTO_N ( ( nm, a, r ) )
967
    PROTO_T ( HASHID nm X TYPE a X TYPE r )
968
{
969
    LIST ( TYPE ) p ;
970
    IDENTIFIER id = unary_free ;
971
    if ( IS_NULL_id ( id ) ) {
972
	/* Allocate new identifier */
973
	NAMESPACE ns = NULL_nspace ;
974
	CONS_type ( a, NULL_list ( TYPE ), p ) ;
975
	MAKE_id_builtin ( nm, dspec_none, ns, builtin_loc, r, p, id ) ;
976
	COPY_id ( id_alias ( id ), unary_all ) ;
977
	unary_all = id ;
978
    } else {
979
	/* Use existing identifier */
980
	COPY_hashid ( id_name ( id ), nm ) ;
981
	COPY_ulong ( id_dump ( id ), LINK_NONE ) ;
982
	COPY_type ( id_builtin_ret ( id ), r ) ;
983
	p = DEREF_list ( id_builtin_ptypes ( id ) ) ;
984
	COPY_type ( HEAD_list ( p ), a ) ;
985
	unary_free = DEREF_id ( id_alias ( id ) ) ;
986
    }
987
    return ( id ) ;
988
}
989
 
990
 
991
/*
992
    CONSTRUCT A BUILT-IN BINARY OPERATOR
993
 
994
    This routine constructs the built-in binary operator 'r nm ( a, b )'.
995
*/
996
 
997
static IDENTIFIER binary_builtin
998
    PROTO_N ( ( nm, a, b, r ) )
999
    PROTO_T ( HASHID nm X TYPE a X TYPE b X TYPE r )
1000
{
1001
    LIST ( TYPE ) p ;
1002
    IDENTIFIER id = binary_free ;
1003
    if ( IS_NULL_id ( id ) ) {
1004
	/* Allocate new identifier */
1005
	NAMESPACE ns = NULL_nspace ;
1006
	CONS_type ( b, NULL_list ( TYPE ), p ) ;
1007
	CONS_type ( a, p, p ) ;
1008
	MAKE_id_builtin ( nm, dspec_none, ns, builtin_loc, r, p, id ) ;
1009
	COPY_id ( id_alias ( id ), binary_all ) ;
1010
	binary_all = id ;
1011
    } else {
1012
	/* Use existing identifier */
1013
	COPY_hashid ( id_name ( id ), nm ) ;
1014
	COPY_ulong ( id_dump ( id ), LINK_NONE ) ;
1015
	COPY_type ( id_builtin_ret ( id ), r ) ;
1016
	p = DEREF_list ( id_builtin_ptypes ( id ) ) ;
1017
	COPY_type ( HEAD_list ( p ), a ) ;
1018
	p = TAIL_list ( p ) ;
1019
	COPY_type ( HEAD_list ( p ), b ) ;
1020
	binary_free = DEREF_id ( id_alias ( id ) ) ;
1021
    }
1022
    return ( id ) ;
1023
}
1024
 
1025
 
1026
/*
1027
    CONSTRUCT A BUILT-IN N-ARY OPERATOR
1028
 
1029
    This routine constructs the built-in n-ary operator 'r nm ( a, p )'.
1030
*/
1031
 
1032
static IDENTIFIER nary_builtin
1033
    PROTO_N ( ( nm, a, p, r ) )
1034
    PROTO_T ( HASHID nm X TYPE a X LIST ( TYPE ) p X TYPE r )
1035
{
1036
    IDENTIFIER id = nary_free ;
1037
    if ( IS_NULL_id ( id ) ) {
1038
	/* Allocate new identifier */
1039
	NAMESPACE ns = NULL_nspace ;
1040
	CONS_type ( a, p, p ) ;
1041
	MAKE_id_builtin ( nm, dspec_none, ns, builtin_loc, r, p, id ) ;
1042
	COPY_id ( id_alias ( id ), nary_all ) ;
1043
	nary_all = id ;
1044
    } else {
1045
	/* Use existing identifier */
1046
	LIST ( TYPE ) q ;
1047
	COPY_hashid ( id_name ( id ), nm ) ;
1048
	COPY_ulong ( id_dump ( id ), LINK_NONE ) ;
1049
	COPY_type ( id_builtin_ret ( id ), r ) ;
1050
	q = DEREF_list ( id_builtin_ptypes ( id ) ) ;
1051
	COPY_type ( HEAD_list ( q ), a ) ;
1052
	COPY_list ( PTR_TAIL_list ( q ), p ) ;
1053
	nary_free = DEREF_id ( id_alias ( id ) ) ;
1054
    }
1055
    return ( id ) ;
1056
}
1057
 
1058
 
1059
/*
1060
    CONSTRUCT OVERLOADED OPERATION CANDIDATE LIST
1061
 
1062
    This routine constructs the candidate list for the overloaded n-ary
1063
    operation op, adding the result to p.  The candidates come from the
1064
    current namespace and the type t, if this is a class.  The routine
1065
    returns the hash table entry for 'operator op'.
1066
*/
1067
 
1068
static HASHID overload_candidates
1069
    PROTO_N ( ( p, op, t, s ) )
1070
    PROTO_T ( CANDIDATE_LIST *p X int op X TYPE t X TYPE s )
1071
{
1072
    /* Look up 'operator op' (non-standard) */
1073
    HASHID nm = lookup_op ( op ) ;
1074
    switch ( op ) {
1075
	case lex_assign :
1076
	case lex_func_Hop :
1077
	case lex_array_Hop :
1078
	case lex_arrow : {
1079
	    /* These can only be member functions */
1080
	    break ;
1081
	}
1082
	default : {
1083
	    /* These can be non-member functions */
1084
	    IDENTIFIER id = find_op_id ( nm ) ;
1085
	    if ( !IS_NULL_type ( s ) ) {
1086
		IGNORE koenig_candidates ( p, id, s, KIND_OP ) ;
1087
	    }
1088
	    IGNORE koenig_candidates ( p, id, t, KIND_OP ) ;
1089
	    if ( !IS_id_dummy ( id ) ) {
1090
		/* Function declared */
1091
		add_candidates ( p, id, 1, KIND_OP ) ;
1092
	    }
1093
	    break ;
1094
	}
1095
    }
1096
 
1097
    /* Look up 't::operator op' */
1098
    if ( IS_type_compound ( t ) ) {
1099
	CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
1100
	NAMESPACE ns = DEREF_nspace ( ctype_member ( ct ) ) ;
1101
	IDENTIFIER id = search_field ( ns, nm, 0, 0 ) ;
1102
	if ( !IS_NULL_id ( id ) ) {
1103
	    if ( IS_id_ambig ( id ) ) {
1104
		/* Ambiguous look-up */
1105
		IGNORE report_ambiguous ( id, 0, 1, 0 ) ;
1106
	    }
1107
	    add_candidates ( p, id, 1, KIND_MEM_OP ) ;
1108
	}
1109
    }
1110
    return ( nm ) ;
1111
}
1112
 
1113
 
1114
/*
1115
    CONSTRUCT LIST OF INTEGRAL TYPES
1116
 
1117
    This routine adds all the promoted integral types to the list res.
1118
*/
1119
 
1120
static LIST ( TYPE ) add_int_types
1121
    PROTO_N ( ( res ) )
1122
    PROTO_T ( LIST ( TYPE ) res )
1123
{
1124
    if ( basetype_info [ ntype_sllong ].key ) {
1125
	res = cons_type_set ( res, type_ullong ) ;
1126
	res = cons_type_set ( res, type_sllong ) ;
1127
    }
1128
    res = cons_type_set ( res, type_ulong ) ;
1129
    res = cons_type_set ( res, type_slong ) ;
1130
    res = cons_type_set ( res, type_uint ) ;
1131
    res = cons_type_set ( res, type_sint ) ;
1132
    return ( res ) ;
1133
}
1134
 
1135
 
1136
/*
1137
    CONSTRUCT LIST OF FLOATING POINT TYPES
1138
 
1139
    This routine adds all the floating point types to the list res.
1140
*/
1141
 
1142
static LIST ( TYPE ) add_float_types
1143
    PROTO_N ( ( res ) )
1144
    PROTO_T ( LIST ( TYPE ) res )
1145
{
1146
    res = cons_type_set ( res, type_ldouble ) ;
1147
    res = cons_type_set ( res, type_double ) ;
1148
    res = cons_type_set ( res, type_float ) ;
1149
    return ( res ) ;
1150
}
1151
 
1152
 
1153
/*
1154
    CONSTRUCT A LIST OF CONVERSION TYPES
1155
 
1156
    This routine constructs a list of types consisting of all the types
1157
    of a certain kind to which the type t can be converted.  Conversions
1158
    which are worse that existing conversions are omitted.  The type kind
1159
    is indicated by the CTYPE macros defined in chktype.h, except that
1160
    CTYPE_INTEGER refers to promoted integer types unless qualified using
1161
    CTYPE_LVALUE.
1162
*/
1163
 
1164
static LIST ( TYPE ) find_type_convs
1165
    PROTO_N ( ( t, a, kind ) )
1166
    PROTO_T ( TYPE t X EXP a X unsigned kind )
1167
{
1168
    LIST ( TYPE ) res = NULL_list ( TYPE ) ;
1169
    unsigned nt = TAG_type ( t ) ;
1170
    switch ( nt ) {
1171
	case type_compound_tag : {
1172
	    /* Class types */
1173
	    LIST ( IDENTIFIER ) conv ;
1174
	    CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
1175
	    complete_class ( ct, 1 ) ;
1176
	    conv = DEREF_list ( ctype_conv ( ct ) ) ;
1177
	    for ( ; !IS_NULL_list ( conv ) ; conv = TAIL_list ( conv ) ) {
1178
		/* Scan through conversion operations */
1179
		TYPE r ;
1180
		HASHID nm ;
1181
		unsigned nr ;
1182
		IDENTIFIER id = DEREF_id ( HEAD_list ( conv ) ) ;
1183
		DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
1184
		if ( ds & dspec_explicit ) continue ;
1185
 
1186
		/* Check for reference types */
1187
		nm = DEREF_hashid ( id_name ( id ) ) ;
1188
		r = DEREF_type ( hashid_conv_type ( nm ) ) ;
1189
		if ( kind == CTYPE_NONE ) {
1190
		    /* Include all types in this case */
1191
		    res = cons_type_set ( res, r ) ;
1192
		    continue ;
1193
		}
1194
		nr = TAG_type ( r ) ;
1195
		if ( nr == type_ref_tag ) {
1196
		    /* Allow for reference bindings */
1197
		    if ( IS_TYPE_LVALUE ( kind ) ) {
1198
			TYPE s = DEREF_type ( type_ref_sub ( r ) ) ;
1199
			CV_SPEC cv = find_cv_qual ( s ) ;
1200
			if ( !( cv & cv_const ) ) {
1201
			    /* Don't include const references */
1202
			    unsigned ns = TAG_type ( s ) ;
1203
			    switch ( ns ) {
1204
				case type_integer_tag :
1205
				case type_bitfield_tag :
1206
				case type_enumerate_tag : {
1207
				    if ( IS_TYPE_INTEGER ( kind ) ) {
1208
					res = cons_type_set ( res, r ) ;
1209
				    }
1210
				    break ;
1211
				}
1212
				case type_floating_tag : {
1213
				    if ( IS_TYPE_FLOAT ( kind ) ) {
1214
					res = cons_type_set ( res, r ) ;
1215
				    }
1216
				    break ;
1217
				}
1218
				case type_ptr_tag : {
1219
				    if ( IS_TYPE_PTR ( kind ) ) {
1220
					res = cons_type_set ( res, r ) ;
1221
				    }
1222
				    break ;
1223
				}
1224
				case type_ptr_mem_tag : {
1225
				    if ( IS_TYPE_PTR_MEM ( kind ) ) {
1226
					res = cons_type_set ( res, r ) ;
1227
				    }
1228
				    break ;
1229
				}
1230
			    }
1231
			}
1232
		    } else {
1233
			/* Allow for reference conversions */
1234
			r = DEREF_type ( type_ref_sub ( r ) ) ;
1235
			nr = TAG_type ( r ) ;
1236
		    }
1237
		}
1238
 
1239
		/* Check return type */
1240
		switch ( nr ) {
1241
		    case type_integer_tag :
1242
		    case type_bitfield_tag :
1243
		    case type_enumerate_tag : {
1244
			/* Integral types */
1245
			if ( IS_TYPE_INTEGER ( kind ) ) {
1246
			    /* Means promoted integer type */
1247
			    TYPE s = promote_type ( r ) ;
1248
			    res = cons_type_set ( res, s ) ;
1249
			} else if ( IS_TYPE_FLOAT ( kind ) ) {
1250
			    /* Can convert to any floating type */
1251
			    res = add_float_types ( res ) ;
1252
			}
1253
			break ;
1254
		    }
1255
		    case type_floating_tag : {
1256
			/* Floating point types */
1257
			if ( IS_TYPE_FLOAT ( kind ) ) {
1258
			    res = cons_type_set ( res, r ) ;
1259
			} else if ( IS_TYPE_INTEGER ( kind ) ) {
1260
			    /* Can convert to any promoted integer type */
1261
			    res = add_int_types ( res ) ;
1262
			}
1263
			break ;
1264
		    }
1265
		    case type_ptr_tag : {
1266
			/* Pointer types */
1267
			if ( IS_TYPE_PTR ( kind ) ) {
1268
			    res = cons_type_set ( res, r ) ;
1269
			}
1270
			break ;
1271
		    }
1272
		    case type_ptr_mem_tag : {
1273
			/* Pointer to member types */
1274
			if ( IS_TYPE_PTR_MEM ( kind ) ) {
1275
			    res = cons_type_set ( res, r ) ;
1276
			}
1277
			break ;
1278
		    }
1279
		}
1280
	    }
1281
	    res = REVERSE_list ( res ) ;
1282
	    break ;
1283
	}
1284
	case type_integer_tag :
1285
	case type_bitfield_tag :
1286
	case type_enumerate_tag : {
1287
	    /* Integral types */
1288
	    if ( IS_TYPE_LVALUE ( kind ) ) {
1289
		/* Lvalue of integral type */
1290
		if ( IS_TYPE_INTEGER ( kind ) ) {
1291
		    if ( nt == type_integer_tag ) {
1292
			t = lvalue_type ( t ) ;
1293
			MAKE_type_ref ( cv_none, t, t ) ;
1294
			CONS_type ( t, res, res ) ;
1295
		    }
1296
		}
1297
	    } else if ( IS_TYPE_INTEGER ( kind ) ) {
1298
		/* Means promoted integer type */
1299
		TYPE s = promote_type ( t ) ;
1300
		CONS_type ( s, res, res ) ;
1301
	    } else if ( IS_TYPE_FLOAT ( kind ) ) {
1302
		/* Can convert to any floating type */
1303
		res = add_float_types ( res ) ;
1304
	    }
1305
	    break ;
1306
	}
1307
	case type_floating_tag : {
1308
	    /* Floating point types */
1309
	    if ( IS_TYPE_LVALUE ( kind ) ) {
1310
		/* Lvalue of floating point type */
1311
		if ( IS_TYPE_FLOAT ( kind ) ) {
1312
		    t = lvalue_type ( t ) ;
1313
		    MAKE_type_ref ( cv_none, t, t ) ;
1314
		    CONS_type ( t, res, res ) ;
1315
		}
1316
	    } else if ( IS_TYPE_FLOAT ( kind ) ) {
1317
		/* Floating point type */
1318
		CONS_type ( t, res, res ) ;
1319
	    } else if ( IS_TYPE_INTEGER ( kind ) ) {
1320
		/* Can convert to any promoted integer type */
1321
		if ( basetype_info [ ntype_sllong ].key ) {
1322
		    CONS_type ( type_ullong, res, res ) ;
1323
		    CONS_type ( type_sllong, res, res ) ;
1324
		}
1325
		CONS_type ( type_ulong, res, res ) ;
1326
		CONS_type ( type_slong, res, res ) ;
1327
		CONS_type ( type_uint, res, res ) ;
1328
		CONS_type ( type_sint, res, res ) ;
1329
	    }
1330
	    break ;
1331
	}
1332
	case type_ptr_tag : {
1333
	    /* Pointer types */
1334
	    if ( IS_TYPE_PTR ( kind ) ) {
1335
		if ( IS_TYPE_LVALUE ( kind ) ) {
1336
		    t = lvalue_type ( t ) ;
1337
		    MAKE_type_ref ( cv_none, t, t ) ;
1338
		}
1339
		CONS_type ( t, res, res ) ;
1340
	    }
1341
	    break ;
1342
	}
1343
	case type_ptr_mem_tag : {
1344
	    /* Pointer to member types */
1345
	    if ( IS_TYPE_PTR_MEM ( kind ) ) {
1346
		if ( IS_TYPE_LVALUE ( kind ) ) {
1347
		    t = lvalue_type ( t ) ;
1348
		    MAKE_type_ref ( cv_none, t, t ) ;
1349
		}
1350
		CONS_type ( t, res, res ) ;
1351
	    }
1352
	    break ;
1353
	}
1354
	case type_array_tag : {
1355
	    /* Allow for array-to-pointer conversion */
1356
	    if ( IS_TYPE_PTR ( kind ) && !IS_TYPE_LVALUE ( kind ) ) {
1357
		TYPE s = DEREF_type ( type_array_sub ( t ) ) ;
1358
		MAKE_type_ptr ( cv_none, s, s ) ;
1359
		CONS_type ( s, res, res ) ;
1360
	    }
1361
	    if ( kind == CTYPE_NONE ) {
1362
		TYPE s = DEREF_type ( type_array_sub ( t ) ) ;
1363
		MAKE_type_ptr ( cv_none, s, t ) ;
1364
	    }
1365
	    break ;
1366
	}
1367
	case type_func_tag : {
1368
	    /* Allow for function-to-pointer conversion */
1369
	    if ( IS_TYPE_PTR ( kind ) && !IS_TYPE_LVALUE ( kind ) ) {
1370
		TYPE s ;
1371
		MAKE_type_ptr ( cv_none, t, s ) ;
1372
		CONS_type ( s, res, res ) ;
1373
	    }
1374
	    if ( kind == CTYPE_NONE ) {
1375
		MAKE_type_ptr ( cv_none, t, t ) ;
1376
	    }
1377
	    break ;
1378
	}
1379
    }
1380
    if ( kind == CTYPE_NONE ) {
1381
	/* Include all types in this case */
1382
	CV_SPEC cv = DEREF_cv ( type_qual ( t ) ) ;
1383
	if ( cv & cv_lvalue ) {
1384
	    /* Turn lvalues into references */
1385
	    MAKE_type_ref ( cv_none, t, t ) ;
1386
	}
1387
	res = cons_type_set ( res, t ) ;
1388
    }
1389
    UNUSED ( a ) ;
1390
    return ( res ) ;
1391
}
1392
 
1393
 
1394
/*
1395
    FILTER A LIST OF POINTER TYPES
1396
 
1397
    This routine scans through the list of pointer or pointer to member
1398
    types pa replacing any types which are not pointers to complete object
1399
    types by the null type.  If fn is true pointer to function types are
1400
    also allowed.
1401
*/
1402
 
1403
static void filter_ptr
1404
    PROTO_N ( ( pa, fn ) )
1405
    PROTO_T ( LIST ( TYPE ) pa X int fn )
1406
{
1407
    while ( !IS_NULL_list ( pa ) ) {
1408
	TYPE ta = DEREF_type ( HEAD_list ( pa ) ) ;
1409
	TYPE sa = ta ;
1410
	if ( IS_type_ref ( sa ) ) {
1411
	    sa = DEREF_type ( type_ref_sub ( sa ) ) ;
1412
	}
1413
	if ( IS_type_ptr ( sa ) ) {
1414
	    sa = DEREF_type ( type_ptr_sub ( sa ) ) ;
1415
	} else {
1416
	    sa = DEREF_type ( type_ptr_mem_sub ( sa ) ) ;
1417
	}
1418
	switch ( TAG_type ( sa ) ) {
1419
	    case type_array_tag : {
1420
		/* Check for incomplete arrays */
1421
		NAT n = DEREF_nat ( type_array_size ( sa ) ) ;
1422
		if ( IS_NULL_nat ( n ) ) ta = NULL_type ;
1423
		break ;
1424
	    }
1425
	    case type_compound_tag : {
1426
		/* Check for incomplete classes */
1427
		CLASS_TYPE cs = DEREF_ctype ( type_compound_defn ( sa ) ) ;
1428
		CLASS_INFO ci = DEREF_cinfo ( ctype_info ( cs ) ) ;
1429
		if ( !( ci & cinfo_complete ) ) ta = NULL_type ;
1430
		break ;
1431
	    }
1432
	    case type_enumerate_tag : {
1433
		/* Check for incomplete enumerations */
1434
		ENUM_TYPE es = DEREF_etype ( type_enumerate_defn ( sa ) ) ;
1435
		CLASS_INFO ei = DEREF_cinfo ( etype_info ( es ) ) ;
1436
		if ( !( ei & cinfo_complete ) ) ta = NULL_type ;
1437
		break ;
1438
	    }
1439
	    case type_top_tag :
1440
	    case type_bottom_tag : {
1441
		/* These are always incomplete */
1442
		ta = NULL_type ;
1443
		break ;
1444
	    }
1445
	    case type_func_tag : {
1446
		/* Deal with function types */
1447
		if ( !fn ) ta = NULL_type ;
1448
		break ;
1449
	    }
1450
	}
1451
	COPY_type ( HEAD_list ( pa ), ta ) ;
1452
	pa = TAIL_list ( pa ) ;
1453
    }
1454
    return ;
1455
}
1456
 
1457
 
1458
/*
1459
    BUILT-IN OPERATOR TYPES
1460
 
1461
    The following macros are used to describe the return types and the
1462
    constraints on the operand types of the built-in operators.
1463
*/
1464
 
1465
#define RTYPE_ARG_1		0
1466
#define RTYPE_ARG_2		1
1467
#define RTYPE_CONT_1		2
1468
#define RTYPE_CONT_2		3
1469
#define RTYPE_ARITH		4
1470
#define RTYPE_BOOL		5
1471
#define RTYPE_PTRDIFF		6
1472
#define RTYPE_OP		7
1473
 
1474
#define OTYPE_NONE		0
1475
#define OTYPE_PTR		1
1476
#define OTYPE_PTR_MEM		2
1477
#define OTYPE_REF_PTR		3
1478
#define OTYPE_REF_MEM		4
1479
#define OTYPE_SELECT		5
1480
#define OTYPE_COND		6
1481
 
1482
 
1483
/*
1484
    FIND RETURN TYPE OF BUILT-IN OPERATOR
1485
 
1486
    This routine finds the return type for a built-in operator with
1487
    operand types ta and tb and return descriptor rtype.  tc gives the
1488
    default return type.  The return type is not used in overload
1489
    resolution but is included for error reporting purposes.
1490
*/
1491
 
1492
static TYPE find_builtin_ret
1493
    PROTO_N ( ( ta, tb, rtype, tc ) )
1494
    PROTO_T ( TYPE ta X TYPE tb X int rtype X TYPE tc )
1495
{
1496
    switch ( rtype ) {
1497
	case RTYPE_ARG_1 : {
1498
	    tc = ta ;
1499
	    break ;
1500
	}
1501
	case RTYPE_ARG_2 : {
1502
	    tc = tb ;
1503
	    break ;
1504
	}
1505
	case RTYPE_CONT_1 : {
1506
	    if ( IS_type_ptr_etc ( ta ) ) {
1507
		tc = DEREF_type ( type_ptr_etc_sub ( ta ) ) ;
1508
	    }
1509
	    break ;
1510
	}
1511
	case RTYPE_CONT_2 : {
1512
	    if ( IS_type_ptr_etc ( tb ) ) {
1513
		tc = DEREF_type ( type_ptr_etc_sub ( tb ) ) ;
1514
	    }
1515
	    break ;
1516
	}
1517
	case RTYPE_ARITH : {
1518
	    tc = arith_type ( ta, tb, NULL_exp, NULL_exp ) ;
1519
	    break ;
1520
	}
1521
	case RTYPE_BOOL : {
1522
	    tc = type_bool ;
1523
	    break ;
1524
	}
1525
	case RTYPE_PTRDIFF : {
1526
	    tc = type_ptrdiff_t ;
1527
	    break ;
1528
	}
1529
    }
1530
    return ( tc ) ;
1531
}
1532
 
1533
 
1534
/*
1535
    CHECK BUILT-IN OPERAND TYPES
1536
 
1537
    This routine checks whether the operand types ta and tb are valid for
1538
    a built-in operator with operand constraints otype.  In some cases
1539
    the return type is returned via pt.
1540
*/
1541
 
1542
static int check_builtin_args
1543
    PROTO_N ( ( ta, tb, otype, pt ) )
1544
    PROTO_T ( TYPE ta X TYPE tb X int otype X TYPE *pt )
1545
{
1546
    int ok = 1 ;
1547
    switch ( otype ) {
1548
	case OTYPE_PTR : {
1549
	    /* Allow equal pointer types */
1550
	    TYPE sa = DEREF_type ( type_ptr_sub ( ta ) ) ;
1551
	    TYPE sb = DEREF_type ( type_ptr_sub ( tb ) ) ;
1552
	    ok = eq_type_unqual ( sa, sb ) ;
1553
	    break ;
1554
	}
1555
	case OTYPE_PTR_MEM : {
1556
	    /* Allow equal pointer member types */
1557
	    CLASS_TYPE ca = DEREF_ctype ( type_ptr_mem_of ( ta ) ) ;
1558
	    CLASS_TYPE cb = DEREF_ctype ( type_ptr_mem_of ( tb ) ) ;
1559
	    if ( eq_ctype ( ca, cb ) ) {
1560
		TYPE sa = DEREF_type ( type_ptr_mem_sub ( ta ) ) ;
1561
		TYPE sb = DEREF_type ( type_ptr_mem_sub ( tb ) ) ;
1562
		ok = eq_type_unqual ( sa, sb ) ;
1563
	    } else {
1564
		ok = 0 ;
1565
	    }
1566
	    break ;
1567
	}
1568
	case OTYPE_REF_PTR : {
1569
	    /* Allow equal pointer types */
1570
	    if ( IS_type_ref ( ta ) ) {
1571
		ta = DEREF_type ( type_ref_sub ( ta ) ) ;
1572
	    }
1573
	    return ( check_builtin_args ( ta, tb, OTYPE_PTR, pt ) ) ;
1574
	}
1575
	case OTYPE_REF_MEM : {
1576
	    /* Allow equal pointer member types */
1577
	    if ( IS_type_ref ( ta ) ) {
1578
		ta = DEREF_type ( type_ref_sub ( ta ) ) ;
1579
	    }
1580
	    return ( check_builtin_args ( ta, tb, OTYPE_PTR_MEM, pt ) ) ;
1581
	}
1582
	case OTYPE_SELECT : {
1583
	    /* Allow pointer member selection */
1584
	    TYPE sa = DEREF_type ( type_ptr_sub ( ta ) ) ;
1585
	    if ( IS_type_compound ( sa ) ) {
1586
		CLASS_TYPE ca = DEREF_ctype ( type_compound_defn ( sa ) ) ;
1587
		CLASS_TYPE cb = DEREF_ctype ( type_ptr_mem_of ( tb ) ) ;
1588
		GRAPH gr = find_base_class ( ca, cb, 0 ) ;
1589
		if ( IS_NULL_graph ( gr ) ) ok = 0 ;
1590
		*pt = DEREF_type ( type_ptr_mem_sub ( tb ) ) ;
1591
	    } else {
1592
		ok = 0 ;
1593
	    }
1594
	    break ;
1595
	}
1596
	case OTYPE_COND : {
1597
	    /* Allow for conditional expressions */
1598
	    int suspect = 0 ;
1599
	    TYPE r = common_type ( ta, tb, &suspect ) ;
1600
	    if ( IS_NULL_type ( r ) ) {
1601
		ok = 0 ;
1602
		if ( IS_type_ref ( ta ) ) {
1603
		    ta = DEREF_type ( type_ref_sub ( ta ) ) ;
1604
		    ok = 1 ;
1605
		}
1606
		if ( IS_type_ref ( tb ) ) {
1607
		    tb = DEREF_type ( type_ref_sub ( tb ) ) ;
1608
		    ok = 1 ;
1609
		}
1610
		if ( ok ) {
1611
		    suspect = 0 ;
1612
		    r = common_type ( ta, tb, &suspect ) ;
1613
		    if ( IS_NULL_type ( r ) ) {
1614
			ok = 0 ;
1615
		    } else {
1616
			*pt = r ;
1617
		    }
1618
		}
1619
	    } else {
1620
		*pt = r ;
1621
	    }
1622
	    break ;
1623
	}
1624
    }
1625
    return ( ok ) ;
1626
}
1627
 
1628
 
1629
/*
1630
    ADD A LIST OF BUILT-IN UNARY CANDIDATES
1631
 
1632
    This routine adds a series of built-in unary candidates for the
1633
    operator nm to the list p.  The possible operand types are given by pa.
1634
    rtype describes how to form the return type from the operand type.
1635
*/
1636
 
1637
static void add_unary_builtin
1638
    PROTO_N ( ( p, nm, pa, rtype ) )
1639
    PROTO_T ( CANDIDATE_LIST *p X HASHID nm X LIST ( TYPE ) pa X int rtype )
1640
{
1641
    while ( !IS_NULL_list ( pa ) ) {
1642
	TYPE ta = DEREF_type ( HEAD_list ( pa ) ) ;
1643
	if ( !IS_NULL_type ( ta ) ) {
1644
	    TYPE tb = find_builtin_ret ( ta, ta, rtype, type_error ) ;
1645
	    IDENTIFIER id = unary_builtin ( nm, ta, tb ) ;
1646
	    add_candidates ( p, id, 1, KIND_BUILTIN ) ;
1647
	}
1648
	pa = TAIL_list ( pa ) ;
1649
    }
1650
    DESTROY_list ( pa, SIZE_type ) ;
1651
    return ;
1652
}
1653
 
1654
 
1655
/*
1656
    ADD A LIST OF BUILT-IN BINARY CANDIDATES
1657
 
1658
    This routine adds a series of built-in binary candidates for the
1659
    operator nm to the list p.  The possible first and second operand types
1660
    are given by pa and pb.  rtype describes how to form the return type
1661
    from the operand types, otype describes any restrictions on the operand
1662
    types.
1663
*/
1664
 
1665
static void add_binary_builtin
1666
    PROTO_N ( ( p, nm, pa, pb, rtype, otype ) )
1667
    PROTO_T ( CANDIDATE_LIST *p X HASHID nm X LIST ( TYPE ) pa X
1668
	      LIST ( TYPE ) pb X int rtype X int otype )
1669
{
1670
    while ( !IS_NULL_list ( pa ) ) {
1671
	TYPE ta = DEREF_type ( HEAD_list ( pa ) ) ;
1672
	if ( !IS_NULL_type ( ta ) ) {
1673
	    LIST ( TYPE ) pc = pb ;
1674
	    while ( !IS_NULL_list ( pc ) ) {
1675
		TYPE tb = DEREF_type ( HEAD_list ( pc ) ) ;
1676
		if ( !IS_NULL_type ( tb ) ) {
1677
		    /* Check operand types */
1678
		    TYPE tc = type_error ;
1679
		    if ( check_builtin_args ( ta, tb, otype, &tc ) ) {
1680
			IDENTIFIER id ;
1681
			tc = find_builtin_ret ( ta, tb, rtype, tc ) ;
1682
			if ( otype == OTYPE_COND ) {
1683
			    ta = tc ;
1684
			    tb = tc ;
1685
			}
1686
			id = binary_builtin ( nm, ta, tb, tc ) ;
1687
			add_candidates ( p, id, 1, KIND_BUILTIN ) ;
1688
		    }
1689
		}
1690
		pc = TAIL_list ( pc ) ;
1691
	    }
1692
	}
1693
	pa = TAIL_list ( pa ) ;
1694
    }
1695
    if ( !EQ_list ( pa, pb ) ) DESTROY_list ( pa, SIZE_type ) ;
1696
    DESTROY_list ( pb, SIZE_type ) ;
1697
    return ;
1698
}
1699
 
1700
 
1701
/*
1702
    OVERLOAD A UNARY OPERATOR
1703
 
1704
    This routine calculates the unary overload operator 'op a'.  This can
1705
    be 'a.operator op ()', 'operator op ( a )' or 'op t ( a )' for some
1706
    type t.  Note that '->' is a special case for op, with 'a->b' being
1707
    expanded as '( a.operator -> () )->b'.  This expansion is done in
1708
    begin_field_exp, this routine returning the null expression to indicate
1709
    that '->' is not overloaded.
1710
*/
1711
 
1712
EXP unary_overload
1713
    PROTO_N ( ( op, a ) )
1714
    PROTO_T ( int op X EXP a )
1715
{
1716
    EXP e ;
1717
    HASHID nm ;
1718
    IDENTIFIER id ;
1719
    LIST ( TYPE ) pa ;
1720
    CANDIDATE_LIST *p = &candidates ;
1721
 
1722
    /* Check for template parameters */
1723
    TYPE t = DEREF_type ( exp_type ( a ) ) ;
1724
    if ( is_templ_type ( t ) ) {
1725
	MAKE_exp_op ( t, op, a, NULL_exp, e ) ;
1726
	return ( e ) ;
1727
    }
1728
 
1729
    /* Construct candidate list */
1730
    p->size = 0 ;
1731
    nm = overload_candidates ( p, op, t, NULL_type ) ;
1732
 
1733
    /* Construct built-in candidates */
1734
    switch ( op ) {
1735
	case lex_plus : {
1736
	    /* Built-in candidates for '+a' */
1737
	    pa = find_type_convs ( t, a, CTYPE_SCALAR ) ;
1738
	    add_unary_builtin ( p, nm, pa, RTYPE_ARG_1 ) ;
1739
	    break ;
1740
	}
1741
	case lex_minus :
1742
	case lex_abs : {
1743
	    /* Built-in candidates for '-a' */
1744
	    pa = find_type_convs ( t, a, CTYPE_ARITH ) ;
1745
	    add_unary_builtin ( p, nm, pa, RTYPE_ARG_1 ) ;
1746
	    break ;
1747
	}
1748
	case lex_compl_H1 : {
1749
	    /* Built-in candidates for '~a' */
1750
	    pa = find_type_convs ( t, a, CTYPE_INT ) ;
1751
	    add_unary_builtin ( p, nm, pa, RTYPE_ARG_1 ) ;
1752
	    break ;
1753
	}
1754
	case lex_not_H1 : {
1755
	    /* Built-in candidate for '!a' */
1756
	    id = unary_builtin ( nm, type_bool, type_bool ) ;
1757
	    add_candidates ( p, id, 1, KIND_BUILTIN ) ;
1758
	    break ;
1759
	}
1760
	case lex_star : {
1761
	    /* Built-in candidates for '*a' */
1762
	    pa = find_type_convs ( t, a, CTYPE_PTR ) ;
1763
	    filter_ptr ( pa, 1 ) ;
1764
	    add_unary_builtin ( p, nm, pa, RTYPE_CONT_1 ) ;
1765
	    break ;
1766
	}
1767
	case lex_plus_Hplus :
1768
	case lex_minus_Hminus : {
1769
	    /* Built-in candidates for '++a' and '--a' */
1770
	    pa = find_type_convs ( t, a, ( CTYPE_LVALUE | CTYPE_PTR ) ) ;
1771
	    filter_ptr ( pa, 0 ) ;
1772
	    add_unary_builtin ( p, nm, pa, RTYPE_ARG_1 ) ;
1773
	    pa = find_type_convs ( t, a, ( CTYPE_LVALUE | CTYPE_ARITH ) ) ;
1774
	    add_unary_builtin ( p, nm, pa, RTYPE_ARG_1 ) ;
1775
	    break ;
1776
	}
1777
	case lex_and_H1 : {
1778
	    /* No built-in candidates for '&a' */
1779
	    break ;
1780
	}
1781
	case lex_arrow : {
1782
	    /* No built-in candidates for 'a->' */
1783
	    break ;
1784
	}
1785
    }
1786
 
1787
    /* Search for overload operator */
1788
    if ( p->size ) {
1789
	CANDIDATE *q ;
1790
	unsigned rank ;
1791
	LIST ( EXP ) args ;
1792
	CONS_exp ( a, NULL_list ( EXP ), args ) ;
1793
	q = resolve_overload ( p, args, NULL_type, 0 ) ;
1794
	unary_free = unary_all ;
1795
	rank = q->rank ;
1796
	if ( rank >= RANK_VIABLE ) {
1797
	    /* Only allow viable resolutions */
1798
	    int kind = q->kind ;
1799
	    IDENTIFIER qid = q->func ;
1800
	    int ow = overload_warn ;
1801
	    if ( rank == RANK_BEST ) {
1802
		/* Unambiguous resolution */
1803
		if ( match_no_viable > 1 && ow ) {
1804
		    if ( do_dump ) dump_builtin ( qid ) ;
1805
		    report ( crt_loc, ERR_over_match_oper_ok ( qid ) ) ;
1806
		}
1807
	    } else {
1808
		/* Ambiguous resolution */
1809
		q = resolve_ambiguous ( p, args, NULL_type, 0 ) ;
1810
		qid = q->func ;
1811
		rank = q->rank ;
1812
		if ( rank == RANK_TARGET ) {
1813
		    ERROR err2 = ERR_over_match_oper_target ( op ) ;
1814
		    qid = make_ambig_func ( p, qid, args, qual_none, &err2 ) ;
1815
		    kind = KIND_OP ;
1816
		    if ( do_dump ) dump_builtin ( qid ) ;
1817
		    report ( crt_loc, err2 ) ;
1818
		} else if ( rank == RANK_VIABLE ) {
1819
		    ERROR err2 = ERR_over_match_oper_ambig ( op ) ;
1820
		    err2 = list_candidates ( err2, p, RANK_VIABLE ) ;
1821
		    report ( crt_loc, err2 ) ;
1822
		}
1823
		overload_warn = 0 ;
1824
	    }
1825
	    if ( kind == KIND_BUILTIN ) {
1826
		/* Built-in resolution */
1827
		if ( op == lex_arrow ) {
1828
		    /* For 'a->' return the null expression */
1829
		    DESTROY_list ( args, SIZE_exp ) ;
1830
		    e = NULL_exp ;
1831
		} else {
1832
		    e = apply_builtin ( qid, args ) ;
1833
		}
1834
	    } else {
1835
		/* Function resolution */
1836
		use_func_id ( qid, 0, suppress_usage ) ;
1837
		e = apply_func_id ( qid, qual_none, NULL_graph, args ) ;
1838
	    }
1839
	    overload_warn = ow ;
1840
	    return ( e ) ;
1841
	}
1842
	DESTROY_list ( args, SIZE_exp ) ;
1843
    }
1844
 
1845
    /* Try operation again */
1846
    if ( op == lex_arrow ) {
1847
	/* For 'a->' return the null expression */
1848
	e = NULL_exp ;
1849
    } else {
1850
	overload_depth++ ;
1851
	e = apply_unary ( op, a, NULL_type, NULL_type, 0 ) ;
1852
	overload_depth-- ;
1853
    }
1854
    return ( e ) ;
1855
}
1856
 
1857
 
1858
/*
1859
    OVERLOAD A BINARY OPERATOR
1860
 
1861
    This routine calculates the binary overload operator 'a op b'.  This can
1862
    be 'a.operator op ( b )', 'operator op ( a, b )' or 't ( a ) op s ( b )'
1863
    for some types t and s.  Note that postfix '++' and '--' are special
1864
    cases for op in which b is a dummy zero operand.
1865
*/
1866
 
1867
EXP binary_overload
1868
    PROTO_N ( ( op, a, b ) )
1869
    PROTO_T ( int op X EXP a X EXP b )
1870
{
1871
    EXP e ;
1872
    HASHID nm ;
1873
    LIST ( TYPE ) pa, pb ;
1874
    CANDIDATE_LIST *p = &candidates ;
1875
 
1876
    /* Check for template parameters */
1877
    TYPE t = DEREF_type ( exp_type ( a ) ) ;
1878
    TYPE s = DEREF_type ( exp_type ( b ) ) ;
1879
    if ( is_templ_type ( t ) ) {
1880
	MAKE_exp_op ( t, op, a, b, e ) ;
1881
	return ( e ) ;
1882
    }
1883
    if ( is_templ_type ( s ) ) {
1884
	MAKE_exp_op ( s, op, a, b, e ) ;
1885
	return ( e ) ;
1886
    }
1887
 
1888
    /* Construct candidate list */
1889
    p->size = 0 ;
1890
    nm = overload_candidates ( p, op, t, s ) ;
1891
 
1892
    /* Construct built-in candidates */
1893
    /* QUERY - overloaded function operands */
1894
    switch ( op ) {
1895
	case lex_logical_Hand_H1 :
1896
	case lex_logical_Hor_H1 : {
1897
	    /* Built-in candidates for 'a && b' and 'a || b' */
1898
	    IDENTIFIER id ;
1899
	    id = binary_builtin ( nm, type_bool, type_bool, type_bool ) ;
1900
	    add_candidates ( p, id, 1, KIND_BUILTIN ) ;
1901
	    break ;
1902
	}
1903
	case lex_and_H1 :
1904
	case lex_or_H1 :
1905
	case lex_rem :
1906
	case lex_xor_H1 : {
1907
	    /* Built-in candidates for 'a & b' etc. */
1908
	    pa = find_type_convs ( t, a, CTYPE_INT ) ;
1909
	    pb = find_type_convs ( s, b, CTYPE_INT ) ;
1910
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_ARITH, OTYPE_NONE ) ;
1911
	    break ;
1912
	}
1913
	case lex_lshift :
1914
	case lex_rshift : {
1915
	    /* Built-in candidates for 'a << b' and 'a >> b' */
1916
	    pa = find_type_convs ( t, a, CTYPE_INT ) ;
1917
	    pb = find_type_convs ( s, b, CTYPE_INT ) ;
1918
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_ARG_1, OTYPE_NONE ) ;
1919
	    break ;
1920
	}
1921
	case lex_minus : {
1922
	    /* Built-in candidates for 'a - b' */
1923
	    pa = find_type_convs ( t, a, CTYPE_PTR ) ;
1924
	    pb = find_type_convs ( s, b, CTYPE_PTR ) ;
1925
	    filter_ptr ( pa, 0 ) ;
1926
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_PTRDIFF, OTYPE_PTR ) ;
1927
	    pa = find_type_convs ( t, a, CTYPE_PTR ) ;
1928
	    CONS_type ( type_ptrdiff_t, NULL_list ( TYPE ), pb ) ;
1929
	    filter_ptr ( pa, 0 ) ;
1930
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_ARG_1, OTYPE_NONE ) ;
1931
	    goto arith_op_lab ;
1932
	}
1933
	case lex_plus : {
1934
	    /* Built-in candidates for 'a + b' */
1935
	    pa = find_type_convs ( t, a, CTYPE_PTR ) ;
1936
	    CONS_type ( type_ptrdiff_t, NULL_list ( TYPE ), pb ) ;
1937
	    filter_ptr ( pa, 0 ) ;
1938
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_ARG_1, OTYPE_NONE ) ;
1939
	    CONS_type ( type_ptrdiff_t, NULL_list ( TYPE ), pa ) ;
1940
	    pb = find_type_convs ( s, b, CTYPE_PTR ) ;
1941
	    filter_ptr ( pb, 0 ) ;
1942
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_ARG_2, OTYPE_NONE ) ;
1943
	    goto arith_op_lab ;
1944
	}
1945
	case lex_array_Hop : {
1946
	    /* Built-in candidates for 'a [b]' */
1947
	    pa = find_type_convs ( t, a, CTYPE_PTR ) ;
1948
	    CONS_type ( type_ptrdiff_t, NULL_list ( TYPE ), pb ) ;
1949
	    filter_ptr ( pa, 0 ) ;
1950
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_CONT_1, OTYPE_NONE ) ;
1951
	    CONS_type ( type_ptrdiff_t, NULL_list ( TYPE ), pa ) ;
1952
	    pb = find_type_convs ( s, b, CTYPE_PTR ) ;
1953
	    filter_ptr ( pb, 0 ) ;
1954
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_CONT_2, OTYPE_NONE ) ;
1955
	    break ;
1956
	}
1957
	case lex_star :
1958
	case lex_div :
1959
	case lex_max :
1960
	case lex_min :
1961
	arith_op_lab : {
1962
	    /* Built-in candidates for 'a * b' and 'a / b' */
1963
	    pa = find_type_convs ( t, a, CTYPE_ARITH ) ;
1964
	    pb = find_type_convs ( s, b, CTYPE_ARITH ) ;
1965
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_ARITH, OTYPE_NONE ) ;
1966
	    break ;
1967
	}
1968
	case lex_eq :
1969
	case lex_not_Heq_H1 : {
1970
	    /* Built-in candidates for 'a == b' and 'a != b' */
1971
	    pa = find_type_convs ( t, a, CTYPE_PTR_MEM ) ;
1972
	    pb = find_type_convs ( s, b, CTYPE_PTR_MEM ) ;
1973
	    if ( is_zero_exp ( a ) ) pa = pb ;
1974
	    if ( is_zero_exp ( b ) ) pb = pa ;
1975
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_BOOL, OTYPE_PTR_MEM ) ;
1976
	    goto relation_op_lab ;
1977
	}
1978
	case lex_greater :
1979
	case lex_greater_Heq :
1980
	case lex_less :
1981
	case lex_less_Heq :
1982
	relation_op_lab : {
1983
	    /* Built-in candidates for 'a > b' etc. */
1984
	    pa = find_type_convs ( t, a, CTYPE_PTR ) ;
1985
	    pb = find_type_convs ( s, b, CTYPE_PTR ) ;
1986
	    if ( is_zero_exp ( a ) ) pa = pb ;
1987
	    if ( is_zero_exp ( b ) ) pb = pa ;
1988
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_BOOL, OTYPE_PTR ) ;
1989
	    pa = find_type_convs ( t, a, CTYPE_ARITH ) ;
1990
	    pb = find_type_convs ( s, b, CTYPE_ARITH ) ;
1991
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_BOOL, OTYPE_NONE ) ;
1992
	    break ;
1993
	}
1994
	case lex_arrow_Hstar : {
1995
	    /* Built-in candidates for 'a->*b' */
1996
	    pa = find_type_convs ( t, a, CTYPE_PTR ) ;
1997
	    pb = find_type_convs ( s, b, CTYPE_PTR_MEM ) ;
1998
	    filter_ptr ( pb, 1 ) ;
1999
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_OP, OTYPE_SELECT ) ;
2000
	    break ;
2001
	}
2002
	case lex_plus_Hplus :
2003
	case lex_minus_Hminus : {
2004
	    /* Built-in candidates for 'a++' and 'a--' */
2005
	    pa = find_type_convs ( t, a, ( CTYPE_LVALUE | CTYPE_PTR ) ) ;
2006
	    CONS_type ( type_sint, NULL_list ( TYPE ), pb ) ;
2007
	    filter_ptr ( pa, 0 ) ;
2008
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_CONT_1, OTYPE_NONE ) ;
2009
	    pa = find_type_convs ( t, a, ( CTYPE_LVALUE | CTYPE_ARITH ) ) ;
2010
	    CONS_type ( type_sint, NULL_list ( TYPE ), pb ) ;
2011
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_CONT_1, OTYPE_NONE ) ;
2012
	    break ;
2013
	}
2014
	case lex_and_Heq_H1 :
2015
	case lex_lshift_Heq :
2016
	case lex_or_Heq_H1 :
2017
	case lex_rem_Heq :
2018
	case lex_rshift_Heq :
2019
	case lex_xor_Heq_H1 : {
2020
	    /* Built-in candidates for 'a &= b' etc. */
2021
	    pa = find_type_convs ( t, a, ( CTYPE_LVALUE | CTYPE_INT ) ) ;
2022
	    pb = find_type_convs ( s, b, CTYPE_INT ) ;
2023
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_ARG_1, OTYPE_NONE ) ;
2024
	    break ;
2025
	}
2026
	case lex_div_Heq :
2027
	case lex_star_Heq : {
2028
	    /* Built-in candidates for 'a /= b' and 'a *= b' */
2029
	    pa = find_type_convs ( t, a, ( CTYPE_LVALUE | CTYPE_ARITH ) ) ;
2030
	    pb = find_type_convs ( s, b, CTYPE_ARITH ) ;
2031
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_ARG_1, OTYPE_NONE ) ;
2032
	    break ;
2033
	}
2034
	case lex_plus_Heq :
2035
	case lex_minus_Heq : {
2036
	    /* Built-in candidates for 'a += b' and 'a -= b' */
2037
	    pa = find_type_convs ( t, a, ( CTYPE_LVALUE | CTYPE_PTR ) ) ;
2038
	    CONS_type ( type_ptrdiff_t, NULL_list ( TYPE ), pb ) ;
2039
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_ARG_1, OTYPE_NONE ) ;
2040
	    pa = find_type_convs ( t, a, ( CTYPE_LVALUE | CTYPE_ARITH ) ) ;
2041
	    pb = find_type_convs ( s, b, CTYPE_ARITH ) ;
2042
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_ARG_1, OTYPE_NONE ) ;
2043
	    break ;
2044
	}
2045
	case lex_assign : {
2046
	    /* Built-in candidates for 'a = b' */
2047
	    pa = find_type_convs ( t, a, ( CTYPE_LVALUE | CTYPE_PTR_MEM ) ) ;
2048
	    pb = find_type_convs ( s, b, CTYPE_PTR_MEM ) ;
2049
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_ARG_1, OTYPE_REF_MEM ) ;
2050
	    pa = find_type_convs ( t, a, ( CTYPE_LVALUE | CTYPE_PTR ) ) ;
2051
	    pb = find_type_convs ( s, b, CTYPE_PTR ) ;
2052
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_ARG_1, OTYPE_REF_PTR ) ;
2053
	    pa = find_type_convs ( t, a, ( CTYPE_LVALUE | CTYPE_ARITH ) ) ;
2054
	    pb = find_type_convs ( s, b, CTYPE_ARITH ) ;
2055
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_ARG_1, OTYPE_NONE ) ;
2056
	    break ;
2057
	}
2058
	case lex_comma : {
2059
	    /* No built-in candidates for 'a, b' */
2060
	    break ;
2061
	}
2062
	case lex_colon : {
2063
	    /* Built-in candidates for '<condition> ? a : b' */
2064
	    LIST ( TYPE ) pc ;
2065
	    pa = find_type_convs ( t, a, CTYPE_NONE ) ;
2066
	    pb = find_type_convs ( s, b, CTYPE_NONE ) ;
2067
	    if ( is_zero_exp ( a ) ) {
2068
		/* Allow for null pointers */
2069
		pc = find_type_convs ( s, b, ( CTYPE_PTR | CTYPE_PTR_MEM ) ) ;
2070
		pa = APPEND_list ( pa, pc ) ;
2071
	    }
2072
	    if ( is_zero_exp ( b ) ) {
2073
		/* Allow for null pointers */
2074
		pc = find_type_convs ( t, a, ( CTYPE_PTR | CTYPE_PTR_MEM ) ) ;
2075
		pb = APPEND_list ( pb, pc ) ;
2076
	    }
2077
	    add_binary_builtin ( p, nm, pa, pb, RTYPE_OP, OTYPE_COND ) ;
2078
	    break ;
2079
	}
2080
    }
2081
 
2082
    /* Search for overload operator */
2083
    if ( p->size ) {
2084
	CANDIDATE *q ;
2085
	unsigned rank ;
2086
	LIST ( EXP ) args ;
2087
	CONS_exp ( b, NULL_list ( EXP ), args ) ;
2088
	CONS_exp ( a, args, args ) ;
2089
	q = resolve_overload ( p, args, NULL_type, 0 ) ;
2090
	binary_free = binary_all ;
2091
	rank = q->rank ;
2092
	if ( rank >= RANK_VIABLE ) {
2093
	    /* Only allow viable resolutions */
2094
	    int kind = q->kind ;
2095
	    IDENTIFIER qid = q->func ;
2096
	    int ow = overload_warn ;
2097
	    if ( rank == RANK_BEST ) {
2098
		/* Unambiguous resolution */
2099
		if ( match_no_viable > 1 && ow ) {
2100
		    if ( do_dump ) dump_builtin ( qid ) ;
2101
		    report ( crt_loc, ERR_over_match_oper_ok ( qid ) ) ;
2102
		}
2103
	    } else {
2104
		/* Ambiguous resolution */
2105
		q = resolve_ambiguous ( p, args, NULL_type, 0 ) ;
2106
		qid = q->func ;
2107
		rank = q->rank ;
2108
		if ( rank == RANK_TARGET ) {
2109
		    ERROR err2 = ERR_over_match_oper_target ( op ) ;
2110
		    qid = make_ambig_func ( p, qid, args, qual_none, &err2 ) ;
2111
		    kind = KIND_OP ;
2112
		    if ( do_dump ) dump_builtin ( qid ) ;
2113
		    report ( crt_loc, err2 ) ;
2114
		} else if ( rank == RANK_VIABLE ) {
2115
		    ERROR err2 = ERR_over_match_oper_ambig ( op ) ;
2116
		    err2 = list_candidates ( err2, p, RANK_VIABLE ) ;
2117
		    report ( crt_loc, err2 ) ;
2118
		}
2119
		overload_warn = 0 ;
2120
	    }
2121
	    if ( kind == KIND_BUILTIN ) {
2122
		/* Built-in resolution */
2123
		if ( op == lex_comma ) {
2124
		    /* Return null expression for 'a, b' */
2125
		    DESTROY_list ( args, SIZE_exp ) ;
2126
		    e = NULL_exp ;
2127
		} else {
2128
		    e = apply_builtin ( qid, args ) ;
2129
		}
2130
	    } else {
2131
		/* Function resolution */
2132
		use_func_id ( qid, 0, suppress_usage ) ;
2133
		e = apply_func_id ( qid, qual_none, NULL_graph, args ) ;
2134
	    }
2135
	    overload_warn = ow ;
2136
	    return ( e ) ;
2137
	}
2138
	DESTROY_list ( args, SIZE_exp ) ;
2139
    }
2140
 
2141
    /* Try operation again */
2142
    if ( op == lex_comma ) {
2143
	/* Return null expression for 'a, b' */
2144
	e = NULL_exp ;
2145
    } else {
2146
	overload_depth++ ;
2147
	e = apply_binary ( op, a, b, NULL_type, NULL_type, 0 ) ;
2148
	overload_depth-- ;
2149
    }
2150
    return ( e ) ;
2151
}
2152
 
2153
 
2154
/*
2155
    OVERLOAD A FUNCTION OPERATOR
2156
 
2157
    This routine calculates the function overload operator 'a ( args )'.
2158
    This is expanded as 'a.operator () ( args )'.
2159
*/
2160
 
2161
EXP function_overload
2162
    PROTO_N ( ( a, args ) )
2163
    PROTO_T ( EXP a X LIST ( EXP ) args )
2164
{
2165
    EXP e ;
2166
    int op = lex_func_Hop ;
2167
 
2168
    /* Construct candidate list */
2169
    TYPE t = DEREF_type ( exp_type ( a ) ) ;
2170
    CANDIDATE_LIST *p = &candidates ;
2171
    p->size = 0 ;
2172
    IGNORE overload_candidates ( p, op, t, NULL_type ) ;
2173
 
2174
    if ( IS_type_compound ( t ) ) {
2175
	LIST ( IDENTIFIER ) conv ;
2176
	HASHID nm = KEYWORD ( lex_zzzz ) ;
2177
	CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
2178
	complete_class ( ct, 1 ) ;
2179
	conv = DEREF_list ( ctype_conv ( ct ) ) ;
2180
	while ( !IS_NULL_list ( conv ) ) {
2181
	    /* Scan through conversion operations */
2182
	    IDENTIFIER cid = DEREF_id ( HEAD_list ( conv ) ) ;
2183
	    DECL_SPEC ds = DEREF_dspec ( id_storage ( cid ) ) ;
2184
	    if ( !( ds & dspec_explicit ) ) {
2185
		HASHID cnm = DEREF_hashid ( id_name ( cid ) ) ;
2186
		TYPE r = DEREF_type ( hashid_conv_type ( cnm ) ) ;
2187
		unsigned nr = TAG_type ( r ) ;
2188
 
2189
		/* Search for pointer or reference to functions */
2190
		if ( nr == type_ptr_tag || nr == type_ref_tag ) {
2191
		    TYPE s = DEREF_type ( type_ptr_etc_sub ( r ) ) ;
2192
		    if ( IS_type_func ( s ) ) {
2193
			LOCATION loc ;
2194
			LIST ( TYPE ) ptypes ;
2195
			TYPE ret = DEREF_type ( type_func_ret ( s ) ) ;
2196
			ptypes = DEREF_list ( type_func_ptypes ( s ) ) ;
2197
			DEREF_loc ( id_loc ( cid ), loc ) ;
2198
			cid = nary_builtin ( nm, r, ptypes, ret ) ;
2199
			COPY_loc ( id_loc ( cid ), loc ) ;
2200
			add_candidates ( p, cid, 1, KIND_BUILTIN ) ;
2201
		    }
2202
		}
2203
	    }
2204
	    conv = TAIL_list ( conv ) ;
2205
	}
2206
    }
2207
 
2208
    /* Search for overload operator */
2209
    if ( p->size ) {
2210
	CANDIDATE *q ;
2211
	unsigned rank ;
2212
	CONS_exp ( a, args, args ) ;
2213
	q = resolve_overload ( p, args, NULL_type, 0 ) ;
2214
	nary_free = nary_all ;
2215
	rank = q->rank ;
2216
	if ( rank >= RANK_VIABLE ) {
2217
	    /* Only allow viable resolutions */
2218
	    int kind = q->kind ;
2219
	    IDENTIFIER qid = q->func ;
2220
	    int ow = overload_warn ;
2221
	    if ( rank == RANK_BEST ) {
2222
		/* Unambiguous resolution */
2223
		if ( match_no_viable > 1 && ow ) {
2224
		    if ( do_dump ) dump_builtin ( qid ) ;
2225
		    report ( crt_loc, ERR_over_match_oper_ok ( qid ) ) ;
2226
		}
2227
	    } else {
2228
		/* Ambiguous resolution */
2229
		q = resolve_ambiguous ( p, args, NULL_type, 0 ) ;
2230
		qid = q->func ;
2231
		rank = q->rank ;
2232
		if ( rank == RANK_TARGET ) {
2233
		    ERROR err = ERR_over_match_oper_target ( op ) ;
2234
		    qid = make_ambig_func ( p, qid, args, qual_none, &err ) ;
2235
		    kind = KIND_OP ;
2236
		    if ( do_dump ) dump_builtin ( qid ) ;
2237
		    report ( crt_loc, err ) ;
2238
		} else if ( rank == RANK_VIABLE ) {
2239
		    ERROR err = ERR_over_match_oper_ambig ( op ) ;
2240
		    err = list_candidates ( err, p, RANK_VIABLE ) ;
2241
		    report ( crt_loc, err ) ;
2242
		}
2243
		overload_warn = 0 ;
2244
	    }
2245
	    if ( kind == KIND_BUILTIN ) {
2246
		/* Built-in resolution */
2247
		e = apply_builtin ( qid, args ) ;
2248
	    } else {
2249
		/* Function resolution */
2250
		use_func_id ( qid, 0, suppress_usage ) ;
2251
		e = apply_func_id ( qid, qual_none, NULL_graph, args ) ;
2252
	    }
2253
	    overload_warn = ow ;
2254
	    return ( e ) ;
2255
	}
2256
	DESTROY_CONS_exp ( destroy, a, args, args ) ;
2257
    }
2258
 
2259
    /* Try operation again with increased overload depth */
2260
    overload_depth++ ;
2261
    e = make_func_exp ( a, args, 0 ) ;
2262
    overload_depth-- ;
2263
    return ( e ) ;
2264
}
2265
 
2266
 
2267
#endif /* LANGUAGE_CPP */