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