Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – tendra.SVN – Blame – /branches/tendra4/src/producers/common/construct/overload.c – Rev 2

Subversion Repositories tendra.SVN

Rev

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

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