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