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