]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/pt.c
re PR c++/7484 (warning with -Wmissing-declarations)
[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
bd9bb3d2
MM
4472 case BASELINK:
4473 /* If we do not handle this case specially, we end up walking
4474 the BINFO hierarchy, which is circular, and therefore
4475 confuses walk_tree. */
4476 *walk_subtrees = 0;
4477 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data))
4478 return error_mark_node;
4479 break;
4480
8d08fdba 4481 default:
8dfaeb63 4482 break;
8d08fdba 4483 }
8dfaeb63
MM
4484
4485 /* We didn't find any template parameters we liked. */
4486 return NULL_TREE;
4487}
4488
a1281f45
KL
4489/* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
4490 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
4491 call FN with the parameter and the DATA.
838dfd8a 4492 If FN returns nonzero, the iteration is terminated, and
8dfaeb63 4493 for_each_template_parm returns 1. Otherwise, the iteration
838dfd8a 4494 continues. If FN never returns a nonzero value, the value
8dfaeb63
MM
4495 returned by for_each_template_parm is 0. If FN is NULL, it is
4496 considered to be the function which always returns 1. */
4497
4498static int
4499for_each_template_parm (t, fn, data)
4500 tree t;
4501 tree_fn_t fn;
4502 void* data;
4503{
4504 struct pair_fn_data pfd;
4505
4506 /* Set up. */
4507 pfd.fn = fn;
4508 pfd.data = data;
4509
4890c2f4
MM
4510 /* Walk the tree. (Conceptually, we would like to walk without
4511 duplicates, but for_each_template_parm_r recursively calls
4512 for_each_template_parm, so we would need to reorganize a fair
4513 bit to use walk_tree_without_duplicates.) */
4514 return walk_tree (&t,
4515 for_each_template_parm_r,
4516 &pfd,
4517 NULL) != NULL_TREE;
8d08fdba
MS
4518}
4519
050367a3
MM
4520int
4521uses_template_parms (t)
4522 tree t;
4523{
4524 return for_each_template_parm (t, 0, 0);
4525}
4526
27fafc8d 4527static int tinst_depth;
e9f32eb5 4528extern int max_tinst_depth;
5566b478 4529#ifdef GATHER_STATISTICS
27fafc8d 4530int depth_reached;
5566b478 4531#endif
8dfaeb63
MM
4532static int tinst_level_tick;
4533static int last_template_error_tick;
8d08fdba 4534
3ae18eaf
JM
4535/* We're starting to instantiate D; record the template instantiation context
4536 for diagnostics and to restore it later. */
4537
742a37d5 4538int
5566b478
MS
4539push_tinst_level (d)
4540 tree d;
8d08fdba 4541{
3ae18eaf 4542 tree new;
8d08fdba 4543
7215f9a0
MS
4544 if (tinst_depth >= max_tinst_depth)
4545 {
8adf5b5e
JM
4546 /* If the instantiation in question still has unbound template parms,
4547 we don't really care if we can't instantiate it, so just return.
4548 This happens with base instantiation for implicit `typename'. */
4549 if (uses_template_parms (d))
4550 return 0;
4551
1139b3d8 4552 last_template_error_tick = tinst_level_tick;
33bd39a2 4553 error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'",
b4f4233d 4554 max_tinst_depth, d);
5566b478 4555
cb753e49 4556 print_instantiation_context ();
5566b478 4557
7215f9a0
MS
4558 return 0;
4559 }
4560
3ae18eaf
JM
4561 new = build_expr_wfl (d, input_filename, lineno, 0);
4562 TREE_CHAIN (new) = current_tinst_level;
8d08fdba 4563 current_tinst_level = new;
5566b478 4564
7215f9a0 4565 ++tinst_depth;
5566b478
MS
4566#ifdef GATHER_STATISTICS
4567 if (tinst_depth > depth_reached)
4568 depth_reached = tinst_depth;
4569#endif
4570
27fafc8d 4571 ++tinst_level_tick;
7215f9a0 4572 return 1;
8d08fdba
MS
4573}
4574
3ae18eaf
JM
4575/* We're done instantiating this template; return to the instantiation
4576 context. */
4577
8d08fdba
MS
4578void
4579pop_tinst_level ()
4580{
3ae18eaf 4581 tree old = current_tinst_level;
8d08fdba 4582
ae58fa02
MM
4583 /* Restore the filename and line number stashed away when we started
4584 this instantiation. */
3ae18eaf
JM
4585 lineno = TINST_LINE (old);
4586 input_filename = TINST_FILE (old);
5f2c99c4 4587 extract_interface_info ();
ae58fa02 4588
3ae18eaf 4589 current_tinst_level = TREE_CHAIN (old);
7215f9a0 4590 --tinst_depth;
27fafc8d 4591 ++tinst_level_tick;
8d08fdba
MS
4592}
4593
3ae18eaf
JM
4594/* We're instantiating a deferred template; restore the template
4595 instantiation context in which the instantiation was requested, which
4596 is one step out from LEVEL. */
4597
4598static void
4599reopen_tinst_level (level)
4600 tree level;
4601{
4602 tree t;
4603
4604 tinst_depth = 0;
4605 for (t = level; t; t = TREE_CHAIN (t))
4606 ++tinst_depth;
4607
4608 current_tinst_level = level;
4609 pop_tinst_level ();
4610}
4611
4612/* Return the outermost template instantiation context, for use with
4613 -falt-external-templates. */
4614
4615tree
8d08fdba
MS
4616tinst_for_decl ()
4617{
3ae18eaf 4618 tree p = current_tinst_level;
8d08fdba
MS
4619
4620 if (p)
3ae18eaf 4621 for (; TREE_CHAIN (p) ; p = TREE_CHAIN (p))
8d08fdba
MS
4622 ;
4623 return p;
4624}
4625
f84b4be9
JM
4626/* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4627 vector of template arguments, as for tsubst.
4628
dc957d14 4629 Returns an appropriate tsubst'd friend declaration. */
f84b4be9
JM
4630
4631static tree
4632tsubst_friend_function (decl, args)
4633 tree decl;
4634 tree args;
4635{
4636 tree new_friend;
27fafc8d 4637 int line = lineno;
3b304f5b 4638 const char *file = input_filename;
27fafc8d
JM
4639
4640 lineno = DECL_SOURCE_LINE (decl);
4641 input_filename = DECL_SOURCE_FILE (decl);
4642
f84b4be9
JM
4643 if (TREE_CODE (decl) == FUNCTION_DECL
4644 && DECL_TEMPLATE_INSTANTIATION (decl)
4645 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
4646 /* This was a friend declared with an explicit template
4647 argument list, e.g.:
4648
4649 friend void f<>(T);
4650
4651 to indicate that f was a template instantiation, not a new
4652 function declaration. Now, we have to figure out what
4653 instantiation of what template. */
4654 {
76e57b45 4655 tree template_id, arglist, fns;
f84b4be9
JM
4656 tree new_args;
4657 tree tmpl;
ed2fa432 4658 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
76e57b45
NS
4659
4660 /* Friend functions are looked up in the containing namespace scope.
4661 We must enter that scope, to avoid finding member functions of the
4662 current cless with same name. */
4663 push_nested_namespace (ns);
4664 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
c2ea3a40 4665 tf_error | tf_warning, NULL_TREE);
76e57b45
NS
4666 pop_nested_namespace (ns);
4667 arglist = tsubst (DECL_TI_ARGS (decl), args,
c2ea3a40 4668 tf_error | tf_warning, NULL_TREE);
76e57b45
NS
4669 template_id = lookup_template_function (fns, arglist);
4670
c2ea3a40 4671 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
36a117a5
MM
4672 tmpl = determine_specialization (template_id, new_friend,
4673 &new_args,
bf8f3f93 4674 /*need_member_template=*/0);
27fafc8d
JM
4675 new_friend = instantiate_template (tmpl, new_args);
4676 goto done;
f84b4be9 4677 }
36a117a5 4678
c2ea3a40 4679 new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
f84b4be9 4680
36a117a5 4681 /* The NEW_FRIEND will look like an instantiation, to the
f84b4be9
JM
4682 compiler, but is not an instantiation from the point of view of
4683 the language. For example, we might have had:
4684
4685 template <class T> struct S {
4686 template <class U> friend void f(T, U);
4687 };
4688
4689 Then, in S<int>, template <class U> void f(int, U) is not an
4690 instantiation of anything. */
4691 DECL_USE_TEMPLATE (new_friend) = 0;
4692 if (TREE_CODE (decl) == TEMPLATE_DECL)
655dc6ee
JM
4693 {
4694 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
4695 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
4696 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
4697 }
36a117a5 4698
92643fea
MM
4699 /* The mangled name for the NEW_FRIEND is incorrect. The function
4700 is not a template instantiation and should not be mangled like
4701 one. Therefore, we forget the mangling here; we'll recompute it
4702 later if we need it. */
36a117a5
MM
4703 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
4704 {
19e7881c 4705 SET_DECL_RTL (new_friend, NULL_RTX);
92643fea 4706 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
36a117a5
MM
4707 }
4708
6eb3bb27 4709 if (DECL_NAMESPACE_SCOPE_P (new_friend))
f84b4be9 4710 {
36a117a5 4711 tree old_decl;
fbf1c34b
MM
4712 tree new_friend_template_info;
4713 tree new_friend_result_template_info;
92da7074 4714 tree ns;
fbf1c34b
MM
4715 int new_friend_is_defn;
4716
4717 /* We must save some information from NEW_FRIEND before calling
4718 duplicate decls since that function will free NEW_FRIEND if
4719 possible. */
4720 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
f84b4be9 4721 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
fbf1c34b
MM
4722 {
4723 /* This declaration is a `primary' template. */
4724 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
4725
4726 new_friend_is_defn
17aec3eb 4727 = DECL_INITIAL (DECL_TEMPLATE_RESULT (new_friend)) != NULL_TREE;
fbf1c34b 4728 new_friend_result_template_info
17aec3eb 4729 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
fbf1c34b
MM
4730 }
4731 else
4732 {
4733 new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE;
4734 new_friend_result_template_info = NULL_TREE;
4735 }
36a117a5 4736
1c227897
MM
4737 /* Inside pushdecl_namespace_level, we will push into the
4738 current namespace. However, the friend function should go
c6002625 4739 into the namespace of the template. */
92da7074
ML
4740 ns = decl_namespace_context (new_friend);
4741 push_nested_namespace (ns);
36a117a5 4742 old_decl = pushdecl_namespace_level (new_friend);
92da7074 4743 pop_nested_namespace (ns);
36a117a5
MM
4744
4745 if (old_decl != new_friend)
4746 {
4747 /* This new friend declaration matched an existing
4748 declaration. For example, given:
4749
4750 template <class T> void f(T);
4751 template <class U> class C {
4752 template <class T> friend void f(T) {}
4753 };
4754
4755 the friend declaration actually provides the definition
4756 of `f', once C has been instantiated for some type. So,
4757 old_decl will be the out-of-class template declaration,
4758 while new_friend is the in-class definition.
4759
4760 But, if `f' was called before this point, the
4761 instantiation of `f' will have DECL_TI_ARGS corresponding
4762 to `T' but not to `U', references to which might appear
4763 in the definition of `f'. Previously, the most general
4764 template for an instantiation of `f' was the out-of-class
4765 version; now it is the in-class version. Therefore, we
4766 run through all specialization of `f', adding to their
4767 DECL_TI_ARGS appropriately. In particular, they need a
4768 new set of outer arguments, corresponding to the
4769 arguments for this class instantiation.
4770
4771 The same situation can arise with something like this:
4772
4773 friend void f(int);
4774 template <class T> class C {
4775 friend void f(T) {}
4776 };
4777
4778 when `C<int>' is instantiated. Now, `f(int)' is defined
4779 in the class. */
4780
fbf1c34b
MM
4781 if (!new_friend_is_defn)
4782 /* On the other hand, if the in-class declaration does
4783 *not* provide a definition, then we don't want to alter
4784 existing definitions. We can just leave everything
4785 alone. */
36a117a5 4786 ;
fbf1c34b 4787 else
36a117a5 4788 {
fbf1c34b
MM
4789 /* Overwrite whatever template info was there before, if
4790 any, with the new template information pertaining to
4791 the declaration. */
4792 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
4793
4794 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
4795 /* duplicate_decls will take care of this case. */
4796 ;
4797 else
36a117a5 4798 {
fbf1c34b
MM
4799 tree t;
4800 tree new_friend_args;
4801
17aec3eb 4802 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
fbf1c34b
MM
4803 = new_friend_result_template_info;
4804
4805 new_friend_args = TI_ARGS (new_friend_template_info);
4806 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
4807 t != NULL_TREE;
4808 t = TREE_CHAIN (t))
4809 {
4810 tree spec = TREE_VALUE (t);
36a117a5 4811
fbf1c34b
MM
4812 DECL_TI_ARGS (spec)
4813 = add_outermost_template_args (new_friend_args,
4814 DECL_TI_ARGS (spec));
fbf1c34b
MM
4815 }
4816
4817 /* Now, since specializations are always supposed to
4818 hang off of the most general template, we must move
4819 them. */
4820 t = most_general_template (old_decl);
4821 if (t != old_decl)
4822 {
4823 DECL_TEMPLATE_SPECIALIZATIONS (t)
4824 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
4825 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
4826 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
4827 }
36a117a5
MM
4828 }
4829 }
4830
4831 /* The information from NEW_FRIEND has been merged into OLD_DECL
4832 by duplicate_decls. */
4833 new_friend = old_decl;
4834 }
f84b4be9 4835 }
d0f062fb 4836 else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend)))
f84b4be9
JM
4837 {
4838 /* Check to see that the declaration is really present, and,
4839 possibly obtain an improved declaration. */
4840 tree fn = check_classfn (DECL_CONTEXT (new_friend),
4841 new_friend);
4842
4843 if (fn)
4844 new_friend = fn;
4845 }
4846
27fafc8d
JM
4847 done:
4848 lineno = line;
4849 input_filename = file;
f84b4be9
JM
4850 return new_friend;
4851}
4852
1aed5355
MM
4853/* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4854 template arguments, as for tsubst.
6757edfe 4855
dc957d14 4856 Returns an appropriate tsubst'd friend type or error_mark_node on
4b054b80 4857 failure. */
6757edfe
MM
4858
4859static tree
1aed5355
MM
4860tsubst_friend_class (friend_tmpl, args)
4861 tree friend_tmpl;
6757edfe
MM
4862 tree args;
4863{
1aed5355 4864 tree friend_type;
25aab5d0 4865 tree tmpl;
3e0ec82f 4866 tree context;
6757edfe 4867
3e0ec82f
MM
4868 context = DECL_CONTEXT (friend_tmpl);
4869
4870 if (context)
77adef84 4871 {
3e0ec82f
MM
4872 if (TREE_CODE (context) == NAMESPACE_DECL)
4873 push_nested_namespace (context);
4874 else
5ce49b4b 4875 push_nested_class (tsubst (context, args, tf_none, NULL_TREE), 2);
3e0ec82f 4876 }
25aab5d0 4877
3e0ec82f
MM
4878 /* First, we look for a class template. */
4879 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0);
25aab5d0 4880
3e0ec82f
MM
4881 /* But, if we don't find one, it might be because we're in a
4882 situation like this:
77adef84 4883
3e0ec82f
MM
4884 template <class T>
4885 struct S {
4886 template <class U>
4887 friend struct S;
4888 };
4889
4890 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
4891 for `S<int>', not the TEMPLATE_DECL. */
4892 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
4893 {
4894 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1);
4895 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
25aab5d0 4896 }
6757edfe 4897
25aab5d0 4898 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
6757edfe
MM
4899 {
4900 /* The friend template has already been declared. Just
36a117a5
MM
4901 check to see that the declarations match, and install any new
4902 default parameters. We must tsubst the default parameters,
4903 of course. We only need the innermost template parameters
4904 because that is all that redeclare_class_template will look
4905 at. */
3e0ec82f
MM
4906 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
4907 > TMPL_ARGS_DEPTH (args))
4908 {
4909 tree parms;
4910 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
4911 args, tf_error | tf_warning);
4912 redeclare_class_template (TREE_TYPE (tmpl), parms);
4913 }
4914
6757edfe
MM
4915 friend_type = TREE_TYPE (tmpl);
4916 }
4917 else
4918 {
4919 /* The friend template has not already been declared. In this
4920 case, the instantiation of the template class will cause the
4921 injection of this template into the global scope. */
c2ea3a40 4922 tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE);
6757edfe
MM
4923
4924 /* The new TMPL is not an instantiation of anything, so we
4925 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4926 the new type because that is supposed to be the corresponding
4927 template decl, i.e., TMPL. */
4928 DECL_USE_TEMPLATE (tmpl) = 0;
4929 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
4930 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
4931
4932 /* Inject this template into the global scope. */
4933 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
4934 }
4935
3e0ec82f
MM
4936 if (context)
4937 {
4938 if (TREE_CODE (context) == NAMESPACE_DECL)
4939 pop_nested_namespace (context);
4940 else
4941 pop_nested_class ();
4942 }
4943
6757edfe
MM
4944 return friend_type;
4945}
f84b4be9 4946
17f29637
KL
4947/* Returns zero if TYPE cannot be completed later due to circularity.
4948 Otherwise returns one. */
4949
d5372501 4950static int
17f29637
KL
4951can_complete_type_without_circularity (type)
4952 tree type;
4953{
4954 if (type == NULL_TREE || type == error_mark_node)
4955 return 0;
4956 else if (COMPLETE_TYPE_P (type))
4957 return 1;
4958 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
4959 return can_complete_type_without_circularity (TREE_TYPE (type));
4960 else if (CLASS_TYPE_P (type) && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
4961 return 0;
4962 else
4963 return 1;
4964}
4965
8d08fdba 4966tree
5566b478
MS
4967instantiate_class_template (type)
4968 tree type;
8d08fdba 4969{
61a127b3 4970 tree template, args, pattern, t;
36a117a5 4971 tree typedecl;
8d08fdba 4972
5566b478 4973 if (type == error_mark_node)
8d08fdba
MS
4974 return error_mark_node;
4975
d0f062fb 4976 if (TYPE_BEING_DEFINED (type) || COMPLETE_TYPE_P (type))
5566b478
MS
4977 return type;
4978
6bdb985f 4979 /* Figure out which template is being instantiated. */
36a117a5 4980 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5566b478 4981 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
73aad9b9 4982
6bdb985f
MM
4983 /* Figure out which arguments are being used to do the
4984 instantiation. */
4985 args = CLASSTYPE_TI_ARGS (type);
4c571114 4986 PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args);
6bdb985f 4987
4c571114
MM
4988 if (pedantic && PARTIAL_INSTANTIATION_P (type))
4989 /* If this is a partial instantiation, then we can't instantiate
4990 the type; there's no telling whether or not one of the
4991 template parameters might eventually be instantiated to some
4992 value that results in a specialization being used. For
4993 example, consider:
4994
4995 template <class T>
4996 struct S {};
4997
4998 template <class U>
4999 void f(S<U>);
5000
5001 template <>
5002 struct S<int> {};
5003
5004 Now, the `S<U>' in `f<int>' is the specialization, not an
5db698f6 5005 instantiation of the original template. */
f31c0a32 5006 return type;
4c571114
MM
5007
5008 /* Determine what specialization of the original template to
5009 instantiate. */
5010 if (PARTIAL_INSTANTIATION_P (type))
6bdb985f
MM
5011 /* There's no telling which specialization is appropriate at this
5012 point. Since all peeking at the innards of this partial
5013 instantiation are extensions (like the "implicit typename"
5014 extension, which allows users to omit the keyword `typename' on
5015 names that are declared as types in template base classes), we
5016 are free to do what we please.
5017
5018 Trying to figure out which partial instantiation to use can
5019 cause a crash. (Some of the template arguments don't even have
5020 types.) So, we just use the most general version. */
5021 t = NULL_TREE;
5022 else
73aad9b9 5023 {
6bdb985f
MM
5024 t = most_specialized_class (template, args);
5025
5026 if (t == error_mark_node)
73aad9b9 5027 {
d8e178a0 5028 const char *str = "candidates are:";
33bd39a2 5029 error ("ambiguous class template instantiation for `%#T'", type);
6bdb985f
MM
5030 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
5031 t = TREE_CHAIN (t))
73aad9b9 5032 {
6bdb985f
MM
5033 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
5034 args))
5035 {
5036 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
5037 str = " ";
5038 }
73aad9b9 5039 }
6bdb985f 5040 TYPE_BEING_DEFINED (type) = 1;
f31c0a32 5041 return error_mark_node;
73aad9b9 5042 }
73aad9b9 5043 }
6bdb985f
MM
5044
5045 if (t)
73aad9b9
JM
5046 pattern = TREE_TYPE (t);
5047 else
5048 pattern = TREE_TYPE (template);
5566b478 5049
4c571114
MM
5050 /* If the template we're instantiating is incomplete, then clearly
5051 there's nothing we can do. */
d0f062fb 5052 if (!COMPLETE_TYPE_P (pattern))
f31c0a32 5053 return type;
5566b478 5054
4c571114
MM
5055 /* If this is a partial instantiation, don't tsubst anything. We will
5056 only use this type for implicit typename, so the actual contents don't
5057 matter. All that matters is whether a particular name is a type. */
5058 if (PARTIAL_INSTANTIATION_P (type))
5059 {
5060 /* The fields set here must be kept in sync with those cleared
5061 in begin_class_definition. */
5062 TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern);
5063 TYPE_FIELDS (type) = TYPE_FIELDS (pattern);
5064 TYPE_METHODS (type) = TYPE_METHODS (pattern);
5065 CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern);
1ddd4323
NS
5066 CLASSTYPE_VBASECLASSES (type) = CLASSTYPE_VBASECLASSES (pattern);
5067
4c571114
MM
5068 /* Pretend that the type is complete, so that we will look
5069 inside it during name lookup and such. */
867580ce 5070 TYPE_SIZE (type) = bitsize_zero_node;
f31c0a32 5071 return type;
4c571114
MM
5072 }
5073
5074 /* If we've recursively instantiated too many templates, stop. */
5075 if (! push_tinst_level (type))
f31c0a32 5076 return type;
4c571114
MM
5077
5078 /* Now we're really doing the instantiation. Mark the type as in
5079 the process of being defined. */
5080 TYPE_BEING_DEFINED (type) = 1;
5081
5082 maybe_push_to_top_level (uses_template_parms (type));
4c571114 5083
73aad9b9 5084 if (t)
36a117a5 5085 {
27631dae 5086 /* This TYPE is actually an instantiation of a partial
36a117a5
MM
5087 specialization. We replace the innermost set of ARGS with
5088 the arguments appropriate for substitution. For example,
5089 given:
5090
5091 template <class T> struct S {};
5092 template <class T> struct S<T*> {};
5093
5094 and supposing that we are instantiating S<int*>, ARGS will
5095 present be {int*} but we need {int}. */
5096 tree inner_args
5097 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
5098 args);
5099
5100 /* If there were multiple levels in ARGS, replacing the
5101 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
5102 want, so we make a copy first. */
5103 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
5104 {
5105 args = copy_node (args);
5106 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
5107 }
5108 else
5109 args = inner_args;
5110 }
8d019cef 5111
5566b478
MS
5112 if (flag_external_templates)
5113 {
5114 if (flag_alt_external_templates)
5115 {
5116 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
5117 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
5566b478
MS
5118 }
5119 else
5120 {
5121 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
5122 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
5123 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
5566b478
MS
5124 }
5125 }
5126 else
8d08fdba 5127 {
5566b478 5128 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8d08fdba
MS
5129 }
5130
f7da6097
MS
5131 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5132 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
f7da6097
MS
5133 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
5134 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
5135 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
834c6dff
MM
5136 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5137 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
f7da6097 5138 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
f7da6097
MS
5139 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5140 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5141 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
5142 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5143 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
f7da6097
MS
5144 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5145 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
4c6b7393
MM
5146 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
5147 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern);
f7da6097
MS
5148 TYPE_USES_MULTIPLE_INHERITANCE (type)
5149 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
5150 TYPE_USES_VIRTUAL_BASECLASSES (type)
5151 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
5152 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5153 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
11cf4d18 5154 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
eff71ab0 5155 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
6bdb8141
JM
5156 if (ANON_AGGR_TYPE_P (pattern))
5157 SET_ANON_AGGR_TYPE_P (type);
f7da6097 5158
3fd71a52
MM
5159 if (TYPE_BINFO_BASETYPES (pattern))
5160 {
5161 tree base_list = NULL_TREE;
5162 tree pbases = TYPE_BINFO_BASETYPES (pattern);
5163 int i;
5566b478 5164
3fd71a52
MM
5165 /* Substitute into each of the bases to determine the actual
5166 basetypes. */
5167 for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i)
5168 {
5169 tree base;
5170 tree access;
5171 tree pbase;
5566b478 5172
3fd71a52 5173 pbase = TREE_VEC_ELT (pbases, i);
711734a9 5174
dc957d14 5175 /* Substitute to figure out the base class. */
c2ea3a40 5176 base = tsubst (BINFO_TYPE (pbase), args, tf_error, NULL_TREE);
3fd71a52
MM
5177 if (base == error_mark_node)
5178 continue;
711734a9 5179
3fd71a52
MM
5180 /* Calculate the correct access node. */
5181 if (TREE_VIA_VIRTUAL (pbase))
5182 {
5183 if (TREE_VIA_PUBLIC (pbase))
5184 access = access_public_virtual_node;
5185 else if (TREE_VIA_PROTECTED (pbase))
5186 access = access_protected_virtual_node;
d6479fe7 5187 else
3fd71a52
MM
5188 access = access_private_virtual_node;
5189 }
5190 else
5191 {
5192 if (TREE_VIA_PUBLIC (pbase))
5193 access = access_public_node;
5194 else if (TREE_VIA_PROTECTED (pbase))
5195 access = access_protected_node;
d6479fe7 5196 else
3fd71a52
MM
5197 access = access_private_node;
5198 }
dfbcd65a 5199
3fd71a52
MM
5200 base_list = tree_cons (access, base, base_list);
5201 }
dfbcd65a 5202
3fd71a52
MM
5203 /* The list is now in reverse order; correct that. */
5204 base_list = nreverse (base_list);
5205
5206 /* Now call xref_basetypes to set up all the base-class
5207 information. */
da15dae6 5208 xref_basetypes (type, base_list);
3fd71a52 5209 }
5566b478 5210
b74a0560
MM
5211 /* Now that our base classes are set up, enter the scope of the
5212 class, so that name lookups into base classes, etc. will work
dc957d14 5213 correctly. This is precisely analogous to what we do in
b74a0560
MM
5214 begin_class_definition when defining an ordinary non-template
5215 class. */
5216 pushclass (type, 1);
5217
5566b478 5218 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
8d08fdba 5219 {
5566b478 5220 tree tag = TREE_VALUE (t);
36a117a5
MM
5221 tree name = TYPE_IDENTIFIER (tag);
5222 tree newtag;
5566b478 5223
c2ea3a40 5224 newtag = tsubst (tag, args, tf_error, NULL_TREE);
a87b4257 5225 my_friendly_assert (newtag != error_mark_node, 20010206);
61a127b3 5226 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
ed44da02 5227 {
486e4077
MM
5228 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
5229 /* Unfortunately, lookup_template_class sets
5230 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5231 instantiation (i.e., for the type of a member template
5232 class nested within a template class.) This behavior is
5233 required for maybe_process_partial_specialization to work
5234 correctly, but is not accurate in this case; the TAG is not
5235 an instantiation of anything. (The corresponding
5236 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
5237 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5238
ed44da02
MM
5239 /* Now, we call pushtag to put this NEWTAG into the scope of
5240 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5241 pushtag calling push_template_decl. We don't have to do
5242 this for enums because it will already have been done in
5243 tsubst_enum. */
5244 if (name)
5245 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5246 pushtag (name, newtag, /*globalize=*/0);
5247 }
8d08fdba
MS
5248 }
5249
5566b478
MS
5250 /* Don't replace enum constants here. */
5251 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
8dd3f57a 5252 if (TREE_CODE (t) != CONST_DECL)
5566b478 5253 {
ae58fa02
MM
5254 tree r;
5255
5256 /* The the file and line for this declaration, to assist in
5257 error message reporting. Since we called push_tinst_level
5258 above, we don't need to restore these. */
5259 lineno = DECL_SOURCE_LINE (t);
5260 input_filename = DECL_SOURCE_FILE (t);
5261
c2ea3a40 5262 r = tsubst (t, args, tf_error | tf_warning, NULL_TREE);
5566b478
MS
5263 if (TREE_CODE (r) == VAR_DECL)
5264 {
fa8d6e85
MM
5265 tree init;
5266
3febd123 5267 if (DECL_INITIALIZED_IN_CLASS_P (r))
fa8d6e85 5268 init = tsubst_expr (DECL_INITIAL (t), args,
c2ea3a40 5269 tf_error | tf_warning, NULL_TREE);
fa8d6e85
MM
5270 else
5271 init = NULL_TREE;
5272
5273 finish_static_data_member_decl (r, init,
5274 /*asmspec_tree=*/NULL_TREE,
fa8d6e85
MM
5275 /*flags=*/0);
5276
3febd123 5277 if (DECL_INITIALIZED_IN_CLASS_P (r))
6ba89f8e 5278 check_static_variable_definition (r, TREE_TYPE (r));
5566b478 5279 }
17f29637
KL
5280 else if (TREE_CODE (r) == FIELD_DECL)
5281 {
5282 /* Determine whether R has a valid type and can be
5283 completed later. If R is invalid, then it is replaced
5284 by error_mark_node so that it will not be added to
5285 TYPE_FIELDS. */
5286 tree rtype = TREE_TYPE (r);
5287 if (!can_complete_type_without_circularity (rtype))
5288 {
492ff760 5289 cxx_incomplete_type_error (r, rtype);
17f29637
KL
5290 r = error_mark_node;
5291 }
5292 }
5293
61a127b3
MM
5294 /* R will have a TREE_CHAIN if and only if it has already been
5295 processed by finish_member_declaration. This can happen
5296 if, for example, it is a TYPE_DECL for a class-scoped
5297 ENUMERAL_TYPE; such a thing will already have been added to
5298 the field list by tsubst_enum above. */
5299 if (!TREE_CHAIN (r))
5300 {
5301 set_current_access_from_decl (r);
5302 finish_member_declaration (r);
5303 }
5566b478 5304 }
8d08fdba 5305
61a127b3
MM
5306 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
5307 for this instantiation. */
5308 for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t))
5309 {
c2ea3a40 5310 tree r = tsubst (t, args, tf_error, NULL_TREE);
61a127b3 5311 set_current_access_from_decl (r);
271e6f02 5312 grok_special_member_properties (r);
61a127b3
MM
5313 finish_member_declaration (r);
5314 }
8d08fdba 5315
2604412d
JM
5316 /* Construct the DECL_FRIENDLIST for the new class type. */
5317 typedecl = TYPE_MAIN_DECL (type);
5318 for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern));
5319 t != NULL_TREE;
5320 t = TREE_CHAIN (t))
f84b4be9 5321 {
2604412d 5322 tree friends;
f84b4be9 5323
2604412d
JM
5324 for (friends = TREE_VALUE (t);
5325 friends != NULL_TREE;
5326 friends = TREE_CHAIN (friends))
0c0aac2f
MM
5327 if (TREE_PURPOSE (friends) == error_mark_node)
5328 add_friend (type,
5329 tsubst_friend_function (TREE_VALUE (friends),
5330 args));
5331 else
a98facb0 5332 abort ();
2604412d 5333 }
5566b478 5334
6757edfe
MM
5335 for (t = CLASSTYPE_FRIEND_CLASSES (pattern);
5336 t != NULL_TREE;
5337 t = TREE_CHAIN (t))
5338 {
5339 tree friend_type = TREE_VALUE (t);
1aed5355 5340 tree new_friend_type;
6757edfe 5341
36a117a5
MM
5342 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5343 new_friend_type = tsubst_friend_class (friend_type, args);
5344 else if (uses_template_parms (friend_type))
c2ea3a40
NS
5345 new_friend_type = tsubst (friend_type, args,
5346 tf_error | tf_warning, NULL_TREE);
36a117a5 5347 else
61fc8c9e
MM
5348 {
5349 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
36a117a5 5350
61fc8c9e
MM
5351 /* The call to xref_tag_from_type does injection for friend
5352 classes. */
5353 push_nested_namespace (ns);
5354 new_friend_type =
5355 xref_tag_from_type (friend_type, NULL_TREE, 1);
5356 pop_nested_namespace (ns);
5357 }
1aed5355
MM
5358
5359 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5360 /* Trick make_friend_class into realizing that the friend
5361 we're adding is a template, not an ordinary class. It's
5362 important that we use make_friend_class since it will
5363 perform some error-checking and output cross-reference
5364 information. */
5365 ++processing_template_decl;
5366
4b054b80
NS
5367 if (new_friend_type != error_mark_node)
5368 make_friend_class (type, new_friend_type);
fc378698 5369
1aed5355
MM
5370 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5371 --processing_template_decl;
6757edfe 5372 }
fc378698 5373
17f29637
KL
5374 /* Now that TYPE_FIELDS and TYPE_METHODS are set up. We can
5375 instantiate templates used by this class. */
2604412d
JM
5376 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
5377 if (TREE_CODE (t) == FIELD_DECL)
5378 {
5379 TREE_TYPE (t) = complete_type (TREE_TYPE (t));
5380 require_complete_type (t);
5381 }
5566b478 5382
61a127b3
MM
5383 /* Set the file and line number information to whatever is given for
5384 the class itself. This puts error messages involving generated
5385 implicit functions at a predictable point, and the same point
5386 that would be used for non-template classes. */
5387 lineno = DECL_SOURCE_LINE (typedecl);
5388 input_filename = DECL_SOURCE_FILE (typedecl);
5389
5390 unreverse_member_declarations (type);
9f33663b 5391 finish_struct_1 (type);
2604412d 5392 CLASSTYPE_GOT_SEMICOLON (type) = 1;
e92cc029 5393
5524676d
JM
5394 /* Clear this now so repo_template_used is happy. */
5395 TYPE_BEING_DEFINED (type) = 0;
2604412d 5396 repo_template_used (type);
8d08fdba 5397
9188c363
MM
5398 /* Now that the class is complete, instantiate default arguments for
5399 any member functions. We don't do this earlier because the
5400 default arguments may reference members of the class. */
5401 if (!PRIMARY_TEMPLATE_P (template))
5402 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5403 if (TREE_CODE (t) == FUNCTION_DECL
dc957d14 5404 /* Implicitly generated member functions will not have template
9188c363
MM
5405 information; they are not instantiations, but instead are
5406 created "fresh" for each instantiation. */
5407 && DECL_TEMPLATE_INFO (t))
5408 tsubst_default_arguments (t);
5409
b74a0560 5410 popclass ();
5566b478
MS
5411 pop_from_top_level ();
5412 pop_tinst_level ();
5413
5414 return type;
8d08fdba
MS
5415}
5416
5417static int
5418list_eq (t1, t2)
5419 tree t1, t2;
5420{
5421 if (t1 == NULL_TREE)
5422 return t2 == NULL_TREE;
5423 if (t2 == NULL_TREE)
5424 return 0;
5425 /* Don't care if one declares its arg const and the other doesn't -- the
5426 main variant of the arg type is all that matters. */
5427 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
5428 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
5429 return 0;
5430 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
5431}
5432
00d3396f
JM
5433/* If arg is a non-type template parameter that does not depend on template
5434 arguments, fold it like we weren't in the body of a template. */
5435
5436static tree
5437maybe_fold_nontype_arg (arg)
5438 tree arg;
5439{
4d5f3fbd 5440 if (arg && !TYPE_P (arg) && !uses_template_parms (arg))
00d3396f
JM
5441 {
5442 /* Sometimes, one of the args was an expression involving a
5443 template constant parameter, like N - 1. Now that we've
5444 tsubst'd, we might have something like 2 - 1. This will
5445 confuse lookup_template_class, so we do constant folding
5446 here. We have to unset processing_template_decl, to
5447 fool build_expr_from_tree() into building an actual
5448 tree. */
5449
4d5f3fbd
KL
5450 /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already
5451 as simple as it's going to get, and trying to reprocess
5452 the trees will break. */
5453 if (!TREE_TYPE (arg))
5454 {
5455 int saved_processing_template_decl = processing_template_decl;
5456 processing_template_decl = 0;
5457 arg = build_expr_from_tree (arg);
5458 processing_template_decl = saved_processing_template_decl;
5459 }
5460
5461 arg = fold (arg);
00d3396f
JM
5462 }
5463 return arg;
5464}
5465
830bfa74
MM
5466/* Substitute ARGS into the vector of template arguments T. */
5467
e9659ab0 5468static tree
4393e105 5469tsubst_template_arg_vector (t, args, complain)
830bfa74
MM
5470 tree t;
5471 tree args;
c2ea3a40 5472 tsubst_flags_t complain;
830bfa74
MM
5473{
5474 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
5475 tree *elts = (tree *) alloca (len * sizeof (tree));
5476
961192e1 5477 memset ((char *) elts, 0, len * sizeof (tree));
830bfa74
MM
5478
5479 for (i = 0; i < len; i++)
5480 {
5481 if (TREE_VEC_ELT (t, i) != NULL_TREE
5482 && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
4393e105
MM
5483 elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i),
5484 args, complain);
830bfa74
MM
5485 else
5486 elts[i] = maybe_fold_nontype_arg
4393e105
MM
5487 (tsubst_expr (TREE_VEC_ELT (t, i), args, complain,
5488 NULL_TREE));
830bfa74 5489
08e72a19
JM
5490 if (elts[i] == error_mark_node)
5491 return error_mark_node;
5492
830bfa74
MM
5493 if (elts[i] != TREE_VEC_ELT (t, i))
5494 need_new = 1;
5495 }
5496
5497 if (!need_new)
5498 return t;
5499
f31c0a32 5500 t = make_tree_vec (len);
830bfa74
MM
5501 for (i = 0; i < len; i++)
5502 TREE_VEC_ELT (t, i) = elts[i];
5503
5504 return t;
5505}
5506
36a117a5
MM
5507/* Return the result of substituting ARGS into the template parameters
5508 given by PARMS. If there are m levels of ARGS and m + n levels of
5509 PARMS, then the result will contain n levels of PARMS. For
5510 example, if PARMS is `template <class T> template <class U>
5511 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5512 result will be `template <int*, double, class V>'. */
5513
e9659ab0 5514static tree
4393e105 5515tsubst_template_parms (parms, args, complain)
36a117a5
MM
5516 tree parms;
5517 tree args;
c2ea3a40 5518 tsubst_flags_t complain;
36a117a5 5519{
f71f87f9
MM
5520 tree r = NULL_TREE;
5521 tree* new_parms;
36a117a5
MM
5522
5523 for (new_parms = &r;
5524 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
5525 new_parms = &(TREE_CHAIN (*new_parms)),
5526 parms = TREE_CHAIN (parms))
5527 {
5528 tree new_vec =
5529 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
5530 int i;
5531
5532 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
5533 {
833aa4c4
NS
5534 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
5535 tree default_value = TREE_PURPOSE (tuple);
5536 tree parm_decl = TREE_VALUE (tuple);
5537
5538 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
5539 default_value = tsubst_expr (default_value, args,
5540 complain, NULL_TREE);
5541 tuple = build_tree_list (maybe_fold_nontype_arg (default_value),
5542 parm_decl);
5543 TREE_VEC_ELT (new_vec, i) = tuple;
36a117a5
MM
5544 }
5545
5546 *new_parms =
4890c2f4
MM
5547 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
5548 - TMPL_ARGS_DEPTH (args)),
36a117a5
MM
5549 new_vec, NULL_TREE);
5550 }
5551
5552 return r;
5553}
5554
ed44da02
MM
5555/* Substitute the ARGS into the indicated aggregate (or enumeration)
5556 type T. If T is not an aggregate or enumeration type, it is
5557 handled as if by tsubst. IN_DECL is as for tsubst. If
838dfd8a 5558 ENTERING_SCOPE is nonzero, T is the context for a template which
dc957d14 5559 we are presently tsubst'ing. Return the substituted value. */
36a117a5 5560
e9659ab0 5561static tree
4393e105 5562tsubst_aggr_type (t, args, complain, in_decl, entering_scope)
36a117a5
MM
5563 tree t;
5564 tree args;
c2ea3a40 5565 tsubst_flags_t complain;
36a117a5
MM
5566 tree in_decl;
5567 int entering_scope;
5568{
5569 if (t == NULL_TREE)
5570 return NULL_TREE;
5571
5572 switch (TREE_CODE (t))
5573 {
5574 case RECORD_TYPE:
5575 if (TYPE_PTRMEMFUNC_P (t))
46c895ac 5576 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
36a117a5
MM
5577
5578 /* else fall through */
ed44da02 5579 case ENUMERAL_TYPE:
36a117a5 5580 case UNION_TYPE:
5db698f6 5581 if (TYPE_TEMPLATE_INFO (t))
36a117a5
MM
5582 {
5583 tree argvec;
5584 tree context;
5585 tree r;
5586
5587 /* First, determine the context for the type we are looking
5588 up. */
5589 if (TYPE_CONTEXT (t) != NULL_TREE)
5590 context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
4393e105 5591 complain,
36a117a5
MM
5592 in_decl, /*entering_scope=*/1);
5593 else
5594 context = NULL_TREE;
5595
5596 /* Then, figure out what arguments are appropriate for the
5597 type we are trying to find. For example, given:
5598
5599 template <class T> struct S;
5600 template <class T, class U> void f(T, U) { S<U> su; }
5601
5602 and supposing that we are instantiating f<int, double>,
5603 then our ARGS will be {int, double}, but, when looking up
5604 S we only want {double}. */
4393e105
MM
5605 argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args,
5606 complain);
08e72a19
JM
5607 if (argvec == error_mark_node)
5608 return error_mark_node;
36a117a5
MM
5609
5610 r = lookup_template_class (t, argvec, in_decl, context,
f9c244b8 5611 entering_scope, complain);
36a117a5 5612
c2ea3a40 5613 return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
36a117a5
MM
5614 }
5615 else
5616 /* This is not a template type, so there's nothing to do. */
5617 return t;
5618
5619 default:
4393e105 5620 return tsubst (t, args, complain, in_decl);
36a117a5
MM
5621 }
5622}
5623
9188c363
MM
5624/* Substitute into the default argument ARG (a default argument for
5625 FN), which has the indicated TYPE. */
5626
5627tree
5628tsubst_default_argument (fn, type, arg)
5629 tree fn;
5630 tree type;
5631 tree arg;
5632{
5633 /* This default argument came from a template. Instantiate the
5634 default argument here, not in tsubst. In the case of
5635 something like:
5636
5637 template <class T>
5638 struct S {
5639 static T t();
5640 void f(T = t());
5641 };
5642
5643 we must be careful to do name lookup in the scope of S<T>,
a29e1034
JM
5644 rather than in the current class.
5645
5646 ??? current_class_type affects a lot more than name lookup. This is
5647 very fragile. Fortunately, it will go away when we do 2-phase name
5648 binding properly. */
9188c363 5649 if (DECL_CLASS_SCOPE_P (fn))
4f1c5b7d 5650 pushclass (DECL_CONTEXT (fn), 2);
9188c363 5651
c2ea3a40
NS
5652 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
5653 tf_error | tf_warning, NULL_TREE);
9188c363
MM
5654
5655 if (DECL_CLASS_SCOPE_P (fn))
5656 popclass ();
5657
5658 /* Make sure the default argument is reasonable. */
5659 arg = check_default_argument (type, arg);
5660
5661 return arg;
5662}
5663
5664/* Substitute into all the default arguments for FN. */
5665
5666static void
5667tsubst_default_arguments (fn)
5668 tree fn;
5669{
5670 tree arg;
5671 tree tmpl_args;
5672
5673 tmpl_args = DECL_TI_ARGS (fn);
5674
5675 /* If this function is not yet instantiated, we certainly don't need
5676 its default arguments. */
5677 if (uses_template_parms (tmpl_args))
5678 return;
5679
5680 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
5681 arg;
5682 arg = TREE_CHAIN (arg))
5683 if (TREE_PURPOSE (arg))
5684 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
5685 TREE_VALUE (arg),
5686 TREE_PURPOSE (arg));
5687}
5688
ae58fa02
MM
5689/* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5690 (already computed) substitution of ARGS into TREE_TYPE (T), if
c2ea3a40
NS
5691 appropriate. Return the result of the substitution. Issue error
5692 and warning messages under control of COMPLAIN. */
00d3396f 5693
e9659ab0 5694static tree
c2ea3a40 5695tsubst_decl (t, args, type, complain)
ae58fa02
MM
5696 tree t;
5697 tree args;
5698 tree type;
c2ea3a40 5699 tsubst_flags_t complain;
8d08fdba 5700{
ae58fa02 5701 int saved_lineno;
3b304f5b 5702 const char *saved_filename;
b370501f 5703 tree r = NULL_TREE;
4b2811e9 5704 tree in_decl = t;
830bfa74 5705
ae58fa02
MM
5706 /* Set the filename and linenumber to improve error-reporting. */
5707 saved_lineno = lineno;
5708 saved_filename = input_filename;
5709 lineno = DECL_SOURCE_LINE (t);
5710 input_filename = DECL_SOURCE_FILE (t);
b7484fbe 5711
8d08fdba
MS
5712 switch (TREE_CODE (t))
5713 {
98c1c668
JM
5714 case TEMPLATE_DECL:
5715 {
5716 /* We can get here when processing a member template function
5717 of a template class. */
98c1c668 5718 tree decl = DECL_TEMPLATE_RESULT (t);
386b8a85 5719 tree spec;
db2767b6 5720 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
98c1c668 5721
db2767b6
MM
5722 if (!is_template_template_parm)
5723 {
36a117a5
MM
5724 /* We might already have an instance of this template.
5725 The ARGS are for the surrounding class type, so the
5726 full args contain the tsubst'd args for the context,
5727 plus the innermost args from the template decl. */
5728 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
5729 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
17aec3eb 5730 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
7ddedda4
MM
5731 tree full_args;
5732
4393e105 5733 full_args = tsubst_template_arg_vector (tmpl_args, args,
c2ea3a40 5734 complain);
36a117a5
MM
5735
5736 /* tsubst_template_arg_vector doesn't copy the vector if
5737 nothing changed. But, *something* should have
5738 changed. */
5739 my_friendly_assert (full_args != tmpl_args, 0);
5740
5741 spec = retrieve_specialization (t, full_args);
db2767b6 5742 if (spec != NULL_TREE)
ae58fa02
MM
5743 {
5744 r = spec;
5745 break;
5746 }
db2767b6 5747 }
98c1c668
JM
5748
5749 /* Make a new template decl. It will be similar to the
5750 original, but will record the current template arguments.
5751 We also create a new function declaration, which is just
5752 like the old one, but points to this new template, rather
5753 than the old one. */
0acf7199 5754 r = copy_decl (t);
ae58fa02
MM
5755 my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
5756 TREE_CHAIN (r) = NULL_TREE;
db2767b6
MM
5757
5758 if (is_template_template_parm)
5759 {
c2ea3a40 5760 tree new_decl = tsubst (decl, args, complain, in_decl);
17aec3eb 5761 DECL_TEMPLATE_RESULT (r) = new_decl;
ae58fa02
MM
5762 TREE_TYPE (r) = TREE_TYPE (new_decl);
5763 break;
db2767b6
MM
5764 }
5765
ae58fa02 5766 DECL_CONTEXT (r)
4f1c5b7d 5767 = tsubst_aggr_type (DECL_CONTEXT (t), args,
c2ea3a40 5768 complain, in_decl,
4393e105 5769 /*entering_scope=*/1);
4f1c5b7d
MM
5770 DECL_VIRTUAL_CONTEXT (r)
5771 = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t), args,
c2ea3a40 5772 complain, in_decl,
4f1c5b7d 5773 /*entering_scope=*/1);
ae58fa02 5774 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
93cdc044
JM
5775
5776 if (TREE_CODE (decl) == TYPE_DECL)
5777 {
c2ea3a40 5778 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
ae58fa02
MM
5779 TREE_TYPE (r) = new_type;
5780 CLASSTYPE_TI_TEMPLATE (new_type) = r;
17aec3eb 5781 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
ae58fa02 5782 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
93cdc044
JM
5783 }
5784 else
5785 {
c2ea3a40 5786 tree new_decl = tsubst (decl, args, complain, in_decl);
17aec3eb
RK
5787
5788 DECL_TEMPLATE_RESULT (r) = new_decl;
ae58fa02
MM
5789 DECL_TI_TEMPLATE (new_decl) = r;
5790 TREE_TYPE (r) = TREE_TYPE (new_decl);
5791 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
93cdc044
JM
5792 }
5793
ae58fa02
MM
5794 SET_DECL_IMPLICIT_INSTANTIATION (r);
5795 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
5796 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
98c1c668
JM
5797
5798 /* The template parameters for this new template are all the
5799 template parameters for the old template, except the
c6002625 5800 outermost level of parameters. */
ae58fa02 5801 DECL_TEMPLATE_PARMS (r)
4393e105 5802 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
c2ea3a40 5803 complain);
98c1c668 5804
93cdc044 5805 if (PRIMARY_TEMPLATE_P (t))
ae58fa02 5806 DECL_PRIMARY_TEMPLATE (r) = r;
93cdc044 5807
8d019cef 5808 /* We don't partially instantiate partial specializations. */
93cdc044 5809 if (TREE_CODE (decl) == TYPE_DECL)
ae58fa02 5810 break;
93cdc044 5811
75650646 5812 /* Record this partial instantiation. */
ae58fa02 5813 register_specialization (r, t,
17aec3eb 5814 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)));
75650646 5815
98c1c668 5816 }
ae58fa02 5817 break;
8d08fdba
MS
5818
5819 case FUNCTION_DECL:
5820 {
386b8a85 5821 tree ctx;
87603ed0 5822 tree argvec = NULL_TREE;
cf38f48a 5823 tree *friends;
36a117a5 5824 tree gen_tmpl;
5566b478 5825 int member;
d8c4447d
MM
5826 int args_depth;
5827 int parms_depth;
5566b478 5828
36a117a5
MM
5829 /* Nobody should be tsubst'ing into non-template functions. */
5830 my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5831
5832 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
5833 {
5834 tree spec;
5835
5836 /* Calculate the most general template of which R is a
5837 specialization, and the complete set of arguments used to
5838 specialize R. */
5839 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
7ddedda4
MM
5840 argvec
5841 = tsubst_template_arg_vector (DECL_TI_ARGS
5842 (DECL_TEMPLATE_RESULT (gen_tmpl)),
c2ea3a40 5843 args, complain);
36a117a5
MM
5844
5845 /* Check to see if we already have this specialization. */
5846 spec = retrieve_specialization (gen_tmpl, argvec);
7ddedda4 5847
36a117a5 5848 if (spec)
ae58fa02
MM
5849 {
5850 r = spec;
5851 break;
5852 }
d8c4447d 5853
f9a7ae04
MM
5854 /* We can see more levels of arguments than parameters if
5855 there was a specialization of a member template, like
5856 this:
5857
5858 template <class T> struct S { template <class U> void f(); }
5859 template <> template <class U> void S<int>::f(U);
5860
dc957d14 5861 Here, we'll be substituting into the specialization,
f9a7ae04
MM
5862 because that's where we can find the code we actually
5863 want to generate, but we'll have enough arguments for
5864 the most general template.
5865
5866 We also deal with the peculiar case:
d8c4447d
MM
5867
5868 template <class T> struct S {
5869 template <class U> friend void f();
5870 };
74b846e0 5871 template <class U> void f() {}
d8c4447d
MM
5872 template S<int>;
5873 template void f<double>();
5874
5875 Here, the ARGS for the instantiation of will be {int,
5876 double}. But, we only need as many ARGS as there are
5877 levels of template parameters in CODE_PATTERN. We are
5878 careful not to get fooled into reducing the ARGS in
5879 situations like:
5880
5881 template <class T> struct S { template <class U> void f(U); }
5882 template <class T> template <> void S<T>::f(int) {}
5883
5884 which we can spot because the pattern will be a
5885 specialization in this case. */
5886 args_depth = TMPL_ARGS_DEPTH (args);
5887 parms_depth =
5888 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
5889 if (args_depth > parms_depth
5890 && !DECL_TEMPLATE_SPECIALIZATION (t))
f9a7ae04 5891 args = get_innermost_template_args (args, parms_depth);
36a117a5
MM
5892 }
5893 else
5894 {
5895 /* This special case arises when we have something like this:
5896
5897 template <class T> struct S {
5898 friend void f<int>(int, double);
5899 };
5900
5901 Here, the DECL_TI_TEMPLATE for the friend declaration
39c01e4c
MM
5902 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5903 being called from tsubst_friend_function, and we want
5904 only to create a new decl (R) with appropriate types so
5905 that we can call determine_specialization. */
5906 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t))
5907 == LOOKUP_EXPR)
5908 || (TREE_CODE (DECL_TI_TEMPLATE (t))
5909 == IDENTIFIER_NODE), 0);
36a117a5
MM
5910 gen_tmpl = NULL_TREE;
5911 }
5912
6eb3bb27 5913 if (DECL_CLASS_SCOPE_P (t))
8d08fdba 5914 {
5566b478
MS
5915 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
5916 member = 2;
5917 else
5918 member = 1;
4f1c5b7d 5919 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
c2ea3a40 5920 complain, t,
36a117a5 5921 /*entering_scope=*/1);
5566b478
MS
5922 }
5923 else
5924 {
5925 member = 0;
4f1c5b7d 5926 ctx = DECL_CONTEXT (t);
5566b478 5927 }
c2ea3a40 5928 type = tsubst (type, args, complain, in_decl);
b3da7bb1
MM
5929 if (type == error_mark_node)
5930 return error_mark_node;
8d08fdba 5931
5566b478
MS
5932 /* We do NOT check for matching decls pushed separately at this
5933 point, as they may not represent instantiations of this
5934 template, and in any case are considered separate under the
0a7394bc 5935 discrete model. */
0acf7199 5936 r = copy_decl (t);
e1467ff2 5937 DECL_USE_TEMPLATE (r) = 0;
5566b478 5938 TREE_TYPE (r) = type;
92643fea
MM
5939 /* Clear out the mangled name and RTL for the instantiation. */
5940 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
5941 SET_DECL_RTL (r, NULL_RTX);
5566b478 5942
4f1c5b7d
MM
5943 DECL_CONTEXT (r) = ctx;
5944 DECL_VIRTUAL_CONTEXT (r)
5945 = tsubst_aggr_type (DECL_VIRTUAL_CONTEXT (t), args,
c2ea3a40 5946 complain, t,
4393e105 5947 /*entering_scope=*/1);
5566b478 5948
1f6e1acc 5949 if (member && DECL_CONV_FN_P (r))
1f84ec23
MM
5950 /* Type-conversion operator. Reconstruct the name, in
5951 case it's the name of one of the template's parameters. */
5952 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
5566b478 5953
4393e105 5954 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
c2ea3a40 5955 complain, t);
477f6664 5956 DECL_RESULT (r) = NULL_TREE;
711734a9
JM
5957
5958 TREE_STATIC (r) = 0;
5959 TREE_PUBLIC (r) = TREE_PUBLIC (t);
5960 DECL_EXTERNAL (r) = 1;
5961 DECL_INTERFACE_KNOWN (r) = 0;
5962 DECL_DEFER_OUTPUT (r) = 0;
5963 TREE_CHAIN (r) = NULL_TREE;
5964 DECL_PENDING_INLINE_INFO (r) = 0;
59026e79 5965 DECL_PENDING_INLINE_P (r) = 0;
655dc6ee 5966 DECL_SAVED_TREE (r) = NULL_TREE;
711734a9 5967 TREE_USED (r) = 0;
db9b2174
MM
5968 if (DECL_CLONED_FUNCTION (r))
5969 {
5970 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
c2ea3a40 5971 args, complain, t);
db9b2174
MM
5972 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
5973 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
5974 }
711734a9 5975
92643fea
MM
5976 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
5977 this in the special friend case mentioned above where
5978 GEN_TMPL is NULL. */
36a117a5 5979 if (gen_tmpl)
386b8a85 5980 {
36a117a5 5981 DECL_TEMPLATE_INFO (r)
e1b3e07d 5982 = tree_cons (gen_tmpl, argvec, NULL_TREE);
36a117a5
MM
5983 SET_DECL_IMPLICIT_INSTANTIATION (r);
5984 register_specialization (r, gen_tmpl, argvec);
5985
9188c363
MM
5986 /* We're not supposed to instantiate default arguments
5987 until they are called, for a template. But, for a
5988 declaration like:
5989
5990 template <class T> void f ()
5991 { extern void g(int i = T()); }
5992
5993 we should do the substitution when the template is
5994 instantiated. We handle the member function case in
5995 instantiate_class_template since the default arguments
5996 might refer to other members of the class. */
5997 if (!member
5998 && !PRIMARY_TEMPLATE_P (gen_tmpl)
5999 && !uses_template_parms (argvec))
6000 tsubst_default_arguments (r);
386b8a85 6001 }
f181d4ae 6002
cf38f48a
MM
6003 /* Copy the list of befriending classes. */
6004 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6005 *friends;
6006 friends = &TREE_CHAIN (*friends))
6007 {
6008 *friends = copy_node (*friends);
6009 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
c2ea3a40 6010 args, complain,
cf38f48a
MM
6011 in_decl);
6012 }
6013
212e7048 6014 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
f181d4ae
MM
6015 {
6016 maybe_retrofit_in_chrg (r);
212e7048
MM
6017 if (DECL_CONSTRUCTOR_P (r))
6018 grok_ctor_properties (ctx, r);
2be678ff
JM
6019 /* If this is an instantiation of a member template, clone it.
6020 If it isn't, that'll be handled by
6021 clone_constructors_and_destructors. */
5e818b93 6022 if (PRIMARY_TEMPLATE_P (gen_tmpl))
271e6f02 6023 clone_function_decl (r, /*update_method_vec_p=*/0);
f181d4ae 6024 }
596ea4e5 6025 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
271e6f02 6026 grok_op_properties (r, DECL_FRIEND_P (r));
8d08fdba 6027 }
ae58fa02 6028 break;
8d08fdba
MS
6029
6030 case PARM_DECL:
6031 {
ae58fa02 6032 r = copy_node (t);
833aa4c4
NS
6033 if (DECL_TEMPLATE_PARM_P (t))
6034 SET_DECL_TEMPLATE_PARM_P (r);
8e51619a 6035
5566b478 6036 TREE_TYPE (r) = type;
89d684bb 6037 c_apply_type_quals_to_decl (cp_type_quals (type), r);
1b8899d1 6038
db2767b6
MM
6039 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6040 DECL_INITIAL (r) = TREE_TYPE (r);
6041 else
4393e105 6042 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
c2ea3a40 6043 complain, in_decl);
db2767b6 6044
5566b478 6045 DECL_CONTEXT (r) = NULL_TREE;
8e51619a
JM
6046
6047 if (!DECL_TEMPLATE_PARM_P (r))
6048 DECL_ARG_TYPE (r) = type_passed_as (type);
8d08fdba 6049 if (TREE_CHAIN (t))
4393e105 6050 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
c2ea3a40 6051 complain, TREE_CHAIN (t));
8d08fdba 6052 }
ae58fa02 6053 break;
8d08fdba 6054
5566b478
MS
6055 case FIELD_DECL:
6056 {
0acf7199 6057 r = copy_decl (t);
1b8899d1 6058 TREE_TYPE (r) = type;
89d684bb 6059 c_apply_type_quals_to_decl (cp_type_quals (type), r);
1b8899d1
MM
6060
6061 /* We don't have to set DECL_CONTEXT here; it is set by
6062 finish_member_declaration. */
4393e105 6063 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
c2ea3a40 6064 complain, in_decl);
5566b478 6065 TREE_CHAIN (r) = NULL_TREE;
dfbb4f34
NS
6066 if (VOID_TYPE_P (type))
6067 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
5566b478 6068 }
ae58fa02 6069 break;
5566b478
MS
6070
6071 case USING_DECL:
6072 {
ae58fa02 6073 r = copy_node (t);
5566b478 6074 DECL_INITIAL (r)
c2ea3a40 6075 = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
5566b478 6076 TREE_CHAIN (r) = NULL_TREE;
5566b478 6077 }
ae58fa02 6078 break;
5566b478 6079
9188c363 6080 case TYPE_DECL:
88e1b92a
JM
6081 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6082 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
9188c363 6083 {
88e1b92a
JM
6084 /* If this is the canonical decl, we don't have to mess with
6085 instantiations, and often we can't (for typename, template
6086 type parms and such). Note that TYPE_NAME is not correct for
6087 the above test if we've copied the type for a typedef. */
b9f39201 6088 r = TYPE_NAME (type);
9188c363
MM
6089 break;
6090 }
6091
6092 /* Fall through. */
6093
5566b478
MS
6094 case VAR_DECL:
6095 {
1cea0434
KG
6096 tree argvec = NULL_TREE;
6097 tree gen_tmpl = NULL_TREE;
36a117a5 6098 tree spec;
1cea0434 6099 tree tmpl = NULL_TREE;
9188c363 6100 tree ctx;
6dfbb909 6101 int local_p;
9188c363 6102
6dfbb909
MM
6103 /* Assume this is a non-local variable. */
6104 local_p = 0;
5566b478 6105
de96bf57 6106 if (TYPE_P (CP_DECL_CONTEXT (t)))
9188c363 6107 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
c2ea3a40 6108 complain,
9188c363 6109 in_decl, /*entering_scope=*/1);
2ba9c47e
MM
6110 else if (DECL_NAMESPACE_SCOPE_P (t))
6111 ctx = DECL_CONTEXT (t);
9188c363 6112 else
6dfbb909
MM
6113 {
6114 /* Subsequent calls to pushdecl will fill this in. */
6115 ctx = NULL_TREE;
2ba9c47e 6116 local_p = 1;
6dfbb909 6117 }
9188c363 6118
36a117a5 6119 /* Check to see if we already have this specialization. */
6dfbb909
MM
6120 if (!local_p)
6121 {
6122 tmpl = DECL_TI_TEMPLATE (t);
6123 gen_tmpl = most_general_template (tmpl);
c2ea3a40 6124 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6dfbb909
MM
6125 spec = retrieve_specialization (gen_tmpl, argvec);
6126 }
9188c363 6127 else
6dfbb909 6128 spec = retrieve_local_specialization (t);
9188c363 6129
36a117a5 6130 if (spec)
ae58fa02
MM
6131 {
6132 r = spec;
6133 break;
6134 }
5566b478 6135
0acf7199 6136 r = copy_decl (t);
01f4137f 6137 TREE_TYPE (r) = type;
89d684bb 6138 c_apply_type_quals_to_decl (cp_type_quals (type), r);
5566b478 6139 DECL_CONTEXT (r) = ctx;
92643fea
MM
6140 /* Clear out the mangled name and RTL for the instantiation. */
6141 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6142 SET_DECL_RTL (r, NULL_RTX);
d11ad92e
MS
6143
6144 /* Don't try to expand the initializer until someone tries to use
6145 this variable; otherwise we run into circular dependencies. */
6146 DECL_INITIAL (r) = NULL_TREE;
19e7881c 6147 SET_DECL_RTL (r, NULL_RTX);
06ceef4e 6148 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
5566b478 6149
e0942dcd
MM
6150 /* Even if the original location is out of scope, the newly
6151 substituted one is not. */
6152 if (TREE_CODE (r) == VAR_DECL)
17bbb839
MM
6153 {
6154 DECL_DEAD_FOR_LOCAL (r) = 0;
6155 DECL_INITIALIZED_P (r) = 0;
6156 }
e0942dcd 6157
6dfbb909
MM
6158 if (!local_p)
6159 {
6160 /* A static data member declaration is always marked
6161 external when it is declared in-class, even if an
6162 initializer is present. We mimic the non-template
6163 processing here. */
6164 DECL_EXTERNAL (r) = 1;
fa8d6e85 6165
6dfbb909
MM
6166 register_specialization (r, gen_tmpl, argvec);
6167 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6168 SET_DECL_IMPLICIT_INSTANTIATION (r);
6169 }
9188c363 6170 else
6dfbb909 6171 register_local_specialization (r, t);
5566b478 6172
5566b478 6173 TREE_CHAIN (r) = NULL_TREE;
dfbb4f34
NS
6174 if (TREE_CODE (r) == VAR_DECL && VOID_TYPE_P (type))
6175 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
5566b478 6176 }
ae58fa02 6177 break;
5566b478 6178
ae58fa02 6179 default:
a98facb0 6180 abort ();
ae58fa02
MM
6181 }
6182
6183 /* Restore the file and line information. */
6184 lineno = saved_lineno;
6185 input_filename = saved_filename;
6186
6187 return r;
6188}
6189
cabc336a
MM
6190/* Substitue into the ARG_TYPES of a function type. */
6191
e9659ab0 6192static tree
4393e105 6193tsubst_arg_types (arg_types, args, complain, in_decl)
cabc336a
MM
6194 tree arg_types;
6195 tree args;
c2ea3a40 6196 tsubst_flags_t complain;
cabc336a
MM
6197 tree in_decl;
6198{
6199 tree remaining_arg_types;
cabc336a
MM
6200 tree type;
6201
6202 if (!arg_types || arg_types == void_list_node)
6203 return arg_types;
6204
6205 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
4393e105
MM
6206 args, complain, in_decl);
6207 if (remaining_arg_types == error_mark_node)
6208 return error_mark_node;
6209
6210 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6211 if (type == error_mark_node)
6212 return error_mark_node;
4b2811e9
NS
6213 if (VOID_TYPE_P (type))
6214 {
c2ea3a40 6215 if (complain & tf_error)
4b2811e9 6216 {
33bd39a2 6217 error ("invalid parameter type `%T'", type);
4b2811e9
NS
6218 if (in_decl)
6219 cp_error_at ("in declaration `%D'", in_decl);
6220 }
6221 return error_mark_node;
6222 }
cabc336a 6223
4393e105
MM
6224 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6225 top-level qualifiers as required. */
6226 type = TYPE_MAIN_VARIANT (type_decays_to (type));
cabc336a
MM
6227
6228 /* Note that we do not substitute into default arguments here. The
6229 standard mandates that they be instantiated only when needed,
6230 which is done in build_over_call. */
51632249
JM
6231 return hash_tree_cons (TREE_PURPOSE (arg_types), type,
6232 remaining_arg_types);
cabc336a
MM
6233
6234}
6235
4393e105
MM
6236/* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6237 *not* handle the exception-specification for FNTYPE, because the
6238 initial substitution of explicitly provided template parameters
6239 during argument deduction forbids substitution into the
6240 exception-specification:
6241
6242 [temp.deduct]
6243
6244 All references in the function type of the function template to the
6245 corresponding template parameters are replaced by the specified tem-
6246 plate argument values. If a substitution in a template parameter or
6247 in the function type of the function template results in an invalid
6248 type, type deduction fails. [Note: The equivalent substitution in
6249 exception specifications is done only when the function is instanti-
6250 ated, at which point a program is ill-formed if the substitution
6251 results in an invalid type.] */
6252
6253static tree
6254tsubst_function_type (t, args, complain, in_decl)
6255 tree t;
6256 tree args;
c2ea3a40 6257 tsubst_flags_t complain;
4393e105
MM
6258 tree in_decl;
6259{
6260 tree return_type;
6261 tree arg_types;
6262 tree fntype;
6263
8dd3f57a 6264 /* The TYPE_CONTEXT is not used for function/method types. */
4393e105
MM
6265 my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0);
6266
46c895ac 6267 /* Substitute the return type. */
4393e105
MM
6268 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6269 if (return_type == error_mark_node)
6270 return error_mark_node;
6271
6272 /* Substitue the argument types. */
6273 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6274 complain, in_decl);
6275 if (arg_types == error_mark_node)
6276 return error_mark_node;
6277
6278 /* Construct a new type node and return it. */
6279 if (TREE_CODE (t) == FUNCTION_TYPE)
6280 fntype = build_function_type (return_type, arg_types);
6281 else
6282 {
6283 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6284 if (! IS_AGGR_TYPE (r))
6285 {
6286 /* [temp.deduct]
6287
6288 Type deduction may fail for any of the following
6289 reasons:
6290
6291 -- Attempting to create "pointer to member of T" when T
6292 is not a class type. */
c2ea3a40 6293 if (complain & tf_error)
33bd39a2 6294 error ("creating pointer to member function of non-class type `%T'",
4393e105
MM
6295 r);
6296 return error_mark_node;
6297 }
6298
6299 fntype = build_cplus_method_type (r, return_type, TREE_CHAIN
6300 (arg_types));
6301 }
c2ea3a40 6302 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
ada846eb 6303 fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
4393e105
MM
6304
6305 return fntype;
6306}
6307
297e73d8
MM
6308/* Substitute into the PARMS of a call-declarator. */
6309
e9659ab0 6310static tree
4393e105 6311tsubst_call_declarator_parms (parms, args, complain, in_decl)
297e73d8
MM
6312 tree parms;
6313 tree args;
c2ea3a40 6314 tsubst_flags_t complain;
297e73d8
MM
6315 tree in_decl;
6316{
6317 tree new_parms;
6318 tree type;
6319 tree defarg;
6320
6321 if (!parms || parms == void_list_node)
6322 return parms;
6323
6324 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
4393e105 6325 args, complain, in_decl);
297e73d8
MM
6326
6327 /* Figure out the type of this parameter. */
4393e105 6328 type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
297e73d8
MM
6329
6330 /* Figure out the default argument as well. Note that we use
4393e105
MM
6331 tsubst_expr since the default argument is really an expression. */
6332 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
297e73d8
MM
6333
6334 /* Chain this parameter on to the front of those we have already
6335 processed. We don't use hash_tree_cons because that function
6336 doesn't check TREE_PARMLIST. */
6337 new_parms = tree_cons (defarg, type, new_parms);
6338
6339 /* And note that these are parameters. */
6340 TREE_PARMLIST (new_parms) = 1;
6341
6342 return new_parms;
6343}
6344
4393e105
MM
6345/* Take the tree structure T and replace template parameters used
6346 therein with the argument vector ARGS. IN_DECL is an associated
6347 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
c2ea3a40
NS
6348 Issue error and warning messages under control of COMPLAIN. Note
6349 that we must be relatively non-tolerant of extensions here, in
6350 order to preserve conformance; if we allow substitutions that
6351 should not be allowed, we may allow argument deductions that should
6352 not succeed, and therefore report ambiguous overload situations
6353 where there are none. In theory, we could allow the substitution,
6354 but indicate that it should have failed, and allow our caller to
6355 make sure that the right thing happens, but we don't try to do this
6356 yet.
4393e105
MM
6357
6358 This function is used for dealing with types, decls and the like;
6359 for expressions, use tsubst_expr or tsubst_copy. */
ae58fa02
MM
6360
6361tree
4393e105 6362tsubst (t, args, complain, in_decl)
ae58fa02 6363 tree t, args;
c2ea3a40 6364 tsubst_flags_t complain;
ae58fa02
MM
6365 tree in_decl;
6366{
0ecfe0b4 6367 tree type, r;
ae58fa02
MM
6368
6369 if (t == NULL_TREE || t == error_mark_node
6370 || t == integer_type_node
6371 || t == void_type_node
6372 || t == char_type_node
6373 || TREE_CODE (t) == NAMESPACE_DECL)
6374 return t;
6375
6376 if (TREE_CODE (t) == IDENTIFIER_NODE)
6377 type = IDENTIFIER_TYPE_VALUE (t);
6378 else
6379 type = TREE_TYPE (t);
6380 if (type == unknown_type_node)
a98facb0 6381 abort ();
ae58fa02
MM
6382
6383 if (type && TREE_CODE (t) != FUNCTION_DECL
6384 && TREE_CODE (t) != TYPENAME_TYPE
6385 && TREE_CODE (t) != TEMPLATE_DECL
4393e105
MM
6386 && TREE_CODE (t) != IDENTIFIER_NODE
6387 && TREE_CODE (t) != FUNCTION_TYPE
6388 && TREE_CODE (t) != METHOD_TYPE)
6389 type = tsubst (type, args, complain, in_decl);
6390 if (type == error_mark_node)
6391 return error_mark_node;
ae58fa02 6392
2f939d94 6393 if (DECL_P (t))
c2ea3a40 6394 return tsubst_decl (t, args, type, complain);
ae58fa02
MM
6395
6396 switch (TREE_CODE (t))
6397 {
6398 case RECORD_TYPE:
6399 case UNION_TYPE:
6400 case ENUMERAL_TYPE:
4393e105
MM
6401 return tsubst_aggr_type (t, args, complain, in_decl,
6402 /*entering_scope=*/0);
ae58fa02
MM
6403
6404 case ERROR_MARK:
6405 case IDENTIFIER_NODE:
ae58fa02
MM
6406 case VOID_TYPE:
6407 case REAL_TYPE:
6408 case COMPLEX_TYPE:
c00996a3 6409 case VECTOR_TYPE:
ae58fa02
MM
6410 case BOOLEAN_TYPE:
6411 case INTEGER_CST:
6412 case REAL_CST:
6413 case STRING_CST:
6414 return t;
6415
6416 case INTEGER_TYPE:
6417 if (t == integer_type_node)
6418 return t;
6419
6420 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
6421 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
6422 return t;
d2e5ee5c 6423
5566b478 6424 {
ddce3528 6425 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
7ddedda4 6426
ddce3528 6427 max = tsubst_expr (omax, args, complain, in_decl);
4393e105
MM
6428 if (max == error_mark_node)
6429 return error_mark_node;
6430
8dd3f57a
MM
6431 /* See if we can reduce this expression to something simpler. */
6432 max = maybe_fold_nontype_arg (max);
fc611ce0 6433 if (!processing_template_decl)
8dd3f57a
MM
6434 max = decl_constant_value (max);
6435
856216bb
MM
6436 if (processing_template_decl
6437 /* When providing explicit arguments to a template
6438 function, but leaving some arguments for subsequent
6439 deduction, MAX may be template-dependent even if we're
c95cd22e
JM
6440 not PROCESSING_TEMPLATE_DECL. We still need to check for
6441 template parms, though; MAX won't be an INTEGER_CST for
6442 dynamic arrays, either. */
6443 || (TREE_CODE (max) != INTEGER_CST
6444 && uses_template_parms (max)))
ae58fa02 6445 {
8dd3f57a
MM
6446 tree itype = make_node (INTEGER_TYPE);
6447 TYPE_MIN_VALUE (itype) = size_zero_node;
6448 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
6449 integer_one_node);
6450 return itype;
ae58fa02
MM
6451 }
6452
ddce3528
JM
6453 if (integer_zerop (omax))
6454 {
6455 /* Still allow an explicit array of size zero. */
6456 if (pedantic)
6457 pedwarn ("creating array with size zero");
6458 }
07c88314
MM
6459 else if (integer_zerop (max)
6460 || (TREE_CODE (max) == INTEGER_CST
6461 && INT_CST_LT (max, integer_zero_node)))
0ecfe0b4 6462 {
4393e105
MM
6463 /* [temp.deduct]
6464
6465 Type deduction may fail for any of the following
6466 reasons:
6467
bf8f3f93
MM
6468 Attempting to create an array with a size that is
6469 zero or negative. */
c2ea3a40 6470 if (complain & tf_error)
33bd39a2 6471 error ("creating array with size zero (`%E')", max);
4393e105
MM
6472
6473 return error_mark_node;
0ecfe0b4
JM
6474 }
6475
c95cd22e 6476 return compute_array_index_type (NULL_TREE, max);
ae58fa02
MM
6477 }
6478
6479 case TEMPLATE_TYPE_PARM:
6480 case TEMPLATE_TEMPLATE_PARM:
a1281f45 6481 case BOUND_TEMPLATE_TEMPLATE_PARM:
ae58fa02
MM
6482 case TEMPLATE_PARM_INDEX:
6483 {
6484 int idx;
6485 int level;
6486 int levels;
0ecfe0b4
JM
6487
6488 r = NULL_TREE;
ae58fa02
MM
6489
6490 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
a1281f45
KL
6491 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
6492 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
ae58fa02
MM
6493 {
6494 idx = TEMPLATE_TYPE_IDX (t);
6495 level = TEMPLATE_TYPE_LEVEL (t);
6496 }
6497 else
6498 {
6499 idx = TEMPLATE_PARM_IDX (t);
6500 level = TEMPLATE_PARM_LEVEL (t);
6501 }
6502
6503 if (TREE_VEC_LENGTH (args) > 0)
6504 {
6505 tree arg = NULL_TREE;
6506
6507 levels = TMPL_ARGS_DEPTH (args);
6508 if (level <= levels)
6509 arg = TMPL_ARG (args, level, idx);
6510
8b5b8b7c
MM
6511 if (arg == error_mark_node)
6512 return error_mark_node;
6513 else if (arg != NULL_TREE)
ae58fa02
MM
6514 {
6515 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
6516 {
2f939d94 6517 my_friendly_assert (TYPE_P (arg), 0);
adecb3f4 6518 return cp_build_qualified_type_real
89d684bb 6519 (arg, cp_type_quals (arg) | cp_type_quals (t),
4f2b0fb2 6520 complain | tf_ignore_bad_quals);
ae58fa02 6521 }
a1281f45 6522 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
ae58fa02 6523 {
a1281f45
KL
6524 /* We are processing a type constructed from
6525 a template template parameter */
6526 tree argvec = tsubst (TYPE_TI_ARGS (t),
6527 args, complain, in_decl);
6528 if (argvec == error_mark_node)
6529 return error_mark_node;
4393e105 6530
a1281f45
KL
6531 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6532 we are resolving nested-types in the signature of
6533 a member function templates.
6534 Otherwise ARG is a TEMPLATE_DECL and is the real
6535 template to be instantiated. */
6536 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
6537 arg = TYPE_NAME (arg);
6538
6539 r = lookup_template_class (arg,
6540 argvec, in_decl,
6541 DECL_CONTEXT (arg),
f9c244b8
NS
6542 /*entering_scope=*/0,
6543 complain);
c2ea3a40
NS
6544 return cp_build_qualified_type_real
6545 (r, TYPE_QUALS (t), complain);
ae58fa02
MM
6546 }
6547 else
a1281f45 6548 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
ae58fa02
MM
6549 return arg;
6550 }
6551 }
51924768 6552 else
a98facb0 6553 abort ();
ae58fa02
MM
6554
6555 if (level == 1)
6556 /* This can happen during the attempted tsubst'ing in
6557 unify. This means that we don't yet have any information
6558 about the template parameter in question. */
6559 return t;
6560
6561 /* If we get here, we must have been looking at a parm for a
6562 more deeply nested template. Make a new version of this
6563 template parameter, but with a lower level. */
6564 switch (TREE_CODE (t))
6565 {
6566 case TEMPLATE_TYPE_PARM:
6567 case TEMPLATE_TEMPLATE_PARM:
a1281f45 6568 case BOUND_TEMPLATE_TEMPLATE_PARM:
89d684bb 6569 if (cp_type_quals (t))
ae58fa02 6570 {
9ccf6541 6571 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
4f2b0fb2
NS
6572 r = cp_build_qualified_type_real
6573 (r, cp_type_quals (t),
6574 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6575 ? tf_ignore_bad_quals : 0));
9ccf6541
MM
6576 }
6577 else
6578 {
11e74ea6 6579 r = copy_type (t);
9ccf6541
MM
6580 TEMPLATE_TYPE_PARM_INDEX (r)
6581 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
6582 r, levels);
6583 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
6584 TYPE_MAIN_VARIANT (r) = r;
6585 TYPE_POINTER_TO (r) = NULL_TREE;
6586 TYPE_REFERENCE_TO (r) = NULL_TREE;
6587
a1281f45 6588 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9ccf6541
MM
6589 {
6590 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
6591 complain, in_decl);
6592 if (argvec == error_mark_node)
6593 return error_mark_node;
4393e105 6594
9ccf6541
MM
6595 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
6596 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
6597 }
ae58fa02
MM
6598 }
6599 break;
6600
6601 case TEMPLATE_PARM_INDEX:
6602 r = reduce_template_parm_level (t, type, levels);
6603 break;
6604
6605 default:
a98facb0 6606 abort ();
ae58fa02
MM
6607 }
6608
5566b478 6609 return r;
ae58fa02 6610 }
5566b478 6611
8d08fdba
MS
6612 case TREE_LIST:
6613 {
6614 tree purpose, value, chain, result;
8d08fdba
MS
6615
6616 if (t == void_list_node)
6617 return t;
6618
8d08fdba
MS
6619 purpose = TREE_PURPOSE (t);
6620 if (purpose)
4393e105
MM
6621 {
6622 purpose = tsubst (purpose, args, complain, in_decl);
6623 if (purpose == error_mark_node)
6624 return error_mark_node;
6625 }
8d08fdba
MS
6626 value = TREE_VALUE (t);
6627 if (value)
4393e105
MM
6628 {
6629 value = tsubst (value, args, complain, in_decl);
6630 if (value == error_mark_node)
6631 return error_mark_node;
6632 }
8d08fdba
MS
6633 chain = TREE_CHAIN (t);
6634 if (chain && chain != void_type_node)
4393e105
MM
6635 {
6636 chain = tsubst (chain, args, complain, in_decl);
6637 if (chain == error_mark_node)
6638 return error_mark_node;
6639 }
8d08fdba
MS
6640 if (purpose == TREE_PURPOSE (t)
6641 && value == TREE_VALUE (t)
6642 && chain == TREE_CHAIN (t))
6643 return t;
cbd5937a
MM
6644 if (TREE_PARMLIST (t))
6645 {
6646 result = tree_cons (purpose, value, chain);
6647 TREE_PARMLIST (result) = 1;
6648 }
6649 else
6650 result = hash_tree_cons (purpose, value, chain);
8d08fdba
MS
6651 return result;
6652 }
6653 case TREE_VEC:
5566b478
MS
6654 if (type != NULL_TREE)
6655 {
1ceaad38
JM
6656 /* A binfo node. We always need to make a copy, of the node
6657 itself and of its BINFO_BASETYPES. */
85b71cf2 6658
5566b478
MS
6659 t = copy_node (t);
6660
c7449f2b
JM
6661 /* Make sure type isn't a typedef copy. */
6662 type = BINFO_TYPE (TYPE_BINFO (type));
6663
5566b478 6664 TREE_TYPE (t) = complete_type (type);
6633d636
MS
6665 if (IS_AGGR_TYPE (type))
6666 {
6667 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
6668 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
6669 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
6670 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
6671 }
5566b478
MS
6672 return t;
6673 }
85b71cf2
JM
6674
6675 /* Otherwise, a vector of template arguments. */
4393e105 6676 return tsubst_template_arg_vector (t, args, complain);
8d08fdba 6677
8d08fdba
MS
6678 case POINTER_TYPE:
6679 case REFERENCE_TYPE:
6680 {
8d08fdba 6681 enum tree_code code;
79a7c7fa 6682
46c895ac 6683 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
8d08fdba
MS
6684 return t;
6685
6686 code = TREE_CODE (t);
4393e105
MM
6687
6688
6689 /* [temp.deduct]
6690
6691 Type deduction may fail for any of the following
6692 reasons:
6693
6694 -- Attempting to create a pointer to reference type.
6695 -- Attempting to create a reference to a reference type or
6696 a reference to void. */
0ecfe0b4
JM
6697 if (TREE_CODE (type) == REFERENCE_TYPE
6698 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
79a7c7fa
JM
6699 {
6700 static int last_line = 0;
3b304f5b 6701 static const char* last_file = 0;
79a7c7fa
JM
6702
6703 /* We keep track of the last time we issued this error
6704 message to avoid spewing a ton of messages during a
6705 single bad template instantiation. */
c2ea3a40
NS
6706 if (complain & tf_error
6707 && (last_line != lineno || last_file != input_filename))
79a7c7fa 6708 {
0ecfe0b4 6709 if (TREE_CODE (type) == VOID_TYPE)
33bd39a2 6710 error ("forming reference to void");
0ecfe0b4 6711 else
33bd39a2 6712 error ("forming %s to reference type `%T'",
0ecfe0b4
JM
6713 (code == POINTER_TYPE) ? "pointer" : "reference",
6714 type);
79a7c7fa
JM
6715 last_line = lineno;
6716 last_file = input_filename;
6717 }
6718
4393e105 6719 return error_mark_node;
79a7c7fa
JM
6720 }
6721 else if (code == POINTER_TYPE)
46c895ac
NS
6722 {
6723 r = build_pointer_type (type);
6724 if (TREE_CODE (type) == METHOD_TYPE)
6725 r = build_ptrmemfunc_type (r);
6726 }
8d08fdba
MS
6727 else
6728 r = build_reference_type (type);
adecb3f4 6729 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
79a7c7fa 6730
a5f1c5f8
NS
6731 if (r != error_mark_node)
6732 /* Will this ever be needed for TYPE_..._TO values? */
6733 layout_type (r);
6734
8d08fdba
MS
6735 return r;
6736 }
a4443a08 6737 case OFFSET_TYPE:
0ecfe0b4 6738 {
4393e105
MM
6739 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
6740 if (r == error_mark_node || !IS_AGGR_TYPE (r))
6741 {
6742 /* [temp.deduct]
6743
6744 Type deduction may fail for any of the following
6745 reasons:
6746
6747 -- Attempting to create "pointer to member of T" when T
6748 is not a class type. */
c2ea3a40
NS
6749 if (complain & tf_error)
6750 error ("creating pointer to member of non-class type `%T'", r);
4393e105
MM
6751 return error_mark_node;
6752 }
46c895ac
NS
6753 if (TREE_CODE (type) == REFERENCE_TYPE)
6754 {
6755 if (complain)
6756 error ("creating pointer to member reference type `%T'", type);
6757
6758 return error_mark_node;
6759 }
6760 my_friendly_assert (TREE_CODE (type) != METHOD_TYPE, 20011231);
6761 if (TREE_CODE (type) == FUNCTION_TYPE)
6762 /* This is really a method type. The cv qualifiers of the
6763 this pointer should _not_ be determined by the cv
6764 qualifiers of the class type. They should be held
6765 somewhere in the FUNCTION_TYPE, but we don't do that at
6766 the moment. Consider
6767 typedef void (Func) () const;
6768
6769 template <typename T1> void Foo (Func T1::*);
6770
6771 */
6772 return build_cplus_method_type (TYPE_MAIN_VARIANT (r),
6773 TREE_TYPE (type),
6774 TYPE_ARG_TYPES (type));
6775 else
6776 return build_offset_type (r, type);
0ecfe0b4 6777 }
8d08fdba
MS
6778 case FUNCTION_TYPE:
6779 case METHOD_TYPE:
6780 {
c11b6f21 6781 tree fntype;
4393e105 6782 tree raises;
8d08fdba 6783
4393e105
MM
6784 fntype = tsubst_function_type (t, args, complain, in_decl);
6785 if (fntype == error_mark_node)
6786 return error_mark_node;
cabc336a 6787
c6002625 6788 /* Substitue the exception specification. */
cabc336a 6789 raises = TYPE_RAISES_EXCEPTIONS (t);
c11b6f21
MS
6790 if (raises)
6791 {
4cc1d462
NS
6792 tree list = NULL_TREE;
6793
6794 if (! TREE_VALUE (raises))
6795 list = raises;
6796 else
6797 for (; raises != NULL_TREE; raises = TREE_CHAIN (raises))
6798 {
6799 tree spec = TREE_VALUE (raises);
6800
6801 spec = tsubst (spec, args, complain, in_decl);
6802 if (spec == error_mark_node)
6803 return spec;
6804 list = add_exception_specifier (list, spec, complain);
6805 }
6806 fntype = build_exception_variant (fntype, list);
c11b6f21
MS
6807 }
6808 return fntype;
8d08fdba
MS
6809 }
6810 case ARRAY_TYPE:
6811 {
4393e105
MM
6812 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
6813 if (domain == error_mark_node)
6814 return error_mark_node;
6815
6816 /* As an optimization, we avoid regenerating the array type if
6817 it will obviously be the same as T. */
8d08fdba
MS
6818 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6819 return t;
0ecfe0b4 6820
4393e105
MM
6821 /* These checks should match the ones in grokdeclarator.
6822
6823 [temp.deduct]
6824
6825 The deduction may fail for any of the following reasons:
6826
6827 -- Attempting to create an array with an element type that
6828 is void, a function type, or a reference type. */
6829 if (TREE_CODE (type) == VOID_TYPE
6830 || TREE_CODE (type) == FUNCTION_TYPE
6831 || TREE_CODE (type) == REFERENCE_TYPE)
0ecfe0b4 6832 {
c2ea3a40 6833 if (complain & tf_error)
33bd39a2 6834 error ("creating array of `%T'", type);
4393e105 6835 return error_mark_node;
0ecfe0b4
JM
6836 }
6837
8d08fdba
MS
6838 r = build_cplus_array_type (type, domain);
6839 return r;
6840 }
6841
8d08fdba 6842 case PLUS_EXPR:
5566b478 6843 case MINUS_EXPR:
4393e105 6844 {
c2ea3a40
NS
6845 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6846 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
4393e105
MM
6847
6848 if (e1 == error_mark_node || e2 == error_mark_node)
6849 return error_mark_node;
6850
6851 return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2));
6852 }
8d08fdba
MS
6853
6854 case NEGATE_EXPR:
6855 case NOP_EXPR:
4393e105 6856 {
c2ea3a40 6857 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
4393e105
MM
6858 if (e == error_mark_node)
6859 return error_mark_node;
6860
6861 return fold (build (TREE_CODE (t), TREE_TYPE (t), e));
6862 }
8d08fdba 6863
5566b478
MS
6864 case TYPENAME_TYPE:
6865 {
4393e105
MM
6866 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
6867 in_decl, /*entering_scope=*/1);
6868 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
6869 complain, in_decl);
6870
6871 if (ctx == error_mark_node || f == error_mark_node)
6872 return error_mark_node;
ae58fa02 6873
bf8f3f93
MM
6874 if (!IS_AGGR_TYPE (ctx))
6875 {
c2ea3a40 6876 if (complain & tf_error)
33bd39a2 6877 error ("`%T' is not a class, struct, or union type",
bf8f3f93
MM
6878 ctx);
6879 return error_mark_node;
6880 }
6881 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
6882 {
6883 /* Normally, make_typename_type does not require that the CTX
6884 have complete type in order to allow things like:
ae58fa02 6885
bf8f3f93 6886 template <class T> struct S { typename S<T>::X Y; };
ae58fa02 6887
bf8f3f93
MM
6888 But, such constructs have already been resolved by this
6889 point, so here CTX really should have complete type, unless
6890 it's a partial instantiation. */
4393e105 6891 ctx = complete_type (ctx);
d0f062fb 6892 if (!COMPLETE_TYPE_P (ctx))
4393e105 6893 {
c2ea3a40 6894 if (complain & tf_error)
7a228918 6895 cxx_incomplete_type_error (NULL_TREE, ctx);
4393e105
MM
6896 return error_mark_node;
6897 }
6898 }
ae58fa02 6899
4f2b0fb2
NS
6900 f = make_typename_type (ctx, f,
6901 (complain & tf_error) | tf_keep_type_decl);
f0bcd168
MM
6902 if (f == error_mark_node)
6903 return f;
4f2b0fb2
NS
6904 if (TREE_CODE (f) == TYPE_DECL)
6905 {
6906 complain |= tf_ignore_bad_quals;
6907 f = TREE_TYPE (f);
6908 }
6909
6910 return cp_build_qualified_type_real
6911 (f, cp_type_quals (f) | cp_type_quals (t), complain);
5566b478 6912 }
c2ea3a40 6913
b8c6534b
KL
6914 case UNBOUND_CLASS_TEMPLATE:
6915 {
6916 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
6917 in_decl, /*entering_scope=*/1);
6918 tree name = TYPE_IDENTIFIER (t);
6919
6920 if (ctx == error_mark_node || name == error_mark_node)
6921 return error_mark_node;
6922
6923 return make_unbound_class_template (ctx, name, complain);
6924 }
6925
5566b478 6926 case INDIRECT_REF:
4393e105 6927 {
c2ea3a40 6928 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
4393e105
MM
6929 if (e == error_mark_node)
6930 return error_mark_node;
6931 return make_pointer_declarator (type, e);
6932 }
6933
5566b478 6934 case ADDR_EXPR:
4393e105 6935 {
c2ea3a40 6936 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
4393e105
MM
6937 if (e == error_mark_node)
6938 return error_mark_node;
6939 return make_reference_declarator (type, e);
6940 }
5566b478
MS
6941
6942 case ARRAY_REF:
4393e105 6943 {
c2ea3a40
NS
6944 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6945 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
4393e105
MM
6946 if (e1 == error_mark_node || e2 == error_mark_node)
6947 return error_mark_node;
6948
718b8ea5 6949 return build_nt (ARRAY_REF, e1, e2, tsubst_expr);
4393e105 6950 }
5566b478
MS
6951
6952 case CALL_EXPR:
4393e105 6953 {
c2ea3a40 6954 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
43f887f9
MM
6955 tree e2 = (tsubst_call_declarator_parms
6956 (CALL_DECLARATOR_PARMS (t), args, complain, in_decl));
6957 tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args,
6958 complain, in_decl);
4393e105
MM
6959
6960 if (e1 == error_mark_node || e2 == error_mark_node
6961 || e3 == error_mark_node)
6962 return error_mark_node;
6963
43f887f9 6964 return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3);
4393e105 6965 }
5566b478 6966
fc378698 6967 case SCOPE_REF:
4393e105 6968 {
c2ea3a40 6969 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
4393e105
MM
6970 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
6971 if (e1 == error_mark_node || e2 == error_mark_node)
6972 return error_mark_node;
6973
718b8ea5 6974 return build_nt (TREE_CODE (t), e1, e2);
4393e105 6975 }
fc378698 6976
b894fc05 6977 case TYPEOF_TYPE:
4393e105 6978 {
c2ea3a40 6979 tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, in_decl);
4393e105
MM
6980 if (e1 == error_mark_node)
6981 return error_mark_node;
6982
6983 return TREE_TYPE (e1);
6984 }
b894fc05 6985
8d08fdba 6986 default:
5566b478 6987 sorry ("use of `%s' in template",
8d08fdba
MS
6988 tree_code_name [(int) TREE_CODE (t)]);
6989 return error_mark_node;
6990 }
6991}
6992
00d3396f
JM
6993/* Like tsubst, but deals with expressions. This function just replaces
6994 template parms; to finish processing the resultant expression, use
6995 tsubst_expr. */
6996
5566b478 6997tree
4393e105 6998tsubst_copy (t, args, complain, in_decl)
98c1c668 6999 tree t, args;
c2ea3a40 7000 tsubst_flags_t complain;
5566b478
MS
7001 tree in_decl;
7002{
7003 enum tree_code code;
8452b1d3 7004 tree r;
8d08fdba 7005
5566b478
MS
7006 if (t == NULL_TREE || t == error_mark_node)
7007 return t;
7008
7009 code = TREE_CODE (t);
b7484fbe 7010
5566b478
MS
7011 switch (code)
7012 {
7013 case PARM_DECL:
a759e627 7014 return do_identifier (DECL_NAME (t), 0, NULL_TREE);
5566b478
MS
7015
7016 case CONST_DECL:
ed44da02
MM
7017 {
7018 tree enum_type;
7019 tree v;
7020
7021 if (!DECL_CONTEXT (t))
7022 /* This is a global enumeration constant. */
7023 return t;
7024
7025 /* Unfortunately, we cannot just call lookup_name here.
9188c363
MM
7026 Consider:
7027
7028 template <int I> int f() {
7029 enum E { a = I };
7030 struct S { void g() { E e = a; } };
7031 };
7032
7033 When we instantiate f<7>::S::g(), say, lookup_name is not
7034 clever enough to find f<7>::a. */
ed44da02 7035 enum_type
4393e105 7036 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
ed44da02
MM
7037 /*entering_scope=*/0);
7038
7039 for (v = TYPE_VALUES (enum_type);
7040 v != NULL_TREE;
7041 v = TREE_CHAIN (v))
7042 if (TREE_PURPOSE (v) == DECL_NAME (t))
7043 return TREE_VALUE (v);
7044
7045 /* We didn't find the name. That should never happen; if
7046 name-lookup found it during preliminary parsing, we
7047 should find it again here during instantiation. */
a98facb0 7048 abort ();
ed44da02 7049 }
db04386f 7050 return t;
ed44da02 7051
5566b478
MS
7052 case FIELD_DECL:
7053 if (DECL_CONTEXT (t))
7054 {
0978790f 7055 tree ctx;
0978790f 7056
4393e105 7057 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
36a117a5 7058 /*entering_scope=*/1);
0978790f 7059 if (ctx != DECL_CONTEXT (t))
5566b478
MS
7060 return lookup_field (ctx, DECL_NAME (t), 0, 0);
7061 }
7062 return t;
7063
7064 case VAR_DECL:
7065 case FUNCTION_DECL:
7066 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
4393e105 7067 t = tsubst (t, args, complain, in_decl);
5566b478
MS
7068 mark_used (t);
7069 return t;
7070
98c1c668
JM
7071 case TEMPLATE_DECL:
7072 if (is_member_template (t))
4393e105 7073 return tsubst (t, args, complain, in_decl);
98c1c668
JM
7074 else
7075 return t;
7076
672476cb
MM
7077 case LOOKUP_EXPR:
7078 {
9da99f7d 7079 /* We must tsubst into a LOOKUP_EXPR in case the names to
672476cb
MM
7080 which it refers is a conversion operator; in that case the
7081 name will change. We avoid making unnecessary copies,
7082 however. */
7083
4393e105 7084 tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
672476cb
MM
7085
7086 if (id != TREE_OPERAND (t, 0))
7087 {
8452b1d3 7088 r = build_nt (LOOKUP_EXPR, id);
672476cb
MM
7089 LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
7090 t = r;
7091 }
7092
7093 return t;
7094 }
7095
5566b478
MS
7096 case CAST_EXPR:
7097 case REINTERPRET_CAST_EXPR:
e92cc029
MS
7098 case CONST_CAST_EXPR:
7099 case STATIC_CAST_EXPR:
7100 case DYNAMIC_CAST_EXPR:
51924768 7101 case NOP_EXPR:
5566b478 7102 return build1
4393e105
MM
7103 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7104 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
5566b478
MS
7105
7106 case INDIRECT_REF:
5566b478
MS
7107 case NEGATE_EXPR:
7108 case TRUTH_NOT_EXPR:
b87692e5 7109 case BIT_NOT_EXPR:
5566b478
MS
7110 case ADDR_EXPR:
7111 case CONVERT_EXPR: /* Unary + */
7112 case SIZEOF_EXPR:
abff8e06 7113 case ALIGNOF_EXPR:
5566b478 7114 case ARROW_EXPR:
fc378698 7115 case THROW_EXPR:
5156628f 7116 case TYPEID_EXPR:
f5733617
SS
7117 case REALPART_EXPR:
7118 case IMAGPART_EXPR:
5566b478 7119 return build1
6a629cac 7120 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
4393e105 7121 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
5566b478
MS
7122
7123 case PLUS_EXPR:
7124 case MINUS_EXPR:
7125 case MULT_EXPR:
7126 case TRUNC_DIV_EXPR:
7127 case CEIL_DIV_EXPR:
7128 case FLOOR_DIV_EXPR:
7129 case ROUND_DIV_EXPR:
7130 case EXACT_DIV_EXPR:
7131 case BIT_AND_EXPR:
7132 case BIT_ANDTC_EXPR:
7133 case BIT_IOR_EXPR:
7134 case BIT_XOR_EXPR:
7135 case TRUNC_MOD_EXPR:
7136 case FLOOR_MOD_EXPR:
7137 case TRUTH_ANDIF_EXPR:
7138 case TRUTH_ORIF_EXPR:
7139 case TRUTH_AND_EXPR:
7140 case TRUTH_OR_EXPR:
7141 case RSHIFT_EXPR:
7142 case LSHIFT_EXPR:
7143 case RROTATE_EXPR:
7144 case LROTATE_EXPR:
7145 case EQ_EXPR:
7146 case NE_EXPR:
7147 case MAX_EXPR:
7148 case MIN_EXPR:
7149 case LE_EXPR:
7150 case GE_EXPR:
7151 case LT_EXPR:
7152 case GT_EXPR:
7153 case COMPONENT_REF:
7154 case ARRAY_REF:
7155 case COMPOUND_EXPR:
7156 case SCOPE_REF:
7157 case DOTSTAR_EXPR:
7158 case MEMBER_REF:
519c9806
MM
7159 case PREDECREMENT_EXPR:
7160 case PREINCREMENT_EXPR:
7161 case POSTDECREMENT_EXPR:
7162 case POSTINCREMENT_EXPR:
5566b478 7163 return build_nt
4393e105
MM
7164 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7165 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
5566b478
MS
7166
7167 case CALL_EXPR:
7168 {
7169 tree fn = TREE_OPERAND (t, 0);
f84b4be9 7170 if (is_overloaded_fn (fn))
4393e105 7171 fn = tsubst_copy (get_first_fn (fn), args, complain, in_decl);
5566b478 7172 else
f84b4be9 7173 /* Sometimes FN is a LOOKUP_EXPR. */
4393e105 7174 fn = tsubst_copy (fn, args, complain, in_decl);
5566b478 7175 return build_nt
4393e105
MM
7176 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7177 in_decl),
5566b478
MS
7178 NULL_TREE);
7179 }
7180
7181 case METHOD_CALL_EXPR:
7182 {
7183 tree name = TREE_OPERAND (t, 0);
7184 if (TREE_CODE (name) == BIT_NOT_EXPR)
7185 {
4393e105
MM
7186 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7187 complain, in_decl);
7bae46f4 7188 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
5566b478
MS
7189 }
7190 else if (TREE_CODE (name) == SCOPE_REF
7191 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7192 {
4393e105
MM
7193 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7194 complain, in_decl);
5566b478 7195 name = TREE_OPERAND (name, 1);
4393e105
MM
7196 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7197 complain, in_decl);
11686454 7198 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
5566b478
MS
7199 name = build_nt (SCOPE_REF, base, name);
7200 }
7201 else
4393e105 7202 name = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
5566b478 7203 return build_nt
4393e105
MM
7204 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args,
7205 complain, in_decl),
7206 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl),
5566b478
MS
7207 NULL_TREE);
7208 }
7209
558475f0
MM
7210 case STMT_EXPR:
7211 /* This processing should really occur in tsubst_expr, However,
7212 tsubst_expr does not recurse into expressions, since it
7213 assumes that there aren't any statements inside them.
7214 Instead, it simply calls build_expr_from_tree. So, we need
7215 to expand the STMT_EXPR here. */
7216 if (!processing_template_decl)
7217 {
b5ac18ea 7218 tree stmt_expr = begin_stmt_expr ();
c2ea3a40 7219 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
b5ac18ea 7220 return finish_stmt_expr (stmt_expr);
558475f0 7221 }
9d9169de
MM
7222
7223 return t;
558475f0 7224
5566b478
MS
7225 case COND_EXPR:
7226 case MODOP_EXPR:
40242ccf 7227 case PSEUDO_DTOR_EXPR:
67da3287 7228 {
8452b1d3 7229 r = build_nt
4393e105
MM
7230 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7231 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7232 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
67da3287
MM
7233 return r;
7234 }
5566b478
MS
7235
7236 case NEW_EXPR:
7237 {
8452b1d3 7238 r = build_nt
4393e105
MM
7239 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7240 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7241 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
5566b478
MS
7242 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7243 return r;
7244 }
7245
7246 case DELETE_EXPR:
7247 {
8452b1d3 7248 r = build_nt
4393e105
MM
7249 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7250 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
5566b478
MS
7251 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7252 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7253 return r;
7254 }
7255
386b8a85
JM
7256 case TEMPLATE_ID_EXPR:
7257 {
00d3396f 7258 /* Substituted template arguments */
4393e105
MM
7259 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7260 in_decl);
856216bb
MM
7261
7262 if (targs && TREE_CODE (targs) == TREE_LIST)
7263 {
7264 tree chain;
7265 for (chain = targs; chain; chain = TREE_CHAIN (chain))
7266 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
7267 }
7268 else if (targs)
7269 {
7270 int i;
7271 for (i = 0; i < TREE_VEC_LENGTH (targs); ++i)
7272 TREE_VEC_ELT (targs, i)
7273 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i));
7274 }
00d3396f
JM
7275
7276 return lookup_template_function
4393e105 7277 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs);
386b8a85
JM
7278 }
7279
5566b478
MS
7280 case TREE_LIST:
7281 {
7282 tree purpose, value, chain;
7283
7284 if (t == void_list_node)
7285 return t;
7286
7287 purpose = TREE_PURPOSE (t);
7288 if (purpose)
4393e105 7289 purpose = tsubst_copy (purpose, args, complain, in_decl);
5566b478
MS
7290 value = TREE_VALUE (t);
7291 if (value)
4393e105 7292 value = tsubst_copy (value, args, complain, in_decl);
5566b478
MS
7293 chain = TREE_CHAIN (t);
7294 if (chain && chain != void_type_node)
4393e105 7295 chain = tsubst_copy (chain, args, complain, in_decl);
5566b478
MS
7296 if (purpose == TREE_PURPOSE (t)
7297 && value == TREE_VALUE (t)
7298 && chain == TREE_CHAIN (t))
7299 return t;
7300 return tree_cons (purpose, value, chain);
7301 }
7302
7303 case RECORD_TYPE:
7304 case UNION_TYPE:
7305 case ENUMERAL_TYPE:
7306 case INTEGER_TYPE:
7307 case TEMPLATE_TYPE_PARM:
73b0fce8 7308 case TEMPLATE_TEMPLATE_PARM:
a1281f45 7309 case BOUND_TEMPLATE_TEMPLATE_PARM:
f84b4be9 7310 case TEMPLATE_PARM_INDEX:
5566b478
MS
7311 case POINTER_TYPE:
7312 case REFERENCE_TYPE:
7313 case OFFSET_TYPE:
7314 case FUNCTION_TYPE:
7315 case METHOD_TYPE:
7316 case ARRAY_TYPE:
7317 case TYPENAME_TYPE:
b8c6534b 7318 case UNBOUND_CLASS_TEMPLATE:
2a2b1d56 7319 case TYPEOF_TYPE:
f84b4be9 7320 case TYPE_DECL:
4393e105 7321 return tsubst (t, args, complain, in_decl);
5566b478 7322
e92cc029 7323 case IDENTIFIER_NODE:
421844e7 7324 if (IDENTIFIER_TYPENAME_P (t))
1f6e1acc
AS
7325 {
7326 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
1f84ec23 7327 return mangle_conv_op_name_for_type (new_type);
1f6e1acc 7328 }
e92cc029
MS
7329 else
7330 return t;
7331
5156628f 7332 case CONSTRUCTOR:
8452b1d3
JM
7333 {
7334 r = build
4393e105
MM
7335 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl),
7336 NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args,
7337 complain, in_decl));
8452b1d3
JM
7338 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
7339 return r;
7340 }
5156628f 7341
371534a9 7342 case VA_ARG_EXPR:
ea333e1c 7343 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
c2ea3a40
NS
7344 in_decl),
7345 tsubst (TREE_TYPE (t), args, complain, in_decl));
f9817201 7346
5566b478
MS
7347 default:
7348 return t;
7349 }
7350}
7351
558475f0 7352/* Like tsubst_copy, but also does semantic processing. */
00d3396f 7353
5566b478 7354tree
4393e105 7355tsubst_expr (t, args, complain, in_decl)
98c1c668 7356 tree t, args;
c2ea3a40 7357 tsubst_flags_t complain;
5566b478
MS
7358 tree in_decl;
7359{
fd10dd09 7360 tree stmt, tmp;
558475f0 7361
5566b478
MS
7362 if (t == NULL_TREE || t == error_mark_node)
7363 return t;
7364
5156628f 7365 if (processing_template_decl)
4393e105 7366 return tsubst_copy (t, args, complain, in_decl);
5566b478 7367
fd10dd09
JM
7368 if (!statement_code_p (TREE_CODE (t)))
7369 return build_expr_from_tree (tsubst_copy (t, args, complain, in_decl));
7370
5566b478 7371 switch (TREE_CODE (t))
8d08fdba 7372 {
558475f0 7373 case RETURN_INIT:
a7e4cfa0 7374 prep_stmt (t);
558475f0
MM
7375 finish_named_return_value
7376 (TREE_OPERAND (t, 0),
c2ea3a40 7377 tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl));
558475f0
MM
7378 break;
7379
7380 case CTOR_INITIALIZER:
2282d28d
MM
7381 prep_stmt (t);
7382 finish_mem_initializers (tsubst_initializer_list
7383 (TREE_OPERAND (t, 0), args));
7384 break;
558475f0 7385
5566b478 7386 case RETURN_STMT:
a7e4cfa0 7387 prep_stmt (t);
d65b1d77 7388 finish_return_stmt (tsubst_expr (RETURN_STMT_EXPR (t),
4393e105 7389 args, complain, in_decl));
5566b478
MS
7390 break;
7391
7392 case EXPR_STMT:
a7e4cfa0 7393 prep_stmt (t);
ad321293 7394 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
4393e105 7395 args, complain, in_decl));
5566b478
MS
7396 break;
7397
9da99f7d
NS
7398 case USING_STMT:
7399 prep_stmt (t);
7400 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
7401 args, complain, in_decl));
7402 break;
7403
5566b478
MS
7404 case DECL_STMT:
7405 {
e0942dcd
MM
7406 tree decl;
7407 tree init;
5566b478 7408
a7e4cfa0 7409 prep_stmt (t);
e0942dcd 7410 decl = DECL_STMT_DECL (t);
acef433b
MM
7411 if (TREE_CODE (decl) == LABEL_DECL)
7412 finish_label_decl (DECL_NAME (decl));
fbfe8c9e
NS
7413 else if (TREE_CODE (decl) == USING_DECL)
7414 {
7415 tree scope = DECL_INITIAL (decl);
7416 tree name = DECL_NAME (decl);
7417
7418 scope = tsubst_expr (scope, args, complain, in_decl);
7419 do_local_using_decl (build_nt (SCOPE_REF, scope, name));
7420 }
acef433b
MM
7421 else
7422 {
7423 init = DECL_INITIAL (decl);
7424 decl = tsubst (decl, args, complain, in_decl);
ce2e5191
NS
7425 if (decl != error_mark_node)
7426 {
01f4137f
RH
7427 if (TREE_CODE (decl) != TYPE_DECL)
7428 /* Make sure the type is instantiated now. */
7429 complete_type (TREE_TYPE (decl));
ce2e5191
NS
7430 if (init)
7431 DECL_INITIAL (decl) = error_mark_node;
7432 /* By marking the declaration as instantiated, we avoid
7433 trying to instantiate it. Since instantiate_decl can't
7434 handle local variables, and since we've already done
7435 all that needs to be done, that's the right thing to
7436 do. */
7437 if (TREE_CODE (decl) == VAR_DECL)
7438 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
01f4137f
RH
7439 maybe_push_decl (decl);
7440 if (DECL_PRETTY_FUNCTION_P (decl))
ed2fa432 7441 {
01f4137f
RH
7442 /* For __PRETTY_FUNCTION__ we have to adjust the
7443 initializer. */
7444 const char *const name
7445 = cxx_printable_name (current_function_decl, 2);
7446 init = cp_fname_init (name);
7447 TREE_TYPE (decl) = TREE_TYPE (init);
ed2fa432 7448 }
01f4137f
RH
7449 else
7450 init = tsubst_expr (init, args, complain, in_decl);
7451 cp_finish_decl (decl, init, NULL_TREE, 0);
ce2e5191 7452 }
acef433b 7453 }
fd10dd09
JM
7454
7455 /* A DECL_STMT can also be used as an expression, in the condition
27631dae 7456 clause of an if/for/while construct. If we aren't followed by
fd10dd09
JM
7457 another statement, return our decl. */
7458 if (TREE_CHAIN (t) == NULL_TREE)
7459 return decl;
5566b478 7460 }
fd10dd09 7461 break;
8d08fdba 7462
5566b478
MS
7463 case FOR_STMT:
7464 {
a7e4cfa0 7465 prep_stmt (t);
5566b478 7466
558475f0 7467 stmt = begin_for_stmt ();
fd10dd09 7468 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
558475f0 7469 finish_for_init_stmt (stmt);
c2ea3a40
NS
7470 finish_for_cond (tsubst_expr (FOR_COND (t),
7471 args, complain, in_decl),
558475f0 7472 stmt);
4393e105 7473 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
558475f0 7474 finish_for_expr (tmp, stmt);
4393e105 7475 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
35b1567d 7476 finish_for_stmt (stmt);
5566b478
MS
7477 }
7478 break;
8d08fdba 7479
5566b478
MS
7480 case WHILE_STMT:
7481 {
a7e4cfa0 7482 prep_stmt (t);
558475f0 7483 stmt = begin_while_stmt ();
ad321293 7484 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
4393e105 7485 args, complain, in_decl),
558475f0 7486 stmt);
4393e105 7487 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
558475f0 7488 finish_while_stmt (stmt);
5566b478
MS
7489 }
7490 break;
8d08fdba 7491
5566b478
MS
7492 case DO_STMT:
7493 {
a7e4cfa0 7494 prep_stmt (t);
558475f0 7495 stmt = begin_do_stmt ();
4393e105 7496 tsubst_expr (DO_BODY (t), args, complain, in_decl);
558475f0 7497 finish_do_body (stmt);
c2ea3a40
NS
7498 finish_do_stmt (tsubst_expr (DO_COND (t),
7499 args, complain, in_decl),
558475f0 7500 stmt);
5566b478
MS
7501 }
7502 break;
a0a33927 7503
5566b478 7504 case IF_STMT:
8d08fdba 7505 {
a7e4cfa0 7506 prep_stmt (t);
558475f0 7507 stmt = begin_if_stmt ();
ad321293 7508 finish_if_stmt_cond (tsubst_expr (IF_COND (t),
4393e105 7509 args, complain, in_decl),
558475f0 7510 stmt);
db5ae43f 7511
ad321293 7512 if (tmp = THEN_CLAUSE (t), tmp)
db5ae43f 7513 {
4393e105 7514 tsubst_expr (tmp, args, complain, in_decl);
558475f0 7515 finish_then_clause (stmt);
db5ae43f
MS
7516 }
7517
ad321293
MM
7518 if (tmp = ELSE_CLAUSE (t), tmp)
7519 {
7520 begin_else_clause ();
4393e105 7521 tsubst_expr (tmp, args, complain, in_decl);
558475f0 7522 finish_else_clause (stmt);
ad321293 7523 }
8d08fdba 7524
ad321293 7525 finish_if_stmt ();
8d08fdba 7526 }
5566b478 7527 break;
8d08fdba 7528
5566b478
MS
7529 case COMPOUND_STMT:
7530 {
a7e4cfa0 7531 prep_stmt (t);
ade3dc07
JM
7532 if (COMPOUND_STMT_BODY_BLOCK (t))
7533 stmt = begin_function_body ();
7534 else
7535 stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
7536
fd10dd09 7537 tsubst_expr (COMPOUND_BODY (t), args, complain, in_decl);
ade3dc07
JM
7538
7539 if (COMPOUND_STMT_BODY_BLOCK (t))
7540 finish_function_body (stmt);
7541 else
7542 finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt);
5566b478
MS
7543 }
7544 break;
8d08fdba 7545
5566b478 7546 case BREAK_STMT:
a7e4cfa0 7547 prep_stmt (t);
ad321293 7548 finish_break_stmt ();
5566b478 7549 break;
8d08fdba 7550
6467930b 7551 case CONTINUE_STMT:
a7e4cfa0 7552 prep_stmt (t);
ad321293 7553 finish_continue_stmt ();
6467930b
MS
7554 break;
7555
5566b478
MS
7556 case SWITCH_STMT:
7557 {
558475f0 7558 tree val;
5566b478 7559
a7e4cfa0 7560 prep_stmt (t);
527f0080 7561 stmt = begin_switch_stmt ();
4393e105 7562 val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl);
527f0080 7563 finish_switch_cond (val, stmt);
558475f0 7564 tsubst_expr (SWITCH_BODY (t), args, complain, in_decl);
35b1567d 7565 finish_switch_stmt (stmt);
5566b478
MS
7566 }
7567 break;
7568
7569 case CASE_LABEL:
a7e4cfa0 7570 prep_stmt (t);
4393e105 7571 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
56cb9733
MM
7572 tsubst_expr (CASE_HIGH (t), args, complain,
7573 in_decl));
5566b478
MS
7574 break;
7575
acef433b
MM
7576 case LABEL_STMT:
7577 lineno = STMT_LINENO (t);
7578 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t)));
5566b478
MS
7579 break;
7580
aa16c0fa
KL
7581 case FILE_STMT:
7582 input_filename = FILE_STMT_FILENAME (t);
7583 add_stmt (build_nt (FILE_STMT, FILE_STMT_FILENAME_NODE (t)));
7584 break;
7585
5566b478 7586 case GOTO_STMT:
a7e4cfa0 7587 prep_stmt (t);
fd10dd09
JM
7588 tmp = GOTO_DESTINATION (t);
7589 if (TREE_CODE (tmp) != LABEL_DECL)
aa09da44
MM
7590 /* Computed goto's must be tsubst'd into. On the other hand,
7591 non-computed gotos must not be; the identifier in question
7592 will have no binding. */
fd10dd09 7593 tmp = tsubst_expr (tmp, args, complain, in_decl);
3fa56191 7594 else
fd10dd09
JM
7595 tmp = DECL_NAME (tmp);
7596 finish_goto_stmt (tmp);
ad321293
MM
7597 break;
7598
7599 case ASM_STMT:
a7e4cfa0 7600 prep_stmt (t);
c87978aa
JM
7601 tmp = finish_asm_stmt
7602 (ASM_CV_QUAL (t),
7603 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
7604 tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
7605 tsubst_expr (ASM_INPUTS (t), args, complain, in_decl),
7606 tsubst_expr (ASM_CLOBBERS (t), args, complain, in_decl));
7607 ASM_INPUT_P (tmp) = ASM_INPUT_P (t);
5566b478 7608 break;
faf5394a
MS
7609
7610 case TRY_BLOCK:
a7e4cfa0 7611 prep_stmt (t);
f1dedc31 7612 if (CLEANUP_P (t))
62409b39 7613 {
57b52417 7614 stmt = begin_try_block ();
62409b39
MM
7615 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7616 finish_cleanup_try_block (stmt);
7617 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
7618 complain, in_decl),
7619 stmt);
7620 }
f1dedc31
MM
7621 else
7622 {
62409b39
MM
7623 if (FN_TRY_BLOCK_P (t))
7624 stmt = begin_function_try_block ();
7625 else
7626 stmt = begin_try_block ();
7627
7628 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7629
7630 if (FN_TRY_BLOCK_P (t))
7631 finish_function_try_block (stmt);
7632 else
7633 finish_try_block (stmt);
7634
fd10dd09 7635 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
84df082b
MM
7636 if (FN_TRY_BLOCK_P (t))
7637 finish_function_handler_sequence (stmt);
7638 else
7639 finish_handler_sequence (stmt);
f1dedc31 7640 }
faf5394a 7641 break;
b35d4555 7642
faf5394a 7643 case HANDLER:
b35d4555
MM
7644 {
7645 tree decl;
b35d4555
MM
7646
7647 prep_stmt (t);
7648 stmt = begin_handler ();
7649 if (HANDLER_PARMS (t))
7650 {
7651 decl = DECL_STMT_DECL (HANDLER_PARMS (t));
7652 decl = tsubst (decl, args, complain, in_decl);
f8191e64
MM
7653 /* Prevent instantiate_decl from trying to instantiate
7654 this variable. We've already done all that needs to be
7655 done. */
7656 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
b35d4555
MM
7657 }
7658 else
7659 decl = NULL_TREE;
1a6025b4 7660 finish_handler_parms (decl, stmt);
b35d4555 7661 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
1a6025b4 7662 finish_handler (stmt);
b35d4555 7663 }
faf5394a
MS
7664 break;
7665
b87692e5 7666 case TAG_DEFN:
a7e4cfa0 7667 prep_stmt (t);
fd10dd09 7668 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
b87692e5
MS
7669 break;
7670
5566b478 7671 default:
fd10dd09 7672 abort ();
5566b478 7673 }
fd10dd09
JM
7674
7675 return tsubst_expr (TREE_CHAIN (t), args, complain, in_decl);
8d08fdba
MS
7676}
7677
6ba89f8e 7678/* Instantiate the indicated variable or function template TMPL with
36a117a5
MM
7679 the template arguments in TARG_PTR. */
7680
5566b478
MS
7681tree
7682instantiate_template (tmpl, targ_ptr)
98c1c668 7683 tree tmpl, targ_ptr;
8d08fdba 7684{
5566b478 7685 tree fndecl;
36a117a5
MM
7686 tree gen_tmpl;
7687 tree spec;
5566b478 7688 int i, len;
36a117a5 7689 tree inner_args;
5566b478 7690
27fafc8d
JM
7691 if (tmpl == error_mark_node)
7692 return error_mark_node;
7693
386b8a85
JM
7694 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
7695
db9b2174
MM
7696 /* If this function is a clone, handle it specially. */
7697 if (DECL_CLONED_FUNCTION_P (tmpl))
3ad97789
NS
7698 {
7699 tree spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr);
7700 tree clone;
7701
c6002625 7702 /* Look for the clone. */
3ad97789
NS
7703 for (clone = TREE_CHAIN (spec);
7704 clone && DECL_CLONED_FUNCTION_P (clone);
7705 clone = TREE_CHAIN (clone))
7706 if (DECL_NAME (clone) == DECL_NAME (tmpl))
7707 return clone;
7708 /* We should always have found the clone by now. */
a98facb0 7709 abort ();
3ad97789
NS
7710 return NULL_TREE;
7711 }
7712
36a117a5
MM
7713 /* Check to see if we already have this specialization. */
7714 spec = retrieve_specialization (tmpl, targ_ptr);
7715 if (spec != NULL_TREE)
7716 return spec;
7717
f9a7ae04
MM
7718 gen_tmpl = most_general_template (tmpl);
7719 if (tmpl != gen_tmpl)
386b8a85 7720 {
36a117a5
MM
7721 /* The TMPL is a partial instantiation. To get a full set of
7722 arguments we must add the arguments used to perform the
7723 partial instantiation. */
7724 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
7725 targ_ptr);
36a117a5
MM
7726
7727 /* Check to see if we already have this specialization. */
7728 spec = retrieve_specialization (gen_tmpl, targ_ptr);
75650646
MM
7729 if (spec != NULL_TREE)
7730 return spec;
386b8a85
JM
7731 }
7732
36a117a5 7733 len = DECL_NTPARMS (gen_tmpl);
f9a7ae04 7734 inner_args = INNERMOST_TEMPLATE_ARGS (targ_ptr);
5566b478
MS
7735 i = len;
7736 while (i--)
8d08fdba 7737 {
36a117a5 7738 tree t = TREE_VEC_ELT (inner_args, i);
2f939d94 7739 if (TYPE_P (t))
5566b478
MS
7740 {
7741 tree nt = target_type (t);
ec255269 7742 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
5566b478 7743 {
33bd39a2
ZW
7744 error ("type `%T' composed from a local class is not a valid template-argument", t);
7745 error (" trying to instantiate `%D'", gen_tmpl);
454fa7a7 7746 return error_mark_node;
5566b478
MS
7747 }
7748 }
8d08fdba
MS
7749 }
7750
5566b478 7751 /* substitute template parameters */
17aec3eb 7752 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
c2ea3a40 7753 targ_ptr, tf_error, gen_tmpl);
36a117a5
MM
7754 /* The DECL_TI_TEMPLATE should always be the immediate parent
7755 template, not the most general template. */
7756 DECL_TI_TEMPLATE (fndecl) = tmpl;
8d08fdba 7757
824b9a4c
MS
7758 if (flag_external_templates)
7759 add_pending_template (fndecl);
7760
94350948 7761 /* If we've just instantiated the main entry point for a function,
3ad97789
NS
7762 instantiate all the alternate entry points as well. We do this
7763 by cloning the instantiation of the main entry point, not by
7764 instantiating the template clones. */
7765 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
7766 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
94350948 7767
5566b478 7768 return fndecl;
8d08fdba 7769}
5566b478 7770
4393e105
MM
7771/* The FN is a TEMPLATE_DECL for a function. The ARGS are the
7772 arguments that are being used when calling it. TARGS is a vector
7773 into which the deduced template arguments are placed.
8d08fdba
MS
7774
7775 Return zero for success, 2 for an incomplete match that doesn't resolve
7776 all the types, and 1 for complete failure. An error message will be
7777 printed only for an incomplete match.
7778
e5214479
JM
7779 If FN is a conversion operator, or we are trying to produce a specific
7780 specialization, RETURN_TYPE is the return type desired.
9f54c803
MM
7781
7782 The EXPLICIT_TARGS are explicit template arguments provided via a
7783 template-id.
6467930b 7784
830bfa74
MM
7785 The parameter STRICT is one of:
7786
7787 DEDUCE_CALL:
7788 We are deducing arguments for a function call, as in
7789 [temp.deduct.call].
7790
7791 DEDUCE_CONV:
7792 We are deducing arguments for a conversion function, as in
7793 [temp.deduct.conv].
7794
7795 DEDUCE_EXACT:
62e4a758
NS
7796 We are deducing arguments when doing an explicit instantiation
7797 as in [temp.explicit], when determining an explicit specialization
7798 as in [temp.expl.spec], or when taking the address of a function
7799 template, as in [temp.deduct.funcaddr].
7800
7801 DEDUCE_ORDER:
830bfa74
MM
7802 We are deducing arguments when calculating the partial
7803 ordering between specializations of function or class
62e4a758 7804 templates, as in [temp.func.order] and [temp.class.order].
4393e105 7805
e5214479
JM
7806 LEN is the number of parms to consider before returning success, or -1
7807 for all. This is used in partial ordering to avoid comparing parms for
7808 which no actual argument was passed, since they are not considered in
7809 overload resolution (and are explicitly excluded from consideration in
7810 partial ordering in [temp.func.order]/6). */
8d08fdba
MS
7811
7812int
4393e105 7813fn_type_unification (fn, explicit_targs, targs, args, return_type,
e5214479 7814 strict, len)
4393e105 7815 tree fn, explicit_targs, targs, args, return_type;
830bfa74 7816 unification_kind_t strict;
e5214479 7817 int len;
386b8a85 7818{
4393e105
MM
7819 tree parms;
7820 tree fntype;
adecb3f4 7821 int result;
386b8a85 7822
4393e105
MM
7823 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
7824
7825 fntype = TREE_TYPE (fn);
7826 if (explicit_targs)
75650646 7827 {
4393e105
MM
7828 /* [temp.deduct]
7829
7830 The specified template arguments must match the template
7831 parameters in kind (i.e., type, nontype, template), and there
7832 must not be more arguments than there are parameters;
7833 otherwise type deduction fails.
7834
7835 Nontype arguments must match the types of the corresponding
7836 nontype template parameters, or must be convertible to the
7837 types of the corresponding nontype parameters as specified in
7838 _temp.arg.nontype_, otherwise type deduction fails.
7839
7840 All references in the function type of the function template
7841 to the corresponding template parameters are replaced by the
7842 specified template argument values. If a substitution in a
7843 template parameter or in the function type of the function
7844 template results in an invalid type, type deduction fails. */
7845 int i;
7846 tree converted_args;
75650646 7847
4393e105
MM
7848 converted_args
7849 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
c2ea3a40 7850 explicit_targs, NULL_TREE, tf_none,
4393e105
MM
7851 /*require_all_arguments=*/0));
7852 if (converted_args == error_mark_node)
75650646 7853 return 1;
386b8a85 7854
c2ea3a40 7855 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
4393e105
MM
7856 if (fntype == error_mark_node)
7857 return 1;
050367a3 7858
4393e105
MM
7859 /* Place the explicitly specified arguments in TARGS. */
7860 for (i = 0; i < TREE_VEC_LENGTH (targs); i++)
7861 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
75650646 7862 }
4393e105
MM
7863
7864 parms = TYPE_ARG_TYPES (fntype);
e5214479
JM
7865 /* Never do unification on the 'this' parameter. */
7866 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
7867 parms = TREE_CHAIN (parms);
8d3631f8
NS
7868
7869 if (return_type)
7870 {
7871 /* We've been given a return type to match, prepend it. */
7872 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
7873 args = tree_cons (NULL_TREE, return_type, args);
16e42e7c
JM
7874 if (len >= 0)
7875 ++len;
4393e105
MM
7876 }
7877
4393e105
MM
7878 /* We allow incomplete unification without an error message here
7879 because the standard doesn't seem to explicitly prohibit it. Our
7880 callers must be ready to deal with unification failures in any
7881 event. */
adecb3f4
MM
7882 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
7883 targs, parms, args, /*subr=*/0,
e5214479 7884 strict, /*allow_incomplete*/1, len);
adecb3f4
MM
7885
7886 if (result == 0)
7887 /* All is well so far. Now, check:
7888
7889 [temp.deduct]
7890
7891 When all template arguments have been deduced, all uses of
7892 template parameters in nondeduced contexts are replaced with
7893 the corresponding deduced argument values. If the
7894 substitution results in an invalid type, as described above,
7895 type deduction fails. */
c2ea3a40 7896 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
adecb3f4
MM
7897 == error_mark_node)
7898 return 1;
7899
7900 return result;
830bfa74
MM
7901}
7902
7903/* Adjust types before performing type deduction, as described in
7904 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
4c7d0dff
MM
7905 sections are symmetric. PARM is the type of a function parameter
7906 or the return type of the conversion function. ARG is the type of
7907 the argument passed to the call, or the type of the value
dc957d14 7908 initialized with the result of the conversion function. */
386b8a85 7909
62e4a758 7910static int
830bfa74
MM
7911maybe_adjust_types_for_deduction (strict, parm, arg)
7912 unification_kind_t strict;
7913 tree* parm;
7914 tree* arg;
7915{
62e4a758
NS
7916 int result = 0;
7917
830bfa74
MM
7918 switch (strict)
7919 {
7920 case DEDUCE_CALL:
7921 break;
7922
7923 case DEDUCE_CONV:
7924 {
4c7d0dff
MM
7925 /* Swap PARM and ARG throughout the remainder of this
7926 function; the handling is precisely symmetric since PARM
7927 will initialize ARG rather than vice versa. */
830bfa74
MM
7928 tree* temp = parm;
7929 parm = arg;
7930 arg = temp;
7931 break;
7932 }
7933
7934 case DEDUCE_EXACT:
7935 /* There is nothing to do in this case. */
62e4a758 7936 return 0;
830bfa74 7937
62e4a758
NS
7938 case DEDUCE_ORDER:
7939 /* DR 214. [temp.func.order] is underspecified, and leads to no
7940 ordering between things like `T *' and `T const &' for `U *'.
7941 The former has T=U and the latter T=U*. The former looks more
7942 specialized and John Spicer considers it well-formed (the EDG
7943 compiler accepts it).
7944
7945 John also confirms that deduction should proceed as in a function
996065b4 7946 call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL.
62e4a758
NS
7947 However, in ordering, ARG can have REFERENCE_TYPE, but no argument
7948 to an actual call can have such a type.
7949
996065b4
NS
7950 If both ARG and PARM are REFERENCE_TYPE, we change neither.
7951 If only ARG is a REFERENCE_TYPE, we look through that and then
7952 proceed as with DEDUCE_CALL (which could further convert it). */
62e4a758
NS
7953 if (TREE_CODE (*arg) == REFERENCE_TYPE)
7954 {
7955 if (TREE_CODE (*parm) == REFERENCE_TYPE)
7956 return 0;
7957 *arg = TREE_TYPE (*arg);
62e4a758
NS
7958 }
7959 break;
830bfa74 7960 default:
a98facb0 7961 abort ();
830bfa74
MM
7962 }
7963
7964 if (TREE_CODE (*parm) != REFERENCE_TYPE)
7965 {
7966 /* [temp.deduct.call]
7967
7968 If P is not a reference type:
7969
7970 --If A is an array type, the pointer type produced by the
7971 array-to-pointer standard conversion (_conv.array_) is
7972 used in place of A for type deduction; otherwise,
7973
7974 --If A is a function type, the pointer type produced by
7975 the function-to-pointer standard conversion
7976 (_conv.func_) is used in place of A for type deduction;
7977 otherwise,
7978
7979 --If A is a cv-qualified type, the top level
7980 cv-qualifiers of A's type are ignored for type
7981 deduction. */
7982 if (TREE_CODE (*arg) == ARRAY_TYPE)
7983 *arg = build_pointer_type (TREE_TYPE (*arg));
d8f8dca1 7984 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
830bfa74
MM
7985 *arg = build_pointer_type (*arg);
7986 else
7987 *arg = TYPE_MAIN_VARIANT (*arg);
7988 }
7989
7990 /* [temp.deduct.call]
7991
7992 If P is a cv-qualified type, the top level cv-qualifiers
7993 of P's type are ignored for type deduction. If P is a
7994 reference type, the type referred to by P is used for
7995 type deduction. */
7996 *parm = TYPE_MAIN_VARIANT (*parm);
7997 if (TREE_CODE (*parm) == REFERENCE_TYPE)
62e4a758
NS
7998 {
7999 *parm = TREE_TYPE (*parm);
8000 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
8001 }
8002 return result;
386b8a85
JM
8003}
8004
e5214479 8005/* Most parms like fn_type_unification.
9f54c803
MM
8006
8007 If SUBR is 1, we're being called recursively (to unify the
8008 arguments of a function or method parameter of a function
8009 template). */
386b8a85 8010
4966381a 8011static int
bd0d5d4a
JM
8012type_unification_real (tparms, targs, xparms, xargs, subr,
8013 strict, allow_incomplete, xlen)
8014 tree tparms, targs, xparms, xargs;
830bfa74
MM
8015 int subr;
8016 unification_kind_t strict;
bd0d5d4a 8017 int allow_incomplete, xlen;
8d08fdba
MS
8018{
8019 tree parm, arg;
8020 int i;
8021 int ntparms = TREE_VEC_LENGTH (tparms);
830bfa74 8022 int sub_strict;
bd0d5d4a
JM
8023 int saw_undeduced = 0;
8024 tree parms, args;
8025 int len;
8d08fdba
MS
8026
8027 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
bd0d5d4a
JM
8028 my_friendly_assert (xparms == NULL_TREE
8029 || TREE_CODE (xparms) == TREE_LIST, 290);
4ba126e4 8030 /* ARGS could be NULL. */
bd0d5d4a
JM
8031 if (xargs)
8032 my_friendly_assert (TREE_CODE (xargs) == TREE_LIST, 291);
8d08fdba
MS
8033 my_friendly_assert (ntparms > 0, 292);
8034
830bfa74
MM
8035 switch (strict)
8036 {
8037 case DEDUCE_CALL:
028d1f20
NS
8038 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
8039 | UNIFY_ALLOW_DERIVED);
830bfa74
MM
8040 break;
8041
8042 case DEDUCE_CONV:
8043 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
8044 break;
8045
8046 case DEDUCE_EXACT:
8047 sub_strict = UNIFY_ALLOW_NONE;
8048 break;
62e4a758
NS
8049
8050 case DEDUCE_ORDER:
8051 sub_strict = UNIFY_ALLOW_NONE;
8052 break;
830bfa74
MM
8053
8054 default:
a98facb0 8055 abort ();
830bfa74
MM
8056 }
8057
bd0d5d4a 8058 if (xlen == 0)
e5214479
JM
8059 return 0;
8060
bd0d5d4a
JM
8061 again:
8062 parms = xparms;
8063 args = xargs;
8064 len = xlen;
8065
8d08fdba
MS
8066 while (parms
8067 && parms != void_list_node
8068 && args
8069 && args != void_list_node)
8070 {
8071 parm = TREE_VALUE (parms);
8072 parms = TREE_CHAIN (parms);
8073 arg = TREE_VALUE (args);
8074 args = TREE_CHAIN (args);
8075
8076 if (arg == error_mark_node)
8077 return 1;
8078 if (arg == unknown_type_node)
34016c81
JM
8079 /* We can't deduce anything from this, but we might get all the
8080 template args from other function args. */
8081 continue;
b7484fbe 8082
03e70705
JM
8083 /* Conversions will be performed on a function argument that
8084 corresponds with a function parameter that contains only
8085 non-deducible template parameters and explicitly specified
8086 template parameters. */
8087 if (! uses_template_parms (parm))
b7484fbe 8088 {
03e70705
JM
8089 tree type;
8090
2f939d94 8091 if (!TYPE_P (arg))
03e70705
JM
8092 type = TREE_TYPE (arg);
8093 else
8094 {
8095 type = arg;
8096 arg = NULL_TREE;
8097 }
8098
62e4a758 8099 if (strict == DEDUCE_EXACT || strict == DEDUCE_ORDER)
03e70705 8100 {
3bfdc719 8101 if (same_type_p (parm, type))
03e70705
JM
8102 continue;
8103 }
03e70705 8104 else
343c89cd
JM
8105 /* It might work; we shouldn't check now, because we might
8106 get into infinite recursion. Overload resolution will
8107 handle it. */
8108 continue;
03e70705 8109
b7484fbe
MS
8110 return 1;
8111 }
8112
2f939d94 8113 if (!TYPE_P (arg))
8d08fdba
MS
8114 {
8115 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
34016c81 8116 if (type_unknown_p (arg))
28cbf42c 8117 {
34016c81
JM
8118 /* [temp.deduct.type] A template-argument can be deduced from
8119 a pointer to function or pointer to member function
8120 argument if the set of overloaded functions does not
8121 contain function templates and at most one of a set of
8122 overloaded functions provides a unique match. */
8123
8124 if (resolve_overloaded_unification
4393e105 8125 (tparms, targs, parm, arg, strict, sub_strict)
34016c81
JM
8126 != 0)
8127 return 1;
8128 continue;
28cbf42c 8129 }
8d08fdba
MS
8130 arg = TREE_TYPE (arg);
8131 }
62e4a758
NS
8132
8133 {
8134 int arg_strict = sub_strict;
8135
8136 if (!subr)
8137 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
4393e105 8138
62e4a758
NS
8139 if (unify (tparms, targs, parm, arg, arg_strict))
8140 return 1;
8141 }
e5214479
JM
8142
8143 /* Are we done with the interesting parms? */
8144 if (--len == 0)
bd0d5d4a 8145 goto done;
8d08fdba
MS
8146 }
8147 /* Fail if we've reached the end of the parm list, and more args
8148 are present, and the parm list isn't variadic. */
8149 if (args && args != void_list_node && parms == void_list_node)
8150 return 1;
8151 /* Fail if parms are left and they don't have default values. */
8152 if (parms
8153 && parms != void_list_node
8154 && TREE_PURPOSE (parms) == NULL_TREE)
8155 return 1;
bd0d5d4a
JM
8156
8157 done:
8d08fdba
MS
8158 if (!subr)
8159 for (i = 0; i < ntparms; i++)
050367a3 8160 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
8d08fdba 8161 {
bd0d5d4a
JM
8162 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8163
8164 /* If this is an undeduced nontype parameter that depends on
8165 a type parameter, try another pass; its type may have been
8166 deduced from a later argument than the one from which
8167 this parameter can be deduced. */
8168 if (TREE_CODE (tparm) == PARM_DECL
8169 && uses_template_parms (TREE_TYPE (tparm))
8170 && !saw_undeduced++)
8171 goto again;
8172
386b8a85 8173 if (!allow_incomplete)
8251199e 8174 error ("incomplete type unification");
8d08fdba
MS
8175 return 2;
8176 }
8177 return 0;
8178}
8179
34016c81
JM
8180/* Subroutine of type_unification_real. Args are like the variables at the
8181 call site. ARG is an overloaded function (or template-id); we try
8182 deducing template args from each of the overloads, and if only one
8183 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
8184
8185static int
8186resolve_overloaded_unification (tparms, targs, parm, arg, strict,
4393e105 8187 sub_strict)
34016c81
JM
8188 tree tparms, targs, parm, arg;
8189 unification_kind_t strict;
8190 int sub_strict;
34016c81
JM
8191{
8192 tree tempargs = copy_node (targs);
8193 int good = 0;
8194
8195 if (TREE_CODE (arg) == ADDR_EXPR)
8196 arg = TREE_OPERAND (arg, 0);
9f3d9e46 8197
d8f8dca1
MM
8198 if (TREE_CODE (arg) == COMPONENT_REF)
8199 /* Handle `&x' where `x' is some static or non-static member
8200 function name. */
8201 arg = TREE_OPERAND (arg, 1);
8202
05e0b2f4
JM
8203 if (TREE_CODE (arg) == OFFSET_REF)
8204 arg = TREE_OPERAND (arg, 1);
8205
9f3d9e46 8206 /* Strip baselink information. */
50ad9642
MM
8207 if (BASELINK_P (arg))
8208 arg = BASELINK_FUNCTIONS (arg);
9f3d9e46 8209
34016c81
JM
8210 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
8211 {
8212 /* If we got some explicit template args, we need to plug them into
8213 the affected templates before we try to unify, in case the
8214 explicit args will completely resolve the templates in question. */
8215
8216 tree expl_subargs = TREE_OPERAND (arg, 1);
8217 arg = TREE_OPERAND (arg, 0);
8218
8219 for (; arg; arg = OVL_NEXT (arg))
8220 {
8221 tree fn = OVL_CURRENT (arg);
8222 tree subargs, elem;
8223
8224 if (TREE_CODE (fn) != TEMPLATE_DECL)
8225 continue;
8226
17aec3eb
RK
8227 subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn),
8228 expl_subargs);
34016c81
JM
8229 if (subargs)
8230 {
c2ea3a40 8231 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
d8f8dca1
MM
8232 if (TREE_CODE (elem) == METHOD_TYPE)
8233 elem = build_ptrmemfunc_type (build_pointer_type (elem));
e97e5263 8234 good += try_one_overload (tparms, targs, tempargs, parm, elem,
4393e105 8235 strict, sub_strict);
34016c81
JM
8236 }
8237 }
8238 }
8239 else if (TREE_CODE (arg) == OVERLOAD)
8240 {
8241 for (; arg; arg = OVL_NEXT (arg))
d8f8dca1
MM
8242 {
8243 tree type = TREE_TYPE (OVL_CURRENT (arg));
8244 if (TREE_CODE (type) == METHOD_TYPE)
8245 type = build_ptrmemfunc_type (build_pointer_type (type));
8246 good += try_one_overload (tparms, targs, tempargs, parm,
8247 type,
4393e105 8248 strict, sub_strict);
d8f8dca1 8249 }
34016c81
JM
8250 }
8251 else
a98facb0 8252 abort ();
34016c81
JM
8253
8254 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8255 to function or pointer to member function argument if the set of
8256 overloaded functions does not contain function templates and at most
8257 one of a set of overloaded functions provides a unique match.
8258
8259 So if we found multiple possibilities, we return success but don't
8260 deduce anything. */
8261
8262 if (good == 1)
8263 {
8264 int i = TREE_VEC_LENGTH (targs);
8265 for (; i--; )
8266 if (TREE_VEC_ELT (tempargs, i))
8267 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
8268 }
8269 if (good)
8270 return 0;
8271
8272 return 1;
8273}
8274
8275/* Subroutine of resolve_overloaded_unification; does deduction for a single
8276 overload. Fills TARGS with any deduced arguments, or error_mark_node if
8277 different overloads deduce different arguments for a given parm.
8278 Returns 1 on success. */
8279
8280static int
e97e5263 8281try_one_overload (tparms, orig_targs, targs, parm, arg, strict,
4393e105 8282 sub_strict)
e97e5263 8283 tree tparms, orig_targs, targs, parm, arg;
34016c81
JM
8284 unification_kind_t strict;
8285 int sub_strict;
34016c81
JM
8286{
8287 int nargs;
8288 tree tempargs;
8289 int i;
8290
8291 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8292 to function or pointer to member function argument if the set of
8293 overloaded functions does not contain function templates and at most
8294 one of a set of overloaded functions provides a unique match.
8295
8296 So if this is a template, just return success. */
8297
8298 if (uses_template_parms (arg))
8299 return 1;
8300
62e4a758 8301 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
34016c81
JM
8302
8303 /* We don't copy orig_targs for this because if we have already deduced
8304 some template args from previous args, unify would complain when we
8305 try to deduce a template parameter for the same argument, even though
8306 there isn't really a conflict. */
8307 nargs = TREE_VEC_LENGTH (targs);
f31c0a32 8308 tempargs = make_tree_vec (nargs);
34016c81 8309
4393e105 8310 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
34016c81
JM
8311 return 0;
8312
8313 /* First make sure we didn't deduce anything that conflicts with
e97e5263 8314 explicitly specified args. */
34016c81
JM
8315 for (i = nargs; i--; )
8316 {
8317 tree elt = TREE_VEC_ELT (tempargs, i);
e97e5263 8318 tree oldelt = TREE_VEC_ELT (orig_targs, i);
34016c81
JM
8319
8320 if (elt == NULL_TREE)
8321 continue;
8322 else if (uses_template_parms (elt))
8323 {
8324 /* Since we're unifying against ourselves, we will fill in template
8325 args used in the function parm list with our own template parms.
8326 Discard them. */
8327 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
8328 continue;
8329 }
8330 else if (oldelt && ! template_args_equal (oldelt, elt))
8331 return 0;
8332 }
8333
8334 for (i = nargs; i--; )
8335 {
8336 tree elt = TREE_VEC_ELT (tempargs, i);
8337
8338 if (elt)
8339 TREE_VEC_ELT (targs, i) = elt;
8340 }
8341
8342 return 1;
8343}
8344
74601d7c
KL
8345/* Verify that nondeduce template argument agrees with the type
8346 obtained from argument deduction. Return nonzero if the
8347 verification fails.
8348
8349 For example:
8350
8351 struct A { typedef int X; };
8352 template <class T, class U> struct C {};
8353 template <class T> struct C<T, typename T::X> {};
8354
8355 Then with the instantiation `C<A, int>', we can deduce that
8356 `T' is `A' but unify () does not check whether `typename T::X'
8357 is `int'. This function ensure that they agree.
8358
8359 TARGS, PARMS are the same as the arguments of unify.
8360 ARGS contains template arguments from all levels. */
8361
8362static int
8363verify_class_unification (targs, parms, args)
8364 tree targs, parms, args;
8365{
e2005c8d
KL
8366 parms = tsubst (parms, add_outermost_template_args (args, targs),
8367 tf_none, NULL_TREE);
8368 if (parms == error_mark_node)
74601d7c
KL
8369 return 1;
8370
e2005c8d 8371 return !comp_template_args (parms, INNERMOST_TEMPLATE_ARGS (args));
74601d7c
KL
8372}
8373
4393e105
MM
8374/* PARM is a template class (perhaps with unbound template
8375 parameters). ARG is a fully instantiated type. If ARG can be
8376 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
8377 TARGS are as for unify. */
fcfb9f96
MM
8378
8379static tree
4393e105 8380try_class_unification (tparms, targs, parm, arg)
fcfb9f96
MM
8381 tree tparms;
8382 tree targs;
4393e105
MM
8383 tree parm;
8384 tree arg;
8385{
4393e105
MM
8386 tree copy_of_targs;
8387
8388 if (!CLASSTYPE_TEMPLATE_INFO (arg)
8389 || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
8390 return NULL_TREE;
8391
8392 /* We need to make a new template argument vector for the call to
8393 unify. If we used TARGS, we'd clutter it up with the result of
8394 the attempted unification, even if this class didn't work out.
8395 We also don't want to commit ourselves to all the unifications
8396 we've already done, since unification is supposed to be done on
8397 an argument-by-argument basis. In other words, consider the
8398 following pathological case:
8399
8400 template <int I, int J, int K>
8401 struct S {};
8402
8403 template <int I, int J>
8404 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
8405
8406 template <int I, int J, int K>
8407 void f(S<I, J, K>, S<I, I, I>);
8408
8409 void g() {
8410 S<0, 0, 0> s0;
8411 S<0, 1, 2> s2;
8412
8413 f(s0, s2);
8414 }
8415
8416 Now, by the time we consider the unification involving `s2', we
8417 already know that we must have `f<0, 0, 0>'. But, even though
0e339752 8418 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
4393e105
MM
8419 because there are two ways to unify base classes of S<0, 1, 2>
8420 with S<I, I, I>. If we kept the already deduced knowledge, we
8421 would reject the possibility I=1. */
f31c0a32 8422 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
4393e105
MM
8423
8424 /* If unification failed, we're done. */
74601d7c
KL
8425 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
8426 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
4393e105 8427 return NULL_TREE;
74601d7c
KL
8428
8429 return arg;
4393e105
MM
8430}
8431
8432/* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
dc957d14 8433 have already discovered to be satisfactory. ARG_BINFO is the binfo
4393e105
MM
8434 for the base class of ARG that we are currently examining. */
8435
8436static tree
8437get_template_base_recursive (tparms, targs, parm,
8438 arg_binfo, rval, flags)
8439 tree tparms;
8440 tree targs;
8441 tree arg_binfo;
fcfb9f96 8442 tree rval;
4393e105
MM
8443 tree parm;
8444 int flags;
fcfb9f96
MM
8445{
8446 tree binfos;
8447 int i, n_baselinks;
4393e105 8448 tree arg = BINFO_TYPE (arg_binfo);
fcfb9f96 8449
4393e105 8450 if (!(flags & GTB_IGNORE_TYPE))
fcfb9f96 8451 {
4393e105
MM
8452 tree r = try_class_unification (tparms, targs,
8453 parm, arg);
fcfb9f96 8454
4393e105 8455 /* If there is more than one satisfactory baseclass, then:
fcfb9f96 8456
4393e105
MM
8457 [temp.deduct.call]
8458
8459 If they yield more than one possible deduced A, the type
8460 deduction fails.
8461
8462 applies. */
8463 if (r && rval && !same_type_p (r, rval))
8464 return error_mark_node;
8465 else if (r)
8466 rval = r;
fcfb9f96
MM
8467 }
8468
4393e105 8469 binfos = BINFO_BASETYPES (arg_binfo);
fcfb9f96
MM
8470 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
8471
8472 /* Process base types. */
8473 for (i = 0; i < n_baselinks; i++)
8474 {
8475 tree base_binfo = TREE_VEC_ELT (binfos, i);
4393e105 8476 int this_virtual;
fcfb9f96 8477
4393e105
MM
8478 /* Skip this base, if we've already seen it. */
8479 if (BINFO_MARKED (base_binfo))
8480 continue;
8481
8482 this_virtual =
8483 (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo);
8484
8485 /* When searching for a non-virtual, we cannot mark virtually
8486 found binfos. */
8487 if (! this_virtual)
8488 SET_BINFO_MARKED (base_binfo);
8489
8490 rval = get_template_base_recursive (tparms, targs,
8491 parm,
8492 base_binfo,
8493 rval,
8494 GTB_VIA_VIRTUAL * this_virtual);
8495
8496 /* If we discovered more than one matching base class, we can
8497 stop now. */
8498 if (rval == error_mark_node)
8499 return error_mark_node;
fcfb9f96
MM
8500 }
8501
8502 return rval;
8503}
8504
4393e105
MM
8505/* Given a template type PARM and a class type ARG, find the unique
8506 base type in ARG that is an instance of PARM. We do not examine
8507 ARG itself; only its base-classes. If there is no appropriate base
8508 class, return NULL_TREE. If there is more than one, return
8509 error_mark_node. PARM may be the type of a partial specialization,
8510 as well as a plain template type. Used by unify. */
fcfb9f96
MM
8511
8512static tree
4393e105 8513get_template_base (tparms, targs, parm, arg)
fcfb9f96
MM
8514 tree tparms;
8515 tree targs;
4393e105
MM
8516 tree parm;
8517 tree arg;
fcfb9f96 8518{
4393e105
MM
8519 tree rval;
8520 tree arg_binfo;
fcfb9f96 8521
4393e105
MM
8522 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92);
8523
8524 arg_binfo = TYPE_BINFO (complete_type (arg));
8525 rval = get_template_base_recursive (tparms, targs,
8526 parm, arg_binfo,
8527 NULL_TREE,
8528 GTB_IGNORE_TYPE);
fcfb9f96 8529
4393e105
MM
8530 /* Since get_template_base_recursive marks the bases classes, we
8531 must unmark them here. */
d6479fe7 8532 dfs_walk (arg_binfo, dfs_unmark, markedp, 0);
fcfb9f96
MM
8533
8534 return rval;
8535}
8536
db2767b6
MM
8537/* Returns the level of DECL, which declares a template parameter. */
8538
e9659ab0 8539static int
db2767b6
MM
8540template_decl_level (decl)
8541 tree decl;
8542{
8543 switch (TREE_CODE (decl))
8544 {
8545 case TYPE_DECL:
8546 case TEMPLATE_DECL:
8547 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
8548
8549 case PARM_DECL:
8550 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
8551
8552 default:
a98facb0 8553 abort ();
5ffe581d 8554 return 0;
db2767b6
MM
8555 }
8556}
8557
830bfa74
MM
8558/* Decide whether ARG can be unified with PARM, considering only the
8559 cv-qualifiers of each type, given STRICT as documented for unify.
838dfd8a 8560 Returns nonzero iff the unification is OK on that basis.*/
e92cc029 8561
e9659ab0 8562static int
830bfa74
MM
8563check_cv_quals_for_unify (strict, arg, parm)
8564 int strict;
8565 tree arg;
8566 tree parm;
8567{
4f2b0fb2
NS
8568 int arg_quals = cp_type_quals (arg);
8569 int parm_quals = cp_type_quals (parm);
8570
8571 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM)
8572 {
8573 /* If the cvr quals of parm will not unify with ARG, they'll be
8574 ignored in instantiation, so we have to do the same here. */
171d2f50 8575 if (TREE_CODE (arg) == REFERENCE_TYPE)
4f2b0fb2
NS
8576 parm_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
8577 if (!POINTER_TYPE_P (arg) &&
8578 TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
8579 parm_quals &= ~TYPE_QUAL_RESTRICT;
8580 }
8581
62e4a758 8582 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
4f2b0fb2 8583 && (arg_quals & parm_quals) != parm_quals)
ef637255
MM
8584 return 0;
8585
62e4a758 8586 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
4f2b0fb2 8587 && (parm_quals & arg_quals) != arg_quals)
ef637255
MM
8588 return 0;
8589
ef637255 8590 return 1;
830bfa74
MM
8591}
8592
8593/* Takes parameters as for type_unification. Returns 0 if the
dc957d14 8594 type deduction succeeds, 1 otherwise. The parameter STRICT is a
830bfa74
MM
8595 bitwise or of the following flags:
8596
8597 UNIFY_ALLOW_NONE:
8598 Require an exact match between PARM and ARG.
8599 UNIFY_ALLOW_MORE_CV_QUAL:
028d1f20
NS
8600 Allow the deduced ARG to be more cv-qualified (by qualification
8601 conversion) than ARG.
830bfa74
MM
8602 UNIFY_ALLOW_LESS_CV_QUAL:
8603 Allow the deduced ARG to be less cv-qualified than ARG.
8604 UNIFY_ALLOW_DERIVED:
8605 Allow the deduced ARG to be a template base class of ARG,
8606 or a pointer to a template base class of the type pointed to by
161c12b0
JM
8607 ARG.
8608 UNIFY_ALLOW_INTEGER:
8609 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
028d1f20
NS
8610 case for more information.
8611 UNIFY_ALLOW_OUTER_LEVEL:
8612 This is the outermost level of a deduction. Used to determine validity
8613 of qualification conversions. A valid qualification conversion must
8614 have const qualified pointers leading up to the inner type which
8615 requires additional CV quals, except at the outer level, where const
8616 is not required [conv.qual]. It would be normal to set this flag in
62e4a758
NS
8617 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
8618 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
8619 This is the outermost level of a deduction, and PARM can be more CV
8620 qualified at this point.
8621 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
8622 This is the outermost level of a deduction, and PARM can be less CV
d9f818d9
RS
8623 qualified at this point.
8624 UNIFY_ALLOW_MAX_CORRECTION:
8625 This is an INTEGER_TYPE's maximum value. Used if the range may
8626 have been derived from a size specification, such as an array size.
8627 If the size was given by a nontype template parameter N, the maximum
8628 value will have the form N-1. The flag says that we can (and indeed
8629 must) unify N with (ARG + 1), an exception to the normal rules on
8630 folding PARM. */
830bfa74 8631
e9659ab0 8632static int
4393e105 8633unify (tparms, targs, parm, arg, strict)
050367a3 8634 tree tparms, targs, parm, arg;
830bfa74 8635 int strict;
8d08fdba
MS
8636{
8637 int idx;
050367a3 8638 tree targ;
db2767b6 8639 tree tparm;
028d1f20 8640 int strict_in = strict;
8d08fdba
MS
8641
8642 /* I don't think this will do the right thing with respect to types.
8643 But the only case I've seen it in so far has been array bounds, where
8644 signedness is the only information lost, and I think that will be
8645 okay. */
8646 while (TREE_CODE (parm) == NOP_EXPR)
8647 parm = TREE_OPERAND (parm, 0);
8648
8649 if (arg == error_mark_node)
8650 return 1;
8651 if (arg == unknown_type_node)
34016c81
JM
8652 /* We can't deduce anything from this, but we might get all the
8653 template args from other function args. */
8654 return 0;
8655
db2767b6 8656 /* If PARM uses template parameters, then we can't bail out here,
6bdb985f 8657 even if ARG == PARM, since we won't record unifications for the
db2767b6
MM
8658 template parameters. We might need them if we're trying to
8659 figure out which of two things is more specialized. */
8660 if (arg == parm && !uses_template_parms (parm))
8d08fdba
MS
8661 return 0;
8662
830bfa74
MM
8663 /* Immediately reject some pairs that won't unify because of
8664 cv-qualification mismatches. */
8665 if (TREE_CODE (arg) == TREE_CODE (parm)
2f939d94 8666 && TYPE_P (arg)
d0ab7624
NS
8667 /* It is the elements of the array which hold the cv quals of an array
8668 type, and the elements might be template type parms. We'll check
8669 when we recurse. */
8670 && TREE_CODE (arg) != ARRAY_TYPE
830bfa74
MM
8671 /* We check the cv-qualifiers when unifying with template type
8672 parameters below. We want to allow ARG `const T' to unify with
8673 PARM `T' for example, when computing which of two templates
8674 is more specialized, for example. */
8675 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
028d1f20 8676 && !check_cv_quals_for_unify (strict_in, arg, parm))
49432171
JM
8677 return 1;
8678
028d1f20 8679 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
3ea099f1 8680 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
028d1f20
NS
8681 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
8682 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
8683 strict &= ~UNIFY_ALLOW_DERIVED;
62e4a758
NS
8684 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
8685 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
d9f818d9 8686 strict &= ~UNIFY_ALLOW_MAX_CORRECTION;
028d1f20 8687
8d08fdba
MS
8688 switch (TREE_CODE (parm))
8689 {
2ca340ae 8690 case TYPENAME_TYPE:
fccef71e 8691 case SCOPE_REF:
b8c6534b 8692 case UNBOUND_CLASS_TEMPLATE:
2ca340ae
JM
8693 /* In a type which contains a nested-name-specifier, template
8694 argument values cannot be deduced for template parameters used
8695 within the nested-name-specifier. */
8696 return 0;
8697
8d08fdba 8698 case TEMPLATE_TYPE_PARM:
73b0fce8 8699 case TEMPLATE_TEMPLATE_PARM:
a1281f45 8700 case BOUND_TEMPLATE_TEMPLATE_PARM:
db2767b6
MM
8701 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
8702
8703 if (TEMPLATE_TYPE_LEVEL (parm)
8704 != template_decl_level (tparm))
8705 /* The PARM is not one we're trying to unify. Just check
8706 to see if it matches ARG. */
8707 return (TREE_CODE (arg) == TREE_CODE (parm)
3bfdc719 8708 && same_type_p (parm, arg)) ? 0 : 1;
73b0fce8 8709 idx = TEMPLATE_TYPE_IDX (parm);
050367a3 8710 targ = TREE_VEC_ELT (targs, idx);
db2767b6 8711 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
050367a3 8712
73b0fce8 8713 /* Check for mixed types and values. */
db2767b6
MM
8714 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
8715 && TREE_CODE (tparm) != TYPE_DECL)
8716 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
8717 && TREE_CODE (tparm) != TEMPLATE_DECL))
73b0fce8
KL
8718 return 1;
8719
a1281f45 8720 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
73b0fce8 8721 {
b429fdf0
KL
8722 /* ARG must be constructed from a template class or a template
8723 template parameter. */
8724 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
8725 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)))
a1281f45 8726 return 1;
73b0fce8 8727
a1281f45
KL
8728 {
8729 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
8730 tree parmvec = TYPE_TI_ARGS (parm);
b429fdf0 8731 tree argvec = TYPE_TI_ARGS (arg);
a1281f45 8732 tree argtmplvec
b429fdf0 8733 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
a1281f45 8734 int i;
73b0fce8 8735
a1281f45
KL
8736 /* The parameter and argument roles have to be switched here
8737 in order to handle default arguments properly. For example,
8738 template<template <class> class TT> void f(TT<int>)
8739 should be able to accept vector<int> which comes from
8740 template <class T, class Allocator = allocator>
8741 class vector. */
8742
8743 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
8744 == error_mark_node)
8745 return 1;
73b0fce8 8746
a1281f45
KL
8747 /* Deduce arguments T, i from TT<T> or TT<i>.
8748 We check each element of PARMVEC and ARGVEC individually
8749 rather than the whole TREE_VEC since they can have
8750 different number of elements. */
6b9b6b15 8751
a1281f45
KL
8752 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
8753 {
8754 tree t = TREE_VEC_ELT (parmvec, i);
73b0fce8 8755
a1281f45
KL
8756 if (unify (tparms, targs, t,
8757 TREE_VEC_ELT (argvec, i),
8758 UNIFY_ALLOW_NONE))
8759 return 1;
73b0fce8 8760 }
a1281f45 8761 }
b429fdf0 8762 arg = TYPE_TI_TEMPLATE (arg);
a1281f45
KL
8763
8764 /* Fall through to deduce template name. */
8765 }
8766
8767 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
8768 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
8769 {
8770 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
8771
8772 /* Simple cases: Value already set, does match or doesn't. */
8773 if (targ != NULL_TREE && template_args_equal (targ, arg))
8774 return 0;
8775 else if (targ)
8776 return 1;
db2767b6
MM
8777 }
8778 else
8779 {
830bfa74
MM
8780 /* If PARM is `const T' and ARG is only `int', we don't have
8781 a match unless we are allowing additional qualification.
8782 If ARG is `const int' and PARM is just `T' that's OK;
8783 that binds `const int' to `T'. */
028d1f20 8784 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
830bfa74 8785 arg, parm))
db2767b6
MM
8786 return 1;
8787
830bfa74
MM
8788 /* Consider the case where ARG is `const volatile int' and
8789 PARM is `const T'. Then, T should be `volatile int'. */
c2ea3a40
NS
8790 arg = cp_build_qualified_type_real
8791 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
adecb3f4
MM
8792 if (arg == error_mark_node)
8793 return 1;
73b0fce8 8794
a1281f45
KL
8795 /* Simple cases: Value already set, does match or doesn't. */
8796 if (targ != NULL_TREE && same_type_p (targ, arg))
8797 return 0;
8798 else if (targ)
8799 return 1;
8800 }
61cd552e
MM
8801
8802 /* Make sure that ARG is not a variable-sized array. (Note that
8803 were talking about variable-sized arrays (like `int[n]'),
8804 rather than arrays of unknown size (like `int[]').) We'll
8805 get very confused by such a type since the bound of the array
8806 will not be computable in an instantiation. Besides, such
8807 types are not allowed in ISO C++, so we can do as we please
8808 here. */
8809 if (TREE_CODE (arg) == ARRAY_TYPE
8810 && !uses_template_parms (arg)
34e225a3 8811 && TYPE_DOMAIN (arg)
61cd552e
MM
8812 && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg)))
8813 != INTEGER_CST))
8814 return 1;
8815
050367a3 8816 TREE_VEC_ELT (targs, idx) = arg;
73b0fce8
KL
8817 return 0;
8818
f84b4be9 8819 case TEMPLATE_PARM_INDEX:
db2767b6
MM
8820 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
8821
8822 if (TEMPLATE_PARM_LEVEL (parm)
8823 != template_decl_level (tparm))
8824 /* The PARM is not one we're trying to unify. Just check
8825 to see if it matches ARG. */
8826 return (TREE_CODE (arg) == TREE_CODE (parm)
67ffc812 8827 && cp_tree_equal (parm, arg) > 0) ? 0 : 1;
db2767b6 8828
f84b4be9 8829 idx = TEMPLATE_PARM_IDX (parm);
050367a3 8830 targ = TREE_VEC_ELT (targs, idx);
db2767b6 8831
050367a3 8832 if (targ)
8d08fdba 8833 {
67ffc812 8834 int i = (cp_tree_equal (targ, arg) > 0);
312e7d50
JM
8835 if (i == 1)
8836 return 0;
8837 else if (i == 0)
8838 return 1;
8839 else
a98facb0 8840 abort ();
8d08fdba 8841 }
8d08fdba 8842
161c12b0
JM
8843 /* [temp.deduct.type] If, in the declaration of a function template
8844 with a non-type template-parameter, the non-type
8845 template-parameter is used in an expression in the function
8846 parameter-list and, if the corresponding template-argument is
8847 deduced, the template-argument type shall match the type of the
8848 template-parameter exactly, except that a template-argument
d7c4edd0
NS
8849 deduced from an array bound may be of any integral type.
8850 The non-type parameter might use already deduced type parameters. */
bd0d5d4a 8851 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
e2005c8d
KL
8852 if (!TREE_TYPE (arg))
8853 /* Template-parameter dependent expression. Just accept it for now.
8854 It will later be processed in convert_template_argument. */
8855 ;
8856 else if (same_type_p (TREE_TYPE (arg), tparm))
8857 /* OK */;
161c12b0 8858 else if ((strict & UNIFY_ALLOW_INTEGER)
bd0d5d4a
JM
8859 && (TREE_CODE (tparm) == INTEGER_TYPE
8860 || TREE_CODE (tparm) == BOOLEAN_TYPE))
161c12b0 8861 /* OK */;
bd0d5d4a
JM
8862 else if (uses_template_parms (tparm))
8863 /* We haven't deduced the type of this parameter yet. Try again
8864 later. */
8865 return 0;
161c12b0
JM
8866 else
8867 return 1;
8868
2a1e9fdd 8869 TREE_VEC_ELT (targs, idx) = arg;
8d08fdba
MS
8870 return 0;
8871
8872 case POINTER_TYPE:
830bfa74 8873 {
830bfa74
MM
8874 if (TREE_CODE (arg) != POINTER_TYPE)
8875 return 1;
8876
8877 /* [temp.deduct.call]
8878
8879 A can be another pointer or pointer to member type that can
8880 be converted to the deduced A via a qualification
8881 conversion (_conv.qual_).
8882
8883 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
8884 This will allow for additional cv-qualification of the
028d1f20 8885 pointed-to types if appropriate. */
830bfa74 8886
028d1f20 8887 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
830bfa74
MM
8888 /* The derived-to-base conversion only persists through one
8889 level of pointers. */
028d1f20 8890 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
830bfa74 8891
3ea099f1
JM
8892 if (TREE_CODE (TREE_TYPE (parm)) == OFFSET_TYPE
8893 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
8894 {
8895 /* Avoid getting confused about cv-quals; don't recurse here.
8896 Pointers to members should really be just OFFSET_TYPE, not
c6002625 8897 this two-level nonsense... */
3ea099f1
JM
8898
8899 parm = TREE_TYPE (parm);
8900 arg = TREE_TYPE (arg);
8901 goto offset;
8902 }
8903
a7a64a77 8904 return unify (tparms, targs, TREE_TYPE (parm),
028d1f20 8905 TREE_TYPE (arg), strict);
830bfa74 8906 }
8d08fdba
MS
8907
8908 case REFERENCE_TYPE:
830bfa74
MM
8909 if (TREE_CODE (arg) != REFERENCE_TYPE)
8910 return 1;
8911 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
028d1f20 8912 strict & UNIFY_ALLOW_MORE_CV_QUAL);
8d08fdba
MS
8913
8914 case ARRAY_TYPE:
8915 if (TREE_CODE (arg) != ARRAY_TYPE)
8916 return 1;
3042d5be
MM
8917 if ((TYPE_DOMAIN (parm) == NULL_TREE)
8918 != (TYPE_DOMAIN (arg) == NULL_TREE))
8919 return 1;
8920 if (TYPE_DOMAIN (parm) != NULL_TREE
830bfa74 8921 && unify (tparms, targs, TYPE_DOMAIN (parm),
4393e105 8922 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0)
8d08fdba 8923 return 1;
830bfa74 8924 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
712467a4 8925 strict & UNIFY_ALLOW_MORE_CV_QUAL);
8d08fdba
MS
8926
8927 case REAL_TYPE:
37c46b43 8928 case COMPLEX_TYPE:
c00996a3 8929 case VECTOR_TYPE:
8d08fdba 8930 case INTEGER_TYPE:
42976354 8931 case BOOLEAN_TYPE:
5ad5a526 8932 case VOID_TYPE:
f376e137
MS
8933 if (TREE_CODE (arg) != TREE_CODE (parm))
8934 return 1;
8935
514a1f18
JM
8936 if (TREE_CODE (parm) == INTEGER_TYPE
8937 && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST)
8d08fdba
MS
8938 {
8939 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
830bfa74 8940 && unify (tparms, targs, TYPE_MIN_VALUE (parm),
4393e105 8941 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER))
8d08fdba
MS
8942 return 1;
8943 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
830bfa74 8944 && unify (tparms, targs, TYPE_MAX_VALUE (parm),
d9f818d9
RS
8945 TYPE_MAX_VALUE (arg),
8946 UNIFY_ALLOW_INTEGER | UNIFY_ALLOW_MAX_CORRECTION))
8d08fdba
MS
8947 return 1;
8948 }
9edc3913 8949 /* We have already checked cv-qualification at the top of the
514a1f18 8950 function. */
9edc3913 8951 else if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
ca79f85d
JM
8952 return 1;
8953
8d08fdba
MS
8954 /* As far as unification is concerned, this wins. Later checks
8955 will invalidate it if necessary. */
8956 return 0;
8957
8958 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
bd6dd845 8959 /* Type INTEGER_CST can come from ordinary constant template args. */
8d08fdba 8960 case INTEGER_CST:
bd6dd845
MS
8961 while (TREE_CODE (arg) == NOP_EXPR)
8962 arg = TREE_OPERAND (arg, 0);
8963
8d08fdba
MS
8964 if (TREE_CODE (arg) != INTEGER_CST)
8965 return 1;
8966 return !tree_int_cst_equal (parm, arg);
8967
8d08fdba
MS
8968 case TREE_VEC:
8969 {
8970 int i;
8971 if (TREE_CODE (arg) != TREE_VEC)
8972 return 1;
8973 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
8974 return 1;
0dc09a61 8975 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
830bfa74 8976 if (unify (tparms, targs,
8d08fdba 8977 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
4393e105 8978 UNIFY_ALLOW_NONE))
8d08fdba
MS
8979 return 1;
8980 return 0;
8981 }
8982
8d08fdba 8983 case RECORD_TYPE:
f181d4ae 8984 case UNION_TYPE:
f181d4ae 8985 if (TREE_CODE (arg) != TREE_CODE (parm))
a4443a08 8986 return 1;
830bfa74 8987
a7a64a77
MM
8988 if (TYPE_PTRMEMFUNC_P (parm))
8989 {
8990 if (!TYPE_PTRMEMFUNC_P (arg))
8991 return 1;
8992
8993 return unify (tparms, targs,
8994 TYPE_PTRMEMFUNC_FN_TYPE (parm),
8995 TYPE_PTRMEMFUNC_FN_TYPE (arg),
8996 strict);
8997 }
8998
5db698f6 8999 if (CLASSTYPE_TEMPLATE_INFO (parm))
5566b478 9000 {
6467930b 9001 tree t = NULL_TREE;
4393e105 9002
028d1f20 9003 if (strict_in & UNIFY_ALLOW_DERIVED)
4393e105
MM
9004 {
9005 /* First, we try to unify the PARM and ARG directly. */
9006 t = try_class_unification (tparms, targs,
9007 parm, arg);
9008
9009 if (!t)
9010 {
9011 /* Fallback to the special case allowed in
9012 [temp.deduct.call]:
9013
9014 If P is a class, and P has the form
9015 template-id, then A can be a derived class of
9016 the deduced A. Likewise, if P is a pointer to
9017 a class of the form template-id, A can be a
9018 pointer to a derived class pointed to by the
9019 deduced A. */
9020 t = get_template_base (tparms, targs,
9021 parm, arg);
9022
9023 if (! t || t == error_mark_node)
9024 return 1;
9025 }
9026 }
6df47b06 9027 else if (CLASSTYPE_TEMPLATE_INFO (arg)
9fbf56f7
MM
9028 && (CLASSTYPE_TI_TEMPLATE (parm)
9029 == CLASSTYPE_TI_TEMPLATE (arg)))
6df47b06
MM
9030 /* Perhaps PARM is something like S<U> and ARG is S<int>.
9031 Then, we should unify `int' and `U'. */
6467930b 9032 t = arg;
4393e105 9033 else
dc957d14 9034 /* There's no chance of unification succeeding. */
5566b478 9035 return 1;
6467930b 9036
830bfa74 9037 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
4393e105 9038 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
5566b478 9039 }
9edc3913 9040 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
5566b478 9041 return 1;
a4443a08 9042 return 0;
8d08fdba
MS
9043
9044 case METHOD_TYPE:
8d08fdba 9045 case FUNCTION_TYPE:
830bfa74 9046 if (TREE_CODE (arg) != TREE_CODE (parm))
8d08fdba 9047 return 1;
830bfa74
MM
9048
9049 if (unify (tparms, targs, TREE_TYPE (parm),
4393e105 9050 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
28cbf42c 9051 return 1;
386b8a85 9052 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
3b3ba9f0 9053 TYPE_ARG_TYPES (arg), 1,
e5214479 9054 DEDUCE_EXACT, 0, -1);
a4443a08
MS
9055
9056 case OFFSET_TYPE:
3ea099f1 9057 offset:
a4443a08
MS
9058 if (TREE_CODE (arg) != OFFSET_TYPE)
9059 return 1;
830bfa74 9060 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
4393e105 9061 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
a4443a08 9062 return 1;
830bfa74 9063 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
4393e105 9064 strict);
a4443a08 9065
f62dbf03 9066 case CONST_DECL:
830bfa74 9067 if (arg != decl_constant_value (parm))
f62dbf03
JM
9068 return 1;
9069 return 0;
9070
027905b4
KL
9071 case TEMPLATE_DECL:
9072 /* Matched cases are handled by the ARG == PARM test above. */
9073 return 1;
9074
53929c47 9075 case MINUS_EXPR:
d9f818d9
RS
9076 if (tree_int_cst_equal (TREE_OPERAND (parm, 1), integer_one_node)
9077 && (strict_in & UNIFY_ALLOW_MAX_CORRECTION))
53929c47
JM
9078 {
9079 /* We handle this case specially, since it comes up with
9080 arrays. In particular, something like:
9081
9082 template <int N> void f(int (&x)[N]);
9083
9084 Here, we are trying to unify the range type, which
9085 looks like [0 ... (N - 1)]. */
9086 tree t, t1, t2;
9087 t1 = TREE_OPERAND (parm, 0);
9088 t2 = TREE_OPERAND (parm, 1);
9089
24e2e58e 9090 t = fold (build (PLUS_EXPR, integer_type_node, arg, t2));
53929c47 9091
4393e105 9092 return unify (tparms, targs, t1, t, strict);
53929c47
JM
9093 }
9094 /* else fall through */
9095
8d08fdba 9096 default:
050367a3 9097 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
74601d7c
KL
9098 {
9099
9100 /* We're looking at an expression. This can happen with
9101 something like:
4393e105 9102
74601d7c
KL
9103 template <int I>
9104 void foo(S<I>, S<I + 2>);
050367a3 9105
74601d7c 9106 This is a "nondeduced context":
050367a3 9107
74601d7c 9108 [deduct.type]
4393e105 9109
74601d7c 9110 The nondeduced contexts are:
050367a3 9111
74601d7c
KL
9112 --A type that is a template-id in which one or more of
9113 the template-arguments is an expression that references
9114 a template-parameter.
050367a3 9115
74601d7c
KL
9116 In these cases, we assume deduction succeeded, but don't
9117 actually infer any unifications. */
9118
9119 if (!uses_template_parms (parm)
9120 && !template_args_equal (parm, arg))
9121 return 1;
9122 else
9123 return 0;
9124 }
050367a3 9125 else
b4c4a9ec
MM
9126 sorry ("use of `%s' in template type unification",
9127 tree_code_name [(int) TREE_CODE (parm)]);
050367a3 9128
8d08fdba
MS
9129 return 1;
9130 }
9131}
8d08fdba 9132\f
03d0f4af
MM
9133/* Called if RESULT is explicitly instantiated, or is a member of an
9134 explicitly instantiated class, or if using -frepo and the
9135 instantiation of RESULT has been assigned to this file. */
9136
faae18ab 9137void
5566b478 9138mark_decl_instantiated (result, extern_p)
faae18ab
MS
9139 tree result;
9140 int extern_p;
9141{
75650646
MM
9142 if (TREE_CODE (result) != FUNCTION_DECL)
9143 /* The TREE_PUBLIC flag for function declarations will have been
9144 set correctly by tsubst. */
9145 TREE_PUBLIC (result) = 1;
faae18ab 9146
3ae18eaf
JM
9147 /* We used to set this unconditionally; we moved that to
9148 do_decl_instantiation so it wouldn't get set on members of
9149 explicit class template instantiations. But we still need to set
9150 it here for the 'extern template' case in order to suppress
9151 implicit instantiations. */
9152 if (extern_p)
9153 SET_DECL_EXPLICIT_INSTANTIATION (result);
9154
faae18ab
MS
9155 if (! extern_p)
9156 {
9157 DECL_INTERFACE_KNOWN (result) = 1;
9158 DECL_NOT_REALLY_EXTERN (result) = 1;
a7d87521 9159
1a408d07
JM
9160 /* Always make artificials weak. */
9161 if (DECL_ARTIFICIAL (result) && flag_weak)
9162 comdat_linkage (result);
a7d87521
JM
9163 /* For WIN32 we also want to put explicit instantiations in
9164 linkonce sections. */
1a408d07 9165 else if (TREE_PUBLIC (result))
b385c841 9166 maybe_make_one_only (result);
faae18ab 9167 }
f49422da 9168 else if (TREE_CODE (result) == FUNCTION_DECL)
56e770bf 9169 defer_fn (result);
faae18ab
MS
9170}
9171
e5214479 9172/* Given two function templates PAT1 and PAT2, return:
6467930b 9173
62e4a758
NS
9174 DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER.
9175
6467930b
MS
9176 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
9177 -1 if PAT2 is more specialized than PAT1.
e5214479
JM
9178 0 if neither is more specialized.
9179
9180 LEN is passed through to fn_type_unification. */
6467930b
MS
9181
9182int
62e4a758 9183more_specialized (pat1, pat2, deduce, len)
e5214479 9184 tree pat1, pat2;
62e4a758 9185 int deduce;
e5214479 9186 int len;
6467930b 9187{
98c1c668 9188 tree targs;
73aad9b9 9189 int winner = 0;
6467930b 9190
62e4a758
NS
9191 targs = get_bindings_real (pat1, DECL_TEMPLATE_RESULT (pat2),
9192 NULL_TREE, 0, deduce, len);
73aad9b9 9193 if (targs)
36a117a5 9194 --winner;
6467930b 9195
62e4a758
NS
9196 targs = get_bindings_real (pat2, DECL_TEMPLATE_RESULT (pat1),
9197 NULL_TREE, 0, deduce, len);
73aad9b9 9198 if (targs)
36a117a5 9199 ++winner;
6467930b 9200
73aad9b9
JM
9201 return winner;
9202}
6467930b 9203
73aad9b9 9204/* Given two class template specialization list nodes PAT1 and PAT2, return:
6467930b 9205
73aad9b9
JM
9206 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
9207 -1 if PAT2 is more specialized than PAT1.
9208 0 if neither is more specialized. */
9209
9210int
9211more_specialized_class (pat1, pat2)
9212 tree pat1, pat2;
9213{
9214 tree targs;
9215 int winner = 0;
9216
36a117a5
MM
9217 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
9218 TREE_PURPOSE (pat2));
73aad9b9
JM
9219 if (targs)
9220 --winner;
9221
36a117a5
MM
9222 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
9223 TREE_PURPOSE (pat1));
73aad9b9 9224 if (targs)
6467930b
MS
9225 ++winner;
9226
9227 return winner;
9228}
73aad9b9
JM
9229
9230/* Return the template arguments that will produce the function signature
e1467ff2 9231 DECL from the function template FN, with the explicit template
76b9a14d 9232 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
4393e105 9233 also match. Return NULL_TREE if no satisfactory arguments could be
62e4a758 9234 found. DEDUCE and LEN are passed through to fn_type_unification. */
e5214479 9235
76b9a14d 9236static tree
62e4a758 9237get_bindings_real (fn, decl, explicit_args, check_rettype, deduce, len)
e1467ff2 9238 tree fn, decl, explicit_args;
62e4a758 9239 int check_rettype, deduce, len;
73aad9b9 9240{
98c1c668 9241 int ntparms = DECL_NTPARMS (fn);
f31c0a32 9242 tree targs = make_tree_vec (ntparms);
4393e105 9243 tree decl_type;
03017874 9244 tree decl_arg_types;
98c1c668
JM
9245 int i;
9246
4393e105
MM
9247 /* Substitute the explicit template arguments into the type of DECL.
9248 The call to fn_type_unification will handle substitution into the
9249 FN. */
9250 decl_type = TREE_TYPE (decl);
9251 if (explicit_args && uses_template_parms (decl_type))
9252 {
9253 tree tmpl;
9254 tree converted_args;
9255
9256 if (DECL_TEMPLATE_INFO (decl))
9257 tmpl = DECL_TI_TEMPLATE (decl);
9258 else
0e339752 9259 /* We can get here for some invalid specializations. */
4393e105
MM
9260 return NULL_TREE;
9261
9262 converted_args
9263 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
9264 explicit_args, NULL_TREE,
c2ea3a40 9265 tf_none, /*require_all_arguments=*/0));
4393e105
MM
9266 if (converted_args == error_mark_node)
9267 return NULL_TREE;
9268
c2ea3a40 9269 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
4393e105
MM
9270 if (decl_type == error_mark_node)
9271 return NULL_TREE;
9272 }
9273
9274 decl_arg_types = TYPE_ARG_TYPES (decl_type);
e5214479
JM
9275 /* Never do unification on the 'this' parameter. */
9276 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
03017874 9277 decl_arg_types = TREE_CHAIN (decl_arg_types);
d7684f2d 9278
e1467ff2 9279 i = fn_type_unification (fn, explicit_args, targs,
03017874 9280 decl_arg_types,
8d3631f8
NS
9281 (check_rettype || DECL_CONV_FN_P (fn)
9282 ? TREE_TYPE (decl_type) : NULL_TREE),
62e4a758 9283 deduce, len);
98c1c668 9284
76b9a14d
JM
9285 if (i != 0)
9286 return NULL_TREE;
9287
76b9a14d
JM
9288 return targs;
9289}
9290
9291/* For most uses, we want to check the return type. */
9292
9293tree
9294get_bindings (fn, decl, explicit_args)
9295 tree fn, decl, explicit_args;
9296{
62e4a758 9297 return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1);
76b9a14d
JM
9298}
9299
e5214479
JM
9300/* But for resolve_overloaded_unification, we only care about the parameter
9301 types. */
76b9a14d
JM
9302
9303static tree
9304get_bindings_overload (fn, decl, explicit_args)
9305 tree fn, decl, explicit_args;
9306{
62e4a758 9307 return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1);
73aad9b9
JM
9308}
9309
36a117a5
MM
9310/* Return the innermost template arguments that, when applied to a
9311 template specialization whose innermost template parameters are
9471d3e2 9312 TPARMS, and whose specialization arguments are PARMS, yield the
36a117a5
MM
9313 ARGS.
9314
9315 For example, suppose we have:
9316
9317 template <class T, class U> struct S {};
9318 template <class T> struct S<T*, int> {};
9319
9320 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
9321 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
9322 int}. The resulting vector will be {double}, indicating that `T'
9323 is bound to `double'. */
9324
bd6dd845 9325static tree
36a117a5
MM
9326get_class_bindings (tparms, parms, args)
9327 tree tparms, parms, args;
73aad9b9 9328{
3b3ba9f0 9329 int i, ntparms = TREE_VEC_LENGTH (tparms);
f31c0a32 9330 tree vec = make_tree_vec (ntparms);
73aad9b9 9331
74601d7c
KL
9332 if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args),
9333 UNIFY_ALLOW_NONE))
fcfb9f96 9334 return NULL_TREE;
73aad9b9
JM
9335
9336 for (i = 0; i < ntparms; ++i)
9337 if (! TREE_VEC_ELT (vec, i))
9338 return NULL_TREE;
9339
74601d7c
KL
9340 if (verify_class_unification (vec, parms, args))
9341 return NULL_TREE;
9342
73aad9b9
JM
9343 return vec;
9344}
9345
104bf76a
MM
9346/* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
9347 Pick the most specialized template, and return the corresponding
9348 instantiation, or if there is no corresponding instantiation, the
e5214479
JM
9349 template itself. If there is no most specialized template,
9350 error_mark_node is returned. If there are no templates at all,
9351 NULL_TREE is returned. */
73aad9b9
JM
9352
9353tree
e5214479 9354most_specialized_instantiation (instantiations)
104bf76a 9355 tree instantiations;
73aad9b9 9356{
104bf76a 9357 tree fn, champ;
73aad9b9
JM
9358 int fate;
9359
104bf76a 9360 if (!instantiations)
73aad9b9
JM
9361 return NULL_TREE;
9362
104bf76a
MM
9363 champ = instantiations;
9364 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
73aad9b9 9365 {
62e4a758
NS
9366 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
9367 DEDUCE_EXACT, -1);
73aad9b9
JM
9368 if (fate == 1)
9369 ;
9370 else
9371 {
9372 if (fate == 0)
9373 {
9374 fn = TREE_CHAIN (fn);
9375 if (! fn)
9376 return error_mark_node;
9377 }
104bf76a 9378 champ = fn;
73aad9b9
JM
9379 }
9380 }
9381
104bf76a 9382 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
73aad9b9 9383 {
62e4a758
NS
9384 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
9385 DEDUCE_EXACT, -1);
73aad9b9
JM
9386 if (fate != 1)
9387 return error_mark_node;
9388 }
9389
104bf76a
MM
9390 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
9391}
9392
9393/* Return the most specialized of the list of templates in FNS that can
9394 produce an instantiation matching DECL, given the explicit template
9395 arguments EXPLICIT_ARGS. */
9396
e9659ab0 9397static tree
104bf76a
MM
9398most_specialized (fns, decl, explicit_args)
9399 tree fns, decl, explicit_args;
9400{
9401 tree candidates = NULL_TREE;
9402 tree fn, args;
9403
9404 for (fn = fns; fn; fn = TREE_CHAIN (fn))
9405 {
9406 tree candidate = TREE_VALUE (fn);
9407
9408 args = get_bindings (candidate, decl, explicit_args);
9409 if (args)
e1b3e07d 9410 candidates = tree_cons (NULL_TREE, candidate, candidates);
104bf76a
MM
9411 }
9412
e5214479 9413 return most_specialized_instantiation (candidates);
73aad9b9
JM
9414}
9415
36a117a5 9416/* If DECL is a specialization of some template, return the most
f9a7ae04
MM
9417 general such template. Otherwise, returns NULL_TREE.
9418
9419 For example, given:
36a117a5
MM
9420
9421 template <class T> struct S { template <class U> void f(U); };
9422
9423 if TMPL is `template <class U> void S<int>::f(U)' this will return
9424 the full template. This function will not trace past partial
9425 specializations, however. For example, given in addition:
9426
9427 template <class T> struct S<T*> { template <class U> void f(U); };
9428
9429 if TMPL is `template <class U> void S<int*>::f(U)' this will return
9430 `template <class T> template <class U> S<T*>::f(U)'. */
73aad9b9 9431
612c671a 9432tree
36a117a5
MM
9433most_general_template (decl)
9434 tree decl;
73aad9b9 9435{
f9a7ae04
MM
9436 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
9437 an immediate specialization. */
9438 if (TREE_CODE (decl) == FUNCTION_DECL)
9439 {
9440 if (DECL_TEMPLATE_INFO (decl)) {
9441 decl = DECL_TI_TEMPLATE (decl);
9442
9443 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
9444 template friend. */
9445 if (TREE_CODE (decl) != TEMPLATE_DECL)
9446 return NULL_TREE;
9447 } else
9448 return NULL_TREE;
9449 }
9450
9451 /* Look for more and more general templates. */
9452 while (DECL_TEMPLATE_INFO (decl))
9453 {
9454 /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or IDENTIFIER_NODE
9455 in some cases. (See cp-tree.h for details.) */
9456 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9457 break;
9458
9459 /* Stop if we run into an explicitly specialized class template. */
9460 if (!DECL_NAMESPACE_SCOPE_P (decl)
9461 && DECL_CONTEXT (decl)
9462 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
9463 break;
9464
9465 decl = DECL_TI_TEMPLATE (decl);
9466 }
36a117a5
MM
9467
9468 return decl;
9469}
9470
9471/* Return the most specialized of the class template specializations
9472 of TMPL which can produce an instantiation matching ARGS, or
9473 error_mark_node if the choice is ambiguous. */
9474
e9659ab0 9475static tree
36a117a5
MM
9476most_specialized_class (tmpl, args)
9477 tree tmpl;
9478 tree args;
9479{
9480 tree list = NULL_TREE;
9481 tree t;
9482 tree champ;
73aad9b9
JM
9483 int fate;
9484
36a117a5
MM
9485 tmpl = most_general_template (tmpl);
9486 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
73aad9b9 9487 {
36a117a5
MM
9488 tree spec_args
9489 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
9490 if (spec_args)
73aad9b9 9491 {
1f8f4a0b 9492 list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
73aad9b9
JM
9493 TREE_TYPE (list) = TREE_TYPE (t);
9494 }
9495 }
9496
9497 if (! list)
9498 return NULL_TREE;
9499
9500 t = list;
9501 champ = t;
9502 t = TREE_CHAIN (t);
9503 for (; t; t = TREE_CHAIN (t))
9504 {
9505 fate = more_specialized_class (champ, t);
9506 if (fate == 1)
9507 ;
9508 else
9509 {
9510 if (fate == 0)
9511 {
9512 t = TREE_CHAIN (t);
9513 if (! t)
9514 return error_mark_node;
9515 }
9516 champ = t;
9517 }
9518 }
9519
9520 for (t = list; t && t != champ; t = TREE_CHAIN (t))
9521 {
85b71cf2 9522 fate = more_specialized_class (champ, t);
73aad9b9
JM
9523 if (fate != 1)
9524 return error_mark_node;
9525 }
9526
9527 return champ;
9528}
9529
eb8845be 9530/* Explicitly instantiate DECL. */
e92cc029 9531
8d08fdba 9532void
eb8845be 9533do_decl_instantiation (tree decl, tree storage)
8d08fdba 9534{
8d08fdba 9535 tree result = NULL_TREE;
faae18ab 9536 int extern_p = 0;
e8abc66f 9537
3fa56191 9538 if (!decl)
dc957d14 9539 /* An error occurred, for which grokdeclarator has already issued
3fa56191
MM
9540 an appropriate message. */
9541 return;
9542 else if (! DECL_LANG_SPECIFIC (decl))
ec255269 9543 {
33bd39a2 9544 error ("explicit instantiation of non-template `%#D'", decl);
ec255269
MS
9545 return;
9546 }
03d0f4af 9547 else if (TREE_CODE (decl) == VAR_DECL)
6633d636 9548 {
03d0f4af
MM
9549 /* There is an asymmetry here in the way VAR_DECLs and
9550 FUNCTION_DECLs are handled by grokdeclarator. In the case of
9551 the latter, the DECL we get back will be marked as a
9552 template instantiation, and the appropriate
9553 DECL_TEMPLATE_INFO will be set up. This does not happen for
9554 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
9555 should handle VAR_DECLs as it currently handles
9556 FUNCTION_DECLs. */
6633d636
MS
9557 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
9558 if (result && TREE_CODE (result) != VAR_DECL)
03d0f4af 9559 {
33bd39a2 9560 error ("no matching template for `%D' found", result);
03d0f4af
MM
9561 return;
9562 }
6633d636
MS
9563 }
9564 else if (TREE_CODE (decl) != FUNCTION_DECL)
9565 {
33bd39a2 9566 error ("explicit instantiation of `%#D'", decl);
6633d636
MS
9567 return;
9568 }
03d0f4af
MM
9569 else
9570 result = decl;
672476cb 9571
03d0f4af 9572 /* Check for various error cases. Note that if the explicit
0e339752 9573 instantiation is valid the RESULT will currently be marked as an
03d0f4af
MM
9574 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
9575 until we get here. */
9576
9577 if (DECL_TEMPLATE_SPECIALIZATION (result))
672476cb 9578 {
07782718
KL
9579 /* DR 259 [temp.spec].
9580
9581 Both an explicit instantiation and a declaration of an explicit
9582 specialization shall not appear in a program unless the explicit
9583 instantiation follows a declaration of the explicit specialization.
03d0f4af 9584
07782718
KL
9585 For a given set of template parameters, if an explicit
9586 instantiation of a template appears after a declaration of an
9587 explicit specialization for that template, the explicit
9588 instantiation has no effect. */
672476cb
MM
9589 return;
9590 }
03d0f4af
MM
9591 else if (DECL_EXPLICIT_INSTANTIATION (result))
9592 {
9593 /* [temp.spec]
98c1c668 9594
03d0f4af
MM
9595 No program shall explicitly instantiate any template more
9596 than once.
9597
2aaf816d
JM
9598 We check DECL_INTERFACE_KNOWN so as not to complain when the first
9599 instantiation was `extern' and the second is not, and EXTERN_P for
9600 the opposite case. If -frepo, chances are we already got marked
dc957d14 9601 as an explicit instantiation because of the repo file. */
2aaf816d 9602 if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository)
33bd39a2 9603 pedwarn ("duplicate explicit instantiation of `%#D'", result);
03d0f4af
MM
9604
9605 /* If we've already instantiated the template, just return now. */
9606 if (DECL_INTERFACE_KNOWN (result))
9607 return;
9608 }
9609 else if (!DECL_IMPLICIT_INSTANTIATION (result))
faae18ab 9610 {
33bd39a2 9611 error ("no matching template for `%D' found", result);
faae18ab
MS
9612 return;
9613 }
03d0f4af 9614 else if (!DECL_TEMPLATE_INFO (result))
6633d636 9615 {
33bd39a2 9616 pedwarn ("explicit instantiation of non-template `%#D'", result);
6633d636
MS
9617 return;
9618 }
9619
a0a33927
MS
9620 if (flag_external_templates)
9621 return;
9622
f0e01782 9623 if (storage == NULL_TREE)
00595019 9624 ;
faae18ab 9625 else if (storage == ridpointers[(int) RID_EXTERN])
03d0f4af 9626 {
c02f5e29 9627 if (pedantic && !in_system_header)
33bd39a2 9628 pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations");
03d0f4af
MM
9629 extern_p = 1;
9630 }
f0e01782 9631 else
33bd39a2 9632 error ("storage class `%D' applied to template instantiation",
f0e01782 9633 storage);
5566b478 9634
03d0f4af 9635 SET_DECL_EXPLICIT_INSTANTIATION (result);
5566b478 9636 mark_decl_instantiated (result, extern_p);
44a8d0b3 9637 repo_template_instantiated (result, extern_p);
c91a56d2 9638 if (! extern_p)
16d53b64 9639 instantiate_decl (result, /*defer_ok=*/1);
7177d104
MS
9640}
9641
faae18ab
MS
9642void
9643mark_class_instantiated (t, extern_p)
9644 tree t;
9645 int extern_p;
9646{
9647 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
eb773359
JM
9648 SET_CLASSTYPE_INTERFACE_KNOWN (t);
9649 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
faae18ab
MS
9650 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
9651 if (! extern_p)
9652 {
9653 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
9654 rest_of_type_compilation (t, 1);
9655 }
9656}
e8abc66f 9657
a1bcc528
JM
9658/* Perform an explicit instantiation of template class T. STORAGE, if
9659 non-null, is the RID for extern, inline or static. COMPLAIN is
838dfd8a 9660 nonzero if this is called from the parser, zero if called recursively,
a1bcc528
JM
9661 since the standard is unclear (as detailed below). */
9662
7177d104 9663void
a1bcc528 9664do_type_instantiation (t, storage, complain)
ca79f85d 9665 tree t, storage;
c2ea3a40 9666 tsubst_flags_t complain;
7177d104 9667{
e8abc66f
MS
9668 int extern_p = 0;
9669 int nomem_p = 0;
5566b478
MS
9670 int static_p = 0;
9671
ca79f85d
JM
9672 if (TREE_CODE (t) == TYPE_DECL)
9673 t = TREE_TYPE (t);
9674
7ddedda4 9675 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
ca79f85d 9676 {
33bd39a2 9677 error ("explicit instantiation of non-template type `%T'", t);
ca79f85d
JM
9678 return;
9679 }
9680
5566b478 9681 complete_type (t);
7177d104 9682
a292b002
MS
9683 /* With -fexternal-templates, explicit instantiations are treated the same
9684 as implicit ones. */
a0a33927
MS
9685 if (flag_external_templates)
9686 return;
9687
d0f062fb 9688 if (!COMPLETE_TYPE_P (t))
f0e01782 9689 {
c2ea3a40 9690 if (complain & tf_error)
33bd39a2 9691 error ("explicit instantiation of `%#T' before definition of template",
a1bcc528 9692 t);
f0e01782
MS
9693 return;
9694 }
9695
03d0f4af 9696 if (storage != NULL_TREE)
f0e01782 9697 {
c02f5e29 9698 if (pedantic && !in_system_header)
33bd39a2 9699 pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations",
8251199e 9700 IDENTIFIER_POINTER (storage));
03d0f4af
MM
9701
9702 if (storage == ridpointers[(int) RID_INLINE])
9703 nomem_p = 1;
9704 else if (storage == ridpointers[(int) RID_EXTERN])
9705 extern_p = 1;
9706 else if (storage == ridpointers[(int) RID_STATIC])
9707 static_p = 1;
9708 else
9709 {
33bd39a2 9710 error ("storage class `%D' applied to template instantiation",
03d0f4af
MM
9711 storage);
9712 extern_p = 0;
9713 }
f0e01782
MS
9714 }
9715
370af2d5 9716 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
03d0f4af 9717 {
07782718 9718 /* DR 259 [temp.spec].
a292b002 9719
07782718
KL
9720 Both an explicit instantiation and a declaration of an explicit
9721 specialization shall not appear in a program unless the explicit
9722 instantiation follows a declaration of the explicit specialization.
9723
9724 For a given set of template parameters, if an explicit
9725 instantiation of a template appears after a declaration of an
9726 explicit specialization for that template, the explicit
9727 instantiation has no effect. */
03d0f4af
MM
9728 return;
9729 }
9730 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
44a8d0b3 9731 {
03d0f4af
MM
9732 /* [temp.spec]
9733
9734 No program shall explicitly instantiate any template more
9735 than once.
9736
2aaf816d
JM
9737 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
9738 was `extern'. If EXTERN_P then the second is. If -frepo, chances
aba649ba 9739 are we already got marked as an explicit instantiation because of the
2aaf816d 9740 repo file. All these cases are OK. */
a1bcc528 9741 if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository
c2ea3a40 9742 && (complain & tf_error))
33bd39a2 9743 pedwarn ("duplicate explicit instantiation of `%#T'", t);
03d0f4af
MM
9744
9745 /* If we've already instantiated the template, just return now. */
9746 if (!CLASSTYPE_INTERFACE_ONLY (t))
9747 return;
44a8d0b3 9748 }
e8abc66f 9749
03d0f4af
MM
9750 mark_class_instantiated (t, extern_p);
9751 repo_template_instantiated (t, extern_p);
9752
e8abc66f
MS
9753 if (nomem_p)
9754 return;
9755
7177d104 9756 {
db5ae43f 9757 tree tmp;
5566b478 9758
03d0f4af
MM
9759 /* In contrast to implicit instantiation, where only the
9760 declarations, and not the definitions, of members are
9761 instantiated, we have here:
9762
9763 [temp.explicit]
9764
9765 The explicit instantiation of a class template specialization
9766 implies the instantiation of all of its members not
9767 previously explicitly specialized in the translation unit
9768 containing the explicit instantiation.
9769
9770 Of course, we can't instantiate member template classes, since
9771 we don't have any arguments for them. Note that the standard
dc957d14 9772 is unclear on whether the instantiation of the members are
03d0f4af
MM
9773 *explicit* instantiations or not. We choose to be generous,
9774 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
9775 the explicit instantiation of a class where some of the members
9776 have no definition in the current translation unit. */
9777
5566b478
MS
9778 if (! static_p)
9779 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
a7d87521 9780 if (TREE_CODE (tmp) == FUNCTION_DECL
1f109f0f 9781 && DECL_TEMPLATE_INSTANTIATION (tmp))
5566b478
MS
9782 {
9783 mark_decl_instantiated (tmp, extern_p);
9784 repo_template_instantiated (tmp, extern_p);
9785 if (! extern_p)
16d53b64 9786 instantiate_decl (tmp, /*defer_ok=*/1);
5566b478
MS
9787 }
9788
9789 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
9790 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
863adfc0 9791 {
5566b478 9792 mark_decl_instantiated (tmp, extern_p);
863adfc0 9793 repo_template_instantiated (tmp, extern_p);
5566b478 9794 if (! extern_p)
16d53b64 9795 instantiate_decl (tmp, /*defer_ok=*/1);
863adfc0 9796 }
7177d104 9797
a292b002 9798 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
e4a84209
MM
9799 if (IS_AGGR_TYPE (TREE_VALUE (tmp))
9800 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp))))
a1bcc528 9801 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage, 0);
a292b002 9802 }
8d08fdba 9803}
a28e3c7f 9804
36a117a5
MM
9805/* Given a function DECL, which is a specialization of TMPL, modify
9806 DECL to be a re-instantiation of TMPL with the same template
9807 arguments. TMPL should be the template into which tsubst'ing
9808 should occur for DECL, not the most general template.
830bfa74
MM
9809
9810 One reason for doing this is a scenario like this:
9811
9812 template <class T>
9813 void f(const T&, int i);
9814
9815 void g() { f(3, 7); }
9816
9817 template <class T>
9818 void f(const T& t, const int i) { }
9819
9820 Note that when the template is first instantiated, with
9821 instantiate_template, the resulting DECL will have no name for the
9822 first parameter, and the wrong type for the second. So, when we go
9823 to instantiate the DECL, we regenerate it. */
9824
e9659ab0 9825static void
830bfa74
MM
9826regenerate_decl_from_template (decl, tmpl)
9827 tree decl;
9828 tree tmpl;
9829{
f9a7ae04
MM
9830 /* The most general version of TMPL. */
9831 tree gen_tmpl;
9832 /* The arguments used to instantiate DECL, from the most general
9833 template. */
830bfa74 9834 tree args;
830bfa74
MM
9835 tree code_pattern;
9836 tree new_decl;
36a117a5 9837 int unregistered;
830bfa74
MM
9838
9839 args = DECL_TI_ARGS (decl);
9840 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
9841
8251199e
JM
9842 /* Unregister the specialization so that when we tsubst we will not
9843 just return DECL. We don't have to unregister DECL from TMPL
9844 because if would only be registered there if it were a partial
9845 instantiation of a specialization, which it isn't: it's a full
9846 instantiation. */
36a117a5
MM
9847 gen_tmpl = most_general_template (tmpl);
9848 unregistered = unregister_specialization (decl, gen_tmpl);
9849
9850 /* If the DECL was not unregistered then something peculiar is
9851 happening: we created a specialization but did not call
9852 register_specialization for it. */
9853 my_friendly_assert (unregistered, 0);
9854
0e902d98 9855 if (DECL_CLASS_SCOPE_P (decl))
61a127b3
MM
9856 /* Make sure that we can see identifiers, and compute access
9857 correctly, for the class members used in the declaration of
0e902d98 9858 this static variable or function. */
61a127b3
MM
9859 pushclass (DECL_CONTEXT (decl), 2);
9860
d8c4447d 9861 /* Do the substitution to get the new declaration. */
c2ea3a40 9862 new_decl = tsubst (code_pattern, args, tf_error, NULL_TREE);
830bfa74
MM
9863
9864 if (TREE_CODE (decl) == VAR_DECL)
9865 {
9866 /* Set up DECL_INITIAL, since tsubst doesn't. */
17bbb839
MM
9867 if (!DECL_INITIALIZED_IN_CLASS_P (decl))
9868 DECL_INITIAL (new_decl) =
9869 tsubst_expr (DECL_INITIAL (code_pattern), args,
9870 tf_error, DECL_TI_TEMPLATE (decl));
830bfa74 9871 }
2b0a63a3 9872 else if (TREE_CODE (decl) == FUNCTION_DECL)
fbf1c34b
MM
9873 {
9874 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
9875 new decl. */
9876 DECL_INITIAL (new_decl) = error_mark_node;
9877 /* And don't complain about a duplicate definition. */
9878 DECL_INITIAL (decl) = NULL_TREE;
9879 }
36a117a5 9880
0e902d98
KL
9881 /* Pop the class context we pushed above. */
9882 if (DECL_CLASS_SCOPE_P (decl))
9883 popclass ();
9884
36a117a5
MM
9885 /* The immediate parent of the new template is still whatever it was
9886 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
9887 general template. We also reset the DECL_ASSEMBLER_NAME since
9888 tsubst always calculates the name as if the function in question
9889 were really a template instance, and sometimes, with friend
9890 functions, this is not so. See tsubst_friend_function for
9891 details. */
9892 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
92643fea 9893 COPY_DECL_ASSEMBLER_NAME (decl, new_decl);
19e7881c 9894 COPY_DECL_RTL (decl, new_decl);
459c43ad 9895 DECL_USE_TEMPLATE (new_decl) = DECL_USE_TEMPLATE (decl);
36a117a5
MM
9896
9897 /* Call duplicate decls to merge the old and new declarations. */
830bfa74
MM
9898 duplicate_decls (new_decl, decl);
9899
36a117a5
MM
9900 /* Now, re-register the specialization. */
9901 register_specialization (decl, gen_tmpl, args);
830bfa74
MM
9902}
9903
16d53b64 9904/* Produce the definition of D, a _DECL generated from a template. If
838dfd8a 9905 DEFER_OK is nonzero, then we don't have to actually do the
16d53b64 9906 instantiation now; we just have to do it sometime. */
f84b4be9 9907
a28e3c7f 9908tree
16d53b64 9909instantiate_decl (d, defer_ok)
5566b478 9910 tree d;
16d53b64 9911 int defer_ok;
a28e3c7f 9912{
36a117a5
MM
9913 tree tmpl = DECL_TI_TEMPLATE (d);
9914 tree args = DECL_TI_ARGS (d);
830bfa74 9915 tree td;
36a117a5
MM
9916 tree code_pattern;
9917 tree spec;
9918 tree gen_tmpl;
5566b478 9919 int pattern_defined;
c27be9b9 9920 int line = lineno;
31a714f6 9921 int need_push;
3b304f5b 9922 const char *file = input_filename;
5566b478 9923
36a117a5
MM
9924 /* This function should only be used to instantiate templates for
9925 functions and static member variables. */
9926 my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL
9927 || TREE_CODE (d) == VAR_DECL, 0);
9928
db9b2174
MM
9929 /* Don't instantiate cloned functions. Instead, instantiate the
9930 functions they cloned. */
9931 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
9932 d = DECL_CLONED_FUNCTION (d);
9933
fbf1c34b 9934 if (DECL_TEMPLATE_INSTANTIATED (d))
03d0f4af 9935 /* D has already been instantiated. It might seem reasonable to
dc957d14 9936 check whether or not D is an explicit instantiation, and, if so,
03d0f4af
MM
9937 stop here. But when an explicit instantiation is deferred
9938 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
9939 is set, even though we still need to do the instantiation. */
36a117a5
MM
9940 return d;
9941
9942 /* If we already have a specialization of this declaration, then
9943 there's no reason to instantiate it. Note that
9944 retrieve_specialization gives us both instantiations and
9945 specializations, so we must explicitly check
9946 DECL_TEMPLATE_SPECIALIZATION. */
9947 gen_tmpl = most_general_template (tmpl);
9948 spec = retrieve_specialization (gen_tmpl, args);
9949 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
9950 return spec;
9951
9952 /* This needs to happen before any tsubsting. */
9953 if (! push_tinst_level (d))
9954 return d;
9955
297a5329
JM
9956 timevar_push (TV_PARSE);
9957
4d85e00e
MM
9958 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
9959 for the instantiation. This is not always the most general
9960 template. Consider, for example:
9961
9962 template <class T>
9963 struct S { template <class U> void f();
9964 template <> void f<int>(); };
9965
9966 and an instantiation of S<double>::f<int>. We want TD to be the
9967 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
9968 td = tmpl;
f9a7ae04
MM
9969 while (/* An instantiation cannot have a definition, so we need a
9970 more general template. */
9971 DECL_TEMPLATE_INSTANTIATION (td)
9972 /* We must also deal with friend templates. Given:
9973
9974 template <class T> struct S {
9975 template <class U> friend void f() {};
9976 };
9977
9978 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
9979 so far as the language is concerned, but that's still
9980 where we get the pattern for the instantiation from. On
dc957d14 9981 other hand, if the definition comes outside the class, say:
f9a7ae04
MM
9982
9983 template <class T> struct S {
9984 template <class U> friend void f();
9985 };
9986 template <class U> friend void f() {}
9987
9988 we don't need to look any further. That's what the check for
9989 DECL_INITIAL is for. */
9990 || (TREE_CODE (d) == FUNCTION_DECL
9991 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td)
9992 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td))))
4d85e00e
MM
9993 {
9994 /* The present template, TD, should not be a definition. If it
9995 were a definition, we should be using it! Note that we
9996 cannot restructure the loop to just keep going until we find
9997 a template with a definition, since that might go too far if
9998 a specialization was declared, but not defined. */
4d85e00e
MM
9999 my_friendly_assert (!(TREE_CODE (d) == VAR_DECL
10000 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))),
10001 0);
10002
10003 /* Fetch the more general template. */
10004 td = DECL_TI_TEMPLATE (td);
10005 }
27bb8339 10006
fee23f54 10007 code_pattern = DECL_TEMPLATE_RESULT (td);
27bb8339 10008
5566b478 10009 if (TREE_CODE (d) == FUNCTION_DECL)
39c76b4f 10010 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
5566b478 10011 else
36a117a5 10012 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
de22184b 10013
de22184b
MS
10014 lineno = DECL_SOURCE_LINE (d);
10015 input_filename = DECL_SOURCE_FILE (d);
10016
de22184b 10017 if (pattern_defined)
5566b478 10018 {
7e9abee9
MM
10019 /* Let the repository code that this template definition is
10020 available.
10021
10022 The repository doesn't need to know about cloned functions
10023 because they never actually show up in the object file. It
10024 does need to know about the clones; those are the symbols
10025 that the linker will be emitting error messages about. */
10026 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (d)
10027 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (d))
10028 {
10029 tree t;
10030
10031 for (t = TREE_CHAIN (d);
10032 t && DECL_CLONED_FUNCTION_P (t);
10033 t = TREE_CHAIN (t))
10034 repo_template_used (t);
10035 }
10036 else
10037 repo_template_used (d);
5566b478
MS
10038
10039 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
10040 {
10041 if (flag_alt_external_templates)
10042 {
10043 if (interface_unknown)
10044 warn_if_unknown_interface (d);
10045 }
fee23f54 10046 else if (DECL_INTERFACE_KNOWN (code_pattern))
5566b478
MS
10047 {
10048 DECL_INTERFACE_KNOWN (d) = 1;
fee23f54 10049 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
5566b478
MS
10050 }
10051 else
fee23f54 10052 warn_if_unknown_interface (code_pattern);
5566b478
MS
10053 }
10054
e92cc029 10055 if (at_eof)
5566b478
MS
10056 import_export_decl (d);
10057 }
10058
4f2b0fb2
NS
10059 if (!defer_ok)
10060 {
10061 /* Recheck the substitutions to obtain any warning messages
10062 about ignoring cv qualifiers. */
10063 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
10064 tree type = TREE_TYPE (gen);
10065
0e902d98
KL
10066 /* Make sure that we can see identifiers, and compute access
10067 correctly. */
10068 if (DECL_CLASS_SCOPE_P (d))
10069 pushclass (DECL_CONTEXT (d), 1);
10070
4f2b0fb2
NS
10071 if (TREE_CODE (gen) == FUNCTION_DECL)
10072 {
10073 tsubst (DECL_ARGUMENTS (gen), args, tf_error | tf_warning, d);
10074 tsubst (TYPE_RAISES_EXCEPTIONS (type), args,
10075 tf_error | tf_warning, d);
10076 /* Don't simply tsubst the function type, as that will give
10077 duplicate warnings about poor parameter qualifications.
10078 The function arguments are the same as the decl_arguments
c6002625 10079 without the top level cv qualifiers. */
4f2b0fb2
NS
10080 type = TREE_TYPE (type);
10081 }
10082 tsubst (type, args, tf_error | tf_warning, d);
0e902d98
KL
10083
10084 if (DECL_CLASS_SCOPE_P (d))
10085 popclass ();
4f2b0fb2
NS
10086 }
10087
46ccf50a
JM
10088 if (TREE_CODE (d) == VAR_DECL && DECL_INITIALIZED_IN_CLASS_P (d)
10089 && DECL_INITIAL (d) == NULL_TREE)
10090 /* We should have set up DECL_INITIAL in instantiate_class_template. */
10091 abort ();
930cd796
MM
10092 /* Reject all external templates except inline functions. */
10093 else if (DECL_INTERFACE_KNOWN (d)
10094 && ! DECL_NOT_REALLY_EXTERN (d)
4f8e1232
MM
10095 && ! (TREE_CODE (d) == FUNCTION_DECL
10096 && DECL_INLINE (d)))
930cd796 10097 goto out;
16d53b64
MM
10098 /* Defer all other templates, unless we have been explicitly
10099 forbidden from doing so. We restore the source position here
10100 because it's used by add_pending_template. */
10101 else if (! pattern_defined || defer_ok)
10102 {
c27be9b9
JM
10103 lineno = line;
10104 input_filename = file;
10105
03d0f4af
MM
10106 if (at_eof && !pattern_defined
10107 && DECL_EXPLICIT_INSTANTIATION (d))
10108 /* [temp.explicit]
10109
10110 The definition of a non-exported function template, a
10111 non-exported member function template, or a non-exported
10112 member function or static data member of a class template
10113 shall be present in every translation unit in which it is
10114 explicitly instantiated. */
33bd39a2 10115 pedwarn
46ccf50a 10116 ("explicit instantiation of `%D' but no definition available", d);
03d0f4af 10117
5566b478 10118 add_pending_template (d);
de22184b 10119 goto out;
5566b478
MS
10120 }
10121
31a714f6
MM
10122 need_push = !global_bindings_p ();
10123 if (need_push)
10124 push_to_top_level ();
414ea4aa 10125
2b0a63a3
MM
10126 /* We're now committed to instantiating this template. Mark it as
10127 instantiated so that recursive calls to instantiate_decl do not
10128 try to instantiate it again. */
fbf1c34b 10129 DECL_TEMPLATE_INSTANTIATED (d) = 1;
120722ac 10130
2b0a63a3
MM
10131 /* Regenerate the declaration in case the template has been modified
10132 by a subsequent redeclaration. */
10133 regenerate_decl_from_template (d, td);
10134
120722ac
MM
10135 /* We already set the file and line above. Reset them now in case
10136 they changed as a result of calling regenerate_decl_from_template. */
5156628f
MS
10137 lineno = DECL_SOURCE_LINE (d);
10138 input_filename = DECL_SOURCE_FILE (d);
10139
5566b478
MS
10140 if (TREE_CODE (d) == VAR_DECL)
10141 {
10142 DECL_IN_AGGR_P (d) = 0;
10143 if (DECL_INTERFACE_KNOWN (d))
10144 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
10145 else
10146 {
10147 DECL_EXTERNAL (d) = 1;
10148 DECL_NOT_REALLY_EXTERN (d) = 1;
10149 }
17bbb839
MM
10150 cp_finish_decl (d,
10151 (!DECL_INITIALIZED_IN_CLASS_P (d)
10152 ? DECL_INITIAL (d) : NULL_TREE),
10153 NULL_TREE, 0);
5566b478
MS
10154 }
10155 else if (TREE_CODE (d) == FUNCTION_DECL)
10156 {
6bbf1598
MM
10157 htab_t saved_local_specializations;
10158
10159 /* Save away the current list, in case we are instantiating one
10160 template from within the body of another. */
10161 saved_local_specializations = local_specializations;
10162
6dfbb909 10163 /* Set up the list of local specializations. */
6dfbb909
MM
10164 local_specializations = htab_create (37,
10165 htab_hash_pointer,
10166 htab_eq_pointer,
10167 NULL);
10168
558475f0 10169 /* Set up context. */
a8f73d4b 10170 start_function (NULL_TREE, d, NULL_TREE, SF_PRE_PARSED);
5566b478 10171
558475f0
MM
10172 /* Substitute into the body of the function. */
10173 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
c2ea3a40 10174 tf_error | tf_warning, tmpl);
558475f0 10175
6dfbb909
MM
10176 /* We don't need the local specializations any more. */
10177 htab_delete (local_specializations);
6bbf1598 10178 local_specializations = saved_local_specializations;
6dfbb909 10179
4d6abc1c 10180 /* Finish the function. */
0acf7199 10181 expand_body (finish_function (0));
5566b478
MS
10182 }
10183
971cbc14
MM
10184 /* We're not deferring instantiation any more. */
10185 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
10186
31a714f6
MM
10187 if (need_push)
10188 pop_from_top_level ();
414ea4aa 10189
de22184b 10190out:
c27be9b9
JM
10191 lineno = line;
10192 input_filename = file;
10193
5566b478 10194 pop_tinst_level ();
a28e3c7f 10195
297a5329
JM
10196 timevar_pop (TV_PARSE);
10197
a28e3c7f
MS
10198 return d;
10199}
5566b478 10200
0aafb128
MM
10201/* Run through the list of templates that we wish we could
10202 instantiate, and instantiate any we can. */
10203
10204int
10205instantiate_pending_templates ()
10206{
10207 tree *t;
46ccf50a 10208 tree last = NULL_TREE;
0aafb128
MM
10209 int instantiated_something = 0;
10210 int reconsider;
10211
10212 do
10213 {
10214 reconsider = 0;
10215
10216 t = &pending_templates;
10217 while (*t)
10218 {
0aafb128
MM
10219 tree instantiation = TREE_VALUE (*t);
10220
3ae18eaf 10221 reopen_tinst_level (TREE_PURPOSE (*t));
0aafb128 10222
2f939d94 10223 if (TYPE_P (instantiation))
0aafb128
MM
10224 {
10225 tree fn;
10226
d0f062fb 10227 if (!COMPLETE_TYPE_P (instantiation))
0aafb128
MM
10228 {
10229 instantiate_class_template (instantiation);
10230 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
10231 for (fn = TYPE_METHODS (instantiation);
10232 fn;
10233 fn = TREE_CHAIN (fn))
10234 if (! DECL_ARTIFICIAL (fn))
16d53b64 10235 instantiate_decl (fn, /*defer_ok=*/0);
d0f062fb 10236 if (COMPLETE_TYPE_P (instantiation))
0aafb128
MM
10237 {
10238 instantiated_something = 1;
10239 reconsider = 1;
10240 }
10241 }
10242
d0f062fb 10243 if (COMPLETE_TYPE_P (instantiation))
0aafb128
MM
10244 /* If INSTANTIATION has been instantiated, then we don't
10245 need to consider it again in the future. */
10246 *t = TREE_CHAIN (*t);
46ccf50a
JM
10247 else
10248 {
10249 last = *t;
10250 t = &TREE_CHAIN (*t);
10251 }
0aafb128
MM
10252 }
10253 else
10254 {
16d53b64 10255 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
0aafb128
MM
10256 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
10257 {
16d53b64
MM
10258 instantiation = instantiate_decl (instantiation,
10259 /*defer_ok=*/0);
0aafb128
MM
10260 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
10261 {
10262 instantiated_something = 1;
10263 reconsider = 1;
10264 }
10265 }
10266
16d53b64 10267 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
0aafb128
MM
10268 || DECL_TEMPLATE_INSTANTIATED (instantiation))
10269 /* If INSTANTIATION has been instantiated, then we don't
10270 need to consider it again in the future. */
10271 *t = TREE_CHAIN (*t);
46ccf50a
JM
10272 else
10273 {
10274 last = *t;
10275 t = &TREE_CHAIN (*t);
10276 }
0aafb128 10277 }
84e5ca0f
NS
10278 tinst_depth = 0;
10279 current_tinst_level = NULL_TREE;
0aafb128 10280 }
46ccf50a 10281 last_pending_template = last;
0aafb128
MM
10282 }
10283 while (reconsider);
10284
10285 return instantiated_something;
10286}
10287
fd74ca0b
MM
10288/* Substitute ARGVEC into T, which is a list of initializers for
10289 either base class or a non-static data member. The TREE_PURPOSEs
10290 are DECLs, and the TREE_VALUEs are the initializer values. Used by
10291 instantiate_decl. */
4393e105 10292
824b9a4c 10293static tree
fd74ca0b 10294tsubst_initializer_list (t, argvec)
5566b478
MS
10295 tree t, argvec;
10296{
2282d28d 10297 tree inits = NULL_TREE;
5566b478
MS
10298
10299 for (; t; t = TREE_CHAIN (t))
10300 {
fd74ca0b
MM
10301 tree decl;
10302 tree init;
10303 tree val;
10304
c2ea3a40 10305 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning,
fd74ca0b 10306 NULL_TREE);
c2ea3a40 10307 init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning,
fd74ca0b
MM
10308 NULL_TREE);
10309
10310 if (!init)
10311 ;
10312 else if (TREE_CODE (init) == TREE_LIST)
10313 for (val = init; val; val = TREE_CHAIN (val))
10314 TREE_VALUE (val) = convert_from_reference (TREE_VALUE (val));
2282d28d 10315 else if (init != void_type_node)
fd74ca0b
MM
10316 init = convert_from_reference (init);
10317
2282d28d
MM
10318 init = expand_member_init (decl, init);
10319 if (init)
10320 {
10321 TREE_CHAIN (init) = inits;
10322 inits = init;
10323 }
5566b478 10324 }
2282d28d 10325 return inits;
5566b478
MS
10326}
10327
61a127b3
MM
10328/* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
10329
10330static void
10331set_current_access_from_decl (decl)
10332 tree decl;
10333{
10334 if (TREE_PRIVATE (decl))
10335 current_access_specifier = access_private_node;
10336 else if (TREE_PROTECTED (decl))
10337 current_access_specifier = access_protected_node;
10338 else
10339 current_access_specifier = access_public_node;
10340}
10341
dbfe2124
MM
10342/* Instantiate an enumerated type. TAG is the template type, NEWTAG
10343 is the instantiation (which should have been created with
10344 start_enum) and ARGS are the template arguments to use. */
b87692e5 10345
dbfe2124
MM
10346static void
10347tsubst_enum (tag, newtag, args)
10348 tree tag;
10349 tree newtag;
10350 tree args;
b87692e5 10351{
dbfe2124 10352 tree e;
b87692e5
MS
10353
10354 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
10355 {
61a127b3 10356 tree value;
58595203 10357
61a127b3
MM
10358 /* Note that in a template enum, the TREE_VALUE is the
10359 CONST_DECL, not the corresponding INTEGER_CST. */
10360 value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)),
c2ea3a40 10361 args, tf_error | tf_warning,
4393e105 10362 NULL_TREE);
61a127b3
MM
10363
10364 /* Give this enumeration constant the correct access. */
10365 set_current_access_from_decl (TREE_VALUE (e));
10366
10367 /* Actually build the enumerator itself. */
58595203 10368 build_enumerator (TREE_PURPOSE (e), value, newtag);
dbfe2124 10369 }
b3d5a58b 10370
219670f1 10371 finish_enum (newtag);
3e72ec9a
GDR
10372 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
10373 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
b87692e5 10374}
36a117a5 10375
1f6e1acc
AS
10376/* DECL is a FUNCTION_DECL that is a template specialization. Return
10377 its type -- but without substituting the innermost set of template
10378 arguments. So, innermost set of template parameters will appear in
10379 the type. If CONTEXTP is non-NULL, then the partially substituted
10380 DECL_CONTEXT (if any) will also be filled in. Similarly, TPARMSP
10381 will be filled in with the substituted template parameters, if it
10382 is non-NULL. */
10383
10384tree
10385get_mostly_instantiated_function_type (decl, contextp, tparmsp)
10386 tree decl;
10387 tree *contextp;
10388 tree *tparmsp;
10389{
10390 tree context = NULL_TREE;
10391 tree fn_type;
10392 tree tmpl;
10393 tree targs;
10394 tree tparms;
10395 int parm_depth;
10396
10397 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10398 targs = DECL_TI_ARGS (decl);
10399 tparms = DECL_TEMPLATE_PARMS (tmpl);
10400 parm_depth = TMPL_PARMS_DEPTH (tparms);
10401
10402 /* There should be as many levels of arguments as there are levels
10403 of parameters. */
10404 my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0);
10405
10406 fn_type = TREE_TYPE (tmpl);
10407 if (DECL_STATIC_FUNCTION_P (decl))
10408 context = DECL_CONTEXT (decl);
10409
10410 if (parm_depth == 1)
10411 /* No substitution is necessary. */
10412 ;
10413 else
10414 {
10415 int i;
10416 tree partial_args;
10417
10418 /* Replace the innermost level of the TARGS with NULL_TREEs to
dc957d14 10419 let tsubst know not to substitute for those parameters. */
1f6e1acc
AS
10420 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
10421 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
10422 SET_TMPL_ARGS_LEVEL (partial_args, i,
10423 TMPL_ARGS_LEVEL (targs, i));
10424 SET_TMPL_ARGS_LEVEL (partial_args,
10425 TMPL_ARGS_DEPTH (targs),
10426 make_tree_vec (DECL_NTPARMS (tmpl)));
10427
10428 /* Now, do the (partial) substitution to figure out the
10429 appropriate function type. */
c2ea3a40 10430 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
1f6e1acc 10431 if (DECL_STATIC_FUNCTION_P (decl))
c2ea3a40 10432 context = tsubst (context, partial_args, tf_error, NULL_TREE);
1f6e1acc
AS
10433
10434 /* Substitute into the template parameters to obtain the real
10435 innermost set of parameters. This step is important if the
10436 innermost set of template parameters contains value
10437 parameters whose types depend on outer template parameters. */
10438 TREE_VEC_LENGTH (partial_args)--;
c2ea3a40 10439 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
1f6e1acc
AS
10440 }
10441
10442 if (contextp)
10443 *contextp = context;
10444 if (tparmsp)
10445 *tparmsp = tparms;
10446
10447 return fn_type;
10448}
669ec2b4 10449
cb753e49 10450/* Return truthvalue if we're processing a template different from
dc957d14 10451 the last one involved in diagnostics. */
cb753e49
GDR
10452int
10453problematic_instantiation_changed ()
10454{
10455 return last_template_error_tick != tinst_level_tick;
10456}
10457
10458/* Remember current template involved in diagnostics. */
10459void
10460record_last_problematic_instantiation ()
10461{
10462 last_template_error_tick = tinst_level_tick;
10463}
10464
10465tree
10466current_instantiation ()
10467{
10468 return current_tinst_level;
10469}
db3f4e4e
NS
10470
10471/* [temp.param] Check that template non-type parm TYPE is of an allowable
838dfd8a 10472 type. Return zero for ok, nonzero for disallowed. Issue error and
c2ea3a40 10473 warning messages under control of COMPLAIN. */
db3f4e4e
NS
10474
10475static int
11b810f1 10476invalid_nontype_parm_type_p (type, complain)
db3f4e4e 10477 tree type;
c2ea3a40 10478 tsubst_flags_t complain;
db3f4e4e
NS
10479{
10480 if (INTEGRAL_TYPE_P (type))
10481 return 0;
10482 else if (POINTER_TYPE_P (type))
10483 return 0;
10484 else if (TYPE_PTRMEM_P (type))
10485 return 0;
10486 else if (TYPE_PTRMEMFUNC_P (type))
10487 return 0;
db3f4e4e
NS
10488 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
10489 return 0;
10490 else if (TREE_CODE (type) == TYPENAME_TYPE)
10491 return 0;
10492
c2ea3a40 10493 if (complain & tf_error)
33bd39a2 10494 error ("`%#T' is not a valid type for a template constant parameter",
db3f4e4e
NS
10495 type);
10496 return 1;
10497}
e2500fed
GK
10498
10499#include "gt-cp-pt.h"
This page took 3.109039 seconds and 5 git commands to generate.