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 "etype_ops.h"
35
#include "exp_ops.h"
36
#include "hashid_ops.h"
37
#include "id_ops.h"
38
#include "inst_ops.h"
39
#include "itype_ops.h"
40
#include "member_ops.h"
41
#include "nat_ops.h"
42
#include "nspace_ops.h"
43
#include "off_ops.h"
44
#include "tok_ops.h"
45
#include "type_ops.h"
46
#include "error.h"
47
#include "catalog.h"
48
#include "option.h"
49
#include "basetype.h"
50
#include "check.h"
51
#include "chktype.h"
52
#include "class.h"
53
#include "compile.h"
54
#include "convert.h"
55
#include "copy.h"
56
#include "declare.h"
57
#include "derive.h"
58
#include "dump.h"
59
#include "expression.h"
60
#include "hash.h"
61
#include "identifier.h"
62
#include "initialise.h"
63
#include "instance.h"
64
#include "macro.h"
65
#include "namespace.h"
66
#include "option.h"
67
#include "overload.h"
68
#include "parse.h"
69
#include "predict.h"
70
#include "statement.h"
71
#include "syntax.h"
72
#include "template.h"
73
#include "tokdef.h"
74
#include "token.h"
75
#include "ustring.h"
76
 
77
 
78
/*
79
    TEMPLATE ARGUMENT HACK FLAG
80
 
81
    This flag can be set to true to indicate that the template argument
82
    hack, namely mapping '>>' to '> >' at the end of a set of template
83
    arguments, should be applied.
84
*/
85
 
86
static int apply_rshift_hack = 0 ;
87
 
88
 
89
/*
90
    SKIP TEMPLATE ARGUMENTS OR PARAMETERS
91
 
92
    This routine skips a set of template arguments or parameters.  It
93
    returns the sequence of preprocessing tokens enclosed between the
94
    initial '<' and the matching closing '>'.
95
*/
96
 
97
static PPTOKEN *skip_template
98
    PROTO_N ( ( id ) )
99
    PROTO_T ( IDENTIFIER id )
100
{
101
    PPTOKEN *q ;
102
    LOCATION loc ;
103
    int templ = 0 ;
104
    int angles = 1 ;
105
    int brackets = 0 ;
106
    int t = crt_lex_token ;
107
    PPTOKEN *p = crt_token ;
108
    loc = crt_loc ;
109
    do {
110
	switch ( t ) {
111
	    case lex_less : {
112
		/* Open angle brackets */
113
		if ( !brackets && templ ) angles++ ;
114
		templ = 0 ;
115
		break ;
116
	    }
117
	    case lex_greater : {
118
		/* Close angle brackets */
119
		if ( !brackets ) angles-- ;
120
		templ = 0 ;
121
		break ;
122
	    }
123
	    case lex_rshift : {
124
		/* Map '>>' to '> >' */
125
		if ( !brackets && apply_rshift_hack ) {
126
		    PPTOKEN *r = new_pptok () ;
127
		    r->tok = lex_greater ;
128
		    r->next = crt_token->next ;
129
		    crt_token->tok = lex_greater ;
130
		    crt_token->next = r ;
131
		    angles-- ;
132
		    report ( crt_loc, ERR_temp_names_hack () ) ;
133
		}
134
		templ = 0 ;
135
		break ;
136
	    }
137
	    case lex_open_Hround :
138
	    case lex_open_Hbrace_H1 :
139
	    case lex_open_Hbrace_H2 :
140
	    case lex_open_Hsquare_H1 :
141
	    case lex_open_Hsquare_H2 : {
142
		/* Open brackets */
143
		brackets++ ;
144
		templ = 0 ;
145
		break ;
146
	    }
147
	    case lex_close_Hround :
148
	    case lex_close_Hbrace_H1 :
149
	    case lex_close_Hbrace_H2 :
150
	    case lex_close_Hsquare_H1 :
151
	    case lex_close_Hsquare_H2 : {
152
		/* Close brackets */
153
		if ( brackets ) brackets-- ;
154
		templ = 0 ;
155
		break ;
156
	    }
157
	    case lex_identifier :
158
	    case lex_template :
159
	    case lex_const_Hcast :
160
	    case lex_static_Hcast :
161
	    case lex_dynamic_Hcast :
162
	    case lex_reinterpret_Hcast : {
163
		/* These may be followed by '<' */
164
		/* NOT YET IMPLEMENTED - but are they? */
165
		templ = 1 ;
166
		break ;
167
	    }
168
	    case lex_eof : {
169
		/* End of file */
170
		if ( IS_NULL_id ( id ) ) {
171
		    report ( loc, ERR_temp_param_eof () ) ;
172
		} else {
173
		    report ( loc, ERR_temp_names_eof ( id ) ) ;
174
		}
175
		angles = 0 ;
176
		break ;
177
	    }
178
	    default : {
179
		/* Other tokens */
180
		templ = 0 ;
181
		break ;
182
	    }
183
	}
184
	q = crt_token ;
185
	t = expand_preproc ( EXPAND_AHEAD ) ;
186
    } while ( angles ) ;
187
    q->tok = lex_close_Htemplate ;
188
    snip_tokens ( p, q ) ;
189
    return ( p ) ;
190
}
191
 
192
 
193
/*
194
    SKIP A SET OF TEMPLATE ARGUMENTS
195
 
196
    This routine skips a set of arguments for the template id.  It is
197
    entered with the current token equal to the template name preceeding
198
    the initial '<' if started is false, and equal to the initial '<'
199
    otherwise.  After skipping the current token is either still the
200
    template name or the token following the template arguments, depending
201
    on the value of started.
202
*/
203
 
204
PPTOKEN *skip_template_args
205
    PROTO_N ( ( id, started ) )
206
    PROTO_T ( IDENTIFIER id X int started )
207
{
208
    PPTOKEN *q ;
209
    PPTOKEN *p = crt_token ;
210
    int t = crt_lex_token ;
211
    if ( started ) {
212
	/* Patch in dummy preprocessing token */
213
	q = patch_tokens ( 1 ) ;
214
	q->tok = t ;
215
	t = lex_ignore_token ;
216
	p->tok = t ;
217
    }
218
    IGNORE expand_preproc ( EXPAND_AHEAD ) ;
219
    crt_lex_token = lex_open_Htemplate ;
220
    crt_token->tok = lex_open_Htemplate ;
221
    q = skip_template ( id ) ;
222
    crt_lex_token = t ;
223
    crt_token = p ;
224
    if ( started ) {
225
	/* Advance to following token */
226
	ADVANCE_LEXER ;
227
    }
228
    return ( q ) ;
229
}
230
 
231
 
232
/*
233
    PARSE A SET OF TEMPLATE ARGUMENTS
234
 
235
    This routine parses the template arguments p.  Note that unlike token
236
    arguments the template argument sorts are deduced by look-ahead rather
237
    than from the template sort.
238
*/
239
 
240
static LIST ( TOKEN ) parse_template_args
241
    PROTO_N ( ( p ) )
242
    PROTO_T ( PPTOKEN *p )
243
{
244
    int t ;
245
    PARSE_STATE st ;
246
    LIST ( TOKEN ) args = NULL_list ( TOKEN ) ;
247
    if ( p == NULL ) return ( args ) ;
248
 
249
    /* Initialise parser */
250
    save_state ( &st, 1 ) ;
251
    init_parser ( p ) ;
252
    ADVANCE_LEXER ;
253
    t = crt_lex_token ;
254
    if ( t == lex_open_Htemplate ) {
255
	/* Step over open bracket */
256
	ADVANCE_LEXER ;
257
	t = crt_lex_token ;
258
    }
259
 
260
    /* Scan through arguments */
261
    if ( t != lex_close_Htemplate ) {
262
	for ( ; ; ) {
263
	    TOKEN arg ;
264
	    if ( predict_typeid ( 2 ) ) {
265
		TYPE r = NULL_type ;
266
		have_type_specifier = 0 ;
267
		if ( predict_typename () ) {
268
		    /* Template argument */
269
		    IDENTIFIER rid = NULL_id ;
270
		    parse_id ( &rid ) ;
271
		    MAKE_tok_class ( r, rid, arg ) ;
272
		} else {
273
		    /* Type argument */
274
		    parse_type ( &r ) ;
275
		    MAKE_tok_type ( btype_lang, r, arg ) ;
276
		}
277
	    } else {
278
		/* Expression argument */
279
		EXP e = NULL_exp ;
280
		TYPE r = NULL_type ;
281
		parse_exp ( &e ) ;
282
		if ( !IS_NULL_exp ( e ) ) {
283
		    r = DEREF_type ( exp_type ( e ) ) ;
284
		}
285
		MAKE_tok_exp ( r, 1, e, arg ) ;
286
	    }
287
	    if ( have_syntax_error ) break ;
288
	    CONS_tok ( arg, args, args ) ;
289
	    t = crt_lex_token ;
290
	    if ( t == lex_close_Htemplate ) {
291
		break ;
292
	    } else if ( t == lex_comma ) {
293
		ADVANCE_LEXER ;
294
	    } else {
295
		t = lex_close_Htemplate ;
296
		report ( crt_loc, ERR_lex_expect ( t ) ) ;
297
		break ;
298
	    }
299
	}
300
    }
301
 
302
    /* Restore state */
303
    restore_state ( &st ) ;
304
    p = restore_parser () ;
305
    free_tok_list ( p ) ;
306
 
307
    /* Return result */
308
    args = REVERSE_list ( args ) ;
309
    return ( args ) ;
310
}
311
 
312
 
313
/*
314
    CHECK A TEMPLATE PARAMETER TYPE
315
 
316
    This routine checks the type t of the template parameter id.
317
*/
318
 
319
static void templ_param_type
320
    PROTO_N ( ( id, t ) )
321
    PROTO_T ( IDENTIFIER id X TYPE t )
322
{
323
    switch ( TAG_type ( t ) ) {
324
	case type_floating_tag :
325
	case type_top_tag :
326
	case type_bottom_tag : {
327
	    /* Illegal parameter types */
328
	    report ( crt_loc, ERR_temp_param_type ( id, t ) ) ;
329
	    break ;
330
	}
331
    }
332
    return ;
333
}
334
 
335
 
336
/*
337
    DEFINE A TEMPLATE PARAMETER
338
 
339
    This routine defines the template parameter id to be arg.
340
*/
341
 
342
static int define_templ_param
343
    PROTO_N ( ( id, arg, tid, def ) )
344
    PROTO_T ( IDENTIFIER id X TOKEN arg X IDENTIFIER tid X int def )
345
{
346
    int ok = 1 ;
347
    TOKEN sort = DEREF_tok ( id_token_sort ( id ) ) ;
348
    unsigned tag = TAG_tok ( sort ) ;
349
    if ( tag == tok_type_tag ) {
350
	/* Type parameter */
351
	TYPE t ;
352
	if ( IS_tok_type ( arg ) ) {
353
	    t = DEREF_type ( tok_type_value ( arg ) ) ;
354
	    if ( def ) t = expand_type ( t, 2 ) ;
355
	    if ( !is_global_type ( t ) ) {
356
		/* Type must have external linkage */
357
		ERROR err = ERR_temp_arg_local ( t ) ;
358
		err = concat_error ( ERR_temp_arg_init ( id, tid ), err ) ;
359
		report ( crt_loc, err ) ;
360
	    }
361
	    COPY_type ( tok_type_value ( arg ), t ) ;
362
	} else {
363
	    /* Non-type argument supplied */
364
	    t = type_error ;
365
	    report ( crt_loc, ERR_temp_arg_type ( id, tid ) ) ;
366
	    ok = 0 ;
367
	}
368
	COPY_type ( tok_type_value ( sort ), t ) ;
369
 
370
    } else if ( tag == tok_exp_tag ) {
371
	/* Expression parameter */
372
	EXP e ;
373
	if ( IS_tok_exp ( arg ) ) {
374
	    int over = 0 ;
375
	    ERROR err = NULL_err ;
376
	    TYPE s1 = DEREF_type ( tok_exp_type ( sort ) ) ;
377
	    TYPE s2 = expand_type ( s1, 2 ) ;
378
	    if ( !EQ_type ( s1, s2 ) ) templ_param_type ( id, s2 ) ;
379
	    e = DEREF_exp ( tok_exp_value ( arg ) ) ;
380
	    if ( def ) {
381
		/* Perform conversion if necessary */
382
		unsigned etag = TAG_exp ( e ) ;
383
		e = convert_reference ( e, REF_ASSIGN ) ;
384
		e = expand_exp ( e, 2, 0 ) ;
385
		if ( IS_exp_address_mem ( e ) ) {
386
		    /* Check for overloaded pointer to members */
387
		    EXP a = DEREF_exp ( exp_address_mem_arg ( e ) ) ;
388
		    if ( IS_exp_member ( a ) ) {
389
			IDENTIFIER mid = DEREF_id ( exp_member_id ( a ) ) ;
390
			if ( IS_id_function_etc ( mid ) ) {
391
			    mid = DEREF_id ( id_function_etc_over ( mid ) ) ;
392
			    if ( !IS_NULL_id ( mid ) ) over = 1 ;
393
			}
394
		    }
395
		}
396
		if ( IS_type_array ( s2 ) ) {
397
		    if ( etag == exp_paren_tag ) e = make_paren_exp ( e ) ;
398
		    e = init_array ( s2, cv_none, e, 1, &err ) ;
399
		} else {
400
		    e = init_assign ( s2, cv_none, e, &err ) ;
401
		}
402
		if ( !IS_NULL_err ( err ) ) err = init_error ( err, 0 ) ;
403
	    }
404
	    if ( is_const_exp ( e, 1 ) ) {
405
		switch ( TAG_type ( s2 ) ) {
406
		    case type_integer_tag :
407
		    case type_floating_tag :
408
		    case type_top_tag :
409
		    case type_bottom_tag :
410
		    case type_enumerate_tag :
411
		    case type_token_tag :
412
		    case type_error_tag : {
413
			/* Constants of these types are alright */
414
			break ;
415
		    }
416
		    default : {
417
			/* Check linkage in other cases */
418
			EXP pa = NULL_exp ;
419
			DECL_SPEC ln = find_exp_linkage ( e, &pa, 0 ) ;
420
			if ( ln & dspec_extern ) {
421
			    /* External linkage */
422
			    /* EMPTY */
423
			} else if ( ln & dspec_static ) {
424
			    /* Internal linkage */
425
			    ERROR err2 = ERR_temp_arg_internal () ;
426
			    err = concat_error ( err, err2 ) ;
427
			} else {
428
			    /* No linkage */
429
			    ERROR err2 = ERR_temp_arg_bad () ;
430
			    err = concat_error ( err, err2 ) ;
431
			}
432
			if ( over ) {
433
			    /* Overloaded pointer to member */
434
			    ERROR err2 = ERR_temp_arg_over () ;
435
			    err = concat_error ( err, err2 ) ;
436
			}
437
			break ;
438
		    }
439
		}
440
	    } else {
441
		err = concat_error ( err, ERR_temp_arg_const () ) ;
442
	    }
443
	    if ( !IS_NULL_err ( err ) ) {
444
		err = concat_error ( ERR_temp_arg_init ( id, tid ), err ) ;
445
		report ( crt_loc, err ) ;
446
	    }
447
	    COPY_type ( tok_exp_type ( arg ), s2 ) ;
448
	    COPY_exp ( tok_exp_value ( arg ), e ) ;
449
	} else {
450
	    /* Non-expression argument supplied */
451
	    e = make_error_exp ( 0 ) ;
452
	    report ( crt_loc, ERR_temp_arg_exp ( id, tid ) ) ;
453
	    ok = 0 ;
454
	}
455
	COPY_exp ( tok_exp_value ( sort ), e ) ;
456
 
457
    } else {
458
	/* Template class parameter */
459
	IDENTIFIER sid ;
460
	if ( IS_tok_class ( arg ) ) {
461
	    sid = DEREF_id ( tok_class_value ( arg ) ) ;
462
	    if ( !IS_NULL_id ( sid ) && IS_id_class_name_etc ( sid ) ) {
463
		TYPE s = DEREF_type ( id_class_name_etc_defn ( sid ) ) ;
464
		if ( !is_global_type ( s ) ) {
465
		    /* Type must have external linkage */
466
		    ERROR err = ERR_temp_arg_local ( s ) ;
467
		    err = concat_error ( ERR_temp_arg_init ( id, tid ), err ) ;
468
		    report ( crt_loc, err ) ;
469
		}
470
	    }
471
	    init_template_param ( id, sid ) ;
472
	} else {
473
	    /* Non-template argument supplied */
474
	    HASHID nm = KEYWORD ( lex_zzzz ) ;
475
	    sid = DEREF_id ( hashid_id ( nm ) ) ;
476
	    report ( crt_loc, ERR_temp_arg_templ ( id, tid ) ) ;
477
	    ok = 0 ;
478
	}
479
	COPY_id ( tok_class_value ( sort ), sid ) ;
480
 
481
    }
482
    return ( ok ) ;
483
}
484
 
485
 
486
/*
487
    DEFAULT TEMPLATE ARGUMENTS FLAG
488
 
489
    This flag may be set to false to suppress template default arguments.
490
*/
491
 
492
int allow_templ_dargs = 1 ;
493
 
494
 
495
/*
496
    CHECK A SET OF TEMPLATE ARGUMENTS
497
 
498
    This routine checks the set of template arguments args for the template
499
    tid of sort tok.  Note that if tid is a function then there may be
500
    less arguments than parameters, in this case in_template_decl is set
501
    to indicate that certain template parameters remain unbound.
502
*/
503
 
504
static LIST ( TOKEN ) check_templ_args
505
    PROTO_N ( ( tok, args, tid ) )
506
    PROTO_T ( TOKEN tok X LIST ( TOKEN ) args X IDENTIFIER tid )
507
{
508
    int s ;
509
    int reported = 0 ;
510
    LIST ( TOKEN ) a = args ;
511
    LIST ( TOKEN ) b = NULL_list ( TOKEN ) ;
512
    LIST ( TOKEN ) d = DEREF_list ( tok_templ_dargs ( tok ) ) ;
513
    LIST ( IDENTIFIER ) pids = DEREF_list ( tok_templ_pids ( tok ) ) ;
514
    LIST ( IDENTIFIER ) qids = pids ;
515
    if ( in_template_decl && depends_on_args ( args, pids, 0, 1 ) ) {
516
	/* Be extra careful in this case */
517
	tok = expand_sort ( tok, 1, 1 ) ;
518
	args = check_templ_args ( tok, args, tid ) ;
519
	return ( args ) ;
520
    }
521
    s = save_token_args ( qids, NULL_list ( TOKEN ) ) ;
522
    if ( !allow_templ_dargs ) d = NULL_list ( TOKEN ) ;
523
    while ( !IS_NULL_list ( pids ) ) {
524
	TOKEN arg = NULL_tok ;
525
	IDENTIFIER pid = DEREF_id ( HEAD_list ( pids ) ) ;
526
	if ( !IS_NULL_list ( a ) ) {
527
	    /* Use argument from list */
528
	    arg = DEREF_tok ( HEAD_list ( a ) ) ;
529
	} else if ( !IS_NULL_list ( d ) ) {
530
	    /* Use default argument */
531
	    arg = DEREF_tok ( HEAD_list ( d ) ) ;
532
	    if ( !IS_NULL_tok ( arg ) ) {
533
		/* Add copy to list of arguments */
534
		arg = expand_sort ( arg, -1, 1 ) ;
535
		CONS_tok ( arg, b, b ) ;
536
	    }
537
	}
538
	if ( IS_NULL_tok ( arg ) ) {
539
	    /* Not enough arguments */
540
	    if ( !reported ) {
541
		if ( IS_id_function_etc ( tid ) ) {
542
		    /* Allow for argument deduction */
543
		    a = NULL_list ( TOKEN ) ;
544
		    in_template_decl++ ;
545
		    break ;
546
		}
547
		report ( crt_loc, ERR_temp_arg_less ( tid ) ) ;
548
		reported = 1 ;
549
	    }
550
	    arg = DEREF_tok ( id_token_sort ( pid ) ) ;
551
	    IGNORE is_bound_tok ( arg, 1 ) ;
552
	    arg = expand_sort ( arg, 2, 1 ) ;
553
	    CONS_tok ( arg, b, b ) ;
554
	}
555
	IGNORE define_templ_param ( pid, arg, tid, 1 ) ;
556
	if ( !IS_NULL_list ( d ) ) d = TAIL_list ( d ) ;
557
	if ( !IS_NULL_list ( a ) ) a = TAIL_list ( a ) ;
558
	pids = TAIL_list ( pids ) ;
559
    }
560
    if ( !IS_NULL_list ( a ) ) {
561
	/* Too many arguments */
562
	report ( crt_loc, ERR_temp_arg_more ( tid ) ) ;
563
    }
564
    if ( !IS_NULL_list ( b ) ) {
565
	/* Add default arguments to list */
566
	b = REVERSE_list ( b ) ;
567
	args = APPEND_list ( args, b ) ;
568
    }
569
    restore_token_args ( qids, s ) ;
570
    return ( args ) ;
571
}
572
 
573
 
574
/*
575
    CHECK A SET OF DEDUCED TEMPLATE ARGUMENTS
576
 
577
    This routine checks the deduced template arguments args for the
578
    template tid with parameters pids.
579
*/
580
 
581
void check_deduced_args
582
    PROTO_N ( ( tid, pids, args ) )
583
    PROTO_T ( IDENTIFIER tid X LIST ( IDENTIFIER ) pids X LIST ( TOKEN ) args )
584
{
585
    while ( !IS_NULL_list ( pids ) && !IS_NULL_list ( args ) ) {
586
	IDENTIFIER pid = DEREF_id ( HEAD_list ( pids ) ) ;
587
	TOKEN arg = DEREF_tok ( HEAD_list ( args ) ) ;
588
	IGNORE define_templ_param ( pid, arg, tid, 0 ) ;
589
	args = TAIL_list ( args ) ;
590
	pids = TAIL_list ( pids ) ;
591
    }
592
    return ;
593
}
594
 
595
 
596
/*
597
    DOES A SET OF TEMPLATE ARGUMENTS MATCH A SORT?
598
 
599
    This routine checks whether the template arguments args form a match
600
    for an initial segment of the template sort tok.
601
*/
602
 
603
static int match_template_args
604
    PROTO_N ( ( tok, args ) )
605
    PROTO_T ( TOKEN tok X LIST ( TOKEN ) args )
606
{
607
    LIST ( IDENTIFIER ) pids = DEREF_list ( tok_templ_pids ( tok ) ) ;
608
    while ( !IS_NULL_list ( pids ) && !IS_NULL_list ( args ) ) {
609
	IDENTIFIER pid = DEREF_id ( HEAD_list ( pids ) ) ;
610
	TOKEN sort = DEREF_tok ( id_token_sort ( pid ) ) ;
611
	TOKEN arg = DEREF_tok ( HEAD_list ( args ) ) ;
612
	if ( TAG_tok ( arg ) != TAG_tok ( sort ) ) {
613
	    /* Argument sorts do not match */
614
	    return ( 0 ) ;
615
	}
616
	args = TAIL_list ( args ) ;
617
	pids = TAIL_list ( pids ) ;
618
    }
619
    if ( !IS_NULL_list ( args ) ) {
620
	/* Too many arguments */
621
	return ( 0 ) ;
622
    }
623
    return ( 1 ) ;
624
}
625
 
626
 
627
/*
628
    APPLY A FUNCTION TEMPLATE
629
 
630
    This routine applies the function template id to the arguments args.
631
    Because id may comprise several overloaded template functions it is
632
    necessary to check each to determine whether the template parameter
633
    sorts match the argument sorts.  If more than one match is found the
634
    result is an overloaded function.
635
*/
636
 
637
static IDENTIFIER apply_func_templ
638
    PROTO_N ( ( id, args, def ) )
639
    PROTO_T ( IDENTIFIER id X LIST ( TOKEN ) args X int def )
640
{
641
    int force = 0 ;
642
    IDENTIFIER tid = NULL_id ;
643
    do {
644
	/* Build up result */
645
	IDENTIFIER fid = id ;
646
	while ( !IS_NULL_id ( fid ) ) {
647
	    TYPE t = DEREF_type ( id_function_etc_type ( fid ) ) ;
648
	    if ( IS_type_templ ( t ) ) {
649
		TOKEN sort = DEREF_tok ( type_templ_sort ( t ) ) ;
650
		if ( force || match_template_args ( sort, args ) ) {
651
		    /* Argument sorts match */
652
		    IDENTIFIER sid = tid ;
653
		    int td = in_template_decl ;
654
		    args = check_templ_args ( sort, args, fid ) ;
655
		    tid = instance_func ( fid, args, 0, def ) ;
656
		    COPY_id ( id_function_etc_over ( tid ), sid ) ;
657
		    in_template_decl = td ;
658
		}
659
	    }
660
	    fid = DEREF_id ( id_function_etc_over ( fid ) ) ;
661
	}
662
	if ( force ) {
663
	    /* Should have bound arguments by now */
664
	    if ( IS_NULL_id ( tid ) ) tid = id ;
665
	} else {
666
	    /* Try again allowing for mismatches */
667
	    force = 1 ;
668
	}
669
    } while ( IS_NULL_id ( tid ) ) ;
670
    return ( tid ) ;
671
}
672
 
673
 
674
/*
675
    APPLY A TYPEDEF TEMPLATE
676
 
677
    This routine applies the typedef template id to the arguments args.
678
*/
679
 
680
static TYPE apply_typedef_templ
681
    PROTO_N ( ( id, args ) )
682
    PROTO_T ( IDENTIFIER id X LIST ( TOKEN ) args )
683
{
684
    TYPE t = DEREF_type ( id_class_name_etc_defn ( id ) ) ;
685
    if ( IS_type_templ ( t ) ) {
686
	int td = in_template_decl ;
687
	TOKEN sort = DEREF_tok ( type_templ_sort ( t ) ) ;
688
	LIST ( IDENTIFIER ) pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
689
	args = check_templ_args ( sort, args, id ) ;
690
	t = DEREF_type ( type_templ_defn ( t ) ) ;
691
	if ( is_templ_type ( t ) ) {
692
	    /* Template template parameter */
693
	    IDENTIFIER tid = DEREF_id ( type_token_tok ( t ) ) ;
694
	    MAKE_type_token ( cv_none, tid, args, t ) ;
695
	} else {
696
	    /* Expand type definition */
697
	    int d = save_token_args ( pids, args ) ;
698
	    TYPE s = expand_type ( t, 1 ) ;
699
	    if ( EQ_type ( s, t ) ) s = copy_typedef ( id, t, cv_none ) ;
700
	    restore_token_args ( pids, d ) ;
701
	    t = s ;
702
	}
703
	in_template_decl = td ;
704
    } else {
705
	report ( crt_loc, ERR_temp_names_not ( id ) ) ;
706
	t = copy_typedef ( id, t, cv_none ) ;
707
    }
708
    return ( t ) ;
709
}
710
 
711
 
712
/*
713
    APPLY A CLASS TEMPLATE
714
 
715
    This routine applies the class template id to the arguments args.
716
*/
717
 
718
static IDENTIFIER apply_type_templ
719
    PROTO_N ( ( id, args, def ) )
720
    PROTO_T ( IDENTIFIER id X LIST ( TOKEN ) args X int def )
721
{
722
    if ( IS_id_class_name ( id ) ) {
723
	/* Class template */
724
	TYPE t ;
725
	DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
726
	if ( ds & dspec_implicit ) {
727
	    /* Allow for nested calls */
728
	    IDENTIFIER tid = find_template ( id, 0 ) ;
729
	    if ( !IS_NULL_id ( tid ) ) id = tid ;
730
	}
731
	t = DEREF_type ( id_class_name_etc_defn ( id ) ) ;
732
	if ( IS_type_templ ( t ) ) {
733
	    int td = in_template_decl ;
734
	    TOKEN sort = DEREF_tok ( type_templ_sort ( t ) ) ;
735
	    args = check_templ_args ( sort, args, id ) ;
736
	    id = instance_type ( id, args, 0, def ) ;
737
	    in_template_decl = td ;
738
	} else {
739
	    report ( crt_loc, ERR_temp_names_not ( id ) ) ;
740
	}
741
    } else {
742
	/* Type alias template */
743
	TYPE t = apply_typedef_templ ( id, args ) ;
744
	if ( IS_type_compound ( t ) ) {
745
	    CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
746
	    complete_class ( ct, def ) ;
747
	    id = DEREF_id ( ctype_name ( ct ) ) ;
748
	} else {
749
	    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
750
	    NAMESPACE ns = DEREF_nspace ( id_parent ( id ) ) ;
751
	    decl_loc = crt_loc ;
752
	    id = make_typedef ( ns, nm, t, dspec_none ) ;
753
	}
754
    }
755
    return ( id ) ;
756
}
757
 
758
 
759
/*
760
    APPLY A TEMPLATE TO A SET OF ARGUMENTS
761
 
762
    This routine applies the template id to the arguments args.
763
*/
764
 
765
IDENTIFIER apply_template
766
    PROTO_N ( ( id, args, def, force ) )
767
    PROTO_T ( IDENTIFIER id X LIST ( TOKEN ) args X int def X int force )
768
{
769
    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
770
    if ( ds & dspec_template ) {
771
	if ( IS_id_function_etc ( id ) ) {
772
	    id = apply_func_templ ( id, args, def ) ;
773
	} else {
774
	    id = apply_type_templ ( id, args, def ) ;
775
	}
776
    } else {
777
	TYPE form ;
778
	MAKE_type_token ( cv_none, id, args, form ) ;
779
	if ( force || is_templ_depend ( form ) ) {
780
	    /* Dummy template identifier */
781
	    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
782
	    NAMESPACE ns = DEREF_nspace ( id_parent ( id ) ) ;
783
	    MAKE_id_undef ( nm, dspec_none, ns, crt_loc, id ) ;
784
	    COPY_type ( id_undef_form ( id ), form ) ;
785
	} else {
786
	    report ( crt_loc, ERR_temp_names_not ( id ) ) ;
787
	}
788
    }
789
    return ( id ) ;
790
}
791
 
792
 
793
/*
794
    PARSE A SET OF NON-TYPE TEMPLATE ARGUMENTS
795
 
796
    This routine parses the template arguments p for the non-class template
797
    id.  This includes both template functions and dummy template identifiers
798
    such as in 'ptr->template id < ... >'.
799
*/
800
 
801
IDENTIFIER parse_id_template
802
    PROTO_N ( ( id, p, def ) )
803
    PROTO_T ( IDENTIFIER id X PPTOKEN *p X int def )
804
{
805
    LIST ( TOKEN ) args = parse_template_args ( p ) ;
806
    id = apply_template ( id, args, def, 1 ) ;
807
    return ( id ) ;
808
}
809
 
810
 
811
/*
812
    PARSE A SET OF TYPE TEMPLATE ARGUMENTS
813
 
814
    This routine parses the template arguments p for the class template
815
    id.  def is passed to instance_type.
816
*/
817
 
818
IDENTIFIER parse_type_template
819
    PROTO_N ( ( id, p, def ) )
820
    PROTO_T ( IDENTIFIER id X PPTOKEN *p X int def )
821
{
822
    LIST ( TOKEN ) args = parse_template_args ( p ) ;
823
    id = apply_type_templ ( id, args, def ) ;
824
    return ( id ) ;
825
}
826
 
827
 
828
/*
829
    PARSE A SET OF TYPEDEF TEMPLATE ARGUMENTS
830
 
831
    This routine parses the template arguments p for the typedef
832
    template id.
833
*/
834
 
835
TYPE parse_typedef_templ
836
    PROTO_N ( ( id, p ) )
837
    PROTO_T ( IDENTIFIER id X PPTOKEN *p )
838
{
839
    LIST ( TOKEN ) args = parse_template_args ( p ) ;
840
    TYPE t = apply_typedef_templ ( id, args ) ;
841
    return ( t ) ;
842
}
843
 
844
 
845
/*
846
    DEDUCE A TEMPLATE TYPE
847
 
848
    This routine deduces the arguments for the template type id called
849
    without arguments.  Within a template class definition the template
850
    name gives the the template applied to the current arguments.
851
    Otherwise template declarations and definitions (for which used is
852
    false) are allowed but other instances are not.
853
*/
854
 
855
TYPE deduce_type_template
856
    PROTO_N ( ( id, used ) )
857
    PROTO_T ( IDENTIFIER id X int used )
858
{
859
    TYPE t = DEREF_type ( id_class_name_etc_defn ( id ) ) ;
860
    if ( used ) {
861
	TYPE s = t ;
862
	while ( IS_type_templ ( s ) ) {
863
	    s = DEREF_type ( type_templ_defn ( s ) ) ;
864
	}
865
	if ( IS_type_compound ( s ) ) {
866
	    CLASS_TYPE cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
867
	    if ( defining_class ( cs ) ) {
868
		/* In class definition */
869
		return ( s ) ;
870
	    }
871
	}
872
	report ( crt_loc, ERR_temp_local_not ( t ) ) ;
873
    }
874
    return ( t ) ;
875
}
876
 
877
 
878
/*
879
    CURRENT TEMPLATE NAMESPACE
880
 
881
    This variable is used within a template declaration to hold the
882
    namespace in which the template parameters are declared.
883
*/
884
 
885
NAMESPACE templ_namespace = NULL_nspace ;
886
 
887
 
888
/*
889
    LIST OF ALL TEMPLATE PARAMETERS
890
 
891
    These lists are dummy values representing the lists of all template
892
    parameters and all template or token parameters.
893
*/
894
 
895
LIST ( IDENTIFIER ) any_templ_param = NULL_list ( IDENTIFIER ) ;
896
LIST ( IDENTIFIER ) any_token_param = NULL_list ( IDENTIFIER ) ;
897
 
898
 
899
/*
900
    PARSE A SET OF TEMPLATE PARAMETERS
901
 
902
    This routine parses a set of template parameters.  It is entered after
903
    the initial 'template' has been read.  ex is true if this was preceded
904
    by 'export'.
905
*/
906
 
907
TOKEN template_params
908
    PROTO_N ( ( ex ) )
909
    PROTO_T ( int ex )
910
{
911
    int t ;
912
    TOKEN tok ;
913
    PPTOKEN *p ;
914
    NAMESPACE ns ;
915
    LOCATION loc ;
916
    PARSE_STATE s ;
917
    int have_darg = 0 ;
918
    unsigned long npars = 0 ;
919
    DECL_SPEC use = dspec_none ;
920
    LIST ( TOKEN ) dargs = NULL_list ( TOKEN ) ;
921
    LIST ( IDENTIFIER ) pids = NULL_list ( IDENTIFIER ) ;
922
 
923
    /* Can't have template declarations inside blocks */
924
    if ( in_function_defn ) {
925
	report ( crt_loc, ERR_temp_decl_scope () ) ;
926
    } else if ( in_class_defn && really_in_function_defn ) {
927
	report ( crt_loc, ERR_temp_mem_local () ) ;
928
    }
929
 
930
    /* Mark exported templates */
931
    if ( ex || option ( OPT_templ_export ) ) use |= dspec_extern ;
932
 
933
    /* Check for initial '<' */
934
    if ( crt_lex_token != lex_less ) {
935
	/* Explicit instantiation */
936
	MAKE_tok_templ ( use, NULL_nspace, tok ) ;
937
	return ( tok ) ;
938
    }
939
 
940
    /* Start template parameter namespace */
941
    ns = make_namespace ( NULL_id, nspace_templ_tag, 0 ) ;
942
    push_namespace ( ns ) ;
943
    in_template_decl++ ;
944
    record_location++ ;
945
 
946
    /* Prepare to parse template parameters */
947
    ADVANCE_LEXER ;
948
    loc = crt_loc ;
949
    p = skip_template ( NULL_id ) ;
950
    save_state ( &s, 1 ) ;
951
    crt_loc = loc ;
952
    init_parser ( p ) ;
953
    ADVANCE_LEXER ;
954
    t = crt_lex_token ;
955
 
956
    /* Parse template parameters */
957
    if ( t != lex_close_Htemplate ) {
958
	for ( ; ; ) {
959
	    /* Declare parameter */
960
	    IDENTIFIER pid = NULL_id ;
961
	    decl_loc = crt_loc ;
962
	    if ( predict_template () ) {
963
		/* Type parameter */
964
		parse_type_param ( &pid ) ;
965
	    } else {
966
		/* Expression parameter */
967
		if ( crt_lex_token == lex_typename ) {
968
		    /* Replace 'typename' by 'class' */
969
		    crt_lex_token = lex_class ;
970
		}
971
		parse_param ( NULL_type, CONTEXT_TEMPL_PARAM, &pid ) ;
972
	    }
973
 
974
	    /* Add parameter to list */
975
	    if ( !IS_NULL_id ( pid ) ) {
976
		DECL_SPEC ds = DEREF_dspec ( id_storage ( pid ) ) ;
977
		ds |= dspec_template ;
978
		COPY_dspec ( id_storage ( pid ), ds ) ;
979
		if ( do_dump ) dump_token_param ( pid ) ;
980
		tok = DEREF_tok ( id_token_sort ( pid ) ) ;
981
		switch ( TAG_tok ( tok ) ) {
982
 
983
		    case tok_exp_tag : {
984
			/* Expression parameter */
985
			int c ;
986
			EXP e ;
987
			TYPE r ;
988
			DECONS_tok_exp ( r, c, e, tok ) ;
989
			templ_param_type ( pid, r ) ;
990
			if ( IS_NULL_exp ( e ) ) {
991
			    if ( have_darg ) have_darg = 2 ;
992
			    tok = NULL_tok ;
993
			} else {
994
			    COPY_exp ( tok_exp_value ( tok ), NULL_exp ) ;
995
			    MAKE_tok_exp ( r, c, e, tok ) ;
996
			    have_darg = 1 ;
997
			}
998
			break ;
999
		    }
1000
 
1001
		    case tok_type_tag : {
1002
			/* Type parameter */
1003
			TYPE r = DEREF_type ( tok_type_value ( tok ) ) ;
1004
			if ( IS_NULL_type ( r ) ) {
1005
			    if ( have_darg ) have_darg = 2 ;
1006
			    tok = NULL_tok ;
1007
			} else {
1008
			    COPY_type ( tok_type_value ( tok ), NULL_type ) ;
1009
			    MAKE_tok_type ( btype_lang, r, tok ) ;
1010
			    have_darg = 1 ;
1011
			}
1012
			break ;
1013
		    }
1014
 
1015
		    case tok_class_tag : {
1016
			/* Template class parameter */
1017
			TYPE r = DEREF_type ( tok_class_type ( tok ) ) ;
1018
			IDENTIFIER cid = DEREF_id ( tok_class_value ( tok ) ) ;
1019
			if ( IS_NULL_id ( cid ) ) {
1020
			    if ( have_darg ) have_darg = 2 ;
1021
			    tok = NULL_tok ;
1022
			} else {
1023
			    COPY_id ( tok_class_value ( tok ), NULL_id ) ;
1024
			    MAKE_tok_class ( r, cid, tok ) ;
1025
			    have_darg = 1 ;
1026
			}
1027
			break ;
1028
		    }
1029
 
1030
		    default : {
1031
			/* Shouldn't occur */
1032
			tok = NULL_tok ;
1033
			break ;
1034
		    }
1035
		}
1036
		if ( have_darg == 2 ) {
1037
		    /* Missing default argument */
1038
		    report ( crt_loc, ERR_temp_param_default ( pid ) ) ;
1039
		}
1040
		CONS_tok ( tok, dargs, dargs ) ;
1041
		CONS_id ( pid, pids, pids ) ;
1042
		npars++ ;
1043
	    }
1044
 
1045
	    /* Check for next parameter */
1046
	    t = crt_lex_token ;
1047
	    if ( t == lex_close_Htemplate ) {
1048
		/* End of parameter list */
1049
		break ;
1050
	    } else if ( t == lex_comma ) {
1051
		/* Move on to next parameter */
1052
		ADVANCE_LEXER ;
1053
	    } else {
1054
		/* Syntax error */
1055
		if ( !have_syntax_error ) {
1056
		    ERROR err = ERR_lex_parse ( crt_token ) ;
1057
		    report ( crt_loc, err ) ;
1058
		}
1059
		break ;
1060
	    }
1061
	}
1062
    }
1063
 
1064
    /* Restore parser */
1065
    restore_state ( &s ) ;
1066
    p = restore_parser () ;
1067
    free_tok_list ( p ) ;
1068
 
1069
    /* Construct the result */
1070
    MAKE_tok_templ ( use, crt_namespace, tok ) ;
1071
    if ( IS_NULL_list ( pids ) ) {
1072
	/* Explicit specialisation */
1073
	IGNORE pop_namespace () ;
1074
	in_template_decl-- ;
1075
	record_location-- ;
1076
    } else {
1077
	IGNORE check_value ( OPT_VAL_template_pars, npars ) ;
1078
	pids = REVERSE_list ( pids ) ;
1079
	dargs = REVERSE_list ( dargs ) ;
1080
	COPY_list ( tok_templ_pids ( tok ), pids ) ;
1081
	COPY_list ( tok_templ_dargs ( tok ), dargs ) ;
1082
	set_proc_token ( pids ) ;
1083
	templ_namespace = ns ;
1084
    }
1085
    return ( tok ) ;
1086
}
1087
 
1088
 
1089
/*
1090
    CREATE A TEMPLATE TYPE QUALIFIER
1091
 
1092
    This routine creates a template type qualifier from the template
1093
    parameters tok and the type t.  It also terminates the template
1094
    parameter namespace while leaving its names in scope.
1095
*/
1096
 
1097
TYPE make_template_type
1098
    PROTO_N ( ( tok, t ) )
1099
    PROTO_T ( TOKEN tok X TYPE t )
1100
{
1101
    TYPE s ;
1102
    LIST ( IDENTIFIER ) pids = DEREF_list ( tok_templ_pids ( tok ) ) ;
1103
    if ( !IS_NULL_list ( pids ) ) {
1104
	/* Remove template parameters */
1105
	IGNORE restore_namespace () ;
1106
    }
1107
    MAKE_type_templ ( cv_none, tok, NULL_type, 0, s ) ;
1108
    if ( !IS_NULL_type ( t ) ) {
1109
	unsigned tag = TAG_type ( t ) ;
1110
	NAMESPACE ns = DEREF_nspace ( tok_templ_pars ( tok ) ) ;
1111
	if ( IS_NULL_nspace ( ns ) ) {
1112
	    /* Can't have 'template template < ... >' */
1113
	    report ( crt_loc, ERR_temp_explicit_templ () ) ;
1114
	    s = NULL_type ;
1115
	} else {
1116
	    if ( tag == type_templ_tag ) {
1117
		tok = DEREF_tok ( type_templ_sort ( t ) ) ;
1118
		ns = DEREF_nspace ( tok_templ_pars ( tok ) ) ;
1119
		if ( IS_NULL_nspace ( ns ) ) {
1120
		    /* Can't have 'template < ... > template' */
1121
		    report ( crt_loc, ERR_temp_explicit_templ () ) ;
1122
		    t = DEREF_type ( type_templ_defn ( t ) ) ;
1123
		    tag = TAG_type ( t ) ;
1124
		}
1125
	    }
1126
	}
1127
	if ( tag == type_func_tag ) {
1128
	    /* Ignore linkage specifiers */
1129
	    CV_SPEC cv = DEREF_cv ( type_func_mqual ( t ) ) ;
1130
	    cv &= ~cv_language ;
1131
	    cv |= cv_cpp ;
1132
	    COPY_cv ( type_func_mqual ( t ), cv ) ;
1133
	}
1134
	s = inject_pre_type ( t, s, 0 ) ;
1135
    }
1136
    return ( s ) ;
1137
}
1138
 
1139
 
1140
/*
1141
    END A TEMPLATE DECLARATION
1142
 
1143
    This routine ends a template declaration.  It removes the names from
1144
    the template parameter namespace from scope.
1145
*/
1146
 
1147
void end_template
1148
    PROTO_N ( ( tok ) )
1149
    PROTO_T ( TOKEN tok )
1150
{
1151
    LIST ( IDENTIFIER ) pids = DEREF_list ( tok_templ_pids ( tok ) ) ;
1152
    if ( !IS_NULL_list ( pids ) ) {
1153
	remove_namespace () ;
1154
	templ_namespace = NULL_nspace ;
1155
	in_template_decl-- ;
1156
	record_location-- ;
1157
	if ( in_template_decl ) {
1158
	    /* Find enclosing template namespace */
1159
	    LIST ( NAMESPACE ) lns = LIST_stack ( namespace_stack ) ;
1160
	    while ( !IS_NULL_list ( lns ) ) {
1161
		NAMESPACE ns = DEREF_nspace ( HEAD_list ( lns ) ) ;
1162
		if ( IS_nspace_templ ( ns ) ) {
1163
		    templ_namespace = ns ;
1164
		    break ;
1165
		}
1166
		lns = TAIL_list ( lns ) ;
1167
	    }
1168
	}
1169
    }
1170
    if ( !in_template_decl ) clear_templates ( 1 ) ;
1171
    return ;
1172
}
1173
 
1174
 
1175
/*
1176
    CHECK A TEMPLATE DECLARATOR
1177
 
1178
    This routine is called whenever the template type t is used to qualify
1179
    a class definition or a function declarator.
1180
*/
1181
 
1182
void template_decl
1183
    PROTO_N ( ( t ) )
1184
    PROTO_T ( TYPE t )
1185
{
1186
    while ( !IS_NULL_type ( t ) && IS_type_templ ( t ) ) {
1187
	TOKEN sort = DEREF_tok ( type_templ_sort ( t ) ) ;
1188
	DECL_SPEC ds = DEREF_dspec ( tok_templ_usage ( sort ) ) ;
1189
	if ( ds & dspec_used ) {
1190
	    /* Already used */
1191
	    report ( crt_loc, ERR_temp_decl_one () ) ;
1192
	}
1193
	ds |= dspec_used ;
1194
	COPY_dspec ( tok_templ_usage ( sort ), ds ) ;
1195
	t = DEREF_type ( type_templ_defn ( t ) ) ;
1196
    }
1197
    return ;
1198
}
1199
 
1200
 
1201
/*
1202
    EXPORT A SET OF TEMPLATE INSTANCES
1203
 
1204
    This routine exports the instances associated with the template type t.
1205
    It returns the non-template component of t.
1206
*/
1207
 
1208
static TYPE export_instances
1209
    PROTO_N ( ( t, def ) )
1210
    PROTO_T ( TYPE t X int def )
1211
{
1212
    while ( IS_type_templ ( t ) ) {
1213
	TOKEN sort = DEREF_tok ( type_templ_sort ( t ) ) ;
1214
	INSTANCE apps = DEREF_inst ( tok_templ_apps ( sort ) ) ;
1215
	while ( !IS_NULL_inst ( apps ) ) {
1216
	    DECL_SPEC acc = DEREF_dspec ( inst_templ_access ( apps ) ) ;
1217
	    if ( !( acc & ( dspec_alias | dspec_main ) ) ) {
1218
		IDENTIFIER id = DEREF_id ( inst_templ_id ( apps ) ) ;
1219
		export_template ( id, def ) ;
1220
	    }
1221
	    acc |= dspec_typedef ;
1222
	    COPY_dspec ( inst_templ_access ( apps ), acc ) ;
1223
	    apps = DEREF_inst ( inst_next ( apps ) ) ;
1224
	}
1225
	t = DEREF_type ( type_templ_defn ( t ) ) ;
1226
    }
1227
    return ( t ) ;
1228
}
1229
 
1230
 
1231
/*
1232
    EXPORT A TEMPLATE IDENTIFIER
1233
 
1234
    This routine marks the template identifier id as having been exported.
1235
    def is 2 for the first explicit declaration of a template, 1 for a
1236
    redeclaration and 0 otherwise.
1237
*/
1238
 
1239
void export_template
1240
    PROTO_N ( ( id, def ) )
1241
    PROTO_T ( IDENTIFIER id X int def )
1242
{
1243
    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
1244
    if ( ds & ( dspec_inherit | dspec_implicit ) ) return ;
1245
    if ( ds & ( dspec_inline | dspec_static ) ) return ;
1246
    if ( def == 0 && ( ds & dspec_typedef ) ) {
1247
	/* Already exported */
1248
	return ;
1249
    }
1250
    ds |= dspec_typedef ;
1251
    COPY_dspec ( id_storage ( id ), ds ) ;
1252
    if ( def == 2 && !has_linkage ( id ) ) {
1253
	/* Can't export anonymous identifiers */
1254
	report ( crt_loc, ERR_temp_decl_export ( id ) ) ;
1255
    }
1256
    switch ( TAG_id ( id ) ) {
1257
	case id_class_name_tag :
1258
	case id_class_alias_tag : {
1259
	    /* Template classes */
1260
	    TYPE t = DEREF_type ( id_class_name_etc_defn ( id ) ) ;
1261
	    t = export_instances ( t, def ) ;
1262
	    if ( IS_type_compound ( t ) ) {
1263
		CLASS_TYPE ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
1264
		IDENTIFIER cid = DEREF_id ( ctype_name ( ct ) ) ;
1265
		if ( EQ_id ( id, cid ) ) {
1266
		    NAMESPACE ns = DEREF_nspace ( ctype_member ( ct ) ) ;
1267
		    MEMBER mem = DEREF_member ( nspace_ctype_first ( ns ) ) ;
1268
		    while ( !IS_NULL_member ( mem ) ) {
1269
			/* Scan through class members */
1270
			IDENTIFIER pid = DEREF_id ( member_id ( mem ) ) ;
1271
			IDENTIFIER qid = DEREF_id ( member_alt ( mem ) ) ;
1272
			if ( !IS_NULL_id ( pid ) ) {
1273
			    export_template ( pid, def ) ;
1274
			}
1275
			if ( !IS_NULL_id ( qid ) && !EQ_id ( qid, pid ) ) {
1276
			    export_template ( qid, def ) ;
1277
			}
1278
			mem = DEREF_member ( member_next ( mem ) ) ;
1279
		    }
1280
		}
1281
	    }
1282
	    break ;
1283
	}
1284
	case id_function_tag :
1285
	case id_mem_func_tag :
1286
	case id_stat_mem_func_tag : {
1287
	    /* Template functions */
1288
	    TYPE t = DEREF_type ( id_function_etc_type ( id ) ) ;
1289
	    IGNORE export_instances ( t, def ) ;
1290
	    update_tag ( id, 0 ) ;
1291
	    break ;
1292
	}
1293
	case id_stat_member_tag : {
1294
	    /* Static data members */
1295
	    update_tag ( id, 0 ) ;
1296
	    break ;
1297
	}
1298
    }
1299
    return ;
1300
}
1301
 
1302
 
1303
/*
1304
    HAS A TEMPLATE BEEN EXPORTED?
1305
 
1306
    This routine checks whether the template instance id has been exported.
1307
*/
1308
 
1309
int is_exported
1310
    PROTO_N ( ( id ) )
1311
    PROTO_T ( IDENTIFIER id )
1312
{
1313
    TYPE form ;
1314
    int def = 0 ;
1315
    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
1316
    if ( ds & dspec_typedef ) return ( 1 ) ;
1317
    form = find_form ( id, &def ) ;
1318
    if ( !IS_NULL_type ( form ) && IS_type_instance ( form ) ) {
1319
	IDENTIFIER tid = DEREF_id ( type_instance_id ( form ) ) ;
1320
	ds = DEREF_dspec ( id_storage ( tid ) ) ;
1321
	if ( ds & dspec_typedef ) {
1322
	    export_template ( id, 0 ) ;
1323
	    return ( 1 ) ;
1324
	}
1325
    }
1326
    return ( 0 ) ;
1327
}
1328
 
1329
 
1330
/*
1331
    CREATE A SET OF PRIMARY TEMPLATE ARGUMENTS
1332
 
1333
    This routine creates a list of primary template arguments corresponding
1334
    to the template parameters pids.
1335
*/
1336
 
1337
LIST ( TOKEN ) make_primary_args
1338
    PROTO_N ( ( pids ) )
1339
    PROTO_T ( LIST ( IDENTIFIER ) pids )
1340
{
1341
    LIST ( TOKEN ) args = NULL_list ( TOKEN ) ;
1342
    while ( !IS_NULL_list ( pids ) ) {
1343
	IDENTIFIER pid = DEREF_id ( HEAD_list ( pids ) ) ;
1344
	TOKEN arg = apply_token ( pid, NULL_list ( TOKEN ) ) ;
1345
	CONS_tok ( arg, args, args ) ;
1346
	pids = TAIL_list ( pids ) ;
1347
    }
1348
    return ( REVERSE_list ( args ) ) ;
1349
}
1350
 
1351
 
1352
/*
1353
    CHECK A SET OF PRIMARY TEMPLATE PARAMETERS
1354
 
1355
    This routine checks the template parameters given by the type t for
1356
    the declaration of the primary template class or function id.  It
1357
    returns the non-template component of t.
1358
*/
1359
 
1360
TYPE check_templ_params
1361
    PROTO_N ( ( t, id ) )
1362
    PROTO_T ( TYPE t X IDENTIFIER id )
1363
{
1364
    int depth = 0 ;
1365
    unsigned tag = TAG_type ( t ) ;
1366
    while ( tag == type_templ_tag ) {
1367
	TOKEN sort = DEREF_tok ( type_templ_sort ( t ) ) ;
1368
	NAMESPACE ns = DEREF_nspace ( tok_templ_pars ( sort ) ) ;
1369
	DECL_SPEC use = DEREF_dspec ( tok_templ_usage ( sort ) ) ;
1370
	LIST ( IDENTIFIER ) pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
1371
	TYPE s = DEREF_type ( type_templ_defn ( t ) ) ;
1372
	tag = TAG_type ( s ) ;
1373
	if ( IS_NULL_list ( pids ) ) {
1374
	    /* No template parameters */
1375
	    if ( IS_NULL_nspace ( ns ) ) {
1376
		/* Explicit instantiation */
1377
		report ( decl_loc, ERR_temp_explicit_bad ( id ) ) ;
1378
	    } else {
1379
		/* Specialisation */
1380
		report ( decl_loc, ERR_temp_param_none ( id ) ) ;
1381
		COPY_id ( nspace_name ( ns ), id ) ;
1382
	    }
1383
	} else {
1384
	    /* Create primary specialisation */
1385
	    TYPE form ;
1386
	    TYPE prim ;
1387
	    INSTANCE apps ;
1388
	    DECL_SPEC ds = ( dspec_template | dspec_extern | dspec_main ) ;
1389
	    LIST ( TOKEN ) args = make_primary_args ( pids ) ;
1390
	    MAKE_type_token ( cv_none, id, args, form ) ;
1391
	    MAKE_type_templ ( cv_none, sort, form, 1, prim ) ;
1392
	    apps = DEREF_inst ( tok_templ_apps ( sort ) ) ;
1393
	    MAKE_inst_templ ( prim, apps, id, ds, all_instances, apps ) ;
1394
	    COPY_inst ( type_token_app ( form ), apps ) ;
1395
	    COPY_inst ( tok_templ_apps ( sort ), apps ) ;
1396
	    all_instances = apps ;
1397
	    if ( tag == type_compound_tag ) {
1398
		CLASS_TYPE cs = DEREF_ctype ( type_compound_defn ( s ) ) ;
1399
		COPY_type ( ctype_form ( cs ), form ) ;
1400
	    } else if ( tag == type_func_tag ) {
1401
		/* Can't have default arguments with function */
1402
		if ( check_templ_dargs ( t ) ) {
1403
		    report ( decl_loc, ERR_temp_param_func () ) ;
1404
		}
1405
	    }
1406
	    COPY_id ( nspace_name ( ns ), id ) ;
1407
	}
1408
	if ( use & dspec_extern ) export_template ( id, 2 ) ;
1409
	depth++ ;
1410
	t = s ;
1411
    }
1412
    if ( depth > 1 ) {
1413
	/* More than one level of templates */
1414
	report ( decl_loc, ERR_temp_decl_bad () ) ;
1415
    }
1416
    return ( t ) ;
1417
}
1418
 
1419
 
1420
/*
1421
    CHECK FOR TEMPLATE DEFAULT ARGUMENTS
1422
 
1423
    This routine returns true if the template type t has default arguments.
1424
*/
1425
 
1426
int check_templ_dargs
1427
    PROTO_N ( ( t ) )
1428
    PROTO_T ( TYPE t )
1429
{
1430
    if ( IS_type_templ ( t ) ) {
1431
	TOKEN sort = DEREF_tok ( type_templ_sort ( t ) ) ;
1432
	LIST ( TOKEN ) dargs = DEREF_list ( tok_templ_dargs ( sort ) ) ;
1433
	while ( !IS_NULL_list ( dargs ) ) {
1434
	    TOKEN darg = DEREF_tok ( HEAD_list ( dargs ) ) ;
1435
	    if ( !IS_NULL_tok ( darg ) ) return ( 1 ) ;
1436
	    dargs = TAIL_list ( dargs ) ;
1437
	}
1438
    }
1439
    return ( 0 ) ;
1440
}
1441
 
1442
 
1443
/*
1444
    FIND AN UNDERLYING TEMPLATE
1445
 
1446
    This routine checks whether the identifier id results from the
1447
    application of a template.  If so it returns the underlying template.
1448
*/
1449
 
1450
IDENTIFIER find_template
1451
    PROTO_N ( ( id, force ) )
1452
    PROTO_T ( IDENTIFIER id X int force )
1453
{
1454
    if ( !IS_NULL_id ( id ) ) {
1455
	switch ( TAG_id ( id ) ) {
1456
	    case id_class_name_tag : {
1457
		/* Template classes */
1458
		CLASS_TYPE ct ;
1459
		int templ = 0 ;
1460
		TYPE t = DEREF_type ( id_class_name_defn ( id ) ) ;
1461
		while ( IS_type_templ ( t ) ) {
1462
		    t = DEREF_type ( type_templ_defn ( t ) ) ;
1463
		    templ = 1 ;
1464
		}
1465
		ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
1466
		t = DEREF_type ( ctype_form ( ct ) ) ;
1467
		if ( !IS_NULL_type ( t ) && IS_type_token ( t ) ) {
1468
		    IDENTIFIER tid = DEREF_id ( type_token_tok ( t ) ) ;
1469
		    if ( !IS_id_token ( tid ) ) return ( tid ) ;
1470
		}
1471
		if ( templ && force ) {
1472
		    /* Primary template class */
1473
		    return ( id ) ;
1474
		}
1475
		break ;
1476
	    }
1477
	    case id_function_tag :
1478
	    case id_mem_func_tag :
1479
	    case id_stat_mem_func_tag : {
1480
		/* Template functions */
1481
		TYPE t = DEREF_type ( id_function_etc_form ( id ) ) ;
1482
		if ( !IS_NULL_type ( t ) && IS_type_token ( t ) ) {
1483
		    IDENTIFIER tid = DEREF_id ( type_token_tok ( t ) ) ;
1484
		    if ( !IS_id_token ( tid ) ) return ( tid ) ;
1485
		}
1486
		if ( force ) {
1487
		    t = DEREF_type ( id_function_etc_type ( id ) ) ;
1488
		    if ( IS_type_templ ( t ) ) {
1489
			/* Primary template function */
1490
			return ( id ) ;
1491
		    }
1492
		}
1493
		break ;
1494
	    }
1495
	    case id_ambig_tag : {
1496
		/* Ambiguous identifiers */
1497
		LIST ( IDENTIFIER ) pids ;
1498
		pids = DEREF_list ( id_ambig_ids ( id ) ) ;
1499
		if ( !IS_NULL_list ( pids ) ) {
1500
		    IDENTIFIER pid = DEREF_id ( HEAD_list ( pids ) ) ;
1501
		    IDENTIFIER tid = find_template ( pid, force ) ;
1502
		    if ( !IS_NULL_id ( tid ) ) {
1503
			pids = TAIL_list ( pids ) ;
1504
			while ( !IS_NULL_list ( pids ) ) {
1505
			    IDENTIFIER sid ;
1506
			    pid = DEREF_id ( HEAD_list ( pids ) ) ;
1507
			    sid = find_template ( pid, force ) ;
1508
			    if ( !EQ_id ( sid, tid ) ) return ( NULL_id ) ;
1509
			    pids = TAIL_list ( pids ) ;
1510
			}
1511
			return ( tid ) ;
1512
		    }
1513
		}
1514
		break ;
1515
	    }
1516
	}
1517
    }
1518
    return ( NULL_id ) ;
1519
}
1520
 
1521
 
1522
/*
1523
    REDECLARE A TEMPLATE PARAMETER
1524
 
1525
    This routine checks the template parameter id for redeclarations.
1526
*/
1527
 
1528
static IDENTIFIER redecl_templ_param
1529
    PROTO_N ( ( id ) )
1530
    PROTO_T ( IDENTIFIER id )
1531
{
1532
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
1533
    MEMBER mem = search_member ( crt_namespace, nm, 1 ) ;
1534
    IDENTIFIER pid = DEREF_id ( member_id ( mem ) ) ;
1535
    if ( !IS_NULL_id ( pid ) ) {
1536
	/* Parameter already defined */
1537
	report ( crt_loc, ERR_temp_param_dup ( nm ) ) ;
1538
	nm = lookup_anon () ;
1539
	id = DEREF_id ( hashid_id ( nm ) ) ;
1540
    }
1541
    return ( id ) ;
1542
}
1543
 
1544
 
1545
/*
1546
    DECLARE A TEMPLATE TYPE PARAMETER
1547
 
1548
    This routine declares a template type parameter named id.
1549
*/
1550
 
1551
IDENTIFIER make_type_param
1552
    PROTO_N ( ( id ) )
1553
    PROTO_T ( IDENTIFIER id )
1554
{
1555
    TOKEN tok ;
1556
    MAKE_tok_type ( btype_template, NULL_type, tok ) ;
1557
    id = redecl_templ_param ( id ) ;
1558
    id = make_token_decl ( tok, 0, id, NULL_id ) ;
1559
    return ( id ) ;
1560
}
1561
 
1562
 
1563
/*
1564
    SET A DEFAULT TEMPLATE TYPE ARGUMENT
1565
 
1566
    This routine sets the default value for the template type parameter id
1567
    to be t.
1568
*/
1569
 
1570
void init_type_param
1571
    PROTO_N ( ( id, t ) )
1572
    PROTO_T ( IDENTIFIER id X TYPE t )
1573
{
1574
    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
1575
    COPY_dspec ( id_storage ( id ), ( ds & ~dspec_pure ) ) ;
1576
    IGNORE define_type_token ( id, t, 0 ) ;
1577
    COPY_dspec ( id_storage ( id ), ds ) ;
1578
    return ;
1579
}
1580
 
1581
 
1582
/*
1583
    DECLARE A TEMPLATE EXPRESSION PARAMETER
1584
 
1585
    This routine declares a template expression parameter named id of
1586
    type t.
1587
*/
1588
 
1589
IDENTIFIER make_exp_param
1590
    PROTO_N ( ( t, id ) )
1591
    PROTO_T ( TYPE t X IDENTIFIER id )
1592
{
1593
    TOKEN tok ;
1594
    t = rvalue_type ( t ) ;
1595
    MAKE_tok_exp ( t, 1, NULL_exp, tok ) ;
1596
    id = make_token_decl ( tok, 0, id, NULL_id ) ;
1597
    return ( id ) ;
1598
}
1599
 
1600
 
1601
/*
1602
    SET A DEFAULT TEMPLATE EXPRESSION ARGUMENT
1603
 
1604
    This routine sets the default value for the template expression
1605
    parameter id to be e.
1606
*/
1607
 
1608
void init_exp_param
1609
    PROTO_N ( ( id, e ) )
1610
    PROTO_T ( IDENTIFIER id X EXP e )
1611
{
1612
    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
1613
    COPY_dspec ( id_storage ( id ), ( ds & ~dspec_pure ) ) ;
1614
    IGNORE define_exp_token ( id, e, 1 ) ;
1615
    COPY_dspec ( id_storage ( id ), ds ) ;
1616
    return ;
1617
}
1618
 
1619
 
1620
/*
1621
    DECLARE A TEMPLATE TEMPLATE PARAMETER
1622
 
1623
    This routine declares a template template parameter named id of type t.
1624
*/
1625
 
1626
IDENTIFIER make_template_param
1627
    PROTO_N ( ( t, id ) )
1628
    PROTO_T ( TYPE t X IDENTIFIER id )
1629
{
1630
    TOKEN tok ;
1631
    MAKE_tok_class ( t, NULL_id, tok ) ;
1632
    id = redecl_templ_param ( id ) ;
1633
    id = make_token_decl ( tok, 0, id, NULL_id ) ;
1634
    return ( id ) ;
1635
}
1636
 
1637
 
1638
/*
1639
    SET A TEMPLATE TEMPLATE ARGUMENT
1640
 
1641
    This routine sets the value for the template template parameter id to
1642
    be tid.  This is used both to set a default argument value and to
1643
    define a template template parameter.
1644
*/
1645
 
1646
void init_template_param
1647
    PROTO_N ( ( id, tid ) )
1648
    PROTO_T ( IDENTIFIER id X IDENTIFIER tid )
1649
{
1650
    if ( !IS_NULL_id ( tid ) ) {
1651
	if ( IS_id_class_name_etc ( tid ) ) {
1652
	    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
1653
	    COPY_dspec ( id_storage ( id ), ( ds & ~dspec_pure ) ) ;
1654
	    IGNORE define_templ_token ( id, tid ) ;
1655
	    COPY_dspec ( id_storage ( id ), ds ) ;
1656
	} else {
1657
	    report ( crt_loc, ERR_temp_arg_templ_not ( id, tid ) ) ;
1658
	}
1659
    }
1660
    return ;
1661
}
1662
 
1663
 
1664
/*
1665
    LIST OF DUMMY TYPE PARAMETERS
1666
 
1667
    This list is used to store all the dummy type parameters created by
1668
    make_dummy_type to avoid duplicates.
1669
*/
1670
 
1671
static LIST ( IDENTIFIER ) dummy_types = NULL_list ( IDENTIFIER ) ;
1672
 
1673
 
1674
/*
1675
    CREATE A DUMMY TYPE PARAMETER
1676
 
1677
    This routine creates a dummy type parameter named id in the namespace
1678
    ns.  bt gives the token type kind.
1679
*/
1680
 
1681
static TYPE make_dummy_type
1682
    PROTO_N ( ( ns, id, bt, args ) )
1683
    PROTO_T ( NAMESPACE ns X IDENTIFIER id X BASE_TYPE bt X
1684
	      LIST ( TOKEN ) args )
1685
{
1686
    TYPE t ;
1687
    HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
1688
    LIST ( IDENTIFIER ) p = dummy_types ;
1689
    while ( !IS_NULL_list ( p ) ) {
1690
	IDENTIFIER pid = DEREF_id ( HEAD_list ( p ) ) ;
1691
	HASHID pnm = DEREF_hashid ( id_name ( pid ) ) ;
1692
	NAMESPACE pns = DEREF_nspace ( id_parent ( pid ) ) ;
1693
	if ( EQ_hashid ( nm, pnm ) && EQ_nspace ( ns, pns ) ) {
1694
	    TOKEN tok = DEREF_tok ( id_token_sort ( pid ) ) ;
1695
	    BASE_TYPE pt = DEREF_btype ( tok_type_kind ( tok ) ) ;
1696
	    if ( bt == pt ) {
1697
		id = pid ;
1698
		break ;
1699
	    }
1700
	}
1701
	p = TAIL_list ( p ) ;
1702
    }
1703
    if ( IS_NULL_list ( p ) ) {
1704
	/* Create new parameter */
1705
	TOKEN tok ;
1706
	DECL_SPEC ds = ( dspec_template | dspec_token | dspec_auto |
1707
			 dspec_pure | dspec_implicit ) ;
1708
	MAKE_tok_type ( bt, NULL_type, tok ) ;
1709
	MAKE_id_token ( nm, ds, ns, crt_loc, tok, NULL_id, id ) ;
1710
	COPY_id ( id_token_alt ( id ), id ) ;
1711
	CONS_id ( id, dummy_types, dummy_types ) ;
1712
    }
1713
    MAKE_type_token ( cv_none, id, args, t ) ;
1714
    return ( t ) ;
1715
}
1716
 
1717
 
1718
/*
1719
    DOES A TYPE REPRESENT A TEMPLATE SPECIALISATION?
1720
 
1721
    This routine checks whether the type t represents an explicit template
1722
    specialisation or instantiation.
1723
*/
1724
 
1725
int is_templ_spec
1726
    PROTO_N ( ( t ) )
1727
    PROTO_T ( TYPE t )
1728
{
1729
    while ( !IS_NULL_type ( t ) && IS_type_templ ( t ) ) {
1730
	LIST ( IDENTIFIER ) pids ;
1731
	TOKEN sort = DEREF_tok ( type_templ_sort ( t ) ) ;
1732
	pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
1733
	if ( IS_NULL_list ( pids ) ) return ( 1 ) ;
1734
	t = DEREF_type ( type_templ_defn ( t ) ) ;
1735
    }
1736
    return ( 0 ) ;
1737
}
1738
 
1739
 
1740
/*
1741
    IS A TYPE A TEMPLATE PARAMETER?
1742
 
1743
    This routine checks whether the type t represents a template parameter
1744
    and a template declaration is currently being processed.
1745
*/
1746
 
1747
int is_templ_type
1748
    PROTO_N ( ( t ) )
1749
    PROTO_T ( TYPE t )
1750
{
1751
    if ( !IS_NULL_type ( t ) && IS_type_token ( t ) ) {
1752
	IDENTIFIER id = DEREF_id ( type_token_tok ( t ) ) ;
1753
	if ( is_templ_param ( id ) ) return ( in_template_decl ) ;
1754
    }
1755
    return ( 0 ) ;
1756
}
1757
 
1758
 
1759
/*
1760
    DOES A TYPE DEPEND ON A TEMPLATE TYPE PARAMETER?
1761
 
1762
    This routine checks whether the type t is dependent on any template
1763
    parameter.
1764
*/
1765
 
1766
int is_templ_depend
1767
    PROTO_N ( ( t ) )
1768
    PROTO_T ( TYPE t )
1769
{
1770
    if ( in_template_decl ) {
1771
	/* Only need to check in a template declaration */
1772
	return ( depends_on ( t, any_templ_param ) ) ;
1773
    }
1774
    return ( 0 ) ;
1775
}
1776
 
1777
 
1778
/*
1779
    IS AN IDENTIFIER A TEMPLATE TYPE PARAMETER?
1780
 
1781
    This routine checks whether the token identifier id represents a
1782
    template type parameter.
1783
*/
1784
 
1785
int is_templ_param
1786
    PROTO_N ( ( id ) )
1787
    PROTO_T ( IDENTIFIER id )
1788
{
1789
    DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
1790
    if ( ( ds & dspec_template ) && ( ds & dspec_auto ) ) return ( 1 ) ;
1791
    return ( 0 ) ;
1792
}
1793
 
1794
 
1795
/*
1796
    IS AN IDENTIFIER AN ALIAS FOR A TEMPLATE TYPE PARAMETER?
1797
 
1798
    This routine checks whether the identifier id is the internal name
1799
    for a template type parameter.
1800
*/
1801
 
1802
int is_templ_alias
1803
    PROTO_N ( ( id ) )
1804
    PROTO_T ( IDENTIFIER id )
1805
{
1806
    unsigned tag = TAG_id ( id ) ;
1807
    if ( tag == id_type_alias_tag ) {
1808
	TYPE t = DEREF_type ( id_type_alias_defn ( id ) ) ;
1809
	if ( IS_type_token ( t ) ) {
1810
	    id = DEREF_id ( type_token_tok ( t ) ) ;
1811
	    tag = TAG_id ( id ) ;
1812
	}
1813
    } else if ( tag == id_token_tag ) {
1814
	id = DEREF_id ( id_token_alt ( id ) ) ;
1815
	tag = TAG_id ( id ) ;
1816
    }
1817
    if ( tag == id_token_tag && is_templ_param ( id ) ) {
1818
	DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
1819
	if ( !( ds & dspec_implicit ) ) return ( 1 ) ;
1820
    }
1821
    return ( 0 ) ;
1822
}
1823
 
1824
 
1825
/*
1826
    IS AN IDENTIFIER A TEMPLATE DECLARATOR?
1827
 
1828
    This routine checks whether the declarator id represents a template
1829
    instance.  If id is a function declaration then t gives the function
1830
    type.
1831
*/
1832
 
1833
int is_templ_decl
1834
    PROTO_N ( ( id, t ) )
1835
    PROTO_T ( IDENTIFIER id X TYPE t )
1836
{
1837
    if ( crt_templ_qualifier ) {
1838
	/* Declaration is a template-id */
1839
	IDENTIFIER tid = find_template ( id, 0 ) ;
1840
	if ( !IS_NULL_id ( tid ) ) return ( 1 ) ;
1841
    }
1842
    if ( !IS_NULL_type ( t ) && crt_id_qualifier != qual_none ) {
1843
	/* Function declarator is a qualified-id */
1844
	int eq = 0 ;
1845
	LIST ( IDENTIFIER ) pids = NULL_list ( IDENTIFIER ) ;
1846
	IDENTIFIER pid = resolve_func ( id, t, 1, 1, pids, &eq ) ;
1847
	if ( !IS_NULL_id ( pid ) ) {
1848
	    IDENTIFIER tid = find_template ( pid, 0 ) ;
1849
	    if ( !IS_NULL_id ( tid ) ) return ( 1 ) ;
1850
	}
1851
    }
1852
    return ( 0 ) ;
1853
}
1854
 
1855
 
1856
/*
1857
    IS A NAMESPACE A TEMPLATE CLASS?
1858
 
1859
    This routine checks whether the namespace ns represents a template
1860
    class or a nested class of a template class or a block of a template
1861
    function.
1862
*/
1863
 
1864
int is_templ_nspace
1865
    PROTO_N ( ( ns ) )
1866
    PROTO_T ( NAMESPACE ns )
1867
{
1868
    while ( !IS_NULL_nspace ( ns ) ) {
1869
	IDENTIFIER tid ;
1870
	IDENTIFIER id = DEREF_id ( nspace_name ( ns ) ) ;
1871
	if ( IS_NULL_id ( id ) ) break ;
1872
	tid = find_template ( id, 1 ) ;
1873
	if ( !IS_NULL_id ( tid ) ) return ( 1 ) ;
1874
	ns = DEREF_nspace ( id_parent ( id ) ) ;
1875
    }
1876
    return ( 0 ) ;
1877
}
1878
 
1879
 
1880
/*
1881
    CHECK A TYPENAME
1882
 
1883
    This routine checks whether 'typename ns::id' can be used to declare
1884
    a type.  If so this type is returned, otherwise the null type is
1885
    returned.  Any following template arguments are dealt with in this
1886
    routine.
1887
*/
1888
 
1889
TYPE check_typename
1890
    PROTO_N ( ( ns, id, key ) )
1891
    PROTO_T ( NAMESPACE ns X IDENTIFIER id X BASE_TYPE key )
1892
{
1893
    TYPE s = NULL_type ;
1894
    if ( in_template_decl ) {
1895
	if ( !IS_NULL_nspace ( ns ) && IS_nspace_ctype ( ns ) ) {
1896
	    IDENTIFIER tid = DEREF_id ( nspace_name ( ns ) ) ;
1897
	    TYPE t = DEREF_type ( id_class_name_etc_defn ( tid ) ) ;
1898
	    while ( IS_type_templ ( t ) ) {
1899
		/* Step over any template qualifiers */
1900
		t = DEREF_type ( type_templ_defn ( t ) ) ;
1901
	    }
1902
	    if ( is_templ_depend ( t ) ) {
1903
		/* Qualifier depends on a template parameter */
1904
		int templ = 0 ;
1905
		LIST ( TOKEN ) args = NULL_list ( TOKEN ) ;
1906
		if ( crt_lex_token == lex_less ) {
1907
		    /* Step over template arguments */
1908
		    PPTOKEN *p = skip_template_args ( NULL_id, 1 ) ;
1909
		    args = parse_template_args ( p ) ;
1910
		    templ = 1 ;
1911
		}
1912
		if ( IS_id_class_name_etc ( id ) ) {
1913
		    if ( templ ) {
1914
			/* Apply template arguments */
1915
			id = apply_template ( id, args, 0, 0 ) ;
1916
		    }
1917
		    s = DEREF_type ( id_class_name_etc_defn ( id ) ) ;
1918
		    if ( IS_type_templ ( s ) ) {
1919
			s = deduce_type_template ( id, 1 ) ;
1920
		    }
1921
		    s = copy_typedef ( id, s, cv_none ) ;
1922
		    COPY_id ( type_name ( s ), id ) ;
1923
		    use_id ( id, 0 ) ;
1924
		} else {
1925
		    BASE_TYPE bt = ( btype_template | btype_typename ) ;
1926
		    if ( templ ) bt |= btype_args ;
1927
		    s = make_dummy_type ( ns, id, bt, args ) ;
1928
		    if ( key != btype_none ) {
1929
			/* Result should be a class */
1930
			id = DEREF_id ( type_token_tok ( s ) ) ;
1931
			args = NULL_list ( TOKEN ) ;
1932
			s = make_dummy_class ( id, args, key ) ;
1933
		    }
1934
		}
1935
	    }
1936
	}
1937
    }
1938
    return ( s ) ;
1939
}
1940
 
1941
 
1942
/*
1943
    DECLARE A TYPENAME
1944
 
1945
    This routine handles a type declared using typename.  ns gives the
1946
    name qualifiers used in the declaration and id gives the actual member
1947
    name.  Any following template arguments are dealt with in this
1948
    routine.
1949
*/
1950
 
1951
TYPE make_typename
1952
    PROTO_N ( ( ns, id ) )
1953
    PROTO_T ( NAMESPACE ns X IDENTIFIER id )
1954
{
1955
    TYPE s = check_typename ( ns, id, btype_none ) ;
1956
    if ( IS_NULL_type ( s ) ) {
1957
	int templ = 0 ;
1958
	LIST ( TOKEN ) args = NULL_list ( TOKEN ) ;
1959
	report ( crt_loc, ERR_temp_res_qual () ) ;
1960
	if ( crt_lex_token == lex_less ) {
1961
	    /* Step over template arguments */
1962
	    PPTOKEN *p = skip_template_args ( NULL_id, 1 ) ;
1963
	    args = parse_template_args ( p ) ;
1964
	    templ = 1 ;
1965
	}
1966
	if ( IS_id_class_name_etc ( id ) ) {
1967
	    /* Name denotes a type - return that */
1968
	    if ( templ ) {
1969
		/* Apply template arguments */
1970
		id = apply_template ( id, args, 0, 0 ) ;
1971
	    }
1972
	    s = DEREF_type ( id_class_name_etc_defn ( id ) ) ;
1973
	    if ( IS_type_templ ( s ) ) {
1974
		s = deduce_type_template ( id, 1 ) ;
1975
	    }
1976
	    s = copy_typedef ( id, s, cv_none ) ;
1977
	    COPY_id ( type_name ( s ), id ) ;
1978
	    use_id ( id, 0 ) ;
1979
	} else {
1980
	    /* Return the error type */
1981
	    s = type_error ;
1982
	}
1983
    }
1984
    return ( s ) ;
1985
}
1986
 
1987
 
1988
/*
1989
    LIST OF BAD TYPENAMES
1990
 
1991
    Without some action, an illegal typename can be reported many times.
1992
    A list of all bad typename look-ups is maintained so that the error is
1993
    only reported once.
1994
*/
1995
 
1996
static LIST ( IDENTIFIER ) non_typenames = NULL_list ( IDENTIFIER ) ;
1997
 
1998
 
1999
/*
2000
    FIND THE TYPE GIVEN BY A TYPENAME
2001
 
2002
    This routine expands the type name id.  If no expansion is possible
2003
    then the null type is returned.  type indicates whether the look-up
2004
    should be for a type name or an object name (the latter is used when
2005
    searching for a type previously declared using typename).
2006
*/
2007
 
2008
TYPE find_typename
2009
    PROTO_N ( ( id, args, bt, type ) )
2010
    PROTO_T ( IDENTIFIER id X LIST ( TOKEN ) args X BASE_TYPE bt X int type )
2011
{
2012
    TYPE t = NULL_type ;
2013
    NAMESPACE ns = DEREF_nspace ( id_parent ( id ) ) ;
2014
    NAMESPACE cns = rescan_nspace ( ns ) ;
2015
    if ( !EQ_nspace ( cns, ns ) ) {
2016
	/* Rescan type name */
2017
	LIST ( IDENTIFIER ) p ;
2018
	HASHID nm = DEREF_hashid ( id_name ( id ) ) ;
2019
	IDENTIFIER tid = search_field ( cns, nm, 0, type ) ;
2020
	if ( !IS_NULL_id ( tid ) && IS_id_class_name_etc ( tid ) ) {
2021
	    /* Type name */
2022
	    if ( bt & btype_args ) {
2023
		/* Apply template arguments */
2024
		tid = apply_template ( tid, args, 0, 0 ) ;
2025
	    }
2026
	    t = DEREF_type ( id_class_name_etc_defn ( tid ) ) ;
2027
	    if ( IS_type_templ ( t ) ) {
2028
		t = deduce_type_template ( tid, 1 ) ;
2029
	    }
2030
	    t = copy_typedef ( tid, t, cv_none ) ;
2031
	    COPY_id ( type_name ( t ), tid ) ;
2032
	    use_id ( tid, 0 ) ;
2033
	    return ( t ) ;
2034
	}
2035
 
2036
	/* Check for template parameters */
2037
	if ( in_template_decl ) {
2038
	    if ( !IS_NULL_nspace ( cns ) && IS_nspace_ctype ( cns ) ) {
2039
		tid = DEREF_id ( nspace_name ( cns ) ) ;
2040
		t = DEREF_type ( id_class_name_etc_defn ( tid ) ) ;
2041
		while ( IS_type_templ ( t ) ) {
2042
		    t = DEREF_type ( type_templ_defn ( t ) ) ;
2043
		}
2044
		if ( is_templ_depend ( t ) ) {
2045
		    t = make_dummy_type ( cns, id, bt, args ) ;
2046
		    return ( t ) ;
2047
		}
2048
	    }
2049
	}
2050
 
2051
	/* Report error */
2052
	p = non_typenames ;
2053
	t = type_error ;
2054
	while ( !IS_NULL_list ( p ) ) {
2055
	    IDENTIFIER pid = DEREF_id ( HEAD_list ( p ) ) ;
2056
	    HASHID pnm = DEREF_hashid ( id_name ( pid ) ) ;
2057
	    NAMESPACE pns = DEREF_nspace ( id_parent ( pid ) ) ;
2058
	    if ( EQ_hashid ( pnm, nm ) && EQ_nspace ( pns, cns ) ) {
2059
		/* Already reported */
2060
		break ;
2061
	    }
2062
	}
2063
	if ( IS_NULL_list ( p ) ) {
2064
	    /* Report undefined type */
2065
	    MAKE_id_type_alias ( nm, dspec_none, cns, crt_loc, t, tid ) ;
2066
	    CONS_id ( tid, non_typenames, non_typenames ) ;
2067
	    report ( crt_loc, ERR_temp_res_type ( cns, nm ) ) ;
2068
	}
2069
    }
2070
    return ( t ) ;
2071
}
2072
 
2073
 
2074
/*
2075
    IDENTIFY TWO LISTS OF TEMPLATE PARAMETERS
2076
 
2077
    This routine identifies the list of template parameters ps with those
2078
    in pt, returning true if this is possible.
2079
*/
2080
 
2081
int eq_templ_params
2082
    PROTO_N ( ( ps, pt ) )
2083
    PROTO_T ( LIST ( IDENTIFIER ) ps X LIST ( IDENTIFIER ) pt )
2084
{
2085
    int ok = 1 ;
2086
    while ( !IS_NULL_list ( ps ) && !IS_NULL_list ( pt ) ) {
2087
	IDENTIFIER is = DEREF_id ( HEAD_list ( ps ) ) ;
2088
	IDENTIFIER it = DEREF_id ( HEAD_list ( pt ) ) ;
2089
	if ( !EQ_id ( is, it ) ) {
2090
	    TOKEN ns, nt ;
2091
	    unsigned vs, vt ;
2092
	    if ( IS_NULL_id ( is ) ) return ( 0 ) ;
2093
	    if ( IS_NULL_id ( it ) ) return ( 0 ) ;
2094
	    ns = DEREF_tok ( id_token_sort ( is ) ) ;
2095
	    nt = DEREF_tok ( id_token_sort ( it ) ) ;
2096
	    vs = TAG_tok ( ns ) ;
2097
	    vt = TAG_tok ( nt ) ;
2098
	    if ( vs != vt ) {
2099
		/* Parameter sorts should be equal */
2100
		ok = 0 ;
2101
		break ;
2102
	    }
2103
	    if ( vs == tok_exp_tag ) {
2104
		/* Check expression parameter types */
2105
		TYPE rs = DEREF_type ( tok_exp_type ( ns ) ) ;
2106
		TYPE rt = DEREF_type ( tok_exp_type ( nt ) ) ;
2107
		rs = expand_type ( rs, 2 ) ;
2108
		if ( eq_type ( rs, rt ) != 1 ) {
2109
		    ok = 0 ;
2110
		    break ;
2111
		}
2112
	    }
2113
	    if ( vs == tok_class_tag ) {
2114
		/* Check template class parameter types */
2115
		TYPE rs = DEREF_type ( tok_class_type ( ns ) ) ;
2116
		TYPE rt = DEREF_type ( tok_class_type ( nt ) ) ;
2117
		rs = expand_type ( rs, 2 ) ;
2118
		if ( eq_template ( rs, rt, 0, 1, 0 ) != 3 ) {
2119
		    ok = 0 ;
2120
		    break ;
2121
		}
2122
	    }
2123
	    it = DEREF_id ( id_alias ( it ) ) ;
2124
	    COPY_id ( id_alias ( is ), it ) ;
2125
	}
2126
	pt = TAIL_list ( pt ) ;
2127
	ps = TAIL_list ( ps ) ;
2128
    }
2129
    if ( !EQ_list ( ps, pt ) ) ok = 0 ;
2130
    return ( ok ) ;
2131
}
2132
 
2133
 
2134
/*
2135
    RESTORE A LIST OF TEMPLATE PARAMETERS
2136
 
2137
    This routine clears the aliases set up by eq_templ_param from the
2138
    list of template parameters ps.
2139
*/
2140
 
2141
void restore_templ_params
2142
    PROTO_N ( ( ps ) )
2143
    PROTO_T ( LIST ( IDENTIFIER ) ps )
2144
{
2145
    while ( !IS_NULL_list ( ps ) ) {
2146
	IDENTIFIER is = DEREF_id ( HEAD_list ( ps ) ) ;
2147
	COPY_id ( id_alias ( is ), is ) ;
2148
	ps = TAIL_list ( ps ) ;
2149
    }
2150
    return ;
2151
}
2152
 
2153
 
2154
/*
2155
    CHECK FOR TEMPLATE TYPE EQUALITY
2156
 
2157
    This routine checks whether the template types s and t are equal
2158
    under a simple renaming of template parameters.  If def is false
2159
    only the template parameters (and not the underlying type) are checked.
2160
    mq and rf are as in eq_func_type, as is the return value.
2161
*/
2162
 
2163
int eq_template
2164
    PROTO_N ( ( s, t, def, mq, rf ) )
2165
    PROTO_T ( TYPE s X TYPE t X int def X int mq X int rf )
2166
{
2167
    TOKEN as = DEREF_tok ( type_templ_sort ( s ) ) ;
2168
    TOKEN at = DEREF_tok ( type_templ_sort ( t ) ) ;
2169
    LIST ( IDENTIFIER ) ps = DEREF_list ( tok_templ_pids ( as ) ) ;
2170
    LIST ( IDENTIFIER ) pt = DEREF_list ( tok_templ_pids ( at ) ) ;
2171
    int eq = eq_templ_params ( ps, pt ) ;
2172
    if ( eq && def ) {
2173
	/* Check for equality of definitions */
2174
	int ft = force_template ;
2175
	TYPE ds = DEREF_type ( type_templ_defn ( s ) ) ;
2176
	TYPE dt = DEREF_type ( type_templ_defn ( t ) ) ;
2177
	force_template = 0 ;
2178
	eq = eq_func_type ( ds, dt, mq, rf ) ;
2179
	force_template = ft ;
2180
    }
2181
    restore_templ_params ( ps ) ;
2182
    return ( eq ) ;
2183
}
2184
 
2185
 
2186
/*
2187
    RENAME TEMPLATE PARAMETERS IN A TYPE
2188
 
2189
    This routine renames the parameters in the given template sort,
2190
    returning the template type formed by applying this renaming to t.
2191
*/
2192
 
2193
static TYPE rename_templ_params
2194
    PROTO_N ( ( sort, t, rec ) )
2195
    PROTO_T ( TOKEN sort X TYPE t X int rec )
2196
{
2197
    if ( rec ) {
2198
	int d ;
2199
	LIST ( TOKEN ) args ;
2200
	LIST ( IDENTIFIER ) pids ;
2201
	LIST ( IDENTIFIER ) qids ;
2202
	pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
2203
	sort = expand_sort ( sort, 1, 1 ) ;
2204
	qids = DEREF_list ( tok_templ_pids ( sort ) ) ;
2205
	args = make_primary_args ( qids ) ;
2206
	d = save_token_args ( pids, args ) ;
2207
	t = expand_type ( t, 1 ) ;
2208
	restore_token_args ( pids, d ) ;
2209
    }
2210
    MAKE_type_templ ( cv_none, sort, t, 1, t ) ;
2211
    return ( t ) ;
2212
}
2213
 
2214
 
2215
/*
2216
    CHECK FOR TEMPLATE TYPE SPECIALISATION
2217
 
2218
    This routine checks whether the type t is a specialisation of the
2219
    template type s.  Type qualifiers are ignored if qu is false.
2220
*/
2221
 
2222
int deduce_template
2223
    PROTO_N ( ( s, t, qu ) )
2224
    PROTO_T ( TYPE s X TYPE t X int qu )
2225
{
2226
    int eq ;
2227
    TYPE r = DEREF_type ( type_templ_defn ( s ) ) ;
2228
    TOKEN sort = DEREF_tok ( type_templ_sort ( s ) ) ;
2229
    LIST ( IDENTIFIER ) pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
2230
    if ( in_template_decl && depends_on ( t, pids ) ) {
2231
	/* Rename parameters if necessary */
2232
	CV_SPEC cv = DEREF_cv ( type_qual ( s ) ) ;
2233
	s = rename_templ_params ( sort, r, 1 ) ;
2234
	COPY_cv ( type_qual ( s ), cv ) ;
2235
	eq = deduce_template ( s, t, qu ) ;
2236
    } else {
2237
	/* Perform argument deduction */
2238
	int d ;
2239
	force_template++ ;
2240
	d = save_token_args ( pids, NULL_list ( TOKEN ) ) ;
2241
	eq = eq_type_qual ( r, t, qu ) ;
2242
	if ( eq == 3 ) eq = 0 ;
2243
	restore_token_args ( pids, d ) ;
2244
	force_template-- ;
2245
    }
2246
    return ( eq ) ;
2247
}
2248
 
2249
 
2250
/*
2251
    REDECLARE A TEMPLATE TYPE
2252
 
2253
    This routine checks the redeclaration of the template id of type ps to
2254
    have type pt.  The primary purpose of this is to check for default
2255
    arguments in the redeclaration.  The non-template components are
2256
    returned via ps and pt.
2257
*/
2258
 
2259
void redecl_template
2260
    PROTO_N ( ( ps, pt, id ) )
2261
    PROTO_T ( TYPE *ps X TYPE *pt X IDENTIFIER id )
2262
{
2263
    TYPE s = *ps ;
2264
    TYPE t = *pt ;
2265
    while ( IS_type_templ ( s ) ) {
2266
	s = DEREF_type ( type_templ_defn ( s ) ) ;
2267
    }
2268
    while ( IS_type_templ ( t ) ) {
2269
	TOKEN sort = DEREF_tok ( type_templ_sort ( t ) ) ;
2270
	DECL_SPEC use = DEREF_dspec ( tok_templ_usage ( sort ) ) ;
2271
	if ( use & dspec_extern ) export_template ( id, 1 ) ;
2272
	if ( check_templ_dargs ( t ) ) {
2273
	    /* Can't have default arguments in redeclaration */
2274
	    report ( decl_loc, ERR_temp_param_redecl () ) ;
2275
	}
2276
	t = DEREF_type ( type_templ_defn ( t ) ) ;
2277
    }
2278
    *pt = t ;
2279
    *ps = s ;
2280
    return ;
2281
}
2282
 
2283
 
2284
/*
2285
    RESET THE PRIMARY FORM OF A TEMPLATE
2286
 
2287
    This routine changes the primary representation of a template from
2288
    s to t.  This is done when, for example, the latter is a definition
2289
    while the former is only a declaration.
2290
*/
2291
 
2292
void reset_primary_templ
2293
    PROTO_N ( ( s, t ) )
2294
    PROTO_T ( TYPE s X TYPE t )
2295
{
2296
    unsigned ns = TAG_type ( s ) ;
2297
    unsigned nt = TAG_type ( t ) ;
2298
    while ( ns == type_templ_tag && nt == type_templ_tag ) {
2299
	TOKEN as = DEREF_tok ( type_templ_sort ( s ) ) ;
2300
	TOKEN at = DEREF_tok ( type_templ_sort ( t ) ) ;
2301
	LIST ( IDENTIFIER ) ps = DEREF_list ( tok_templ_pids ( as ) ) ;
2302
	LIST ( IDENTIFIER ) pt = DEREF_list ( tok_templ_pids ( at ) ) ;
2303
	INSTANCE apps = DEREF_inst ( tok_templ_apps ( as ) ) ;
2304
	INSTANCE app = apps ;
2305
	LIST ( TOKEN ) dargs = DEREF_list ( tok_templ_dargs ( as ) ) ;
2306
	while ( !IS_NULL_inst ( app ) ) {
2307
	    DECL_SPEC ds = DEREF_dspec ( inst_templ_access ( app ) ) ;
2308
	    if ( ds & dspec_main ) {
2309
		/* Replace primary template instance */
2310
		TYPE form = DEREF_type ( inst_form ( app ) ) ;
2311
		LIST ( TOKEN ) args = make_primary_args ( pt ) ;
2312
		COPY_tok ( type_templ_sort ( form ), at ) ;
2313
		form = DEREF_type ( type_templ_defn ( form ) ) ;
2314
		COPY_list ( type_token_args ( form ), args ) ;
2315
	    }
2316
	    app = DEREF_inst ( inst_next ( app ) ) ;
2317
	}
2318
	if ( check_templ_dargs ( s ) ) {
2319
	    /* Expand default arguments */
2320
	    LIST ( TOKEN ) args = make_primary_args ( pt ) ;
2321
	    int d = save_token_args ( ps, args ) ;
2322
	    dargs = expand_args ( dargs, 1, 1 ) ;
2323
	    restore_token_args ( ps, d ) ;
2324
	}
2325
	COPY_list ( tok_templ_dargs ( at ), dargs ) ;
2326
	COPY_inst ( tok_templ_apps ( at ), apps ) ;
2327
	s = DEREF_type ( type_templ_defn ( s ) ) ;
2328
	t = DEREF_type ( type_templ_defn ( t ) ) ;
2329
	ns = TAG_type ( s ) ;
2330
	nt = TAG_type ( t ) ;
2331
    }
2332
    return ;
2333
}
2334
 
2335
 
2336
/*
2337
    IS AN IDENTIFIER A TEMPLATE PARAMETER?
2338
 
2339
    This routine checks whether the token identifier id is one of the
2340
    template or token parameters given by pids.
2341
*/
2342
 
2343
int depends_on_param
2344
    PROTO_N ( ( id, pids ) )
2345
    PROTO_T ( IDENTIFIER id X LIST ( IDENTIFIER ) pids )
2346
{
2347
    if ( IS_id_token ( id ) ) {
2348
	DECL_SPEC ds = DEREF_dspec ( id_storage ( id ) ) ;
2349
	if ( !( ds & dspec_ignore ) ) {
2350
	    if ( EQ_list ( pids, any_templ_param ) ) {
2351
		/* Short-cut for list of all template parameters */
2352
		if ( ( ds & dspec_template ) && ( ds & dspec_auto ) ) {
2353
		    return ( 1 ) ;
2354
		}
2355
		return ( 0 ) ;
2356
	    }
2357
	    if ( EQ_list ( pids, any_token_param ) ) {
2358
		/* Short-cut for list of all token parameters */
2359
		if ( ds & dspec_auto ) return ( 1 ) ;
2360
		return ( 0 ) ;
2361
	    }
2362
	    while ( !IS_NULL_list ( pids ) ) {
2363
		IDENTIFIER pid = DEREF_id ( HEAD_list ( pids ) ) ;
2364
		if ( EQ_id ( pid, id ) ) return ( 1 ) ;
2365
		pids = TAIL_list ( pids ) ;
2366
	    }
2367
	}
2368
    }
2369
    return ( 0 ) ;
2370
}
2371
 
2372
 
2373
/*
2374
    DOES AN IDENTIFIER DEPEND ON A TEMPLATE PARAMETER?
2375
 
2376
    This routine checks whether the identifier id is one of the template
2377
    parameters pids or is a template function with an argument depending
2378
    on pids.  If use is true then any other identifiers found are marked
2379
    as used.
2380
*/
2381
 
2382
static int depends_on_id
2383
    PROTO_N ( ( id, pids, use ) )
2384
    PROTO_T ( IDENTIFIER id X LIST ( IDENTIFIER ) pids X int use )
2385
{
2386
    if ( !IS_NULL_id ( id ) ) {
2387
	NAMESPACE ns ;
2388
	switch ( TAG_id ( id ) ) {
2389
	    case id_class_name_tag : {
2390
		/* Check for template classes */
2391
		TYPE form ;
2392
		CLASS_TYPE ct ;
2393
		TYPE t = DEREF_type ( id_class_name_defn ( id ) ) ;
2394
		while ( IS_type_templ ( t ) ) {
2395
		    t = DEREF_type ( type_templ_defn ( t ) ) ;
2396
		}
2397
		ct = DEREF_ctype ( type_compound_defn ( t ) ) ;
2398
		form = DEREF_type ( ctype_form ( ct ) ) ;
2399
		if ( !IS_NULL_type ( form ) ) {
2400
		    if ( depends_on ( form, pids ) ) return ( 1 ) ;
2401
		}
2402
		break ;
2403
	    }
2404
	    case id_function_tag :
2405
	    case id_mem_func_tag :
2406
	    case id_stat_mem_func_tag : {
2407
		/* Check for template functions */
2408
		TYPE form = DEREF_type ( id_function_etc_form ( id ) ) ;
2409
		if ( !IS_NULL_type ( form ) ) {
2410
		    /* Check function form */
2411
		    if ( depends_on ( form, pids ) ) return ( 1 ) ;
2412
		}
2413
		if ( use ) reuse_id ( id, 0 ) ;
2414
		break ;
2415
	    }
2416
	    case id_token_tag : {
2417
		/* Check for template parameters */
2418
		if ( depends_on_param ( id, pids ) ) return ( 1 ) ;
2419
		break ;
2420
	    }
2421
	    case id_ambig_tag : {
2422
		/* Check ambiguous identifiers */
2423
		LIST ( IDENTIFIER ) qids ;
2424
		qids = DEREF_list ( id_ambig_ids ( id ) ) ;
2425
		while ( !IS_NULL_list ( qids ) ) {
2426
		    IDENTIFIER qid = DEREF_id ( HEAD_list ( qids ) ) ;
2427
		    if ( depends_on_id ( qid, pids, use ) ) return ( 1 ) ;
2428
		    qids = TAIL_list ( qids ) ;
2429
		}
2430
		break ;
2431
	    }
2432
	    case id_stat_member_tag : {
2433
		/* Mark static data members */
2434
		if ( use ) reuse_id ( id, 0 ) ;
2435
		break ;
2436
	    }
2437
	}
2438
	ns = DEREF_nspace ( id_parent ( id ) ) ;
2439
	if ( !IS_NULL_nspace ( ns ) ) {
2440
	    /* Check enclosing namespace */
2441
	    IDENTIFIER cid = DEREF_id ( nspace_name ( ns ) ) ;
2442
	    return ( depends_on_id ( cid, pids, 0 ) ) ;
2443
	}
2444
    }
2445
    return ( 0 ) ;
2446
}
2447
 
2448
 
2449
/*
2450
    DOES A LIST OF TOKEN ARGUMENTS DEPEND ON A TEMPLATE PARAMETER?
2451
 
2452
    This routine checks whether the list of token arguments args depends
2453
    on one of the template parameters pids.  If next is true then the
2454
    algorithm is modified to check whether any token argument depends
2455
    on a later template parameter (e.g. does the first element of args
2456
    depend on the second, third, etc. element of pids).
2457
*/
2458
 
2459
int depends_on_args
2460
    PROTO_N ( ( args, pids, use, next ) )
2461
    PROTO_T ( LIST ( TOKEN ) args X LIST ( IDENTIFIER ) pids X
2462
	      int use X int next )
2463
{
2464
    while ( !IS_NULL_list ( args ) ) {
2465
	TOKEN tok = DEREF_tok ( HEAD_list ( args ) ) ;
2466
	if ( next ) {
2467
	    /* Move on to next parameter */
2468
	    if ( IS_NULL_list ( pids ) ) break ;
2469
	    pids = TAIL_list ( pids ) ;
2470
	}
2471
	if ( !IS_NULL_tok ( tok ) ) {
2472
	    switch ( TAG_tok ( tok ) ) {
2473
		case tok_exp_tag : {
2474
		    EXP e = DEREF_exp ( tok_exp_value ( tok ) ) ;
2475
		    if ( depends_on_exp ( e, pids, use ) ) return ( 1 ) ;
2476
		    break ;
2477
		}
2478
		case tok_stmt_tag : {
2479
		    EXP e = DEREF_exp ( tok_stmt_value ( tok ) ) ;
2480
		    if ( depends_on_exp ( e, pids, use ) ) return ( 1 ) ;
2481
		    break ;
2482
		}
2483
		case tok_nat_tag :
2484
		case tok_snat_tag : {
2485
		    NAT n = DEREF_nat ( tok_nat_etc_value ( tok ) ) ;
2486
		    if ( depends_on_nat ( n, pids, use ) ) return ( 1 ) ;
2487
		    break ;
2488
		}
2489
		case tok_type_tag : {
2490
		    TYPE t = DEREF_type ( tok_type_value ( tok ) ) ;
2491
		    if ( depends_on ( t, pids ) ) return ( 1 ) ;
2492
		    break ;
2493
		}
2494
		case tok_member_tag : {
2495
		    OFFSET off = DEREF_off ( tok_member_value ( tok ) ) ;
2496
		    if ( depends_on_off ( off, pids, use ) ) return ( 1 ) ;
2497
		    break ;
2498
		}
2499
		case tok_class_tag : {
2500
		    /* NOT YET IMPLEMENTED */
2501
		    break ;
2502
		}
2503
	    }
2504
	}
2505
	args = TAIL_list ( args ) ;
2506
    }
2507
    return ( 0 ) ;
2508
}
2509
 
2510
 
2511
/*
2512
    DOES AN INTEGRAL CONSTANT DEPEND ON A TEMPLATE PARAMETER?
2513
 
2514
    This routine checks whether the integral constant n depends on one
2515
    of the template parameters pids.
2516
*/
2517
 
2518
int depends_on_nat
2519
    PROTO_N ( ( n, pids, use ) )
2520
    PROTO_T ( NAT n X LIST ( IDENTIFIER ) pids X int use )
2521
{
2522
    if ( !IS_NULL_nat ( n ) ) {
2523
	switch ( TAG_nat ( n ) ) {
2524
	    case nat_calc_tag : {
2525
		EXP e = DEREF_exp ( nat_calc_value ( n ) ) ;
2526
		return ( depends_on_exp ( e, pids, use ) ) ;
2527
	    }
2528
	    case nat_token_tag : {
2529
		IDENTIFIER tid = DEREF_id ( nat_token_tok ( n ) ) ;
2530
		LIST ( TOKEN ) args = DEREF_list ( nat_token_args ( n ) ) ;
2531
		if ( depends_on_param ( tid, pids ) ) return ( 2 ) ;
2532
		if ( depends_on_args ( args, pids, use, 0 ) ) return ( 1 ) ;
2533
		break ;
2534
	    }
2535
	}
2536
    }
2537
    return ( 0 ) ;
2538
}
2539
 
2540
 
2541
/*
2542
    DOES A LIST OF EXPRESSIONS DEPEND ON A TEMPLATE PARAMETER?
2543
 
2544
    This routine checks whether the list of expressions p depends on one
2545
    of the template parameters pids.
2546
*/
2547
 
2548
static int depends_on_exp_list
2549
    PROTO_N ( ( p, pids, use ) )
2550
    PROTO_T ( LIST ( EXP ) p X LIST ( IDENTIFIER ) pids X int use )
2551
{
2552
    while ( !IS_NULL_list ( p ) ) {
2553
	EXP a = DEREF_exp ( HEAD_list ( p ) ) ;
2554
	if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2555
	p = TAIL_list ( p ) ;
2556
    }
2557
    return ( 0 ) ;
2558
}
2559
 
2560
 
2561
/*
2562
    DOES AN EXPRESSION DEPEND ON A TEMPLATE PARAMETER?
2563
 
2564
    This routine checks whether the expression e depends on one of the
2565
    template parameters pids.  If e is actually a template parameter
2566
    then 2 is returned.
2567
*/
2568
 
2569
int depends_on_exp
2570
    PROTO_N ( ( e, pids, use ) )
2571
    PROTO_T ( EXP e X LIST ( IDENTIFIER ) pids X int use )
2572
{
2573
    if ( !IS_NULL_exp ( e ) ) {
2574
	unsigned tag = TAG_exp ( e ) ;
2575
	TYPE t = DEREF_type ( exp_type ( e ) ) ;
2576
	if ( tag == exp_token_tag ) {
2577
	    /* Check for template parameters */
2578
	    IDENTIFIER tid = DEREF_id ( exp_token_tok ( e ) ) ;
2579
	    LIST ( TOKEN ) args = DEREF_list ( exp_token_args ( e ) ) ;
2580
	    if ( depends_on_param ( tid, pids ) ) return ( 2 ) ;
2581
	    if ( depends_on_args ( args, pids, use, 0 ) ) return ( 1 ) ;
2582
	}
2583
	if ( depends_on ( t, pids ) ) return ( 1 ) ;
2584
	ASSERT ( ORDER_exp == 88 ) ;
2585
	switch ( tag ) {
2586
	    case exp_identifier_tag :
2587
	    case exp_member_tag :
2588
	    case exp_ambiguous_tag :
2589
	    case exp_undeclared_tag : {
2590
		IDENTIFIER id = DEREF_id ( exp_identifier_etc_id ( e ) ) ;
2591
		if ( depends_on_id ( id, pids, use ) ) return ( 1 ) ;
2592
		break ;
2593
	    }
2594
	    case exp_int_lit_tag : {
2595
		NAT n = DEREF_nat ( exp_int_lit_nat ( e ) ) ;
2596
		return ( depends_on_nat ( n, pids, use ) ) ;
2597
	    }
2598
	    case exp_paren_tag :
2599
	    case exp_copy_tag : {
2600
		EXP a = DEREF_exp ( exp_paren_etc_arg ( e ) ) ;
2601
		return ( depends_on_exp ( a, pids, use ) ) ;
2602
	    }
2603
	    case exp_assign_tag : {
2604
		EXP a = DEREF_exp ( exp_assign_ref ( e ) ) ;
2605
		EXP b = DEREF_exp ( exp_assign_arg ( e ) ) ;
2606
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2607
		if ( depends_on_exp ( b, pids, use ) ) return ( 1 ) ;
2608
		break ;
2609
	    }
2610
	    case exp_init_tag : {
2611
		IDENTIFIER id = DEREF_id ( exp_init_id ( e ) ) ;
2612
		EXP a = DEREF_exp ( exp_init_arg ( e ) ) ;
2613
		if ( depends_on_id ( id, pids, use ) ) return ( 1 ) ;
2614
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2615
		break ;
2616
	    }
2617
	    case exp_preinc_tag : {
2618
		EXP a = DEREF_exp ( exp_preinc_op ( e ) ) ;
2619
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2620
		break ;
2621
	    }
2622
	    case exp_postinc_tag : {
2623
		EXP a = DEREF_exp ( exp_postinc_op ( e ) ) ;
2624
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2625
		break ;
2626
	    }
2627
	    case exp_indir_tag : {
2628
		EXP a = DEREF_exp ( exp_indir_ptr ( e ) ) ;
2629
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2630
		break ;
2631
	    }
2632
	    case exp_contents_tag : {
2633
		EXP a = DEREF_exp ( exp_contents_ptr ( e ) ) ;
2634
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2635
		break ;
2636
	    }
2637
	    case exp_address_tag : {
2638
		EXP a = DEREF_exp ( exp_address_arg ( e ) ) ;
2639
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2640
		break ;
2641
	    }
2642
	    case exp_address_mem_tag : {
2643
		EXP a = DEREF_exp ( exp_address_mem_arg ( e ) ) ;
2644
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2645
		break ;
2646
	    }
2647
	    case exp_func_tag : {
2648
		EXP a = DEREF_exp ( exp_func_fn ( e ) ) ;
2649
		LIST ( EXP ) p = DEREF_list ( exp_func_args ( e ) ) ;
2650
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2651
		if ( depends_on_exp_list ( p, pids, use ) ) return ( 1 ) ;
2652
		break ;
2653
	    }
2654
	    case exp_func_id_tag : {
2655
		IDENTIFIER id = DEREF_id ( exp_func_id_id ( e ) ) ;
2656
		LIST ( EXP ) p = DEREF_list ( exp_func_id_args ( e ) ) ;
2657
		if ( depends_on_id ( id, pids, use ) ) return ( 1 ) ;
2658
		if ( depends_on_exp_list ( p, pids, use ) ) return ( 1 ) ;
2659
		break ;
2660
	    }
2661
	    case exp_call_tag : {
2662
		EXP a = DEREF_exp ( exp_call_ptr ( e ) ) ;
2663
		EXP b = DEREF_exp ( exp_call_arg ( e ) ) ;
2664
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2665
		if ( depends_on_exp ( b, pids, use ) ) return ( 1 ) ;
2666
		break ;
2667
	    }
2668
	    case exp_negate_tag :
2669
	    case exp_compl_tag :
2670
	    case exp_not_tag :
2671
	    case exp_abs_tag : {
2672
		EXP a = DEREF_exp ( exp_negate_etc_arg ( e ) ) ;
2673
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2674
		break ;
2675
	    }
2676
	    case exp_plus_tag :
2677
	    case exp_minus_tag :
2678
	    case exp_mult_tag :
2679
	    case exp_div_tag :
2680
	    case exp_rem_tag :
2681
	    case exp_and_tag :
2682
	    case exp_or_tag :
2683
	    case exp_xor_tag :
2684
	    case exp_log_and_tag :
2685
	    case exp_log_or_tag :
2686
	    case exp_lshift_tag :
2687
	    case exp_rshift_tag :
2688
	    case exp_max_tag :
2689
	    case exp_min_tag : {
2690
		EXP a = DEREF_exp ( exp_plus_etc_arg1 ( e ) ) ;
2691
		EXP b = DEREF_exp ( exp_plus_etc_arg2 ( e ) ) ;
2692
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2693
		if ( depends_on_exp ( b, pids, use ) ) return ( 1 ) ;
2694
		break ;
2695
	    }
2696
	    case exp_test_tag : {
2697
		EXP a = DEREF_exp ( exp_test_arg ( e ) ) ;
2698
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2699
		break ;
2700
	    }
2701
	    case exp_compare_tag : {
2702
		EXP a = DEREF_exp ( exp_compare_arg1 ( e ) ) ;
2703
		EXP b = DEREF_exp ( exp_compare_arg2 ( e ) ) ;
2704
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2705
		if ( depends_on_exp ( b, pids, use ) ) return ( 1 ) ;
2706
		break ;
2707
	    }
2708
	    case exp_cast_tag : {
2709
		EXP a = DEREF_exp ( exp_cast_arg ( e ) ) ;
2710
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2711
		break ;
2712
	    }
2713
	    case exp_base_cast_tag : {
2714
		EXP a = DEREF_exp ( exp_base_cast_arg ( e ) ) ;
2715
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2716
		break ;
2717
	    }
2718
	    case exp_dyn_cast_tag : {
2719
		EXP a = DEREF_exp ( exp_dyn_cast_arg ( e ) ) ;
2720
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2721
		break ;
2722
	    }
2723
	    case exp_add_ptr_tag : {
2724
		EXP a = DEREF_exp ( exp_add_ptr_ptr ( e ) ) ;
2725
		OFFSET off = DEREF_off ( exp_add_ptr_off ( e ) ) ;
2726
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2727
		if ( depends_on_off ( off, pids, use ) ) return ( 1 ) ;
2728
		break ;
2729
	    }
2730
	    case exp_offset_size_tag : {
2731
		OFFSET off = DEREF_off ( exp_offset_size_off ( e ) ) ;
2732
		TYPE s = DEREF_type ( exp_offset_size_step ( e ) ) ;
2733
		if ( depends_on_off ( off, pids, use ) ) return ( 1 ) ;
2734
		if ( depends_on ( s, pids ) ) return ( 1 ) ;
2735
		break ;
2736
	    }
2737
	    case exp_constr_tag : {
2738
		EXP a = DEREF_exp ( exp_constr_call ( e ) ) ;
2739
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2740
		break ;
2741
	    }
2742
	    case exp_destr_tag : {
2743
		EXP a = DEREF_exp ( exp_destr_call ( e ) ) ;
2744
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2745
		break ;
2746
	    }
2747
	    case exp_alloc_tag : {
2748
		EXP a = DEREF_exp ( exp_alloc_call ( e ) ) ;
2749
		EXP b = DEREF_exp ( exp_alloc_init ( e ) ) ;
2750
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2751
		if ( depends_on_exp ( b, pids, use ) ) return ( 1 ) ;
2752
		break ;
2753
	    }
2754
	    case exp_dealloc_tag : {
2755
		EXP a = DEREF_exp ( exp_dealloc_term ( e ) ) ;
2756
		EXP b = DEREF_exp ( exp_dealloc_call ( e ) ) ;
2757
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2758
		if ( depends_on_exp ( b, pids, use ) ) return ( 1 ) ;
2759
		break ;
2760
	    }
2761
	    case exp_rtti_tag : {
2762
		EXP a = DEREF_exp ( exp_rtti_arg ( e ) ) ;
2763
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2764
		break ;
2765
	    }
2766
	    case exp_rtti_type_tag : {
2767
		TYPE s = DEREF_type ( exp_rtti_type_arg ( e ) ) ;
2768
		if ( depends_on ( s, pids ) ) return ( 1 ) ;
2769
		break ;
2770
	    }
2771
	    case exp_rtti_no_tag : {
2772
		TYPE s = DEREF_type ( exp_rtti_no_arg ( e ) ) ;
2773
		if ( depends_on ( s, pids ) ) return ( 1 ) ;
2774
		break ;
2775
	    }
2776
	    case exp_dynamic_tag : {
2777
		EXP a = DEREF_exp ( exp_dynamic_arg ( e ) ) ;
2778
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2779
		break ;
2780
	    }
2781
	    case exp_aggregate_tag : {
2782
		LIST ( EXP ) p = DEREF_list ( exp_aggregate_args ( e ) ) ;
2783
		if ( depends_on_exp_list ( p, pids, use ) ) return ( 1 ) ;
2784
		break ;
2785
	    }
2786
	    case exp_initialiser_tag : {
2787
		LIST ( EXP ) p = DEREF_list ( exp_initialiser_args ( e ) ) ;
2788
		if ( depends_on_exp_list ( p, pids, use ) ) return ( 1 ) ;
2789
		break ;
2790
	    }
2791
	    case exp_nof_tag : {
2792
		EXP a = DEREF_exp ( exp_nof_start ( e ) ) ;
2793
		EXP b = DEREF_exp ( exp_nof_pad ( e ) ) ;
2794
		EXP c = DEREF_exp ( exp_nof_end ( e ) ) ;
2795
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2796
		if ( depends_on_exp ( b, pids, use ) ) return ( 1 ) ;
2797
		if ( depends_on_exp ( c, pids, use ) ) return ( 1 ) ;
2798
		break ;
2799
	    }
2800
	    case exp_comma_tag : {
2801
		LIST ( EXP ) p = DEREF_list ( exp_comma_args ( e ) ) ;
2802
		if ( depends_on_exp_list ( p, pids, use ) ) return ( 1 ) ;
2803
		break ;
2804
	    }
2805
	    case exp_set_tag : {
2806
		EXP a = DEREF_exp ( exp_set_arg ( e ) ) ;
2807
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2808
		break ;
2809
	    }
2810
	    case exp_unused_tag : {
2811
		EXP a = DEREF_exp ( exp_unused_arg ( e ) ) ;
2812
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2813
		break ;
2814
	    }
2815
	    case exp_sequence_tag : {
2816
		LIST ( EXP ) p = DEREF_list ( exp_sequence_first ( e ) ) ;
2817
		if ( depends_on_exp_list ( p, pids, use ) ) return ( 1 ) ;
2818
		break ;
2819
	    }
2820
	    case exp_if_stmt_tag : {
2821
		EXP c = DEREF_exp ( exp_if_stmt_cond ( e ) ) ;
2822
		EXP a = DEREF_exp ( exp_if_stmt_true_code ( e ) ) ;
2823
		EXP b = DEREF_exp ( exp_if_stmt_false_code ( e ) ) ;
2824
		if ( depends_on_exp ( c, pids, use ) ) return ( 1 ) ;
2825
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2826
		if ( depends_on_exp ( b, pids, use ) ) return ( 1 ) ;
2827
		break ;
2828
	    }
2829
	    case exp_try_block_tag : {
2830
		EXP a = DEREF_exp ( exp_try_block_body ( e ) ) ;
2831
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2832
		break ;
2833
	    }
2834
	    case exp_exception_tag : {
2835
		EXP a = DEREF_exp ( exp_exception_arg ( e ) ) ;
2836
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2837
		break ;
2838
	    }
2839
	    case exp_op_tag : {
2840
		EXP a = DEREF_exp ( exp_op_arg1 ( e ) ) ;
2841
		EXP b = DEREF_exp ( exp_op_arg2 ( e ) ) ;
2842
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2843
		if ( depends_on_exp ( b, pids, use ) ) return ( 1 ) ;
2844
		break ;
2845
	    }
2846
	    case exp_opn_tag : {
2847
		LIST ( EXP ) p = DEREF_list ( exp_opn_args ( e ) ) ;
2848
		if ( depends_on_exp_list ( p, pids, use ) ) return ( 1 ) ;
2849
		break ;
2850
	    }
2851
	    case exp_location_tag : {
2852
		EXP a = DEREF_exp ( exp_location_arg ( e ) ) ;
2853
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2854
		break ;
2855
	    }
2856
	    case exp_dummy_tag : {
2857
		EXP a = DEREF_exp ( exp_dummy_value ( e ) ) ;
2858
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2859
		break ;
2860
	    }
2861
	}
2862
    }
2863
    return ( 0 ) ;
2864
}
2865
 
2866
 
2867
/*
2868
    DOES AN OFFSET DEPEND ON A TEMPLATE PARAMETER?
2869
 
2870
    This routine checks whether the offset off depends on one of the
2871
    template parameters pids.
2872
*/
2873
 
2874
int depends_on_off
2875
    PROTO_N ( ( off, pids, use ) )
2876
    PROTO_T ( OFFSET off X LIST ( IDENTIFIER ) pids X int use )
2877
{
2878
    if ( !IS_NULL_off ( off ) ) {
2879
	ASSERT ( ORDER_off == 13 ) ;
2880
	switch ( TAG_off ( off ) ) {
2881
	    case off_zero_tag : {
2882
		TYPE t = DEREF_type ( off_zero_type ( off ) ) ;
2883
		if ( depends_on ( t, pids ) ) return ( 1 ) ;
2884
		break ;
2885
	    }
2886
	    case off_type_tag : {
2887
		TYPE t = DEREF_type ( off_type_type ( off ) ) ;
2888
		if ( depends_on ( t, pids ) ) return ( 1 ) ;
2889
		break ;
2890
	    }
2891
	    case off_array_tag : {
2892
		TYPE t = DEREF_type ( off_array_type ( off ) ) ;
2893
		if ( depends_on ( t, pids ) ) return ( 1 ) ;
2894
		break ;
2895
	    }
2896
	    case off_extra_tag : {
2897
		TYPE t = DEREF_type ( off_extra_type ( off ) ) ;
2898
		if ( depends_on ( t, pids ) ) return ( 1 ) ;
2899
		break ;
2900
	    }
2901
#if 0
2902
	    case off_base_tag : {
2903
		GRAPH graph = DEREF_graph ( off_base_graph ( off ) ) ;
2904
		break ;
2905
	    }
2906
	    case off_deriv_tag : {
2907
		GRAPH graph = DEREF_graph ( off_deriv_graph ( off ) ) ;
2908
		OFFSET direct = DEREF_off ( off_deriv_direct ( off ) ) ;
2909
		OFFSET indirect = DEREF_off ( off_deriv_indirect ( off ) ) ;
2910
		break ;
2911
	    }
2912
	    case off_member_tag : {
2913
		IDENTIFIER id = DEREF_id ( off_member_id ( off ) ) ;
2914
		break ;
2915
	    }
2916
#endif
2917
	    case off_ptr_mem_tag : {
2918
		EXP a = DEREF_exp ( off_ptr_mem_arg ( off ) ) ;
2919
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2920
		break ;
2921
	    }
2922
	    case off_negate_tag : {
2923
		OFFSET a = DEREF_off ( off_negate_arg ( off ) ) ;
2924
		if ( depends_on_off ( a, pids, use ) ) return ( 1 ) ;
2925
		break ;
2926
	    }
2927
	    case off_plus_tag : {
2928
		OFFSET a = DEREF_off ( off_plus_arg1 ( off ) ) ;
2929
		OFFSET b = DEREF_off ( off_plus_arg2 ( off ) ) ;
2930
		if ( depends_on_off ( a, pids, use ) ) return ( 1 ) ;
2931
		if ( depends_on_off ( b, pids, use ) ) return ( 1 ) ;
2932
		break ;
2933
	    }
2934
	    case off_mult_tag : {
2935
		OFFSET a = DEREF_off ( off_mult_arg1 ( off ) ) ;
2936
		EXP b = DEREF_exp ( off_mult_arg2 ( off ) ) ;
2937
		if ( depends_on_off ( a, pids, use ) ) return ( 1 ) ;
2938
		if ( depends_on_exp ( b, pids, use ) ) return ( 1 ) ;
2939
		break ;
2940
	    }
2941
	    case off_ptr_diff_tag : {
2942
		EXP a = DEREF_exp ( off_ptr_diff_ptr1 ( off ) ) ;
2943
		EXP b = DEREF_exp ( off_ptr_diff_ptr2 ( off ) ) ;
2944
		if ( depends_on_exp ( a, pids, use ) ) return ( 1 ) ;
2945
		if ( depends_on_exp ( b, pids, use ) ) return ( 1 ) ;
2946
		break ;
2947
	    }
2948
	    case off_token_tag : {
2949
		IDENTIFIER tid = DEREF_id ( off_token_tok ( off ) ) ;
2950
		LIST ( TOKEN ) args = DEREF_list ( off_token_args ( off ) ) ;
2951
		if ( depends_on_param ( tid, pids ) ) return ( 2 ) ;
2952
		if ( depends_on_args ( args, pids, use, 0 ) ) return ( 1 ) ;
2953
		break ;
2954
	    }
2955
	}
2956
    }
2957
    return ( 0 ) ;
2958
}
2959
 
2960
 
2961
/*
2962
    DOES A TYPE DEPEND ON A TEMPLATE PARAMETER?
2963
 
2964
    This routine checks whether the type t depends on one of the template
2965
    parameters pids.
2966
*/
2967
 
2968
int depends_on
2969
    PROTO_N ( ( t, pids ) )
2970
    PROTO_T ( TYPE t X LIST ( IDENTIFIER ) pids )
2971
{
2972
    if ( !IS_NULL_type ( t ) ) {
2973
	ASSERT ( ORDER_type == 18 ) ;
2974
	switch ( TAG_type ( t ) ) {
2975
	    case type_ptr_tag :
2976
	    case type_ref_tag : {
2977
		TYPE s = DEREF_type ( type_ptr_etc_sub ( t ) ) ;
2978
		return ( depends_on ( s, pids ) ) ;
2979
	    }
2980
	    case type_ptr_mem_tag : {
2981
		TYPE s = DEREF_type ( type_ptr_mem_sub ( t ) ) ;
2982
		CLASS_TYPE cr = DEREF_ctype ( type_ptr_mem_of ( t ) ) ;
2983
		TYPE r = DEREF_type ( ctype_form ( cr ) ) ;
2984
		if ( depends_on ( s, pids ) ) return ( 1 ) ;
2985
		return ( depends_on ( r, pids ) ) ;
2986
	    }
2987
	    case type_func_tag : {
2988
		TYPE r = DEREF_type ( type_func_ret ( t ) ) ;
2989
		LIST ( TYPE ) p = DEREF_list ( type_func_mtypes ( t ) ) ;
2990
		if ( depends_on ( r, pids ) ) return ( 1 ) ;
2991
		while ( !IS_NULL_list ( p ) ) {
2992
		    TYPE s = DEREF_type ( HEAD_list ( p ) ) ;
2993
		    if ( depends_on ( s, pids ) ) return ( 1 ) ;
2994
		    p = TAIL_list ( p ) ;
2995
		}
2996
		break ;
2997
	    }
2998
	    case type_array_tag : {
2999
		TYPE s = DEREF_type ( type_array_sub ( t ) ) ;
3000
		NAT n = DEREF_nat ( type_array_size ( t ) ) ;
3001
		if ( depends_on ( s, pids ) ) return ( 1 ) ;
3002
		return ( depends_on_nat ( n, pids, 0 ) ) ;
3003
	    }
3004
	    case type_bitfield_tag : {
3005
		INT_TYPE it = DEREF_itype ( type_bitfield_defn ( t ) ) ;
3006
		TYPE s = DEREF_type ( itype_bitfield_sub ( it ) ) ;
3007
		NAT n = DEREF_nat ( itype_bitfield_size ( it ) ) ;
3008
		if ( depends_on ( s, pids ) ) return ( 1 ) ;
3009
		return ( depends_on_nat ( n, pids, 0 ) ) ;
3010
	    }
3011
	    case type_compound_tag : {
3012
		CLASS_TYPE cs = DEREF_ctype ( type_compound_defn ( t ) ) ;
3013
		IDENTIFIER cid = DEREF_id ( ctype_name ( cs ) ) ;
3014
		return ( depends_on_id ( cid, pids, 0 ) ) ;
3015
	    }
3016
	    case type_enumerate_tag : {
3017
		ENUM_TYPE et = DEREF_etype ( type_enumerate_defn ( t ) ) ;
3018
		IDENTIFIER eid = DEREF_id ( etype_name ( et ) ) ;
3019
		return ( depends_on_id ( eid, pids, 0 ) ) ;
3020
	    }
3021
	    case type_token_tag : {
3022
		IDENTIFIER tid = DEREF_id ( type_token_tok ( t ) ) ;
3023
		LIST ( TOKEN ) args = DEREF_list ( type_token_args ( t ) ) ;
3024
		if ( depends_on_param ( tid, pids ) ) return ( 1 ) ;
3025
		if ( depends_on_args ( args, pids, 0, 0 ) ) return ( 1 ) ;
3026
		if ( IS_id_token ( tid ) ) {
3027
		    TOKEN sort = DEREF_tok ( id_token_sort ( tid ) ) ;
3028
		    if ( IS_tok_type ( sort ) ) {
3029
			BASE_TYPE bt ;
3030
			bt = DEREF_btype ( tok_type_kind ( sort ) ) ;
3031
			if ( bt & btype_typename ) {
3032
			    /* Allow for typename */
3033
			    return ( depends_on_id ( tid, pids, 0 ) ) ;
3034
			}
3035
		    }
3036
		}
3037
		break ;
3038
	    }
3039
	    case type_templ_tag : {
3040
		int dep ;
3041
		LIST ( IDENTIFIER ) qids ;
3042
		TYPE s = DEREF_type ( type_templ_defn ( t ) ) ;
3043
		TOKEN sort = DEREF_tok ( type_templ_sort ( t ) ) ;
3044
		qids = DEREF_list ( tok_templ_pids ( sort ) ) ;
3045
		while ( !IS_NULL_list ( qids ) ) {
3046
		    /* Suppress template parameters */
3047
		    IDENTIFIER qid = DEREF_id ( HEAD_list ( qids ) ) ;
3048
		    DECL_SPEC ds = DEREF_dspec ( id_storage ( qid ) ) ;
3049
		    ds |= dspec_ignore ;
3050
		    COPY_dspec ( id_storage ( qid ), ds ) ;
3051
		    qids = TAIL_list ( qids ) ;
3052
		}
3053
		dep = depends_on ( s, pids ) ;
3054
		qids = DEREF_list ( tok_templ_pids ( sort ) ) ;
3055
		while ( !IS_NULL_list ( qids ) ) {
3056
		    /* Restore template parameters */
3057
		    IDENTIFIER qid = DEREF_id ( HEAD_list ( qids ) ) ;
3058
		    DECL_SPEC ds = DEREF_dspec ( id_storage ( qid ) ) ;
3059
		    ds &= ~dspec_ignore ;
3060
		    COPY_dspec ( id_storage ( qid ), ds ) ;
3061
		    qids = TAIL_list ( qids ) ;
3062
		}
3063
		return ( dep ) ;
3064
	    }
3065
	}
3066
    }
3067
    return ( 0 ) ;
3068
}
3069
 
3070
 
3071
/*
3072
    DOES A FUNCTION CALL DEPEND ON A TEMPLATE PARAMETER?
3073
 
3074
    This routine checks whether the function call 'id ( args )' depends
3075
    on a template parameter.
3076
*/
3077
 
3078
int dependent_call
3079
    PROTO_N ( ( id, args ) )
3080
    PROTO_T ( IDENTIFIER id X LIST ( EXP ) args )
3081
{
3082
    if ( in_template_decl ) {
3083
	/* Only check in a template declaration */
3084
	LIST ( IDENTIFIER ) pids = any_templ_param ;
3085
	if ( depends_on_id ( id, pids, 0 ) ) return ( 1 ) ;
3086
	if ( IS_id_function_etc ( id ) ) {
3087
	    while ( !IS_NULL_id ( id ) ) {
3088
		TYPE t = DEREF_type ( id_function_etc_type ( id ) ) ;
3089
		if ( depends_on ( t, pids ) ) return ( 1 ) ;
3090
		id = DEREF_id ( id_function_etc_over ( id ) ) ;
3091
	    }
3092
	}
3093
	while ( !IS_NULL_list ( args ) ) {
3094
	    EXP a = DEREF_exp ( HEAD_list ( args ) ) ;
3095
	    if ( !IS_NULL_exp ( a ) ) {
3096
		/* Check argument type */
3097
		TYPE t = DEREF_type ( exp_type ( a ) ) ;
3098
		if ( depends_on ( t, pids ) ) return ( 1 ) ;
3099
	    }
3100
	    args = TAIL_list ( args ) ;
3101
	}
3102
    }
3103
    return ( 0 ) ;
3104
}
3105
 
3106
 
3107
/*
3108
    DOES A FUNCTION CAST DEPEND ON A TEMPLATE PARAMETER?
3109
 
3110
    This routine checks whether the resolution of the overloaded function
3111
    id to the type t depends on a template parameter.
3112
*/
3113
 
3114
int dependent_cast
3115
    PROTO_N ( ( id, t ) )
3116
    PROTO_T ( IDENTIFIER id X TYPE t )
3117
{
3118
    if ( in_template_decl ) {
3119
	/* Only check in a template declaration */
3120
	LIST ( IDENTIFIER ) pids = any_templ_param ;
3121
	if ( depends_on_id ( id, pids, 0 ) ) return ( 1 ) ;
3122
	if ( depends_on ( t, pids ) ) return ( 1 ) ;
3123
    }
3124
    return ( 0 ) ;
3125
}
3126
 
3127
 
3128
/*
3129
    DOES A CONVERSION DEPEND ON A TEMPLATE PARAMETER?
3130
 
3131
    This routine checks whether the conversion 't ( args )' depends
3132
    on a template parameter.
3133
*/
3134
 
3135
int dependent_conv
3136
    PROTO_N ( ( t, args ) )
3137
    PROTO_T ( TYPE t X LIST ( EXP ) args )
3138
{
3139
    if ( in_template_decl ) {
3140
	/* Only check in a template declaration */
3141
	LIST ( IDENTIFIER ) pids = any_templ_param ;
3142
	if ( depends_on ( t, pids ) ) return ( 1 ) ;
3143
	while ( !IS_NULL_list ( args ) ) {
3144
	    EXP a = DEREF_exp ( HEAD_list ( args ) ) ;
3145
	    if ( !IS_NULL_exp ( a ) ) {
3146
		/* Check argument type */
3147
		TYPE s = DEREF_type ( exp_type ( a ) ) ;
3148
		if ( depends_on ( s, pids ) ) return ( 1 ) ;
3149
	    }
3150
	    args = TAIL_list ( args ) ;
3151
	}
3152
    }
3153
    return ( 0 ) ;
3154
}
3155
 
3156
 
3157
/*
3158
    DOES AN IDENTIFIER DEPEND ON A TEMPLATE PARAMETER?
3159
 
3160
    This routine checks whether the identifier id depends on a template
3161
    parameter.
3162
*/
3163
 
3164
int dependent_id
3165
    PROTO_N ( ( id ) )
3166
    PROTO_T ( IDENTIFIER id )
3167
{
3168
    if ( in_template_decl ) {
3169
	/* Only check in a template declaration */
3170
	LIST ( IDENTIFIER ) pids = any_templ_param ;
3171
	if ( depends_on_id ( id, pids, 0 ) ) return ( 1 ) ;
3172
    }
3173
    return ( 0 ) ;
3174
}
3175
 
3176
 
3177
/*
3178
    MARK THE IDENTIFIERS IN AN EXPRESSION AS USED
3179
 
3180
    This routine marks all the identifiers in the expression e as having
3181
    been used.  This routine is combined with the depends_on functions
3182
    only because they happen to give a convenient tree-walking skeleton.
3183
*/
3184
 
3185
void mark_used
3186
    PROTO_N ( ( e ) )
3187
    PROTO_T ( EXP e )
3188
{
3189
    if ( !suppress_usage ) {
3190
	IGNORE depends_on_exp ( e, NULL_list ( IDENTIFIER ), 1 ) ;
3191
    }
3192
    return ;
3193
}
3194
 
3195
 
3196
/*
3197
    FIND AN INJECTED TYPE
3198
 
3199
    This routine modifies the type t which is injected from a template
3200
    into an enclosing scope (for example, a friend of a template class)
3201
    by qualifying it by copies of any unbound template qualifiers.
3202
*/
3203
 
3204
TYPE injected_type
3205
    PROTO_N ( ( t, rec ) )
3206
    PROTO_T ( TYPE t X int rec )
3207
{
3208
    IDENTIFIER pid = NULL_id ;
3209
    LIST ( NAMESPACE ) lns = LIST_stack ( namespace_stack ) ;
3210
    while ( !IS_NULL_list ( lns ) ) {
3211
	NAMESPACE ns = DEREF_nspace ( HEAD_list ( lns ) ) ;
3212
	IDENTIFIER id = DEREF_id ( nspace_name ( ns ) ) ;
3213
	if ( !IS_NULL_id ( id ) ) {
3214
	    if ( !EQ_id ( id, pid ) ) {
3215
		TYPE s = NULL_type ;
3216
		switch ( TAG_id ( id ) ) {
3217
		    case id_class_name_tag :
3218
		    case id_class_alias_tag : {
3219
			s = DEREF_type ( id_class_name_etc_defn ( id ) ) ;
3220
			break ;
3221
		    }
3222
		    case id_function_tag :
3223
		    case id_mem_func_tag :
3224
		    case id_stat_mem_func_tag : {
3225
			s = DEREF_type ( id_function_etc_type ( id ) ) ;
3226
			break ;
3227
		    }
3228
		}
3229
		if ( !IS_NULL_type ( s ) && IS_type_templ ( s ) ) {
3230
		    LIST ( IDENTIFIER ) pids ;
3231
		    TOKEN sort = DEREF_tok ( type_templ_sort ( s ) ) ;
3232
		    pids = DEREF_list ( tok_templ_pids ( sort ) ) ;
3233
		    if ( depends_on ( t, pids ) ) {
3234
			t = rename_templ_params ( sort, t, rec ) ;
3235
		    }
3236
		}
3237
		pid = id ;
3238
	    }
3239
	}
3240
	lns = TAIL_list ( lns ) ;
3241
    }
3242
    return ( t ) ;
3243
}
3244
 
3245
 
3246
/*
3247
    DUMMY TEMPLATE PARAMETER TYPE
3248
 
3249
    This variable gives a dummy template parameter type which allows the
3250
    propagation of types dependent in some non-obvious fashion on some
3251
    template parameter.
3252
*/
3253
 
3254
TYPE type_templ_param ;
3255
 
3256
 
3257
/*
3258
    INITIALISE TEMPLATE ROUTINES
3259
 
3260
    This routine initialises the template routines.  In particular it
3261
    initialises the dummy template parameter type.
3262
*/
3263
 
3264
void init_templates
3265
    PROTO_Z ()
3266
{
3267
    string s = ustrlit ( "<type>" ) ;
3268
    unsigned long h = hash ( s ) ;
3269
    HASHID nm = lookup_name ( s, h, 0, lex_identifier ) ;
3270
    IDENTIFIER id = DEREF_id ( hashid_id ( nm ) ) ;
3271
    LIST ( TOKEN ) args = NULL_list ( TOKEN ) ;
3272
    TYPE t = make_dummy_type ( crt_namespace, id, btype_template, args ) ;
3273
    type_templ_param = t ;
3274
    CONS_id ( NULL_id, NULL_list ( IDENTIFIER ), any_templ_param ) ;
3275
    CONS_id ( NULL_id, NULL_list ( IDENTIFIER ), any_token_param ) ;
3276
    return ;
3277
}