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