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