PR c++/33342
[official-gcc.git] / gcc / cp / pt.c
blobe26958cb7b8d71d9c460ebd52b3594efe13400a3
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
4 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
5 Rewritten by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* Known bugs or deficiencies include:
25 all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win". */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "obstack.h"
33 #include "tree.h"
34 #include "pointer-set.h"
35 #include "flags.h"
36 #include "c-common.h"
37 #include "cp-tree.h"
38 #include "cp-objcp-common.h"
39 #include "tree-inline.h"
40 #include "decl.h"
41 #include "output.h"
42 #include "except.h"
43 #include "toplev.h"
44 #include "rtl.h"
45 #include "timevar.h"
46 #include "tree-iterator.h"
47 #include "vecprim.h"
49 /* The type of functions taking a tree, and some additional data, and
50 returning an int. */
51 typedef int (*tree_fn_t) (tree, void*);
53 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
54 instantiations have been deferred, either because their definitions
55 were not yet available, or because we were putting off doing the work. */
56 struct pending_template GTY (()) {
57 struct pending_template *next;
58 struct tinst_level *tinst;
61 static GTY(()) struct pending_template *pending_templates;
62 static GTY(()) struct pending_template *last_pending_template;
64 int processing_template_parmlist;
65 static int template_header_count;
67 static GTY(()) tree saved_trees;
68 static VEC(int,heap) *inline_parm_levels;
70 static GTY(()) struct tinst_level *current_tinst_level;
72 static GTY(()) tree saved_access_scope;
74 /* Live only within one (recursive) call to tsubst_expr. We use
75 this to pass the statement expression node from the STMT_EXPR
76 to the EXPR_STMT that is its result. */
77 static tree cur_stmt_expr;
79 /* A map from local variable declarations in the body of the template
80 presently being instantiated to the corresponding instantiated
81 local variables. */
82 static htab_t local_specializations;
84 /* Contains canonical template parameter types. The vector is indexed by
85 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
86 TREE_LIST, whose TREE_VALUEs contain the canonical template
87 parameters of various types and levels. */
88 static GTY(()) VEC(tree,gc) *canonical_template_parms;
90 #define UNIFY_ALLOW_NONE 0
91 #define UNIFY_ALLOW_MORE_CV_QUAL 1
92 #define UNIFY_ALLOW_LESS_CV_QUAL 2
93 #define UNIFY_ALLOW_DERIVED 4
94 #define UNIFY_ALLOW_INTEGER 8
95 #define UNIFY_ALLOW_OUTER_LEVEL 16
96 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
97 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
99 static void push_access_scope (tree);
100 static void pop_access_scope (tree);
101 static bool resolve_overloaded_unification (tree, tree, tree, tree,
102 unification_kind_t, int);
103 static int try_one_overload (tree, tree, tree, tree, tree,
104 unification_kind_t, int, bool);
105 static int unify (tree, tree, tree, tree, int);
106 static void add_pending_template (tree);
107 static int push_tinst_level (tree);
108 static void pop_tinst_level (void);
109 static tree reopen_tinst_level (struct tinst_level *);
110 static tree tsubst_initializer_list (tree, tree);
111 static tree get_class_bindings (tree, tree, tree);
112 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
113 bool, bool);
114 static void tsubst_enum (tree, tree, tree);
115 static tree add_to_template_args (tree, tree);
116 static tree add_outermost_template_args (tree, tree);
117 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
118 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
119 tree);
120 static int type_unification_real (tree, tree, tree, tree,
121 int, unification_kind_t, int);
122 static void note_template_header (int);
123 static tree convert_nontype_argument_function (tree, tree);
124 static tree convert_nontype_argument (tree, tree);
125 static tree convert_template_argument (tree, tree, tree,
126 tsubst_flags_t, int, tree);
127 static int for_each_template_parm (tree, tree_fn_t, void*,
128 struct pointer_set_t*);
129 static tree expand_template_argument_pack (tree);
130 static tree build_template_parm_index (int, int, int, tree, tree);
131 static bool inline_needs_template_parms (tree);
132 static void push_inline_template_parms_recursive (tree, int);
133 static tree retrieve_local_specialization (tree);
134 static void register_local_specialization (tree, tree);
135 static tree reduce_template_parm_level (tree, tree, int);
136 static int mark_template_parm (tree, void *);
137 static int template_parm_this_level_p (tree, void *);
138 static tree tsubst_friend_function (tree, tree);
139 static tree tsubst_friend_class (tree, tree);
140 static int can_complete_type_without_circularity (tree);
141 static tree get_bindings (tree, tree, tree, bool);
142 static int template_decl_level (tree);
143 static int check_cv_quals_for_unify (int, tree, tree);
144 static void template_parm_level_and_index (tree, int*, int*);
145 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
146 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
147 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
148 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
149 static void regenerate_decl_from_template (tree, tree);
150 static tree most_specialized_class (tree, tree);
151 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
152 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
153 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
154 static bool check_specialization_scope (void);
155 static tree process_partial_specialization (tree);
156 static void set_current_access_from_decl (tree);
157 static tree get_template_base (tree, tree, tree, tree);
158 static tree try_class_unification (tree, tree, tree, tree);
159 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
160 tree, tree);
161 static int template_args_equal (tree, tree);
162 static void tsubst_default_arguments (tree);
163 static tree for_each_template_parm_r (tree *, int *, void *);
164 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
165 static void copy_default_args_to_explicit_spec (tree);
166 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
167 static int eq_local_specializations (const void *, const void *);
168 static bool dependent_template_arg_p (tree);
169 static bool any_template_arguments_need_structural_equality_p (tree);
170 static bool dependent_type_p_r (tree);
171 static tree tsubst (tree, tree, tsubst_flags_t, tree);
172 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
173 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
174 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
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, or VAR_DECL for static member variable (need by
179 instantiate_decl). */
181 static void
182 push_access_scope (tree t)
184 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
185 || TREE_CODE (t) == VAR_DECL);
187 if (DECL_FRIEND_CONTEXT (t))
188 push_nested_class (DECL_FRIEND_CONTEXT (t));
189 else if (DECL_CLASS_SCOPE_P (t))
190 push_nested_class (DECL_CONTEXT (t));
191 else
192 push_to_top_level ();
194 if (TREE_CODE (t) == FUNCTION_DECL)
196 saved_access_scope = tree_cons
197 (NULL_TREE, current_function_decl, saved_access_scope);
198 current_function_decl = t;
202 /* Restore the scope set up by push_access_scope. T is the node we
203 are processing. */
205 static void
206 pop_access_scope (tree t)
208 if (TREE_CODE (t) == FUNCTION_DECL)
210 current_function_decl = TREE_VALUE (saved_access_scope);
211 saved_access_scope = TREE_CHAIN (saved_access_scope);
214 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
215 pop_nested_class ();
216 else
217 pop_from_top_level ();
220 /* Do any processing required when DECL (a member template
221 declaration) is finished. Returns the TEMPLATE_DECL corresponding
222 to DECL, unless it is a specialization, in which case the DECL
223 itself is returned. */
225 tree
226 finish_member_template_decl (tree decl)
228 if (decl == error_mark_node)
229 return error_mark_node;
231 gcc_assert (DECL_P (decl));
233 if (TREE_CODE (decl) == TYPE_DECL)
235 tree type;
237 type = TREE_TYPE (decl);
238 if (IS_AGGR_TYPE (type)
239 && CLASSTYPE_TEMPLATE_INFO (type)
240 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
242 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
243 check_member_template (tmpl);
244 return tmpl;
246 return NULL_TREE;
248 else if (TREE_CODE (decl) == FIELD_DECL)
249 error ("data member %qD cannot be a member template", decl);
250 else if (DECL_TEMPLATE_INFO (decl))
252 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
254 check_member_template (DECL_TI_TEMPLATE (decl));
255 return DECL_TI_TEMPLATE (decl);
257 else
258 return decl;
260 else
261 error ("invalid member template declaration %qD", decl);
263 return error_mark_node;
266 /* Returns the template nesting level of the indicated class TYPE.
268 For example, in:
269 template <class T>
270 struct A
272 template <class U>
273 struct B {};
276 A<T>::B<U> has depth two, while A<T> has depth one.
277 Both A<T>::B<int> and A<int>::B<U> have depth one, if
278 they are instantiations, not specializations.
280 This function is guaranteed to return 0 if passed NULL_TREE so
281 that, for example, `template_class_depth (current_class_type)' is
282 always safe. */
285 template_class_depth (tree type)
287 int depth;
289 for (depth = 0;
290 type && TREE_CODE (type) != NAMESPACE_DECL;
291 type = (TREE_CODE (type) == FUNCTION_DECL)
292 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
294 if (TREE_CODE (type) != FUNCTION_DECL)
296 if (CLASSTYPE_TEMPLATE_INFO (type)
297 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
298 && uses_template_parms (CLASSTYPE_TI_ARGS (type)))
299 ++depth;
301 else
303 if (DECL_TEMPLATE_INFO (type)
304 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
305 && uses_template_parms (DECL_TI_ARGS (type)))
306 ++depth;
310 return depth;
313 /* Subroutine of maybe_begin_member_template_processing.
314 Returns true if processing DECL needs us to push template parms. */
316 static bool
317 inline_needs_template_parms (tree decl)
319 if (! DECL_TEMPLATE_INFO (decl))
320 return false;
322 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
323 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
326 /* Subroutine of maybe_begin_member_template_processing.
327 Push the template parms in PARMS, starting from LEVELS steps into the
328 chain, and ending at the beginning, since template parms are listed
329 innermost first. */
331 static void
332 push_inline_template_parms_recursive (tree parmlist, int levels)
334 tree parms = TREE_VALUE (parmlist);
335 int i;
337 if (levels > 1)
338 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
340 ++processing_template_decl;
341 current_template_parms
342 = tree_cons (size_int (processing_template_decl),
343 parms, current_template_parms);
344 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
346 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
347 NULL);
348 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
350 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
352 if (parm == error_mark_node)
353 continue;
355 gcc_assert (DECL_P (parm));
357 switch (TREE_CODE (parm))
359 case TYPE_DECL:
360 case TEMPLATE_DECL:
361 pushdecl (parm);
362 break;
364 case PARM_DECL:
366 /* Make a CONST_DECL as is done in process_template_parm.
367 It is ugly that we recreate this here; the original
368 version built in process_template_parm is no longer
369 available. */
370 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
371 TREE_TYPE (parm));
372 DECL_ARTIFICIAL (decl) = 1;
373 TREE_CONSTANT (decl) = 1;
374 TREE_INVARIANT (decl) = 1;
375 TREE_READONLY (decl) = 1;
376 DECL_INITIAL (decl) = DECL_INITIAL (parm);
377 SET_DECL_TEMPLATE_PARM_P (decl);
378 pushdecl (decl);
380 break;
382 default:
383 gcc_unreachable ();
388 /* Restore the template parameter context for a member template or
389 a friend template defined in a class definition. */
391 void
392 maybe_begin_member_template_processing (tree decl)
394 tree parms;
395 int levels = 0;
397 if (inline_needs_template_parms (decl))
399 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
400 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
402 if (DECL_TEMPLATE_SPECIALIZATION (decl))
404 --levels;
405 parms = TREE_CHAIN (parms);
408 push_inline_template_parms_recursive (parms, levels);
411 /* Remember how many levels of template parameters we pushed so that
412 we can pop them later. */
413 VEC_safe_push (int, heap, inline_parm_levels, levels);
416 /* Undo the effects of maybe_begin_member_template_processing. */
418 void
419 maybe_end_member_template_processing (void)
421 int i;
422 int last;
424 if (VEC_length (int, inline_parm_levels) == 0)
425 return;
427 last = VEC_pop (int, inline_parm_levels);
428 for (i = 0; i < last; ++i)
430 --processing_template_decl;
431 current_template_parms = TREE_CHAIN (current_template_parms);
432 poplevel (0, 0, 0);
436 /* Return a new template argument vector which contains all of ARGS,
437 but has as its innermost set of arguments the EXTRA_ARGS. */
439 static tree
440 add_to_template_args (tree args, tree extra_args)
442 tree new_args;
443 int extra_depth;
444 int i;
445 int j;
447 extra_depth = TMPL_ARGS_DEPTH (extra_args);
448 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
450 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
451 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
453 for (j = 1; j <= extra_depth; ++j, ++i)
454 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
456 return new_args;
459 /* Like add_to_template_args, but only the outermost ARGS are added to
460 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
461 (EXTRA_ARGS) levels are added. This function is used to combine
462 the template arguments from a partial instantiation with the
463 template arguments used to attain the full instantiation from the
464 partial instantiation. */
466 static tree
467 add_outermost_template_args (tree args, tree extra_args)
469 tree new_args;
471 /* If there are more levels of EXTRA_ARGS than there are ARGS,
472 something very fishy is going on. */
473 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
475 /* If *all* the new arguments will be the EXTRA_ARGS, just return
476 them. */
477 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
478 return extra_args;
480 /* For the moment, we make ARGS look like it contains fewer levels. */
481 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
483 new_args = add_to_template_args (args, extra_args);
485 /* Now, we restore ARGS to its full dimensions. */
486 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
488 return new_args;
491 /* Return the N levels of innermost template arguments from the ARGS. */
493 tree
494 get_innermost_template_args (tree args, int n)
496 tree new_args;
497 int extra_levels;
498 int i;
500 gcc_assert (n >= 0);
502 /* If N is 1, just return the innermost set of template arguments. */
503 if (n == 1)
504 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
506 /* If we're not removing anything, just return the arguments we were
507 given. */
508 extra_levels = TMPL_ARGS_DEPTH (args) - n;
509 gcc_assert (extra_levels >= 0);
510 if (extra_levels == 0)
511 return args;
513 /* Make a new set of arguments, not containing the outer arguments. */
514 new_args = make_tree_vec (n);
515 for (i = 1; i <= n; ++i)
516 SET_TMPL_ARGS_LEVEL (new_args, i,
517 TMPL_ARGS_LEVEL (args, i + extra_levels));
519 return new_args;
522 /* The inverse of get_innermost_template_args: Return all but the innermost
523 EXTRA_LEVELS levels of template arguments from the ARGS. */
525 static tree
526 strip_innermost_template_args (tree args, int extra_levels)
528 tree new_args;
529 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
530 int i;
532 gcc_assert (n >= 0);
534 /* If N is 1, just return the outermost set of template arguments. */
535 if (n == 1)
536 return TMPL_ARGS_LEVEL (args, 1);
538 /* If we're not removing anything, just return the arguments we were
539 given. */
540 gcc_assert (extra_levels >= 0);
541 if (extra_levels == 0)
542 return args;
544 /* Make a new set of arguments, not containing the inner arguments. */
545 new_args = make_tree_vec (n);
546 for (i = 1; i <= n; ++i)
547 SET_TMPL_ARGS_LEVEL (new_args, i,
548 TMPL_ARGS_LEVEL (args, i));
550 return new_args;
553 /* We've got a template header coming up; push to a new level for storing
554 the parms. */
556 void
557 begin_template_parm_list (void)
559 /* We use a non-tag-transparent scope here, which causes pushtag to
560 put tags in this scope, rather than in the enclosing class or
561 namespace scope. This is the right thing, since we want
562 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
563 global template class, push_template_decl handles putting the
564 TEMPLATE_DECL into top-level scope. For a nested template class,
565 e.g.:
567 template <class T> struct S1 {
568 template <class T> struct S2 {};
571 pushtag contains special code to call pushdecl_with_scope on the
572 TEMPLATE_DECL for S2. */
573 begin_scope (sk_template_parms, NULL);
574 ++processing_template_decl;
575 ++processing_template_parmlist;
576 note_template_header (0);
579 /* This routine is called when a specialization is declared. If it is
580 invalid to declare a specialization here, an error is reported and
581 false is returned, otherwise this routine will return true. */
583 static bool
584 check_specialization_scope (void)
586 tree scope = current_scope ();
588 /* [temp.expl.spec]
590 An explicit specialization shall be declared in the namespace of
591 which the template is a member, or, for member templates, in the
592 namespace of which the enclosing class or enclosing class
593 template is a member. An explicit specialization of a member
594 function, member class or static data member of a class template
595 shall be declared in the namespace of which the class template
596 is a member. */
597 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
599 error ("explicit specialization in non-namespace scope %qD", scope);
600 return false;
603 /* [temp.expl.spec]
605 In an explicit specialization declaration for a member of a class
606 template or a member template that appears in namespace scope,
607 the member template and some of its enclosing class templates may
608 remain unspecialized, except that the declaration shall not
609 explicitly specialize a class member template if its enclosing
610 class templates are not explicitly specialized as well. */
611 if (current_template_parms)
613 error ("enclosing class templates are not explicitly specialized");
614 return false;
617 return true;
620 /* We've just seen template <>. */
622 bool
623 begin_specialization (void)
625 begin_scope (sk_template_spec, NULL);
626 note_template_header (1);
627 return check_specialization_scope ();
630 /* Called at then end of processing a declaration preceded by
631 template<>. */
633 void
634 end_specialization (void)
636 finish_scope ();
637 reset_specialization ();
640 /* Any template <>'s that we have seen thus far are not referring to a
641 function specialization. */
643 void
644 reset_specialization (void)
646 processing_specialization = 0;
647 template_header_count = 0;
650 /* We've just seen a template header. If SPECIALIZATION is nonzero,
651 it was of the form template <>. */
653 static void
654 note_template_header (int specialization)
656 processing_specialization = specialization;
657 template_header_count++;
660 /* We're beginning an explicit instantiation. */
662 void
663 begin_explicit_instantiation (void)
665 gcc_assert (!processing_explicit_instantiation);
666 processing_explicit_instantiation = true;
670 void
671 end_explicit_instantiation (void)
673 gcc_assert (processing_explicit_instantiation);
674 processing_explicit_instantiation = false;
677 /* An explicit specialization or partial specialization TMPL is being
678 declared. Check that the namespace in which the specialization is
679 occurring is permissible. Returns false iff it is invalid to
680 specialize TMPL in the current namespace. */
682 static bool
683 check_specialization_namespace (tree tmpl)
685 tree tpl_ns = decl_namespace_context (tmpl);
687 /* [tmpl.expl.spec]
689 An explicit specialization shall be declared in the namespace of
690 which the template is a member, or, for member templates, in the
691 namespace of which the enclosing class or enclosing class
692 template is a member. An explicit specialization of a member
693 function, member class or static data member of a class template
694 shall be declared in the namespace of which the class template is
695 a member. */
696 if (is_associated_namespace (current_namespace, tpl_ns))
697 /* Same or super-using namespace. */
698 return true;
699 else
701 pedwarn ("specialization of %qD in different namespace", tmpl);
702 pedwarn (" from definition of %q+#D", tmpl);
703 return false;
707 /* SPEC is an explicit instantiation. Check that it is valid to
708 perform this explicit instantiation in the current namespace. */
710 static void
711 check_explicit_instantiation_namespace (tree spec)
713 tree ns;
715 /* DR 275: An explicit instantiation shall appear in an enclosing
716 namespace of its template. */
717 ns = decl_namespace_context (spec);
718 if (!is_ancestor (current_namespace, ns))
719 pedwarn ("explicit instantiation of %qD in namespace %qD "
720 "(which does not enclose namespace %qD)",
721 spec, current_namespace, ns);
724 /* The TYPE is being declared. If it is a template type, that means it
725 is a partial specialization. Do appropriate error-checking. */
727 tree
728 maybe_process_partial_specialization (tree type)
730 tree context;
732 if (type == error_mark_node)
733 return error_mark_node;
735 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
737 error ("name of class shadows template template parameter %qD",
738 TYPE_NAME (type));
739 return error_mark_node;
742 context = TYPE_CONTEXT (type);
744 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
746 /* This is for ordinary explicit specialization and partial
747 specialization of a template class such as:
749 template <> class C<int>;
753 template <class T> class C<T*>;
755 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
757 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
758 && !COMPLETE_TYPE_P (type))
760 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
761 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
762 if (processing_template_decl)
763 push_template_decl (TYPE_MAIN_DECL (type));
765 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
766 error ("specialization of %qT after instantiation", type);
768 else if (CLASS_TYPE_P (type)
769 && !CLASSTYPE_USE_TEMPLATE (type)
770 && CLASSTYPE_TEMPLATE_INFO (type)
771 && context && CLASS_TYPE_P (context)
772 && CLASSTYPE_TEMPLATE_INFO (context))
774 /* This is for an explicit specialization of member class
775 template according to [temp.expl.spec/18]:
777 template <> template <class U> class C<int>::D;
779 The context `C<int>' must be an implicit instantiation.
780 Otherwise this is just a member class template declared
781 earlier like:
783 template <> class C<int> { template <class U> class D; };
784 template <> template <class U> class C<int>::D;
786 In the first case, `C<int>::D' is a specialization of `C<T>::D'
787 while in the second case, `C<int>::D' is a primary template
788 and `C<T>::D' may not exist. */
790 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
791 && !COMPLETE_TYPE_P (type))
793 tree t;
795 if (current_namespace
796 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
798 pedwarn ("specializing %q#T in different namespace", type);
799 pedwarn (" from definition of %q+#D",
800 CLASSTYPE_TI_TEMPLATE (type));
803 /* Check for invalid specialization after instantiation:
805 template <> template <> class C<int>::D<int>;
806 template <> template <class U> class C<int>::D; */
808 for (t = DECL_TEMPLATE_INSTANTIATIONS
809 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
810 t; t = TREE_CHAIN (t))
811 if (TREE_VALUE (t) != type
812 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
813 error ("specialization %qT after instantiation %qT",
814 type, TREE_VALUE (t));
816 /* Mark TYPE as a specialization. And as a result, we only
817 have one level of template argument for the innermost
818 class template. */
819 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
820 CLASSTYPE_TI_ARGS (type)
821 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
824 else if (processing_specialization)
826 error ("explicit specialization of non-template %qT", type);
827 return error_mark_node;
830 return type;
833 /* Returns nonzero if we can optimize the retrieval of specializations
834 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
835 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
837 static inline bool
838 optimize_specialization_lookup_p (tree tmpl)
840 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
841 && DECL_CLASS_SCOPE_P (tmpl)
842 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
843 parameter. */
844 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
845 /* The optimized lookup depends on the fact that the
846 template arguments for the member function template apply
847 purely to the containing class, which is not true if the
848 containing class is an explicit or partial
849 specialization. */
850 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
851 && !DECL_MEMBER_TEMPLATE_P (tmpl)
852 && !DECL_CONV_FN_P (tmpl)
853 /* It is possible to have a template that is not a member
854 template and is not a member of a template class:
856 template <typename T>
857 struct S { friend A::f(); };
859 Here, the friend function is a template, but the context does
860 not have template information. The optimized lookup relies
861 on having ARGS be the template arguments for both the class
862 and the function template. */
863 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
866 /* Retrieve the specialization (in the sense of [temp.spec] - a
867 specialization is either an instantiation or an explicit
868 specialization) of TMPL for the given template ARGS. If there is
869 no such specialization, return NULL_TREE. The ARGS are a vector of
870 arguments, or a vector of vectors of arguments, in the case of
871 templates with more than one level of parameters.
873 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
874 then we search for a partial specialization matching ARGS. This
875 parameter is ignored if TMPL is not a class template. */
877 static tree
878 retrieve_specialization (tree tmpl, tree args,
879 bool class_specializations_p)
881 if (args == error_mark_node)
882 return NULL_TREE;
884 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
886 /* There should be as many levels of arguments as there are
887 levels of parameters. */
888 gcc_assert (TMPL_ARGS_DEPTH (args)
889 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
891 if (optimize_specialization_lookup_p (tmpl))
893 tree class_template;
894 tree class_specialization;
895 VEC(tree,gc) *methods;
896 tree fns;
897 int idx;
899 /* The template arguments actually apply to the containing
900 class. Find the class specialization with those
901 arguments. */
902 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
903 class_specialization
904 = retrieve_specialization (class_template, args,
905 /*class_specializations_p=*/false);
906 if (!class_specialization)
907 return NULL_TREE;
908 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
909 for the specialization. */
910 idx = class_method_index_for_fn (class_specialization, tmpl);
911 if (idx == -1)
912 return NULL_TREE;
913 /* Iterate through the methods with the indicated name, looking
914 for the one that has an instance of TMPL. */
915 methods = CLASSTYPE_METHOD_VEC (class_specialization);
916 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
918 tree fn = OVL_CURRENT (fns);
919 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
920 return fn;
922 return NULL_TREE;
924 else
926 tree *sp;
927 tree *head;
929 /* Class templates store their instantiations on the
930 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
931 DECL_TEMPLATE_SPECIALIZATIONS list. */
932 if (!class_specializations_p
933 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
934 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
935 else
936 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
937 head = sp;
938 /* Iterate through the list until we find a matching template. */
939 while (*sp != NULL_TREE)
941 tree spec = *sp;
943 if (comp_template_args (TREE_PURPOSE (spec), args))
945 /* Use the move-to-front heuristic to speed up future
946 searches. */
947 if (spec != *head)
949 *sp = TREE_CHAIN (*sp);
950 TREE_CHAIN (spec) = *head;
951 *head = spec;
953 return TREE_VALUE (spec);
955 sp = &TREE_CHAIN (spec);
959 return NULL_TREE;
962 /* Like retrieve_specialization, but for local declarations. */
964 static tree
965 retrieve_local_specialization (tree tmpl)
967 tree spec = (tree) htab_find_with_hash (local_specializations, tmpl,
968 htab_hash_pointer (tmpl));
969 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
972 /* Returns nonzero iff DECL is a specialization of TMPL. */
975 is_specialization_of (tree decl, tree tmpl)
977 tree t;
979 if (TREE_CODE (decl) == FUNCTION_DECL)
981 for (t = decl;
982 t != NULL_TREE;
983 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
984 if (t == tmpl)
985 return 1;
987 else
989 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
991 for (t = TREE_TYPE (decl);
992 t != NULL_TREE;
993 t = CLASSTYPE_USE_TEMPLATE (t)
994 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
995 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
996 return 1;
999 return 0;
1002 /* Returns nonzero iff DECL is a specialization of friend declaration
1003 FRIEND according to [temp.friend]. */
1005 bool
1006 is_specialization_of_friend (tree decl, tree friend)
1008 bool need_template = true;
1009 int template_depth;
1011 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1012 || TREE_CODE (decl) == TYPE_DECL);
1014 /* For [temp.friend/6] when FRIEND is an ordinary member function
1015 of a template class, we want to check if DECL is a specialization
1016 if this. */
1017 if (TREE_CODE (friend) == FUNCTION_DECL
1018 && DECL_TEMPLATE_INFO (friend)
1019 && !DECL_USE_TEMPLATE (friend))
1021 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1022 friend = DECL_TI_TEMPLATE (friend);
1023 need_template = false;
1025 else if (TREE_CODE (friend) == TEMPLATE_DECL
1026 && !PRIMARY_TEMPLATE_P (friend))
1027 need_template = false;
1029 /* There is nothing to do if this is not a template friend. */
1030 if (TREE_CODE (friend) != TEMPLATE_DECL)
1031 return false;
1033 if (is_specialization_of (decl, friend))
1034 return true;
1036 /* [temp.friend/6]
1037 A member of a class template may be declared to be a friend of a
1038 non-template class. In this case, the corresponding member of
1039 every specialization of the class template is a friend of the
1040 class granting friendship.
1042 For example, given a template friend declaration
1044 template <class T> friend void A<T>::f();
1046 the member function below is considered a friend
1048 template <> struct A<int> {
1049 void f();
1052 For this type of template friend, TEMPLATE_DEPTH below will be
1053 nonzero. To determine if DECL is a friend of FRIEND, we first
1054 check if the enclosing class is a specialization of another. */
1056 template_depth = template_class_depth (DECL_CONTEXT (friend));
1057 if (template_depth
1058 && DECL_CLASS_SCOPE_P (decl)
1059 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1060 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
1062 /* Next, we check the members themselves. In order to handle
1063 a few tricky cases, such as when FRIEND's are
1065 template <class T> friend void A<T>::g(T t);
1066 template <class T> template <T t> friend void A<T>::h();
1068 and DECL's are
1070 void A<int>::g(int);
1071 template <int> void A<int>::h();
1073 we need to figure out ARGS, the template arguments from
1074 the context of DECL. This is required for template substitution
1075 of `T' in the function parameter of `g' and template parameter
1076 of `h' in the above examples. Here ARGS corresponds to `int'. */
1078 tree context = DECL_CONTEXT (decl);
1079 tree args = NULL_TREE;
1080 int current_depth = 0;
1082 while (current_depth < template_depth)
1084 if (CLASSTYPE_TEMPLATE_INFO (context))
1086 if (current_depth == 0)
1087 args = TYPE_TI_ARGS (context);
1088 else
1089 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1090 current_depth++;
1092 context = TYPE_CONTEXT (context);
1095 if (TREE_CODE (decl) == FUNCTION_DECL)
1097 bool is_template;
1098 tree friend_type;
1099 tree decl_type;
1100 tree friend_args_type;
1101 tree decl_args_type;
1103 /* Make sure that both DECL and FRIEND are templates or
1104 non-templates. */
1105 is_template = DECL_TEMPLATE_INFO (decl)
1106 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1107 if (need_template ^ is_template)
1108 return false;
1109 else if (is_template)
1111 /* If both are templates, check template parameter list. */
1112 tree friend_parms
1113 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1114 args, tf_none);
1115 if (!comp_template_parms
1116 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1117 friend_parms))
1118 return false;
1120 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1122 else
1123 decl_type = TREE_TYPE (decl);
1125 friend_type = tsubst_function_type (TREE_TYPE (friend), args,
1126 tf_none, NULL_TREE);
1127 if (friend_type == error_mark_node)
1128 return false;
1130 /* Check if return types match. */
1131 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1132 return false;
1134 /* Check if function parameter types match, ignoring the
1135 `this' parameter. */
1136 friend_args_type = TYPE_ARG_TYPES (friend_type);
1137 decl_args_type = TYPE_ARG_TYPES (decl_type);
1138 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1139 friend_args_type = TREE_CHAIN (friend_args_type);
1140 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1141 decl_args_type = TREE_CHAIN (decl_args_type);
1143 return compparms (decl_args_type, friend_args_type);
1145 else
1147 /* DECL is a TYPE_DECL */
1148 bool is_template;
1149 tree decl_type = TREE_TYPE (decl);
1151 /* Make sure that both DECL and FRIEND are templates or
1152 non-templates. */
1153 is_template
1154 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1155 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1157 if (need_template ^ is_template)
1158 return false;
1159 else if (is_template)
1161 tree friend_parms;
1162 /* If both are templates, check the name of the two
1163 TEMPLATE_DECL's first because is_friend didn't. */
1164 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1165 != DECL_NAME (friend))
1166 return false;
1168 /* Now check template parameter list. */
1169 friend_parms
1170 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1171 args, tf_none);
1172 return comp_template_parms
1173 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1174 friend_parms);
1176 else
1177 return (DECL_NAME (decl)
1178 == DECL_NAME (friend));
1181 return false;
1184 /* Register the specialization SPEC as a specialization of TMPL with
1185 the indicated ARGS. IS_FRIEND indicates whether the specialization
1186 is actually just a friend declaration. Returns SPEC, or an
1187 equivalent prior declaration, if available. */
1189 static tree
1190 register_specialization (tree spec, tree tmpl, tree args, bool is_friend)
1192 tree fn;
1194 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1196 if (TREE_CODE (spec) == FUNCTION_DECL
1197 && uses_template_parms (DECL_TI_ARGS (spec)))
1198 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1199 register it; we want the corresponding TEMPLATE_DECL instead.
1200 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1201 the more obvious `uses_template_parms (spec)' to avoid problems
1202 with default function arguments. In particular, given
1203 something like this:
1205 template <class T> void f(T t1, T t = T())
1207 the default argument expression is not substituted for in an
1208 instantiation unless and until it is actually needed. */
1209 return spec;
1211 fn = retrieve_specialization (tmpl, args,
1212 /*class_specializations_p=*/false);
1213 /* We can sometimes try to re-register a specialization that we've
1214 already got. In particular, regenerate_decl_from_template calls
1215 duplicate_decls which will update the specialization list. But,
1216 we'll still get called again here anyhow. It's more convenient
1217 to simply allow this than to try to prevent it. */
1218 if (fn == spec)
1219 return spec;
1220 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1222 if (DECL_TEMPLATE_INSTANTIATION (fn))
1224 if (TREE_USED (fn)
1225 || DECL_EXPLICIT_INSTANTIATION (fn))
1227 error ("specialization of %qD after instantiation",
1228 fn);
1229 return error_mark_node;
1231 else
1233 tree clone;
1234 /* This situation should occur only if the first
1235 specialization is an implicit instantiation, the
1236 second is an explicit specialization, and the
1237 implicit instantiation has not yet been used. That
1238 situation can occur if we have implicitly
1239 instantiated a member function and then specialized
1240 it later.
1242 We can also wind up here if a friend declaration that
1243 looked like an instantiation turns out to be a
1244 specialization:
1246 template <class T> void foo(T);
1247 class S { friend void foo<>(int) };
1248 template <> void foo(int);
1250 We transform the existing DECL in place so that any
1251 pointers to it become pointers to the updated
1252 declaration.
1254 If there was a definition for the template, but not
1255 for the specialization, we want this to look as if
1256 there were no definition, and vice versa. */
1257 DECL_INITIAL (fn) = NULL_TREE;
1258 duplicate_decls (spec, fn, is_friend);
1259 /* The call to duplicate_decls will have applied
1260 [temp.expl.spec]:
1262 An explicit specialization of a function template
1263 is inline only if it is explicitly declared to be,
1264 and independently of whether its function template
1267 to the primary function; now copy the inline bits to
1268 the various clones. */
1269 FOR_EACH_CLONE (clone, fn)
1271 DECL_DECLARED_INLINE_P (clone)
1272 = DECL_DECLARED_INLINE_P (fn);
1273 DECL_INLINE (clone)
1274 = DECL_INLINE (fn);
1276 check_specialization_namespace (fn);
1278 return fn;
1281 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1283 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1284 /* Dup decl failed, but this is a new definition. Set the
1285 line number so any errors match this new
1286 definition. */
1287 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1289 return fn;
1293 /* A specialization must be declared in the same namespace as the
1294 template it is specializing. */
1295 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1296 && !check_specialization_namespace (tmpl))
1297 DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl));
1299 if (!optimize_specialization_lookup_p (tmpl))
1300 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1301 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1303 return spec;
1306 /* Unregister the specialization SPEC as a specialization of TMPL.
1307 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1308 if the SPEC was listed as a specialization of TMPL. */
1310 bool
1311 reregister_specialization (tree spec, tree tmpl, tree new_spec)
1313 tree* s;
1315 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1316 *s != NULL_TREE;
1317 s = &TREE_CHAIN (*s))
1318 if (TREE_VALUE (*s) == spec)
1320 if (!new_spec)
1321 *s = TREE_CHAIN (*s);
1322 else
1323 TREE_VALUE (*s) = new_spec;
1324 return 1;
1327 return 0;
1330 /* Compare an entry in the local specializations hash table P1 (which
1331 is really a pointer to a TREE_LIST) with P2 (which is really a
1332 DECL). */
1334 static int
1335 eq_local_specializations (const void *p1, const void *p2)
1337 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1340 /* Hash P1, an entry in the local specializations table. */
1342 static hashval_t
1343 hash_local_specialization (const void* p1)
1345 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1348 /* Like register_specialization, but for local declarations. We are
1349 registering SPEC, an instantiation of TMPL. */
1351 static void
1352 register_local_specialization (tree spec, tree tmpl)
1354 void **slot;
1356 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1357 htab_hash_pointer (tmpl), INSERT);
1358 *slot = build_tree_list (spec, tmpl);
1361 /* TYPE is a class type. Returns true if TYPE is an explicitly
1362 specialized class. */
1364 bool
1365 explicit_class_specialization_p (tree type)
1367 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1368 return false;
1369 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1372 /* Print the list of candidate FNS in an error message. */
1374 void
1375 print_candidates (tree fns)
1377 tree fn;
1379 const char *str = "candidates are:";
1381 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1383 tree f;
1385 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1386 error ("%s %+#D", str, OVL_CURRENT (f));
1387 str = " ";
1391 /* Returns the template (one of the functions given by TEMPLATE_ID)
1392 which can be specialized to match the indicated DECL with the
1393 explicit template args given in TEMPLATE_ID. The DECL may be
1394 NULL_TREE if none is available. In that case, the functions in
1395 TEMPLATE_ID are non-members.
1397 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1398 specialization of a member template.
1400 The TEMPLATE_COUNT is the number of references to qualifying
1401 template classes that appeared in the name of the function. See
1402 check_explicit_specialization for a more accurate description.
1404 TSK indicates what kind of template declaration (if any) is being
1405 declared. TSK_TEMPLATE indicates that the declaration given by
1406 DECL, though a FUNCTION_DECL, has template parameters, and is
1407 therefore a template function.
1409 The template args (those explicitly specified and those deduced)
1410 are output in a newly created vector *TARGS_OUT.
1412 If it is impossible to determine the result, an error message is
1413 issued. The error_mark_node is returned to indicate failure. */
1415 static tree
1416 determine_specialization (tree template_id,
1417 tree decl,
1418 tree* targs_out,
1419 int need_member_template,
1420 int template_count,
1421 tmpl_spec_kind tsk)
1423 tree fns;
1424 tree targs;
1425 tree explicit_targs;
1426 tree candidates = NULL_TREE;
1427 /* A TREE_LIST of templates of which DECL may be a specialization.
1428 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1429 corresponding TREE_PURPOSE is the set of template arguments that,
1430 when used to instantiate the template, would produce a function
1431 with the signature of DECL. */
1432 tree templates = NULL_TREE;
1433 int header_count;
1434 struct cp_binding_level *b;
1436 *targs_out = NULL_TREE;
1438 if (template_id == error_mark_node || decl == error_mark_node)
1439 return error_mark_node;
1441 fns = TREE_OPERAND (template_id, 0);
1442 explicit_targs = TREE_OPERAND (template_id, 1);
1444 if (fns == error_mark_node)
1445 return error_mark_node;
1447 /* Check for baselinks. */
1448 if (BASELINK_P (fns))
1449 fns = BASELINK_FUNCTIONS (fns);
1451 if (!is_overloaded_fn (fns))
1453 error ("%qD is not a function template", fns);
1454 return error_mark_node;
1457 /* Count the number of template headers specified for this
1458 specialization. */
1459 header_count = 0;
1460 for (b = current_binding_level;
1461 b->kind == sk_template_parms;
1462 b = b->level_chain)
1463 ++header_count;
1465 for (; fns; fns = OVL_NEXT (fns))
1467 tree fn = OVL_CURRENT (fns);
1469 if (TREE_CODE (fn) == TEMPLATE_DECL)
1471 tree decl_arg_types;
1472 tree fn_arg_types;
1474 /* In case of explicit specialization, we need to check if
1475 the number of template headers appearing in the specialization
1476 is correct. This is usually done in check_explicit_specialization,
1477 but the check done there cannot be exhaustive when specializing
1478 member functions. Consider the following code:
1480 template <> void A<int>::f(int);
1481 template <> template <> void A<int>::f(int);
1483 Assuming that A<int> is not itself an explicit specialization
1484 already, the first line specializes "f" which is a non-template
1485 member function, whilst the second line specializes "f" which
1486 is a template member function. So both lines are syntactically
1487 correct, and check_explicit_specialization does not reject
1488 them.
1490 Here, we can do better, as we are matching the specialization
1491 against the declarations. We count the number of template
1492 headers, and we check if they match TEMPLATE_COUNT + 1
1493 (TEMPLATE_COUNT is the number of qualifying template classes,
1494 plus there must be another header for the member template
1495 itself).
1497 Notice that if header_count is zero, this is not a
1498 specialization but rather a template instantiation, so there
1499 is no check we can perform here. */
1500 if (header_count && header_count != template_count + 1)
1501 continue;
1503 /* Check that the number of template arguments at the
1504 innermost level for DECL is the same as for FN. */
1505 if (current_binding_level->kind == sk_template_parms
1506 && !current_binding_level->explicit_spec_p
1507 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1508 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1509 (current_template_parms))))
1510 continue;
1512 /* DECL might be a specialization of FN. */
1513 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1514 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1516 /* For a non-static member function, we need to make sure
1517 that the const qualification is the same. Since
1518 get_bindings does not try to merge the "this" parameter,
1519 we must do the comparison explicitly. */
1520 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1521 && !same_type_p (TREE_VALUE (fn_arg_types),
1522 TREE_VALUE (decl_arg_types)))
1523 continue;
1525 /* Skip the "this" parameter and, for constructors of
1526 classes with virtual bases, the VTT parameter. A
1527 full specialization of a constructor will have a VTT
1528 parameter, but a template never will. */
1529 decl_arg_types
1530 = skip_artificial_parms_for (decl, decl_arg_types);
1531 fn_arg_types
1532 = skip_artificial_parms_for (fn, fn_arg_types);
1534 /* Check that the number of function parameters matches.
1535 For example,
1536 template <class T> void f(int i = 0);
1537 template <> void f<int>();
1538 The specialization f<int> is invalid but is not caught
1539 by get_bindings below. */
1540 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1541 continue;
1543 /* Function templates cannot be specializations; there are
1544 no partial specializations of functions. Therefore, if
1545 the type of DECL does not match FN, there is no
1546 match. */
1547 if (tsk == tsk_template)
1549 if (compparms (fn_arg_types, decl_arg_types))
1550 candidates = tree_cons (NULL_TREE, fn, candidates);
1551 continue;
1554 /* See whether this function might be a specialization of this
1555 template. */
1556 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1558 if (!targs)
1559 /* We cannot deduce template arguments that when used to
1560 specialize TMPL will produce DECL. */
1561 continue;
1563 /* Save this template, and the arguments deduced. */
1564 templates = tree_cons (targs, fn, templates);
1566 else if (need_member_template)
1567 /* FN is an ordinary member function, and we need a
1568 specialization of a member template. */
1570 else if (TREE_CODE (fn) != FUNCTION_DECL)
1571 /* We can get IDENTIFIER_NODEs here in certain erroneous
1572 cases. */
1574 else if (!DECL_FUNCTION_MEMBER_P (fn))
1575 /* This is just an ordinary non-member function. Nothing can
1576 be a specialization of that. */
1578 else if (DECL_ARTIFICIAL (fn))
1579 /* Cannot specialize functions that are created implicitly. */
1581 else
1583 tree decl_arg_types;
1585 /* This is an ordinary member function. However, since
1586 we're here, we can assume it's enclosing class is a
1587 template class. For example,
1589 template <typename T> struct S { void f(); };
1590 template <> void S<int>::f() {}
1592 Here, S<int>::f is a non-template, but S<int> is a
1593 template class. If FN has the same type as DECL, we
1594 might be in business. */
1596 if (!DECL_TEMPLATE_INFO (fn))
1597 /* Its enclosing class is an explicit specialization
1598 of a template class. This is not a candidate. */
1599 continue;
1601 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1602 TREE_TYPE (TREE_TYPE (fn))))
1603 /* The return types differ. */
1604 continue;
1606 /* Adjust the type of DECL in case FN is a static member. */
1607 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1608 if (DECL_STATIC_FUNCTION_P (fn)
1609 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1610 decl_arg_types = TREE_CHAIN (decl_arg_types);
1612 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1613 decl_arg_types))
1614 /* They match! */
1615 candidates = tree_cons (NULL_TREE, fn, candidates);
1619 if (templates && TREE_CHAIN (templates))
1621 /* We have:
1623 [temp.expl.spec]
1625 It is possible for a specialization with a given function
1626 signature to be instantiated from more than one function
1627 template. In such cases, explicit specification of the
1628 template arguments must be used to uniquely identify the
1629 function template specialization being specialized.
1631 Note that here, there's no suggestion that we're supposed to
1632 determine which of the candidate templates is most
1633 specialized. However, we, also have:
1635 [temp.func.order]
1637 Partial ordering of overloaded function template
1638 declarations is used in the following contexts to select
1639 the function template to which a function template
1640 specialization refers:
1642 -- when an explicit specialization refers to a function
1643 template.
1645 So, we do use the partial ordering rules, at least for now.
1646 This extension can only serve to make invalid programs valid,
1647 so it's safe. And, there is strong anecdotal evidence that
1648 the committee intended the partial ordering rules to apply;
1649 the EDG front end has that behavior, and John Spicer claims
1650 that the committee simply forgot to delete the wording in
1651 [temp.expl.spec]. */
1652 tree tmpl = most_specialized_instantiation (templates);
1653 if (tmpl != error_mark_node)
1655 templates = tmpl;
1656 TREE_CHAIN (templates) = NULL_TREE;
1660 if (templates == NULL_TREE && candidates == NULL_TREE)
1662 error ("template-id %qD for %q+D does not match any template "
1663 "declaration", template_id, decl);
1664 return error_mark_node;
1666 else if ((templates && TREE_CHAIN (templates))
1667 || (candidates && TREE_CHAIN (candidates))
1668 || (templates && candidates))
1670 error ("ambiguous template specialization %qD for %q+D",
1671 template_id, decl);
1672 chainon (candidates, templates);
1673 print_candidates (candidates);
1674 return error_mark_node;
1677 /* We have one, and exactly one, match. */
1678 if (candidates)
1680 tree fn = TREE_VALUE (candidates);
1681 /* DECL is a re-declaration of a template function. */
1682 if (TREE_CODE (fn) == TEMPLATE_DECL)
1683 return fn;
1684 /* It was a specialization of an ordinary member function in a
1685 template class. */
1686 *targs_out = copy_node (DECL_TI_ARGS (fn));
1687 return DECL_TI_TEMPLATE (fn);
1690 /* It was a specialization of a template. */
1691 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1692 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1694 *targs_out = copy_node (targs);
1695 SET_TMPL_ARGS_LEVEL (*targs_out,
1696 TMPL_ARGS_DEPTH (*targs_out),
1697 TREE_PURPOSE (templates));
1699 else
1700 *targs_out = TREE_PURPOSE (templates);
1701 return TREE_VALUE (templates);
1704 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1705 but with the default argument values filled in from those in the
1706 TMPL_TYPES. */
1708 static tree
1709 copy_default_args_to_explicit_spec_1 (tree spec_types,
1710 tree tmpl_types)
1712 tree new_spec_types;
1714 if (!spec_types)
1715 return NULL_TREE;
1717 if (spec_types == void_list_node)
1718 return void_list_node;
1720 /* Substitute into the rest of the list. */
1721 new_spec_types =
1722 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1723 TREE_CHAIN (tmpl_types));
1725 /* Add the default argument for this parameter. */
1726 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1727 TREE_VALUE (spec_types),
1728 new_spec_types);
1731 /* DECL is an explicit specialization. Replicate default arguments
1732 from the template it specializes. (That way, code like:
1734 template <class T> void f(T = 3);
1735 template <> void f(double);
1736 void g () { f (); }
1738 works, as required.) An alternative approach would be to look up
1739 the correct default arguments at the call-site, but this approach
1740 is consistent with how implicit instantiations are handled. */
1742 static void
1743 copy_default_args_to_explicit_spec (tree decl)
1745 tree tmpl;
1746 tree spec_types;
1747 tree tmpl_types;
1748 tree new_spec_types;
1749 tree old_type;
1750 tree new_type;
1751 tree t;
1752 tree object_type = NULL_TREE;
1753 tree in_charge = NULL_TREE;
1754 tree vtt = NULL_TREE;
1756 /* See if there's anything we need to do. */
1757 tmpl = DECL_TI_TEMPLATE (decl);
1758 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1759 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1760 if (TREE_PURPOSE (t))
1761 break;
1762 if (!t)
1763 return;
1765 old_type = TREE_TYPE (decl);
1766 spec_types = TYPE_ARG_TYPES (old_type);
1768 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1770 /* Remove the this pointer, but remember the object's type for
1771 CV quals. */
1772 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1773 spec_types = TREE_CHAIN (spec_types);
1774 tmpl_types = TREE_CHAIN (tmpl_types);
1776 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1778 /* DECL may contain more parameters than TMPL due to the extra
1779 in-charge parameter in constructors and destructors. */
1780 in_charge = spec_types;
1781 spec_types = TREE_CHAIN (spec_types);
1783 if (DECL_HAS_VTT_PARM_P (decl))
1785 vtt = spec_types;
1786 spec_types = TREE_CHAIN (spec_types);
1790 /* Compute the merged default arguments. */
1791 new_spec_types =
1792 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1794 /* Compute the new FUNCTION_TYPE. */
1795 if (object_type)
1797 if (vtt)
1798 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1799 TREE_VALUE (vtt),
1800 new_spec_types);
1802 if (in_charge)
1803 /* Put the in-charge parameter back. */
1804 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1805 TREE_VALUE (in_charge),
1806 new_spec_types);
1808 new_type = build_method_type_directly (object_type,
1809 TREE_TYPE (old_type),
1810 new_spec_types);
1812 else
1813 new_type = build_function_type (TREE_TYPE (old_type),
1814 new_spec_types);
1815 new_type = cp_build_type_attribute_variant (new_type,
1816 TYPE_ATTRIBUTES (old_type));
1817 new_type = build_exception_variant (new_type,
1818 TYPE_RAISES_EXCEPTIONS (old_type));
1819 TREE_TYPE (decl) = new_type;
1822 /* Check to see if the function just declared, as indicated in
1823 DECLARATOR, and in DECL, is a specialization of a function
1824 template. We may also discover that the declaration is an explicit
1825 instantiation at this point.
1827 Returns DECL, or an equivalent declaration that should be used
1828 instead if all goes well. Issues an error message if something is
1829 amiss. Returns error_mark_node if the error is not easily
1830 recoverable.
1832 FLAGS is a bitmask consisting of the following flags:
1834 2: The function has a definition.
1835 4: The function is a friend.
1837 The TEMPLATE_COUNT is the number of references to qualifying
1838 template classes that appeared in the name of the function. For
1839 example, in
1841 template <class T> struct S { void f(); };
1842 void S<int>::f();
1844 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1845 classes are not counted in the TEMPLATE_COUNT, so that in
1847 template <class T> struct S {};
1848 template <> struct S<int> { void f(); }
1849 template <> void S<int>::f();
1851 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1852 invalid; there should be no template <>.)
1854 If the function is a specialization, it is marked as such via
1855 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1856 is set up correctly, and it is added to the list of specializations
1857 for that template. */
1859 tree
1860 check_explicit_specialization (tree declarator,
1861 tree decl,
1862 int template_count,
1863 int flags)
1865 int have_def = flags & 2;
1866 int is_friend = flags & 4;
1867 int specialization = 0;
1868 int explicit_instantiation = 0;
1869 int member_specialization = 0;
1870 tree ctype = DECL_CLASS_CONTEXT (decl);
1871 tree dname = DECL_NAME (decl);
1872 tmpl_spec_kind tsk;
1874 if (is_friend)
1876 if (!processing_specialization)
1877 tsk = tsk_none;
1878 else
1879 tsk = tsk_excessive_parms;
1881 else
1882 tsk = current_tmpl_spec_kind (template_count);
1884 switch (tsk)
1886 case tsk_none:
1887 if (processing_specialization)
1889 specialization = 1;
1890 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1892 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1894 if (is_friend)
1895 /* This could be something like:
1897 template <class T> void f(T);
1898 class S { friend void f<>(int); } */
1899 specialization = 1;
1900 else
1902 /* This case handles bogus declarations like template <>
1903 template <class T> void f<int>(); */
1905 error ("template-id %qD in declaration of primary template",
1906 declarator);
1907 return decl;
1910 break;
1912 case tsk_invalid_member_spec:
1913 /* The error has already been reported in
1914 check_specialization_scope. */
1915 return error_mark_node;
1917 case tsk_invalid_expl_inst:
1918 error ("template parameter list used in explicit instantiation");
1920 /* Fall through. */
1922 case tsk_expl_inst:
1923 if (have_def)
1924 error ("definition provided for explicit instantiation");
1926 explicit_instantiation = 1;
1927 break;
1929 case tsk_excessive_parms:
1930 case tsk_insufficient_parms:
1931 if (tsk == tsk_excessive_parms)
1932 error ("too many template parameter lists in declaration of %qD",
1933 decl);
1934 else if (template_header_count)
1935 error("too few template parameter lists in declaration of %qD", decl);
1936 else
1937 error("explicit specialization of %qD must be introduced by "
1938 "%<template <>%>", decl);
1940 /* Fall through. */
1941 case tsk_expl_spec:
1942 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1943 if (ctype)
1944 member_specialization = 1;
1945 else
1946 specialization = 1;
1947 break;
1949 case tsk_template:
1950 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1952 /* This case handles bogus declarations like template <>
1953 template <class T> void f<int>(); */
1955 if (uses_template_parms (declarator))
1956 error ("function template partial specialization %qD "
1957 "is not allowed", declarator);
1958 else
1959 error ("template-id %qD in declaration of primary template",
1960 declarator);
1961 return decl;
1964 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1965 /* This is a specialization of a member template, without
1966 specialization the containing class. Something like:
1968 template <class T> struct S {
1969 template <class U> void f (U);
1971 template <> template <class U> void S<int>::f(U) {}
1973 That's a specialization -- but of the entire template. */
1974 specialization = 1;
1975 break;
1977 default:
1978 gcc_unreachable ();
1981 if (specialization || member_specialization)
1983 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1984 for (; t; t = TREE_CHAIN (t))
1985 if (TREE_PURPOSE (t))
1987 pedwarn
1988 ("default argument specified in explicit specialization");
1989 break;
1993 if (specialization || member_specialization || explicit_instantiation)
1995 tree tmpl = NULL_TREE;
1996 tree targs = NULL_TREE;
1998 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1999 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2001 tree fns;
2003 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2004 if (ctype)
2005 fns = dname;
2006 else
2008 /* If there is no class context, the explicit instantiation
2009 must be at namespace scope. */
2010 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2012 /* Find the namespace binding, using the declaration
2013 context. */
2014 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2015 false, true);
2016 if (fns == error_mark_node || !is_overloaded_fn (fns))
2018 error ("%qD is not a template function", dname);
2019 fns = error_mark_node;
2021 else
2023 tree fn = OVL_CURRENT (fns);
2024 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2025 CP_DECL_CONTEXT (fn)))
2026 error ("%qD is not declared in %qD",
2027 decl, current_namespace);
2031 declarator = lookup_template_function (fns, NULL_TREE);
2034 if (declarator == error_mark_node)
2035 return error_mark_node;
2037 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2039 if (!explicit_instantiation)
2040 /* A specialization in class scope. This is invalid,
2041 but the error will already have been flagged by
2042 check_specialization_scope. */
2043 return error_mark_node;
2044 else
2046 /* It's not valid to write an explicit instantiation in
2047 class scope, e.g.:
2049 class C { template void f(); }
2051 This case is caught by the parser. However, on
2052 something like:
2054 template class C { void f(); };
2056 (which is invalid) we can get here. The error will be
2057 issued later. */
2061 return decl;
2063 else if (ctype != NULL_TREE
2064 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2065 IDENTIFIER_NODE))
2067 /* Find the list of functions in ctype that have the same
2068 name as the declared function. */
2069 tree name = TREE_OPERAND (declarator, 0);
2070 tree fns = NULL_TREE;
2071 int idx;
2073 if (constructor_name_p (name, ctype))
2075 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2077 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
2078 : !CLASSTYPE_DESTRUCTORS (ctype))
2080 /* From [temp.expl.spec]:
2082 If such an explicit specialization for the member
2083 of a class template names an implicitly-declared
2084 special member function (clause _special_), the
2085 program is ill-formed.
2087 Similar language is found in [temp.explicit]. */
2088 error ("specialization of implicitly-declared special member function");
2089 return error_mark_node;
2092 name = is_constructor ? ctor_identifier : dtor_identifier;
2095 if (!DECL_CONV_FN_P (decl))
2097 idx = lookup_fnfields_1 (ctype, name);
2098 if (idx >= 0)
2099 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2101 else
2103 VEC(tree,gc) *methods;
2104 tree ovl;
2106 /* For a type-conversion operator, we cannot do a
2107 name-based lookup. We might be looking for `operator
2108 int' which will be a specialization of `operator T'.
2109 So, we find *all* the conversion operators, and then
2110 select from them. */
2111 fns = NULL_TREE;
2113 methods = CLASSTYPE_METHOD_VEC (ctype);
2114 if (methods)
2115 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2116 VEC_iterate (tree, methods, idx, ovl);
2117 ++idx)
2119 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2120 /* There are no more conversion functions. */
2121 break;
2123 /* Glue all these conversion functions together
2124 with those we already have. */
2125 for (; ovl; ovl = OVL_NEXT (ovl))
2126 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2130 if (fns == NULL_TREE)
2132 error ("no member function %qD declared in %qT", name, ctype);
2133 return error_mark_node;
2135 else
2136 TREE_OPERAND (declarator, 0) = fns;
2139 /* Figure out what exactly is being specialized at this point.
2140 Note that for an explicit instantiation, even one for a
2141 member function, we cannot tell apriori whether the
2142 instantiation is for a member template, or just a member
2143 function of a template class. Even if a member template is
2144 being instantiated, the member template arguments may be
2145 elided if they can be deduced from the rest of the
2146 declaration. */
2147 tmpl = determine_specialization (declarator, decl,
2148 &targs,
2149 member_specialization,
2150 template_count,
2151 tsk);
2153 if (!tmpl || tmpl == error_mark_node)
2154 /* We couldn't figure out what this declaration was
2155 specializing. */
2156 return error_mark_node;
2157 else
2159 tree gen_tmpl = most_general_template (tmpl);
2161 if (explicit_instantiation)
2163 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2164 is done by do_decl_instantiation later. */
2166 int arg_depth = TMPL_ARGS_DEPTH (targs);
2167 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2169 if (arg_depth > parm_depth)
2171 /* If TMPL is not the most general template (for
2172 example, if TMPL is a friend template that is
2173 injected into namespace scope), then there will
2174 be too many levels of TARGS. Remove some of them
2175 here. */
2176 int i;
2177 tree new_targs;
2179 new_targs = make_tree_vec (parm_depth);
2180 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2181 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2182 = TREE_VEC_ELT (targs, i);
2183 targs = new_targs;
2186 return instantiate_template (tmpl, targs, tf_error);
2189 /* If we thought that the DECL was a member function, but it
2190 turns out to be specializing a static member function,
2191 make DECL a static member function as well. */
2192 if (DECL_STATIC_FUNCTION_P (tmpl)
2193 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2194 revert_static_member_fn (decl);
2196 /* If this is a specialization of a member template of a
2197 template class, we want to return the TEMPLATE_DECL, not
2198 the specialization of it. */
2199 if (tsk == tsk_template)
2201 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2202 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
2203 if (have_def)
2205 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2206 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
2207 = DECL_SOURCE_LOCATION (decl);
2208 /* We want to use the argument list specified in the
2209 definition, not in the original declaration. */
2210 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
2211 = DECL_ARGUMENTS (decl);
2213 return tmpl;
2216 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2217 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2219 /* Inherit default function arguments from the template
2220 DECL is specializing. */
2221 copy_default_args_to_explicit_spec (decl);
2223 /* This specialization has the same protection as the
2224 template it specializes. */
2225 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2226 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2228 /* 7.1.1-1 [dcl.stc]
2230 A storage-class-specifier shall not be specified in an
2231 explicit specialization...
2233 The parser rejects these, so unless action is taken here,
2234 explicit function specializations will always appear with
2235 global linkage.
2237 The action recommended by the C++ CWG in response to C++
2238 defect report 605 is to make the storage class and linkage
2239 of the explicit specialization match the templated function:
2241 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2243 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2245 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2246 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2248 /* This specialization has the same linkage and visibility as
2249 the function template it specializes. */
2250 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2251 if (! TREE_PUBLIC (decl))
2253 DECL_INTERFACE_KNOWN (decl) = 1;
2254 DECL_NOT_REALLY_EXTERN (decl) = 1;
2256 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2257 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2259 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2260 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2264 /* If DECL is a friend declaration, declared using an
2265 unqualified name, the namespace associated with DECL may
2266 have been set incorrectly. For example, in:
2268 template <typename T> void f(T);
2269 namespace N {
2270 struct S { friend void f<int>(int); }
2273 we will have set the DECL_CONTEXT for the friend
2274 declaration to N, rather than to the global namespace. */
2275 if (DECL_NAMESPACE_SCOPE_P (decl))
2276 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2278 if (is_friend && !have_def)
2279 /* This is not really a declaration of a specialization.
2280 It's just the name of an instantiation. But, it's not
2281 a request for an instantiation, either. */
2282 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2283 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2284 /* This is indeed a specialization. In case of constructors
2285 and destructors, we need in-charge and not-in-charge
2286 versions in V3 ABI. */
2287 clone_function_decl (decl, /*update_method_vec_p=*/0);
2289 /* Register this specialization so that we can find it
2290 again. */
2291 decl = register_specialization (decl, gen_tmpl, targs, is_friend);
2295 return decl;
2298 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2299 parameters. These are represented in the same format used for
2300 DECL_TEMPLATE_PARMS. */
2303 comp_template_parms (const_tree parms1, const_tree parms2)
2305 const_tree p1;
2306 const_tree p2;
2308 if (parms1 == parms2)
2309 return 1;
2311 for (p1 = parms1, p2 = parms2;
2312 p1 != NULL_TREE && p2 != NULL_TREE;
2313 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2315 tree t1 = TREE_VALUE (p1);
2316 tree t2 = TREE_VALUE (p2);
2317 int i;
2319 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2320 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2322 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2323 return 0;
2325 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2327 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2328 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2330 /* If either of the template parameters are invalid, assume
2331 they match for the sake of error recovery. */
2332 if (parm1 == error_mark_node || parm2 == error_mark_node)
2333 return 1;
2335 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2336 return 0;
2338 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2339 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2340 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2341 continue;
2342 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2343 return 0;
2347 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2348 /* One set of parameters has more parameters lists than the
2349 other. */
2350 return 0;
2352 return 1;
2355 /* Determine whether PARM is a parameter pack. */
2356 bool
2357 template_parameter_pack_p (const_tree parm)
2359 /* Determine if we have a non-type template parameter pack. */
2360 if (TREE_CODE (parm) == PARM_DECL)
2361 return (DECL_TEMPLATE_PARM_P (parm)
2362 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2364 /* If this is a list of template parameters, we could get a
2365 TYPE_DECL or a TEMPLATE_DECL. */
2366 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2367 parm = TREE_TYPE (parm);
2369 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2370 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2371 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2374 /* Determine whether ARGS describes a variadic template args list,
2375 i.e., one that is terminated by a template argument pack. */
2376 static bool
2377 template_args_variadic_p (tree args)
2379 int nargs;
2380 tree last_parm;
2382 if (args == NULL_TREE)
2383 return false;
2385 args = INNERMOST_TEMPLATE_ARGS (args);
2386 nargs = TREE_VEC_LENGTH (args);
2388 if (nargs == 0)
2389 return false;
2391 last_parm = TREE_VEC_ELT (args, nargs - 1);
2393 return ARGUMENT_PACK_P (last_parm);
2396 /* Generate a new name for the parameter pack name NAME (an
2397 IDENTIFIER_NODE) that incorporates its */
2398 static tree
2399 make_ith_pack_parameter_name (tree name, int i)
2401 /* Munge the name to include the parameter index. */
2402 char numbuf[128];
2403 char* newname;
2405 sprintf(numbuf, "%i", i);
2406 newname = (char*)alloca (IDENTIFIER_LENGTH (name) + strlen(numbuf) + 2);
2407 sprintf(newname, "%s#%i", IDENTIFIER_POINTER (name), i);
2408 return get_identifier (newname);
2411 /* Structure used to track the progress of find_parameter_pack_r. */
2412 struct find_parameter_pack_data
2414 tree* parameter_packs;
2415 struct pointer_set_t *visited;
2418 /* Identifiers all of the argument packs that occur in a template
2419 argument and appends them to the TREE_LIST inside DATA, which is a
2420 find_parameter_pack_Data structure. This is a subroutine of
2421 make_pack_expansion and uses_parameter_packs. */
2422 static tree
2423 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2425 tree t = *tp;
2426 struct find_parameter_pack_data* ppd =
2427 (struct find_parameter_pack_data*)data;
2429 if (TYPE_P (t))
2431 tree context = TYPE_CONTEXT (t);
2432 cp_walk_tree (&context, &find_parameter_packs_r, ppd, ppd->visited);
2435 /* This switch statement will return immediately if we don't find a
2436 parameter pack. */
2437 switch (TREE_CODE (t))
2439 case TEMPLATE_PARM_INDEX:
2440 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2441 break;
2442 return NULL_TREE;
2444 case BOUND_TEMPLATE_TEMPLATE_PARM:
2445 /* Check the template arguments. */
2446 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2447 ppd->visited);
2449 /* Dig out the underlying TEMPLATE_TEMPLATE_PARM. */
2450 t = TYPE_TI_TEMPLATE (t);
2451 if (DECL_P (t) && TREE_TYPE (t))
2452 t = TREE_TYPE (t);
2453 *walk_subtrees = 0;
2455 /* Fall through. */
2457 case TEMPLATE_TYPE_PARM:
2458 case TEMPLATE_TEMPLATE_PARM:
2459 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2460 break;
2461 return NULL_TREE;
2463 case PARM_DECL:
2464 if (FUNCTION_PARAMETER_PACK_P (t))
2466 /* We don't want to walk into the type of a PARM_DECL,
2467 because we don't want to see the type parameter pack.*/
2468 *walk_subtrees = 0;
2469 break;
2471 return NULL_TREE;
2473 case RECORD_TYPE:
2474 if (TYPE_PTRMEMFUNC_P (t))
2475 return NULL_TREE;
2476 /* Fall through. */
2478 case UNION_TYPE:
2479 case ENUMERAL_TYPE:
2480 if (TYPE_TEMPLATE_INFO (t))
2482 tree args = TREE_VALUE (TYPE_TEMPLATE_INFO (t));
2483 cp_walk_tree (&args, &find_parameter_packs_r, ppd, ppd->visited);
2486 *walk_subtrees = 0;
2487 return NULL_TREE;
2489 case TEMPLATE_DECL:
2490 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
2491 && TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (t)))
2492 break;
2494 *walk_subtrees = 0;
2495 return NULL_TREE;
2497 case TYPE_PACK_EXPANSION:
2498 case EXPR_PACK_EXPANSION:
2499 *walk_subtrees = 0;
2500 return NULL_TREE;
2502 case INTEGER_TYPE:
2503 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
2504 ppd, ppd->visited);
2505 *walk_subtrees = 0;
2506 return NULL_TREE;
2508 default:
2509 return NULL_TREE;
2512 /* Add this parameter pack to the list. */
2513 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2515 return NULL_TREE;
2518 /* Determines if the expression or type T uses any parameter packs. */
2519 bool
2520 uses_parameter_packs (tree t)
2522 tree parameter_packs = NULL_TREE;
2523 struct find_parameter_pack_data ppd;
2524 ppd.parameter_packs = &parameter_packs;
2525 ppd.visited = pointer_set_create ();
2526 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
2527 pointer_set_destroy (ppd.visited);
2528 return parameter_packs != NULL_TREE;
2531 /* Turn ARG, which may be an expression, type, or a TREE_LIST
2532 representation a base-class initializer into a parameter pack
2533 expansion. If all goes well, the resulting node will be an
2534 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
2535 respectively. */
2536 tree
2537 make_pack_expansion (tree arg)
2539 tree result;
2540 tree parameter_packs = NULL_TREE;
2541 bool for_types = false;
2542 struct find_parameter_pack_data ppd;
2544 if (!arg || arg == error_mark_node)
2545 return arg;
2547 if (TREE_CODE (arg) == TREE_LIST)
2549 /* The only time we will see a TREE_LIST here is for a base
2550 class initializer. In this case, the TREE_PURPOSE will be a
2551 _TYPE node (representing the base class expansion we're
2552 initializing) and the TREE_VALUE will be a TREE_LIST
2553 containing the initialization arguments.
2555 The resulting expansion looks somewhat different from most
2556 expansions. Rather than returning just one _EXPANSION, we
2557 return a TREE_LIST whose TREE_PURPOSE is a
2558 TYPE_PACK_EXPANSION containing the bases that will be
2559 initialized. The TREE_VALUE will be identical to the
2560 original TREE_VALUE, which is a list of arguments that will
2561 be passed to each base. We do not introduce any new pack
2562 expansion nodes into the TREE_VALUE (although it is possible
2563 that some already exist), because the TREE_PURPOSE and
2564 TREE_VALUE all need to be expanded together with the same
2565 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
2566 resulting TREE_PURPOSE will mention the parameter packs in
2567 both the bases and the arguments to the bases. */
2568 tree purpose;
2569 tree value;
2570 tree parameter_packs = NULL_TREE;
2572 /* Determine which parameter packs will be used by the base
2573 class expansion. */
2574 ppd.visited = pointer_set_create ();
2575 ppd.parameter_packs = &parameter_packs;
2576 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
2577 &ppd, ppd.visited);
2579 if (parameter_packs == NULL_TREE)
2581 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
2582 pointer_set_destroy (ppd.visited);
2583 return error_mark_node;
2586 if (TREE_VALUE (arg) != void_type_node)
2588 /* Collect the sets of parameter packs used in each of the
2589 initialization arguments. */
2590 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
2592 /* Determine which parameter packs will be expanded in this
2593 argument. */
2594 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
2595 &ppd, ppd.visited);
2599 pointer_set_destroy (ppd.visited);
2601 /* Create the pack expansion type for the base type. */
2602 purpose = make_node (TYPE_PACK_EXPANSION);
2603 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
2604 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
2606 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2607 they will rarely be compared to anything. */
2608 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
2610 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
2613 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
2614 for_types = true;
2616 /* Build the PACK_EXPANSION_* node. */
2617 result = make_node (for_types ? TYPE_PACK_EXPANSION : EXPR_PACK_EXPANSION);
2618 SET_PACK_EXPANSION_PATTERN (result, arg);
2619 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
2621 /* Propagate type and const-expression information. */
2622 TREE_TYPE (result) = TREE_TYPE (arg);
2623 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
2625 else
2626 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2627 they will rarely be compared to anything. */
2628 SET_TYPE_STRUCTURAL_EQUALITY (result);
2630 /* Determine which parameter packs will be expanded. */
2631 ppd.parameter_packs = &parameter_packs;
2632 ppd.visited = pointer_set_create ();
2633 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
2634 pointer_set_destroy (ppd.visited);
2636 /* Make sure we found some parameter packs. */
2637 if (parameter_packs == NULL_TREE)
2639 if (TYPE_P (arg))
2640 error ("expansion pattern %<%T%> contains no argument packs", arg);
2641 else
2642 error ("expansion pattern %<%E%> contains no argument packs", arg);
2643 return error_mark_node;
2645 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
2647 return result;
2650 /* Checks T for any "bare" parameter packs, which have not yet been
2651 expanded, and issues an error if any are found. This operation can
2652 only be done on full expressions or types (e.g., an expression
2653 statement, "if" condition, etc.), because we could have expressions like:
2655 foo(f(g(h(args)))...)
2657 where "args" is a parameter pack. check_for_bare_parameter_packs
2658 should not be called for the subexpressions args, h(args),
2659 g(h(args)), or f(g(h(args))), because we would produce erroneous
2660 error messages.
2662 Returns TRUE if there were no bare parameter packs, returns FALSE
2663 (and emits an error) if there were bare parameter packs.*/
2664 bool
2665 check_for_bare_parameter_packs (tree t)
2667 tree parameter_packs = NULL_TREE;
2668 struct find_parameter_pack_data ppd;
2670 if (!processing_template_decl || !t || t == error_mark_node)
2671 return true;
2673 if (TREE_CODE (t) == TYPE_DECL)
2674 t = TREE_TYPE (t);
2676 ppd.parameter_packs = &parameter_packs;
2677 ppd.visited = pointer_set_create ();
2678 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
2679 pointer_set_destroy (ppd.visited);
2681 if (parameter_packs)
2683 error ("parameter packs not expanded with `...':");
2684 while (parameter_packs)
2686 tree pack = TREE_VALUE (parameter_packs);
2687 tree name = NULL_TREE;
2689 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
2690 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
2691 name = TYPE_NAME (pack);
2692 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
2693 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
2694 else
2695 name = DECL_NAME (pack);
2696 inform (" %qD", name);
2698 parameter_packs = TREE_CHAIN (parameter_packs);
2701 return false;
2704 return true;
2707 /* Expand any parameter packs that occur in the template arguments in
2708 ARGS. */
2709 tree
2710 expand_template_argument_pack (tree args)
2712 tree result_args = NULL_TREE;
2713 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
2714 int num_result_args = -1;
2716 /* First, determine if we need to expand anything, and the number of
2717 slots we'll need. */
2718 for (in_arg = 0; in_arg < nargs; ++in_arg)
2720 tree arg = TREE_VEC_ELT (args, in_arg);
2721 if (ARGUMENT_PACK_P (arg))
2723 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
2724 if (num_result_args < 0)
2725 num_result_args = in_arg + num_packed;
2726 else
2727 num_result_args += num_packed;
2729 else
2731 if (num_result_args >= 0)
2732 num_result_args++;
2736 /* If no expansion is necessary, we're done. */
2737 if (num_result_args < 0)
2738 return args;
2740 /* Expand arguments. */
2741 result_args = make_tree_vec (num_result_args);
2742 for (in_arg = 0; in_arg < nargs; ++in_arg)
2744 tree arg = TREE_VEC_ELT (args, in_arg);
2745 if (ARGUMENT_PACK_P (arg))
2747 tree packed = ARGUMENT_PACK_ARGS (arg);
2748 int i, num_packed = TREE_VEC_LENGTH (packed);
2749 for (i = 0; i < num_packed; ++i, ++out_arg)
2750 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
2752 else
2754 TREE_VEC_ELT (result_args, out_arg) = arg;
2755 ++out_arg;
2759 return result_args;
2762 /* Complain if DECL shadows a template parameter.
2764 [temp.local]: A template-parameter shall not be redeclared within its
2765 scope (including nested scopes). */
2767 void
2768 check_template_shadow (tree decl)
2770 tree olddecl;
2772 /* If we're not in a template, we can't possibly shadow a template
2773 parameter. */
2774 if (!current_template_parms)
2775 return;
2777 /* Figure out what we're shadowing. */
2778 if (TREE_CODE (decl) == OVERLOAD)
2779 decl = OVL_CURRENT (decl);
2780 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
2782 /* If there's no previous binding for this name, we're not shadowing
2783 anything, let alone a template parameter. */
2784 if (!olddecl)
2785 return;
2787 /* If we're not shadowing a template parameter, we're done. Note
2788 that OLDDECL might be an OVERLOAD (or perhaps even an
2789 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2790 node. */
2791 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2792 return;
2794 /* We check for decl != olddecl to avoid bogus errors for using a
2795 name inside a class. We check TPFI to avoid duplicate errors for
2796 inline member templates. */
2797 if (decl == olddecl
2798 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2799 return;
2801 error ("declaration of %q+#D", decl);
2802 error (" shadows template parm %q+#D", olddecl);
2805 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2806 ORIG_LEVEL, DECL, and TYPE. */
2808 static tree
2809 build_template_parm_index (int index,
2810 int level,
2811 int orig_level,
2812 tree decl,
2813 tree type)
2815 tree t = make_node (TEMPLATE_PARM_INDEX);
2816 TEMPLATE_PARM_IDX (t) = index;
2817 TEMPLATE_PARM_LEVEL (t) = level;
2818 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2819 TEMPLATE_PARM_DECL (t) = decl;
2820 TREE_TYPE (t) = type;
2821 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2822 TREE_INVARIANT (t) = TREE_INVARIANT (decl);
2823 TREE_READONLY (t) = TREE_READONLY (decl);
2825 return t;
2828 /* Find the canonical type parameter for the given template type
2829 parameter. Returns the canonical type parameter, which may be TYPE
2830 if no such parameter existed. */
2831 static tree
2832 canonical_type_parameter (tree type)
2834 tree list;
2835 int idx = TEMPLATE_TYPE_IDX (type);
2836 if (!canonical_template_parms)
2837 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
2839 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
2840 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
2842 list = VEC_index (tree, canonical_template_parms, idx);
2843 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
2844 list = TREE_CHAIN (list);
2846 if (list)
2847 return TREE_VALUE (list);
2848 else
2850 VEC_replace(tree, canonical_template_parms, idx,
2851 tree_cons (NULL_TREE, type,
2852 VEC_index (tree, canonical_template_parms, idx)));
2853 return type;
2857 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2858 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2859 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2860 new one is created. */
2862 static tree
2863 reduce_template_parm_level (tree index, tree type, int levels)
2865 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2866 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2867 != TEMPLATE_PARM_LEVEL (index) - levels))
2869 tree orig_decl = TEMPLATE_PARM_DECL (index);
2870 tree decl, t;
2872 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2873 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2874 TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
2875 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2876 DECL_ARTIFICIAL (decl) = 1;
2877 SET_DECL_TEMPLATE_PARM_P (decl);
2879 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2880 TEMPLATE_PARM_LEVEL (index) - levels,
2881 TEMPLATE_PARM_ORIG_LEVEL (index),
2882 decl, type);
2883 TEMPLATE_PARM_DESCENDANTS (index) = t;
2884 TEMPLATE_PARM_PARAMETER_PACK (t)
2885 = TEMPLATE_PARM_PARAMETER_PACK (index);
2887 /* Template template parameters need this. */
2888 if (TREE_CODE (decl) != CONST_DECL)
2889 DECL_TEMPLATE_PARMS (decl)
2890 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
2893 return TEMPLATE_PARM_DESCENDANTS (index);
2896 /* Process information from new template parameter PARM and append it to the
2897 LIST being built. This new parameter is a non-type parameter iff
2898 IS_NON_TYPE is true. This new parameter is a parameter
2899 pack iff IS_PARAMETER_PACK is true. */
2901 tree
2902 process_template_parm (tree list, tree parm, bool is_non_type,
2903 bool is_parameter_pack)
2905 tree decl = 0;
2906 tree defval;
2907 tree err_parm_list;
2908 int idx = 0;
2910 gcc_assert (TREE_CODE (parm) == TREE_LIST);
2911 defval = TREE_PURPOSE (parm);
2913 if (list)
2915 tree p = tree_last (list);
2917 if (p && TREE_VALUE (p) != error_mark_node)
2919 p = TREE_VALUE (p);
2920 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2921 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2922 else
2923 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2926 ++idx;
2928 else
2929 idx = 0;
2931 if (is_non_type)
2933 parm = TREE_VALUE (parm);
2935 SET_DECL_TEMPLATE_PARM_P (parm);
2937 if (TREE_TYPE (parm) == error_mark_node)
2939 err_parm_list = build_tree_list (defval, parm);
2940 TREE_VALUE (err_parm_list) = error_mark_node;
2941 return chainon (list, err_parm_list);
2943 else
2945 /* [temp.param]
2947 The top-level cv-qualifiers on the template-parameter are
2948 ignored when determining its type. */
2949 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2950 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2952 err_parm_list = build_tree_list (defval, parm);
2953 TREE_VALUE (err_parm_list) = error_mark_node;
2954 return chainon (list, err_parm_list);
2957 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
2959 /* This template parameter is not a parameter pack, but it
2960 should be. Complain about "bare" parameter packs. */
2961 check_for_bare_parameter_packs (TREE_TYPE (parm));
2963 /* Recover by calling this a parameter pack. */
2964 is_parameter_pack = true;
2968 /* A template parameter is not modifiable. */
2969 TREE_CONSTANT (parm) = 1;
2970 TREE_INVARIANT (parm) = 1;
2971 TREE_READONLY (parm) = 1;
2972 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2973 TREE_CONSTANT (decl) = 1;
2974 TREE_INVARIANT (decl) = 1;
2975 TREE_READONLY (decl) = 1;
2976 DECL_INITIAL (parm) = DECL_INITIAL (decl)
2977 = build_template_parm_index (idx, processing_template_decl,
2978 processing_template_decl,
2979 decl, TREE_TYPE (parm));
2981 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
2982 = is_parameter_pack;
2984 else
2986 tree t;
2987 parm = TREE_VALUE (TREE_VALUE (parm));
2989 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2991 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2992 /* This is for distinguishing between real templates and template
2993 template parameters */
2994 TREE_TYPE (parm) = t;
2995 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2996 decl = parm;
2998 else
3000 t = make_aggr_type (TEMPLATE_TYPE_PARM);
3001 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3002 decl = build_decl (TYPE_DECL, parm, t);
3005 TYPE_NAME (t) = decl;
3006 TYPE_STUB_DECL (t) = decl;
3007 parm = decl;
3008 TEMPLATE_TYPE_PARM_INDEX (t)
3009 = build_template_parm_index (idx, processing_template_decl,
3010 processing_template_decl,
3011 decl, TREE_TYPE (parm));
3012 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3013 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3015 DECL_ARTIFICIAL (decl) = 1;
3016 SET_DECL_TEMPLATE_PARM_P (decl);
3017 pushdecl (decl);
3018 parm = build_tree_list (defval, parm);
3019 return chainon (list, parm);
3022 /* The end of a template parameter list has been reached. Process the
3023 tree list into a parameter vector, converting each parameter into a more
3024 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3025 as PARM_DECLs. */
3027 tree
3028 end_template_parm_list (tree parms)
3030 int nparms;
3031 tree parm, next;
3032 tree saved_parmlist = make_tree_vec (list_length (parms));
3034 current_template_parms
3035 = tree_cons (size_int (processing_template_decl),
3036 saved_parmlist, current_template_parms);
3038 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3040 next = TREE_CHAIN (parm);
3041 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3042 TREE_CHAIN (parm) = NULL_TREE;
3045 --processing_template_parmlist;
3047 return saved_parmlist;
3050 /* end_template_decl is called after a template declaration is seen. */
3052 void
3053 end_template_decl (void)
3055 reset_specialization ();
3057 if (! processing_template_decl)
3058 return;
3060 /* This matches the pushlevel in begin_template_parm_list. */
3061 finish_scope ();
3063 --processing_template_decl;
3064 current_template_parms = TREE_CHAIN (current_template_parms);
3067 /* Within the declaration of a template, return all levels of template
3068 parameters that apply. The template parameters are represented as
3069 a TREE_VEC, in the form documented in cp-tree.h for template
3070 arguments. */
3072 static tree
3073 current_template_args (void)
3075 tree header;
3076 tree args = NULL_TREE;
3077 int length = TMPL_PARMS_DEPTH (current_template_parms);
3078 int l = length;
3080 /* If there is only one level of template parameters, we do not
3081 create a TREE_VEC of TREE_VECs. Instead, we return a single
3082 TREE_VEC containing the arguments. */
3083 if (length > 1)
3084 args = make_tree_vec (length);
3086 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3088 tree a = copy_node (TREE_VALUE (header));
3089 int i;
3091 TREE_TYPE (a) = NULL_TREE;
3092 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3094 tree t = TREE_VEC_ELT (a, i);
3096 /* T will be a list if we are called from within a
3097 begin/end_template_parm_list pair, but a vector directly
3098 if within a begin/end_member_template_processing pair. */
3099 if (TREE_CODE (t) == TREE_LIST)
3101 t = TREE_VALUE (t);
3103 if (t != error_mark_node)
3105 if (TREE_CODE (t) == TYPE_DECL
3106 || TREE_CODE (t) == TEMPLATE_DECL)
3108 t = TREE_TYPE (t);
3110 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3112 /* Turn this argument into a TYPE_ARGUMENT_PACK
3113 with a single element, which expands T. */
3114 tree vec = make_tree_vec (1);
3115 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3117 t = make_node (TYPE_ARGUMENT_PACK);
3118 SET_ARGUMENT_PACK_ARGS (t, vec);
3121 else
3123 t = DECL_INITIAL (t);
3125 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3127 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3128 with a single element, which expands T. */
3129 tree vec = make_tree_vec (1);
3130 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3131 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3133 t = make_node (NONTYPE_ARGUMENT_PACK);
3134 SET_ARGUMENT_PACK_ARGS (t, vec);
3135 TREE_TYPE (t) = type;
3139 TREE_VEC_ELT (a, i) = t;
3143 if (length > 1)
3144 TREE_VEC_ELT (args, --l) = a;
3145 else
3146 args = a;
3149 return args;
3152 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3153 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3154 a member template. Used by push_template_decl below. */
3156 static tree
3157 build_template_decl (tree decl, tree parms, bool member_template_p)
3159 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3160 DECL_TEMPLATE_PARMS (tmpl) = parms;
3161 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3162 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3163 if (DECL_LANG_SPECIFIC (decl))
3165 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
3166 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
3167 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
3168 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
3169 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
3170 if (DECL_OVERLOADED_OPERATOR_P (decl))
3171 SET_OVERLOADED_OPERATOR_CODE (tmpl,
3172 DECL_OVERLOADED_OPERATOR_P (decl));
3175 return tmpl;
3178 struct template_parm_data
3180 /* The level of the template parameters we are currently
3181 processing. */
3182 int level;
3184 /* The index of the specialization argument we are currently
3185 processing. */
3186 int current_arg;
3188 /* An array whose size is the number of template parameters. The
3189 elements are nonzero if the parameter has been used in any one
3190 of the arguments processed so far. */
3191 int* parms;
3193 /* An array whose size is the number of template arguments. The
3194 elements are nonzero if the argument makes use of template
3195 parameters of this level. */
3196 int* arg_uses_template_parms;
3199 /* Subroutine of push_template_decl used to see if each template
3200 parameter in a partial specialization is used in the explicit
3201 argument list. If T is of the LEVEL given in DATA (which is
3202 treated as a template_parm_data*), then DATA->PARMS is marked
3203 appropriately. */
3205 static int
3206 mark_template_parm (tree t, void* data)
3208 int level;
3209 int idx;
3210 struct template_parm_data* tpd = (struct template_parm_data*) data;
3212 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3214 level = TEMPLATE_PARM_LEVEL (t);
3215 idx = TEMPLATE_PARM_IDX (t);
3217 else
3219 level = TEMPLATE_TYPE_LEVEL (t);
3220 idx = TEMPLATE_TYPE_IDX (t);
3223 if (level == tpd->level)
3225 tpd->parms[idx] = 1;
3226 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3229 /* Return zero so that for_each_template_parm will continue the
3230 traversal of the tree; we want to mark *every* template parm. */
3231 return 0;
3234 /* Process the partial specialization DECL. */
3236 static tree
3237 process_partial_specialization (tree decl)
3239 tree type = TREE_TYPE (decl);
3240 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3241 tree specargs = CLASSTYPE_TI_ARGS (type);
3242 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3243 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3244 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3245 int nargs = TREE_VEC_LENGTH (inner_args);
3246 int ntparms = TREE_VEC_LENGTH (inner_parms);
3247 int i;
3248 int did_error_intro = 0;
3249 struct template_parm_data tpd;
3250 struct template_parm_data tpd2;
3252 /* We check that each of the template parameters given in the
3253 partial specialization is used in the argument list to the
3254 specialization. For example:
3256 template <class T> struct S;
3257 template <class T> struct S<T*>;
3259 The second declaration is OK because `T*' uses the template
3260 parameter T, whereas
3262 template <class T> struct S<int>;
3264 is no good. Even trickier is:
3266 template <class T>
3267 struct S1
3269 template <class U>
3270 struct S2;
3271 template <class U>
3272 struct S2<T>;
3275 The S2<T> declaration is actually invalid; it is a
3276 full-specialization. Of course,
3278 template <class U>
3279 struct S2<T (*)(U)>;
3281 or some such would have been OK. */
3282 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3283 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
3284 memset (tpd.parms, 0, sizeof (int) * ntparms);
3286 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
3287 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3288 for (i = 0; i < nargs; ++i)
3290 tpd.current_arg = i;
3291 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3292 &mark_template_parm,
3293 &tpd,
3294 NULL);
3296 for (i = 0; i < ntparms; ++i)
3297 if (tpd.parms[i] == 0)
3299 /* One of the template parms was not used in the
3300 specialization. */
3301 if (!did_error_intro)
3303 error ("template parameters not used in partial specialization:");
3304 did_error_intro = 1;
3307 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3310 /* [temp.class.spec]
3312 The argument list of the specialization shall not be identical to
3313 the implicit argument list of the primary template. */
3314 if (comp_template_args
3315 (inner_args,
3316 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3317 (maintmpl)))))
3318 error ("partial specialization %qT does not specialize any template arguments", type);
3320 /* [temp.class.spec]
3322 A partially specialized non-type argument expression shall not
3323 involve template parameters of the partial specialization except
3324 when the argument expression is a simple identifier.
3326 The type of a template parameter corresponding to a specialized
3327 non-type argument shall not be dependent on a parameter of the
3328 specialization.
3330 Also, we verify that pack expansions only occur at the
3331 end of the argument list. */
3332 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3333 tpd2.parms = 0;
3334 for (i = 0; i < nargs; ++i)
3336 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3337 tree arg = TREE_VEC_ELT (inner_args, i);
3338 tree packed_args = NULL_TREE;
3339 int j, len = 1;
3341 if (ARGUMENT_PACK_P (arg))
3343 /* Extract the arguments from the argument pack. We'll be
3344 iterating over these in the following loop. */
3345 packed_args = ARGUMENT_PACK_ARGS (arg);
3346 len = TREE_VEC_LENGTH (packed_args);
3349 for (j = 0; j < len; j++)
3351 if (packed_args)
3352 /* Get the Jth argument in the parameter pack. */
3353 arg = TREE_VEC_ELT (packed_args, j);
3355 if (PACK_EXPANSION_P (arg))
3357 /* Pack expansions must come at the end of the
3358 argument list. */
3359 if ((packed_args && j < len - 1)
3360 || (!packed_args && i < nargs - 1))
3362 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3363 error ("parameter pack argument %qE must be at the end of the template argument list", arg);
3364 else
3365 error ("parameter pack argument %qT must be at the end of the template argument list", arg);
3369 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3370 /* We only care about the pattern. */
3371 arg = PACK_EXPANSION_PATTERN (arg);
3373 if (/* These first two lines are the `non-type' bit. */
3374 !TYPE_P (arg)
3375 && TREE_CODE (arg) != TEMPLATE_DECL
3376 /* This next line is the `argument expression is not just a
3377 simple identifier' condition and also the `specialized
3378 non-type argument' bit. */
3379 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3381 if ((!packed_args && tpd.arg_uses_template_parms[i])
3382 || (packed_args && uses_template_parms (arg)))
3383 error ("template argument %qE involves template parameter(s)",
3384 arg);
3385 else
3387 /* Look at the corresponding template parameter,
3388 marking which template parameters its type depends
3389 upon. */
3390 tree type = TREE_TYPE (parm);
3392 if (!tpd2.parms)
3394 /* We haven't yet initialized TPD2. Do so now. */
3395 tpd2.arg_uses_template_parms
3396 = (int *) alloca (sizeof (int) * nargs);
3397 /* The number of parameters here is the number in the
3398 main template, which, as checked in the assertion
3399 above, is NARGS. */
3400 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
3401 tpd2.level =
3402 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
3405 /* Mark the template parameters. But this time, we're
3406 looking for the template parameters of the main
3407 template, not in the specialization. */
3408 tpd2.current_arg = i;
3409 tpd2.arg_uses_template_parms[i] = 0;
3410 memset (tpd2.parms, 0, sizeof (int) * nargs);
3411 for_each_template_parm (type,
3412 &mark_template_parm,
3413 &tpd2,
3414 NULL);
3416 if (tpd2.arg_uses_template_parms [i])
3418 /* The type depended on some template parameters.
3419 If they are fully specialized in the
3420 specialization, that's OK. */
3421 int j;
3422 for (j = 0; j < nargs; ++j)
3423 if (tpd2.parms[j] != 0
3424 && tpd.arg_uses_template_parms [j])
3426 error ("type %qT of template argument %qE depends "
3427 "on template parameter(s)",
3428 type,
3429 arg);
3430 break;
3438 if (retrieve_specialization (maintmpl, specargs,
3439 /*class_specializations_p=*/true))
3440 /* We've already got this specialization. */
3441 return decl;
3443 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
3444 = tree_cons (specargs, inner_parms,
3445 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
3446 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
3447 return decl;
3450 /* Check that a template declaration's use of default arguments is not
3451 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
3452 nonzero if DECL is the thing declared by a primary template.
3453 IS_PARTIAL is nonzero if DECL is a partial specialization.
3456 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3457 declaration (but not a definition); 1 indicates a declaration, 2
3458 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3459 emitted for extraneous default arguments.
3461 Returns TRUE if there were no errors found, FALSE otherwise. */
3463 bool
3464 check_default_tmpl_args (tree decl, tree parms, int is_primary,
3465 int is_partial, int is_friend_decl)
3467 const char *msg;
3468 int last_level_to_check;
3469 tree parm_level;
3470 bool no_errors = true;
3472 /* [temp.param]
3474 A default template-argument shall not be specified in a
3475 function template declaration or a function template definition, nor
3476 in the template-parameter-list of the definition of a member of a
3477 class template. */
3479 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
3480 /* You can't have a function template declaration in a local
3481 scope, nor you can you define a member of a class template in a
3482 local scope. */
3483 return true;
3485 if (current_class_type
3486 && !TYPE_BEING_DEFINED (current_class_type)
3487 && DECL_LANG_SPECIFIC (decl)
3488 /* If this is either a friend defined in the scope of the class
3489 or a member function. */
3490 && (DECL_FUNCTION_MEMBER_P (decl)
3491 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
3492 : DECL_FRIEND_CONTEXT (decl)
3493 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
3494 : false)
3495 /* And, if it was a member function, it really was defined in
3496 the scope of the class. */
3497 && (!DECL_FUNCTION_MEMBER_P (decl)
3498 || DECL_INITIALIZED_IN_CLASS_P (decl)))
3499 /* We already checked these parameters when the template was
3500 declared, so there's no need to do it again now. This function
3501 was defined in class scope, but we're processing it's body now
3502 that the class is complete. */
3503 return true;
3505 /* Core issue 226 (C++0x only): the following only applies to class
3506 templates. */
3507 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
3509 /* [temp.param]
3511 If a template-parameter has a default template-argument, all
3512 subsequent template-parameters shall have a default
3513 template-argument supplied. */
3514 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
3516 tree inner_parms = TREE_VALUE (parm_level);
3517 int ntparms = TREE_VEC_LENGTH (inner_parms);
3518 int seen_def_arg_p = 0;
3519 int i;
3521 for (i = 0; i < ntparms; ++i)
3523 tree parm = TREE_VEC_ELT (inner_parms, i);
3525 if (parm == error_mark_node)
3526 continue;
3528 if (TREE_PURPOSE (parm))
3529 seen_def_arg_p = 1;
3530 else if (seen_def_arg_p)
3532 error ("no default argument for %qD", TREE_VALUE (parm));
3533 /* For better subsequent error-recovery, we indicate that
3534 there should have been a default argument. */
3535 TREE_PURPOSE (parm) = error_mark_node;
3536 no_errors = false;
3542 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
3543 || is_partial
3544 || !is_primary
3545 || is_friend_decl)
3546 /* For an ordinary class template, default template arguments are
3547 allowed at the innermost level, e.g.:
3548 template <class T = int>
3549 struct S {};
3550 but, in a partial specialization, they're not allowed even
3551 there, as we have in [temp.class.spec]:
3553 The template parameter list of a specialization shall not
3554 contain default template argument values.
3556 So, for a partial specialization, or for a function template
3557 (in C++98/C++03), we look at all of them. */
3559 else
3560 /* But, for a primary class template that is not a partial
3561 specialization we look at all template parameters except the
3562 innermost ones. */
3563 parms = TREE_CHAIN (parms);
3565 /* Figure out what error message to issue. */
3566 if (is_friend_decl == 2)
3567 msg = "default template arguments may not be used in function template friend re-declaration";
3568 else if (is_friend_decl)
3569 msg = "default template arguments may not be used in function template friend declarations";
3570 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
3571 msg = "default template arguments may not be used in function templates";
3572 else if (is_partial)
3573 msg = "default template arguments may not be used in partial specializations";
3574 else
3575 msg = "default argument for template parameter for class enclosing %qD";
3577 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
3578 /* If we're inside a class definition, there's no need to
3579 examine the parameters to the class itself. On the one
3580 hand, they will be checked when the class is defined, and,
3581 on the other, default arguments are valid in things like:
3582 template <class T = double>
3583 struct S { template <class U> void f(U); };
3584 Here the default argument for `S' has no bearing on the
3585 declaration of `f'. */
3586 last_level_to_check = template_class_depth (current_class_type) + 1;
3587 else
3588 /* Check everything. */
3589 last_level_to_check = 0;
3591 for (parm_level = parms;
3592 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
3593 parm_level = TREE_CHAIN (parm_level))
3595 tree inner_parms = TREE_VALUE (parm_level);
3596 int i;
3597 int ntparms;
3599 ntparms = TREE_VEC_LENGTH (inner_parms);
3600 for (i = 0; i < ntparms; ++i)
3602 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
3603 continue;
3605 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
3607 if (msg)
3609 no_errors = false;
3610 if (is_friend_decl == 2)
3611 return no_errors;
3613 error (msg, decl);
3614 msg = 0;
3617 /* Clear out the default argument so that we are not
3618 confused later. */
3619 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
3623 /* At this point, if we're still interested in issuing messages,
3624 they must apply to classes surrounding the object declared. */
3625 if (msg)
3626 msg = "default argument for template parameter for class enclosing %qD";
3629 return no_errors;
3632 /* Worker for push_template_decl_real, called via
3633 for_each_template_parm. DATA is really an int, indicating the
3634 level of the parameters we are interested in. If T is a template
3635 parameter of that level, return nonzero. */
3637 static int
3638 template_parm_this_level_p (tree t, void* data)
3640 int this_level = *(int *)data;
3641 int level;
3643 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3644 level = TEMPLATE_PARM_LEVEL (t);
3645 else
3646 level = TEMPLATE_TYPE_LEVEL (t);
3647 return level == this_level;
3650 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
3651 parameters given by current_template_args, or reuses a
3652 previously existing one, if appropriate. Returns the DECL, or an
3653 equivalent one, if it is replaced via a call to duplicate_decls.
3655 If IS_FRIEND is true, DECL is a friend declaration. */
3657 tree
3658 push_template_decl_real (tree decl, bool is_friend)
3660 tree tmpl;
3661 tree args;
3662 tree info;
3663 tree ctx;
3664 int primary;
3665 int is_partial;
3666 int new_template_p = 0;
3667 /* True if the template is a member template, in the sense of
3668 [temp.mem]. */
3669 bool member_template_p = false;
3671 if (decl == error_mark_node)
3672 return decl;
3674 /* See if this is a partial specialization. */
3675 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
3676 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3677 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
3679 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
3680 is_friend = true;
3682 if (is_friend)
3683 /* For a friend, we want the context of the friend function, not
3684 the type of which it is a friend. */
3685 ctx = DECL_CONTEXT (decl);
3686 else if (CP_DECL_CONTEXT (decl)
3687 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
3688 /* In the case of a virtual function, we want the class in which
3689 it is defined. */
3690 ctx = CP_DECL_CONTEXT (decl);
3691 else
3692 /* Otherwise, if we're currently defining some class, the DECL
3693 is assumed to be a member of the class. */
3694 ctx = current_scope ();
3696 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
3697 ctx = NULL_TREE;
3699 if (!DECL_CONTEXT (decl))
3700 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
3702 /* See if this is a primary template. */
3703 if (is_friend && ctx)
3704 /* A friend template that specifies a class context, i.e.
3705 template <typename T> friend void A<T>::f();
3706 is not primary. */
3707 primary = 0;
3708 else
3709 primary = template_parm_scope_p ();
3711 if (primary)
3713 if (DECL_CLASS_SCOPE_P (decl))
3714 member_template_p = true;
3715 if (TREE_CODE (decl) == TYPE_DECL
3716 && ANON_AGGRNAME_P (DECL_NAME (decl)))
3717 error ("template class without a name");
3718 else if (TREE_CODE (decl) == FUNCTION_DECL)
3720 if (DECL_DESTRUCTOR_P (decl))
3722 /* [temp.mem]
3724 A destructor shall not be a member template. */
3725 error ("destructor %qD declared as member template", decl);
3726 return error_mark_node;
3728 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
3729 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
3730 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
3731 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
3732 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
3733 == void_list_node)))
3735 /* [basic.stc.dynamic.allocation]
3737 An allocation function can be a function
3738 template. ... Template allocation functions shall
3739 have two or more parameters. */
3740 error ("invalid template declaration of %qD", decl);
3741 return error_mark_node;
3744 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3745 && CLASS_TYPE_P (TREE_TYPE (decl)))
3746 /* OK */;
3747 else
3749 error ("template declaration of %q#D", decl);
3750 return error_mark_node;
3754 /* Check to see that the rules regarding the use of default
3755 arguments are not being violated. */
3756 check_default_tmpl_args (decl, current_template_parms,
3757 primary, is_partial, /*is_friend_decl=*/0);
3759 /* Ensure that there are no parameter packs in the type of this
3760 declaration that have not been expanded. */
3761 if (TREE_CODE (decl) == FUNCTION_DECL)
3763 /* Check each of the arguments individually to see if there are
3764 any bare parameter packs. */
3765 tree type = TREE_TYPE (decl);
3766 tree arg = DECL_ARGUMENTS (decl);
3767 tree argtype = TYPE_ARG_TYPES (type);
3769 while (arg && argtype)
3771 if (!FUNCTION_PARAMETER_PACK_P (arg)
3772 && !check_for_bare_parameter_packs (TREE_TYPE (arg)))
3774 /* This is a PARM_DECL that contains unexpanded parameter
3775 packs. We have already complained about this in the
3776 check_for_bare_parameter_packs call, so just replace
3777 these types with ERROR_MARK_NODE. */
3778 TREE_TYPE (arg) = error_mark_node;
3779 TREE_VALUE (argtype) = error_mark_node;
3782 arg = TREE_CHAIN (arg);
3783 argtype = TREE_CHAIN (argtype);
3786 /* Check for bare parameter packs in the return type and the
3787 exception specifiers. */
3788 check_for_bare_parameter_packs (TREE_TYPE (type));
3789 check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type));
3791 else
3792 check_for_bare_parameter_packs (TREE_TYPE (decl));
3794 if (is_partial)
3795 return process_partial_specialization (decl);
3797 /* A primary class template can only have one parameter pack, at the
3798 end of the template parameter list. */
3799 if (primary && TREE_CODE (decl) == TYPE_DECL)
3801 tree inner_parms
3802 = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3803 int i, len = TREE_VEC_LENGTH (inner_parms);
3804 for (i = 0; i < len - 1; i++)
3806 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
3808 if (template_parameter_pack_p (parm))
3810 if (TREE_CODE (parm) == PARM_DECL)
3811 error ("parameter pack %qE must be at the end of the"
3812 " template parameter list", parm);
3813 else
3814 error ("parameter pack %qT must be at the end of the"
3815 " template parameter list", TREE_TYPE (parm));
3820 args = current_template_args ();
3822 if (!ctx
3823 || TREE_CODE (ctx) == FUNCTION_DECL
3824 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
3825 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
3827 if (DECL_LANG_SPECIFIC (decl)
3828 && DECL_TEMPLATE_INFO (decl)
3829 && DECL_TI_TEMPLATE (decl))
3830 tmpl = DECL_TI_TEMPLATE (decl);
3831 /* If DECL is a TYPE_DECL for a class-template, then there won't
3832 be DECL_LANG_SPECIFIC. The information equivalent to
3833 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3834 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3835 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3836 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3838 /* Since a template declaration already existed for this
3839 class-type, we must be redeclaring it here. Make sure
3840 that the redeclaration is valid. */
3841 redeclare_class_template (TREE_TYPE (decl),
3842 current_template_parms);
3843 /* We don't need to create a new TEMPLATE_DECL; just use the
3844 one we already had. */
3845 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3847 else
3849 tmpl = build_template_decl (decl, current_template_parms,
3850 member_template_p);
3851 new_template_p = 1;
3853 if (DECL_LANG_SPECIFIC (decl)
3854 && DECL_TEMPLATE_SPECIALIZATION (decl))
3856 /* A specialization of a member template of a template
3857 class. */
3858 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3859 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
3860 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3864 else
3866 tree a, t, current, parms;
3867 int i;
3869 if (TREE_CODE (decl) == TYPE_DECL)
3871 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
3872 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
3873 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3874 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3875 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3876 else
3878 error ("%qD does not declare a template type", decl);
3879 return decl;
3882 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
3884 error ("template definition of non-template %q#D", decl);
3885 return decl;
3887 else
3888 tmpl = DECL_TI_TEMPLATE (decl);
3890 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3891 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
3892 && DECL_TEMPLATE_SPECIALIZATION (decl)
3893 && DECL_MEMBER_TEMPLATE_P (tmpl))
3895 tree new_tmpl;
3897 /* The declaration is a specialization of a member
3898 template, declared outside the class. Therefore, the
3899 innermost template arguments will be NULL, so we
3900 replace them with the arguments determined by the
3901 earlier call to check_explicit_specialization. */
3902 args = DECL_TI_ARGS (decl);
3904 new_tmpl
3905 = build_template_decl (decl, current_template_parms,
3906 member_template_p);
3907 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3908 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3909 DECL_TI_TEMPLATE (decl) = new_tmpl;
3910 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3911 DECL_TEMPLATE_INFO (new_tmpl)
3912 = tree_cons (tmpl, args, NULL_TREE);
3914 register_specialization (new_tmpl,
3915 most_general_template (tmpl),
3916 args,
3917 is_friend);
3918 return decl;
3921 /* Make sure the template headers we got make sense. */
3923 parms = DECL_TEMPLATE_PARMS (tmpl);
3924 i = TMPL_PARMS_DEPTH (parms);
3925 if (TMPL_ARGS_DEPTH (args) != i)
3927 error ("expected %d levels of template parms for %q#D, got %d",
3928 i, decl, TMPL_ARGS_DEPTH (args));
3930 else
3931 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
3933 a = TMPL_ARGS_LEVEL (args, i);
3934 t = INNERMOST_TEMPLATE_PARMS (parms);
3936 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
3938 if (current == decl)
3939 error ("got %d template parameters for %q#D",
3940 TREE_VEC_LENGTH (a), decl);
3941 else
3942 error ("got %d template parameters for %q#T",
3943 TREE_VEC_LENGTH (a), current);
3944 error (" but %d required", TREE_VEC_LENGTH (t));
3945 return error_mark_node;
3948 /* Perhaps we should also check that the parms are used in the
3949 appropriate qualifying scopes in the declarator? */
3951 if (current == decl)
3952 current = ctx;
3953 else
3954 current = (TYPE_P (current)
3955 ? TYPE_CONTEXT (current)
3956 : DECL_CONTEXT (current));
3960 DECL_TEMPLATE_RESULT (tmpl) = decl;
3961 TREE_TYPE (tmpl) = TREE_TYPE (decl);
3963 /* Push template declarations for global functions and types. Note
3964 that we do not try to push a global template friend declared in a
3965 template class; such a thing may well depend on the template
3966 parameters of the class. */
3967 if (new_template_p && !ctx
3968 && !(is_friend && template_class_depth (current_class_type) > 0))
3970 tmpl = pushdecl_namespace_level (tmpl, is_friend);
3971 if (tmpl == error_mark_node)
3972 return error_mark_node;
3974 /* Hide template friend classes that haven't been declared yet. */
3975 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
3977 DECL_ANTICIPATED (tmpl) = 1;
3978 DECL_FRIEND_P (tmpl) = 1;
3982 if (primary)
3984 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
3985 if (DECL_CONV_FN_P (tmpl))
3987 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3989 /* It is a conversion operator. See if the type converted to
3990 depends on innermost template operands. */
3992 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
3993 depth))
3994 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
3998 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
3999 back to its most general template. If TMPL is a specialization,
4000 ARGS may only have the innermost set of arguments. Add the missing
4001 argument levels if necessary. */
4002 if (DECL_TEMPLATE_INFO (tmpl))
4003 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4005 info = tree_cons (tmpl, args, NULL_TREE);
4007 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4008 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4009 else if (DECL_LANG_SPECIFIC (decl))
4010 DECL_TEMPLATE_INFO (decl) = info;
4012 return DECL_TEMPLATE_RESULT (tmpl);
4015 tree
4016 push_template_decl (tree decl)
4018 return push_template_decl_real (decl, false);
4021 /* Called when a class template TYPE is redeclared with the indicated
4022 template PARMS, e.g.:
4024 template <class T> struct S;
4025 template <class T> struct S {}; */
4027 bool
4028 redeclare_class_template (tree type, tree parms)
4030 tree tmpl;
4031 tree tmpl_parms;
4032 int i;
4034 if (!TYPE_TEMPLATE_INFO (type))
4036 error ("%qT is not a template type", type);
4037 return false;
4040 tmpl = TYPE_TI_TEMPLATE (type);
4041 if (!PRIMARY_TEMPLATE_P (tmpl))
4042 /* The type is nested in some template class. Nothing to worry
4043 about here; there are no new template parameters for the nested
4044 type. */
4045 return true;
4047 if (!parms)
4049 error ("template specifiers not specified in declaration of %qD",
4050 tmpl);
4051 return false;
4054 parms = INNERMOST_TEMPLATE_PARMS (parms);
4055 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4057 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4059 error ("previous declaration %q+D", tmpl);
4060 error ("used %d template parameter(s) instead of %d",
4061 TREE_VEC_LENGTH (tmpl_parms),
4062 TREE_VEC_LENGTH (parms));
4063 return false;
4066 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4068 tree tmpl_parm;
4069 tree parm;
4070 tree tmpl_default;
4071 tree parm_default;
4073 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4074 || TREE_VEC_ELT (parms, i) == error_mark_node)
4075 continue;
4077 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4078 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4079 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4080 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4082 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4083 TEMPLATE_DECL. */
4084 if (tmpl_parm != error_mark_node
4085 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4086 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4087 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))))
4089 error ("template parameter %q+#D", tmpl_parm);
4090 error ("redeclared here as %q#D", parm);
4091 return false;
4094 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4096 /* We have in [temp.param]:
4098 A template-parameter may not be given default arguments
4099 by two different declarations in the same scope. */
4100 error ("redefinition of default argument for %q#D", parm);
4101 error ("%J original definition appeared here", tmpl_parm);
4102 return false;
4105 if (parm_default != NULL_TREE)
4106 /* Update the previous template parameters (which are the ones
4107 that will really count) with the new default value. */
4108 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4109 else if (tmpl_default != NULL_TREE)
4110 /* Update the new parameters, too; they'll be used as the
4111 parameters for any members. */
4112 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4115 return true;
4118 /* Simplify EXPR if it is a non-dependent expression. Returns the
4119 (possibly simplified) expression. */
4121 tree
4122 fold_non_dependent_expr (tree expr)
4124 if (expr == NULL_TREE)
4125 return NULL_TREE;
4127 /* If we're in a template, but EXPR isn't value dependent, simplify
4128 it. We're supposed to treat:
4130 template <typename T> void f(T[1 + 1]);
4131 template <typename T> void f(T[2]);
4133 as two declarations of the same function, for example. */
4134 if (processing_template_decl
4135 && !type_dependent_expression_p (expr)
4136 && !value_dependent_expression_p (expr))
4138 HOST_WIDE_INT saved_processing_template_decl;
4140 saved_processing_template_decl = processing_template_decl;
4141 processing_template_decl = 0;
4142 expr = tsubst_copy_and_build (expr,
4143 /*args=*/NULL_TREE,
4144 tf_error,
4145 /*in_decl=*/NULL_TREE,
4146 /*function_p=*/false,
4147 /*integral_constant_expression_p=*/true);
4148 processing_template_decl = saved_processing_template_decl;
4150 return expr;
4153 /* EXPR is an expression which is used in a constant-expression context.
4154 For instance, it could be a VAR_DECL with a constant initializer.
4155 Extract the innest constant expression.
4157 This is basically a more powerful version of
4158 integral_constant_value, which can be used also in templates where
4159 initializers can maintain a syntactic rather than semantic form
4160 (even if they are non-dependent, for access-checking purposes). */
4162 static tree
4163 fold_decl_constant_value (tree expr)
4165 tree const_expr = expr;
4168 expr = fold_non_dependent_expr (const_expr);
4169 const_expr = integral_constant_value (expr);
4171 while (expr != const_expr);
4173 return expr;
4176 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4177 must be a function or a pointer-to-function type, as specified
4178 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4179 and check that the resulting function has external linkage. */
4181 static tree
4182 convert_nontype_argument_function (tree type, tree expr)
4184 tree fns = expr;
4185 tree fn, fn_no_ptr;
4187 fn = instantiate_type (type, fns, tf_none);
4188 if (fn == error_mark_node)
4189 return error_mark_node;
4191 fn_no_ptr = fn;
4192 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4193 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4194 if (TREE_CODE (fn_no_ptr) == BASELINK)
4195 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4197 /* [temp.arg.nontype]/1
4199 A template-argument for a non-type, non-template template-parameter
4200 shall be one of:
4201 [...]
4202 -- the address of an object or function with external linkage. */
4203 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4205 error ("%qE is not a valid template argument for type %qT "
4206 "because function %qD has not external linkage",
4207 expr, type, fn_no_ptr);
4208 return NULL_TREE;
4211 return fn;
4214 /* Attempt to convert the non-type template parameter EXPR to the
4215 indicated TYPE. If the conversion is successful, return the
4216 converted value. If the conversion is unsuccessful, return
4217 NULL_TREE if we issued an error message, or error_mark_node if we
4218 did not. We issue error messages for out-and-out bad template
4219 parameters, but not simply because the conversion failed, since we
4220 might be just trying to do argument deduction. Both TYPE and EXPR
4221 must be non-dependent.
4223 The conversion follows the special rules described in
4224 [temp.arg.nontype], and it is much more strict than an implicit
4225 conversion.
4227 This function is called twice for each template argument (see
4228 lookup_template_class for a more accurate description of this
4229 problem). This means that we need to handle expressions which
4230 are not valid in a C++ source, but can be created from the
4231 first call (for instance, casts to perform conversions). These
4232 hacks can go away after we fix the double coercion problem. */
4234 static tree
4235 convert_nontype_argument (tree type, tree expr)
4237 tree expr_type;
4239 /* Detect immediately string literals as invalid non-type argument.
4240 This special-case is not needed for correctness (we would easily
4241 catch this later), but only to provide better diagnostic for this
4242 common user mistake. As suggested by DR 100, we do not mention
4243 linkage issues in the diagnostic as this is not the point. */
4244 if (TREE_CODE (expr) == STRING_CST)
4246 error ("%qE is not a valid template argument for type %qT "
4247 "because string literals can never be used in this context",
4248 expr, type);
4249 return NULL_TREE;
4252 /* If we are in a template, EXPR may be non-dependent, but still
4253 have a syntactic, rather than semantic, form. For example, EXPR
4254 might be a SCOPE_REF, rather than the VAR_DECL to which the
4255 SCOPE_REF refers. Preserving the qualifying scope is necessary
4256 so that access checking can be performed when the template is
4257 instantiated -- but here we need the resolved form so that we can
4258 convert the argument. */
4259 expr = fold_non_dependent_expr (expr);
4260 if (error_operand_p (expr))
4261 return error_mark_node;
4262 expr_type = TREE_TYPE (expr);
4264 /* HACK: Due to double coercion, we can get a
4265 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4266 which is the tree that we built on the first call (see
4267 below when coercing to reference to object or to reference to
4268 function). We just strip everything and get to the arg.
4269 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4270 for examples. */
4271 if (TREE_CODE (expr) == NOP_EXPR)
4273 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
4275 /* ??? Maybe we could use convert_from_reference here, but we
4276 would need to relax its constraints because the NOP_EXPR
4277 could actually change the type to something more cv-qualified,
4278 and this is not folded by convert_from_reference. */
4279 tree addr = TREE_OPERAND (expr, 0);
4280 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
4281 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
4282 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
4283 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4284 (TREE_TYPE (expr_type),
4285 TREE_TYPE (TREE_TYPE (addr))));
4287 expr = TREE_OPERAND (addr, 0);
4288 expr_type = TREE_TYPE (expr);
4291 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4292 parameter is a pointer to object, through decay and
4293 qualification conversion. Let's strip everything. */
4294 else if (TYPE_PTROBV_P (type))
4296 STRIP_NOPS (expr);
4297 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
4298 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
4299 /* Skip the ADDR_EXPR only if it is part of the decay for
4300 an array. Otherwise, it is part of the original argument
4301 in the source code. */
4302 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
4303 expr = TREE_OPERAND (expr, 0);
4304 expr_type = TREE_TYPE (expr);
4308 /* [temp.arg.nontype]/5, bullet 1
4310 For a non-type template-parameter of integral or enumeration type,
4311 integral promotions (_conv.prom_) and integral conversions
4312 (_conv.integral_) are applied. */
4313 if (INTEGRAL_TYPE_P (type))
4315 if (!INTEGRAL_TYPE_P (expr_type))
4316 return error_mark_node;
4318 expr = fold_decl_constant_value (expr);
4319 /* Notice that there are constant expressions like '4 % 0' which
4320 do not fold into integer constants. */
4321 if (TREE_CODE (expr) != INTEGER_CST)
4323 error ("%qE is not a valid template argument for type %qT "
4324 "because it is a non-constant expression", expr, type);
4325 return NULL_TREE;
4328 /* At this point, an implicit conversion does what we want,
4329 because we already know that the expression is of integral
4330 type. */
4331 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
4332 if (expr == error_mark_node)
4333 return error_mark_node;
4335 /* Conversion was allowed: fold it to a bare integer constant. */
4336 expr = fold (expr);
4338 /* [temp.arg.nontype]/5, bullet 2
4340 For a non-type template-parameter of type pointer to object,
4341 qualification conversions (_conv.qual_) and the array-to-pointer
4342 conversion (_conv.array_) are applied. */
4343 else if (TYPE_PTROBV_P (type))
4345 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
4347 A template-argument for a non-type, non-template template-parameter
4348 shall be one of: [...]
4350 -- the name of a non-type template-parameter;
4351 -- the address of an object or function with external linkage, [...]
4352 expressed as "& id-expression" where the & is optional if the name
4353 refers to a function or array, or if the corresponding
4354 template-parameter is a reference.
4356 Here, we do not care about functions, as they are invalid anyway
4357 for a parameter of type pointer-to-object. */
4359 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
4360 /* Non-type template parameters are OK. */
4362 else if (TREE_CODE (expr) != ADDR_EXPR
4363 && TREE_CODE (expr_type) != ARRAY_TYPE)
4365 if (TREE_CODE (expr) == VAR_DECL)
4367 error ("%qD is not a valid template argument "
4368 "because %qD is a variable, not the address of "
4369 "a variable",
4370 expr, expr);
4371 return NULL_TREE;
4373 /* Other values, like integer constants, might be valid
4374 non-type arguments of some other type. */
4375 return error_mark_node;
4377 else
4379 tree decl;
4381 decl = ((TREE_CODE (expr) == ADDR_EXPR)
4382 ? TREE_OPERAND (expr, 0) : expr);
4383 if (TREE_CODE (decl) != VAR_DECL)
4385 error ("%qE is not a valid template argument of type %qT "
4386 "because %qE is not a variable",
4387 expr, type, decl);
4388 return NULL_TREE;
4390 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
4392 error ("%qE is not a valid template argument of type %qT "
4393 "because %qD does not have external linkage",
4394 expr, type, decl);
4395 return NULL_TREE;
4399 expr = decay_conversion (expr);
4400 if (expr == error_mark_node)
4401 return error_mark_node;
4403 expr = perform_qualification_conversions (type, expr);
4404 if (expr == error_mark_node)
4405 return error_mark_node;
4407 /* [temp.arg.nontype]/5, bullet 3
4409 For a non-type template-parameter of type reference to object, no
4410 conversions apply. The type referred to by the reference may be more
4411 cv-qualified than the (otherwise identical) type of the
4412 template-argument. The template-parameter is bound directly to the
4413 template-argument, which must be an lvalue. */
4414 else if (TYPE_REF_OBJ_P (type))
4416 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
4417 expr_type))
4418 return error_mark_node;
4420 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
4422 error ("%qE is not a valid template argument for type %qT "
4423 "because of conflicts in cv-qualification", expr, type);
4424 return NULL_TREE;
4427 if (!real_lvalue_p (expr))
4429 error ("%qE is not a valid template argument for type %qT "
4430 "because it is not an lvalue", expr, type);
4431 return NULL_TREE;
4434 /* [temp.arg.nontype]/1
4436 A template-argument for a non-type, non-template template-parameter
4437 shall be one of: [...]
4439 -- the address of an object or function with external linkage. */
4440 if (!DECL_EXTERNAL_LINKAGE_P (expr))
4442 error ("%qE is not a valid template argument for type %qT "
4443 "because object %qD has not external linkage",
4444 expr, type, expr);
4445 return NULL_TREE;
4448 expr = build_nop (type, build_address (expr));
4450 /* [temp.arg.nontype]/5, bullet 4
4452 For a non-type template-parameter of type pointer to function, only
4453 the function-to-pointer conversion (_conv.func_) is applied. If the
4454 template-argument represents a set of overloaded functions (or a
4455 pointer to such), the matching function is selected from the set
4456 (_over.over_). */
4457 else if (TYPE_PTRFN_P (type))
4459 /* If the argument is a template-id, we might not have enough
4460 context information to decay the pointer. */
4461 if (!type_unknown_p (expr_type))
4463 expr = decay_conversion (expr);
4464 if (expr == error_mark_node)
4465 return error_mark_node;
4468 expr = convert_nontype_argument_function (type, expr);
4469 if (!expr || expr == error_mark_node)
4470 return expr;
4472 /* [temp.arg.nontype]/5, bullet 5
4474 For a non-type template-parameter of type reference to function, no
4475 conversions apply. If the template-argument represents a set of
4476 overloaded functions, the matching function is selected from the set
4477 (_over.over_). */
4478 else if (TYPE_REFFN_P (type))
4480 if (TREE_CODE (expr) == ADDR_EXPR)
4482 error ("%qE is not a valid template argument for type %qT "
4483 "because it is a pointer", expr, type);
4484 inform ("try using %qE instead", TREE_OPERAND (expr, 0));
4485 return NULL_TREE;
4488 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
4489 if (!expr || expr == error_mark_node)
4490 return expr;
4492 expr = build_nop (type, build_address (expr));
4494 /* [temp.arg.nontype]/5, bullet 6
4496 For a non-type template-parameter of type pointer to member function,
4497 no conversions apply. If the template-argument represents a set of
4498 overloaded member functions, the matching member function is selected
4499 from the set (_over.over_). */
4500 else if (TYPE_PTRMEMFUNC_P (type))
4502 expr = instantiate_type (type, expr, tf_none);
4503 if (expr == error_mark_node)
4504 return error_mark_node;
4506 /* There is no way to disable standard conversions in
4507 resolve_address_of_overloaded_function (called by
4508 instantiate_type). It is possible that the call succeeded by
4509 converting &B::I to &D::I (where B is a base of D), so we need
4510 to reject this conversion here.
4512 Actually, even if there was a way to disable standard conversions,
4513 it would still be better to reject them here so that we can
4514 provide a superior diagnostic. */
4515 if (!same_type_p (TREE_TYPE (expr), type))
4517 /* Make sure we are just one standard conversion off. */
4518 gcc_assert (can_convert (type, TREE_TYPE (expr)));
4519 error ("%qE is not a valid template argument for type %qT "
4520 "because it is of type %qT", expr, type,
4521 TREE_TYPE (expr));
4522 inform ("standard conversions are not allowed in this context");
4523 return NULL_TREE;
4526 /* [temp.arg.nontype]/5, bullet 7
4528 For a non-type template-parameter of type pointer to data member,
4529 qualification conversions (_conv.qual_) are applied. */
4530 else if (TYPE_PTRMEM_P (type))
4532 expr = perform_qualification_conversions (type, expr);
4533 if (expr == error_mark_node)
4534 return expr;
4536 /* A template non-type parameter must be one of the above. */
4537 else
4538 gcc_unreachable ();
4540 /* Sanity check: did we actually convert the argument to the
4541 right type? */
4542 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
4543 return expr;
4547 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
4548 template template parameters. Both PARM_PARMS and ARG_PARMS are
4549 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
4550 or PARM_DECL.
4552 Consider the example:
4553 template <class T> class A;
4554 template<template <class U> class TT> class B;
4556 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
4557 the parameters to A, and OUTER_ARGS contains A. */
4559 static int
4560 coerce_template_template_parms (tree parm_parms,
4561 tree arg_parms,
4562 tsubst_flags_t complain,
4563 tree in_decl,
4564 tree outer_args)
4566 int nparms, nargs, i;
4567 tree parm, arg;
4569 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
4570 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
4572 nparms = TREE_VEC_LENGTH (parm_parms);
4573 nargs = TREE_VEC_LENGTH (arg_parms);
4575 if (nargs != nparms)
4576 return 0;
4578 for (i = 0; i < nparms; ++i)
4580 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
4581 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
4582 continue;
4584 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
4585 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
4587 if (arg == NULL_TREE || arg == error_mark_node
4588 || parm == NULL_TREE || parm == error_mark_node)
4589 return 0;
4591 if (TREE_CODE (arg) != TREE_CODE (parm))
4592 return 0;
4594 switch (TREE_CODE (parm))
4596 case TEMPLATE_DECL:
4597 /* We encounter instantiations of templates like
4598 template <template <template <class> class> class TT>
4599 class C; */
4601 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
4602 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
4604 if (!coerce_template_template_parms
4605 (parmparm, argparm, complain, in_decl, outer_args))
4606 return 0;
4608 /* Fall through. */
4610 case TYPE_DECL:
4611 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))
4612 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg)))
4613 /* One is a parameter pack, the other is not. */
4614 return 0;
4615 break;
4617 case PARM_DECL:
4618 /* The tsubst call is used to handle cases such as
4620 template <int> class C {};
4621 template <class T, template <T> class TT> class D {};
4622 D<int, C> d;
4624 i.e. the parameter list of TT depends on earlier parameters. */
4625 if (!dependent_type_p (TREE_TYPE (arg))
4626 && !same_type_p
4627 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
4628 TREE_TYPE (arg)))
4629 return 0;
4631 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4632 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg)))
4633 /* One is a parameter pack, the other is not. */
4634 return 0;
4635 break;
4637 default:
4638 gcc_unreachable ();
4641 return 1;
4644 /* Convert the indicated template ARG as necessary to match the
4645 indicated template PARM. Returns the converted ARG, or
4646 error_mark_node if the conversion was unsuccessful. Error and
4647 warning messages are issued under control of COMPLAIN. This
4648 conversion is for the Ith parameter in the parameter list. ARGS is
4649 the full set of template arguments deduced so far. */
4651 static tree
4652 convert_template_argument (tree parm,
4653 tree arg,
4654 tree args,
4655 tsubst_flags_t complain,
4656 int i,
4657 tree in_decl)
4659 tree val;
4660 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
4661 tree check_arg = arg;
4663 if (TREE_CODE (arg) == TREE_LIST
4664 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
4666 /* The template argument was the name of some
4667 member function. That's usually
4668 invalid, but static members are OK. In any
4669 case, grab the underlying fields/functions
4670 and issue an error later if required. */
4671 arg = TREE_VALUE (arg);
4672 TREE_TYPE (arg) = unknown_type_node;
4675 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
4676 requires_type = (TREE_CODE (parm) == TYPE_DECL
4677 || requires_tmpl_type);
4679 /* When determining whether an argument pack expansion is a template,
4680 look at the pattern. */
4681 if (TREE_CODE (check_arg) == TYPE_PACK_EXPANSION)
4682 check_arg = PACK_EXPANSION_PATTERN (check_arg);
4684 is_tmpl_type =
4685 ((TREE_CODE (check_arg) == TEMPLATE_DECL
4686 && TREE_CODE (DECL_TEMPLATE_RESULT (check_arg)) == TYPE_DECL)
4687 || TREE_CODE (check_arg) == TEMPLATE_TEMPLATE_PARM
4688 || TREE_CODE (check_arg) == UNBOUND_CLASS_TEMPLATE);
4690 if (is_tmpl_type
4691 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4692 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
4693 arg = TYPE_STUB_DECL (arg);
4695 is_type = TYPE_P (arg) || is_tmpl_type;
4697 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
4698 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
4700 pedwarn ("to refer to a type member of a template parameter, "
4701 "use %<typename %E%>", arg);
4703 arg = make_typename_type (TREE_OPERAND (arg, 0),
4704 TREE_OPERAND (arg, 1),
4705 typename_type,
4706 complain & tf_error);
4707 is_type = 1;
4709 if (is_type != requires_type)
4711 if (in_decl)
4713 if (complain & tf_error)
4715 error ("type/value mismatch at argument %d in template "
4716 "parameter list for %qD",
4717 i + 1, in_decl);
4718 if (is_type)
4719 error (" expected a constant of type %qT, got %qT",
4720 TREE_TYPE (parm),
4721 (is_tmpl_type ? DECL_NAME (arg) : arg));
4722 else if (requires_tmpl_type)
4723 error (" expected a class template, got %qE", arg);
4724 else
4725 error (" expected a type, got %qE", arg);
4728 return error_mark_node;
4730 if (is_tmpl_type ^ requires_tmpl_type)
4732 if (in_decl && (complain & tf_error))
4734 error ("type/value mismatch at argument %d in template "
4735 "parameter list for %qD",
4736 i + 1, in_decl);
4737 if (is_tmpl_type)
4738 error (" expected a type, got %qT", DECL_NAME (arg));
4739 else
4740 error (" expected a class template, got %qT", arg);
4742 return error_mark_node;
4745 if (is_type)
4747 if (requires_tmpl_type)
4749 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
4750 /* The number of argument required is not known yet.
4751 Just accept it for now. */
4752 val = TREE_TYPE (arg);
4753 else
4755 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
4756 tree argparm;
4758 check_arg = arg;
4759 /* When determining whether a pack expansion is a template,
4760 look at the pattern. */
4761 if (TREE_CODE (check_arg) == TYPE_PACK_EXPANSION)
4762 check_arg = PACK_EXPANSION_PATTERN (check_arg);
4764 argparm = DECL_INNERMOST_TEMPLATE_PARMS (check_arg);
4766 if (coerce_template_template_parms (parmparm, argparm,
4767 complain, in_decl,
4768 args))
4770 val = arg;
4772 /* TEMPLATE_TEMPLATE_PARM node is preferred over
4773 TEMPLATE_DECL. */
4774 if (val != error_mark_node)
4776 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
4777 val = TREE_TYPE (val);
4778 else if (TREE_CODE (val) == TYPE_PACK_EXPANSION
4779 && DECL_TEMPLATE_TEMPLATE_PARM_P (check_arg))
4781 val = TREE_TYPE (check_arg);
4782 val = make_pack_expansion (val);
4786 else
4788 if (in_decl && (complain & tf_error))
4790 error ("type/value mismatch at argument %d in "
4791 "template parameter list for %qD",
4792 i + 1, in_decl);
4793 error (" expected a template of type %qD, got %qD",
4794 parm, arg);
4797 val = error_mark_node;
4801 else
4802 val = arg;
4803 /* We only form one instance of each template specialization.
4804 Therefore, if we use a non-canonical variant (i.e., a
4805 typedef), any future messages referring to the type will use
4806 the typedef, which is confusing if those future uses do not
4807 themselves also use the typedef. */
4808 if (TYPE_P (val))
4809 val = canonical_type_variant (val);
4811 else
4813 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
4815 if (invalid_nontype_parm_type_p (t, complain))
4816 return error_mark_node;
4818 if (!uses_template_parms (arg) && !uses_template_parms (t))
4819 /* We used to call digest_init here. However, digest_init
4820 will report errors, which we don't want when complain
4821 is zero. More importantly, digest_init will try too
4822 hard to convert things: for example, `0' should not be
4823 converted to pointer type at this point according to
4824 the standard. Accepting this is not merely an
4825 extension, since deciding whether or not these
4826 conversions can occur is part of determining which
4827 function template to call, or whether a given explicit
4828 argument specification is valid. */
4829 val = convert_nontype_argument (t, arg);
4830 else
4831 val = arg;
4833 if (val == NULL_TREE)
4834 val = error_mark_node;
4835 else if (val == error_mark_node && (complain & tf_error))
4836 error ("could not convert template argument %qE to %qT", arg, t);
4839 return val;
4842 /* Coerces the remaining template arguments in INNER_ARGS (from
4843 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
4844 Returns the coerced argument pack. PARM_IDX is the position of this
4845 parameter in the template parameter list. ARGS is the original
4846 template argument list. */
4847 static tree
4848 coerce_template_parameter_pack (tree parms,
4849 int parm_idx,
4850 tree args,
4851 tree inner_args,
4852 int arg_idx,
4853 tree new_args,
4854 int* lost,
4855 tree in_decl,
4856 tsubst_flags_t complain)
4858 tree parm = TREE_VEC_ELT (parms, parm_idx);
4859 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
4860 tree packed_args;
4861 tree argument_pack;
4862 tree packed_types = NULL_TREE;
4864 if (arg_idx > nargs)
4865 arg_idx = nargs;
4867 packed_args = make_tree_vec (nargs - arg_idx);
4869 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
4870 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
4872 /* When the template parameter is a non-type template
4873 parameter pack whose type uses parameter packs, we need
4874 to look at each of the template arguments
4875 separately. Build a vector of the types for these
4876 non-type template parameters in PACKED_TYPES. */
4877 tree expansion
4878 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
4879 packed_types = tsubst_pack_expansion (expansion, args,
4880 complain, in_decl);
4882 if (packed_types == error_mark_node)
4883 return error_mark_node;
4885 /* Check that we have the right number of arguments. */
4886 if (arg_idx < nargs
4887 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
4888 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
4890 int needed_parms
4891 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
4892 error ("wrong number of template arguments (%d, should be %d)",
4893 nargs, needed_parms);
4894 return error_mark_node;
4897 /* If we aren't able to check the actual arguments now
4898 (because they haven't been expanded yet), we can at least
4899 verify that all of the types used for the non-type
4900 template parameter pack are, in fact, valid for non-type
4901 template parameters. */
4902 if (arg_idx < nargs
4903 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
4905 int j, len = TREE_VEC_LENGTH (packed_types);
4906 for (j = 0; j < len; ++j)
4908 tree t = TREE_VEC_ELT (packed_types, j);
4909 if (invalid_nontype_parm_type_p (t, complain))
4910 return error_mark_node;
4915 /* Convert the remaining arguments, which will be a part of the
4916 parameter pack "parm". */
4917 for (; arg_idx < nargs; ++arg_idx)
4919 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
4920 tree actual_parm = TREE_VALUE (parm);
4922 if (packed_types && !PACK_EXPANSION_P (arg))
4924 /* When we have a vector of types (corresponding to the
4925 non-type template parameter pack that uses parameter
4926 packs in its type, as mention above), and the
4927 argument is not an expansion (which expands to a
4928 currently unknown number of arguments), clone the
4929 parm and give it the next type in PACKED_TYPES. */
4930 actual_parm = copy_node (actual_parm);
4931 TREE_TYPE (actual_parm) =
4932 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
4935 arg = convert_template_argument (actual_parm,
4936 arg, new_args, complain, parm_idx,
4937 in_decl);
4938 if (arg == error_mark_node)
4939 (*lost)++;
4940 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
4943 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
4944 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
4945 argument_pack = make_node (TYPE_ARGUMENT_PACK);
4946 else
4948 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
4949 TREE_TYPE (argument_pack) = TREE_TYPE (TREE_VALUE (parm));
4950 TREE_CONSTANT (argument_pack) = 1;
4953 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
4954 return argument_pack;
4957 /* Convert all template arguments to their appropriate types, and
4958 return a vector containing the innermost resulting template
4959 arguments. If any error occurs, return error_mark_node. Error and
4960 warning messages are issued under control of COMPLAIN.
4962 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
4963 for arguments not specified in ARGS. Otherwise, if
4964 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
4965 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
4966 USE_DEFAULT_ARGS is false, then all arguments must be specified in
4967 ARGS. */
4969 static tree
4970 coerce_template_parms (tree parms,
4971 tree args,
4972 tree in_decl,
4973 tsubst_flags_t complain,
4974 bool require_all_args,
4975 bool use_default_args)
4977 int nparms, nargs, parm_idx, arg_idx, lost = 0;
4978 tree inner_args;
4979 tree new_args;
4980 tree new_inner_args;
4981 bool saved_skip_evaluation;
4983 /* When used as a boolean value, indicates whether this is a
4984 variadic template parameter list. Since it's an int, we can also
4985 subtract it from nparms to get the number of non-variadic
4986 parameters. */
4987 int variadic_p = 0;
4989 inner_args
4990 = expand_template_argument_pack (INNERMOST_TEMPLATE_ARGS (args));
4992 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
4993 nparms = TREE_VEC_LENGTH (parms);
4995 /* Determine if there are any parameter packs. */
4996 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
4998 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
4999 if (template_parameter_pack_p (tparm))
5001 variadic_p = 1;
5002 break;
5006 if ((nargs > nparms - variadic_p && !variadic_p)
5007 || (nargs < nparms - variadic_p
5008 && require_all_args
5009 && (!use_default_args
5010 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
5011 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
5013 if (complain & tf_error)
5015 const char *or_more = "";
5016 if (variadic_p)
5018 or_more = " or more";
5019 --nparms;
5022 error ("wrong number of template arguments (%d, should be %d%s)",
5023 nargs, nparms, or_more);
5025 if (in_decl)
5026 error ("provided for %q+D", in_decl);
5029 return error_mark_node;
5032 /* We need to evaluate the template arguments, even though this
5033 template-id may be nested within a "sizeof". */
5034 saved_skip_evaluation = skip_evaluation;
5035 skip_evaluation = false;
5036 new_inner_args = make_tree_vec (nparms);
5037 new_args = add_outermost_template_args (args, new_inner_args);
5038 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
5040 tree arg;
5041 tree parm;
5043 /* Get the Ith template parameter. */
5044 parm = TREE_VEC_ELT (parms, parm_idx);
5046 if (parm == error_mark_node)
5048 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
5049 continue;
5052 /* Calculate the next argument. */
5053 if (template_parameter_pack_p (TREE_VALUE (parm)))
5055 /* All remaining arguments will be placed in the
5056 template parameter pack PARM. */
5057 arg = coerce_template_parameter_pack (parms, parm_idx, args,
5058 inner_args, arg_idx,
5059 new_args, &lost,
5060 in_decl, complain);
5062 /* Store this argument. */
5063 if (arg == error_mark_node)
5064 lost++;
5065 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
5067 /* We are done with all of the arguments. */
5068 arg_idx = nargs;
5070 continue;
5072 else if (arg_idx < nargs)
5074 arg = TREE_VEC_ELT (inner_args, arg_idx);
5076 if (arg && PACK_EXPANSION_P (arg))
5078 /* If ARG is a pack expansion, but PARM is not a
5079 template parameter pack (if it were, we would have
5080 handled it above), we're trying to expand into a
5081 fixed-length argument list. */
5082 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5083 error ("cannot expand %<%E%> into a fixed-length "
5084 "argument list", arg);
5085 else
5086 error ("cannot expand %<%T%> into a fixed-length "
5087 "argument list", arg);
5090 else if (require_all_args)
5091 /* There must be a default arg in this case. */
5092 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
5093 complain, in_decl);
5094 else
5095 break;
5097 if (arg == error_mark_node)
5099 if (complain & tf_error)
5100 error ("template argument %d is invalid", arg_idx + 1);
5102 else if (!arg)
5103 /* This only occurs if there was an error in the template
5104 parameter list itself (which we would already have
5105 reported) that we are trying to recover from, e.g., a class
5106 template with a parameter list such as
5107 template<typename..., typename>. */
5108 return error_mark_node;
5109 else
5110 arg = convert_template_argument (TREE_VALUE (parm),
5111 arg, new_args, complain,
5112 parm_idx, in_decl);
5114 if (arg == error_mark_node)
5115 lost++;
5116 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
5118 skip_evaluation = saved_skip_evaluation;
5120 if (lost)
5121 return error_mark_node;
5123 return new_inner_args;
5126 /* Returns 1 if template args OT and NT are equivalent. */
5128 static int
5129 template_args_equal (tree ot, tree nt)
5131 if (nt == ot)
5132 return 1;
5134 if (TREE_CODE (nt) == TREE_VEC)
5135 /* For member templates */
5136 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
5137 else if (PACK_EXPANSION_P (ot))
5138 return PACK_EXPANSION_P (nt)
5139 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
5140 PACK_EXPANSION_PATTERN (nt));
5141 else if (TYPE_P (nt))
5142 return TYPE_P (ot) && same_type_p (ot, nt);
5143 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
5144 return 0;
5145 else
5146 return cp_tree_equal (ot, nt);
5149 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
5150 of template arguments. Returns 0 otherwise. */
5153 comp_template_args (tree oldargs, tree newargs)
5155 int i;
5157 oldargs = expand_template_argument_pack (oldargs);
5158 newargs = expand_template_argument_pack (newargs);
5160 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
5161 return 0;
5163 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
5165 tree nt = TREE_VEC_ELT (newargs, i);
5166 tree ot = TREE_VEC_ELT (oldargs, i);
5168 if (! template_args_equal (ot, nt))
5169 return 0;
5171 return 1;
5174 static void
5175 add_pending_template (tree d)
5177 tree ti = (TYPE_P (d)
5178 ? CLASSTYPE_TEMPLATE_INFO (d)
5179 : DECL_TEMPLATE_INFO (d));
5180 struct pending_template *pt;
5181 int level;
5183 if (TI_PENDING_TEMPLATE_FLAG (ti))
5184 return;
5186 /* We are called both from instantiate_decl, where we've already had a
5187 tinst_level pushed, and instantiate_template, where we haven't.
5188 Compensate. */
5189 level = !current_tinst_level || current_tinst_level->decl != d;
5191 if (level)
5192 push_tinst_level (d);
5194 pt = GGC_NEW (struct pending_template);
5195 pt->next = NULL;
5196 pt->tinst = current_tinst_level;
5197 if (last_pending_template)
5198 last_pending_template->next = pt;
5199 else
5200 pending_templates = pt;
5202 last_pending_template = pt;
5204 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
5206 if (level)
5207 pop_tinst_level ();
5211 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
5212 ARGLIST. Valid choices for FNS are given in the cp-tree.def
5213 documentation for TEMPLATE_ID_EXPR. */
5215 tree
5216 lookup_template_function (tree fns, tree arglist)
5218 tree type;
5220 if (fns == error_mark_node || arglist == error_mark_node)
5221 return error_mark_node;
5223 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
5224 gcc_assert (fns && (is_overloaded_fn (fns)
5225 || TREE_CODE (fns) == IDENTIFIER_NODE));
5227 if (BASELINK_P (fns))
5229 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
5230 unknown_type_node,
5231 BASELINK_FUNCTIONS (fns),
5232 arglist);
5233 return fns;
5236 type = TREE_TYPE (fns);
5237 if (TREE_CODE (fns) == OVERLOAD || !type)
5238 type = unknown_type_node;
5240 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
5243 /* Within the scope of a template class S<T>, the name S gets bound
5244 (in build_self_reference) to a TYPE_DECL for the class, not a
5245 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
5246 or one of its enclosing classes, and that type is a template,
5247 return the associated TEMPLATE_DECL. Otherwise, the original
5248 DECL is returned. */
5250 tree
5251 maybe_get_template_decl_from_type_decl (tree decl)
5253 return (decl != NULL_TREE
5254 && TREE_CODE (decl) == TYPE_DECL
5255 && DECL_ARTIFICIAL (decl)
5256 && CLASS_TYPE_P (TREE_TYPE (decl))
5257 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
5258 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
5261 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
5262 parameters, find the desired type.
5264 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
5266 IN_DECL, if non-NULL, is the template declaration we are trying to
5267 instantiate.
5269 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
5270 the class we are looking up.
5272 Issue error and warning messages under control of COMPLAIN.
5274 If the template class is really a local class in a template
5275 function, then the FUNCTION_CONTEXT is the function in which it is
5276 being instantiated.
5278 ??? Note that this function is currently called *twice* for each
5279 template-id: the first time from the parser, while creating the
5280 incomplete type (finish_template_type), and the second type during the
5281 real instantiation (instantiate_template_class). This is surely something
5282 that we want to avoid. It also causes some problems with argument
5283 coercion (see convert_nontype_argument for more information on this). */
5285 tree
5286 lookup_template_class (tree d1,
5287 tree arglist,
5288 tree in_decl,
5289 tree context,
5290 int entering_scope,
5291 tsubst_flags_t complain)
5293 tree template = NULL_TREE, parmlist;
5294 tree t;
5296 timevar_push (TV_NAME_LOOKUP);
5298 if (TREE_CODE (d1) == IDENTIFIER_NODE)
5300 tree value = innermost_non_namespace_value (d1);
5301 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
5302 template = value;
5303 else
5305 if (context)
5306 push_decl_namespace (context);
5307 template = lookup_name (d1);
5308 template = maybe_get_template_decl_from_type_decl (template);
5309 if (context)
5310 pop_decl_namespace ();
5312 if (template)
5313 context = DECL_CONTEXT (template);
5315 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
5317 tree type = TREE_TYPE (d1);
5319 /* If we are declaring a constructor, say A<T>::A<T>, we will get
5320 an implicit typename for the second A. Deal with it. */
5321 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
5322 type = TREE_TYPE (type);
5324 if (CLASSTYPE_TEMPLATE_INFO (type))
5326 template = CLASSTYPE_TI_TEMPLATE (type);
5327 d1 = DECL_NAME (template);
5330 else if (TREE_CODE (d1) == ENUMERAL_TYPE
5331 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
5333 template = TYPE_TI_TEMPLATE (d1);
5334 d1 = DECL_NAME (template);
5336 else if (TREE_CODE (d1) == TEMPLATE_DECL
5337 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
5339 template = d1;
5340 d1 = DECL_NAME (template);
5341 context = DECL_CONTEXT (template);
5344 /* Issue an error message if we didn't find a template. */
5345 if (! template)
5347 if (complain & tf_error)
5348 error ("%qT is not a template", d1);
5349 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5352 if (TREE_CODE (template) != TEMPLATE_DECL
5353 /* Make sure it's a user visible template, if it was named by
5354 the user. */
5355 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
5356 && !PRIMARY_TEMPLATE_P (template)))
5358 if (complain & tf_error)
5360 error ("non-template type %qT used as a template", d1);
5361 if (in_decl)
5362 error ("for template declaration %q+D", in_decl);
5364 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5367 complain &= ~tf_user;
5369 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
5371 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
5372 template arguments */
5374 tree parm;
5375 tree arglist2;
5377 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
5379 /* Consider an example where a template template parameter declared as
5381 template <class T, class U = std::allocator<T> > class TT
5383 The template parameter level of T and U are one level larger than
5384 of TT. To proper process the default argument of U, say when an
5385 instantiation `TT<int>' is seen, we need to build the full
5386 arguments containing {int} as the innermost level. Outer levels,
5387 available when not appearing as default template argument, can be
5388 obtained from `current_template_args ()'.
5390 Suppose that TT is later substituted with std::vector. The above
5391 instantiation is `TT<int, std::allocator<T> >' with TT at
5392 level 1, and T at level 2, while the template arguments at level 1
5393 becomes {std::vector} and the inner level 2 is {int}. */
5395 if (current_template_parms)
5396 arglist = add_to_template_args (current_template_args (), arglist);
5398 arglist2 = coerce_template_parms (parmlist, arglist, template,
5399 complain,
5400 /*require_all_args=*/true,
5401 /*use_default_args=*/true);
5402 if (arglist2 == error_mark_node
5403 || (!uses_template_parms (arglist2)
5404 && check_instantiated_args (template, arglist2, complain)))
5405 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5407 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
5408 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
5410 else
5412 tree template_type = TREE_TYPE (template);
5413 tree gen_tmpl;
5414 tree type_decl;
5415 tree found = NULL_TREE;
5416 int arg_depth;
5417 int parm_depth;
5418 int is_partial_instantiation;
5420 gen_tmpl = most_general_template (template);
5421 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
5422 parm_depth = TMPL_PARMS_DEPTH (parmlist);
5423 arg_depth = TMPL_ARGS_DEPTH (arglist);
5425 if (arg_depth == 1 && parm_depth > 1)
5427 /* We've been given an incomplete set of template arguments.
5428 For example, given:
5430 template <class T> struct S1 {
5431 template <class U> struct S2 {};
5432 template <class U> struct S2<U*> {};
5435 we will be called with an ARGLIST of `U*', but the
5436 TEMPLATE will be `template <class T> template
5437 <class U> struct S1<T>::S2'. We must fill in the missing
5438 arguments. */
5439 arglist
5440 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
5441 arglist);
5442 arg_depth = TMPL_ARGS_DEPTH (arglist);
5445 /* Now we should have enough arguments. */
5446 gcc_assert (parm_depth == arg_depth);
5448 /* From here on, we're only interested in the most general
5449 template. */
5450 template = gen_tmpl;
5452 /* Calculate the BOUND_ARGS. These will be the args that are
5453 actually tsubst'd into the definition to create the
5454 instantiation. */
5455 if (parm_depth > 1)
5457 /* We have multiple levels of arguments to coerce, at once. */
5458 int i;
5459 int saved_depth = TMPL_ARGS_DEPTH (arglist);
5461 tree bound_args = make_tree_vec (parm_depth);
5463 for (i = saved_depth,
5464 t = DECL_TEMPLATE_PARMS (template);
5465 i > 0 && t != NULL_TREE;
5466 --i, t = TREE_CHAIN (t))
5468 tree a = coerce_template_parms (TREE_VALUE (t),
5469 arglist, template,
5470 complain,
5471 /*require_all_args=*/true,
5472 /*use_default_args=*/true);
5474 /* Don't process further if one of the levels fails. */
5475 if (a == error_mark_node)
5477 /* Restore the ARGLIST to its full size. */
5478 TREE_VEC_LENGTH (arglist) = saved_depth;
5479 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5482 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
5484 /* We temporarily reduce the length of the ARGLIST so
5485 that coerce_template_parms will see only the arguments
5486 corresponding to the template parameters it is
5487 examining. */
5488 TREE_VEC_LENGTH (arglist)--;
5491 /* Restore the ARGLIST to its full size. */
5492 TREE_VEC_LENGTH (arglist) = saved_depth;
5494 arglist = bound_args;
5496 else
5497 arglist
5498 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
5499 INNERMOST_TEMPLATE_ARGS (arglist),
5500 template,
5501 complain,
5502 /*require_all_args=*/true,
5503 /*use_default_args=*/true);
5505 if (arglist == error_mark_node)
5506 /* We were unable to bind the arguments. */
5507 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5509 /* In the scope of a template class, explicit references to the
5510 template class refer to the type of the template, not any
5511 instantiation of it. For example, in:
5513 template <class T> class C { void f(C<T>); }
5515 the `C<T>' is just the same as `C'. Outside of the
5516 class, however, such a reference is an instantiation. */
5517 if (comp_template_args (TYPE_TI_ARGS (template_type),
5518 arglist))
5520 found = template_type;
5522 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
5524 tree ctx;
5526 for (ctx = current_class_type;
5527 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
5528 ctx = (TYPE_P (ctx)
5529 ? TYPE_CONTEXT (ctx)
5530 : DECL_CONTEXT (ctx)))
5531 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
5532 goto found_ctx;
5534 /* We're not in the scope of the class, so the
5535 TEMPLATE_TYPE is not the type we want after all. */
5536 found = NULL_TREE;
5537 found_ctx:;
5540 if (found)
5541 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5543 /* If we already have this specialization, return it. */
5544 found = retrieve_specialization (template, arglist,
5545 /*class_specializations_p=*/false);
5546 if (found)
5547 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5549 /* This type is a "partial instantiation" if any of the template
5550 arguments still involve template parameters. Note that we set
5551 IS_PARTIAL_INSTANTIATION for partial specializations as
5552 well. */
5553 is_partial_instantiation = uses_template_parms (arglist);
5555 /* If the deduced arguments are invalid, then the binding
5556 failed. */
5557 if (!is_partial_instantiation
5558 && check_instantiated_args (template,
5559 INNERMOST_TEMPLATE_ARGS (arglist),
5560 complain))
5561 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5563 if (!is_partial_instantiation
5564 && !PRIMARY_TEMPLATE_P (template)
5565 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
5567 found = xref_tag_from_type (TREE_TYPE (template),
5568 DECL_NAME (template),
5569 /*tag_scope=*/ts_global);
5570 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5573 context = tsubst (DECL_CONTEXT (template), arglist,
5574 complain, in_decl);
5575 if (!context)
5576 context = global_namespace;
5578 /* Create the type. */
5579 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
5581 if (!is_partial_instantiation)
5583 set_current_access_from_decl (TYPE_NAME (template_type));
5584 t = start_enum (TYPE_IDENTIFIER (template_type));
5586 else
5587 /* We don't want to call start_enum for this type, since
5588 the values for the enumeration constants may involve
5589 template parameters. And, no one should be interested
5590 in the enumeration constants for such a type. */
5591 t = make_node (ENUMERAL_TYPE);
5593 else
5595 t = make_aggr_type (TREE_CODE (template_type));
5596 CLASSTYPE_DECLARED_CLASS (t)
5597 = CLASSTYPE_DECLARED_CLASS (template_type);
5598 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
5599 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
5601 /* A local class. Make sure the decl gets registered properly. */
5602 if (context == current_function_decl)
5603 pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current);
5605 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
5606 /* This instantiation is another name for the primary
5607 template type. Set the TYPE_CANONICAL field
5608 appropriately. */
5609 TYPE_CANONICAL (t) = template_type;
5610 else if (any_template_arguments_need_structural_equality_p (arglist))
5611 /* Some of the template arguments require structural
5612 equality testing, so this template class requires
5613 structural equality testing. */
5614 SET_TYPE_STRUCTURAL_EQUALITY (t);
5617 /* If we called start_enum or pushtag above, this information
5618 will already be set up. */
5619 if (!TYPE_NAME (t))
5621 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5623 type_decl = create_implicit_typedef (DECL_NAME (template), t);
5624 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
5625 TYPE_STUB_DECL (t) = type_decl;
5626 DECL_SOURCE_LOCATION (type_decl)
5627 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
5629 else
5630 type_decl = TYPE_NAME (t);
5632 TREE_PRIVATE (type_decl)
5633 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
5634 TREE_PROTECTED (type_decl)
5635 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
5636 DECL_IN_SYSTEM_HEADER (type_decl)
5637 = DECL_IN_SYSTEM_HEADER (template);
5638 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
5640 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
5641 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
5644 /* Set up the template information. We have to figure out which
5645 template is the immediate parent if this is a full
5646 instantiation. */
5647 if (parm_depth == 1 || is_partial_instantiation
5648 || !PRIMARY_TEMPLATE_P (template))
5649 /* This case is easy; there are no member templates involved. */
5650 found = template;
5651 else
5653 /* This is a full instantiation of a member template. Look
5654 for a partial instantiation of which this is an instance. */
5656 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
5657 found; found = TREE_CHAIN (found))
5659 int success;
5660 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
5662 /* We only want partial instantiations, here, not
5663 specializations or full instantiations. */
5664 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
5665 || !uses_template_parms (TREE_VALUE (found)))
5666 continue;
5668 /* Temporarily reduce by one the number of levels in the
5669 ARGLIST and in FOUND so as to avoid comparing the
5670 last set of arguments. */
5671 TREE_VEC_LENGTH (arglist)--;
5672 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
5674 /* See if the arguments match. If they do, then TMPL is
5675 the partial instantiation we want. */
5676 success = comp_template_args (TREE_PURPOSE (found), arglist);
5678 /* Restore the argument vectors to their full size. */
5679 TREE_VEC_LENGTH (arglist)++;
5680 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
5682 if (success)
5684 found = tmpl;
5685 break;
5689 if (!found)
5691 /* There was no partial instantiation. This happens
5692 where C<T> is a member template of A<T> and it's used
5693 in something like
5695 template <typename T> struct B { A<T>::C<int> m; };
5696 B<float>;
5698 Create the partial instantiation.
5700 TREE_VEC_LENGTH (arglist)--;
5701 found = tsubst (template, arglist, complain, NULL_TREE);
5702 TREE_VEC_LENGTH (arglist)++;
5706 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
5707 DECL_TEMPLATE_INSTANTIATIONS (template)
5708 = tree_cons (arglist, t,
5709 DECL_TEMPLATE_INSTANTIATIONS (template));
5711 if (TREE_CODE (t) == ENUMERAL_TYPE
5712 && !is_partial_instantiation)
5713 /* Now that the type has been registered on the instantiations
5714 list, we set up the enumerators. Because the enumeration
5715 constants may involve the enumeration type itself, we make
5716 sure to register the type first, and then create the
5717 constants. That way, doing tsubst_expr for the enumeration
5718 constants won't result in recursive calls here; we'll find
5719 the instantiation and exit above. */
5720 tsubst_enum (template_type, t, arglist);
5722 if (is_partial_instantiation)
5723 /* If the type makes use of template parameters, the
5724 code that generates debugging information will crash. */
5725 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
5727 /* Possibly limit visibility based on template args. */
5728 TREE_PUBLIC (type_decl) = 1;
5729 determine_visibility (type_decl);
5731 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
5733 timevar_pop (TV_NAME_LOOKUP);
5736 struct pair_fn_data
5738 tree_fn_t fn;
5739 void *data;
5740 struct pointer_set_t *visited;
5743 /* Called from for_each_template_parm via walk_tree. */
5745 static tree
5746 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
5748 tree t = *tp;
5749 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
5750 tree_fn_t fn = pfd->fn;
5751 void *data = pfd->data;
5753 if (TYPE_P (t)
5754 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
5755 return error_mark_node;
5757 switch (TREE_CODE (t))
5759 case RECORD_TYPE:
5760 if (TYPE_PTRMEMFUNC_P (t))
5761 break;
5762 /* Fall through. */
5764 case UNION_TYPE:
5765 case ENUMERAL_TYPE:
5766 if (!TYPE_TEMPLATE_INFO (t))
5767 *walk_subtrees = 0;
5768 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
5769 fn, data, pfd->visited))
5770 return error_mark_node;
5771 break;
5773 case INTEGER_TYPE:
5774 if (for_each_template_parm (TYPE_MIN_VALUE (t),
5775 fn, data, pfd->visited)
5776 || for_each_template_parm (TYPE_MAX_VALUE (t),
5777 fn, data, pfd->visited))
5778 return error_mark_node;
5779 break;
5781 case METHOD_TYPE:
5782 /* Since we're not going to walk subtrees, we have to do this
5783 explicitly here. */
5784 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
5785 pfd->visited))
5786 return error_mark_node;
5787 /* Fall through. */
5789 case FUNCTION_TYPE:
5790 /* Check the return type. */
5791 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
5792 return error_mark_node;
5794 /* Check the parameter types. Since default arguments are not
5795 instantiated until they are needed, the TYPE_ARG_TYPES may
5796 contain expressions that involve template parameters. But,
5797 no-one should be looking at them yet. And, once they're
5798 instantiated, they don't contain template parameters, so
5799 there's no point in looking at them then, either. */
5801 tree parm;
5803 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
5804 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
5805 pfd->visited))
5806 return error_mark_node;
5808 /* Since we've already handled the TYPE_ARG_TYPES, we don't
5809 want walk_tree walking into them itself. */
5810 *walk_subtrees = 0;
5812 break;
5814 case TYPEOF_TYPE:
5815 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
5816 pfd->visited))
5817 return error_mark_node;
5818 break;
5820 case FUNCTION_DECL:
5821 case VAR_DECL:
5822 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
5823 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
5824 pfd->visited))
5825 return error_mark_node;
5826 /* Fall through. */
5828 case PARM_DECL:
5829 case CONST_DECL:
5830 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
5831 && for_each_template_parm (DECL_INITIAL (t), fn, data,
5832 pfd->visited))
5833 return error_mark_node;
5834 if (DECL_CONTEXT (t)
5835 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
5836 pfd->visited))
5837 return error_mark_node;
5838 break;
5840 case BOUND_TEMPLATE_TEMPLATE_PARM:
5841 /* Record template parameters such as `T' inside `TT<T>'. */
5842 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
5843 return error_mark_node;
5844 /* Fall through. */
5846 case TEMPLATE_TEMPLATE_PARM:
5847 case TEMPLATE_TYPE_PARM:
5848 case TEMPLATE_PARM_INDEX:
5849 if (fn && (*fn)(t, data))
5850 return error_mark_node;
5851 else if (!fn)
5852 return error_mark_node;
5853 break;
5855 case TEMPLATE_DECL:
5856 /* A template template parameter is encountered. */
5857 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
5858 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
5859 return error_mark_node;
5861 /* Already substituted template template parameter */
5862 *walk_subtrees = 0;
5863 break;
5865 case TYPENAME_TYPE:
5866 if (!fn
5867 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
5868 data, pfd->visited))
5869 return error_mark_node;
5870 break;
5872 case CONSTRUCTOR:
5873 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
5874 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
5875 (TREE_TYPE (t)), fn, data,
5876 pfd->visited))
5877 return error_mark_node;
5878 break;
5880 case INDIRECT_REF:
5881 case COMPONENT_REF:
5882 /* If there's no type, then this thing must be some expression
5883 involving template parameters. */
5884 if (!fn && !TREE_TYPE (t))
5885 return error_mark_node;
5886 break;
5888 case MODOP_EXPR:
5889 case CAST_EXPR:
5890 case REINTERPRET_CAST_EXPR:
5891 case CONST_CAST_EXPR:
5892 case STATIC_CAST_EXPR:
5893 case DYNAMIC_CAST_EXPR:
5894 case ARROW_EXPR:
5895 case DOTSTAR_EXPR:
5896 case TYPEID_EXPR:
5897 case PSEUDO_DTOR_EXPR:
5898 if (!fn)
5899 return error_mark_node;
5900 break;
5902 default:
5903 break;
5906 /* We didn't find any template parameters we liked. */
5907 return NULL_TREE;
5910 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
5911 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
5912 call FN with the parameter and the DATA.
5913 If FN returns nonzero, the iteration is terminated, and
5914 for_each_template_parm returns 1. Otherwise, the iteration
5915 continues. If FN never returns a nonzero value, the value
5916 returned by for_each_template_parm is 0. If FN is NULL, it is
5917 considered to be the function which always returns 1. */
5919 static int
5920 for_each_template_parm (tree t, tree_fn_t fn, void* data,
5921 struct pointer_set_t *visited)
5923 struct pair_fn_data pfd;
5924 int result;
5926 /* Set up. */
5927 pfd.fn = fn;
5928 pfd.data = data;
5930 /* Walk the tree. (Conceptually, we would like to walk without
5931 duplicates, but for_each_template_parm_r recursively calls
5932 for_each_template_parm, so we would need to reorganize a fair
5933 bit to use walk_tree_without_duplicates, so we keep our own
5934 visited list.) */
5935 if (visited)
5936 pfd.visited = visited;
5937 else
5938 pfd.visited = pointer_set_create ();
5939 result = cp_walk_tree (&t,
5940 for_each_template_parm_r,
5941 &pfd,
5942 pfd.visited) != NULL_TREE;
5944 /* Clean up. */
5945 if (!visited)
5947 pointer_set_destroy (pfd.visited);
5948 pfd.visited = 0;
5951 return result;
5954 /* Returns true if T depends on any template parameter. */
5957 uses_template_parms (tree t)
5959 bool dependent_p;
5960 int saved_processing_template_decl;
5962 saved_processing_template_decl = processing_template_decl;
5963 if (!saved_processing_template_decl)
5964 processing_template_decl = 1;
5965 if (TYPE_P (t))
5966 dependent_p = dependent_type_p (t);
5967 else if (TREE_CODE (t) == TREE_VEC)
5968 dependent_p = any_dependent_template_arguments_p (t);
5969 else if (TREE_CODE (t) == TREE_LIST)
5970 dependent_p = (uses_template_parms (TREE_VALUE (t))
5971 || uses_template_parms (TREE_CHAIN (t)));
5972 else if (TREE_CODE (t) == TYPE_DECL)
5973 dependent_p = dependent_type_p (TREE_TYPE (t));
5974 else if (DECL_P (t)
5975 || EXPR_P (t)
5976 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
5977 || TREE_CODE (t) == OVERLOAD
5978 || TREE_CODE (t) == BASELINK
5979 || TREE_CODE (t) == IDENTIFIER_NODE
5980 || TREE_CODE (t) == TRAIT_EXPR
5981 || CONSTANT_CLASS_P (t))
5982 dependent_p = (type_dependent_expression_p (t)
5983 || value_dependent_expression_p (t));
5984 else
5986 gcc_assert (t == error_mark_node);
5987 dependent_p = false;
5990 processing_template_decl = saved_processing_template_decl;
5992 return dependent_p;
5995 /* Returns true if T depends on any template parameter with level LEVEL. */
5998 uses_template_parms_level (tree t, int level)
6000 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
6003 static int tinst_depth;
6004 extern int max_tinst_depth;
6005 #ifdef GATHER_STATISTICS
6006 int depth_reached;
6007 #endif
6008 static int tinst_level_tick;
6009 static int last_template_error_tick;
6011 /* We're starting to instantiate D; record the template instantiation context
6012 for diagnostics and to restore it later. */
6014 static int
6015 push_tinst_level (tree d)
6017 struct tinst_level *new;
6019 if (tinst_depth >= max_tinst_depth)
6021 /* If the instantiation in question still has unbound template parms,
6022 we don't really care if we can't instantiate it, so just return.
6023 This happens with base instantiation for implicit `typename'. */
6024 if (uses_template_parms (d))
6025 return 0;
6027 last_template_error_tick = tinst_level_tick;
6028 error ("template instantiation depth exceeds maximum of %d (use "
6029 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
6030 max_tinst_depth, d);
6032 print_instantiation_context ();
6034 return 0;
6037 new = GGC_NEW (struct tinst_level);
6038 new->decl = d;
6039 new->locus = input_location;
6040 new->in_system_header_p = in_system_header;
6041 new->next = current_tinst_level;
6042 current_tinst_level = new;
6044 ++tinst_depth;
6045 #ifdef GATHER_STATISTICS
6046 if (tinst_depth > depth_reached)
6047 depth_reached = tinst_depth;
6048 #endif
6050 ++tinst_level_tick;
6051 return 1;
6054 /* We're done instantiating this template; return to the instantiation
6055 context. */
6057 static void
6058 pop_tinst_level (void)
6060 /* Restore the filename and line number stashed away when we started
6061 this instantiation. */
6062 input_location = current_tinst_level->locus;
6063 in_system_header = current_tinst_level->in_system_header_p;
6064 current_tinst_level = current_tinst_level->next;
6065 --tinst_depth;
6066 ++tinst_level_tick;
6069 /* We're instantiating a deferred template; restore the template
6070 instantiation context in which the instantiation was requested, which
6071 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
6073 static tree
6074 reopen_tinst_level (struct tinst_level *level)
6076 struct tinst_level *t;
6078 tinst_depth = 0;
6079 for (t = level; t; t = t->next)
6080 ++tinst_depth;
6082 current_tinst_level = level;
6083 pop_tinst_level ();
6084 return level->decl;
6087 /* Returns the TINST_LEVEL which gives the original instantiation
6088 context. */
6090 struct tinst_level *
6091 outermost_tinst_level (void)
6093 struct tinst_level *level = current_tinst_level;
6094 if (level)
6095 while (level->next)
6096 level = level->next;
6097 return level;
6100 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
6101 vector of template arguments, as for tsubst.
6103 Returns an appropriate tsubst'd friend declaration. */
6105 static tree
6106 tsubst_friend_function (tree decl, tree args)
6108 tree new_friend;
6110 if (TREE_CODE (decl) == FUNCTION_DECL
6111 && DECL_TEMPLATE_INSTANTIATION (decl)
6112 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
6113 /* This was a friend declared with an explicit template
6114 argument list, e.g.:
6116 friend void f<>(T);
6118 to indicate that f was a template instantiation, not a new
6119 function declaration. Now, we have to figure out what
6120 instantiation of what template. */
6122 tree template_id, arglist, fns;
6123 tree new_args;
6124 tree tmpl;
6125 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
6127 /* Friend functions are looked up in the containing namespace scope.
6128 We must enter that scope, to avoid finding member functions of the
6129 current cless with same name. */
6130 push_nested_namespace (ns);
6131 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
6132 tf_warning_or_error, NULL_TREE,
6133 /*integral_constant_expression_p=*/false);
6134 pop_nested_namespace (ns);
6135 arglist = tsubst (DECL_TI_ARGS (decl), args,
6136 tf_warning_or_error, NULL_TREE);
6137 template_id = lookup_template_function (fns, arglist);
6139 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6140 tmpl = determine_specialization (template_id, new_friend,
6141 &new_args,
6142 /*need_member_template=*/0,
6143 TREE_VEC_LENGTH (args),
6144 tsk_none);
6145 return instantiate_template (tmpl, new_args, tf_error);
6148 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6150 /* The NEW_FRIEND will look like an instantiation, to the
6151 compiler, but is not an instantiation from the point of view of
6152 the language. For example, we might have had:
6154 template <class T> struct S {
6155 template <class U> friend void f(T, U);
6158 Then, in S<int>, template <class U> void f(int, U) is not an
6159 instantiation of anything. */
6160 if (new_friend == error_mark_node)
6161 return error_mark_node;
6163 DECL_USE_TEMPLATE (new_friend) = 0;
6164 if (TREE_CODE (decl) == TEMPLATE_DECL)
6166 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
6167 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
6168 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
6171 /* The mangled name for the NEW_FRIEND is incorrect. The function
6172 is not a template instantiation and should not be mangled like
6173 one. Therefore, we forget the mangling here; we'll recompute it
6174 later if we need it. */
6175 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
6177 SET_DECL_RTL (new_friend, NULL_RTX);
6178 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
6181 if (DECL_NAMESPACE_SCOPE_P (new_friend))
6183 tree old_decl;
6184 tree new_friend_template_info;
6185 tree new_friend_result_template_info;
6186 tree ns;
6187 int new_friend_is_defn;
6189 /* We must save some information from NEW_FRIEND before calling
6190 duplicate decls since that function will free NEW_FRIEND if
6191 possible. */
6192 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
6193 new_friend_is_defn =
6194 (DECL_INITIAL (DECL_TEMPLATE_RESULT
6195 (template_for_substitution (new_friend)))
6196 != NULL_TREE);
6197 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
6199 /* This declaration is a `primary' template. */
6200 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
6202 new_friend_result_template_info
6203 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
6205 else
6206 new_friend_result_template_info = NULL_TREE;
6208 /* Make the init_value nonzero so pushdecl knows this is a defn. */
6209 if (new_friend_is_defn)
6210 DECL_INITIAL (new_friend) = error_mark_node;
6212 /* Inside pushdecl_namespace_level, we will push into the
6213 current namespace. However, the friend function should go
6214 into the namespace of the template. */
6215 ns = decl_namespace_context (new_friend);
6216 push_nested_namespace (ns);
6217 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
6218 pop_nested_namespace (ns);
6220 if (old_decl == error_mark_node)
6221 return error_mark_node;
6223 if (old_decl != new_friend)
6225 /* This new friend declaration matched an existing
6226 declaration. For example, given:
6228 template <class T> void f(T);
6229 template <class U> class C {
6230 template <class T> friend void f(T) {}
6233 the friend declaration actually provides the definition
6234 of `f', once C has been instantiated for some type. So,
6235 old_decl will be the out-of-class template declaration,
6236 while new_friend is the in-class definition.
6238 But, if `f' was called before this point, the
6239 instantiation of `f' will have DECL_TI_ARGS corresponding
6240 to `T' but not to `U', references to which might appear
6241 in the definition of `f'. Previously, the most general
6242 template for an instantiation of `f' was the out-of-class
6243 version; now it is the in-class version. Therefore, we
6244 run through all specialization of `f', adding to their
6245 DECL_TI_ARGS appropriately. In particular, they need a
6246 new set of outer arguments, corresponding to the
6247 arguments for this class instantiation.
6249 The same situation can arise with something like this:
6251 friend void f(int);
6252 template <class T> class C {
6253 friend void f(T) {}
6256 when `C<int>' is instantiated. Now, `f(int)' is defined
6257 in the class. */
6259 if (!new_friend_is_defn)
6260 /* On the other hand, if the in-class declaration does
6261 *not* provide a definition, then we don't want to alter
6262 existing definitions. We can just leave everything
6263 alone. */
6265 else
6267 /* Overwrite whatever template info was there before, if
6268 any, with the new template information pertaining to
6269 the declaration. */
6270 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
6272 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
6273 reregister_specialization (new_friend,
6274 most_general_template (old_decl),
6275 old_decl);
6276 else
6278 tree t;
6279 tree new_friend_args;
6281 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
6282 = new_friend_result_template_info;
6284 new_friend_args = TI_ARGS (new_friend_template_info);
6285 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
6286 t != NULL_TREE;
6287 t = TREE_CHAIN (t))
6289 tree spec = TREE_VALUE (t);
6291 DECL_TI_ARGS (spec)
6292 = add_outermost_template_args (new_friend_args,
6293 DECL_TI_ARGS (spec));
6296 /* Now, since specializations are always supposed to
6297 hang off of the most general template, we must move
6298 them. */
6299 t = most_general_template (old_decl);
6300 if (t != old_decl)
6302 DECL_TEMPLATE_SPECIALIZATIONS (t)
6303 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
6304 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
6305 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
6310 /* The information from NEW_FRIEND has been merged into OLD_DECL
6311 by duplicate_decls. */
6312 new_friend = old_decl;
6315 else
6317 tree context = DECL_CONTEXT (new_friend);
6318 bool dependent_p;
6320 /* In the code
6321 template <class T> class C {
6322 template <class U> friend void C1<U>::f (); // case 1
6323 friend void C2<T>::f (); // case 2
6325 we only need to make sure CONTEXT is a complete type for
6326 case 2. To distinguish between the two cases, we note that
6327 CONTEXT of case 1 remains dependent type after tsubst while
6328 this isn't true for case 2. */
6329 ++processing_template_decl;
6330 dependent_p = dependent_type_p (context);
6331 --processing_template_decl;
6333 if (!dependent_p
6334 && !complete_type_or_else (context, NULL_TREE))
6335 return error_mark_node;
6337 if (COMPLETE_TYPE_P (context))
6339 /* Check to see that the declaration is really present, and,
6340 possibly obtain an improved declaration. */
6341 tree fn = check_classfn (context,
6342 new_friend, NULL_TREE);
6344 if (fn)
6345 new_friend = fn;
6349 return new_friend;
6352 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
6353 template arguments, as for tsubst.
6355 Returns an appropriate tsubst'd friend type or error_mark_node on
6356 failure. */
6358 static tree
6359 tsubst_friend_class (tree friend_tmpl, tree args)
6361 tree friend_type;
6362 tree tmpl;
6363 tree context;
6365 context = DECL_CONTEXT (friend_tmpl);
6367 if (context)
6369 if (TREE_CODE (context) == NAMESPACE_DECL)
6370 push_nested_namespace (context);
6371 else
6372 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
6375 /* Look for a class template declaration. We look for hidden names
6376 because two friend declarations of the same template are the
6377 same. For example, in:
6379 struct A {
6380 template <typename> friend class F;
6382 template <typename> struct B {
6383 template <typename> friend class F;
6386 both F templates are the same. */
6387 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
6388 /*block_p=*/true, 0,
6389 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
6391 /* But, if we don't find one, it might be because we're in a
6392 situation like this:
6394 template <class T>
6395 struct S {
6396 template <class U>
6397 friend struct S;
6400 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
6401 for `S<int>', not the TEMPLATE_DECL. */
6402 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
6404 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
6405 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
6408 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
6410 /* The friend template has already been declared. Just
6411 check to see that the declarations match, and install any new
6412 default parameters. We must tsubst the default parameters,
6413 of course. We only need the innermost template parameters
6414 because that is all that redeclare_class_template will look
6415 at. */
6416 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
6417 > TMPL_ARGS_DEPTH (args))
6419 tree parms;
6420 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
6421 args, tf_warning_or_error);
6422 redeclare_class_template (TREE_TYPE (tmpl), parms);
6425 friend_type = TREE_TYPE (tmpl);
6427 else
6429 /* The friend template has not already been declared. In this
6430 case, the instantiation of the template class will cause the
6431 injection of this template into the global scope. */
6432 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
6433 if (tmpl == error_mark_node)
6434 return error_mark_node;
6436 /* The new TMPL is not an instantiation of anything, so we
6437 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
6438 the new type because that is supposed to be the corresponding
6439 template decl, i.e., TMPL. */
6440 DECL_USE_TEMPLATE (tmpl) = 0;
6441 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
6442 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
6443 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
6444 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
6446 /* Inject this template into the global scope. */
6447 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
6450 if (context)
6452 if (TREE_CODE (context) == NAMESPACE_DECL)
6453 pop_nested_namespace (context);
6454 else
6455 pop_nested_class ();
6458 return friend_type;
6461 /* Returns zero if TYPE cannot be completed later due to circularity.
6462 Otherwise returns one. */
6464 static int
6465 can_complete_type_without_circularity (tree type)
6467 if (type == NULL_TREE || type == error_mark_node)
6468 return 0;
6469 else if (COMPLETE_TYPE_P (type))
6470 return 1;
6471 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
6472 return can_complete_type_without_circularity (TREE_TYPE (type));
6473 else if (CLASS_TYPE_P (type)
6474 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
6475 return 0;
6476 else
6477 return 1;
6480 tree
6481 instantiate_class_template (tree type)
6483 tree template, args, pattern, t, member;
6484 tree typedecl;
6485 tree pbinfo;
6486 tree base_list;
6488 if (type == error_mark_node)
6489 return error_mark_node;
6491 if (TYPE_BEING_DEFINED (type)
6492 || COMPLETE_TYPE_P (type)
6493 || dependent_type_p (type))
6494 return type;
6496 /* Figure out which template is being instantiated. */
6497 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
6498 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
6500 /* Determine what specialization of the original template to
6501 instantiate. */
6502 t = most_specialized_class (type, template);
6503 if (t == error_mark_node)
6505 TYPE_BEING_DEFINED (type) = 1;
6506 return error_mark_node;
6508 else if (t)
6510 /* This TYPE is actually an instantiation of a partial
6511 specialization. We replace the innermost set of ARGS with
6512 the arguments appropriate for substitution. For example,
6513 given:
6515 template <class T> struct S {};
6516 template <class T> struct S<T*> {};
6518 and supposing that we are instantiating S<int*>, ARGS will
6519 presently be {int*} -- but we need {int}. */
6520 pattern = TREE_TYPE (t);
6521 args = TREE_PURPOSE (t);
6523 else
6525 pattern = TREE_TYPE (template);
6526 args = CLASSTYPE_TI_ARGS (type);
6529 /* If the template we're instantiating is incomplete, then clearly
6530 there's nothing we can do. */
6531 if (!COMPLETE_TYPE_P (pattern))
6532 return type;
6534 /* If we've recursively instantiated too many templates, stop. */
6535 if (! push_tinst_level (type))
6536 return type;
6538 /* Now we're really doing the instantiation. Mark the type as in
6539 the process of being defined. */
6540 TYPE_BEING_DEFINED (type) = 1;
6542 /* We may be in the middle of deferred access check. Disable
6543 it now. */
6544 push_deferring_access_checks (dk_no_deferred);
6546 push_to_top_level ();
6548 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
6550 /* Set the input location to the template definition. This is needed
6551 if tsubsting causes an error. */
6552 typedecl = TYPE_MAIN_DECL (type);
6553 input_location = DECL_SOURCE_LOCATION (typedecl);
6554 in_system_header = DECL_IN_SYSTEM_HEADER (typedecl);
6556 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
6557 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
6558 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
6559 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
6560 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
6561 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
6562 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
6563 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
6564 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
6565 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
6566 TYPE_PACKED (type) = TYPE_PACKED (pattern);
6567 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
6568 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
6569 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
6570 if (ANON_AGGR_TYPE_P (pattern))
6571 SET_ANON_AGGR_TYPE_P (type);
6572 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
6574 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
6575 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
6578 pbinfo = TYPE_BINFO (pattern);
6580 /* We should never instantiate a nested class before its enclosing
6581 class; we need to look up the nested class by name before we can
6582 instantiate it, and that lookup should instantiate the enclosing
6583 class. */
6584 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
6585 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
6586 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
6588 base_list = NULL_TREE;
6589 if (BINFO_N_BASE_BINFOS (pbinfo))
6591 tree pbase_binfo;
6592 tree context = TYPE_CONTEXT (type);
6593 tree pushed_scope;
6594 int i;
6596 /* We must enter the scope containing the type, as that is where
6597 the accessibility of types named in dependent bases are
6598 looked up from. */
6599 pushed_scope = push_scope (context ? context : global_namespace);
6601 /* Substitute into each of the bases to determine the actual
6602 basetypes. */
6603 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
6605 tree base;
6606 tree access = BINFO_BASE_ACCESS (pbinfo, i);
6607 tree expanded_bases = NULL_TREE;
6608 int idx, len = 1;
6610 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
6612 expanded_bases =
6613 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
6614 args, tf_error, NULL_TREE);
6615 if (expanded_bases == error_mark_node)
6616 continue;
6618 len = TREE_VEC_LENGTH (expanded_bases);
6621 for (idx = 0; idx < len; idx++)
6623 if (expanded_bases)
6624 /* Extract the already-expanded base class. */
6625 base = TREE_VEC_ELT (expanded_bases, idx);
6626 else
6627 /* Substitute to figure out the base class. */
6628 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
6629 NULL_TREE);
6631 if (base == error_mark_node)
6632 continue;
6634 base_list = tree_cons (access, base, base_list);
6635 if (BINFO_VIRTUAL_P (pbase_binfo))
6636 TREE_TYPE (base_list) = integer_type_node;
6640 /* The list is now in reverse order; correct that. */
6641 base_list = nreverse (base_list);
6643 if (pushed_scope)
6644 pop_scope (pushed_scope);
6646 /* Now call xref_basetypes to set up all the base-class
6647 information. */
6648 xref_basetypes (type, base_list);
6651 /* Now that our base classes are set up, enter the scope of the
6652 class, so that name lookups into base classes, etc. will work
6653 correctly. This is precisely analogous to what we do in
6654 begin_class_definition when defining an ordinary non-template
6655 class. */
6656 pushclass (type);
6658 /* Now members are processed in the order of declaration. */
6659 for (member = CLASSTYPE_DECL_LIST (pattern);
6660 member; member = TREE_CHAIN (member))
6662 tree t = TREE_VALUE (member);
6664 if (TREE_PURPOSE (member))
6666 if (TYPE_P (t))
6668 /* Build new CLASSTYPE_NESTED_UTDS. */
6670 tree newtag;
6671 bool class_template_p;
6673 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
6674 && TYPE_LANG_SPECIFIC (t)
6675 && CLASSTYPE_IS_TEMPLATE (t));
6676 /* If the member is a class template, then -- even after
6677 substitution -- there may be dependent types in the
6678 template argument list for the class. We increment
6679 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
6680 that function will assume that no types are dependent
6681 when outside of a template. */
6682 if (class_template_p)
6683 ++processing_template_decl;
6684 newtag = tsubst (t, args, tf_error, NULL_TREE);
6685 if (class_template_p)
6686 --processing_template_decl;
6687 if (newtag == error_mark_node)
6688 continue;
6690 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
6692 tree name = TYPE_IDENTIFIER (t);
6694 if (class_template_p)
6695 /* Unfortunately, lookup_template_class sets
6696 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
6697 instantiation (i.e., for the type of a member
6698 template class nested within a template class.)
6699 This behavior is required for
6700 maybe_process_partial_specialization to work
6701 correctly, but is not accurate in this case;
6702 the TAG is not an instantiation of anything.
6703 (The corresponding TEMPLATE_DECL is an
6704 instantiation, but the TYPE is not.) */
6705 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
6707 /* Now, we call pushtag to put this NEWTAG into the scope of
6708 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
6709 pushtag calling push_template_decl. We don't have to do
6710 this for enums because it will already have been done in
6711 tsubst_enum. */
6712 if (name)
6713 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
6714 pushtag (name, newtag, /*tag_scope=*/ts_current);
6717 else if (TREE_CODE (t) == FUNCTION_DECL
6718 || DECL_FUNCTION_TEMPLATE_P (t))
6720 /* Build new TYPE_METHODS. */
6721 tree r;
6723 if (TREE_CODE (t) == TEMPLATE_DECL)
6724 ++processing_template_decl;
6725 r = tsubst (t, args, tf_error, NULL_TREE);
6726 if (TREE_CODE (t) == TEMPLATE_DECL)
6727 --processing_template_decl;
6728 set_current_access_from_decl (r);
6729 finish_member_declaration (r);
6731 else
6733 /* Build new TYPE_FIELDS. */
6734 if (TREE_CODE (t) == STATIC_ASSERT)
6736 tree condition =
6737 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
6738 tf_warning_or_error, NULL_TREE,
6739 /*integral_constant_expression_p=*/true);
6740 finish_static_assert (condition,
6741 STATIC_ASSERT_MESSAGE (t),
6742 STATIC_ASSERT_SOURCE_LOCATION (t),
6743 /*member_p=*/true);
6745 else if (TREE_CODE (t) != CONST_DECL)
6747 tree r;
6749 /* The the file and line for this declaration, to
6750 assist in error message reporting. Since we
6751 called push_tinst_level above, we don't need to
6752 restore these. */
6753 input_location = DECL_SOURCE_LOCATION (t);
6755 if (TREE_CODE (t) == TEMPLATE_DECL)
6756 ++processing_template_decl;
6757 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
6758 if (TREE_CODE (t) == TEMPLATE_DECL)
6759 --processing_template_decl;
6760 if (TREE_CODE (r) == VAR_DECL)
6762 /* In [temp.inst]:
6764 [t]he initialization (and any associated
6765 side-effects) of a static data member does
6766 not occur unless the static data member is
6767 itself used in a way that requires the
6768 definition of the static data member to
6769 exist.
6771 Therefore, we do not substitute into the
6772 initialized for the static data member here. */
6773 finish_static_data_member_decl
6775 /*init=*/NULL_TREE,
6776 /*init_const_expr_p=*/false,
6777 /*asmspec_tree=*/NULL_TREE,
6778 /*flags=*/0);
6779 if (DECL_INITIALIZED_IN_CLASS_P (r))
6780 check_static_variable_definition (r, TREE_TYPE (r));
6782 else if (TREE_CODE (r) == FIELD_DECL)
6784 /* Determine whether R has a valid type and can be
6785 completed later. If R is invalid, then it is
6786 replaced by error_mark_node so that it will not be
6787 added to TYPE_FIELDS. */
6788 tree rtype = TREE_TYPE (r);
6789 if (can_complete_type_without_circularity (rtype))
6790 complete_type (rtype);
6792 if (!COMPLETE_TYPE_P (rtype))
6794 cxx_incomplete_type_error (r, rtype);
6795 r = error_mark_node;
6799 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
6800 such a thing will already have been added to the field
6801 list by tsubst_enum in finish_member_declaration in the
6802 CLASSTYPE_NESTED_UTDS case above. */
6803 if (!(TREE_CODE (r) == TYPE_DECL
6804 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
6805 && DECL_ARTIFICIAL (r)))
6807 set_current_access_from_decl (r);
6808 finish_member_declaration (r);
6813 else
6815 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
6817 /* Build new CLASSTYPE_FRIEND_CLASSES. */
6819 tree friend_type = t;
6820 bool adjust_processing_template_decl = false;
6822 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
6824 /* template <class T> friend class C; */
6825 friend_type = tsubst_friend_class (friend_type, args);
6826 adjust_processing_template_decl = true;
6828 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
6830 /* template <class T> friend class C::D; */
6831 friend_type = tsubst (friend_type, args,
6832 tf_warning_or_error, NULL_TREE);
6833 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
6834 friend_type = TREE_TYPE (friend_type);
6835 adjust_processing_template_decl = true;
6837 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
6839 /* This could be either
6841 friend class T::C;
6843 when dependent_type_p is false or
6845 template <class U> friend class T::C;
6847 otherwise. */
6848 friend_type = tsubst (friend_type, args,
6849 tf_warning_or_error, NULL_TREE);
6850 /* Bump processing_template_decl for correct
6851 dependent_type_p calculation. */
6852 ++processing_template_decl;
6853 if (dependent_type_p (friend_type))
6854 adjust_processing_template_decl = true;
6855 --processing_template_decl;
6857 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
6858 && hidden_name_p (TYPE_NAME (friend_type)))
6860 /* friend class C;
6862 where C hasn't been declared yet. Let's lookup name
6863 from namespace scope directly, bypassing any name that
6864 come from dependent base class. */
6865 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
6867 /* The call to xref_tag_from_type does injection for friend
6868 classes. */
6869 push_nested_namespace (ns);
6870 friend_type =
6871 xref_tag_from_type (friend_type, NULL_TREE,
6872 /*tag_scope=*/ts_current);
6873 pop_nested_namespace (ns);
6875 else if (uses_template_parms (friend_type))
6876 /* friend class C<T>; */
6877 friend_type = tsubst (friend_type, args,
6878 tf_warning_or_error, NULL_TREE);
6879 /* Otherwise it's
6881 friend class C;
6883 where C is already declared or
6885 friend class C<int>;
6887 We don't have to do anything in these cases. */
6889 if (adjust_processing_template_decl)
6890 /* Trick make_friend_class into realizing that the friend
6891 we're adding is a template, not an ordinary class. It's
6892 important that we use make_friend_class since it will
6893 perform some error-checking and output cross-reference
6894 information. */
6895 ++processing_template_decl;
6897 if (friend_type != error_mark_node)
6898 make_friend_class (type, friend_type, /*complain=*/false);
6900 if (adjust_processing_template_decl)
6901 --processing_template_decl;
6903 else
6905 /* Build new DECL_FRIENDLIST. */
6906 tree r;
6908 /* The the file and line for this declaration, to
6909 assist in error message reporting. Since we
6910 called push_tinst_level above, we don't need to
6911 restore these. */
6912 input_location = DECL_SOURCE_LOCATION (t);
6914 if (TREE_CODE (t) == TEMPLATE_DECL)
6916 ++processing_template_decl;
6917 push_deferring_access_checks (dk_no_check);
6920 r = tsubst_friend_function (t, args);
6921 add_friend (type, r, /*complain=*/false);
6922 if (TREE_CODE (t) == TEMPLATE_DECL)
6924 pop_deferring_access_checks ();
6925 --processing_template_decl;
6931 /* Set the file and line number information to whatever is given for
6932 the class itself. This puts error messages involving generated
6933 implicit functions at a predictable point, and the same point
6934 that would be used for non-template classes. */
6935 input_location = DECL_SOURCE_LOCATION (typedecl);
6937 unreverse_member_declarations (type);
6938 finish_struct_1 (type);
6939 TYPE_BEING_DEFINED (type) = 0;
6941 /* Now that the class is complete, instantiate default arguments for
6942 any member functions. We don't do this earlier because the
6943 default arguments may reference members of the class. */
6944 if (!PRIMARY_TEMPLATE_P (template))
6945 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
6946 if (TREE_CODE (t) == FUNCTION_DECL
6947 /* Implicitly generated member functions will not have template
6948 information; they are not instantiations, but instead are
6949 created "fresh" for each instantiation. */
6950 && DECL_TEMPLATE_INFO (t))
6951 tsubst_default_arguments (t);
6953 popclass ();
6954 pop_from_top_level ();
6955 pop_deferring_access_checks ();
6956 pop_tinst_level ();
6958 /* The vtable for a template class can be emitted in any translation
6959 unit in which the class is instantiated. When there is no key
6960 method, however, finish_struct_1 will already have added TYPE to
6961 the keyed_classes list. */
6962 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
6963 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
6965 return type;
6968 static tree
6969 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6971 tree r;
6973 if (!t)
6974 r = t;
6975 else if (TYPE_P (t))
6976 r = tsubst (t, args, complain, in_decl);
6977 else
6979 r = tsubst_expr (t, args, complain, in_decl,
6980 /*integral_constant_expression_p=*/true);
6981 r = fold_non_dependent_expr (r);
6983 return r;
6986 /* Substitute ARGS into T, which is an pack expansion
6987 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
6988 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
6989 (if only a partial substitution could be performed) or
6990 ERROR_MARK_NODE if there was an error. */
6991 tree
6992 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
6993 tree in_decl)
6995 tree pattern;
6996 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
6997 tree first_arg_pack; int i, len = -1;
6998 tree result;
6999 int incomplete = 0;
7001 gcc_assert (PACK_EXPANSION_P (t));
7002 pattern = PACK_EXPANSION_PATTERN (t);
7004 /* Determine the argument packs that will instantiate the parameter
7005 packs used in the expansion expression. While we're at it,
7006 compute the number of arguments to be expanded and make sure it
7007 is consistent. */
7008 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
7009 pack = TREE_CHAIN (pack))
7011 tree parm_pack = TREE_VALUE (pack);
7012 tree arg_pack = NULL_TREE;
7013 tree orig_arg = NULL_TREE;
7015 if (TREE_CODE (parm_pack) == PARM_DECL)
7017 if (local_specializations)
7018 arg_pack = retrieve_local_specialization (parm_pack);
7020 else
7022 int level, idx, levels;
7023 template_parm_level_and_index (parm_pack, &level, &idx);
7025 levels = TMPL_ARGS_DEPTH (args);
7026 if (level <= levels)
7027 arg_pack = TMPL_ARG (args, level, idx);
7030 orig_arg = arg_pack;
7031 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
7032 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
7034 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
7035 /* This can only happen if we forget to expand an argument
7036 pack somewhere else. Just return an error, silently. */
7038 result = make_tree_vec (1);
7039 TREE_VEC_ELT (result, 0) = error_mark_node;
7040 return result;
7043 if (arg_pack)
7045 int my_len =
7046 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
7048 /* It's all-or-nothing with incomplete argument packs. */
7049 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7050 return error_mark_node;
7052 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7053 incomplete = 1;
7055 if (len < 0)
7057 len = my_len;
7058 first_arg_pack = arg_pack;
7060 else if (len != my_len)
7062 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
7063 error ("mismatched argument pack lengths while expanding "
7064 "%<%T%>",
7065 pattern);
7066 else
7067 error ("mismatched argument pack lengths while expanding "
7068 "%<%E%>",
7069 pattern);
7070 return error_mark_node;
7073 /* Keep track of the parameter packs and their corresponding
7074 argument packs. */
7075 packs = tree_cons (parm_pack, arg_pack, packs);
7076 TREE_TYPE (packs) = orig_arg;
7078 else
7079 /* We can't substitute for this parameter pack. */
7080 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
7081 TREE_VALUE (pack),
7082 unsubstituted_packs);
7085 /* We cannot expand this expansion expression, because we don't have
7086 all of the argument packs we need. Substitute into the pattern
7087 and return a PACK_EXPANSION_*. The caller will need to deal with
7088 that. */
7089 if (unsubstituted_packs)
7090 return make_pack_expansion (tsubst (pattern, args, complain,
7091 in_decl));
7093 /* We could not find any argument packs that work. */
7094 if (len < 0)
7095 return error_mark_node;
7097 /* For each argument in each argument pack, substitute into the
7098 pattern. */
7099 result = make_tree_vec (len + incomplete);
7100 for (i = 0; i < len + incomplete; ++i)
7102 /* For parameter pack, change the substitution of the parameter
7103 pack to the ith argument in its argument pack, then expand
7104 the pattern. */
7105 for (pack = packs; pack; pack = TREE_CHAIN (pack))
7107 tree parm = TREE_PURPOSE (pack);
7109 if (TREE_CODE (parm) == PARM_DECL)
7111 /* Select the Ith argument from the pack. */
7112 tree arg = make_node (ARGUMENT_PACK_SELECT);
7113 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
7114 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
7115 mark_used (parm);
7116 register_local_specialization (arg, parm);
7118 else
7120 tree value = parm;
7121 int idx, level;
7122 template_parm_level_and_index (parm, &level, &idx);
7124 if (i < len)
7126 /* Select the Ith argument from the pack. */
7127 value = make_node (ARGUMENT_PACK_SELECT);
7128 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
7129 ARGUMENT_PACK_SELECT_INDEX (value) = i;
7132 /* Update the corresponding argument. */
7133 TMPL_ARG (args, level, idx) = value;
7137 /* Substitute into the PATTERN with the altered arguments. */
7138 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
7139 TREE_VEC_ELT (result, i) =
7140 tsubst_expr (pattern, args, complain, in_decl,
7141 /*integral_constant_expression_p=*/false);
7142 else
7143 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
7145 if (i == len)
7146 /* When we have incomplete argument packs, the last "expanded"
7147 result is itself a pack expansion, which allows us
7148 to deduce more arguments. */
7149 TREE_VEC_ELT (result, i) =
7150 make_pack_expansion (TREE_VEC_ELT (result, i));
7152 if (TREE_VEC_ELT (result, i) == error_mark_node)
7154 result = error_mark_node;
7155 break;
7159 /* Update ARGS to restore the substitution from parameter packs to
7160 their argument packs. */
7161 for (pack = packs; pack; pack = TREE_CHAIN (pack))
7163 tree parm = TREE_PURPOSE (pack);
7165 if (TREE_CODE (parm) == PARM_DECL)
7166 register_local_specialization (TREE_TYPE (pack), parm);
7167 else
7169 int idx, level;
7170 template_parm_level_and_index (parm, &level, &idx);
7172 /* Update the corresponding argument. */
7173 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
7174 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
7175 TREE_TYPE (pack);
7176 else
7177 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
7181 return result;
7184 /* Substitute ARGS into the vector or list of template arguments T. */
7186 static tree
7187 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7189 tree orig_t = t;
7190 int len = TREE_VEC_LENGTH (t);
7191 int need_new = 0, i, expanded_len_adjust = 0, out;
7192 tree *elts = (tree *) alloca (len * sizeof (tree));
7194 for (i = 0; i < len; i++)
7196 tree orig_arg = TREE_VEC_ELT (t, i);
7197 tree new_arg;
7199 if (TREE_CODE (orig_arg) == TREE_VEC)
7200 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
7201 else if (PACK_EXPANSION_P (orig_arg))
7203 /* Substitute into an expansion expression. */
7204 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
7206 if (TREE_CODE (new_arg) == TREE_VEC)
7207 /* Add to the expanded length adjustment the number of
7208 expanded arguments. We subtract one from this
7209 measurement, because the argument pack expression
7210 itself is already counted as 1 in
7211 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
7212 the argument pack is empty. */
7213 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
7215 else if (ARGUMENT_PACK_P (orig_arg))
7217 /* Substitute into each of the arguments. */
7218 new_arg = make_node (TREE_CODE (orig_arg));
7220 SET_ARGUMENT_PACK_ARGS (
7221 new_arg,
7222 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
7223 args, complain, in_decl));
7225 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
7226 new_arg = error_mark_node;
7228 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
7229 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
7230 complain, in_decl);
7231 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
7233 if (TREE_TYPE (new_arg) == error_mark_node)
7234 new_arg = error_mark_node;
7237 else
7238 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
7240 if (new_arg == error_mark_node)
7241 return error_mark_node;
7243 elts[i] = new_arg;
7244 if (new_arg != orig_arg)
7245 need_new = 1;
7248 if (!need_new)
7249 return t;
7251 /* Make space for the expanded arguments coming from template
7252 argument packs. */
7253 t = make_tree_vec (len + expanded_len_adjust);
7254 for (i = 0, out = 0; i < len; i++)
7256 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
7257 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
7258 && TREE_CODE (elts[i]) == TREE_VEC)
7260 int idx;
7262 /* Now expand the template argument pack "in place". */
7263 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
7264 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
7266 else
7268 TREE_VEC_ELT (t, out) = elts[i];
7269 out++;
7273 return t;
7276 /* Return the result of substituting ARGS into the template parameters
7277 given by PARMS. If there are m levels of ARGS and m + n levels of
7278 PARMS, then the result will contain n levels of PARMS. For
7279 example, if PARMS is `template <class T> template <class U>
7280 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
7281 result will be `template <int*, double, class V>'. */
7283 static tree
7284 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
7286 tree r = NULL_TREE;
7287 tree* new_parms;
7289 /* When substituting into a template, we must set
7290 PROCESSING_TEMPLATE_DECL as the template parameters may be
7291 dependent if they are based on one-another, and the dependency
7292 predicates are short-circuit outside of templates. */
7293 ++processing_template_decl;
7295 for (new_parms = &r;
7296 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
7297 new_parms = &(TREE_CHAIN (*new_parms)),
7298 parms = TREE_CHAIN (parms))
7300 tree new_vec =
7301 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
7302 int i;
7304 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
7306 tree tuple;
7307 tree default_value;
7308 tree parm_decl;
7310 if (parms == error_mark_node)
7311 continue;
7313 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
7315 if (tuple == error_mark_node)
7316 continue;
7318 default_value = TREE_PURPOSE (tuple);
7319 parm_decl = TREE_VALUE (tuple);
7321 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
7322 if (TREE_CODE (parm_decl) == PARM_DECL
7323 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
7324 parm_decl = error_mark_node;
7325 default_value = tsubst_template_arg (default_value, args,
7326 complain, NULL_TREE);
7328 tuple = build_tree_list (default_value, parm_decl);
7329 TREE_VEC_ELT (new_vec, i) = tuple;
7332 *new_parms =
7333 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
7334 - TMPL_ARGS_DEPTH (args)),
7335 new_vec, NULL_TREE);
7338 --processing_template_decl;
7340 return r;
7343 /* Substitute the ARGS into the indicated aggregate (or enumeration)
7344 type T. If T is not an aggregate or enumeration type, it is
7345 handled as if by tsubst. IN_DECL is as for tsubst. If
7346 ENTERING_SCOPE is nonzero, T is the context for a template which
7347 we are presently tsubst'ing. Return the substituted value. */
7349 static tree
7350 tsubst_aggr_type (tree t,
7351 tree args,
7352 tsubst_flags_t complain,
7353 tree in_decl,
7354 int entering_scope)
7356 if (t == NULL_TREE)
7357 return NULL_TREE;
7359 switch (TREE_CODE (t))
7361 case RECORD_TYPE:
7362 if (TYPE_PTRMEMFUNC_P (t))
7363 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
7365 /* Else fall through. */
7366 case ENUMERAL_TYPE:
7367 case UNION_TYPE:
7368 if (TYPE_TEMPLATE_INFO (t))
7370 tree argvec;
7371 tree context;
7372 tree r;
7373 bool saved_skip_evaluation;
7375 /* In "sizeof(X<I>)" we need to evaluate "I". */
7376 saved_skip_evaluation = skip_evaluation;
7377 skip_evaluation = false;
7379 /* First, determine the context for the type we are looking
7380 up. */
7381 context = TYPE_CONTEXT (t);
7382 if (context)
7383 context = tsubst_aggr_type (context, args, complain,
7384 in_decl, /*entering_scope=*/1);
7386 /* Then, figure out what arguments are appropriate for the
7387 type we are trying to find. For example, given:
7389 template <class T> struct S;
7390 template <class T, class U> void f(T, U) { S<U> su; }
7392 and supposing that we are instantiating f<int, double>,
7393 then our ARGS will be {int, double}, but, when looking up
7394 S we only want {double}. */
7395 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
7396 complain, in_decl);
7397 if (argvec == error_mark_node)
7398 r = error_mark_node;
7399 else
7401 r = lookup_template_class (t, argvec, in_decl, context,
7402 entering_scope, complain);
7403 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
7406 skip_evaluation = saved_skip_evaluation;
7408 return r;
7410 else
7411 /* This is not a template type, so there's nothing to do. */
7412 return t;
7414 default:
7415 return tsubst (t, args, complain, in_decl);
7419 /* Substitute into the default argument ARG (a default argument for
7420 FN), which has the indicated TYPE. */
7422 tree
7423 tsubst_default_argument (tree fn, tree type, tree arg)
7425 tree saved_class_ptr = NULL_TREE;
7426 tree saved_class_ref = NULL_TREE;
7428 /* This default argument came from a template. Instantiate the
7429 default argument here, not in tsubst. In the case of
7430 something like:
7432 template <class T>
7433 struct S {
7434 static T t();
7435 void f(T = t());
7438 we must be careful to do name lookup in the scope of S<T>,
7439 rather than in the current class. */
7440 push_access_scope (fn);
7441 /* The "this" pointer is not valid in a default argument. */
7442 if (cfun)
7444 saved_class_ptr = current_class_ptr;
7445 cp_function_chain->x_current_class_ptr = NULL_TREE;
7446 saved_class_ref = current_class_ref;
7447 cp_function_chain->x_current_class_ref = NULL_TREE;
7450 push_deferring_access_checks(dk_no_deferred);
7451 /* The default argument expression may cause implicitly defined
7452 member functions to be synthesized, which will result in garbage
7453 collection. We must treat this situation as if we were within
7454 the body of function so as to avoid collecting live data on the
7455 stack. */
7456 ++function_depth;
7457 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
7458 tf_warning_or_error, NULL_TREE,
7459 /*integral_constant_expression_p=*/false);
7460 --function_depth;
7461 pop_deferring_access_checks();
7463 /* Restore the "this" pointer. */
7464 if (cfun)
7466 cp_function_chain->x_current_class_ptr = saved_class_ptr;
7467 cp_function_chain->x_current_class_ref = saved_class_ref;
7470 pop_access_scope (fn);
7472 /* Make sure the default argument is reasonable. */
7473 arg = check_default_argument (type, arg);
7475 return arg;
7478 /* Substitute into all the default arguments for FN. */
7480 static void
7481 tsubst_default_arguments (tree fn)
7483 tree arg;
7484 tree tmpl_args;
7486 tmpl_args = DECL_TI_ARGS (fn);
7488 /* If this function is not yet instantiated, we certainly don't need
7489 its default arguments. */
7490 if (uses_template_parms (tmpl_args))
7491 return;
7493 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
7494 arg;
7495 arg = TREE_CHAIN (arg))
7496 if (TREE_PURPOSE (arg))
7497 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
7498 TREE_VALUE (arg),
7499 TREE_PURPOSE (arg));
7502 /* Substitute the ARGS into the T, which is a _DECL. Return the
7503 result of the substitution. Issue error and warning messages under
7504 control of COMPLAIN. */
7506 static tree
7507 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
7509 location_t saved_loc;
7510 tree r = NULL_TREE;
7511 tree in_decl = t;
7513 /* Set the filename and linenumber to improve error-reporting. */
7514 saved_loc = input_location;
7515 input_location = DECL_SOURCE_LOCATION (t);
7517 switch (TREE_CODE (t))
7519 case TEMPLATE_DECL:
7521 /* We can get here when processing a member function template,
7522 member class template, and template template parameter of
7523 a template class. */
7524 tree decl = DECL_TEMPLATE_RESULT (t);
7525 tree spec;
7526 tree tmpl_args;
7527 tree full_args;
7529 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
7531 /* Template template parameter is treated here. */
7532 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7533 if (new_type == error_mark_node)
7534 return error_mark_node;
7536 r = copy_decl (t);
7537 TREE_CHAIN (r) = NULL_TREE;
7538 TREE_TYPE (r) = new_type;
7539 DECL_TEMPLATE_RESULT (r)
7540 = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
7541 DECL_TEMPLATE_PARMS (r)
7542 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
7543 complain);
7544 TYPE_NAME (new_type) = r;
7545 break;
7548 /* We might already have an instance of this template.
7549 The ARGS are for the surrounding class type, so the
7550 full args contain the tsubst'd args for the context,
7551 plus the innermost args from the template decl. */
7552 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
7553 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
7554 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
7555 /* Because this is a template, the arguments will still be
7556 dependent, even after substitution. If
7557 PROCESSING_TEMPLATE_DECL is not set, the dependency
7558 predicates will short-circuit. */
7559 ++processing_template_decl;
7560 full_args = tsubst_template_args (tmpl_args, args,
7561 complain, in_decl);
7562 --processing_template_decl;
7563 if (full_args == error_mark_node)
7564 return error_mark_node;
7566 /* tsubst_template_args doesn't copy the vector if
7567 nothing changed. But, *something* should have
7568 changed. */
7569 gcc_assert (full_args != tmpl_args);
7571 spec = retrieve_specialization (t, full_args,
7572 /*class_specializations_p=*/true);
7573 if (spec != NULL_TREE)
7575 r = spec;
7576 break;
7579 /* Make a new template decl. It will be similar to the
7580 original, but will record the current template arguments.
7581 We also create a new function declaration, which is just
7582 like the old one, but points to this new template, rather
7583 than the old one. */
7584 r = copy_decl (t);
7585 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
7586 TREE_CHAIN (r) = NULL_TREE;
7588 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
7590 if (TREE_CODE (decl) == TYPE_DECL)
7592 tree new_type;
7593 ++processing_template_decl;
7594 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7595 --processing_template_decl;
7596 if (new_type == error_mark_node)
7597 return error_mark_node;
7599 TREE_TYPE (r) = new_type;
7600 CLASSTYPE_TI_TEMPLATE (new_type) = r;
7601 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
7602 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
7603 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
7605 else
7607 tree new_decl;
7608 ++processing_template_decl;
7609 new_decl = tsubst (decl, args, complain, in_decl);
7610 --processing_template_decl;
7611 if (new_decl == error_mark_node)
7612 return error_mark_node;
7614 DECL_TEMPLATE_RESULT (r) = new_decl;
7615 DECL_TI_TEMPLATE (new_decl) = r;
7616 TREE_TYPE (r) = TREE_TYPE (new_decl);
7617 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
7618 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
7621 SET_DECL_IMPLICIT_INSTANTIATION (r);
7622 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
7623 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
7625 /* The template parameters for this new template are all the
7626 template parameters for the old template, except the
7627 outermost level of parameters. */
7628 DECL_TEMPLATE_PARMS (r)
7629 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
7630 complain);
7632 if (PRIMARY_TEMPLATE_P (t))
7633 DECL_PRIMARY_TEMPLATE (r) = r;
7635 if (TREE_CODE (decl) != TYPE_DECL)
7636 /* Record this non-type partial instantiation. */
7637 register_specialization (r, t,
7638 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
7639 false);
7641 break;
7643 case FUNCTION_DECL:
7645 tree ctx;
7646 tree argvec = NULL_TREE;
7647 tree *friends;
7648 tree gen_tmpl;
7649 tree type;
7650 int member;
7651 int args_depth;
7652 int parms_depth;
7654 /* Nobody should be tsubst'ing into non-template functions. */
7655 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
7657 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
7659 tree spec;
7660 bool dependent_p;
7662 /* If T is not dependent, just return it. We have to
7663 increment PROCESSING_TEMPLATE_DECL because
7664 value_dependent_expression_p assumes that nothing is
7665 dependent when PROCESSING_TEMPLATE_DECL is zero. */
7666 ++processing_template_decl;
7667 dependent_p = value_dependent_expression_p (t);
7668 --processing_template_decl;
7669 if (!dependent_p)
7670 return t;
7672 /* Calculate the most general template of which R is a
7673 specialization, and the complete set of arguments used to
7674 specialize R. */
7675 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
7676 argvec = tsubst_template_args (DECL_TI_ARGS
7677 (DECL_TEMPLATE_RESULT (gen_tmpl)),
7678 args, complain, in_decl);
7680 /* Check to see if we already have this specialization. */
7681 spec = retrieve_specialization (gen_tmpl, argvec,
7682 /*class_specializations_p=*/false);
7684 if (spec)
7686 r = spec;
7687 break;
7690 /* We can see more levels of arguments than parameters if
7691 there was a specialization of a member template, like
7692 this:
7694 template <class T> struct S { template <class U> void f(); }
7695 template <> template <class U> void S<int>::f(U);
7697 Here, we'll be substituting into the specialization,
7698 because that's where we can find the code we actually
7699 want to generate, but we'll have enough arguments for
7700 the most general template.
7702 We also deal with the peculiar case:
7704 template <class T> struct S {
7705 template <class U> friend void f();
7707 template <class U> void f() {}
7708 template S<int>;
7709 template void f<double>();
7711 Here, the ARGS for the instantiation of will be {int,
7712 double}. But, we only need as many ARGS as there are
7713 levels of template parameters in CODE_PATTERN. We are
7714 careful not to get fooled into reducing the ARGS in
7715 situations like:
7717 template <class T> struct S { template <class U> void f(U); }
7718 template <class T> template <> void S<T>::f(int) {}
7720 which we can spot because the pattern will be a
7721 specialization in this case. */
7722 args_depth = TMPL_ARGS_DEPTH (args);
7723 parms_depth =
7724 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
7725 if (args_depth > parms_depth
7726 && !DECL_TEMPLATE_SPECIALIZATION (t))
7727 args = get_innermost_template_args (args, parms_depth);
7729 else
7731 /* This special case arises when we have something like this:
7733 template <class T> struct S {
7734 friend void f<int>(int, double);
7737 Here, the DECL_TI_TEMPLATE for the friend declaration
7738 will be an IDENTIFIER_NODE. We are being called from
7739 tsubst_friend_function, and we want only to create a
7740 new decl (R) with appropriate types so that we can call
7741 determine_specialization. */
7742 gen_tmpl = NULL_TREE;
7745 if (DECL_CLASS_SCOPE_P (t))
7747 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
7748 member = 2;
7749 else
7750 member = 1;
7751 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
7752 complain, t, /*entering_scope=*/1);
7754 else
7756 member = 0;
7757 ctx = DECL_CONTEXT (t);
7759 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7760 if (type == error_mark_node)
7761 return error_mark_node;
7763 /* We do NOT check for matching decls pushed separately at this
7764 point, as they may not represent instantiations of this
7765 template, and in any case are considered separate under the
7766 discrete model. */
7767 r = copy_decl (t);
7768 DECL_USE_TEMPLATE (r) = 0;
7769 TREE_TYPE (r) = type;
7770 /* Clear out the mangled name and RTL for the instantiation. */
7771 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
7772 SET_DECL_RTL (r, NULL_RTX);
7773 DECL_INITIAL (r) = NULL_TREE;
7774 DECL_CONTEXT (r) = ctx;
7776 if (member && DECL_CONV_FN_P (r))
7777 /* Type-conversion operator. Reconstruct the name, in
7778 case it's the name of one of the template's parameters. */
7779 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
7781 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
7782 complain, t);
7783 DECL_RESULT (r) = NULL_TREE;
7785 TREE_STATIC (r) = 0;
7786 TREE_PUBLIC (r) = TREE_PUBLIC (t);
7787 DECL_EXTERNAL (r) = 1;
7788 /* If this is an instantiation of a function with internal
7789 linkage, we already know what object file linkage will be
7790 assigned to the instantiation. */
7791 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
7792 DECL_DEFER_OUTPUT (r) = 0;
7793 TREE_CHAIN (r) = NULL_TREE;
7794 DECL_PENDING_INLINE_INFO (r) = 0;
7795 DECL_PENDING_INLINE_P (r) = 0;
7796 DECL_SAVED_TREE (r) = NULL_TREE;
7797 TREE_USED (r) = 0;
7798 if (DECL_CLONED_FUNCTION (r))
7800 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
7801 args, complain, t);
7802 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
7803 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
7806 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
7807 this in the special friend case mentioned above where
7808 GEN_TMPL is NULL. */
7809 if (gen_tmpl)
7811 DECL_TEMPLATE_INFO (r)
7812 = tree_cons (gen_tmpl, argvec, NULL_TREE);
7813 SET_DECL_IMPLICIT_INSTANTIATION (r);
7814 register_specialization (r, gen_tmpl, argvec, false);
7816 /* We're not supposed to instantiate default arguments
7817 until they are called, for a template. But, for a
7818 declaration like:
7820 template <class T> void f ()
7821 { extern void g(int i = T()); }
7823 we should do the substitution when the template is
7824 instantiated. We handle the member function case in
7825 instantiate_class_template since the default arguments
7826 might refer to other members of the class. */
7827 if (!member
7828 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7829 && !uses_template_parms (argvec))
7830 tsubst_default_arguments (r);
7832 else
7833 DECL_TEMPLATE_INFO (r) = NULL_TREE;
7835 /* Copy the list of befriending classes. */
7836 for (friends = &DECL_BEFRIENDING_CLASSES (r);
7837 *friends;
7838 friends = &TREE_CHAIN (*friends))
7840 *friends = copy_node (*friends);
7841 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
7842 args, complain,
7843 in_decl);
7846 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
7848 maybe_retrofit_in_chrg (r);
7849 if (DECL_CONSTRUCTOR_P (r))
7850 grok_ctor_properties (ctx, r);
7851 /* If this is an instantiation of a member template, clone it.
7852 If it isn't, that'll be handled by
7853 clone_constructors_and_destructors. */
7854 if (PRIMARY_TEMPLATE_P (gen_tmpl))
7855 clone_function_decl (r, /*update_method_vec_p=*/0);
7857 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))
7858 && !grok_op_properties (r, (complain & tf_error) != 0))
7859 return error_mark_node;
7861 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
7862 SET_DECL_FRIEND_CONTEXT (r,
7863 tsubst (DECL_FRIEND_CONTEXT (t),
7864 args, complain, in_decl));
7866 /* Possibly limit visibility based on template args. */
7867 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
7868 if (DECL_VISIBILITY_SPECIFIED (t))
7870 DECL_VISIBILITY_SPECIFIED (r) = 0;
7871 DECL_ATTRIBUTES (r)
7872 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
7874 determine_visibility (r);
7876 break;
7878 case PARM_DECL:
7880 tree type = NULL_TREE;
7881 int i, len = 1;
7882 tree expanded_types = NULL_TREE;
7883 tree prev_r = NULL_TREE;
7884 tree first_r = NULL_TREE;
7886 if (FUNCTION_PARAMETER_PACK_P (t))
7888 /* If there is a local specialization that isn't a
7889 parameter pack, it means that we're doing a "simple"
7890 substitution from inside tsubst_pack_expansion. Just
7891 return the local specialization (which will be a single
7892 parm). */
7893 tree spec = NULL_TREE;
7894 if (local_specializations)
7895 spec = retrieve_local_specialization (t);
7896 if (spec
7897 && TREE_CODE (spec) == PARM_DECL
7898 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
7899 return spec;
7901 /* Expand the TYPE_PACK_EXPANSION that provides the types for
7902 the parameters in this function parameter pack. */
7903 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
7904 complain, in_decl);
7905 if (TREE_CODE (expanded_types) == TREE_VEC)
7907 len = TREE_VEC_LENGTH (expanded_types);
7909 /* Zero-length parameter packs are boring. Just substitute
7910 into the chain. */
7911 if (len == 0)
7912 return tsubst (TREE_CHAIN (t), args, complain,
7913 TREE_CHAIN (t));
7915 else
7917 /* All we did was update the type. Make a note of that. */
7918 type = expanded_types;
7919 expanded_types = NULL_TREE;
7923 /* Loop through all of the parameter's we'll build. When T is
7924 a function parameter pack, LEN is the number of expanded
7925 types in EXPANDED_TYPES; otherwise, LEN is 1. */
7926 r = NULL_TREE;
7927 for (i = 0; i < len; ++i)
7929 prev_r = r;
7930 r = copy_node (t);
7931 if (DECL_TEMPLATE_PARM_P (t))
7932 SET_DECL_TEMPLATE_PARM_P (r);
7934 if (expanded_types)
7935 /* We're on the Ith parameter of the function parameter
7936 pack. */
7938 /* Get the Ith type. */
7939 type = TREE_VEC_ELT (expanded_types, i);
7941 if (DECL_NAME (r))
7942 /* Rename the parameter to include the index. */
7943 DECL_NAME (r) =
7944 make_ith_pack_parameter_name (DECL_NAME (r), i);
7946 else if (!type)
7947 /* We're dealing with a normal parameter. */
7948 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7950 type = type_decays_to (type);
7951 TREE_TYPE (r) = type;
7952 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
7954 if (DECL_INITIAL (r))
7956 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
7957 DECL_INITIAL (r) = TREE_TYPE (r);
7958 else
7959 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
7960 complain, in_decl);
7963 DECL_CONTEXT (r) = NULL_TREE;
7965 if (!DECL_TEMPLATE_PARM_P (r))
7966 DECL_ARG_TYPE (r) = type_passed_as (type);
7968 /* Keep track of the first new parameter we
7969 generate. That's what will be returned to the
7970 caller. */
7971 if (!first_r)
7972 first_r = r;
7974 /* Build a proper chain of parameters when substituting
7975 into a function parameter pack. */
7976 if (prev_r)
7977 TREE_CHAIN (prev_r) = r;
7980 if (TREE_CHAIN (t))
7981 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
7982 complain, TREE_CHAIN (t));
7984 /* FIRST_R contains the start of the chain we've built. */
7985 r = first_r;
7987 break;
7989 case FIELD_DECL:
7991 tree type;
7993 r = copy_decl (t);
7994 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7995 if (type == error_mark_node)
7996 return error_mark_node;
7997 TREE_TYPE (r) = type;
7998 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8000 /* DECL_INITIAL gives the number of bits in a bit-field. */
8001 DECL_INITIAL (r)
8002 = tsubst_expr (DECL_INITIAL (t), args,
8003 complain, in_decl,
8004 /*integral_constant_expression_p=*/true);
8005 /* We don't have to set DECL_CONTEXT here; it is set by
8006 finish_member_declaration. */
8007 TREE_CHAIN (r) = NULL_TREE;
8008 if (VOID_TYPE_P (type))
8009 error ("instantiation of %q+D as type %qT", r, type);
8011 break;
8013 case USING_DECL:
8014 /* We reach here only for member using decls. */
8015 if (DECL_DEPENDENT_P (t))
8017 r = do_class_using_decl
8018 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
8019 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
8020 if (!r)
8021 r = error_mark_node;
8023 else
8025 r = copy_node (t);
8026 TREE_CHAIN (r) = NULL_TREE;
8028 break;
8030 case TYPE_DECL:
8031 case VAR_DECL:
8033 tree argvec = NULL_TREE;
8034 tree gen_tmpl = NULL_TREE;
8035 tree spec;
8036 tree tmpl = NULL_TREE;
8037 tree ctx;
8038 tree type = NULL_TREE;
8039 bool local_p;
8041 if (TREE_CODE (t) == TYPE_DECL)
8043 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8044 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
8045 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
8047 /* If this is the canonical decl, we don't have to
8048 mess with instantiations, and often we can't (for
8049 typename, template type parms and such). Note that
8050 TYPE_NAME is not correct for the above test if
8051 we've copied the type for a typedef. */
8052 r = TYPE_NAME (type);
8053 break;
8057 /* Check to see if we already have the specialization we
8058 need. */
8059 spec = NULL_TREE;
8060 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
8062 /* T is a static data member or namespace-scope entity.
8063 We have to substitute into namespace-scope variables
8064 (even though such entities are never templates) because
8065 of cases like:
8067 template <class T> void f() { extern T t; }
8069 where the entity referenced is not known until
8070 instantiation time. */
8071 local_p = false;
8072 ctx = DECL_CONTEXT (t);
8073 if (DECL_CLASS_SCOPE_P (t))
8075 ctx = tsubst_aggr_type (ctx, args,
8076 complain,
8077 in_decl, /*entering_scope=*/1);
8078 /* If CTX is unchanged, then T is in fact the
8079 specialization we want. That situation occurs when
8080 referencing a static data member within in its own
8081 class. We can use pointer equality, rather than
8082 same_type_p, because DECL_CONTEXT is always
8083 canonical. */
8084 if (ctx == DECL_CONTEXT (t))
8085 spec = t;
8088 if (!spec)
8090 tmpl = DECL_TI_TEMPLATE (t);
8091 gen_tmpl = most_general_template (tmpl);
8092 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
8093 spec = (retrieve_specialization
8094 (gen_tmpl, argvec,
8095 /*class_specializations_p=*/false));
8098 else
8100 /* A local variable. */
8101 local_p = true;
8102 /* Subsequent calls to pushdecl will fill this in. */
8103 ctx = NULL_TREE;
8104 spec = retrieve_local_specialization (t);
8106 /* If we already have the specialization we need, there is
8107 nothing more to do. */
8108 if (spec)
8110 r = spec;
8111 break;
8114 /* Create a new node for the specialization we need. */
8115 r = copy_decl (t);
8116 if (TREE_CODE (r) == VAR_DECL)
8118 /* Even if the original location is out of scope, the
8119 newly substituted one is not. */
8120 DECL_DEAD_FOR_LOCAL (r) = 0;
8121 DECL_INITIALIZED_P (r) = 0;
8122 DECL_TEMPLATE_INSTANTIATED (r) = 0;
8123 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8124 if (type == error_mark_node)
8125 return error_mark_node;
8126 if (TREE_CODE (type) == FUNCTION_TYPE)
8128 /* It may seem that this case cannot occur, since:
8130 typedef void f();
8131 void g() { f x; }
8133 declares a function, not a variable. However:
8135 typedef void f();
8136 template <typename T> void g() { T t; }
8137 template void g<f>();
8139 is an attempt to declare a variable with function
8140 type. */
8141 error ("variable %qD has function type",
8142 /* R is not yet sufficiently initialized, so we
8143 just use its name. */
8144 DECL_NAME (r));
8145 return error_mark_node;
8147 type = complete_type (type);
8148 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
8149 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
8150 type = check_var_type (DECL_NAME (r), type);
8152 if (DECL_HAS_VALUE_EXPR_P (t))
8154 tree ve = DECL_VALUE_EXPR (t);
8155 ve = tsubst_expr (ve, args, complain, in_decl,
8156 /*constant_expression_p=*/false);
8157 SET_DECL_VALUE_EXPR (r, ve);
8160 else if (DECL_SELF_REFERENCE_P (t))
8161 SET_DECL_SELF_REFERENCE_P (r);
8162 TREE_TYPE (r) = type;
8163 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8164 DECL_CONTEXT (r) = ctx;
8165 /* Clear out the mangled name and RTL for the instantiation. */
8166 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
8167 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
8168 SET_DECL_RTL (r, NULL_RTX);
8169 /* The initializer must not be expanded until it is required;
8170 see [temp.inst]. */
8171 DECL_INITIAL (r) = NULL_TREE;
8172 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
8173 SET_DECL_RTL (r, NULL_RTX);
8174 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
8175 if (TREE_CODE (r) == VAR_DECL)
8177 /* Possibly limit visibility based on template args. */
8178 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
8179 if (DECL_VISIBILITY_SPECIFIED (t))
8181 DECL_VISIBILITY_SPECIFIED (r) = 0;
8182 DECL_ATTRIBUTES (r)
8183 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
8185 determine_visibility (r);
8188 if (!local_p)
8190 /* A static data member declaration is always marked
8191 external when it is declared in-class, even if an
8192 initializer is present. We mimic the non-template
8193 processing here. */
8194 DECL_EXTERNAL (r) = 1;
8196 register_specialization (r, gen_tmpl, argvec, false);
8197 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
8198 SET_DECL_IMPLICIT_INSTANTIATION (r);
8200 else
8201 register_local_specialization (r, t);
8203 TREE_CHAIN (r) = NULL_TREE;
8204 layout_decl (r, 0);
8206 break;
8208 default:
8209 gcc_unreachable ();
8212 /* Restore the file and line information. */
8213 input_location = saved_loc;
8215 return r;
8218 /* Substitute into the ARG_TYPES of a function type. */
8220 static tree
8221 tsubst_arg_types (tree arg_types,
8222 tree args,
8223 tsubst_flags_t complain,
8224 tree in_decl)
8226 tree remaining_arg_types;
8227 tree type = NULL_TREE;
8228 int i = 1;
8229 tree expanded_args = NULL_TREE;
8230 tree default_arg;
8232 if (!arg_types || arg_types == void_list_node)
8233 return arg_types;
8235 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
8236 args, complain, in_decl);
8237 if (remaining_arg_types == error_mark_node)
8238 return error_mark_node;
8240 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
8242 /* For a pack expansion, perform substitution on the
8243 entire expression. Later on, we'll handle the arguments
8244 one-by-one. */
8245 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
8246 args, complain, in_decl);
8248 if (TREE_CODE (expanded_args) == TREE_VEC)
8249 /* So that we'll spin through the parameters, one by one. */
8250 i = TREE_VEC_LENGTH (expanded_args);
8251 else
8253 /* We only partially substituted into the parameter
8254 pack. Our type is TYPE_PACK_EXPANSION. */
8255 type = expanded_args;
8256 expanded_args = NULL_TREE;
8260 while (i > 0) {
8261 --i;
8263 if (expanded_args)
8264 type = TREE_VEC_ELT (expanded_args, i);
8265 else if (!type)
8266 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
8268 if (type == error_mark_node)
8269 return error_mark_node;
8270 if (VOID_TYPE_P (type))
8272 if (complain & tf_error)
8274 error ("invalid parameter type %qT", type);
8275 if (in_decl)
8276 error ("in declaration %q+D", in_decl);
8278 return error_mark_node;
8281 /* Do array-to-pointer, function-to-pointer conversion, and ignore
8282 top-level qualifiers as required. */
8283 type = TYPE_MAIN_VARIANT (type_decays_to (type));
8285 /* We do not substitute into default arguments here. The standard
8286 mandates that they be instantiated only when needed, which is
8287 done in build_over_call. */
8288 default_arg = TREE_PURPOSE (arg_types);
8290 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
8292 /* We've instantiated a template before its default arguments
8293 have been parsed. This can happen for a nested template
8294 class, and is not an error unless we require the default
8295 argument in a call of this function. */
8296 remaining_arg_types =
8297 tree_cons (default_arg, type, remaining_arg_types);
8298 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
8299 remaining_arg_types);
8301 else
8302 remaining_arg_types =
8303 hash_tree_cons (default_arg, type, remaining_arg_types);
8306 return remaining_arg_types;
8309 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
8310 *not* handle the exception-specification for FNTYPE, because the
8311 initial substitution of explicitly provided template parameters
8312 during argument deduction forbids substitution into the
8313 exception-specification:
8315 [temp.deduct]
8317 All references in the function type of the function template to the
8318 corresponding template parameters are replaced by the specified tem-
8319 plate argument values. If a substitution in a template parameter or
8320 in the function type of the function template results in an invalid
8321 type, type deduction fails. [Note: The equivalent substitution in
8322 exception specifications is done only when the function is instanti-
8323 ated, at which point a program is ill-formed if the substitution
8324 results in an invalid type.] */
8326 static tree
8327 tsubst_function_type (tree t,
8328 tree args,
8329 tsubst_flags_t complain,
8330 tree in_decl)
8332 tree return_type;
8333 tree arg_types;
8334 tree fntype;
8336 /* The TYPE_CONTEXT is not used for function/method types. */
8337 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
8339 /* Substitute the return type. */
8340 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8341 if (return_type == error_mark_node)
8342 return error_mark_node;
8343 /* The standard does not presently indicate that creation of a
8344 function type with an invalid return type is a deduction failure.
8345 However, that is clearly analogous to creating an array of "void"
8346 or a reference to a reference. This is core issue #486. */
8347 if (TREE_CODE (return_type) == ARRAY_TYPE
8348 || TREE_CODE (return_type) == FUNCTION_TYPE)
8350 if (complain & tf_error)
8352 if (TREE_CODE (return_type) == ARRAY_TYPE)
8353 error ("function returning an array");
8354 else
8355 error ("function returning a function");
8357 return error_mark_node;
8360 /* Substitute the argument types. */
8361 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
8362 complain, in_decl);
8363 if (arg_types == error_mark_node)
8364 return error_mark_node;
8366 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
8367 && in_decl != NULL_TREE
8368 && !TREE_NO_WARNING (in_decl)
8369 && (SCALAR_TYPE_P (return_type) || VOID_TYPE_P (return_type)))
8370 warning (OPT_Wreturn_type,
8371 "type qualifiers ignored on function return type");
8373 /* Construct a new type node and return it. */
8374 if (TREE_CODE (t) == FUNCTION_TYPE)
8375 fntype = build_function_type (return_type, arg_types);
8376 else
8378 tree r = TREE_TYPE (TREE_VALUE (arg_types));
8379 if (! IS_AGGR_TYPE (r))
8381 /* [temp.deduct]
8383 Type deduction may fail for any of the following
8384 reasons:
8386 -- Attempting to create "pointer to member of T" when T
8387 is not a class type. */
8388 if (complain & tf_error)
8389 error ("creating pointer to member function of non-class type %qT",
8391 return error_mark_node;
8394 fntype = build_method_type_directly (r, return_type,
8395 TREE_CHAIN (arg_types));
8397 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
8398 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
8400 return fntype;
8403 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
8404 ARGS into that specification, and return the substituted
8405 specification. If there is no specification, return NULL_TREE. */
8407 static tree
8408 tsubst_exception_specification (tree fntype,
8409 tree args,
8410 tsubst_flags_t complain,
8411 tree in_decl)
8413 tree specs;
8414 tree new_specs;
8416 specs = TYPE_RAISES_EXCEPTIONS (fntype);
8417 new_specs = NULL_TREE;
8418 if (specs)
8420 if (! TREE_VALUE (specs))
8421 new_specs = specs;
8422 else
8423 while (specs)
8425 tree spec;
8426 int i, len = 1;
8427 tree expanded_specs = NULL_TREE;
8429 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
8431 /* Expand the pack expansion type. */
8432 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
8433 args, complain,
8434 in_decl);
8435 len = TREE_VEC_LENGTH (expanded_specs);
8438 for (i = 0; i < len; ++i)
8440 if (expanded_specs)
8441 spec = TREE_VEC_ELT (expanded_specs, i);
8442 else
8443 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
8444 if (spec == error_mark_node)
8445 return spec;
8446 new_specs = add_exception_specifier (new_specs, spec,
8447 complain);
8450 specs = TREE_CHAIN (specs);
8453 return new_specs;
8456 /* Take the tree structure T and replace template parameters used
8457 therein with the argument vector ARGS. IN_DECL is an associated
8458 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
8459 Issue error and warning messages under control of COMPLAIN. Note
8460 that we must be relatively non-tolerant of extensions here, in
8461 order to preserve conformance; if we allow substitutions that
8462 should not be allowed, we may allow argument deductions that should
8463 not succeed, and therefore report ambiguous overload situations
8464 where there are none. In theory, we could allow the substitution,
8465 but indicate that it should have failed, and allow our caller to
8466 make sure that the right thing happens, but we don't try to do this
8467 yet.
8469 This function is used for dealing with types, decls and the like;
8470 for expressions, use tsubst_expr or tsubst_copy. */
8472 static tree
8473 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8475 tree type, r;
8477 if (t == NULL_TREE || t == error_mark_node
8478 || t == integer_type_node
8479 || t == void_type_node
8480 || t == char_type_node
8481 || t == unknown_type_node
8482 || TREE_CODE (t) == NAMESPACE_DECL)
8483 return t;
8485 if (DECL_P (t))
8486 return tsubst_decl (t, args, complain);
8488 if (TREE_CODE (t) == IDENTIFIER_NODE)
8489 type = IDENTIFIER_TYPE_VALUE (t);
8490 else
8491 type = TREE_TYPE (t);
8493 gcc_assert (type != unknown_type_node);
8495 if (type
8496 && TREE_CODE (t) != TYPENAME_TYPE
8497 && TREE_CODE (t) != IDENTIFIER_NODE
8498 && TREE_CODE (t) != FUNCTION_TYPE
8499 && TREE_CODE (t) != METHOD_TYPE)
8500 type = tsubst (type, args, complain, in_decl);
8501 if (type == error_mark_node)
8502 return error_mark_node;
8504 switch (TREE_CODE (t))
8506 case RECORD_TYPE:
8507 case UNION_TYPE:
8508 case ENUMERAL_TYPE:
8509 return tsubst_aggr_type (t, args, complain, in_decl,
8510 /*entering_scope=*/0);
8512 case ERROR_MARK:
8513 case IDENTIFIER_NODE:
8514 case VOID_TYPE:
8515 case REAL_TYPE:
8516 case COMPLEX_TYPE:
8517 case VECTOR_TYPE:
8518 case BOOLEAN_TYPE:
8519 case INTEGER_CST:
8520 case REAL_CST:
8521 case STRING_CST:
8522 return t;
8524 case INTEGER_TYPE:
8525 if (t == integer_type_node)
8526 return t;
8528 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
8529 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
8530 return t;
8533 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
8535 max = tsubst_expr (omax, args, complain, in_decl,
8536 /*integral_constant_expression_p=*/false);
8537 max = fold_decl_constant_value (max);
8539 if (TREE_CODE (max) != INTEGER_CST
8540 && TREE_CODE (max) != TEMPLATE_PARM_INDEX
8541 && !at_function_scope_p ())
8543 if (complain & tf_error)
8544 error ("array bound is not an integer constant");
8545 return error_mark_node;
8548 /* [temp.deduct]
8550 Type deduction may fail for any of the following
8551 reasons:
8553 Attempting to create an array with a size that is
8554 zero or negative. */
8555 if (integer_zerop (max) && !(complain & tf_error))
8556 /* We must fail if performing argument deduction (as
8557 indicated by the state of complain), so that
8558 another substitution can be found. */
8559 return error_mark_node;
8560 else if (TREE_CODE (max) == INTEGER_CST
8561 && INT_CST_LT (max, integer_zero_node))
8563 if (complain & tf_error)
8564 error ("creating array with negative size (%qE)", max);
8566 return error_mark_node;
8569 return compute_array_index_type (NULL_TREE, max);
8572 case TEMPLATE_TYPE_PARM:
8573 case TEMPLATE_TEMPLATE_PARM:
8574 case BOUND_TEMPLATE_TEMPLATE_PARM:
8575 case TEMPLATE_PARM_INDEX:
8577 int idx;
8578 int level;
8579 int levels;
8580 tree arg = NULL_TREE;
8582 r = NULL_TREE;
8584 gcc_assert (TREE_VEC_LENGTH (args) > 0);
8585 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
8586 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
8587 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
8589 idx = TEMPLATE_TYPE_IDX (t);
8590 level = TEMPLATE_TYPE_LEVEL (t);
8592 else
8594 idx = TEMPLATE_PARM_IDX (t);
8595 level = TEMPLATE_PARM_LEVEL (t);
8598 levels = TMPL_ARGS_DEPTH (args);
8599 if (level <= levels)
8601 arg = TMPL_ARG (args, level, idx);
8603 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
8604 /* See through ARGUMENT_PACK_SELECT arguments. */
8605 arg = ARGUMENT_PACK_SELECT_ARG (arg);
8608 if (arg == error_mark_node)
8609 return error_mark_node;
8610 else if (arg != NULL_TREE)
8612 if (ARGUMENT_PACK_P (arg))
8613 /* If ARG is an argument pack, we don't actually want to
8614 perform a substitution here, because substitutions
8615 for argument packs are only done
8616 element-by-element. We can get to this point when
8617 substituting the type of a non-type template
8618 parameter pack, when that type actually contains
8619 template parameter packs from an outer template, e.g.,
8621 template<typename... Types> struct A {
8622 template<Types... Values> struct B { };
8623 }; */
8624 return t;
8626 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
8628 int quals;
8629 gcc_assert (TYPE_P (arg));
8631 /* cv-quals from the template are discarded when
8632 substituting in a function or reference type. */
8633 if (TREE_CODE (arg) == FUNCTION_TYPE
8634 || TREE_CODE (arg) == METHOD_TYPE
8635 || TREE_CODE (arg) == REFERENCE_TYPE)
8636 quals = cp_type_quals (arg);
8637 else
8638 quals = cp_type_quals (arg) | cp_type_quals (t);
8640 return cp_build_qualified_type_real
8641 (arg, quals, complain | tf_ignore_bad_quals);
8643 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
8645 /* We are processing a type constructed from a
8646 template template parameter. */
8647 tree argvec = tsubst (TYPE_TI_ARGS (t),
8648 args, complain, in_decl);
8649 if (argvec == error_mark_node)
8650 return error_mark_node;
8652 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
8653 are resolving nested-types in the signature of a
8654 member function templates. Otherwise ARG is a
8655 TEMPLATE_DECL and is the real template to be
8656 instantiated. */
8657 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
8658 arg = TYPE_NAME (arg);
8660 r = lookup_template_class (arg,
8661 argvec, in_decl,
8662 DECL_CONTEXT (arg),
8663 /*entering_scope=*/0,
8664 complain);
8665 return cp_build_qualified_type_real
8666 (r, TYPE_QUALS (t), complain);
8668 else
8669 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
8670 return arg;
8673 if (level == 1)
8674 /* This can happen during the attempted tsubst'ing in
8675 unify. This means that we don't yet have any information
8676 about the template parameter in question. */
8677 return t;
8679 /* If we get here, we must have been looking at a parm for a
8680 more deeply nested template. Make a new version of this
8681 template parameter, but with a lower level. */
8682 switch (TREE_CODE (t))
8684 case TEMPLATE_TYPE_PARM:
8685 case TEMPLATE_TEMPLATE_PARM:
8686 case BOUND_TEMPLATE_TEMPLATE_PARM:
8687 if (cp_type_quals (t))
8689 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
8690 r = cp_build_qualified_type_real
8691 (r, cp_type_quals (t),
8692 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
8693 ? tf_ignore_bad_quals : 0));
8695 else
8697 r = copy_type (t);
8698 TEMPLATE_TYPE_PARM_INDEX (r)
8699 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
8700 r, levels);
8701 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
8702 TYPE_MAIN_VARIANT (r) = r;
8703 TYPE_POINTER_TO (r) = NULL_TREE;
8704 TYPE_REFERENCE_TO (r) = NULL_TREE;
8706 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
8707 /* We have reduced the level of the template
8708 template parameter, but not the levels of its
8709 template parameters, so canonical_type_parameter
8710 will not be able to find the canonical template
8711 template parameter for this level. Thus, we
8712 require structural equality checking to compare
8713 TEMPLATE_TEMPLATE_PARMs. */
8714 SET_TYPE_STRUCTURAL_EQUALITY (r);
8715 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
8716 SET_TYPE_STRUCTURAL_EQUALITY (r);
8717 else
8718 TYPE_CANONICAL (r) = canonical_type_parameter (r);
8720 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
8722 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
8723 complain, in_decl);
8724 if (argvec == error_mark_node)
8725 return error_mark_node;
8727 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
8728 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
8731 break;
8733 case TEMPLATE_PARM_INDEX:
8734 r = reduce_template_parm_level (t, type, levels);
8735 break;
8737 default:
8738 gcc_unreachable ();
8741 return r;
8744 case TREE_LIST:
8746 tree purpose, value, chain;
8748 if (t == void_list_node)
8749 return t;
8751 purpose = TREE_PURPOSE (t);
8752 if (purpose)
8754 purpose = tsubst (purpose, args, complain, in_decl);
8755 if (purpose == error_mark_node)
8756 return error_mark_node;
8758 value = TREE_VALUE (t);
8759 if (value)
8761 value = tsubst (value, args, complain, in_decl);
8762 if (value == error_mark_node)
8763 return error_mark_node;
8765 chain = TREE_CHAIN (t);
8766 if (chain && chain != void_type_node)
8768 chain = tsubst (chain, args, complain, in_decl);
8769 if (chain == error_mark_node)
8770 return error_mark_node;
8772 if (purpose == TREE_PURPOSE (t)
8773 && value == TREE_VALUE (t)
8774 && chain == TREE_CHAIN (t))
8775 return t;
8776 return hash_tree_cons (purpose, value, chain);
8779 case TREE_BINFO:
8780 /* We should never be tsubsting a binfo. */
8781 gcc_unreachable ();
8783 case TREE_VEC:
8784 /* A vector of template arguments. */
8785 gcc_assert (!type);
8786 return tsubst_template_args (t, args, complain, in_decl);
8788 case POINTER_TYPE:
8789 case REFERENCE_TYPE:
8791 enum tree_code code;
8793 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
8794 return t;
8796 code = TREE_CODE (t);
8799 /* [temp.deduct]
8801 Type deduction may fail for any of the following
8802 reasons:
8804 -- Attempting to create a pointer to reference type.
8805 -- Attempting to create a reference to a reference type or
8806 a reference to void.
8808 Core issue 106 says that creating a reference to a reference
8809 during instantiation is no longer a cause for failure. We
8810 only enforce this check in strict C++98 mode. */
8811 if ((TREE_CODE (type) == REFERENCE_TYPE
8812 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
8813 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
8815 static location_t last_loc;
8817 /* We keep track of the last time we issued this error
8818 message to avoid spewing a ton of messages during a
8819 single bad template instantiation. */
8820 if (complain & tf_error
8821 #ifdef USE_MAPPED_LOCATION
8822 && last_loc != input_location
8823 #else
8824 && (last_loc.line != input_line
8825 || last_loc.file != input_filename)
8826 #endif
8829 if (TREE_CODE (type) == VOID_TYPE)
8830 error ("forming reference to void");
8831 else
8832 error ("forming %s to reference type %qT",
8833 (code == POINTER_TYPE) ? "pointer" : "reference",
8834 type);
8835 last_loc = input_location;
8838 return error_mark_node;
8840 else if (code == POINTER_TYPE)
8842 r = build_pointer_type (type);
8843 if (TREE_CODE (type) == METHOD_TYPE)
8844 r = build_ptrmemfunc_type (r);
8846 else if (TREE_CODE (type) == REFERENCE_TYPE)
8847 /* In C++0x, during template argument substitution, when there is an
8848 attempt to create a reference to a reference type, reference
8849 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
8851 "If a template-argument for a template-parameter T names a type
8852 that is a reference to a type A, an attempt to create the type
8853 'lvalue reference to cv T' creates the type 'lvalue reference to
8854 A,' while an attempt to create the type type rvalue reference to
8855 cv T' creates the type T"
8857 r = cp_build_reference_type
8858 (TREE_TYPE (type),
8859 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
8860 else
8861 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
8862 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
8864 if (r != error_mark_node)
8865 /* Will this ever be needed for TYPE_..._TO values? */
8866 layout_type (r);
8868 return r;
8870 case OFFSET_TYPE:
8872 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
8873 if (r == error_mark_node || !IS_AGGR_TYPE (r))
8875 /* [temp.deduct]
8877 Type deduction may fail for any of the following
8878 reasons:
8880 -- Attempting to create "pointer to member of T" when T
8881 is not a class type. */
8882 if (complain & tf_error)
8883 error ("creating pointer to member of non-class type %qT", r);
8884 return error_mark_node;
8886 if (TREE_CODE (type) == REFERENCE_TYPE)
8888 if (complain & tf_error)
8889 error ("creating pointer to member reference type %qT", type);
8890 return error_mark_node;
8892 if (TREE_CODE (type) == VOID_TYPE)
8894 if (complain & tf_error)
8895 error ("creating pointer to member of type void");
8896 return error_mark_node;
8898 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
8899 if (TREE_CODE (type) == FUNCTION_TYPE)
8901 /* The type of the implicit object parameter gets its
8902 cv-qualifiers from the FUNCTION_TYPE. */
8903 tree method_type;
8904 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
8905 cp_type_quals (type));
8906 tree memptr;
8907 method_type = build_method_type_directly (this_type,
8908 TREE_TYPE (type),
8909 TYPE_ARG_TYPES (type));
8910 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
8911 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
8912 complain);
8914 else
8915 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
8916 TYPE_QUALS (t),
8917 complain);
8919 case FUNCTION_TYPE:
8920 case METHOD_TYPE:
8922 tree fntype;
8923 tree specs;
8924 fntype = tsubst_function_type (t, args, complain, in_decl);
8925 if (fntype == error_mark_node)
8926 return error_mark_node;
8928 /* Substitute the exception specification. */
8929 specs = tsubst_exception_specification (t, args, complain,
8930 in_decl);
8931 if (specs == error_mark_node)
8932 return error_mark_node;
8933 if (specs)
8934 fntype = build_exception_variant (fntype, specs);
8935 return fntype;
8937 case ARRAY_TYPE:
8939 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
8940 if (domain == error_mark_node)
8941 return error_mark_node;
8943 /* As an optimization, we avoid regenerating the array type if
8944 it will obviously be the same as T. */
8945 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8946 return t;
8948 /* These checks should match the ones in grokdeclarator.
8950 [temp.deduct]
8952 The deduction may fail for any of the following reasons:
8954 -- Attempting to create an array with an element type that
8955 is void, a function type, or a reference type, or [DR337]
8956 an abstract class type. */
8957 if (TREE_CODE (type) == VOID_TYPE
8958 || TREE_CODE (type) == FUNCTION_TYPE
8959 || TREE_CODE (type) == REFERENCE_TYPE)
8961 if (complain & tf_error)
8962 error ("creating array of %qT", type);
8963 return error_mark_node;
8965 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
8967 if (complain & tf_error)
8968 error ("creating array of %qT, which is an abstract class type",
8969 type);
8970 return error_mark_node;
8973 r = build_cplus_array_type (type, domain);
8974 return r;
8977 case PLUS_EXPR:
8978 case MINUS_EXPR:
8980 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
8981 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
8983 if (e1 == error_mark_node || e2 == error_mark_node)
8984 return error_mark_node;
8986 return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
8989 case NEGATE_EXPR:
8990 case NOP_EXPR:
8992 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
8993 if (e == error_mark_node)
8994 return error_mark_node;
8996 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
8999 case TYPENAME_TYPE:
9001 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
9002 in_decl, /*entering_scope=*/1);
9003 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
9004 complain, in_decl);
9006 if (ctx == error_mark_node || f == error_mark_node)
9007 return error_mark_node;
9009 if (!IS_AGGR_TYPE (ctx))
9011 if (complain & tf_error)
9012 error ("%qT is not a class, struct, or union type", ctx);
9013 return error_mark_node;
9015 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
9017 /* Normally, make_typename_type does not require that the CTX
9018 have complete type in order to allow things like:
9020 template <class T> struct S { typename S<T>::X Y; };
9022 But, such constructs have already been resolved by this
9023 point, so here CTX really should have complete type, unless
9024 it's a partial instantiation. */
9025 ctx = complete_type (ctx);
9026 if (!COMPLETE_TYPE_P (ctx))
9028 if (complain & tf_error)
9029 cxx_incomplete_type_error (NULL_TREE, ctx);
9030 return error_mark_node;
9034 f = make_typename_type (ctx, f, typename_type,
9035 (complain & tf_error) | tf_keep_type_decl);
9036 if (f == error_mark_node)
9037 return f;
9038 if (TREE_CODE (f) == TYPE_DECL)
9040 complain |= tf_ignore_bad_quals;
9041 f = TREE_TYPE (f);
9044 if (TREE_CODE (f) != TYPENAME_TYPE)
9046 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
9047 error ("%qT resolves to %qT, which is not an enumeration type",
9048 t, f);
9049 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
9050 error ("%qT resolves to %qT, which is is not a class type",
9051 t, f);
9054 return cp_build_qualified_type_real
9055 (f, cp_type_quals (f) | cp_type_quals (t), complain);
9058 case UNBOUND_CLASS_TEMPLATE:
9060 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
9061 in_decl, /*entering_scope=*/1);
9062 tree name = TYPE_IDENTIFIER (t);
9063 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
9065 if (ctx == error_mark_node || name == error_mark_node)
9066 return error_mark_node;
9068 if (parm_list)
9069 parm_list = tsubst_template_parms (parm_list, args, complain);
9070 return make_unbound_class_template (ctx, name, parm_list, complain);
9073 case INDIRECT_REF:
9074 case ADDR_EXPR:
9075 case CALL_EXPR:
9076 gcc_unreachable ();
9078 case ARRAY_REF:
9080 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9081 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
9082 /*integral_constant_expression_p=*/false);
9083 if (e1 == error_mark_node || e2 == error_mark_node)
9084 return error_mark_node;
9086 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
9089 case SCOPE_REF:
9091 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9092 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
9093 if (e1 == error_mark_node || e2 == error_mark_node)
9094 return error_mark_node;
9096 return build_qualified_name (/*type=*/NULL_TREE,
9097 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
9100 case TYPEOF_TYPE:
9102 tree type;
9104 type = finish_typeof (tsubst_expr
9105 (TYPEOF_TYPE_EXPR (t), args,
9106 complain, in_decl,
9107 /*integral_constant_expression_p=*/false));
9108 return cp_build_qualified_type_real (type,
9109 cp_type_quals (t)
9110 | cp_type_quals (type),
9111 complain);
9114 case DECLTYPE_TYPE:
9116 tree type;
9118 type =
9119 finish_decltype_type (tsubst_expr
9120 (DECLTYPE_TYPE_EXPR (t), args,
9121 complain, in_decl,
9122 /*integral_constant_expression_p=*/false),
9123 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
9124 return cp_build_qualified_type_real (type,
9125 cp_type_quals (t)
9126 | cp_type_quals (type),
9127 complain);
9130 case TYPE_ARGUMENT_PACK:
9131 case NONTYPE_ARGUMENT_PACK:
9133 tree r = make_node (TREE_CODE (t));
9134 tree packed_out =
9135 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
9136 args,
9137 complain,
9138 in_decl);
9139 SET_ARGUMENT_PACK_ARGS (r, packed_out);
9141 /* For template nontype argument packs, also substitute into
9142 the type. */
9143 if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
9144 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
9146 return r;
9148 break;
9150 default:
9151 sorry ("use of %qs in template",
9152 tree_code_name [(int) TREE_CODE (t)]);
9153 return error_mark_node;
9157 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
9158 type of the expression on the left-hand side of the "." or "->"
9159 operator. */
9161 static tree
9162 tsubst_baselink (tree baselink, tree object_type,
9163 tree args, tsubst_flags_t complain, tree in_decl)
9165 tree name;
9166 tree qualifying_scope;
9167 tree fns;
9168 tree optype;
9169 tree template_args = 0;
9170 bool template_id_p = false;
9172 /* A baselink indicates a function from a base class. Both the
9173 BASELINK_ACCESS_BINFO and the base class referenced may
9174 indicate bases of the template class, rather than the
9175 instantiated class. In addition, lookups that were not
9176 ambiguous before may be ambiguous now. Therefore, we perform
9177 the lookup again. */
9178 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
9179 qualifying_scope = tsubst (qualifying_scope, args,
9180 complain, in_decl);
9181 fns = BASELINK_FUNCTIONS (baselink);
9182 optype = BASELINK_OPTYPE (baselink);
9183 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
9185 template_id_p = true;
9186 template_args = TREE_OPERAND (fns, 1);
9187 fns = TREE_OPERAND (fns, 0);
9188 if (template_args)
9189 template_args = tsubst_template_args (template_args, args,
9190 complain, in_decl);
9192 name = DECL_NAME (get_first_fn (fns));
9193 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
9195 /* If lookup found a single function, mark it as used at this
9196 point. (If it lookup found multiple functions the one selected
9197 later by overload resolution will be marked as used at that
9198 point.) */
9199 if (BASELINK_P (baselink))
9200 fns = BASELINK_FUNCTIONS (baselink);
9201 if (!template_id_p && !really_overloaded_fn (fns))
9202 mark_used (OVL_CURRENT (fns));
9204 /* Add back the template arguments, if present. */
9205 if (BASELINK_P (baselink) && template_id_p)
9206 BASELINK_FUNCTIONS (baselink)
9207 = build_nt (TEMPLATE_ID_EXPR,
9208 BASELINK_FUNCTIONS (baselink),
9209 template_args);
9210 /* Update the conversion operator type. */
9211 BASELINK_OPTYPE (baselink)
9212 = tsubst (optype, args, complain, in_decl);
9214 if (!object_type)
9215 object_type = current_class_type;
9216 return adjust_result_of_qualified_name_lookup (baselink,
9217 qualifying_scope,
9218 object_type);
9221 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
9222 true if the qualified-id will be a postfix-expression in-and-of
9223 itself; false if more of the postfix-expression follows the
9224 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
9225 of "&". */
9227 static tree
9228 tsubst_qualified_id (tree qualified_id, tree args,
9229 tsubst_flags_t complain, tree in_decl,
9230 bool done, bool address_p)
9232 tree expr;
9233 tree scope;
9234 tree name;
9235 bool is_template;
9236 tree template_args;
9238 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
9240 /* Figure out what name to look up. */
9241 name = TREE_OPERAND (qualified_id, 1);
9242 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
9244 is_template = true;
9245 template_args = TREE_OPERAND (name, 1);
9246 if (template_args)
9247 template_args = tsubst_template_args (template_args, args,
9248 complain, in_decl);
9249 name = TREE_OPERAND (name, 0);
9251 else
9253 is_template = false;
9254 template_args = NULL_TREE;
9257 /* Substitute into the qualifying scope. When there are no ARGS, we
9258 are just trying to simplify a non-dependent expression. In that
9259 case the qualifying scope may be dependent, and, in any case,
9260 substituting will not help. */
9261 scope = TREE_OPERAND (qualified_id, 0);
9262 if (args)
9264 scope = tsubst (scope, args, complain, in_decl);
9265 expr = tsubst_copy (name, args, complain, in_decl);
9267 else
9268 expr = name;
9270 if (dependent_type_p (scope))
9271 return build_qualified_name (/*type=*/NULL_TREE,
9272 scope, expr,
9273 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
9275 if (!BASELINK_P (name) && !DECL_P (expr))
9277 if (TREE_CODE (expr) == BIT_NOT_EXPR)
9278 /* If this were actually a destructor call, it would have been
9279 parsed as such by the parser. */
9280 expr = error_mark_node;
9281 else
9282 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
9283 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
9284 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
9286 if (complain & tf_error)
9288 error ("dependent-name %qE is parsed as a non-type, but "
9289 "instantiation yields a type", qualified_id);
9290 inform ("say %<typename %E%> if a type is meant", qualified_id);
9292 return error_mark_node;
9296 if (DECL_P (expr))
9298 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
9299 scope);
9300 /* Remember that there was a reference to this entity. */
9301 mark_used (expr);
9304 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
9306 if (complain & tf_error)
9307 qualified_name_lookup_error (scope,
9308 TREE_OPERAND (qualified_id, 1),
9309 expr);
9310 return error_mark_node;
9313 if (is_template)
9314 expr = lookup_template_function (expr, template_args);
9316 if (expr == error_mark_node && complain & tf_error)
9317 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
9318 expr);
9319 else if (TYPE_P (scope))
9321 expr = (adjust_result_of_qualified_name_lookup
9322 (expr, scope, current_class_type));
9323 expr = (finish_qualified_id_expr
9324 (scope, expr, done, address_p,
9325 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
9326 /*template_arg_p=*/false));
9329 /* Expressions do not generally have reference type. */
9330 if (TREE_CODE (expr) != SCOPE_REF
9331 /* However, if we're about to form a pointer-to-member, we just
9332 want the referenced member referenced. */
9333 && TREE_CODE (expr) != OFFSET_REF)
9334 expr = convert_from_reference (expr);
9336 return expr;
9339 /* Like tsubst, but deals with expressions. This function just replaces
9340 template parms; to finish processing the resultant expression, use
9341 tsubst_expr. */
9343 static tree
9344 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9346 enum tree_code code;
9347 tree r;
9349 if (t == NULL_TREE || t == error_mark_node)
9350 return t;
9352 code = TREE_CODE (t);
9354 switch (code)
9356 case PARM_DECL:
9357 r = retrieve_local_specialization (t);
9358 gcc_assert (r != NULL);
9359 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
9360 r = ARGUMENT_PACK_SELECT_ARG (r);
9361 mark_used (r);
9362 return r;
9364 case CONST_DECL:
9366 tree enum_type;
9367 tree v;
9369 if (DECL_TEMPLATE_PARM_P (t))
9370 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
9371 /* There is no need to substitute into namespace-scope
9372 enumerators. */
9373 if (DECL_NAMESPACE_SCOPE_P (t))
9374 return t;
9375 /* If ARGS is NULL, then T is known to be non-dependent. */
9376 if (args == NULL_TREE)
9377 return integral_constant_value (t);
9379 /* Unfortunately, we cannot just call lookup_name here.
9380 Consider:
9382 template <int I> int f() {
9383 enum E { a = I };
9384 struct S { void g() { E e = a; } };
9387 When we instantiate f<7>::S::g(), say, lookup_name is not
9388 clever enough to find f<7>::a. */
9389 enum_type
9390 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
9391 /*entering_scope=*/0);
9393 for (v = TYPE_VALUES (enum_type);
9394 v != NULL_TREE;
9395 v = TREE_CHAIN (v))
9396 if (TREE_PURPOSE (v) == DECL_NAME (t))
9397 return TREE_VALUE (v);
9399 /* We didn't find the name. That should never happen; if
9400 name-lookup found it during preliminary parsing, we
9401 should find it again here during instantiation. */
9402 gcc_unreachable ();
9404 return t;
9406 case FIELD_DECL:
9407 if (DECL_CONTEXT (t))
9409 tree ctx;
9411 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
9412 /*entering_scope=*/1);
9413 if (ctx != DECL_CONTEXT (t))
9415 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
9416 if (!r)
9418 if (complain & tf_error)
9419 error ("using invalid field %qD", t);
9420 return error_mark_node;
9422 return r;
9426 return t;
9428 case VAR_DECL:
9429 case FUNCTION_DECL:
9430 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9431 || local_variable_p (t))
9432 t = tsubst (t, args, complain, in_decl);
9433 mark_used (t);
9434 return t;
9436 case BASELINK:
9437 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
9439 case TEMPLATE_DECL:
9440 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9441 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
9442 args, complain, in_decl);
9443 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
9444 return tsubst (t, args, complain, in_decl);
9445 else if (DECL_CLASS_SCOPE_P (t)
9446 && uses_template_parms (DECL_CONTEXT (t)))
9448 /* Template template argument like the following example need
9449 special treatment:
9451 template <template <class> class TT> struct C {};
9452 template <class T> struct D {
9453 template <class U> struct E {};
9454 C<E> c; // #1
9456 D<int> d; // #2
9458 We are processing the template argument `E' in #1 for
9459 the template instantiation #2. Originally, `E' is a
9460 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
9461 have to substitute this with one having context `D<int>'. */
9463 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
9464 return lookup_field (context, DECL_NAME(t), 0, false);
9466 else
9467 /* Ordinary template template argument. */
9468 return t;
9470 case CAST_EXPR:
9471 case REINTERPRET_CAST_EXPR:
9472 case CONST_CAST_EXPR:
9473 case STATIC_CAST_EXPR:
9474 case DYNAMIC_CAST_EXPR:
9475 case NOP_EXPR:
9476 return build1
9477 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
9478 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
9480 case SIZEOF_EXPR:
9481 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
9483 /* We only want to compute the number of arguments. */
9484 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
9485 complain, in_decl);
9486 return build_int_cst (size_type_node, TREE_VEC_LENGTH (expanded));
9488 /* Fall through */
9490 case INDIRECT_REF:
9491 case NEGATE_EXPR:
9492 case TRUTH_NOT_EXPR:
9493 case BIT_NOT_EXPR:
9494 case ADDR_EXPR:
9495 case UNARY_PLUS_EXPR: /* Unary + */
9496 case ALIGNOF_EXPR:
9497 case ARROW_EXPR:
9498 case THROW_EXPR:
9499 case TYPEID_EXPR:
9500 case REALPART_EXPR:
9501 case IMAGPART_EXPR:
9502 return build1
9503 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
9504 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
9506 case COMPONENT_REF:
9508 tree object;
9509 tree name;
9511 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
9512 name = TREE_OPERAND (t, 1);
9513 if (TREE_CODE (name) == BIT_NOT_EXPR)
9515 name = tsubst_copy (TREE_OPERAND (name, 0), args,
9516 complain, in_decl);
9517 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
9519 else if (TREE_CODE (name) == SCOPE_REF
9520 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
9522 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
9523 complain, in_decl);
9524 name = TREE_OPERAND (name, 1);
9525 name = tsubst_copy (TREE_OPERAND (name, 0), args,
9526 complain, in_decl);
9527 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
9528 name = build_qualified_name (/*type=*/NULL_TREE,
9529 base, name,
9530 /*template_p=*/false);
9532 else if (TREE_CODE (name) == BASELINK)
9533 name = tsubst_baselink (name,
9534 non_reference (TREE_TYPE (object)),
9535 args, complain,
9536 in_decl);
9537 else
9538 name = tsubst_copy (name, args, complain, in_decl);
9539 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
9542 case PLUS_EXPR:
9543 case MINUS_EXPR:
9544 case MULT_EXPR:
9545 case TRUNC_DIV_EXPR:
9546 case CEIL_DIV_EXPR:
9547 case FLOOR_DIV_EXPR:
9548 case ROUND_DIV_EXPR:
9549 case EXACT_DIV_EXPR:
9550 case BIT_AND_EXPR:
9551 case BIT_IOR_EXPR:
9552 case BIT_XOR_EXPR:
9553 case TRUNC_MOD_EXPR:
9554 case FLOOR_MOD_EXPR:
9555 case TRUTH_ANDIF_EXPR:
9556 case TRUTH_ORIF_EXPR:
9557 case TRUTH_AND_EXPR:
9558 case TRUTH_OR_EXPR:
9559 case RSHIFT_EXPR:
9560 case LSHIFT_EXPR:
9561 case RROTATE_EXPR:
9562 case LROTATE_EXPR:
9563 case EQ_EXPR:
9564 case NE_EXPR:
9565 case MAX_EXPR:
9566 case MIN_EXPR:
9567 case LE_EXPR:
9568 case GE_EXPR:
9569 case LT_EXPR:
9570 case GT_EXPR:
9571 case COMPOUND_EXPR:
9572 case DOTSTAR_EXPR:
9573 case MEMBER_REF:
9574 case PREDECREMENT_EXPR:
9575 case PREINCREMENT_EXPR:
9576 case POSTDECREMENT_EXPR:
9577 case POSTINCREMENT_EXPR:
9578 return build_nt
9579 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9580 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
9582 case SCOPE_REF:
9583 return build_qualified_name (/*type=*/NULL_TREE,
9584 tsubst_copy (TREE_OPERAND (t, 0),
9585 args, complain, in_decl),
9586 tsubst_copy (TREE_OPERAND (t, 1),
9587 args, complain, in_decl),
9588 QUALIFIED_NAME_IS_TEMPLATE (t));
9590 case ARRAY_REF:
9591 return build_nt
9592 (ARRAY_REF,
9593 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9594 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
9595 NULL_TREE, NULL_TREE);
9597 case CALL_EXPR:
9599 int n = VL_EXP_OPERAND_LENGTH (t);
9600 tree result = build_vl_exp (CALL_EXPR, n);
9601 int i;
9602 for (i = 0; i < n; i++)
9603 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
9604 complain, in_decl);
9605 return result;
9608 case COND_EXPR:
9609 case MODOP_EXPR:
9610 case PSEUDO_DTOR_EXPR:
9612 r = build_nt
9613 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9614 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
9615 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
9616 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
9617 return r;
9620 case NEW_EXPR:
9622 r = build_nt
9623 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9624 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
9625 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
9626 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
9627 return r;
9630 case DELETE_EXPR:
9632 r = build_nt
9633 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
9634 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
9635 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
9636 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
9637 return r;
9640 case TEMPLATE_ID_EXPR:
9642 /* Substituted template arguments */
9643 tree fn = TREE_OPERAND (t, 0);
9644 tree targs = TREE_OPERAND (t, 1);
9646 fn = tsubst_copy (fn, args, complain, in_decl);
9647 if (targs)
9648 targs = tsubst_template_args (targs, args, complain, in_decl);
9650 return lookup_template_function (fn, targs);
9653 case TREE_LIST:
9655 tree purpose, value, chain;
9657 if (t == void_list_node)
9658 return t;
9660 purpose = TREE_PURPOSE (t);
9661 if (purpose)
9662 purpose = tsubst_copy (purpose, args, complain, in_decl);
9663 value = TREE_VALUE (t);
9664 if (value)
9665 value = tsubst_copy (value, args, complain, in_decl);
9666 chain = TREE_CHAIN (t);
9667 if (chain && chain != void_type_node)
9668 chain = tsubst_copy (chain, args, complain, in_decl);
9669 if (purpose == TREE_PURPOSE (t)
9670 && value == TREE_VALUE (t)
9671 && chain == TREE_CHAIN (t))
9672 return t;
9673 return tree_cons (purpose, value, chain);
9676 case RECORD_TYPE:
9677 case UNION_TYPE:
9678 case ENUMERAL_TYPE:
9679 case INTEGER_TYPE:
9680 case TEMPLATE_TYPE_PARM:
9681 case TEMPLATE_TEMPLATE_PARM:
9682 case BOUND_TEMPLATE_TEMPLATE_PARM:
9683 case TEMPLATE_PARM_INDEX:
9684 case POINTER_TYPE:
9685 case REFERENCE_TYPE:
9686 case OFFSET_TYPE:
9687 case FUNCTION_TYPE:
9688 case METHOD_TYPE:
9689 case ARRAY_TYPE:
9690 case TYPENAME_TYPE:
9691 case UNBOUND_CLASS_TEMPLATE:
9692 case TYPEOF_TYPE:
9693 case DECLTYPE_TYPE:
9694 case TYPE_DECL:
9695 return tsubst (t, args, complain, in_decl);
9697 case IDENTIFIER_NODE:
9698 if (IDENTIFIER_TYPENAME_P (t))
9700 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9701 return mangle_conv_op_name_for_type (new_type);
9703 else
9704 return t;
9706 case CONSTRUCTOR:
9707 /* This is handled by tsubst_copy_and_build. */
9708 gcc_unreachable ();
9710 case VA_ARG_EXPR:
9711 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
9712 in_decl),
9713 tsubst (TREE_TYPE (t), args, complain, in_decl));
9715 case CLEANUP_POINT_EXPR:
9716 /* We shouldn't have built any of these during initial template
9717 generation. Instead, they should be built during instantiation
9718 in response to the saved STMT_IS_FULL_EXPR_P setting. */
9719 gcc_unreachable ();
9721 case OFFSET_REF:
9722 mark_used (TREE_OPERAND (t, 1));
9723 return t;
9725 case EXPR_PACK_EXPANSION:
9726 error ("invalid use of pack expansion expression");
9727 return error_mark_node;
9729 case NONTYPE_ARGUMENT_PACK:
9730 error ("use %<...%> to expand argument pack");
9731 return error_mark_node;
9733 default:
9734 return t;
9738 /* Like tsubst_copy, but specifically for OpenMP clauses. */
9740 static tree
9741 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
9742 tree in_decl)
9744 tree new_clauses = NULL, nc, oc;
9746 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
9748 nc = copy_node (oc);
9749 OMP_CLAUSE_CHAIN (nc) = new_clauses;
9750 new_clauses = nc;
9752 switch (OMP_CLAUSE_CODE (nc))
9754 case OMP_CLAUSE_PRIVATE:
9755 case OMP_CLAUSE_SHARED:
9756 case OMP_CLAUSE_FIRSTPRIVATE:
9757 case OMP_CLAUSE_LASTPRIVATE:
9758 case OMP_CLAUSE_REDUCTION:
9759 case OMP_CLAUSE_COPYIN:
9760 case OMP_CLAUSE_COPYPRIVATE:
9761 case OMP_CLAUSE_IF:
9762 case OMP_CLAUSE_NUM_THREADS:
9763 case OMP_CLAUSE_SCHEDULE:
9764 OMP_CLAUSE_OPERAND (nc, 0)
9765 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
9766 in_decl, /*integral_constant_expression_p=*/false);
9767 break;
9768 case OMP_CLAUSE_NOWAIT:
9769 case OMP_CLAUSE_ORDERED:
9770 case OMP_CLAUSE_DEFAULT:
9771 break;
9772 default:
9773 gcc_unreachable ();
9777 return finish_omp_clauses (nreverse (new_clauses));
9780 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
9782 static tree
9783 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
9784 tree in_decl)
9786 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
9788 tree purpose, value, chain;
9790 if (t == NULL)
9791 return t;
9793 if (TREE_CODE (t) != TREE_LIST)
9794 return tsubst_copy_and_build (t, args, complain, in_decl,
9795 /*function_p=*/false,
9796 /*integral_constant_expression_p=*/false);
9798 if (t == void_list_node)
9799 return t;
9801 purpose = TREE_PURPOSE (t);
9802 if (purpose)
9803 purpose = RECUR (purpose);
9804 value = TREE_VALUE (t);
9805 if (value)
9806 value = RECUR (value);
9807 chain = TREE_CHAIN (t);
9808 if (chain && chain != void_type_node)
9809 chain = RECUR (chain);
9810 return tree_cons (purpose, value, chain);
9811 #undef RECUR
9814 /* Like tsubst_copy for expressions, etc. but also does semantic
9815 processing. */
9817 static tree
9818 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
9819 bool integral_constant_expression_p)
9821 #define RECUR(NODE) \
9822 tsubst_expr ((NODE), args, complain, in_decl, \
9823 integral_constant_expression_p)
9825 tree stmt, tmp;
9827 if (t == NULL_TREE || t == error_mark_node)
9828 return t;
9830 if (EXPR_HAS_LOCATION (t))
9831 input_location = EXPR_LOCATION (t);
9832 if (STATEMENT_CODE_P (TREE_CODE (t)))
9833 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
9835 switch (TREE_CODE (t))
9837 case STATEMENT_LIST:
9839 tree_stmt_iterator i;
9840 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
9841 RECUR (tsi_stmt (i));
9842 break;
9845 case CTOR_INITIALIZER:
9846 finish_mem_initializers (tsubst_initializer_list
9847 (TREE_OPERAND (t, 0), args));
9848 break;
9850 case RETURN_EXPR:
9851 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
9852 break;
9854 case EXPR_STMT:
9855 tmp = RECUR (EXPR_STMT_EXPR (t));
9856 if (EXPR_STMT_STMT_EXPR_RESULT (t))
9857 finish_stmt_expr_expr (tmp, cur_stmt_expr);
9858 else
9859 finish_expr_stmt (tmp);
9860 break;
9862 case USING_STMT:
9863 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
9864 break;
9866 case DECL_EXPR:
9868 tree decl;
9869 tree init;
9871 decl = DECL_EXPR_DECL (t);
9872 if (TREE_CODE (decl) == LABEL_DECL)
9873 finish_label_decl (DECL_NAME (decl));
9874 else if (TREE_CODE (decl) == USING_DECL)
9876 tree scope = USING_DECL_SCOPE (decl);
9877 tree name = DECL_NAME (decl);
9878 tree decl;
9880 scope = RECUR (scope);
9881 decl = lookup_qualified_name (scope, name,
9882 /*is_type_p=*/false,
9883 /*complain=*/false);
9884 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
9885 qualified_name_lookup_error (scope, name, decl);
9886 else
9887 do_local_using_decl (decl, scope, name);
9889 else
9891 init = DECL_INITIAL (decl);
9892 decl = tsubst (decl, args, complain, in_decl);
9893 if (decl != error_mark_node)
9895 /* By marking the declaration as instantiated, we avoid
9896 trying to instantiate it. Since instantiate_decl can't
9897 handle local variables, and since we've already done
9898 all that needs to be done, that's the right thing to
9899 do. */
9900 if (TREE_CODE (decl) == VAR_DECL)
9901 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
9902 if (TREE_CODE (decl) == VAR_DECL
9903 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
9904 /* Anonymous aggregates are a special case. */
9905 finish_anon_union (decl);
9906 else
9908 maybe_push_decl (decl);
9909 if (TREE_CODE (decl) == VAR_DECL
9910 && DECL_PRETTY_FUNCTION_P (decl))
9912 /* For __PRETTY_FUNCTION__ we have to adjust the
9913 initializer. */
9914 const char *const name
9915 = cxx_printable_name (current_function_decl, 2);
9916 init = cp_fname_init (name, &TREE_TYPE (decl));
9918 else
9920 tree t = RECUR (init);
9922 if (init && !t)
9923 /* If we had an initializer but it
9924 instantiated to nothing,
9925 value-initialize the object. This will
9926 only occur when the initializer was a
9927 pack expansion where the parameter packs
9928 used in that expansion were of length
9929 zero. */
9930 init = build_default_init (TREE_TYPE (decl),
9931 NULL_TREE);
9932 else
9933 init = t;
9936 finish_decl (decl, init, NULL_TREE);
9941 /* A DECL_EXPR can also be used as an expression, in the condition
9942 clause of an if/for/while construct. */
9943 return decl;
9946 case FOR_STMT:
9947 stmt = begin_for_stmt ();
9948 RECUR (FOR_INIT_STMT (t));
9949 finish_for_init_stmt (stmt);
9950 tmp = RECUR (FOR_COND (t));
9951 finish_for_cond (tmp, stmt);
9952 tmp = RECUR (FOR_EXPR (t));
9953 finish_for_expr (tmp, stmt);
9954 RECUR (FOR_BODY (t));
9955 finish_for_stmt (stmt);
9956 break;
9958 case WHILE_STMT:
9959 stmt = begin_while_stmt ();
9960 tmp = RECUR (WHILE_COND (t));
9961 finish_while_stmt_cond (tmp, stmt);
9962 RECUR (WHILE_BODY (t));
9963 finish_while_stmt (stmt);
9964 break;
9966 case DO_STMT:
9967 stmt = begin_do_stmt ();
9968 RECUR (DO_BODY (t));
9969 finish_do_body (stmt);
9970 tmp = RECUR (DO_COND (t));
9971 finish_do_stmt (tmp, stmt);
9972 break;
9974 case IF_STMT:
9975 stmt = begin_if_stmt ();
9976 tmp = RECUR (IF_COND (t));
9977 finish_if_stmt_cond (tmp, stmt);
9978 RECUR (THEN_CLAUSE (t));
9979 finish_then_clause (stmt);
9981 if (ELSE_CLAUSE (t))
9983 begin_else_clause (stmt);
9984 RECUR (ELSE_CLAUSE (t));
9985 finish_else_clause (stmt);
9988 finish_if_stmt (stmt);
9989 break;
9991 case BIND_EXPR:
9992 if (BIND_EXPR_BODY_BLOCK (t))
9993 stmt = begin_function_body ();
9994 else
9995 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
9996 ? BCS_TRY_BLOCK : 0);
9998 RECUR (BIND_EXPR_BODY (t));
10000 if (BIND_EXPR_BODY_BLOCK (t))
10001 finish_function_body (stmt);
10002 else
10003 finish_compound_stmt (stmt);
10004 break;
10006 case BREAK_STMT:
10007 finish_break_stmt ();
10008 break;
10010 case CONTINUE_STMT:
10011 finish_continue_stmt ();
10012 break;
10014 case SWITCH_STMT:
10015 stmt = begin_switch_stmt ();
10016 tmp = RECUR (SWITCH_STMT_COND (t));
10017 finish_switch_cond (tmp, stmt);
10018 RECUR (SWITCH_STMT_BODY (t));
10019 finish_switch_stmt (stmt);
10020 break;
10022 case CASE_LABEL_EXPR:
10023 finish_case_label (RECUR (CASE_LOW (t)),
10024 RECUR (CASE_HIGH (t)));
10025 break;
10027 case LABEL_EXPR:
10028 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
10029 break;
10031 case GOTO_EXPR:
10032 tmp = GOTO_DESTINATION (t);
10033 if (TREE_CODE (tmp) != LABEL_DECL)
10034 /* Computed goto's must be tsubst'd into. On the other hand,
10035 non-computed gotos must not be; the identifier in question
10036 will have no binding. */
10037 tmp = RECUR (tmp);
10038 else
10039 tmp = DECL_NAME (tmp);
10040 finish_goto_stmt (tmp);
10041 break;
10043 case ASM_EXPR:
10044 tmp = finish_asm_stmt
10045 (ASM_VOLATILE_P (t),
10046 RECUR (ASM_STRING (t)),
10047 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
10048 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
10049 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
10051 tree asm_expr = tmp;
10052 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
10053 asm_expr = TREE_OPERAND (asm_expr, 0);
10054 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
10056 break;
10058 case TRY_BLOCK:
10059 if (CLEANUP_P (t))
10061 stmt = begin_try_block ();
10062 RECUR (TRY_STMTS (t));
10063 finish_cleanup_try_block (stmt);
10064 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
10066 else
10068 tree compound_stmt = NULL_TREE;
10070 if (FN_TRY_BLOCK_P (t))
10071 stmt = begin_function_try_block (&compound_stmt);
10072 else
10073 stmt = begin_try_block ();
10075 RECUR (TRY_STMTS (t));
10077 if (FN_TRY_BLOCK_P (t))
10078 finish_function_try_block (stmt);
10079 else
10080 finish_try_block (stmt);
10082 RECUR (TRY_HANDLERS (t));
10083 if (FN_TRY_BLOCK_P (t))
10084 finish_function_handler_sequence (stmt, compound_stmt);
10085 else
10086 finish_handler_sequence (stmt);
10088 break;
10090 case HANDLER:
10092 tree decl = HANDLER_PARMS (t);
10094 if (decl)
10096 decl = tsubst (decl, args, complain, in_decl);
10097 /* Prevent instantiate_decl from trying to instantiate
10098 this variable. We've already done all that needs to be
10099 done. */
10100 if (decl != error_mark_node)
10101 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
10103 stmt = begin_handler ();
10104 finish_handler_parms (decl, stmt);
10105 RECUR (HANDLER_BODY (t));
10106 finish_handler (stmt);
10108 break;
10110 case TAG_DEFN:
10111 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
10112 break;
10114 case STATIC_ASSERT:
10116 tree condition =
10117 tsubst_expr (STATIC_ASSERT_CONDITION (t),
10118 args,
10119 complain, in_decl,
10120 /*integral_constant_expression_p=*/true);
10121 finish_static_assert (condition,
10122 STATIC_ASSERT_MESSAGE (t),
10123 STATIC_ASSERT_SOURCE_LOCATION (t),
10124 /*member_p=*/false);
10126 break;
10128 case OMP_PARALLEL:
10129 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
10130 args, complain, in_decl);
10131 stmt = begin_omp_parallel ();
10132 RECUR (OMP_PARALLEL_BODY (t));
10133 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
10134 = OMP_PARALLEL_COMBINED (t);
10135 break;
10137 case OMP_FOR:
10139 tree clauses, decl, init, cond, incr, body, pre_body;
10141 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
10142 args, complain, in_decl);
10143 init = OMP_FOR_INIT (t);
10144 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
10145 decl = RECUR (TREE_OPERAND (init, 0));
10146 init = RECUR (TREE_OPERAND (init, 1));
10147 cond = RECUR (OMP_FOR_COND (t));
10148 incr = RECUR (OMP_FOR_INCR (t));
10150 stmt = begin_omp_structured_block ();
10152 pre_body = push_stmt_list ();
10153 RECUR (OMP_FOR_PRE_BODY (t));
10154 pre_body = pop_stmt_list (pre_body);
10156 body = push_stmt_list ();
10157 RECUR (OMP_FOR_BODY (t));
10158 body = pop_stmt_list (body);
10160 t = finish_omp_for (EXPR_LOCATION (t), decl, init, cond, incr, body,
10161 pre_body);
10162 if (t)
10163 OMP_FOR_CLAUSES (t) = clauses;
10165 add_stmt (finish_omp_structured_block (stmt));
10167 break;
10169 case OMP_SECTIONS:
10170 case OMP_SINGLE:
10171 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
10172 stmt = push_stmt_list ();
10173 RECUR (OMP_BODY (t));
10174 stmt = pop_stmt_list (stmt);
10176 t = copy_node (t);
10177 OMP_BODY (t) = stmt;
10178 OMP_CLAUSES (t) = tmp;
10179 add_stmt (t);
10180 break;
10182 case OMP_SECTION:
10183 case OMP_CRITICAL:
10184 case OMP_MASTER:
10185 case OMP_ORDERED:
10186 stmt = push_stmt_list ();
10187 RECUR (OMP_BODY (t));
10188 stmt = pop_stmt_list (stmt);
10190 t = copy_node (t);
10191 OMP_BODY (t) = stmt;
10192 add_stmt (t);
10193 break;
10195 case OMP_ATOMIC:
10196 if (OMP_ATOMIC_DEPENDENT_P (t))
10198 tree op1 = TREE_OPERAND (t, 1);
10199 tree lhs = RECUR (TREE_OPERAND (op1, 0));
10200 tree rhs = RECUR (TREE_OPERAND (op1, 1));
10201 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
10203 break;
10205 case EXPR_PACK_EXPANSION:
10206 error ("invalid use of pack expansion expression");
10207 return error_mark_node;
10209 case NONTYPE_ARGUMENT_PACK:
10210 error ("use %<...%> to expand argument pack");
10211 return error_mark_node;
10213 default:
10214 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
10216 return tsubst_copy_and_build (t, args, complain, in_decl,
10217 /*function_p=*/false,
10218 integral_constant_expression_p);
10221 return NULL_TREE;
10222 #undef RECUR
10225 /* T is a postfix-expression that is not being used in a function
10226 call. Return the substituted version of T. */
10228 static tree
10229 tsubst_non_call_postfix_expression (tree t, tree args,
10230 tsubst_flags_t complain,
10231 tree in_decl)
10233 if (TREE_CODE (t) == SCOPE_REF)
10234 t = tsubst_qualified_id (t, args, complain, in_decl,
10235 /*done=*/false, /*address_p=*/false);
10236 else
10237 t = tsubst_copy_and_build (t, args, complain, in_decl,
10238 /*function_p=*/false,
10239 /*integral_constant_expression_p=*/false);
10241 return t;
10244 /* Like tsubst but deals with expressions and performs semantic
10245 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
10247 tree
10248 tsubst_copy_and_build (tree t,
10249 tree args,
10250 tsubst_flags_t complain,
10251 tree in_decl,
10252 bool function_p,
10253 bool integral_constant_expression_p)
10255 #define RECUR(NODE) \
10256 tsubst_copy_and_build (NODE, args, complain, in_decl, \
10257 /*function_p=*/false, \
10258 integral_constant_expression_p)
10260 tree op1;
10262 if (t == NULL_TREE || t == error_mark_node)
10263 return t;
10265 switch (TREE_CODE (t))
10267 case USING_DECL:
10268 t = DECL_NAME (t);
10269 /* Fall through. */
10270 case IDENTIFIER_NODE:
10272 tree decl;
10273 cp_id_kind idk;
10274 bool non_integral_constant_expression_p;
10275 const char *error_msg;
10277 if (IDENTIFIER_TYPENAME_P (t))
10279 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10280 t = mangle_conv_op_name_for_type (new_type);
10283 /* Look up the name. */
10284 decl = lookup_name (t);
10286 /* By convention, expressions use ERROR_MARK_NODE to indicate
10287 failure, not NULL_TREE. */
10288 if (decl == NULL_TREE)
10289 decl = error_mark_node;
10291 decl = finish_id_expression (t, decl, NULL_TREE,
10292 &idk,
10293 integral_constant_expression_p,
10294 /*allow_non_integral_constant_expression_p=*/false,
10295 &non_integral_constant_expression_p,
10296 /*template_p=*/false,
10297 /*done=*/true,
10298 /*address_p=*/false,
10299 /*template_arg_p=*/false,
10300 &error_msg);
10301 if (error_msg)
10302 error (error_msg);
10303 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
10304 decl = unqualified_name_lookup_error (decl);
10305 return decl;
10308 case TEMPLATE_ID_EXPR:
10310 tree object;
10311 tree template = RECUR (TREE_OPERAND (t, 0));
10312 tree targs = TREE_OPERAND (t, 1);
10314 if (targs)
10315 targs = tsubst_template_args (targs, args, complain, in_decl);
10317 if (TREE_CODE (template) == COMPONENT_REF)
10319 object = TREE_OPERAND (template, 0);
10320 template = TREE_OPERAND (template, 1);
10322 else
10323 object = NULL_TREE;
10324 template = lookup_template_function (template, targs);
10326 if (object)
10327 return build3 (COMPONENT_REF, TREE_TYPE (template),
10328 object, template, NULL_TREE);
10329 else
10330 return baselink_for_fns (template);
10333 case INDIRECT_REF:
10335 tree r = RECUR (TREE_OPERAND (t, 0));
10337 if (REFERENCE_REF_P (t))
10339 /* A type conversion to reference type will be enclosed in
10340 such an indirect ref, but the substitution of the cast
10341 will have also added such an indirect ref. */
10342 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
10343 r = convert_from_reference (r);
10345 else
10346 r = build_x_indirect_ref (r, "unary *");
10347 return r;
10350 case NOP_EXPR:
10351 return build_nop
10352 (tsubst (TREE_TYPE (t), args, complain, in_decl),
10353 RECUR (TREE_OPERAND (t, 0)));
10355 case CAST_EXPR:
10356 case REINTERPRET_CAST_EXPR:
10357 case CONST_CAST_EXPR:
10358 case DYNAMIC_CAST_EXPR:
10359 case STATIC_CAST_EXPR:
10361 tree type;
10362 tree op;
10364 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10365 if (integral_constant_expression_p
10366 && !cast_valid_in_integral_constant_expression_p (type))
10368 error ("a cast to a type other than an integral or "
10369 "enumeration type cannot appear in a constant-expression");
10370 return error_mark_node;
10373 op = RECUR (TREE_OPERAND (t, 0));
10375 switch (TREE_CODE (t))
10377 case CAST_EXPR:
10378 return build_functional_cast (type, op);
10379 case REINTERPRET_CAST_EXPR:
10380 return build_reinterpret_cast (type, op);
10381 case CONST_CAST_EXPR:
10382 return build_const_cast (type, op);
10383 case DYNAMIC_CAST_EXPR:
10384 return build_dynamic_cast (type, op);
10385 case STATIC_CAST_EXPR:
10386 return build_static_cast (type, op);
10387 default:
10388 gcc_unreachable ();
10392 case POSTDECREMENT_EXPR:
10393 case POSTINCREMENT_EXPR:
10394 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
10395 args, complain, in_decl);
10396 return build_x_unary_op (TREE_CODE (t), op1);
10398 case PREDECREMENT_EXPR:
10399 case PREINCREMENT_EXPR:
10400 case NEGATE_EXPR:
10401 case BIT_NOT_EXPR:
10402 case ABS_EXPR:
10403 case TRUTH_NOT_EXPR:
10404 case UNARY_PLUS_EXPR: /* Unary + */
10405 case REALPART_EXPR:
10406 case IMAGPART_EXPR:
10407 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
10409 case ADDR_EXPR:
10410 op1 = TREE_OPERAND (t, 0);
10411 if (TREE_CODE (op1) == SCOPE_REF)
10412 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
10413 /*done=*/true, /*address_p=*/true);
10414 else
10415 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
10416 in_decl);
10417 if (TREE_CODE (op1) == LABEL_DECL)
10418 return finish_label_address_expr (DECL_NAME (op1));
10419 return build_x_unary_op (ADDR_EXPR, op1);
10421 case PLUS_EXPR:
10422 case MINUS_EXPR:
10423 case MULT_EXPR:
10424 case TRUNC_DIV_EXPR:
10425 case CEIL_DIV_EXPR:
10426 case FLOOR_DIV_EXPR:
10427 case ROUND_DIV_EXPR:
10428 case EXACT_DIV_EXPR:
10429 case BIT_AND_EXPR:
10430 case BIT_IOR_EXPR:
10431 case BIT_XOR_EXPR:
10432 case TRUNC_MOD_EXPR:
10433 case FLOOR_MOD_EXPR:
10434 case TRUTH_ANDIF_EXPR:
10435 case TRUTH_ORIF_EXPR:
10436 case TRUTH_AND_EXPR:
10437 case TRUTH_OR_EXPR:
10438 case RSHIFT_EXPR:
10439 case LSHIFT_EXPR:
10440 case RROTATE_EXPR:
10441 case LROTATE_EXPR:
10442 case EQ_EXPR:
10443 case NE_EXPR:
10444 case MAX_EXPR:
10445 case MIN_EXPR:
10446 case LE_EXPR:
10447 case GE_EXPR:
10448 case LT_EXPR:
10449 case GT_EXPR:
10450 case MEMBER_REF:
10451 case DOTSTAR_EXPR:
10452 return build_x_binary_op
10453 (TREE_CODE (t),
10454 RECUR (TREE_OPERAND (t, 0)),
10455 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
10456 ? ERROR_MARK
10457 : TREE_CODE (TREE_OPERAND (t, 0))),
10458 RECUR (TREE_OPERAND (t, 1)),
10459 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
10460 ? ERROR_MARK
10461 : TREE_CODE (TREE_OPERAND (t, 1))),
10462 /*overloaded_p=*/NULL);
10464 case SCOPE_REF:
10465 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
10466 /*address_p=*/false);
10467 case ARRAY_REF:
10468 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
10469 args, complain, in_decl);
10470 return build_x_binary_op (ARRAY_REF, op1,
10471 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
10472 ? ERROR_MARK
10473 : TREE_CODE (TREE_OPERAND (t, 0))),
10474 RECUR (TREE_OPERAND (t, 1)),
10475 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
10476 ? ERROR_MARK
10477 : TREE_CODE (TREE_OPERAND (t, 1))),
10478 /*overloaded_p=*/NULL);
10480 case SIZEOF_EXPR:
10481 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
10483 /* We only want to compute the number of arguments. */
10484 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
10485 complain, in_decl);
10486 return build_int_cst (size_type_node, TREE_VEC_LENGTH (expanded));
10488 /* Fall through */
10490 case ALIGNOF_EXPR:
10491 op1 = TREE_OPERAND (t, 0);
10492 if (!args)
10494 /* When there are no ARGS, we are trying to evaluate a
10495 non-dependent expression from the parser. Trying to do
10496 the substitutions may not work. */
10497 if (!TYPE_P (op1))
10498 op1 = TREE_TYPE (op1);
10500 else
10502 ++skip_evaluation;
10503 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
10504 /*function_p=*/false,
10505 /*integral_constant_expression_p=*/false);
10506 --skip_evaluation;
10508 if (TYPE_P (op1))
10509 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
10510 else
10511 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
10513 case MODOP_EXPR:
10515 tree r = build_x_modify_expr
10516 (RECUR (TREE_OPERAND (t, 0)),
10517 TREE_CODE (TREE_OPERAND (t, 1)),
10518 RECUR (TREE_OPERAND (t, 2)));
10519 /* TREE_NO_WARNING must be set if either the expression was
10520 parenthesized or it uses an operator such as >>= rather
10521 than plain assignment. In the former case, it was already
10522 set and must be copied. In the latter case,
10523 build_x_modify_expr sets it and it must not be reset
10524 here. */
10525 if (TREE_NO_WARNING (t))
10526 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
10527 return r;
10530 case ARROW_EXPR:
10531 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
10532 args, complain, in_decl);
10533 /* Remember that there was a reference to this entity. */
10534 if (DECL_P (op1))
10535 mark_used (op1);
10536 return build_x_arrow (op1);
10538 case NEW_EXPR:
10540 tree init = RECUR (TREE_OPERAND (t, 3));
10542 if (TREE_OPERAND (t, 3) && !init)
10543 /* If there was an initializer in the the original tree, but
10544 it instantiated to an empty list, then we should pass on
10545 VOID_ZERO_NODE to tell build_new that it was an empty
10546 initializer () rather than no initializer. This can only
10547 happen when the initializer is a pack expansion whose
10548 parameter packs are of length zero. */
10549 init = void_zero_node;
10551 return build_new
10552 (RECUR (TREE_OPERAND (t, 0)),
10553 RECUR (TREE_OPERAND (t, 1)),
10554 RECUR (TREE_OPERAND (t, 2)),
10555 init,
10556 NEW_EXPR_USE_GLOBAL (t));
10559 case DELETE_EXPR:
10560 return delete_sanity
10561 (RECUR (TREE_OPERAND (t, 0)),
10562 RECUR (TREE_OPERAND (t, 1)),
10563 DELETE_EXPR_USE_VEC (t),
10564 DELETE_EXPR_USE_GLOBAL (t));
10566 case COMPOUND_EXPR:
10567 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
10568 RECUR (TREE_OPERAND (t, 1)));
10570 case CALL_EXPR:
10572 tree function;
10573 tree call_args;
10574 bool qualified_p;
10575 bool koenig_p;
10577 function = CALL_EXPR_FN (t);
10578 /* When we parsed the expression, we determined whether or
10579 not Koenig lookup should be performed. */
10580 koenig_p = KOENIG_LOOKUP_P (t);
10581 if (TREE_CODE (function) == SCOPE_REF)
10583 qualified_p = true;
10584 function = tsubst_qualified_id (function, args, complain, in_decl,
10585 /*done=*/false,
10586 /*address_p=*/false);
10588 else
10590 if (TREE_CODE (function) == COMPONENT_REF)
10592 tree op = TREE_OPERAND (function, 1);
10594 qualified_p = (TREE_CODE (op) == SCOPE_REF
10595 || (BASELINK_P (op)
10596 && BASELINK_QUALIFIED_P (op)));
10598 else
10599 qualified_p = false;
10601 function = tsubst_copy_and_build (function, args, complain,
10602 in_decl,
10603 !qualified_p,
10604 integral_constant_expression_p);
10606 if (BASELINK_P (function))
10607 qualified_p = true;
10610 /* FIXME: Rewrite this so as not to construct an arglist. */
10611 call_args = RECUR (CALL_EXPR_ARGS (t));
10613 /* We do not perform argument-dependent lookup if normal
10614 lookup finds a non-function, in accordance with the
10615 expected resolution of DR 218. */
10616 if (koenig_p
10617 && ((is_overloaded_fn (function)
10618 /* If lookup found a member function, the Koenig lookup is
10619 not appropriate, even if an unqualified-name was used
10620 to denote the function. */
10621 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
10622 || TREE_CODE (function) == IDENTIFIER_NODE))
10623 function = perform_koenig_lookup (function, call_args);
10625 if (TREE_CODE (function) == IDENTIFIER_NODE)
10627 unqualified_name_lookup_error (function);
10628 return error_mark_node;
10631 /* Remember that there was a reference to this entity. */
10632 if (DECL_P (function))
10633 mark_used (function);
10635 if (TREE_CODE (function) == OFFSET_REF)
10636 return build_offset_ref_call_from_tree (function, call_args);
10637 if (TREE_CODE (function) == COMPONENT_REF)
10639 if (!BASELINK_P (TREE_OPERAND (function, 1)))
10640 return finish_call_expr (function, call_args,
10641 /*disallow_virtual=*/false,
10642 /*koenig_p=*/false);
10643 else
10644 return (build_new_method_call
10645 (TREE_OPERAND (function, 0),
10646 TREE_OPERAND (function, 1),
10647 call_args, NULL_TREE,
10648 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
10649 /*fn_p=*/NULL));
10651 return finish_call_expr (function, call_args,
10652 /*disallow_virtual=*/qualified_p,
10653 koenig_p);
10656 case COND_EXPR:
10657 return build_x_conditional_expr
10658 (RECUR (TREE_OPERAND (t, 0)),
10659 RECUR (TREE_OPERAND (t, 1)),
10660 RECUR (TREE_OPERAND (t, 2)));
10662 case PSEUDO_DTOR_EXPR:
10663 return finish_pseudo_destructor_expr
10664 (RECUR (TREE_OPERAND (t, 0)),
10665 RECUR (TREE_OPERAND (t, 1)),
10666 RECUR (TREE_OPERAND (t, 2)));
10668 case TREE_LIST:
10670 tree purpose, value, chain;
10672 if (t == void_list_node)
10673 return t;
10675 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
10676 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
10678 /* We have pack expansions, so expand those and
10679 create a new list out of it. */
10680 tree purposevec = NULL_TREE;
10681 tree valuevec = NULL_TREE;
10682 tree chain;
10683 int i, len = -1;
10685 /* Expand the argument expressions. */
10686 if (TREE_PURPOSE (t))
10687 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
10688 complain, in_decl);
10689 if (TREE_VALUE (t))
10690 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
10691 complain, in_decl);
10693 /* Build the rest of the list. */
10694 chain = TREE_CHAIN (t);
10695 if (chain && chain != void_type_node)
10696 chain = RECUR (chain);
10698 /* Determine the number of arguments. */
10699 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
10701 len = TREE_VEC_LENGTH (purposevec);
10702 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
10704 else if (TREE_CODE (valuevec) == TREE_VEC)
10705 len = TREE_VEC_LENGTH (valuevec);
10706 else
10708 /* Since we only performed a partial substitution into
10709 the argument pack, we only return a single list
10710 node. */
10711 if (purposevec == TREE_PURPOSE (t)
10712 && valuevec == TREE_VALUE (t)
10713 && chain == TREE_CHAIN (t))
10714 return t;
10716 return tree_cons (purposevec, valuevec, chain);
10719 /* Convert the argument vectors into a TREE_LIST */
10720 i = len;
10721 while (i > 0)
10723 /* Grab the Ith values. */
10724 i--;
10725 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
10726 : NULL_TREE;
10727 value
10728 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
10729 : NULL_TREE;
10731 /* Build the list (backwards). */
10732 chain = tree_cons (purpose, value, chain);
10735 return chain;
10738 purpose = TREE_PURPOSE (t);
10739 if (purpose)
10740 purpose = RECUR (purpose);
10741 value = TREE_VALUE (t);
10742 if (value)
10743 value = RECUR (value);
10744 chain = TREE_CHAIN (t);
10745 if (chain && chain != void_type_node)
10746 chain = RECUR (chain);
10747 if (purpose == TREE_PURPOSE (t)
10748 && value == TREE_VALUE (t)
10749 && chain == TREE_CHAIN (t))
10750 return t;
10751 return tree_cons (purpose, value, chain);
10754 case COMPONENT_REF:
10756 tree object;
10757 tree object_type;
10758 tree member;
10760 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
10761 args, complain, in_decl);
10762 /* Remember that there was a reference to this entity. */
10763 if (DECL_P (object))
10764 mark_used (object);
10765 object_type = TREE_TYPE (object);
10767 member = TREE_OPERAND (t, 1);
10768 if (BASELINK_P (member))
10769 member = tsubst_baselink (member,
10770 non_reference (TREE_TYPE (object)),
10771 args, complain, in_decl);
10772 else
10773 member = tsubst_copy (member, args, complain, in_decl);
10774 if (member == error_mark_node)
10775 return error_mark_node;
10777 if (object_type && !CLASS_TYPE_P (object_type))
10779 if (TREE_CODE (member) == BIT_NOT_EXPR)
10780 return finish_pseudo_destructor_expr (object,
10781 NULL_TREE,
10782 object_type);
10783 else if (TREE_CODE (member) == SCOPE_REF
10784 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
10785 return finish_pseudo_destructor_expr (object,
10786 object,
10787 object_type);
10789 else if (TREE_CODE (member) == SCOPE_REF
10790 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
10792 tree tmpl;
10793 tree args;
10795 /* Lookup the template functions now that we know what the
10796 scope is. */
10797 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
10798 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
10799 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
10800 /*is_type_p=*/false,
10801 /*complain=*/false);
10802 if (BASELINK_P (member))
10804 BASELINK_FUNCTIONS (member)
10805 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
10806 args);
10807 member = (adjust_result_of_qualified_name_lookup
10808 (member, BINFO_TYPE (BASELINK_BINFO (member)),
10809 object_type));
10811 else
10813 qualified_name_lookup_error (object_type, tmpl, member);
10814 return error_mark_node;
10817 else if (TREE_CODE (member) == SCOPE_REF
10818 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
10819 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
10821 if (complain & tf_error)
10823 if (TYPE_P (TREE_OPERAND (member, 0)))
10824 error ("%qT is not a class or namespace",
10825 TREE_OPERAND (member, 0));
10826 else
10827 error ("%qD is not a class or namespace",
10828 TREE_OPERAND (member, 0));
10830 return error_mark_node;
10832 else if (TREE_CODE (member) == FIELD_DECL)
10833 return finish_non_static_data_member (member, object, NULL_TREE);
10835 return finish_class_member_access_expr (object, member,
10836 /*template_p=*/false);
10839 case THROW_EXPR:
10840 return build_throw
10841 (RECUR (TREE_OPERAND (t, 0)));
10843 case CONSTRUCTOR:
10845 VEC(constructor_elt,gc) *n;
10846 constructor_elt *ce;
10847 unsigned HOST_WIDE_INT idx;
10848 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10849 bool process_index_p;
10850 int newlen;
10851 bool need_copy_p = false;
10853 if (type == error_mark_node)
10854 return error_mark_node;
10856 /* digest_init will do the wrong thing if we let it. */
10857 if (type && TYPE_PTRMEMFUNC_P (type))
10858 return t;
10860 /* We do not want to process the index of aggregate
10861 initializers as they are identifier nodes which will be
10862 looked up by digest_init. */
10863 process_index_p = !(type && IS_AGGR_TYPE (type));
10865 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
10866 newlen = VEC_length (constructor_elt, n);
10867 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
10869 if (ce->index && process_index_p)
10870 ce->index = RECUR (ce->index);
10872 if (PACK_EXPANSION_P (ce->value))
10874 /* Substitute into the pack expansion. */
10875 ce->value = tsubst_pack_expansion (ce->value, args, complain,
10876 in_decl);
10878 if (TREE_VEC_LENGTH (ce->value) == 1)
10879 /* Just move the argument into place. */
10880 ce->value = TREE_VEC_ELT (ce->value, 0);
10881 else
10883 /* Update the length of the final CONSTRUCTOR
10884 arguments vector, and note that we will need to
10885 copy.*/
10886 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
10887 need_copy_p = true;
10890 else
10891 ce->value = RECUR (ce->value);
10894 if (need_copy_p)
10896 VEC(constructor_elt,gc) *old_n = n;
10898 n = VEC_alloc (constructor_elt, gc, newlen);
10899 for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
10900 idx++)
10902 if (TREE_CODE (ce->value) == TREE_VEC)
10904 int i, len = TREE_VEC_LENGTH (ce->value);
10905 for (i = 0; i < len; ++i)
10906 CONSTRUCTOR_APPEND_ELT (n, 0,
10907 TREE_VEC_ELT (ce->value, i));
10909 else
10910 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
10914 if (TREE_HAS_CONSTRUCTOR (t))
10915 return finish_compound_literal (type, n);
10917 return build_constructor (NULL_TREE, n);
10920 case TYPEID_EXPR:
10922 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
10923 if (TYPE_P (operand_0))
10924 return get_typeid (operand_0);
10925 return build_typeid (operand_0);
10928 case VAR_DECL:
10929 if (!args)
10930 return t;
10931 /* Fall through */
10933 case PARM_DECL:
10935 tree r = tsubst_copy (t, args, complain, in_decl);
10937 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
10938 /* If the original type was a reference, we'll be wrapped in
10939 the appropriate INDIRECT_REF. */
10940 r = convert_from_reference (r);
10941 return r;
10944 case VA_ARG_EXPR:
10945 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
10946 tsubst_copy (TREE_TYPE (t), args, complain,
10947 in_decl));
10949 case OFFSETOF_EXPR:
10950 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
10952 case TRAIT_EXPR:
10954 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
10955 complain, in_decl);
10957 tree type2 = TRAIT_EXPR_TYPE2 (t);
10958 if (type2)
10959 type2 = tsubst_copy (type2, args, complain, in_decl);
10961 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
10964 case STMT_EXPR:
10966 tree old_stmt_expr = cur_stmt_expr;
10967 tree stmt_expr = begin_stmt_expr ();
10969 cur_stmt_expr = stmt_expr;
10970 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
10971 integral_constant_expression_p);
10972 stmt_expr = finish_stmt_expr (stmt_expr, false);
10973 cur_stmt_expr = old_stmt_expr;
10975 return stmt_expr;
10978 case CONST_DECL:
10979 t = tsubst_copy (t, args, complain, in_decl);
10980 /* As in finish_id_expression, we resolve enumeration constants
10981 to their underlying values. */
10982 if (TREE_CODE (t) == CONST_DECL)
10984 used_types_insert (TREE_TYPE (t));
10985 return DECL_INITIAL (t);
10987 return t;
10989 default:
10990 /* Handle Objective-C++ constructs, if appropriate. */
10992 tree subst
10993 = objcp_tsubst_copy_and_build (t, args, complain,
10994 in_decl, /*function_p=*/false);
10995 if (subst)
10996 return subst;
10998 return tsubst_copy (t, args, complain, in_decl);
11001 #undef RECUR
11004 /* Verify that the instantiated ARGS are valid. For type arguments,
11005 make sure that the type's linkage is ok. For non-type arguments,
11006 make sure they are constants if they are integral or enumerations.
11007 Emit an error under control of COMPLAIN, and return TRUE on error. */
11009 static bool
11010 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
11012 int ix, len = DECL_NTPARMS (tmpl);
11013 bool result = false;
11015 for (ix = 0; ix != len; ix++)
11017 tree t = TREE_VEC_ELT (args, ix);
11019 if (TYPE_P (t))
11021 /* [basic.link]: A name with no linkage (notably, the name
11022 of a class or enumeration declared in a local scope)
11023 shall not be used to declare an entity with linkage.
11024 This implies that names with no linkage cannot be used as
11025 template arguments. */
11026 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
11028 if (nt)
11030 /* DR 488 makes use of a type with no linkage cause
11031 type deduction to fail. */
11032 if (complain & tf_error)
11034 if (TYPE_ANONYMOUS_P (nt))
11035 error ("%qT is/uses anonymous type", t);
11036 else
11037 error ("template argument for %qD uses local type %qT",
11038 tmpl, t);
11040 result = true;
11042 /* In order to avoid all sorts of complications, we do not
11043 allow variably-modified types as template arguments. */
11044 else if (variably_modified_type_p (t, NULL_TREE))
11046 if (complain & tf_error)
11047 error ("%qT is a variably modified type", t);
11048 result = true;
11051 /* A non-type argument of integral or enumerated type must be a
11052 constant. */
11053 else if (TREE_TYPE (t)
11054 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
11055 && !TREE_CONSTANT (t))
11057 if (complain & tf_error)
11058 error ("integral expression %qE is not constant", t);
11059 result = true;
11062 if (result && (complain & tf_error))
11063 error (" trying to instantiate %qD", tmpl);
11064 return result;
11067 /* Instantiate the indicated variable or function template TMPL with
11068 the template arguments in TARG_PTR. */
11070 tree
11071 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
11073 tree fndecl;
11074 tree gen_tmpl;
11075 tree spec;
11076 HOST_WIDE_INT saved_processing_template_decl;
11078 if (tmpl == error_mark_node)
11079 return error_mark_node;
11081 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
11083 /* If this function is a clone, handle it specially. */
11084 if (DECL_CLONED_FUNCTION_P (tmpl))
11086 tree spec;
11087 tree clone;
11089 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
11090 complain);
11091 if (spec == error_mark_node)
11092 return error_mark_node;
11094 /* Look for the clone. */
11095 FOR_EACH_CLONE (clone, spec)
11096 if (DECL_NAME (clone) == DECL_NAME (tmpl))
11097 return clone;
11098 /* We should always have found the clone by now. */
11099 gcc_unreachable ();
11100 return NULL_TREE;
11103 /* Check to see if we already have this specialization. */
11104 spec = retrieve_specialization (tmpl, targ_ptr,
11105 /*class_specializations_p=*/false);
11106 if (spec != NULL_TREE)
11107 return spec;
11109 gen_tmpl = most_general_template (tmpl);
11110 if (tmpl != gen_tmpl)
11112 /* The TMPL is a partial instantiation. To get a full set of
11113 arguments we must add the arguments used to perform the
11114 partial instantiation. */
11115 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
11116 targ_ptr);
11118 /* Check to see if we already have this specialization. */
11119 spec = retrieve_specialization (gen_tmpl, targ_ptr,
11120 /*class_specializations_p=*/false);
11121 if (spec != NULL_TREE)
11122 return spec;
11125 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
11126 complain))
11127 return error_mark_node;
11129 /* We are building a FUNCTION_DECL, during which the access of its
11130 parameters and return types have to be checked. However this
11131 FUNCTION_DECL which is the desired context for access checking
11132 is not built yet. We solve this chicken-and-egg problem by
11133 deferring all checks until we have the FUNCTION_DECL. */
11134 push_deferring_access_checks (dk_deferred);
11136 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
11137 (because, for example, we have encountered a non-dependent
11138 function call in the body of a template function and must now
11139 determine which of several overloaded functions will be called),
11140 within the instantiation itself we are not processing a
11141 template. */
11142 saved_processing_template_decl = processing_template_decl;
11143 processing_template_decl = 0;
11144 /* Substitute template parameters to obtain the specialization. */
11145 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
11146 targ_ptr, complain, gen_tmpl);
11147 processing_template_decl = saved_processing_template_decl;
11148 if (fndecl == error_mark_node)
11149 return error_mark_node;
11151 /* Now we know the specialization, compute access previously
11152 deferred. */
11153 push_access_scope (fndecl);
11154 perform_deferred_access_checks ();
11155 pop_access_scope (fndecl);
11156 pop_deferring_access_checks ();
11158 /* The DECL_TI_TEMPLATE should always be the immediate parent
11159 template, not the most general template. */
11160 DECL_TI_TEMPLATE (fndecl) = tmpl;
11162 /* If we've just instantiated the main entry point for a function,
11163 instantiate all the alternate entry points as well. We do this
11164 by cloning the instantiation of the main entry point, not by
11165 instantiating the template clones. */
11166 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
11167 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
11169 return fndecl;
11172 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
11173 arguments that are being used when calling it. TARGS is a vector
11174 into which the deduced template arguments are placed.
11176 Return zero for success, 2 for an incomplete match that doesn't resolve
11177 all the types, and 1 for complete failure. An error message will be
11178 printed only for an incomplete match.
11180 If FN is a conversion operator, or we are trying to produce a specific
11181 specialization, RETURN_TYPE is the return type desired.
11183 The EXPLICIT_TARGS are explicit template arguments provided via a
11184 template-id.
11186 The parameter STRICT is one of:
11188 DEDUCE_CALL:
11189 We are deducing arguments for a function call, as in
11190 [temp.deduct.call].
11192 DEDUCE_CONV:
11193 We are deducing arguments for a conversion function, as in
11194 [temp.deduct.conv].
11196 DEDUCE_EXACT:
11197 We are deducing arguments when doing an explicit instantiation
11198 as in [temp.explicit], when determining an explicit specialization
11199 as in [temp.expl.spec], or when taking the address of a function
11200 template, as in [temp.deduct.funcaddr]. */
11203 fn_type_unification (tree fn,
11204 tree explicit_targs,
11205 tree targs,
11206 tree args,
11207 tree return_type,
11208 unification_kind_t strict,
11209 int flags)
11211 tree parms;
11212 tree fntype;
11213 int result;
11214 bool incomplete_argument_packs_p = false;
11216 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
11218 fntype = TREE_TYPE (fn);
11219 if (explicit_targs)
11221 /* [temp.deduct]
11223 The specified template arguments must match the template
11224 parameters in kind (i.e., type, nontype, template), and there
11225 must not be more arguments than there are parameters;
11226 otherwise type deduction fails.
11228 Nontype arguments must match the types of the corresponding
11229 nontype template parameters, or must be convertible to the
11230 types of the corresponding nontype parameters as specified in
11231 _temp.arg.nontype_, otherwise type deduction fails.
11233 All references in the function type of the function template
11234 to the corresponding template parameters are replaced by the
11235 specified template argument values. If a substitution in a
11236 template parameter or in the function type of the function
11237 template results in an invalid type, type deduction fails. */
11238 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
11239 int i, len = TREE_VEC_LENGTH (tparms);
11240 tree converted_args;
11241 bool incomplete = false;
11243 if (explicit_targs == error_mark_node)
11244 return 1;
11246 converted_args
11247 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
11248 /*require_all_args=*/false,
11249 /*use_default_args=*/false));
11250 if (converted_args == error_mark_node)
11251 return 1;
11253 /* Substitute the explicit args into the function type. This is
11254 necessary so that, for instance, explicitly declared function
11255 arguments can match null pointed constants. If we were given
11256 an incomplete set of explicit args, we must not do semantic
11257 processing during substitution as we could create partial
11258 instantiations. */
11259 for (i = 0; i < len; i++)
11261 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
11262 bool parameter_pack = false;
11264 /* Dig out the actual parm. */
11265 if (TREE_CODE (parm) == TYPE_DECL
11266 || TREE_CODE (parm) == TEMPLATE_DECL)
11268 parm = TREE_TYPE (parm);
11269 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
11271 else if (TREE_CODE (parm) == PARM_DECL)
11273 parm = DECL_INITIAL (parm);
11274 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
11277 if (parameter_pack)
11279 int level, idx;
11280 tree targ;
11281 template_parm_level_and_index (parm, &level, &idx);
11283 /* Mark the argument pack as "incomplete". We could
11284 still deduce more arguments during unification. */
11285 targ = TMPL_ARG (converted_args, level, idx);
11286 if (targ)
11288 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
11289 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
11290 = ARGUMENT_PACK_ARGS (targ);
11293 /* We have some incomplete argument packs. */
11294 incomplete_argument_packs_p = true;
11298 if (incomplete_argument_packs_p)
11299 /* Any substitution is guaranteed to be incomplete if there
11300 are incomplete argument packs, because we can still deduce
11301 more arguments. */
11302 incomplete = 1;
11303 else
11304 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
11306 processing_template_decl += incomplete;
11307 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
11308 processing_template_decl -= incomplete;
11310 if (fntype == error_mark_node)
11311 return 1;
11313 /* Place the explicitly specified arguments in TARGS. */
11314 for (i = NUM_TMPL_ARGS (converted_args); i--;)
11315 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
11318 /* Never do unification on the 'this' parameter. */
11319 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
11321 if (return_type)
11323 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
11324 args = tree_cons (NULL_TREE, return_type, args);
11327 /* We allow incomplete unification without an error message here
11328 because the standard doesn't seem to explicitly prohibit it. Our
11329 callers must be ready to deal with unification failures in any
11330 event. */
11331 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
11332 targs, parms, args, /*subr=*/0,
11333 strict, flags);
11335 if (result == 0 && incomplete_argument_packs_p)
11337 int i, len = NUM_TMPL_ARGS (targs);
11339 /* Clear the "incomplete" flags on all argument packs. */
11340 for (i = 0; i < len; i++)
11342 tree arg = TREE_VEC_ELT (targs, i);
11343 if (ARGUMENT_PACK_P (arg))
11345 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
11346 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
11351 if (result == 0)
11352 /* All is well so far. Now, check:
11354 [temp.deduct]
11356 When all template arguments have been deduced, all uses of
11357 template parameters in nondeduced contexts are replaced with
11358 the corresponding deduced argument values. If the
11359 substitution results in an invalid type, as described above,
11360 type deduction fails. */
11361 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
11362 == error_mark_node)
11363 return 1;
11365 return result;
11368 /* Adjust types before performing type deduction, as described in
11369 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
11370 sections are symmetric. PARM is the type of a function parameter
11371 or the return type of the conversion function. ARG is the type of
11372 the argument passed to the call, or the type of the value
11373 initialized with the result of the conversion function.
11374 ARG_EXPR is the original argument expression, which may be null. */
11376 static int
11377 maybe_adjust_types_for_deduction (unification_kind_t strict,
11378 tree* parm,
11379 tree* arg,
11380 tree arg_expr)
11382 int result = 0;
11384 switch (strict)
11386 case DEDUCE_CALL:
11387 break;
11389 case DEDUCE_CONV:
11391 /* Swap PARM and ARG throughout the remainder of this
11392 function; the handling is precisely symmetric since PARM
11393 will initialize ARG rather than vice versa. */
11394 tree* temp = parm;
11395 parm = arg;
11396 arg = temp;
11397 break;
11400 case DEDUCE_EXACT:
11401 /* There is nothing to do in this case. */
11402 return 0;
11404 default:
11405 gcc_unreachable ();
11408 if (TREE_CODE (*parm) != REFERENCE_TYPE)
11410 /* [temp.deduct.call]
11412 If P is not a reference type:
11414 --If A is an array type, the pointer type produced by the
11415 array-to-pointer standard conversion (_conv.array_) is
11416 used in place of A for type deduction; otherwise,
11418 --If A is a function type, the pointer type produced by
11419 the function-to-pointer standard conversion
11420 (_conv.func_) is used in place of A for type deduction;
11421 otherwise,
11423 --If A is a cv-qualified type, the top level
11424 cv-qualifiers of A's type are ignored for type
11425 deduction. */
11426 if (TREE_CODE (*arg) == ARRAY_TYPE)
11427 *arg = build_pointer_type (TREE_TYPE (*arg));
11428 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
11429 *arg = build_pointer_type (*arg);
11430 else
11431 *arg = TYPE_MAIN_VARIANT (*arg);
11434 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
11435 of the form T&&, where T is a template parameter, and the argument
11436 is an lvalue, T is deduced as A& */
11437 if (TREE_CODE (*parm) == REFERENCE_TYPE
11438 && TYPE_REF_IS_RVALUE (*parm)
11439 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
11440 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
11441 && arg_expr && real_lvalue_p (arg_expr))
11442 *arg = build_reference_type (*arg);
11444 /* [temp.deduct.call]
11446 If P is a cv-qualified type, the top level cv-qualifiers
11447 of P's type are ignored for type deduction. If P is a
11448 reference type, the type referred to by P is used for
11449 type deduction. */
11450 *parm = TYPE_MAIN_VARIANT (*parm);
11451 if (TREE_CODE (*parm) == REFERENCE_TYPE)
11453 *parm = TREE_TYPE (*parm);
11454 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
11457 /* DR 322. For conversion deduction, remove a reference type on parm
11458 too (which has been swapped into ARG). */
11459 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
11460 *arg = TREE_TYPE (*arg);
11462 return result;
11465 /* Most parms like fn_type_unification.
11467 If SUBR is 1, we're being called recursively (to unify the
11468 arguments of a function or method parameter of a function
11469 template). */
11471 static int
11472 type_unification_real (tree tparms,
11473 tree targs,
11474 tree xparms,
11475 tree xargs,
11476 int subr,
11477 unification_kind_t strict,
11478 int flags)
11480 tree parm, arg, arg_expr;
11481 int i;
11482 int ntparms = TREE_VEC_LENGTH (tparms);
11483 int sub_strict;
11484 int saw_undeduced = 0;
11485 tree parms, args;
11487 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
11488 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
11489 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
11490 gcc_assert (ntparms > 0);
11492 switch (strict)
11494 case DEDUCE_CALL:
11495 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
11496 | UNIFY_ALLOW_DERIVED);
11497 break;
11499 case DEDUCE_CONV:
11500 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
11501 break;
11503 case DEDUCE_EXACT:
11504 sub_strict = UNIFY_ALLOW_NONE;
11505 break;
11507 default:
11508 gcc_unreachable ();
11511 again:
11512 parms = xparms;
11513 args = xargs;
11515 while (parms && parms != void_list_node
11516 && args && args != void_list_node)
11518 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
11519 break;
11521 parm = TREE_VALUE (parms);
11522 parms = TREE_CHAIN (parms);
11523 arg = TREE_VALUE (args);
11524 args = TREE_CHAIN (args);
11525 arg_expr = NULL;
11527 if (arg == error_mark_node)
11528 return 1;
11529 if (arg == unknown_type_node)
11530 /* We can't deduce anything from this, but we might get all the
11531 template args from other function args. */
11532 continue;
11534 /* Conversions will be performed on a function argument that
11535 corresponds with a function parameter that contains only
11536 non-deducible template parameters and explicitly specified
11537 template parameters. */
11538 if (!uses_template_parms (parm))
11540 tree type;
11542 if (!TYPE_P (arg))
11543 type = TREE_TYPE (arg);
11544 else
11545 type = arg;
11547 if (same_type_p (parm, type))
11548 continue;
11549 if (strict != DEDUCE_EXACT
11550 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
11551 flags))
11552 continue;
11554 return 1;
11557 if (!TYPE_P (arg))
11559 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
11560 if (type_unknown_p (arg))
11562 /* [temp.deduct.type]
11564 A template-argument can be deduced from a pointer to
11565 function or pointer to member function argument if
11566 the set of overloaded functions does not contain
11567 function templates and at most one of a set of
11568 overloaded functions provides a unique match. */
11569 if (resolve_overloaded_unification
11570 (tparms, targs, parm, arg, strict, sub_strict))
11571 continue;
11573 return 1;
11575 arg_expr = arg;
11576 arg = unlowered_expr_type (arg);
11577 if (arg == error_mark_node)
11578 return 1;
11582 int arg_strict = sub_strict;
11584 if (!subr)
11585 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
11586 arg_expr);
11588 if (unify (tparms, targs, parm, arg, arg_strict))
11589 return 1;
11594 if (parms
11595 && parms != void_list_node
11596 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
11598 /* Unify the remaining arguments with the pack expansion type. */
11599 tree argvec;
11600 tree parmvec = make_tree_vec (1);
11601 int len = 0;
11602 tree t;
11604 /* Count the number of arguments that remain. */
11605 for (t = args; t && t != void_list_node; t = TREE_CHAIN (t))
11606 len++;
11608 /* Allocate a TREE_VEC and copy in all of the arguments */
11609 argvec = make_tree_vec (len);
11610 for (i = 0; args && args != void_list_node; args = TREE_CHAIN (args))
11612 TREE_VEC_ELT (argvec, i) = TREE_VALUE (args);
11613 ++i;
11616 /* Copy the parameter into parmvec. */
11617 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
11618 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
11619 /*call_args_p=*/true, /*subr=*/subr))
11620 return 1;
11622 /* Advance to the end of the list of parameters. */
11623 parms = TREE_CHAIN (parms);
11626 /* Fail if we've reached the end of the parm list, and more args
11627 are present, and the parm list isn't variadic. */
11628 if (args && args != void_list_node && parms == void_list_node)
11629 return 1;
11630 /* Fail if parms are left and they don't have default values. */
11631 if (parms && parms != void_list_node
11632 && TREE_PURPOSE (parms) == NULL_TREE)
11633 return 1;
11635 if (!subr)
11636 for (i = 0; i < ntparms; i++)
11637 if (!TREE_VEC_ELT (targs, i))
11639 tree tparm;
11641 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
11642 continue;
11644 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
11646 /* If this is an undeduced nontype parameter that depends on
11647 a type parameter, try another pass; its type may have been
11648 deduced from a later argument than the one from which
11649 this parameter can be deduced. */
11650 if (TREE_CODE (tparm) == PARM_DECL
11651 && uses_template_parms (TREE_TYPE (tparm))
11652 && !saw_undeduced++)
11653 goto again;
11655 /* Core issue #226 (C++0x) [temp.deduct]:
11657 If a template argument has not been deduced, its
11658 default template argument, if any, is used.
11660 When we are in C++98 mode, TREE_PURPOSE will either
11661 be NULL_TREE or ERROR_MARK_NODE, so we do not need
11662 to explicitly check cxx_dialect here. */
11663 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
11665 tree arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)),
11666 targs, tf_none, NULL_TREE);
11667 if (arg == error_mark_node)
11668 return 1;
11669 else
11671 TREE_VEC_ELT (targs, i) = arg;
11672 continue;
11676 /* If the type parameter is a parameter pack, then it will
11677 be deduced to an empty parameter pack. */
11678 if (template_parameter_pack_p (tparm))
11680 tree arg;
11682 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
11684 arg = make_node (NONTYPE_ARGUMENT_PACK);
11685 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
11686 TREE_CONSTANT (arg) = 1;
11688 else
11689 arg = make_node (TYPE_ARGUMENT_PACK);
11691 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
11693 TREE_VEC_ELT (targs, i) = arg;
11694 continue;
11697 return 2;
11700 return 0;
11703 /* Subroutine of type_unification_real. Args are like the variables
11704 at the call site. ARG is an overloaded function (or template-id);
11705 we try deducing template args from each of the overloads, and if
11706 only one succeeds, we go with that. Modifies TARGS and returns
11707 true on success. */
11709 static bool
11710 resolve_overloaded_unification (tree tparms,
11711 tree targs,
11712 tree parm,
11713 tree arg,
11714 unification_kind_t strict,
11715 int sub_strict)
11717 tree tempargs = copy_node (targs);
11718 int good = 0;
11719 bool addr_p;
11721 if (TREE_CODE (arg) == ADDR_EXPR)
11723 arg = TREE_OPERAND (arg, 0);
11724 addr_p = true;
11726 else
11727 addr_p = false;
11729 if (TREE_CODE (arg) == COMPONENT_REF)
11730 /* Handle `&x' where `x' is some static or non-static member
11731 function name. */
11732 arg = TREE_OPERAND (arg, 1);
11734 if (TREE_CODE (arg) == OFFSET_REF)
11735 arg = TREE_OPERAND (arg, 1);
11737 /* Strip baselink information. */
11738 if (BASELINK_P (arg))
11739 arg = BASELINK_FUNCTIONS (arg);
11741 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
11743 /* If we got some explicit template args, we need to plug them into
11744 the affected templates before we try to unify, in case the
11745 explicit args will completely resolve the templates in question. */
11747 tree expl_subargs = TREE_OPERAND (arg, 1);
11748 arg = TREE_OPERAND (arg, 0);
11750 for (; arg; arg = OVL_NEXT (arg))
11752 tree fn = OVL_CURRENT (arg);
11753 tree subargs, elem;
11755 if (TREE_CODE (fn) != TEMPLATE_DECL)
11756 continue;
11758 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
11759 expl_subargs, /*check_ret=*/false);
11760 if (subargs)
11762 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
11763 good += try_one_overload (tparms, targs, tempargs, parm,
11764 elem, strict, sub_strict, addr_p);
11768 else if (TREE_CODE (arg) != OVERLOAD
11769 && TREE_CODE (arg) != FUNCTION_DECL)
11770 /* If ARG is, for example, "(0, &f)" then its type will be unknown
11771 -- but the deduction does not succeed because the expression is
11772 not just the function on its own. */
11773 return false;
11774 else
11775 for (; arg; arg = OVL_NEXT (arg))
11776 good += try_one_overload (tparms, targs, tempargs, parm,
11777 TREE_TYPE (OVL_CURRENT (arg)),
11778 strict, sub_strict, addr_p);
11780 /* [temp.deduct.type] A template-argument can be deduced from a pointer
11781 to function or pointer to member function argument if the set of
11782 overloaded functions does not contain function templates and at most
11783 one of a set of overloaded functions provides a unique match.
11785 So if we found multiple possibilities, we return success but don't
11786 deduce anything. */
11788 if (good == 1)
11790 int i = TREE_VEC_LENGTH (targs);
11791 for (; i--; )
11792 if (TREE_VEC_ELT (tempargs, i))
11793 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
11795 if (good)
11796 return true;
11798 return false;
11801 /* Subroutine of resolve_overloaded_unification; does deduction for a single
11802 overload. Fills TARGS with any deduced arguments, or error_mark_node if
11803 different overloads deduce different arguments for a given parm.
11804 ADDR_P is true if the expression for which deduction is being
11805 performed was of the form "& fn" rather than simply "fn".
11807 Returns 1 on success. */
11809 static int
11810 try_one_overload (tree tparms,
11811 tree orig_targs,
11812 tree targs,
11813 tree parm,
11814 tree arg,
11815 unification_kind_t strict,
11816 int sub_strict,
11817 bool addr_p)
11819 int nargs;
11820 tree tempargs;
11821 int i;
11823 /* [temp.deduct.type] A template-argument can be deduced from a pointer
11824 to function or pointer to member function argument if the set of
11825 overloaded functions does not contain function templates and at most
11826 one of a set of overloaded functions provides a unique match.
11828 So if this is a template, just return success. */
11830 if (uses_template_parms (arg))
11831 return 1;
11833 if (TREE_CODE (arg) == METHOD_TYPE)
11834 arg = build_ptrmemfunc_type (build_pointer_type (arg));
11835 else if (addr_p)
11836 arg = build_pointer_type (arg);
11838 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
11840 /* We don't copy orig_targs for this because if we have already deduced
11841 some template args from previous args, unify would complain when we
11842 try to deduce a template parameter for the same argument, even though
11843 there isn't really a conflict. */
11844 nargs = TREE_VEC_LENGTH (targs);
11845 tempargs = make_tree_vec (nargs);
11847 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
11848 return 0;
11850 /* First make sure we didn't deduce anything that conflicts with
11851 explicitly specified args. */
11852 for (i = nargs; i--; )
11854 tree elt = TREE_VEC_ELT (tempargs, i);
11855 tree oldelt = TREE_VEC_ELT (orig_targs, i);
11857 if (!elt)
11858 /*NOP*/;
11859 else if (uses_template_parms (elt))
11860 /* Since we're unifying against ourselves, we will fill in
11861 template args used in the function parm list with our own
11862 template parms. Discard them. */
11863 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
11864 else if (oldelt && !template_args_equal (oldelt, elt))
11865 return 0;
11868 for (i = nargs; i--; )
11870 tree elt = TREE_VEC_ELT (tempargs, i);
11872 if (elt)
11873 TREE_VEC_ELT (targs, i) = elt;
11876 return 1;
11879 /* PARM is a template class (perhaps with unbound template
11880 parameters). ARG is a fully instantiated type. If ARG can be
11881 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
11882 TARGS are as for unify. */
11884 static tree
11885 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
11887 tree copy_of_targs;
11889 if (!CLASSTYPE_TEMPLATE_INFO (arg)
11890 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
11891 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
11892 return NULL_TREE;
11894 /* We need to make a new template argument vector for the call to
11895 unify. If we used TARGS, we'd clutter it up with the result of
11896 the attempted unification, even if this class didn't work out.
11897 We also don't want to commit ourselves to all the unifications
11898 we've already done, since unification is supposed to be done on
11899 an argument-by-argument basis. In other words, consider the
11900 following pathological case:
11902 template <int I, int J, int K>
11903 struct S {};
11905 template <int I, int J>
11906 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
11908 template <int I, int J, int K>
11909 void f(S<I, J, K>, S<I, I, I>);
11911 void g() {
11912 S<0, 0, 0> s0;
11913 S<0, 1, 2> s2;
11915 f(s0, s2);
11918 Now, by the time we consider the unification involving `s2', we
11919 already know that we must have `f<0, 0, 0>'. But, even though
11920 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
11921 because there are two ways to unify base classes of S<0, 1, 2>
11922 with S<I, I, I>. If we kept the already deduced knowledge, we
11923 would reject the possibility I=1. */
11924 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
11926 /* If unification failed, we're done. */
11927 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
11928 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
11929 return NULL_TREE;
11931 return arg;
11934 /* Given a template type PARM and a class type ARG, find the unique
11935 base type in ARG that is an instance of PARM. We do not examine
11936 ARG itself; only its base-classes. If there is not exactly one
11937 appropriate base class, return NULL_TREE. PARM may be the type of
11938 a partial specialization, as well as a plain template type. Used
11939 by unify. */
11941 static tree
11942 get_template_base (tree tparms, tree targs, tree parm, tree arg)
11944 tree rval = NULL_TREE;
11945 tree binfo;
11947 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
11949 binfo = TYPE_BINFO (complete_type (arg));
11950 if (!binfo)
11951 /* The type could not be completed. */
11952 return NULL_TREE;
11954 /* Walk in inheritance graph order. The search order is not
11955 important, and this avoids multiple walks of virtual bases. */
11956 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
11958 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
11960 if (r)
11962 /* If there is more than one satisfactory baseclass, then:
11964 [temp.deduct.call]
11966 If they yield more than one possible deduced A, the type
11967 deduction fails.
11969 applies. */
11970 if (rval && !same_type_p (r, rval))
11971 return NULL_TREE;
11973 rval = r;
11977 return rval;
11980 /* Returns the level of DECL, which declares a template parameter. */
11982 static int
11983 template_decl_level (tree decl)
11985 switch (TREE_CODE (decl))
11987 case TYPE_DECL:
11988 case TEMPLATE_DECL:
11989 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
11991 case PARM_DECL:
11992 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
11994 default:
11995 gcc_unreachable ();
11997 return 0;
12000 /* Decide whether ARG can be unified with PARM, considering only the
12001 cv-qualifiers of each type, given STRICT as documented for unify.
12002 Returns nonzero iff the unification is OK on that basis. */
12004 static int
12005 check_cv_quals_for_unify (int strict, tree arg, tree parm)
12007 int arg_quals = cp_type_quals (arg);
12008 int parm_quals = cp_type_quals (parm);
12010 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
12011 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
12013 /* Although a CVR qualifier is ignored when being applied to a
12014 substituted template parameter ([8.3.2]/1 for example), that
12015 does not apply during deduction [14.8.2.4]/1, (even though
12016 that is not explicitly mentioned, [14.8.2.4]/9 indicates
12017 this). Except when we're allowing additional CV qualifiers
12018 at the outer level [14.8.2.1]/3,1st bullet. */
12019 if ((TREE_CODE (arg) == REFERENCE_TYPE
12020 || TREE_CODE (arg) == FUNCTION_TYPE
12021 || TREE_CODE (arg) == METHOD_TYPE)
12022 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
12023 return 0;
12025 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
12026 && (parm_quals & TYPE_QUAL_RESTRICT))
12027 return 0;
12030 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
12031 && (arg_quals & parm_quals) != parm_quals)
12032 return 0;
12034 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
12035 && (parm_quals & arg_quals) != arg_quals)
12036 return 0;
12038 return 1;
12041 /* Determines the LEVEL and INDEX for the template parameter PARM. */
12042 void
12043 template_parm_level_and_index (tree parm, int* level, int* index)
12045 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
12046 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
12047 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
12049 *index = TEMPLATE_TYPE_IDX (parm);
12050 *level = TEMPLATE_TYPE_LEVEL (parm);
12052 else
12054 *index = TEMPLATE_PARM_IDX (parm);
12055 *level = TEMPLATE_PARM_LEVEL (parm);
12059 /* Unifies the remaining arguments in PACKED_ARGS with the pack
12060 expansion at the end of PACKED_PARMS. Returns 0 if the type
12061 deduction succeeds, 1 otherwise. STRICT is the same as in
12062 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
12063 call argument list. We'll need to adjust the arguments to make them
12064 types. SUBR tells us if this is from a recursive call to
12065 type_unification_real. */
12067 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
12068 tree packed_args, int strict, bool call_args_p,
12069 bool subr)
12071 tree parm
12072 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
12073 tree pattern = PACK_EXPANSION_PATTERN (parm);
12074 tree pack, packs = NULL_TREE;
12075 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
12076 int len = TREE_VEC_LENGTH (packed_args);
12078 /* Determine the parameter packs we will be deducing from the
12079 pattern, and record their current deductions. */
12080 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
12081 pack; pack = TREE_CHAIN (pack))
12083 tree parm_pack = TREE_VALUE (pack);
12084 int idx, level;
12086 /* Determine the index and level of this parameter pack. */
12087 template_parm_level_and_index (parm_pack, &level, &idx);
12089 /* Keep track of the parameter packs and their corresponding
12090 argument packs. */
12091 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
12092 TREE_TYPE (packs) = make_tree_vec (len - start);
12095 /* Loop through all of the arguments that have not yet been
12096 unified and unify each with the pattern. */
12097 for (i = start; i < len; i++)
12099 tree parm = pattern;
12101 /* For each parameter pack, clear out the deduced value so that
12102 we can deduce it again. */
12103 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12105 int idx, level;
12106 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
12108 TMPL_ARG (targs, level, idx) = NULL_TREE;
12111 /* Unify the pattern with the current argument. */
12113 tree arg = TREE_VEC_ELT (packed_args, i);
12114 int arg_strict = strict;
12115 bool skip_arg_p = false;
12117 if (call_args_p)
12119 int sub_strict;
12121 /* This mirrors what we do in type_unification_real. */
12122 switch (strict)
12124 case DEDUCE_CALL:
12125 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
12126 | UNIFY_ALLOW_MORE_CV_QUAL
12127 | UNIFY_ALLOW_DERIVED);
12128 break;
12130 case DEDUCE_CONV:
12131 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
12132 break;
12134 case DEDUCE_EXACT:
12135 sub_strict = UNIFY_ALLOW_NONE;
12136 break;
12138 default:
12139 gcc_unreachable ();
12142 if (!TYPE_P (arg))
12144 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
12145 if (type_unknown_p (arg))
12147 /* [temp.deduct.type] A template-argument can be
12148 deduced from a pointer to function or pointer
12149 to member function argument if the set of
12150 overloaded functions does not contain function
12151 templates and at most one of a set of
12152 overloaded functions provides a unique
12153 match. */
12155 if (resolve_overloaded_unification
12156 (tparms, targs, parm, arg, strict, sub_strict)
12157 != 0)
12158 return 1;
12159 skip_arg_p = true;
12162 if (!skip_arg_p)
12164 arg = TREE_TYPE (arg);
12165 if (arg == error_mark_node)
12166 return 1;
12170 arg_strict = sub_strict;
12172 if (!subr)
12173 arg_strict |=
12174 maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
12177 if (!skip_arg_p)
12179 if (unify (tparms, targs, parm, arg, arg_strict))
12180 return 1;
12184 /* For each parameter pack, collect the deduced value. */
12185 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12187 int idx, level;
12188 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
12190 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
12191 TMPL_ARG (targs, level, idx);
12195 /* Verify that the results of unification with the parameter packs
12196 produce results consistent with what we've seen before, and make
12197 the deduced argument packs available. */
12198 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12200 tree old_pack = TREE_VALUE (pack);
12201 tree new_args = TREE_TYPE (pack);
12203 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
12205 /* Prepend the explicit arguments onto NEW_ARGS. */
12206 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
12207 tree old_args = new_args;
12208 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
12209 int len = explicit_len + TREE_VEC_LENGTH (old_args);
12211 /* Copy the explicit arguments. */
12212 new_args = make_tree_vec (len);
12213 for (i = 0; i < explicit_len; i++)
12214 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
12216 /* Copy the deduced arguments. */
12217 for (; i < len; i++)
12218 TREE_VEC_ELT (new_args, i) =
12219 TREE_VEC_ELT (old_args, i - explicit_len);
12222 if (!old_pack)
12224 tree result;
12225 int idx, level;
12227 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
12229 /* Build the deduced *_ARGUMENT_PACK. */
12230 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
12232 result = make_node (NONTYPE_ARGUMENT_PACK);
12233 TREE_TYPE (result) =
12234 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
12235 TREE_CONSTANT (result) = 1;
12237 else
12238 result = make_node (TYPE_ARGUMENT_PACK);
12240 SET_ARGUMENT_PACK_ARGS (result, new_args);
12242 /* Note the deduced argument packs for this parameter
12243 pack. */
12244 TMPL_ARG (targs, level, idx) = result;
12246 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
12247 && (ARGUMENT_PACK_ARGS (old_pack)
12248 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
12250 /* We only had the explicitly-provided arguments before, but
12251 now we have a complete set of arguments. */
12252 int idx, level;
12253 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
12254 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
12256 /* Keep the original deduced argument pack. */
12257 TMPL_ARG (targs, level, idx) = old_pack;
12259 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
12260 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
12261 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
12263 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
12264 new_args))
12265 /* Inconsistent unification of this parameter pack. */
12266 return 1;
12267 else
12269 int idx, level;
12271 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
12273 /* Keep the original deduced argument pack. */
12274 TMPL_ARG (targs, level, idx) = old_pack;
12278 return 0;
12281 /* Deduce the value of template parameters. TPARMS is the (innermost)
12282 set of template parameters to a template. TARGS is the bindings
12283 for those template parameters, as determined thus far; TARGS may
12284 include template arguments for outer levels of template parameters
12285 as well. PARM is a parameter to a template function, or a
12286 subcomponent of that parameter; ARG is the corresponding argument.
12287 This function attempts to match PARM with ARG in a manner
12288 consistent with the existing assignments in TARGS. If more values
12289 are deduced, then TARGS is updated.
12291 Returns 0 if the type deduction succeeds, 1 otherwise. The
12292 parameter STRICT is a bitwise or of the following flags:
12294 UNIFY_ALLOW_NONE:
12295 Require an exact match between PARM and ARG.
12296 UNIFY_ALLOW_MORE_CV_QUAL:
12297 Allow the deduced ARG to be more cv-qualified (by qualification
12298 conversion) than ARG.
12299 UNIFY_ALLOW_LESS_CV_QUAL:
12300 Allow the deduced ARG to be less cv-qualified than ARG.
12301 UNIFY_ALLOW_DERIVED:
12302 Allow the deduced ARG to be a template base class of ARG,
12303 or a pointer to a template base class of the type pointed to by
12304 ARG.
12305 UNIFY_ALLOW_INTEGER:
12306 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
12307 case for more information.
12308 UNIFY_ALLOW_OUTER_LEVEL:
12309 This is the outermost level of a deduction. Used to determine validity
12310 of qualification conversions. A valid qualification conversion must
12311 have const qualified pointers leading up to the inner type which
12312 requires additional CV quals, except at the outer level, where const
12313 is not required [conv.qual]. It would be normal to set this flag in
12314 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
12315 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
12316 This is the outermost level of a deduction, and PARM can be more CV
12317 qualified at this point.
12318 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
12319 This is the outermost level of a deduction, and PARM can be less CV
12320 qualified at this point. */
12322 static int
12323 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
12325 int idx;
12326 tree targ;
12327 tree tparm;
12328 int strict_in = strict;
12330 /* I don't think this will do the right thing with respect to types.
12331 But the only case I've seen it in so far has been array bounds, where
12332 signedness is the only information lost, and I think that will be
12333 okay. */
12334 while (TREE_CODE (parm) == NOP_EXPR)
12335 parm = TREE_OPERAND (parm, 0);
12337 if (arg == error_mark_node)
12338 return 1;
12339 if (arg == unknown_type_node)
12340 /* We can't deduce anything from this, but we might get all the
12341 template args from other function args. */
12342 return 0;
12344 /* If PARM uses template parameters, then we can't bail out here,
12345 even if ARG == PARM, since we won't record unifications for the
12346 template parameters. We might need them if we're trying to
12347 figure out which of two things is more specialized. */
12348 if (arg == parm && !uses_template_parms (parm))
12349 return 0;
12351 /* Immediately reject some pairs that won't unify because of
12352 cv-qualification mismatches. */
12353 if (TREE_CODE (arg) == TREE_CODE (parm)
12354 && TYPE_P (arg)
12355 /* It is the elements of the array which hold the cv quals of an array
12356 type, and the elements might be template type parms. We'll check
12357 when we recurse. */
12358 && TREE_CODE (arg) != ARRAY_TYPE
12359 /* We check the cv-qualifiers when unifying with template type
12360 parameters below. We want to allow ARG `const T' to unify with
12361 PARM `T' for example, when computing which of two templates
12362 is more specialized, for example. */
12363 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
12364 && !check_cv_quals_for_unify (strict_in, arg, parm))
12365 return 1;
12367 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
12368 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
12369 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
12370 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
12371 strict &= ~UNIFY_ALLOW_DERIVED;
12372 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
12373 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
12375 switch (TREE_CODE (parm))
12377 case TYPENAME_TYPE:
12378 case SCOPE_REF:
12379 case UNBOUND_CLASS_TEMPLATE:
12380 /* In a type which contains a nested-name-specifier, template
12381 argument values cannot be deduced for template parameters used
12382 within the nested-name-specifier. */
12383 return 0;
12385 case TEMPLATE_TYPE_PARM:
12386 case TEMPLATE_TEMPLATE_PARM:
12387 case BOUND_TEMPLATE_TEMPLATE_PARM:
12388 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
12389 if (tparm == error_mark_node)
12390 return 1;
12392 if (TEMPLATE_TYPE_LEVEL (parm)
12393 != template_decl_level (tparm))
12394 /* The PARM is not one we're trying to unify. Just check
12395 to see if it matches ARG. */
12396 return (TREE_CODE (arg) == TREE_CODE (parm)
12397 && same_type_p (parm, arg)) ? 0 : 1;
12398 idx = TEMPLATE_TYPE_IDX (parm);
12399 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
12400 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
12402 /* Check for mixed types and values. */
12403 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
12404 && TREE_CODE (tparm) != TYPE_DECL)
12405 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
12406 && TREE_CODE (tparm) != TEMPLATE_DECL))
12407 return 1;
12409 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
12411 /* ARG must be constructed from a template class or a template
12412 template parameter. */
12413 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
12414 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
12415 return 1;
12418 tree parmvec = TYPE_TI_ARGS (parm);
12419 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
12420 tree argtmplvec
12421 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
12422 int i;
12424 /* The resolution to DR150 makes clear that default
12425 arguments for an N-argument may not be used to bind T
12426 to a template template parameter with fewer than N
12427 parameters. It is not safe to permit the binding of
12428 default arguments as an extension, as that may change
12429 the meaning of a conforming program. Consider:
12431 struct Dense { static const unsigned int dim = 1; };
12433 template <template <typename> class View,
12434 typename Block>
12435 void operator+(float, View<Block> const&);
12437 template <typename Block,
12438 unsigned int Dim = Block::dim>
12439 struct Lvalue_proxy { operator float() const; };
12441 void
12442 test_1d (void) {
12443 Lvalue_proxy<Dense> p;
12444 float b;
12445 b + p;
12448 Here, if Lvalue_proxy is permitted to bind to View, then
12449 the global operator+ will be used; if they are not, the
12450 Lvalue_proxy will be converted to float. */
12451 if (coerce_template_parms (argtmplvec, parmvec,
12452 TYPE_TI_TEMPLATE (parm),
12453 tf_none,
12454 /*require_all_args=*/true,
12455 /*use_default_args=*/false)
12456 == error_mark_node)
12457 return 1;
12459 /* Deduce arguments T, i from TT<T> or TT<i>.
12460 We check each element of PARMVEC and ARGVEC individually
12461 rather than the whole TREE_VEC since they can have
12462 different number of elements. */
12464 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
12466 if (unify (tparms, targs,
12467 TREE_VEC_ELT (parmvec, i),
12468 TREE_VEC_ELT (argvec, i),
12469 UNIFY_ALLOW_NONE))
12470 return 1;
12473 arg = TYPE_TI_TEMPLATE (arg);
12475 /* Fall through to deduce template name. */
12478 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
12479 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
12481 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
12483 /* Simple cases: Value already set, does match or doesn't. */
12484 if (targ != NULL_TREE && template_args_equal (targ, arg))
12485 return 0;
12486 else if (targ)
12487 return 1;
12489 else
12491 /* If PARM is `const T' and ARG is only `int', we don't have
12492 a match unless we are allowing additional qualification.
12493 If ARG is `const int' and PARM is just `T' that's OK;
12494 that binds `const int' to `T'. */
12495 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
12496 arg, parm))
12497 return 1;
12499 /* Consider the case where ARG is `const volatile int' and
12500 PARM is `const T'. Then, T should be `volatile int'. */
12501 arg = cp_build_qualified_type_real
12502 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
12503 if (arg == error_mark_node)
12504 return 1;
12506 /* Simple cases: Value already set, does match or doesn't. */
12507 if (targ != NULL_TREE && same_type_p (targ, arg))
12508 return 0;
12509 else if (targ)
12510 return 1;
12512 /* Make sure that ARG is not a variable-sized array. (Note
12513 that were talking about variable-sized arrays (like
12514 `int[n]'), rather than arrays of unknown size (like
12515 `int[]').) We'll get very confused by such a type since
12516 the bound of the array will not be computable in an
12517 instantiation. Besides, such types are not allowed in
12518 ISO C++, so we can do as we please here. */
12519 if (variably_modified_type_p (arg, NULL_TREE))
12520 return 1;
12523 /* If ARG is a parameter pack or an expansion, we cannot unify
12524 against it unless PARM is also a parameter pack. */
12525 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
12526 && !template_parameter_pack_p (parm))
12527 return 1;
12529 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
12530 return 0;
12532 case TEMPLATE_PARM_INDEX:
12533 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
12534 if (tparm == error_mark_node)
12535 return 1;
12537 if (TEMPLATE_PARM_LEVEL (parm)
12538 != template_decl_level (tparm))
12539 /* The PARM is not one we're trying to unify. Just check
12540 to see if it matches ARG. */
12541 return !(TREE_CODE (arg) == TREE_CODE (parm)
12542 && cp_tree_equal (parm, arg));
12544 idx = TEMPLATE_PARM_IDX (parm);
12545 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
12547 if (targ)
12548 return !cp_tree_equal (targ, arg);
12550 /* [temp.deduct.type] If, in the declaration of a function template
12551 with a non-type template-parameter, the non-type
12552 template-parameter is used in an expression in the function
12553 parameter-list and, if the corresponding template-argument is
12554 deduced, the template-argument type shall match the type of the
12555 template-parameter exactly, except that a template-argument
12556 deduced from an array bound may be of any integral type.
12557 The non-type parameter might use already deduced type parameters. */
12558 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
12559 if (!TREE_TYPE (arg))
12560 /* Template-parameter dependent expression. Just accept it for now.
12561 It will later be processed in convert_template_argument. */
12563 else if (same_type_p (TREE_TYPE (arg), tparm))
12564 /* OK */;
12565 else if ((strict & UNIFY_ALLOW_INTEGER)
12566 && (TREE_CODE (tparm) == INTEGER_TYPE
12567 || TREE_CODE (tparm) == BOOLEAN_TYPE))
12568 /* Convert the ARG to the type of PARM; the deduced non-type
12569 template argument must exactly match the types of the
12570 corresponding parameter. */
12571 arg = fold (build_nop (TREE_TYPE (parm), arg));
12572 else if (uses_template_parms (tparm))
12573 /* We haven't deduced the type of this parameter yet. Try again
12574 later. */
12575 return 0;
12576 else
12577 return 1;
12579 /* If ARG is a parameter pack or an expansion, we cannot unify
12580 against it unless PARM is also a parameter pack. */
12581 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
12582 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
12583 return 1;
12585 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
12586 return 0;
12588 case PTRMEM_CST:
12590 /* A pointer-to-member constant can be unified only with
12591 another constant. */
12592 if (TREE_CODE (arg) != PTRMEM_CST)
12593 return 1;
12595 /* Just unify the class member. It would be useless (and possibly
12596 wrong, depending on the strict flags) to unify also
12597 PTRMEM_CST_CLASS, because we want to be sure that both parm and
12598 arg refer to the same variable, even if through different
12599 classes. For instance:
12601 struct A { int x; };
12602 struct B : A { };
12604 Unification of &A::x and &B::x must succeed. */
12605 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
12606 PTRMEM_CST_MEMBER (arg), strict);
12609 case POINTER_TYPE:
12611 if (TREE_CODE (arg) != POINTER_TYPE)
12612 return 1;
12614 /* [temp.deduct.call]
12616 A can be another pointer or pointer to member type that can
12617 be converted to the deduced A via a qualification
12618 conversion (_conv.qual_).
12620 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
12621 This will allow for additional cv-qualification of the
12622 pointed-to types if appropriate. */
12624 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
12625 /* The derived-to-base conversion only persists through one
12626 level of pointers. */
12627 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
12629 return unify (tparms, targs, TREE_TYPE (parm),
12630 TREE_TYPE (arg), strict);
12633 case REFERENCE_TYPE:
12634 if (TREE_CODE (arg) != REFERENCE_TYPE)
12635 return 1;
12636 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
12637 strict & UNIFY_ALLOW_MORE_CV_QUAL);
12639 case ARRAY_TYPE:
12640 if (TREE_CODE (arg) != ARRAY_TYPE)
12641 return 1;
12642 if ((TYPE_DOMAIN (parm) == NULL_TREE)
12643 != (TYPE_DOMAIN (arg) == NULL_TREE))
12644 return 1;
12645 if (TYPE_DOMAIN (parm) != NULL_TREE)
12647 tree parm_max;
12648 tree arg_max;
12649 bool parm_cst;
12650 bool arg_cst;
12652 /* Our representation of array types uses "N - 1" as the
12653 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
12654 not an integer constant. We cannot unify arbitrarily
12655 complex expressions, so we eliminate the MINUS_EXPRs
12656 here. */
12657 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
12658 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
12659 if (!parm_cst)
12661 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
12662 parm_max = TREE_OPERAND (parm_max, 0);
12664 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
12665 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
12666 if (!arg_cst)
12668 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
12669 trying to unify the type of a variable with the type
12670 of a template parameter. For example:
12672 template <unsigned int N>
12673 void f (char (&) [N]);
12674 int g();
12675 void h(int i) {
12676 char a[g(i)];
12677 f(a);
12680 Here, the type of the ARG will be "int [g(i)]", and
12681 may be a SAVE_EXPR, etc. */
12682 if (TREE_CODE (arg_max) != MINUS_EXPR)
12683 return 1;
12684 arg_max = TREE_OPERAND (arg_max, 0);
12687 /* If only one of the bounds used a MINUS_EXPR, compensate
12688 by adding one to the other bound. */
12689 if (parm_cst && !arg_cst)
12690 parm_max = fold_build2 (PLUS_EXPR,
12691 integer_type_node,
12692 parm_max,
12693 integer_one_node);
12694 else if (arg_cst && !parm_cst)
12695 arg_max = fold_build2 (PLUS_EXPR,
12696 integer_type_node,
12697 arg_max,
12698 integer_one_node);
12700 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
12701 return 1;
12703 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
12704 strict & UNIFY_ALLOW_MORE_CV_QUAL);
12706 case REAL_TYPE:
12707 case COMPLEX_TYPE:
12708 case VECTOR_TYPE:
12709 case INTEGER_TYPE:
12710 case BOOLEAN_TYPE:
12711 case ENUMERAL_TYPE:
12712 case VOID_TYPE:
12713 if (TREE_CODE (arg) != TREE_CODE (parm))
12714 return 1;
12716 /* We have already checked cv-qualification at the top of the
12717 function. */
12718 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
12719 return 1;
12721 /* As far as unification is concerned, this wins. Later checks
12722 will invalidate it if necessary. */
12723 return 0;
12725 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
12726 /* Type INTEGER_CST can come from ordinary constant template args. */
12727 case INTEGER_CST:
12728 while (TREE_CODE (arg) == NOP_EXPR)
12729 arg = TREE_OPERAND (arg, 0);
12731 if (TREE_CODE (arg) != INTEGER_CST)
12732 return 1;
12733 return !tree_int_cst_equal (parm, arg);
12735 case TREE_VEC:
12737 int i;
12738 if (TREE_CODE (arg) != TREE_VEC)
12739 return 1;
12740 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
12741 return 1;
12742 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
12743 if (unify (tparms, targs,
12744 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
12745 UNIFY_ALLOW_NONE))
12746 return 1;
12747 return 0;
12750 case RECORD_TYPE:
12751 case UNION_TYPE:
12752 if (TREE_CODE (arg) != TREE_CODE (parm))
12753 return 1;
12755 if (TYPE_PTRMEMFUNC_P (parm))
12757 if (!TYPE_PTRMEMFUNC_P (arg))
12758 return 1;
12760 return unify (tparms, targs,
12761 TYPE_PTRMEMFUNC_FN_TYPE (parm),
12762 TYPE_PTRMEMFUNC_FN_TYPE (arg),
12763 strict);
12766 if (CLASSTYPE_TEMPLATE_INFO (parm))
12768 tree t = NULL_TREE;
12770 if (strict_in & UNIFY_ALLOW_DERIVED)
12772 /* First, we try to unify the PARM and ARG directly. */
12773 t = try_class_unification (tparms, targs,
12774 parm, arg);
12776 if (!t)
12778 /* Fallback to the special case allowed in
12779 [temp.deduct.call]:
12781 If P is a class, and P has the form
12782 template-id, then A can be a derived class of
12783 the deduced A. Likewise, if P is a pointer to
12784 a class of the form template-id, A can be a
12785 pointer to a derived class pointed to by the
12786 deduced A. */
12787 t = get_template_base (tparms, targs, parm, arg);
12789 if (!t)
12790 return 1;
12793 else if (CLASSTYPE_TEMPLATE_INFO (arg)
12794 && (CLASSTYPE_TI_TEMPLATE (parm)
12795 == CLASSTYPE_TI_TEMPLATE (arg)))
12796 /* Perhaps PARM is something like S<U> and ARG is S<int>.
12797 Then, we should unify `int' and `U'. */
12798 t = arg;
12799 else
12800 /* There's no chance of unification succeeding. */
12801 return 1;
12803 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
12804 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
12806 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
12807 return 1;
12808 return 0;
12810 case METHOD_TYPE:
12811 case FUNCTION_TYPE:
12812 if (TREE_CODE (arg) != TREE_CODE (parm))
12813 return 1;
12815 /* CV qualifications for methods can never be deduced, they must
12816 match exactly. We need to check them explicitly here,
12817 because type_unification_real treats them as any other
12818 cvqualified parameter. */
12819 if (TREE_CODE (parm) == METHOD_TYPE
12820 && (!check_cv_quals_for_unify
12821 (UNIFY_ALLOW_NONE,
12822 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
12823 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
12824 return 1;
12826 if (unify (tparms, targs, TREE_TYPE (parm),
12827 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
12828 return 1;
12829 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
12830 TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT,
12831 LOOKUP_NORMAL);
12833 case OFFSET_TYPE:
12834 /* Unify a pointer to member with a pointer to member function, which
12835 deduces the type of the member as a function type. */
12836 if (TYPE_PTRMEMFUNC_P (arg))
12838 tree method_type;
12839 tree fntype;
12840 cp_cv_quals cv_quals;
12842 /* Check top-level cv qualifiers */
12843 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
12844 return 1;
12846 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
12847 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
12848 return 1;
12850 /* Determine the type of the function we are unifying against. */
12851 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
12852 fntype =
12853 build_function_type (TREE_TYPE (method_type),
12854 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
12856 /* Extract the cv-qualifiers of the member function from the
12857 implicit object parameter and place them on the function
12858 type to be restored later. */
12859 cv_quals =
12860 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
12861 fntype = build_qualified_type (fntype, cv_quals);
12862 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
12865 if (TREE_CODE (arg) != OFFSET_TYPE)
12866 return 1;
12867 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
12868 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
12869 return 1;
12870 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
12871 strict);
12873 case CONST_DECL:
12874 if (DECL_TEMPLATE_PARM_P (parm))
12875 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
12876 if (arg != integral_constant_value (parm))
12877 return 1;
12878 return 0;
12880 case FIELD_DECL:
12881 case TEMPLATE_DECL:
12882 /* Matched cases are handled by the ARG == PARM test above. */
12883 return 1;
12885 case TYPE_ARGUMENT_PACK:
12886 case NONTYPE_ARGUMENT_PACK:
12888 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
12889 tree packed_args = ARGUMENT_PACK_ARGS (arg);
12890 int i, len = TREE_VEC_LENGTH (packed_parms);
12891 int argslen = TREE_VEC_LENGTH (packed_args);
12892 int parm_variadic_p = 0;
12894 /* Check if the parameters end in a pack, making them variadic. */
12895 if (len > 0
12896 && PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, len - 1)))
12897 parm_variadic_p = 1;
12899 /* If we don't have enough arguments to satisfy the parameters
12900 (not counting the pack expression at the end), or we have
12901 too many arguments for a parameter list that doesn't end in
12902 a pack expression, we can't unify. */
12903 if (argslen < (len - parm_variadic_p)
12904 || (argslen > len && !parm_variadic_p))
12905 return 1;
12907 /* Unify all of the parameters that precede the (optional)
12908 pack expression. */
12909 for (i = 0; i < len - parm_variadic_p; ++i)
12911 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
12912 TREE_VEC_ELT (packed_args, i), strict))
12913 return 1;
12916 if (parm_variadic_p)
12917 return unify_pack_expansion (tparms, targs,
12918 packed_parms, packed_args,
12919 strict, /*call_args_p=*/false,
12920 /*subr=*/false);
12921 return 0;
12924 break;
12926 case TYPEOF_TYPE:
12927 case DECLTYPE_TYPE:
12928 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
12929 nodes. */
12930 return 0;
12932 default:
12933 gcc_assert (EXPR_P (parm));
12935 /* We must be looking at an expression. This can happen with
12936 something like:
12938 template <int I>
12939 void foo(S<I>, S<I + 2>);
12941 This is a "nondeduced context":
12943 [deduct.type]
12945 The nondeduced contexts are:
12947 --A type that is a template-id in which one or more of
12948 the template-arguments is an expression that references
12949 a template-parameter.
12951 In these cases, we assume deduction succeeded, but don't
12952 actually infer any unifications. */
12954 if (!uses_template_parms (parm)
12955 && !template_args_equal (parm, arg))
12956 return 1;
12957 else
12958 return 0;
12962 /* Note that DECL can be defined in this translation unit, if
12963 required. */
12965 static void
12966 mark_definable (tree decl)
12968 tree clone;
12969 DECL_NOT_REALLY_EXTERN (decl) = 1;
12970 FOR_EACH_CLONE (clone, decl)
12971 DECL_NOT_REALLY_EXTERN (clone) = 1;
12974 /* Called if RESULT is explicitly instantiated, or is a member of an
12975 explicitly instantiated class. */
12977 void
12978 mark_decl_instantiated (tree result, int extern_p)
12980 SET_DECL_EXPLICIT_INSTANTIATION (result);
12982 /* If this entity has already been written out, it's too late to
12983 make any modifications. */
12984 if (TREE_ASM_WRITTEN (result))
12985 return;
12987 if (TREE_CODE (result) != FUNCTION_DECL)
12988 /* The TREE_PUBLIC flag for function declarations will have been
12989 set correctly by tsubst. */
12990 TREE_PUBLIC (result) = 1;
12992 /* This might have been set by an earlier implicit instantiation. */
12993 DECL_COMDAT (result) = 0;
12995 if (extern_p)
12996 DECL_NOT_REALLY_EXTERN (result) = 0;
12997 else
12999 mark_definable (result);
13000 /* Always make artificials weak. */
13001 if (DECL_ARTIFICIAL (result) && flag_weak)
13002 comdat_linkage (result);
13003 /* For WIN32 we also want to put explicit instantiations in
13004 linkonce sections. */
13005 else if (TREE_PUBLIC (result))
13006 maybe_make_one_only (result);
13009 /* If EXTERN_P, then this function will not be emitted -- unless
13010 followed by an explicit instantiation, at which point its linkage
13011 will be adjusted. If !EXTERN_P, then this function will be
13012 emitted here. In neither circumstance do we want
13013 import_export_decl to adjust the linkage. */
13014 DECL_INTERFACE_KNOWN (result) = 1;
13017 /* Given two function templates PAT1 and PAT2, return:
13019 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
13020 -1 if PAT2 is more specialized than PAT1.
13021 0 if neither is more specialized.
13023 LEN indicates the number of parameters we should consider
13024 (defaulted parameters should not be considered).
13026 The 1998 std underspecified function template partial ordering, and
13027 DR214 addresses the issue. We take pairs of arguments, one from
13028 each of the templates, and deduce them against each other. One of
13029 the templates will be more specialized if all the *other*
13030 template's arguments deduce against its arguments and at least one
13031 of its arguments *does* *not* deduce against the other template's
13032 corresponding argument. Deduction is done as for class templates.
13033 The arguments used in deduction have reference and top level cv
13034 qualifiers removed. Iff both arguments were originally reference
13035 types *and* deduction succeeds in both directions, the template
13036 with the more cv-qualified argument wins for that pairing (if
13037 neither is more cv-qualified, they both are equal). Unlike regular
13038 deduction, after all the arguments have been deduced in this way,
13039 we do *not* verify the deduced template argument values can be
13040 substituted into non-deduced contexts, nor do we have to verify
13041 that all template arguments have been deduced. */
13044 more_specialized_fn (tree pat1, tree pat2, int len)
13046 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
13047 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
13048 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
13049 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
13050 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
13051 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
13052 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
13053 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
13054 int better1 = 0;
13055 int better2 = 0;
13057 /* Remove the this parameter from non-static member functions. If
13058 one is a non-static member function and the other is not a static
13059 member function, remove the first parameter from that function
13060 also. This situation occurs for operator functions where we
13061 locate both a member function (with this pointer) and non-member
13062 operator (with explicit first operand). */
13063 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
13065 len--; /* LEN is the number of significant arguments for DECL1 */
13066 args1 = TREE_CHAIN (args1);
13067 if (!DECL_STATIC_FUNCTION_P (decl2))
13068 args2 = TREE_CHAIN (args2);
13070 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
13072 args2 = TREE_CHAIN (args2);
13073 if (!DECL_STATIC_FUNCTION_P (decl1))
13075 len--;
13076 args1 = TREE_CHAIN (args1);
13080 /* If only one is a conversion operator, they are unordered. */
13081 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
13082 return 0;
13084 /* Consider the return type for a conversion function */
13085 if (DECL_CONV_FN_P (decl1))
13087 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
13088 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
13089 len++;
13092 processing_template_decl++;
13094 while (len--)
13096 tree arg1 = TREE_VALUE (args1);
13097 tree arg2 = TREE_VALUE (args2);
13098 int deduce1, deduce2;
13099 int quals1 = -1;
13100 int quals2 = -1;
13102 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
13103 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
13105 /* When both arguments are pack expansions, we need only
13106 unify the patterns themselves. */
13107 arg1 = PACK_EXPANSION_PATTERN (arg1);
13108 arg2 = PACK_EXPANSION_PATTERN (arg2);
13110 /* This is the last comparison we need to do. */
13111 len = 0;
13114 if (TREE_CODE (arg1) == REFERENCE_TYPE)
13116 arg1 = TREE_TYPE (arg1);
13117 quals1 = cp_type_quals (arg1);
13120 if (TREE_CODE (arg2) == REFERENCE_TYPE)
13122 arg2 = TREE_TYPE (arg2);
13123 quals2 = cp_type_quals (arg2);
13126 if ((quals1 < 0) != (quals2 < 0))
13128 /* Only of the args is a reference, see if we should apply
13129 array/function pointer decay to it. This is not part of
13130 DR214, but is, IMHO, consistent with the deduction rules
13131 for the function call itself, and with our earlier
13132 implementation of the underspecified partial ordering
13133 rules. (nathan). */
13134 if (quals1 >= 0)
13136 switch (TREE_CODE (arg1))
13138 case ARRAY_TYPE:
13139 arg1 = TREE_TYPE (arg1);
13140 /* FALLTHROUGH. */
13141 case FUNCTION_TYPE:
13142 arg1 = build_pointer_type (arg1);
13143 break;
13145 default:
13146 break;
13149 else
13151 switch (TREE_CODE (arg2))
13153 case ARRAY_TYPE:
13154 arg2 = TREE_TYPE (arg2);
13155 /* FALLTHROUGH. */
13156 case FUNCTION_TYPE:
13157 arg2 = build_pointer_type (arg2);
13158 break;
13160 default:
13161 break;
13166 arg1 = TYPE_MAIN_VARIANT (arg1);
13167 arg2 = TYPE_MAIN_VARIANT (arg2);
13169 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
13171 int i, len2 = list_length (args2);
13172 tree parmvec = make_tree_vec (1);
13173 tree argvec = make_tree_vec (len2);
13174 tree ta = args2;
13176 /* Setup the parameter vector, which contains only ARG1. */
13177 TREE_VEC_ELT (parmvec, 0) = arg1;
13179 /* Setup the argument vector, which contains the remaining
13180 arguments. */
13181 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
13182 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
13184 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
13185 argvec, UNIFY_ALLOW_NONE,
13186 /*call_args_p=*/false,
13187 /*subr=*/0);
13189 /* We cannot deduce in the other direction, because ARG1 is
13190 a pack expansion but ARG2 is not. */
13191 deduce2 = 0;
13193 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
13195 int i, len1 = list_length (args1);
13196 tree parmvec = make_tree_vec (1);
13197 tree argvec = make_tree_vec (len1);
13198 tree ta = args1;
13200 /* Setup the parameter vector, which contains only ARG1. */
13201 TREE_VEC_ELT (parmvec, 0) = arg2;
13203 /* Setup the argument vector, which contains the remaining
13204 arguments. */
13205 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
13206 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
13208 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
13209 argvec, UNIFY_ALLOW_NONE,
13210 /*call_args_p=*/false,
13211 /*subr=*/0);
13213 /* We cannot deduce in the other direction, because ARG2 is
13214 a pack expansion but ARG1 is not.*/
13215 deduce1 = 0;
13218 else
13220 /* The normal case, where neither argument is a pack
13221 expansion. */
13222 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
13223 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
13226 if (!deduce1)
13227 better2 = -1;
13228 if (!deduce2)
13229 better1 = -1;
13230 if (better1 < 0 && better2 < 0)
13231 /* We've failed to deduce something in either direction.
13232 These must be unordered. */
13233 break;
13235 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
13237 /* Deduces in both directions, see if quals can
13238 disambiguate. Pretend the worse one failed to deduce. */
13239 if ((quals1 & quals2) == quals2)
13240 deduce1 = 0;
13241 if ((quals1 & quals2) == quals1)
13242 deduce2 = 0;
13244 if (deduce1 && !deduce2 && !better2)
13245 better2 = 1;
13246 if (deduce2 && !deduce1 && !better1)
13247 better1 = 1;
13249 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
13250 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
13251 /* We have already processed all of the arguments in our
13252 handing of the pack expansion type. */
13253 len = 0;
13255 args1 = TREE_CHAIN (args1);
13256 args2 = TREE_CHAIN (args2);
13259 processing_template_decl--;
13261 /* All things being equal, if the next argument is a pack expansion
13262 for one function but not for the other, prefer the
13263 non-variadic function. */
13264 if ((better1 > 0) - (better2 > 0) == 0
13265 && args1 && TREE_VALUE (args1)
13266 && args2 && TREE_VALUE (args2))
13268 if (TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION)
13269 return TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION ? 0 : -1;
13270 else if (TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION)
13271 return 1;
13274 return (better1 > 0) - (better2 > 0);
13277 /* Determine which of two partial specializations is more specialized.
13279 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
13280 to the first partial specialization. The TREE_VALUE is the
13281 innermost set of template parameters for the partial
13282 specialization. PAT2 is similar, but for the second template.
13284 Return 1 if the first partial specialization is more specialized;
13285 -1 if the second is more specialized; 0 if neither is more
13286 specialized.
13288 See [temp.class.order] for information about determining which of
13289 two templates is more specialized. */
13291 static int
13292 more_specialized_class (tree pat1, tree pat2)
13294 tree targs;
13295 tree tmpl1, tmpl2;
13296 int winner = 0;
13297 bool any_deductions = false;
13299 tmpl1 = TREE_TYPE (pat1);
13300 tmpl2 = TREE_TYPE (pat2);
13302 /* Just like what happens for functions, if we are ordering between
13303 different class template specializations, we may encounter dependent
13304 types in the arguments, and we need our dependency check functions
13305 to behave correctly. */
13306 ++processing_template_decl;
13307 targs = get_class_bindings (TREE_VALUE (pat1),
13308 CLASSTYPE_TI_ARGS (tmpl1),
13309 CLASSTYPE_TI_ARGS (tmpl2));
13310 if (targs)
13312 --winner;
13313 any_deductions = true;
13316 targs = get_class_bindings (TREE_VALUE (pat2),
13317 CLASSTYPE_TI_ARGS (tmpl2),
13318 CLASSTYPE_TI_ARGS (tmpl1));
13319 if (targs)
13321 ++winner;
13322 any_deductions = true;
13324 --processing_template_decl;
13326 /* In the case of a tie where at least one of the class templates
13327 has a parameter pack at the end, the template with the most
13328 non-packed parameters wins. */
13329 if (winner == 0
13330 && any_deductions
13331 && (template_args_variadic_p (TREE_PURPOSE (pat1))
13332 || template_args_variadic_p (TREE_PURPOSE (pat2))))
13334 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
13335 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
13336 int len1 = TREE_VEC_LENGTH (args1);
13337 int len2 = TREE_VEC_LENGTH (args2);
13339 /* We don't count the pack expansion at the end. */
13340 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
13341 --len1;
13342 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
13343 --len2;
13345 if (len1 > len2)
13346 return 1;
13347 else if (len1 < len2)
13348 return -1;
13351 return winner;
13354 /* Return the template arguments that will produce the function signature
13355 DECL from the function template FN, with the explicit template
13356 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
13357 also match. Return NULL_TREE if no satisfactory arguments could be
13358 found. */
13360 static tree
13361 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
13363 int ntparms = DECL_NTPARMS (fn);
13364 tree targs = make_tree_vec (ntparms);
13365 tree decl_type;
13366 tree decl_arg_types;
13368 /* Substitute the explicit template arguments into the type of DECL.
13369 The call to fn_type_unification will handle substitution into the
13370 FN. */
13371 decl_type = TREE_TYPE (decl);
13372 if (explicit_args && uses_template_parms (decl_type))
13374 tree tmpl;
13375 tree converted_args;
13377 if (DECL_TEMPLATE_INFO (decl))
13378 tmpl = DECL_TI_TEMPLATE (decl);
13379 else
13380 /* We can get here for some invalid specializations. */
13381 return NULL_TREE;
13383 converted_args
13384 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
13385 explicit_args, NULL_TREE,
13386 tf_none,
13387 /*require_all_args=*/false,
13388 /*use_default_args=*/false);
13389 if (converted_args == error_mark_node)
13390 return NULL_TREE;
13392 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
13393 if (decl_type == error_mark_node)
13394 return NULL_TREE;
13397 /* Never do unification on the 'this' parameter. */
13398 decl_arg_types = skip_artificial_parms_for (decl,
13399 TYPE_ARG_TYPES (decl_type));
13401 if (fn_type_unification (fn, explicit_args, targs,
13402 decl_arg_types,
13403 (check_rettype || DECL_CONV_FN_P (fn)
13404 ? TREE_TYPE (decl_type) : NULL_TREE),
13405 DEDUCE_EXACT, LOOKUP_NORMAL))
13406 return NULL_TREE;
13408 return targs;
13411 /* Return the innermost template arguments that, when applied to a
13412 template specialization whose innermost template parameters are
13413 TPARMS, and whose specialization arguments are PARMS, yield the
13414 ARGS.
13416 For example, suppose we have:
13418 template <class T, class U> struct S {};
13419 template <class T> struct S<T*, int> {};
13421 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
13422 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
13423 int}. The resulting vector will be {double}, indicating that `T'
13424 is bound to `double'. */
13426 static tree
13427 get_class_bindings (tree tparms, tree spec_args, tree args)
13429 int i, ntparms = TREE_VEC_LENGTH (tparms);
13430 tree deduced_args;
13431 tree innermost_deduced_args;
13433 innermost_deduced_args = make_tree_vec (ntparms);
13434 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13436 deduced_args = copy_node (args);
13437 SET_TMPL_ARGS_LEVEL (deduced_args,
13438 TMPL_ARGS_DEPTH (deduced_args),
13439 innermost_deduced_args);
13441 else
13442 deduced_args = innermost_deduced_args;
13444 if (unify (tparms, deduced_args,
13445 INNERMOST_TEMPLATE_ARGS (spec_args),
13446 INNERMOST_TEMPLATE_ARGS (args),
13447 UNIFY_ALLOW_NONE))
13448 return NULL_TREE;
13450 for (i = 0; i < ntparms; ++i)
13451 if (! TREE_VEC_ELT (innermost_deduced_args, i))
13452 return NULL_TREE;
13454 /* Verify that nondeduced template arguments agree with the type
13455 obtained from argument deduction.
13457 For example:
13459 struct A { typedef int X; };
13460 template <class T, class U> struct C {};
13461 template <class T> struct C<T, typename T::X> {};
13463 Then with the instantiation `C<A, int>', we can deduce that
13464 `T' is `A' but unify () does not check whether `typename T::X'
13465 is `int'. */
13466 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
13467 if (spec_args == error_mark_node
13468 /* We only need to check the innermost arguments; the other
13469 arguments will always agree. */
13470 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
13471 INNERMOST_TEMPLATE_ARGS (args)))
13472 return NULL_TREE;
13474 return deduced_args;
13477 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
13478 Return the TREE_LIST node with the most specialized template, if
13479 any. If there is no most specialized template, the error_mark_node
13480 is returned.
13482 Note that this function does not look at, or modify, the
13483 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
13484 returned is one of the elements of INSTANTIATIONS, callers may
13485 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
13486 and retrieve it from the value returned. */
13488 tree
13489 most_specialized_instantiation (tree templates)
13491 tree fn, champ;
13493 ++processing_template_decl;
13495 champ = templates;
13496 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
13498 int fate = 0;
13500 if (get_bindings (TREE_VALUE (champ),
13501 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
13502 NULL_TREE, /*check_ret=*/false))
13503 fate--;
13505 if (get_bindings (TREE_VALUE (fn),
13506 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
13507 NULL_TREE, /*check_ret=*/false))
13508 fate++;
13510 if (fate == -1)
13511 champ = fn;
13512 else if (!fate)
13514 /* Equally specialized, move to next function. If there
13515 is no next function, nothing's most specialized. */
13516 fn = TREE_CHAIN (fn);
13517 champ = fn;
13518 if (!fn)
13519 break;
13523 if (champ)
13524 /* Now verify that champ is better than everything earlier in the
13525 instantiation list. */
13526 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
13527 if (get_bindings (TREE_VALUE (champ),
13528 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
13529 NULL_TREE, /*check_ret=*/false)
13530 || !get_bindings (TREE_VALUE (fn),
13531 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
13532 NULL_TREE, /*check_ret=*/false))
13534 champ = NULL_TREE;
13535 break;
13538 processing_template_decl--;
13540 if (!champ)
13541 return error_mark_node;
13543 return champ;
13546 /* If DECL is a specialization of some template, return the most
13547 general such template. Otherwise, returns NULL_TREE.
13549 For example, given:
13551 template <class T> struct S { template <class U> void f(U); };
13553 if TMPL is `template <class U> void S<int>::f(U)' this will return
13554 the full template. This function will not trace past partial
13555 specializations, however. For example, given in addition:
13557 template <class T> struct S<T*> { template <class U> void f(U); };
13559 if TMPL is `template <class U> void S<int*>::f(U)' this will return
13560 `template <class T> template <class U> S<T*>::f(U)'. */
13562 tree
13563 most_general_template (tree decl)
13565 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
13566 an immediate specialization. */
13567 if (TREE_CODE (decl) == FUNCTION_DECL)
13569 if (DECL_TEMPLATE_INFO (decl)) {
13570 decl = DECL_TI_TEMPLATE (decl);
13572 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
13573 template friend. */
13574 if (TREE_CODE (decl) != TEMPLATE_DECL)
13575 return NULL_TREE;
13576 } else
13577 return NULL_TREE;
13580 /* Look for more and more general templates. */
13581 while (DECL_TEMPLATE_INFO (decl))
13583 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
13584 (See cp-tree.h for details.) */
13585 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
13586 break;
13588 if (CLASS_TYPE_P (TREE_TYPE (decl))
13589 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
13590 break;
13592 /* Stop if we run into an explicitly specialized class template. */
13593 if (!DECL_NAMESPACE_SCOPE_P (decl)
13594 && DECL_CONTEXT (decl)
13595 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
13596 break;
13598 decl = DECL_TI_TEMPLATE (decl);
13601 return decl;
13604 /* Return the most specialized of the class template partial
13605 specializations of TMPL which can produce TYPE, a specialization of
13606 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
13607 a _TYPE node corresponding to the partial specialization, while the
13608 TREE_PURPOSE is the set of template arguments that must be
13609 substituted into the TREE_TYPE in order to generate TYPE.
13611 If the choice of partial specialization is ambiguous, a diagnostic
13612 is issued, and the error_mark_node is returned. If there are no
13613 partial specializations of TMPL matching TYPE, then NULL_TREE is
13614 returned. */
13616 static tree
13617 most_specialized_class (tree type, tree tmpl)
13619 tree list = NULL_TREE;
13620 tree t;
13621 tree champ;
13622 int fate;
13623 bool ambiguous_p;
13624 tree args;
13625 tree outer_args = NULL_TREE;
13627 tmpl = most_general_template (tmpl);
13628 args = CLASSTYPE_TI_ARGS (type);
13630 /* For determining which partial specialization to use, only the
13631 innermost args are interesting. */
13632 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13634 outer_args = strip_innermost_template_args (args, 1);
13635 args = INNERMOST_TEMPLATE_ARGS (args);
13638 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
13640 tree partial_spec_args;
13641 tree spec_args;
13642 tree parms = TREE_VALUE (t);
13644 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
13645 if (outer_args)
13647 int i;
13649 ++processing_template_decl;
13651 /* Discard the outer levels of args, and then substitute in the
13652 template args from the enclosing class. */
13653 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
13654 partial_spec_args = tsubst_template_args
13655 (partial_spec_args, outer_args, tf_none, NULL_TREE);
13657 /* PARMS already refers to just the innermost parms, but the
13658 template parms in partial_spec_args had their levels lowered
13659 by tsubst, so we need to do the same for the parm list. We
13660 can't just tsubst the TREE_VEC itself, as tsubst wants to
13661 treat a TREE_VEC as an argument vector. */
13662 parms = copy_node (parms);
13663 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
13664 TREE_VEC_ELT (parms, i) =
13665 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
13667 --processing_template_decl;
13669 spec_args = get_class_bindings (parms,
13670 partial_spec_args,
13671 args);
13672 if (spec_args)
13674 if (outer_args)
13675 spec_args = add_to_template_args (outer_args, spec_args);
13676 list = tree_cons (spec_args, TREE_VALUE (t), list);
13677 TREE_TYPE (list) = TREE_TYPE (t);
13681 if (! list)
13682 return NULL_TREE;
13684 ambiguous_p = false;
13685 t = list;
13686 champ = t;
13687 t = TREE_CHAIN (t);
13688 for (; t; t = TREE_CHAIN (t))
13690 fate = more_specialized_class (champ, t);
13691 if (fate == 1)
13693 else
13695 if (fate == 0)
13697 t = TREE_CHAIN (t);
13698 if (! t)
13700 ambiguous_p = true;
13701 break;
13704 champ = t;
13708 if (!ambiguous_p)
13709 for (t = list; t && t != champ; t = TREE_CHAIN (t))
13711 fate = more_specialized_class (champ, t);
13712 if (fate != 1)
13714 ambiguous_p = true;
13715 break;
13719 if (ambiguous_p)
13721 const char *str = "candidates are:";
13722 error ("ambiguous class template instantiation for %q#T", type);
13723 for (t = list; t; t = TREE_CHAIN (t))
13725 error ("%s %+#T", str, TREE_TYPE (t));
13726 str = " ";
13728 return error_mark_node;
13731 return champ;
13734 /* Explicitly instantiate DECL. */
13736 void
13737 do_decl_instantiation (tree decl, tree storage)
13739 tree result = NULL_TREE;
13740 int extern_p = 0;
13742 if (!decl || decl == error_mark_node)
13743 /* An error occurred, for which grokdeclarator has already issued
13744 an appropriate message. */
13745 return;
13746 else if (! DECL_LANG_SPECIFIC (decl))
13748 error ("explicit instantiation of non-template %q#D", decl);
13749 return;
13751 else if (TREE_CODE (decl) == VAR_DECL)
13753 /* There is an asymmetry here in the way VAR_DECLs and
13754 FUNCTION_DECLs are handled by grokdeclarator. In the case of
13755 the latter, the DECL we get back will be marked as a
13756 template instantiation, and the appropriate
13757 DECL_TEMPLATE_INFO will be set up. This does not happen for
13758 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
13759 should handle VAR_DECLs as it currently handles
13760 FUNCTION_DECLs. */
13761 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
13762 if (!result || TREE_CODE (result) != VAR_DECL)
13764 error ("no matching template for %qD found", decl);
13765 return;
13767 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
13769 error ("type %qT for explicit instantiation %qD does not match "
13770 "declared type %qT", TREE_TYPE (result), decl,
13771 TREE_TYPE (decl));
13772 return;
13775 else if (TREE_CODE (decl) != FUNCTION_DECL)
13777 error ("explicit instantiation of %q#D", decl);
13778 return;
13780 else
13781 result = decl;
13783 /* Check for various error cases. Note that if the explicit
13784 instantiation is valid the RESULT will currently be marked as an
13785 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
13786 until we get here. */
13788 if (DECL_TEMPLATE_SPECIALIZATION (result))
13790 /* DR 259 [temp.spec].
13792 Both an explicit instantiation and a declaration of an explicit
13793 specialization shall not appear in a program unless the explicit
13794 instantiation follows a declaration of the explicit specialization.
13796 For a given set of template parameters, if an explicit
13797 instantiation of a template appears after a declaration of an
13798 explicit specialization for that template, the explicit
13799 instantiation has no effect. */
13800 return;
13802 else if (DECL_EXPLICIT_INSTANTIATION (result))
13804 /* [temp.spec]
13806 No program shall explicitly instantiate any template more
13807 than once.
13809 We check DECL_NOT_REALLY_EXTERN so as not to complain when
13810 the first instantiation was `extern' and the second is not,
13811 and EXTERN_P for the opposite case. */
13812 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
13813 pedwarn ("duplicate explicit instantiation of %q#D", result);
13814 /* If an "extern" explicit instantiation follows an ordinary
13815 explicit instantiation, the template is instantiated. */
13816 if (extern_p)
13817 return;
13819 else if (!DECL_IMPLICIT_INSTANTIATION (result))
13821 error ("no matching template for %qD found", result);
13822 return;
13824 else if (!DECL_TEMPLATE_INFO (result))
13826 pedwarn ("explicit instantiation of non-template %q#D", result);
13827 return;
13830 if (storage == NULL_TREE)
13832 else if (storage == ridpointers[(int) RID_EXTERN])
13834 if (pedantic && !in_system_header)
13835 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
13836 "instantiations");
13837 extern_p = 1;
13839 else
13840 error ("storage class %qD applied to template instantiation", storage);
13842 check_explicit_instantiation_namespace (result);
13843 mark_decl_instantiated (result, extern_p);
13844 if (! extern_p)
13845 instantiate_decl (result, /*defer_ok=*/1,
13846 /*expl_inst_class_mem_p=*/false);
13849 static void
13850 mark_class_instantiated (tree t, int extern_p)
13852 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
13853 SET_CLASSTYPE_INTERFACE_KNOWN (t);
13854 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
13855 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
13856 if (! extern_p)
13858 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
13859 rest_of_type_compilation (t, 1);
13863 /* Called from do_type_instantiation through binding_table_foreach to
13864 do recursive instantiation for the type bound in ENTRY. */
13865 static void
13866 bt_instantiate_type_proc (binding_entry entry, void *data)
13868 tree storage = *(tree *) data;
13870 if (IS_AGGR_TYPE (entry->type)
13871 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
13872 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
13875 /* Called from do_type_instantiation to instantiate a member
13876 (a member function or a static member variable) of an
13877 explicitly instantiated class template. */
13878 static void
13879 instantiate_class_member (tree decl, int extern_p)
13881 mark_decl_instantiated (decl, extern_p);
13882 if (! extern_p)
13883 instantiate_decl (decl, /*defer_ok=*/1,
13884 /*expl_inst_class_mem_p=*/true);
13887 /* Perform an explicit instantiation of template class T. STORAGE, if
13888 non-null, is the RID for extern, inline or static. COMPLAIN is
13889 nonzero if this is called from the parser, zero if called recursively,
13890 since the standard is unclear (as detailed below). */
13892 void
13893 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
13895 int extern_p = 0;
13896 int nomem_p = 0;
13897 int static_p = 0;
13898 int previous_instantiation_extern_p = 0;
13900 if (TREE_CODE (t) == TYPE_DECL)
13901 t = TREE_TYPE (t);
13903 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
13905 error ("explicit instantiation of non-template type %qT", t);
13906 return;
13909 complete_type (t);
13911 if (!COMPLETE_TYPE_P (t))
13913 if (complain & tf_error)
13914 error ("explicit instantiation of %q#T before definition of template",
13916 return;
13919 if (storage != NULL_TREE)
13921 if (pedantic && !in_system_header)
13922 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
13923 storage);
13925 if (storage == ridpointers[(int) RID_INLINE])
13926 nomem_p = 1;
13927 else if (storage == ridpointers[(int) RID_EXTERN])
13928 extern_p = 1;
13929 else if (storage == ridpointers[(int) RID_STATIC])
13930 static_p = 1;
13931 else
13933 error ("storage class %qD applied to template instantiation",
13934 storage);
13935 extern_p = 0;
13939 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
13941 /* DR 259 [temp.spec].
13943 Both an explicit instantiation and a declaration of an explicit
13944 specialization shall not appear in a program unless the explicit
13945 instantiation follows a declaration of the explicit specialization.
13947 For a given set of template parameters, if an explicit
13948 instantiation of a template appears after a declaration of an
13949 explicit specialization for that template, the explicit
13950 instantiation has no effect. */
13951 return;
13953 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
13955 /* [temp.spec]
13957 No program shall explicitly instantiate any template more
13958 than once.
13960 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
13961 instantiation was `extern'. If EXTERN_P then the second is.
13962 These cases are OK. */
13963 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
13965 if (!previous_instantiation_extern_p && !extern_p
13966 && (complain & tf_error))
13967 pedwarn ("duplicate explicit instantiation of %q#T", t);
13969 /* If we've already instantiated the template, just return now. */
13970 if (!CLASSTYPE_INTERFACE_ONLY (t))
13971 return;
13974 check_explicit_instantiation_namespace (TYPE_NAME (t));
13975 mark_class_instantiated (t, extern_p);
13977 if (nomem_p)
13978 return;
13981 tree tmp;
13983 /* In contrast to implicit instantiation, where only the
13984 declarations, and not the definitions, of members are
13985 instantiated, we have here:
13987 [temp.explicit]
13989 The explicit instantiation of a class template specialization
13990 implies the instantiation of all of its members not
13991 previously explicitly specialized in the translation unit
13992 containing the explicit instantiation.
13994 Of course, we can't instantiate member template classes, since
13995 we don't have any arguments for them. Note that the standard
13996 is unclear on whether the instantiation of the members are
13997 *explicit* instantiations or not. However, the most natural
13998 interpretation is that it should be an explicit instantiation. */
14000 if (! static_p)
14001 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
14002 if (TREE_CODE (tmp) == FUNCTION_DECL
14003 && DECL_TEMPLATE_INSTANTIATION (tmp))
14004 instantiate_class_member (tmp, extern_p);
14006 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
14007 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
14008 instantiate_class_member (tmp, extern_p);
14010 if (CLASSTYPE_NESTED_UTDS (t))
14011 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
14012 bt_instantiate_type_proc, &storage);
14016 /* Given a function DECL, which is a specialization of TMPL, modify
14017 DECL to be a re-instantiation of TMPL with the same template
14018 arguments. TMPL should be the template into which tsubst'ing
14019 should occur for DECL, not the most general template.
14021 One reason for doing this is a scenario like this:
14023 template <class T>
14024 void f(const T&, int i);
14026 void g() { f(3, 7); }
14028 template <class T>
14029 void f(const T& t, const int i) { }
14031 Note that when the template is first instantiated, with
14032 instantiate_template, the resulting DECL will have no name for the
14033 first parameter, and the wrong type for the second. So, when we go
14034 to instantiate the DECL, we regenerate it. */
14036 static void
14037 regenerate_decl_from_template (tree decl, tree tmpl)
14039 /* The arguments used to instantiate DECL, from the most general
14040 template. */
14041 tree args;
14042 tree code_pattern;
14044 args = DECL_TI_ARGS (decl);
14045 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
14047 /* Make sure that we can see identifiers, and compute access
14048 correctly. */
14049 push_access_scope (decl);
14051 if (TREE_CODE (decl) == FUNCTION_DECL)
14053 tree decl_parm;
14054 tree pattern_parm;
14055 tree specs;
14056 int args_depth;
14057 int parms_depth;
14059 args_depth = TMPL_ARGS_DEPTH (args);
14060 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
14061 if (args_depth > parms_depth)
14062 args = get_innermost_template_args (args, parms_depth);
14064 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
14065 args, tf_error, NULL_TREE);
14066 if (specs)
14067 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
14068 specs);
14070 /* Merge parameter declarations. */
14071 decl_parm = skip_artificial_parms_for (decl,
14072 DECL_ARGUMENTS (decl));
14073 pattern_parm
14074 = skip_artificial_parms_for (code_pattern,
14075 DECL_ARGUMENTS (code_pattern));
14076 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
14078 tree parm_type;
14079 tree attributes;
14081 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
14082 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
14083 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
14084 NULL_TREE);
14085 parm_type = type_decays_to (parm_type);
14086 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
14087 TREE_TYPE (decl_parm) = parm_type;
14088 attributes = DECL_ATTRIBUTES (pattern_parm);
14089 if (DECL_ATTRIBUTES (decl_parm) != attributes)
14091 DECL_ATTRIBUTES (decl_parm) = attributes;
14092 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
14094 decl_parm = TREE_CHAIN (decl_parm);
14095 pattern_parm = TREE_CHAIN (pattern_parm);
14097 /* Merge any parameters that match with the function parameter
14098 pack. */
14099 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
14101 int i, len;
14102 tree expanded_types;
14103 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14104 the parameters in this function parameter pack. */
14105 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
14106 args, tf_error, NULL_TREE);
14107 len = TREE_VEC_LENGTH (expanded_types);
14108 for (i = 0; i < len; i++)
14110 tree parm_type;
14111 tree attributes;
14113 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
14114 /* Rename the parameter to include the index. */
14115 DECL_NAME (decl_parm) =
14116 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
14117 parm_type = TREE_VEC_ELT (expanded_types, i);
14118 parm_type = type_decays_to (parm_type);
14119 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
14120 TREE_TYPE (decl_parm) = parm_type;
14121 attributes = DECL_ATTRIBUTES (pattern_parm);
14122 if (DECL_ATTRIBUTES (decl_parm) != attributes)
14124 DECL_ATTRIBUTES (decl_parm) = attributes;
14125 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
14127 decl_parm = TREE_CHAIN (decl_parm);
14130 /* Merge additional specifiers from the CODE_PATTERN. */
14131 if (DECL_DECLARED_INLINE_P (code_pattern)
14132 && !DECL_DECLARED_INLINE_P (decl))
14133 DECL_DECLARED_INLINE_P (decl) = 1;
14134 if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
14135 DECL_INLINE (decl) = 1;
14137 else if (TREE_CODE (decl) == VAR_DECL)
14138 DECL_INITIAL (decl) =
14139 tsubst_expr (DECL_INITIAL (code_pattern), args,
14140 tf_error, DECL_TI_TEMPLATE (decl),
14141 /*integral_constant_expression_p=*/false);
14142 else
14143 gcc_unreachable ();
14145 pop_access_scope (decl);
14148 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
14149 substituted to get DECL. */
14151 tree
14152 template_for_substitution (tree decl)
14154 tree tmpl = DECL_TI_TEMPLATE (decl);
14156 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
14157 for the instantiation. This is not always the most general
14158 template. Consider, for example:
14160 template <class T>
14161 struct S { template <class U> void f();
14162 template <> void f<int>(); };
14164 and an instantiation of S<double>::f<int>. We want TD to be the
14165 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
14166 while (/* An instantiation cannot have a definition, so we need a
14167 more general template. */
14168 DECL_TEMPLATE_INSTANTIATION (tmpl)
14169 /* We must also deal with friend templates. Given:
14171 template <class T> struct S {
14172 template <class U> friend void f() {};
14175 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
14176 so far as the language is concerned, but that's still
14177 where we get the pattern for the instantiation from. On
14178 other hand, if the definition comes outside the class, say:
14180 template <class T> struct S {
14181 template <class U> friend void f();
14183 template <class U> friend void f() {}
14185 we don't need to look any further. That's what the check for
14186 DECL_INITIAL is for. */
14187 || (TREE_CODE (decl) == FUNCTION_DECL
14188 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
14189 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
14191 /* The present template, TD, should not be a definition. If it
14192 were a definition, we should be using it! Note that we
14193 cannot restructure the loop to just keep going until we find
14194 a template with a definition, since that might go too far if
14195 a specialization was declared, but not defined. */
14196 gcc_assert (TREE_CODE (decl) != VAR_DECL
14197 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
14199 /* Fetch the more general template. */
14200 tmpl = DECL_TI_TEMPLATE (tmpl);
14203 return tmpl;
14206 /* Produce the definition of D, a _DECL generated from a template. If
14207 DEFER_OK is nonzero, then we don't have to actually do the
14208 instantiation now; we just have to do it sometime. Normally it is
14209 an error if this is an explicit instantiation but D is undefined.
14210 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
14211 explicitly instantiated class template. */
14213 tree
14214 instantiate_decl (tree d, int defer_ok,
14215 bool expl_inst_class_mem_p)
14217 tree tmpl = DECL_TI_TEMPLATE (d);
14218 tree gen_args;
14219 tree args;
14220 tree td;
14221 tree code_pattern;
14222 tree spec;
14223 tree gen_tmpl;
14224 bool pattern_defined;
14225 int need_push;
14226 location_t saved_loc = input_location;
14227 int saved_in_system_header = in_system_header;
14228 bool external_p;
14230 /* This function should only be used to instantiate templates for
14231 functions and static member variables. */
14232 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
14233 || TREE_CODE (d) == VAR_DECL);
14235 /* Variables are never deferred; if instantiation is required, they
14236 are instantiated right away. That allows for better code in the
14237 case that an expression refers to the value of the variable --
14238 if the variable has a constant value the referring expression can
14239 take advantage of that fact. */
14240 if (TREE_CODE (d) == VAR_DECL)
14241 defer_ok = 0;
14243 /* Don't instantiate cloned functions. Instead, instantiate the
14244 functions they cloned. */
14245 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
14246 d = DECL_CLONED_FUNCTION (d);
14248 if (DECL_TEMPLATE_INSTANTIATED (d))
14249 /* D has already been instantiated. It might seem reasonable to
14250 check whether or not D is an explicit instantiation, and, if so,
14251 stop here. But when an explicit instantiation is deferred
14252 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
14253 is set, even though we still need to do the instantiation. */
14254 return d;
14256 /* If we already have a specialization of this declaration, then
14257 there's no reason to instantiate it. Note that
14258 retrieve_specialization gives us both instantiations and
14259 specializations, so we must explicitly check
14260 DECL_TEMPLATE_SPECIALIZATION. */
14261 gen_tmpl = most_general_template (tmpl);
14262 gen_args = DECL_TI_ARGS (d);
14263 spec = retrieve_specialization (gen_tmpl, gen_args,
14264 /*class_specializations_p=*/false);
14265 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
14266 return spec;
14268 /* This needs to happen before any tsubsting. */
14269 if (! push_tinst_level (d))
14270 return d;
14272 timevar_push (TV_PARSE);
14274 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
14275 for the instantiation. */
14276 td = template_for_substitution (d);
14277 code_pattern = DECL_TEMPLATE_RESULT (td);
14279 /* We should never be trying to instantiate a member of a class
14280 template or partial specialization. */
14281 gcc_assert (d != code_pattern);
14283 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
14284 || DECL_TEMPLATE_SPECIALIZATION (td))
14285 /* In the case of a friend template whose definition is provided
14286 outside the class, we may have too many arguments. Drop the
14287 ones we don't need. The same is true for specializations. */
14288 args = get_innermost_template_args
14289 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
14290 else
14291 args = gen_args;
14293 if (TREE_CODE (d) == FUNCTION_DECL)
14294 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
14295 else
14296 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
14298 /* We may be in the middle of deferred access check. Disable it now. */
14299 push_deferring_access_checks (dk_no_deferred);
14301 /* Unless an explicit instantiation directive has already determined
14302 the linkage of D, remember that a definition is available for
14303 this entity. */
14304 if (pattern_defined
14305 && !DECL_INTERFACE_KNOWN (d)
14306 && !DECL_NOT_REALLY_EXTERN (d))
14307 mark_definable (d);
14309 input_location = DECL_SOURCE_LOCATION (d);
14310 in_system_header = DECL_IN_SYSTEM_HEADER (d);
14312 /* If D is a member of an explicitly instantiated class template,
14313 and no definition is available, treat it like an implicit
14314 instantiation. */
14315 if (!pattern_defined && expl_inst_class_mem_p
14316 && DECL_EXPLICIT_INSTANTIATION (d))
14318 DECL_NOT_REALLY_EXTERN (d) = 0;
14319 DECL_INTERFACE_KNOWN (d) = 0;
14320 SET_DECL_IMPLICIT_INSTANTIATION (d);
14323 if (!defer_ok)
14325 /* Recheck the substitutions to obtain any warning messages
14326 about ignoring cv qualifiers. */
14327 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
14328 tree type = TREE_TYPE (gen);
14330 /* Make sure that we can see identifiers, and compute access
14331 correctly. D is already the target FUNCTION_DECL with the
14332 right context. */
14333 push_access_scope (d);
14335 if (TREE_CODE (gen) == FUNCTION_DECL)
14337 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
14338 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
14340 /* Don't simply tsubst the function type, as that will give
14341 duplicate warnings about poor parameter qualifications.
14342 The function arguments are the same as the decl_arguments
14343 without the top level cv qualifiers. */
14344 type = TREE_TYPE (type);
14346 tsubst (type, gen_args, tf_warning_or_error, d);
14348 pop_access_scope (d);
14351 /* Check to see whether we know that this template will be
14352 instantiated in some other file, as with "extern template"
14353 extension. */
14354 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
14355 /* In general, we do not instantiate such templates... */
14356 if (external_p
14357 /* ... but we instantiate inline functions so that we can inline
14358 them and ... */
14359 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))
14360 /* ... we instantiate static data members whose values are
14361 needed in integral constant expressions. */
14362 && ! (TREE_CODE (d) == VAR_DECL
14363 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
14364 goto out;
14365 /* Defer all other templates, unless we have been explicitly
14366 forbidden from doing so. */
14367 if (/* If there is no definition, we cannot instantiate the
14368 template. */
14369 ! pattern_defined
14370 /* If it's OK to postpone instantiation, do so. */
14371 || defer_ok
14372 /* If this is a static data member that will be defined
14373 elsewhere, we don't want to instantiate the entire data
14374 member, but we do want to instantiate the initializer so that
14375 we can substitute that elsewhere. */
14376 || (external_p && TREE_CODE (d) == VAR_DECL))
14378 /* The definition of the static data member is now required so
14379 we must substitute the initializer. */
14380 if (TREE_CODE (d) == VAR_DECL
14381 && !DECL_INITIAL (d)
14382 && DECL_INITIAL (code_pattern))
14384 tree ns;
14385 tree init;
14387 ns = decl_namespace_context (d);
14388 push_nested_namespace (ns);
14389 push_nested_class (DECL_CONTEXT (d));
14390 init = tsubst_expr (DECL_INITIAL (code_pattern),
14391 args,
14392 tf_warning_or_error, NULL_TREE,
14393 /*integral_constant_expression_p=*/false);
14394 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
14395 /*asmspec_tree=*/NULL_TREE,
14396 LOOKUP_ONLYCONVERTING);
14397 pop_nested_class ();
14398 pop_nested_namespace (ns);
14401 /* We restore the source position here because it's used by
14402 add_pending_template. */
14403 input_location = saved_loc;
14405 if (at_eof && !pattern_defined
14406 && DECL_EXPLICIT_INSTANTIATION (d))
14407 /* [temp.explicit]
14409 The definition of a non-exported function template, a
14410 non-exported member function template, or a non-exported
14411 member function or static data member of a class template
14412 shall be present in every translation unit in which it is
14413 explicitly instantiated. */
14414 pedwarn
14415 ("explicit instantiation of %qD but no definition available", d);
14417 /* ??? Historically, we have instantiated inline functions, even
14418 when marked as "extern template". */
14419 if (!(external_p && TREE_CODE (d) == VAR_DECL))
14420 add_pending_template (d);
14421 goto out;
14423 /* Tell the repository that D is available in this translation unit
14424 -- and see if it is supposed to be instantiated here. */
14425 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
14427 /* In a PCH file, despite the fact that the repository hasn't
14428 requested instantiation in the PCH it is still possible that
14429 an instantiation will be required in a file that includes the
14430 PCH. */
14431 if (pch_file)
14432 add_pending_template (d);
14433 /* Instantiate inline functions so that the inliner can do its
14434 job, even though we'll not be emitting a copy of this
14435 function. */
14436 if (!(TREE_CODE (d) == FUNCTION_DECL
14437 && flag_inline_trees
14438 && DECL_DECLARED_INLINE_P (d)))
14439 goto out;
14442 need_push = !cfun || !global_bindings_p ();
14443 if (need_push)
14444 push_to_top_level ();
14446 /* Mark D as instantiated so that recursive calls to
14447 instantiate_decl do not try to instantiate it again. */
14448 DECL_TEMPLATE_INSTANTIATED (d) = 1;
14450 /* Regenerate the declaration in case the template has been modified
14451 by a subsequent redeclaration. */
14452 regenerate_decl_from_template (d, td);
14454 /* We already set the file and line above. Reset them now in case
14455 they changed as a result of calling regenerate_decl_from_template. */
14456 input_location = DECL_SOURCE_LOCATION (d);
14458 if (TREE_CODE (d) == VAR_DECL)
14460 tree init;
14462 /* Clear out DECL_RTL; whatever was there before may not be right
14463 since we've reset the type of the declaration. */
14464 SET_DECL_RTL (d, NULL_RTX);
14465 DECL_IN_AGGR_P (d) = 0;
14467 /* The initializer is placed in DECL_INITIAL by
14468 regenerate_decl_from_template. Pull it out so that
14469 finish_decl can process it. */
14470 init = DECL_INITIAL (d);
14471 DECL_INITIAL (d) = NULL_TREE;
14472 DECL_INITIALIZED_P (d) = 0;
14474 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
14475 initializer. That function will defer actual emission until
14476 we have a chance to determine linkage. */
14477 DECL_EXTERNAL (d) = 0;
14479 /* Enter the scope of D so that access-checking works correctly. */
14480 push_nested_class (DECL_CONTEXT (d));
14481 finish_decl (d, init, NULL_TREE);
14482 pop_nested_class ();
14484 else if (TREE_CODE (d) == FUNCTION_DECL)
14486 htab_t saved_local_specializations;
14487 tree subst_decl;
14488 tree tmpl_parm;
14489 tree spec_parm;
14491 /* Save away the current list, in case we are instantiating one
14492 template from within the body of another. */
14493 saved_local_specializations = local_specializations;
14495 /* Set up the list of local specializations. */
14496 local_specializations = htab_create (37,
14497 hash_local_specialization,
14498 eq_local_specializations,
14499 NULL);
14501 /* Set up context. */
14502 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
14504 /* Create substitution entries for the parameters. */
14505 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
14506 tmpl_parm = DECL_ARGUMENTS (subst_decl);
14507 spec_parm = DECL_ARGUMENTS (d);
14508 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
14510 register_local_specialization (spec_parm, tmpl_parm);
14511 spec_parm = skip_artificial_parms_for (d, spec_parm);
14512 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
14514 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
14516 register_local_specialization (spec_parm, tmpl_parm);
14517 tmpl_parm = TREE_CHAIN (tmpl_parm);
14518 spec_parm = TREE_CHAIN (spec_parm);
14520 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
14522 /* Collect all of the extra "packed" parameters into an
14523 argument pack. */
14524 tree parmvec;
14525 tree parmtypevec;
14526 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
14527 tree argtypepack = make_node (TYPE_ARGUMENT_PACK);
14528 int i, len = 0;
14529 tree t;
14531 /* Count how many parameters remain. */
14532 for (t = spec_parm; t; t = TREE_CHAIN (t))
14533 len++;
14535 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
14536 parmvec = make_tree_vec (len);
14537 parmtypevec = make_tree_vec (len);
14538 for(i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
14540 TREE_VEC_ELT (parmvec, i) = spec_parm;
14541 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
14544 /* Build the argument packs. */
14545 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
14546 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
14547 TREE_TYPE (argpack) = argtypepack;
14549 /* Register the (value) argument pack as a specialization of
14550 TMPL_PARM, then move on. */
14551 register_local_specialization (argpack, tmpl_parm);
14552 tmpl_parm = TREE_CHAIN (tmpl_parm);
14554 gcc_assert (!spec_parm);
14556 /* Substitute into the body of the function. */
14557 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
14558 tf_warning_or_error, tmpl,
14559 /*integral_constant_expression_p=*/false);
14561 /* Set the current input_location to the end of the function
14562 so that finish_function knows where we are. */
14563 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
14565 /* We don't need the local specializations any more. */
14566 htab_delete (local_specializations);
14567 local_specializations = saved_local_specializations;
14569 /* Finish the function. */
14570 d = finish_function (0);
14571 expand_or_defer_fn (d);
14574 /* We're not deferring instantiation any more. */
14575 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
14577 if (need_push)
14578 pop_from_top_level ();
14580 out:
14581 input_location = saved_loc;
14582 in_system_header = saved_in_system_header;
14583 pop_deferring_access_checks ();
14584 pop_tinst_level ();
14586 timevar_pop (TV_PARSE);
14588 return d;
14591 /* Run through the list of templates that we wish we could
14592 instantiate, and instantiate any we can. RETRIES is the
14593 number of times we retry pending template instantiation. */
14595 void
14596 instantiate_pending_templates (int retries)
14598 int reconsider;
14599 location_t saved_loc = input_location;
14600 int saved_in_system_header = in_system_header;
14602 /* Instantiating templates may trigger vtable generation. This in turn
14603 may require further template instantiations. We place a limit here
14604 to avoid infinite loop. */
14605 if (pending_templates && retries >= max_tinst_depth)
14607 tree decl = pending_templates->tinst->decl;
14609 error ("template instantiation depth exceeds maximum of %d"
14610 " instantiating %q+D, possibly from virtual table generation"
14611 " (use -ftemplate-depth-NN to increase the maximum)",
14612 max_tinst_depth, decl);
14613 if (TREE_CODE (decl) == FUNCTION_DECL)
14614 /* Pretend that we defined it. */
14615 DECL_INITIAL (decl) = error_mark_node;
14616 return;
14621 struct pending_template **t = &pending_templates;
14622 struct pending_template *last = NULL;
14623 reconsider = 0;
14624 while (*t)
14626 tree instantiation = reopen_tinst_level ((*t)->tinst);
14627 bool complete = false;
14629 if (TYPE_P (instantiation))
14631 tree fn;
14633 if (!COMPLETE_TYPE_P (instantiation))
14635 instantiate_class_template (instantiation);
14636 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
14637 for (fn = TYPE_METHODS (instantiation);
14639 fn = TREE_CHAIN (fn))
14640 if (! DECL_ARTIFICIAL (fn))
14641 instantiate_decl (fn,
14642 /*defer_ok=*/0,
14643 /*expl_inst_class_mem_p=*/false);
14644 if (COMPLETE_TYPE_P (instantiation))
14645 reconsider = 1;
14648 complete = COMPLETE_TYPE_P (instantiation);
14650 else
14652 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
14653 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
14655 instantiation
14656 = instantiate_decl (instantiation,
14657 /*defer_ok=*/0,
14658 /*expl_inst_class_mem_p=*/false);
14659 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
14660 reconsider = 1;
14663 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
14664 || DECL_TEMPLATE_INSTANTIATED (instantiation));
14667 if (complete)
14668 /* If INSTANTIATION has been instantiated, then we don't
14669 need to consider it again in the future. */
14670 *t = (*t)->next;
14671 else
14673 last = *t;
14674 t = &(*t)->next;
14676 tinst_depth = 0;
14677 current_tinst_level = NULL;
14679 last_pending_template = last;
14681 while (reconsider);
14683 input_location = saved_loc;
14684 in_system_header = saved_in_system_header;
14687 /* Substitute ARGVEC into T, which is a list of initializers for
14688 either base class or a non-static data member. The TREE_PURPOSEs
14689 are DECLs, and the TREE_VALUEs are the initializer values. Used by
14690 instantiate_decl. */
14692 static tree
14693 tsubst_initializer_list (tree t, tree argvec)
14695 tree inits = NULL_TREE;
14697 for (; t; t = TREE_CHAIN (t))
14699 tree decl;
14700 tree init;
14701 tree expanded_bases = NULL_TREE;
14702 tree expanded_arguments = NULL_TREE;
14703 int i, len = 1;
14705 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
14707 tree expr;
14708 tree arg;
14710 /* Expand the base class expansion type into separate base
14711 classes. */
14712 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
14713 tf_warning_or_error,
14714 NULL_TREE);
14715 if (expanded_bases == error_mark_node)
14716 continue;
14718 /* We'll be building separate TREE_LISTs of arguments for
14719 each base. */
14720 len = TREE_VEC_LENGTH (expanded_bases);
14721 expanded_arguments = make_tree_vec (len);
14722 for (i = 0; i < len; i++)
14723 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
14725 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
14726 expand each argument in the TREE_VALUE of t. */
14727 expr = make_node (EXPR_PACK_EXPANSION);
14728 PACK_EXPANSION_PARAMETER_PACKS (expr) =
14729 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
14731 /* Substitute parameter packs into each argument in the
14732 TREE_LIST. */
14733 in_base_initializer = 1;
14734 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
14736 tree expanded_exprs;
14738 /* Expand the argument. */
14739 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
14740 expanded_exprs = tsubst_pack_expansion (expr, argvec,
14741 tf_warning_or_error,
14742 NULL_TREE);
14744 /* Prepend each of the expanded expressions to the
14745 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
14746 for (i = 0; i < len; i++)
14748 TREE_VEC_ELT (expanded_arguments, i) =
14749 tree_cons (NULL_TREE, TREE_VEC_ELT (expanded_exprs, i),
14750 TREE_VEC_ELT (expanded_arguments, i));
14753 in_base_initializer = 0;
14755 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
14756 since we built them backwards. */
14757 for (i = 0; i < len; i++)
14759 TREE_VEC_ELT (expanded_arguments, i) =
14760 nreverse (TREE_VEC_ELT (expanded_arguments, i));
14764 for (i = 0; i < len; ++i)
14766 if (expanded_bases)
14768 decl = TREE_VEC_ELT (expanded_bases, i);
14769 decl = expand_member_init (decl);
14770 init = TREE_VEC_ELT (expanded_arguments, i);
14772 else
14774 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
14775 tf_warning_or_error, NULL_TREE);
14777 decl = expand_member_init (decl);
14778 if (decl && !DECL_P (decl))
14779 in_base_initializer = 1;
14781 init = tsubst_expr (TREE_VALUE (t), argvec,
14782 tf_warning_or_error, NULL_TREE,
14783 /*integral_constant_expression_p=*/false);
14784 in_base_initializer = 0;
14787 if (decl)
14789 init = build_tree_list (decl, init);
14790 TREE_CHAIN (init) = inits;
14791 inits = init;
14795 return inits;
14798 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
14800 static void
14801 set_current_access_from_decl (tree decl)
14803 if (TREE_PRIVATE (decl))
14804 current_access_specifier = access_private_node;
14805 else if (TREE_PROTECTED (decl))
14806 current_access_specifier = access_protected_node;
14807 else
14808 current_access_specifier = access_public_node;
14811 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
14812 is the instantiation (which should have been created with
14813 start_enum) and ARGS are the template arguments to use. */
14815 static void
14816 tsubst_enum (tree tag, tree newtag, tree args)
14818 tree e;
14820 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
14822 tree value;
14823 tree decl;
14825 decl = TREE_VALUE (e);
14826 /* Note that in a template enum, the TREE_VALUE is the
14827 CONST_DECL, not the corresponding INTEGER_CST. */
14828 value = tsubst_expr (DECL_INITIAL (decl),
14829 args, tf_warning_or_error, NULL_TREE,
14830 /*integral_constant_expression_p=*/true);
14832 /* Give this enumeration constant the correct access. */
14833 set_current_access_from_decl (decl);
14835 /* Actually build the enumerator itself. */
14836 build_enumerator (DECL_NAME (decl), value, newtag);
14839 finish_enum (newtag);
14840 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
14841 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
14844 /* DECL is a FUNCTION_DECL that is a template specialization. Return
14845 its type -- but without substituting the innermost set of template
14846 arguments. So, innermost set of template parameters will appear in
14847 the type. */
14849 tree
14850 get_mostly_instantiated_function_type (tree decl)
14852 tree fn_type;
14853 tree tmpl;
14854 tree targs;
14855 tree tparms;
14856 int parm_depth;
14858 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14859 targs = DECL_TI_ARGS (decl);
14860 tparms = DECL_TEMPLATE_PARMS (tmpl);
14861 parm_depth = TMPL_PARMS_DEPTH (tparms);
14863 /* There should be as many levels of arguments as there are levels
14864 of parameters. */
14865 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
14867 fn_type = TREE_TYPE (tmpl);
14869 if (parm_depth == 1)
14870 /* No substitution is necessary. */
14872 else
14874 int i, save_access_control;
14875 tree partial_args;
14877 /* Replace the innermost level of the TARGS with NULL_TREEs to
14878 let tsubst know not to substitute for those parameters. */
14879 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
14880 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
14881 SET_TMPL_ARGS_LEVEL (partial_args, i,
14882 TMPL_ARGS_LEVEL (targs, i));
14883 SET_TMPL_ARGS_LEVEL (partial_args,
14884 TMPL_ARGS_DEPTH (targs),
14885 make_tree_vec (DECL_NTPARMS (tmpl)));
14887 /* Disable access control as this function is used only during
14888 name-mangling. */
14889 save_access_control = flag_access_control;
14890 flag_access_control = 0;
14892 ++processing_template_decl;
14893 /* Now, do the (partial) substitution to figure out the
14894 appropriate function type. */
14895 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
14896 --processing_template_decl;
14898 /* Substitute into the template parameters to obtain the real
14899 innermost set of parameters. This step is important if the
14900 innermost set of template parameters contains value
14901 parameters whose types depend on outer template parameters. */
14902 TREE_VEC_LENGTH (partial_args)--;
14903 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
14905 flag_access_control = save_access_control;
14908 return fn_type;
14911 /* Return truthvalue if we're processing a template different from
14912 the last one involved in diagnostics. */
14914 problematic_instantiation_changed (void)
14916 return last_template_error_tick != tinst_level_tick;
14919 /* Remember current template involved in diagnostics. */
14920 void
14921 record_last_problematic_instantiation (void)
14923 last_template_error_tick = tinst_level_tick;
14926 struct tinst_level *
14927 current_instantiation (void)
14929 return current_tinst_level;
14932 /* [temp.param] Check that template non-type parm TYPE is of an allowable
14933 type. Return zero for ok, nonzero for disallowed. Issue error and
14934 warning messages under control of COMPLAIN. */
14936 static int
14937 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
14939 if (INTEGRAL_TYPE_P (type))
14940 return 0;
14941 else if (POINTER_TYPE_P (type))
14942 return 0;
14943 else if (TYPE_PTR_TO_MEMBER_P (type))
14944 return 0;
14945 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
14946 return 0;
14947 else if (TREE_CODE (type) == TYPENAME_TYPE)
14948 return 0;
14950 if (complain & tf_error)
14951 error ("%q#T is not a valid type for a template constant parameter", type);
14952 return 1;
14955 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
14956 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
14958 static bool
14959 dependent_type_p_r (tree type)
14961 tree scope;
14963 /* [temp.dep.type]
14965 A type is dependent if it is:
14967 -- a template parameter. Template template parameters are types
14968 for us (since TYPE_P holds true for them) so we handle
14969 them here. */
14970 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
14971 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
14972 return true;
14973 /* -- a qualified-id with a nested-name-specifier which contains a
14974 class-name that names a dependent type or whose unqualified-id
14975 names a dependent type. */
14976 if (TREE_CODE (type) == TYPENAME_TYPE)
14977 return true;
14978 /* -- a cv-qualified type where the cv-unqualified type is
14979 dependent. */
14980 type = TYPE_MAIN_VARIANT (type);
14981 /* -- a compound type constructed from any dependent type. */
14982 if (TYPE_PTR_TO_MEMBER_P (type))
14983 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
14984 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
14985 (type)));
14986 else if (TREE_CODE (type) == POINTER_TYPE
14987 || TREE_CODE (type) == REFERENCE_TYPE)
14988 return dependent_type_p (TREE_TYPE (type));
14989 else if (TREE_CODE (type) == FUNCTION_TYPE
14990 || TREE_CODE (type) == METHOD_TYPE)
14992 tree arg_type;
14994 if (dependent_type_p (TREE_TYPE (type)))
14995 return true;
14996 for (arg_type = TYPE_ARG_TYPES (type);
14997 arg_type;
14998 arg_type = TREE_CHAIN (arg_type))
14999 if (dependent_type_p (TREE_VALUE (arg_type)))
15000 return true;
15001 return false;
15003 /* -- an array type constructed from any dependent type or whose
15004 size is specified by a constant expression that is
15005 value-dependent. */
15006 if (TREE_CODE (type) == ARRAY_TYPE)
15008 if (TYPE_DOMAIN (type)
15009 && ((value_dependent_expression_p
15010 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
15011 || (type_dependent_expression_p
15012 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
15013 return true;
15014 return dependent_type_p (TREE_TYPE (type));
15017 /* -- a template-id in which either the template name is a template
15018 parameter ... */
15019 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
15020 return true;
15021 /* ... or any of the template arguments is a dependent type or
15022 an expression that is type-dependent or value-dependent. */
15023 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
15024 && (any_dependent_template_arguments_p
15025 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
15026 return true;
15028 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
15029 argument of the `typeof' expression is not type-dependent, then
15030 it should already been have resolved. */
15031 if (TREE_CODE (type) == TYPEOF_TYPE
15032 || TREE_CODE (type) == DECLTYPE_TYPE)
15033 return true;
15035 /* A template argument pack is dependent if any of its packed
15036 arguments are. */
15037 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
15039 tree args = ARGUMENT_PACK_ARGS (type);
15040 int i, len = TREE_VEC_LENGTH (args);
15041 for (i = 0; i < len; ++i)
15042 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
15043 return true;
15046 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
15047 be template parameters. */
15048 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
15049 return true;
15051 /* The standard does not specifically mention types that are local
15052 to template functions or local classes, but they should be
15053 considered dependent too. For example:
15055 template <int I> void f() {
15056 enum E { a = I };
15057 S<sizeof (E)> s;
15060 The size of `E' cannot be known until the value of `I' has been
15061 determined. Therefore, `E' must be considered dependent. */
15062 scope = TYPE_CONTEXT (type);
15063 if (scope && TYPE_P (scope))
15064 return dependent_type_p (scope);
15065 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
15066 return type_dependent_expression_p (scope);
15068 /* Other types are non-dependent. */
15069 return false;
15072 /* Returns TRUE if TYPE is dependent, in the sense of
15073 [temp.dep.type]. */
15075 bool
15076 dependent_type_p (tree type)
15078 /* If there are no template parameters in scope, then there can't be
15079 any dependent types. */
15080 if (!processing_template_decl)
15082 /* If we are not processing a template, then nobody should be
15083 providing us with a dependent type. */
15084 gcc_assert (type);
15085 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM);
15086 return false;
15089 /* If the type is NULL, we have not computed a type for the entity
15090 in question; in that case, the type is dependent. */
15091 if (!type)
15092 return true;
15094 /* Erroneous types can be considered non-dependent. */
15095 if (type == error_mark_node)
15096 return false;
15098 /* If we have not already computed the appropriate value for TYPE,
15099 do so now. */
15100 if (!TYPE_DEPENDENT_P_VALID (type))
15102 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
15103 TYPE_DEPENDENT_P_VALID (type) = 1;
15106 return TYPE_DEPENDENT_P (type);
15109 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
15111 static bool
15112 dependent_scope_ref_p (tree expression, bool criterion (tree))
15114 tree scope;
15115 tree name;
15117 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
15119 if (!TYPE_P (TREE_OPERAND (expression, 0)))
15120 return true;
15122 scope = TREE_OPERAND (expression, 0);
15123 name = TREE_OPERAND (expression, 1);
15125 /* [temp.dep.expr]
15127 An id-expression is type-dependent if it contains a
15128 nested-name-specifier that contains a class-name that names a
15129 dependent type. */
15130 /* The suggested resolution to Core Issue 2 implies that if the
15131 qualifying type is the current class, then we must peek
15132 inside it. */
15133 if (DECL_P (name)
15134 && currently_open_class (scope)
15135 && !criterion (name))
15136 return false;
15137 if (dependent_type_p (scope))
15138 return true;
15140 return false;
15143 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
15144 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
15145 expression. */
15147 bool
15148 value_dependent_expression_p (tree expression)
15150 if (!processing_template_decl)
15151 return false;
15153 /* A name declared with a dependent type. */
15154 if (DECL_P (expression) && type_dependent_expression_p (expression))
15155 return true;
15157 switch (TREE_CODE (expression))
15159 case IDENTIFIER_NODE:
15160 /* A name that has not been looked up -- must be dependent. */
15161 return true;
15163 case TEMPLATE_PARM_INDEX:
15164 /* A non-type template parm. */
15165 return true;
15167 case CONST_DECL:
15168 /* A non-type template parm. */
15169 if (DECL_TEMPLATE_PARM_P (expression))
15170 return true;
15171 return false;
15173 case VAR_DECL:
15174 /* A constant with integral or enumeration type and is initialized
15175 with an expression that is value-dependent. */
15176 if (DECL_INITIAL (expression)
15177 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
15178 && value_dependent_expression_p (DECL_INITIAL (expression)))
15179 return true;
15180 return false;
15182 case DYNAMIC_CAST_EXPR:
15183 case STATIC_CAST_EXPR:
15184 case CONST_CAST_EXPR:
15185 case REINTERPRET_CAST_EXPR:
15186 case CAST_EXPR:
15187 /* These expressions are value-dependent if the type to which
15188 the cast occurs is dependent or the expression being casted
15189 is value-dependent. */
15191 tree type = TREE_TYPE (expression);
15193 if (dependent_type_p (type))
15194 return true;
15196 /* A functional cast has a list of operands. */
15197 expression = TREE_OPERAND (expression, 0);
15198 if (!expression)
15200 /* If there are no operands, it must be an expression such
15201 as "int()". This should not happen for aggregate types
15202 because it would form non-constant expressions. */
15203 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
15205 return false;
15208 if (TREE_CODE (expression) == TREE_LIST)
15209 return any_value_dependent_elements_p (expression);
15211 return value_dependent_expression_p (expression);
15214 case SIZEOF_EXPR:
15215 case ALIGNOF_EXPR:
15216 /* A `sizeof' expression is value-dependent if the operand is
15217 type-dependent or is a pack expansion. */
15218 expression = TREE_OPERAND (expression, 0);
15219 if (PACK_EXPANSION_P (expression))
15220 return true;
15221 else if (TYPE_P (expression))
15222 return dependent_type_p (expression);
15223 return type_dependent_expression_p (expression);
15225 case SCOPE_REF:
15226 return dependent_scope_ref_p (expression, value_dependent_expression_p);
15228 case COMPONENT_REF:
15229 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
15230 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
15232 case CALL_EXPR:
15233 /* A CALL_EXPR may appear in a constant expression if it is a
15234 call to a builtin function, e.g., __builtin_constant_p. All
15235 such calls are value-dependent. */
15236 return true;
15238 case NONTYPE_ARGUMENT_PACK:
15239 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
15240 is value-dependent. */
15242 tree values = ARGUMENT_PACK_ARGS (expression);
15243 int i, len = TREE_VEC_LENGTH (values);
15245 for (i = 0; i < len; ++i)
15246 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
15247 return true;
15249 return false;
15252 case TRAIT_EXPR:
15254 tree type2 = TRAIT_EXPR_TYPE2 (expression);
15255 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
15256 || (type2 ? dependent_type_p (type2) : false));
15259 case MODOP_EXPR:
15260 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
15261 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
15263 default:
15264 /* A constant expression is value-dependent if any subexpression is
15265 value-dependent. */
15266 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
15268 case tcc_reference:
15269 case tcc_unary:
15270 return (value_dependent_expression_p
15271 (TREE_OPERAND (expression, 0)));
15273 case tcc_comparison:
15274 case tcc_binary:
15275 return ((value_dependent_expression_p
15276 (TREE_OPERAND (expression, 0)))
15277 || (value_dependent_expression_p
15278 (TREE_OPERAND (expression, 1))));
15280 case tcc_expression:
15281 case tcc_vl_exp:
15283 int i;
15284 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
15285 /* In some cases, some of the operands may be missing.
15286 (For example, in the case of PREDECREMENT_EXPR, the
15287 amount to increment by may be missing.) That doesn't
15288 make the expression dependent. */
15289 if (TREE_OPERAND (expression, i)
15290 && (value_dependent_expression_p
15291 (TREE_OPERAND (expression, i))))
15292 return true;
15293 return false;
15296 default:
15297 break;
15301 /* The expression is not value-dependent. */
15302 return false;
15305 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
15306 [temp.dep.expr]. */
15308 bool
15309 type_dependent_expression_p (tree expression)
15311 if (!processing_template_decl)
15312 return false;
15314 if (expression == error_mark_node)
15315 return false;
15317 /* An unresolved name is always dependent. */
15318 if (TREE_CODE (expression) == IDENTIFIER_NODE
15319 || TREE_CODE (expression) == USING_DECL)
15320 return true;
15322 /* Some expression forms are never type-dependent. */
15323 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
15324 || TREE_CODE (expression) == SIZEOF_EXPR
15325 || TREE_CODE (expression) == ALIGNOF_EXPR
15326 || TREE_CODE (expression) == TRAIT_EXPR
15327 || TREE_CODE (expression) == TYPEID_EXPR
15328 || TREE_CODE (expression) == DELETE_EXPR
15329 || TREE_CODE (expression) == VEC_DELETE_EXPR
15330 || TREE_CODE (expression) == THROW_EXPR)
15331 return false;
15333 /* The types of these expressions depends only on the type to which
15334 the cast occurs. */
15335 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
15336 || TREE_CODE (expression) == STATIC_CAST_EXPR
15337 || TREE_CODE (expression) == CONST_CAST_EXPR
15338 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
15339 || TREE_CODE (expression) == CAST_EXPR)
15340 return dependent_type_p (TREE_TYPE (expression));
15342 /* The types of these expressions depends only on the type created
15343 by the expression. */
15344 if (TREE_CODE (expression) == NEW_EXPR
15345 || TREE_CODE (expression) == VEC_NEW_EXPR)
15347 /* For NEW_EXPR tree nodes created inside a template, either
15348 the object type itself or a TREE_LIST may appear as the
15349 operand 1. */
15350 tree type = TREE_OPERAND (expression, 1);
15351 if (TREE_CODE (type) == TREE_LIST)
15352 /* This is an array type. We need to check array dimensions
15353 as well. */
15354 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
15355 || value_dependent_expression_p
15356 (TREE_OPERAND (TREE_VALUE (type), 1));
15357 else
15358 return dependent_type_p (type);
15361 if (TREE_CODE (expression) == SCOPE_REF
15362 && dependent_scope_ref_p (expression,
15363 type_dependent_expression_p))
15364 return true;
15366 if (TREE_CODE (expression) == FUNCTION_DECL
15367 && DECL_LANG_SPECIFIC (expression)
15368 && DECL_TEMPLATE_INFO (expression)
15369 && (any_dependent_template_arguments_p
15370 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
15371 return true;
15373 if (TREE_CODE (expression) == TEMPLATE_DECL
15374 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
15375 return false;
15377 if (TREE_CODE (expression) == STMT_EXPR)
15378 expression = stmt_expr_value_expr (expression);
15380 if (TREE_TYPE (expression) == unknown_type_node)
15382 if (TREE_CODE (expression) == ADDR_EXPR)
15383 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
15384 if (TREE_CODE (expression) == COMPONENT_REF
15385 || TREE_CODE (expression) == OFFSET_REF)
15387 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
15388 return true;
15389 expression = TREE_OPERAND (expression, 1);
15390 if (TREE_CODE (expression) == IDENTIFIER_NODE)
15391 return false;
15393 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
15394 if (TREE_CODE (expression) == SCOPE_REF)
15395 return false;
15397 if (TREE_CODE (expression) == BASELINK)
15398 expression = BASELINK_FUNCTIONS (expression);
15400 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
15402 if (any_dependent_template_arguments_p
15403 (TREE_OPERAND (expression, 1)))
15404 return true;
15405 expression = TREE_OPERAND (expression, 0);
15407 gcc_assert (TREE_CODE (expression) == OVERLOAD
15408 || TREE_CODE (expression) == FUNCTION_DECL);
15410 while (expression)
15412 if (type_dependent_expression_p (OVL_CURRENT (expression)))
15413 return true;
15414 expression = OVL_NEXT (expression);
15416 return false;
15419 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
15421 return (dependent_type_p (TREE_TYPE (expression)));
15424 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
15425 contains a type-dependent expression. */
15427 bool
15428 any_type_dependent_arguments_p (const_tree args)
15430 while (args)
15432 tree arg = TREE_VALUE (args);
15434 if (type_dependent_expression_p (arg))
15435 return true;
15436 args = TREE_CHAIN (args);
15438 return false;
15441 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
15442 expressions) contains any value-dependent expressions. */
15444 bool
15445 any_value_dependent_elements_p (const_tree list)
15447 for (; list; list = TREE_CHAIN (list))
15448 if (value_dependent_expression_p (TREE_VALUE (list)))
15449 return true;
15451 return false;
15454 /* Returns TRUE if the ARG (a template argument) is dependent. */
15456 bool
15457 dependent_template_arg_p (tree arg)
15459 if (!processing_template_decl)
15460 return false;
15462 if (TREE_CODE (arg) == TEMPLATE_DECL
15463 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15464 return dependent_template_p (arg);
15465 else if (ARGUMENT_PACK_P (arg))
15467 tree args = ARGUMENT_PACK_ARGS (arg);
15468 int i, len = TREE_VEC_LENGTH (args);
15469 for (i = 0; i < len; ++i)
15471 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
15472 return true;
15475 return false;
15477 else if (TYPE_P (arg))
15478 return dependent_type_p (arg);
15479 else
15480 return (type_dependent_expression_p (arg)
15481 || value_dependent_expression_p (arg));
15484 /* Returns true if ARGS (a collection of template arguments) contains
15485 any types that require structural equality testing. */
15487 bool
15488 any_template_arguments_need_structural_equality_p (tree args)
15490 int i;
15491 int j;
15493 if (!args)
15494 return false;
15495 if (args == error_mark_node)
15496 return true;
15498 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
15500 tree level = TMPL_ARGS_LEVEL (args, i + 1);
15501 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
15503 tree arg = TREE_VEC_ELT (level, j);
15504 tree packed_args = NULL_TREE;
15505 int k, len = 1;
15507 if (ARGUMENT_PACK_P (arg))
15509 /* Look inside the argument pack. */
15510 packed_args = ARGUMENT_PACK_ARGS (arg);
15511 len = TREE_VEC_LENGTH (packed_args);
15514 for (k = 0; k < len; ++k)
15516 if (packed_args)
15517 arg = TREE_VEC_ELT (packed_args, k);
15519 if (error_operand_p (arg))
15520 return true;
15521 else if (TREE_CODE (arg) == TEMPLATE_DECL
15522 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15523 continue;
15524 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
15525 return true;
15526 else if (!TYPE_P (arg) && TREE_TYPE (arg)
15527 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
15528 return true;
15533 return false;
15536 /* Returns true if ARGS (a collection of template arguments) contains
15537 any dependent arguments. */
15539 bool
15540 any_dependent_template_arguments_p (const_tree args)
15542 int i;
15543 int j;
15545 if (!args)
15546 return false;
15547 if (args == error_mark_node)
15548 return true;
15550 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
15552 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
15553 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
15554 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
15555 return true;
15558 return false;
15561 /* Returns TRUE if the template TMPL is dependent. */
15563 bool
15564 dependent_template_p (tree tmpl)
15566 if (TREE_CODE (tmpl) == OVERLOAD)
15568 while (tmpl)
15570 if (dependent_template_p (OVL_FUNCTION (tmpl)))
15571 return true;
15572 tmpl = OVL_CHAIN (tmpl);
15574 return false;
15577 /* Template template parameters are dependent. */
15578 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
15579 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
15580 return true;
15581 /* So are names that have not been looked up. */
15582 if (TREE_CODE (tmpl) == SCOPE_REF
15583 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
15584 return true;
15585 /* So are member templates of dependent classes. */
15586 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
15587 return dependent_type_p (DECL_CONTEXT (tmpl));
15588 return false;
15591 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
15593 bool
15594 dependent_template_id_p (tree tmpl, tree args)
15596 return (dependent_template_p (tmpl)
15597 || any_dependent_template_arguments_p (args));
15600 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
15601 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
15602 no such TYPE can be found. Note that this function peers inside
15603 uninstantiated templates and therefore should be used only in
15604 extremely limited situations. ONLY_CURRENT_P restricts this
15605 peering to the currently open classes hierarchy (which is required
15606 when comparing types). */
15608 tree
15609 resolve_typename_type (tree type, bool only_current_p)
15611 tree scope;
15612 tree name;
15613 tree decl;
15614 int quals;
15615 tree pushed_scope;
15616 tree result;
15618 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
15620 scope = TYPE_CONTEXT (type);
15621 name = TYPE_IDENTIFIER (type);
15623 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
15624 it first before we can figure out what NAME refers to. */
15625 if (TREE_CODE (scope) == TYPENAME_TYPE)
15626 scope = resolve_typename_type (scope, only_current_p);
15627 /* If we don't know what SCOPE refers to, then we cannot resolve the
15628 TYPENAME_TYPE. */
15629 if (TREE_CODE (scope) == TYPENAME_TYPE)
15630 return type;
15631 /* If the SCOPE is a template type parameter, we have no way of
15632 resolving the name. */
15633 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
15634 return type;
15635 /* If the SCOPE is not the current instantiation, there's no reason
15636 to look inside it. */
15637 if (only_current_p && !currently_open_class (scope))
15638 return type;
15639 /* If SCOPE is a partial instantiation, it will not have a valid
15640 TYPE_FIELDS list, so use the original template. */
15641 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
15642 /* Enter the SCOPE so that name lookup will be resolved as if we
15643 were in the class definition. In particular, SCOPE will no
15644 longer be considered a dependent type. */
15645 pushed_scope = push_scope (scope);
15646 /* Look up the declaration. */
15647 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
15649 result = NULL_TREE;
15651 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
15652 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
15653 if (!decl)
15654 /*nop*/;
15655 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
15656 && TREE_CODE (decl) == TYPE_DECL)
15658 result = TREE_TYPE (decl);
15659 if (result == error_mark_node)
15660 result = NULL_TREE;
15662 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
15663 && DECL_CLASS_TEMPLATE_P (decl))
15665 tree tmpl;
15666 tree args;
15667 /* Obtain the template and the arguments. */
15668 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
15669 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
15670 /* Instantiate the template. */
15671 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
15672 /*entering_scope=*/0,
15673 tf_error | tf_user);
15674 if (result == error_mark_node)
15675 result = NULL_TREE;
15678 /* Leave the SCOPE. */
15679 if (pushed_scope)
15680 pop_scope (pushed_scope);
15682 /* If we failed to resolve it, return the original typename. */
15683 if (!result)
15684 return type;
15686 /* If lookup found a typename type, resolve that too. */
15687 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
15689 /* Ill-formed programs can cause infinite recursion here, so we
15690 must catch that. */
15691 TYPENAME_IS_RESOLVING_P (type) = 1;
15692 result = resolve_typename_type (result, only_current_p);
15693 TYPENAME_IS_RESOLVING_P (type) = 0;
15696 /* Qualify the resulting type. */
15697 quals = cp_type_quals (type);
15698 if (quals)
15699 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
15701 return result;
15704 /* EXPR is an expression which is not type-dependent. Return a proxy
15705 for EXPR that can be used to compute the types of larger
15706 expressions containing EXPR. */
15708 tree
15709 build_non_dependent_expr (tree expr)
15711 tree inner_expr;
15713 /* Preserve null pointer constants so that the type of things like
15714 "p == 0" where "p" is a pointer can be determined. */
15715 if (null_ptr_cst_p (expr))
15716 return expr;
15717 /* Preserve OVERLOADs; the functions must be available to resolve
15718 types. */
15719 inner_expr = expr;
15720 if (TREE_CODE (inner_expr) == STMT_EXPR)
15721 inner_expr = stmt_expr_value_expr (inner_expr);
15722 if (TREE_CODE (inner_expr) == ADDR_EXPR)
15723 inner_expr = TREE_OPERAND (inner_expr, 0);
15724 if (TREE_CODE (inner_expr) == COMPONENT_REF)
15725 inner_expr = TREE_OPERAND (inner_expr, 1);
15726 if (is_overloaded_fn (inner_expr)
15727 || TREE_CODE (inner_expr) == OFFSET_REF)
15728 return expr;
15729 /* There is no need to return a proxy for a variable. */
15730 if (TREE_CODE (expr) == VAR_DECL)
15731 return expr;
15732 /* Preserve string constants; conversions from string constants to
15733 "char *" are allowed, even though normally a "const char *"
15734 cannot be used to initialize a "char *". */
15735 if (TREE_CODE (expr) == STRING_CST)
15736 return expr;
15737 /* Preserve arithmetic constants, as an optimization -- there is no
15738 reason to create a new node. */
15739 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
15740 return expr;
15741 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
15742 There is at least one place where we want to know that a
15743 particular expression is a throw-expression: when checking a ?:
15744 expression, there are special rules if the second or third
15745 argument is a throw-expression. */
15746 if (TREE_CODE (expr) == THROW_EXPR)
15747 return expr;
15749 if (TREE_CODE (expr) == COND_EXPR)
15750 return build3 (COND_EXPR,
15751 TREE_TYPE (expr),
15752 TREE_OPERAND (expr, 0),
15753 (TREE_OPERAND (expr, 1)
15754 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
15755 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
15756 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
15757 if (TREE_CODE (expr) == COMPOUND_EXPR
15758 && !COMPOUND_EXPR_OVERLOADED (expr))
15759 return build2 (COMPOUND_EXPR,
15760 TREE_TYPE (expr),
15761 TREE_OPERAND (expr, 0),
15762 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
15764 /* If the type is unknown, it can't really be non-dependent */
15765 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
15767 /* Otherwise, build a NON_DEPENDENT_EXPR.
15769 REFERENCE_TYPEs are not stripped for expressions in templates
15770 because doing so would play havoc with mangling. Consider, for
15771 example:
15773 template <typename T> void f<T& g>() { g(); }
15775 In the body of "f", the expression for "g" will have
15776 REFERENCE_TYPE, even though the standard says that it should
15777 not. The reason is that we must preserve the syntactic form of
15778 the expression so that mangling (say) "f<g>" inside the body of
15779 "f" works out correctly. Therefore, the REFERENCE_TYPE is
15780 stripped here. */
15781 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
15784 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
15785 Return a new TREE_LIST with the various arguments replaced with
15786 equivalent non-dependent expressions. */
15788 tree
15789 build_non_dependent_args (tree args)
15791 tree a;
15792 tree new_args;
15794 new_args = NULL_TREE;
15795 for (a = args; a; a = TREE_CHAIN (a))
15796 new_args = tree_cons (NULL_TREE,
15797 build_non_dependent_expr (TREE_VALUE (a)),
15798 new_args);
15799 return nreverse (new_args);
15802 #include "gt-cp-pt.h"