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 "graph_ops.h"
67
#include "hashid_ops.h"
68
#include "id_ops.h"
69
#include "itype_ops.h"
70
#include "member_ops.h"
71
#include "nspace_ops.h"
72
#include "tok_ops.h"
73
#include "type_ops.h"
74
#include "error.h"
75
#include "catalog.h"
76
#include "assign.h"
77
#include "basetype.h"
78
#include "cast.h"
79
#include "chktype.h"
80
#include "class.h"
81
#include "construct.h"
82
#include "convert.h"
83
#include "copy.h"
84
#include "declare.h"
85
#include "derive.h"
86
#include "destroy.h"
87
#include "dump.h"
88
#include "exception.h"
89
#include "expression.h"
90
#include "function.h"
91
#include "hash.h"
92
#include "identifier.h"
93
#include "initialise.h"
94
#include "instance.h"
95
#include "namespace.h"
96
#include "operator.h"
97
#include "option.h"
98
#include "overload.h"
99
#include "predict.h"
100
#include "syntax.h"
101
#include "template.h"
102
#include "tokdef.h"
103
#include "token.h"
104
#include "ustring.h"
105
#include "xalloc.h"
106
 
107
 
108
/*
109
    CANDIDATE LIST
110
 
111
    This variable gives the main candidate list used in overload resolution.
112
*/
113
 
7 7u83 114
CANDIDATE_LIST candidates = { NULL, 0, 0, NULL, 0 };
2 7u83 115
 
116
 
117
/*
118
    ADD A CANDIDATE TO A LIST
119
 
120
    This routine adds the candidate given by id, bid and kind to the
121
    candidate list p.
122
*/
123
 
7 7u83 124
static void
125
add_candidate(CANDIDATE_LIST *p, IDENTIFIER id, IDENTIFIER bid, int kind)
2 7u83 126
{
7 7u83 127
	CANDIDATE *q = p->elem;
128
	unsigned n = p->size;
129
	unsigned m = p->max_size;
130
	if (n >= m) {
131
		/* Increase size if necessary */
132
		m += 200;
133
		q = xrealloc_nof(q, CANDIDATE, m);
134
		p->elem = q;
135
		p->max_size = m;
136
	}
137
	q[n].func = id;
138
	q[n].base = bid;
139
	q[n].kind = kind;
140
	q[n].rank = RANK_NONE;
141
	q[n].convs = NULL;
142
	q[n].cond = NULL_exp;
143
	p->size = n + 1;
144
	return;
2 7u83 145
}
146
 
147
 
148
/*
149
    ADD AN IDENTIFIER TO A CANDIDATE LIST
150
 
151
    This routine adds the overloaded function identifier id to the candidate
152
    list given by p.  The kind argument is stored in the kind field of each
153
    candidate.  The table of candidate functions may need extending.  Note
154
    that the candidates are added in the reverse order in which they were
155
    declared.
156
*/
157
 
7 7u83 158
void
159
add_candidates(CANDIDATE_LIST *p, IDENTIFIER id, int over, int kind)
2 7u83 160
{
7 7u83 161
	unsigned tag = TAG_id(id);
162
	switch (tag) {
163
	case id_function_tag:
164
	case id_mem_func_tag:
165
	case id_stat_mem_func_tag:
166
		/* Functions */
167
		while (!IS_NULL_id(id)) {
168
			IDENTIFIER bid = DEREF_id(id_alias(id));
169
			add_candidate(p, id, bid, kind);
170
			if (!over) {
171
				break;
172
			}
173
			id = DEREF_id(id_function_etc_over(id));
174
		}
175
		break;
176
	case id_builtin_tag:
177
		/* Built-in operators */
178
		add_candidate(p, id, id, kind);
179
		break;
180
	case id_ambig_tag: {
181
		/* Ambiguous functions */
182
		LIST(IDENTIFIER) r = DEREF_list(id_ambig_ids(id));
183
		if (over) {
184
			over = DEREF_int(id_ambig_over(id));
185
		}
186
		while (!IS_NULL_list(r)) {
187
			IDENTIFIER rid = DEREF_id(HEAD_list(r));
188
			add_candidates(p, rid, over, kind);
189
			r = TAIL_list(r);
190
		}
191
		break;
2 7u83 192
	}
193
	}
7 7u83 194
	return;
2 7u83 195
}
196
 
197
 
198
/*
199
    LOOK UP A FUNCTION IN AN ARGUMENT NAMESPACE
200
 
201
    This routine looks up the function id in the namespace containing the
202
    type name cid.  Any functions found are added to the candidate list p.
203
*/
204
 
7 7u83 205
static IDENTIFIER
206
koenig_id(CANDIDATE_LIST *p, IDENTIFIER id, IDENTIFIER cid, int kind)
2 7u83 207
{
7 7u83 208
	if (!IS_NULL_id(cid)) {
209
		NAMESPACE cns = DEREF_nspace(id_parent(cid));
210
		if (!IS_NULL_nspace(cns)) {
211
			switch (TAG_nspace(cns)) {
212
			case nspace_named_tag:
213
			case nspace_unnamed_tag:
214
			case nspace_global_tag: {
215
				/* Look up identifier in parent class */
216
				HASHID nm = DEREF_hashid(id_name(id));
217
				MEMBER mem = search_member(cns, nm, 0);
218
				if (!IS_NULL_member(mem)) {
219
					IDENTIFIER fid =
220
					    DEREF_id(member_id(mem));
221
					if (!IS_NULL_id(fid) &&
222
					    !EQ_id(fid, id)) {
223
						add_candidates(p, fid, 1, kind);
224
						id = fid;
225
					}
226
				}
227
				break;
2 7u83 228
			}
7 7u83 229
			case nspace_ctype_tag: {
230
				/* Allow for nested classes */
231
				IDENTIFIER pid = DEREF_id(nspace_name(cns));
232
				id = koenig_id(p, id, pid, kind);
233
				break;
234
			}
235
			}
2 7u83 236
		}
237
	}
7 7u83 238
	return (id);
2 7u83 239
}
240
 
241
 
242
/*
243
    PERFORM ARGUMENT DEPENDENT NAME LOOK-UP FOR A CLASS
244
 
245
    This routine performs argument dependent name look-up for the function
246
    id and the argument class type ct.
247
*/
248
 
7 7u83 249
static IDENTIFIER
250
koenig_class(CANDIDATE_LIST *p, IDENTIFIER id, CLASS_TYPE ct, int kind)
2 7u83 251
{
7 7u83 252
	TYPE form = DEREF_type(ctype_form(ct));
253
	if (IS_NULL_type(form)) {
254
		IDENTIFIER cid = DEREF_id(ctype_name(ct));
255
		GRAPH gr = DEREF_graph(ctype_base(ct));
256
		LIST(GRAPH) br = DEREF_list(graph_tails(gr));
257
		id = koenig_id(p, id, cid, kind);
258
		while (!IS_NULL_list(br)) {
259
			GRAPH gs = DEREF_graph(HEAD_list(br));
260
			CLASS_TYPE cs = DEREF_ctype(graph_head(gs));
261
			id = koenig_class(p, id, cs, kind);
262
			br = TAIL_list(br);
263
		}
264
	} else {
265
		id = koenig_candidates(p, id, form, kind);
2 7u83 266
	}
7 7u83 267
	return (id);
2 7u83 268
}
269
 
270
 
271
/*
272
    PERFORM ARGUMENT DEPENDENT NAME LOOK-UP FOR A TOKEN ARGUMENT
273
 
274
    This routine performs argument dependent name look-up for the function
275
    id and the token argument tok.
276
*/
277
 
7 7u83 278
static IDENTIFIER
279
koenig_token(CANDIDATE_LIST *p, IDENTIFIER id, TOKEN tok, int kind)
2 7u83 280
{
7 7u83 281
	if (!IS_NULL_tok(tok)) {
282
		ASSERT(ORDER_tok == 10);
283
		switch (TAG_tok(tok)) {
284
		case tok_type_tag: {
285
			TYPE t = DEREF_type(tok_type_value(tok));
286
			id = koenig_candidates(p, id, t, kind);
287
			break;
288
		}
289
		case tok_class_tag: {
290
			IDENTIFIER tid = DEREF_id(tok_class_value(tok));
291
			id = koenig_id(p, id, tid, kind);
292
			break;
293
		}
294
		}
2 7u83 295
	}
7 7u83 296
	return (id);
2 7u83 297
}
298
 
299
 
300
/*
301
    PERFORM ARGUMENT DEPENDENT NAME LOOK-UP
302
 
303
    This routine performs argument dependent name look-up for the function
304
    id and the argument type t, adding any candidates found to the list p.
305
*/
306
 
7 7u83 307
IDENTIFIER
308
koenig_candidates(CANDIDATE_LIST *p, IDENTIFIER id, TYPE t, int kind)
2 7u83 309
{
7 7u83 310
	if (!IS_NULL_type(t)) {
311
		ASSERT(ORDER_type == 18);
312
		switch (TAG_type(t)) {
313
		case type_ptr_tag:
314
		case type_ref_tag: {
315
			TYPE s = DEREF_type(type_ptr_etc_sub(t));
316
			id = koenig_candidates(p, id, s, kind);
317
			break;
2 7u83 318
		}
7 7u83 319
		case type_ptr_mem_tag: {
320
			CLASS_TYPE cs = DEREF_ctype(type_ptr_mem_of(t));
321
			TYPE s = DEREF_type(type_ptr_mem_sub(t));
322
			id = koenig_class(p, id, cs, kind);
323
			id = koenig_candidates(p, id, s, kind);
324
			break;
2 7u83 325
		}
7 7u83 326
		case type_func_tag: {
327
			TYPE r = DEREF_type(type_func_ret(t));
328
			LIST(TYPE) q = DEREF_list(type_func_ptypes(t));
329
			id = koenig_candidates(p, id, r, kind);
330
			while (!IS_NULL_list(q)) {
331
				TYPE s = DEREF_type(HEAD_list(q));
332
				id = koenig_candidates(p, id, s, kind);
333
				q = TAIL_list(q);
334
			}
335
			break;
2 7u83 336
		}
7 7u83 337
		case type_array_tag: {
338
			TYPE s = DEREF_type(type_array_sub(t));
339
			id = koenig_candidates(p, id, s, kind);
340
			break;
341
		}
342
		case type_compound_tag: {
343
			CLASS_TYPE ct = DEREF_ctype(type_compound_defn(t));
344
			id = koenig_class(p, id, ct, kind);
345
			break;
346
		}
347
		case type_enumerate_tag: {
348
			ENUM_TYPE et = DEREF_etype(type_enumerate_defn(t));
349
			IDENTIFIER eid = DEREF_id(etype_name(et));
350
			id = koenig_id(p, id, eid, kind);
351
			break;
352
		}
353
		case type_token_tag: {
354
			IDENTIFIER tid = DEREF_id(type_token_tok(t));
355
			LIST(TOKEN) args = DEREF_list(type_token_args(t));
356
			if (IS_id_token(tid)) {
357
				tid = DEREF_id(id_token_alt(tid));
358
			}
359
			id = koenig_id(p, id, tid, kind);
360
			while (!IS_NULL_list(args)) {
361
				TOKEN arg = DEREF_tok(HEAD_list(args));
362
				id = koenig_token(p, id, arg, kind);
363
				args = TAIL_list(args);
364
			}
365
			break;
366
		}
367
		case type_templ_tag: {
368
			TYPE s = DEREF_type(type_templ_defn(t));
369
			id = koenig_candidates(p, id, s, kind);
370
			break;
371
		}
372
		}
2 7u83 373
	}
7 7u83 374
	return (id);
2 7u83 375
}
376
 
377
 
378
/*
379
    FREE A LIST OF CANDIDATES
380
 
381
    This routine frees the elements of the candidate list p.
382
*/
383
 
7 7u83 384
void
385
free_candidates(CANDIDATE_LIST *p)
2 7u83 386
{
7 7u83 387
	xfree_nof(p->elem);
388
	xfree_nof(p->convs);
389
	p->elem = NULL;
390
	p->size = 0;
391
	p->max_size = 0;
392
	p->convs = NULL;
393
	p->nconvs = 0;
394
	return;
2 7u83 395
}
396
 
397
 
398
/*
399
    SWAP PARAMETER TYPE FIELDS FOR A FUNCTION
400
 
401
    Overload resolution is based on the mtypes field of the function type.
402
    This routine can be used to force resolution on the ptypes field by
403
    swapping the two fields for the overloaded function identifier id.
404
    Note that after overload resolution the fields should be swapped back.
405
*/
406
 
7 7u83 407
void
408
swap_ptypes(IDENTIFIER id)
2 7u83 409
{
7 7u83 410
	LIST(TYPE) ptypes;
411
	LIST(TYPE) mtypes;
412
	TYPE fn = DEREF_type(id_function_etc_type(id));
413
	while (IS_type_templ(fn)) {
414
		/* Allow for template functions */
415
		fn = DEREF_type(type_templ_defn(fn));
416
	}
417
	ptypes = DEREF_list(type_func_ptypes(fn));
418
	mtypes = DEREF_list(type_func_mtypes(fn));
419
	if (!EQ_list(ptypes, mtypes)) {
420
		COPY_list(type_func_ptypes(fn), mtypes);
421
		COPY_list(type_func_mtypes(fn), ptypes);
422
	}
423
	return;
2 7u83 424
}
425
 
426
 
427
/*
428
    SWAP PARAMETER TYPE FIELDS FOR CANDIDATES FUNCTIONS
429
 
430
    This routine swaps the parameter type fields for all the constructor
431
    candidates in p, starting with the nth entry.
432
*/
433
 
7 7u83 434
void
435
swap_candidates(CANDIDATE_LIST *p, unsigned n)
2 7u83 436
{
7 7u83 437
	unsigned i, m = p->size;
438
	for (i = n; i < m; i++) {
439
		CANDIDATE *r = p->elem + i;
440
		if (r->kind == KIND_CONSTR) {
441
			swap_ptypes(r->base);
442
		}
2 7u83 443
	}
7 7u83 444
	return;
2 7u83 445
}
446
 
447
 
448
/*
449
    LIST CANDIDATES FOR ERROR REPORTING
450
 
451
    This routine appends messages listing all the candidates from the list
452
    p of rank at least rank to the end of the error message err.  No action
453
    is taken if err is the null error.
454
*/
455
 
7 7u83 456
ERROR
457
list_candidates(ERROR err, CANDIDATE_LIST *p, unsigned rank)
2 7u83 458
{
7 7u83 459
	if (!IS_NULL_err(err)) {
460
		ERROR err2 = ERR_over_match_viable_list();
461
		if (!IS_NULL_err(err2)) {
462
			unsigned m = 0;
463
			CANDIDATE *q = p->elem;
464
			unsigned n = p->size;
465
			err = concat_error(err, err2);
466
			while (n) {
467
				CANDIDATE *r = q + (--n);
468
				if (r->rank >= rank) {
469
					/* List viable candidates */
470
					IDENTIFIER bid = r->base;
471
					DECL_SPEC ds =
472
					    DEREF_dspec(id_storage(bid));
473
					if (!(ds & dspec_register)) {
474
						IDENTIFIER id = r->func;
475
						if (IS_id_builtin(id)) {
476
							/* Dump dummy built-in
477
							 * operators */
478
							if (do_dump) {
479
								dump_builtin(id);
480
							}
481
						}
482
						m++;
483
						err2 = ERR_fail_list_item(m, id, id_loc(id));
484
						err = concat_error(err, err2);
485
						ds |= dspec_register;
486
						COPY_dspec(id_storage(bid), ds);
487
					}
488
				}
2 7u83 489
			}
7 7u83 490
			if (m) {
491
				n = p->size;
492
				while (n) {
493
					/* Clear marks */
494
					CANDIDATE *r = q + (--n);
495
					IDENTIFIER bid = r->base;
496
					DECL_SPEC ds =
497
					    DEREF_dspec(id_storage(bid));
498
					if (ds & dspec_register) {
499
						ds &= ~dspec_register;
500
						COPY_dspec(id_storage(bid), ds);
501
					}
502
				}
503
			}
504
			err = concat_error(err, ERR_fail_list_end(m));
2 7u83 505
		}
506
	}
7 7u83 507
	return (err);
2 7u83 508
}
509
 
510
 
511
/*
512
    OVERLOAD MATCHING INFORMATION
513
 
514
    These variables are used to store information gained during overload
515
    resolution.  If match_no_args is nonzero it gives the number of
516
    candidates which take the right number of arguments, otherwise if it
517
    is zero it indicates that either there are no such functions or there
518
    is only one candidate.  Similarly match_no_viable gives the number of
519
    viable candidates (counting viable templates twice).
520
*/
521
 
7 7u83 522
unsigned match_no_args = 0;
523
unsigned match_no_viable = 0;
524
static unsigned viable_templates = 0;
525
int resolved_kind = KIND_FUNC;
2 7u83 526
 
527
 
528
/*
529
    IS A CANDIDATE FUNCTION PLAUSIBLE?
530
 
531
    A candidate function is plausible for a list of arguments if it accepts
532
    that number of arguments.  This routine tests whether the candidate r
533
    is plausible.  It returns the absolute difference between the number
534
    of arguments and the number of parameters (i.e. zero for a plausible
535
    candidate).
536
*/
537
 
7 7u83 538
static unsigned
539
plausible_candidate(CANDIDATE *r, unsigned nargs)
2 7u83 540
{
7 7u83 541
	IDENTIFIER id = r->base;
542
	if (IS_id_builtin(id)) {
543
		/* Built-in candidates */
544
		LIST(TYPE) mtypes = DEREF_list(id_builtin_ptypes(id));
545
		unsigned npars = LENGTH_list(mtypes);
546
		if (nargs == npars) {
547
			/* Equal numbers of arguments and parameters */
548
			return (0);
549
		} else if (nargs > npars) {
550
			/* More arguments than parameters */
551
			return (nargs - npars);
552
		} else {
553
			/* Less arguments than parameters */
554
			return (npars - nargs);
555
		}
2 7u83 556
	} else {
7 7u83 557
		/* Function candidates */
558
		unsigned npars;
559
		LIST(TYPE) mtypes;
560
		TYPE fn = DEREF_type(id_function_etc_type(id));
561
		while (IS_type_templ(fn)) {
562
			/* Allow for template functions */
563
			fn = DEREF_type(type_templ_defn(fn));
564
		}
565
		mtypes = DEREF_list(type_func_mtypes(fn));
566
		npars = LENGTH_list(mtypes);
567
		if (nargs == npars) {
568
			/* Equal numbers of arguments and parameters */
569
			return (0);
570
		} else if (nargs > npars) {
571
			/* More arguments than parameters */
572
			int ell = DEREF_int(type_func_ellipsis(fn));
573
			if (ell) {
574
				/* Match with ellipsis */
575
				return (0);
576
			}
577
			return (nargs - npars);
578
		} else {
579
			/* Less arguments than parameters */
580
			unsigned margs = min_no_args(fn);
581
			if (nargs >= margs) {
582
				/* Match with default arguments */
583
				return (0);
584
			}
585
			return (margs - nargs);
586
		}
2 7u83 587
	}
7 7u83 588
	/* NOTREACHED */
2 7u83 589
}
590
 
591
 
592
/*
593
    INHERITANCE MATCHING FLAG
594
 
595
    This flag may be set to true to make the match for the implicit
596
    this parameter for an inherited function as good as a match for
597
    a non-inherited function from the point of view of function
598
    overloading.
599
*/
600
 
7 7u83 601
int match_this = 0;
2 7u83 602
 
603
 
604
/*
605
    IS A PLAUSIBLE CANDIDATE FUNCTION VIABLE?
606
 
607
    A plausible candidate function is viable for a list of arguments if
608
    there is an implicit conversion sequence for each argument to the
609
    corresponding parameter type.  This routine tests whether the plausible
610
    candidate r is viable, building up the corresponding list of conversions.
611
    It returns the number of arguments for which a sequence exists, plus
612
    one.
613
*/
614
 
7 7u83 615
static unsigned
616
viable_candidate(CANDIDATE *r, LIST(EXP) args, TYPE ret)
2 7u83 617
{
7 7u83 618
	TYPE rtype;
619
	int bind = 0;
620
	unsigned conv;
621
	unsigned match = 1;
622
	LIST(TYPE) mtypes;
623
	IDENTIFIER id = r->base;
624
	CONVERSION *convs = r->convs;
625
	HASHID nm = DEREF_hashid(id_name(id));
2 7u83 626
 
7 7u83 627
	/* Extract type information */
628
	if (IS_id_builtin(id)) {
629
		if (IS_hashid_op(nm)) {
630
			int op = DEREF_int(hashid_op_lex(nm));
631
			if (op == lex_assign) {
632
				bind = 1;
633
			}
2 7u83 634
		}
7 7u83 635
		mtypes = DEREF_list(id_builtin_ptypes(id));
636
		rtype = DEREF_type(id_builtin_ret(id));
2 7u83 637
	} else {
7 7u83 638
		LIST(TYPE) ptypes;
639
		TYPE fn = DEREF_type(id_function_etc_type(id));
640
		if (IS_type_templ(fn)) {
641
			/* Allow for template functions */
642
			ERROR err = NULL_err;
643
			if (r->kind == KIND_CONSTR) {
644
				/* Deal with swapped candidates */
645
				IDENTIFIER tid;
646
				EXP a = NULL_exp;
647
				LIST(EXP) dargs;
648
				swap_ptypes(id);
649
				CONS_exp(a, args, dargs);
650
				tid = deduce_args(id, dargs, 2, 0, 1, &err);
651
				DESTROY_CONS_exp(destroy, a, args, dargs);
652
				UNUSED(a);
653
				if (IS_NULL_id(tid)) {
654
					swap_ptypes(id);
655
					return (0);
656
				}
657
				swap_ptypes(tid);
658
				id = tid;
659
			} else {
660
				/* Deal with normal candidates */
661
				id = deduce_args(id, args, 2, 0, 1, &err);
662
				if (IS_NULL_id(id)) {
663
					return (0);
664
				}
665
			}
666
			fn = DEREF_type(id_function_etc_type(id));
667
			if (!IS_type_func(fn)) {
668
				return (0);
669
			}
670
			viable_templates++;
671
			r->func = id;
672
			r->base = id;
673
		}
674
		mtypes = DEREF_list(type_func_mtypes(fn));
675
		ptypes = DEREF_list(type_func_ptypes(fn));
676
		if (IS_hashid_constr(nm)) {
677
			rtype = DEREF_type(hashid_constr_type(nm));
2 7u83 678
		} else {
7 7u83 679
			rtype = DEREF_type(type_func_ret(fn));
2 7u83 680
		}
7 7u83 681
		if (!EQ_list(mtypes, ptypes)) {
682
			/* Member function */
683
			if (!IS_NULL_list(ptypes)) {
684
				ptypes = TAIL_list(ptypes);
685
			}
686
			if (EQ_list(mtypes, ptypes)) {
687
				/* Parameter types have been swapped */
688
				/* EMPTY */
689
			} else {
690
				/* Matching implicit this parameter */
691
				if (match_this) {
692
					/* Force exact match */
693
					bind = 2;
694
				} else {
695
					IDENTIFIER fid = r->func;
696
					DECL_SPEC ds =
697
					    DEREF_dspec(id_storage(fid));
698
					if ((ds & dspec_inherit) &&
699
					    !(ds & dspec_alias)) {
700
						/* Inherited member function */
701
						bind = 3;
702
					} else {
703
						bind = 2;
704
					}
705
				}
706
			}
707
		}
2 7u83 708
	}
709
 
7 7u83 710
	/* Check for implicit conversion sequences */
711
	while (!IS_NULL_list(mtypes) && !IS_NULL_list(args)) {
712
		conv = convs->rank;
713
		if (conv == CONV_NONE) {
714
			/* Recalculate if necessary */
715
			EXP a = DEREF_exp(HEAD_list(args));
716
			TYPE t = DEREF_type(HEAD_list(mtypes));
717
			convs->to = t;
718
			if (IS_NULL_exp(a)) {
719
				/* A null expression counts as an exact match */
720
				convs->from = t;
721
				conv = CONV_EXACT;
722
			} else {
723
				/* Calculate conversion sequence */
724
				TYPE s = DEREF_type(exp_type(a));
725
				convs->from = s;
726
				if (IS_NULL_type(ret)) {
727
					conv = convert_seq(convs, a, bind, 0);
728
				} else {
729
					conv =
730
					    std_convert_seq(convs, a, bind, 0);
731
				}
732
				convs->from = s;
733
				convs->to = t;
734
			}
2 7u83 735
		}
7 7u83 736
		if (conv != CONV_NONE) {
737
			match++;
738
		}
739
		convs->rank = conv;
740
		convs++;
741
		args = TAIL_list(args);
742
		mtypes = TAIL_list(mtypes);
743
		bind = 0;
2 7u83 744
	}
745
 
7 7u83 746
	/* Remaining arguments match with ellipsis */
747
	while (!IS_NULL_list(args)) {
748
		convs->rank = CONV_ELLIPSIS;
749
		convs++;
750
		match++;
751
		args = TAIL_list(args);
752
	}
2 7u83 753
 
7 7u83 754
	/* Check for return conversion sequences */
755
	if (!IS_NULL_type(ret)) {
756
		conv = convs->rank;
757
		if (conv == CONV_NONE) {
758
			convs->to = ret;
759
			convs->from = rtype;
760
			conv = std_convert_seq(convs, NULL_exp, 0, 0);
761
			convs->from = rtype;
762
			convs->to = ret;
763
		}
764
	} else {
765
		conv = CONV_ELLIPSIS;
2 7u83 766
	}
7 7u83 767
	convs->rank = conv;
768
	return (match);
2 7u83 769
}
770
 
771
 
772
/*
773
    COMPARE TWO FUNCTIONS
774
 
775
    This routine compares the candidate functions rid and sid to determine
776
    which is better under the template specialisation rules.  It returns
777
    1 if rid is better than sid, 2 if sid is better than rid, and 0 otherwise.
778
*/
779
 
7 7u83 780
static int
781
compare_funcs(IDENTIFIER rid, IDENTIFIER sid)
2 7u83 782
{
7 7u83 783
	int res = 0;
784
	if (EQ_id(rid, sid)) {
785
		/* Select first if equal */
786
		res = 1;
2 7u83 787
	} else {
7 7u83 788
		if (IS_NULL_id(rid)) {
789
			res = 2;
790
		} else if (IS_NULL_id(sid)) {
791
			res = 1;
2 7u83 792
		} else {
7 7u83 793
			rid = find_template(rid, 0);
794
			sid = find_template(sid, 0);
795
			if (!IS_NULL_id(rid)) {
796
				if (!IS_NULL_id(sid)) {
797
					/* Both template functions */
798
					res = compare_specs(rid, sid);
799
				} else {
800
					/* rid is a template function */
801
					res = 2;
802
				}
803
			} else if (!IS_NULL_id(sid)) {
804
				/* sid is a template function */
805
				res = 1;
806
			}
2 7u83 807
		}
808
	}
7 7u83 809
	return (res);
2 7u83 810
}
811
 
812
 
813
/*
814
    COMPARE TWO CANDIDATE FUNCTIONS
815
 
816
    This routine compares the candidate functions r and s for the argument
817
    list args and return type ret.  It returns 1 if r is better than s, 2 if
818
    s is better than r, and some other value otherwise.  Better in this
819
    sense means at least as good for all arguments, plus strictly better
820
    in at least one argument or the return type.
821
*/
822
 
7 7u83 823
static int
824
compare_candidates(CANDIDATE *r, CANDIDATE *s, LIST(EXP) args, TYPE ret)
2 7u83 825
{
7 7u83 826
	int res = 0;
827
	CONVERSION *cr = r->convs;
828
	CONVERSION *cs = s->convs;
2 7u83 829
 
7 7u83 830
	/* Compare each argument in turn */
831
	while (!IS_NULL_list(args)) {
832
		int cmp = compare_seq(cr, cs);
833
		if (cmp == 1) {
834
			/* r better in this argument */
835
			if (res == 2) {
836
				return (0);
837
			}
838
			res = 1;
839
		} else if (cmp == 2) {
840
			/* s better in this argument */
841
			if (res == 1) {
842
				return (0);
843
			}
844
			res = 2;
845
		}
846
		cr++;
847
		cs++;
848
		args = TAIL_list(args);
2 7u83 849
	}
850
 
7 7u83 851
	/* Tie breakers */
852
	if (res == 0) {
853
		res = compare_funcs(r->base, s->base);
854
		if (res == 0 && !IS_NULL_type(ret)) {
855
			/* Resolve on return type */
856
			res = compare_seq(cr, cs);
857
		}
2 7u83 858
	}
7 7u83 859
	return (res);
2 7u83 860
}
861
 
862
 
863
/*
864
    OVERLOADED FUNCTION RESOLUTION
865
 
866
    This routine selects the best match from the list of overloaded function
867
    given by p based on the argument list args and the return type ret (which
868
    may be the null type).  The list of candidate functions will not be empty.
869
    If replay is true then the conversion sequences have already been
870
    calculated and only the final tournament to select the best viable
871
    candidate is necessary.
872
*/
873
 
7 7u83 874
CANDIDATE *
875
resolve_overload(CANDIDATE_LIST *p, LIST(EXP) args, TYPE ret, int replay)
2 7u83 876
{
7 7u83 877
	unsigned round;
878
	unsigned match = 0;
879
	CANDIDATE *q = p->elem;
880
	CANDIDATE *best = q;
881
	CANDIDATE *beat = NULL;
882
	unsigned i, n = p->size;
2 7u83 883
 
7 7u83 884
	if (!replay) {
885
		/* Eliminate all non-viable candidates */
886
		unsigned nargs;
887
		unsigned margs;
888
		unsigned mconvs;
889
		unsigned best_margs;
890
		unsigned nconvs = p->nconvs;
891
		CONVERSION *convs = p->convs;
2 7u83 892
 
7 7u83 893
		/* Check for plausible candidates */
894
		nargs = LENGTH_list(args);
895
		best_margs = nargs + 1;
896
		for (i = 0; i < n; i++) {
897
			CANDIDATE *r = q + i;
898
			if (r->rank != RANK_IGNORE) {
899
				margs = plausible_candidate(r, nargs);
900
				if (margs == 0) {
901
					/* Plausible candidate */
902
					r->rank = RANK_ARGS;
903
					best = r;
904
					match++;
905
				} else if (match == 0 && margs <= best_margs) {
906
					/* Most nearly plausible candidate so
907
					 * far */
908
					best_margs = margs;
909
					best = r;
910
				}
911
			}
2 7u83 912
		}
7 7u83 913
		match_no_args = match;
914
		if (match == 0) {
915
			/* No plausible candidates - return most nearly
916
			 * plausible */
917
			match_no_viable = 0;
918
			return (best);
919
		}
2 7u83 920
 
7 7u83 921
		/* Allocate room for conversion ranks */
922
		nargs++;
923
		mconvs = match * nargs;
924
		if (mconvs >= nconvs) {
925
			nconvs = mconvs + 200;
926
			convs = xrealloc_nof(convs, CONVERSION, nconvs);
927
			p->nconvs = nconvs;
928
			p->convs = convs;
929
		}
2 7u83 930
 
7 7u83 931
		/* Check for viable candidates */
932
		match = 0;
933
		best_margs = 0;
934
		viable_templates = 0;
935
		for (i = 0; i < n; i++) {
936
			CANDIDATE *r = q + i;
937
			if (r->rank == RANK_ARGS) {
938
				/* Only check plausible candidates */
939
				unsigned m;
940
				r->convs = convs;
941
				for (m = 0; m < nargs; m++) {
942
					convs->rank = CONV_NONE;
943
					convs++;
944
				}
945
				margs = viable_candidate(r, args, ret);
946
				if (margs == nargs) {
947
					/* Viable candidate */
948
					r->rank = RANK_VIABLE;
949
					best = r;
950
					match++;
951
				} else if (match == 0 && margs >= best_margs) {
952
					/* Most nearly viable candidate so
953
					 * far */
954
					best_margs = margs;
955
					best = r;
956
				}
957
			}
2 7u83 958
		}
7 7u83 959
		match_no_viable = match + viable_templates;
960
		if (match == 0) {
961
			/* No viable candidates - return most nearly viable */
962
			return (best);
2 7u83 963
		}
7 7u83 964
		if (match == 1) {
965
			/* Exactly one viable candidate - must be the winner */
966
			best->rank = RANK_BEST;
967
			return (best);
968
		}
969
	} else {
970
		/* Replay - pick a viable candidate */
971
		for (i = 0; i < n; i++) {
972
			CANDIDATE *r = q + i;
973
			if (r->rank >= RANK_VIABLE) {
974
				r->rank = RANK_VIABLE;
975
				best = r;
976
				match++;
977
			}
978
		}
2 7u83 979
	}
980
 
7 7u83 981
	/* Play tournament among viable candidates */
982
	round = RANK_VIABLE;
983
	while (match > 1) {
984
		CANDIDATE *s = NULL;
985
		match = 0;
986
		for (i = 0; i < n; i++) {
987
			CANDIDATE *r = q + i;
988
			if (r->rank == round) {
989
				/* Only check those through to this round */
990
				if (s == NULL) {
991
					/* r is first candidate in contest */
992
					s = r;
993
				} else {
994
					/* r is second candidate in contest */
995
					int cmp =
996
					    compare_candidates(s, r, args, ret);
997
					if (cmp == 1) {
998
						/* s wins */
999
						s->rank = round + 1;
1000
						r->rank = RANK_VIABLE;
1001
						best = s;
1002
						beat = r;
1003
						match++;
1004
					} else if (cmp == 2) {
1005
						/* r wins */
1006
						r->rank = round + 1;
1007
						s->rank = RANK_VIABLE;
1008
						best = r;
1009
						beat = s;
1010
						match++;
1011
					} else {
1012
						/* Draw - both eliminated */
1013
						s->rank = RANK_VIABLE;
1014
						r->rank = RANK_VIABLE;
1015
					}
1016
					s = NULL;
1017
				}
1018
			}
2 7u83 1019
		}
7 7u83 1020
		if (s != NULL) {
1021
			/* Bye - s goes through */
1022
			s->rank = round + 1;
1023
			best = s;
1024
			match++;
1025
		}
1026
		round++;
2 7u83 1027
	}
1028
 
7 7u83 1029
	/* Examine the tournament winner */
1030
	if (match == 1) {
1031
		/* Tournament winner must now beat all the others */
1032
		best->rank = RANK_BEST;
1033
		for (i = 0; i < n; i++) {
1034
			CANDIDATE *r = q + i;
1035
			if (r->rank >= RANK_VIABLE) {
1036
				if (r == best) {
1037
					/* Don't compare against self */
1038
					/* EMPTY */
1039
				} else if (r == beat) {
1040
					/* This has already been beaten */
1041
					/* EMPTY */
1042
				} else {
1043
					int cmp =
1044
					    compare_candidates(best, r, args,
1045
							       ret);
1046
					if (cmp != 1) {
1047
						/* r is at least as good as
1048
						 * best */
1049
						best->rank = RANK_VIABLE;
1050
						break;
1051
					}
1052
				}
1053
			}
2 7u83 1054
		}
7 7u83 1055
	} else {
1056
		/* No clear tournament winner */
1057
		if (best->rank >= RANK_VIABLE) {
1058
			best->rank = RANK_VIABLE;
1059
		}
2 7u83 1060
	}
7 7u83 1061
	return (best);
2 7u83 1062
}
1063
 
1064
 
1065
/*
1066
    FIND A LIST OF POSSIBLE TYPE VALUES
1067
 
1068
    This routine constructs a list of the possible resolutions for the
1069
    target dependent type t or its promotion.  In the latter case prom
1070
    is set to true.  If neither t or its promotion is target dependent
1071
    then the empty list is returned.
1072
*/
1073
 
7 7u83 1074
static LIST(TYPE)
1075
possible_types(TYPE t, int *prom)
2 7u83 1076
{
7 7u83 1077
	LIST(TYPE) r = NULL_list(TYPE);
1078
	switch (TAG_type(t)) {
1079
	case type_integer_tag: {
1080
		/* Integral type */
1081
		INT_TYPE it = DEREF_itype(type_integer_rep(t));
1082
		r = DEREF_list(itype_cases(it));
1083
		if (LENGTH_list(r) == 1) {
1084
			/* Type is not target dependent */
1085
			t = DEREF_type(itype_prom(it));
1086
			it = DEREF_itype(type_integer_rep(t));
1087
			r = DEREF_list(itype_cases(it));
1088
			if (LENGTH_list(r) == 1) {
1089
				/* Promoted type is not target dependent */
1090
				r = NULL_list(TYPE);
1091
			} else {
1092
				*prom = 1;
1093
			}
2 7u83 1094
		}
7 7u83 1095
		break;
2 7u83 1096
	}
7 7u83 1097
	case type_bitfield_tag:
1098
	case type_enumerate_tag:
1099
		/* Enumeration and bitfield types */
1100
		t = promote_type(t);
1101
		r = possible_types(t, prom);
1102
		*prom = 1;
1103
		break;
2 7u83 1104
	}
7 7u83 1105
	return (r);
2 7u83 1106
}
1107
 
1108
 
1109
/*
1110
    CHECK FOR TARGET DEPENDENT OVERLOADED FUNCTION RESOLUTIONS
1111
 
1112
    This routine is called when an ambiguous overload resolution is
1113
    detected to determine which of the viable candidates to proceed with.
1114
    The parameters are as in resolve_overload.
1115
*/
1116
 
7 7u83 1117
CANDIDATE *
1118
resolve_ambiguous(CANDIDATE_LIST *p, LIST(EXP) args, TYPE ret, int depth)
2 7u83 1119
{
7 7u83 1120
    CANDIDATE *q = p->elem;
1121
    CANDIDATE *best = q;
1122
    unsigned i, n = p->size;
1123
    unsigned overall_rank = RANK_VIABLE;
2 7u83 1124
 
1125
    /* Check for arguments of error type */
7 7u83 1126
    if (depth == 0) {
1127
	LIST(EXP) a = args;
1128
	while (!IS_NULL_list(a)) {
1129
	    EXP e = DEREF_exp(HEAD_list(a));
1130
	    if (!IS_NULL_exp(e)) {
1131
		TYPE t = DEREF_type(exp_type(e));
1132
		if (IS_type_error(t)) {
2 7u83 1133
		    /* Error types are ignored */
7 7u83 1134
		    overall_rank = RANK_BEST;
1135
		    break;
2 7u83 1136
		}
1137
	    }
7 7u83 1138
	    a = TAIL_list(a);
2 7u83 1139
	}
1140
    }
1141
 
1142
    /* Check for target dependent resolutions */
7 7u83 1143
    if (overall_rank != RANK_BEST) {
1144
	unsigned na = 0;
1145
	LIST(EXP) a = args;
1146
	EXP saved[100];
1147
	EXP *save = saved;
1148
	if (n >= 100) {
1149
		save = xmalloc_nof(EXP, n);
1150
	}
1151
	for (i = 0; i < n; i++)save[i] = NULL_exp;
1152
	while (!IS_NULL_list(a)) {
1153
	    EXP e = DEREF_exp(HEAD_list(a));
1154
	    if (!IS_NULL_exp(e)) {
1155
		int prom = 0;
1156
		int have_match = 0;
1157
		TYPE t = DEREF_type(exp_type(e));
1158
		LIST(TYPE) pt = possible_types(t, &prom);
1159
		if (!IS_NULL_list(pt)) {
1160
		    CANDIDATE *r;
1161
		    LIST(TYPE) ps = pt;
1162
		    unsigned new_rank = RANK_TARGET;
1163
		    if (prom) {
2 7u83 1164
			/* Only use promoted types if no exact match */
7 7u83 1165
			for (i = 0; i < n; i++) {
1166
			    r = q + i;
1167
			    if (r->rank >= RANK_VIABLE) {
1168
				if (r->convs[na].rank == CONV_EXACT) {
1169
				    ps = NULL_list(TYPE);
1170
				    new_rank = overall_rank;
1171
				    break;
2 7u83 1172
				}
1173
			    }
1174
			}
1175
		    }
7 7u83 1176
		    overall_rank = new_rank;
1177
		    while (!IS_NULL_list(ps)) {
1178
			TYPE u = t;
1179
			TYPE s = DEREF_type(HEAD_list(ps));
1180
			COPY_type(exp_type(e), s);
1181
			for (i = 0; i < n; i++) {
1182
			    r = q + i;
1183
			    if (r->rank >= RANK_VIABLE) {
2 7u83 1184
				/* Recalculate conversions */
7 7u83 1185
				r->convs[na].rank = CONV_NONE;
1186
				IGNORE viable_candidate(r, args, ret);
1187
				r->rank = RANK_VIABLE;
2 7u83 1188
			    }
1189
			}
7 7u83 1190
			r = resolve_overload(p, args, ret, 1);
1191
			if (r->rank == RANK_VIABLE) {
2 7u83 1192
			    /* Check further arguments */
7 7u83 1193
			    for (i = 0; i < n; i++) {
2 7u83 1194
				/* Save conditions */
7 7u83 1195
				save[i] = q[i].cond;
1196
				q[i].cond = NULL_exp;
2 7u83 1197
			    }
7 7u83 1198
			    r = resolve_ambiguous(p, args, ret, 1);
1199
			    for (i = 0; i < n; i++) {
2 7u83 1200
				/* Restore conditions */
7 7u83 1201
				EXP c = q[i].cond;
1202
				q[i].cond = save[i];
1203
				save[i] = c;
2 7u83 1204
			    }
1205
			}
7 7u83 1206
			COPY_type(exp_type(e), t);
1207
			if (r->rank >= RANK_TARGET) {
2 7u83 1208
			    /* Create condition for resolution */
7 7u83 1209
			    if (prom) {
1210
				    u = promote_type(u);
1211
			    }
1212
			    for (i = 0; i < n; i++) {
1213
				r = q + i;
1214
				if (r->rank >= RANK_TARGET) {
1215
				    EXP c1, c2;
1216
				    TYPE ti = type_sint;
1217
				    TYPE tb = type_bool;
1218
				    NTEST nt = ntest_eq;
1219
				    MAKE_exp_rtti_no(ti, u, c1);
1220
				    MAKE_exp_rtti_no(ti, s, c2);
1221
				    MAKE_exp_compare(tb, nt, c1, c2, c1);
1222
				    c2 = save[i];
1223
				    if (!IS_NULL_exp(c2)) {
1224
					MAKE_exp_log_and(tb, c1, c2, c1);
2 7u83 1225
				    }
7 7u83 1226
				    c2 = r->cond;
1227
				    if (!IS_NULL_exp(c2)) {
1228
					MAKE_exp_log_or(tb, c1, c2, c1);
2 7u83 1229
				    }
7 7u83 1230
				    r->cond = c1;
1231
				    have_match = 1;
2 7u83 1232
				}
7 7u83 1233
				save[i] = NULL_exp;
2 7u83 1234
			    }
7 7u83 1235
			} else if (option(OPT_overload_strict)) {
2 7u83 1236
			    /* Don't have match in all cases */
7 7u83 1237
			    have_match = 0;
1238
			    break;
2 7u83 1239
			}
7 7u83 1240
			ps = TAIL_list(ps);
2 7u83 1241
		    }
7 7u83 1242
		    if (!have_match) {
1243
			overall_rank = RANK_VIABLE;
1244
			break;
2 7u83 1245
		    }
1246
		}
1247
	    }
7 7u83 1248
	    a = TAIL_list(a);
1249
	    na++;
2 7u83 1250
	}
7 7u83 1251
	if (save != saved) {
1252
		xfree_nof(save);
1253
	}
2 7u83 1254
    }
1255
 
1256
    /* Select last viable candidate */
7 7u83 1257
    for (i = 0; i < n; i++) {
1258
	CANDIDATE *r = q + i;
1259
	if (r->rank >= RANK_VIABLE) {
1260
	    if (overall_rank == RANK_TARGET) {
1261
		if (IS_NULL_exp(r->cond)) {
1262
		    if (depth == 0)r->rank = RANK_ARGS;
2 7u83 1263
		} else {
7 7u83 1264
		    r->rank = overall_rank;
1265
		    best = r;
2 7u83 1266
		}
1267
	    } else {
7 7u83 1268
		r->rank = overall_rank;
1269
		best = r;
2 7u83 1270
	    }
1271
	}
1272
    }
7 7u83 1273
    return (best);
2 7u83 1274
}
1275
 
1276
 
1277
/*
1278
    LIST OF ALL AMBIGUOUS OVERLOADED FUNCTIONS
1279
 
1280
    This list is used to hold all the dummy tokenised functions constructed
1281
    to represent target dependent overload resolutions.  The functions are
1282
    determined by the list of candidates, the list of argument types and
1283
    the qualifier used.
1284
*/
1285
 
1286
typedef struct ambig_func_tag {
7 7u83 1287
	IDENTIFIER id;
1288
	LIST(IDENTIFIER) funcs;
1289
	LIST(TYPE) types;
1290
	QUALIFIER qual;
1291
	struct ambig_func_tag *next;
1292
} AMBIG_FUNCTION;
2 7u83 1293
 
7 7u83 1294
static AMBIG_FUNCTION *all_ambig_funcs = NULL;
2 7u83 1295
 
1296
 
1297
/*
1298
    FIND A PREVIOUS AMBIGUOUS OVERLOADED FUNCTION
1299
 
1300
    This routine searches the list of all ambiguous overloaded functions
1301
    for one which candidate functions p, argument types q and qualifier
1302
    qual.
1303
*/
1304
 
7 7u83 1305
static IDENTIFIER
1306
previous_ambig_func(LIST(IDENTIFIER) p, LIST(TYPE) q, QUALIFIER qual)
2 7u83 1307
{
7 7u83 1308
	AMBIG_FUNCTION *f = all_ambig_funcs;
1309
	if (f) {
1310
		unsigned np = LENGTH_list(p);
1311
		unsigned nq = LENGTH_list(q);
1312
		while (f != NULL) {
1313
			if (f->qual == qual) {
1314
				int ok = 1;
1315
				LIST(IDENTIFIER) pf = f->funcs;
1316
				LIST(TYPE) qf = f->types;
1317
				if (LENGTH_list(pf) == np) {
1318
					LIST(IDENTIFIER) pg = p;
1319
					while (!IS_NULL_list(pg)) {
1320
						IDENTIFIER nf =
1321
						    DEREF_id(HEAD_list(pf));
1322
						IDENTIFIER ng =
1323
						    DEREF_id(HEAD_list(pg));
1324
						if (!EQ_id(nf, ng)) {
1325
							ok = 0;
1326
							break;
1327
						}
1328
						pf = TAIL_list(pf);
1329
						pg = TAIL_list(pg);
1330
					}
1331
				}
1332
				if (ok && LENGTH_list(qf) == nq) {
1333
					LIST(TYPE) qg = q;
1334
					while (!IS_NULL_list(qg)) {
1335
						TYPE tf =
1336
						    DEREF_type(HEAD_list(qf));
1337
						TYPE tg =
1338
						    DEREF_type(HEAD_list(qg));
1339
						if (!eq_type(tf, tg)) {
1340
							ok = 0;
1341
							break;
1342
						}
1343
						qf = TAIL_list(qf);
1344
						qg = TAIL_list(qg);
1345
					}
1346
				}
1347
				if (ok) {
1348
					return (f->id);
1349
				}
2 7u83 1350
			}
7 7u83 1351
			f = f->next;
2 7u83 1352
		}
1353
	}
7 7u83 1354
	return (NULL_id);
2 7u83 1355
}
1356
 
1357
 
1358
/*
1359
    CONSTRUCT AN AMBIGUOUS OVERLOADED FUNCTION
1360
 
1361
    This routine constructs a dummy tokenised function to represent the
1362
    target dependent overload resolution given by the candidates p for
1363
    the function call 'id ( args )' detected by the previous call to
1364
    resolve_ambiguous.  Any errors arising are added to err.
1365
*/
1366
 
7 7u83 1367
IDENTIFIER
1368
make_ambig_func(CANDIDATE_LIST *p, IDENTIFIER id, LIST(EXP) args,
1369
		QUALIFIER qual, ERROR *err)
2 7u83 1370
{
7 7u83 1371
	EXP res;
1372
	TYPE fn;
1373
	TOKEN tok;
1374
	TYPE form;
1375
	MEMBER mem;
1376
	DECL_SPEC ds;
1377
	IDENTIFIER tid;
1378
	LIST(TYPE) mt;
1379
	AMBIG_FUNCTION *all;
1380
	TYPE ret = NULL_type;
1381
	TYPE et = type_error;
1382
	CANDIDATE *q = p->elem;
1383
	unsigned i, n = p->size;
1384
	NAMESPACE ns = crt_namespace;
1385
	QUALIFIER cq = crt_id_qualifier;
1386
	int tq = crt_templ_qualifier;
1387
	int td = have_type_declaration;
1388
	unsigned tag = id_function_tag;
1389
	HASHID nm = DEREF_hashid(id_name(id));
2 7u83 1390
 
7 7u83 1391
	/* List of viable candidates */
1392
	LIST(EXP) conds = NULL_list(EXP);
1393
	LIST(TYPE) types = NULL_list(TYPE);
1394
	LIST(IDENTIFIER) funcs = NULL_list(IDENTIFIER);
2 7u83 1395
 
7 7u83 1396
	/* Check function return types */
1397
	for (i = 0; i < n; i++) {
1398
		CANDIDATE *r = q + i;
1399
		if (r->rank >= RANK_VIABLE) {
1400
			int suspect = 0;
1401
			TYPE ta = NULL_type;
1402
			IDENTIFIER fid = r->base;
1403
			switch (TAG_id(fid)) {
1404
			case id_builtin_tag: {
1405
				ta = DEREF_type(id_builtin_ret(fid));
1406
				break;
2 7u83 1407
			}
7 7u83 1408
			case id_function_tag: {
1409
				TYPE fa = DEREF_type(id_function_type(fid));
1410
				if (IS_type_func(fa)) {
1411
					ta = DEREF_type(type_func_ret(fa));
1412
				}
1413
				break;
2 7u83 1414
			}
7 7u83 1415
			case id_mem_func_tag: {
1416
				TYPE fa = DEREF_type(id_mem_func_type(fid));
1417
				if (IS_type_func(fa)) {
1418
					HASHID fnm = DEREF_hashid(id_name(fid));
1419
					mt = DEREF_list(type_func_mtypes(fa));
1420
					et = DEREF_type(HEAD_list(mt));
1421
					if (IS_hashid_constr(fnm)) {
1422
						ta = DEREF_type(hashid_constr_type(fnm));
1423
					} else {
1424
						ta = DEREF_type(type_func_ret(fa));
1425
						tag = id_mem_func_tag;
1426
					}
1427
				}
1428
				break;
1429
			}
1430
			case id_stat_mem_func_tag: {
1431
				TYPE fa =
1432
				    DEREF_type(id_stat_mem_func_type(fid));
1433
				if (IS_type_func(fa)) {
1434
					mt = DEREF_list(type_func_mtypes(fa));
1435
					et = DEREF_type(HEAD_list(mt));
1436
					ta = DEREF_type(type_func_ret(fa));
1437
					if (tag != id_mem_func_tag) {
1438
						tag = id_stat_mem_func_tag;
1439
					}
1440
				}
1441
				break;
1442
			}
1443
			}
1444
			ret = common_type(ret, ta, &suspect);
1445
			if (suspect) {
1446
				ret = NULL_type;
1447
			}
1448
			CONS_exp(r->cond, conds, conds);
1449
			CONS_id(fid, funcs, funcs);
2 7u83 1450
		}
1451
	}
7 7u83 1452
	if (IS_NULL_type(ret)) {
1453
		/* Couldn't bring return types to common type */
1454
		ERROR err2 = ERR_over_match_best_common();
1455
		add_error(err, err2);
1456
		ret = type_error;
1457
	}
1458
	if (err != KILL_err) {
1459
		*err = list_candidates(*err, p, RANK_VIABLE);
1460
	}
2 7u83 1461
 
7 7u83 1462
	/* Create list of argument types */
1463
	while (!IS_NULL_list(args)) {
1464
		TYPE t;
1465
		EXP e = DEREF_exp(HEAD_list(args));
1466
		if (IS_NULL_exp(e)) {
1467
			t = et;
1468
			et = type_error;
1469
		} else {
1470
			t = DEREF_type(exp_type(e));
1471
			if (IS_type_bitfield(t)) {
1472
				t = promote_type(t);
1473
			} else {
1474
				CV_SPEC cv = DEREF_cv(type_qual(t));
1475
				if (cv & cv_lvalue) {
1476
					MAKE_type_ref(cv_none, t, t);
1477
				}
1478
			}
2 7u83 1479
		}
7 7u83 1480
		CONS_type(t, types, types);
1481
		args = TAIL_list(args);
2 7u83 1482
	}
7 7u83 1483
	types = REVERSE_list(types);
2 7u83 1484
 
7 7u83 1485
	/* Check for previous instance */
1486
	tid = previous_ambig_func(funcs, types, qual);
1487
	if (!IS_NULL_id(tid)) {
1488
		DESTROY_list(funcs, SIZE_id);
1489
		DESTROY_list(types, SIZE_type);
1490
		free_exp_list(conds, 1);
1491
		return (tid);
1492
	}
2 7u83 1493
 
7 7u83 1494
	/* Create new instance */
1495
	all = xmalloc_one(AMBIG_FUNCTION);
1496
	all->id = NULL_id;
1497
	all->funcs = funcs;
1498
	all->types = types;
1499
	all->qual = qual;
1500
	all->next = all_ambig_funcs;
1501
	all_ambig_funcs = all;
2 7u83 1502
 
7 7u83 1503
	/* Scan through argument types */
1504
	crt_id_qualifier = qual_none;
1505
	crt_templ_qualifier = 0;
1506
	have_type_declaration = TYPE_DECL_NONE;
1507
	decl_loc = crt_loc;
1508
	begin_param(id);
1509
	mt = types;
1510
	while (!IS_NULL_list(mt)) {
1511
		HASHID pnm = lookup_anon();
1512
		IDENTIFIER pid = DEREF_id(hashid_id(pnm));
1513
		TYPE t = DEREF_type(HEAD_list(mt));
1514
		pid = make_param_decl(dspec_none, t, pid, CONTEXT_PARAMETER);
1515
		init_param(pid, NULL_exp);
1516
		mt = TAIL_list(mt);
1517
	}
1518
	fn = make_func_type(ret, FUNC_NONE, cv_none, empty_type_set);
1519
	if (tag != id_function_tag) {
1520
		mt = DEREF_list(type_func_ptypes(fn));
1521
		mt = TAIL_list(mt);
1522
		COPY_list(type_func_ptypes(fn), mt);
1523
	}
1524
	end_param();
2 7u83 1525
 
7 7u83 1526
	/* Create a function identifier */
1527
	ds = (dspec_static | dspec_token | dspec_reserve);
1528
	MAKE_id_function_etc(tag, nm, ds, ns, crt_loc, fn, NULL_id, id);
2 7u83 1529
 
7 7u83 1530
	/* Create token identifier */
1531
	ns = token_namespace;
1532
	nm = lookup_anon();
1533
	mem = search_member(ns, nm, 1);
1534
	MAKE_tok_func(fn, tok);
1535
	MAKE_id_token(nm, ds, ns, crt_loc, tok, id, tid);
1536
	MAKE_type_token(cv_none, tid, NULL_list(TOKEN), form);
1537
	COPY_type(id_function_etc_form(id), form);
1538
	set_member(mem, tid);
1539
	tok = func_proc_token(tok);
2 7u83 1540
 
7 7u83 1541
	/* Define token */
1542
	res = install_error(&crt_loc, ERR_over_match_best_install());
1543
	while (!IS_NULL_list(funcs)) {
1544
		EXP c, f;
1545
		LIST(EXP) fargs = NULL_list(EXP);
1546
		LIST(IDENTIFIER) pids = DEREF_list(tok_proc_bids(tok));
1547
		IDENTIFIER fid = DEREF_id(HEAD_list(funcs));
1548
		DESTROY_CONS_exp(destroy, c, conds, conds);
1549
		while (!IS_NULL_list(pids)) {
1550
			/* Build up argument list */
1551
			TYPE t;
1552
			int prom = 0;
1553
			LIST(TYPE) pt;
1554
			IDENTIFIER pid = DEREF_id(HEAD_list(pids));
1555
			EXP e = apply_exp_token(pid, NULL_list(TOKEN), 0);
1556
			e = convert_reference(e, REF_NORMAL);
1557
			t = DEREF_type(exp_type(e));
1558
			pt = possible_types(t, &prom);
1559
			if (!IS_NULL_list(pt)) {
1560
				/* Mark target dependent arguments */
1561
				MAKE_exp_paren(t, e, e);
1562
			}
1563
			CONS_exp(e, fargs, fargs);
1564
			pids = TAIL_list(pids);
1565
		}
1566
		fargs = REVERSE_list(fargs);
1567
		if (IS_id_builtin(fid)) {
1568
			f = apply_builtin(fid, fargs);
1569
		} else {
1570
			HASHID fnm = DEREF_hashid(id_name(fid));
1571
			use_func_id(fid, 1, 0);
1572
			if (IS_hashid_constr(fnm)) {
1573
				f = apply_constr(fid, fargs);
1574
			} else {
1575
				f = apply_func_id(fid, qual, NULL_graph, fargs);
1576
			}
1577
		}
1578
		if (!IS_type_top_etc(ret)) {
1579
			/* Convert to common return type */
1580
			ERROR err2 = NULL_err;
1581
			f = convert_reference(f, REF_ASSIGN);
1582
			f = cast_exp(ret, f, &err2, CAST_IMPLICIT);
1583
			if (!IS_NULL_err(err2)) {
1584
				report(crt_loc, err2);
1585
			}
1586
		}
1587
		if (IS_NULL_exp(res)) {
1588
			free_exp(c, 1);
1589
			res = f;
1590
		} else {
1591
			MAKE_exp_hash_if (ret, c, f, res, res);
1592
		}
1593
		funcs = TAIL_list(funcs);
2 7u83 1594
	}
7 7u83 1595
	IGNORE define_exp_token(tid, res, 1);
2 7u83 1596
 
7 7u83 1597
	/* Restore previous state */
1598
	have_type_declaration = td;
1599
	crt_templ_qualifier = tq;
1600
	crt_id_qualifier = cq;
1601
	all->id = id;
1602
	return (id);
2 7u83 1603
}
1604
 
1605
 
1606
/*
1607
    IS AN IDENTIFIER A FUNCTION TEMPLATE?
1608
 
1609
    This routine returns true if id is a function template.  In this case
1610
    overload resolution is always done even if id is not overloaded.
1611
*/
1612
 
7 7u83 1613
static int
1614
is_template_func(IDENTIFIER id)
2 7u83 1615
{
7 7u83 1616
	if (IS_id_function_etc(id)) {
1617
		TYPE fn = DEREF_type(id_function_etc_type(id));
1618
		if (IS_type_templ(fn)) {
1619
			return (1);
1620
		}
1621
	}
1622
	return (0);
2 7u83 1623
}
1624
 
1625
 
1626
/*
1627
    RESOLVE AN OVERLOADED FUNCTION CALL
1628
 
1629
    This routine resolves the overloaded function call 'id ( args )' where
1630
    id is an identifier expression.  dep is true when id is a simple
1631
    unqualified function name.  It returns the identifier for id which
1632
    provides the best match for the given arguments.  For ambiguities the
1633
    identifier returned is the first of the viable functions to be declared
1634
    (this is a side effect of the algorithm in resolve_overload and the
1635
    way in which overloaded functions are represented).
1636
*/
1637
 
7 7u83 1638
IDENTIFIER
1639
resolve_call(IDENTIFIER id, LIST(EXP) args, QUALIFIER qual, int dep)
2 7u83 1640
{
7 7u83 1641
	/* Build up candidate list */
1642
	unsigned sz;
1643
	IDENTIFIER fid = id;
1644
	CANDIDATE_LIST *p = &candidates;
1645
	p->size = 0;
1646
	if (dep) {
1647
		/* Allow for argument dependent look-up */
1648
		LIST(EXP) q = args;
1649
		while (!IS_NULL_list(q)) {
1650
			EXP a = DEREF_exp(HEAD_list(q));
1651
			if (!IS_NULL_exp(a)) {
1652
				TYPE t = DEREF_type(exp_type(a));
1653
				IGNORE koenig_candidates(p, fid, t, KIND_FUNC);
1654
			}
1655
			q = TAIL_list(q);
1656
		}
2 7u83 1657
	}
7 7u83 1658
	add_candidates(p, fid, 1, KIND_FUNC);
1659
	sz = p->size;
1660
	if (sz == 0) {
1661
		/* No candidates */
1662
		EXP a;
1663
		int fn = 2;
1664
		QUALIFIER cq = crt_id_qualifier;
1665
		crt_id_qualifier = (qual & qual_explicit);
1666
		if (qual & qual_mark) {
1667
			fn = 1;
1668
		}
1669
		a = implicit_id_exp(fid, fn);
1670
		if (IS_exp_identifier_etc(a)) {
1671
			fid = DEREF_id(exp_identifier_etc_id(a));
1672
			add_candidates(p, fid, 1, KIND_FUNC);
1673
			sz = p->size;
1674
			if (sz != 0) {
1675
				fid = p->elem[0].func;
1676
			}
1677
		}
1678
		crt_id_qualifier = cq;
1679
	} else {
1680
		fid = p->elem[0].func;
2 7u83 1681
	}
1682
 
7 7u83 1683
	/* Resolve overloaded functions */
1684
	if (sz > 1) {
1685
		CANDIDATE *q = resolve_overload(p, args, NULL_type, 0);
1686
		IDENTIFIER qid = q->func;
1687
		unsigned rank = q->rank;
1688
		int kind = q->kind;
1689
		if (rank == RANK_BEST) {
1690
			/* Unambiguous resolution */
1691
			if (match_no_viable > 1) {
1692
				ERROR err = ERR_over_match_call_ok(qid);
1693
				if (!IS_NULL_err(err)) {
1694
					report(crt_loc, err);
1695
				}
1696
			}
1697
		} else if (rank == RANK_VIABLE) {
1698
			/* Ambiguous resolution */
1699
			q = resolve_ambiguous(p, args, NULL_type, 0);
1700
			qid = q->func;
1701
			rank = q->rank;
1702
			if (rank == RANK_TARGET) {
1703
				ERROR err = ERR_over_match_call_target(id);
1704
				qid = make_ambig_func(p, qid, args, qual, &err);
1705
				kind = KIND_FUNC;
1706
				report(crt_loc, err);
1707
			} else if (rank == RANK_VIABLE) {
1708
				ERROR err = ERR_over_match_call_ambig(id);
1709
				err = list_candidates(err, p, RANK_VIABLE);
1710
				report(crt_loc, err);
1711
			}
1712
		} else {
1713
			/* Unviable resolution */
1714
			report(crt_loc, ERR_over_match_viable_none(id));
1715
		}
1716
		resolved_kind = kind;
1717
		return (qid);
2 7u83 1718
	}
1719
 
7 7u83 1720
	/* Check template functions */
1721
	if (is_template_func(fid)) {
1722
		ERROR err = NULL_err;
1723
		IDENTIFIER qid = deduce_args(fid, args, 2, 0, 1, &err);
1724
		if (IS_NULL_id(qid)) {
1725
			/* No viable resolution */
1726
			err = concat_error(err, ERR_over_match_viable_none(id));
1727
			qid = fid;
1728
		}
1729
		if (!IS_NULL_err(err)) {
1730
			/* Report any argument deduction errors */
1731
			err = concat_error(err, ERR_temp_deduct_fail(id));
1732
			report(crt_loc, err);
1733
		} else {
1734
			/* Successful argument deduction */
1735
			report(crt_loc, ERR_over_match_call_ok(qid));
1736
		}
1737
		fid = qid;
2 7u83 1738
	}
1739
 
7 7u83 1740
	/* Don't check non-overloaded functions */
1741
	match_no_args = 0;
1742
	match_no_viable = 0;
1743
	resolved_kind = KIND_FUNC;
1744
	return (fid);
2 7u83 1745
}
1746
 
1747
 
1748
/*
1749
    RESOLVE AN AMBIGUOUS FUNCTION ADDRESS
1750
 
1751
    This routine resolves the list of functions ids to see which is the
1752
    best under the template specialisation rules.  If no one best candidate
1753
    is found the null identifier is returned.  This rule is not actually
1754
    in ISO C++, but it could have been.
1755
*/
1756
 
7 7u83 1757
static IDENTIFIER
1758
resolve_ambig_func(LIST(IDENTIFIER) ids, int depth)
2 7u83 1759
{
7 7u83 1760
	if (!IS_NULL_list(ids)) {
1761
		int cmp;
1762
		IDENTIFIER rid, sid;
1763
		LIST(IDENTIFIER) pids = ids;
2 7u83 1764
 
7 7u83 1765
		/* Find the first two functions */
1766
		rid = DEREF_id(HEAD_list(pids));
1767
		pids = TAIL_list(pids);
1768
		if (IS_NULL_list(pids)) {
1769
			return (rid);
1770
		}
1771
		sid = DEREF_id(HEAD_list(pids));
1772
		pids = TAIL_list(pids);
2 7u83 1773
 
7 7u83 1774
		/* Compare them */
1775
		cmp = compare_funcs(rid, sid);
1776
		if (cmp == 2) {
1777
			rid = sid;
1778
		} else if (cmp != 1) {
1779
			rid = NULL_id;
1780
		}
2 7u83 1781
 
7 7u83 1782
		/* Compare the winner against the best of the rest */
1783
		sid = resolve_ambig_func(pids, depth + 1);
1784
		cmp = compare_funcs(rid, sid);
1785
		if (cmp == 2) {
1786
			rid = sid;
1787
		} else if (cmp != 1) {
1788
			rid = NULL_id;
1789
		}
2 7u83 1790
 
7 7u83 1791
		/* Check overall winner */
1792
		if (depth == 0 && !IS_NULL_id(rid)) {
1793
			pids = ids;
1794
			while (!IS_NULL_list(pids)) {
1795
				sid = DEREF_id(HEAD_list(pids));
1796
				if (!EQ_id(rid, sid)) {
1797
					cmp = compare_funcs(rid, sid);
1798
					if (cmp != 1) {
1799
						return (NULL_id);
1800
					}
1801
				}
1802
				pids = TAIL_list(pids);
1803
			}
2 7u83 1804
		}
7 7u83 1805
		return (rid);
2 7u83 1806
	}
7 7u83 1807
	return (NULL_id);
2 7u83 1808
}
1809
 
1810
 
1811
/*
1812
    FIND AN OVERLOADED FUNCTION OF A GIVEN TYPE
1813
 
1814
    This routine returns whichever of the set of overloaded or ambiguous
1815
    functions id has type t.  Type deduction is allowed for the template
1816
    parameters pids if templ is true. The null identifier is returned if
1817
    none has the correct type, an ambiguous identifier is returned if
1818
    more than one has the correct type (although overload resolution
1819
    occurs if res is true).  If there is a match ignoring linkage
1820
    specifiers then this is returned, the value of eq_func_type for the
1821
    result is assigned to peq.
1822
*/
1823
 
7 7u83 1824
IDENTIFIER
1825
resolve_func(IDENTIFIER id, TYPE t, int templ, int res, LIST(IDENTIFIER) pids,
1826
	     int *peq)
2 7u83 1827
{
7 7u83 1828
	int best = 2;
1829
	IDENTIFIER rid = NULL_id;
1830
	switch (TAG_id(id)) {
1831
	case id_function_tag:
1832
	case id_mem_func_tag:
1833
	case id_stat_mem_func_tag: {
1834
		/* Check overloaded functions */
1835
		IDENTIFIER fid = id;
1836
		int have_templates = 0;
1837
		LIST(IDENTIFIER) rids = NULL_list(IDENTIFIER);
1838
		while (!IS_NULL_id(fid)) {
1839
			TYPE s;
1840
			int d = 0;
1841
			int eq = 0;
1842
			IDENTIFIER tid = fid;
1843
			if (!IS_NULL_list(pids)) {
1844
				/* Save template parameter values */
1845
				d = save_token_args(pids, NULL_list(TOKEN));
1846
			}
1847
			s = DEREF_type(id_function_etc_type(fid));
1848
			if (IS_type_templ(s) && templ) {
1849
				/* Template functions */
1850
				tid = deduce_func(tid, t, &eq);
1851
				if (!IS_NULL_id(tid)) {
1852
					have_templates = 1;
1853
				}
1854
			} else {
1855
				/* Simple functions */
2 7u83 1856
#if LANGUAGE_CPP
7 7u83 1857
				eq = eq_func_type(s, t, 1, 0);
2 7u83 1858
#else
7 7u83 1859
				TYPE r = type_composite(s, t, 1, 0, KILL_err,
1860
							0);
1861
				if (!IS_NULL_type(r)) {
1862
					eq = 3;
1863
				}
2 7u83 1864
#endif
7 7u83 1865
			}
1866
			if (eq >= best) {
1867
				if (eq > best) {
1868
					/* Better match than previous */
1869
					if (!IS_NULL_list(rids)) {
1870
						DESTROY_list(rids, SIZE_id);
1871
						rids = NULL_list(IDENTIFIER);
1872
					}
1873
					rid = NULL_id;
1874
					best = eq;
1875
				}
1876
				if (!IS_NULL_id(rid)) {
1877
					CONS_id(rid, rids, rids);
1878
				}
1879
				rid = tid;
1880
			}
1881
			if (!IS_NULL_list(pids)) {
1882
				/* Restore template parameters */
1883
				restore_token_args(pids, d);
1884
			}
1885
			fid = DEREF_id(id_function_etc_over(fid));
2 7u83 1886
		}
7 7u83 1887
		if (!IS_NULL_list(rids)) {
1888
			/* Construct ambiguous result */
1889
			HASHID nm;
1890
			NAMESPACE ns;
1891
			DECL_SPEC ds;
1892
			CONS_id(rid, rids, rids);
1893
			if (have_templates && res) {
1894
				/* Perform overload resolution */
1895
				rid = resolve_ambig_func(rids, 0);
1896
				if (!IS_NULL_id(rid)) {
1897
					DESTROY_list(rids, SIZE_id);
1898
					break;
1899
				}
2 7u83 1900
			}
7 7u83 1901
			nm = DEREF_hashid(id_name(id));
1902
			ns = DEREF_nspace(id_parent(id));
1903
			ds = find_ambig_dspec(rids);
1904
			MAKE_id_ambig(nm, ds, ns, crt_loc, rids, 0, rid);
2 7u83 1905
		}
7 7u83 1906
		break;
2 7u83 1907
	}
1908
 
7 7u83 1909
	case id_ambig_tag: {
1910
		/* Check ambiguous functions */
1911
		LIST(IDENTIFIER) rids = NULL_list(IDENTIFIER);
1912
		LIST(IDENTIFIER) p = DEREF_list(id_ambig_ids(id));
1913
		while (!IS_NULL_list(p)) {
1914
			int eq = 0;
1915
			IDENTIFIER pid = DEREF_id(HEAD_list(p));
1916
			pid = resolve_func(pid, t, templ, 0, pids, &eq);
1917
			if (!IS_NULL_id(pid) && eq >= best) {
1918
				if (eq > best) {
1919
					/* Better match than previous */
1920
					if (!IS_NULL_list(rids)) {
1921
						DESTROY_list(rids, SIZE_id);
1922
						rids = NULL_list(IDENTIFIER);
1923
					}
1924
					rid = NULL_id;
1925
					best = eq;
1926
				}
1927
				if (IS_id_ambig(pid)) {
1928
					LIST(IDENTIFIER) q;
1929
					q = DEREF_list(id_ambig_ids(pid));
1930
					while (!IS_NULL_list(q)) {
1931
						pid = DEREF_id(HEAD_list(q));
1932
						if (!IS_NULL_id(rid)) {
1933
							CONS_id(rid, rids, rids);
1934
						}
1935
						rid = pid;
1936
						q = TAIL_list(q);
1937
					}
1938
				} else {
1939
					if (!IS_NULL_id(rid)) {
1940
						CONS_id(rid, rids, rids);
1941
					}
1942
					rid = pid;
1943
				}
2 7u83 1944
			}
7 7u83 1945
			p = TAIL_list(p);
1946
		}
1947
		if (!IS_NULL_list(rids)) {
1948
			HASHID nm;
1949
			NAMESPACE ns;
1950
			DECL_SPEC ds;
1951
			CONS_id(rid, rids, rids);
1952
			if (res) {
1953
				/* Perform overload resolution */
1954
				rid = resolve_ambig_func(rids, 0);
1955
				if (!IS_NULL_id(rid)) {
1956
					DESTROY_list(rids, SIZE_id);
1957
					break;
1958
				}
2 7u83 1959
			}
7 7u83 1960
			nm = DEREF_hashid(id_name(id));
1961
			ns = DEREF_nspace(id_parent(id));
1962
			ds = find_ambig_dspec(rids);
1963
			MAKE_id_ambig(nm, ds, ns, crt_loc, rids, 0, rid);
2 7u83 1964
		}
7 7u83 1965
		break;
2 7u83 1966
	}
7 7u83 1967
	}
1968
	if (IS_NULL_id(rid)) {
1969
		best = 0;
1970
	}
1971
	*peq = best;
1972
	return (rid);
2 7u83 1973
}
1974
 
1975
 
1976
/*
1977
    CREATE A RESOLVED OVERLOADED FUNCTION EXPRESSION
1978
 
1979
    This routine creates a resolved overloaded function expression for
1980
    the function id.
1981
*/
1982
 
7 7u83 1983
static EXP
1984
make_resolved_exp(IDENTIFIER id, QUALIFIER q, EXP b, int addr, int paren)
2 7u83 1985
{
7 7u83 1986
	EXP e;
1987
	TYPE fn = DEREF_type(id_function_etc_type(id));
1988
	if (IS_id_mem_func(id)) {
1989
		if (!IS_NULL_exp(b)) {
1990
			report(crt_loc, ERR_expr_ref_call());
1991
			q = qual_nested;
1992
		}
1993
		MAKE_exp_member(fn, id, q, e);
1994
	} else {
1995
		MAKE_exp_identifier(fn, id, q, e);
1996
		if (!IS_NULL_exp(b)) {
1997
			e = join_exp(b, e);
1998
		}
2 7u83 1999
	}
7 7u83 2000
	if (addr) {
2001
		if (paren) {
2002
			e = make_paren_exp(e);
2003
		}
2004
		e = make_ref_exp(e, 1);
2005
	}
2006
	return (e);
2 7u83 2007
}
2008
 
2009
 
2010
/*
2011
    RESOLVE THE ADDRESS OF AN OVERLOADED FUNCTION
2012
 
2013
    This routine checks the conversion of the expression e to type t.  If
2014
    e is an overloaded function identifier expression and t is a function
2015
    type, or pointer to function type, or pointer to member function type,
2016
    then the various versions of e are checked for the one which matches
2017
    the function type underlying t in the context of the template
2018
    parameters pids.  An error is added to err if this does not exist.
2019
    The result is the appropriate version of e.  Note that member
2020
    expressions which resolve to static function members are replaced
2021
    by identifier expressions at this stage (see make_id_exp).
2022
*/
2023
 
7 7u83 2024
EXP
2025
resolve_cast(TYPE t, EXP e, ERROR *err, int use, int rescan,
2026
	     LIST(IDENTIFIER) pids)
2 7u83 2027
{
7 7u83 2028
	/* Check for identifier expressions */
2029
	EXP a = e;
2030
	QUALIFIER q;
2031
	int addr = 0;
2032
	int paren = 0;
2033
	IDENTIFIER id;
2034
	EXP b = NULL_exp;
2035
	unsigned tag = TAG_exp(a);
2036
	while (tag == exp_paren_tag) {
2037
		a = DEREF_exp(exp_paren_arg(a));
2038
		tag = TAG_exp(a);
2 7u83 2039
	}
7 7u83 2040
	if (tag == exp_address_tag) {
2041
		/* Address expression */
2042
		a = DEREF_exp(exp_address_arg(a));
2043
		tag = TAG_exp(a);
2044
		addr = 1;
2045
	} else if (tag == exp_address_mem_tag) {
2046
		/* Address of member expression */
2047
		paren = DEREF_int(exp_address_mem_paren(a));
2048
		a = DEREF_exp(exp_address_mem_arg(a));
2049
		tag = TAG_exp(a);
2050
		addr = 1;
2051
	} else if (tag == exp_op_tag) {
2052
		/* Check for undetermined address expressions */
2053
		int op = DEREF_int(exp_op_lex(a));
2054
		EXP c = DEREF_exp(exp_op_arg2(a));
2055
		if (op == lex_and_H1 && IS_NULL_exp(c)) {
2056
			a = DEREF_exp(exp_op_arg1(a));
2057
			tag = TAG_exp(a);
2058
			addr = 1;
2 7u83 2059
		}
2060
	}
7 7u83 2061
	while (tag == exp_paren_tag) {
2062
		a = DEREF_exp(exp_paren_arg(a));
2063
		paren = 1;
2064
		tag = TAG_exp(a);
2065
	}
2066
	if (tag == exp_call_tag) {
2067
		/* Member reference expression */
2068
		b = DEREF_exp(exp_call_arg(a));
2069
		a = DEREF_exp(exp_call_ptr(a));
2070
	}
2071
	if (!IS_exp_identifier_etc(a)) {
2072
		/* Not an identifier expression */
2073
		return (e);
2074
	}
2 7u83 2075
 
7 7u83 2076
	/* Mark identifiers as resolved */
2077
	id = DEREF_id(exp_identifier_etc_id(a));
2078
	q = DEREF_qual(exp_identifier_etc_qual(a));
2079
	if (q & qual_mark) {
2080
		return (e);
2 7u83 2081
	}
7 7u83 2082
	if (rescan) {
2083
		/* Rescan function name if necessary */
2084
		id = rescan_func_id(id, q);
2085
	}
2086
	q |= qual_mark;
2 7u83 2087
 
7 7u83 2088
	/* Check for overloaded functions */
2089
	tag = TAG_id(id);
2090
	switch (tag) {
2091
	case id_function_tag:
2092
	case id_mem_func_tag:
2093
	case id_stat_mem_func_tag:
2094
function_lab: {
2095
		       /* Check functions for overloading */
2096
		       TYPE s;
2097
		       IDENTIFIER over;
2098
		       if (rescan) {
2099
			       goto overload_lab;
2100
		       }
2101
		       if (dependent_cast(id, t)) {
2102
			       /* Allow for template parameters */
2103
			       if (!IS_type_func(t)) {
2104
				       t = rvalue_type(t);
2105
			       }
2106
			       MAKE_exp_op(t, lex_function, e, NULL_exp, e);
2107
			       return (e);
2108
		       }
2109
		       over = DEREF_id(id_function_etc_over(id));
2110
		       if (!IS_NULL_id(over)) {
2111
			       goto overload_lab;
2112
		       }
2113
		       s = DEREF_type(id_function_etc_type(id));
2114
		       if (IS_type_templ(s)) {
2115
			       goto overload_lab;
2116
		       }
2117
		       if (tag == id_mem_func_tag) {
2118
			       if (!IS_NULL_exp(b) || addr) {
2119
				       /* Force error in this case */
2120
				       e = make_resolved_exp(id, q, b, addr,
2121
							     paren);
2122
			       }
2123
		       }
2124
		       if (use) {
2125
			       COPY_qual(exp_identifier_etc_qual(a), q);
2126
			       use_id(id, suppress_usage);
2127
		       }
2128
		       break;
2129
	       }
2130
 
2131
	case id_ambig_tag:
2132
	case id_undef_tag:
2133
overload_lab: {
2134
		      /* Overloaded and ambiguous functions */
2135
		      TYPE fn = find_func_type(t);
2136
		      if (!IS_NULL_type(fn)) {
2137
			      /* Overload resolution */
2138
			      int eq = 0;
2139
			      IDENTIFIER rid;
2140
			      if (dependent_cast(id, t)) {
2141
				      /* Allow for template parameters */
2142
				      if (!IS_type_func(t)) {
2143
					      t = rvalue_type(t);
2144
				      }
2145
				      MAKE_exp_op(t, lex_function, e, NULL_exp,
2146
						  e);
2147
				      return (e);
2148
			      }
2149
			      if (tag == id_undef_tag) {
2150
				      goto default_lab;
2151
			      }
2152
			      rid = resolve_func(id, fn, 1, 0, pids, &eq);
2153
			      if (!IS_NULL_id(rid)) {
2154
				      if (IS_id_ambig(rid)) {
2155
					      /* Ambiguous resolution */
2156
					      if (use) {
2157
						      /* Select one function */
2158
						      LIST(IDENTIFIER) ids;
2159
						      IGNORE report_ambiguous(rid, 0, 0, 0);
2160
						      ids = DEREF_list(id_ambig_ids(rid));
2161
						      rid = resolve_ambig_func(ids, 0);
2162
						      if (IS_NULL_id(rid)) {
2163
							      rid = DEREF_id(HEAD_list(ids));
2164
						      }
2165
					      } else {
2166
						      e = NULL_exp;
2167
						      rid = NULL_id;
2168
					      }
2169
				      }
2170
				      if (!IS_NULL_id(rid)) {
2171
					      /* Unique resolution */
2172
					      report(crt_loc, ERR_over_over_ok(rid));
2173
					      e = make_resolved_exp(rid, q, b, addr, paren);
2174
					      if (use) {
2175
						      use_id(rid, suppress_usage);
2176
					      }
2177
				      }
2178
			      } else {
2179
				      /* Unsuccessful resolution */
2180
				      if (use) {
2181
					      add_error(err, ERR_over_over_none(id, fn));
2182
					      e = make_error_exp(0);
2183
				      } else {
2184
					      e = NULL_exp;
2185
				      }
2186
			      }
2187
		      } else {
2188
			      /* No context for resolution */
2189
			      if (tag == id_undef_tag) {
2190
				      goto default_lab;
2191
			      }
2192
			      if (use) {
2193
				      add_error(err, ERR_over_over_context(id));
2194
				      e = make_error_exp(0);
2195
			      } else {
2196
				      e = NULL_exp;
2197
			      }
2198
		      }
2199
		      break;
2200
	      }
2201
 
2202
	default:
2203
default_lab:
2204
	      /* Other identifiers */
2205
	      if (rescan) {
2206
		      if (use) {
2207
			      EXP c = implicit_id_exp(id, 1);
2208
			      if (IS_exp_identifier_etc(c)) {
2209
				      id = DEREF_id(exp_identifier_etc_id(a));
2210
				      if (IS_id_function_etc(id)) {
2211
					      goto function_lab;
2212
				      }
2213
			      }
2214
		      } else {
2215
			      e = NULL_exp;
2216
		      }
2217
	      }
2218
	      break;
2 7u83 2219
	}
7 7u83 2220
	return (e);
2 7u83 2221
}