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