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, 1998
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
#include "config.h"
32
#include "c_types.h"
33
#include "ctype_ops.h"
34
#include "exp_ops.h"
35
#include "graph_ops.h"
36
#include "hashid_ops.h"
37
#include "id_ops.h"
38
#include "inst_ops.h"
39
#include "member_ops.h"
40
#include "nspace_ops.h"
41
#include "tok_ops.h"
42
#include "type_ops.h"
43
#include "error.h"
44
#include "catalog.h"
45
#include "option.h"
46
#include "allocate.h"
47
#include "basetype.h"
48
#include "check.h"
49
#include "chktype.h"
50
#include "class.h"
51
#include "compile.h"
52
#include "construct.h"
53
#include "copy.h"
54
#include "declare.h"
55
#include "derive.h"
56
#include "destroy.h"
57
#include "dump.h"
58
#include "file.h"
59
#include "function.h"
60
#include "hash.h"
61
#include "identifier.h"
62
#include "instance.h"
63
#include "namespace.h"
64
#include "operator.h"
65
#include "overload.h"
66
#include "predict.h"
67
#include "redeclare.h"
68
#include "template.h"
69
#include "tokdef.h"
70
#include "token.h"
71
static void copy_template PROTO_S ( ( IDENTIFIER, int ) ) ;
72
 
73
 
74
/*
75
    LIST OF ALL TEMPLATE INSTANCES
76
 
77
    All template instances are formed into a linked list by their prev
78
    field (most recent first).
79
*/
80
 
81
INSTANCE all_instances = NULL_inst ;
82
 
83
 
84
/*
85
    JOIN TWO LISTS OF TEMPLATE ARGUMENTS
86
 
87
    This routine copies the template arguments p to the start of the list q.
88
*/
89
 
90
static LIST ( TOKEN ) add_template_args
91
    PROTO_N ( ( p, q ) )
92
    PROTO_T ( LIST ( TOKEN ) p X LIST ( TOKEN ) q )
93
{
94
    if ( !IS_NULL_list ( p ) ) {
95
	TOKEN tok = DEREF_tok ( HEAD_list ( p ) ) ;
96
	tok = expand_sort ( tok, -1, 1 ) ;
97
	p = TAIL_list ( p ) ;
98
	q = add_template_args ( p, q ) ;
99
	CONS_tok ( tok, q, q ) ;
100
    }
101
    return ( q ) ;
102
}
103
 
104
 
105
/*
106
    CREATE A PARTIAL FUNCTION INSTANCE
107
 
108
    This routine is a special case of instance_func which allows for the
109
    case where some of the template arguments are given explicitly and
110
    others are deduced.  id gives the template with any explicit arguments
111
    already bound, and args gives the implicitly deduced arguments which
112
    are bound to the parameters pids.
113
*/
114
 
115
static IDENTIFIER inst_func_deduce
116
    PROTO_N ( ( id, pids, args, d ) )
117
    PROTO_T ( IDENTIFIER id X LIST ( IDENTIFIER ) pids X
118
	      LIST ( TOKEN ) args X int d )
119
{
120
    IDENTIFIER fid = DEREF_id ( id_alias ( id ) ) ;
121
    TYPE form = DEREF_type ( id_function_etc_form ( fid ) ) ;
122
    if ( !IS_NULL_type ( form ) && IS_type_token ( form ) ) {
123
	LIST ( TOKEN ) dargs ;
124
	fid = DEREF_id ( type_token_tok ( form ) ) ;
125
	dargs = DEREF_list ( type_token_args ( form ) ) ;
126
	if ( !IS_NULL_list ( dargs ) ) {
127
	    /* Partially specified template */
128
	    TYPE s = DEREF_type ( id_function_etc_type ( fid ) ) ;
129
	    TOKEN sort = DEREF_tok ( type_templ_sort ( s ) ) ;
130
	    restore_token_args ( pids, d ) ;
131
	    args = add_template_args ( dargs, args ) ;
132
	    pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
133
	    d = save_token_args ( pids, args ) ;
134
	    fid = inst_func_deduce ( fid, pids, args, d ) ;
135
	    return ( fid ) ;
136
	}
137
    }
138
    fid = instance_func ( fid, args, 1, 0 ) ;
139
    restore_token_args ( pids, d ) ;
140
    return ( fid ) ;
141
}
142
 
143
 
144
/*
145
    DEDUCE A BASE CLASS
146
 
147
    This routine returns a list of all the base classes of gr which can
148
    be deduced to be equal to the class ct.
149
*/
150
 
151
static LIST ( GRAPH ) deduce_graph
152
    PROTO_N ( ( gr, ct, pids ) )
153
    PROTO_T ( GRAPH gr X CLASS_TYPE ct X LIST ( IDENTIFIER ) pids )
154
{
155
    LIST ( GRAPH ) pr = NULL_list ( GRAPH ) ;
156
    CLASS_TYPE cr = DEREF_ctype ( graph_head ( gr ) ) ;
157
    DECL_SPEC acc = DEREF_dspec ( graph_access ( gr ) ) ;
158
 
159
    /* Check for equality */
160
    int d = save_token_args ( pids, NULL_list ( TOKEN ) ) ;
161
    if ( eq_ctype ( cr, ct ) ) CONS_graph ( gr, pr, pr ) ;
162
    restore_token_args ( pids, d ) ;
163
 
164
    /* Examine base classes */
165
    if ( acc & dspec_main ) {
166
	LIST ( GRAPH ) br = DEREF_list ( graph_tails ( gr ) ) ;
167
	while ( !IS_NULL_list ( br ) ) {
168
	    GRAPH gs = DEREF_graph ( HEAD_list ( br ) ) ;
169
	    LIST ( GRAPH ) ps = deduce_graph ( gs, ct, pids ) ;
170
	    while ( !IS_NULL_list ( ps ) ) {
171
		/* Add deduced graphs to list */
172
		LIST ( GRAPH ) pt = pr ;
173
		DESTROY_CONS_graph ( destroy, gs, ps, ps ) ;
174
		while ( !IS_NULL_list ( pt ) ) {
175
		    /* Search for previous deductions */
176
		    GRAPH gt = DEREF_graph ( HEAD_list ( pt ) ) ;
177
		    if ( eq_graph ( gt, gs ) ) break ;
178
		    pt = TAIL_list ( pt ) ;
179
		}
180
		if ( IS_NULL_list ( pt ) ) {
181
		    /* Not previously deduced */
182
		    CONS_graph ( gs, pr, pr ) ;
183
		}
184
	    }
185
	    br = TAIL_list ( br ) ;
186
	}
187
    }
188
    return ( pr ) ;
189
}
190
 
191
 
192
/*
193
    DEDUCE A DERIVED FUNCTION TEMPLATE PARAMETER TYPE
194
 
195
    This routine attempts to deduce the function template parameter type t
196
    from the corresponding function argument type s in the case where both
197
    are classes.  s can be deduced to be a derived class of a template class
198
    rather than having to be equal to t.
199
*/
200
 
201
static int deduce_derive
202
    PROTO_N ( ( t, s, pids ) )
203
    PROTO_T ( TYPE t X TYPE s X LIST ( IDENTIFIER ) pids )
204
{
205
    CLASS_TYPE cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
206
    CLASS_INFO ci = DEREF_cinfo ( ctype_info ( cs ) ) ;
207
    if ( ci & cinfo_templ_base ) {
208
	/* Only check if s has a template base class */
209
	CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
210
	GRAPH gs = DEREF_graph ( ctype_base ( cs ) ) ;
211
	LIST ( GRAPH ) ps = deduce_graph ( gs, ct, pids ) ;
212
	if ( !IS_NULL_list ( ps ) ) {
213
	    /* Deduction succeeded */
214
	    DESTROY_CONS_graph ( destroy, gs, ps, ps ) ;
215
	    if ( IS_NULL_list ( ps ) ) {
216
		/* Unambiguous deduction */
217
		TYPE fs ;
218
		cs = DEREF_ctype ( graph_head ( gs ) ) ;
219
		fs = DEREF_type ( ctype_form ( cs ) ) ;
220
		if ( !IS_NULL_type ( fs ) && IS_type_token ( fs ) ) {
221
		    IDENTIFIER fid = DEREF_id ( type_token_tok ( fs ) ) ;
222
		    if ( !IS_id_token ( fid ) ) {
223
			/* cs is a template class */
224
			return ( eq_ctype ( ct, cs ) ) ;
225
		    }
226
		}
227
		return ( 0 ) ;
228
	    }
229
	    DESTROY_list ( ps, SIZE_graph ) ;
230
	}
231
    }
232
    return ( 0 ) ;
233
}
234
 
235
 
236
/*
237
    DEDUCE A FUNCTION TEMPLATE PARAMETER TYPE
238
 
239
    This routine attempts to deduce the function template parameter type t
240
    from the corresponding function argument type s.  Qualification
241
    conversions and other inexact type conversions are allowed.  The
242
    routine returns true if the deduction was successful.
243
*/
244
 
245
static int deduce_param
246
    PROTO_N ( ( t, s, pids ) )
247
    PROTO_T ( TYPE t X TYPE s X LIST ( IDENTIFIER ) pids )
248
{
249
    int go = 1 ;
250
    int depth = 0 ;
251
    int all_const = 1 ;
252
    do {
253
	unsigned nt = TAG_type ( t ) ;
254
	unsigned ns = TAG_type ( s ) ;
255
	CV_SPEC qt = find_cv_qual ( t ) ;
256
	CV_SPEC qs = find_cv_qual ( s ) ;
257
	qt &= cv_qual ;
258
	qs &= cv_qual ;
259
	if ( qt != qs ) {
260
	    /* Allow for qualification conversions */
261
	    if ( ( qs & ~qt ) || !all_const ) {
262
		return ( eq_type ( t, s ) ) ;
263
	    }
264
	}
265
	if ( depth && !( qt & cv_const ) ) all_const = 0 ;
266
	if ( nt == ns ) {
267
	    switch ( nt ) {
268
		case type_ptr_tag :
269
		case type_ref_tag : {
270
		    /* Continue checking pointer types */
271
		    t = DEREF_type ( type_ptr_etc_sub ( t ) ) ;
272
		    s = DEREF_type ( type_ptr_etc_sub ( s ) ) ;
273
		    depth++ ;
274
		    break ;
275
		}
276
		case type_ptr_mem_tag : {
277
		    /* Continue checking pointer to member types */
278
		    CLASS_TYPE ct = DEREF_ctype ( type_ptr_mem_of ( t ) ) ;
279
		    CLASS_TYPE cs = DEREF_ctype ( type_ptr_mem_of ( s ) ) ;
280
		    if ( !eq_ctype ( ct, cs ) ) return ( 0 ) ;
281
		    t = DEREF_type ( type_ptr_mem_sub ( t ) ) ;
282
		    s = DEREF_type ( type_ptr_mem_sub ( s ) ) ;
283
		    depth += 2 ;
284
		    break ;
285
		}
286
		case type_compound_tag : {
287
		    /* Allow derived template classes */
288
		    if ( depth < 2 && deduce_derive ( t, s, pids ) ) {
289
			return ( 1 ) ;
290
		    }
291
		    go = 0 ;
292
		    break ;
293
		}
294
		default : {
295
		    /* Now check for equality */
296
		    go = 0 ;
297
		    break ;
298
		}
299
	    }
300
	} else {
301
	    /* Now check for equality */
302
	    go = 0 ;
303
	}
304
    } while ( go ) ;
305
    return ( eq_type_unqual ( t, s ) ) ;
306
}
307
 
308
 
309
/*
310
    PERFORM ARGUMENT DEDUCTION FOR A FUNCTION TEMPLATE
311
 
312
    This routine performs argument deduction for the call of the function
313
    template id with the arguments args.  Qualification conversions and
314
    other inexact deductions are allowed if qual is true.  The null
315
    identifier is returned to indicate that argument deduction fails.
316
    The instance corresponding to the deduced arguments is only created
317
    and returned if create is true.  If force is true then type deduction
318
    continues after a failure has occurred.
319
*/
320
 
321
IDENTIFIER deduce_args
322
    PROTO_N ( ( id, args, qual, force, create, err ) )
323
    PROTO_T ( IDENTIFIER id X LIST ( EXP ) args X int qual X int force X
324
	      int create X ERROR *err )
325
{
326
    IDENTIFIER rid = NULL_id ;
327
    TYPE s = DEREF_type ( id_function_etc_type ( id ) ) ;
328
    TYPE r = DEREF_type ( type_templ_defn ( s ) ) ;
329
    if ( IS_type_func ( r ) ) {
330
	int d ;
331
	int ok = 2 ;
332
	int started = 0 ;
333
	ERROR err2 = NULL_err ;
334
	TOKEN sort = DEREF_tok ( type_templ_sort ( s ) ) ;
335
	LIST ( TYPE ) pars = DEREF_list ( type_func_mtypes ( r ) ) ;
336
	LIST ( IDENTIFIER ) pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
337
	if ( err == KILL_err ) err = &err2 ;
338
	force_template++ ;
339
	d = save_token_args ( pids, NULL_list ( TOKEN ) ) ;
340
 
341
	/* Scan through arguments */
342
	while ( !IS_NULL_list ( pars ) && !IS_NULL_list ( args ) ) {
343
	    int dep = 1 ;
344
	    TYPE p = DEREF_type ( HEAD_list ( pars ) ) ;
345
	    EXP a = DEREF_exp ( HEAD_list ( args ) ) ;
346
	    if ( qual ) dep = depends_on ( p, pids ) ;
347
	    if ( !IS_NULL_exp ( a ) && dep ) {
348
		int eq ;
349
		TYPE q ;
350
		int d2 = 0 ;
351
		ERROR ferr = NULL_err ;
352
		if ( started ) {
353
		    /* Each argument deduction is independent */
354
		    d2 = save_token_args ( pids, NULL_list ( TOKEN ) ) ;
355
		}
356
		if ( IS_type_ref ( p ) ) {
357
		    /* Use referenced type for type deduction */
358
		    p = DEREF_type ( type_ref_sub ( p ) ) ;
359
		    a = resolve_cast ( p, a, &ferr, 0, 0, pids ) ;
360
		    if ( IS_NULL_exp ( a ) ) {
361
			q = redef_type ;
362
		    } else {
363
			q = DEREF_type ( exp_type ( a ) ) ;
364
		    }
365
		} else {
366
		    /* Convert argument type */
367
		    a = resolve_cast ( p, a, &ferr, 0, 0, pids ) ;
368
		    if ( IS_NULL_exp ( a ) ) {
369
			q = redef_type ;
370
		    } else {
371
			q = DEREF_type ( exp_type ( a ) ) ;
372
			switch ( TAG_type ( q ) ) {
373
			    case type_func_tag : {
374
				/* Function to pointer */
375
				MAKE_type_ptr ( cv_none, q, q ) ;
376
				break ;
377
			    }
378
			    case type_array_tag : {
379
				/* Array to pointer */
380
				q = DEREF_type ( type_array_sub ( q ) ) ;
381
				MAKE_type_ptr ( cv_none, q, q ) ;
382
				break ;
383
			    }
384
			    case type_bitfield_tag : {
385
				/* Promote bitfields */
386
				q = promote_type ( q ) ;
387
				break ;
388
			    }
389
			    default : {
390
				/* Ignore type qualifiers */
391
				q = qualify_type ( q, cv_none, 0 ) ;
392
				break ;
393
			    }
394
			}
395
		    }
396
		}
397
		if ( qual ) {
398
		    /* Allow qualification conversions */
399
		    eq = deduce_param ( p, q, pids ) ;
400
		} else {
401
		    /* Require exact conversion */
402
		    eq = eq_type ( p, q ) ;
403
		}
404
		if ( !eq ) {
405
		    /* Type deduction should be identical */
406
		    ok = force ;
407
		}
408
		if ( started ) {
409
		    /* Combine argument deductions */
410
		    if ( !merge_token_args ( pids, d2, qual ) ) ok = force ;
411
		}
412
		started = 1 ;
413
		if ( !ok ) break ;
414
	    }
415
	    args = TAIL_list ( args ) ;
416
	    pars = TAIL_list ( pars ) ;
417
	}
418
 
419
	/* Check the combined results */
420
	if ( ok ) {
421
	    LIST ( TOKEN ) targs = make_token_args ( id, pids, err ) ;
422
	    if ( ok != 2 ) {
423
		/* Report unviable resolution */
424
		add_error ( err, ERR_over_match_viable_none ( id ) ) ;
425
	    }
426
	    if ( IS_NULL_err ( *err ) || force ) {
427
		/* Successful deduction */
428
		if ( create ) {
429
		    check_deduced_args ( id, pids, targs ) ;
430
		    rid = inst_func_deduce ( id, pids, targs, d ) ;
431
		    force_template-- ;
432
		    return ( rid ) ;
433
		}
434
		rid = id ;
435
	    }
436
	    DESTROY_list ( targs, SIZE_tok ) ;
437
	}
438
	restore_token_args ( pids, d ) ;
439
	force_template-- ;
440
    }
441
    UNUSED ( qual ) ;
442
    return ( rid ) ;
443
}
444
 
445
 
446
/*
447
    SPECIALISE A FUNCTION TEMPLATE TO A FUNCTION TYPE
448
 
449
    This routine constructs checks whether an instance of the function
450
    template id of type t exists.  The null identifier is returned to
451
    indicate that no such instance exists.  peq is as in resolve_func.
452
*/
453
 
454
IDENTIFIER deduce_func
455
    PROTO_N ( ( id, t, peq ) )
456
    PROTO_T ( IDENTIFIER id X TYPE t X int *peq )
457
{
458
    TYPE s = DEREF_type ( id_function_etc_type ( id ) ) ;
459
    if ( IS_type_templ ( s ) ) {
460
	/* Template function */
461
	int d ;
462
	int eq = 0 ;
463
	TYPE r = DEREF_type ( type_templ_defn ( s ) ) ;
464
	TOKEN sort = DEREF_tok ( type_templ_sort ( s ) ) ;
465
	LIST ( IDENTIFIER ) pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
466
	force_template++ ;
467
	d = save_token_args ( pids, NULL_list ( TOKEN ) ) ;
468
	if ( IS_type_func ( r ) && IS_type_func ( t ) ) {
469
	    eq = eq_func_type ( r, t, 1, 0 ) ;
470
	} else {
471
	    int cmp = eq_type ( r, t ) ;
472
	    if ( cmp == 1 || cmp == 2 ) eq = 3 ;
473
	}
474
	if ( eq >= 2 ) {
475
	    /* Types match - form instance */
476
	    ERROR err = NULL_err ;
477
	    LIST ( TOKEN ) args = make_token_args ( id, pids, &err ) ;
478
	    if ( IS_NULL_err ( err ) ) {
479
		/* Successful deduction */
480
		IDENTIFIER rid ;
481
		check_deduced_args ( id, pids, args ) ;
482
		rid = inst_func_deduce ( id, pids, args, d ) ;
483
		force_template-- ;
484
		*peq = eq ;
485
		return ( rid ) ;
486
	    }
487
	    destroy_error ( err, 1 ) ;
488
	}
489
	restore_token_args ( pids, d ) ;
490
	force_template-- ;
491
    } else {
492
	/* Simple function */
493
	int eq = eq_func_type ( s, t, 1, 0 ) ;
494
	if ( eq >= 2 ) {
495
	    *peq = eq ;
496
	    return ( id ) ;
497
	}
498
    }
499
    *peq = 0 ;
500
    return ( NULL_id ) ;
501
}
502
 
503
 
504
/*
505
    DEDUCE A CONVERSION FUNCTION TYPE
506
 
507
    This routine is used to find the type of the specialisation of a
508
    template conversion function of type t which may be used for a
509
    conversion to type r.  If no such specialisation exists the null
510
    type is returned.
511
*/
512
 
513
TYPE deduce_conv
514
    PROTO_N ( ( t, r ) )
515
    PROTO_T ( TYPE t X TYPE r )
516
{
517
    if ( IS_type_templ ( t ) ) {
518
	int d ;
519
	TOKEN sort = DEREF_tok ( type_templ_sort ( t ) ) ;
520
	LIST ( IDENTIFIER ) pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
521
	force_template++ ;
522
	d = save_token_args ( pids, NULL_list ( TOKEN ) ) ;
523
	t = DEREF_type ( type_templ_defn ( t ) ) ;
524
	t = deduce_conv ( t, r ) ;
525
	restore_token_args ( pids, d ) ;
526
	force_template-- ;
527
    } else {
528
	int eq ;
529
	CV_SPEC cv ;
530
	TYPE s = DEREF_type ( type_func_ret ( t ) ) ;
531
	if ( IS_type_ref ( s ) && !IS_type_ref ( r ) ) {
532
	    s = DEREF_type ( type_ref_sub ( s ) ) ;
533
	}
534
	cv = DEREF_cv ( type_qual ( r ) ) ;
535
	s = qualify_type ( s, cv, 0 ) ;
536
	eq = eq_type ( s, r ) ;
537
	if ( eq == 1 || eq == 2 ) {
538
	    /* Match found */
539
	    t = expand_type ( t, 2 ) ;
540
	} else {
541
	    /* No match found */
542
	    t = NULL_type ;
543
	}
544
    }
545
    return ( t ) ;
546
}
547
 
548
 
549
/*
550
    FIND AN UNDERLYING TEMPLATE
551
 
552
    This routine finds the underlying form for the template application id.
553
    If this is an undefined class then pi is set to true.
554
*/
555
 
556
TYPE find_form
557
    PROTO_N ( ( id, pi ) )
558
    PROTO_T ( IDENTIFIER id X int *pi )
559
{
560
    TYPE t = NULL_type ;
561
    if ( !IS_NULL_id ( id ) ) {
562
	switch ( TAG_id ( id ) ) {
563
	    case id_class_name_tag : {
564
		/* Template classes */
565
		CLASS_TYPE ct ;
566
		CLASS_INFO ci ;
567
		t = DEREF_type ( id_class_name_defn ( id ) ) ;
568
		while ( IS_type_templ ( t ) ) {
569
		    t = DEREF_type ( type_templ_defn ( t ) ) ;
570
		}
571
		ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
572
		ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
573
		if ( !( ci & cinfo_defined ) ) *pi = 1 ;
574
		t = DEREF_type ( ctype_form ( ct ) ) ;
575
		break ;
576
	    }
577
	    case id_function_tag :
578
	    case id_mem_func_tag :
579
	    case id_stat_mem_func_tag : {
580
		/* Template functions */
581
		t = DEREF_type ( id_function_etc_form ( id ) ) ;
582
		break ;
583
	    }
584
	    case id_stat_member_tag : {
585
		/* Static data members of template classes */
586
		EXP d = DEREF_exp ( id_stat_member_term ( id ) ) ;
587
		if ( !IS_NULL_exp ( d ) && IS_exp_paren ( d ) ) {
588
		    t = DEREF_type ( exp_type ( d ) ) ;
589
		}
590
		break ;
591
	    }
592
	}
593
    }
594
    return ( t ) ;
595
}
596
 
597
 
598
/*
599
    FIND THE INSTANCES FOR A TEMPLATE
600
 
601
    This routine returns the list of all instances for the template tid.
602
*/
603
 
604
static INSTANCE find_templ_apps
605
    PROTO_N ( ( tid ) )
606
    PROTO_T ( IDENTIFIER tid )
607
{
608
    TYPE s ;
609
    TOKEN sort ;
610
    INSTANCE apps ;
611
    if ( IS_id_class_name_etc ( tid ) ) {
612
	s = DEREF_type ( id_class_name_etc_defn ( tid ) ) ;
613
    } else {
614
	s = DEREF_type ( id_function_etc_type ( tid ) ) ;
615
    }
616
    sort = DEREF_tok ( type_templ_sort ( s ) ) ;
617
    apps = DEREF_inst ( tok_templ_apps ( sort ) ) ;
618
    return ( apps ) ;
619
}
620
 
621
 
622
/*
623
    FIND A TEMPLATE INSTANCE
624
 
625
    This routine searches for a previous instance of the template tid
626
    of sort tok with the template arguments args.
627
*/
628
 
629
static IDENTIFIER find_instance
630
    PROTO_N ( ( tid, tok, args, def ) )
631
    PROTO_T ( IDENTIFIER tid X TOKEN tok X LIST ( TOKEN ) args X int def )
632
{
633
    INSTANCE apps ;
634
    int fs = force_tokdef ;
635
    int ft = force_template ;
636
    force_tokdef = 0 ;
637
    force_template = 0 ;
638
    apps = DEREF_inst ( tok_templ_apps ( tok ) ) ;
639
    while ( !IS_NULL_inst ( apps ) ) {
640
	DECL_SPEC acc = DEREF_dspec ( inst_templ_access ( apps ) ) ;
641
	if ( !( acc & dspec_alias ) ) {
642
	    IDENTIFIER fid ;
643
	    LIST ( TOKEN ) fargs ;
644
	    TYPE form = DEREF_type ( inst_form ( apps ) ) ;
645
	    while ( IS_type_templ ( form ) ) {
646
		form = DEREF_type ( type_templ_defn ( form ) ) ;
647
	    }
648
	    fid = DEREF_id ( type_token_tok ( form ) ) ;
649
	    fargs = DEREF_list ( type_token_args ( form ) ) ;
650
	    if ( eq_token_args ( tid, fid, args, fargs ) ) {
651
		/* Match found */
652
		IDENTIFIER id = DEREF_id ( inst_templ_id ( apps ) ) ;
653
		if ( def ) {
654
		    /* Mark instance as used */
655
		    acc |= dspec_used ;
656
		    COPY_dspec ( inst_templ_access ( apps ), acc ) ;
657
		}
658
		force_template = ft ;
659
		force_tokdef = fs ;
660
		return ( id ) ;
661
	    }
662
	}
663
	apps = DEREF_inst ( inst_next ( apps ) ) ;
664
    }
665
    force_template = ft ;
666
    force_tokdef = fs ;
667
    return ( NULL_id ) ;
668
}
669
 
670
 
671
/*
672
    VALIDATE A SET OF TEMPLATE ARGUMENTS
673
 
674
    This routine performs a final validation for the template arguments
675
    args for the template id of sort sort.
676
*/
677
 
678
static void valid_template_args
679
    PROTO_N ( ( id, sort, args ) )
680
    PROTO_T ( IDENTIFIER id X TOKEN sort X LIST ( TOKEN ) args )
681
{
682
    LIST ( IDENTIFIER ) pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
683
    while ( !IS_NULL_list ( pids ) && !IS_NULL_list ( args ) ) {
684
	TOKEN arg = DEREF_tok ( HEAD_list ( args ) ) ;
685
	IDENTIFIER pid = DEREF_id ( HEAD_list ( pids ) ) ;
686
	TOKEN par = DEREF_tok ( id_token_sort ( pid ) ) ;
687
	unsigned kind = TAG_tok ( par ) ;
688
	if ( !IS_NULL_tok ( arg ) && TAG_tok ( arg ) == kind ) {
689
	    if ( kind == tok_type_tag ) {
690
		/* Check template types */
691
		TYPE t = DEREF_type ( tok_type_value ( arg ) ) ;
692
		BASE_TYPE bt = DEREF_btype ( tok_type_kind ( par ) ) ;
693
		bt &= btype_named ;
694
		if ( bt != btype_none ) {
695
		    /* Check elaborated template type */
696
		    int ok = 0 ;
697
		    unsigned tag = TAG_type ( t ) ;
698
		    if ( bt == btype_enum ) {
699
			if ( tag == type_enumerate_tag ) ok = 1 ;
700
		    } else {
701
			if ( tag == type_compound_tag ) {
702
			    CLASS_TYPE ct ;
703
			    BASE_TYPE key ;
704
			    ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
705
			    key = find_class_key ( ct ) ;
706
			    ok = equal_key ( bt, key ) ;
707
			}
708
		    }
709
		    if ( !ok && tag != type_error_tag ) {
710
			/* Report type mismatch */
711
			ERROR err = ERR_temp_res_key ( bt, pid, id, t ) ;
712
			report ( crt_loc, err ) ;
713
		    }
714
		}
715
		break ;
716
	    }
717
	}
718
	pids = TAIL_list ( pids ) ;
719
	args = TAIL_list ( args ) ;
720
    }
721
    return ;
722
}
723
 
724
 
725
/*
726
    FIND THE MOST SPECIALISED OF A LIST OF SPECIALISATIONS
727
 
728
    This routine forms the main body of body_match.  It runs a tournament
729
    to find the most specialised of the template specialisations apps.
730
    Note that the result is not necessarily more specialised than all
731
    the other elements of apps, but if there is such a most specialised
732
    element then it will be the winner of this tournament.
733
*/
734
 
735
static INSTANCE best_match_aux
736
    PROTO_N ( ( apps ) )
737
    PROTO_T ( LIST ( INSTANCE ) apps )
738
{
739
    int cmp ;
740
    TYPE t, s ;
741
    INSTANCE a, b ;
742
    if ( IS_NULL_list ( apps ) ) return ( NULL_inst ) ;
743
    a = DEREF_inst ( HEAD_list ( apps ) ) ;
744
    b = best_match_aux ( TAIL_list ( apps ) ) ;
745
    if ( IS_NULL_inst ( b ) ) return ( a ) ;
746
    t = DEREF_type ( inst_form ( a ) ) ;
747
    s = DEREF_type ( inst_form ( b ) ) ;
748
    cmp = eq_type ( t, s ) ;
749
    if ( cmp == 2 ) return ( b ) ;
750
    if ( cmp == 3 ) return ( a ) ;
751
    return ( NULL_inst ) ;
752
}
753
 
754
 
755
/*
756
    FIND THE MOST SPECIALISED OF A LIST OF SPECIALISATIONS
757
 
758
    This routine finds the most specialised of the template specialisations
759
    apps.  It returns the null instance if there is no match or the result
760
    is ambiguous.
761
*/
762
 
763
static INSTANCE best_match
764
    PROTO_N ( ( apps ) )
765
    PROTO_T ( LIST ( INSTANCE ) apps )
766
{
767
    INSTANCE a = best_match_aux ( apps ) ;
768
    if ( !IS_NULL_inst ( a ) && LENGTH_list ( apps ) > 2 ) {
769
	TYPE t = DEREF_type ( inst_form ( a ) ) ;
770
	while ( !IS_NULL_list ( apps ) ) {
771
	    INSTANCE b = DEREF_inst ( HEAD_list ( apps ) ) ;
772
	    if ( !EQ_inst ( b, a ) ) {
773
		TYPE s = DEREF_type ( inst_form ( b ) ) ;
774
		int cmp = eq_type ( t, s ) ;
775
		if ( cmp != 3 ) {
776
		    /* a is not more specialised than b */
777
		    a = NULL_inst ;
778
		    break ;
779
		}
780
	    }
781
	    apps = TAIL_list ( apps ) ;
782
	}
783
    }
784
    return ( a ) ;
785
}
786
 
787
 
788
/*
789
    DOES A TEMPLATE CLASS INSTANCE SPECIALISE A MEMBER?
790
 
791
    This routine checks whether the template class instance app contains
792
    a specialisation of the member mid.
793
*/
794
 
795
static int specialise_member
796
    PROTO_N ( ( app, mid ) )
797
    PROTO_T ( INSTANCE app X IDENTIFIER mid )
798
{
799
    if ( !IS_NULL_id ( mid ) ) {
800
	LIST ( IDENTIFIER ) mems = DEREF_list ( inst_templ_mems ( app ) ) ;
801
	while ( !IS_NULL_list ( mems ) ) {
802
	    IDENTIFIER nid = DEREF_id ( HEAD_list ( mems ) ) ;
803
	    if ( EQ_id ( nid, mid ) ) return ( 1 ) ;
804
	    mems = TAIL_list ( mems ) ;
805
	}
806
    }
807
    return ( 0 ) ;
808
}
809
 
810
 
811
/*
812
    FIND THE MOST SPECIALISED TEMPLATE MATCHING AN INSTANCE
813
 
814
    This routine returns the most specialised template specialisation
815
    from the list apps which contains a specialisation of the member mid
816
    and matches the instance form.  If there is no such specialisation
817
    or the result is ambiguous then the null instance is returned.
818
*/
819
 
820
static INSTANCE match_form
821
    PROTO_N ( ( app, form, mid ) )
822
    PROTO_T ( INSTANCE app X TYPE form X IDENTIFIER mid )
823
{
824
    INSTANCE best = NULL_inst ;
825
    LIST ( INSTANCE ) match = NULL_list ( INSTANCE ) ;
826
    force_template++ ;
827
    while ( !IS_NULL_inst ( app ) ) {
828
	DECL_SPEC acc = DEREF_dspec ( inst_templ_access ( app ) ) ;
829
	if ( !( acc & ( dspec_alias | dspec_main ) ) ) {
830
	    if ( ( acc & dspec_extern ) || specialise_member ( app, mid ) ) {
831
		TYPE prev = DEREF_type ( inst_form ( app ) ) ;
832
		int cmp = eq_type ( prev, form ) ;
833
		if ( cmp == 1 || cmp == 2 ) {
834
		    /* Matches specialisation */
835
		    CONS_inst ( app, match, match ) ;
836
		}
837
	    }
838
	}
839
	app = DEREF_inst ( inst_next ( app ) ) ;
840
    }
841
    if ( !IS_NULL_list ( match ) ) {
842
	/* Determine most specialised match */
843
	best = best_match ( match ) ;
844
	if ( IS_NULL_inst ( best ) ) {
845
	    /* Ambiguous specialisation */
846
	    report ( crt_loc, ERR_temp_class_spec_ambig ( form ) ) ;
847
	} else {
848
	    /* Unambiguous specialisation */
849
	    IDENTIFIER bid = DEREF_id ( inst_templ_id ( best ) ) ;
850
	    report ( crt_loc, ERR_temp_class_spec_match ( bid ) ) ;
851
	}
852
	DESTROY_list ( match, SIZE_inst ) ;
853
    }
854
    force_template-- ;
855
    return ( best ) ;
856
}
857
 
858
 
859
/*
860
    DEDUCE ARGUMENTS FOR TEMPLATE SPECIALISATION
861
 
862
    This routine deduces the arguments required for the template
863
    specialisation spec to instantiate the matching instance form.
864
*/
865
 
866
static TYPE specialise_args
867
    PROTO_N ( ( spec, form ) )
868
    PROTO_T ( INSTANCE spec X TYPE form )
869
{
870
    TYPE s = DEREF_type ( inst_form ( spec ) ) ;
871
    if ( IS_type_templ ( s ) ) {
872
	int d ;
873
	int eq ;
874
	TYPE r = DEREF_type ( type_templ_defn ( s ) ) ;
875
	TOKEN sort = DEREF_tok ( type_templ_sort ( s ) ) ;
876
	LIST ( IDENTIFIER ) pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
877
	force_template++ ;
878
	d = save_token_args ( pids, NULL_list ( TOKEN ) ) ;
879
	eq = eq_type ( r, form ) ;
880
	if ( eq == 1 || eq == 2 ) {
881
	    /* Argument deduction successful */
882
	    ERROR err = NULL_err ;
883
	    IDENTIFIER id = DEREF_id ( inst_templ_id ( spec ) ) ;
884
	    LIST ( TOKEN ) args = make_token_args ( id, pids, &err ) ;
885
	    if ( IS_NULL_err ( err ) ) {
886
		/* Successful deduction */
887
		MAKE_type_token ( cv_none, id, args, form ) ;
888
		COPY_inst ( type_token_app ( form ), spec ) ;
889
	    } else {
890
		destroy_error ( err, 1 ) ;
891
	    }
892
	}
893
	restore_token_args ( pids, d ) ;
894
	force_template-- ;
895
    } else {
896
	IDENTIFIER id = DEREF_id ( inst_templ_id ( spec ) ) ;
897
	MAKE_type_token ( cv_none, id, NULL_list ( TOKEN ), form ) ;
898
    }
899
    return ( form ) ;
900
}
901
 
902
 
903
/*
904
    MATCH A TEMPLATE SPECIALISATION
905
 
906
    This routine finds the template specialisation which best matches the
907
    template instance given by form.  If mid is the null identifier then
908
    only explicit specialisations are considered.  Otherwise any
909
    specialisation which specialises the member mid is considered.
910
*/
911
 
912
static TYPE specialise_form
913
    PROTO_N ( ( form, mid ) )
914
    PROTO_T ( TYPE form X IDENTIFIER mid )
915
{
916
    if ( !IS_NULL_type ( form ) && IS_type_token ( form ) ) {
917
	IDENTIFIER tid = DEREF_id ( type_token_tok ( form ) ) ;
918
	INSTANCE app = DEREF_inst ( type_token_app ( form ) ) ;
919
	if ( !IS_NULL_inst ( app ) ) {
920
	    TYPE spec = DEREF_type ( inst_templ_spec ( app ) ) ;
921
	    if ( IS_NULL_type ( spec ) || !IS_NULL_id ( mid ) ) {
922
		/* Not previously determined */
923
		DECL_SPEC acc = DEREF_dspec ( inst_templ_access ( app ) ) ;
924
		if ( acc & dspec_explicit ) {
925
		    /* Explicit specialisation */
926
		    IDENTIFIER id = DEREF_id ( inst_templ_id ( app ) ) ;
927
		    LIST ( TOKEN ) args = NULL_list ( TOKEN ) ;
928
		    MAKE_type_token ( cv_none, id, args, spec ) ;
929
		    COPY_inst ( type_token_app ( spec ), app ) ;
930
		} else if ( acc & dspec_instance ) {
931
		    /* Find matching partial specialisations */
932
		    INSTANCE best = find_templ_apps ( tid ) ;
933
		    best = match_form ( best, form, mid ) ;
934
		    if ( !IS_NULL_inst ( best ) ) {
935
			/* Use matching specialisation */
936
			spec = specialise_args ( best, form ) ;
937
		    } else {
938
			/* Use primary form */
939
			spec = form ;
940
		    }
941
		}
942
		if ( IS_NULL_id ( mid ) ) {
943
		    /* Record best explicit match */
944
		    COPY_type ( inst_templ_spec ( app ), spec ) ;
945
		}
946
	    }
947
	    if ( !IS_NULL_type ( spec ) ) form = spec ;
948
	}
949
    }
950
    return ( form ) ;
951
}
952
 
953
 
954
/*
955
    FIND THE COPIED VERSION OF A CLASS MEMBER
956
 
957
    This routine is identical to find_copied except that it allows for
958
    template instances when type is not 2.
959
*/
960
 
961
static IDENTIFIER find_copied_member
962
    PROTO_N ( ( cid, id, res, type ) )
963
    PROTO_T ( IDENTIFIER cid X IDENTIFIER id X int res X int type )
964
{
965
    if ( type != 2 ) {
966
	int undef = 0 ;
967
	TYPE form = find_form ( id, &undef ) ;
968
	if ( !IS_NULL_type ( form ) && IS_type_token ( form ) ) {
969
	    /* Template instance */
970
	    IDENTIFIER tid = DEREF_id ( type_token_tok ( form ) ) ;
971
	    LIST ( TOKEN ) args = DEREF_list ( type_token_args ( form ) ) ;
972
	    tid = find_copied ( cid, tid, 1 ) ;
973
	    id = apply_template ( tid, args, 0, 0 ) ;
974
	    return ( id ) ;
975
	}
976
    }
977
    id = find_copied ( cid, id, res ) ;
978
    return ( id ) ;
979
}
980
 
981
 
982
/*
983
    MATCH A TEMPLATE MEMBER SPECIALISATION
984
 
985
    This routine finds the specialisation best matching the template instance
986
    or class template member id and specialising the member pid.
987
*/
988
 
989
static IDENTIFIER match_specialise
990
    PROTO_N ( ( id, pid ) )
991
    PROTO_T ( IDENTIFIER id X IDENTIFIER pid )
992
{
993
    int undef = 0 ;
994
    IDENTIFIER tid = NULL_id ;
995
    TYPE form = find_form ( id, &undef ) ;
996
    if ( !IS_NULL_type ( form ) ) {
997
	if ( IS_type_token ( form ) ) {
998
	    /* Template instance */
999
	    TYPE spec = specialise_form ( form, pid ) ;
1000
	    tid = DEREF_id ( type_token_tok ( spec ) ) ;
1001
	} else if ( IS_type_instance ( form ) ) {
1002
	    /* Member of template class */
1003
	    NAMESPACE cns ;
1004
	    tid = DEREF_id ( type_instance_id ( form ) ) ;
1005
	    cns = DEREF_nspace ( id_parent ( id ) ) ;
1006
	    if ( !IS_NULL_nspace ( cns ) ) {
1007
		IDENTIFIER cid = DEREF_id ( nspace_name ( cns ) ) ;
1008
		IDENTIFIER rid = match_specialise ( cid, tid ) ;
1009
		if ( !IS_NULL_id ( rid ) ) {
1010
		    if ( EQ_id ( rid, cid ) ) {
1011
			tid = id ;
1012
		    } else {
1013
			tid = find_copied_member ( rid, tid, 1, 2 ) ;
1014
		    }
1015
		}
1016
	    }
1017
	}
1018
    }
1019
    return ( tid ) ;
1020
}
1021
 
1022
 
1023
/*
1024
    SET TEMPLATE PARAMETERS
1025
 
1026
    This routine sets the template parameters for the instance form.
1027
*/
1028
 
1029
static int set_templ_args
1030
    PROTO_N ( ( form ) )
1031
    PROTO_T ( TYPE form )
1032
{
1033
    int d = 0 ;
1034
    INSTANCE app = DEREF_inst ( type_token_app ( form ) ) ;
1035
    TYPE spec = DEREF_type ( inst_form ( app ) ) ;
1036
    if ( IS_type_templ ( spec ) ) {
1037
	TOKEN sort = DEREF_tok ( type_templ_sort ( spec ) ) ;
1038
	LIST ( IDENTIFIER ) pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
1039
	LIST ( TOKEN ) args = DEREF_list ( type_token_args ( form ) ) ;
1040
	d = save_token_args ( pids, args ) ;
1041
    }
1042
    return ( d ) ;
1043
}
1044
 
1045
 
1046
/*
1047
    RESTORE TEMPLATE PARAMETERS
1048
 
1049
    This routine restores the template parameters for the instance form.
1050
    d is the value returned from the corresponding call to set_templ_args.
1051
*/
1052
 
1053
static void restore_templ_args
1054
    PROTO_N ( ( form, d ) )
1055
    PROTO_T ( TYPE form X int d )
1056
{
1057
    INSTANCE app = DEREF_inst ( type_token_app ( form ) ) ;
1058
    TYPE spec = DEREF_type ( inst_form ( app ) ) ;
1059
    if ( IS_type_templ ( spec ) ) {
1060
	TOKEN sort = DEREF_tok ( type_templ_sort ( spec ) ) ;
1061
	LIST ( IDENTIFIER ) pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
1062
	restore_token_args ( pids, d ) ;
1063
    }
1064
    return ;
1065
}
1066
 
1067
 
1068
/*
1069
    REPORT THE INSTANTIATION OF A TEMPLATE
1070
 
1071
    This routine reports the instantiation of the template with the
1072
    given form.
1073
*/
1074
 
1075
static void report_instance
1076
    PROTO_N ( ( form ) )
1077
    PROTO_T ( TYPE form )
1078
{
1079
    ERROR err = ERR_temp_inst_def ( form ) ;
1080
    if ( !IS_NULL_err ( err ) ) {
1081
	if ( is_templ_depend ( form ) ) {
1082
	    destroy_error ( err, 1 ) ;
1083
	} else {
1084
	    report ( crt_loc, err ) ;
1085
	}
1086
    }
1087
    return ;
1088
}
1089
 
1090
 
1091
/*
1092
    INSTANTIATE A FUNCTION TEMPLATE
1093
 
1094
    This routine creates an instance of the function template id with
1095
    the template arguments args.
1096
*/
1097
 
1098
IDENTIFIER instance_func
1099
    PROTO_N ( ( id, args, func, def ) )
1100
    PROTO_T ( IDENTIFIER id X LIST ( TOKEN ) args X int func X int def )
1101
{
1102
    int d = 0 ;
1103
    IDENTIFIER tid ;
1104
    IDENTIFIER rid = DEREF_id ( id_alias ( id ) ) ;
1105
    TYPE s = DEREF_type ( id_function_etc_type ( rid ) ) ;
1106
    TOKEN sort = DEREF_tok ( type_templ_sort ( s ) ) ;
1107
    LIST ( IDENTIFIER ) pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
1108
    if ( func ) {
1109
	/* Arguments already bound */
1110
	/* EMPTY */
1111
    } else {
1112
	/* Bind arguments to parameters */
1113
	d = save_token_args ( pids, args ) ;
1114
    }
1115
 
1116
    /* Find template instance */
1117
    tid = find_instance ( rid, sort, args, def ) ;
1118
    if ( IS_NULL_id ( tid ) ) {
1119
	/* Create a new instance */
1120
	TYPE form ;
1121
	ERROR perr ;
1122
	PTR ( LOCATION ) ploc ;
1123
	valid_template_args ( rid, sort, args ) ;
1124
	MAKE_type_token ( cv_none, rid, args, form ) ;
1125
	dump_template++ ;
1126
	ploc = MAKE_ptr ( SIZE_loc ) ;
1127
	COPY_loc ( ploc, crt_loc ) ;
1128
	perr = set_prefix ( ERR_temp_inst_comment ( form, ploc ) ) ;
1129
	if ( incr_value ( OPT_VAL_instance_depth ) ) {
1130
	    DECL_SPEC ds ;
1131
	    INSTANCE apps  ;
1132
	    HASHID nm = DEREF_hashid ( id_name ( rid ) ) ;
1133
 
1134
	    /* Create new instance */
1135
	    tid = copy_id ( rid, 2 ) ;
1136
	    nm = expand_name ( nm, NULL_ctype ) ;
1137
	    COPY_hashid ( id_name ( tid ), nm ) ;
1138
	    ds = DEREF_dspec ( id_storage ( tid ) ) ;
1139
	    ds &= ~( dspec_used | dspec_called | dspec_done | dspec_defn ) ;
1140
	    ds &= ~dspec_template ;
1141
	    ds |= dspec_instance ;
1142
	    COPY_dspec ( id_storage ( tid ), ds ) ;
1143
	    COPY_exp ( id_function_etc_defn ( tid ), NULL_exp ) ;
1144
	    COPY_id ( id_function_etc_over ( tid ), NULL_id ) ;
1145
	    COPY_type ( id_function_etc_form ( tid ), form ) ;
1146
 
1147
	    /* Check operator type */
1148
	    s = DEREF_type ( id_function_etc_type ( tid ) ) ;
1149
#if LANGUAGE_CPP
1150
	    if ( IS_hashid_op ( nm ) ) {
1151
		int mem = 1 ;
1152
		int alloc = 0 ;
1153
		if ( IS_id_function ( tid ) ) mem = 0 ;
1154
		s = check_operator ( s, tid, mem, &alloc ) ;
1155
		if ( alloc ) recheck_allocator ( tid, alloc ) ;
1156
	    }
1157
#endif
1158
 
1159
	    /* Add new template application */
1160
	    if ( IS_type_func ( s ) ) {
1161
		/* Full specialisation */
1162
		ds = dspec_instance ;
1163
	    } else {
1164
		/* Partial specialisation */
1165
		ds = dspec_implicit ;
1166
	    }
1167
	    if ( def ) ds |= dspec_used ;
1168
	    if ( is_templ_depend ( form ) ) ds |= dspec_mutable ;
1169
	    apps = DEREF_inst ( tok_templ_apps ( sort ) ) ;
1170
	    MAKE_inst_templ ( form, apps, tid, ds, all_instances, apps ) ;
1171
	    COPY_inst ( type_token_app ( form ), apps ) ;
1172
	    COPY_inst ( tok_templ_apps ( sort ), apps ) ;
1173
	    all_instances = apps ;
1174
	    if ( do_dump ) {
1175
		/* Dump template instance information */
1176
		dump_declare ( tid, &crt_loc, 0 ) ;
1177
		dump_instance ( tid, form, form ) ;
1178
	    }
1179
 
1180
	} else {
1181
	    /* Instantiation depth too great */
1182
	    tid = rid ;
1183
	}
1184
	decr_value ( OPT_VAL_instance_depth ) ;
1185
	restore_prefix ( perr ) ;
1186
	DESTROY_ptr ( ploc, SIZE_loc ) ;
1187
	dump_template-- ;
1188
    }
1189
    if ( func ) {
1190
	/* Check for templates */
1191
	s = DEREF_type ( id_function_etc_type ( tid ) ) ;
1192
	if ( IS_type_templ ( s ) ) tid = NULL_id ;
1193
    } else {
1194
	restore_token_args ( pids, d ) ;
1195
    }
1196
    return ( tid ) ;
1197
}
1198
 
1199
 
1200
/*
1201
    INSTANTIATE A CLASS TEMPLATE
1202
 
1203
    This routine creates an instance of the class template id with the
1204
    template arguments args.  def is true if the class should be
1205
    defined.
1206
*/
1207
 
1208
IDENTIFIER instance_type
1209
    PROTO_N ( ( id, args, type, def ) )
1210
    PROTO_T ( IDENTIFIER id X LIST ( TOKEN ) args X int type X int def )
1211
{
1212
    int d = 0 ;
1213
    int undef = 0 ;
1214
    CLASS_TYPE cs ;
1215
    IDENTIFIER tid ;
1216
    TYPE form = NULL_type ;
1217
    IDENTIFIER rid = DEREF_id ( id_alias ( id ) ) ;
1218
    TYPE s = DEREF_type ( id_class_name_defn ( rid ) ) ;
1219
    TOKEN sort = DEREF_tok ( type_templ_sort ( s ) ) ;
1220
    LIST ( IDENTIFIER ) pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
1221
    if ( type ) {
1222
	/* Arguments already bound */
1223
	/* EMPTY */
1224
    } else {
1225
	/* Bind arguments to parameters */
1226
	d = save_token_args ( pids, args ) ;
1227
    }
1228
 
1229
    /* Check template class */
1230
    while ( IS_type_templ ( s ) ) {
1231
	s = DEREF_type ( type_templ_defn ( s ) ) ;
1232
    }
1233
    cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
1234
 
1235
    /* Find template instance */
1236
    tid = find_instance ( rid, sort, args, def ) ;
1237
    if ( !IS_NULL_id ( tid ) && def ) {
1238
	form = find_form ( tid, &undef ) ;
1239
    }
1240
    if ( IS_NULL_id ( tid ) || ( def && undef ) ) {
1241
	/* Create a new instance or define an existing one */
1242
	ERROR perr ;
1243
	PTR ( LOCATION ) ploc ;
1244
	if ( IS_NULL_type ( form ) ) {
1245
	    valid_template_args ( rid, sort, args ) ;
1246
	    MAKE_type_token ( cv_none, rid, args, form ) ;
1247
	}
1248
	dump_template++ ;
1249
	if ( def ) report_instance ( form ) ;
1250
	ploc = MAKE_ptr ( SIZE_loc ) ;
1251
	COPY_loc ( ploc, crt_loc ) ;
1252
	perr = set_prefix ( ERR_temp_inst_comment ( form, ploc ) ) ;
1253
	if ( incr_value ( OPT_VAL_instance_depth ) ) {
1254
	    TYPE t ;
1255
	    int d2 = 0 ;
1256
	    DECL_SPEC ds ;
1257
	    CLASS_TYPE ct ;
1258
	    int created = 0 ;
1259
	    TYPE spec = form ;
1260
	    CLASS_INFO ci = cinfo_templ_base ;
1261
 
1262
	    /* Create new instance if necessary */
1263
	    if ( IS_NULL_id ( tid ) ) {
1264
		INSTANCE apps ;
1265
		tid = copy_id ( rid, 2 ) ;
1266
		ds = DEREF_dspec ( id_storage ( tid ) ) ;
1267
		ds &= ~( dspec_used | dspec_done | dspec_defn ) ;
1268
		ds &= ~dspec_template ;
1269
		ds |= dspec_instance ;
1270
		COPY_dspec ( id_storage ( tid ), ds ) ;
1271
 
1272
		/* Add new template application */
1273
		ds = dspec_instance ;
1274
		if ( def ) ds |= dspec_used ;
1275
		if ( is_templ_depend ( form ) ) ds |= dspec_mutable ;
1276
		apps = DEREF_inst ( tok_templ_apps ( sort ) ) ;
1277
		MAKE_inst_templ ( form, apps, tid, ds, all_instances, apps ) ;
1278
		COPY_inst ( type_token_app ( form ), apps ) ;
1279
		COPY_inst ( tok_templ_apps ( sort ), apps ) ;
1280
		all_instances = apps ;
1281
		created = 1 ;
1282
	    }
1283
 
1284
	    /* Check for matching specialisations */
1285
	    if ( def ) {
1286
		spec = specialise_form ( form, NULL_id ) ;
1287
		if ( !EQ_type ( spec, form ) ) {
1288
		    /* Specialisation found */
1289
		    d2 = set_templ_args ( spec ) ;
1290
		    rid = DEREF_id ( type_token_tok ( spec ) ) ;
1291
		    s = DEREF_type ( id_class_name_defn ( rid ) ) ;
1292
		    while ( IS_type_templ ( s ) ) {
1293
			s = DEREF_type ( type_templ_defn ( s ) ) ;
1294
		    }
1295
		    cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
1296
		    if ( !created ) {
1297
			if ( do_dump ) dump_instance ( tid, form, spec ) ;
1298
		    }
1299
		}
1300
	    }
1301
 
1302
	    /* Instantiate class members */
1303
	    t = DEREF_type ( id_class_name_defn ( tid ) ) ;
1304
	    s = DEREF_type ( ctype_form ( cs ) ) ;
1305
	    COPY_type ( ctype_form ( cs ), t ) ;
1306
	    while ( IS_type_templ ( t ) ) {
1307
		ci = cinfo_template ;
1308
		t = DEREF_type ( type_templ_defn ( t ) ) ;
1309
	    }
1310
	    ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
1311
	    COPY_type ( ctype_form ( ct ), form ) ;
1312
	    if ( do_dump && created ) {
1313
		/* Dump template instance information */
1314
		dump_declare ( tid, &crt_loc, 0 ) ;
1315
		dump_instance ( tid, form, spec ) ;
1316
	    }
1317
	    copy_members ( ct, cs, ci, def ) ;
1318
	    COPY_type ( ctype_form ( cs ), s ) ;
1319
	    if ( !EQ_type ( spec, form ) ) {
1320
		/* Reset specialisation parameters */
1321
		restore_templ_args ( spec, d2 ) ;
1322
	    }
1323
 
1324
	} else {
1325
	    /* Instantiation depth too great */
1326
	    if ( IS_NULL_id ( tid ) ) tid = rid ;
1327
	}
1328
	decr_value ( OPT_VAL_instance_depth ) ;
1329
	restore_prefix ( perr ) ;
1330
	DESTROY_ptr ( ploc, SIZE_loc ) ;
1331
	dump_template-- ;
1332
    }
1333
    if ( type ) {
1334
	/* Check for templates */
1335
	TYPE t = DEREF_type ( id_class_name_defn ( tid ) ) ;
1336
	if ( IS_type_templ ( t ) ) tid = NULL_id ;
1337
    } else {
1338
	restore_token_args ( pids, d ) ;
1339
    }
1340
    return ( tid ) ;
1341
}
1342
 
1343
 
1344
/*
1345
    COMPLETE A CLASS DEFINITION
1346
 
1347
    This routine is called with def true whenever a class type is
1348
    encountered which is complete but not defined in a context where a
1349
    complete type is required.  If ct is a template class instance then
1350
    the definition is provided.  If def is false then ct is marked as
1351
    complete if possible.
1352
*/
1353
 
1354
void complete_class
1355
    PROTO_N ( ( ct, def ) )
1356
    PROTO_T ( CLASS_TYPE ct X int def )
1357
{
1358
    CLASS_INFO ci = DEREF_cinfo ( ctype_info ( ct ) ) ;
1359
    if ( !( ci & cinfo_defined ) ) {
1360
	IDENTIFIER cid = DEREF_id ( ctype_name ( ct ) ) ;
1361
	IDENTIFIER sid = match_specialise ( cid, NULL_id ) ;
1362
	if ( !IS_NULL_id ( sid ) && IS_id_class_name ( sid ) ) {
1363
	    /* Template class instance */
1364
	    CLASS_TYPE cs ;
1365
	    CLASS_INFO cj ;
1366
	    TYPE s = DEREF_type ( id_class_name_defn ( sid ) ) ;
1367
	    while ( IS_type_templ ( s ) ) {
1368
		s = DEREF_type ( type_templ_defn ( s ) ) ;
1369
	    }
1370
	    cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
1371
	    if ( !EQ_ctype ( cs, ct ) ) {
1372
		/* Allow for nested template classes */
1373
		complete_class ( cs, def ) ;
1374
	    }
1375
	    cj = DEREF_cinfo ( ctype_info ( cs ) ) ;
1376
	    if ( cj & cinfo_complete ) {
1377
		/* Template class is complete */
1378
		ci |= cinfo_complete ;
1379
		COPY_cinfo ( ctype_info ( ct ), ci ) ;
1380
		if ( def ) {
1381
		    /* Define template class */
1382
		    TYPE form = DEREF_type ( ctype_form ( ct ) ) ;
1383
		    if ( !IS_NULL_type ( form ) ) {
1384
			if ( IS_type_token ( form ) ) {
1385
			    /* Template instance */
1386
			    IDENTIFIER tid ;
1387
			    LIST ( TOKEN ) args ;
1388
			    tid = DEREF_id ( type_token_tok ( form ) ) ;
1389
			    args = DEREF_list ( type_token_args ( form ) ) ;
1390
			    IGNORE instance_type ( tid, args, 0, 1 ) ;
1391
			} else {
1392
			    /* Nested template class */
1393
			    EXP e ;
1394
			    MAKE_exp_value ( s, e ) ;
1395
			    IGNORE define_templ_member ( cid, sid, form, e ) ;
1396
			}
1397
		    }
1398
		}
1399
	    }
1400
	}
1401
    }
1402
    return ;
1403
}
1404
 
1405
 
1406
/*
1407
    CHECK THE ARGUMENTS OF A TEMPLATE SPECIALISATION
1408
 
1409
    This routine checks the template specialisation declared with
1410
    parameters pids and arguments form.
1411
*/
1412
 
1413
static void check_spec_args
1414
    PROTO_N ( ( pids, form ) )
1415
    PROTO_T ( LIST ( IDENTIFIER ) pids X TYPE form )
1416
{
1417
    LIST ( TOKEN ) args = DEREF_list ( type_token_args ( form ) ) ;
1418
    while ( !IS_NULL_list ( args ) ) {
1419
	TOKEN a = DEREF_tok ( HEAD_list ( args ) ) ;
1420
	if ( IS_tok_exp ( a ) ) {
1421
	    /* Non-type argument */
1422
	    EXP e = DEREF_exp ( tok_exp_value ( a ) ) ;
1423
	    if ( depends_on_exp ( e, pids, 0 ) == 1 ) {
1424
		report ( crt_loc, ERR_temp_class_spec_depend ( form ) ) ;
1425
	    }
1426
	}
1427
	args = TAIL_list ( args ) ;
1428
    }
1429
    return ;
1430
}
1431
 
1432
 
1433
/*
1434
    CHECK A TEMPLATE SPECIALISATION
1435
 
1436
    This routine checks the specialisation spec of the template tid.
1437
    It identifies spec with any previous matching specialisation and
1438
    returns this previous version.  If no such match is found the
1439
    original instance is returned.
1440
*/
1441
 
1442
static INSTANCE check_specialise
1443
    PROTO_N ( ( tid, spec, type ) )
1444
    PROTO_T ( IDENTIFIER tid X INSTANCE spec X int type )
1445
{
1446
    INSTANCE apps ;
1447
    ERROR merr = NULL_err ;
1448
    INSTANCE eq = NULL_inst ;
1449
    TYPE form = DEREF_type ( inst_form ( spec ) ) ;
1450
 
1451
    /* Scan through previous instances */
1452
    force_template++ ;
1453
    apps = find_templ_apps ( tid ) ;
1454
    while ( !IS_NULL_inst ( apps ) ) {
1455
	if ( !EQ_inst ( spec, apps ) ) {
1456
	    DECL_SPEC acc = DEREF_dspec ( inst_templ_access ( apps ) ) ;
1457
	    if ( !( acc & dspec_alias ) ) {
1458
		/* Compare with previous instance */
1459
		TYPE prev = DEREF_type ( inst_form ( apps ) ) ;
1460
		int cmp = eq_type ( form, prev ) ;
1461
		if ( acc & dspec_main ) {
1462
		    /* Comparison with primary template */
1463
		    if ( cmp == 1 && ( type < 2 && crt_templ_qualifier ) ) {
1464
			ERROR err = ERR_temp_class_spec_primary ( form ) ;
1465
			report ( crt_loc, err ) ;
1466
			crt_templ_qualifier = 0 ;
1467
		    } else if ( cmp == 2 || cmp == 4 ) {
1468
			ERROR err = ERR_temp_class_spec_primary ( form ) ;
1469
			report ( crt_loc, err ) ;
1470
		    }
1471
		}
1472
		if ( cmp == 2 && ( acc & dspec_instance ) ) {
1473
		    if ( !( acc & dspec_explicit ) && type < 3 ) {
1474
			/* Specialisation matches previous use */
1475
			ERROR err = ERR_temp_spec_post ( form, prev ) ;
1476
			merr = concat_error ( merr, err ) ;
1477
		    }
1478
		}
1479
		if ( cmp == 1 ) {
1480
		    /* Equality of template specialisations */
1481
		    eq = apps ;
1482
		    break ;
1483
		}
1484
	    }
1485
	}
1486
	apps = DEREF_inst ( inst_next ( apps ) ) ;
1487
    }
1488
    force_template-- ;
1489
 
1490
    /* Identify equal specialisations */
1491
    if ( !IS_NULL_inst ( eq ) ) {
1492
	TYPE prev = DEREF_type ( inst_form ( eq ) ) ;
1493
	DECL_SPEC acc = DEREF_dspec ( inst_templ_access ( spec ) ) ;
1494
	acc |= dspec_alias ;
1495
	COPY_dspec ( inst_templ_access ( spec ), acc ) ;
1496
	COPY_inst ( inst_alias ( spec ), eq ) ;
1497
	if ( !IS_NULL_err ( merr ) ) destroy_error ( merr, 1 ) ;
1498
 
1499
	/* Identify template parameters */
1500
	if ( IS_type_templ ( form ) && IS_type_templ ( prev ) ) {
1501
	    TOKEN as = DEREF_tok ( type_templ_sort ( form ) ) ;
1502
	    TOKEN at = DEREF_tok ( type_templ_sort ( prev ) ) ;
1503
	    LIST ( IDENTIFIER ) ps = DEREF_list ( tok_templ_pids ( as ) ) ;
1504
	    LIST ( IDENTIFIER ) pt = DEREF_list ( tok_templ_pids ( at ) ) ;
1505
	    IGNORE eq_templ_params ( ps, pt ) ;
1506
 
1507
	    /* Re-check more recent instances */
1508
	    apps = all_instances ;
1509
	    while ( !EQ_inst ( apps, spec ) ) {
1510
		acc = DEREF_dspec ( inst_templ_access ( apps ) ) ;
1511
		if ( !( acc & dspec_alias ) ) {
1512
		    IDENTIFIER pid ;
1513
		    prev = DEREF_type ( inst_form ( apps ) ) ;
1514
		    while ( IS_type_templ ( prev ) ) {
1515
			prev = DEREF_type ( type_templ_defn ( prev ) ) ;
1516
		    }
1517
		    pid = DEREF_id ( type_token_tok ( prev ) ) ;
1518
		    IGNORE check_specialise ( pid, apps, 3 ) ;
1519
		}
1520
		apps = DEREF_inst ( inst_templ_prev ( apps ) ) ;
1521
	    }
1522
	}
1523
    } else {
1524
	/* Report matching instances */
1525
	if ( !IS_NULL_err ( merr ) ) report ( crt_loc, merr ) ;
1526
	eq = spec ;
1527
    }
1528
    return ( eq ) ;
1529
}
1530
 
1531
 
1532
/*
1533
    ADJUST THE LINKAGE OF A TEMPLATE FUNCTION
1534
 
1535
    A specialisation of a template function is inline only if it is
1536
    explicitly declared to be, independently of whether its function
1537
    template is.  However no storage class specifiers may be given for
1538
    a specialisation.  This routine adjusts the linkage of the template
1539
    function id of form form declared with declaration specifiers ds.
1540
*/
1541
 
1542
static void adjust_func_templ
1543
    PROTO_N ( ( id, ds, form ) )
1544
    PROTO_T ( IDENTIFIER id X DECL_SPEC ds X TYPE form )
1545
{
1546
    if ( !IS_NULL_type ( form ) ) {
1547
	DECL_SPEC pds ;
1548
	int redecl = 0 ;
1549
	if ( IS_type_token ( form ) ) {
1550
	    INSTANCE app = DEREF_inst ( type_token_app ( form ) ) ;
1551
	    if ( !IS_NULL_inst ( app ) ) {
1552
		/* Check for redeclarations */
1553
		DECL_SPEC acc = DEREF_dspec ( inst_templ_access ( app ) ) ;
1554
		if ( acc & dspec_static ) redecl = 1 ;
1555
		acc |= dspec_static ;
1556
		COPY_dspec ( inst_templ_access ( app ), acc ) ;
1557
	    }
1558
	} else if ( IS_type_instance ( form ) ) {
1559
	    /* Check for redeclarations */
1560
	    DECL_SPEC acc = DEREF_dspec ( type_instance_access ( form ) ) ;
1561
	    if ( acc & dspec_static ) redecl = 1 ;
1562
	    acc |= dspec_static ;
1563
	    COPY_dspec ( type_instance_access ( form ), acc ) ;
1564
	}
1565
	if ( !redecl && !IS_NULL_id ( id ) && IS_id_function_etc ( id ) ) {
1566
	    /* Adjust inline specifier */
1567
	    pds = DEREF_dspec ( id_storage ( id ) ) ;
1568
	    pds &= ~dspec_inline ;
1569
	    if ( ds & dspec_inline ) {
1570
		/* Mark inline functions */
1571
		pds |= dspec_inline ;
1572
	    }
1573
	    COPY_dspec ( id_storage ( id ), pds ) ;
1574
	}
1575
	pds = ( ds & dspec_storage ) ;
1576
	if ( pds != dspec_none ) {
1577
	    /* Check for storage class specifiers */
1578
	    report ( decl_loc, ERR_dcl_stc_expl_spec ( pds ) ) ;
1579
	}
1580
	if ( crt_linkage == dspec_c ) {
1581
	    /* Check for C linkage */
1582
	    report ( decl_loc, ERR_temp_decl_linkage () ) ;
1583
	}
1584
    }
1585
    return ;
1586
}
1587
 
1588
 
1589
/*
1590
    EXAMINE A TEMPLATE SPECIALISATION TYPE
1591
 
1592
    This routine examines the template specialisation t of the given form.
1593
    expl is true for an explicit specialisation and false for a simple
1594
    redeclaration.  The routine returns 2 if t represents an explicit
1595
    instantiation, 1 if it represents an explicit specialisation, and 0
1596
    otherwise.
1597
*/
1598
 
1599
static int bind_templ_spec
1600
    PROTO_N ( ( pid, t, form, type, expl ) )
1601
    PROTO_T ( IDENTIFIER *pid X TYPE t X TYPE form X int type X int expl )
1602
{
1603
    int def = 0 ;
1604
    if ( expl ) {
1605
	DECL_SPEC acc ;
1606
	TOKEN sort = NULL_tok ;
1607
	NAMESPACE ns = crt_namespace ;
1608
	LIST ( IDENTIFIER ) pids = NULL_list ( IDENTIFIER ) ;
1609
	INSTANCE spec = DEREF_inst ( type_token_app ( form ) ) ;
1610
	IDENTIFIER tid = DEREF_id ( type_token_tok ( form ) ) ;
1611
	if ( IS_type_templ ( t ) ) {
1612
	    /* Find template information */
1613
	    sort = DEREF_tok ( type_templ_sort ( t ) ) ;
1614
	    pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
1615
	    ns = DEREF_nspace ( tok_templ_pars ( sort ) ) ;
1616
	}
1617
	if ( IS_NULL_list ( pids ) ) {
1618
	    if ( IS_NULL_nspace ( ns ) ) {
1619
		/* Explicit instantiation */
1620
		acc = DEREF_dspec ( inst_templ_access ( spec ) ) ;
1621
		if ( acc & dspec_register ) {
1622
		    report ( decl_loc, ERR_temp_spec_reinst ( form ) ) ;
1623
		} else if ( acc & dspec_explicit ) {
1624
		    report ( decl_loc, ERR_temp_spec_redecl ( form ) ) ;
1625
		}
1626
		acc |= dspec_register ;
1627
		COPY_dspec ( inst_templ_access ( spec ), acc ) ;
1628
		def = ( type == 2 ? 3 : 2 ) ;
1629
	    } else {
1630
		/* Explicit specialisation */
1631
		spec = check_specialise ( tid, spec, type ) ;
1632
		if ( type != 2 ) {
1633
		    acc = DEREF_dspec ( inst_templ_access ( spec ) ) ;
1634
		    if ( acc & dspec_register ) {
1635
			report ( decl_loc, ERR_temp_spec_redecl ( form ) ) ;
1636
		    } else if ( acc & dspec_explicit ) {
1637
			report ( decl_loc, ERR_temp_spec_respec ( form ) ) ;
1638
		    } else if ( acc & ( dspec_used | dspec_called ) ) {
1639
			report ( decl_loc, ERR_temp_spec_used ( form ) ) ;
1640
		    }
1641
		    acc |= dspec_explicit ;
1642
		    COPY_dspec ( inst_templ_access ( spec ), acc ) ;
1643
		}
1644
		*pid = DEREF_id ( inst_templ_id ( spec ) ) ;
1645
		def = 1 ;
1646
	    }
1647
 
1648
	} else {
1649
	    /* Partial specialisation */
1650
	    check_spec_args ( pids, form ) ;
1651
	    if ( check_templ_dargs ( t ) ) {
1652
		/* Check template default arguments */
1653
		report ( decl_loc, ERR_temp_class_spec_darg () ) ;
1654
	    }
1655
	    MAKE_type_templ ( cv_none, sort, form, 1, form ) ;
1656
	    COPY_type ( inst_form ( spec ), form ) ;
1657
	    spec = check_specialise ( tid, spec, type ) ;
1658
	    acc = DEREF_dspec ( inst_templ_access ( spec ) ) ;
1659
	    acc &= ~dspec_instance ;
1660
	    acc |= dspec_template ;
1661
	    if ( type == 0 && !( acc & dspec_main ) ) {
1662
		/* Can't partially specialise functions */
1663
		report ( decl_loc, ERR_temp_decl_func () ) ;
1664
	    }
1665
	    if ( type != 2 ) {
1666
		if ( acc & dspec_called ) {
1667
		    /* Have specialised members */
1668
		    report ( decl_loc, ERR_temp_spec_used ( form ) ) ;
1669
		}
1670
		acc |= dspec_extern ;
1671
	    }
1672
	    COPY_dspec ( inst_templ_access ( spec ), acc ) ;
1673
	    *pid = DEREF_id ( inst_templ_id ( spec ) ) ;
1674
	}
1675
	if ( type == 0 && check_func_dargs ( t, 0, 0 ) ) {
1676
	    /* Check function default arguments */
1677
	    report ( decl_loc, ERR_temp_class_spec_darg () ) ;
1678
	}
1679
    }
1680
    return ( def ) ;
1681
}
1682
 
1683
 
1684
/*
1685
    TEMPLATE SPECIALISATION FLAG
1686
 
1687
    This flag is set by bind_specialise to indicate the type of template
1688
    declaration encountered.  The values are as in bind_templ_spec.
1689
*/
1690
 
1691
int bound_specialise = 0 ;
1692
 
1693
 
1694
/*
1695
    BIND TEMPLATE PARAMETERS IN A TEMPLATE SPECIALISATION
1696
 
1697
    This routine binds any template parameters in the template
1698
    specialisation given by the declarator id of type t and declaration
1699
    specifiers ds.  It returns those components of t which bind to the
1700
    underlying object.  For example in 'template < class T > int A < T >::a'
1701
    the component of the type which binds to 'a' is 'int'.  The
1702
    'template < class T >' component binds to the 'A < T >::' qualifier.
1703
    Note that this analysis may be done prior to any replacement of
1704
    inferred types in t.
1705
*/
1706
 
1707
TYPE bind_specialise
1708
    PROTO_N ( ( pid, t, ds, type, force, init ) )
1709
    PROTO_T ( IDENTIFIER *pid X TYPE t X DECL_SPEC ds X
1710
	      int type X int force X int init )
1711
{
1712
    IDENTIFIER id = *pid ;
1713
    if ( !IS_NULL_id ( id ) ) {
1714
	DECL_SPEC pds = DEREF_dspec ( id_storage ( id ) ) ;
1715
	if ( pds & dspec_instance ) {
1716
	    /* Template instance */
1717
	    TYPE f ;
1718
	    int def = 0 ;
1719
	    int undef = 0 ;
1720
	    INSTANCE spec = NULL_inst ;
1721
 
1722
	    /* Examine enclosing class */
1723
	    NAMESPACE ns = DEREF_nspace ( id_parent ( id ) ) ;
1724
	    if ( !IS_NULL_nspace ( ns ) && IS_nspace_ctype ( ns ) ) {
1725
		IDENTIFIER cid = DEREF_id ( nspace_name ( ns ) ) ;
1726
		IDENTIFIER sid = cid ;
1727
		t = bind_specialise ( &sid, t, ds, 2, 0, init ) ;
1728
		if ( !EQ_id ( sid, cid ) ) {
1729
		    /* Changed specialisation */
1730
		    IDENTIFIER qid = id ;
1731
		    id = find_copied_member ( sid, qid, 0, type ) ;
1732
		    if ( type != 2 ) {
1733
			/* Update namespace stacks */
1734
			QUALIFIER q = qual_nested ;
1735
			end_declarator ( qid, 1 ) ;
1736
			begin_declarator ( id, q, NULL_nspace, 1 ) ;
1737
		    }
1738
		    *pid = id ;
1739
		}
1740
		f = find_form ( sid, &undef ) ;
1741
		if ( !IS_NULL_type ( f ) && IS_type_token ( f ) ) {
1742
		    spec = DEREF_inst ( type_token_app ( f ) ) ;
1743
		}
1744
	    }
1745
 
1746
	    /* Select overloaded function */
1747
	    if ( type == 0 && IS_id_function_etc ( id ) ) {
1748
		TYPE fn = t ;
1749
		if ( !IS_NULL_type ( fn ) ) {
1750
		    TYPE ret ;
1751
		    int eq = 0 ;
1752
		    IDENTIFIER fid ;
1753
		    LIST ( IDENTIFIER ) pids = NULL_list ( IDENTIFIER ) ;
1754
		    if ( IS_type_templ ( fn ) ) {
1755
			fn = DEREF_type ( type_templ_defn ( fn ) ) ;
1756
		    }
1757
		    ret = inferred_return ( fn, id ) ;
1758
		    fid = resolve_func ( id, fn, 1, 0, pids, &eq ) ;
1759
		    if ( IS_NULL_id ( fid ) ) {
1760
			/* No match found */
1761
			report ( decl_loc, ERR_temp_spec_type ( fn, id ) ) ;
1762
		    } else if ( IS_id_ambig ( fid ) ) {
1763
			/* More than one match found */
1764
			IGNORE report_ambiguous ( fid, 0, 0, 0 ) ;
1765
			fid = NULL_id ;
1766
		    }
1767
		    if ( !IS_NULL_type ( ret ) ) {
1768
			/* Restore return type */
1769
			COPY_type ( type_func_ret ( fn ), ret ) ;
1770
		    }
1771
		    *pid = fid ;
1772
		    id = fid ;
1773
		}
1774
	    }
1775
 
1776
	    /* Examine template instance */
1777
	    f = find_form ( id, &undef ) ;
1778
	    if ( !IS_NULL_type ( f ) && IS_type_token ( f ) ) {
1779
		IDENTIFIER tid = DEREF_id ( type_token_tok ( f ) ) ;
1780
		unsigned tag = TAG_id ( tid ) ;
1781
		if ( tag != id_token_tag ) {
1782
		    /* Examine template qualifiers */
1783
		    if ( !IS_NULL_type ( t ) && IS_type_templ ( t ) ) {
1784
			def = bind_templ_spec ( &id, t, f, type, 1 ) ;
1785
			if ( def == 1 || def == 2 ) {
1786
			    /* Explicit specialisations */
1787
			    t = DEREF_type ( type_templ_defn ( t ) ) ;
1788
			} else if ( def == 0 ) {
1789
			    /* Partial specialisations */
1790
			    if ( type == 2 || crt_templ_qualifier ) {
1791
				t = DEREF_type ( type_templ_defn ( t ) ) ;
1792
			    }
1793
			}
1794
		    } else {
1795
			int expl = 0 ;
1796
			TYPE s = type_error ;
1797
			if ( !( ds & dspec_friend ) ) {
1798
			    /* A friend declaration is allowed */
1799
			    report ( decl_loc, ERR_temp_spec_prefix () ) ;
1800
			    expl = 1 ;
1801
			}
1802
			def = bind_templ_spec ( &id, s, f, type, expl ) ;
1803
		    }
1804
		    if ( tag == id_class_name_tag ) {
1805
			if ( type == 0 ) {
1806
			    report ( decl_loc, ERR_temp_spec_bad ( f ) ) ;
1807
			    id = NULL_id ;
1808
			}
1809
		    } else {
1810
			if ( type ) {
1811
			    report ( decl_loc, ERR_temp_spec_bad ( f ) ) ;
1812
			    id = NULL_id ;
1813
			} else {
1814
			}
1815
		    }
1816
		    bound_specialise = def ;
1817
		    *pid = id ;
1818
		}
1819
	    } else {
1820
		/* Check for explicit instantiations */
1821
		if ( !IS_NULL_type ( t ) && IS_type_templ ( t ) ) {
1822
		    TOKEN sort = DEREF_tok ( type_templ_sort ( t ) ) ;
1823
		    NAMESPACE pns = DEREF_nspace ( tok_templ_pars ( sort ) ) ;
1824
		    if ( IS_NULL_nspace ( pns ) ) {
1825
			if ( !IS_NULL_type ( f ) && IS_type_instance ( f ) ) {
1826
			    def = ( type == 2 ? 3 : 2 ) ;
1827
			} else {
1828
			    report ( decl_loc, ERR_temp_explicit_templ () ) ;
1829
			}
1830
			t = DEREF_type ( type_templ_defn ( t ) ) ;
1831
			bound_specialise = def ;
1832
		    }
1833
		}
1834
	    }
1835
 
1836
	    /* Adjust function linkage */
1837
	    if ( !type ) adjust_func_templ ( id, ds, f ) ;
1838
 
1839
	    /* Record member specialisations */
1840
	    if ( !IS_NULL_inst ( spec ) ) {
1841
		DECL_SPEC acc ;
1842
		LIST ( IDENTIFIER ) mems ;
1843
		if ( def == 0 || def == 1 ) {
1844
		    if ( !IS_NULL_type ( f ) && IS_type_instance ( f ) ) {
1845
			IDENTIFIER mid = DEREF_id ( type_instance_id ( f ) ) ;
1846
			mems = DEREF_list ( inst_templ_mems ( spec ) ) ;
1847
			CONS_id ( mid, mems, mems ) ;
1848
			COPY_list ( inst_templ_mems ( spec ), mems ) ;
1849
		    }
1850
		}
1851
		acc = DEREF_dspec ( inst_templ_access ( spec ) ) ;
1852
		acc |= dspec_called ;
1853
		COPY_dspec ( inst_templ_access ( spec ), acc ) ;
1854
	    }
1855
 
1856
	    /* Mark explicit specialisations */
1857
	    if ( def == 1 && !IS_NULL_id ( id ) ) {
1858
		if ( type != 2 ) {
1859
		    /* Explicit specialisations are exported */
1860
		    export_template ( id, 1 ) ;
1861
		}
1862
	    }
1863
 
1864
	    /* Mark explicit instantiations */
1865
	    if ( def == 2 && !IS_NULL_id ( id ) ) {
1866
		if ( force == 2 ) {
1867
		    /* template-id required in instantiation */
1868
		    ERROR err = ERR_temp_explicit_id ( id ) ;
1869
		    report ( decl_loc, err ) ;
1870
		}
1871
		if ( init ) {
1872
		    /* Can't have definition as well */
1873
		    report ( decl_loc, ERR_temp_explicit_def () ) ;
1874
		}
1875
		define_template ( id, 1 ) ;
1876
	    }
1877
 
1878
	} else if ( force ) {
1879
	    /* Not a template instance */
1880
	    if ( pds & dspec_template ) {
1881
		if ( IS_id_function_etc ( id ) ) {
1882
		    /* Template function */
1883
		    allow_templ_dargs = 0 ;
1884
		    *pid = parse_id_template ( id, NIL ( PPTOKEN ), 0 ) ;
1885
		    allow_templ_dargs = 1 ;
1886
		    crt_templ_qualifier = 1 ;
1887
		    t = bind_specialise ( pid, t, ds, type, 2, init ) ;
1888
		} else {
1889
		    /* Template class */
1890
		    report ( decl_loc, ERR_temp_param_none ( id ) ) ;
1891
		    *pid = NULL_id ;
1892
		}
1893
	    } else {
1894
		NAMESPACE ns = DEREF_nspace ( id_parent ( id ) ) ;
1895
		if ( !IS_NULL_nspace ( ns ) && IS_nspace_ctype ( ns ) ) {
1896
		    IDENTIFIER cid = DEREF_id ( nspace_name ( ns ) ) ;
1897
		    IDENTIFIER sid = cid ;
1898
		    t = bind_specialise ( &sid, t, ds, 2, 0, init ) ;
1899
		    if ( !EQ_id ( sid, cid ) ) {
1900
			/* Changed specialisation */
1901
			IDENTIFIER qid = id ;
1902
			id = find_copied_member ( sid, qid, 0, type ) ;
1903
			if ( type != 2 ) {
1904
			    /* Update namespace stacks */
1905
			    QUALIFIER q = qual_nested ;
1906
			    end_declarator ( qid, 1 ) ;
1907
			    begin_declarator ( id, q, NULL_nspace, 1 ) ;
1908
			}
1909
			*pid = id ;
1910
		    }
1911
		}
1912
		if ( IS_type_templ ( t ) ) {
1913
		    /* Invalid template declaration */
1914
		    id = underlying_id ( id ) ;
1915
		    report ( decl_loc, ERR_temp_param_none ( id ) ) ;
1916
		    *pid = NULL_id ;
1917
		}
1918
	    }
1919
	}
1920
    }
1921
    return ( t ) ;
1922
}
1923
 
1924
 
1925
/*
1926
    SYNTHESISE A LIST OF ARGUMENTS FOR A FUNCTION
1927
 
1928
    This routine synthesises a list of dummy arguments for the function
1929
    template id.
1930
*/
1931
 
1932
static LIST ( EXP ) synthesise_args
1933
    PROTO_N ( ( id ) )
1934
    PROTO_T ( IDENTIFIER id )
1935
{
1936
    LIST ( TYPE ) pars ;
1937
    LIST ( EXP ) args = NULL_list ( EXP ) ;
1938
    TYPE fn = DEREF_type ( id_function_etc_type ( id ) ) ;
1939
    while ( IS_type_templ ( fn ) ) {
1940
	fn = DEREF_type ( type_templ_defn ( fn ) ) ;
1941
    }
1942
    pars = DEREF_list ( type_func_mtypes ( fn ) ) ;
1943
    while ( !IS_NULL_list ( pars ) ) {
1944
	EXP a ;
1945
	TYPE t = DEREF_type ( HEAD_list ( pars ) ) ;
1946
	if ( IS_type_ref ( t ) ) {
1947
	    /* Do reference conversions */
1948
	    t = DEREF_type ( type_ref_sub ( t ) ) ;
1949
	}
1950
	MAKE_exp_value ( t, a ) ;
1951
	CONS_exp ( a, args, args ) ;
1952
	pars = TAIL_list ( pars ) ;
1953
    }
1954
    return ( REVERSE_list ( args ) ) ;
1955
}
1956
 
1957
 
1958
/*
1959
    FIND THE MORE SPECIALISED OF TWO FUNCTION TEMPLATES
1960
 
1961
    This routine compares the function templates tid and sid.  It returns
1962
    1 if tid is more specialised than sid, 2 if sid is more specialised,
1963
    and 0 otherwise.
1964
*/
1965
 
1966
int compare_specs
1967
    PROTO_N ( ( tid, sid ) )
1968
    PROTO_T ( IDENTIFIER tid X IDENTIFIER sid )
1969
{
1970
    if ( !EQ_id ( tid, sid ) ) {
1971
	IDENTIFIER ds, dt ;
1972
	LIST ( EXP ) args ;
1973
	ERROR err = NULL_err ;
1974
	args = synthesise_args ( sid ) ;
1975
	ds = deduce_args ( tid, args, 0, 0, 0, &err ) ;
1976
	free_exp_list ( args, 1 ) ;
1977
	args = synthesise_args ( tid ) ;
1978
	dt = deduce_args ( sid, args, 0, 0, 0, &err ) ;
1979
	free_exp_list ( args, 1 ) ;
1980
	if ( !IS_NULL_err ( err ) ) destroy_error ( err, 1 ) ;
1981
	if ( !IS_NULL_id ( ds ) ) {
1982
	    if ( IS_NULL_id ( dt ) ) return ( 2 ) ;
1983
	} else {
1984
	    if ( !IS_NULL_id ( dt ) ) return ( 1 ) ;
1985
	}
1986
    }
1987
    return ( 0 ) ;
1988
}
1989
 
1990
 
1991
/*
1992
    CHECK A TEMPLATE FUNCTION DECLARATION
1993
 
1994
    This routine checks the declaration of the function id which is
1995
    either a template function itself or overloads a template function.
1996
*/
1997
 
1998
void templ_func_decl
1999
    PROTO_N ( ( id ) )
2000
    PROTO_T ( IDENTIFIER id )
2001
{
2002
    while ( !IS_NULL_id ( id ) ) {
2003
	DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
2004
	ds |= dspec_template ;
2005
	COPY_dspec ( id_storage ( id ), ds ) ;
2006
	id = DEREF_id ( id_function_etc_over ( id ) ) ;
2007
    }
2008
    return ;
2009
}
2010
 
2011
 
2012
/*
2013
    LIST OF TEMPLATES FOR DEFINITION
2014
 
2015
    The variable pending_templates holds a list of all the template
2016
    instances which should be defined at the next available opportunity.
2017
    The variable still_pending_templates holds a list of all those
2018
    instances which should be defined if the corresponding template
2019
    is defined at some later stage.
2020
*/
2021
 
2022
LIST ( IDENTIFIER ) pending_templates = NULL_list ( IDENTIFIER ) ;
2023
LIST ( IDENTIFIER ) still_pending_templates = NULL_list ( IDENTIFIER ) ;
2024
 
2025
 
2026
/*
2027
    MARK ALL MEMBERS OF A TEMPLATE CLASS FOR DEFINITION
2028
 
2029
    This routine adds all members of the template class ct to the list of
2030
    templates to be defined.
2031
*/
2032
 
2033
static void define_members
2034
    PROTO_N ( ( ct ) )
2035
    PROTO_T ( CLASS_TYPE ct )
2036
{
2037
    TYPE form = DEREF_type ( ctype_form ( ct ) ) ;
2038
    if ( !IS_NULL_type ( form ) ) {
2039
	NAMESPACE ns = DEREF_nspace ( ctype_member ( ct ) ) ;
2040
	MEMBER mem = DEREF_member ( nspace_ctype_first ( ns ) ) ;
2041
	GRAPH gr = DEREF_graph ( ctype_base ( ct ) ) ;
2042
	LIST ( GRAPH ) br = DEREF_list ( graph_tails ( gr ) ) ;
2043
	while ( !IS_NULL_list ( br ) ) {
2044
	    /* Scan through base classes */
2045
	    GRAPH gs = DEREF_graph ( HEAD_list ( br ) ) ;
2046
	    CLASS_TYPE cs = DEREF_ctype ( graph_head ( gs ) ) ;
2047
	    define_members ( cs ) ;
2048
	    br = TAIL_list ( br ) ;
2049
	}
2050
	while ( !IS_NULL_member ( mem ) ) {
2051
	    /* Scan through class members */
2052
	    IDENTIFIER id = DEREF_id ( member_id ( mem ) ) ;
2053
	    IDENTIFIER alt = DEREF_id ( member_alt ( mem ) ) ;
2054
	    if ( !IS_NULL_id ( id ) ) {
2055
		define_template ( id, 1 ) ;
2056
	    }
2057
	    if ( !IS_NULL_id ( alt ) && !EQ_id ( id, alt ) ) {
2058
		define_template ( alt, 2 ) ;
2059
	    }
2060
	    mem = DEREF_member ( member_next ( mem ) ) ;
2061
	}
2062
    }
2063
    return ;
2064
}
2065
 
2066
 
2067
/*
2068
    MARK A TEMPLATE FOR DEFINITION
2069
 
2070
    This routine adds the template instance id to the list of templates
2071
    to be defined.  The identifier is marked as defined even though the
2072
    actual definition occurs later.  Explicit instantiations are indicated
2073
    by expl being true.
2074
*/
2075
 
2076
void define_template
2077
    PROTO_N ( ( id, expl ) )
2078
    PROTO_T ( IDENTIFIER id X int expl )
2079
{
2080
    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
2081
    if ( ds & dspec_inherit ) {
2082
	/* Inherited functions */
2083
	id = DEREF_id ( id_alias ( id ) ) ;
2084
	ds = DEREF_dspec ( id_storage ( id ) ) ;
2085
    }
2086
    if ( ( ds & dspec_instance ) && !( ds & dspec_implicit ) ) {
2087
	if ( dependent_id ( id ) ) {
2088
	    /* Ignore template dependent instantiations */
2089
	    return ;
2090
	}
2091
	switch ( TAG_id ( id ) ) {
2092
	    case id_class_name_tag :
2093
	    case id_class_alias_tag : {
2094
		if ( expl ) {
2095
		    TYPE t = DEREF_type ( id_class_name_etc_defn ( id ) ) ;
2096
		    if ( IS_type_compound ( t ) ) {
2097
			/* Define all template members */
2098
			ERROR err = check_incomplete ( t ) ;
2099
			if ( IS_NULL_err ( err ) ) {
2100
			    CLASS_TYPE ct ;
2101
			    NAMESPACE ns = DEREF_nspace ( id_parent ( id ) ) ;
2102
			    check_decl_nspace ( id, ns, 1, crt_namespace ) ;
2103
			    ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
2104
			    define_members ( ct ) ;
2105
			} else {
2106
			    ERROR err2 = ERR_temp_explicit_incompl () ;
2107
			    err = concat_error ( err, err2 ) ;
2108
			    report ( crt_loc, err ) ;
2109
			}
2110
		    }
2111
		}
2112
		break ;
2113
	    }
2114
	    case id_function_tag :
2115
	    case id_mem_func_tag :
2116
	    case id_stat_mem_func_tag : {
2117
		/* Template functions */
2118
		if ( !( ds & dspec_defn ) ) {
2119
		    CONS_id ( id, pending_templates, pending_templates ) ;
2120
		    COPY_dspec ( id_storage ( id ), ( ds | dspec_defn ) ) ;
2121
		    COPY_loc ( id_loc ( id ), crt_loc ) ;
2122
		}
2123
		if ( expl == 2 ) {
2124
		    /* Allow for overloaded functions */
2125
		    id = DEREF_id ( id_function_etc_over ( id ) ) ;
2126
		    if ( !IS_NULL_id ( id ) ) define_template ( id, 2 ) ;
2127
		}
2128
		break ;
2129
	    }
2130
	    case id_stat_member_tag : {
2131
		/* Static data members of class templates */
2132
		if ( !( ds & dspec_defn ) ) {
2133
		    TYPE t = DEREF_type ( id_stat_member_type ( id ) ) ;
2134
		    CV_SPEC cv = DEREF_cv ( type_qual ( t ) ) ;
2135
		    COPY_dspec ( id_storage ( id ), ( ds | dspec_defn ) ) ;
2136
		    COPY_loc ( id_loc ( id ), crt_loc ) ;
2137
		    CONS_id ( id, pending_templates, pending_templates ) ;
2138
		    if ( cv == ( cv_lvalue | cv_const ) ) {
2139
			copy_template ( id, 2 ) ;
2140
		    }
2141
		}
2142
		break ;
2143
	    }
2144
	}
2145
    }
2146
    return ;
2147
}
2148
 
2149
 
2150
/*
2151
    BIND TEMPLATE ARGUMENTS AND DEFINE A TEMPLATE
2152
 
2153
    This routine binds the template arguments for the identifier tid to
2154
    the specialisation sid and then defines id to be e.  It returns false
2155
    if any template argument depends on an unbound template parameter.
2156
*/
2157
 
2158
static int bind_template
2159
    PROTO_N ( ( tid, id, sid, e, bound ) )
2160
    PROTO_T ( IDENTIFIER tid X IDENTIFIER id X IDENTIFIER sid X
2161
	      EXP e X int bound )
2162
{
2163
    if ( IS_NULL_id ( tid ) ) {
2164
	/* Binding complete - copy definition */
2165
	if ( !bound ) {
2166
	    /* Suppress output of object definition */
2167
	    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
2168
	    COPY_dspec ( id_storage ( id ), ( ds | dspec_done ) ) ;
2169
	}
2170
	copy_object ( id, e ) ;
2171
 
2172
    } else {
2173
	/* Examine parent namespace */
2174
	TYPE p, q ;
2175
	int undef = 0 ;
2176
	IDENTIFIER pid = NULL_id ;
2177
	IDENTIFIER qid = NULL_id ;
2178
	NAMESPACE pns = DEREF_nspace ( id_parent ( tid ) ) ;
2179
	NAMESPACE qns = DEREF_nspace ( id_parent ( sid ) ) ;
2180
	if ( !IS_NULL_nspace ( pns ) ) {
2181
	    switch ( TAG_nspace ( pns ) ) {
2182
		case nspace_block_tag :
2183
		case nspace_param_tag :
2184
		case nspace_dummy_tag :
2185
		case nspace_ctype_tag : {
2186
		    /* Find enclosing class or function */
2187
		    pid = DEREF_id ( nspace_name ( pns ) ) ;
2188
		    qid = DEREF_id ( nspace_name ( qns ) ) ;
2189
		    break ;
2190
		}
2191
	    }
2192
	}
2193
 
2194
	/* Bind template arguments */
2195
	p = find_form ( tid, &undef ) ;
2196
	q = find_form ( sid, &undef ) ;
2197
	if ( !IS_NULL_type ( p ) && IS_type_token ( p ) ) {
2198
	    /* Template application found */
2199
	    TYPE s = NULL_type ;
2200
	    IDENTIFIER rid = NULL_id ;
2201
	    DECL_SPEC acc = dspec_none ;
2202
	    if ( !IS_NULL_type ( q ) && IS_type_token ( q ) ) {
2203
		/* Check for specialised template */
2204
		INSTANCE spec = DEREF_inst ( type_token_app ( q ) ) ;
2205
		s = DEREF_type ( inst_form ( spec ) ) ;
2206
		rid = DEREF_id ( inst_templ_id ( spec ) ) ;
2207
		acc = DEREF_dspec ( inst_templ_access ( spec ) ) ;
2208
	    }
2209
	    if ( !( acc & dspec_template ) ) {
2210
		/* Not specialised - use primary template */
2211
		rid = DEREF_id ( type_token_tok ( p ) ) ;
2212
		if ( IS_id_class_name ( rid ) ) {
2213
		    s = DEREF_type ( id_class_name_defn ( rid ) ) ;
2214
		} else {
2215
		    s = DEREF_type ( id_function_etc_type ( rid ) ) ;
2216
		}
2217
		acc = dspec_main ;
2218
	    }
2219
	    if ( IS_type_templ ( s ) ) {
2220
		/* Bind template parameters */
2221
		int d ;
2222
		LIST ( TOKEN ) args ;
2223
		LIST ( IDENTIFIER ) pids ;
2224
		TOKEN sort = DEREF_tok ( type_templ_sort ( s ) ) ;
2225
		pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
2226
		if ( acc & dspec_main ) {
2227
		    /* Bound to primary template */
2228
		    args = DEREF_list ( type_token_args ( p ) ) ;
2229
		} else {
2230
		    /* Bound to template specialisation */
2231
		    args = NULL_list ( TOKEN ) ;
2232
		}
2233
		d = save_token_args ( pids, args ) ;
2234
		if ( IS_NULL_list ( args ) ) {
2235
		    force_template++ ;
2236
		    s = DEREF_type ( type_templ_defn ( s ) ) ;
2237
		    if ( !eq_type ( s, p ) ) {
2238
			FAIL ( bind_template failed ) ;
2239
			bound = 0 ;
2240
		    }
2241
		    force_template-- ;
2242
		}
2243
 
2244
		/* Bind parent templates */
2245
		if ( is_templ_depend ( p ) ) bound = 0 ;
2246
		if ( IS_id_class_name ( rid ) ) {
2247
		    TYPE t ;
2248
		    CLASS_TYPE cs ;
2249
		    s = DEREF_type ( id_class_name_defn ( rid ) ) ;
2250
		    while ( IS_type_templ ( s ) ) {
2251
			s = DEREF_type ( type_templ_defn ( s ) ) ;
2252
		    }
2253
		    cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
2254
		    s = DEREF_type ( ctype_form ( cs ) ) ;
2255
		    t = DEREF_type ( id_class_name_defn ( tid ) ) ;
2256
		    COPY_type ( ctype_form ( cs ), t ) ;
2257
		    bound = bind_template ( pid, id, qid, e, bound ) ;
2258
		    COPY_type ( ctype_form ( cs ), s ) ;
2259
		} else {
2260
		    bound = bind_template ( pid, id, qid, e, bound ) ;
2261
		}
2262
		restore_token_args ( pids, d ) ;
2263
 
2264
	    } else {
2265
		/* Explicit specialisation */
2266
		bound = 0 ;
2267
	    }
2268
 
2269
	} else {
2270
	    /* Bind parent templates */
2271
	    bound = bind_template ( pid, id, qid, e, bound ) ;
2272
	}
2273
    }
2274
    return ( bound ) ;
2275
}
2276
 
2277
 
2278
/*
2279
    DEFINE A TEMPLATE MEMBER
2280
 
2281
    This routine defines the template member id given by form to be the
2282
    specialisation tid of value e.  It returns the declaration specifiers
2283
    of the result.
2284
*/
2285
 
2286
DECL_SPEC define_templ_member
2287
    PROTO_N ( ( id, tid, form, e ) )
2288
    PROTO_T ( IDENTIFIER id X IDENTIFIER tid X TYPE form X EXP e )
2289
{
2290
    ERROR perr ;
2291
    DECL_SPEC ds ;
2292
    PTR ( LOCATION ) ploc ;
2293
    dump_template++ ;
2294
    report_instance ( form ) ;
2295
    ploc = MAKE_ptr ( SIZE_loc ) ;
2296
    COPY_loc ( ploc, crt_loc ) ;
2297
    perr = set_prefix ( ERR_temp_inst_comment ( form, ploc ) ) ;
2298
    if ( incr_value ( OPT_VAL_instance_depth ) ) {
2299
	/* Bind template arguments and copy function */
2300
	LOCATION loc ;
2301
	loc = crt_loc ;
2302
	bad_crt_loc++ ;
2303
	DEREF_loc ( id_loc ( tid ), crt_loc ) ;
2304
	IGNORE bind_template ( id, id, tid, e, 1 ) ;
2305
	ds = DEREF_dspec ( id_storage ( id ) ) ;
2306
	clear_templates ( 0 ) ;
2307
	crt_loc = loc ;
2308
	bad_crt_loc-- ;
2309
    } else {
2310
	/* Instantiation depth too great */
2311
	ds = DEREF_dspec ( id_storage ( id ) ) ;
2312
    }
2313
    decr_value ( OPT_VAL_instance_depth ) ;
2314
    restore_prefix ( perr ) ;
2315
    DESTROY_ptr ( ploc, SIZE_loc ) ;
2316
    dump_template-- ;
2317
    return ( ds ) ;
2318
}
2319
 
2320
 
2321
/*
2322
    DEFINE A TEMPLATE
2323
 
2324
    This routine defines the template application id.  force is true to
2325
    indicate the end of the translation unit when a non-exported template
2326
    should have been defined.
2327
*/
2328
 
2329
static void copy_template
2330
    PROTO_N ( ( id, force ) )
2331
    PROTO_T ( IDENTIFIER id X int force )
2332
{
2333
    /* Find the template information */
2334
    EXP e = NULL_exp ;
2335
    TYPE form = NULL_type ;
2336
    IDENTIFIER tid = NULL_id ;
2337
    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
2338
    switch ( TAG_id ( id ) ) {
2339
	case id_function_tag :
2340
	case id_mem_func_tag :
2341
	case id_stat_mem_func_tag : {
2342
	    /* Template functions */
2343
	    e = DEREF_exp ( id_function_etc_defn ( id ) ) ;
2344
	    if ( !IS_NULL_exp ( e ) ) {
2345
		/* Template already defined */
2346
		return ;
2347
	    }
2348
	    form = DEREF_type ( id_function_etc_form ( id ) ) ;
2349
	    tid = match_specialise ( id, NULL_id ) ;
2350
	    if ( !IS_NULL_id ( tid ) ) {
2351
		/* Find function definition */
2352
		DECL_SPEC tds = DEREF_dspec ( id_storage ( tid ) ) ;
2353
		if ( ( tds & dspec_instance ) && !( tds & dspec_defn ) ) {
2354
		    /* This is itself a template */
2355
		    if ( !EQ_id ( tid, id ) ) copy_template ( tid, force ) ;
2356
		}
2357
		e = DEREF_exp ( id_function_etc_defn ( tid ) ) ;
2358
	    }
2359
	    break ;
2360
	}
2361
	case id_stat_member_tag : {
2362
	    /* Static data members */
2363
	    int undef = 0 ;
2364
	    e = DEREF_exp ( id_stat_member_init ( id ) ) ;
2365
	    if ( !IS_NULL_exp ( e ) ) {
2366
		/* Template member already defined */
2367
		return ;
2368
	    }
2369
	    form = find_form ( id, &undef ) ;
2370
	    tid = match_specialise ( id, NULL_id ) ;
2371
	    if ( !IS_NULL_id ( tid ) ) {
2372
		/* Find static member definition */
2373
		DECL_SPEC tds = DEREF_dspec ( id_storage ( tid ) ) ;
2374
		if ( ( tds & dspec_instance ) && !( tds & dspec_defn ) ) {
2375
		    /* This is itself a template */
2376
		    if ( !EQ_id ( tid, id ) ) copy_template ( tid, force ) ;
2377
		}
2378
		e = DEREF_exp ( id_stat_member_init ( tid ) ) ;
2379
	    }
2380
	    if ( force == 2 ) {
2381
		/* Check for constants only */
2382
		if ( IS_NULL_exp ( e ) ) return ;
2383
		switch ( TAG_exp ( e ) ) {
2384
		    case exp_int_lit_tag : break ;
2385
		    case exp_null_tag : break ;
2386
		    default : return ;
2387
		}
2388
	    }
2389
	    break ;
2390
	}
2391
    }
2392
 
2393
    /* Check for exported templates */
2394
    if ( is_exported ( tid ) ) {
2395
	export_template ( id, 0 ) ;
2396
	ds = DEREF_dspec ( id_storage ( id ) ) ;
2397
    }
2398
 
2399
    /* Define the object */
2400
    if ( IS_NULL_exp ( e ) ) {
2401
	if ( force && !is_exported ( id ) ) {
2402
	    /* Check for non-exported templates */
2403
	    if ( !( ds & dspec_inline ) ) {
2404
		report ( crt_loc, ERR_temp_decl_undef ( id ) ) ;
2405
		export_template ( id, 0 ) ;
2406
		ds = DEREF_dspec ( id_storage ( id ) ) ;
2407
	    }
2408
	}
2409
	CONS_id ( id, still_pending_templates, still_pending_templates ) ;
2410
	ds &= ~dspec_defn ;
2411
    } else {
2412
	ds = define_templ_member ( id, tid, form, e ) ;
2413
	ds |= dspec_defn ;
2414
    }
2415
    COPY_dspec ( id_storage ( id ), ds ) ;
2416
    return ;
2417
}
2418
 
2419
 
2420
/*
2421
    DEFINE A LIST OF TEMPLATES
2422
 
2423
    This routine calls copy_template for each element of the list p.
2424
*/
2425
 
2426
static void copy_template_list
2427
    PROTO_N ( ( p, force ) )
2428
    PROTO_T ( LIST ( IDENTIFIER ) p X int force )
2429
{
2430
    if ( !IS_NULL_list ( p ) ) {
2431
	IDENTIFIER id ;
2432
	DESTROY_CONS_id ( destroy, id, p, p ) ;
2433
	copy_template_list ( p, force ) ;
2434
	DEREF_loc ( id_loc ( id ), crt_loc ) ;
2435
	copy_template ( id, force ) ;
2436
    }
2437
    return ;
2438
}
2439
 
2440
 
2441
/*
2442
    DEFINE ALL PENDING TEMPLATES
2443
 
2444
    This routine defines all the template instances in the list of pending
2445
    templates.  The list of still pending templates is only checked if
2446
    templ is nonzero.  A templ value of 2 is used to indicate the end of
2447
    the file.
2448
*/
2449
 
2450
void clear_templates
2451
    PROTO_N ( ( templ ) )
2452
    PROTO_T ( int templ )
2453
{
2454
    if ( !in_function_defn && !in_class_defn ) {
2455
	LIST ( IDENTIFIER ) p = pending_templates ;
2456
	if ( templ ) {
2457
	    /* Include still pending templates */
2458
	    p = APPEND_list ( still_pending_templates, p ) ;
2459
	    still_pending_templates = NULL_list ( IDENTIFIER ) ;
2460
	}
2461
	if ( !IS_NULL_list ( p ) ) {
2462
	    LOCATION loc ;
2463
	    loc = crt_loc ;
2464
	    bad_crt_loc++ ;
2465
	    while ( !IS_NULL_list ( p ) ) {
2466
		/* Scan through pending templates */
2467
		pending_templates = NULL_list ( IDENTIFIER ) ;
2468
		copy_template_list ( p, 0 ) ;
2469
		p = pending_templates ;
2470
	    }
2471
	    if ( templ == 2 ) {
2472
		/* Check for exported templates */
2473
		p = still_pending_templates ;
2474
		if ( !IS_NULL_list ( p ) ) {
2475
		    still_pending_templates = NULL_list ( IDENTIFIER ) ;
2476
		    copy_template_list ( p, 1 ) ;
2477
		}
2478
	    }
2479
	    crt_loc = loc ;
2480
	    bad_crt_loc-- ;
2481
	}
2482
    }
2483
    return ;
2484
}