Subversion Repositories tendra.SVN

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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