Subversion Repositories tendra.SVN

Rev

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

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