Subversion Repositories tendra.SVN

Rev

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

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