]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/pt.c
sparc.h (LEGITIMIZE_RELOAD_ADDRESS): Do not create (mem (lo_sum (...)) for TFmode...
[thirdparty/gcc.git] / gcc / cp / pt.c
CommitLineData
8d08fdba 1/* Handle parameterized types (templates) for GNU C++.
4c571114 2 Copyright (C) 1992, 93-97, 1998, 1999 Free Software Foundation, Inc.
8d08fdba 3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
fc378698 4 Rewritten by Jason Merrill (jason@cygnus.com).
8d08fdba
MS
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING. If not, write to
e9fa0c7c
RK
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
8d08fdba
MS
22
23/* Known bugs or deficiencies include:
e92cc029 24
e92cc029
MS
25 all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win". */
8d08fdba
MS
27
28#include "config.h"
8d052bc7 29#include "system.h"
8d08fdba
MS
30#include "obstack.h"
31
32#include "tree.h"
33#include "flags.h"
34#include "cp-tree.h"
35#include "decl.h"
36#include "parse.h"
f0e01782 37#include "lex.h"
e8abc66f 38#include "output.h"
a9aedbc2 39#include "defaults.h"
49c249e1 40#include "except.h"
54f92bfb 41#include "toplev.h"
49c249e1 42
050367a3
MM
43/* The type of functions taking a tree, and some additional data, and
44 returning an int. */
45typedef int (*tree_fn_t) PROTO((tree, void*));
46
8d08fdba 47extern struct obstack permanent_obstack;
8d08fdba
MS
48
49extern int lineno;
50extern char *input_filename;
51struct pending_inline *pending_template_expansions;
52
5566b478
MS
53tree current_template_parms;
54HOST_WIDE_INT processing_template_decl;
8d08fdba 55
5566b478
MS
56tree pending_templates;
57static tree *template_tail = &pending_templates;
58
73aad9b9
JM
59tree maybe_templates;
60static tree *maybe_template_tail = &maybe_templates;
61
5566b478 62int minimal_parse_mode;
75b0bbce 63
92eca640 64int processing_specialization;
75650646 65int processing_explicit_instantiation;
67ffc812 66int processing_template_parmlist;
386b8a85
JM
67static int template_header_count;
68
75650646
MM
69static tree saved_trees;
70
8d08fdba
MS
71#define obstack_chunk_alloc xmalloc
72#define obstack_chunk_free free
73
830bfa74
MM
74#define UNIFY_ALLOW_NONE 0
75#define UNIFY_ALLOW_MORE_CV_QUAL 1
76#define UNIFY_ALLOW_LESS_CV_QUAL 2
77#define UNIFY_ALLOW_DERIVED 4
161c12b0 78#define UNIFY_ALLOW_INTEGER 8
830bfa74 79
34016c81
JM
80static int unify PROTO((tree, tree, tree, tree, int, int*));
81static int resolve_overloaded_unification PROTO((tree, tree, tree, tree,
82 unification_kind_t, int,
83 int*));
e97e5263 84static int try_one_overload PROTO((tree, tree, tree, tree, tree,
34016c81 85 unification_kind_t, int, int*));
830bfa74 86static int unify PROTO((tree, tree, tree, tree, int, int*));
49c249e1
JM
87static void add_pending_template PROTO((tree));
88static int push_tinst_level PROTO((tree));
89static tree classtype_mangled_name PROTO((tree));
36a117a5 90static char *mangle_class_name_for_template PROTO((char *, tree, tree));
49c249e1 91static tree tsubst_expr_values PROTO((tree, tree));
49c249e1 92static int list_eq PROTO((tree, tree));
36a117a5 93static tree get_class_bindings PROTO((tree, tree, tree));
744fac59 94static tree coerce_template_parms PROTO((tree, tree, tree, int, int));
dbfe2124 95static void tsubst_enum PROTO((tree, tree, tree));
98c1c668 96static tree add_to_template_args PROTO((tree, tree));
36a117a5 97static tree add_outermost_template_args PROTO((tree, tree));
830bfa74
MM
98static void maybe_adjust_types_for_deduction PROTO((unification_kind_t, tree*,
99 tree*));
050367a3 100static int type_unification_real PROTO((tree, tree, tree, tree,
830bfa74 101 int, unification_kind_t, int, int*));
386b8a85 102static void note_template_header PROTO((int));
840b09b4 103static tree maybe_fold_nontype_arg PROTO((tree));
e1467ff2 104static tree convert_nontype_argument PROTO((tree, tree));
8b5b8b7c
MM
105static tree convert_template_argument PROTO ((tree, tree, tree, int,
106 int , tree));
76b9a14d 107static tree get_bindings_overload PROTO((tree, tree, tree));
050367a3 108static int for_each_template_parm PROTO((tree, tree_fn_t, void*));
4b580446 109static tree build_template_parm_index PROTO((int, int, int, tree, tree));
69ac77ce
JL
110static int inline_needs_template_parms PROTO((tree));
111static void push_inline_template_parms_recursive PROTO((tree, int));
112static tree retrieve_specialization PROTO((tree, tree));
36a117a5
MM
113static tree register_specialization PROTO((tree, tree, tree));
114static int unregister_specialization PROTO((tree, tree));
69ac77ce
JL
115static tree reduce_template_parm_level PROTO((tree, tree, int));
116static tree build_template_decl PROTO((tree, tree));
117static int mark_template_parm PROTO((tree, void *));
118static tree tsubst_friend_function PROTO((tree, tree));
6757edfe 119static tree tsubst_friend_class PROTO((tree, tree));
69ac77ce 120static tree get_bindings_real PROTO((tree, tree, tree, int));
04a81d94 121static int template_decl_level PROTO((tree));
a2b60a0e 122static tree maybe_get_template_decl_from_type_decl PROTO((tree));
830bfa74
MM
123static int check_cv_quals_for_unify PROTO((int, tree, tree));
124static tree tsubst_template_arg_vector PROTO((tree, tree));
36a117a5 125static tree tsubst_template_parms PROTO((tree, tree));
830bfa74 126static void regenerate_decl_from_template PROTO((tree, tree));
36a117a5
MM
127static tree most_specialized PROTO((tree, tree, tree));
128static tree most_specialized_class PROTO((tree, tree));
129static tree most_general_template PROTO((tree));
130static void set_mangled_name_for_template_decl PROTO((tree));
39c01e4c 131static int template_class_depth_real PROTO((tree, int));
ae58fa02
MM
132static tree tsubst_aggr_type PROTO((tree, tree, tree, int));
133static tree tsubst_decl PROTO((tree, tree, tree, tree));
cabc336a 134static tree tsubst_arg_types PROTO((tree, tree, tree));
6c30752f
MM
135static void check_specialization_scope PROTO((void));
136static tree process_partial_specialization PROTO((tree));
61a127b3 137static void set_current_access_from_decl PROTO((tree));
6ba89f8e 138static void check_default_tmpl_args PROTO((tree, tree, int, int));
36a117a5
MM
139
140/* We use TREE_VECs to hold template arguments. If there is only one
141 level of template arguments, then the TREE_VEC contains the
142 arguments directly. If there is more than one level of template
143 arguments, then each entry in the TREE_VEC is itself a TREE_VEC,
144 containing the template arguments for a single level. The first
145 entry in the outer TREE_VEC is the outermost level of template
146 parameters; the last is the innermost.
147
148 It is incorrect to ever form a template argument vector containing
149 only one level of arguments, but which is a TREE_VEC containing as
150 its only entry the TREE_VEC for that level. */
151
36a117a5
MM
152/* Non-zero if the template arguments is actually a vector of vectors,
153 rather than just a vector. */
154#define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \
155 (NODE != NULL_TREE \
156 && TREE_CODE (NODE) == TREE_VEC \
157 && TREE_VEC_LENGTH (NODE) > 0 \
158 && TREE_VEC_ELT (NODE, 0) != NULL_TREE \
67ffc812
MM
159 && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
160
e4a84209
MM
161/* The depth of a template argument vector. When called directly by
162 the parser, we use a TREE_LIST rather than a TREE_VEC to represent
163 template arguments. In fact, we may even see NULL_TREE if there
164 are no template arguments. In both of those cases, there is only
165 one level of template arguments. */
166#define TMPL_ARGS_DEPTH(NODE) \
167 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1)
168
36a117a5
MM
169/* The LEVELth level of the template ARGS. Note that template
170 parameter levels are indexed from 1, not from 0. */
171#define TMPL_ARGS_LEVEL(ARGS, LEVEL) \
172 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \
173 ? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS)
174
175/* Set the LEVELth level of the template ARGS to VAL. This macro does
176 not work with single-level argument vectors. */
177#define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \
178 (TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL))
179
180/* Accesses the IDXth parameter in the LEVELth level of the ARGS. */
181#define TMPL_ARG(ARGS, LEVEL, IDX) \
182 (TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX))
183
184/* Set the IDXth element in the LEVELth level of ARGS to VAL. This
185 macro does not work with single-level argument vectors. */
186#define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \
187 (TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL))
188
e4a84209
MM
189/* Given a single level of template arguments in NODE, return the
190 number of arguments. */
191#define NUM_TMPL_ARGS(NODE) \
192 ((NODE) == NULL_TREE ? 0 \
193 : (TREE_CODE (NODE) == TREE_VEC \
194 ? TREE_VEC_LENGTH (NODE) : list_length (NODE)))
195
36a117a5
MM
196/* The number of levels of template parameters given by NODE. */
197#define TMPL_PARMS_DEPTH(NODE) \
198 (TREE_INT_CST_HIGH (TREE_PURPOSE (NODE)))
199
e1467ff2
MM
200/* Do any processing required when DECL (a member template declaration
201 using TEMPLATE_PARAMETERS as its innermost parameter list) is
202 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
203 it is a specialization, in which case the DECL itself is returned. */
204
205tree
61a127b3 206finish_member_template_decl (decl)
e1467ff2
MM
207 tree decl;
208{
93cdc044
JM
209 if (decl == NULL_TREE || decl == void_type_node)
210 return NULL_TREE;
3ddfb0e6
MM
211 else if (decl == error_mark_node)
212 /* By returning NULL_TREE, the parser will just ignore this
213 declaration. We have already issued the error. */
214 return NULL_TREE;
93cdc044
JM
215 else if (TREE_CODE (decl) == TREE_LIST)
216 {
5f311aec 217 /* Assume that the class is the only declspec. */
93cdc044 218 decl = TREE_VALUE (decl);
8d019cef
JM
219 if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl)
220 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl))
93cdc044
JM
221 {
222 tree tmpl = CLASSTYPE_TI_TEMPLATE (decl);
223 check_member_template (tmpl);
224 return tmpl;
225 }
8d019cef 226 return NULL_TREE;
93cdc044 227 }
a1da6cba 228 else if (DECL_TEMPLATE_INFO (decl))
e1467ff2 229 {
a1da6cba
MM
230 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
231 {
232 check_member_template (DECL_TI_TEMPLATE (decl));
233 return DECL_TI_TEMPLATE (decl);
234 }
235 else
236 return decl;
237 }
238 else
8251199e 239 cp_error ("invalid member template declaration `%D'", decl);
e1467ff2 240
a1da6cba 241 return error_mark_node;
f84b4be9 242}
e1467ff2 243
f84b4be9
JM
244/* Returns the template nesting level of the indicated class TYPE.
245
246 For example, in:
247 template <class T>
248 struct A
249 {
250 template <class U>
251 struct B {};
252 };
253
39c01e4c
MM
254 A<T>::B<U> has depth two, while A<T> has depth one.
255 Both A<T>::B<int> and A<int>::B<U> have depth one, if
256 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
257 specializations.
258
259 This function is guaranteed to return 0 if passed NULL_TREE so
260 that, for example, `template_class_depth (current_class_type)' is
261 always safe. */
f84b4be9
JM
262
263int
39c01e4c 264template_class_depth_real (type, count_specializations)
f84b4be9 265 tree type;
39c01e4c 266 int count_specializations;
f84b4be9 267{
93cdc044 268 int depth;
f84b4be9 269
2c73f9f5 270 for (depth = 0;
ed44da02
MM
271 type && TREE_CODE (type) != NAMESPACE_DECL;
272 type = (TREE_CODE (type) == FUNCTION_DECL)
273 ? DECL_REAL_CONTEXT (type) : TYPE_CONTEXT (type))
274 {
275 if (TREE_CODE (type) != FUNCTION_DECL)
276 {
277 if (CLASSTYPE_TEMPLATE_INFO (type)
278 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
279 && ((count_specializations
280 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
281 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
282 ++depth;
283 }
284 else
285 {
286 if (DECL_TEMPLATE_INFO (type)
287 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
288 && ((count_specializations
289 && DECL_TEMPLATE_SPECIALIZATION (type))
290 || uses_template_parms (DECL_TI_ARGS (type))))
291 ++depth;
292 }
293 }
f84b4be9
JM
294
295 return depth;
e1467ff2 296}
98c1c668 297
39c01e4c
MM
298/* Returns the template nesting level of the indicated class TYPE.
299 Like template_class_depth_real, but instantiations do not count in
300 the depth. */
301
302int
303template_class_depth (type)
304 tree type;
305{
306 return template_class_depth_real (type, /*count_specializations=*/0);
307}
308
cae40af6
JM
309/* Returns 1 if processing DECL as part of do_pending_inlines
310 needs us to push template parms. */
311
312static int
313inline_needs_template_parms (decl)
314 tree decl;
315{
316 if (! DECL_TEMPLATE_INFO (decl))
317 return 0;
f84b4be9 318
36a117a5 319 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
cae40af6
JM
320 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
321}
322
323/* Subroutine of maybe_begin_member_template_processing.
324 Push the template parms in PARMS, starting from LEVELS steps into the
325 chain, and ending at the beginning, since template parms are listed
326 innermost first. */
327
328static void
329push_inline_template_parms_recursive (parmlist, levels)
330 tree parmlist;
331 int levels;
332{
333 tree parms = TREE_VALUE (parmlist);
334 int i;
335
336 if (levels > 1)
337 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
786b5245 338
98c1c668 339 ++processing_template_decl;
cae40af6 340 current_template_parms
98c1c668
JM
341 = tree_cons (build_int_2 (0, processing_template_decl),
342 parms, current_template_parms);
cae40af6
JM
343 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
344
786b5245 345 pushlevel (0);
98c1c668
JM
346 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
347 {
786b5245
MM
348 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
349 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0);
cae40af6 350
98c1c668
JM
351 switch (TREE_CODE (parm))
352 {
786b5245 353 case TYPE_DECL:
73b0fce8 354 case TEMPLATE_DECL:
98c1c668
JM
355 pushdecl (parm);
356 break;
786b5245
MM
357
358 case PARM_DECL:
359 {
fc03edb3
MM
360 /* Make a CONST_DECL as is done in process_template_parm.
361 It is ugly that we recreate this here; the original
362 version built in process_template_parm is no longer
363 available. */
786b5245
MM
364 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
365 TREE_TYPE (parm));
fc03edb3 366 SET_DECL_ARTIFICIAL (decl);
786b5245 367 DECL_INITIAL (decl) = DECL_INITIAL (parm);
fc03edb3 368 DECL_TEMPLATE_PARM_P (decl) = 1;
786b5245
MM
369 pushdecl (decl);
370 }
cae40af6 371 break;
786b5245 372
98c1c668
JM
373 default:
374 my_friendly_abort (0);
375 }
376 }
377}
378
cae40af6
JM
379/* Restore the template parameter context for a member template or
380 a friend template defined in a class definition. */
381
382void
383maybe_begin_member_template_processing (decl)
384 tree decl;
385{
386 tree parms;
387 int levels;
388
389 if (! inline_needs_template_parms (decl))
390 return;
391
36a117a5 392 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
cae40af6 393
36a117a5 394 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
cae40af6
JM
395
396 if (DECL_TEMPLATE_SPECIALIZATION (decl))
397 {
398 --levels;
399 parms = TREE_CHAIN (parms);
400 }
401
402 push_inline_template_parms_recursive (parms, levels);
403}
404
98c1c668
JM
405/* Undo the effects of begin_member_template_processing. */
406
407void
b370501f 408maybe_end_member_template_processing ()
98c1c668
JM
409{
410 if (! processing_template_decl)
411 return;
412
cae40af6
JM
413 while (current_template_parms
414 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
415 {
416 --processing_template_decl;
417 current_template_parms = TREE_CHAIN (current_template_parms);
418 poplevel (0, 0, 0);
419 }
98c1c668
JM
420}
421
cae40af6 422/* Returns non-zero iff T is a member template function. We must be
f84b4be9 423 careful as in
75650646
MM
424
425 template <class T> class C { void f(); }
426
427 Here, f is a template function, and a member, but not a member
428 template. This function does not concern itself with the origin of
429 T, only its present state. So if we have
430
431 template <class T> class C { template <class U> void f(U); }
432
433 then neither C<int>::f<char> nor C<T>::f<double> is considered
f181d4ae
MM
434 to be a member template. But, `template <class U> void
435 C<int>::f(U)' is considered a member template. */
98c1c668 436
cae40af6
JM
437int
438is_member_template (t)
98c1c668
JM
439 tree t;
440{
f181d4ae 441 if (!DECL_FUNCTION_TEMPLATE_P (t))
75650646 442 /* Anything that isn't a function or a template function is
aa5f3bad
MM
443 certainly not a member template. */
444 return 0;
445
cae40af6
JM
446 /* A local class can't have member templates. */
447 if (hack_decl_function_context (t))
448 return 0;
449
f181d4ae 450 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))
f84b4be9
JM
451 /* If there are more levels of template parameters than
452 there are template classes surrounding the declaration,
453 then we have a member template. */
f181d4ae
MM
454 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
455 template_class_depth (DECL_CLASS_CONTEXT (t))));
f84b4be9 456}
98c1c668 457
ba4f4e5d 458#if 0 /* UNUSED */
2ee4e084
MM
459/* Returns non-zero iff T is a member template class. See
460 is_member_template for a description of what precisely constitutes
461 a member template. */
462
463int
464is_member_template_class (t)
465 tree t;
466{
467 if (!DECL_CLASS_TEMPLATE_P (t))
468 /* Anything that isn't a class template, is certainly not a member
469 template. */
470 return 0;
471
472 if (!DECL_CLASS_SCOPE_P (t))
473 /* Anything whose context isn't a class type is surely not a
474 member template. */
475 return 0;
476
477 /* If there are more levels of template parameters than there are
478 template classes surrounding the declaration, then we have a
479 member template. */
36a117a5 480 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
2ee4e084
MM
481 template_class_depth (DECL_CONTEXT (t)));
482}
ba4f4e5d 483#endif
2ee4e084 484
36a117a5
MM
485/* Return a new template argument vector which contains all of ARGS,
486 but has as its innermost set of arguments the EXTRA_ARGS. The
487 resulting vector will be built on a temporary obstack, and so must
488 be explicitly copied to the permanent obstack, if required. */
e6f1275f
JM
489
490static tree
36a117a5
MM
491add_to_template_args (args, extra_args)
492 tree args;
493 tree extra_args;
e6f1275f 494{
36a117a5
MM
495 tree new_args;
496 int extra_depth;
497 int i;
498 int j;
e6f1275f 499
36a117a5
MM
500 extra_depth = TMPL_ARGS_DEPTH (extra_args);
501 new_args = make_temp_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
e6f1275f 502
36a117a5
MM
503 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
504 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
e6f1275f 505
36a117a5
MM
506 for (j = 1; j <= extra_depth; ++j, ++i)
507 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
508
e6f1275f
JM
509 return new_args;
510}
511
36a117a5
MM
512/* Like add_to_template_args, but only the outermost ARGS are added to
513 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
514 (EXTRA_ARGS) levels are added. This function is used to combine
515 the template arguments from a partial instantiation with the
516 template arguments used to attain the full instantiation from the
517 partial instantiation. */
98c1c668 518
4966381a 519static tree
36a117a5 520add_outermost_template_args (args, extra_args)
98c1c668
JM
521 tree args;
522 tree extra_args;
523{
524 tree new_args;
525
e4a84209
MM
526 /* If there are more levels of EXTRA_ARGS than there are ARGS,
527 something very fishy is going on. */
528 my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args),
529 0);
530
531 /* If *all* the new arguments will be the EXTRA_ARGS, just return
532 them. */
533 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
534 return extra_args;
535
36a117a5
MM
536 /* For the moment, we make ARGS look like it contains fewer levels. */
537 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
538
539 new_args = add_to_template_args (args, extra_args);
98c1c668 540
36a117a5
MM
541 /* Now, we restore ARGS to its full dimensions. */
542 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
98c1c668
JM
543
544 return new_args;
545}
5566b478
MS
546
547/* We've got a template header coming up; push to a new level for storing
548 the parms. */
8d08fdba 549
8d08fdba
MS
550void
551begin_template_parm_list ()
552{
6757edfe
MM
553 /* We use a non-tag-transparent scope here, which causes pushtag to
554 put tags in this scope, rather than in the enclosing class or
555 namespace scope. This is the right thing, since we want
556 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
557 global template class, push_template_decl handles putting the
558 TEMPLATE_DECL into top-level scope. For a nested template class,
559 e.g.:
560
561 template <class T> struct S1 {
562 template <class T> struct S2 {};
563 };
564
565 pushtag contains special code to call pushdecl_with_scope on the
566 TEMPLATE_DECL for S2. */
8d08fdba 567 pushlevel (0);
5566b478 568 declare_pseudo_global_level ();
5156628f 569 ++processing_template_decl;
67ffc812 570 ++processing_template_parmlist;
386b8a85
JM
571 note_template_header (0);
572}
573
6c30752f
MM
574/* This routine is called when a specialization is declared. If it is
575 illegal to declare a specialization here, an error is reported. */
576
577void
578check_specialization_scope ()
579{
580 tree scope = current_scope ();
3ddfb0e6 581
6c30752f
MM
582 /* [temp.expl.spec]
583
584 An explicit specialization shall be declared in the namespace of
585 which the template is a member, or, for member templates, in the
586 namespace of which the enclosing class or enclosing class
587 template is a member. An explicit specialization of a member
588 function, member class or static data member of a class template
589 shall be declared in the namespace of which the class template
590 is a member. */
591 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
8251199e 592 cp_error ("explicit specialization in non-namespace scope `%D'",
6c30752f 593 scope);
3ddfb0e6 594
6c30752f
MM
595 /* [temp.expl.spec]
596
597 In an explicit specialization declaration for a member of a class
598 template or a member template that appears in namespace scope,
599 the member template and some of its enclosing class templates may
600 remain unspecialized, except that the declaration shall not
601 explicitly specialize a class member template if its enclosing
602 class templates are not explicitly specialized as well. */
603 if (current_template_parms)
fd4de5ff 604 cp_error ("enclosing class templates are not explicitly specialized");
6c30752f
MM
605}
606
386b8a85
JM
607/* We've just seen template <>. */
608
609void
610begin_specialization ()
611{
612 note_template_header (1);
6c30752f 613 check_specialization_scope ();
386b8a85
JM
614}
615
386b8a85
JM
616/* Called at then end of processing a declaration preceeded by
617 template<>. */
618
619void
620end_specialization ()
621{
622 reset_specialization ();
623}
624
386b8a85
JM
625/* Any template <>'s that we have seen thus far are not referring to a
626 function specialization. */
627
628void
629reset_specialization ()
630{
631 processing_specialization = 0;
632 template_header_count = 0;
633}
634
386b8a85
JM
635/* We've just seen a template header. If SPECIALIZATION is non-zero,
636 it was of the form template <>. */
637
4966381a 638static void
386b8a85
JM
639note_template_header (specialization)
640 int specialization;
641{
642 processing_specialization = specialization;
643 template_header_count++;
644}
645
75650646 646/* We're beginning an explicit instantiation. */
386b8a85 647
75650646
MM
648void
649begin_explicit_instantiation ()
386b8a85 650{
75650646
MM
651 ++processing_explicit_instantiation;
652}
386b8a85 653
386b8a85 654
75650646
MM
655void
656end_explicit_instantiation ()
657{
658 my_friendly_assert(processing_explicit_instantiation > 0, 0);
659 --processing_explicit_instantiation;
660}
386b8a85 661
36a117a5
MM
662/* The TYPE is being declared. If it is a template type, that means it
663 is a partial specialization. Do appropriate error-checking. */
664
665void
666maybe_process_partial_specialization (type)
667 tree type;
668{
669 if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type))
670 {
671 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
672 && TYPE_SIZE (type) == NULL_TREE)
673 {
674 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
675 if (processing_template_decl)
676 push_template_decl (TYPE_MAIN_DECL (type));
677 }
678 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
8251199e 679 cp_error ("specialization of `%T' after instantiation", type);
36a117a5
MM
680 }
681}
682
75650646
MM
683/* Retrieve the specialization (in the sense of [temp.spec] - a
684 specialization is either an instantiation or an explicit
685 specialization) of TMPL for the given template ARGS. If there is
686 no such specialization, return NULL_TREE. The ARGS are a vector of
687 arguments, or a vector of vectors of arguments, in the case of
688 templates with more than one level of parameters. */
689
690static tree
691retrieve_specialization (tmpl, args)
692 tree tmpl;
693 tree args;
694{
695 tree s;
696
697 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
698
36a117a5
MM
699 /* There should be as many levels of arguments as there are
700 levels of parameters. */
701 my_friendly_assert (TMPL_ARGS_DEPTH (args)
702 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
703 0);
704
75650646
MM
705 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
706 s != NULL_TREE;
707 s = TREE_CHAIN (s))
708 if (comp_template_args (TREE_PURPOSE (s), args))
709 return TREE_VALUE (s);
710
711 return NULL_TREE;
386b8a85
JM
712}
713
6757edfe
MM
714/* Returns non-zero iff DECL is a specialization of TMPL. */
715
716int
717is_specialization_of (decl, tmpl)
718 tree decl;
719 tree tmpl;
720{
721 tree t;
722
723 if (TREE_CODE (decl) == FUNCTION_DECL)
724 {
725 for (t = decl;
726 t != NULL_TREE;
727 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
728 if (t == tmpl)
729 return 1;
730 }
731 else
732 {
733 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0);
734
735 for (t = TREE_TYPE (decl);
736 t != NULL_TREE;
737 t = CLASSTYPE_USE_TEMPLATE (t)
738 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
3bfdc719
MM
739 if (same_type_p (TYPE_MAIN_VARIANT (t),
740 TYPE_MAIN_VARIANT (TREE_TYPE (tmpl))))
6757edfe
MM
741 return 1;
742 }
386b8a85 743
6757edfe
MM
744 return 0;
745}
75650646
MM
746
747/* Register the specialization SPEC as a specialization of TMPL with
36a117a5
MM
748 the indicated ARGS. Returns SPEC, or an equivalent prior
749 declaration, if available. */
75650646 750
36a117a5 751static tree
75650646
MM
752register_specialization (spec, tmpl, args)
753 tree spec;
754 tree tmpl;
755 tree args;
756{
757 tree s;
758
759 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
760
36a117a5
MM
761 if (TREE_CODE (spec) == FUNCTION_DECL
762 && uses_template_parms (DECL_TI_ARGS (spec)))
763 /* This is the FUNCTION_DECL for a partial instantiation. Don't
764 register it; we want the corresponding TEMPLATE_DECL instead.
765 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
766 the more obvious `uses_template_parms (spec)' to avoid problems
767 with default function arguments. In particular, given
768 something like this:
769
770 template <class T> void f(T t1, T t = T())
771
772 the default argument expression is not substituted for in an
773 instantiation unless and until it is actually needed. */
774 return spec;
75650646 775
36a117a5
MM
776 /* There should be as many levels of arguments as there are
777 levels of parameters. */
778 my_friendly_assert (TMPL_ARGS_DEPTH (args)
779 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
780 0);
781
75650646
MM
782 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
783 s != NULL_TREE;
784 s = TREE_CHAIN (s))
785 if (comp_template_args (TREE_PURPOSE (s), args))
786 {
787 tree fn = TREE_VALUE (s);
788
789 if (DECL_TEMPLATE_SPECIALIZATION (spec))
790 {
791 if (DECL_TEMPLATE_INSTANTIATION (fn))
792 {
793 if (TREE_USED (fn)
794 || DECL_EXPLICIT_INSTANTIATION (fn))
795 {
8251199e 796 cp_error ("specialization of %D after instantiation",
75650646 797 fn);
36a117a5 798 return spec;
75650646
MM
799 }
800 else
801 {
802 /* This situation should occur only if the first
803 specialization is an implicit instantiation,
804 the second is an explicit specialization, and
805 the implicit instantiation has not yet been
806 used. That situation can occur if we have
36a117a5
MM
807 implicitly instantiated a member function and
808 then specialized it later.
809
810 We can also wind up here if a friend
811 declaration that looked like an instantiation
812 turns out to be a specialization:
813
814 template <class T> void foo(T);
815 class S { friend void foo<>(int) };
816 template <> void foo(int);
817
818 We transform the existing DECL in place so that
819 any pointers to it become pointers to the
fbf1c34b
MM
820 updated declaration.
821
822 If there was a definition for the template, but
823 not for the specialization, we want this to
824 look as if there is no definition, and vice
825 versa. */
826 DECL_INITIAL (fn) = NULL_TREE;
827 duplicate_decls (spec, fn);
828
829 return fn;
75650646
MM
830 }
831 }
832 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
833 {
fbf1c34b
MM
834 duplicate_decls (spec, fn);
835 return fn;
75650646
MM
836 }
837 }
838 }
839
840 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
841 = perm_tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
36a117a5
MM
842
843 return spec;
844}
845
846/* Unregister the specialization SPEC as a specialization of TMPL.
847 Returns nonzero if the SPEC was listed as a specialization of
848 TMPL. */
849
850static int
851unregister_specialization (spec, tmpl)
852 tree spec;
853 tree tmpl;
854{
855 tree* s;
856
857 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
858 *s != NULL_TREE;
859 s = &TREE_CHAIN (*s))
860 if (TREE_VALUE (*s) == spec)
861 {
862 *s = TREE_CHAIN (*s);
863 return 1;
864 }
865
866 return 0;
75650646
MM
867}
868
e1467ff2
MM
869/* Print the list of candidate FNS in an error message. */
870
104bf76a 871void
e1467ff2
MM
872print_candidates (fns)
873 tree fns;
874{
875 tree fn;
876
877 char* str = "candidates are:";
878
879 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
880 {
8251199e 881 cp_error_at ("%s %+#D", str, TREE_VALUE (fn));
e1467ff2
MM
882 str = " ";
883 }
884}
885
75650646 886/* Returns the template (one of the functions given by TEMPLATE_ID)
e1467ff2
MM
887 which can be specialized to match the indicated DECL with the
888 explicit template args given in TEMPLATE_ID. If
889 NEED_MEMBER_TEMPLATE is true the function is a specialization of a
890 member template. The template args (those explicitly specified and
891 those deduced) are output in a newly created vector *TARGS_OUT. If
892 it is impossible to determine the result, an error message is
893 issued, unless COMPLAIN is 0. The DECL may be NULL_TREE if none is
894 available. */
75650646 895
386b8a85 896tree
e1467ff2 897determine_specialization (template_id, decl, targs_out,
75650646
MM
898 need_member_template,
899 complain)
386b8a85 900 tree template_id;
e1467ff2 901 tree decl;
386b8a85
JM
902 tree* targs_out;
903 int need_member_template;
904 int complain;
905{
f2e48b67 906 tree fns, targs_in;
e1467ff2 907 tree templates = NULL_TREE;
386b8a85 908 tree fn;
386b8a85 909
e1467ff2
MM
910 *targs_out = NULL_TREE;
911
f2e48b67
BK
912 if (template_id == error_mark_node)
913 return error_mark_node;
914
915 fns = TREE_OPERAND (template_id, 0);
916 targs_in = TREE_OPERAND (template_id, 1);
917
27fafc8d
JM
918 if (fns == error_mark_node)
919 return error_mark_node;
920
2c73f9f5
ML
921 /* Check for baselinks. */
922 if (TREE_CODE (fns) == TREE_LIST)
923 fns = TREE_VALUE (fns);
386b8a85 924
2c73f9f5 925 for (; fns; fns = OVL_NEXT (fns))
386b8a85 926 {
75650646
MM
927 tree tmpl;
928
2c73f9f5 929 fn = OVL_CURRENT (fns);
75650646
MM
930 if (!need_member_template
931 && TREE_CODE (fn) == FUNCTION_DECL
e1467ff2 932 && DECL_FUNCTION_MEMBER_P (fn)
75650646
MM
933 && DECL_USE_TEMPLATE (fn)
934 && DECL_TI_TEMPLATE (fn))
e1467ff2
MM
935 /* We can get here when processing something like:
936 template <class T> class X { void f(); }
937 template <> void X<int>::f() {}
938 We're specializing a member function, but not a member
939 template. */
75650646
MM
940 tmpl = DECL_TI_TEMPLATE (fn);
941 else if (TREE_CODE (fn) != TEMPLATE_DECL
e1467ff2 942 || (need_member_template && !is_member_template (fn)))
798eed5e 943 continue;
75650646
MM
944 else
945 tmpl = fn;
386b8a85 946
75650646 947 if (list_length (targs_in) > DECL_NTPARMS (tmpl))
386b8a85
JM
948 continue;
949
e1467ff2 950 if (decl == NULL_TREE)
386b8a85 951 {
34016c81
JM
952 /* Unify against ourselves to make sure that the args we have
953 make sense and there aren't any undeducible parms. It's OK if
954 not all the parms are specified; they might be deduced
955 later. */
956 tree targs = get_bindings_overload (tmpl, DECL_RESULT (tmpl),
957 targs_in);
958
959 if (targs)
e1467ff2
MM
960 /* Unification was successful. */
961 templates = scratch_tree_cons (targs, tmpl, templates);
386b8a85 962 }
e1467ff2
MM
963 else
964 templates = scratch_tree_cons (NULL_TREE, tmpl, templates);
386b8a85 965 }
75650646 966
e1467ff2 967 if (decl != NULL_TREE)
386b8a85 968 {
e1467ff2 969 tree tmpl = most_specialized (templates, decl, targs_in);
36a117a5
MM
970 tree inner_args;
971 tree tmpl_args;
e1467ff2
MM
972
973 if (tmpl == error_mark_node)
974 goto ambiguous;
975 else if (tmpl == NULL_TREE)
976 goto no_match;
977
36a117a5
MM
978 inner_args = get_bindings (tmpl, decl, targs_in);
979 tmpl_args = DECL_TI_ARGS (DECL_RESULT (tmpl));
980 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (tmpl_args))
981 {
982 *targs_out = copy_node (tmpl_args);
983 SET_TMPL_ARGS_LEVEL (*targs_out,
984 TMPL_ARGS_DEPTH (*targs_out),
985 inner_args);
986 }
987 else
988 *targs_out = inner_args;
989
e1467ff2
MM
990 return tmpl;
991 }
992
993 if (templates == NULL_TREE)
994 {
995 no_match:
386b8a85 996 if (complain)
27fafc8d 997 {
8251199e 998 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
27fafc8d
JM
999 template_id, decl);
1000 return error_mark_node;
1001 }
386b8a85
JM
1002 return NULL_TREE;
1003 }
34016c81
JM
1004 else if (TREE_CHAIN (templates) != NULL_TREE
1005 || uses_template_parms (TREE_PURPOSE (templates)))
386b8a85 1006 {
e1467ff2 1007 ambiguous:
386b8a85
JM
1008 if (complain)
1009 {
8251199e 1010 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
27fafc8d 1011 template_id, decl);
e1467ff2 1012 print_candidates (templates);
27fafc8d 1013 return error_mark_node;
386b8a85 1014 }
386b8a85
JM
1015 return NULL_TREE;
1016 }
1017
1018 /* We have one, and exactly one, match. */
e1467ff2
MM
1019 *targs_out = TREE_PURPOSE (templates);
1020 return TREE_VALUE (templates);
8d08fdba 1021}
6757edfe 1022
386b8a85 1023/* Check to see if the function just declared, as indicated in
75650646
MM
1024 DECLARATOR, and in DECL, is a specialization of a function
1025 template. We may also discover that the declaration is an explicit
1026 instantiation at this point.
1027
e1467ff2
MM
1028 Returns DECL, or an equivalent declaration that should be used
1029 instead.
75650646 1030
75650646
MM
1031 FLAGS is a bitmask consisting of the following flags:
1032
75650646
MM
1033 2: The function has a definition.
1034 4: The function is a friend.
75650646
MM
1035
1036 The TEMPLATE_COUNT is the number of references to qualifying
1037 template classes that appeared in the name of the function. For
1038 example, in
1039
1040 template <class T> struct S { void f(); };
1041 void S<int>::f();
1042
1043 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1044 classes are not counted in the TEMPLATE_COUNT, so that in
1045
1046 template <class T> struct S {};
1047 template <> struct S<int> { void f(); }
36a117a5 1048 template <> void S<int>::f();
75650646
MM
1049
1050 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1051 illegal; there should be no template <>.)
1052
1053 If the function is a specialization, it is marked as such via
1054 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1055 is set up correctly, and it is added to the list of specializations
1056 for that template. */
386b8a85 1057
e1467ff2 1058tree
27bb8339 1059check_explicit_specialization (declarator, decl, template_count, flags)
386b8a85
JM
1060 tree declarator;
1061 tree decl;
1062 int template_count;
1063 int flags;
1064{
75650646
MM
1065 int have_def = flags & 2;
1066 int is_friend = flags & 4;
1067 int specialization = 0;
e1467ff2 1068 int explicit_instantiation = 0;
fd4de5ff 1069 int member_specialization = 0;
75650646
MM
1070
1071 tree ctype = DECL_CLASS_CONTEXT (decl);
1072 tree dname = DECL_NAME (decl);
386b8a85 1073
fd4de5ff 1074 if (processing_specialization)
386b8a85 1075 {
fd4de5ff 1076 /* The last template header was of the form template <>. */
75650646 1077
fd4de5ff
MM
1078 if (template_header_count > template_count)
1079 {
1080 /* There were more template headers than qualifying template
1081 classes. */
1082 if (template_header_count - template_count > 1)
1083 /* There shouldn't be that many template parameter
75650646
MM
1084 lists. There can be at most one parameter list for
1085 every qualifying class, plus one for the function
1086 itself. */
fd4de5ff 1087 cp_error ("too many template parameter lists in declaration of `%D'", decl);
75650646 1088
fd4de5ff
MM
1089 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1090 if (ctype)
1091 member_specialization = 1;
1092 else
1093 specialization = 1;
75650646 1094 }
fd4de5ff 1095 else if (template_header_count == template_count)
75650646 1096 {
fd4de5ff
MM
1097 /* The counts are equal. So, this might be a
1098 specialization, but it is not a specialization of a
1099 member template. It might be something like
1100
1101 template <class T> struct S {
1102 void f(int i);
1103 };
1104 template <>
1105 void S<int>::f(int i) {} */
1106 specialization = 1;
1107 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
75650646 1108 }
fd4de5ff 1109 else
386b8a85 1110 {
fd4de5ff
MM
1111 /* This cannot be an explicit specialization. There are not
1112 enough headers for all of the qualifying classes. For
1113 example, we might have:
1114
1115 template <>
1116 void S<int>::T<char>::f();
75650646 1117
fd4de5ff
MM
1118 But, we're missing another template <>. */
1119 cp_error("too few template parameter lists in declaration of `%D'", decl);
1120 return decl;
1121 }
1122 }
1123 else if (processing_explicit_instantiation)
1124 {
1125 if (template_header_count)
1126 cp_error ("template parameter list used in explicit instantiation");
1127
1128 if (have_def)
1129 cp_error ("definition provided for explicit instantiation");
75650646 1130
fd4de5ff
MM
1131 explicit_instantiation = 1;
1132 }
1133 else if (ctype != NULL_TREE
1134 && !TYPE_BEING_DEFINED (ctype)
1135 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)
1136 && !is_friend)
1137 {
1138 /* This case catches outdated code that looks like this:
1139
1140 template <class T> struct S { void f(); };
1141 void S<int>::f() {} // Missing template <>
1142
1143 We disable this check when the type is being defined to
1144 avoid complaining about default compiler-generated
1145 constructors, destructors, and assignment operators.
1146 Since the type is an instantiation, not a specialization,
1147 these are the only functions that can be defined before
1148 the class is complete. */
75650646 1149
c6f2ed0d
JM
1150 /* If they said
1151 template <class T> void S<int>::f() {}
1152 that's bogus. */
fd4de5ff
MM
1153 if (template_header_count)
1154 {
1155 cp_error ("template parameters specified in specialization");
1156 return decl;
c6f2ed0d 1157 }
fd4de5ff
MM
1158
1159 if (pedantic)
1160 cp_pedwarn
1161 ("explicit specialization not preceded by `template <>'");
1162 specialization = 1;
1163 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1164 }
1165 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1166 {
1167 if (is_friend)
1168 /* This could be something like:
1169
1170 template <class T> void f(T);
1171 class S { friend void f<>(int); } */
1172 specialization = 1;
1173 else
c6f2ed0d 1174 {
fd4de5ff
MM
1175 /* This case handles bogus declarations like template <>
1176 template <class T> void f<int>(); */
75650646 1177
fd4de5ff
MM
1178 cp_error ("template-id `%D' in declaration of primary template",
1179 declarator);
1180 return decl;
386b8a85 1181 }
75650646 1182 }
386b8a85 1183
670960ac
JM
1184 if (specialization || member_specialization)
1185 {
1186 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1187 for (; t; t = TREE_CHAIN (t))
1188 if (TREE_PURPOSE (t))
1189 {
8251199e
JM
1190 cp_pedwarn
1191 ("default argument specified in explicit specialization");
670960ac
JM
1192 break;
1193 }
f3400fe2
JM
1194 if (current_lang_name == lang_name_c)
1195 cp_error ("template specialization with C linkage");
670960ac
JM
1196 }
1197
e1467ff2 1198 if (specialization || member_specialization || explicit_instantiation)
75650646 1199 {
36a117a5 1200 tree gen_tmpl;
75650646
MM
1201 tree tmpl = NULL_TREE;
1202 tree targs = NULL_TREE;
75650646
MM
1203
1204 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
386b8a85
JM
1205 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1206 {
1207 tree fns;
1208
1209 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
1210 0);
1211 if (!ctype)
30394414 1212 fns = IDENTIFIER_NAMESPACE_VALUE (dname);
386b8a85
JM
1213 else
1214 fns = dname;
1215
75650646
MM
1216 declarator =
1217 lookup_template_function (fns, NULL_TREE);
386b8a85
JM
1218 }
1219
f2e48b67
BK
1220 if (declarator == error_mark_node)
1221 return error_mark_node;
1222
1223 if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
386b8a85
JM
1224 {
1225 /* A friend declaration. We can't do much, because we don't
1226 know what this resolves to, yet. */
1227 my_friendly_assert (is_friend != 0, 0);
e1467ff2 1228 my_friendly_assert (!explicit_instantiation, 0);
386b8a85 1229 SET_DECL_IMPLICIT_INSTANTIATION (decl);
e1467ff2 1230 return decl;
386b8a85
JM
1231 }
1232
75650646
MM
1233 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1234 {
8ec2ac34 1235 if (!explicit_instantiation)
6c30752f
MM
1236 /* A specialization in class scope. This is illegal,
1237 but the error will already have been flagged by
1238 check_specialization_scope. */
1239 return error_mark_node;
8ec2ac34 1240 else
b370501f
KG
1241 {
1242 /* It's not legal to write an explicit instantiation in
1243 class scope, e.g.:
8ec2ac34 1244
b370501f 1245 class C { template void f(); }
8ec2ac34 1246
b370501f
KG
1247 This case is caught by the parser. However, on
1248 something like:
8ec2ac34 1249
b370501f 1250 template class C { void f(); };
8ec2ac34 1251
b370501f
KG
1252 (which is illegal) we can get here. The error will be
1253 issued later. */
1254 ;
1255 }
8ec2ac34 1256
e1467ff2 1257 return decl;
75650646
MM
1258 }
1259 else if (ctype != NULL_TREE
1260 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1261 IDENTIFIER_NODE))
386b8a85 1262 {
75650646
MM
1263 /* Find the list of functions in ctype that have the same
1264 name as the declared function. */
1265 tree name = TREE_OPERAND (declarator, 0);
386b8a85 1266 tree fns;
75650646
MM
1267
1268 if (name == constructor_name (ctype)
1269 || name == constructor_name_full (ctype))
386b8a85 1270 {
75650646
MM
1271 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1272
1273 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1274 : !TYPE_HAS_DESTRUCTOR (ctype))
1275 {
1276 /* From [temp.expl.spec]:
e1467ff2 1277
75650646
MM
1278 If such an explicit specialization for the member
1279 of a class template names an implicitly-declared
1280 special member function (clause _special_), the
e1467ff2
MM
1281 program is ill-formed.
1282
1283 Similar language is found in [temp.explicit]. */
8251199e 1284 cp_error ("specialization of implicitly-declared special member function");
75650646 1285
e1467ff2 1286 return decl;
75650646 1287 }
386b8a85 1288
42da2fd8 1289 name = is_constructor ? ctor_identifier : dtor_identifier;
75650646 1290 }
42da2fd8
MM
1291
1292 fns = lookup_fnfields (TYPE_BINFO (ctype), name, 1);
386b8a85
JM
1293
1294 if (fns == NULL_TREE)
1295 {
8251199e 1296 cp_error ("no member function `%s' declared in `%T'",
75650646 1297 IDENTIFIER_POINTER (name),
386b8a85 1298 ctype);
e1467ff2 1299 return decl;
386b8a85
JM
1300 }
1301 else
1302 TREE_OPERAND (declarator, 0) = fns;
1303 }
75650646 1304
e1467ff2
MM
1305 /* Figure out what exactly is being specialized at this point.
1306 Note that for an explicit instantiation, even one for a
38e01259 1307 member function, we cannot tell apriori whether the
e1467ff2 1308 instantiation is for a member template, or just a member
36a117a5
MM
1309 function of a template class. Even if a member template is
1310 being instantiated, the member template arguments may be
1311 elided if they can be deduced from the rest of the
1312 declaration. */
e1467ff2
MM
1313 tmpl = determine_specialization (declarator, decl,
1314 &targs,
75650646
MM
1315 member_specialization,
1316 1);
386b8a85 1317
82a7448c 1318 if (tmpl && tmpl != error_mark_node)
386b8a85 1319 {
36a117a5
MM
1320 gen_tmpl = most_general_template (tmpl);
1321
e1467ff2
MM
1322 if (explicit_instantiation)
1323 {
03d0f4af
MM
1324 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1325 is done by do_decl_instantiation later. */
36a117a5 1326 decl = instantiate_template (tmpl, innermost_args (targs));
e1467ff2
MM
1327 return decl;
1328 }
36a117a5
MM
1329
1330 /* If we though that the DECL was a member function, but it
1331 turns out to be specializing a static member function,
1332 make DECL a static member function as well. */
1333 if (DECL_STATIC_FUNCTION_P (tmpl)
1334 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
366c0f1e
MM
1335 {
1336 revert_static_member_fn (&decl, 0, 0);
1337 last_function_parms = TREE_CHAIN (last_function_parms);
1338 }
e1467ff2 1339
36a117a5
MM
1340 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1341 DECL_TEMPLATE_INFO (decl)
1342 = perm_tree_cons (tmpl, targs, NULL_TREE);
1343
e1467ff2
MM
1344 /* Mangle the function name appropriately. Note that we do
1345 not mangle specializations of non-template member
1346 functions of template classes, e.g. with
36a117a5 1347
e1467ff2 1348 template <class T> struct S { void f(); }
36a117a5 1349
e1467ff2 1350 and given the specialization
36a117a5 1351
e1467ff2 1352 template <> void S<int>::f() {}
36a117a5 1353
e1467ff2
MM
1354 we do not mangle S<int>::f() here. That's because it's
1355 just an ordinary member function and doesn't need special
36a117a5
MM
1356 treatment. We do this here so that the ordinary,
1357 non-template, name-mangling algorith will not be used
1358 later. */
e1467ff2 1359 if ((is_member_template (tmpl) || ctype == NULL_TREE)
75650646 1360 && name_mangling_version >= 1)
36a117a5 1361 set_mangled_name_for_template_decl (decl);
386b8a85
JM
1362
1363 if (is_friend && !have_def)
36a117a5
MM
1364 /* This is not really a declaration of a specialization.
1365 It's just the name of an instantiation. But, it's not
1366 a request for an instantiation, either. */
fbf1c34b 1367 SET_DECL_IMPLICIT_INSTANTIATION (decl);
75650646 1368
36a117a5
MM
1369 /* Register this specialization so that we can find it
1370 again. */
1371 decl = register_specialization (decl, gen_tmpl, targs);
75650646 1372
e1467ff2 1373 return decl;
386b8a85
JM
1374 }
1375 }
75650646 1376
e1467ff2 1377 return decl;
386b8a85 1378}
75650646 1379
39c01e4c
MM
1380/* TYPE is being declared. Verify that the use of template headers
1381 and such is reasonable. Issue error messages if not. */
1382
1383void
1384maybe_check_template_type (type)
1385 tree type;
1386{
1387 if (template_header_count)
1388 {
1389 /* We are in the scope of some `template <...>' header. */
1390
1391 int context_depth
1392 = template_class_depth_real (TYPE_CONTEXT (type),
1393 /*count_specializations=*/1);
1394
1395 if (template_header_count <= context_depth)
1396 /* This is OK; the template headers are for the context. We
1397 are actually too lenient here; like
1398 check_explicit_specialization we should consider the number
1399 of template types included in the actual declaration. For
1400 example,
1401
1402 template <class T> struct S {
1403 template <class U> template <class V>
1404 struct I {};
1405 };
1406
1407 is illegal, but:
1408
1409 template <class T> struct S {
1410 template <class U> struct I;
1411 };
1412
1413 template <class T> template <class U.
1414 struct S<T>::I {};
1415
1416 is not. */
1417 ;
1418 else if (template_header_count > context_depth + 1)
1419 /* There are two many template parameter lists. */
8251199e 1420 cp_error ("too many template parameter lists in declaration of `%T'", type);
39c01e4c
MM
1421 }
1422}
1423
75650646
MM
1424/* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1425 parameters. These are represented in the same format used for
1426 DECL_TEMPLATE_PARMS. */
1427
1428int comp_template_parms (parms1, parms2)
1429 tree parms1;
1430 tree parms2;
1431{
1432 tree p1;
1433 tree p2;
1434
1435 if (parms1 == parms2)
1436 return 1;
1437
1438 for (p1 = parms1, p2 = parms2;
1439 p1 != NULL_TREE && p2 != NULL_TREE;
1440 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1441 {
1442 tree t1 = TREE_VALUE (p1);
1443 tree t2 = TREE_VALUE (p2);
1444 int i;
1445
1446 my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0);
1447 my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0);
1448
1449 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
1450 return 0;
1451
1452 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
1453 {
1454 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
1455 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
1456
1457 if (TREE_CODE (parm1) != TREE_CODE (parm2))
1458 return 0;
1459
1460 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
1461 continue;
3bfdc719 1462 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
75650646
MM
1463 return 0;
1464 }
1465 }
1466
1467 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
1468 /* One set of parameters has more parameters lists than the
1469 other. */
1470 return 0;
1471
1472 return 1;
1473}
1474
f3400fe2
JM
1475/* Complain if DECL shadows a template parameter.
1476
1477 [temp.local]: A template-parameter shall not be redeclared within its
1478 scope (including nested scopes). */
1479
1480void
1481check_template_shadow (decl)
1482 tree decl;
1483{
f181d4ae 1484 tree olddecl = IDENTIFIER_VALUE (DECL_NAME (decl));
f3400fe2 1485
f181d4ae
MM
1486 if (current_template_parms && olddecl)
1487 {
f3400fe2
JM
1488 /* We check for decl != olddecl to avoid bogus errors for using a
1489 name inside a class. We check TPFI to avoid duplicate errors for
1490 inline member templates. */
fc03edb3 1491 if (decl != olddecl && DECL_TEMPLATE_PARM_P (olddecl)
f3400fe2
JM
1492 && ! TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
1493 {
1494 cp_error_at ("declaration of `%#D'", decl);
1495 cp_error_at (" shadows template parm `%#D'", olddecl);
1496 }
1497 }
1498}
22a7be53 1499
f3400fe2 1500/* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
f84b4be9
JM
1501 ORIG_LEVEL, DECL, and TYPE. */
1502
1503static tree
1504build_template_parm_index (index, level, orig_level, decl, type)
1505 int index;
1506 int level;
1507 int orig_level;
1508 tree decl;
1509 tree type;
1510{
1511 tree t = make_node (TEMPLATE_PARM_INDEX);
1512 TEMPLATE_PARM_IDX (t) = index;
1513 TEMPLATE_PARM_LEVEL (t) = level;
1514 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
1515 TEMPLATE_PARM_DECL (t) = decl;
1516 TREE_TYPE (t) = type;
1517
1518 return t;
1519}
1520
f84b4be9 1521/* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
93cdc044 1522 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
f84b4be9
JM
1523 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1524 new one is created. */
1525
1526static tree
93cdc044 1527reduce_template_parm_level (index, type, levels)
f84b4be9
JM
1528 tree index;
1529 tree type;
93cdc044 1530 int levels;
f84b4be9
JM
1531{
1532 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
1533 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
93cdc044 1534 != TEMPLATE_PARM_LEVEL (index) - levels))
f84b4be9
JM
1535 {
1536 tree decl
1537 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)),
1538 DECL_NAME (TEMPLATE_PARM_DECL (index)),
1539 type);
1540 tree t
1541 = build_template_parm_index (TEMPLATE_PARM_IDX (index),
93cdc044 1542 TEMPLATE_PARM_LEVEL (index) - levels,
f84b4be9
JM
1543 TEMPLATE_PARM_ORIG_LEVEL (index),
1544 decl, type);
1545 TEMPLATE_PARM_DESCENDANTS (index) = t;
cae40af6
JM
1546
1547 /* Template template parameters need this. */
1548 DECL_TEMPLATE_PARMS (decl)
1549 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
f84b4be9
JM
1550 }
1551
1552 return TEMPLATE_PARM_DESCENDANTS (index);
1553}
1554
8d08fdba 1555/* Process information from new template parameter NEXT and append it to the
5566b478 1556 LIST being built. */
e92cc029 1557
8d08fdba
MS
1558tree
1559process_template_parm (list, next)
1560 tree list, next;
1561{
1562 tree parm;
1563 tree decl = 0;
a292b002 1564 tree defval;
5566b478 1565 int is_type, idx;
f84b4be9 1566
8d08fdba
MS
1567 parm = next;
1568 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
a292b002
MS
1569 defval = TREE_PURPOSE (parm);
1570 parm = TREE_VALUE (parm);
1571 is_type = TREE_PURPOSE (parm) == class_type_node;
5566b478
MS
1572
1573 if (list)
1574 {
1575 tree p = TREE_VALUE (tree_last (list));
1576
1577 if (TREE_CODE (p) == TYPE_DECL)
1578 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
73b0fce8
KL
1579 else if (TREE_CODE (p) == TEMPLATE_DECL)
1580 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p)));
5566b478 1581 else
f84b4be9 1582 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
5566b478
MS
1583 ++idx;
1584 }
1585 else
1586 idx = 0;
1587
8d08fdba
MS
1588 if (!is_type)
1589 {
a292b002 1590 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
8d08fdba 1591 /* is a const-param */
a292b002 1592 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
c11b6f21 1593 PARM, 0, NULL_TREE);
8d08fdba
MS
1594 /* A template parameter is not modifiable. */
1595 TREE_READONLY (parm) = 1;
c91a56d2 1596 if (IS_AGGR_TYPE (TREE_TYPE (parm))
410c2fba
KL
1597 && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM
1598 && TREE_CODE (TREE_TYPE (parm)) != TYPENAME_TYPE)
8d08fdba 1599 {
8251199e 1600 cp_error ("`%#T' is not a valid type for a template constant parameter",
c91a56d2
MS
1601 TREE_TYPE (parm));
1602 if (DECL_NAME (parm) == NULL_TREE)
8251199e 1603 error (" a template type parameter must begin with `class' or `typename'");
8d08fdba
MS
1604 TREE_TYPE (parm) = void_type_node;
1605 }
37c46b43
MS
1606 else if (pedantic
1607 && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE
1608 || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE))
8251199e 1609 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
eb66be0e 1610 TREE_TYPE (parm));
8d08fdba
MS
1611 if (TREE_PERMANENT (parm) == 0)
1612 {
1613 parm = copy_node (parm);
1614 TREE_PERMANENT (parm) = 1;
1615 }
8d08fdba 1616 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
f84b4be9
JM
1617 DECL_INITIAL (parm) = DECL_INITIAL (decl)
1618 = build_template_parm_index (idx, processing_template_decl,
1619 processing_template_decl,
1620 decl, TREE_TYPE (parm));
8d08fdba
MS
1621 }
1622 else
1623 {
73b0fce8
KL
1624 tree t;
1625 parm = TREE_VALUE (parm);
1626
1627 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
1628 {
1629 t = make_lang_type (TEMPLATE_TEMPLATE_PARM);
1630 /* This is for distinguishing between real templates and template
1631 template parameters */
1632 TREE_TYPE (parm) = t;
1633 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
1634 decl = parm;
1635 }
1636 else
1637 {
1638 t = make_lang_type (TEMPLATE_TYPE_PARM);
1639 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1640 decl = build_decl (TYPE_DECL, parm, t);
1641 }
1642
d2e5ee5c
MS
1643 TYPE_NAME (t) = decl;
1644 TYPE_STUB_DECL (t) = decl;
a292b002 1645 parm = decl;
f84b4be9
JM
1646 TEMPLATE_TYPE_PARM_INDEX (t)
1647 = build_template_parm_index (idx, processing_template_decl,
1648 processing_template_decl,
1649 decl, TREE_TYPE (parm));
8d08fdba 1650 }
8ccc31eb 1651 SET_DECL_ARTIFICIAL (decl);
fc03edb3 1652 DECL_TEMPLATE_PARM_P (decl) = 1;
8d08fdba 1653 pushdecl (decl);
a292b002 1654 parm = build_tree_list (defval, parm);
8d08fdba
MS
1655 return chainon (list, parm);
1656}
1657
1658/* The end of a template parameter list has been reached. Process the
1659 tree list into a parameter vector, converting each parameter into a more
1660 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1661 as PARM_DECLs. */
1662
1663tree
1664end_template_parm_list (parms)
1665 tree parms;
1666{
5566b478 1667 int nparms;
8d08fdba 1668 tree parm;
5566b478
MS
1669 tree saved_parmlist = make_tree_vec (list_length (parms));
1670
5566b478
MS
1671 current_template_parms
1672 = tree_cons (build_int_2 (0, processing_template_decl),
1673 saved_parmlist, current_template_parms);
8d08fdba
MS
1674
1675 for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
5566b478 1676 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
a292b002 1677
67ffc812
MM
1678 --processing_template_parmlist;
1679
8d08fdba
MS
1680 return saved_parmlist;
1681}
1682
5566b478
MS
1683/* end_template_decl is called after a template declaration is seen. */
1684
8d08fdba 1685void
5566b478 1686end_template_decl ()
8d08fdba 1687{
386b8a85
JM
1688 reset_specialization ();
1689
5156628f 1690 if (! processing_template_decl)
73aad9b9
JM
1691 return;
1692
5566b478
MS
1693 /* This matches the pushlevel in begin_template_parm_list. */
1694 poplevel (0, 0, 0);
8d08fdba 1695
5566b478
MS
1696 --processing_template_decl;
1697 current_template_parms = TREE_CHAIN (current_template_parms);
1698 (void) get_pending_sizes (); /* Why? */
1699}
8d08fdba 1700
36a117a5
MM
1701/* Given a template argument vector containing the template PARMS.
1702 The innermost PARMS are given first. */
9a3b49ac
MS
1703
1704tree
1705current_template_args ()
5566b478 1706{
36a117a5 1707 tree header;
b370501f 1708 tree args = NULL_TREE;
36a117a5 1709 int length = TMPL_PARMS_DEPTH (current_template_parms);
98c1c668
JM
1710 int l = length;
1711
36a117a5
MM
1712 /* If there is only one level of template parameters, we do not
1713 create a TREE_VEC of TREE_VECs. Instead, we return a single
1714 TREE_VEC containing the arguments. */
1715 if (length > 1)
1716 args = make_tree_vec (length);
1717
1718 for (header = current_template_parms; header; header = TREE_CHAIN (header))
8d08fdba 1719 {
5566b478 1720 tree a = copy_node (TREE_VALUE (header));
36a117a5
MM
1721 int i;
1722
5566b478 1723 TREE_TYPE (a) = NULL_TREE;
36a117a5 1724 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
5566b478 1725 {
98c1c668
JM
1726 tree t = TREE_VEC_ELT (a, i);
1727
36a117a5 1728 /* T will be a list if we are called from within a
98c1c668
JM
1729 begin/end_template_parm_list pair, but a vector directly
1730 if within a begin/end_member_template_processing pair. */
1731 if (TREE_CODE (t) == TREE_LIST)
1732 {
1733 t = TREE_VALUE (t);
1734
73b0fce8
KL
1735 if (TREE_CODE (t) == TYPE_DECL
1736 || TREE_CODE (t) == TEMPLATE_DECL)
98c1c668
JM
1737 t = TREE_TYPE (t);
1738 else
1739 t = DECL_INITIAL (t);
36a117a5 1740 TREE_VEC_ELT (a, i) = t;
98c1c668 1741 }
5566b478 1742 }
36a117a5
MM
1743
1744 if (length > 1)
1745 TREE_VEC_ELT (args, --l) = a;
1746 else
1747 args = a;
8d08fdba
MS
1748 }
1749
9a3b49ac
MS
1750 return args;
1751}
75650646 1752
e1467ff2
MM
1753/* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1754 template PARMS. Used by push_template_decl below. */
1755
75650646
MM
1756static tree
1757build_template_decl (decl, parms)
1758 tree decl;
1759 tree parms;
1760{
1761 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
1762 DECL_TEMPLATE_PARMS (tmpl) = parms;
1763 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
1764 if (DECL_LANG_SPECIFIC (decl))
1765 {
1766 DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl);
1767 DECL_STATIC_FUNCTION_P (tmpl) =
1768 DECL_STATIC_FUNCTION_P (decl);
1769 }
1770
1771 return tmpl;
1772}
1773
050367a3
MM
1774struct template_parm_data
1775{
6c30752f
MM
1776 /* The level of the template parameters we are currently
1777 processing. */
050367a3 1778 int level;
6c30752f
MM
1779
1780 /* The index of the specialization argument we are currently
1781 processing. */
1782 int current_arg;
1783
1784 /* An array whose size is the number of template parameters. The
1785 elements are non-zero if the parameter has been used in any one
1786 of the arguments processed so far. */
050367a3 1787 int* parms;
6c30752f
MM
1788
1789 /* An array whose size is the number of template arguments. The
1790 elements are non-zero if the argument makes use of template
1791 parameters of this level. */
1792 int* arg_uses_template_parms;
050367a3
MM
1793};
1794
1795/* Subroutine of push_template_decl used to see if each template
1796 parameter in a partial specialization is used in the explicit
1797 argument list. If T is of the LEVEL given in DATA (which is
1798 treated as a template_parm_data*), then DATA->PARMS is marked
1799 appropriately. */
1800
1801static int
1802mark_template_parm (t, data)
1803 tree t;
1804 void* data;
1805{
1806 int level;
1807 int idx;
1808 struct template_parm_data* tpd = (struct template_parm_data*) data;
1809
1810 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
1811 {
1812 level = TEMPLATE_PARM_LEVEL (t);
1813 idx = TEMPLATE_PARM_IDX (t);
1814 }
1815 else
1816 {
1817 level = TEMPLATE_TYPE_LEVEL (t);
1818 idx = TEMPLATE_TYPE_IDX (t);
1819 }
1820
1821 if (level == tpd->level)
6c30752f
MM
1822 {
1823 tpd->parms[idx] = 1;
1824 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
1825 }
050367a3
MM
1826
1827 /* Return zero so that for_each_template_parm will continue the
1828 traversal of the tree; we want to mark *every* template parm. */
1829 return 0;
1830}
1831
6c30752f
MM
1832/* Process the partial specialization DECL. */
1833
1834tree
1835process_partial_specialization (decl)
1836 tree decl;
1837{
1838 tree type = TREE_TYPE (decl);
1839 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
1840 tree specargs = CLASSTYPE_TI_ARGS (type);
1841 tree inner_args = innermost_args (specargs);
1842 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
1843 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
1844 int nargs = TREE_VEC_LENGTH (inner_args);
1845 int ntparms = TREE_VEC_LENGTH (inner_parms);
1846 int i;
1847 int did_error_intro = 0;
6c30752f
MM
1848 struct template_parm_data tpd;
1849 struct template_parm_data tpd2;
1850
6c30752f
MM
1851 /* We check that each of the template parameters given in the
1852 partial specialization is used in the argument list to the
1853 specialization. For example:
1854
1855 template <class T> struct S;
1856 template <class T> struct S<T*>;
1857
1858 The second declaration is OK because `T*' uses the template
1859 parameter T, whereas
1860
1861 template <class T> struct S<int>;
1862
1863 is no good. Even trickier is:
1864
1865 template <class T>
1866 struct S1
1867 {
1868 template <class U>
1869 struct S2;
1870 template <class U>
1871 struct S2<T>;
1872 };
1873
1874 The S2<T> declaration is actually illegal; it is a
1875 full-specialization. Of course,
1876
1877 template <class U>
1878 struct S2<T (*)(U)>;
1879
1880 or some such would have been OK. */
1881 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
1882 tpd.parms = alloca (sizeof (int) * ntparms);
1ddb2906 1883 bzero ((PTR) tpd.parms, sizeof (int) * ntparms);
6c30752f
MM
1884
1885 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
1ddb2906 1886 bzero ((PTR) tpd.arg_uses_template_parms, sizeof (int) * nargs);
6c30752f
MM
1887 for (i = 0; i < nargs; ++i)
1888 {
1889 tpd.current_arg = i;
1890 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
1891 &mark_template_parm,
1892 &tpd);
1893 }
1894 for (i = 0; i < ntparms; ++i)
1895 if (tpd.parms[i] == 0)
1896 {
1897 /* One of the template parms was not used in the
1898 specialization. */
1899 if (!did_error_intro)
1900 {
8251199e 1901 cp_error ("template parameters not used in partial specialization:");
6c30752f
MM
1902 did_error_intro = 1;
1903 }
1904
8251199e 1905 cp_error (" `%D'",
6c30752f
MM
1906 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
1907 }
1908
1909 /* [temp.class.spec]
1910
1911 The argument list of the specialization shall not be identical to
1912 the implicit argument list of the primary template. */
1913 if (comp_template_args (inner_args,
1914 innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE
1915 (maintmpl)))))
8251199e 1916 cp_error ("partial specialization `%T' does not specialize any template arguments", type);
6c30752f
MM
1917
1918 /* [temp.class.spec]
1919
1920 A partially specialized non-type argument expression shall not
1921 involve template parameters of the partial specialization except
1922 when the argument expression is a simple identifier.
1923
1924 The type of a template parameter corresponding to a specialized
1925 non-type argument shall not be dependent on a parameter of the
1926 specialization. */
1927 my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0);
1928 tpd2.parms = 0;
1929 for (i = 0; i < nargs; ++i)
1930 {
1931 tree arg = TREE_VEC_ELT (inner_args, i);
1932 if (/* These first two lines are the `non-type' bit. */
1933 TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
1934 && TREE_CODE (arg) != TEMPLATE_DECL
1935 /* This next line is the `argument expression is not just a
1936 simple identifier' condition and also the `specialized
1937 non-type argument' bit. */
1938 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
1939 {
1940 if (tpd.arg_uses_template_parms[i])
8251199e 1941 cp_error ("template argument `%E' involves template parameter(s)", arg);
6c30752f
MM
1942 else
1943 {
1944 /* Look at the corresponding template parameter,
1945 marking which template parameters its type depends
1946 upon. */
1947 tree type =
1948 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
1949 i)));
1950
1951 if (!tpd2.parms)
1952 {
1953 /* We haven't yet initialized TPD2. Do so now. */
1954 tpd2.arg_uses_template_parms
1955 = (int*) alloca (sizeof (int) * nargs);
104bf76a 1956 /* The number of parameters here is the number in the
76a83782
RH
1957 main template, which, as checked in the assertion
1958 above, is NARGS. */
6c30752f
MM
1959 tpd2.parms = (int*) alloca (sizeof (int) * nargs);
1960 tpd2.level =
1961 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
1962 }
1963
104bf76a 1964 /* Mark the template parameters. But this time, we're
6c30752f
MM
1965 looking for the template parameters of the main
1966 template, not in the specialization. */
1967 tpd2.current_arg = i;
1968 tpd2.arg_uses_template_parms[i] = 0;
1ddb2906 1969 bzero ((PTR) tpd2.parms, sizeof (int) * nargs);
6c30752f
MM
1970 for_each_template_parm (type,
1971 &mark_template_parm,
1972 &tpd2);
1973
1974 if (tpd2.arg_uses_template_parms [i])
1975 {
1976 /* The type depended on some template parameters.
1977 If they are fully specialized in the
1978 specialization, that's OK. */
1979 int j;
1980 for (j = 0; j < nargs; ++j)
1981 if (tpd2.parms[j] != 0
1982 && tpd.arg_uses_template_parms [j])
1983 {
104bf76a 1984 cp_error ("type `%T' of template argument `%E' depends on template parameter(s)",
6c30752f
MM
1985 type,
1986 arg);
1987 break;
1988 }
1989 }
1990 }
1991 }
1992 }
1993
1994 if (retrieve_specialization (maintmpl, specargs))
1995 /* We've already got this specialization. */
1996 return decl;
1997
1998 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type)
1999 = perm_tree_cons (inner_args, inner_parms,
2000 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2001 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2002 return decl;
2003}
2004
6ba89f8e
MM
2005/* Check that a template declaration's use of default arguments is not
2006 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2007 non-zero if DECL is the thing declared by a primary template.
2008 IS_PARTIAL is non-zero if DECL is a partial specialization. */
2009
2010static void
2011check_default_tmpl_args (decl, parms, is_primary, is_partial)
2012 tree decl;
2013 tree parms;
2014 int is_primary;
2015 int is_partial;
2016{
2017 char* msg;
2018 int last_level_to_check;
2019
2020 /* [temp.param]
2021
2022 A default template-argument shall not be specified in a
2023 function template declaration or a function template definition, nor
2024 in the template-parameter-list of the definition of a member of a
2025 class template. */
2026
2027 if (current_class_type
2028 && !TYPE_BEING_DEFINED (current_class_type)
2029 && DECL_REAL_CONTEXT (decl) == current_class_type
2030 && DECL_DEFINED_IN_CLASS_P (decl))
2031 /* We already checked these parameters when the template was
2032 declared, so there's no need to do it again now. This is an
2033 inline member function definition. */
2034 return;
2035
2036 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2037 /* For an ordinary class template, default template arguments are
2038 allowed at the innermost level, e.g.:
2039 template <class T = int>
2040 struct S {};
2041 but, in a partial specialization, they're not allowed even
2042 there, as we have in [temp.class.spec]:
2043
2044 The template parameter list of a specialization shall not
2045 contain default template argument values.
2046
2047 So, for a partial specialization, or for a function template,
2048 we look at all of them. */
2049 ;
2050 else
2051 /* But, for a primary class template that is not a partial
2052 specialization we look at all template parameters except the
2053 innermost ones. */
2054 parms = TREE_CHAIN (parms);
2055
2056 /* Figure out what error message to issue. */
2057 if (TREE_CODE (decl) == FUNCTION_DECL)
2058 msg = "default argument for template parameter in function template `%D'";
2059 else if (is_partial)
2060 msg = "default argument in partial specialization `%D'";
2061 else
2062 msg = "default argument for template parameter for class enclosing `%D'";
2063
2064 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2065 /* If we're inside a class definition, there's no need to
104bf76a 2066 examine the parameters to the class itself. On the one
6ba89f8e
MM
2067 hand, they will be checked when the class is defined, and,
2068 on the other, default arguments are legal in things like:
2069 template <class T = double>
2070 struct S { template <class U> void f(U); };
2071 Here the default argument for `S' has no bearing on the
2072 declaration of `f'. */
2073 last_level_to_check = template_class_depth (current_class_type) + 1;
2074 else
2075 /* Check everything. */
2076 last_level_to_check = 0;
2077
2078 for (; parms && TMPL_PARMS_DEPTH (parms) >= last_level_to_check;
2079 parms = TREE_CHAIN (parms))
2080 {
2081 tree inner_parms = TREE_VALUE (parms);
2082 int i, ntparms;
2083
2084 ntparms = TREE_VEC_LENGTH (inner_parms);
2085 for (i = 0; i < ntparms; ++i)
2086 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2087 {
2088 if (msg)
2089 {
2090 cp_error (msg, decl);
2091 msg = 0;
2092 }
2093
2094 /* Clear out the default argument so that we are not
2095 confused later. */
2096 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2097 }
2098
2099 /* At this point, if we're still interested in issuing messages,
2100 they must apply to classes surrounding the object declared. */
2101 if (msg)
2102 msg = "default argument for template parameter for class enclosing `%D'";
2103 }
2104}
2105
3ac3d9ea 2106/* Creates a TEMPLATE_DECL for the indicated DECL using the template
f84b4be9
JM
2107 parameters given by current_template_args, or reuses a
2108 previously existing one, if appropriate. Returns the DECL, or an
6757edfe
MM
2109 equivalent one, if it is replaced via a call to duplicate_decls.
2110
2111 If IS_FRIEND is non-zero, DECL is a friend declaration. */
3ac3d9ea
MM
2112
2113tree
6757edfe 2114push_template_decl_real (decl, is_friend)
9a3b49ac 2115 tree decl;
6757edfe 2116 int is_friend;
9a3b49ac
MS
2117{
2118 tree tmpl;
f84b4be9 2119 tree args;
9a3b49ac 2120 tree info;
f84b4be9
JM
2121 tree ctx;
2122 int primary;
6ba89f8e
MM
2123 int is_partial;
2124
2125 /* See if this is a partial specialization. */
2126 is_partial = (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)
2127 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2128 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
6757edfe
MM
2129
2130 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
f84b4be9
JM
2131
2132 if (is_friend)
2133 /* For a friend, we want the context of the friend function, not
2134 the type of which it is a friend. */
2135 ctx = DECL_CONTEXT (decl);
2c73f9f5
ML
2136 else if (DECL_REAL_CONTEXT (decl)
2137 && TREE_CODE (DECL_REAL_CONTEXT (decl)) != NAMESPACE_DECL)
f84b4be9
JM
2138 /* In the case of a virtual function, we want the class in which
2139 it is defined. */
2140 ctx = DECL_REAL_CONTEXT (decl);
2141 else
2142 /* Otherwise, if we're currently definining some class, the DECL
2143 is assumed to be a member of the class. */
2144 ctx = current_class_type;
2145
2c73f9f5
ML
2146 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2147 ctx = NULL_TREE;
2148
2149 if (!DECL_CONTEXT (decl))
cb0dbb9a 2150 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 2151
93cdc044
JM
2152 /* For determining whether this is a primary template or not, we're really
2153 interested in the lexical context, not the true context. */
2154 if (is_friend)
36a117a5 2155 info = current_class_type;
93cdc044
JM
2156 else
2157 info = ctx;
2158
6ba89f8e 2159 /* See if this is a primary template. */
93cdc044
JM
2160 if (info && TREE_CODE (info) == FUNCTION_DECL)
2161 primary = 0;
6757edfe
MM
2162 /* Note that template_class_depth returns 0 if given NULL_TREE, so
2163 this next line works even when we are at global scope. */
2164 else if (processing_template_decl > template_class_depth (info))
9a3b49ac 2165 primary = 1;
f84b4be9
JM
2166 else
2167 primary = 0;
9a3b49ac 2168
83566abf
JM
2169 if (primary)
2170 {
2171 if (current_lang_name == lang_name_c)
8251199e 2172 cp_error ("template with C linkage");
83566abf 2173 if (TREE_CODE (decl) == TYPE_DECL && ANON_AGGRNAME_P (DECL_NAME (decl)))
8251199e 2174 cp_error ("template class without a name");
ed44da02
MM
2175 if (TREE_CODE (decl) == TYPE_DECL
2176 && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
8251199e 2177 cp_error ("template declaration of `%#T'", TREE_TYPE (decl));
83566abf
JM
2178 }
2179
6ba89f8e
MM
2180 /* Check to see that the rules regarding the use of default
2181 arguments are not being violated. */
2182 check_default_tmpl_args (decl, current_template_parms,
2183 primary, is_partial);
73aad9b9 2184
6ba89f8e
MM
2185 if (is_partial)
2186 return process_partial_specialization (decl);
d32789d8 2187
9a3b49ac
MS
2188 args = current_template_args ();
2189
f84b4be9
JM
2190 if (!ctx
2191 || TREE_CODE (ctx) == FUNCTION_DECL
2192 || TYPE_BEING_DEFINED (ctx)
2193 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
8d08fdba 2194 {
75650646 2195 if (DECL_LANG_SPECIFIC (decl)
f84b4be9
JM
2196 && DECL_TEMPLATE_INFO (decl)
2197 && DECL_TI_TEMPLATE (decl))
2198 tmpl = DECL_TI_TEMPLATE (decl);
2199 else
786b5245 2200 {
f84b4be9
JM
2201 tmpl = build_template_decl (decl, current_template_parms);
2202
2203 if (DECL_LANG_SPECIFIC (decl)
2204 && DECL_TEMPLATE_SPECIALIZATION (decl))
2205 {
2206 /* A specialization of a member template of a template
2207 class. */
2208 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2209 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
2210 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
2211 }
786b5245 2212 }
8d08fdba
MS
2213 }
2214 else
2215 {
e1a5ccf7 2216 tree a, t, current, parms;
ba4f4e5d 2217 int i;
6633d636 2218
73aad9b9 2219 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
8251199e 2220 cp_error ("must specialize `%#T' before defining member `%#D'",
73aad9b9 2221 ctx, decl);
6b9ab5cc
MM
2222 if (TREE_CODE (decl) == TYPE_DECL)
2223 {
ed44da02
MM
2224 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
2225 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
2226 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2227 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2228 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
6b9ab5cc
MM
2229 else
2230 {
8251199e 2231 cp_error ("`%D' does not declare a template type", decl);
6b9ab5cc
MM
2232 return decl;
2233 }
2234 }
fc378698 2235 else if (! DECL_TEMPLATE_INFO (decl))
c91a56d2 2236 {
8251199e 2237 cp_error ("template definition of non-template `%#D'", decl);
3ac3d9ea 2238 return decl;
c91a56d2 2239 }
8d08fdba 2240 else
5566b478 2241 tmpl = DECL_TI_TEMPLATE (decl);
98c1c668 2242
e1a5ccf7
JM
2243 if (is_member_template (tmpl)
2244 && DECL_FUNCTION_TEMPLATE_P (tmpl)
2245 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
2246 && DECL_TEMPLATE_SPECIALIZATION (decl))
98c1c668 2247 {
e1a5ccf7
JM
2248 tree new_tmpl;
2249
2250 /* The declaration is a specialization of a member
2251 template, declared outside the class. Therefore, the
2252 innermost template arguments will be NULL, so we
2253 replace them with the arguments determined by the
2254 earlier call to check_explicit_specialization. */
2255 args = DECL_TI_ARGS (decl);
2256
2257 new_tmpl
2258 = build_template_decl (decl, current_template_parms);
2259 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
2260 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
2261 DECL_TI_TEMPLATE (decl) = new_tmpl;
2262 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
2263 DECL_TEMPLATE_INFO (new_tmpl) =
2264 perm_tree_cons (tmpl, args, NULL_TREE);
2265
2266 register_specialization (new_tmpl, tmpl, args);
2267 return decl;
98c1c668 2268 }
98c1c668 2269
e1a5ccf7 2270 /* Make sure the template headers we got make sense. */
6633d636 2271
e1a5ccf7
JM
2272 parms = DECL_TEMPLATE_PARMS (tmpl);
2273 i = TMPL_PARMS_DEPTH (parms);
2274 if (TMPL_ARGS_DEPTH (args) != i)
98c1c668 2275 {
e1a5ccf7
JM
2276 cp_error ("expected %d levels of template parms for `%#D', got %d",
2277 i, decl, TMPL_ARGS_DEPTH (args));
98c1c668 2278 }
e1a5ccf7
JM
2279 else
2280 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
2281 {
2282 a = TMPL_ARGS_LEVEL (args, i);
2283 t = INNERMOST_TEMPLATE_PARMS (parms);
2284
2285 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
2286 {
2287 if (current == decl)
2288 cp_error ("got %d template parameters for `%#D'",
2289 TREE_VEC_LENGTH (a), decl);
2290 else
2291 cp_error ("got %d template parameters for `%#T'",
2292 TREE_VEC_LENGTH (a), current);
2293 cp_error (" but %d required", TREE_VEC_LENGTH (t));
2294 }
98c1c668 2295
e1a5ccf7
JM
2296 /* Perhaps we should also check that the parms are used in the
2297 appropriate qualifying scopes in the declarator? */
6633d636 2298
e1a5ccf7
JM
2299 if (current == decl)
2300 current = ctx;
2301 else
2302 current = TYPE_CONTEXT (current);
2303 }
5566b478 2304 }
8d08fdba 2305
5566b478
MS
2306 DECL_TEMPLATE_RESULT (tmpl) = decl;
2307 TREE_TYPE (tmpl) = TREE_TYPE (decl);
8d08fdba 2308
36a117a5
MM
2309 /* Push template declarations for global functions and types. Note
2310 that we do not try to push a global template friend declared in a
2311 template class; such a thing may well depend on the template
39c01e4c 2312 parameters of the class. */
36a117a5
MM
2313 if (! ctx
2314 && !(is_friend && template_class_depth (current_class_type) > 0))
2c73f9f5 2315 tmpl = pushdecl_namespace_level (tmpl);
8d08fdba 2316
5566b478 2317 if (primary)
6757edfe 2318 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5566b478
MS
2319
2320 info = perm_tree_cons (tmpl, args, NULL_TREE);
2321
824b9a4c 2322 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
8d08fdba 2323 {
ed44da02
MM
2324 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
2325 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
2326 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE)
75650646 2327 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
51c184be 2328 }
ec255269 2329 else if (! DECL_LANG_SPECIFIC (decl))
8251199e 2330 cp_error ("template declaration of `%#D'", decl);
51c184be 2331 else
5566b478 2332 DECL_TEMPLATE_INFO (decl) = info;
3ac3d9ea
MM
2333
2334 return DECL_TEMPLATE_RESULT (tmpl);
8d08fdba
MS
2335}
2336
6757edfe
MM
2337tree
2338push_template_decl (decl)
2339 tree decl;
2340{
2341 return push_template_decl_real (decl, 0);
2342}
2343
2344/* Called when a class template TYPE is redeclared with the indicated
2345 template PARMS, e.g.:
7fe6899f
MM
2346
2347 template <class T> struct S;
2348 template <class T> struct S {}; */
2349
2350void
6757edfe 2351redeclare_class_template (type, parms)
7fe6899f 2352 tree type;
6757edfe 2353 tree parms;
7fe6899f
MM
2354{
2355 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
6757edfe 2356 tree tmpl_parms;
7fe6899f
MM
2357 int i;
2358
2359 if (!PRIMARY_TEMPLATE_P (tmpl))
2360 /* The type is nested in some template class. Nothing to worry
2361 about here; there are no new template parameters for the nested
2362 type. */
2363 return;
2364
6757edfe
MM
2365 parms = INNERMOST_TEMPLATE_PARMS (parms);
2366 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
2367
7fe6899f
MM
2368 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
2369 {
8251199e
JM
2370 cp_error_at ("previous declaration `%D'", tmpl);
2371 cp_error ("used %d template parameter%s instead of %d",
7fe6899f
MM
2372 TREE_VEC_LENGTH (tmpl_parms),
2373 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
2374 TREE_VEC_LENGTH (parms));
2375 return;
2376 }
2377
2378 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
2379 {
2380 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
2381 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
2382 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
2383 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
2384
2385 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm))
2386 {
8251199e
JM
2387 cp_error_at ("template parameter `%#D'", tmpl_parm);
2388 cp_error ("redeclared here as `%#D'", parm);
7fe6899f
MM
2389 return;
2390 }
2391
2392 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
2393 {
2394 /* We have in [temp.param]:
2395
2396 A template-parameter may not be given default arguments
2397 by two different declarations in the same scope. */
8251199e
JM
2398 cp_error ("redefinition of default argument for `%#D'", parm);
2399 cp_error_at (" original definition appeared here", tmpl_parm);
7fe6899f
MM
2400 return;
2401 }
2402
2403 if (parm_default != NULL_TREE)
2404 /* Update the previous template parameters (which are the ones
2405 that will really count) with the new default value. */
2406 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
2407 }
2408}
75650646 2409
75650646
MM
2410/* Attempt to convert the non-type template parameter EXPR to the
2411 indicated TYPE. If the conversion is successful, return the
2412 converted value. If the conversion is unsuccesful, return
2413 NULL_TREE if we issued an error message, or error_mark_node if we
2414 did not. We issue error messages for out-and-out bad template
2415 parameters, but not simply because the conversion failed, since we
2416 might be just trying to do argument deduction. By the time this
2417 function is called, neither TYPE nor EXPR may make use of template
2418 parameters. */
2419
2420static tree
e1467ff2 2421convert_nontype_argument (type, expr)
75650646
MM
2422 tree type;
2423 tree expr;
2424{
2425 tree expr_type = TREE_TYPE (expr);
2426
2427 /* A template-argument for a non-type, non-template
2428 template-parameter shall be one of:
2429
2430 --an integral constant-expression of integral or enumeration
2431 type; or
2432
2433 --the name of a non-type template-parameter; or
2434
2435 --the name of an object or function with external linkage,
2436 including function templates and function template-ids but
86052cc3 2437 excluding non-static class members, expressed as id-expression;
75650646
MM
2438 or
2439
2440 --the address of an object or function with external linkage,
2441 including function templates and function template-ids but
2442 excluding non-static class members, expressed as & id-expression
2443 where the & is optional if the name refers to a function or
2444 array; or
2445
2446 --a pointer to member expressed as described in _expr.unary.op_. */
2447
86052cc3
JM
2448 /* An integral constant-expression can include const variables
2449 or enumerators. */
2450 if (INTEGRAL_TYPE_P (expr_type) && TREE_READONLY_DECL_P (expr))
2451 expr = decl_constant_value (expr);
2452
7bf2682f
MM
2453 if (is_overloaded_fn (expr))
2454 /* OK for now. We'll check that it has external linkage later.
2455 Check this first since if expr_type is the unknown_type_node
2456 we would otherwise complain below. */
2457 ;
f3400fe2
JM
2458 else if (TYPE_PTR_P (expr_type)
2459 || TREE_CODE (expr_type) == ARRAY_TYPE
2460 || TREE_CODE (type) == REFERENCE_TYPE
75650646
MM
2461 /* If expr is the address of an overloaded function, we
2462 will get the unknown_type_node at this point. */
2463 || expr_type == unknown_type_node)
2464 {
2465 tree referent;
49bf0d6f
MM
2466 tree e = expr;
2467 STRIP_NOPS (e);
75650646 2468
f3400fe2
JM
2469 if (TREE_CODE (type) == REFERENCE_TYPE
2470 || TREE_CODE (expr_type) == ARRAY_TYPE)
2471 referent = e;
2472 else
75650646 2473 {
f3400fe2
JM
2474 if (TREE_CODE (e) != ADDR_EXPR)
2475 {
2476 bad_argument:
2477 cp_error ("`%E' is not a valid template argument", expr);
2478 error ("it must be %s%s with external linkage",
2479 TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE
2480 ? "a pointer to " : "",
2481 TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == FUNCTION_TYPE
2482 ? "a function" : "an object");
2483 return NULL_TREE;
2484 }
2485
2486 referent = TREE_OPERAND (e, 0);
2487 STRIP_NOPS (referent);
75650646
MM
2488 }
2489
75650646
MM
2490 if (TREE_CODE (referent) == STRING_CST)
2491 {
8251199e 2492 cp_error ("string literal %E is not a valid template argument",
75650646 2493 referent);
8251199e 2494 error ("because it is the address of an object with static linkage");
75650646
MM
2495 return NULL_TREE;
2496 }
2497
2498 if (is_overloaded_fn (referent))
2499 /* We'll check that it has external linkage later. */
2500 ;
2501 else if (TREE_CODE (referent) != VAR_DECL)
2502 goto bad_argument;
2503 else if (!TREE_PUBLIC (referent))
2504 {
8251199e 2505 cp_error ("address of non-extern `%E' cannot be used as template argument", referent);
75650646
MM
2506 return error_mark_node;
2507 }
2508 }
f3400fe2
JM
2509 else if (INTEGRAL_TYPE_P (expr_type)
2510 || TYPE_PTRMEM_P (expr_type)
2511 || TYPE_PTRMEMFUNC_P (expr_type)
2512 /* The next two are g++ extensions. */
2513 || TREE_CODE (expr_type) == REAL_TYPE
2514 || TREE_CODE (expr_type) == COMPLEX_TYPE)
75650646 2515 {
f3400fe2
JM
2516 if (! TREE_CONSTANT (expr))
2517 {
2518 non_constant:
2519 cp_error ("non-constant `%E' cannot be used as template argument",
2520 expr);
2521 return NULL_TREE;
2522 }
75650646 2523 }
75650646
MM
2524 else
2525 {
8251199e 2526 cp_error ("object `%E' cannot be used as template argument", expr);
75650646
MM
2527 return NULL_TREE;
2528 }
2529
2530 switch (TREE_CODE (type))
2531 {
2532 case INTEGER_TYPE:
2533 case BOOLEAN_TYPE:
2534 case ENUMERAL_TYPE:
2535 /* For a non-type template-parameter of integral or enumeration
2536 type, integral promotions (_conv.prom_) and integral
2537 conversions (_conv.integral_) are applied. */
2538 if (!INTEGRAL_TYPE_P (expr_type))
2539 return error_mark_node;
2540
2541 /* It's safe to call digest_init in this case; we know we're
2542 just converting one integral constant expression to another. */
db02b6b9 2543 expr = digest_init (type, expr, (tree*) 0);
75650646 2544
db02b6b9 2545 if (TREE_CODE (expr) != INTEGER_CST)
f3400fe2 2546 /* Curiously, some TREE_CONSTANT integral expressions do not
db02b6b9
MM
2547 simplify to integer constants. For example, `3 % 0',
2548 remains a TRUNC_MOD_EXPR. */
2549 goto non_constant;
2550
2551 return expr;
2552
abff8e06
JM
2553 case REAL_TYPE:
2554 case COMPLEX_TYPE:
2555 /* These are g++ extensions. */
2556 if (TREE_CODE (expr_type) != TREE_CODE (type))
2557 return error_mark_node;
2558
db02b6b9
MM
2559 expr = digest_init (type, expr, (tree*) 0);
2560
2561 if (TREE_CODE (expr) != REAL_CST)
2562 goto non_constant;
2563
2564 return expr;
abff8e06 2565
75650646
MM
2566 case POINTER_TYPE:
2567 {
2568 tree type_pointed_to = TREE_TYPE (type);
2569
2570 if (TYPE_PTRMEM_P (type))
2571 /* For a non-type template-parameter of type pointer to data
2572 member, qualification conversions (_conv.qual_) are
2573 applied. */
2574 return perform_qualification_conversions (type, expr);
2575 else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
2576 {
2577 /* For a non-type template-parameter of type pointer to
2578 function, only the function-to-pointer conversion
2579 (_conv.func_) is applied. If the template-argument
2580 represents a set of overloaded functions (or a pointer to
2581 such), the matching function is selected from the set
2582 (_over.over_). */
2583 tree fns;
2584 tree fn;
2585
7bf2682f 2586 if (TREE_CODE (expr) == ADDR_EXPR)
75650646
MM
2587 fns = TREE_OPERAND (expr, 0);
2588 else
2589 fns = expr;
2590
a05273d4 2591 fn = instantiate_type (type_pointed_to, fns, 0);
75650646
MM
2592
2593 if (fn == error_mark_node)
2594 return error_mark_node;
2595
2596 if (!TREE_PUBLIC (fn))
2597 {
2598 if (really_overloaded_fn (fns))
2599 return error_mark_node;
2600 else
2601 goto bad_argument;
2602 }
2603
2604 expr = build_unary_op (ADDR_EXPR, fn, 0);
2605
3bfdc719 2606 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
75650646
MM
2607 0);
2608 return expr;
2609 }
2610 else
2611 {
2612 /* For a non-type template-parameter of type pointer to
2613 object, qualification conversions (_conv.qual_) and the
2614 array-to-pointer conversion (_conv.array_) are applied.
2615 [Note: In particular, neither the null pointer conversion
2616 (_conv.ptr_) nor the derived-to-base conversion
2617 (_conv.ptr_) are applied. Although 0 is a valid
2618 template-argument for a non-type template-parameter of
2619 integral type, it is not a valid template-argument for a
e1467ff2
MM
2620 non-type template-parameter of pointer type.]
2621
2622 The call to decay_conversion performs the
2623 array-to-pointer conversion, if appropriate. */
2624 expr = decay_conversion (expr);
75650646
MM
2625
2626 if (expr == error_mark_node)
2627 return error_mark_node;
2628 else
2629 return perform_qualification_conversions (type, expr);
2630 }
2631 }
2632 break;
2633
2634 case REFERENCE_TYPE:
2635 {
2636 tree type_referred_to = TREE_TYPE (type);
2637
2638 if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
2639 {
2640 /* For a non-type template-parameter of type reference to
39c01e4c
MM
2641 function, no conversions apply. If the
2642 template-argument represents a set of overloaded
2643 functions, the matching function is selected from the
2644 set (_over.over_). */
75650646
MM
2645 tree fns = expr;
2646 tree fn;
2647
a05273d4 2648 fn = instantiate_type (type_referred_to, fns, 0);
e6e174e5
JM
2649
2650 if (fn == error_mark_node)
2651 return error_mark_node;
75650646
MM
2652
2653 if (!TREE_PUBLIC (fn))
2654 {
2655 if (really_overloaded_fn (fns))
2656 /* Don't issue an error here; we might get a different
2657 function if the overloading had worked out
2658 differently. */
2659 return error_mark_node;
2660 else
2661 goto bad_argument;
2662 }
2663
3bfdc719
MM
2664 my_friendly_assert (same_type_p (type_referred_to,
2665 TREE_TYPE (fn)),
75650646
MM
2666 0);
2667
2668 return fn;
2669 }
2670 else
2671 {
2672 /* For a non-type template-parameter of type reference to
2673 object, no conversions apply. The type referred to by the
2674 reference may be more cv-qualified than the (otherwise
2675 identical) type of the template-argument. The
2676 template-parameter is bound directly to the
2677 template-argument, which must be an lvalue. */
ff350acd
JM
2678 if ((TYPE_MAIN_VARIANT (expr_type)
2679 != TYPE_MAIN_VARIANT (type_referred_to))
91063b51
MM
2680 || !at_least_as_qualified_p (type_referred_to,
2681 expr_type)
75650646
MM
2682 || !real_lvalue_p (expr))
2683 return error_mark_node;
2684 else
2685 return expr;
2686 }
2687 }
2688 break;
2689
2690 case RECORD_TYPE:
2691 {
410c2fba
KL
2692 if (!TYPE_PTRMEMFUNC_P (type))
2693 /* This handles templates like
2694 template<class T, T t> void f();
2695 when T is substituted with any class. The second template
2696 parameter becomes invalid and the template candidate is
2697 rejected. */
2698 return error_mark_node;
75650646
MM
2699
2700 /* For a non-type template-parameter of type pointer to member
2701 function, no conversions apply. If the template-argument
2702 represents a set of overloaded member functions, the
2703 matching member function is selected from the set
2704 (_over.over_). */
2705
2706 if (!TYPE_PTRMEMFUNC_P (expr_type) &&
2707 expr_type != unknown_type_node)
2708 return error_mark_node;
2709
2710 if (TREE_CODE (expr) == CONSTRUCTOR)
2711 {
2712 /* A ptr-to-member constant. */
3bfdc719 2713 if (!same_type_p (type, expr_type))
75650646
MM
2714 return error_mark_node;
2715 else
2716 return expr;
2717 }
2718
2719 if (TREE_CODE (expr) != ADDR_EXPR)
2720 return error_mark_node;
2721
d8f8dca1 2722 expr = instantiate_type (type, expr, 0);
75650646 2723
d8f8dca1 2724 if (expr == error_mark_node)
75650646
MM
2725 return error_mark_node;
2726
3bfdc719 2727 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
75650646
MM
2728 0);
2729 return expr;
2730 }
2731 break;
2732
2733 default:
2734 /* All non-type parameters must have one of these types. */
2735 my_friendly_abort (0);
2736 break;
2737 }
2738
2739 return error_mark_node;
2740}
2741
744fac59
KL
2742/* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
2743 template template parameters. Both PARM_PARMS and ARG_PARMS are
2744 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
2745 or PARM_DECL.
2746
2747 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
2748 the case, then extra parameters must have default arguments.
2749
2750 Consider the example:
2751 template <class T, class Allocator = allocator> class vector;
2752 template<template <class U> class TT> class C;
2753
2754 C<vector> is a valid instantiation. PARM_PARMS for the above code
2755 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
2756 T and Allocator) and OUTER_ARGS contains the argument that is used to
2757 substitute the TT parameter. */
2758
2759static int
2760coerce_template_template_parms (parm_parms, arg_parms, in_decl, outer_args)
2761 tree parm_parms, arg_parms, in_decl, outer_args;
2762{
2763 int nparms, nargs, i;
2764 tree parm, arg;
2765
2766 my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0);
2767 my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0);
2768
2769 nparms = TREE_VEC_LENGTH (parm_parms);
2770 nargs = TREE_VEC_LENGTH (arg_parms);
2771
2772 /* The rule here is opposite of coerce_template_parms. */
2773 if (nargs < nparms
2774 || (nargs > nparms
2775 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
2776 return 0;
2777
2778 for (i = 0; i < nparms; ++i)
2779 {
2780 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
2781 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
2782
2783 if (arg == NULL_TREE || arg == error_mark_node
2784 || parm == NULL_TREE || parm == error_mark_node)
2785 return 0;
2786
2787 if (TREE_CODE (arg) != TREE_CODE (parm))
2788 return 0;
2789
2790 switch (TREE_CODE (parm))
2791 {
2792 case TYPE_DECL:
2793 break;
2794
2795 case TEMPLATE_DECL:
2796 /* We encounter instantiations of templates like
2797 template <template <template <class> class> class TT>
2798 class C; */
2799 sorry ("nested template template parameter");
2800 return 0;
2801
2802 case PARM_DECL:
2803 /* The tsubst call is used to handle cases such as
2804 template <class T, template <T> class TT> class D;
2805 i.e. the parameter list of TT depends on earlier parameters. */
3bfdc719
MM
2806 if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args, in_decl),
2807 TREE_TYPE (arg)))
744fac59
KL
2808 return 0;
2809 break;
2810
2811 default:
2812 my_friendly_abort (0);
2813 }
2814 }
2815 return 1;
2816}
2817
8b5b8b7c
MM
2818/* Convert the indicated template ARG as necessary to match the
2819 indicated template PARM. Returns the converted ARG, or
2820 error_mark_node if the conversion was unsuccessful. Error messages
2821 are issued if COMPLAIN is non-zero. This conversion is for the Ith
2822 parameter in the parameter list. ARGS is the full set of template
2823 arguments deduced so far. */
2824
2825static tree
2826convert_template_argument (parm, arg, args, complain, i, in_decl)
2827 tree parm;
2828 tree arg;
2829 tree args;
2830 int complain;
2831 int i;
2832 tree in_decl;
2833{
2834 tree val;
2835 tree inner_args;
2836 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
2837
2838 inner_args = innermost_args (args);
2839
2840 if (TREE_CODE (arg) == TREE_LIST
2841 && TREE_TYPE (arg) != NULL_TREE
2842 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
2843 {
2844 /* The template argument was the name of some
2845 member function. That's usually
2846 illegal, but static members are OK. In any
2847 case, grab the underlying fields/functions
2848 and issue an error later if required. */
2849 arg = TREE_VALUE (arg);
2850 TREE_TYPE (arg) = unknown_type_node;
2851 }
2852
2853 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
2854 requires_type = (TREE_CODE (parm) == TYPE_DECL
2855 || requires_tmpl_type);
2856
2857 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
2858 we also accept implicitly created TYPE_DECL as a valid argument.
2859 This is necessary to handle the case where we pass a template name
2860 to a template template parameter in a scope where we've derived from
2861 in instantiation of that template, so the template name refers to that
2862 instantiation. We really ought to handle this better. */
2863 is_tmpl_type
2864 = ((TREE_CODE (arg) == TEMPLATE_DECL
2865 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
2866 || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7ddedda4 2867 && !TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (arg))
8b5b8b7c
MM
2868 || (TREE_CODE (arg) == RECORD_TYPE
2869 && CLASSTYPE_TEMPLATE_INFO (arg)
2870 && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL
2871 && DECL_ARTIFICIAL (TYPE_NAME (arg))
2872 && requires_tmpl_type
b54ccf71 2873 && is_base_of_enclosing_class (arg, current_class_type)));
8b5b8b7c
MM
2874 if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2875 arg = TYPE_STUB_DECL (arg);
2876 else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE)
2877 arg = CLASSTYPE_TI_TEMPLATE (arg);
2878
2879 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't' || is_tmpl_type;
2880
2881 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
2882 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
2883 {
8251199e
JM
2884 cp_pedwarn ("to refer to a type member of a template parameter,");
2885 cp_pedwarn (" use `typename %E'", arg);
8b5b8b7c
MM
2886
2887 arg = make_typename_type (TREE_OPERAND (arg, 0),
2888 TREE_OPERAND (arg, 1));
2889 is_type = 1;
2890 }
2891 if (is_type != requires_type)
2892 {
2893 if (in_decl)
2894 {
2895 if (complain)
2896 {
8251199e 2897 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
8b5b8b7c
MM
2898 i + 1, in_decl);
2899 if (is_type)
8251199e 2900 cp_error (" expected a constant of type `%T', got `%T'",
8b5b8b7c
MM
2901 TREE_TYPE (parm),
2902 (is_tmpl_type ? DECL_NAME (arg) : arg));
2903 else
8251199e 2904 cp_error (" expected a type, got `%E'", arg);
8b5b8b7c
MM
2905 }
2906 }
2907 return error_mark_node;
2908 }
2909 if (is_tmpl_type ^ requires_tmpl_type)
2910 {
2911 if (in_decl && complain)
2912 {
8251199e 2913 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
8b5b8b7c
MM
2914 i + 1, in_decl);
2915 if (is_tmpl_type)
8251199e 2916 cp_error (" expected a type, got `%T'", DECL_NAME (arg));
8b5b8b7c 2917 else
8251199e 2918 cp_error (" expected a class template, got `%T'", arg);
8b5b8b7c
MM
2919 }
2920 return error_mark_node;
2921 }
2922
2923 if (is_type)
2924 {
2925 if (requires_tmpl_type)
2926 {
2927 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
2928 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
2929
2930 if (coerce_template_template_parms (parmparm, argparm,
2931 in_decl, inner_args))
2932 {
2933 val = arg;
2934
2935 /* TEMPLATE_TEMPLATE_PARM node is preferred over
2936 TEMPLATE_DECL. */
2937 if (val != error_mark_node
2938 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
2939 val = TREE_TYPE (val);
2940 }
2941 else
2942 {
2943 if (in_decl && complain)
2944 {
8251199e 2945 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
8b5b8b7c 2946 i + 1, in_decl);
8251199e 2947 cp_error (" expected a template of type `%D', got `%D'", parm, arg);
8b5b8b7c
MM
2948 }
2949
2950 val = error_mark_node;
2951 }
2952 }
2953 else
2954 {
2955 val = groktypename (arg);
2956 if (! processing_template_decl)
2957 {
2958 /* [basic.link]: A name with no linkage (notably, the
2959 name of a class or enumeration declared in a local
2960 scope) shall not be used to declare an entity with
2961 linkage. This implies that names with no linkage
2962 cannot be used as template arguments. */
2963 tree t = no_linkage_check (val);
2964 if (t)
2965 {
2966 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8251199e
JM
2967 cp_pedwarn
2968 ("template-argument `%T' uses anonymous type", val);
8b5b8b7c 2969 else
8251199e
JM
2970 cp_error
2971 ("template-argument `%T' uses local type `%T'",
8b5b8b7c
MM
2972 val, t);
2973 return error_mark_node;
2974 }
2975 }
2976 }
2977 }
2978 else
2979 {
2980 tree t = tsubst (TREE_TYPE (parm), args, in_decl);
2981
2982 if (processing_template_decl)
2983 arg = maybe_fold_nontype_arg (arg);
2984
2985 if (!uses_template_parms (arg) && !uses_template_parms (t))
2986 /* We used to call digest_init here. However, digest_init
2987 will report errors, which we don't want when complain
2988 is zero. More importantly, digest_init will try too
2989 hard to convert things: for example, `0' should not be
2990 converted to pointer type at this point according to
2991 the standard. Accepting this is not merely an
2992 extension, since deciding whether or not these
2993 conversions can occur is part of determining which
2994 function template to call, or whether a given epxlicit
2995 argument specification is legal. */
2996 val = convert_nontype_argument (t, arg);
2997 else
2998 val = arg;
2999
3000 if (val == NULL_TREE)
3001 val = error_mark_node;
3002 else if (val == error_mark_node && complain)
8251199e 3003 cp_error ("could not convert template argument `%E' to `%T'",
8b5b8b7c
MM
3004 arg, t);
3005 }
3006
3007 return val;
3008}
3009
3010/* Convert all template arguments to their appropriate types, and
3011 return a vector containing the innermost resulting template
3012 arguments. If any error occurs, return error_mark_node, and, if
3013 COMPLAIN is non-zero, issue an error message. Some error messages
3014 are issued even if COMPLAIN is zero; for instance, if a template
3015 argument is composed from a local class.
75650646
MM
3016
3017 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
3018 provided in ARGLIST, or else trailing parameters must have default
3019 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
cabc336a
MM
3020 deduction for any unspecified trailing arguments.
3021
3022 The resulting TREE_VEC is allocated on a temporary obstack, and
3023 must be explicitly copied if it will be permanent. */
de575009 3024
8d08fdba 3025static tree
8b5b8b7c 3026coerce_template_parms (parms, args, in_decl,
75650646 3027 complain,
744fac59 3028 require_all_arguments)
8b5b8b7c 3029 tree parms, args;
8d08fdba 3030 tree in_decl;
75650646
MM
3031 int complain;
3032 int require_all_arguments;
8d08fdba 3033{
a292b002 3034 int nparms, nargs, i, lost = 0;
e4a84209 3035 tree inner_args;
8b5b8b7c
MM
3036 tree new_args;
3037 tree new_inner_args;
a292b002 3038
8b5b8b7c 3039 inner_args = innermost_args (args);
e4a84209 3040 nargs = NUM_TMPL_ARGS (inner_args);
a292b002
MS
3041 nparms = TREE_VEC_LENGTH (parms);
3042
3043 if (nargs > nparms
3044 || (nargs < nparms
75650646 3045 && require_all_arguments
a292b002 3046 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
8d08fdba 3047 {
75650646
MM
3048 if (complain)
3049 {
8251199e 3050 cp_error ("wrong number of template arguments (%d, should be %d)",
e4a84209 3051 nargs, nparms);
75650646
MM
3052
3053 if (in_decl)
8251199e 3054 cp_error_at ("provided for `%D'", in_decl);
75650646
MM
3055 }
3056
8d08fdba
MS
3057 return error_mark_node;
3058 }
3059
cabc336a 3060 new_inner_args = make_temp_vec (nparms);
8b5b8b7c
MM
3061 new_args = add_outermost_template_args (args, new_inner_args);
3062 for (i = 0; i < nparms; i++)
8d08fdba 3063 {
8b5b8b7c
MM
3064 tree arg;
3065 tree parm;
e4a84209 3066
8b5b8b7c
MM
3067 /* Get the Ith template parameter. */
3068 parm = TREE_VEC_ELT (parms, i);
75650646 3069
8b5b8b7c
MM
3070 /* Calculate the Ith argument. */
3071 if (inner_args && TREE_CODE (inner_args) == TREE_LIST)
8d08fdba 3072 {
8b5b8b7c
MM
3073 arg = TREE_VALUE (inner_args);
3074 inner_args = TREE_CHAIN (inner_args);
8d08fdba 3075 }
8b5b8b7c
MM
3076 else if (i < nargs)
3077 arg = TREE_VEC_ELT (inner_args, i);
3078 /* If no template argument was supplied, look for a default
3079 value. */
3080 else if (TREE_PURPOSE (parm) == NULL_TREE)
3081 {
3082 /* There was no default value. */
3083 my_friendly_assert (!require_all_arguments, 0);
3084 break;
3085 }
3086 else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
3087 arg = tsubst (TREE_PURPOSE (parm), new_args, in_decl);
3088 else
3089 arg = tsubst_expr (TREE_PURPOSE (parm), new_args, in_decl);
e4a84209 3090
8b5b8b7c 3091 /* Now, convert the Ith argument, as necessary. */
75650646
MM
3092 if (arg == NULL_TREE)
3093 /* We're out of arguments. */
3094 {
3095 my_friendly_assert (!require_all_arguments, 0);
3096 break;
3097 }
8b5b8b7c 3098 else if (arg == error_mark_node)
75650646 3099 {
8251199e 3100 cp_error ("template argument %d is invalid", i + 1);
8b5b8b7c 3101 arg = error_mark_node;
8d08fdba 3102 }
8b5b8b7c
MM
3103 else
3104 arg = convert_template_argument (TREE_VALUE (parm),
3105 arg, new_args, complain, i,
3106 in_decl);
3107
3108 if (arg == error_mark_node)
8d08fdba 3109 lost++;
8b5b8b7c 3110 TREE_VEC_ELT (new_inner_args, i) = arg;
8d08fdba 3111 }
8b5b8b7c 3112
8d08fdba
MS
3113 if (lost)
3114 return error_mark_node;
8b5b8b7c
MM
3115
3116 return new_inner_args;
8d08fdba
MS
3117}
3118
34016c81
JM
3119/* Returns 1 if template args OT and NT are equivalent. */
3120
3121int
3122template_args_equal (ot, nt)
3123 tree ot, nt;
3124{
3125 if (nt == ot)
3126 return 1;
3127 if (TREE_CODE (nt) != TREE_CODE (ot))
3128 return 0;
3129 if (TREE_CODE (nt) == TREE_VEC)
3130 /* For member templates */
3131 return comp_template_args (ot, nt);
3132 else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
3bfdc719 3133 return same_type_p (ot, nt);
34016c81
JM
3134 else
3135 return (cp_tree_equal (ot, nt) > 0);
3136}
3137
3138/* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
f84b4be9
JM
3139 of template arguments. Returns 0 otherwise. */
3140
6757edfe 3141int
5566b478
MS
3142comp_template_args (oldargs, newargs)
3143 tree oldargs, newargs;
3144{
3145 int i;
3146
386b8a85
JM
3147 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
3148 return 0;
3149
5566b478
MS
3150 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
3151 {
3152 tree nt = TREE_VEC_ELT (newargs, i);
3153 tree ot = TREE_VEC_ELT (oldargs, i);
3154
34016c81 3155 if (! template_args_equal (ot, nt))
61a127b3 3156 return 0;
5566b478
MS
3157 }
3158 return 1;
3159}
3160
8d08fdba
MS
3161/* Given class template name and parameter list, produce a user-friendly name
3162 for the instantiation. */
e92cc029 3163
8d08fdba 3164static char *
36a117a5 3165mangle_class_name_for_template (name, parms, arglist)
8d08fdba
MS
3166 char *name;
3167 tree parms, arglist;
3168{
3169 static struct obstack scratch_obstack;
3170 static char *scratch_firstobj;
3171 int i, nparms;
8d08fdba
MS
3172
3173 if (!scratch_firstobj)
fc378698 3174 gcc_obstack_init (&scratch_obstack);
8d08fdba
MS
3175 else
3176 obstack_free (&scratch_obstack, scratch_firstobj);
fc378698 3177 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
8d08fdba 3178
8d08fdba 3179#define ccat(c) obstack_1grow (&scratch_obstack, (c));
8d08fdba 3180#define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
8d08fdba
MS
3181
3182 cat (name);
3183 ccat ('<');
3184 nparms = TREE_VEC_LENGTH (parms);
36a117a5 3185 arglist = innermost_args (arglist);
8d08fdba
MS
3186 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
3187 for (i = 0; i < nparms; i++)
3188 {
a292b002
MS
3189 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3190 tree arg = TREE_VEC_ELT (arglist, i);
8d08fdba
MS
3191
3192 if (i)
3193 ccat (',');
3194
a292b002 3195 if (TREE_CODE (parm) == TYPE_DECL)
8d08fdba 3196 {
294471c3 3197 cat (type_as_string_real (arg, 0, 1));
8d08fdba
MS
3198 continue;
3199 }
73b0fce8
KL
3200 else if (TREE_CODE (parm) == TEMPLATE_DECL)
3201 {
3202 if (TREE_CODE (arg) == TEMPLATE_DECL)
2c73f9f5
ML
3203 {
3204 /* Already substituted with real template. Just output
3205 the template name here */
cb0dbb9a
JM
3206 tree context = DECL_CONTEXT (arg);
3207 if (context)
2c73f9f5 3208 {
cb0dbb9a 3209 my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL, 980422);
2c73f9f5
ML
3210 cat(decl_as_string (DECL_CONTEXT (arg), 0));
3211 cat("::");
3212 }
3213 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
3214 }
73b0fce8
KL
3215 else
3216 /* Output the parameter declaration */
294471c3 3217 cat (type_as_string_real (arg, 0, 1));
73b0fce8
KL
3218 continue;
3219 }
8d08fdba
MS
3220 else
3221 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
3222
3223 if (TREE_CODE (arg) == TREE_LIST)
3224 {
3225 /* New list cell was built because old chain link was in
3226 use. */
3227 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
3228 arg = TREE_VALUE (arg);
3229 }
3230 /* No need to check arglist against parmlist here; we did that
3231 in coerce_template_parms, called from lookup_template_class. */
3232 cat (expr_as_string (arg, 0));
3233 }
3234 {
3235 char *bufp = obstack_next_free (&scratch_obstack);
3236 int offset = 0;
3237 while (bufp[offset - 1] == ' ')
3238 offset--;
3239 obstack_blank_fast (&scratch_obstack, offset);
3240
3241 /* B<C<char> >, not B<C<char>> */
3242 if (bufp[offset - 1] == '>')
3243 ccat (' ');
3244 }
3245 ccat ('>');
3246 ccat ('\0');
3247 return (char *) obstack_base (&scratch_obstack);
8d08fdba
MS
3248}
3249
bd6dd845 3250static tree
5566b478
MS
3251classtype_mangled_name (t)
3252 tree t;
3253{
3254 if (CLASSTYPE_TEMPLATE_INFO (t)
36a117a5
MM
3255 /* Specializations have already had their names set up in
3256 lookup_template_class. */
9fbf56f7
MM
3257 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
3258 {
3259 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
3260
36a117a5
MM
3261 /* For non-primary templates, the template parameters are
3262 implicit from their surrounding context. */
9fbf56f7
MM
3263 if (PRIMARY_TEMPLATE_P (tmpl))
3264 {
3265 tree name = DECL_NAME (tmpl);
3266 char *mangled_name = mangle_class_name_for_template
3267 (IDENTIFIER_POINTER (name),
3268 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
3269 CLASSTYPE_TI_ARGS (t));
3270 tree id = get_identifier (mangled_name);
3271 IDENTIFIER_TEMPLATE (id) = name;
3272 return id;
3273 }
5566b478 3274 }
9fbf56f7
MM
3275
3276 return TYPE_IDENTIFIER (t);
5566b478
MS
3277}
3278
3279static void
3280add_pending_template (d)
3281 tree d;
3282{
e92cc029
MS
3283 tree ti;
3284
3285 if (TREE_CODE_CLASS (TREE_CODE (d)) == 't')
3286 ti = CLASSTYPE_TEMPLATE_INFO (d);
3287 else
3288 ti = DECL_TEMPLATE_INFO (d);
3289
824b9a4c 3290 if (TI_PENDING_TEMPLATE_FLAG (ti))
5566b478
MS
3291 return;
3292
3293 *template_tail = perm_tree_cons
1139b3d8 3294 (build_srcloc_here (), d, NULL_TREE);
5566b478 3295 template_tail = &TREE_CHAIN (*template_tail);
824b9a4c 3296 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
5566b478
MS
3297}
3298
386b8a85
JM
3299
3300/* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3301 may be either a _DECL or an overloaded function or an
3302 IDENTIFIER_NODE), and ARGLIST. */
3303
3304tree
3305lookup_template_function (fns, arglist)
3306 tree fns, arglist;
3307{
2c73f9f5 3308 tree type;
050367a3 3309
386b8a85
JM
3310 if (fns == NULL_TREE)
3311 {
8251199e 3312 cp_error ("non-template used as template");
386b8a85
JM
3313 return error_mark_node;
3314 }
3315
2c73f9f5
ML
3316 type = TREE_TYPE (fns);
3317 if (TREE_CODE (fns) == OVERLOAD || !type)
3318 type = unknown_type_node;
3319
672476cb
MM
3320 if (processing_template_decl)
3321 return build_min (TEMPLATE_ID_EXPR, type, fns, arglist);
3322 else
3323 return build (TEMPLATE_ID_EXPR, type, fns, arglist);
386b8a85
JM
3324}
3325
a2b60a0e
MM
3326/* Within the scope of a template class S<T>, the name S gets bound
3327 (in build_self_reference) to a TYPE_DECL for the class, not a
3328 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3329 or one of its enclosing classes, and that type is a template,
3330 return the associated TEMPLATE_DECL. Otherwise, the original
3331 DECL is returned. */
3332
3333tree
3334maybe_get_template_decl_from_type_decl (decl)
3335 tree decl;
3336{
3337 return (decl != NULL_TREE
3338 && TREE_CODE (decl) == TYPE_DECL
3339 && DECL_ARTIFICIAL (decl)
3340 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
3341 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
3342}
386b8a85 3343
8d08fdba
MS
3344/* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3345 parameters, find the desired type.
3346
3347 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
36a117a5
MM
3348 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3349 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3350 otherwise.) Since ARGLIST is build on the decl_obstack, we must
3351 copy it here to keep it from being reclaimed when the decl storage
3352 is reclaimed.
8d08fdba
MS
3353
3354 IN_DECL, if non-NULL, is the template declaration we are trying to
75650646
MM
3355 instantiate.
3356
36a117a5
MM
3357 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3358 the class we are looking up.
3359
75650646
MM
3360 If the template class is really a local class in a template
3361 function, then the FUNCTION_CONTEXT is the function in which it is
3362 being instantiated. */
e92cc029 3363
8d08fdba 3364tree
36a117a5 3365lookup_template_class (d1, arglist, in_decl, context, entering_scope)
8d08fdba
MS
3366 tree d1, arglist;
3367 tree in_decl;
e1467ff2 3368 tree context;
36a117a5 3369 int entering_scope;
8d08fdba 3370{
a703fb38 3371 tree template = NULL_TREE, parmlist;
dbfe2124 3372 tree t;
5566b478
MS
3373
3374 if (TREE_CODE (d1) == IDENTIFIER_NODE)
3375 {
f181d4ae
MM
3376 if (IDENTIFIER_VALUE (d1)
3377 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1)))
3378 template = IDENTIFIER_VALUE (d1);
73b0fce8
KL
3379 else
3380 {
2c73f9f5
ML
3381 if (context)
3382 push_decl_namespace (context);
67ffc812
MM
3383 if (current_class_type != NULL_TREE)
3384 template =
3385 maybe_get_template_decl_from_type_decl
3386 (IDENTIFIER_CLASS_VALUE (d1));
a2b60a0e 3387 if (template == NULL_TREE)
2c73f9f5
ML
3388 template = lookup_name_nonclass (d1);
3389 if (context)
3390 pop_decl_namespace ();
73b0fce8 3391 }
8d019cef
JM
3392 if (template)
3393 context = DECL_CONTEXT (template);
5566b478 3394 }
c91a56d2
MS
3395 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
3396 {
f3400fe2
JM
3397 if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1)))
3398 {
3399 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1));
3400 d1 = DECL_NAME (template);
3401 }
c91a56d2 3402 }
ed44da02
MM
3403 else if (TREE_CODE (d1) == ENUMERAL_TYPE
3404 || (TREE_CODE_CLASS (TREE_CODE (d1)) == 't'
3405 && IS_AGGR_TYPE (d1)))
5566b478 3406 {
ed44da02 3407 template = TYPE_TI_TEMPLATE (d1);
5566b478
MS
3408 d1 = DECL_NAME (template);
3409 }
93cdc044
JM
3410 else if (TREE_CODE (d1) == TEMPLATE_DECL
3411 && TREE_CODE (DECL_RESULT (d1)) == TYPE_DECL)
3412 {
3413 template = d1;
3414 d1 = DECL_NAME (template);
3415 context = DECL_CONTEXT (template);
3416 }
5566b478
MS
3417 else
3418 my_friendly_abort (272);
8d08fdba 3419
8d08fdba 3420 /* With something like `template <class T> class X class X { ... };'
f181d4ae
MM
3421 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
3422 We don't want to do that, but we have to deal with the situation,
3423 so let's give them some syntax errors to chew on instead of a
3424 crash. */
8d08fdba 3425 if (! template)
f3400fe2
JM
3426 {
3427 cp_error ("`%T' is not a template", d1);
3428 return error_mark_node;
3429 }
2c73f9f5 3430
c3baf4b5
JM
3431 if (context == NULL_TREE)
3432 context = global_namespace;
2c73f9f5 3433
8d08fdba
MS
3434 if (TREE_CODE (template) != TEMPLATE_DECL)
3435 {
8251199e 3436 cp_error ("non-template type `%T' used as a template", d1);
8d08fdba 3437 if (in_decl)
8251199e 3438 cp_error_at ("for template declaration `%D'", in_decl);
8d08fdba
MS
3439 return error_mark_node;
3440 }
8d08fdba 3441
73b0fce8
KL
3442 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3443 {
3444 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3445 template arguments */
3446
3447 tree parm = copy_template_template_parm (TREE_TYPE (template));
3448 tree template2 = TYPE_STUB_DECL (parm);
3449 tree arglist2;
3450
73b0fce8
KL
3451 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
3452
744fac59 3453 arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1);
73b0fce8
KL
3454 if (arglist2 == error_mark_node)
3455 return error_mark_node;
3456
3457 arglist2 = copy_to_permanent (arglist2);
7ddedda4 3458 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm)
73b0fce8
KL
3459 = perm_tree_cons (template2, arglist2, NULL_TREE);
3460 TYPE_SIZE (parm) = 0;
3461 return parm;
3462 }
36a117a5 3463 else
8d08fdba 3464 {
36a117a5
MM
3465 tree template_type = TREE_TYPE (template);
3466 tree type_decl;
3467 tree found = NULL_TREE;
3468 int arg_depth;
3469 int parm_depth;
dbfe2124 3470 int is_partial_instantiation;
830bfa74 3471
36a117a5
MM
3472 template = most_general_template (template);
3473 parmlist = DECL_TEMPLATE_PARMS (template);
3474 parm_depth = TMPL_PARMS_DEPTH (parmlist);
3475 arg_depth = TMPL_ARGS_DEPTH (arglist);
3476
7ddedda4
MM
3477 /* We build up the coerced arguments and such on the
3478 momentary_obstack. */
3479 push_momentary ();
3480
36a117a5
MM
3481 if (arg_depth == 1 && parm_depth > 1)
3482 {
39c01e4c 3483 /* We've been given an incomplete set of template arguments.
36a117a5
MM
3484 For example, given:
3485
3486 template <class T> struct S1 {
3487 template <class U> struct S2 {};
3488 template <class U> struct S2<U*> {};
3489 };
3490
3491 we will be called with an ARGLIST of `U*', but the
3492 TEMPLATE will be `template <class T> template
3493 <class U> struct S1<T>::S2'. We must fill in the missing
3494 arguments. */
3495 my_friendly_assert (context != NULL_TREE, 0);
39c01e4c
MM
3496 while (!IS_AGGR_TYPE_CODE (TREE_CODE (context))
3497 && context != global_namespace)
36a117a5 3498 context = DECL_REAL_CONTEXT (context);
39c01e4c
MM
3499
3500 if (context == global_namespace)
3501 /* This is bad. We cannot get enough arguments, even from
3502 the surrounding context, to resolve this class. One
3503 case where this might happen is (illegal) code like:
3504
3505 template <class U>
3506 template <class T>
3507 struct S {
3508 A(const A<T>& a) {}
3509 };
3510
3511 We should catch this error sooner (at the opening curly
3512 for `S', but it is better to be safe than sorry here. */
3513 {
8251199e 3514 cp_error ("invalid use of `%D'", template);
39c01e4c
MM
3515 return error_mark_node;
3516 }
3517
ed44da02 3518 arglist = add_to_template_args (TYPE_TI_ARGS (context),
36a117a5
MM
3519 arglist);
3520 arg_depth = TMPL_ARGS_DEPTH (arglist);
3521 }
5566b478 3522
36a117a5
MM
3523 my_friendly_assert (parm_depth == arg_depth, 0);
3524
3525 /* Calculate the BOUND_ARGS. These will be the args that are
3526 actually tsubst'd into the definition to create the
3527 instantiation. */
3528 if (parm_depth > 1)
830bfa74
MM
3529 {
3530 /* We have multiple levels of arguments to coerce, at once. */
830bfa74 3531 int i;
e4a84209 3532 int saved_depth = TMPL_ARGS_DEPTH (arglist);
36a117a5 3533
cabc336a 3534 tree bound_args = make_temp_vec (parm_depth);
830bfa74 3535
e4a84209 3536 for (i = saved_depth,
830bfa74 3537 t = DECL_TEMPLATE_PARMS (template);
e4a84209 3538 i > 0 && t != NULL_TREE;
830bfa74 3539 --i, t = TREE_CHAIN (t))
e4a84209
MM
3540 {
3541 tree a = coerce_template_parms (TREE_VALUE (t),
3542 arglist, template, 1, 1);
3543 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
3544
3545 /* We temporarily reduce the length of the ARGLIST so
3546 that coerce_template_parms will see only the arguments
3547 corresponding to the template parameters it is
3548 examining. */
3549 TREE_VEC_LENGTH (arglist)--;
3550 }
3551
3552 /* Restore the ARGLIST to its full size. */
3553 TREE_VEC_LENGTH (arglist) = saved_depth;
3554
36a117a5 3555 arglist = bound_args;
830bfa74
MM
3556 }
3557 else
36a117a5
MM
3558 arglist
3559 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
3560 innermost_args (arglist),
3561 template, 1, 1);
3562
3563 if (arglist == error_mark_node)
3564 /* We were unable to bind the arguments. */
5566b478 3565 return error_mark_node;
5566b478 3566
36a117a5
MM
3567 /* In the scope of a template class, explicit references to the
3568 template class refer to the type of the template, not any
3569 instantiation of it. For example, in:
3570
3571 template <class T> class C { void f(C<T>); }
3572
3573 the `C<T>' is just the same as `C'. Outside of the
3574 class, however, such a reference is an instantiation. */
ed44da02 3575 if (comp_template_args (TYPE_TI_ARGS (template_type),
36a117a5
MM
3576 arglist))
3577 {
3578 found = template_type;
3579
3580 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
5566b478 3581 {
36a117a5
MM
3582 tree ctx;
3583
3584 /* Note that we use DECL_CONTEXT, rather than
3585 CP_DECL_CONTEXT, so that the termination test is
3586 always just `ctx'. We're not interested in namepace
3587 scopes. */
3588 for (ctx = current_class_type;
3589 ctx;
3590 ctx = (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't')
3591 ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx))
3bfdc719 3592 if (same_type_p (ctx, template_type))
36a117a5
MM
3593 break;
3594
3595 if (!ctx)
3596 /* We're not in the scope of the class, so the
3597 TEMPLATE_TYPE is not the type we want after
3598 all. */
3599 found = NULL_TREE;
5566b478
MS
3600 }
3601 }
36a117a5
MM
3602
3603 if (!found)
75650646 3604 {
36a117a5
MM
3605 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
3606 found; found = TREE_CHAIN (found))
3607 if (comp_template_args (TREE_PURPOSE (found), arglist))
3608 break;
75650646 3609
36a117a5
MM
3610 if (found)
3611 found = TREE_VALUE (found);
8d08fdba 3612 }
36a117a5
MM
3613
3614 if (found)
8d08fdba 3615 {
7ddedda4 3616 pop_momentary ();
36a117a5 3617 return found;
8d08fdba 3618 }
5566b478 3619
36a117a5
MM
3620 /* Since we didn't find the type, we'll have to create it.
3621 Since we'll be saving this type on the
3622 DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */
3623 push_obstacks (&permanent_obstack, &permanent_obstack);
3624
dbfe2124 3625 /* This type is a "partial instantiation" if any of the template
486e4077
MM
3626 arguments still inolve template parameters. Note that we set
3627 IS_PARTIAL_INSTANTIATION for partial specializations as
3628 well. */
dbfe2124
MM
3629 is_partial_instantiation = uses_template_parms (arglist);
3630
36a117a5 3631 /* Create the type. */
ed44da02
MM
3632 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
3633 {
dbfe2124 3634 if (!is_partial_instantiation)
61a127b3 3635 t = start_enum (TYPE_IDENTIFIER (template_type));
ed44da02 3636 else
dbfe2124 3637 /* We don't want to call start_enum for this type, since
ed44da02
MM
3638 the values for the enumeration constants may involve
3639 template parameters. And, no one should be interested
3640 in the enumeration constants for such a type. */
3641 t = make_node (ENUMERAL_TYPE);
3642 }
3643 else
3644 {
3645 t = make_lang_type (TREE_CODE (template_type));
3646 CLASSTYPE_DECLARED_CLASS (t)
3647 = CLASSTYPE_DECLARED_CLASS (template_type);
3648 CLASSTYPE_GOT_SEMICOLON (t) = 1;
3649 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
f668f160 3650 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
ed44da02
MM
3651 }
3652
61a127b3 3653 /* If we called start_enum above, this information will already
ed44da02
MM
3654 be set up. */
3655 if (!TYPE_NAME (t))
3656 {
3657 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
36a117a5 3658
ed44da02
MM
3659 /* Create a stub TYPE_DECL for it. */
3660 type_decl = build_decl (TYPE_DECL, DECL_NAME (template), t);
3661 SET_DECL_ARTIFICIAL (type_decl);
3662 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
3663 DECL_SOURCE_FILE (type_decl)
3664 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type));
3665 DECL_SOURCE_LINE (type_decl)
3666 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type));
3667 TYPE_STUB_DECL (t) = TYPE_NAME (t) = type_decl;
3668 }
3669 else
3670 type_decl = TYPE_NAME (t);
36a117a5 3671
9fbf56f7
MM
3672 /* Set up the template information. We have to figure out which
3673 template is the immediate parent if this is a full
3674 instantiation. */
3675 if (parm_depth == 1 || is_partial_instantiation
3676 || !PRIMARY_TEMPLATE_P (template))
3677 /* This case is easy; there are no member templates involved. */
3678 found = template;
3679 else
3680 {
3681 /* This is a full instantiation of a member template. There
3682 should be some partial instantiation of which this is an
3683 instance. */
3684
3685 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
3686 found; found = TREE_CHAIN (found))
3687 {
3688 int success;
3689 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
3690
3691 /* We only want partial instantiations, here, not
3692 specializations or full instantiations. */
3693 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
3694 || !uses_template_parms (TREE_VALUE (found)))
3695 continue;
3696
3697 /* Temporarily reduce by one the number of levels in the
3698 ARGLIST and in FOUND so as to avoid comparing the
3699 last set of arguments. */
3700 TREE_VEC_LENGTH (arglist)--;
3701 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
3702
3703 /* See if the arguments match. If they do, then TMPL is
3704 the partial instantiation we want. */
3705 success = comp_template_args (TREE_PURPOSE (found), arglist);
3706
3707 /* Restore the argument vectors to their full size. */
3708 TREE_VEC_LENGTH (arglist)++;
3709 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
3710
3711 if (success)
3712 {
3713 found = tmpl;
3714 break;
3715 }
3716 }
3717
3718 if (!found)
3719 my_friendly_abort (0);
3720 }
3721
5566b478 3722 arglist = copy_to_permanent (arglist);
ed44da02 3723 SET_TYPE_TEMPLATE_INFO (t,
9fbf56f7 3724 tree_cons (found, arglist, NULL_TREE));
dbfe2124
MM
3725 DECL_TEMPLATE_INSTANTIATIONS (template)
3726 = tree_cons (arglist, t,
3727 DECL_TEMPLATE_INSTANTIATIONS (template));
3728
3729 if (TREE_CODE (t) == ENUMERAL_TYPE
3730 && !is_partial_instantiation)
61a127b3
MM
3731 /* Now that the type has been registered on the instantiations
3732 list, we set up the enumerators. Because the enumeration
3733 constants may involve the enumeration type itself, we make
3734 sure to register the type first, and then create the
3735 constants. That way, doing tsubst_expr for the enumeration
3736 constants won't result in recursive calls here; we'll find
3737 the instantiation and exit above. */
3738 tsubst_enum (template_type, t, arglist);
dbfe2124
MM
3739
3740 /* We're done with the permanent obstack, now. */
3741 pop_obstacks ();
7ddedda4
MM
3742 /* We're also done with the momentary allocation we started
3743 above. */
3744 pop_momentary ();
5566b478 3745
36a117a5
MM
3746 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
3747 is set up. */
ed44da02
MM
3748 if (TREE_CODE (t) != ENUMERAL_TYPE)
3749 DECL_NAME (type_decl) = classtype_mangled_name (t);
36a117a5 3750 DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl);
dbfe2124 3751 if (!is_partial_instantiation)
36a117a5
MM
3752 {
3753 DECL_ASSEMBLER_NAME (type_decl)
3754 = get_identifier (build_overload_name (t, 1, 1));
50a6dbd7
JM
3755
3756 /* For backwards compatibility; code that uses
3757 -fexternal-templates expects looking up a template to
3758 instantiate it. I think DDD still relies on this.
3759 (jason 8/20/1998) */
ed44da02
MM
3760 if (TREE_CODE (t) != ENUMERAL_TYPE
3761 && flag_external_templates
36a117a5
MM
3762 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
3763 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
3764 add_pending_template (t);
3765 }
3766 else
077e7015
MM
3767 /* If the type makes use of template parameters, the
3768 code that generates debugging information will crash. */
3769 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
8d08fdba 3770
36a117a5
MM
3771 return t;
3772 }
8d08fdba
MS
3773}
3774\f
050367a3
MM
3775/* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
3776 TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
3777 If FN returns non-zero, the iteration is terminated, and
3778 for_each_template_parm returns 1. Otherwise, the iteration
3779 continues. If FN never returns a non-zero value, the value
3780 returned by for_each_template_parm is 0. If FN is NULL, it is
3781 considered to be the function which always returns 1. */
3782
8d08fdba 3783int
050367a3 3784for_each_template_parm (t, fn, data)
8d08fdba 3785 tree t;
050367a3
MM
3786 tree_fn_t fn;
3787 void* data;
8d08fdba
MS
3788{
3789 if (!t)
3790 return 0;
581d38d0
MM
3791
3792 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't'
3793 && for_each_template_parm (TYPE_CONTEXT (t), fn, data))
3794 return 1;
3795
8d08fdba
MS
3796 switch (TREE_CODE (t))
3797 {
3798 case INDIRECT_REF:
3799 case COMPONENT_REF:
3800 /* We assume that the object must be instantiated in order to build
3801 the COMPONENT_REF, so we test only whether the type of the
3802 COMPONENT_REF uses template parms. */
050367a3 3803 return for_each_template_parm (TREE_TYPE (t), fn, data);
8d08fdba
MS
3804
3805 case IDENTIFIER_NODE:
3806 if (!IDENTIFIER_TEMPLATE (t))
3807 return 0;
5566b478 3808 my_friendly_abort (42);
8d08fdba
MS
3809
3810 /* aggregates of tree nodes */
3811 case TREE_VEC:
3812 {
3813 int i = TREE_VEC_LENGTH (t);
3814 while (i--)
050367a3 3815 if (for_each_template_parm (TREE_VEC_ELT (t, i), fn, data))
8d08fdba
MS
3816 return 1;
3817 return 0;
3818 }
3819 case TREE_LIST:
050367a3
MM
3820 if (for_each_template_parm (TREE_PURPOSE (t), fn, data)
3821 || for_each_template_parm (TREE_VALUE (t), fn, data))
8d08fdba 3822 return 1;
050367a3 3823 return for_each_template_parm (TREE_CHAIN (t), fn, data);
8d08fdba 3824
2c73f9f5
ML
3825 case OVERLOAD:
3826 if (for_each_template_parm (OVL_FUNCTION (t), fn, data))
3827 return 1;
3828 return for_each_template_parm (OVL_CHAIN (t), fn, data);
3829
8d08fdba
MS
3830 /* constructed type nodes */
3831 case POINTER_TYPE:
3832 case REFERENCE_TYPE:
050367a3 3833 return for_each_template_parm (TREE_TYPE (t), fn, data);
ed44da02 3834
8d08fdba 3835 case RECORD_TYPE:
b7484fbe 3836 if (TYPE_PTRMEMFUNC_FLAG (t))
050367a3
MM
3837 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t),
3838 fn, data);
ed44da02
MM
3839 /* Fall through. */
3840
8d08fdba 3841 case UNION_TYPE:
ed44da02
MM
3842 case ENUMERAL_TYPE:
3843 if (! TYPE_TEMPLATE_INFO (t))
8d08fdba 3844 return 0;
050367a3 3845 return for_each_template_parm (TREE_VALUE
ed44da02 3846 (TYPE_TEMPLATE_INFO (t)),
050367a3 3847 fn, data);
588c2d1c
MM
3848 case METHOD_TYPE:
3849 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data))
8d08fdba 3850 return 1;
588c2d1c
MM
3851 /* Fall through. */
3852
3853 case FUNCTION_TYPE:
3854 /* Check the parameter types. Since default arguments are not
3855 instantiated until they are needed, the TYPE_ARG_TYPES may
3856 contain expressions that involve template parameters. But,
3857 no-one should be looking at them yet. And, once they're
3858 instantiated, they don't contain template parameters, so
3859 there's no point in looking at them then, either. */
3860 {
3861 tree parm;
3862
3863 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
3864 if (for_each_template_parm (TREE_VALUE (parm), fn, data))
3865 return 1;
3866 }
3867
3868 /* Check the return type, too. */
050367a3 3869 return for_each_template_parm (TREE_TYPE (t), fn, data);
588c2d1c 3870
8d08fdba 3871 case ARRAY_TYPE:
050367a3 3872 if (for_each_template_parm (TYPE_DOMAIN (t), fn, data))
8d08fdba 3873 return 1;
050367a3 3874 return for_each_template_parm (TREE_TYPE (t), fn, data);
8d08fdba 3875 case OFFSET_TYPE:
050367a3 3876 if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t), fn, data))
8d08fdba 3877 return 1;
050367a3 3878 return for_each_template_parm (TREE_TYPE (t), fn, data);
8d08fdba
MS
3879
3880 /* decl nodes */
3881 case TYPE_DECL:
050367a3 3882 return for_each_template_parm (TREE_TYPE (t), fn, data);
5566b478 3883
73b0fce8
KL
3884 case TEMPLATE_DECL:
3885 /* A template template parameter is encountered */
3886 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
744fac59
KL
3887 return for_each_template_parm (TREE_TYPE (t), fn, data);
3888 /* Already substituted template template parameter */
73b0fce8
KL
3889 return 0;
3890
3ac3d9ea 3891 case CONST_DECL:
050367a3 3892 if (for_each_template_parm (DECL_INITIAL (t), fn, data))
3ac3d9ea
MM
3893 return 1;
3894 goto check_type_and_context;
3895
8d08fdba 3896 case FUNCTION_DECL:
5566b478
MS
3897 case VAR_DECL:
3898 /* ??? What about FIELD_DECLs? */
3899 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
050367a3 3900 && for_each_template_parm (DECL_TI_ARGS (t), fn, data))
8d08fdba
MS
3901 return 1;
3902 /* fall through */
8d08fdba 3903 case PARM_DECL:
3ac3d9ea 3904 check_type_and_context:
050367a3 3905 if (for_each_template_parm (TREE_TYPE (t), fn, data))
5566b478 3906 return 1;
050367a3
MM
3907 if (DECL_CONTEXT (t)
3908 && for_each_template_parm (DECL_CONTEXT (t), fn, data))
8d08fdba 3909 return 1;
8d08fdba
MS
3910 return 0;
3911
3912 case CALL_EXPR:
050367a3 3913 return for_each_template_parm (TREE_TYPE (t), fn, data);
8d08fdba 3914 case ADDR_EXPR:
050367a3 3915 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
8d08fdba
MS
3916
3917 /* template parm nodes */
73b0fce8 3918 case TEMPLATE_TEMPLATE_PARM:
744fac59 3919 /* Record template parameters such as `T' inside `TT<T>'. */
7ddedda4
MM
3920 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)
3921 && for_each_template_parm (TYPE_TI_ARGS (t), fn, data))
744fac59
KL
3922 return 1;
3923 case TEMPLATE_TYPE_PARM:
f84b4be9 3924 case TEMPLATE_PARM_INDEX:
050367a3
MM
3925 if (fn)
3926 return (*fn)(t, data);
3927 else
3928 return 1;
8d08fdba
MS
3929
3930 /* simple type nodes */
3931 case INTEGER_TYPE:
050367a3 3932 if (for_each_template_parm (TYPE_MIN_VALUE (t), fn, data))
8d08fdba 3933 return 1;
050367a3 3934 return for_each_template_parm (TYPE_MAX_VALUE (t), fn, data);
8d08fdba
MS
3935
3936 case REAL_TYPE:
37c46b43 3937 case COMPLEX_TYPE:
8d08fdba 3938 case VOID_TYPE:
2986ae00 3939 case BOOLEAN_TYPE:
2c73f9f5 3940 case NAMESPACE_DECL:
8d08fdba
MS
3941 return 0;
3942
3943 /* constants */
3944 case INTEGER_CST:
3945 case REAL_CST:
3946 case STRING_CST:
3947 return 0;
3948
3949 case ERROR_MARK:
3950 /* Non-error_mark_node ERROR_MARKs are bad things. */
3951 my_friendly_assert (t == error_mark_node, 274);
3952 /* NOTREACHED */
3953 return 0;
3954
ec255269 3955 case LOOKUP_EXPR:
5566b478 3956 case TYPENAME_TYPE:
8d08fdba
MS
3957 return 1;
3958
61a127b3
MM
3959 case PTRMEM_CST:
3960 return for_each_template_parm (TREE_TYPE (t), fn, data);
3961
5156628f 3962 case SCOPE_REF:
050367a3 3963 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
5156628f 3964
db5ae43f
MS
3965 case CONSTRUCTOR:
3966 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
050367a3
MM
3967 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
3968 (TREE_TYPE (t)), fn, data);
3969 return for_each_template_parm (TREE_OPERAND (t, 1), fn, data);
db5ae43f 3970
42976354
BK
3971 case MODOP_EXPR:
3972 case CAST_EXPR:
3973 case REINTERPRET_CAST_EXPR:
3974 case CONST_CAST_EXPR:
3975 case STATIC_CAST_EXPR:
3976 case DYNAMIC_CAST_EXPR:
42976354
BK
3977 case ARROW_EXPR:
3978 case DOTSTAR_EXPR:
3979 case TYPEID_EXPR:
3980 return 1;
3981
abff8e06
JM
3982 case SIZEOF_EXPR:
3983 case ALIGNOF_EXPR:
050367a3 3984 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
abff8e06 3985
8d08fdba
MS
3986 default:
3987 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3988 {
3989 case '1':
3990 case '2':
ec255269 3991 case 'e':
8d08fdba
MS
3992 case '<':
3993 {
3994 int i;
bbc06b4b 3995 for (i = first_rtl_op (TREE_CODE (t)); --i >= 0;)
050367a3 3996 if (for_each_template_parm (TREE_OPERAND (t, i), fn, data))
8d08fdba
MS
3997 return 1;
3998 return 0;
3999 }
4000 default:
4001 break;
4002 }
4003 sorry ("testing %s for template parms",
4004 tree_code_name [(int) TREE_CODE (t)]);
4005 my_friendly_abort (82);
4006 /* NOTREACHED */
4007 return 0;
4008 }
4009}
4010
050367a3
MM
4011int
4012uses_template_parms (t)
4013 tree t;
4014{
4015 return for_each_template_parm (t, 0, 0);
4016}
4017
27fafc8d
JM
4018static struct tinst_level *current_tinst_level;
4019static struct tinst_level *free_tinst_level;
4020static int tinst_depth;
e9f32eb5 4021extern int max_tinst_depth;
5566b478 4022#ifdef GATHER_STATISTICS
27fafc8d 4023int depth_reached;
5566b478 4024#endif
27fafc8d
JM
4025int tinst_level_tick;
4026int last_template_error_tick;
8d08fdba 4027
7bae46f4 4028/* Print out all the template instantiations that we are currently
27fafc8d
JM
4029 working on. If ERR, we are being called from cp_thing, so do
4030 the right thing for an error message. */
7bae46f4 4031
27fafc8d
JM
4032static void
4033print_template_context (err)
4034 int err;
7bae46f4
JM
4035{
4036 struct tinst_level *p = current_tinst_level;
4037 int line = lineno;
4038 char *file = input_filename;
4039
49e04385 4040 if (err && p)
27fafc8d 4041 {
49e04385
MM
4042 if (current_function_decl != p->decl
4043 && current_function_decl != NULL_TREE)
4044 /* We can get here during the processing of some synthesized
4045 method. Then, p->decl will be the function that's causing
4046 the synthesis. */
4047 ;
27fafc8d 4048 else
27fafc8d 4049 {
49e04385
MM
4050 if (current_function_decl == p->decl)
4051 /* Avoid redundancy with the the "In function" line. */;
4052 else
4053 fprintf (stderr, "%s: In instantiation of `%s':\n",
4054 file, decl_as_string (p->decl, 0));
4055
038fb86f
JM
4056 line = p->line;
4057 file = p->file;
27fafc8d
JM
4058 p = p->next;
4059 }
4060 }
4061
7bae46f4
JM
4062 for (; p; p = p->next)
4063 {
038fb86f
JM
4064 fprintf (stderr, "%s:%d: instantiated from `%s'\n", file, line,
4065 decl_as_string (p->decl, 0));
4066 line = p->line;
4067 file = p->file;
7bae46f4 4068 }
038fb86f 4069 fprintf (stderr, "%s:%d: instantiated from here\n", file, line);
7bae46f4
JM
4070}
4071
27fafc8d
JM
4072/* Called from cp_thing to print the template context for an error. */
4073
4074void
4075maybe_print_template_context ()
4076{
4077 if (last_template_error_tick == tinst_level_tick
4078 || current_tinst_level == 0)
4079 return;
4080
4081 last_template_error_tick = tinst_level_tick;
4082 print_template_context (1);
4083}
4084
bd6dd845 4085static int
5566b478
MS
4086push_tinst_level (d)
4087 tree d;
8d08fdba
MS
4088{
4089 struct tinst_level *new;
8d08fdba 4090
7215f9a0
MS
4091 if (tinst_depth >= max_tinst_depth)
4092 {
8adf5b5e
JM
4093 /* If the instantiation in question still has unbound template parms,
4094 we don't really care if we can't instantiate it, so just return.
4095 This happens with base instantiation for implicit `typename'. */
4096 if (uses_template_parms (d))
4097 return 0;
4098
1139b3d8 4099 last_template_error_tick = tinst_level_tick;
8251199e 4100 error ("template instantiation depth exceeds maximum of %d",
7215f9a0 4101 max_tinst_depth);
8251199e
JM
4102 error (" (use -ftemplate-depth-NN to increase the maximum)");
4103 cp_error (" instantiating `%D'", d);
5566b478 4104
27fafc8d 4105 print_template_context (0);
5566b478 4106
7215f9a0
MS
4107 return 0;
4108 }
4109
8d08fdba
MS
4110 if (free_tinst_level)
4111 {
4112 new = free_tinst_level;
4113 free_tinst_level = new->next;
4114 }
4115 else
4116 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
4117
5566b478
MS
4118 new->decl = d;
4119 new->line = lineno;
4120 new->file = input_filename;
8d08fdba
MS
4121 new->next = current_tinst_level;
4122 current_tinst_level = new;
5566b478 4123
7215f9a0 4124 ++tinst_depth;
5566b478
MS
4125#ifdef GATHER_STATISTICS
4126 if (tinst_depth > depth_reached)
4127 depth_reached = tinst_depth;
4128#endif
4129
27fafc8d 4130 ++tinst_level_tick;
7215f9a0 4131 return 1;
8d08fdba
MS
4132}
4133
4134void
4135pop_tinst_level ()
4136{
4137 struct tinst_level *old = current_tinst_level;
4138
ae58fa02
MM
4139 /* Restore the filename and line number stashed away when we started
4140 this instantiation. */
4141 lineno = old->line;
4142 input_filename = old->file;
5f2c99c4 4143 extract_interface_info ();
ae58fa02 4144
8d08fdba
MS
4145 current_tinst_level = old->next;
4146 old->next = free_tinst_level;
4147 free_tinst_level = old;
7215f9a0 4148 --tinst_depth;
27fafc8d 4149 ++tinst_level_tick;
8d08fdba
MS
4150}
4151
4152struct tinst_level *
4153tinst_for_decl ()
4154{
4155 struct tinst_level *p = current_tinst_level;
4156
4157 if (p)
4158 for (; p->next ; p = p->next )
4159 ;
4160 return p;
4161}
4162
f84b4be9
JM
4163/* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4164 vector of template arguments, as for tsubst.
4165
4166 Returns an appropriate tsbust'd friend declaration. */
4167
4168static tree
4169tsubst_friend_function (decl, args)
4170 tree decl;
4171 tree args;
4172{
4173 tree new_friend;
27fafc8d
JM
4174 int line = lineno;
4175 char *file = input_filename;
4176
4177 lineno = DECL_SOURCE_LINE (decl);
4178 input_filename = DECL_SOURCE_FILE (decl);
4179
f84b4be9
JM
4180 if (TREE_CODE (decl) == FUNCTION_DECL
4181 && DECL_TEMPLATE_INSTANTIATION (decl)
4182 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
4183 /* This was a friend declared with an explicit template
4184 argument list, e.g.:
4185
4186 friend void f<>(T);
4187
4188 to indicate that f was a template instantiation, not a new
4189 function declaration. Now, we have to figure out what
4190 instantiation of what template. */
4191 {
4192 tree template_id;
4193 tree new_args;
4194 tree tmpl;
f84b4be9
JM
4195
4196 template_id
4197 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl),
f7d98d58 4198 args, NULL_TREE),
f84b4be9 4199 tsubst (DECL_TI_ARGS (decl),
f7d98d58 4200 args, NULL_TREE));
39c01e4c
MM
4201 /* FIXME: The decl we create via the next tsubst could be
4202 created on a temporary obstack. */
f7d98d58 4203 new_friend = tsubst (decl, args, NULL_TREE);
36a117a5
MM
4204 tmpl = determine_specialization (template_id, new_friend,
4205 &new_args,
4206 /*need_member_template=*/0,
4207 /*complain=*/1);
27fafc8d
JM
4208 new_friend = instantiate_template (tmpl, new_args);
4209 goto done;
f84b4be9 4210 }
36a117a5
MM
4211
4212 new_friend = tsubst (decl, args, NULL_TREE);
f84b4be9 4213
36a117a5 4214 /* The NEW_FRIEND will look like an instantiation, to the
f84b4be9
JM
4215 compiler, but is not an instantiation from the point of view of
4216 the language. For example, we might have had:
4217
4218 template <class T> struct S {
4219 template <class U> friend void f(T, U);
4220 };
4221
4222 Then, in S<int>, template <class U> void f(int, U) is not an
4223 instantiation of anything. */
4224 DECL_USE_TEMPLATE (new_friend) = 0;
4225 if (TREE_CODE (decl) == TEMPLATE_DECL)
4226 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
36a117a5
MM
4227
4228 /* The mangled name for the NEW_FRIEND is incorrect. The call to
4229 tsubst will have resulted in a call to
4230 set_mangled_name_for_template_decl. But, the function is not a
4231 template instantiation and should not be mangled like one.
4232 Therefore, we remangle the function name. We don't have to do
4233 this if the NEW_FRIEND is a template since
4234 set_mangled_name_for_template_decl doesn't do anything if the
4235 function declaration still uses template arguments. */
4236 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
4237 {
4238 set_mangled_name_for_decl (new_friend);
4239 DECL_RTL (new_friend) = 0;
4240 make_decl_rtl (new_friend, NULL_PTR, 1);
4241 }
4242
6eb3bb27 4243 if (DECL_NAMESPACE_SCOPE_P (new_friend))
f84b4be9 4244 {
36a117a5 4245 tree old_decl;
fbf1c34b
MM
4246 tree new_friend_template_info;
4247 tree new_friend_result_template_info;
4248 int new_friend_is_defn;
4249
4250 /* We must save some information from NEW_FRIEND before calling
4251 duplicate decls since that function will free NEW_FRIEND if
4252 possible. */
4253 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
f84b4be9 4254 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
fbf1c34b
MM
4255 {
4256 /* This declaration is a `primary' template. */
4257 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
4258
4259 new_friend_is_defn
4260 = DECL_INITIAL (DECL_RESULT (new_friend)) != NULL_TREE;
4261 new_friend_result_template_info
4262 = DECL_TEMPLATE_INFO (DECL_RESULT (new_friend));
4263 }
4264 else
4265 {
4266 new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE;
4267 new_friend_result_template_info = NULL_TREE;
4268 }
36a117a5 4269
36a117a5
MM
4270 old_decl = pushdecl_namespace_level (new_friend);
4271
4272 if (old_decl != new_friend)
4273 {
4274 /* This new friend declaration matched an existing
4275 declaration. For example, given:
4276
4277 template <class T> void f(T);
4278 template <class U> class C {
4279 template <class T> friend void f(T) {}
4280 };
4281
4282 the friend declaration actually provides the definition
4283 of `f', once C has been instantiated for some type. So,
4284 old_decl will be the out-of-class template declaration,
4285 while new_friend is the in-class definition.
4286
4287 But, if `f' was called before this point, the
4288 instantiation of `f' will have DECL_TI_ARGS corresponding
4289 to `T' but not to `U', references to which might appear
4290 in the definition of `f'. Previously, the most general
4291 template for an instantiation of `f' was the out-of-class
4292 version; now it is the in-class version. Therefore, we
4293 run through all specialization of `f', adding to their
4294 DECL_TI_ARGS appropriately. In particular, they need a
4295 new set of outer arguments, corresponding to the
4296 arguments for this class instantiation.
4297
4298 The same situation can arise with something like this:
4299
4300 friend void f(int);
4301 template <class T> class C {
4302 friend void f(T) {}
4303 };
4304
4305 when `C<int>' is instantiated. Now, `f(int)' is defined
4306 in the class. */
4307
fbf1c34b
MM
4308 if (!new_friend_is_defn)
4309 /* On the other hand, if the in-class declaration does
4310 *not* provide a definition, then we don't want to alter
4311 existing definitions. We can just leave everything
4312 alone. */
36a117a5 4313 ;
fbf1c34b 4314 else
36a117a5 4315 {
fbf1c34b
MM
4316 /* Overwrite whatever template info was there before, if
4317 any, with the new template information pertaining to
4318 the declaration. */
4319 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
4320
4321 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
4322 /* duplicate_decls will take care of this case. */
4323 ;
4324 else
36a117a5 4325 {
fbf1c34b
MM
4326 tree t;
4327 tree new_friend_args;
4328
4329 DECL_TEMPLATE_INFO (DECL_RESULT (old_decl))
4330 = new_friend_result_template_info;
4331
4332 new_friend_args = TI_ARGS (new_friend_template_info);
4333 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
4334 t != NULL_TREE;
4335 t = TREE_CHAIN (t))
4336 {
4337 tree spec = TREE_VALUE (t);
36a117a5 4338
fbf1c34b
MM
4339 DECL_TI_ARGS (spec)
4340 = add_outermost_template_args (new_friend_args,
4341 DECL_TI_ARGS (spec));
4342 DECL_TI_ARGS (spec)
4343 = copy_to_permanent (DECL_TI_ARGS (spec));
4344 }
4345
4346 /* Now, since specializations are always supposed to
4347 hang off of the most general template, we must move
4348 them. */
4349 t = most_general_template (old_decl);
4350 if (t != old_decl)
4351 {
4352 DECL_TEMPLATE_SPECIALIZATIONS (t)
4353 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
4354 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
4355 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
4356 }
36a117a5
MM
4357 }
4358 }
4359
4360 /* The information from NEW_FRIEND has been merged into OLD_DECL
4361 by duplicate_decls. */
4362 new_friend = old_decl;
4363 }
f84b4be9
JM
4364 }
4365 else if (TYPE_SIZE (DECL_CONTEXT (new_friend)))
4366 {
4367 /* Check to see that the declaration is really present, and,
4368 possibly obtain an improved declaration. */
4369 tree fn = check_classfn (DECL_CONTEXT (new_friend),
4370 new_friend);
4371
4372 if (fn)
4373 new_friend = fn;
4374 }
4375
27fafc8d
JM
4376 done:
4377 lineno = line;
4378 input_filename = file;
f84b4be9
JM
4379 return new_friend;
4380}
4381
1aed5355
MM
4382/* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4383 template arguments, as for tsubst.
6757edfe
MM
4384
4385 Returns an appropriate tsbust'd friend type. */
4386
4387static tree
1aed5355
MM
4388tsubst_friend_class (friend_tmpl, args)
4389 tree friend_tmpl;
6757edfe
MM
4390 tree args;
4391{
1aed5355 4392 tree friend_type;
f9c3243c 4393 tree tmpl = lookup_name (DECL_NAME (friend_tmpl), 1);
6757edfe
MM
4394
4395 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4396
4397 if (tmpl != NULL_TREE && DECL_CLASS_TEMPLATE_P (tmpl))
4398 {
4399 /* The friend template has already been declared. Just
36a117a5
MM
4400 check to see that the declarations match, and install any new
4401 default parameters. We must tsubst the default parameters,
4402 of course. We only need the innermost template parameters
4403 because that is all that redeclare_class_template will look
4404 at. */
4405 tree parms
4406 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
4407 args);
4408 redeclare_class_template (TREE_TYPE (tmpl), parms);
6757edfe
MM
4409 friend_type = TREE_TYPE (tmpl);
4410 }
4411 else
4412 {
4413 /* The friend template has not already been declared. In this
4414 case, the instantiation of the template class will cause the
4415 injection of this template into the global scope. */
f9c3243c 4416 tmpl = tsubst (friend_tmpl, args, NULL_TREE);
6757edfe
MM
4417
4418 /* The new TMPL is not an instantiation of anything, so we
4419 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4420 the new type because that is supposed to be the corresponding
4421 template decl, i.e., TMPL. */
4422 DECL_USE_TEMPLATE (tmpl) = 0;
4423 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
4424 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
4425
4426 /* Inject this template into the global scope. */
4427 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
4428 }
4429
4430 return friend_type;
4431}
f84b4be9 4432
8d08fdba 4433tree
5566b478
MS
4434instantiate_class_template (type)
4435 tree type;
8d08fdba 4436{
61a127b3 4437 tree template, args, pattern, t;
36a117a5 4438 tree typedecl;
8d08fdba 4439
5566b478 4440 if (type == error_mark_node)
8d08fdba
MS
4441 return error_mark_node;
4442
5566b478
MS
4443 if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
4444 return type;
4445
7ddedda4
MM
4446 /* We want to allocate temporary vectors of template arguments and
4447 template argument expressions on the momentary obstack, not on
4448 the expression obstack. Otherwise, all the space allocated in
4449 argument coercion and such is simply lost. */
4450 push_momentary ();
4451
6bdb985f 4452 /* Figure out which template is being instantiated. */
36a117a5 4453 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5566b478 4454 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
73aad9b9 4455
6bdb985f
MM
4456 /* Figure out which arguments are being used to do the
4457 instantiation. */
4458 args = CLASSTYPE_TI_ARGS (type);
4c571114 4459 PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args);
6bdb985f 4460
4c571114
MM
4461 if (pedantic && PARTIAL_INSTANTIATION_P (type))
4462 /* If this is a partial instantiation, then we can't instantiate
4463 the type; there's no telling whether or not one of the
4464 template parameters might eventually be instantiated to some
4465 value that results in a specialization being used. For
4466 example, consider:
4467
4468 template <class T>
4469 struct S {};
4470
4471 template <class U>
4472 void f(S<U>);
4473
4474 template <>
4475 struct S<int> {};
4476
4477 Now, the `S<U>' in `f<int>' is the specialization, not an
5db698f6 4478 instantiation of the original template. */
4c571114
MM
4479 goto end;
4480
4481 /* Determine what specialization of the original template to
4482 instantiate. */
4483 if (PARTIAL_INSTANTIATION_P (type))
6bdb985f
MM
4484 /* There's no telling which specialization is appropriate at this
4485 point. Since all peeking at the innards of this partial
4486 instantiation are extensions (like the "implicit typename"
4487 extension, which allows users to omit the keyword `typename' on
4488 names that are declared as types in template base classes), we
4489 are free to do what we please.
4490
4491 Trying to figure out which partial instantiation to use can
4492 cause a crash. (Some of the template arguments don't even have
4493 types.) So, we just use the most general version. */
4494 t = NULL_TREE;
4495 else
73aad9b9 4496 {
6bdb985f
MM
4497 t = most_specialized_class (template, args);
4498
4499 if (t == error_mark_node)
73aad9b9 4500 {
6bdb985f
MM
4501 char *str = "candidates are:";
4502 cp_error ("ambiguous class template instantiation for `%#T'", type);
4503 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
4504 t = TREE_CHAIN (t))
73aad9b9 4505 {
6bdb985f
MM
4506 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4507 args))
4508 {
4509 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
4510 str = " ";
4511 }
73aad9b9 4512 }
6bdb985f
MM
4513 TYPE_BEING_DEFINED (type) = 1;
4514 type = error_mark_node;
4515 goto end;
73aad9b9 4516 }
73aad9b9 4517 }
6bdb985f
MM
4518
4519 if (t)
73aad9b9
JM
4520 pattern = TREE_TYPE (t);
4521 else
4522 pattern = TREE_TYPE (template);
5566b478 4523
4c571114
MM
4524 /* If the template we're instantiating is incomplete, then clearly
4525 there's nothing we can do. */
5566b478 4526 if (TYPE_SIZE (pattern) == NULL_TREE)
7ddedda4 4527 goto end;
5566b478 4528
4c571114
MM
4529 /* If this is a partial instantiation, don't tsubst anything. We will
4530 only use this type for implicit typename, so the actual contents don't
4531 matter. All that matters is whether a particular name is a type. */
4532 if (PARTIAL_INSTANTIATION_P (type))
4533 {
4534 /* The fields set here must be kept in sync with those cleared
4535 in begin_class_definition. */
4536 TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern);
4537 TYPE_FIELDS (type) = TYPE_FIELDS (pattern);
4538 TYPE_METHODS (type) = TYPE_METHODS (pattern);
4539 CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern);
4540 /* Pretend that the type is complete, so that we will look
4541 inside it during name lookup and such. */
4542 TYPE_SIZE (type) = integer_zero_node;
4543 goto end;
4544 }
4545
4546 /* If we've recursively instantiated too many templates, stop. */
4547 if (! push_tinst_level (type))
4548 goto end;
4549
4550 /* Now we're really doing the instantiation. Mark the type as in
4551 the process of being defined. */
4552 TYPE_BEING_DEFINED (type) = 1;
4553
4554 maybe_push_to_top_level (uses_template_parms (type));
4555 pushclass (type, 0);
4556
73aad9b9 4557 if (t)
36a117a5
MM
4558 {
4559 /* This TYPE is actually a instantiation of of a partial
4560 specialization. We replace the innermost set of ARGS with
4561 the arguments appropriate for substitution. For example,
4562 given:
4563
4564 template <class T> struct S {};
4565 template <class T> struct S<T*> {};
4566
4567 and supposing that we are instantiating S<int*>, ARGS will
4568 present be {int*} but we need {int}. */
4569 tree inner_args
4570 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4571 args);
4572
4573 /* If there were multiple levels in ARGS, replacing the
4574 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
4575 want, so we make a copy first. */
4576 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
4577 {
4578 args = copy_node (args);
4579 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
4580 }
4581 else
4582 args = inner_args;
4583 }
8d019cef 4584
5566b478
MS
4585 if (flag_external_templates)
4586 {
4587 if (flag_alt_external_templates)
4588 {
4589 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
4590 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
4591 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
2604412d
JM
4592 = (! CLASSTYPE_INTERFACE_ONLY (type)
4593 && CLASSTYPE_INTERFACE_KNOWN (type));
5566b478
MS
4594 }
4595 else
4596 {
4597 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
4598 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
4599 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
4600 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
2604412d
JM
4601 = (! CLASSTYPE_INTERFACE_ONLY (type)
4602 && CLASSTYPE_INTERFACE_KNOWN (type));
5566b478
MS
4603 }
4604 }
4605 else
8d08fdba 4606 {
5566b478
MS
4607 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
4608 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
8d08fdba
MS
4609 }
4610
f7da6097
MS
4611 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
4612 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
4613 TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern);
4614 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
4615 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
4616 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
4617 TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
4618 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
4619 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
4620 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
4621 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
4622 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
4623 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
4624 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
f7da6097
MS
4625 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
4626 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
4627 TYPE_USES_COMPLEX_INHERITANCE (type)
4628 = TYPE_USES_COMPLEX_INHERITANCE (pattern);
4629 TYPE_USES_MULTIPLE_INHERITANCE (type)
4630 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
4631 TYPE_USES_VIRTUAL_BASECLASSES (type)
4632 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
4633 TYPE_PACKED (type) = TYPE_PACKED (pattern);
4634 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
eff71ab0 4635 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
f7da6097 4636
4c571114
MM
4637 /* We must copy the arguments to the permanent obstack since
4638 during the tsubst'ing below they may wind up in the
4639 DECL_TI_ARGS of some instantiated member template. */
4640 args = copy_to_permanent (args);
2604412d 4641
5566b478
MS
4642 {
4643 tree binfo = TYPE_BINFO (type);
4644 tree pbases = TYPE_BINFO_BASETYPES (pattern);
4645
4646 if (pbases)
4647 {
4648 tree bases;
4649 int i;
4650 int len = TREE_VEC_LENGTH (pbases);
de22184b 4651 bases = make_tree_vec (len);
5566b478
MS
4652 for (i = 0; i < len; ++i)
4653 {
711734a9 4654 tree elt, basetype;
5566b478
MS
4655
4656 TREE_VEC_ELT (bases, i) = elt
f7d98d58 4657 = tsubst (TREE_VEC_ELT (pbases, i), args, NULL_TREE);
5566b478
MS
4658 BINFO_INHERITANCE_CHAIN (elt) = binfo;
4659
711734a9
JM
4660 basetype = TREE_TYPE (elt);
4661
4662 if (! IS_AGGR_TYPE (basetype))
8251199e
JM
4663 cp_error
4664 ("base type `%T' of `%T' fails to be a struct or class type",
711734a9
JM
4665 basetype, type);
4666 else if (TYPE_SIZE (complete_type (basetype)) == NULL_TREE)
8251199e 4667 cp_error ("base class `%T' of `%T' has incomplete type",
711734a9
JM
4668 basetype, type);
4669
4670 /* These are set up in xref_basetypes for normal classes, so
4671 we have to handle them here for template bases. */
dfbcd65a
JM
4672
4673 unshare_base_binfos (elt);
4674
711734a9
JM
4675 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4676 {
4677 TYPE_USES_VIRTUAL_BASECLASSES (type) = 1;
4678 TYPE_USES_COMPLEX_INHERITANCE (type) = 1;
4679 }
4680 TYPE_GETS_NEW (type) |= TYPE_GETS_NEW (basetype);
4681 TYPE_GETS_DELETE (type) |= TYPE_GETS_DELETE (basetype);
5566b478 4682 }
de22184b
MS
4683 /* Don't initialize this until the vector is filled out, or
4684 lookups will crash. */
4685 BINFO_BASETYPES (binfo) = bases;
5566b478
MS
4686 }
4687 }
4688
5566b478 4689 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
8d08fdba 4690 {
5566b478 4691 tree tag = TREE_VALUE (t);
36a117a5
MM
4692 tree name = TYPE_IDENTIFIER (tag);
4693 tree newtag;
5566b478 4694
ed44da02 4695 newtag = tsubst (tag, args, NULL_TREE);
61a127b3 4696 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
ed44da02 4697 {
486e4077
MM
4698 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
4699 /* Unfortunately, lookup_template_class sets
4700 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
4701 instantiation (i.e., for the type of a member template
4702 class nested within a template class.) This behavior is
4703 required for maybe_process_partial_specialization to work
4704 correctly, but is not accurate in this case; the TAG is not
4705 an instantiation of anything. (The corresponding
4706 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
4707 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
4708
ed44da02
MM
4709 /* Now, we call pushtag to put this NEWTAG into the scope of
4710 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
4711 pushtag calling push_template_decl. We don't have to do
4712 this for enums because it will already have been done in
4713 tsubst_enum. */
4714 if (name)
4715 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
4716 pushtag (name, newtag, /*globalize=*/0);
4717 }
8d08fdba
MS
4718 }
4719
5566b478
MS
4720 /* Don't replace enum constants here. */
4721 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
4722 if (TREE_CODE (t) != CONST_DECL)
4723 {
ae58fa02
MM
4724 tree r;
4725
4726 /* The the file and line for this declaration, to assist in
4727 error message reporting. Since we called push_tinst_level
4728 above, we don't need to restore these. */
4729 lineno = DECL_SOURCE_LINE (t);
4730 input_filename = DECL_SOURCE_FILE (t);
4731
4732 r = tsubst (t, args, NULL_TREE);
5566b478
MS
4733 if (TREE_CODE (r) == VAR_DECL)
4734 {
2604412d
JM
4735 pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
4736 /* Perhaps we should do more of grokfield here. */
5566b478
MS
4737 start_decl_1 (r);
4738 DECL_IN_AGGR_P (r) = 1;
4739 DECL_EXTERNAL (r) = 1;
4740 cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0);
6ba89f8e
MM
4741 if (DECL_DEFINED_IN_CLASS_P (r))
4742 check_static_variable_definition (r, TREE_TYPE (r));
5566b478 4743 }
61a127b3
MM
4744
4745 /* R will have a TREE_CHAIN if and only if it has already been
4746 processed by finish_member_declaration. This can happen
4747 if, for example, it is a TYPE_DECL for a class-scoped
4748 ENUMERAL_TYPE; such a thing will already have been added to
4749 the field list by tsubst_enum above. */
4750 if (!TREE_CHAIN (r))
4751 {
4752 set_current_access_from_decl (r);
4753 finish_member_declaration (r);
4754 }
5566b478 4755 }
8d08fdba 4756
61a127b3
MM
4757 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
4758 for this instantiation. */
4759 for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t))
4760 {
4761 tree r = tsubst (t, args, NULL_TREE);
4762 set_current_access_from_decl (r);
4763 finish_member_declaration (r);
4764 }
8d08fdba 4765
2604412d
JM
4766 /* Construct the DECL_FRIENDLIST for the new class type. */
4767 typedecl = TYPE_MAIN_DECL (type);
4768 for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern));
4769 t != NULL_TREE;
4770 t = TREE_CHAIN (t))
f84b4be9 4771 {
2604412d 4772 tree friends;
f84b4be9 4773
2604412d
JM
4774 DECL_FRIENDLIST (typedecl)
4775 = tree_cons (TREE_PURPOSE (t), NULL_TREE,
4776 DECL_FRIENDLIST (typedecl));
f84b4be9 4777
2604412d
JM
4778 for (friends = TREE_VALUE (t);
4779 friends != NULL_TREE;
4780 friends = TREE_CHAIN (friends))
4781 {
4782 if (TREE_PURPOSE (friends) == error_mark_node)
f84b4be9 4783 {
2604412d
JM
4784 TREE_VALUE (DECL_FRIENDLIST (typedecl))
4785 = tree_cons (error_mark_node,
4786 tsubst_friend_function (TREE_VALUE (friends),
4787 args),
4788 TREE_VALUE (DECL_FRIENDLIST (typedecl)));
4789 }
4790 else
4791 {
4792 TREE_VALUE (DECL_FRIENDLIST (typedecl))
4793 = tree_cons (tsubst (TREE_PURPOSE (friends), args, NULL_TREE),
4794 NULL_TREE,
4795 TREE_VALUE (DECL_FRIENDLIST (typedecl)));
f84b4be9
JM
4796
4797 }
4798 }
2604412d 4799 }
5566b478 4800
6757edfe
MM
4801 for (t = CLASSTYPE_FRIEND_CLASSES (pattern);
4802 t != NULL_TREE;
4803 t = TREE_CHAIN (t))
4804 {
4805 tree friend_type = TREE_VALUE (t);
1aed5355 4806 tree new_friend_type;
6757edfe 4807
36a117a5
MM
4808 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
4809 new_friend_type = tsubst_friend_class (friend_type, args);
4810 else if (uses_template_parms (friend_type))
4811 new_friend_type = tsubst (friend_type, args, NULL_TREE);
4812 else
6757edfe
MM
4813 /* The call to xref_tag_from_type does injection for friend
4814 classes. */
1aed5355 4815 new_friend_type =
36a117a5
MM
4816 xref_tag_from_type (friend_type, NULL_TREE, 1);
4817
1aed5355
MM
4818
4819 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
4820 /* Trick make_friend_class into realizing that the friend
4821 we're adding is a template, not an ordinary class. It's
4822 important that we use make_friend_class since it will
4823 perform some error-checking and output cross-reference
4824 information. */
4825 ++processing_template_decl;
4826
4827 make_friend_class (type, new_friend_type);
fc378698 4828
1aed5355
MM
4829 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
4830 --processing_template_decl;
6757edfe 4831 }
fc378698 4832
2604412d
JM
4833 /* This does injection for friend functions. */
4834 if (!processing_template_decl)
4835 {
4836 t = tsubst (DECL_TEMPLATE_INJECT (template), args, NULL_TREE);
5566b478 4837
2604412d
JM
4838 for (; t; t = TREE_CHAIN (t))
4839 {
4840 tree d = TREE_VALUE (t);
fc378698 4841
2604412d
JM
4842 if (TREE_CODE (d) == TYPE_DECL)
4843 /* Already injected. */;
4844 else
4845 pushdecl (d);
4846 }
4847 }
5566b478 4848
2604412d
JM
4849 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
4850 if (TREE_CODE (t) == FIELD_DECL)
4851 {
4852 TREE_TYPE (t) = complete_type (TREE_TYPE (t));
4853 require_complete_type (t);
4854 }
5566b478 4855
61a127b3
MM
4856 /* Set the file and line number information to whatever is given for
4857 the class itself. This puts error messages involving generated
4858 implicit functions at a predictable point, and the same point
4859 that would be used for non-template classes. */
4860 lineno = DECL_SOURCE_LINE (typedecl);
4861 input_filename = DECL_SOURCE_FILE (typedecl);
4862
4863 unreverse_member_declarations (type);
2604412d
JM
4864 type = finish_struct_1 (type, 0);
4865 CLASSTYPE_GOT_SEMICOLON (type) = 1;
e92cc029 4866
5524676d
JM
4867 /* Clear this now so repo_template_used is happy. */
4868 TYPE_BEING_DEFINED (type) = 0;
2604412d 4869 repo_template_used (type);
8d08fdba 4870
5566b478 4871 popclass (0);
5566b478
MS
4872 pop_from_top_level ();
4873 pop_tinst_level ();
4874
7ddedda4
MM
4875 end:
4876 pop_momentary ();
4877
5566b478 4878 return type;
8d08fdba
MS
4879}
4880
4881static int
4882list_eq (t1, t2)
4883 tree t1, t2;
4884{
4885 if (t1 == NULL_TREE)
4886 return t2 == NULL_TREE;
4887 if (t2 == NULL_TREE)
4888 return 0;
4889 /* Don't care if one declares its arg const and the other doesn't -- the
4890 main variant of the arg type is all that matters. */
4891 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
4892 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
4893 return 0;
4894 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
4895}
4896
00d3396f
JM
4897/* If arg is a non-type template parameter that does not depend on template
4898 arguments, fold it like we weren't in the body of a template. */
4899
4900static tree
4901maybe_fold_nontype_arg (arg)
4902 tree arg;
4903{
4904 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
4905 && !uses_template_parms (arg))
4906 {
4907 /* Sometimes, one of the args was an expression involving a
4908 template constant parameter, like N - 1. Now that we've
4909 tsubst'd, we might have something like 2 - 1. This will
4910 confuse lookup_template_class, so we do constant folding
4911 here. We have to unset processing_template_decl, to
4912 fool build_expr_from_tree() into building an actual
4913 tree. */
4914
4915 int saved_processing_template_decl = processing_template_decl;
4916 processing_template_decl = 0;
4917 arg = fold (build_expr_from_tree (arg));
4918 processing_template_decl = saved_processing_template_decl;
4919 }
4920 return arg;
4921}
4922
a221d52f 4923/* Return the TREE_VEC with the arguments for the innermost template header,
36a117a5
MM
4924 where ARGS is either that or the VEC of VECs for all the
4925 arguments. */
a221d52f
JM
4926
4927tree
36a117a5 4928innermost_args (args)
a221d52f 4929 tree args;
a221d52f 4930{
36a117a5 4931 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
a221d52f 4932}
f84b4be9 4933
830bfa74
MM
4934/* Substitute ARGS into the vector of template arguments T. */
4935
4936tree
4937tsubst_template_arg_vector (t, args)
4938 tree t;
4939 tree args;
4940{
4941 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
4942 tree *elts = (tree *) alloca (len * sizeof (tree));
4943
4944 bzero ((char *) elts, len * sizeof (tree));
4945
4946 for (i = 0; i < len; i++)
4947 {
4948 if (TREE_VEC_ELT (t, i) != NULL_TREE
4949 && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
4950 elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i), args);
4951 else
4952 elts[i] = maybe_fold_nontype_arg
4953 (tsubst_expr (TREE_VEC_ELT (t, i), args, NULL_TREE));
4954
4955 if (elts[i] != TREE_VEC_ELT (t, i))
4956 need_new = 1;
4957 }
4958
4959 if (!need_new)
4960 return t;
4961
7ddedda4 4962 t = make_temp_vec (len);
830bfa74
MM
4963 for (i = 0; i < len; i++)
4964 TREE_VEC_ELT (t, i) = elts[i];
4965
4966 return t;
4967}
4968
36a117a5
MM
4969/* Return the result of substituting ARGS into the template parameters
4970 given by PARMS. If there are m levels of ARGS and m + n levels of
4971 PARMS, then the result will contain n levels of PARMS. For
4972 example, if PARMS is `template <class T> template <class U>
4973 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
4974 result will be `template <int*, double, class V>'. */
4975
4976tree
4977tsubst_template_parms (parms, args)
4978 tree parms;
4979 tree args;
4980{
4981 tree r;
4982 tree* new_parms = &r;
4983
4984 for (new_parms = &r;
4985 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
4986 new_parms = &(TREE_CHAIN (*new_parms)),
4987 parms = TREE_CHAIN (parms))
4988 {
4989 tree new_vec =
4990 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
4991 int i;
4992
4993 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
4994 {
4995 tree default_value =
4996 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i));
4997 tree parm_decl =
4998 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i));
4999
5000 TREE_VEC_ELT (new_vec, i)
5001 = build_tree_list (tsubst (default_value, args, NULL_TREE),
5002 tsubst (parm_decl, args, NULL_TREE));
5003
5004 }
5005
5006 *new_parms =
5007 tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms)
5008 - TMPL_ARGS_DEPTH (args))),
5009 new_vec, NULL_TREE);
5010 }
5011
5012 return r;
5013}
5014
ed44da02
MM
5015/* Substitute the ARGS into the indicated aggregate (or enumeration)
5016 type T. If T is not an aggregate or enumeration type, it is
5017 handled as if by tsubst. IN_DECL is as for tsubst. If
5018 ENTERING_SCOPE is non-zero, T is the context for a template which
5019 we are presently tsubst'ing. Return the subsituted value. */
36a117a5
MM
5020
5021tree
5022tsubst_aggr_type (t, args, in_decl, entering_scope)
5023 tree t;
5024 tree args;
5025 tree in_decl;
5026 int entering_scope;
5027{
5028 if (t == NULL_TREE)
5029 return NULL_TREE;
5030
5031 switch (TREE_CODE (t))
5032 {
5033 case RECORD_TYPE:
5034 if (TYPE_PTRMEMFUNC_P (t))
5035 {
5036 tree r = build_ptrmemfunc_type
5037 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, in_decl));
91063b51 5038 return cp_build_qualified_type (r, TYPE_QUALS (t));
36a117a5
MM
5039 }
5040
5041 /* else fall through */
ed44da02 5042 case ENUMERAL_TYPE:
36a117a5 5043 case UNION_TYPE:
5db698f6 5044 if (TYPE_TEMPLATE_INFO (t))
36a117a5
MM
5045 {
5046 tree argvec;
5047 tree context;
5048 tree r;
5049
5050 /* First, determine the context for the type we are looking
5051 up. */
5052 if (TYPE_CONTEXT (t) != NULL_TREE)
5053 context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
5054 in_decl, /*entering_scope=*/1);
5055 else
5056 context = NULL_TREE;
5057
5058 /* Then, figure out what arguments are appropriate for the
5059 type we are trying to find. For example, given:
5060
5061 template <class T> struct S;
5062 template <class T, class U> void f(T, U) { S<U> su; }
5063
5064 and supposing that we are instantiating f<int, double>,
5065 then our ARGS will be {int, double}, but, when looking up
5066 S we only want {double}. */
7ddedda4
MM
5067 push_momentary ();
5068 argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args);
36a117a5
MM
5069
5070 r = lookup_template_class (t, argvec, in_decl, context,
5071 entering_scope);
7ddedda4 5072 pop_momentary ();
36a117a5 5073
91063b51 5074 return cp_build_qualified_type (r, TYPE_QUALS (t));
36a117a5
MM
5075 }
5076 else
5077 /* This is not a template type, so there's nothing to do. */
5078 return t;
5079
5080 default:
5081 return tsubst (t, args, in_decl);
5082 }
5083}
5084
ae58fa02
MM
5085/* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5086 (already computed) substitution of ARGS into TREE_TYPE (T), if
5087 appropriate. Return the result of the substitution. IN_DECL is as
5088 for tsubst. */
00d3396f 5089
75b0bbce 5090tree
ae58fa02
MM
5091tsubst_decl (t, args, type, in_decl)
5092 tree t;
5093 tree args;
5094 tree type;
8d08fdba
MS
5095 tree in_decl;
5096{
ae58fa02
MM
5097 int saved_lineno;
5098 char* saved_filename;
b370501f 5099 tree r = NULL_TREE;
830bfa74 5100
ae58fa02
MM
5101 /* Set the filename and linenumber to improve error-reporting. */
5102 saved_lineno = lineno;
5103 saved_filename = input_filename;
5104 lineno = DECL_SOURCE_LINE (t);
5105 input_filename = DECL_SOURCE_FILE (t);
b7484fbe 5106
8d08fdba
MS
5107 switch (TREE_CODE (t))
5108 {
98c1c668
JM
5109 case TEMPLATE_DECL:
5110 {
5111 /* We can get here when processing a member template function
5112 of a template class. */
98c1c668 5113 tree decl = DECL_TEMPLATE_RESULT (t);
386b8a85 5114 tree spec;
db2767b6 5115 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
98c1c668 5116
db2767b6
MM
5117 if (!is_template_template_parm)
5118 {
36a117a5
MM
5119 /* We might already have an instance of this template.
5120 The ARGS are for the surrounding class type, so the
5121 full args contain the tsubst'd args for the context,
5122 plus the innermost args from the template decl. */
5123 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
5124 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
5125 : DECL_TI_ARGS (DECL_RESULT (t));
7ddedda4
MM
5126 tree full_args;
5127
5128 push_momentary ();
5129 full_args = tsubst_template_arg_vector (tmpl_args, args);
36a117a5
MM
5130
5131 /* tsubst_template_arg_vector doesn't copy the vector if
5132 nothing changed. But, *something* should have
5133 changed. */
5134 my_friendly_assert (full_args != tmpl_args, 0);
5135
5136 spec = retrieve_specialization (t, full_args);
7ddedda4 5137 pop_momentary ();
db2767b6 5138 if (spec != NULL_TREE)
ae58fa02
MM
5139 {
5140 r = spec;
5141 break;
5142 }
db2767b6 5143 }
98c1c668
JM
5144
5145 /* Make a new template decl. It will be similar to the
5146 original, but will record the current template arguments.
5147 We also create a new function declaration, which is just
5148 like the old one, but points to this new template, rather
5149 than the old one. */
ae58fa02
MM
5150 r = copy_node (t);
5151 copy_lang_decl (r);
5152 my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
5153 TREE_CHAIN (r) = NULL_TREE;
db2767b6
MM
5154
5155 if (is_template_template_parm)
5156 {
5157 tree new_decl = tsubst (decl, args, in_decl);
ae58fa02
MM
5158 DECL_RESULT (r) = new_decl;
5159 TREE_TYPE (r) = TREE_TYPE (new_decl);
5160 break;
db2767b6
MM
5161 }
5162
ae58fa02 5163 DECL_CONTEXT (r)
36a117a5
MM
5164 = tsubst_aggr_type (DECL_CONTEXT (t), args, in_decl,
5165 /*entering_scope=*/1);
ae58fa02 5166 DECL_CLASS_CONTEXT (r)
36a117a5
MM
5167 = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, in_decl,
5168 /*entering_scope=*/1);
ae58fa02 5169 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
93cdc044
JM
5170
5171 if (TREE_CODE (decl) == TYPE_DECL)
5172 {
5173 tree new_type = tsubst (TREE_TYPE (t), args, in_decl);
ae58fa02
MM
5174 TREE_TYPE (r) = new_type;
5175 CLASSTYPE_TI_TEMPLATE (new_type) = r;
5176 DECL_RESULT (r) = TYPE_MAIN_DECL (new_type);
5177 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
93cdc044
JM
5178 }
5179 else
5180 {
5181 tree new_decl = tsubst (decl, args, in_decl);
ae58fa02
MM
5182 DECL_RESULT (r) = new_decl;
5183 DECL_TI_TEMPLATE (new_decl) = r;
5184 TREE_TYPE (r) = TREE_TYPE (new_decl);
5185 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
93cdc044
JM
5186 }
5187
ae58fa02
MM
5188 SET_DECL_IMPLICIT_INSTANTIATION (r);
5189 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
5190 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
98c1c668
JM
5191
5192 /* The template parameters for this new template are all the
5193 template parameters for the old template, except the
5194 outermost level of parameters. */
ae58fa02 5195 DECL_TEMPLATE_PARMS (r)
36a117a5 5196 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args);
98c1c668 5197
93cdc044 5198 if (PRIMARY_TEMPLATE_P (t))
ae58fa02 5199 DECL_PRIMARY_TEMPLATE (r) = r;
93cdc044 5200
8d019cef 5201 /* We don't partially instantiate partial specializations. */
93cdc044 5202 if (TREE_CODE (decl) == TYPE_DECL)
ae58fa02 5203 break;
93cdc044 5204
75650646
MM
5205 for (spec = DECL_TEMPLATE_SPECIALIZATIONS (t);
5206 spec != NULL_TREE;
5207 spec = TREE_CHAIN (spec))
5208 {
5209 /* It helps to consider example here. Consider:
5210
5211 template <class T>
5212 struct S {
5213 template <class U>
5214 void f(U u);
5215
5216 template <>
5217 void f(T* t) {}
5218 };
5219
5220 Now, for example, we are instantiating S<int>::f(U u).
5221 We want to make a template:
5222
5223 template <class U>
5224 void S<int>::f(U);
5225
5226 It will have a specialization, for the case U = int*, of
5227 the form:
5228
5229 template <>
5230 void S<int>::f<int*>(int*);
5231
5232 This specialization will be an instantiation of
5233 the specialization given in the declaration of S, with
5234 argument list int*. */
5235
5236 tree fn = TREE_VALUE (spec);
5237 tree spec_args;
5238 tree new_fn;
5239
5240 if (!DECL_TEMPLATE_SPECIALIZATION (fn))
5241 /* Instantiations are on the same list, but they're of
5242 no concern to us. */
5243 continue;
5244
36a117a5
MM
5245 if (TREE_CODE (fn) != TEMPLATE_DECL)
5246 /* A full specialization. There's no need to record
5247 that here. */
5248 continue;
5249
5250 spec_args = tsubst (DECL_TI_ARGS (fn), args, in_decl);
5251 new_fn = tsubst (DECL_RESULT (most_general_template (fn)),
5252 spec_args, in_decl);
5253 DECL_TI_TEMPLATE (new_fn) = fn;
ae58fa02 5254 register_specialization (new_fn, r,
36a117a5 5255 innermost_args (spec_args));
75650646
MM
5256 }
5257
5258 /* Record this partial instantiation. */
ae58fa02
MM
5259 register_specialization (r, t,
5260 DECL_TI_ARGS (DECL_RESULT (r)));
75650646 5261
98c1c668 5262 }
ae58fa02 5263 break;
8d08fdba
MS
5264
5265 case FUNCTION_DECL:
5266 {
386b8a85 5267 tree ctx;
87603ed0 5268 tree argvec = NULL_TREE;
36a117a5 5269 tree gen_tmpl;
5566b478 5270 int member;
d8c4447d
MM
5271 int args_depth;
5272 int parms_depth;
5566b478 5273
36a117a5
MM
5274 /* Nobody should be tsubst'ing into non-template functions. */
5275 my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5276
5277 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
5278 {
5279 tree spec;
5280
7ddedda4
MM
5281 /* Allocate template arguments on the momentary obstack,
5282 in case we don't need to keep them. */
5283 push_momentary ();
5284
36a117a5
MM
5285 /* Calculate the most general template of which R is a
5286 specialization, and the complete set of arguments used to
5287 specialize R. */
5288 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
7ddedda4
MM
5289 argvec
5290 = tsubst_template_arg_vector (DECL_TI_ARGS
5291 (DECL_TEMPLATE_RESULT (gen_tmpl)),
5292 args);
36a117a5
MM
5293
5294 /* Check to see if we already have this specialization. */
5295 spec = retrieve_specialization (gen_tmpl, argvec);
7ddedda4 5296
36a117a5 5297 if (spec)
ae58fa02
MM
5298 {
5299 r = spec;
7ddedda4 5300 pop_momentary ();
ae58fa02
MM
5301 break;
5302 }
d8c4447d 5303
7ddedda4
MM
5304 /* We're going to need to keep the ARGVEC, so we copy it
5305 here. */
5306 argvec = copy_to_permanent (argvec);
5307 pop_momentary ();
5308
d8c4447d
MM
5309 /* Here, we deal with the peculiar case:
5310
5311 template <class T> struct S {
5312 template <class U> friend void f();
5313 };
5314 template <class U> friend void f() {}
5315 template S<int>;
5316 template void f<double>();
5317
5318 Here, the ARGS for the instantiation of will be {int,
5319 double}. But, we only need as many ARGS as there are
5320 levels of template parameters in CODE_PATTERN. We are
5321 careful not to get fooled into reducing the ARGS in
5322 situations like:
5323
5324 template <class T> struct S { template <class U> void f(U); }
5325 template <class T> template <> void S<T>::f(int) {}
5326
5327 which we can spot because the pattern will be a
5328 specialization in this case. */
5329 args_depth = TMPL_ARGS_DEPTH (args);
5330 parms_depth =
5331 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
5332 if (args_depth > parms_depth
5333 && !DECL_TEMPLATE_SPECIALIZATION (t))
5334 {
5335 my_friendly_assert (DECL_FRIEND_P (t), 0);
5336
5337 if (parms_depth > 1)
5338 {
5339 int i;
5340
5341 args = make_temp_vec (parms_depth);
5342 for (i = 0; i < parms_depth; ++i)
5343 TREE_VEC_ELT (args, i) =
5344 TREE_VEC_ELT (args, i + (args_depth - parms_depth));
5345 }
5346 else
5347 args = TREE_VEC_ELT (args, args_depth - parms_depth);
5348 }
36a117a5
MM
5349 }
5350 else
5351 {
5352 /* This special case arises when we have something like this:
5353
5354 template <class T> struct S {
5355 friend void f<int>(int, double);
5356 };
5357
5358 Here, the DECL_TI_TEMPLATE for the friend declaration
39c01e4c
MM
5359 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5360 being called from tsubst_friend_function, and we want
5361 only to create a new decl (R) with appropriate types so
5362 that we can call determine_specialization. */
5363 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t))
5364 == LOOKUP_EXPR)
5365 || (TREE_CODE (DECL_TI_TEMPLATE (t))
5366 == IDENTIFIER_NODE), 0);
36a117a5
MM
5367 gen_tmpl = NULL_TREE;
5368 }
5369
6eb3bb27 5370 if (DECL_CLASS_SCOPE_P (t))
8d08fdba 5371 {
5566b478
MS
5372 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
5373 member = 2;
5374 else
5375 member = 1;
36a117a5
MM
5376 ctx = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, t,
5377 /*entering_scope=*/1);
5566b478
MS
5378 }
5379 else
5380 {
5381 member = 0;
5382 ctx = NULL_TREE;
5566b478 5383 }
eff71ab0 5384 type = tsubst (type, args, in_decl);
8d08fdba 5385
5566b478
MS
5386 /* We do NOT check for matching decls pushed separately at this
5387 point, as they may not represent instantiations of this
5388 template, and in any case are considered separate under the
312e7d50 5389 discrete model. Instead, see add_maybe_template. */
5566b478
MS
5390
5391 r = copy_node (t);
5392 copy_lang_decl (r);
e1467ff2 5393 DECL_USE_TEMPLATE (r) = 0;
5566b478
MS
5394 TREE_TYPE (r) = type;
5395
5396 DECL_CONTEXT (r)
36a117a5 5397 = tsubst_aggr_type (DECL_CONTEXT (t), args, t, /*entering_scope=*/1);
5566b478
MS
5398 DECL_CLASS_CONTEXT (r) = ctx;
5399
4c571114 5400 if (member && IDENTIFIER_TYPENAME_P (DECL_NAME (r)))
f181d4ae
MM
5401 /* Type-conversion operator. Reconstruct the name, in
5402 case it's the name of one of the template's parameters. */
5403 DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
5566b478 5404
711734a9
JM
5405 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, t);
5406 DECL_MAIN_VARIANT (r) = r;
5407 DECL_RESULT (r) = NULL_TREE;
711734a9
JM
5408
5409 TREE_STATIC (r) = 0;
5410 TREE_PUBLIC (r) = TREE_PUBLIC (t);
5411 DECL_EXTERNAL (r) = 1;
5412 DECL_INTERFACE_KNOWN (r) = 0;
5413 DECL_DEFER_OUTPUT (r) = 0;
5414 TREE_CHAIN (r) = NULL_TREE;
5415 DECL_PENDING_INLINE_INFO (r) = 0;
5416 TREE_USED (r) = 0;
5417
36a117a5
MM
5418 /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled
5419 name. There's no need to do this in the special friend
5420 case mentioned above where GEN_TMPL is NULL. */
5421 if (gen_tmpl)
386b8a85 5422 {
7ddedda4
MM
5423 /* The ARGVEC was built on the momentary obstack. Make it
5424 permanent now. */
5425 argvec = copy_to_permanent (argvec);
36a117a5
MM
5426 DECL_TEMPLATE_INFO (r)
5427 = perm_tree_cons (gen_tmpl, argvec, NULL_TREE);
5428 SET_DECL_IMPLICIT_INSTANTIATION (r);
5429 register_specialization (r, gen_tmpl, argvec);
5430
5431 /* Set the mangled name for R. */
5432 if (DECL_DESTRUCTOR_P (t))
5433 DECL_ASSEMBLER_NAME (r) = build_destructor_name (ctx);
5434 else
386b8a85 5435 {
36a117a5
MM
5436 /* Instantiations of template functions must be mangled
5437 specially, in order to conform to 14.5.5.1
5438 [temp.over.link]. */
5439 tree tmpl = DECL_TI_TEMPLATE (t);
386b8a85 5440
36a117a5
MM
5441 /* TMPL will be NULL if this is a specialization of a
5442 member function of a template class. */
5443 if (name_mangling_version < 1
5444 || tmpl == NULL_TREE
5445 || (member && !is_member_template (tmpl)
5446 && !DECL_TEMPLATE_INFO (tmpl)))
5447 set_mangled_name_for_decl (r);
75650646 5448 else
36a117a5 5449 set_mangled_name_for_template_decl (r);
386b8a85 5450 }
36a117a5
MM
5451
5452 DECL_RTL (r) = 0;
5453 make_decl_rtl (r, NULL_PTR, 1);
5454
5455 /* Like grokfndecl. If we don't do this, pushdecl will
5456 mess up our TREE_CHAIN because it doesn't find a
5457 previous decl. Sigh. */
5458 if (member
c36ae80f 5459 && ! uses_template_parms (r)
36a117a5
MM
5460 && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r))
5461 == NULL_TREE))
5462 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r), r);
386b8a85 5463 }
f181d4ae
MM
5464
5465 if (DECL_CONSTRUCTOR_P (r))
5466 {
5467 maybe_retrofit_in_chrg (r);
5468 grok_ctor_properties (ctx, r);
5469 }
5470 if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
5471 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
8d08fdba 5472 }
ae58fa02 5473 break;
8d08fdba
MS
5474
5475 case PARM_DECL:
5476 {
ae58fa02 5477 r = copy_node (t);
5566b478 5478 TREE_TYPE (r) = type;
db2767b6
MM
5479 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
5480 DECL_INITIAL (r) = TREE_TYPE (r);
5481 else
5482 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, in_decl);
5483
5566b478 5484 DECL_CONTEXT (r) = NULL_TREE;
f83b0cb6
JM
5485#ifdef PROMOTE_PROTOTYPES
5486 if ((TREE_CODE (type) == INTEGER_TYPE
5487 || TREE_CODE (type) == ENUMERAL_TYPE)
5488 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5489 DECL_ARG_TYPE (r) = integer_type_node;
5490#endif
8d08fdba 5491 if (TREE_CHAIN (t))
f7d98d58 5492 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, TREE_CHAIN (t));
8d08fdba 5493 }
ae58fa02 5494 break;
8d08fdba 5495
5566b478
MS
5496 case FIELD_DECL:
5497 {
ae58fa02 5498 r = copy_node (t);
5566b478
MS
5499 TREE_TYPE (r) = type;
5500 copy_lang_decl (r);
5501#if 0
f7d98d58 5502 DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, in_decl);
5566b478 5503#endif
f7d98d58 5504 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, in_decl);
5566b478 5505 TREE_CHAIN (r) = NULL_TREE;
519ebd1e 5506 if (TREE_CODE (type) == VOID_TYPE)
8251199e 5507 cp_error_at ("instantiation of `%D' as type void", r);
5566b478 5508 }
ae58fa02 5509 break;
5566b478
MS
5510
5511 case USING_DECL:
5512 {
ae58fa02 5513 r = copy_node (t);
5566b478 5514 DECL_INITIAL (r)
f7d98d58 5515 = tsubst_copy (DECL_INITIAL (t), args, in_decl);
5566b478 5516 TREE_CHAIN (r) = NULL_TREE;
5566b478 5517 }
ae58fa02 5518 break;
5566b478
MS
5519
5520 case VAR_DECL:
5521 {
36a117a5
MM
5522 tree argvec;
5523 tree gen_tmpl;
5524 tree spec;
5525 tree tmpl;
5526 tree ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, in_decl,
5527 /*entering_scope=*/1);
5528
5529 /* Nobody should be tsubst'ing into non-template variables. */
5530 my_friendly_assert (DECL_LANG_SPECIFIC (t)
5531 && DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5566b478 5532
36a117a5
MM
5533 /* Check to see if we already have this specialization. */
5534 tmpl = DECL_TI_TEMPLATE (t);
5535 gen_tmpl = most_general_template (tmpl);
5536 argvec = tsubst (DECL_TI_ARGS (t), args, in_decl);
5537 spec = retrieve_specialization (gen_tmpl, argvec);
5538
5539 if (spec)
ae58fa02
MM
5540 {
5541 r = spec;
5542 break;
5543 }
5566b478
MS
5544
5545 r = copy_node (t);
5546 TREE_TYPE (r) = type;
5547 DECL_CONTEXT (r) = ctx;
5548 if (TREE_STATIC (r))
5549 DECL_ASSEMBLER_NAME (r)
5550 = build_static_name (DECL_CONTEXT (r), DECL_NAME (r));
d11ad92e
MS
5551
5552 /* Don't try to expand the initializer until someone tries to use
5553 this variable; otherwise we run into circular dependencies. */
5554 DECL_INITIAL (r) = NULL_TREE;
5566b478
MS
5555 DECL_RTL (r) = 0;
5556 DECL_SIZE (r) = 0;
36a117a5
MM
5557 copy_lang_decl (r);
5558 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
5566b478 5559
36a117a5
MM
5560 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
5561 SET_DECL_IMPLICIT_INSTANTIATION (r);
5562 register_specialization (r, gen_tmpl, argvec);
5566b478 5563
5566b478 5564 TREE_CHAIN (r) = NULL_TREE;
519ebd1e 5565 if (TREE_CODE (type) == VOID_TYPE)
8251199e 5566 cp_error_at ("instantiation of `%D' as type void", r);
5566b478 5567 }
ae58fa02 5568 break;
5566b478
MS
5569
5570 case TYPE_DECL:
d2e5ee5c 5571 if (t == TYPE_NAME (TREE_TYPE (t)))
ae58fa02
MM
5572 r = TYPE_NAME (type);
5573 else
5574 {
5575 r = copy_node (t);
5576 TREE_TYPE (r) = type;
5577 DECL_CONTEXT (r) = current_class_type;
5578 TREE_CHAIN (r) = NULL_TREE;
5579 }
5580 break;
5581
5582 default:
5583 my_friendly_abort (0);
5584 }
5585
5586 /* Restore the file and line information. */
5587 lineno = saved_lineno;
5588 input_filename = saved_filename;
5589
5590 return r;
5591}
5592
cabc336a
MM
5593/* Substitue into the ARG_TYPES of a function type. */
5594
5595tree
5596tsubst_arg_types (arg_types, args, in_decl)
5597 tree arg_types;
5598 tree args;
5599 tree in_decl;
5600{
5601 tree remaining_arg_types;
cabc336a
MM
5602 tree type;
5603
5604 if (!arg_types || arg_types == void_list_node)
5605 return arg_types;
5606
5607 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
5608 args, in_decl);
5609
5610 /* We use TYPE_MAIN_VARIANT is because top-level qualifiers don't
5611 matter on function types. */
5612 type = TYPE_MAIN_VARIANT (type_decays_to
5613 (tsubst (TREE_VALUE (arg_types),
5614 args, in_decl)));
5615
5616 /* Note that we do not substitute into default arguments here. The
5617 standard mandates that they be instantiated only when needed,
5618 which is done in build_over_call. */
5619 return hash_tree_cons_simple (TREE_PURPOSE (arg_types), type,
5620 remaining_arg_types);
5621
5622}
5623
ae58fa02
MM
5624/* Take the tree structure T and replace template parameters used therein
5625 with the argument vector ARGS. IN_DECL is an associated decl for
5626 diagnostics.
5627
5628 tsubst is used for dealing with types, decls and the like; for
5629 expressions, use tsubst_expr or tsubst_copy. */
5630
5631tree
5632tsubst (t, args, in_decl)
5633 tree t, args;
5634 tree in_decl;
5635{
0ecfe0b4 5636 tree type, r;
ae58fa02
MM
5637
5638 if (t == NULL_TREE || t == error_mark_node
5639 || t == integer_type_node
5640 || t == void_type_node
5641 || t == char_type_node
5642 || TREE_CODE (t) == NAMESPACE_DECL)
5643 return t;
5644
5645 if (TREE_CODE (t) == IDENTIFIER_NODE)
5646 type = IDENTIFIER_TYPE_VALUE (t);
5647 else
5648 type = TREE_TYPE (t);
5649 if (type == unknown_type_node)
5650 my_friendly_abort (42);
5651
5652 if (type && TREE_CODE (t) != FUNCTION_DECL
5653 && TREE_CODE (t) != TYPENAME_TYPE
5654 && TREE_CODE (t) != TEMPLATE_DECL
5655 && TREE_CODE (t) != IDENTIFIER_NODE)
5656 type = tsubst (type, args, in_decl);
5657
5658 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
5659 return tsubst_decl (t, args, type, in_decl);
5660
5661 switch (TREE_CODE (t))
5662 {
5663 case RECORD_TYPE:
5664 case UNION_TYPE:
5665 case ENUMERAL_TYPE:
5666 return tsubst_aggr_type (t, args, in_decl, /*entering_scope=*/0);
5667
5668 case ERROR_MARK:
5669 case IDENTIFIER_NODE:
5670 case OP_IDENTIFIER:
5671 case VOID_TYPE:
5672 case REAL_TYPE:
5673 case COMPLEX_TYPE:
5674 case BOOLEAN_TYPE:
5675 case INTEGER_CST:
5676 case REAL_CST:
5677 case STRING_CST:
5678 return t;
5679
5680 case INTEGER_TYPE:
5681 if (t == integer_type_node)
5682 return t;
5683
5684 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
5685 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
5686 return t;
d2e5ee5c 5687
5566b478 5688 {
ae58fa02 5689 tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
7ddedda4 5690
ae58fa02
MM
5691 max = tsubst_expr (max, args, in_decl);
5692 if (processing_template_decl)
5693 {
5694 tree itype = make_node (INTEGER_TYPE);
5695 TYPE_MIN_VALUE (itype) = size_zero_node;
5696 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
5697 integer_one_node);
5698 return itype;
5699 }
5700
0ecfe0b4
JM
5701 if (pedantic && integer_zerop (max))
5702 pedwarn ("creating array with size zero");
5703 else if (INT_CST_LT (max, integer_zero_node))
5704 {
5705 cp_error ("creating array with size `%E'", max);
5706 max = integer_one_node;
5707 }
5708
ae58fa02 5709 max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1));
7ddedda4
MM
5710 if (!TREE_PERMANENT (max) && !allocation_temporary_p ())
5711 max = copy_to_permanent (max);
120ecb8a 5712 return build_index_type (max);
ae58fa02
MM
5713 }
5714
5715 case TEMPLATE_TYPE_PARM:
5716 case TEMPLATE_TEMPLATE_PARM:
5717 case TEMPLATE_PARM_INDEX:
5718 {
5719 int idx;
5720 int level;
5721 int levels;
0ecfe0b4
JM
5722
5723 r = NULL_TREE;
ae58fa02
MM
5724
5725 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
5726 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
5727 {
5728 idx = TEMPLATE_TYPE_IDX (t);
5729 level = TEMPLATE_TYPE_LEVEL (t);
5730 }
5731 else
5732 {
5733 idx = TEMPLATE_PARM_IDX (t);
5734 level = TEMPLATE_PARM_LEVEL (t);
5735 }
5736
5737 if (TREE_VEC_LENGTH (args) > 0)
5738 {
5739 tree arg = NULL_TREE;
5740
5741 levels = TMPL_ARGS_DEPTH (args);
5742 if (level <= levels)
5743 arg = TMPL_ARG (args, level, idx);
5744
8b5b8b7c
MM
5745 if (arg == error_mark_node)
5746 return error_mark_node;
5747 else if (arg != NULL_TREE)
ae58fa02
MM
5748 {
5749 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5750 {
5751 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg))
5752 == 't', 0);
91063b51
MM
5753 return cp_build_qualified_type
5754 (arg, CP_TYPE_QUALS (arg) | CP_TYPE_QUALS (t));
ae58fa02
MM
5755 }
5756 else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
5757 {
7ddedda4 5758 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t))
ae58fa02
MM
5759 {
5760 /* We are processing a type constructed from
5761 a template template parameter */
7ddedda4 5762 tree argvec = tsubst (TYPE_TI_ARGS (t),
ae58fa02 5763 args, in_decl);
ae58fa02
MM
5764
5765 /* We can get a TEMPLATE_TEMPLATE_PARM here when
5766 we are resolving nested-types in the signature of
5767 a member function templates.
5768 Otherwise ARG is a TEMPLATE_DECL and is the real
5769 template to be instantiated. */
5770 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
5771 arg = TYPE_NAME (arg);
5772
5773 r = lookup_template_class (DECL_NAME (arg),
5774 argvec, in_decl,
5775 DECL_CONTEXT (arg),
5776 /*entering_scope=*/0);
91063b51 5777 return cp_build_qualified_type (r, TYPE_QUALS (t));
ae58fa02
MM
5778 }
5779 else
5780 /* We are processing a template argument list. */
5781 return arg;
5782 }
5783 else
5784 return arg;
5785 }
5786 }
51924768
JM
5787 else
5788 my_friendly_abort (981018);
ae58fa02
MM
5789
5790 if (level == 1)
5791 /* This can happen during the attempted tsubst'ing in
5792 unify. This means that we don't yet have any information
5793 about the template parameter in question. */
5794 return t;
5795
5796 /* If we get here, we must have been looking at a parm for a
5797 more deeply nested template. Make a new version of this
5798 template parameter, but with a lower level. */
5799 switch (TREE_CODE (t))
5800 {
5801 case TEMPLATE_TYPE_PARM:
5802 case TEMPLATE_TEMPLATE_PARM:
5803 r = copy_node (t);
5804 TEMPLATE_TYPE_PARM_INDEX (r)
5805 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
5806 r, levels);
5807 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
5808 TYPE_MAIN_VARIANT (r) = r;
5809 TYPE_POINTER_TO (r) = NULL_TREE;
5810 TYPE_REFERENCE_TO (r) = NULL_TREE;
5811
5812 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
7ddedda4 5813 && TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t))
ae58fa02 5814 {
7ddedda4
MM
5815 tree argvec = tsubst (TYPE_TI_ARGS (t), args, in_decl);
5816 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
ae58fa02
MM
5817 = perm_tree_cons (TYPE_NAME (t), argvec, NULL_TREE);
5818 }
5819 break;
5820
5821 case TEMPLATE_PARM_INDEX:
5822 r = reduce_template_parm_level (t, type, levels);
5823 break;
5824
5825 default:
5826 my_friendly_abort (0);
5827 }
5828
5566b478 5829 return r;
ae58fa02 5830 }
5566b478 5831
8d08fdba
MS
5832 case TREE_LIST:
5833 {
5834 tree purpose, value, chain, result;
5835 int via_public, via_virtual, via_protected;
5836
5837 if (t == void_list_node)
5838 return t;
5839
5840 via_public = TREE_VIA_PUBLIC (t);
5841 via_protected = TREE_VIA_PROTECTED (t);
5842 via_virtual = TREE_VIA_VIRTUAL (t);
5843
5844 purpose = TREE_PURPOSE (t);
5845 if (purpose)
f7d98d58 5846 purpose = tsubst (purpose, args, in_decl);
8d08fdba
MS
5847 value = TREE_VALUE (t);
5848 if (value)
f7d98d58 5849 value = tsubst (value, args, in_decl);
8d08fdba
MS
5850 chain = TREE_CHAIN (t);
5851 if (chain && chain != void_type_node)
f7d98d58 5852 chain = tsubst (chain, args, in_decl);
8d08fdba
MS
5853 if (purpose == TREE_PURPOSE (t)
5854 && value == TREE_VALUE (t)
5855 && chain == TREE_CHAIN (t))
5856 return t;
5857 result = hash_tree_cons (via_public, via_virtual, via_protected,
5858 purpose, value, chain);
5859 TREE_PARMLIST (result) = TREE_PARMLIST (t);
5860 return result;
5861 }
5862 case TREE_VEC:
5566b478
MS
5863 if (type != NULL_TREE)
5864 {
1ceaad38
JM
5865 /* A binfo node. We always need to make a copy, of the node
5866 itself and of its BINFO_BASETYPES. */
85b71cf2 5867
5566b478
MS
5868 t = copy_node (t);
5869
c7449f2b
JM
5870 /* Make sure type isn't a typedef copy. */
5871 type = BINFO_TYPE (TYPE_BINFO (type));
5872
5566b478 5873 TREE_TYPE (t) = complete_type (type);
6633d636
MS
5874 if (IS_AGGR_TYPE (type))
5875 {
5876 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
5877 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
5878 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
5879 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
5880 }
5566b478
MS
5881 return t;
5882 }
85b71cf2
JM
5883
5884 /* Otherwise, a vector of template arguments. */
830bfa74 5885 return tsubst_template_arg_vector (t, args);
8d08fdba 5886
8d08fdba
MS
5887 case POINTER_TYPE:
5888 case REFERENCE_TYPE:
5889 {
8d08fdba 5890 enum tree_code code;
79a7c7fa 5891
8d08fdba
MS
5892 if (type == TREE_TYPE (t))
5893 return t;
5894
5895 code = TREE_CODE (t);
0ecfe0b4
JM
5896 if (TREE_CODE (type) == REFERENCE_TYPE
5897 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
79a7c7fa
JM
5898 {
5899 static int last_line = 0;
5900 static char* last_file = 0;
5901
5902 /* We keep track of the last time we issued this error
5903 message to avoid spewing a ton of messages during a
5904 single bad template instantiation. */
5905 if (last_line != lineno ||
5906 last_file != input_filename)
5907 {
0ecfe0b4
JM
5908 if (TREE_CODE (type) == VOID_TYPE)
5909 cp_error ("forming reference to void");
5910 else
5911 cp_error ("forming %s to reference type `%T'",
5912 (code == POINTER_TYPE) ? "pointer" : "reference",
5913 type);
79a7c7fa
JM
5914 last_line = lineno;
5915 last_file = input_filename;
5916 }
5917
5918 /* Use the underlying type in an attempt at error
5919 recovery; maybe the user meant vector<int> and wrote
5920 vector<int&>, or some such. */
5921 if (code == REFERENCE_TYPE)
5922 r = type;
5923 else
5924 r = build_pointer_type (TREE_TYPE (type));
5925 }
5926 else if (code == POINTER_TYPE)
8d08fdba
MS
5927 r = build_pointer_type (type);
5928 else
5929 r = build_reference_type (type);
91063b51 5930 r = cp_build_qualified_type (r, TYPE_QUALS (t));
79a7c7fa 5931
8d08fdba
MS
5932 /* Will this ever be needed for TYPE_..._TO values? */
5933 layout_type (r);
5934 return r;
5935 }
a4443a08 5936 case OFFSET_TYPE:
0ecfe0b4
JM
5937 {
5938 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, in_decl);
5939 if (! IS_AGGR_TYPE (r))
5940 cp_error ("creating pointer to member of non-class type `%T'", r);
5941 return build_offset_type (r, type);
5942 }
8d08fdba
MS
5943 case FUNCTION_TYPE:
5944 case METHOD_TYPE:
5945 {
cabc336a
MM
5946 tree arg_types;
5947 tree raises;
c11b6f21 5948 tree fntype;
8d08fdba 5949
cabc336a
MM
5950 /* The TYPE_CONTEXT is not used for function/method types. */
5951 my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0);
5952
5953 /* Substitue the argument types. */
5954 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, in_decl);
8d08fdba
MS
5955
5956 /* Construct a new type node and return it. */
cabc336a
MM
5957 if (TREE_CODE (t) == FUNCTION_TYPE)
5958 fntype = build_function_type (type, arg_types);
0ecfe0b4
JM
5959 else
5960 {
5961 r = TREE_TYPE (TREE_VALUE (arg_types));
5962 if (! IS_AGGR_TYPE (r))
5963 cp_error ("creating pointer to member function of non-class type `%T'",
5964 r);
5965
5966 fntype = build_cplus_method_type (r, type, TREE_CHAIN (arg_types));
5967 }
91063b51 5968 fntype = build_qualified_type (fntype, TYPE_QUALS (t));
cabc336a
MM
5969
5970 /* Substitue the exception specification. */
5971 raises = TYPE_RAISES_EXCEPTIONS (t);
c11b6f21
MS
5972 if (raises)
5973 {
f7d98d58 5974 raises = tsubst (raises, args, in_decl);
c11b6f21
MS
5975 fntype = build_exception_variant (fntype, raises);
5976 }
5977 return fntype;
8d08fdba
MS
5978 }
5979 case ARRAY_TYPE:
5980 {
f7d98d58 5981 tree domain = tsubst (TYPE_DOMAIN (t), args, in_decl);
8d08fdba
MS
5982 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
5983 return t;
0ecfe0b4
JM
5984
5985 /* These checks should match the ones in grokdeclarator. */
5986 if (TREE_CODE (type) == VOID_TYPE)
5987 {
5988 cp_error ("creating array of void");
5989 type = build_pointer_type (type);
5990 }
5991 else if (TREE_CODE (type) == FUNCTION_TYPE)
5992 {
5993 cp_error ("creating array of functions `%T'", type);
5994 type = build_pointer_type (type);
5995 }
5996 else if (TREE_CODE (type) == REFERENCE_TYPE)
5997 {
5998 cp_error ("creating array of references `%T'", type);
5999 type = TREE_TYPE (type);
6000 }
6001
8d08fdba
MS
6002 r = build_cplus_array_type (type, domain);
6003 return r;
6004 }
6005
8d08fdba 6006 case PLUS_EXPR:
5566b478 6007 case MINUS_EXPR:
8d08fdba 6008 return fold (build (TREE_CODE (t), TREE_TYPE (t),
f7d98d58
JM
6009 tsubst (TREE_OPERAND (t, 0), args, in_decl),
6010 tsubst (TREE_OPERAND (t, 1), args, in_decl)));
8d08fdba
MS
6011
6012 case NEGATE_EXPR:
6013 case NOP_EXPR:
6014 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
f7d98d58 6015 tsubst (TREE_OPERAND (t, 0), args, in_decl)));
8d08fdba 6016
5566b478
MS
6017 case TYPENAME_TYPE:
6018 {
36a117a5
MM
6019 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, in_decl,
6020 /*entering_scope=*/1);
b2b7d40a 6021 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, in_decl);
ae58fa02
MM
6022
6023 /* Normally, make_typename_type does not require that the CTX
6024 have complete type in order to allow things like:
6025
6026 template <class T> struct S { typename S<T>::X Y; };
6027
6028 But, such constructs have already been resolved by this
6029 point, so here CTX really should have complete type, unless
6030 it's a partial instantiation. */
6031 if (!uses_template_parms (ctx)
6032 && !complete_type_or_else (ctx))
6033 return error_mark_node;
6034
b2b7d40a 6035 f = make_typename_type (ctx, f);
91063b51
MM
6036 return cp_build_qualified_type (f,
6037 CP_TYPE_QUALS (f)
6038 | CP_TYPE_QUALS (t));
5566b478
MS
6039 }
6040
6041 case INDIRECT_REF:
6042 return make_pointer_declarator
f7d98d58 6043 (type, tsubst (TREE_OPERAND (t, 0), args, in_decl));
5566b478
MS
6044
6045 case ADDR_EXPR:
6046 return make_reference_declarator
f7d98d58 6047 (type, tsubst (TREE_OPERAND (t, 0), args, in_decl));
5566b478
MS
6048
6049 case ARRAY_REF:
6050 return build_parse_node
f7d98d58
JM
6051 (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, in_decl),
6052 tsubst_expr (TREE_OPERAND (t, 1), args, in_decl));
5566b478
MS
6053
6054 case CALL_EXPR:
c11b6f21 6055 return make_call_declarator
f7d98d58
JM
6056 (tsubst (TREE_OPERAND (t, 0), args, in_decl),
6057 tsubst (TREE_OPERAND (t, 1), args, in_decl),
c11b6f21 6058 TREE_OPERAND (t, 2),
f7d98d58 6059 tsubst (TREE_TYPE (t), args, in_decl));
5566b478 6060
fc378698
MS
6061 case SCOPE_REF:
6062 return build_parse_node
f7d98d58
JM
6063 (TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, in_decl),
6064 tsubst (TREE_OPERAND (t, 1), args, in_decl));
fc378698 6065
b894fc05
JM
6066 case TYPEOF_TYPE:
6067 return TREE_TYPE (tsubst_expr (TYPE_FIELDS (t), args, in_decl));
6068
8d08fdba 6069 default:
5566b478 6070 sorry ("use of `%s' in template",
8d08fdba
MS
6071 tree_code_name [(int) TREE_CODE (t)]);
6072 return error_mark_node;
6073 }
6074}
6075
5566b478
MS
6076void
6077do_pushlevel ()
6078{
6079 emit_line_note (input_filename, lineno);
6080 pushlevel (0);
6081 clear_last_expr ();
6082 push_momentary ();
6083 expand_start_bindings (0);
6084}
6085
8d08fdba 6086tree
5566b478 6087do_poplevel ()
8d08fdba 6088{
5566b478 6089 tree t;
a703fb38 6090 int saved_warn_unused = 0;
8d08fdba 6091
85b71cf2
JM
6092 if (processing_template_decl)
6093 {
6094 saved_warn_unused = warn_unused;
6095 warn_unused = 0;
6096 }
8baa713c 6097 expand_end_bindings (getdecls (), kept_level_p (), 0);
85b71cf2
JM
6098 if (processing_template_decl)
6099 warn_unused = saved_warn_unused;
5566b478
MS
6100 t = poplevel (kept_level_p (), 1, 0);
6101 pop_momentary ();
6102 return t;
6103}
8d08fdba 6104
00d3396f
JM
6105/* Like tsubst, but deals with expressions. This function just replaces
6106 template parms; to finish processing the resultant expression, use
6107 tsubst_expr. */
6108
5566b478 6109tree
f7d98d58 6110tsubst_copy (t, args, in_decl)
98c1c668 6111 tree t, args;
5566b478
MS
6112 tree in_decl;
6113{
6114 enum tree_code code;
8452b1d3 6115 tree r;
8d08fdba 6116
5566b478
MS
6117 if (t == NULL_TREE || t == error_mark_node)
6118 return t;
6119
6120 code = TREE_CODE (t);
b7484fbe 6121
5566b478
MS
6122 switch (code)
6123 {
6124 case PARM_DECL:
a759e627 6125 return do_identifier (DECL_NAME (t), 0, NULL_TREE);
5566b478
MS
6126
6127 case CONST_DECL:
ed44da02
MM
6128 {
6129 tree enum_type;
6130 tree v;
6131
6132 if (!DECL_CONTEXT (t))
6133 /* This is a global enumeration constant. */
6134 return t;
6135
6136 /* Unfortunately, we cannot just call lookup_name here.
6137 Consider:
6138
6139 template <int I> int f() {
6140 enum E { a = I };
6141 struct S { void g() { E e = a; } };
6142 };
6143
6144 When we instantiate f<7>::S::g(), say, lookup_name is not
6145 clever enough to find f<7>::a. */
6146 enum_type
6147 = tsubst_aggr_type (TREE_TYPE (t), args, in_decl,
6148 /*entering_scope=*/0);
6149
6150 for (v = TYPE_VALUES (enum_type);
6151 v != NULL_TREE;
6152 v = TREE_CHAIN (v))
6153 if (TREE_PURPOSE (v) == DECL_NAME (t))
6154 return TREE_VALUE (v);
6155
6156 /* We didn't find the name. That should never happen; if
6157 name-lookup found it during preliminary parsing, we
6158 should find it again here during instantiation. */
6159 my_friendly_abort (0);
6160 }
db04386f 6161 return t;
ed44da02 6162
5566b478
MS
6163 case FIELD_DECL:
6164 if (DECL_CONTEXT (t))
6165 {
0978790f 6166 tree ctx;
0978790f 6167
36a117a5
MM
6168 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, in_decl,
6169 /*entering_scope=*/1);
0978790f 6170 if (ctx != DECL_CONTEXT (t))
5566b478
MS
6171 return lookup_field (ctx, DECL_NAME (t), 0, 0);
6172 }
6173 return t;
6174
6175 case VAR_DECL:
6176 case FUNCTION_DECL:
6177 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
f7d98d58 6178 t = tsubst (t, args, in_decl);
5566b478
MS
6179 mark_used (t);
6180 return t;
6181
98c1c668
JM
6182 case TEMPLATE_DECL:
6183 if (is_member_template (t))
f7d98d58 6184 return tsubst (t, args, in_decl);
98c1c668
JM
6185 else
6186 return t;
6187
672476cb
MM
6188 case LOOKUP_EXPR:
6189 {
6190 /* We must tsbust into a LOOKUP_EXPR in case the names to
6191 which it refers is a conversion operator; in that case the
6192 name will change. We avoid making unnecessary copies,
6193 however. */
6194
6195 tree id = tsubst_copy (TREE_OPERAND (t, 0), args, in_decl);
6196
6197 if (id != TREE_OPERAND (t, 0))
6198 {
8452b1d3 6199 r = build_nt (LOOKUP_EXPR, id);
672476cb
MM
6200 LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
6201 t = r;
6202 }
6203
6204 return t;
6205 }
6206
5566b478
MS
6207 case CAST_EXPR:
6208 case REINTERPRET_CAST_EXPR:
e92cc029
MS
6209 case CONST_CAST_EXPR:
6210 case STATIC_CAST_EXPR:
6211 case DYNAMIC_CAST_EXPR:
51924768 6212 case NOP_EXPR:
5566b478 6213 return build1
f7d98d58
JM
6214 (code, tsubst (TREE_TYPE (t), args, in_decl),
6215 tsubst_copy (TREE_OPERAND (t, 0), args, in_decl));
5566b478
MS
6216
6217 case INDIRECT_REF:
6218 case PREDECREMENT_EXPR:
6219 case PREINCREMENT_EXPR:
6220 case POSTDECREMENT_EXPR:
6221 case POSTINCREMENT_EXPR:
6222 case NEGATE_EXPR:
6223 case TRUTH_NOT_EXPR:
b87692e5 6224 case BIT_NOT_EXPR:
5566b478
MS
6225 case ADDR_EXPR:
6226 case CONVERT_EXPR: /* Unary + */
6227 case SIZEOF_EXPR:
abff8e06 6228 case ALIGNOF_EXPR:
5566b478 6229 case ARROW_EXPR:
fc378698 6230 case THROW_EXPR:
5156628f 6231 case TYPEID_EXPR:
5566b478
MS
6232 return build1
6233 (code, NULL_TREE,
f7d98d58 6234 tsubst_copy (TREE_OPERAND (t, 0), args, in_decl));
5566b478
MS
6235
6236 case PLUS_EXPR:
6237 case MINUS_EXPR:
6238 case MULT_EXPR:
6239 case TRUNC_DIV_EXPR:
6240 case CEIL_DIV_EXPR:
6241 case FLOOR_DIV_EXPR:
6242 case ROUND_DIV_EXPR:
6243 case EXACT_DIV_EXPR:
6244 case BIT_AND_EXPR:
6245 case BIT_ANDTC_EXPR:
6246 case BIT_IOR_EXPR:
6247 case BIT_XOR_EXPR:
6248 case TRUNC_MOD_EXPR:
6249 case FLOOR_MOD_EXPR:
6250 case TRUTH_ANDIF_EXPR:
6251 case TRUTH_ORIF_EXPR:
6252 case TRUTH_AND_EXPR:
6253 case TRUTH_OR_EXPR:
6254 case RSHIFT_EXPR:
6255 case LSHIFT_EXPR:
6256 case RROTATE_EXPR:
6257 case LROTATE_EXPR:
6258 case EQ_EXPR:
6259 case NE_EXPR:
6260 case MAX_EXPR:
6261 case MIN_EXPR:
6262 case LE_EXPR:
6263 case GE_EXPR:
6264 case LT_EXPR:
6265 case GT_EXPR:
6266 case COMPONENT_REF:
6267 case ARRAY_REF:
6268 case COMPOUND_EXPR:
6269 case SCOPE_REF:
6270 case DOTSTAR_EXPR:
6271 case MEMBER_REF:
6272 return build_nt
f7d98d58
JM
6273 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
6274 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl));
5566b478
MS
6275
6276 case CALL_EXPR:
6277 {
6278 tree fn = TREE_OPERAND (t, 0);
f84b4be9 6279 if (is_overloaded_fn (fn))
f7d98d58 6280 fn = tsubst_copy (get_first_fn (fn), args, in_decl);
5566b478 6281 else
f84b4be9 6282 /* Sometimes FN is a LOOKUP_EXPR. */
f7d98d58 6283 fn = tsubst_copy (fn, args, in_decl);
5566b478 6284 return build_nt
f7d98d58 6285 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
5566b478
MS
6286 NULL_TREE);
6287 }
6288
6289 case METHOD_CALL_EXPR:
6290 {
6291 tree name = TREE_OPERAND (t, 0);
6292 if (TREE_CODE (name) == BIT_NOT_EXPR)
6293 {
f7d98d58 6294 name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
7bae46f4 6295 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
5566b478
MS
6296 }
6297 else if (TREE_CODE (name) == SCOPE_REF
6298 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
6299 {
f7d98d58 6300 tree base = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
5566b478 6301 name = TREE_OPERAND (name, 1);
f7d98d58 6302 name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
11686454 6303 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
5566b478
MS
6304 name = build_nt (SCOPE_REF, base, name);
6305 }
6306 else
f7d98d58 6307 name = tsubst_copy (TREE_OPERAND (t, 0), args, in_decl);
5566b478 6308 return build_nt
f7d98d58
JM
6309 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
6310 tsubst_copy (TREE_OPERAND (t, 2), args, in_decl),
5566b478
MS
6311 NULL_TREE);
6312 }
6313
67da3287 6314 case BIND_EXPR:
5566b478
MS
6315 case COND_EXPR:
6316 case MODOP_EXPR:
67da3287 6317 {
8452b1d3 6318 r = build_nt
f7d98d58
JM
6319 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
6320 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
6321 tsubst_copy (TREE_OPERAND (t, 2), args, in_decl));
67da3287
MM
6322
6323 if (code == BIND_EXPR && !processing_template_decl)
6324 {
6325 /* This processing should really occur in tsubst_expr,
6326 However, tsubst_expr does not recurse into expressions,
6327 since it assumes that there aren't any statements
6328 inside them. Instead, it simply calls
6329 build_expr_from_tree. So, we need to expand the
6330 BIND_EXPR here. */
b4c4a9ec 6331 tree rtl_expr = begin_stmt_expr ();
544c188d 6332 tree block = tsubst_expr (TREE_OPERAND (r, 1), args, in_decl);
b4c4a9ec 6333 r = finish_stmt_expr (rtl_expr, block);
67da3287
MM
6334 }
6335
6336 return r;
6337 }
5566b478
MS
6338
6339 case NEW_EXPR:
6340 {
8452b1d3 6341 r = build_nt
f7d98d58
JM
6342 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
6343 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
6344 tsubst_copy (TREE_OPERAND (t, 2), args, in_decl));
5566b478
MS
6345 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
6346 return r;
6347 }
6348
6349 case DELETE_EXPR:
6350 {
8452b1d3 6351 r = build_nt
f7d98d58
JM
6352 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
6353 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl));
5566b478
MS
6354 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
6355 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
6356 return r;
6357 }
6358
386b8a85
JM
6359 case TEMPLATE_ID_EXPR:
6360 {
00d3396f 6361 /* Substituted template arguments */
f7d98d58 6362 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, in_decl);
00d3396f
JM
6363 tree chain;
6364 for (chain = targs; chain; chain = TREE_CHAIN (chain))
6365 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
6366
6367 return lookup_template_function
f7d98d58 6368 (tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), targs);
386b8a85
JM
6369 }
6370
5566b478
MS
6371 case TREE_LIST:
6372 {
6373 tree purpose, value, chain;
6374
6375 if (t == void_list_node)
6376 return t;
6377
6378 purpose = TREE_PURPOSE (t);
6379 if (purpose)
f7d98d58 6380 purpose = tsubst_copy (purpose, args, in_decl);
5566b478
MS
6381 value = TREE_VALUE (t);
6382 if (value)
f7d98d58 6383 value = tsubst_copy (value, args, in_decl);
5566b478
MS
6384 chain = TREE_CHAIN (t);
6385 if (chain && chain != void_type_node)
f7d98d58 6386 chain = tsubst_copy (chain, args, in_decl);
5566b478
MS
6387 if (purpose == TREE_PURPOSE (t)
6388 && value == TREE_VALUE (t)
6389 && chain == TREE_CHAIN (t))
6390 return t;
6391 return tree_cons (purpose, value, chain);
6392 }
6393
6394 case RECORD_TYPE:
6395 case UNION_TYPE:
6396 case ENUMERAL_TYPE:
6397 case INTEGER_TYPE:
6398 case TEMPLATE_TYPE_PARM:
73b0fce8 6399 case TEMPLATE_TEMPLATE_PARM:
f84b4be9 6400 case TEMPLATE_PARM_INDEX:
5566b478
MS
6401 case POINTER_TYPE:
6402 case REFERENCE_TYPE:
6403 case OFFSET_TYPE:
6404 case FUNCTION_TYPE:
6405 case METHOD_TYPE:
6406 case ARRAY_TYPE:
6407 case TYPENAME_TYPE:
f84b4be9 6408 case TYPE_DECL:
f7d98d58 6409 return tsubst (t, args, in_decl);
5566b478 6410
e92cc029 6411 case IDENTIFIER_NODE:
8e710788
BK
6412 if (IDENTIFIER_TYPENAME_P (t)
6413 /* Make sure it's not just a variable named `__opr', for instance,
6414 which can occur in some existing code. */
6415 && TREE_TYPE (t))
e92cc029 6416 return build_typename_overload
f7d98d58 6417 (tsubst (TREE_TYPE (t), args, in_decl));
e92cc029
MS
6418 else
6419 return t;
6420
5156628f 6421 case CONSTRUCTOR:
8452b1d3
JM
6422 {
6423 r = build
6424 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, in_decl), NULL_TREE,
6425 tsubst_copy (CONSTRUCTOR_ELTS (t), args, in_decl));
6426 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
6427 return r;
6428 }
5156628f 6429
5566b478
MS
6430 default:
6431 return t;
6432 }
6433}
6434
00d3396f
JM
6435/* Like tsubst_copy, but also does semantic processing and RTL expansion. */
6436
5566b478 6437tree
f7d98d58 6438tsubst_expr (t, args, in_decl)
98c1c668 6439 tree t, args;
5566b478
MS
6440 tree in_decl;
6441{
6442 if (t == NULL_TREE || t == error_mark_node)
6443 return t;
6444
5156628f 6445 if (processing_template_decl)
f7d98d58 6446 return tsubst_copy (t, args, in_decl);
5566b478
MS
6447
6448 switch (TREE_CODE (t))
8d08fdba 6449 {
5566b478
MS
6450 case RETURN_STMT:
6451 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6452 finish_return_stmt (tsubst_expr (RETURN_EXPR (t),
6453 args, in_decl));
5566b478
MS
6454 break;
6455
6456 case EXPR_STMT:
6457 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6458 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
6459 args, in_decl));
5566b478
MS
6460 break;
6461
6462 case DECL_STMT:
6463 {
6464 int i = suspend_momentary ();
67d743fe 6465 tree dcl, init;
5566b478
MS
6466
6467 lineno = TREE_COMPLEXITY (t);
6468 emit_line_note (input_filename, lineno);
6469 dcl = start_decl
f7d98d58
JM
6470 (tsubst (TREE_OPERAND (t, 0), args, in_decl),
6471 tsubst (TREE_OPERAND (t, 1), args, in_decl),
a1774733 6472 TREE_OPERAND (t, 2) != 0, NULL_TREE, NULL_TREE);
f7d98d58 6473 init = tsubst_expr (TREE_OPERAND (t, 2), args, in_decl);
5566b478 6474 cp_finish_decl
a0128b67 6475 (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
5566b478
MS
6476 resume_momentary (i);
6477 return dcl;
6478 }
8d08fdba 6479
5566b478
MS
6480 case FOR_STMT:
6481 {
6482 tree tmp;
5566b478 6483 lineno = TREE_COMPLEXITY (t);
5566b478 6484
ad321293
MM
6485 begin_for_stmt ();
6486 for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp))
6487 tsubst_expr (tmp, args, in_decl);
6488 finish_for_init_stmt (NULL_TREE);
6489 finish_for_cond (tsubst_expr (FOR_COND (t), args,
6490 in_decl),
6491 NULL_TREE);
6492 tmp = tsubst_expr (FOR_EXPR (t), args, in_decl);
6493 finish_for_expr (tmp, NULL_TREE);
6494 tsubst_expr (FOR_BODY (t), args, in_decl);
6495 finish_for_stmt (tmp, NULL_TREE);
5566b478
MS
6496 }
6497 break;
8d08fdba 6498
5566b478
MS
6499 case WHILE_STMT:
6500 {
5566b478 6501 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6502 begin_while_stmt ();
6503 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
6504 args, in_decl),
6505 NULL_TREE);
6506 tsubst_expr (WHILE_BODY (t), args, in_decl);
6507 finish_while_stmt (NULL_TREE);
5566b478
MS
6508 }
6509 break;
8d08fdba 6510
5566b478
MS
6511 case DO_STMT:
6512 {
5566b478 6513 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6514 begin_do_stmt ();
6515 tsubst_expr (DO_BODY (t), args, in_decl);
6516 finish_do_body (NULL_TREE);
6517 finish_do_stmt (tsubst_expr (DO_COND (t), args,
6518 in_decl),
6519 NULL_TREE);
5566b478
MS
6520 }
6521 break;
a0a33927 6522
5566b478 6523 case IF_STMT:
8d08fdba 6524 {
5566b478 6525 tree tmp;
5566b478
MS
6526
6527 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6528 begin_if_stmt ();
6529 finish_if_stmt_cond (tsubst_expr (IF_COND (t),
6530 args, in_decl),
6531 NULL_TREE);
db5ae43f 6532
ad321293 6533 if (tmp = THEN_CLAUSE (t), tmp)
db5ae43f 6534 {
f7d98d58 6535 tsubst_expr (tmp, args, in_decl);
ad321293 6536 finish_then_clause (NULL_TREE);
db5ae43f
MS
6537 }
6538
ad321293
MM
6539 if (tmp = ELSE_CLAUSE (t), tmp)
6540 {
6541 begin_else_clause ();
6542 tsubst_expr (tmp, args, in_decl);
6543 finish_else_clause (NULL_TREE);
6544 }
8d08fdba 6545
ad321293 6546 finish_if_stmt ();
8d08fdba 6547 }
5566b478 6548 break;
8d08fdba 6549
5566b478
MS
6550 case COMPOUND_STMT:
6551 {
ad321293 6552 tree substmt;
8d08fdba 6553
5566b478 6554 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6555 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
6556 for (substmt = COMPOUND_BODY (t);
6557 substmt != NULL_TREE;
6558 substmt = TREE_CHAIN (substmt))
f7d98d58 6559 tsubst_expr (substmt, args, in_decl);
ad321293
MM
6560 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t),
6561 NULL_TREE);
5566b478
MS
6562 }
6563 break;
8d08fdba 6564
5566b478
MS
6565 case BREAK_STMT:
6566 lineno = TREE_COMPLEXITY (t);
ad321293 6567 finish_break_stmt ();
5566b478 6568 break;
8d08fdba 6569
6467930b
MS
6570 case CONTINUE_STMT:
6571 lineno = TREE_COMPLEXITY (t);
ad321293 6572 finish_continue_stmt ();
6467930b
MS
6573 break;
6574
5566b478
MS
6575 case SWITCH_STMT:
6576 {
6577 tree val, tmp;
5566b478
MS
6578
6579 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6580 begin_switch_stmt ();
6581 val = tsubst_expr (SWITCH_COND (t), args, in_decl);
6582 finish_switch_cond (val);
5566b478
MS
6583
6584 if (tmp = TREE_OPERAND (t, 1), tmp)
f7d98d58 6585 tsubst_expr (tmp, args, in_decl);
8d08fdba 6586
ad321293 6587 finish_switch_stmt (val, NULL_TREE);
5566b478
MS
6588 }
6589 break;
6590
6591 case CASE_LABEL:
ad321293
MM
6592 finish_case_label (tsubst_expr (CASE_LOW (t), args, in_decl),
6593 tsubst_expr (CASE_HIGH (t), args, in_decl));
5566b478
MS
6594 break;
6595
6596 case LABEL_DECL:
6597 t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
6598 DECL_NAME (t));
6599 if (t)
6600 expand_label (t);
6601 break;
6602
6603 case GOTO_STMT:
6604 lineno = TREE_COMPLEXITY (t);
aa09da44
MM
6605 t = GOTO_DESTINATION (t);
6606 if (TREE_CODE (t) != IDENTIFIER_NODE)
6607 /* Computed goto's must be tsubst'd into. On the other hand,
6608 non-computed gotos must not be; the identifier in question
6609 will have no binding. */
6610 t = tsubst_expr (t, args, in_decl);
6611 finish_goto_stmt (t);
ad321293
MM
6612 break;
6613
6614 case ASM_STMT:
6615 lineno = TREE_COMPLEXITY (t);
6616 finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t), args, in_decl),
6617 tsubst_expr (ASM_STRING (t), args, in_decl),
6618 tsubst_expr (ASM_OUTPUTS (t), args, in_decl),
6619 tsubst_expr (ASM_INPUTS (t), args, in_decl),
6620 tsubst_expr (ASM_CLOBBERS (t), args, in_decl));
5566b478 6621 break;
faf5394a
MS
6622
6623 case TRY_BLOCK:
6624 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6625 begin_try_block ();
6626 tsubst_expr (TRY_STMTS (t), args, in_decl);
6627 finish_try_block (NULL_TREE);
faf5394a 6628 {
ad321293 6629 tree handler = TRY_HANDLERS (t);
faf5394a 6630 for (; handler; handler = TREE_CHAIN (handler))
f7d98d58 6631 tsubst_expr (handler, args, in_decl);
faf5394a 6632 }
ad321293 6633 finish_handler_sequence (NULL_TREE);
faf5394a
MS
6634 break;
6635
6636 case HANDLER:
6637 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6638 begin_handler ();
6639 if (HANDLER_PARMS (t))
faf5394a 6640 {
ad321293 6641 tree d = HANDLER_PARMS (t);
faf5394a 6642 expand_start_catch_block
f7d98d58
JM
6643 (tsubst (TREE_OPERAND (d, 1), args, in_decl),
6644 tsubst (TREE_OPERAND (d, 0), args, in_decl));
faf5394a
MS
6645 }
6646 else
6647 expand_start_catch_block (NULL_TREE, NULL_TREE);
ad321293
MM
6648 finish_handler_parms (NULL_TREE);
6649 tsubst_expr (HANDLER_BODY (t), args, in_decl);
6650 finish_handler (NULL_TREE);
faf5394a
MS
6651 break;
6652
b87692e5
MS
6653 case TAG_DEFN:
6654 lineno = TREE_COMPLEXITY (t);
6655 t = TREE_TYPE (t);
6656 if (TREE_CODE (t) == ENUMERAL_TYPE)
ed44da02 6657 tsubst (t, args, NULL_TREE);
b87692e5
MS
6658 break;
6659
5566b478 6660 default:
f7d98d58 6661 return build_expr_from_tree (tsubst_copy (t, args, in_decl));
5566b478
MS
6662 }
6663 return NULL_TREE;
8d08fdba
MS
6664}
6665
6ba89f8e 6666/* Instantiate the indicated variable or function template TMPL with
36a117a5
MM
6667 the template arguments in TARG_PTR. */
6668
5566b478
MS
6669tree
6670instantiate_template (tmpl, targ_ptr)
98c1c668 6671 tree tmpl, targ_ptr;
8d08fdba 6672{
5566b478 6673 tree fndecl;
36a117a5
MM
6674 tree gen_tmpl;
6675 tree spec;
5566b478
MS
6676 int i, len;
6677 struct obstack *old_fmp_obstack;
6678 extern struct obstack *function_maybepermanent_obstack;
36a117a5 6679 tree inner_args;
5566b478 6680
27fafc8d
JM
6681 if (tmpl == error_mark_node)
6682 return error_mark_node;
6683
386b8a85
JM
6684 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
6685
36a117a5
MM
6686 /* Check to see if we already have this specialization. */
6687 spec = retrieve_specialization (tmpl, targ_ptr);
6688 if (spec != NULL_TREE)
6689 return spec;
6690
6691 if (DECL_TEMPLATE_INFO (tmpl))
386b8a85 6692 {
36a117a5
MM
6693 /* The TMPL is a partial instantiation. To get a full set of
6694 arguments we must add the arguments used to perform the
6695 partial instantiation. */
6696 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
6697 targ_ptr);
6698 gen_tmpl = most_general_template (tmpl);
6699
6700 /* Check to see if we already have this specialization. */
6701 spec = retrieve_specialization (gen_tmpl, targ_ptr);
75650646
MM
6702 if (spec != NULL_TREE)
6703 return spec;
386b8a85 6704 }
36a117a5
MM
6705 else
6706 gen_tmpl = tmpl;
386b8a85 6707
5566b478
MS
6708 push_obstacks (&permanent_obstack, &permanent_obstack);
6709 old_fmp_obstack = function_maybepermanent_obstack;
6710 function_maybepermanent_obstack = &permanent_obstack;
8d08fdba 6711
36a117a5
MM
6712 len = DECL_NTPARMS (gen_tmpl);
6713 inner_args = innermost_args (targ_ptr);
5566b478
MS
6714 i = len;
6715 while (i--)
8d08fdba 6716 {
36a117a5 6717 tree t = TREE_VEC_ELT (inner_args, i);
5566b478
MS
6718 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
6719 {
6720 tree nt = target_type (t);
ec255269 6721 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
5566b478 6722 {
8251199e
JM
6723 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
6724 cp_error (" trying to instantiate `%D'", gen_tmpl);
5566b478
MS
6725 fndecl = error_mark_node;
6726 goto out;
6727 }
6728 }
8d08fdba 6729 }
e66d884e 6730 targ_ptr = copy_to_permanent (targ_ptr);
8d08fdba 6731
5566b478 6732 /* substitute template parameters */
36a117a5
MM
6733 fndecl = tsubst (DECL_RESULT (gen_tmpl), targ_ptr, gen_tmpl);
6734 /* The DECL_TI_TEMPLATE should always be the immediate parent
6735 template, not the most general template. */
6736 DECL_TI_TEMPLATE (fndecl) = tmpl;
8d08fdba 6737
824b9a4c
MS
6738 if (flag_external_templates)
6739 add_pending_template (fndecl);
6740
5566b478
MS
6741 out:
6742 function_maybepermanent_obstack = old_fmp_obstack;
6743 pop_obstacks ();
8d08fdba 6744
5566b478 6745 return fndecl;
8d08fdba 6746}
5566b478
MS
6747
6748/* Push the name of the class template into the scope of the instantiation. */
8d08fdba
MS
6749
6750void
5566b478
MS
6751overload_template_name (type)
6752 tree type;
8d08fdba 6753{
5566b478
MS
6754 tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
6755 tree decl;
8d08fdba 6756
5566b478
MS
6757 if (IDENTIFIER_CLASS_VALUE (id)
6758 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
6759 return;
8d08fdba 6760
5566b478
MS
6761 decl = build_decl (TYPE_DECL, id, type);
6762 SET_DECL_ARTIFICIAL (decl);
6763 pushdecl_class_level (decl);
8d08fdba
MS
6764}
6765
956d6950 6766/* Like type_unification but designed specially to handle conversion
9f54c803
MM
6767 operators.
6768
6769 The FN is a TEMPLATE_DECL for a function. The ARGS are the
6770 arguments that are being used when calling it.
6771
6772 If FN is a conversion operator, RETURN_TYPE is the type desired as
6773 the result of the conversion operator.
6774
6775 The EXTRA_FN_ARG, if any, is the type of an additional
6776 parameter to be added to the beginning of FN's parameter list.
6777
6778 The other arguments are as for type_unification. */
98c1c668
JM
6779
6780int
d7684f2d
MM
6781fn_type_unification (fn, explicit_targs, targs, args, return_type,
6782 strict, extra_fn_arg)
386b8a85 6783 tree fn, explicit_targs, targs, args, return_type;
830bfa74 6784 unification_kind_t strict;
d7684f2d 6785 tree extra_fn_arg;
98c1c668 6786{
9f54c803 6787 tree parms;
98c1c668
JM
6788
6789 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
6790
9f54c803
MM
6791 parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
6792
aa45967f 6793 if (DECL_CONV_FN_P (fn))
98c1c668
JM
6794 {
6795 /* This is a template conversion operator. Use the return types
6796 as well as the argument types. */
9f54c803
MM
6797 parms = scratch_tree_cons (NULL_TREE,
6798 TREE_TYPE (TREE_TYPE (fn)),
6799 parms);
6800 args = scratch_tree_cons (NULL_TREE, return_type, args);
98c1c668
JM
6801 }
6802
d7684f2d 6803 if (extra_fn_arg != NULL_TREE)
9f54c803 6804 parms = scratch_tree_cons (NULL_TREE, extra_fn_arg, parms);
d7684f2d 6805
7cd55873
MM
6806 /* We allow incomplete unification without an error message here
6807 because the standard doesn't seem to explicitly prohibit it. Our
6808 callers must be ready to deal with unification failures in any
6809 event. */
9f54c803
MM
6810 return type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn),
6811 targs,
6812 parms,
6813 args,
6814 explicit_targs,
6815 strict, 1);
98c1c668
JM
6816}
6817
6818
8d08fdba
MS
6819/* Type unification.
6820
6821 We have a function template signature with one or more references to
6822 template parameters, and a parameter list we wish to fit to this
6823 template. If possible, produce a list of parameters for the template
6824 which will cause it to fit the supplied parameter list.
6825
6826 Return zero for success, 2 for an incomplete match that doesn't resolve
6827 all the types, and 1 for complete failure. An error message will be
6828 printed only for an incomplete match.
6829
9f54c803 6830 TPARMS[NTPARMS] is an array of template parameter types.
8d08fdba 6831
9f54c803
MM
6832 TARGS[NTPARMS] is the array into which the deduced template
6833 parameter values are placed. PARMS is the function template's
6834 signature (using TEMPLATE_PARM_IDX nodes), and ARGS is the argument
6835 list we're trying to match against it.
6836
6837 The EXPLICIT_TARGS are explicit template arguments provided via a
6838 template-id.
6467930b 6839
830bfa74
MM
6840 The parameter STRICT is one of:
6841
6842 DEDUCE_CALL:
6843 We are deducing arguments for a function call, as in
6844 [temp.deduct.call].
6845
6846 DEDUCE_CONV:
6847 We are deducing arguments for a conversion function, as in
6848 [temp.deduct.conv].
6849
6850 DEDUCE_EXACT:
6851 We are deducing arguments when calculating the partial
6852 ordering between specializations of function or class
6853 templates, as in [temp.func.order] and [temp.class.order],
6854 when doing an explicit instantiation as in [temp.explicit],
9f54c803 6855 when determining an explicit specialization as in
830bfa74
MM
6856 [temp.expl.spec], or when taking the address of a function
6857 template, as in [temp.deduct.funcaddr]. */
8d08fdba
MS
6858
6859int
9f54c803 6860type_unification (tparms, targs, parms, args, explicit_targs,
386b8a85 6861 strict, allow_incomplete)
9f54c803 6862 tree tparms, targs, parms, args, explicit_targs;
830bfa74
MM
6863 unification_kind_t strict;
6864 int allow_incomplete;
386b8a85 6865{
050367a3 6866 int* explicit_mask;
386b8a85 6867 int i;
386b8a85 6868
830bfa74 6869 for (i = 0; i < TREE_VEC_LENGTH (tparms); i++)
050367a3 6870 TREE_VEC_ELT (targs, i) = NULL_TREE;
386b8a85 6871
9f54c803 6872 if (explicit_targs != NULL_TREE)
75650646
MM
6873 {
6874 tree arg_vec;
9f54c803 6875 arg_vec = coerce_template_parms (tparms, explicit_targs, NULL_TREE, 0,
744fac59 6876 0);
75650646
MM
6877
6878 if (arg_vec == error_mark_node)
6879 return 1;
386b8a85 6880
050367a3 6881 explicit_mask = alloca (sizeof (int) * TREE_VEC_LENGTH (targs));
7dee3f36 6882 bzero ((char *) explicit_mask, sizeof(int) * TREE_VEC_LENGTH (targs));
050367a3 6883
75650646
MM
6884 for (i = 0;
6885 i < TREE_VEC_LENGTH (arg_vec)
050367a3 6886 && TREE_VEC_ELT (arg_vec, i) != NULL_TREE;
75650646 6887 ++i)
050367a3
MM
6888 {
6889 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (arg_vec, i);
6890 /* Let unify know that this argument was explicit. */
6891 explicit_mask [i] = 1;
6892 }
75650646 6893 }
050367a3
MM
6894 else
6895 explicit_mask = 0;
386b8a85 6896
830bfa74
MM
6897 return
6898 type_unification_real (tparms, targs, parms, args, 0,
6899 strict, allow_incomplete, explicit_mask);
6900}
6901
6902/* Adjust types before performing type deduction, as described in
6903 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
4c7d0dff
MM
6904 sections are symmetric. PARM is the type of a function parameter
6905 or the return type of the conversion function. ARG is the type of
6906 the argument passed to the call, or the type of the value
6907 intialized with the result of the conversion function. */
386b8a85 6908
830bfa74
MM
6909void
6910maybe_adjust_types_for_deduction (strict, parm, arg)
6911 unification_kind_t strict;
6912 tree* parm;
6913 tree* arg;
6914{
6915 switch (strict)
6916 {
6917 case DEDUCE_CALL:
6918 break;
6919
6920 case DEDUCE_CONV:
6921 {
4c7d0dff
MM
6922 /* Swap PARM and ARG throughout the remainder of this
6923 function; the handling is precisely symmetric since PARM
6924 will initialize ARG rather than vice versa. */
830bfa74
MM
6925 tree* temp = parm;
6926 parm = arg;
6927 arg = temp;
6928 break;
6929 }
6930
6931 case DEDUCE_EXACT:
6932 /* There is nothing to do in this case. */
6933 return;
6934
6935 default:
6936 my_friendly_abort (0);
6937 }
6938
6939 if (TREE_CODE (*parm) != REFERENCE_TYPE)
6940 {
6941 /* [temp.deduct.call]
6942
6943 If P is not a reference type:
6944
6945 --If A is an array type, the pointer type produced by the
6946 array-to-pointer standard conversion (_conv.array_) is
6947 used in place of A for type deduction; otherwise,
6948
6949 --If A is a function type, the pointer type produced by
6950 the function-to-pointer standard conversion
6951 (_conv.func_) is used in place of A for type deduction;
6952 otherwise,
6953
6954 --If A is a cv-qualified type, the top level
6955 cv-qualifiers of A's type are ignored for type
6956 deduction. */
6957 if (TREE_CODE (*arg) == ARRAY_TYPE)
6958 *arg = build_pointer_type (TREE_TYPE (*arg));
d8f8dca1 6959 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
830bfa74
MM
6960 *arg = build_pointer_type (*arg);
6961 else
6962 *arg = TYPE_MAIN_VARIANT (*arg);
6963 }
6964
6965 /* [temp.deduct.call]
6966
6967 If P is a cv-qualified type, the top level cv-qualifiers
6968 of P's type are ignored for type deduction. If P is a
6969 reference type, the type referred to by P is used for
6970 type deduction. */
6971 *parm = TYPE_MAIN_VARIANT (*parm);
6972 if (TREE_CODE (*parm) == REFERENCE_TYPE)
6973 *parm = TREE_TYPE (*parm);
386b8a85
JM
6974}
6975
050367a3
MM
6976/* Like type_unfication. EXPLICIT_MASK, if non-NULL, is an array of
6977 integers, with ones in positions corresponding to arguments in
9f54c803
MM
6978 targs that were provided explicitly, and zeros elsewhere.
6979
6980 If SUBR is 1, we're being called recursively (to unify the
6981 arguments of a function or method parameter of a function
6982 template). */
386b8a85 6983
4966381a 6984static int
3b3ba9f0 6985type_unification_real (tparms, targs, parms, args, subr,
050367a3
MM
6986 strict, allow_incomplete, explicit_mask)
6987 tree tparms, targs, parms, args;
830bfa74
MM
6988 int subr;
6989 unification_kind_t strict;
6990 int allow_incomplete;
050367a3 6991 int* explicit_mask;
8d08fdba
MS
6992{
6993 tree parm, arg;
6994 int i;
6995 int ntparms = TREE_VEC_LENGTH (tparms);
830bfa74 6996 int sub_strict;
8d08fdba
MS
6997
6998 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
386b8a85
JM
6999 my_friendly_assert (parms == NULL_TREE
7000 || TREE_CODE (parms) == TREE_LIST, 290);
51c184be 7001 /* ARGS could be NULL (via a call from parse.y to
8d08fdba
MS
7002 build_x_function_call). */
7003 if (args)
7004 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
7005 my_friendly_assert (ntparms > 0, 292);
7006
830bfa74
MM
7007 switch (strict)
7008 {
7009 case DEDUCE_CALL:
7010 sub_strict = UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_DERIVED;
7011 break;
7012
7013 case DEDUCE_CONV:
7014 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
7015 break;
7016
7017 case DEDUCE_EXACT:
7018 sub_strict = UNIFY_ALLOW_NONE;
7019 break;
7020
7021 default:
7022 my_friendly_abort (0);
7023 }
7024
8d08fdba
MS
7025 while (parms
7026 && parms != void_list_node
7027 && args
7028 && args != void_list_node)
7029 {
7030 parm = TREE_VALUE (parms);
7031 parms = TREE_CHAIN (parms);
7032 arg = TREE_VALUE (args);
7033 args = TREE_CHAIN (args);
7034
7035 if (arg == error_mark_node)
7036 return 1;
7037 if (arg == unknown_type_node)
34016c81
JM
7038 /* We can't deduce anything from this, but we might get all the
7039 template args from other function args. */
7040 continue;
b7484fbe 7041
03e70705
JM
7042 /* Conversions will be performed on a function argument that
7043 corresponds with a function parameter that contains only
7044 non-deducible template parameters and explicitly specified
7045 template parameters. */
7046 if (! uses_template_parms (parm))
b7484fbe 7047 {
03e70705
JM
7048 tree type;
7049
7050 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
7051 type = TREE_TYPE (arg);
7052 else
7053 {
7054 type = arg;
7055 arg = NULL_TREE;
7056 }
7057
830bfa74 7058 if (strict == DEDUCE_EXACT)
03e70705 7059 {
3bfdc719 7060 if (same_type_p (parm, type))
03e70705
JM
7061 continue;
7062 }
03e70705 7063 else
343c89cd
JM
7064 /* It might work; we shouldn't check now, because we might
7065 get into infinite recursion. Overload resolution will
7066 handle it. */
7067 continue;
03e70705 7068
b7484fbe
MS
7069 return 1;
7070 }
7071
8d08fdba
MS
7072#if 0
7073 if (TREE_CODE (arg) == VAR_DECL)
7074 arg = TREE_TYPE (arg);
7075 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
7076 arg = TREE_TYPE (arg);
7077#else
7078 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
7079 {
7080 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
34016c81 7081 if (type_unknown_p (arg))
28cbf42c 7082 {
34016c81
JM
7083 /* [temp.deduct.type] A template-argument can be deduced from
7084 a pointer to function or pointer to member function
7085 argument if the set of overloaded functions does not
7086 contain function templates and at most one of a set of
7087 overloaded functions provides a unique match. */
7088
7089 if (resolve_overloaded_unification
7090 (tparms, targs, parm, arg, strict, sub_strict, explicit_mask)
7091 != 0)
7092 return 1;
7093 continue;
28cbf42c 7094 }
8d08fdba
MS
7095 arg = TREE_TYPE (arg);
7096 }
7097#endif
830bfa74
MM
7098 if (!subr)
7099 maybe_adjust_types_for_deduction (strict, &parm, &arg);
db5ae43f 7100
830bfa74 7101 switch (unify (tparms, targs, parm, arg, sub_strict,
050367a3 7102 explicit_mask))
8d08fdba
MS
7103 {
7104 case 0:
7105 break;
7106 case 1:
7107 return 1;
7108 }
7109 }
7110 /* Fail if we've reached the end of the parm list, and more args
7111 are present, and the parm list isn't variadic. */
7112 if (args && args != void_list_node && parms == void_list_node)
7113 return 1;
7114 /* Fail if parms are left and they don't have default values. */
7115 if (parms
7116 && parms != void_list_node
7117 && TREE_PURPOSE (parms) == NULL_TREE)
7118 return 1;
7119 if (!subr)
7120 for (i = 0; i < ntparms; i++)
050367a3 7121 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
8d08fdba 7122 {
386b8a85 7123 if (!allow_incomplete)
8251199e 7124 error ("incomplete type unification");
8d08fdba
MS
7125 return 2;
7126 }
7127 return 0;
7128}
7129
34016c81
JM
7130/* Subroutine of type_unification_real. Args are like the variables at the
7131 call site. ARG is an overloaded function (or template-id); we try
7132 deducing template args from each of the overloads, and if only one
7133 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
7134
7135static int
7136resolve_overloaded_unification (tparms, targs, parm, arg, strict,
7137 sub_strict, explicit_mask)
7138 tree tparms, targs, parm, arg;
7139 unification_kind_t strict;
7140 int sub_strict;
7141 int* explicit_mask;
7142{
7143 tree tempargs = copy_node (targs);
7144 int good = 0;
7145
7146 if (TREE_CODE (arg) == ADDR_EXPR)
7147 arg = TREE_OPERAND (arg, 0);
9f3d9e46 7148
d8f8dca1
MM
7149 if (TREE_CODE (arg) == COMPONENT_REF)
7150 /* Handle `&x' where `x' is some static or non-static member
7151 function name. */
7152 arg = TREE_OPERAND (arg, 1);
7153
9f3d9e46
JM
7154 /* Strip baselink information. */
7155 while (TREE_CODE (arg) == TREE_LIST)
7156 arg = TREE_VALUE (arg);
7157
34016c81
JM
7158 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
7159 {
7160 /* If we got some explicit template args, we need to plug them into
7161 the affected templates before we try to unify, in case the
7162 explicit args will completely resolve the templates in question. */
7163
7164 tree expl_subargs = TREE_OPERAND (arg, 1);
7165 arg = TREE_OPERAND (arg, 0);
7166
7167 for (; arg; arg = OVL_NEXT (arg))
7168 {
7169 tree fn = OVL_CURRENT (arg);
7170 tree subargs, elem;
7171
7172 if (TREE_CODE (fn) != TEMPLATE_DECL)
7173 continue;
7174
7175 subargs = get_bindings_overload (fn, DECL_RESULT (fn), expl_subargs);
7176 if (subargs)
7177 {
7178 elem = tsubst (TREE_TYPE (fn), subargs, NULL_TREE);
d8f8dca1
MM
7179 if (TREE_CODE (elem) == METHOD_TYPE)
7180 elem = build_ptrmemfunc_type (build_pointer_type (elem));
e97e5263 7181 good += try_one_overload (tparms, targs, tempargs, parm, elem,
34016c81
JM
7182 strict, sub_strict, explicit_mask);
7183 }
7184 }
7185 }
7186 else if (TREE_CODE (arg) == OVERLOAD)
7187 {
7188 for (; arg; arg = OVL_NEXT (arg))
d8f8dca1
MM
7189 {
7190 tree type = TREE_TYPE (OVL_CURRENT (arg));
7191 if (TREE_CODE (type) == METHOD_TYPE)
7192 type = build_ptrmemfunc_type (build_pointer_type (type));
7193 good += try_one_overload (tparms, targs, tempargs, parm,
7194 type,
7195 strict, sub_strict, explicit_mask);
7196 }
34016c81
JM
7197 }
7198 else
7199 my_friendly_abort (981006);
7200
7201 /* [temp.deduct.type] A template-argument can be deduced from a pointer
7202 to function or pointer to member function argument if the set of
7203 overloaded functions does not contain function templates and at most
7204 one of a set of overloaded functions provides a unique match.
7205
7206 So if we found multiple possibilities, we return success but don't
7207 deduce anything. */
7208
7209 if (good == 1)
7210 {
7211 int i = TREE_VEC_LENGTH (targs);
7212 for (; i--; )
7213 if (TREE_VEC_ELT (tempargs, i))
7214 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
7215 }
7216 if (good)
7217 return 0;
7218
7219 return 1;
7220}
7221
7222/* Subroutine of resolve_overloaded_unification; does deduction for a single
7223 overload. Fills TARGS with any deduced arguments, or error_mark_node if
7224 different overloads deduce different arguments for a given parm.
7225 Returns 1 on success. */
7226
7227static int
e97e5263 7228try_one_overload (tparms, orig_targs, targs, parm, arg, strict,
34016c81 7229 sub_strict, explicit_mask)
e97e5263 7230 tree tparms, orig_targs, targs, parm, arg;
34016c81
JM
7231 unification_kind_t strict;
7232 int sub_strict;
7233 int* explicit_mask;
7234{
7235 int nargs;
7236 tree tempargs;
7237 int i;
7238
7239 /* [temp.deduct.type] A template-argument can be deduced from a pointer
7240 to function or pointer to member function argument if the set of
7241 overloaded functions does not contain function templates and at most
7242 one of a set of overloaded functions provides a unique match.
7243
7244 So if this is a template, just return success. */
7245
7246 if (uses_template_parms (arg))
7247 return 1;
7248
7249 maybe_adjust_types_for_deduction (strict, &parm, &arg);
7250
7251 /* We don't copy orig_targs for this because if we have already deduced
7252 some template args from previous args, unify would complain when we
7253 try to deduce a template parameter for the same argument, even though
7254 there isn't really a conflict. */
7255 nargs = TREE_VEC_LENGTH (targs);
7256 tempargs = make_scratch_vec (nargs);
7257
7258 if (unify (tparms, tempargs, parm, arg, sub_strict, explicit_mask) != 0)
7259 return 0;
7260
7261 /* First make sure we didn't deduce anything that conflicts with
e97e5263 7262 explicitly specified args. */
34016c81
JM
7263 for (i = nargs; i--; )
7264 {
7265 tree elt = TREE_VEC_ELT (tempargs, i);
e97e5263 7266 tree oldelt = TREE_VEC_ELT (orig_targs, i);
34016c81
JM
7267
7268 if (elt == NULL_TREE)
7269 continue;
7270 else if (uses_template_parms (elt))
7271 {
7272 /* Since we're unifying against ourselves, we will fill in template
7273 args used in the function parm list with our own template parms.
7274 Discard them. */
7275 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
7276 continue;
7277 }
7278 else if (oldelt && ! template_args_equal (oldelt, elt))
7279 return 0;
7280 }
7281
7282 for (i = nargs; i--; )
7283 {
7284 tree elt = TREE_VEC_ELT (tempargs, i);
7285
7286 if (elt)
7287 TREE_VEC_ELT (targs, i) = elt;
7288 }
7289
7290 return 1;
7291}
7292
db2767b6
MM
7293/* Returns the level of DECL, which declares a template parameter. */
7294
04a81d94 7295int
db2767b6
MM
7296template_decl_level (decl)
7297 tree decl;
7298{
7299 switch (TREE_CODE (decl))
7300 {
7301 case TYPE_DECL:
7302 case TEMPLATE_DECL:
7303 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
7304
7305 case PARM_DECL:
7306 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
7307
7308 default:
7309 my_friendly_abort (0);
5ffe581d 7310 return 0;
db2767b6
MM
7311 }
7312}
7313
830bfa74
MM
7314/* Decide whether ARG can be unified with PARM, considering only the
7315 cv-qualifiers of each type, given STRICT as documented for unify.
7316 Returns non-zero iff the unification is OK on that basis.*/
e92cc029 7317
830bfa74
MM
7318int
7319check_cv_quals_for_unify (strict, arg, parm)
7320 int strict;
7321 tree arg;
7322 tree parm;
7323{
7324 return !((!(strict & UNIFY_ALLOW_MORE_CV_QUAL)
91063b51 7325 && !at_least_as_qualified_p (arg, parm))
830bfa74 7326 || (!(strict & UNIFY_ALLOW_LESS_CV_QUAL)
91063b51 7327 && (!at_least_as_qualified_p (parm, arg))));
830bfa74
MM
7328}
7329
7330/* Takes parameters as for type_unification. Returns 0 if the
7331 type deduction suceeds, 1 otherwise. The parameter STRICT is a
7332 bitwise or of the following flags:
7333
7334 UNIFY_ALLOW_NONE:
7335 Require an exact match between PARM and ARG.
7336 UNIFY_ALLOW_MORE_CV_QUAL:
7337 Allow the deduced ARG to be more cv-qualified than ARG.
7338 UNIFY_ALLOW_LESS_CV_QUAL:
7339 Allow the deduced ARG to be less cv-qualified than ARG.
7340 UNIFY_ALLOW_DERIVED:
7341 Allow the deduced ARG to be a template base class of ARG,
7342 or a pointer to a template base class of the type pointed to by
161c12b0
JM
7343 ARG.
7344 UNIFY_ALLOW_INTEGER:
7345 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
7346 case for more information. */
830bfa74
MM
7347
7348int
7349unify (tparms, targs, parm, arg, strict, explicit_mask)
050367a3 7350 tree tparms, targs, parm, arg;
830bfa74 7351 int strict;
050367a3 7352 int* explicit_mask;
8d08fdba
MS
7353{
7354 int idx;
050367a3 7355 tree targ;
db2767b6 7356 tree tparm;
8d08fdba
MS
7357
7358 /* I don't think this will do the right thing with respect to types.
7359 But the only case I've seen it in so far has been array bounds, where
7360 signedness is the only information lost, and I think that will be
7361 okay. */
7362 while (TREE_CODE (parm) == NOP_EXPR)
7363 parm = TREE_OPERAND (parm, 0);
7364
7365 if (arg == error_mark_node)
7366 return 1;
7367 if (arg == unknown_type_node)
34016c81
JM
7368 /* We can't deduce anything from this, but we might get all the
7369 template args from other function args. */
7370 return 0;
7371
db2767b6 7372 /* If PARM uses template parameters, then we can't bail out here,
6bdb985f 7373 even if ARG == PARM, since we won't record unifications for the
db2767b6
MM
7374 template parameters. We might need them if we're trying to
7375 figure out which of two things is more specialized. */
7376 if (arg == parm && !uses_template_parms (parm))
8d08fdba
MS
7377 return 0;
7378
830bfa74
MM
7379 /* Immediately reject some pairs that won't unify because of
7380 cv-qualification mismatches. */
7381 if (TREE_CODE (arg) == TREE_CODE (parm)
49432171 7382 && TREE_CODE_CLASS (TREE_CODE (arg)) == 't'
830bfa74
MM
7383 /* We check the cv-qualifiers when unifying with template type
7384 parameters below. We want to allow ARG `const T' to unify with
7385 PARM `T' for example, when computing which of two templates
7386 is more specialized, for example. */
7387 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
7388 && !check_cv_quals_for_unify (strict, arg, parm))
49432171
JM
7389 return 1;
7390
8d08fdba
MS
7391 switch (TREE_CODE (parm))
7392 {
2ca340ae
JM
7393 case TYPENAME_TYPE:
7394 /* In a type which contains a nested-name-specifier, template
7395 argument values cannot be deduced for template parameters used
7396 within the nested-name-specifier. */
7397 return 0;
7398
8d08fdba 7399 case TEMPLATE_TYPE_PARM:
73b0fce8 7400 case TEMPLATE_TEMPLATE_PARM:
db2767b6
MM
7401 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
7402
7403 if (TEMPLATE_TYPE_LEVEL (parm)
7404 != template_decl_level (tparm))
7405 /* The PARM is not one we're trying to unify. Just check
7406 to see if it matches ARG. */
7407 return (TREE_CODE (arg) == TREE_CODE (parm)
3bfdc719 7408 && same_type_p (parm, arg)) ? 0 : 1;
73b0fce8 7409 idx = TEMPLATE_TYPE_IDX (parm);
050367a3 7410 targ = TREE_VEC_ELT (targs, idx);
db2767b6 7411 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
050367a3 7412
73b0fce8 7413 /* Check for mixed types and values. */
db2767b6
MM
7414 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
7415 && TREE_CODE (tparm) != TYPE_DECL)
7416 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
7417 && TREE_CODE (tparm) != TEMPLATE_DECL))
73b0fce8
KL
7418 return 1;
7419
050367a3
MM
7420 if (!strict && targ != NULL_TREE
7421 && explicit_mask && explicit_mask[idx])
73b0fce8
KL
7422 /* An explicit template argument. Don't even try to match
7423 here; the overload resolution code will manage check to
7424 see whether the call is legal. */
7425 return 0;
7426
db2767b6 7427 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
73b0fce8 7428 {
7ddedda4 7429 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm))
db2767b6
MM
7430 {
7431 /* We arrive here when PARM does not involve template
7432 specialization. */
73b0fce8 7433
db2767b6
MM
7434 /* ARG must be constructed from a template class. */
7435 if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg))
7436 return 1;
73b0fce8 7437
db2767b6 7438 {
7ddedda4
MM
7439 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
7440 tree parmvec = TYPE_TI_ARGS (parm);
db2767b6
MM
7441 tree argvec = CLASSTYPE_TI_ARGS (arg);
7442 tree argtmplvec
7443 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg));
7444 int i;
7445
7446 /* The parameter and argument roles have to be switched here
7447 in order to handle default arguments properly. For example,
7448 template<template <class> class TT> void f(TT<int>)
7449 should be able to accept vector<int> which comes from
744fac59 7450 template <class T, class Allocator = allocator>
73b0fce8
KL
7451 class vector. */
7452
744fac59 7453 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 1, 1)
db2767b6
MM
7454 == error_mark_node)
7455 return 1;
73b0fce8 7456
db2767b6
MM
7457 /* Deduce arguments T, i from TT<T> or TT<i>. */
7458 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
7459 {
7460 tree t = TREE_VEC_ELT (parmvec, i);
7461 if (TREE_CODE (t) != TEMPLATE_TYPE_PARM
7462 && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM
7463 && TREE_CODE (t) != TEMPLATE_PARM_INDEX)
7464 continue;
73b0fce8 7465
db2767b6 7466 /* This argument can be deduced. */
73b0fce8 7467
830bfa74
MM
7468 if (unify (tparms, targs, t,
7469 TREE_VEC_ELT (argvec, i),
7470 UNIFY_ALLOW_NONE, explicit_mask))
db2767b6
MM
7471 return 1;
7472 }
73b0fce8 7473 }
db2767b6
MM
7474 arg = CLASSTYPE_TI_TEMPLATE (arg);
7475 }
7476 }
7477 else
7478 {
830bfa74
MM
7479 /* If PARM is `const T' and ARG is only `int', we don't have
7480 a match unless we are allowing additional qualification.
7481 If ARG is `const int' and PARM is just `T' that's OK;
7482 that binds `const int' to `T'. */
7483 if (!check_cv_quals_for_unify (strict | UNIFY_ALLOW_LESS_CV_QUAL,
7484 arg, parm))
db2767b6
MM
7485 return 1;
7486
830bfa74
MM
7487 /* Consider the case where ARG is `const volatile int' and
7488 PARM is `const T'. Then, T should be `volatile int'. */
7489 arg =
91063b51
MM
7490 cp_build_qualified_type (arg,
7491 CP_TYPE_QUALS (arg)
7492 & ~CP_TYPE_QUALS (parm));
73b0fce8
KL
7493 }
7494
7495 /* Simple cases: Value already set, does match or doesn't. */
3a56f0ab 7496 if (targ != NULL_TREE
3bfdc719 7497 && (same_type_p (targ, arg)
3a56f0ab 7498 || (explicit_mask && explicit_mask[idx])))
73b0fce8 7499 return 0;
050367a3 7500 else if (targ)
73b0fce8 7501 return 1;
050367a3 7502 TREE_VEC_ELT (targs, idx) = arg;
73b0fce8
KL
7503 return 0;
7504
f84b4be9 7505 case TEMPLATE_PARM_INDEX:
db2767b6
MM
7506 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
7507
7508 if (TEMPLATE_PARM_LEVEL (parm)
7509 != template_decl_level (tparm))
7510 /* The PARM is not one we're trying to unify. Just check
7511 to see if it matches ARG. */
7512 return (TREE_CODE (arg) == TREE_CODE (parm)
67ffc812 7513 && cp_tree_equal (parm, arg) > 0) ? 0 : 1;
db2767b6 7514
f84b4be9 7515 idx = TEMPLATE_PARM_IDX (parm);
050367a3 7516 targ = TREE_VEC_ELT (targs, idx);
db2767b6 7517
050367a3 7518 if (targ)
8d08fdba 7519 {
67ffc812 7520 int i = (cp_tree_equal (targ, arg) > 0);
312e7d50
JM
7521 if (i == 1)
7522 return 0;
7523 else if (i == 0)
7524 return 1;
7525 else
7526 my_friendly_abort (42);
8d08fdba 7527 }
8d08fdba 7528
161c12b0
JM
7529 /* [temp.deduct.type] If, in the declaration of a function template
7530 with a non-type template-parameter, the non-type
7531 template-parameter is used in an expression in the function
7532 parameter-list and, if the corresponding template-argument is
7533 deduced, the template-argument type shall match the type of the
7534 template-parameter exactly, except that a template-argument
7535 deduced from an array bound may be of any integral type. */
7536 if (same_type_p (TREE_TYPE (arg), TREE_TYPE (parm)))
7537 /* OK */;
7538 else if ((strict & UNIFY_ALLOW_INTEGER)
7539 && (TREE_CODE (TREE_TYPE (parm)) == INTEGER_TYPE
7540 || TREE_CODE (TREE_TYPE (parm)) == BOOLEAN_TYPE))
7541 /* OK */;
7542 else
7543 return 1;
7544
050367a3 7545 TREE_VEC_ELT (targs, idx) = copy_to_permanent (arg);
8d08fdba
MS
7546 return 0;
7547
7548 case POINTER_TYPE:
830bfa74
MM
7549 {
7550 int sub_strict;
4ac14744 7551
830bfa74
MM
7552 if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
7553 return (unify (tparms, targs, parm,
7554 TYPE_PTRMEMFUNC_FN_TYPE (arg), strict,
7555 explicit_mask));
7556
7557 if (TREE_CODE (arg) != POINTER_TYPE)
7558 return 1;
7559
7560 /* [temp.deduct.call]
7561
7562 A can be another pointer or pointer to member type that can
7563 be converted to the deduced A via a qualification
7564 conversion (_conv.qual_).
7565
7566 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
7567 This will allow for additional cv-qualification of the
7568 pointed-to types if appropriate. In general, this is a bit
7569 too generous; we are only supposed to allow qualification
7570 conversions and this method will allow an ARG of char** and
7571 a deduced ARG of const char**. However, overload
7572 resolution will subsequently invalidate the candidate, so
7573 this is probably OK. */
7574 sub_strict = strict;
7575
7576 if (TREE_CODE (TREE_TYPE (arg)) != RECORD_TYPE
7577 || TYPE_PTRMEMFUNC_FLAG (TREE_TYPE (arg)))
7578 /* The derived-to-base conversion only persists through one
7579 level of pointers. */
7580 sub_strict &= ~UNIFY_ALLOW_DERIVED;
7581
7582 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE
7583 (arg), sub_strict, explicit_mask);
7584 }
8d08fdba
MS
7585
7586 case REFERENCE_TYPE:
830bfa74
MM
7587 if (TREE_CODE (arg) != REFERENCE_TYPE)
7588 return 1;
7589 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
7590 UNIFY_ALLOW_NONE, explicit_mask);
8d08fdba
MS
7591
7592 case ARRAY_TYPE:
7593 if (TREE_CODE (arg) != ARRAY_TYPE)
7594 return 1;
3042d5be
MM
7595 if ((TYPE_DOMAIN (parm) == NULL_TREE)
7596 != (TYPE_DOMAIN (arg) == NULL_TREE))
7597 return 1;
7598 if (TYPE_DOMAIN (parm) != NULL_TREE
830bfa74
MM
7599 && unify (tparms, targs, TYPE_DOMAIN (parm),
7600 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE, explicit_mask) != 0)
8d08fdba 7601 return 1;
830bfa74
MM
7602 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
7603 UNIFY_ALLOW_NONE, explicit_mask);
8d08fdba
MS
7604
7605 case REAL_TYPE:
37c46b43 7606 case COMPLEX_TYPE:
8d08fdba 7607 case INTEGER_TYPE:
42976354 7608 case BOOLEAN_TYPE:
5ad5a526 7609 case VOID_TYPE:
f376e137
MS
7610 if (TREE_CODE (arg) != TREE_CODE (parm))
7611 return 1;
7612
514a1f18
JM
7613 if (TREE_CODE (parm) == INTEGER_TYPE
7614 && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST)
8d08fdba
MS
7615 {
7616 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
830bfa74 7617 && unify (tparms, targs, TYPE_MIN_VALUE (parm),
161c12b0
JM
7618 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER,
7619 explicit_mask))
8d08fdba
MS
7620 return 1;
7621 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
830bfa74 7622 && unify (tparms, targs, TYPE_MAX_VALUE (parm),
161c12b0
JM
7623 TYPE_MAX_VALUE (arg), UNIFY_ALLOW_INTEGER,
7624 explicit_mask))
8d08fdba
MS
7625 return 1;
7626 }
514a1f18
JM
7627 /* We use the TYPE_MAIN_VARIANT since we have already
7628 checked cv-qualification at the top of the
7629 function. */
3bfdc719
MM
7630 else if (!same_type_p (TYPE_MAIN_VARIANT (arg),
7631 TYPE_MAIN_VARIANT (parm)))
ca79f85d
JM
7632 return 1;
7633
8d08fdba
MS
7634 /* As far as unification is concerned, this wins. Later checks
7635 will invalidate it if necessary. */
7636 return 0;
7637
7638 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
bd6dd845 7639 /* Type INTEGER_CST can come from ordinary constant template args. */
8d08fdba 7640 case INTEGER_CST:
bd6dd845
MS
7641 while (TREE_CODE (arg) == NOP_EXPR)
7642 arg = TREE_OPERAND (arg, 0);
7643
8d08fdba
MS
7644 if (TREE_CODE (arg) != INTEGER_CST)
7645 return 1;
7646 return !tree_int_cst_equal (parm, arg);
7647
8d08fdba
MS
7648 case TREE_VEC:
7649 {
7650 int i;
7651 if (TREE_CODE (arg) != TREE_VEC)
7652 return 1;
7653 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
7654 return 1;
7655 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
830bfa74 7656 if (unify (tparms, targs,
8d08fdba 7657 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
830bfa74 7658 UNIFY_ALLOW_NONE, explicit_mask))
8d08fdba
MS
7659 return 1;
7660 return 0;
7661 }
7662
8d08fdba 7663 case RECORD_TYPE:
f181d4ae 7664 case UNION_TYPE:
db5ae43f 7665 if (TYPE_PTRMEMFUNC_FLAG (parm))
830bfa74 7666 return unify (tparms, targs, TYPE_PTRMEMFUNC_FN_TYPE (parm),
050367a3 7667 arg, strict, explicit_mask);
8d08fdba 7668
f181d4ae 7669 if (TREE_CODE (arg) != TREE_CODE (parm))
a4443a08 7670 return 1;
830bfa74 7671
5db698f6 7672 if (CLASSTYPE_TEMPLATE_INFO (parm))
5566b478 7673 {
6467930b 7674 tree t = NULL_TREE;
830bfa74
MM
7675 if (strict & UNIFY_ALLOW_DERIVED)
7676 /* [temp.deduct.call]
7677
7678 If P is a class, and P has the form template-id, then A
7679 can be a derived class of the deduced A. Likewise, if
7680 P is a pointer to a class of the form template-id, A
7681 can be a pointer to a derived class pointed to by the
6df47b06
MM
7682 deduced A.
7683
7684 The call to get_template_base also handles the case
7685 where PARM and ARG are the same type, i.e., where no
7686 derivation is involved. */
6467930b 7687 t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg);
6df47b06 7688 else if (CLASSTYPE_TEMPLATE_INFO (arg)
9fbf56f7
MM
7689 && (CLASSTYPE_TI_TEMPLATE (parm)
7690 == CLASSTYPE_TI_TEMPLATE (arg)))
6df47b06
MM
7691 /* Perhaps PARM is something like S<U> and ARG is S<int>.
7692 Then, we should unify `int' and `U'. */
6467930b 7693 t = arg;
6df47b06 7694
6467930b 7695 if (! t || t == error_mark_node)
5566b478 7696 return 1;
6467930b 7697
830bfa74
MM
7698 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
7699 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE,
7700 explicit_mask);
5566b478 7701 }
3bfdc719
MM
7702 else if (!same_type_p (TYPE_MAIN_VARIANT (parm),
7703 TYPE_MAIN_VARIANT (arg)))
5566b478 7704 return 1;
a4443a08 7705 return 0;
8d08fdba
MS
7706
7707 case METHOD_TYPE:
8d08fdba 7708 case FUNCTION_TYPE:
830bfa74 7709 if (TREE_CODE (arg) != TREE_CODE (parm))
8d08fdba 7710 return 1;
830bfa74
MM
7711
7712 if (unify (tparms, targs, TREE_TYPE (parm),
7713 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explicit_mask))
28cbf42c 7714 return 1;
386b8a85 7715 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
3b3ba9f0 7716 TYPE_ARG_TYPES (arg), 1,
830bfa74 7717 DEDUCE_EXACT, 0, explicit_mask);
a4443a08
MS
7718
7719 case OFFSET_TYPE:
7720 if (TREE_CODE (arg) != OFFSET_TYPE)
7721 return 1;
830bfa74
MM
7722 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
7723 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE, explicit_mask))
a4443a08 7724 return 1;
830bfa74 7725 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9f3d9e46 7726 strict, explicit_mask);
a4443a08 7727
f62dbf03 7728 case CONST_DECL:
830bfa74 7729 if (arg != decl_constant_value (parm))
f62dbf03
JM
7730 return 1;
7731 return 0;
7732
027905b4
KL
7733 case TEMPLATE_DECL:
7734 /* Matched cases are handled by the ARG == PARM test above. */
7735 return 1;
7736
53929c47
JM
7737 case MINUS_EXPR:
7738 if (TREE_CODE (TREE_OPERAND (parm, 1)) == INTEGER_CST)
7739 {
7740 /* We handle this case specially, since it comes up with
7741 arrays. In particular, something like:
7742
7743 template <int N> void f(int (&x)[N]);
7744
7745 Here, we are trying to unify the range type, which
7746 looks like [0 ... (N - 1)]. */
7747 tree t, t1, t2;
7748 t1 = TREE_OPERAND (parm, 0);
7749 t2 = TREE_OPERAND (parm, 1);
7750
7751 /* Should this be a regular fold? */
7752 t = maybe_fold_nontype_arg (build (PLUS_EXPR,
7753 integer_type_node,
7754 arg, t2));
7755
161c12b0 7756 return unify (tparms, targs, t1, t, strict, explicit_mask);
53929c47
JM
7757 }
7758 /* else fall through */
7759
8d08fdba 7760 default:
050367a3
MM
7761 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
7762 {
7763 /* We're looking at an expression. This can happen with
7764 something like:
7765
7766 template <int I>
7767 void foo(S<I>, S<I + 2>);
7768
7769 If the call looked like:
7770
7771 foo(S<2>(), S<4>());
7772
7773 we would have already matched `I' with `2'. Now, we'd
7774 like to know if `4' matches `I + 2'. So, we substitute
7775 into that expression, and fold constants, in the hope of
7776 figuring it out. */
7777 tree t =
7778 maybe_fold_nontype_arg (tsubst_expr (parm, targs, NULL_TREE));
53929c47 7779 tree a = maybe_fold_nontype_arg (arg);
050367a3 7780
53929c47 7781 if (!IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t))))
050367a3 7782 /* Good, we mangaged to simplify the exression. */
53929c47 7783 return unify (tparms, targs, t, a, UNIFY_ALLOW_NONE,
050367a3
MM
7784 explicit_mask);
7785 else
7786 /* Bad, we couldn't simplify this. Assume it doesn't
7787 unify. */
7788 return 1;
7789 }
7790 else
b4c4a9ec
MM
7791 sorry ("use of `%s' in template type unification",
7792 tree_code_name [(int) TREE_CODE (parm)]);
050367a3 7793
8d08fdba
MS
7794 return 1;
7795 }
7796}
8d08fdba 7797\f
03d0f4af
MM
7798/* Called if RESULT is explicitly instantiated, or is a member of an
7799 explicitly instantiated class, or if using -frepo and the
7800 instantiation of RESULT has been assigned to this file. */
7801
faae18ab 7802void
5566b478 7803mark_decl_instantiated (result, extern_p)
faae18ab
MS
7804 tree result;
7805 int extern_p;
7806{
75650646
MM
7807 if (TREE_CODE (result) != FUNCTION_DECL)
7808 /* The TREE_PUBLIC flag for function declarations will have been
7809 set correctly by tsubst. */
7810 TREE_PUBLIC (result) = 1;
faae18ab
MS
7811
7812 if (! extern_p)
7813 {
7814 DECL_INTERFACE_KNOWN (result) = 1;
7815 DECL_NOT_REALLY_EXTERN (result) = 1;
a7d87521 7816
1a408d07
JM
7817 /* Always make artificials weak. */
7818 if (DECL_ARTIFICIAL (result) && flag_weak)
7819 comdat_linkage (result);
a7d87521
JM
7820 /* For WIN32 we also want to put explicit instantiations in
7821 linkonce sections. */
1a408d07 7822 else if (TREE_PUBLIC (result))
b385c841 7823 maybe_make_one_only (result);
faae18ab 7824 }
f49422da
MS
7825 else if (TREE_CODE (result) == FUNCTION_DECL)
7826 mark_inline_for_output (result);
faae18ab
MS
7827}
7828
e1467ff2
MM
7829/* Given two function templates PAT1 and PAT2, and explicit template
7830 arguments EXPLICIT_ARGS return:
6467930b
MS
7831
7832 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
7833 -1 if PAT2 is more specialized than PAT1.
7834 0 if neither is more specialized. */
7835
7836int
e1467ff2
MM
7837more_specialized (pat1, pat2, explicit_args)
7838 tree pat1, pat2, explicit_args;
6467930b 7839{
98c1c668 7840 tree targs;
73aad9b9 7841 int winner = 0;
6467930b 7842
76b9a14d 7843 targs = get_bindings_overload (pat1, pat2, explicit_args);
73aad9b9 7844 if (targs)
36a117a5 7845 --winner;
6467930b 7846
76b9a14d 7847 targs = get_bindings_overload (pat2, pat1, explicit_args);
73aad9b9 7848 if (targs)
36a117a5 7849 ++winner;
6467930b 7850
73aad9b9
JM
7851 return winner;
7852}
6467930b 7853
73aad9b9 7854/* Given two class template specialization list nodes PAT1 and PAT2, return:
6467930b 7855
73aad9b9
JM
7856 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
7857 -1 if PAT2 is more specialized than PAT1.
7858 0 if neither is more specialized. */
7859
7860int
7861more_specialized_class (pat1, pat2)
7862 tree pat1, pat2;
7863{
7864 tree targs;
7865 int winner = 0;
7866
36a117a5
MM
7867 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
7868 TREE_PURPOSE (pat2));
73aad9b9
JM
7869 if (targs)
7870 --winner;
7871
36a117a5
MM
7872 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
7873 TREE_PURPOSE (pat1));
73aad9b9 7874 if (targs)
6467930b
MS
7875 ++winner;
7876
7877 return winner;
7878}
73aad9b9
JM
7879
7880/* Return the template arguments that will produce the function signature
e1467ff2 7881 DECL from the function template FN, with the explicit template
76b9a14d
JM
7882 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
7883 also match. */
73aad9b9 7884
76b9a14d
JM
7885static tree
7886get_bindings_real (fn, decl, explicit_args, check_rettype)
e1467ff2 7887 tree fn, decl, explicit_args;
76b9a14d 7888 int check_rettype;
73aad9b9 7889{
98c1c668 7890 int ntparms = DECL_NTPARMS (fn);
e66d884e 7891 tree targs = make_scratch_vec (ntparms);
d7684f2d
MM
7892 tree decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
7893 tree extra_fn_arg = NULL_TREE;
98c1c668
JM
7894 int i;
7895
d7684f2d
MM
7896 if (DECL_STATIC_FUNCTION_P (fn)
7897 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
7898 {
7899 /* Sometimes we are trying to figure out what's being
7900 specialized by a declaration that looks like a method, and it
7901 turns out to be a static member function. */
7902 if (CLASSTYPE_TEMPLATE_INFO (DECL_REAL_CONTEXT (fn))
7903 && !is_member_template (fn))
7904 /* The natural thing to do here seems to be to remove the
7905 spurious `this' parameter from the DECL, but that prevents
7906 unification from making use of the class type. So,
7907 instead, we have fn_type_unification add to the parameters
7908 for FN. */
7909 extra_fn_arg = build_pointer_type (DECL_REAL_CONTEXT (fn));
7910 else
7911 /* In this case, though, adding the extra_fn_arg can confuse
7912 things, so we remove from decl_arg_types instead. */
7913 decl_arg_types = TREE_CHAIN (decl_arg_types);
7914 }
7915
e1467ff2 7916 i = fn_type_unification (fn, explicit_args, targs,
d7684f2d 7917 decl_arg_types,
98c1c668 7918 TREE_TYPE (TREE_TYPE (decl)),
830bfa74 7919 DEDUCE_EXACT,
d7684f2d 7920 extra_fn_arg);
98c1c668 7921
76b9a14d
JM
7922 if (i != 0)
7923 return NULL_TREE;
7924
7925 if (check_rettype)
e1467ff2
MM
7926 {
7927 /* Check to see that the resulting return type is also OK. */
36a117a5 7928 tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)), targs,
e6f1275f 7929 NULL_TREE);
e1467ff2 7930
3bfdc719 7931 if (!same_type_p (t, TREE_TYPE (TREE_TYPE (decl))))
e1467ff2 7932 return NULL_TREE;
e1467ff2
MM
7933 }
7934
76b9a14d
JM
7935 return targs;
7936}
7937
7938/* For most uses, we want to check the return type. */
7939
7940tree
7941get_bindings (fn, decl, explicit_args)
7942 tree fn, decl, explicit_args;
7943{
7944 return get_bindings_real (fn, decl, explicit_args, 1);
7945}
7946
7947/* But for more_specialized, we only care about the parameter types. */
7948
7949static tree
7950get_bindings_overload (fn, decl, explicit_args)
7951 tree fn, decl, explicit_args;
7952{
7953 return get_bindings_real (fn, decl, explicit_args, 0);
73aad9b9
JM
7954}
7955
36a117a5
MM
7956/* Return the innermost template arguments that, when applied to a
7957 template specialization whose innermost template parameters are
7958 TPARMS, and whose specialization arguments are ARGS, yield the
7959 ARGS.
7960
7961 For example, suppose we have:
7962
7963 template <class T, class U> struct S {};
7964 template <class T> struct S<T*, int> {};
7965
7966 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
7967 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
7968 int}. The resulting vector will be {double}, indicating that `T'
7969 is bound to `double'. */
7970
bd6dd845 7971static tree
36a117a5
MM
7972get_class_bindings (tparms, parms, args)
7973 tree tparms, parms, args;
73aad9b9 7974{
3b3ba9f0 7975 int i, ntparms = TREE_VEC_LENGTH (tparms);
73aad9b9
JM
7976 tree vec = make_temp_vec (ntparms);
7977
36a117a5 7978 args = innermost_args (args);
8d019cef 7979
73aad9b9
JM
7980 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
7981 {
830bfa74 7982 switch (unify (tparms, vec,
050367a3 7983 TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i),
830bfa74 7984 UNIFY_ALLOW_NONE, 0))
73aad9b9
JM
7985 {
7986 case 0:
7987 break;
7988 case 1:
7989 return NULL_TREE;
7990 }
7991 }
7992
7993 for (i = 0; i < ntparms; ++i)
7994 if (! TREE_VEC_ELT (vec, i))
7995 return NULL_TREE;
7996
7997 return vec;
7998}
7999
104bf76a
MM
8000/* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
8001 Pick the most specialized template, and return the corresponding
8002 instantiation, or if there is no corresponding instantiation, the
8003 template itself. EXPLICIT_ARGS is any template arguments explicity
8004 mentioned in a template-id. If there is no most specialized
8005 tempalte, error_mark_node is returned. If there are no templates
8006 at all, NULL_TREE is returned. */
73aad9b9
JM
8007
8008tree
104bf76a
MM
8009most_specialized_instantiation (instantiations, explicit_args)
8010 tree instantiations;
8011 tree explicit_args;
73aad9b9 8012{
104bf76a 8013 tree fn, champ;
73aad9b9
JM
8014 int fate;
8015
104bf76a 8016 if (!instantiations)
73aad9b9
JM
8017 return NULL_TREE;
8018
104bf76a
MM
8019 champ = instantiations;
8020 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
73aad9b9 8021 {
104bf76a
MM
8022 fate = more_specialized (TREE_VALUE (champ),
8023 TREE_VALUE (fn), explicit_args);
73aad9b9
JM
8024 if (fate == 1)
8025 ;
8026 else
8027 {
8028 if (fate == 0)
8029 {
8030 fn = TREE_CHAIN (fn);
8031 if (! fn)
8032 return error_mark_node;
8033 }
104bf76a 8034 champ = fn;
73aad9b9
JM
8035 }
8036 }
8037
104bf76a 8038 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
73aad9b9 8039 {
104bf76a
MM
8040 fate = more_specialized (TREE_VALUE (champ),
8041 TREE_VALUE (fn), explicit_args);
73aad9b9
JM
8042 if (fate != 1)
8043 return error_mark_node;
8044 }
8045
104bf76a
MM
8046 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
8047}
8048
8049/* Return the most specialized of the list of templates in FNS that can
8050 produce an instantiation matching DECL, given the explicit template
8051 arguments EXPLICIT_ARGS. */
8052
8053tree
8054most_specialized (fns, decl, explicit_args)
8055 tree fns, decl, explicit_args;
8056{
8057 tree candidates = NULL_TREE;
8058 tree fn, args;
8059
8060 for (fn = fns; fn; fn = TREE_CHAIN (fn))
8061 {
8062 tree candidate = TREE_VALUE (fn);
8063
8064 args = get_bindings (candidate, decl, explicit_args);
8065 if (args)
8066 candidates = scratch_tree_cons (NULL_TREE, candidate,
8067 candidates);
8068 }
8069
8070 return most_specialized_instantiation (candidates, explicit_args);
73aad9b9
JM
8071}
8072
36a117a5
MM
8073/* If DECL is a specialization of some template, return the most
8074 general such template. For example, given:
8075
8076 template <class T> struct S { template <class U> void f(U); };
8077
8078 if TMPL is `template <class U> void S<int>::f(U)' this will return
8079 the full template. This function will not trace past partial
8080 specializations, however. For example, given in addition:
8081
8082 template <class T> struct S<T*> { template <class U> void f(U); };
8083
8084 if TMPL is `template <class U> void S<int*>::f(U)' this will return
8085 `template <class T> template <class U> S<T*>::f(U)'. */
73aad9b9
JM
8086
8087tree
36a117a5
MM
8088most_general_template (decl)
8089 tree decl;
73aad9b9 8090{
36a117a5
MM
8091 while (DECL_TEMPLATE_INFO (decl))
8092 decl = DECL_TI_TEMPLATE (decl);
8093
8094 return decl;
8095}
8096
8097/* Return the most specialized of the class template specializations
8098 of TMPL which can produce an instantiation matching ARGS, or
8099 error_mark_node if the choice is ambiguous. */
8100
8101tree
8102most_specialized_class (tmpl, args)
8103 tree tmpl;
8104 tree args;
8105{
8106 tree list = NULL_TREE;
8107 tree t;
8108 tree champ;
73aad9b9
JM
8109 int fate;
8110
36a117a5
MM
8111 tmpl = most_general_template (tmpl);
8112 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
73aad9b9 8113 {
36a117a5
MM
8114 tree spec_args
8115 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
8116 if (spec_args)
73aad9b9
JM
8117 {
8118 list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
8119 TREE_TYPE (list) = TREE_TYPE (t);
8120 }
8121 }
8122
8123 if (! list)
8124 return NULL_TREE;
8125
8126 t = list;
8127 champ = t;
8128 t = TREE_CHAIN (t);
8129 for (; t; t = TREE_CHAIN (t))
8130 {
8131 fate = more_specialized_class (champ, t);
8132 if (fate == 1)
8133 ;
8134 else
8135 {
8136 if (fate == 0)
8137 {
8138 t = TREE_CHAIN (t);
8139 if (! t)
8140 return error_mark_node;
8141 }
8142 champ = t;
8143 }
8144 }
8145
8146 for (t = list; t && t != champ; t = TREE_CHAIN (t))
8147 {
85b71cf2 8148 fate = more_specialized_class (champ, t);
73aad9b9
JM
8149 if (fate != 1)
8150 return error_mark_node;
8151 }
8152
8153 return champ;
8154}
8155
8d08fdba 8156/* called from the parser. */
e92cc029 8157
8d08fdba 8158void
6633d636 8159do_decl_instantiation (declspecs, declarator, storage)
f0e01782 8160 tree declspecs, declarator, storage;
8d08fdba 8161{
c11b6f21 8162 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
8d08fdba 8163 tree result = NULL_TREE;
faae18ab 8164 int extern_p = 0;
e8abc66f 8165
ec255269
MS
8166 if (! DECL_LANG_SPECIFIC (decl))
8167 {
8251199e 8168 cp_error ("explicit instantiation of non-template `%#D'", decl);
ec255269
MS
8169 return;
8170 }
03d0f4af 8171 else if (TREE_CODE (decl) == VAR_DECL)
6633d636 8172 {
03d0f4af
MM
8173 /* There is an asymmetry here in the way VAR_DECLs and
8174 FUNCTION_DECLs are handled by grokdeclarator. In the case of
8175 the latter, the DECL we get back will be marked as a
8176 template instantiation, and the appropriate
8177 DECL_TEMPLATE_INFO will be set up. This does not happen for
8178 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
8179 should handle VAR_DECLs as it currently handles
8180 FUNCTION_DECLs. */
6633d636
MS
8181 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
8182 if (result && TREE_CODE (result) != VAR_DECL)
03d0f4af 8183 {
8251199e 8184 cp_error ("no matching template for `%D' found", result);
03d0f4af
MM
8185 return;
8186 }
6633d636
MS
8187 }
8188 else if (TREE_CODE (decl) != FUNCTION_DECL)
8189 {
8251199e 8190 cp_error ("explicit instantiation of `%#D'", decl);
6633d636
MS
8191 return;
8192 }
03d0f4af
MM
8193 else
8194 result = decl;
672476cb 8195
03d0f4af
MM
8196 /* Check for various error cases. Note that if the explicit
8197 instantiation is legal the RESULT will currently be marked as an
8198 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
8199 until we get here. */
8200
8201 if (DECL_TEMPLATE_SPECIALIZATION (result))
672476cb 8202 {
03d0f4af
MM
8203 /* [temp.spec]
8204
8205 No program shall both explicitly instantiate and explicitly
8206 specialize a template. */
8251199e
JM
8207 cp_error ("explicit instantiation of `%#D' after", result);
8208 cp_error_at ("explicit specialization here", result);
672476cb
MM
8209 return;
8210 }
03d0f4af
MM
8211 else if (DECL_EXPLICIT_INSTANTIATION (result))
8212 {
8213 /* [temp.spec]
98c1c668 8214
03d0f4af
MM
8215 No program shall explicitly instantiate any template more
8216 than once.
8217
8218 We check DECL_INTERFACE_KNOWN so as not to complain when the
8219 first instantiation was `extern' and the second is not, and
8220 EXTERN_P for the opposite case. */
8221 if (DECL_INTERFACE_KNOWN (result) && !extern_p)
8251199e 8222 cp_error ("duplicate explicit instantiation of `%#D'", result);
03d0f4af
MM
8223
8224 /* If we've already instantiated the template, just return now. */
8225 if (DECL_INTERFACE_KNOWN (result))
8226 return;
8227 }
8228 else if (!DECL_IMPLICIT_INSTANTIATION (result))
faae18ab 8229 {
8251199e 8230 cp_error ("no matching template for `%D' found", result);
faae18ab
MS
8231 return;
8232 }
03d0f4af 8233 else if (!DECL_TEMPLATE_INFO (result))
6633d636 8234 {
8251199e 8235 cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
6633d636
MS
8236 return;
8237 }
8238
a0a33927
MS
8239 if (flag_external_templates)
8240 return;
8241
f0e01782 8242 if (storage == NULL_TREE)
00595019 8243 ;
faae18ab 8244 else if (storage == ridpointers[(int) RID_EXTERN])
03d0f4af
MM
8245 {
8246 if (pedantic)
8251199e 8247 cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations");
03d0f4af
MM
8248 extern_p = 1;
8249 }
f0e01782 8250 else
8251199e 8251 cp_error ("storage class `%D' applied to template instantiation",
f0e01782 8252 storage);
5566b478 8253
03d0f4af 8254 SET_DECL_EXPLICIT_INSTANTIATION (result);
5566b478 8255 mark_decl_instantiated (result, extern_p);
44a8d0b3 8256 repo_template_instantiated (result, extern_p);
c91a56d2
MS
8257 if (! extern_p)
8258 instantiate_decl (result);
7177d104
MS
8259}
8260
faae18ab
MS
8261void
8262mark_class_instantiated (t, extern_p)
8263 tree t;
8264 int extern_p;
8265{
8266 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
eb773359
JM
8267 SET_CLASSTYPE_INTERFACE_KNOWN (t);
8268 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
faae18ab
MS
8269 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
8270 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
8271 if (! extern_p)
8272 {
8273 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
8274 rest_of_type_compilation (t, 1);
8275 }
8276}
e8abc66f 8277
7177d104 8278void
ca79f85d
JM
8279do_type_instantiation (t, storage)
8280 tree t, storage;
7177d104 8281{
e8abc66f
MS
8282 int extern_p = 0;
8283 int nomem_p = 0;
5566b478
MS
8284 int static_p = 0;
8285
ca79f85d
JM
8286 if (TREE_CODE (t) == TYPE_DECL)
8287 t = TREE_TYPE (t);
8288
7ddedda4 8289 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
ca79f85d 8290 {
8251199e 8291 cp_error ("explicit instantiation of non-template type `%T'", t);
ca79f85d
JM
8292 return;
8293 }
8294
5566b478 8295 complete_type (t);
7177d104 8296
a292b002
MS
8297 /* With -fexternal-templates, explicit instantiations are treated the same
8298 as implicit ones. */
a0a33927
MS
8299 if (flag_external_templates)
8300 return;
8301
f0e01782
MS
8302 if (TYPE_SIZE (t) == NULL_TREE)
8303 {
8251199e 8304 cp_error ("explicit instantiation of `%#T' before definition of template",
f0e01782
MS
8305 t);
8306 return;
8307 }
8308
03d0f4af 8309 if (storage != NULL_TREE)
f0e01782 8310 {
03d0f4af 8311 if (pedantic)
8251199e
JM
8312 cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations",
8313 IDENTIFIER_POINTER (storage));
03d0f4af
MM
8314
8315 if (storage == ridpointers[(int) RID_INLINE])
8316 nomem_p = 1;
8317 else if (storage == ridpointers[(int) RID_EXTERN])
8318 extern_p = 1;
8319 else if (storage == ridpointers[(int) RID_STATIC])
8320 static_p = 1;
8321 else
8322 {
8251199e 8323 cp_error ("storage class `%D' applied to template instantiation",
03d0f4af
MM
8324 storage);
8325 extern_p = 0;
8326 }
f0e01782
MS
8327 }
8328
03d0f4af
MM
8329 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
8330 {
8331 /* [temp.spec]
a292b002 8332
03d0f4af
MM
8333 No program shall both explicitly instantiate and explicitly
8334 specialize a template. */
8251199e
JM
8335 cp_error ("explicit instantiation of `%#T' after", t);
8336 cp_error_at ("explicit specialization here", t);
03d0f4af
MM
8337 return;
8338 }
8339 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
44a8d0b3 8340 {
03d0f4af
MM
8341 /* [temp.spec]
8342
8343 No program shall explicitly instantiate any template more
8344 than once.
8345
8346 If CLASSTYPE_INTERFACE_ONLY, then the first explicit
8347 instantiation was `extern', and if EXTERN_P then the second
8348 is. Both cases are OK. */
8349 if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p)
8251199e 8350 cp_error ("duplicate explicit instantiation of `%#T'", t);
03d0f4af
MM
8351
8352 /* If we've already instantiated the template, just return now. */
8353 if (!CLASSTYPE_INTERFACE_ONLY (t))
8354 return;
44a8d0b3 8355 }
e8abc66f 8356
03d0f4af
MM
8357 mark_class_instantiated (t, extern_p);
8358 repo_template_instantiated (t, extern_p);
8359
e8abc66f
MS
8360 if (nomem_p)
8361 return;
8362
7177d104 8363 {
db5ae43f 8364 tree tmp;
5566b478 8365
03d0f4af
MM
8366 /* In contrast to implicit instantiation, where only the
8367 declarations, and not the definitions, of members are
8368 instantiated, we have here:
8369
8370 [temp.explicit]
8371
8372 The explicit instantiation of a class template specialization
8373 implies the instantiation of all of its members not
8374 previously explicitly specialized in the translation unit
8375 containing the explicit instantiation.
8376
8377 Of course, we can't instantiate member template classes, since
8378 we don't have any arguments for them. Note that the standard
8379 is unclear on whether the instatiation of the members are
8380 *explicit* instantiations or not. We choose to be generous,
8381 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
8382 the explicit instantiation of a class where some of the members
8383 have no definition in the current translation unit. */
8384
5566b478
MS
8385 if (! static_p)
8386 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
a7d87521 8387 if (TREE_CODE (tmp) == FUNCTION_DECL
1f109f0f 8388 && DECL_TEMPLATE_INSTANTIATION (tmp))
5566b478
MS
8389 {
8390 mark_decl_instantiated (tmp, extern_p);
8391 repo_template_instantiated (tmp, extern_p);
8392 if (! extern_p)
8393 instantiate_decl (tmp);
8394 }
8395
8396 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
8397 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
863adfc0 8398 {
5566b478 8399 mark_decl_instantiated (tmp, extern_p);
863adfc0 8400 repo_template_instantiated (tmp, extern_p);
5566b478
MS
8401 if (! extern_p)
8402 instantiate_decl (tmp);
863adfc0 8403 }
7177d104 8404
a292b002 8405 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
e4a84209
MM
8406 if (IS_AGGR_TYPE (TREE_VALUE (tmp))
8407 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp))))
f376e137 8408 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
a292b002 8409 }
8d08fdba 8410}
a28e3c7f 8411
36a117a5
MM
8412/* Given a function DECL, which is a specialization of TMPL, modify
8413 DECL to be a re-instantiation of TMPL with the same template
8414 arguments. TMPL should be the template into which tsubst'ing
8415 should occur for DECL, not the most general template.
830bfa74
MM
8416
8417 One reason for doing this is a scenario like this:
8418
8419 template <class T>
8420 void f(const T&, int i);
8421
8422 void g() { f(3, 7); }
8423
8424 template <class T>
8425 void f(const T& t, const int i) { }
8426
8427 Note that when the template is first instantiated, with
8428 instantiate_template, the resulting DECL will have no name for the
8429 first parameter, and the wrong type for the second. So, when we go
8430 to instantiate the DECL, we regenerate it. */
8431
8432void
8433regenerate_decl_from_template (decl, tmpl)
8434 tree decl;
8435 tree tmpl;
8436{
8437 tree args;
830bfa74
MM
8438 tree code_pattern;
8439 tree new_decl;
36a117a5
MM
8440 tree gen_tmpl;
8441 int unregistered;
830bfa74
MM
8442
8443 args = DECL_TI_ARGS (decl);
8444 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
8445
8251199e
JM
8446 /* Unregister the specialization so that when we tsubst we will not
8447 just return DECL. We don't have to unregister DECL from TMPL
8448 because if would only be registered there if it were a partial
8449 instantiation of a specialization, which it isn't: it's a full
8450 instantiation. */
36a117a5
MM
8451 gen_tmpl = most_general_template (tmpl);
8452 unregistered = unregister_specialization (decl, gen_tmpl);
8453
8454 /* If the DECL was not unregistered then something peculiar is
8455 happening: we created a specialization but did not call
8456 register_specialization for it. */
8457 my_friendly_assert (unregistered, 0);
8458
61a127b3
MM
8459 if (TREE_CODE (decl) == VAR_DECL)
8460 /* Make sure that we can see identifiers, and compute access
8461 correctly, for the class members used in the declaration of
8462 this static variable. */
8463 pushclass (DECL_CONTEXT (decl), 2);
8464
d8c4447d
MM
8465 /* Do the substitution to get the new declaration. */
8466 new_decl = tsubst (code_pattern, args, NULL_TREE);
830bfa74
MM
8467
8468 if (TREE_CODE (decl) == VAR_DECL)
8469 {
8470 /* Set up DECL_INITIAL, since tsubst doesn't. */
830bfa74 8471 DECL_INITIAL (new_decl) =
d8c4447d 8472 tsubst_expr (DECL_INITIAL (code_pattern), args,
830bfa74 8473 DECL_TI_TEMPLATE (decl));
61a127b3 8474 /* Pop the class context we pushed above. */
830bfa74
MM
8475 popclass (1);
8476 }
8477
8478 if (TREE_CODE (decl) == FUNCTION_DECL)
fbf1c34b
MM
8479 {
8480 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
8481 new decl. */
8482 DECL_INITIAL (new_decl) = error_mark_node;
8483 /* And don't complain about a duplicate definition. */
8484 DECL_INITIAL (decl) = NULL_TREE;
8485 }
36a117a5
MM
8486
8487 /* The immediate parent of the new template is still whatever it was
8488 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
8489 general template. We also reset the DECL_ASSEMBLER_NAME since
8490 tsubst always calculates the name as if the function in question
8491 were really a template instance, and sometimes, with friend
8492 functions, this is not so. See tsubst_friend_function for
8493 details. */
8494 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
8495 DECL_ASSEMBLER_NAME (new_decl) = DECL_ASSEMBLER_NAME (decl);
8496 DECL_RTL (new_decl) = DECL_RTL (decl);
8497
8498 /* Call duplicate decls to merge the old and new declarations. */
830bfa74
MM
8499 duplicate_decls (new_decl, decl);
8500
36a117a5
MM
8501 /* Now, re-register the specialization. */
8502 register_specialization (decl, gen_tmpl, args);
830bfa74
MM
8503}
8504
f84b4be9
JM
8505/* Produce the definition of D, a _DECL generated from a template. */
8506
a28e3c7f 8507tree
5566b478
MS
8508instantiate_decl (d)
8509 tree d;
a28e3c7f 8510{
36a117a5
MM
8511 tree tmpl = DECL_TI_TEMPLATE (d);
8512 tree args = DECL_TI_ARGS (d);
830bfa74 8513 tree td;
36a117a5
MM
8514 tree code_pattern;
8515 tree spec;
8516 tree gen_tmpl;
5566b478 8517 int nested = in_function_p ();
5566b478 8518 int pattern_defined;
c27be9b9
JM
8519 int line = lineno;
8520 char *file = input_filename;
5566b478 8521
36a117a5
MM
8522 /* This function should only be used to instantiate templates for
8523 functions and static member variables. */
8524 my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL
8525 || TREE_CODE (d) == VAR_DECL, 0);
8526
fbf1c34b 8527 if (DECL_TEMPLATE_INSTANTIATED (d))
03d0f4af
MM
8528 /* D has already been instantiated. It might seem reasonable to
8529 check whether or not D is an explict instantiation, and, if so,
8530 stop here. But when an explicit instantiation is deferred
8531 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
8532 is set, even though we still need to do the instantiation. */
36a117a5
MM
8533 return d;
8534
8535 /* If we already have a specialization of this declaration, then
8536 there's no reason to instantiate it. Note that
8537 retrieve_specialization gives us both instantiations and
8538 specializations, so we must explicitly check
8539 DECL_TEMPLATE_SPECIALIZATION. */
8540 gen_tmpl = most_general_template (tmpl);
8541 spec = retrieve_specialization (gen_tmpl, args);
8542 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
8543 return spec;
8544
8545 /* This needs to happen before any tsubsting. */
8546 if (! push_tinst_level (d))
8547 return d;
8548
4d85e00e
MM
8549 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
8550 for the instantiation. This is not always the most general
8551 template. Consider, for example:
8552
8553 template <class T>
8554 struct S { template <class U> void f();
8555 template <> void f<int>(); };
8556
8557 and an instantiation of S<double>::f<int>. We want TD to be the
8558 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
8559 td = tmpl;
8560 for (td = tmpl;
8561 /* An instantiation cannot have a definition, so we need a
8562 more general template. */
8563 DECL_TEMPLATE_INSTANTIATION (td)
8564 /* We must also deal with friend templates. Given:
8565
8566 template <class T> struct S {
8567 template <class U> friend void f() {};
8568 };
8569
8570 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
8571 so far as the language is concerned, but that's still
8572 where we get the pattern for the instantiation from. On
8573 ther hand, if the definition comes outside the class, say:
8574
8575 template <class T> struct S {
8576 template <class U> friend void f();
8577 };
8578 template <class U> friend void f() {}
8579
8580 we don't need to look any further. That's what the check for
8581 DECL_INITIAL is for. */
8582 || (TREE_CODE (d) == FUNCTION_DECL
61289ca3 8583 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td)
4d85e00e 8584 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td)));
f84b4be9 8585 )
4d85e00e
MM
8586 {
8587 /* The present template, TD, should not be a definition. If it
8588 were a definition, we should be using it! Note that we
8589 cannot restructure the loop to just keep going until we find
8590 a template with a definition, since that might go too far if
8591 a specialization was declared, but not defined. */
4d85e00e
MM
8592 my_friendly_assert (!(TREE_CODE (d) == VAR_DECL
8593 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))),
8594 0);
8595
8596 /* Fetch the more general template. */
8597 td = DECL_TI_TEMPLATE (td);
8598 }
27bb8339 8599
fee23f54 8600 code_pattern = DECL_TEMPLATE_RESULT (td);
27bb8339 8601
5566b478 8602 if (TREE_CODE (d) == FUNCTION_DECL)
36a117a5 8603 pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE);
5566b478 8604 else
36a117a5 8605 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
de22184b
MS
8606
8607 push_to_top_level ();
8608 lineno = DECL_SOURCE_LINE (d);
8609 input_filename = DECL_SOURCE_FILE (d);
8610
de22184b 8611 if (pattern_defined)
5566b478
MS
8612 {
8613 repo_template_used (d);
8614
8615 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
8616 {
8617 if (flag_alt_external_templates)
8618 {
8619 if (interface_unknown)
8620 warn_if_unknown_interface (d);
8621 }
fee23f54 8622 else if (DECL_INTERFACE_KNOWN (code_pattern))
5566b478
MS
8623 {
8624 DECL_INTERFACE_KNOWN (d) = 1;
fee23f54 8625 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
5566b478
MS
8626 }
8627 else
fee23f54 8628 warn_if_unknown_interface (code_pattern);
5566b478
MS
8629 }
8630
e92cc029 8631 if (at_eof)
5566b478
MS
8632 import_export_decl (d);
8633 }
8634
c4ae3f91
JM
8635 /* Reject all external templates except inline functions. */
8636 if (DECL_INTERFACE_KNOWN (d)
8637 && ! DECL_NOT_REALLY_EXTERN (d)
8638 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d)))
8639 goto out;
8640
67ffc812 8641 if (TREE_CODE (d) == VAR_DECL
711734a9 8642 && TREE_READONLY (d)
67ffc812
MM
8643 && DECL_INITIAL (d) == NULL_TREE
8644 && DECL_INITIAL (code_pattern) != NULL_TREE)
8645 /* We need to set up DECL_INITIAL regardless of pattern_defined if
8646 the variable is a static const initialized in the class body. */;
8647 else if (! pattern_defined
8648 || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested)
8649 && ! at_eof))
5566b478 8650 {
67ffc812 8651 /* Defer all templates except inline functions used in another
c27be9b9
JM
8652 function. We restore the source position here because it's used
8653 by add_pending_template. */
8654 lineno = line;
8655 input_filename = file;
8656
03d0f4af
MM
8657 if (at_eof && !pattern_defined
8658 && DECL_EXPLICIT_INSTANTIATION (d))
8659 /* [temp.explicit]
8660
8661 The definition of a non-exported function template, a
8662 non-exported member function template, or a non-exported
8663 member function or static data member of a class template
8664 shall be present in every translation unit in which it is
8665 explicitly instantiated. */
8251199e 8666 cp_error ("explicit instantiation of `%D' but no definition available",
03d0f4af
MM
8667 d);
8668
5566b478 8669 add_pending_template (d);
de22184b 8670 goto out;
5566b478
MS
8671 }
8672
120722ac 8673 regenerate_decl_from_template (d, td);
fbf1c34b 8674 DECL_TEMPLATE_INSTANTIATED (d) = 1;
120722ac
MM
8675
8676 /* We already set the file and line above. Reset them now in case
8677 they changed as a result of calling regenerate_decl_from_template. */
5156628f
MS
8678 lineno = DECL_SOURCE_LINE (d);
8679 input_filename = DECL_SOURCE_FILE (d);
8680
5566b478
MS
8681 if (TREE_CODE (d) == VAR_DECL)
8682 {
8683 DECL_IN_AGGR_P (d) = 0;
8684 if (DECL_INTERFACE_KNOWN (d))
8685 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
8686 else
8687 {
8688 DECL_EXTERNAL (d) = 1;
8689 DECL_NOT_REALLY_EXTERN (d) = 1;
8690 }
8691 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
8692 }
8693 else if (TREE_CODE (d) == FUNCTION_DECL)
8694 {
fee23f54 8695 tree t = DECL_SAVED_TREE (code_pattern);
5566b478 8696
c11b6f21 8697 start_function (NULL_TREE, d, NULL_TREE, 1);
5566b478
MS
8698 store_parm_decls ();
8699
e76a2646
MS
8700 if (t && TREE_CODE (t) == RETURN_INIT)
8701 {
8702 store_return_init
8703 (TREE_OPERAND (t, 0),
f7d98d58 8704 tsubst_expr (TREE_OPERAND (t, 1), args, tmpl));
e76a2646
MS
8705 t = TREE_CHAIN (t);
8706 }
8707
5566b478
MS
8708 if (t && TREE_CODE (t) == CTOR_INITIALIZER)
8709 {
8710 current_member_init_list
8711 = tsubst_expr_values (TREE_OPERAND (t, 0), args);
8712 current_base_init_list
8713 = tsubst_expr_values (TREE_OPERAND (t, 1), args);
8714 t = TREE_CHAIN (t);
8715 }
8716
8717 setup_vtbl_ptr ();
8718 /* Always keep the BLOCK node associated with the outermost
956d6950 8719 pair of curly braces of a function. These are needed
5566b478
MS
8720 for correct operation of dwarfout.c. */
8721 keep_next_level ();
8722
8723 my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
f7d98d58 8724 tsubst_expr (t, args, tmpl);
a28e3c7f 8725
5566b478 8726 finish_function (lineno, 0, nested);
5566b478
MS
8727 }
8728
de22184b 8729out:
c27be9b9
JM
8730 lineno = line;
8731 input_filename = file;
8732
5566b478 8733 pop_from_top_level ();
5566b478 8734 pop_tinst_level ();
a28e3c7f 8735
a28e3c7f
MS
8736 return d;
8737}
5566b478 8738
824b9a4c 8739static tree
5566b478
MS
8740tsubst_expr_values (t, argvec)
8741 tree t, argvec;
8742{
8743 tree first = NULL_TREE;
8744 tree *p = &first;
8745
8746 for (; t; t = TREE_CHAIN (t))
8747 {
f7d98d58
JM
8748 tree pur = tsubst_copy (TREE_PURPOSE (t), argvec, NULL_TREE);
8749 tree val = tsubst_expr (TREE_VALUE (t), argvec, NULL_TREE);
5566b478
MS
8750 *p = build_tree_list (pur, val);
8751 p = &TREE_CHAIN (*p);
8752 }
8753 return first;
8754}
8755
8756tree last_tree;
8757
8758void
8759add_tree (t)
8760 tree t;
8761{
8762 last_tree = TREE_CHAIN (last_tree) = t;
8763}
73aad9b9 8764
75650646
MM
8765
8766void
8767begin_tree ()
8768{
8769 saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees);
8770 last_tree = NULL_TREE;
8771}
8772
8773
8774void
8775end_tree ()
8776{
8777 my_friendly_assert (saved_trees != NULL_TREE, 0);
8778
8779 last_tree = TREE_VALUE (saved_trees);
8780 saved_trees = TREE_CHAIN (saved_trees);
8781}
8782
73aad9b9
JM
8783/* D is an undefined function declaration in the presence of templates with
8784 the same name, listed in FNS. If one of them can produce D as an
8785 instantiation, remember this so we can instantiate it at EOF if D has
8786 not been defined by that time. */
8787
8788void
8789add_maybe_template (d, fns)
8790 tree d, fns;
8791{
8792 tree t;
8793
8794 if (DECL_MAYBE_TEMPLATE (d))
8795 return;
8796
e1467ff2 8797 t = most_specialized (fns, d, NULL_TREE);
73aad9b9
JM
8798 if (! t)
8799 return;
8800 if (t == error_mark_node)
8801 {
8251199e 8802 cp_error ("ambiguous template instantiation for `%D'", d);
73aad9b9
JM
8803 return;
8804 }
8805
8806 *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE);
8807 maybe_template_tail = &TREE_CHAIN (*maybe_template_tail);
8808 DECL_MAYBE_TEMPLATE (d) = 1;
8809}
b87692e5 8810
61a127b3
MM
8811/* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
8812
8813static void
8814set_current_access_from_decl (decl)
8815 tree decl;
8816{
8817 if (TREE_PRIVATE (decl))
8818 current_access_specifier = access_private_node;
8819 else if (TREE_PROTECTED (decl))
8820 current_access_specifier = access_protected_node;
8821 else
8822 current_access_specifier = access_public_node;
8823}
8824
dbfe2124
MM
8825/* Instantiate an enumerated type. TAG is the template type, NEWTAG
8826 is the instantiation (which should have been created with
8827 start_enum) and ARGS are the template arguments to use. */
b87692e5 8828
dbfe2124
MM
8829static void
8830tsubst_enum (tag, newtag, args)
8831 tree tag;
8832 tree newtag;
8833 tree args;
b87692e5 8834{
dbfe2124 8835 tree e;
b87692e5
MS
8836
8837 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
8838 {
61a127b3
MM
8839 tree value;
8840 tree elt;
8841
8842 /* Note that in a template enum, the TREE_VALUE is the
8843 CONST_DECL, not the corresponding INTEGER_CST. */
8844 value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)),
72f2bd78 8845 args,
61a127b3
MM
8846 NULL_TREE);
8847
8848 /* Give this enumeration constant the correct access. */
8849 set_current_access_from_decl (TREE_VALUE (e));
8850
8851 /* Actually build the enumerator itself. */
8852 elt = build_enumerator (TREE_PURPOSE (e), value, newtag);
b87692e5 8853
dbfe2124
MM
8854 /* We save the enumerators we have built so far in the
8855 TYPE_VALUES so that if the enumeration constants for
8856 subsequent enumerators involve those for previous ones,
8857 tsubst_copy will be able to find them. */
8858 TREE_CHAIN (elt) = TYPE_VALUES (newtag);
8859 TYPE_VALUES (newtag) = elt;
8860 }
b3d5a58b 8861
dbfe2124 8862 finish_enum (newtag);
b87692e5 8863}
36a117a5
MM
8864
8865/* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that
8866 is either an instantiation or specialization of a template
8867 function. */
8868
8869static void
8870set_mangled_name_for_template_decl (decl)
8871 tree decl;
8872{
8873 tree saved_namespace;
b370501f 8874 tree context = NULL_TREE;
36a117a5
MM
8875 tree fn_type;
8876 tree ret_type;
8877 tree parm_types;
8878 tree tparms;
8879 tree targs;
8880 tree tmpl;
8881 int parm_depth;
8882
8883 my_friendly_assert (TREE_CODE (decl) == FUNCTION_DECL, 0);
8884 my_friendly_assert (DECL_TEMPLATE_INFO (decl) != NULL_TREE, 0);
8885
8886 /* The names of template functions must be mangled so as to indicate
8887 what template is being specialized with what template arguments.
8888 For example, each of the following three functions must get
8889 different mangled names:
8890
8891 void f(int);
8892 template <> void f<7>(int);
8893 template <> void f<8>(int); */
8894
8895 targs = DECL_TI_ARGS (decl);
8896 if (uses_template_parms (targs))
8897 /* This DECL is for a partial instantiation. There's no need to
8898 mangle the name of such an entity. */
8899 return;
8900
8901 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
8902 tparms = DECL_TEMPLATE_PARMS (tmpl);
8903 parm_depth = TMPL_PARMS_DEPTH (tparms);
8904
8905 /* There should be as many levels of arguments as there are levels
8906 of parameters. */
8907 my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0);
8908
8909 /* We now compute the PARMS and RET_TYPE to give to
8910 build_decl_overload_real. The PARMS and RET_TYPE are the
8911 parameter and return types of the template, after all but the
8912 innermost template arguments have been substituted, not the
8913 parameter and return types of the function DECL. For example,
8914 given:
8915
8916 template <class T> T f(T);
8917
8918 both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'.
8919 A more subtle example is:
8920
8921 template <class T> struct S { template <class U> void f(T, U); }
8922
8923 Here, if DECL is `void S<int>::f(int, double)', PARMS should be
8924 {int, U}. Thus, the args that we want to subsitute into the
8925 return and parameter type for the function are those in TARGS,
8926 with the innermost level omitted. */
8927 fn_type = TREE_TYPE (tmpl);
8928 if (DECL_STATIC_FUNCTION_P (decl))
61a127b3 8929 context = DECL_CLASS_CONTEXT (decl);
36a117a5
MM
8930
8931 if (parm_depth == 1)
8932 /* No substitution is necessary. */
8933 ;
8934 else
8935 {
8936 int i;
8937 tree partial_args;
8938
8939 /* Replace the innermost level of the TARGS with NULL_TREEs to
8940 let tsubst know not to subsitute for those parameters. */
8941 partial_args = make_temp_vec (TREE_VEC_LENGTH (targs));
8942 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
8943 SET_TMPL_ARGS_LEVEL (partial_args, i,
8944 TMPL_ARGS_LEVEL (targs, i));
8945 SET_TMPL_ARGS_LEVEL (partial_args,
8946 TMPL_ARGS_DEPTH (targs),
8947 make_temp_vec (DECL_NTPARMS (tmpl)));
8948
8949 /* Now, do the (partial) substitution to figure out the
8950 appropriate function type. */
8951 fn_type = tsubst (fn_type, partial_args, NULL_TREE);
8952 if (DECL_STATIC_FUNCTION_P (decl))
8953 context = tsubst (context, partial_args, NULL_TREE);
8954
8955 /* Substitute into the template parameters to obtain the real
8956 innermost set of parameters. This step is important if the
8957 innermost set of template parameters contains value
8958 parameters whose types depend on outer template parameters. */
8959 TREE_VEC_LENGTH (partial_args)--;
8960 tparms = tsubst_template_parms (tparms, partial_args);
8961 }
8962
8963 /* Now, get the innermost parameters and arguments, and figure out
8964 the parameter and return types. */
8965 tparms = INNERMOST_TEMPLATE_PARMS (tparms);
8966 targs = innermost_args (targs);
8967 ret_type = TREE_TYPE (fn_type);
8968 parm_types = TYPE_ARG_TYPES (fn_type);
8969
8970 /* For a static member function, we generate a fake `this' pointer,
8971 for the purposes of mangling. This indicates of which class the
8972 function is a member. Because of:
8973
8974 [class.static]
8975
8976 There shall not be a static and a nonstatic member function
8977 with the same name and the same parameter types
8978
8979 we don't have to worry that this will result in a clash with a
8980 non-static member function. */
8981 if (DECL_STATIC_FUNCTION_P (decl))
8982 parm_types = hash_tree_chain (build_pointer_type (context), parm_types);
8983
8984 /* There should be the same number of template parameters as
8985 template arguments. */
8986 my_friendly_assert (TREE_VEC_LENGTH (tparms) == TREE_VEC_LENGTH (targs),
8987 0);
8988
8989 /* If the template is in a namespace, we need to put that into the
8990 mangled name. Unfortunately, build_decl_overload_real does not
8991 get the decl to mangle, so it relies on the current
8992 namespace. Therefore, we set that here temporarily. */
8993 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 980702);
8994 saved_namespace = current_namespace;
8995 current_namespace = CP_DECL_CONTEXT (decl);
8996
8997 /* Actually set the DCL_ASSEMBLER_NAME. */
8998 DECL_ASSEMBLER_NAME (decl)
8999 = build_decl_overload_real (DECL_NAME (decl), parm_types, ret_type,
9000 tparms, targs,
9001 DECL_FUNCTION_MEMBER_P (decl)
9002 + DECL_CONSTRUCTOR_P (decl));
9003
9004 /* Restore the previously active namespace. */
9005 current_namespace = saved_namespace;
9006}