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, 2008, 2009
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
31 #include "coretypes.h"
35 #include "pointer-set.h"
39 #include "cp-objcp-common.h"
40 #include "tree-inline.h"
47 #include "tree-iterator.h"
50 /* The type of functions taking a tree, and some additional data, and
52 typedef int (*tree_fn_t) (tree, void*);
54 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
55 instantiations have been deferred, either because their definitions
56 were not yet available, or because we were putting off doing the work. */
57 struct GTY (()) pending_template {
58 struct pending_template *next;
59 struct tinst_level *tinst;
62 static GTY(()) struct pending_template *pending_templates;
63 static GTY(()) struct pending_template *last_pending_template;
65 int processing_template_parmlist;
66 static int template_header_count;
68 static GTY(()) tree saved_trees;
69 static VEC(int,heap) *inline_parm_levels;
71 static GTY(()) struct tinst_level *current_tinst_level;
73 static GTY(()) tree saved_access_scope;
75 /* Live only within one (recursive) call to tsubst_expr. We use
76 this to pass the statement expression node from the STMT_EXPR
77 to the EXPR_STMT that is its result. */
78 static tree cur_stmt_expr;
80 /* A map from local variable declarations in the body of the template
81 presently being instantiated to the corresponding instantiated
83 static htab_t local_specializations;
85 /* Contains canonical template parameter types. The vector is indexed by
86 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
87 TREE_LIST, whose TREE_VALUEs contain the canonical template
88 parameters of various types and levels. */
89 static GTY(()) VEC(tree,gc) *canonical_template_parms;
91 #define UNIFY_ALLOW_NONE 0
92 #define UNIFY_ALLOW_MORE_CV_QUAL 1
93 #define UNIFY_ALLOW_LESS_CV_QUAL 2
94 #define UNIFY_ALLOW_DERIVED 4
95 #define UNIFY_ALLOW_INTEGER 8
96 #define UNIFY_ALLOW_OUTER_LEVEL 16
97 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
98 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
100 static void push_access_scope (tree);
101 static void pop_access_scope (tree);
102 static bool resolve_overloaded_unification (tree, tree, tree, tree,
103 unification_kind_t, int);
104 static int try_one_overload (tree, tree, tree, tree, tree,
105 unification_kind_t, int, bool);
106 static int unify (tree, tree, tree, tree, int);
107 static void add_pending_template (tree);
108 static int push_tinst_level (tree);
109 static void pop_tinst_level (void);
110 static tree reopen_tinst_level (struct tinst_level *);
111 static tree tsubst_initializer_list (tree, tree);
112 static tree get_class_bindings (tree, tree, tree);
113 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
115 static void tsubst_enum (tree, tree, tree);
116 static tree add_to_template_args (tree, tree);
117 static tree add_outermost_template_args (tree, tree);
118 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
119 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
121 static int type_unification_real (tree, tree, tree, tree,
122 int, unification_kind_t, int);
123 static void note_template_header (int);
124 static tree convert_nontype_argument_function (tree, tree);
125 static tree convert_nontype_argument (tree, tree);
126 static tree convert_template_argument (tree, tree, tree,
127 tsubst_flags_t, int, tree);
128 static int for_each_template_parm (tree, tree_fn_t, void*,
129 struct pointer_set_t*, bool);
130 static tree expand_template_argument_pack (tree);
131 static tree build_template_parm_index (int, int, int, tree, tree);
132 static bool inline_needs_template_parms (tree);
133 static void push_inline_template_parms_recursive (tree, int);
134 static tree retrieve_local_specialization (tree);
135 static void register_local_specialization (tree, tree);
136 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
137 static int mark_template_parm (tree, void *);
138 static int template_parm_this_level_p (tree, void *);
139 static tree tsubst_friend_function (tree, tree);
140 static tree tsubst_friend_class (tree, tree);
141 static int can_complete_type_without_circularity (tree);
142 static tree get_bindings (tree, tree, tree, bool);
143 static int template_decl_level (tree);
144 static int check_cv_quals_for_unify (int, tree, tree);
145 static void template_parm_level_and_index (tree, int*, int*);
146 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
147 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
148 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
149 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
150 static void regenerate_decl_from_template (tree, tree);
151 static tree most_specialized_class (tree, tree);
152 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
153 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
154 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
155 static bool check_specialization_scope (void);
156 static tree process_partial_specialization (tree);
157 static void set_current_access_from_decl (tree);
158 static tree get_template_base (tree, tree, tree, tree);
159 static tree try_class_unification (tree, tree, tree, tree);
160 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
162 static bool template_template_parm_bindings_ok_p (tree, tree);
163 static int template_args_equal (tree, tree);
164 static void tsubst_default_arguments (tree);
165 static tree for_each_template_parm_r (tree *, int *, void *);
166 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
167 static void copy_default_args_to_explicit_spec (tree);
168 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
169 static int eq_local_specializations (const void *, const void *);
170 static bool dependent_template_arg_p (tree);
171 static bool any_template_arguments_need_structural_equality_p (tree);
172 static bool dependent_type_p_r (tree);
173 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
174 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
175 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
176 static tree tsubst_decl (tree, tree, tsubst_flags_t);
177 static void perform_typedefs_access_check (tree tmpl, tree targs);
179 /* Make the current scope suitable for access checking when we are
180 processing T. T can be FUNCTION_DECL for instantiated function
181 template, or VAR_DECL for static member variable (need by
182 instantiate_decl). */
185 push_access_scope (tree t)
187 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
188 || TREE_CODE (t) == VAR_DECL);
190 if (DECL_FRIEND_CONTEXT (t))
191 push_nested_class (DECL_FRIEND_CONTEXT (t));
192 else if (DECL_CLASS_SCOPE_P (t))
193 push_nested_class (DECL_CONTEXT (t));
195 push_to_top_level ();
197 if (TREE_CODE (t) == FUNCTION_DECL)
199 saved_access_scope = tree_cons
200 (NULL_TREE, current_function_decl, saved_access_scope);
201 current_function_decl = t;
205 /* Restore the scope set up by push_access_scope. T is the node we
209 pop_access_scope (tree t)
211 if (TREE_CODE (t) == FUNCTION_DECL)
213 current_function_decl = TREE_VALUE (saved_access_scope);
214 saved_access_scope = TREE_CHAIN (saved_access_scope);
217 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
220 pop_from_top_level ();
223 /* Do any processing required when DECL (a member template
224 declaration) is finished. Returns the TEMPLATE_DECL corresponding
225 to DECL, unless it is a specialization, in which case the DECL
226 itself is returned. */
229 finish_member_template_decl (tree decl)
231 if (decl == error_mark_node)
232 return error_mark_node;
234 gcc_assert (DECL_P (decl));
236 if (TREE_CODE (decl) == TYPE_DECL)
240 type = TREE_TYPE (decl);
241 if (type == error_mark_node)
242 return error_mark_node;
243 if (MAYBE_CLASS_TYPE_P (type)
244 && CLASSTYPE_TEMPLATE_INFO (type)
245 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
247 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
248 check_member_template (tmpl);
253 else if (TREE_CODE (decl) == FIELD_DECL)
254 error ("data member %qD cannot be a member template", decl);
255 else if (DECL_TEMPLATE_INFO (decl))
257 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
259 check_member_template (DECL_TI_TEMPLATE (decl));
260 return DECL_TI_TEMPLATE (decl);
266 error ("invalid member template declaration %qD", decl);
268 return error_mark_node;
271 /* Return the template info node corresponding to T, whatever T is. */
274 get_template_info (tree t)
276 tree tinfo = NULL_TREE;
278 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
279 tinfo = DECL_TEMPLATE_INFO (t);
281 if (!tinfo && TREE_CODE (t) == TYPE_DECL)
284 if (TAGGED_TYPE_P (t))
285 tinfo = TYPE_TEMPLATE_INFO (t);
290 /* Returns the template nesting level of the indicated class TYPE.
300 A<T>::B<U> has depth two, while A<T> has depth one.
301 Both A<T>::B<int> and A<int>::B<U> have depth one, if
302 they are instantiations, not specializations.
304 This function is guaranteed to return 0 if passed NULL_TREE so
305 that, for example, `template_class_depth (current_class_type)' is
309 template_class_depth (tree type)
314 type && TREE_CODE (type) != NAMESPACE_DECL;
315 type = (TREE_CODE (type) == FUNCTION_DECL)
316 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
318 tree tinfo = get_template_info (type);
320 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
321 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
328 /* Subroutine of maybe_begin_member_template_processing.
329 Returns true if processing DECL needs us to push template parms. */
332 inline_needs_template_parms (tree decl)
334 if (! DECL_TEMPLATE_INFO (decl))
337 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
338 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
341 /* Subroutine of maybe_begin_member_template_processing.
342 Push the template parms in PARMS, starting from LEVELS steps into the
343 chain, and ending at the beginning, since template parms are listed
347 push_inline_template_parms_recursive (tree parmlist, int levels)
349 tree parms = TREE_VALUE (parmlist);
353 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
355 ++processing_template_decl;
356 current_template_parms
357 = tree_cons (size_int (processing_template_decl),
358 parms, current_template_parms);
359 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
361 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
363 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
365 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
367 if (parm == error_mark_node)
370 gcc_assert (DECL_P (parm));
372 switch (TREE_CODE (parm))
381 /* Make a CONST_DECL as is done in process_template_parm.
382 It is ugly that we recreate this here; the original
383 version built in process_template_parm is no longer
385 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
387 DECL_ARTIFICIAL (decl) = 1;
388 TREE_CONSTANT (decl) = 1;
389 TREE_READONLY (decl) = 1;
390 DECL_INITIAL (decl) = DECL_INITIAL (parm);
391 SET_DECL_TEMPLATE_PARM_P (decl);
402 /* Restore the template parameter context for a member template or
403 a friend template defined in a class definition. */
406 maybe_begin_member_template_processing (tree decl)
411 if (inline_needs_template_parms (decl))
413 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
414 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
416 if (DECL_TEMPLATE_SPECIALIZATION (decl))
419 parms = TREE_CHAIN (parms);
422 push_inline_template_parms_recursive (parms, levels);
425 /* Remember how many levels of template parameters we pushed so that
426 we can pop them later. */
427 VEC_safe_push (int, heap, inline_parm_levels, levels);
430 /* Undo the effects of maybe_begin_member_template_processing. */
433 maybe_end_member_template_processing (void)
438 if (VEC_length (int, inline_parm_levels) == 0)
441 last = VEC_pop (int, inline_parm_levels);
442 for (i = 0; i < last; ++i)
444 --processing_template_decl;
445 current_template_parms = TREE_CHAIN (current_template_parms);
450 /* Return a new template argument vector which contains all of ARGS,
451 but has as its innermost set of arguments the EXTRA_ARGS. */
454 add_to_template_args (tree args, tree extra_args)
461 extra_depth = TMPL_ARGS_DEPTH (extra_args);
462 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
464 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
465 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
467 for (j = 1; j <= extra_depth; ++j, ++i)
468 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
473 /* Like add_to_template_args, but only the outermost ARGS are added to
474 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
475 (EXTRA_ARGS) levels are added. This function is used to combine
476 the template arguments from a partial instantiation with the
477 template arguments used to attain the full instantiation from the
478 partial instantiation. */
481 add_outermost_template_args (tree args, tree extra_args)
485 /* If there are more levels of EXTRA_ARGS than there are ARGS,
486 something very fishy is going on. */
487 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
489 /* If *all* the new arguments will be the EXTRA_ARGS, just return
491 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
494 /* For the moment, we make ARGS look like it contains fewer levels. */
495 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
497 new_args = add_to_template_args (args, extra_args);
499 /* Now, we restore ARGS to its full dimensions. */
500 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
505 /* Return the N levels of innermost template arguments from the ARGS. */
508 get_innermost_template_args (tree args, int n)
516 /* If N is 1, just return the innermost set of template arguments. */
518 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
520 /* If we're not removing anything, just return the arguments we were
522 extra_levels = TMPL_ARGS_DEPTH (args) - n;
523 gcc_assert (extra_levels >= 0);
524 if (extra_levels == 0)
527 /* Make a new set of arguments, not containing the outer arguments. */
528 new_args = make_tree_vec (n);
529 for (i = 1; i <= n; ++i)
530 SET_TMPL_ARGS_LEVEL (new_args, i,
531 TMPL_ARGS_LEVEL (args, i + extra_levels));
536 /* The inverse of get_innermost_template_args: Return all but the innermost
537 EXTRA_LEVELS levels of template arguments from the ARGS. */
540 strip_innermost_template_args (tree args, int extra_levels)
543 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
548 /* If N is 1, just return the outermost set of template arguments. */
550 return TMPL_ARGS_LEVEL (args, 1);
552 /* If we're not removing anything, just return the arguments we were
554 gcc_assert (extra_levels >= 0);
555 if (extra_levels == 0)
558 /* Make a new set of arguments, not containing the inner arguments. */
559 new_args = make_tree_vec (n);
560 for (i = 1; i <= n; ++i)
561 SET_TMPL_ARGS_LEVEL (new_args, i,
562 TMPL_ARGS_LEVEL (args, i));
567 /* We've got a template header coming up; push to a new level for storing
571 begin_template_parm_list (void)
573 /* We use a non-tag-transparent scope here, which causes pushtag to
574 put tags in this scope, rather than in the enclosing class or
575 namespace scope. This is the right thing, since we want
576 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
577 global template class, push_template_decl handles putting the
578 TEMPLATE_DECL into top-level scope. For a nested template class,
581 template <class T> struct S1 {
582 template <class T> struct S2 {};
585 pushtag contains special code to call pushdecl_with_scope on the
586 TEMPLATE_DECL for S2. */
587 begin_scope (sk_template_parms, NULL);
588 ++processing_template_decl;
589 ++processing_template_parmlist;
590 note_template_header (0);
593 /* This routine is called when a specialization is declared. If it is
594 invalid to declare a specialization here, an error is reported and
595 false is returned, otherwise this routine will return true. */
598 check_specialization_scope (void)
600 tree scope = current_scope ();
604 An explicit specialization shall be declared in the namespace of
605 which the template is a member, or, for member templates, in the
606 namespace of which the enclosing class or enclosing class
607 template is a member. An explicit specialization of a member
608 function, member class or static data member of a class template
609 shall be declared in the namespace of which the class template
611 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
613 error ("explicit specialization in non-namespace scope %qD", scope);
619 In an explicit specialization declaration for a member of a class
620 template or a member template that appears in namespace scope,
621 the member template and some of its enclosing class templates may
622 remain unspecialized, except that the declaration shall not
623 explicitly specialize a class member template if its enclosing
624 class templates are not explicitly specialized as well. */
625 if (current_template_parms)
627 error ("enclosing class templates are not explicitly specialized");
634 /* We've just seen template <>. */
637 begin_specialization (void)
639 begin_scope (sk_template_spec, NULL);
640 note_template_header (1);
641 return check_specialization_scope ();
644 /* Called at then end of processing a declaration preceded by
648 end_specialization (void)
651 reset_specialization ();
654 /* Any template <>'s that we have seen thus far are not referring to a
655 function specialization. */
658 reset_specialization (void)
660 processing_specialization = 0;
661 template_header_count = 0;
664 /* We've just seen a template header. If SPECIALIZATION is nonzero,
665 it was of the form template <>. */
668 note_template_header (int specialization)
670 processing_specialization = specialization;
671 template_header_count++;
674 /* We're beginning an explicit instantiation. */
677 begin_explicit_instantiation (void)
679 gcc_assert (!processing_explicit_instantiation);
680 processing_explicit_instantiation = true;
685 end_explicit_instantiation (void)
687 gcc_assert (processing_explicit_instantiation);
688 processing_explicit_instantiation = false;
691 /* An explicit specialization or partial specialization TMPL is being
692 declared. Check that the namespace in which the specialization is
693 occurring is permissible. Returns false iff it is invalid to
694 specialize TMPL in the current namespace. */
697 check_specialization_namespace (tree tmpl)
699 tree tpl_ns = decl_namespace_context (tmpl);
703 An explicit specialization shall be declared in the namespace of
704 which the template is a member, or, for member templates, in the
705 namespace of which the enclosing class or enclosing class
706 template is a member. An explicit specialization of a member
707 function, member class or static data member of a class template
708 shall be declared in the namespace of which the class template is
710 if (is_associated_namespace (current_namespace, tpl_ns))
711 /* Same or super-using namespace. */
715 permerror (input_location, "specialization of %qD in different namespace", tmpl);
716 permerror (input_location, " from definition of %q+#D", tmpl);
721 /* SPEC is an explicit instantiation. Check that it is valid to
722 perform this explicit instantiation in the current namespace. */
725 check_explicit_instantiation_namespace (tree spec)
729 /* DR 275: An explicit instantiation shall appear in an enclosing
730 namespace of its template. */
731 ns = decl_namespace_context (spec);
732 if (!is_ancestor (current_namespace, ns))
733 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
734 "(which does not enclose namespace %qD)",
735 spec, current_namespace, ns);
738 /* The TYPE is being declared. If it is a template type, that means it
739 is a partial specialization. Do appropriate error-checking. */
742 maybe_process_partial_specialization (tree type)
746 if (type == error_mark_node)
747 return error_mark_node;
749 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
751 error ("name of class shadows template template parameter %qD",
753 return error_mark_node;
756 context = TYPE_CONTEXT (type);
758 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
760 /* This is for ordinary explicit specialization and partial
761 specialization of a template class such as:
763 template <> class C<int>;
767 template <class T> class C<T*>;
769 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
771 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
772 && !COMPLETE_TYPE_P (type))
774 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
775 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
776 if (processing_template_decl)
778 if (push_template_decl (TYPE_MAIN_DECL (type))
780 return error_mark_node;
783 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
784 error ("specialization of %qT after instantiation", type);
786 else if (CLASS_TYPE_P (type)
787 && !CLASSTYPE_USE_TEMPLATE (type)
788 && CLASSTYPE_TEMPLATE_INFO (type)
789 && context && CLASS_TYPE_P (context)
790 && CLASSTYPE_TEMPLATE_INFO (context))
792 /* This is for an explicit specialization of member class
793 template according to [temp.expl.spec/18]:
795 template <> template <class U> class C<int>::D;
797 The context `C<int>' must be an implicit instantiation.
798 Otherwise this is just a member class template declared
801 template <> class C<int> { template <class U> class D; };
802 template <> template <class U> class C<int>::D;
804 In the first case, `C<int>::D' is a specialization of `C<T>::D'
805 while in the second case, `C<int>::D' is a primary template
806 and `C<T>::D' may not exist. */
808 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
809 && !COMPLETE_TYPE_P (type))
813 if (current_namespace
814 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
816 permerror (input_location, "specializing %q#T in different namespace", type);
817 permerror (input_location, " from definition of %q+#D",
818 CLASSTYPE_TI_TEMPLATE (type));
821 /* Check for invalid specialization after instantiation:
823 template <> template <> class C<int>::D<int>;
824 template <> template <class U> class C<int>::D; */
826 for (t = DECL_TEMPLATE_INSTANTIATIONS
827 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
828 t; t = TREE_CHAIN (t))
829 if (TREE_VALUE (t) != type
830 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
831 error ("specialization %qT after instantiation %qT",
832 type, TREE_VALUE (t));
834 /* Mark TYPE as a specialization. And as a result, we only
835 have one level of template argument for the innermost
837 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
838 CLASSTYPE_TI_ARGS (type)
839 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
842 else if (processing_specialization)
844 error ("explicit specialization of non-template %qT", type);
845 return error_mark_node;
851 /* Returns nonzero if we can optimize the retrieval of specializations
852 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
853 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
856 optimize_specialization_lookup_p (tree tmpl)
858 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
859 && DECL_CLASS_SCOPE_P (tmpl)
860 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
862 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
863 /* The optimized lookup depends on the fact that the
864 template arguments for the member function template apply
865 purely to the containing class, which is not true if the
866 containing class is an explicit or partial
868 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
869 && !DECL_MEMBER_TEMPLATE_P (tmpl)
870 && !DECL_CONV_FN_P (tmpl)
871 /* It is possible to have a template that is not a member
872 template and is not a member of a template class:
874 template <typename T>
875 struct S { friend A::f(); };
877 Here, the friend function is a template, but the context does
878 not have template information. The optimized lookup relies
879 on having ARGS be the template arguments for both the class
880 and the function template. */
881 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
884 /* Retrieve the specialization (in the sense of [temp.spec] - a
885 specialization is either an instantiation or an explicit
886 specialization) of TMPL for the given template ARGS. If there is
887 no such specialization, return NULL_TREE. The ARGS are a vector of
888 arguments, or a vector of vectors of arguments, in the case of
889 templates with more than one level of parameters.
891 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
892 then we search for a partial specialization matching ARGS. This
893 parameter is ignored if TMPL is not a class template. */
896 retrieve_specialization (tree tmpl, tree args,
897 bool class_specializations_p)
899 if (args == error_mark_node)
902 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
904 /* There should be as many levels of arguments as there are
905 levels of parameters. */
906 gcc_assert (TMPL_ARGS_DEPTH (args)
907 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
909 if (optimize_specialization_lookup_p (tmpl))
912 tree class_specialization;
913 VEC(tree,gc) *methods;
917 /* The template arguments actually apply to the containing
918 class. Find the class specialization with those
920 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
922 = retrieve_specialization (class_template, args,
923 /*class_specializations_p=*/false);
924 if (!class_specialization)
926 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
927 for the specialization. */
928 idx = class_method_index_for_fn (class_specialization, tmpl);
931 /* Iterate through the methods with the indicated name, looking
932 for the one that has an instance of TMPL. */
933 methods = CLASSTYPE_METHOD_VEC (class_specialization);
934 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
936 tree fn = OVL_CURRENT (fns);
937 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
947 /* Class templates store their instantiations on the
948 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
949 DECL_TEMPLATE_SPECIALIZATIONS list. */
950 if (!class_specializations_p
951 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL
952 && !is_typedef_decl (DECL_TEMPLATE_RESULT (tmpl))
953 && TAGGED_TYPE_P (TREE_TYPE (tmpl)))
954 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
956 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
958 /* Iterate through the list until we find a matching template. */
959 while (*sp != NULL_TREE)
963 if (comp_template_args (TREE_PURPOSE (spec), args))
965 /* Use the move-to-front heuristic to speed up future
969 *sp = TREE_CHAIN (*sp);
970 TREE_CHAIN (spec) = *head;
973 return TREE_VALUE (spec);
975 sp = &TREE_CHAIN (spec);
982 /* Like retrieve_specialization, but for local declarations. */
985 retrieve_local_specialization (tree tmpl)
989 if (local_specializations == NULL)
992 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
993 htab_hash_pointer (tmpl));
994 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
997 /* Returns nonzero iff DECL is a specialization of TMPL. */
1000 is_specialization_of (tree decl, tree tmpl)
1004 if (TREE_CODE (decl) == FUNCTION_DECL)
1008 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1014 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1016 for (t = TREE_TYPE (decl);
1018 t = CLASSTYPE_USE_TEMPLATE (t)
1019 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1020 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1027 /* Returns nonzero iff DECL is a specialization of friend declaration
1028 FRIEND_DECL according to [temp.friend]. */
1031 is_specialization_of_friend (tree decl, tree friend_decl)
1033 bool need_template = true;
1036 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1037 || TREE_CODE (decl) == TYPE_DECL);
1039 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1040 of a template class, we want to check if DECL is a specialization
1042 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1043 && DECL_TEMPLATE_INFO (friend_decl)
1044 && !DECL_USE_TEMPLATE (friend_decl))
1046 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1047 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1048 need_template = false;
1050 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1051 && !PRIMARY_TEMPLATE_P (friend_decl))
1052 need_template = false;
1054 /* There is nothing to do if this is not a template friend. */
1055 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1058 if (is_specialization_of (decl, friend_decl))
1062 A member of a class template may be declared to be a friend of a
1063 non-template class. In this case, the corresponding member of
1064 every specialization of the class template is a friend of the
1065 class granting friendship.
1067 For example, given a template friend declaration
1069 template <class T> friend void A<T>::f();
1071 the member function below is considered a friend
1073 template <> struct A<int> {
1077 For this type of template friend, TEMPLATE_DEPTH below will be
1078 nonzero. To determine if DECL is a friend of FRIEND, we first
1079 check if the enclosing class is a specialization of another. */
1081 template_depth = template_class_depth (DECL_CONTEXT (friend_decl));
1083 && DECL_CLASS_SCOPE_P (decl)
1084 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1085 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1087 /* Next, we check the members themselves. In order to handle
1088 a few tricky cases, such as when FRIEND_DECL's are
1090 template <class T> friend void A<T>::g(T t);
1091 template <class T> template <T t> friend void A<T>::h();
1095 void A<int>::g(int);
1096 template <int> void A<int>::h();
1098 we need to figure out ARGS, the template arguments from
1099 the context of DECL. This is required for template substitution
1100 of `T' in the function parameter of `g' and template parameter
1101 of `h' in the above examples. Here ARGS corresponds to `int'. */
1103 tree context = DECL_CONTEXT (decl);
1104 tree args = NULL_TREE;
1105 int current_depth = 0;
1107 while (current_depth < template_depth)
1109 if (CLASSTYPE_TEMPLATE_INFO (context))
1111 if (current_depth == 0)
1112 args = TYPE_TI_ARGS (context);
1114 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1117 context = TYPE_CONTEXT (context);
1120 if (TREE_CODE (decl) == FUNCTION_DECL)
1125 tree friend_args_type;
1126 tree decl_args_type;
1128 /* Make sure that both DECL and FRIEND_DECL are templates or
1130 is_template = DECL_TEMPLATE_INFO (decl)
1131 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1132 if (need_template ^ is_template)
1134 else if (is_template)
1136 /* If both are templates, check template parameter list. */
1138 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1140 if (!comp_template_parms
1141 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1145 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1148 decl_type = TREE_TYPE (decl);
1150 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1151 tf_none, NULL_TREE);
1152 if (friend_type == error_mark_node)
1155 /* Check if return types match. */
1156 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1159 /* Check if function parameter types match, ignoring the
1160 `this' parameter. */
1161 friend_args_type = TYPE_ARG_TYPES (friend_type);
1162 decl_args_type = TYPE_ARG_TYPES (decl_type);
1163 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1164 friend_args_type = TREE_CHAIN (friend_args_type);
1165 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1166 decl_args_type = TREE_CHAIN (decl_args_type);
1168 return compparms (decl_args_type, friend_args_type);
1172 /* DECL is a TYPE_DECL */
1174 tree decl_type = TREE_TYPE (decl);
1176 /* Make sure that both DECL and FRIEND_DECL are templates or
1179 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1180 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1182 if (need_template ^ is_template)
1184 else if (is_template)
1187 /* If both are templates, check the name of the two
1188 TEMPLATE_DECL's first because is_friend didn't. */
1189 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1190 != DECL_NAME (friend_decl))
1193 /* Now check template parameter list. */
1195 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1197 return comp_template_parms
1198 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1202 return (DECL_NAME (decl)
1203 == DECL_NAME (friend_decl));
1209 /* Register the specialization SPEC as a specialization of TMPL with
1210 the indicated ARGS. IS_FRIEND indicates whether the specialization
1211 is actually just a friend declaration. Returns SPEC, or an
1212 equivalent prior declaration, if available. */
1215 register_specialization (tree spec, tree tmpl, tree args, bool is_friend)
1219 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1221 if (TREE_CODE (spec) == FUNCTION_DECL
1222 && uses_template_parms (DECL_TI_ARGS (spec)))
1223 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1224 register it; we want the corresponding TEMPLATE_DECL instead.
1225 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1226 the more obvious `uses_template_parms (spec)' to avoid problems
1227 with default function arguments. In particular, given
1228 something like this:
1230 template <class T> void f(T t1, T t = T())
1232 the default argument expression is not substituted for in an
1233 instantiation unless and until it is actually needed. */
1236 fn = retrieve_specialization (tmpl, args,
1237 /*class_specializations_p=*/false);
1238 /* We can sometimes try to re-register a specialization that we've
1239 already got. In particular, regenerate_decl_from_template calls
1240 duplicate_decls which will update the specialization list. But,
1241 we'll still get called again here anyhow. It's more convenient
1242 to simply allow this than to try to prevent it. */
1245 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1247 if (DECL_TEMPLATE_INSTANTIATION (fn))
1250 || DECL_EXPLICIT_INSTANTIATION (fn))
1252 error ("specialization of %qD after instantiation",
1254 return error_mark_node;
1259 /* This situation should occur only if the first
1260 specialization is an implicit instantiation, the
1261 second is an explicit specialization, and the
1262 implicit instantiation has not yet been used. That
1263 situation can occur if we have implicitly
1264 instantiated a member function and then specialized
1267 We can also wind up here if a friend declaration that
1268 looked like an instantiation turns out to be a
1271 template <class T> void foo(T);
1272 class S { friend void foo<>(int) };
1273 template <> void foo(int);
1275 We transform the existing DECL in place so that any
1276 pointers to it become pointers to the updated
1279 If there was a definition for the template, but not
1280 for the specialization, we want this to look as if
1281 there were no definition, and vice versa. */
1282 DECL_INITIAL (fn) = NULL_TREE;
1283 duplicate_decls (spec, fn, is_friend);
1284 /* The call to duplicate_decls will have applied
1287 An explicit specialization of a function template
1288 is inline only if it is explicitly declared to be,
1289 and independently of whether its function template
1292 to the primary function; now copy the inline bits to
1293 the various clones. */
1294 FOR_EACH_CLONE (clone, fn)
1295 DECL_DECLARED_INLINE_P (clone)
1296 = DECL_DECLARED_INLINE_P (fn);
1297 check_specialization_namespace (fn);
1302 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1304 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1305 /* Dup decl failed, but this is a new definition. Set the
1306 line number so any errors match this new
1308 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1314 /* A specialization must be declared in the same namespace as the
1315 template it is specializing. */
1316 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1317 && !check_specialization_namespace (tmpl))
1318 DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl));
1320 if (!optimize_specialization_lookup_p (tmpl))
1321 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1322 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1327 /* Unregister the specialization SPEC as a specialization of TMPL.
1328 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1329 if the SPEC was listed as a specialization of TMPL. */
1332 reregister_specialization (tree spec, tree tmpl, tree new_spec)
1336 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1338 s = &TREE_CHAIN (*s))
1339 if (TREE_VALUE (*s) == spec)
1342 *s = TREE_CHAIN (*s);
1344 TREE_VALUE (*s) = new_spec;
1351 /* Compare an entry in the local specializations hash table P1 (which
1352 is really a pointer to a TREE_LIST) with P2 (which is really a
1356 eq_local_specializations (const void *p1, const void *p2)
1358 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1361 /* Hash P1, an entry in the local specializations table. */
1364 hash_local_specialization (const void* p1)
1366 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1369 /* Like register_specialization, but for local declarations. We are
1370 registering SPEC, an instantiation of TMPL. */
1373 register_local_specialization (tree spec, tree tmpl)
1377 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1378 htab_hash_pointer (tmpl), INSERT);
1379 *slot = build_tree_list (spec, tmpl);
1382 /* TYPE is a class type. Returns true if TYPE is an explicitly
1383 specialized class. */
1386 explicit_class_specialization_p (tree type)
1388 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1390 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1393 /* Print the list of candidate FNS in an error message. */
1396 print_candidates (tree fns)
1400 const char *str = "candidates are:";
1402 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1406 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1407 error ("%s %+#D", str, OVL_CURRENT (f));
1412 /* Returns the template (one of the functions given by TEMPLATE_ID)
1413 which can be specialized to match the indicated DECL with the
1414 explicit template args given in TEMPLATE_ID. The DECL may be
1415 NULL_TREE if none is available. In that case, the functions in
1416 TEMPLATE_ID are non-members.
1418 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1419 specialization of a member template.
1421 The TEMPLATE_COUNT is the number of references to qualifying
1422 template classes that appeared in the name of the function. See
1423 check_explicit_specialization for a more accurate description.
1425 TSK indicates what kind of template declaration (if any) is being
1426 declared. TSK_TEMPLATE indicates that the declaration given by
1427 DECL, though a FUNCTION_DECL, has template parameters, and is
1428 therefore a template function.
1430 The template args (those explicitly specified and those deduced)
1431 are output in a newly created vector *TARGS_OUT.
1433 If it is impossible to determine the result, an error message is
1434 issued. The error_mark_node is returned to indicate failure. */
1437 determine_specialization (tree template_id,
1440 int need_member_template,
1446 tree explicit_targs;
1447 tree candidates = NULL_TREE;
1448 /* A TREE_LIST of templates of which DECL may be a specialization.
1449 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1450 corresponding TREE_PURPOSE is the set of template arguments that,
1451 when used to instantiate the template, would produce a function
1452 with the signature of DECL. */
1453 tree templates = NULL_TREE;
1455 struct cp_binding_level *b;
1457 *targs_out = NULL_TREE;
1459 if (template_id == error_mark_node || decl == error_mark_node)
1460 return error_mark_node;
1462 fns = TREE_OPERAND (template_id, 0);
1463 explicit_targs = TREE_OPERAND (template_id, 1);
1465 if (fns == error_mark_node)
1466 return error_mark_node;
1468 /* Check for baselinks. */
1469 if (BASELINK_P (fns))
1470 fns = BASELINK_FUNCTIONS (fns);
1472 if (!is_overloaded_fn (fns))
1474 error ("%qD is not a function template", fns);
1475 return error_mark_node;
1478 /* Count the number of template headers specified for this
1481 for (b = current_binding_level;
1482 b->kind == sk_template_parms;
1486 for (; fns; fns = OVL_NEXT (fns))
1488 tree fn = OVL_CURRENT (fns);
1490 if (TREE_CODE (fn) == TEMPLATE_DECL)
1492 tree decl_arg_types;
1495 /* In case of explicit specialization, we need to check if
1496 the number of template headers appearing in the specialization
1497 is correct. This is usually done in check_explicit_specialization,
1498 but the check done there cannot be exhaustive when specializing
1499 member functions. Consider the following code:
1501 template <> void A<int>::f(int);
1502 template <> template <> void A<int>::f(int);
1504 Assuming that A<int> is not itself an explicit specialization
1505 already, the first line specializes "f" which is a non-template
1506 member function, whilst the second line specializes "f" which
1507 is a template member function. So both lines are syntactically
1508 correct, and check_explicit_specialization does not reject
1511 Here, we can do better, as we are matching the specialization
1512 against the declarations. We count the number of template
1513 headers, and we check if they match TEMPLATE_COUNT + 1
1514 (TEMPLATE_COUNT is the number of qualifying template classes,
1515 plus there must be another header for the member template
1518 Notice that if header_count is zero, this is not a
1519 specialization but rather a template instantiation, so there
1520 is no check we can perform here. */
1521 if (header_count && header_count != template_count + 1)
1524 /* Check that the number of template arguments at the
1525 innermost level for DECL is the same as for FN. */
1526 if (current_binding_level->kind == sk_template_parms
1527 && !current_binding_level->explicit_spec_p
1528 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1529 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1530 (current_template_parms))))
1533 /* DECL might be a specialization of FN. */
1534 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1535 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1537 /* For a non-static member function, we need to make sure
1538 that the const qualification is the same. Since
1539 get_bindings does not try to merge the "this" parameter,
1540 we must do the comparison explicitly. */
1541 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1542 && !same_type_p (TREE_VALUE (fn_arg_types),
1543 TREE_VALUE (decl_arg_types)))
1546 /* Skip the "this" parameter and, for constructors of
1547 classes with virtual bases, the VTT parameter. A
1548 full specialization of a constructor will have a VTT
1549 parameter, but a template never will. */
1551 = skip_artificial_parms_for (decl, decl_arg_types);
1553 = skip_artificial_parms_for (fn, fn_arg_types);
1555 /* Check that the number of function parameters matches.
1557 template <class T> void f(int i = 0);
1558 template <> void f<int>();
1559 The specialization f<int> is invalid but is not caught
1560 by get_bindings below. */
1561 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1564 /* Function templates cannot be specializations; there are
1565 no partial specializations of functions. Therefore, if
1566 the type of DECL does not match FN, there is no
1568 if (tsk == tsk_template)
1570 if (compparms (fn_arg_types, decl_arg_types))
1571 candidates = tree_cons (NULL_TREE, fn, candidates);
1575 /* See whether this function might be a specialization of this
1577 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1580 /* We cannot deduce template arguments that when used to
1581 specialize TMPL will produce DECL. */
1584 /* Save this template, and the arguments deduced. */
1585 templates = tree_cons (targs, fn, templates);
1587 else if (need_member_template)
1588 /* FN is an ordinary member function, and we need a
1589 specialization of a member template. */
1591 else if (TREE_CODE (fn) != FUNCTION_DECL)
1592 /* We can get IDENTIFIER_NODEs here in certain erroneous
1595 else if (!DECL_FUNCTION_MEMBER_P (fn))
1596 /* This is just an ordinary non-member function. Nothing can
1597 be a specialization of that. */
1599 else if (DECL_ARTIFICIAL (fn))
1600 /* Cannot specialize functions that are created implicitly. */
1604 tree decl_arg_types;
1606 /* This is an ordinary member function. However, since
1607 we're here, we can assume it's enclosing class is a
1608 template class. For example,
1610 template <typename T> struct S { void f(); };
1611 template <> void S<int>::f() {}
1613 Here, S<int>::f is a non-template, but S<int> is a
1614 template class. If FN has the same type as DECL, we
1615 might be in business. */
1617 if (!DECL_TEMPLATE_INFO (fn))
1618 /* Its enclosing class is an explicit specialization
1619 of a template class. This is not a candidate. */
1622 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1623 TREE_TYPE (TREE_TYPE (fn))))
1624 /* The return types differ. */
1627 /* Adjust the type of DECL in case FN is a static member. */
1628 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1629 if (DECL_STATIC_FUNCTION_P (fn)
1630 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1631 decl_arg_types = TREE_CHAIN (decl_arg_types);
1633 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1636 candidates = tree_cons (NULL_TREE, fn, candidates);
1640 if (templates && TREE_CHAIN (templates))
1646 It is possible for a specialization with a given function
1647 signature to be instantiated from more than one function
1648 template. In such cases, explicit specification of the
1649 template arguments must be used to uniquely identify the
1650 function template specialization being specialized.
1652 Note that here, there's no suggestion that we're supposed to
1653 determine which of the candidate templates is most
1654 specialized. However, we, also have:
1658 Partial ordering of overloaded function template
1659 declarations is used in the following contexts to select
1660 the function template to which a function template
1661 specialization refers:
1663 -- when an explicit specialization refers to a function
1666 So, we do use the partial ordering rules, at least for now.
1667 This extension can only serve to make invalid programs valid,
1668 so it's safe. And, there is strong anecdotal evidence that
1669 the committee intended the partial ordering rules to apply;
1670 the EDG front end has that behavior, and John Spicer claims
1671 that the committee simply forgot to delete the wording in
1672 [temp.expl.spec]. */
1673 tree tmpl = most_specialized_instantiation (templates);
1674 if (tmpl != error_mark_node)
1677 TREE_CHAIN (templates) = NULL_TREE;
1681 if (templates == NULL_TREE && candidates == NULL_TREE)
1683 error ("template-id %qD for %q+D does not match any template "
1684 "declaration", template_id, decl);
1685 return error_mark_node;
1687 else if ((templates && TREE_CHAIN (templates))
1688 || (candidates && TREE_CHAIN (candidates))
1689 || (templates && candidates))
1691 error ("ambiguous template specialization %qD for %q+D",
1693 chainon (candidates, templates);
1694 print_candidates (candidates);
1695 return error_mark_node;
1698 /* We have one, and exactly one, match. */
1701 tree fn = TREE_VALUE (candidates);
1702 /* DECL is a re-declaration of a template function. */
1703 if (TREE_CODE (fn) == TEMPLATE_DECL)
1705 /* It was a specialization of an ordinary member function in a
1707 *targs_out = copy_node (DECL_TI_ARGS (fn));
1708 return DECL_TI_TEMPLATE (fn);
1711 /* It was a specialization of a template. */
1712 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1713 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1715 *targs_out = copy_node (targs);
1716 SET_TMPL_ARGS_LEVEL (*targs_out,
1717 TMPL_ARGS_DEPTH (*targs_out),
1718 TREE_PURPOSE (templates));
1721 *targs_out = TREE_PURPOSE (templates);
1722 return TREE_VALUE (templates);
1725 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1726 but with the default argument values filled in from those in the
1730 copy_default_args_to_explicit_spec_1 (tree spec_types,
1733 tree new_spec_types;
1738 if (spec_types == void_list_node)
1739 return void_list_node;
1741 /* Substitute into the rest of the list. */
1743 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1744 TREE_CHAIN (tmpl_types));
1746 /* Add the default argument for this parameter. */
1747 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1748 TREE_VALUE (spec_types),
1752 /* DECL is an explicit specialization. Replicate default arguments
1753 from the template it specializes. (That way, code like:
1755 template <class T> void f(T = 3);
1756 template <> void f(double);
1759 works, as required.) An alternative approach would be to look up
1760 the correct default arguments at the call-site, but this approach
1761 is consistent with how implicit instantiations are handled. */
1764 copy_default_args_to_explicit_spec (tree decl)
1769 tree new_spec_types;
1773 tree object_type = NULL_TREE;
1774 tree in_charge = NULL_TREE;
1775 tree vtt = NULL_TREE;
1777 /* See if there's anything we need to do. */
1778 tmpl = DECL_TI_TEMPLATE (decl);
1779 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1780 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1781 if (TREE_PURPOSE (t))
1786 old_type = TREE_TYPE (decl);
1787 spec_types = TYPE_ARG_TYPES (old_type);
1789 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1791 /* Remove the this pointer, but remember the object's type for
1793 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1794 spec_types = TREE_CHAIN (spec_types);
1795 tmpl_types = TREE_CHAIN (tmpl_types);
1797 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1799 /* DECL may contain more parameters than TMPL due to the extra
1800 in-charge parameter in constructors and destructors. */
1801 in_charge = spec_types;
1802 spec_types = TREE_CHAIN (spec_types);
1804 if (DECL_HAS_VTT_PARM_P (decl))
1807 spec_types = TREE_CHAIN (spec_types);
1811 /* Compute the merged default arguments. */
1813 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1815 /* Compute the new FUNCTION_TYPE. */
1819 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1824 /* Put the in-charge parameter back. */
1825 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1826 TREE_VALUE (in_charge),
1829 new_type = build_method_type_directly (object_type,
1830 TREE_TYPE (old_type),
1834 new_type = build_function_type (TREE_TYPE (old_type),
1836 new_type = cp_build_type_attribute_variant (new_type,
1837 TYPE_ATTRIBUTES (old_type));
1838 new_type = build_exception_variant (new_type,
1839 TYPE_RAISES_EXCEPTIONS (old_type));
1840 TREE_TYPE (decl) = new_type;
1843 /* Check to see if the function just declared, as indicated in
1844 DECLARATOR, and in DECL, is a specialization of a function
1845 template. We may also discover that the declaration is an explicit
1846 instantiation at this point.
1848 Returns DECL, or an equivalent declaration that should be used
1849 instead if all goes well. Issues an error message if something is
1850 amiss. Returns error_mark_node if the error is not easily
1853 FLAGS is a bitmask consisting of the following flags:
1855 2: The function has a definition.
1856 4: The function is a friend.
1858 The TEMPLATE_COUNT is the number of references to qualifying
1859 template classes that appeared in the name of the function. For
1862 template <class T> struct S { void f(); };
1865 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1866 classes are not counted in the TEMPLATE_COUNT, so that in
1868 template <class T> struct S {};
1869 template <> struct S<int> { void f(); }
1870 template <> void S<int>::f();
1872 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1873 invalid; there should be no template <>.)
1875 If the function is a specialization, it is marked as such via
1876 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1877 is set up correctly, and it is added to the list of specializations
1878 for that template. */
1881 check_explicit_specialization (tree declarator,
1886 int have_def = flags & 2;
1887 int is_friend = flags & 4;
1888 int specialization = 0;
1889 int explicit_instantiation = 0;
1890 int member_specialization = 0;
1891 tree ctype = DECL_CLASS_CONTEXT (decl);
1892 tree dname = DECL_NAME (decl);
1897 if (!processing_specialization)
1900 tsk = tsk_excessive_parms;
1903 tsk = current_tmpl_spec_kind (template_count);
1908 if (processing_specialization)
1911 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1913 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1916 /* This could be something like:
1918 template <class T> void f(T);
1919 class S { friend void f<>(int); } */
1923 /* This case handles bogus declarations like template <>
1924 template <class T> void f<int>(); */
1926 error ("template-id %qD in declaration of primary template",
1933 case tsk_invalid_member_spec:
1934 /* The error has already been reported in
1935 check_specialization_scope. */
1936 return error_mark_node;
1938 case tsk_invalid_expl_inst:
1939 error ("template parameter list used in explicit instantiation");
1945 error ("definition provided for explicit instantiation");
1947 explicit_instantiation = 1;
1950 case tsk_excessive_parms:
1951 case tsk_insufficient_parms:
1952 if (tsk == tsk_excessive_parms)
1953 error ("too many template parameter lists in declaration of %qD",
1955 else if (template_header_count)
1956 error("too few template parameter lists in declaration of %qD", decl);
1958 error("explicit specialization of %qD must be introduced by "
1959 "%<template <>%>", decl);
1963 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1965 member_specialization = 1;
1971 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1973 /* This case handles bogus declarations like template <>
1974 template <class T> void f<int>(); */
1976 if (uses_template_parms (declarator))
1977 error ("function template partial specialization %qD "
1978 "is not allowed", declarator);
1980 error ("template-id %qD in declaration of primary template",
1985 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1986 /* This is a specialization of a member template, without
1987 specialization the containing class. Something like:
1989 template <class T> struct S {
1990 template <class U> void f (U);
1992 template <> template <class U> void S<int>::f(U) {}
1994 That's a specialization -- but of the entire template. */
2002 if (specialization || member_specialization)
2004 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2005 for (; t; t = TREE_CHAIN (t))
2006 if (TREE_PURPOSE (t))
2008 permerror (input_location,
2009 "default argument specified in explicit specialization");
2014 if (specialization || member_specialization || explicit_instantiation)
2016 tree tmpl = NULL_TREE;
2017 tree targs = NULL_TREE;
2019 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2020 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2024 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2029 /* If there is no class context, the explicit instantiation
2030 must be at namespace scope. */
2031 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2033 /* Find the namespace binding, using the declaration
2035 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2037 if (fns == error_mark_node || !is_overloaded_fn (fns))
2039 error ("%qD is not a template function", dname);
2040 fns = error_mark_node;
2044 tree fn = OVL_CURRENT (fns);
2045 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2046 CP_DECL_CONTEXT (fn)))
2047 error ("%qD is not declared in %qD",
2048 decl, current_namespace);
2052 declarator = lookup_template_function (fns, NULL_TREE);
2055 if (declarator == error_mark_node)
2056 return error_mark_node;
2058 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2060 if (!explicit_instantiation)
2061 /* A specialization in class scope. This is invalid,
2062 but the error will already have been flagged by
2063 check_specialization_scope. */
2064 return error_mark_node;
2067 /* It's not valid to write an explicit instantiation in
2070 class C { template void f(); }
2072 This case is caught by the parser. However, on
2075 template class C { void f(); };
2077 (which is invalid) we can get here. The error will be
2084 else if (ctype != NULL_TREE
2085 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2088 /* Find the list of functions in ctype that have the same
2089 name as the declared function. */
2090 tree name = TREE_OPERAND (declarator, 0);
2091 tree fns = NULL_TREE;
2094 if (constructor_name_p (name, ctype))
2096 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2098 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2099 : !CLASSTYPE_DESTRUCTORS (ctype))
2101 /* From [temp.expl.spec]:
2103 If such an explicit specialization for the member
2104 of a class template names an implicitly-declared
2105 special member function (clause _special_), the
2106 program is ill-formed.
2108 Similar language is found in [temp.explicit]. */
2109 error ("specialization of implicitly-declared special member function");
2110 return error_mark_node;
2113 name = is_constructor ? ctor_identifier : dtor_identifier;
2116 if (!DECL_CONV_FN_P (decl))
2118 idx = lookup_fnfields_1 (ctype, name);
2120 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2124 VEC(tree,gc) *methods;
2127 /* For a type-conversion operator, we cannot do a
2128 name-based lookup. We might be looking for `operator
2129 int' which will be a specialization of `operator T'.
2130 So, we find *all* the conversion operators, and then
2131 select from them. */
2134 methods = CLASSTYPE_METHOD_VEC (ctype);
2136 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2137 VEC_iterate (tree, methods, idx, ovl);
2140 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2141 /* There are no more conversion functions. */
2144 /* Glue all these conversion functions together
2145 with those we already have. */
2146 for (; ovl; ovl = OVL_NEXT (ovl))
2147 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2151 if (fns == NULL_TREE)
2153 error ("no member function %qD declared in %qT", name, ctype);
2154 return error_mark_node;
2157 TREE_OPERAND (declarator, 0) = fns;
2160 /* Figure out what exactly is being specialized at this point.
2161 Note that for an explicit instantiation, even one for a
2162 member function, we cannot tell apriori whether the
2163 instantiation is for a member template, or just a member
2164 function of a template class. Even if a member template is
2165 being instantiated, the member template arguments may be
2166 elided if they can be deduced from the rest of the
2168 tmpl = determine_specialization (declarator, decl,
2170 member_specialization,
2174 if (!tmpl || tmpl == error_mark_node)
2175 /* We couldn't figure out what this declaration was
2177 return error_mark_node;
2180 tree gen_tmpl = most_general_template (tmpl);
2182 if (explicit_instantiation)
2184 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2185 is done by do_decl_instantiation later. */
2187 int arg_depth = TMPL_ARGS_DEPTH (targs);
2188 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2190 if (arg_depth > parm_depth)
2192 /* If TMPL is not the most general template (for
2193 example, if TMPL is a friend template that is
2194 injected into namespace scope), then there will
2195 be too many levels of TARGS. Remove some of them
2200 new_targs = make_tree_vec (parm_depth);
2201 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2202 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2203 = TREE_VEC_ELT (targs, i);
2207 return instantiate_template (tmpl, targs, tf_error);
2210 /* If we thought that the DECL was a member function, but it
2211 turns out to be specializing a static member function,
2212 make DECL a static member function as well. */
2213 if (DECL_STATIC_FUNCTION_P (tmpl)
2214 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2215 revert_static_member_fn (decl);
2217 /* If this is a specialization of a member template of a
2218 template class, we want to return the TEMPLATE_DECL, not
2219 the specialization of it. */
2220 if (tsk == tsk_template)
2222 tree result = DECL_TEMPLATE_RESULT (tmpl);
2223 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2224 DECL_INITIAL (result) = NULL_TREE;
2228 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2229 DECL_SOURCE_LOCATION (result)
2230 = DECL_SOURCE_LOCATION (decl);
2231 /* We want to use the argument list specified in the
2232 definition, not in the original declaration. */
2233 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2234 for (parm = DECL_ARGUMENTS (result); parm;
2235 parm = TREE_CHAIN (parm))
2236 DECL_CONTEXT (parm) = result;
2241 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2242 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2244 /* Inherit default function arguments from the template
2245 DECL is specializing. */
2246 copy_default_args_to_explicit_spec (decl);
2248 /* This specialization has the same protection as the
2249 template it specializes. */
2250 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2251 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2253 /* 7.1.1-1 [dcl.stc]
2255 A storage-class-specifier shall not be specified in an
2256 explicit specialization...
2258 The parser rejects these, so unless action is taken here,
2259 explicit function specializations will always appear with
2262 The action recommended by the C++ CWG in response to C++
2263 defect report 605 is to make the storage class and linkage
2264 of the explicit specialization match the templated function:
2266 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2268 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2270 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2271 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2273 /* This specialization has the same linkage and visibility as
2274 the function template it specializes. */
2275 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2276 if (! TREE_PUBLIC (decl))
2278 DECL_INTERFACE_KNOWN (decl) = 1;
2279 DECL_NOT_REALLY_EXTERN (decl) = 1;
2281 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2282 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2284 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2285 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2289 /* If DECL is a friend declaration, declared using an
2290 unqualified name, the namespace associated with DECL may
2291 have been set incorrectly. For example, in:
2293 template <typename T> void f(T);
2295 struct S { friend void f<int>(int); }
2298 we will have set the DECL_CONTEXT for the friend
2299 declaration to N, rather than to the global namespace. */
2300 if (DECL_NAMESPACE_SCOPE_P (decl))
2301 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2303 if (is_friend && !have_def)
2304 /* This is not really a declaration of a specialization.
2305 It's just the name of an instantiation. But, it's not
2306 a request for an instantiation, either. */
2307 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2308 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2309 /* This is indeed a specialization. In case of constructors
2310 and destructors, we need in-charge and not-in-charge
2311 versions in V3 ABI. */
2312 clone_function_decl (decl, /*update_method_vec_p=*/0);
2314 /* Register this specialization so that we can find it
2316 decl = register_specialization (decl, gen_tmpl, targs, is_friend);
2323 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2324 parameters. These are represented in the same format used for
2325 DECL_TEMPLATE_PARMS. */
2328 comp_template_parms (const_tree parms1, const_tree parms2)
2333 if (parms1 == parms2)
2336 for (p1 = parms1, p2 = parms2;
2337 p1 != NULL_TREE && p2 != NULL_TREE;
2338 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2340 tree t1 = TREE_VALUE (p1);
2341 tree t2 = TREE_VALUE (p2);
2344 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2345 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2347 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2350 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2352 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2353 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2355 /* If either of the template parameters are invalid, assume
2356 they match for the sake of error recovery. */
2357 if (parm1 == error_mark_node || parm2 == error_mark_node)
2360 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2363 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2364 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2365 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2367 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2372 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2373 /* One set of parameters has more parameters lists than the
2380 /* Determine whether PARM is a parameter pack. */
2382 template_parameter_pack_p (const_tree parm)
2384 /* Determine if we have a non-type template parameter pack. */
2385 if (TREE_CODE (parm) == PARM_DECL)
2386 return (DECL_TEMPLATE_PARM_P (parm)
2387 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2389 /* If this is a list of template parameters, we could get a
2390 TYPE_DECL or a TEMPLATE_DECL. */
2391 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2392 parm = TREE_TYPE (parm);
2394 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2395 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2396 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2399 /* Determine whether ARGS describes a variadic template args list,
2400 i.e., one that is terminated by a template argument pack. */
2402 template_args_variadic_p (tree args)
2407 if (args == NULL_TREE)
2410 args = INNERMOST_TEMPLATE_ARGS (args);
2411 nargs = TREE_VEC_LENGTH (args);
2416 last_parm = TREE_VEC_ELT (args, nargs - 1);
2418 return ARGUMENT_PACK_P (last_parm);
2421 /* Generate a new name for the parameter pack name NAME (an
2422 IDENTIFIER_NODE) that incorporates its */
2424 make_ith_pack_parameter_name (tree name, int i)
2426 /* Munge the name to include the parameter index. */
2430 sprintf(numbuf, "%i", i);
2431 newname = (char*)alloca (IDENTIFIER_LENGTH (name) + strlen(numbuf) + 2);
2432 sprintf(newname, "%s#%i", IDENTIFIER_POINTER (name), i);
2433 return get_identifier (newname);
2436 /* Structure used to track the progress of find_parameter_packs_r. */
2437 struct find_parameter_pack_data
2439 /* TREE_LIST that will contain all of the parameter packs found by
2441 tree* parameter_packs;
2443 /* Set of AST nodes that have been visited by the traversal. */
2444 struct pointer_set_t *visited;
2447 /* Identifies all of the argument packs that occur in a template
2448 argument and appends them to the TREE_LIST inside DATA, which is a
2449 find_parameter_pack_data structure. This is a subroutine of
2450 make_pack_expansion and uses_parameter_packs. */
2452 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2455 struct find_parameter_pack_data* ppd =
2456 (struct find_parameter_pack_data*)data;
2457 bool parameter_pack_p = false;
2459 /* Identify whether this is a parameter pack or not. */
2460 switch (TREE_CODE (t))
2462 case TEMPLATE_PARM_INDEX:
2463 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2464 parameter_pack_p = true;
2467 case TEMPLATE_TYPE_PARM:
2468 case TEMPLATE_TEMPLATE_PARM:
2469 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2470 parameter_pack_p = true;
2474 if (FUNCTION_PARAMETER_PACK_P (t))
2476 /* We don't want to walk into the type of a PARM_DECL,
2477 because we don't want to see the type parameter pack. */
2479 parameter_pack_p = true;
2484 /* Not a parameter pack. */
2488 if (parameter_pack_p)
2490 /* Add this parameter pack to the list. */
2491 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2495 cp_walk_tree (&TYPE_CONTEXT (t),
2496 &find_parameter_packs_r, ppd, ppd->visited);
2498 /* This switch statement will return immediately if we don't find a
2500 switch (TREE_CODE (t))
2502 case TEMPLATE_PARM_INDEX:
2505 case BOUND_TEMPLATE_TEMPLATE_PARM:
2506 /* Check the template itself. */
2507 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
2508 &find_parameter_packs_r, ppd, ppd->visited);
2509 /* Check the template arguments. */
2510 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2515 case TEMPLATE_TYPE_PARM:
2516 case TEMPLATE_TEMPLATE_PARM:
2523 if (TYPE_PTRMEMFUNC_P (t))
2529 if (TYPE_TEMPLATE_INFO (t))
2530 cp_walk_tree (&TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
2531 &find_parameter_packs_r, ppd, ppd->visited);
2537 cp_walk_tree (&TREE_TYPE (t),
2538 &find_parameter_packs_r, ppd, ppd->visited);
2542 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
2547 case TYPE_PACK_EXPANSION:
2548 case EXPR_PACK_EXPANSION:
2553 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
2558 case IDENTIFIER_NODE:
2559 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
2571 /* Determines if the expression or type T uses any parameter packs. */
2573 uses_parameter_packs (tree t)
2575 tree parameter_packs = NULL_TREE;
2576 struct find_parameter_pack_data ppd;
2577 ppd.parameter_packs = ¶meter_packs;
2578 ppd.visited = pointer_set_create ();
2579 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
2580 pointer_set_destroy (ppd.visited);
2581 return parameter_packs != NULL_TREE;
2584 /* Turn ARG, which may be an expression, type, or a TREE_LIST
2585 representation a base-class initializer into a parameter pack
2586 expansion. If all goes well, the resulting node will be an
2587 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
2590 make_pack_expansion (tree arg)
2593 tree parameter_packs = NULL_TREE;
2594 bool for_types = false;
2595 struct find_parameter_pack_data ppd;
2597 if (!arg || arg == error_mark_node)
2600 if (TREE_CODE (arg) == TREE_LIST)
2602 /* The only time we will see a TREE_LIST here is for a base
2603 class initializer. In this case, the TREE_PURPOSE will be a
2604 _TYPE node (representing the base class expansion we're
2605 initializing) and the TREE_VALUE will be a TREE_LIST
2606 containing the initialization arguments.
2608 The resulting expansion looks somewhat different from most
2609 expansions. Rather than returning just one _EXPANSION, we
2610 return a TREE_LIST whose TREE_PURPOSE is a
2611 TYPE_PACK_EXPANSION containing the bases that will be
2612 initialized. The TREE_VALUE will be identical to the
2613 original TREE_VALUE, which is a list of arguments that will
2614 be passed to each base. We do not introduce any new pack
2615 expansion nodes into the TREE_VALUE (although it is possible
2616 that some already exist), because the TREE_PURPOSE and
2617 TREE_VALUE all need to be expanded together with the same
2618 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
2619 resulting TREE_PURPOSE will mention the parameter packs in
2620 both the bases and the arguments to the bases. */
2623 tree parameter_packs = NULL_TREE;
2625 /* Determine which parameter packs will be used by the base
2627 ppd.visited = pointer_set_create ();
2628 ppd.parameter_packs = ¶meter_packs;
2629 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
2632 if (parameter_packs == NULL_TREE)
2634 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
2635 pointer_set_destroy (ppd.visited);
2636 return error_mark_node;
2639 if (TREE_VALUE (arg) != void_type_node)
2641 /* Collect the sets of parameter packs used in each of the
2642 initialization arguments. */
2643 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
2645 /* Determine which parameter packs will be expanded in this
2647 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
2652 pointer_set_destroy (ppd.visited);
2654 /* Create the pack expansion type for the base type. */
2655 purpose = make_node (TYPE_PACK_EXPANSION);
2656 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
2657 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
2659 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2660 they will rarely be compared to anything. */
2661 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
2663 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
2666 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
2669 /* Build the PACK_EXPANSION_* node. */
2670 result = make_node (for_types ? TYPE_PACK_EXPANSION : EXPR_PACK_EXPANSION);
2671 SET_PACK_EXPANSION_PATTERN (result, arg);
2672 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
2674 /* Propagate type and const-expression information. */
2675 TREE_TYPE (result) = TREE_TYPE (arg);
2676 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
2679 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2680 they will rarely be compared to anything. */
2681 SET_TYPE_STRUCTURAL_EQUALITY (result);
2683 /* Determine which parameter packs will be expanded. */
2684 ppd.parameter_packs = ¶meter_packs;
2685 ppd.visited = pointer_set_create ();
2686 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
2687 pointer_set_destroy (ppd.visited);
2689 /* Make sure we found some parameter packs. */
2690 if (parameter_packs == NULL_TREE)
2693 error ("expansion pattern %<%T%> contains no argument packs", arg);
2695 error ("expansion pattern %<%E%> contains no argument packs", arg);
2696 return error_mark_node;
2698 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
2703 /* Checks T for any "bare" parameter packs, which have not yet been
2704 expanded, and issues an error if any are found. This operation can
2705 only be done on full expressions or types (e.g., an expression
2706 statement, "if" condition, etc.), because we could have expressions like:
2708 foo(f(g(h(args)))...)
2710 where "args" is a parameter pack. check_for_bare_parameter_packs
2711 should not be called for the subexpressions args, h(args),
2712 g(h(args)), or f(g(h(args))), because we would produce erroneous
2715 Returns TRUE and emits an error if there were bare parameter packs,
2716 returns FALSE otherwise. */
2718 check_for_bare_parameter_packs (tree t)
2720 tree parameter_packs = NULL_TREE;
2721 struct find_parameter_pack_data ppd;
2723 if (!processing_template_decl || !t || t == error_mark_node)
2726 if (TREE_CODE (t) == TYPE_DECL)
2729 ppd.parameter_packs = ¶meter_packs;
2730 ppd.visited = pointer_set_create ();
2731 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
2732 pointer_set_destroy (ppd.visited);
2734 if (parameter_packs)
2736 error ("parameter packs not expanded with %<...%>:");
2737 while (parameter_packs)
2739 tree pack = TREE_VALUE (parameter_packs);
2740 tree name = NULL_TREE;
2742 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
2743 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
2744 name = TYPE_NAME (pack);
2745 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
2746 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
2748 name = DECL_NAME (pack);
2751 inform (input_location, " %qD", name);
2753 inform (input_location, " <anonymous>");
2755 parameter_packs = TREE_CHAIN (parameter_packs);
2764 /* Expand any parameter packs that occur in the template arguments in
2767 expand_template_argument_pack (tree args)
2769 tree result_args = NULL_TREE;
2770 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
2771 int num_result_args = -1;
2773 /* First, determine if we need to expand anything, and the number of
2774 slots we'll need. */
2775 for (in_arg = 0; in_arg < nargs; ++in_arg)
2777 tree arg = TREE_VEC_ELT (args, in_arg);
2778 if (ARGUMENT_PACK_P (arg))
2780 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
2781 if (num_result_args < 0)
2782 num_result_args = in_arg + num_packed;
2784 num_result_args += num_packed;
2788 if (num_result_args >= 0)
2793 /* If no expansion is necessary, we're done. */
2794 if (num_result_args < 0)
2797 /* Expand arguments. */
2798 result_args = make_tree_vec (num_result_args);
2799 for (in_arg = 0; in_arg < nargs; ++in_arg)
2801 tree arg = TREE_VEC_ELT (args, in_arg);
2802 if (ARGUMENT_PACK_P (arg))
2804 tree packed = ARGUMENT_PACK_ARGS (arg);
2805 int i, num_packed = TREE_VEC_LENGTH (packed);
2806 for (i = 0; i < num_packed; ++i, ++out_arg)
2807 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
2811 TREE_VEC_ELT (result_args, out_arg) = arg;
2819 /* Checks if DECL shadows a template parameter.
2821 [temp.local]: A template-parameter shall not be redeclared within its
2822 scope (including nested scopes).
2824 Emits an error and returns TRUE if the DECL shadows a parameter,
2825 returns FALSE otherwise. */
2828 check_template_shadow (tree decl)
2832 /* If we're not in a template, we can't possibly shadow a template
2834 if (!current_template_parms)
2837 /* Figure out what we're shadowing. */
2838 if (TREE_CODE (decl) == OVERLOAD)
2839 decl = OVL_CURRENT (decl);
2840 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
2842 /* If there's no previous binding for this name, we're not shadowing
2843 anything, let alone a template parameter. */
2847 /* If we're not shadowing a template parameter, we're done. Note
2848 that OLDDECL might be an OVERLOAD (or perhaps even an
2849 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2851 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2854 /* We check for decl != olddecl to avoid bogus errors for using a
2855 name inside a class. We check TPFI to avoid duplicate errors for
2856 inline member templates. */
2858 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2861 error ("declaration of %q+#D", decl);
2862 error (" shadows template parm %q+#D", olddecl);
2866 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2867 ORIG_LEVEL, DECL, and TYPE. */
2870 build_template_parm_index (int index,
2876 tree t = make_node (TEMPLATE_PARM_INDEX);
2877 TEMPLATE_PARM_IDX (t) = index;
2878 TEMPLATE_PARM_LEVEL (t) = level;
2879 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2880 TEMPLATE_PARM_DECL (t) = decl;
2881 TREE_TYPE (t) = type;
2882 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2883 TREE_READONLY (t) = TREE_READONLY (decl);
2888 /* Find the canonical type parameter for the given template type
2889 parameter. Returns the canonical type parameter, which may be TYPE
2890 if no such parameter existed. */
2892 canonical_type_parameter (tree type)
2895 int idx = TEMPLATE_TYPE_IDX (type);
2896 if (!canonical_template_parms)
2897 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
2899 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
2900 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
2902 list = VEC_index (tree, canonical_template_parms, idx);
2903 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
2904 list = TREE_CHAIN (list);
2907 return TREE_VALUE (list);
2910 VEC_replace(tree, canonical_template_parms, idx,
2911 tree_cons (NULL_TREE, type,
2912 VEC_index (tree, canonical_template_parms, idx)));
2917 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2918 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2919 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2920 new one is created. */
2923 reduce_template_parm_level (tree index, tree type, int levels, tree args,
2924 tsubst_flags_t complain)
2926 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2927 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2928 != TEMPLATE_PARM_LEVEL (index) - levels))
2930 tree orig_decl = TEMPLATE_PARM_DECL (index);
2933 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2934 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2935 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2936 DECL_ARTIFICIAL (decl) = 1;
2937 SET_DECL_TEMPLATE_PARM_P (decl);
2939 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2940 TEMPLATE_PARM_LEVEL (index) - levels,
2941 TEMPLATE_PARM_ORIG_LEVEL (index),
2943 TEMPLATE_PARM_DESCENDANTS (index) = t;
2944 TEMPLATE_PARM_PARAMETER_PACK (t)
2945 = TEMPLATE_PARM_PARAMETER_PACK (index);
2947 /* Template template parameters need this. */
2948 if (TREE_CODE (decl) == TEMPLATE_DECL)
2949 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
2950 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
2954 return TEMPLATE_PARM_DESCENDANTS (index);
2957 /* Process information from new template parameter PARM and append it to the
2958 LIST being built. This new parameter is a non-type parameter iff
2959 IS_NON_TYPE is true. This new parameter is a parameter
2960 pack iff IS_PARAMETER_PACK is true. */
2963 process_template_parm (tree list, tree parm, bool is_non_type,
2964 bool is_parameter_pack)
2971 gcc_assert (TREE_CODE (parm) == TREE_LIST);
2972 defval = TREE_PURPOSE (parm);
2976 tree p = tree_last (list);
2978 if (p && TREE_VALUE (p) != error_mark_node)
2981 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2982 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2984 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2994 parm = TREE_VALUE (parm);
2996 SET_DECL_TEMPLATE_PARM_P (parm);
2998 if (TREE_TYPE (parm) == error_mark_node)
3000 err_parm_list = build_tree_list (defval, parm);
3001 TREE_VALUE (err_parm_list) = error_mark_node;
3002 return chainon (list, err_parm_list);
3008 The top-level cv-qualifiers on the template-parameter are
3009 ignored when determining its type. */
3010 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3011 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3013 err_parm_list = build_tree_list (defval, parm);
3014 TREE_VALUE (err_parm_list) = error_mark_node;
3015 return chainon (list, err_parm_list);
3018 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3020 /* This template parameter is not a parameter pack, but it
3021 should be. Complain about "bare" parameter packs. */
3022 check_for_bare_parameter_packs (TREE_TYPE (parm));
3024 /* Recover by calling this a parameter pack. */
3025 is_parameter_pack = true;
3029 /* A template parameter is not modifiable. */
3030 TREE_CONSTANT (parm) = 1;
3031 TREE_READONLY (parm) = 1;
3032 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3033 TREE_CONSTANT (decl) = 1;
3034 TREE_READONLY (decl) = 1;
3035 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3036 = build_template_parm_index (idx, processing_template_decl,
3037 processing_template_decl,
3038 decl, TREE_TYPE (parm));
3040 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3041 = is_parameter_pack;
3046 parm = TREE_VALUE (TREE_VALUE (parm));
3048 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3050 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3051 /* This is for distinguishing between real templates and template
3052 template parameters */
3053 TREE_TYPE (parm) = t;
3054 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3059 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3060 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3061 decl = build_decl (TYPE_DECL, parm, t);
3064 TYPE_NAME (t) = decl;
3065 TYPE_STUB_DECL (t) = decl;
3067 TEMPLATE_TYPE_PARM_INDEX (t)
3068 = build_template_parm_index (idx, processing_template_decl,
3069 processing_template_decl,
3070 decl, TREE_TYPE (parm));
3071 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3072 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3074 DECL_ARTIFICIAL (decl) = 1;
3075 SET_DECL_TEMPLATE_PARM_P (decl);
3077 parm = build_tree_list (defval, parm);
3078 return chainon (list, parm);
3081 /* The end of a template parameter list has been reached. Process the
3082 tree list into a parameter vector, converting each parameter into a more
3083 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3087 end_template_parm_list (tree parms)
3091 tree saved_parmlist = make_tree_vec (list_length (parms));
3093 current_template_parms
3094 = tree_cons (size_int (processing_template_decl),
3095 saved_parmlist, current_template_parms);
3097 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3099 next = TREE_CHAIN (parm);
3100 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3101 TREE_CHAIN (parm) = NULL_TREE;
3104 --processing_template_parmlist;
3106 return saved_parmlist;
3109 /* end_template_decl is called after a template declaration is seen. */
3112 end_template_decl (void)
3114 reset_specialization ();
3116 if (! processing_template_decl)
3119 /* This matches the pushlevel in begin_template_parm_list. */
3122 --processing_template_decl;
3123 current_template_parms = TREE_CHAIN (current_template_parms);
3126 /* Within the declaration of a template, return all levels of template
3127 parameters that apply. The template parameters are represented as
3128 a TREE_VEC, in the form documented in cp-tree.h for template
3132 current_template_args (void)
3135 tree args = NULL_TREE;
3136 int length = TMPL_PARMS_DEPTH (current_template_parms);
3139 /* If there is only one level of template parameters, we do not
3140 create a TREE_VEC of TREE_VECs. Instead, we return a single
3141 TREE_VEC containing the arguments. */
3143 args = make_tree_vec (length);
3145 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3147 tree a = copy_node (TREE_VALUE (header));
3150 TREE_TYPE (a) = NULL_TREE;
3151 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3153 tree t = TREE_VEC_ELT (a, i);
3155 /* T will be a list if we are called from within a
3156 begin/end_template_parm_list pair, but a vector directly
3157 if within a begin/end_member_template_processing pair. */
3158 if (TREE_CODE (t) == TREE_LIST)
3162 if (!error_operand_p (t))
3164 if (TREE_CODE (t) == TYPE_DECL
3165 || TREE_CODE (t) == TEMPLATE_DECL)
3169 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3171 /* Turn this argument into a TYPE_ARGUMENT_PACK
3172 with a single element, which expands T. */
3173 tree vec = make_tree_vec (1);
3174 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3176 t = make_node (TYPE_ARGUMENT_PACK);
3177 SET_ARGUMENT_PACK_ARGS (t, vec);
3182 t = DECL_INITIAL (t);
3184 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3186 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3187 with a single element, which expands T. */
3188 tree vec = make_tree_vec (1);
3189 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3190 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3192 t = make_node (NONTYPE_ARGUMENT_PACK);
3193 SET_ARGUMENT_PACK_ARGS (t, vec);
3194 TREE_TYPE (t) = type;
3197 TREE_VEC_ELT (a, i) = t;
3203 TREE_VEC_ELT (args, --l) = a;
3211 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3212 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3213 a member template. Used by push_template_decl below. */
3216 build_template_decl (tree decl, tree parms, bool member_template_p)
3218 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3219 DECL_TEMPLATE_PARMS (tmpl) = parms;
3220 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3221 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3222 if (DECL_LANG_SPECIFIC (decl))
3224 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
3225 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
3226 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
3227 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
3228 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
3229 if (DECL_OVERLOADED_OPERATOR_P (decl))
3230 SET_OVERLOADED_OPERATOR_CODE (tmpl,
3231 DECL_OVERLOADED_OPERATOR_P (decl));
3237 struct template_parm_data
3239 /* The level of the template parameters we are currently
3243 /* The index of the specialization argument we are currently
3247 /* An array whose size is the number of template parameters. The
3248 elements are nonzero if the parameter has been used in any one
3249 of the arguments processed so far. */
3252 /* An array whose size is the number of template arguments. The
3253 elements are nonzero if the argument makes use of template
3254 parameters of this level. */
3255 int* arg_uses_template_parms;
3258 /* Subroutine of push_template_decl used to see if each template
3259 parameter in a partial specialization is used in the explicit
3260 argument list. If T is of the LEVEL given in DATA (which is
3261 treated as a template_parm_data*), then DATA->PARMS is marked
3265 mark_template_parm (tree t, void* data)
3269 struct template_parm_data* tpd = (struct template_parm_data*) data;
3271 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3273 level = TEMPLATE_PARM_LEVEL (t);
3274 idx = TEMPLATE_PARM_IDX (t);
3278 level = TEMPLATE_TYPE_LEVEL (t);
3279 idx = TEMPLATE_TYPE_IDX (t);
3282 if (level == tpd->level)
3284 tpd->parms[idx] = 1;
3285 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3288 /* Return zero so that for_each_template_parm will continue the
3289 traversal of the tree; we want to mark *every* template parm. */
3293 /* Process the partial specialization DECL. */
3296 process_partial_specialization (tree decl)
3298 tree type = TREE_TYPE (decl);
3299 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3300 tree specargs = CLASSTYPE_TI_ARGS (type);
3301 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3302 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3304 int nargs = TREE_VEC_LENGTH (inner_args);
3307 int did_error_intro = 0;
3308 struct template_parm_data tpd;
3309 struct template_parm_data tpd2;
3311 gcc_assert (current_template_parms);
3313 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3314 ntparms = TREE_VEC_LENGTH (inner_parms);
3316 /* We check that each of the template parameters given in the
3317 partial specialization is used in the argument list to the
3318 specialization. For example:
3320 template <class T> struct S;
3321 template <class T> struct S<T*>;
3323 The second declaration is OK because `T*' uses the template
3324 parameter T, whereas
3326 template <class T> struct S<int>;
3328 is no good. Even trickier is:
3339 The S2<T> declaration is actually invalid; it is a
3340 full-specialization. Of course,
3343 struct S2<T (*)(U)>;
3345 or some such would have been OK. */
3346 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3347 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
3348 memset (tpd.parms, 0, sizeof (int) * ntparms);
3350 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
3351 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3352 for (i = 0; i < nargs; ++i)
3354 tpd.current_arg = i;
3355 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3356 &mark_template_parm,
3359 /*include_nondeduced_p=*/false);
3361 for (i = 0; i < ntparms; ++i)
3362 if (tpd.parms[i] == 0)
3364 /* One of the template parms was not used in the
3366 if (!did_error_intro)
3368 error ("template parameters not used in partial specialization:");
3369 did_error_intro = 1;
3372 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3375 /* [temp.class.spec]
3377 The argument list of the specialization shall not be identical to
3378 the implicit argument list of the primary template. */
3379 if (comp_template_args
3381 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3383 error ("partial specialization %qT does not specialize any template arguments", type);
3385 /* [temp.class.spec]
3387 A partially specialized non-type argument expression shall not
3388 involve template parameters of the partial specialization except
3389 when the argument expression is a simple identifier.
3391 The type of a template parameter corresponding to a specialized
3392 non-type argument shall not be dependent on a parameter of the
3395 Also, we verify that pack expansions only occur at the
3396 end of the argument list. */
3397 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3399 for (i = 0; i < nargs; ++i)
3401 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3402 tree arg = TREE_VEC_ELT (inner_args, i);
3403 tree packed_args = NULL_TREE;
3406 if (ARGUMENT_PACK_P (arg))
3408 /* Extract the arguments from the argument pack. We'll be
3409 iterating over these in the following loop. */
3410 packed_args = ARGUMENT_PACK_ARGS (arg);
3411 len = TREE_VEC_LENGTH (packed_args);
3414 for (j = 0; j < len; j++)
3417 /* Get the Jth argument in the parameter pack. */
3418 arg = TREE_VEC_ELT (packed_args, j);
3420 if (PACK_EXPANSION_P (arg))
3422 /* Pack expansions must come at the end of the
3424 if ((packed_args && j < len - 1)
3425 || (!packed_args && i < nargs - 1))
3427 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3428 error ("parameter pack argument %qE must be at the end of the template argument list", arg);
3430 error ("parameter pack argument %qT must be at the end of the template argument list", arg);
3433 TREE_VEC_ELT (packed_args, j) = error_mark_node;
3437 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3438 /* We only care about the pattern. */
3439 arg = PACK_EXPANSION_PATTERN (arg);
3441 if (/* These first two lines are the `non-type' bit. */
3443 && TREE_CODE (arg) != TEMPLATE_DECL
3444 /* This next line is the `argument expression is not just a
3445 simple identifier' condition and also the `specialized
3446 non-type argument' bit. */
3447 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3449 if ((!packed_args && tpd.arg_uses_template_parms[i])
3450 || (packed_args && uses_template_parms (arg)))
3451 error ("template argument %qE involves template parameter(s)",
3455 /* Look at the corresponding template parameter,
3456 marking which template parameters its type depends
3458 tree type = TREE_TYPE (parm);
3462 /* We haven't yet initialized TPD2. Do so now. */
3463 tpd2.arg_uses_template_parms
3464 = (int *) alloca (sizeof (int) * nargs);
3465 /* The number of parameters here is the number in the
3466 main template, which, as checked in the assertion
3468 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
3470 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
3473 /* Mark the template parameters. But this time, we're
3474 looking for the template parameters of the main
3475 template, not in the specialization. */
3476 tpd2.current_arg = i;
3477 tpd2.arg_uses_template_parms[i] = 0;
3478 memset (tpd2.parms, 0, sizeof (int) * nargs);
3479 for_each_template_parm (type,
3480 &mark_template_parm,
3483 /*include_nondeduced_p=*/false);
3485 if (tpd2.arg_uses_template_parms [i])
3487 /* The type depended on some template parameters.
3488 If they are fully specialized in the
3489 specialization, that's OK. */
3491 for (j = 0; j < nargs; ++j)
3492 if (tpd2.parms[j] != 0
3493 && tpd.arg_uses_template_parms [j])
3495 error ("type %qT of template argument %qE depends "
3496 "on template parameter(s)",
3507 if (retrieve_specialization (maintmpl, specargs,
3508 /*class_specializations_p=*/true))
3509 /* We've already got this specialization. */
3512 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
3513 = tree_cons (specargs, inner_parms,
3514 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
3515 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
3519 /* Check that a template declaration's use of default arguments and
3520 parameter packs is not invalid. Here, PARMS are the template
3521 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
3522 a primary template. IS_PARTIAL is nonzero if DECL is a partial
3526 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3527 declaration (but not a definition); 1 indicates a declaration, 2
3528 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3529 emitted for extraneous default arguments.
3531 Returns TRUE if there were no errors found, FALSE otherwise. */
3534 check_default_tmpl_args (tree decl, tree parms, int is_primary,
3535 int is_partial, int is_friend_decl)
3538 int last_level_to_check;
3540 bool no_errors = true;
3544 A default template-argument shall not be specified in a
3545 function template declaration or a function template definition, nor
3546 in the template-parameter-list of the definition of a member of a
3549 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
3550 /* You can't have a function template declaration in a local
3551 scope, nor you can you define a member of a class template in a
3555 if (current_class_type
3556 && !TYPE_BEING_DEFINED (current_class_type)
3557 && DECL_LANG_SPECIFIC (decl)
3558 /* If this is either a friend defined in the scope of the class
3559 or a member function. */
3560 && (DECL_FUNCTION_MEMBER_P (decl)
3561 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
3562 : DECL_FRIEND_CONTEXT (decl)
3563 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
3565 /* And, if it was a member function, it really was defined in
3566 the scope of the class. */
3567 && (!DECL_FUNCTION_MEMBER_P (decl)
3568 || DECL_INITIALIZED_IN_CLASS_P (decl)))
3569 /* We already checked these parameters when the template was
3570 declared, so there's no need to do it again now. This function
3571 was defined in class scope, but we're processing it's body now
3572 that the class is complete. */
3575 /* Core issue 226 (C++0x only): the following only applies to class
3577 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
3581 If a template-parameter has a default template-argument, all
3582 subsequent template-parameters shall have a default
3583 template-argument supplied. */
3584 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
3586 tree inner_parms = TREE_VALUE (parm_level);
3587 int ntparms = TREE_VEC_LENGTH (inner_parms);
3588 int seen_def_arg_p = 0;
3591 for (i = 0; i < ntparms; ++i)
3593 tree parm = TREE_VEC_ELT (inner_parms, i);
3595 if (parm == error_mark_node)
3598 if (TREE_PURPOSE (parm))
3600 else if (seen_def_arg_p
3601 && !template_parameter_pack_p (TREE_VALUE (parm)))
3603 error ("no default argument for %qD", TREE_VALUE (parm));
3604 /* For better subsequent error-recovery, we indicate that
3605 there should have been a default argument. */
3606 TREE_PURPOSE (parm) = error_mark_node;
3612 /* Don't complain about an enclosing partial
3614 && parm_level == parms
3615 && TREE_CODE (decl) == TYPE_DECL
3617 && template_parameter_pack_p (TREE_VALUE (parm)))
3619 /* A primary class template can only have one
3620 parameter pack, at the end of the template
3623 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
3624 error ("parameter pack %qE must be at the end of the"
3625 " template parameter list", TREE_VALUE (parm));
3627 error ("parameter pack %qT must be at the end of the"
3628 " template parameter list",
3629 TREE_TYPE (TREE_VALUE (parm)));
3631 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
3639 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
3643 /* For an ordinary class template, default template arguments are
3644 allowed at the innermost level, e.g.:
3645 template <class T = int>
3647 but, in a partial specialization, they're not allowed even
3648 there, as we have in [temp.class.spec]:
3650 The template parameter list of a specialization shall not
3651 contain default template argument values.
3653 So, for a partial specialization, or for a function template
3654 (in C++98/C++03), we look at all of them. */
3657 /* But, for a primary class template that is not a partial
3658 specialization we look at all template parameters except the
3660 parms = TREE_CHAIN (parms);
3662 /* Figure out what error message to issue. */
3663 if (is_friend_decl == 2)
3664 msg = "default template arguments may not be used in function template friend re-declaration";
3665 else if (is_friend_decl)
3666 msg = "default template arguments may not be used in function template friend declarations";
3667 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
3668 msg = "default template arguments may not be used in function templates";
3669 else if (is_partial)
3670 msg = "default template arguments may not be used in partial specializations";
3672 msg = "default argument for template parameter for class enclosing %qD";
3674 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
3675 /* If we're inside a class definition, there's no need to
3676 examine the parameters to the class itself. On the one
3677 hand, they will be checked when the class is defined, and,
3678 on the other, default arguments are valid in things like:
3679 template <class T = double>
3680 struct S { template <class U> void f(U); };
3681 Here the default argument for `S' has no bearing on the
3682 declaration of `f'. */
3683 last_level_to_check = template_class_depth (current_class_type) + 1;
3685 /* Check everything. */
3686 last_level_to_check = 0;
3688 for (parm_level = parms;
3689 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
3690 parm_level = TREE_CHAIN (parm_level))
3692 tree inner_parms = TREE_VALUE (parm_level);
3696 ntparms = TREE_VEC_LENGTH (inner_parms);
3697 for (i = 0; i < ntparms; ++i)
3699 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
3702 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
3707 if (is_friend_decl == 2)
3714 /* Clear out the default argument so that we are not
3716 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
3720 /* At this point, if we're still interested in issuing messages,
3721 they must apply to classes surrounding the object declared. */
3723 msg = "default argument for template parameter for class enclosing %qD";
3729 /* Worker for push_template_decl_real, called via
3730 for_each_template_parm. DATA is really an int, indicating the
3731 level of the parameters we are interested in. If T is a template
3732 parameter of that level, return nonzero. */
3735 template_parm_this_level_p (tree t, void* data)
3737 int this_level = *(int *)data;
3740 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3741 level = TEMPLATE_PARM_LEVEL (t);
3743 level = TEMPLATE_TYPE_LEVEL (t);
3744 return level == this_level;
3747 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
3748 parameters given by current_template_args, or reuses a
3749 previously existing one, if appropriate. Returns the DECL, or an
3750 equivalent one, if it is replaced via a call to duplicate_decls.
3752 If IS_FRIEND is true, DECL is a friend declaration. */
3755 push_template_decl_real (tree decl, bool is_friend)
3763 int new_template_p = 0;
3764 /* True if the template is a member template, in the sense of
3766 bool member_template_p = false;
3768 if (decl == error_mark_node || !current_template_parms)
3769 return error_mark_node;
3771 /* See if this is a partial specialization. */
3772 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
3773 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3774 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
3776 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
3780 /* For a friend, we want the context of the friend function, not
3781 the type of which it is a friend. */
3782 ctx = DECL_CONTEXT (decl);
3783 else if (CP_DECL_CONTEXT (decl)
3784 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
3785 /* In the case of a virtual function, we want the class in which
3787 ctx = CP_DECL_CONTEXT (decl);
3789 /* Otherwise, if we're currently defining some class, the DECL
3790 is assumed to be a member of the class. */
3791 ctx = current_scope ();
3793 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
3796 if (!DECL_CONTEXT (decl))
3797 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
3799 /* See if this is a primary template. */
3800 if (is_friend && ctx)
3801 /* A friend template that specifies a class context, i.e.
3802 template <typename T> friend void A<T>::f();
3806 primary = template_parm_scope_p ();
3810 if (DECL_CLASS_SCOPE_P (decl))
3811 member_template_p = true;
3812 if (TREE_CODE (decl) == TYPE_DECL
3813 && ANON_AGGRNAME_P (DECL_NAME (decl)))
3815 error ("template class without a name");
3816 return error_mark_node;
3818 else if (TREE_CODE (decl) == FUNCTION_DECL)
3820 if (DECL_DESTRUCTOR_P (decl))
3824 A destructor shall not be a member template. */
3825 error ("destructor %qD declared as member template", decl);
3826 return error_mark_node;
3828 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
3829 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
3830 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
3831 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
3832 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
3833 == void_list_node)))
3835 /* [basic.stc.dynamic.allocation]
3837 An allocation function can be a function
3838 template. ... Template allocation functions shall
3839 have two or more parameters. */
3840 error ("invalid template declaration of %qD", decl);
3841 return error_mark_node;
3844 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3845 && CLASS_TYPE_P (TREE_TYPE (decl)))
3849 error ("template declaration of %q#D", decl);
3850 return error_mark_node;
3854 /* Check to see that the rules regarding the use of default
3855 arguments are not being violated. */
3856 check_default_tmpl_args (decl, current_template_parms,
3857 primary, is_partial, /*is_friend_decl=*/0);
3859 /* Ensure that there are no parameter packs in the type of this
3860 declaration that have not been expanded. */
3861 if (TREE_CODE (decl) == FUNCTION_DECL)
3863 /* Check each of the arguments individually to see if there are
3864 any bare parameter packs. */
3865 tree type = TREE_TYPE (decl);
3866 tree arg = DECL_ARGUMENTS (decl);
3867 tree argtype = TYPE_ARG_TYPES (type);
3869 while (arg && argtype)
3871 if (!FUNCTION_PARAMETER_PACK_P (arg)
3872 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
3874 /* This is a PARM_DECL that contains unexpanded parameter
3875 packs. We have already complained about this in the
3876 check_for_bare_parameter_packs call, so just replace
3877 these types with ERROR_MARK_NODE. */
3878 TREE_TYPE (arg) = error_mark_node;
3879 TREE_VALUE (argtype) = error_mark_node;
3882 arg = TREE_CHAIN (arg);
3883 argtype = TREE_CHAIN (argtype);
3886 /* Check for bare parameter packs in the return type and the
3887 exception specifiers. */
3888 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
3889 /* Errors were already issued, set return type to int
3890 as the frontend doesn't expect error_mark_node as
3892 TREE_TYPE (type) = integer_type_node;
3893 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
3894 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
3896 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3898 TREE_TYPE (decl) = error_mark_node;
3899 return error_mark_node;
3903 return process_partial_specialization (decl);
3905 args = current_template_args ();
3908 || TREE_CODE (ctx) == FUNCTION_DECL
3909 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
3910 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
3912 if (DECL_LANG_SPECIFIC (decl)
3913 && DECL_TEMPLATE_INFO (decl)
3914 && DECL_TI_TEMPLATE (decl))
3915 tmpl = DECL_TI_TEMPLATE (decl);
3916 /* If DECL is a TYPE_DECL for a class-template, then there won't
3917 be DECL_LANG_SPECIFIC. The information equivalent to
3918 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3919 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3920 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3921 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3923 /* Since a template declaration already existed for this
3924 class-type, we must be redeclaring it here. Make sure
3925 that the redeclaration is valid. */
3926 redeclare_class_template (TREE_TYPE (decl),
3927 current_template_parms);
3928 /* We don't need to create a new TEMPLATE_DECL; just use the
3929 one we already had. */
3930 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3934 tmpl = build_template_decl (decl, current_template_parms,
3938 if (DECL_LANG_SPECIFIC (decl)
3939 && DECL_TEMPLATE_SPECIALIZATION (decl))
3941 /* A specialization of a member template of a template
3943 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3944 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
3945 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3951 tree a, t, current, parms;
3953 tree tinfo = get_template_info (decl);
3957 error ("template definition of non-template %q#D", decl);
3958 return error_mark_node;
3961 tmpl = TI_TEMPLATE (tinfo);
3963 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3964 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
3965 && DECL_TEMPLATE_SPECIALIZATION (decl)
3966 && DECL_MEMBER_TEMPLATE_P (tmpl))
3970 /* The declaration is a specialization of a member
3971 template, declared outside the class. Therefore, the
3972 innermost template arguments will be NULL, so we
3973 replace them with the arguments determined by the
3974 earlier call to check_explicit_specialization. */
3975 args = DECL_TI_ARGS (decl);
3978 = build_template_decl (decl, current_template_parms,
3980 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3981 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3982 DECL_TI_TEMPLATE (decl) = new_tmpl;
3983 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3984 DECL_TEMPLATE_INFO (new_tmpl)
3985 = tree_cons (tmpl, args, NULL_TREE);
3987 register_specialization (new_tmpl,
3988 most_general_template (tmpl),
3994 /* Make sure the template headers we got make sense. */
3996 parms = DECL_TEMPLATE_PARMS (tmpl);
3997 i = TMPL_PARMS_DEPTH (parms);
3998 if (TMPL_ARGS_DEPTH (args) != i)
4000 error ("expected %d levels of template parms for %q#D, got %d",
4001 i, decl, TMPL_ARGS_DEPTH (args));
4004 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4006 a = TMPL_ARGS_LEVEL (args, i);
4007 t = INNERMOST_TEMPLATE_PARMS (parms);
4009 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4011 if (current == decl)
4012 error ("got %d template parameters for %q#D",
4013 TREE_VEC_LENGTH (a), decl);
4015 error ("got %d template parameters for %q#T",
4016 TREE_VEC_LENGTH (a), current);
4017 error (" but %d required", TREE_VEC_LENGTH (t));
4018 return error_mark_node;
4021 if (current == decl)
4024 current = (TYPE_P (current)
4025 ? TYPE_CONTEXT (current)
4026 : DECL_CONTEXT (current));
4029 /* Check that the parms are used in the appropriate qualifying scopes
4030 in the declarator. */
4031 if (!comp_template_args
4033 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4036 template arguments to %qD do not match original template %qD",
4037 decl, DECL_TEMPLATE_RESULT (tmpl));
4038 if (!uses_template_parms (TI_ARGS (tinfo)))
4039 inform (input_location, "use template<> for an explicit specialization");
4040 /* Avoid crash in import_export_decl. */
4041 DECL_INTERFACE_KNOWN (decl) = 1;
4042 return error_mark_node;
4046 DECL_TEMPLATE_RESULT (tmpl) = decl;
4047 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4049 /* Push template declarations for global functions and types. Note
4050 that we do not try to push a global template friend declared in a
4051 template class; such a thing may well depend on the template
4052 parameters of the class. */
4053 if (new_template_p && !ctx
4054 && !(is_friend && template_class_depth (current_class_type) > 0))
4056 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4057 if (tmpl == error_mark_node)
4058 return error_mark_node;
4060 /* Hide template friend classes that haven't been declared yet. */
4061 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4063 DECL_ANTICIPATED (tmpl) = 1;
4064 DECL_FRIEND_P (tmpl) = 1;
4070 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4073 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4074 if (DECL_CONV_FN_P (tmpl))
4076 int depth = TMPL_PARMS_DEPTH (parms);
4078 /* It is a conversion operator. See if the type converted to
4079 depends on innermost template operands. */
4081 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4083 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4086 /* Give template template parms a DECL_CONTEXT of the template
4087 for which they are a parameter. */
4088 parms = INNERMOST_TEMPLATE_PARMS (parms);
4089 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4091 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4092 if (TREE_CODE (parm) == TEMPLATE_DECL)
4093 DECL_CONTEXT (parm) = tmpl;
4097 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4098 back to its most general template. If TMPL is a specialization,
4099 ARGS may only have the innermost set of arguments. Add the missing
4100 argument levels if necessary. */
4101 if (DECL_TEMPLATE_INFO (tmpl))
4102 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4104 info = tree_cons (tmpl, args, NULL_TREE);
4106 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4107 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4108 else if (DECL_LANG_SPECIFIC (decl))
4109 DECL_TEMPLATE_INFO (decl) = info;
4111 return DECL_TEMPLATE_RESULT (tmpl);
4115 push_template_decl (tree decl)
4117 return push_template_decl_real (decl, false);
4120 /* Called when a class template TYPE is redeclared with the indicated
4121 template PARMS, e.g.:
4123 template <class T> struct S;
4124 template <class T> struct S {}; */
4127 redeclare_class_template (tree type, tree parms)
4133 if (!TYPE_TEMPLATE_INFO (type))
4135 error ("%qT is not a template type", type);
4139 tmpl = TYPE_TI_TEMPLATE (type);
4140 if (!PRIMARY_TEMPLATE_P (tmpl))
4141 /* The type is nested in some template class. Nothing to worry
4142 about here; there are no new template parameters for the nested
4148 error ("template specifiers not specified in declaration of %qD",
4153 parms = INNERMOST_TEMPLATE_PARMS (parms);
4154 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4156 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4158 error ("redeclared with %d template parameter(s)",
4159 TREE_VEC_LENGTH (parms));
4160 inform (input_location, "previous declaration %q+D used %d template parameter(s)",
4161 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4165 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4172 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4173 || TREE_VEC_ELT (parms, i) == error_mark_node)
4176 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4177 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4178 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4179 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4181 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4183 if (tmpl_parm != error_mark_node
4184 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4185 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4186 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4187 || (TREE_CODE (tmpl_parm) != PARM_DECL
4188 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4189 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4190 || (TREE_CODE (tmpl_parm) == PARM_DECL
4191 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4192 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))))))
4194 error ("template parameter %q+#D", tmpl_parm);
4195 error ("redeclared here as %q#D", parm);
4199 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4201 /* We have in [temp.param]:
4203 A template-parameter may not be given default arguments
4204 by two different declarations in the same scope. */
4205 error ("redefinition of default argument for %q#D", parm);
4206 inform (input_location, "%Joriginal definition appeared here", tmpl_parm);
4210 if (parm_default != NULL_TREE)
4211 /* Update the previous template parameters (which are the ones
4212 that will really count) with the new default value. */
4213 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4214 else if (tmpl_default != NULL_TREE)
4215 /* Update the new parameters, too; they'll be used as the
4216 parameters for any members. */
4217 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4223 /* Simplify EXPR if it is a non-dependent expression. Returns the
4224 (possibly simplified) expression. */
4227 fold_non_dependent_expr (tree expr)
4229 if (expr == NULL_TREE)
4232 /* If we're in a template, but EXPR isn't value dependent, simplify
4233 it. We're supposed to treat:
4235 template <typename T> void f(T[1 + 1]);
4236 template <typename T> void f(T[2]);
4238 as two declarations of the same function, for example. */
4239 if (processing_template_decl
4240 && !type_dependent_expression_p (expr)
4241 && !value_dependent_expression_p (expr))
4243 HOST_WIDE_INT saved_processing_template_decl;
4245 saved_processing_template_decl = processing_template_decl;
4246 processing_template_decl = 0;
4247 expr = tsubst_copy_and_build (expr,
4250 /*in_decl=*/NULL_TREE,
4251 /*function_p=*/false,
4252 /*integral_constant_expression_p=*/true);
4253 processing_template_decl = saved_processing_template_decl;
4258 /* EXPR is an expression which is used in a constant-expression context.
4259 For instance, it could be a VAR_DECL with a constant initializer.
4260 Extract the innermost constant expression.
4262 This is basically a more powerful version of
4263 integral_constant_value, which can be used also in templates where
4264 initializers can maintain a syntactic rather than semantic form
4265 (even if they are non-dependent, for access-checking purposes). */
4268 fold_decl_constant_value (tree expr)
4270 tree const_expr = expr;
4273 expr = fold_non_dependent_expr (const_expr);
4274 const_expr = integral_constant_value (expr);
4276 while (expr != const_expr);
4281 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4282 must be a function or a pointer-to-function type, as specified
4283 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4284 and check that the resulting function has external linkage. */
4287 convert_nontype_argument_function (tree type, tree expr)
4292 fn = instantiate_type (type, fns, tf_none);
4293 if (fn == error_mark_node)
4294 return error_mark_node;
4297 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4298 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4299 if (TREE_CODE (fn_no_ptr) == BASELINK)
4300 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4302 /* [temp.arg.nontype]/1
4304 A template-argument for a non-type, non-template template-parameter
4307 -- the address of an object or function with external linkage. */
4308 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4310 error ("%qE is not a valid template argument for type %qT "
4311 "because function %qD has not external linkage",
4312 expr, type, fn_no_ptr);
4319 /* Attempt to convert the non-type template parameter EXPR to the
4320 indicated TYPE. If the conversion is successful, return the
4321 converted value. If the conversion is unsuccessful, return
4322 NULL_TREE if we issued an error message, or error_mark_node if we
4323 did not. We issue error messages for out-and-out bad template
4324 parameters, but not simply because the conversion failed, since we
4325 might be just trying to do argument deduction. Both TYPE and EXPR
4326 must be non-dependent.
4328 The conversion follows the special rules described in
4329 [temp.arg.nontype], and it is much more strict than an implicit
4332 This function is called twice for each template argument (see
4333 lookup_template_class for a more accurate description of this
4334 problem). This means that we need to handle expressions which
4335 are not valid in a C++ source, but can be created from the
4336 first call (for instance, casts to perform conversions). These
4337 hacks can go away after we fix the double coercion problem. */
4340 convert_nontype_argument (tree type, tree expr)
4344 /* Detect immediately string literals as invalid non-type argument.
4345 This special-case is not needed for correctness (we would easily
4346 catch this later), but only to provide better diagnostic for this
4347 common user mistake. As suggested by DR 100, we do not mention
4348 linkage issues in the diagnostic as this is not the point. */
4349 if (TREE_CODE (expr) == STRING_CST)
4351 error ("%qE is not a valid template argument for type %qT "
4352 "because string literals can never be used in this context",
4357 /* If we are in a template, EXPR may be non-dependent, but still
4358 have a syntactic, rather than semantic, form. For example, EXPR
4359 might be a SCOPE_REF, rather than the VAR_DECL to which the
4360 SCOPE_REF refers. Preserving the qualifying scope is necessary
4361 so that access checking can be performed when the template is
4362 instantiated -- but here we need the resolved form so that we can
4363 convert the argument. */
4364 expr = fold_non_dependent_expr (expr);
4365 if (error_operand_p (expr))
4366 return error_mark_node;
4367 expr_type = TREE_TYPE (expr);
4369 /* HACK: Due to double coercion, we can get a
4370 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4371 which is the tree that we built on the first call (see
4372 below when coercing to reference to object or to reference to
4373 function). We just strip everything and get to the arg.
4374 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4376 if (TREE_CODE (expr) == NOP_EXPR)
4378 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
4380 /* ??? Maybe we could use convert_from_reference here, but we
4381 would need to relax its constraints because the NOP_EXPR
4382 could actually change the type to something more cv-qualified,
4383 and this is not folded by convert_from_reference. */
4384 tree addr = TREE_OPERAND (expr, 0);
4385 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
4386 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
4387 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
4388 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4389 (TREE_TYPE (expr_type),
4390 TREE_TYPE (TREE_TYPE (addr))));
4392 expr = TREE_OPERAND (addr, 0);
4393 expr_type = TREE_TYPE (expr);
4396 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4397 parameter is a pointer to object, through decay and
4398 qualification conversion. Let's strip everything. */
4399 else if (TYPE_PTROBV_P (type))
4402 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
4403 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
4404 /* Skip the ADDR_EXPR only if it is part of the decay for
4405 an array. Otherwise, it is part of the original argument
4406 in the source code. */
4407 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
4408 expr = TREE_OPERAND (expr, 0);
4409 expr_type = TREE_TYPE (expr);
4413 /* [temp.arg.nontype]/5, bullet 1
4415 For a non-type template-parameter of integral or enumeration type,
4416 integral promotions (_conv.prom_) and integral conversions
4417 (_conv.integral_) are applied. */
4418 if (INTEGRAL_TYPE_P (type))
4420 if (!INTEGRAL_TYPE_P (expr_type))
4421 return error_mark_node;
4423 expr = fold_decl_constant_value (expr);
4424 /* Notice that there are constant expressions like '4 % 0' which
4425 do not fold into integer constants. */
4426 if (TREE_CODE (expr) != INTEGER_CST)
4428 error ("%qE is not a valid template argument for type %qT "
4429 "because it is a non-constant expression", expr, type);
4433 /* At this point, an implicit conversion does what we want,
4434 because we already know that the expression is of integral
4436 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
4437 if (expr == error_mark_node)
4438 return error_mark_node;
4440 /* Conversion was allowed: fold it to a bare integer constant. */
4443 /* [temp.arg.nontype]/5, bullet 2
4445 For a non-type template-parameter of type pointer to object,
4446 qualification conversions (_conv.qual_) and the array-to-pointer
4447 conversion (_conv.array_) are applied. */
4448 else if (TYPE_PTROBV_P (type))
4450 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
4452 A template-argument for a non-type, non-template template-parameter
4453 shall be one of: [...]
4455 -- the name of a non-type template-parameter;
4456 -- the address of an object or function with external linkage, [...]
4457 expressed as "& id-expression" where the & is optional if the name
4458 refers to a function or array, or if the corresponding
4459 template-parameter is a reference.
4461 Here, we do not care about functions, as they are invalid anyway
4462 for a parameter of type pointer-to-object. */
4464 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
4465 /* Non-type template parameters are OK. */
4467 else if (TREE_CODE (expr) != ADDR_EXPR
4468 && TREE_CODE (expr_type) != ARRAY_TYPE)
4470 if (TREE_CODE (expr) == VAR_DECL)
4472 error ("%qD is not a valid template argument "
4473 "because %qD is a variable, not the address of "
4478 /* Other values, like integer constants, might be valid
4479 non-type arguments of some other type. */
4480 return error_mark_node;
4486 decl = ((TREE_CODE (expr) == ADDR_EXPR)
4487 ? TREE_OPERAND (expr, 0) : expr);
4488 if (TREE_CODE (decl) != VAR_DECL)
4490 error ("%qE is not a valid template argument of type %qT "
4491 "because %qE is not a variable",
4495 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
4497 error ("%qE is not a valid template argument of type %qT "
4498 "because %qD does not have external linkage",
4504 expr = decay_conversion (expr);
4505 if (expr == error_mark_node)
4506 return error_mark_node;
4508 expr = perform_qualification_conversions (type, expr);
4509 if (expr == error_mark_node)
4510 return error_mark_node;
4512 /* [temp.arg.nontype]/5, bullet 3
4514 For a non-type template-parameter of type reference to object, no
4515 conversions apply. The type referred to by the reference may be more
4516 cv-qualified than the (otherwise identical) type of the
4517 template-argument. The template-parameter is bound directly to the
4518 template-argument, which must be an lvalue. */
4519 else if (TYPE_REF_OBJ_P (type))
4521 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
4523 return error_mark_node;
4525 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
4527 error ("%qE is not a valid template argument for type %qT "
4528 "because of conflicts in cv-qualification", expr, type);
4532 if (!real_lvalue_p (expr))
4534 error ("%qE is not a valid template argument for type %qT "
4535 "because it is not an lvalue", expr, type);
4539 /* [temp.arg.nontype]/1
4541 A template-argument for a non-type, non-template template-parameter
4542 shall be one of: [...]
4544 -- the address of an object or function with external linkage. */
4545 if (!DECL_EXTERNAL_LINKAGE_P (expr))
4547 error ("%qE is not a valid template argument for type %qT "
4548 "because object %qD has not external linkage",
4553 expr = build_nop (type, build_address (expr));
4555 /* [temp.arg.nontype]/5, bullet 4
4557 For a non-type template-parameter of type pointer to function, only
4558 the function-to-pointer conversion (_conv.func_) is applied. If the
4559 template-argument represents a set of overloaded functions (or a
4560 pointer to such), the matching function is selected from the set
4562 else if (TYPE_PTRFN_P (type))
4564 /* If the argument is a template-id, we might not have enough
4565 context information to decay the pointer. */
4566 if (!type_unknown_p (expr_type))
4568 expr = decay_conversion (expr);
4569 if (expr == error_mark_node)
4570 return error_mark_node;
4573 expr = convert_nontype_argument_function (type, expr);
4574 if (!expr || expr == error_mark_node)
4577 if (TREE_CODE (expr) != ADDR_EXPR)
4579 error ("%qE is not a valid template argument for type %qT", expr, type);
4580 error ("it must be the address of a function with external linkage");
4584 /* [temp.arg.nontype]/5, bullet 5
4586 For a non-type template-parameter of type reference to function, no
4587 conversions apply. If the template-argument represents a set of
4588 overloaded functions, the matching function is selected from the set
4590 else if (TYPE_REFFN_P (type))
4592 if (TREE_CODE (expr) == ADDR_EXPR)
4594 error ("%qE is not a valid template argument for type %qT "
4595 "because it is a pointer", expr, type);
4596 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
4600 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
4601 if (!expr || expr == error_mark_node)
4604 expr = build_nop (type, build_address (expr));
4606 /* [temp.arg.nontype]/5, bullet 6
4608 For a non-type template-parameter of type pointer to member function,
4609 no conversions apply. If the template-argument represents a set of
4610 overloaded member functions, the matching member function is selected
4611 from the set (_over.over_). */
4612 else if (TYPE_PTRMEMFUNC_P (type))
4614 expr = instantiate_type (type, expr, tf_none);
4615 if (expr == error_mark_node)
4616 return error_mark_node;
4618 /* There is no way to disable standard conversions in
4619 resolve_address_of_overloaded_function (called by
4620 instantiate_type). It is possible that the call succeeded by
4621 converting &B::I to &D::I (where B is a base of D), so we need
4622 to reject this conversion here.
4624 Actually, even if there was a way to disable standard conversions,
4625 it would still be better to reject them here so that we can
4626 provide a superior diagnostic. */
4627 if (!same_type_p (TREE_TYPE (expr), type))
4629 /* Make sure we are just one standard conversion off. */
4630 gcc_assert (can_convert (type, TREE_TYPE (expr)));
4631 error ("%qE is not a valid template argument for type %qT "
4632 "because it is of type %qT", expr, type,
4634 inform (input_location, "standard conversions are not allowed in this context");
4638 /* [temp.arg.nontype]/5, bullet 7
4640 For a non-type template-parameter of type pointer to data member,
4641 qualification conversions (_conv.qual_) are applied. */
4642 else if (TYPE_PTRMEM_P (type))
4644 expr = perform_qualification_conversions (type, expr);
4645 if (expr == error_mark_node)
4648 /* A template non-type parameter must be one of the above. */
4652 /* Sanity check: did we actually convert the argument to the
4654 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
4658 /* Subroutine of coerce_template_template_parms, which returns 1 if
4659 PARM_PARM and ARG_PARM match using the rule for the template
4660 parameters of template template parameters. Both PARM and ARG are
4661 template parameters; the rest of the arguments are the same as for
4662 coerce_template_template_parms.
4665 coerce_template_template_parm (tree parm,
4667 tsubst_flags_t complain,
4671 if (arg == NULL_TREE || arg == error_mark_node
4672 || parm == NULL_TREE || parm == error_mark_node)
4675 if (TREE_CODE (arg) != TREE_CODE (parm))
4678 switch (TREE_CODE (parm))
4681 /* We encounter instantiations of templates like
4682 template <template <template <class> class> class TT>
4685 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
4686 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
4688 if (!coerce_template_template_parms
4689 (parmparm, argparm, complain, in_decl, outer_args))
4695 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
4696 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
4697 /* Argument is a parameter pack but parameter is not. */
4702 /* The tsubst call is used to handle cases such as
4704 template <int> class C {};
4705 template <class T, template <T> class TT> class D {};
4708 i.e. the parameter list of TT depends on earlier parameters. */
4709 if (!uses_template_parms (TREE_TYPE (arg))
4711 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
4715 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
4716 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
4717 /* Argument is a parameter pack but parameter is not. */
4730 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
4731 template template parameters. Both PARM_PARMS and ARG_PARMS are
4732 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
4735 Consider the example:
4736 template <class T> class A;
4737 template<template <class U> class TT> class B;
4739 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
4740 the parameters to A, and OUTER_ARGS contains A. */
4743 coerce_template_template_parms (tree parm_parms,
4745 tsubst_flags_t complain,
4749 int nparms, nargs, i;
4753 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
4754 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
4756 nparms = TREE_VEC_LENGTH (parm_parms);
4757 nargs = TREE_VEC_LENGTH (arg_parms);
4759 /* Determine whether we have a parameter pack at the end of the
4760 template template parameter's template parameter list. */
4761 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
4763 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
4765 if (parm == error_mark_node)
4768 switch (TREE_CODE (parm))
4772 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
4777 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
4787 && !(variadic_p && nargs >= nparms - 1))
4790 /* Check all of the template parameters except the parameter pack at
4791 the end (if any). */
4792 for (i = 0; i < nparms - variadic_p; ++i)
4794 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
4795 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
4798 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
4799 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
4801 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
4809 /* Check each of the template parameters in the template
4810 argument against the template parameter pack at the end of
4811 the template template parameter. */
4812 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
4815 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
4817 for (; i < nargs; ++i)
4819 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
4822 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
4824 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
4833 /* Verifies that the deduced template arguments (in TARGS) for the
4834 template template parameters (in TPARMS) represent valid bindings,
4835 by comparing the template parameter list of each template argument
4836 to the template parameter list of its corresponding template
4837 template parameter, in accordance with DR150. This
4838 routine can only be called after all template arguments have been
4839 deduced. It will return TRUE if all of the template template
4840 parameter bindings are okay, FALSE otherwise. */
4842 template_template_parm_bindings_ok_p (tree tparms, tree targs)
4844 int i, ntparms = TREE_VEC_LENGTH (tparms);
4847 /* We're dealing with template parms in this process. */
4848 ++processing_template_decl;
4850 targs = INNERMOST_TEMPLATE_ARGS (targs);
4852 for (i = 0; i < ntparms; ++i)
4854 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
4855 tree targ = TREE_VEC_ELT (targs, i);
4857 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
4859 tree packed_args = NULL_TREE;
4862 if (ARGUMENT_PACK_P (targ))
4864 /* Look inside the argument pack. */
4865 packed_args = ARGUMENT_PACK_ARGS (targ);
4866 len = TREE_VEC_LENGTH (packed_args);
4869 for (idx = 0; idx < len; ++idx)
4871 tree targ_parms = NULL_TREE;
4874 /* Extract the next argument from the argument
4876 targ = TREE_VEC_ELT (packed_args, idx);
4878 if (PACK_EXPANSION_P (targ))
4879 /* Look at the pattern of the pack expansion. */
4880 targ = PACK_EXPANSION_PATTERN (targ);
4882 /* Extract the template parameters from the template
4884 if (TREE_CODE (targ) == TEMPLATE_DECL)
4885 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
4886 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
4887 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
4889 /* Verify that we can coerce the template template
4890 parameters from the template argument to the template
4891 parameter. This requires an exact match. */
4893 && !coerce_template_template_parms
4894 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
4909 --processing_template_decl;
4913 /* Convert the indicated template ARG as necessary to match the
4914 indicated template PARM. Returns the converted ARG, or
4915 error_mark_node if the conversion was unsuccessful. Error and
4916 warning messages are issued under control of COMPLAIN. This
4917 conversion is for the Ith parameter in the parameter list. ARGS is
4918 the full set of template arguments deduced so far. */
4921 convert_template_argument (tree parm,
4924 tsubst_flags_t complain,
4930 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
4932 if (TREE_CODE (arg) == TREE_LIST
4933 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
4935 /* The template argument was the name of some
4936 member function. That's usually
4937 invalid, but static members are OK. In any
4938 case, grab the underlying fields/functions
4939 and issue an error later if required. */
4940 orig_arg = TREE_VALUE (arg);
4941 TREE_TYPE (arg) = unknown_type_node;
4946 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
4947 requires_type = (TREE_CODE (parm) == TYPE_DECL
4948 || requires_tmpl_type);
4950 /* When determining whether an argument pack expansion is a template,
4951 look at the pattern. */
4952 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
4953 arg = PACK_EXPANSION_PATTERN (arg);
4956 ((TREE_CODE (arg) == TEMPLATE_DECL
4957 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
4958 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4959 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
4962 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4963 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
4964 arg = TYPE_STUB_DECL (arg);
4966 is_type = TYPE_P (arg) || is_tmpl_type;
4968 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
4969 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
4971 permerror (input_location, "to refer to a type member of a template parameter, "
4972 "use %<typename %E%>", orig_arg);
4974 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
4975 TREE_OPERAND (arg, 1),
4977 complain & tf_error);
4981 if (is_type != requires_type)
4985 if (complain & tf_error)
4987 error ("type/value mismatch at argument %d in template "
4988 "parameter list for %qD",
4991 error (" expected a constant of type %qT, got %qT",
4993 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
4994 else if (requires_tmpl_type)
4995 error (" expected a class template, got %qE", orig_arg);
4997 error (" expected a type, got %qE", orig_arg);
5000 return error_mark_node;
5002 if (is_tmpl_type ^ requires_tmpl_type)
5004 if (in_decl && (complain & tf_error))
5006 error ("type/value mismatch at argument %d in template "
5007 "parameter list for %qD",
5010 error (" expected a type, got %qT", DECL_NAME (arg));
5012 error (" expected a class template, got %qT", orig_arg);
5014 return error_mark_node;
5019 if (requires_tmpl_type)
5021 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
5022 /* The number of argument required is not known yet.
5023 Just accept it for now. */
5024 val = TREE_TYPE (arg);
5027 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5030 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5032 if (coerce_template_template_parms (parmparm, argparm,
5038 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5040 if (val != error_mark_node)
5042 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
5043 val = TREE_TYPE (val);
5044 else if (TREE_CODE (val) == TYPE_PACK_EXPANSION
5045 && DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
5047 val = TREE_TYPE (arg);
5048 val = make_pack_expansion (val);
5054 if (in_decl && (complain & tf_error))
5056 error ("type/value mismatch at argument %d in "
5057 "template parameter list for %qD",
5059 error (" expected a template of type %qD, got %qD",
5063 val = error_mark_node;
5069 /* We only form one instance of each template specialization.
5070 Therefore, if we use a non-canonical variant (i.e., a
5071 typedef), any future messages referring to the type will use
5072 the typedef, which is confusing if those future uses do not
5073 themselves also use the typedef. */
5075 val = canonical_type_variant (val);
5079 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
5081 if (invalid_nontype_parm_type_p (t, complain))
5082 return error_mark_node;
5084 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
5086 if (same_type_p (t, TREE_TYPE (orig_arg)))
5090 /* Not sure if this is reachable, but it doesn't hurt
5092 error ("type mismatch in nontype parameter pack");
5093 val = error_mark_node;
5096 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
5097 /* We used to call digest_init here. However, digest_init
5098 will report errors, which we don't want when complain
5099 is zero. More importantly, digest_init will try too
5100 hard to convert things: for example, `0' should not be
5101 converted to pointer type at this point according to
5102 the standard. Accepting this is not merely an
5103 extension, since deciding whether or not these
5104 conversions can occur is part of determining which
5105 function template to call, or whether a given explicit
5106 argument specification is valid. */
5107 val = convert_nontype_argument (t, orig_arg);
5111 if (val == NULL_TREE)
5112 val = error_mark_node;
5113 else if (val == error_mark_node && (complain & tf_error))
5114 error ("could not convert template argument %qE to %qT", orig_arg, t);
5120 /* Coerces the remaining template arguments in INNER_ARGS (from
5121 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5122 Returns the coerced argument pack. PARM_IDX is the position of this
5123 parameter in the template parameter list. ARGS is the original
5124 template argument list. */
5126 coerce_template_parameter_pack (tree parms,
5134 tsubst_flags_t complain)
5136 tree parm = TREE_VEC_ELT (parms, parm_idx);
5137 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5140 tree packed_types = NULL_TREE;
5142 if (arg_idx > nargs)
5145 packed_args = make_tree_vec (nargs - arg_idx);
5147 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
5148 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
5150 /* When the template parameter is a non-type template
5151 parameter pack whose type uses parameter packs, we need
5152 to look at each of the template arguments
5153 separately. Build a vector of the types for these
5154 non-type template parameters in PACKED_TYPES. */
5156 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
5157 packed_types = tsubst_pack_expansion (expansion, args,
5160 if (packed_types == error_mark_node)
5161 return error_mark_node;
5163 /* Check that we have the right number of arguments. */
5165 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
5166 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
5169 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
5170 error ("wrong number of template arguments (%d, should be %d)",
5171 nargs, needed_parms);
5172 return error_mark_node;
5175 /* If we aren't able to check the actual arguments now
5176 (because they haven't been expanded yet), we can at least
5177 verify that all of the types used for the non-type
5178 template parameter pack are, in fact, valid for non-type
5179 template parameters. */
5181 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
5183 int j, len = TREE_VEC_LENGTH (packed_types);
5184 for (j = 0; j < len; ++j)
5186 tree t = TREE_VEC_ELT (packed_types, j);
5187 if (invalid_nontype_parm_type_p (t, complain))
5188 return error_mark_node;
5193 /* Convert the remaining arguments, which will be a part of the
5194 parameter pack "parm". */
5195 for (; arg_idx < nargs; ++arg_idx)
5197 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
5198 tree actual_parm = TREE_VALUE (parm);
5200 if (packed_types && !PACK_EXPANSION_P (arg))
5202 /* When we have a vector of types (corresponding to the
5203 non-type template parameter pack that uses parameter
5204 packs in its type, as mention above), and the
5205 argument is not an expansion (which expands to a
5206 currently unknown number of arguments), clone the
5207 parm and give it the next type in PACKED_TYPES. */
5208 actual_parm = copy_node (actual_parm);
5209 TREE_TYPE (actual_parm) =
5210 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
5213 if (arg != error_mark_node)
5214 arg = convert_template_argument (actual_parm,
5215 arg, new_args, complain, parm_idx,
5217 if (arg == error_mark_node)
5219 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
5222 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
5223 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
5224 argument_pack = make_node (TYPE_ARGUMENT_PACK);
5227 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
5228 TREE_TYPE (argument_pack)
5229 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
5230 TREE_CONSTANT (argument_pack) = 1;
5233 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
5234 return argument_pack;
5237 /* Convert all template arguments to their appropriate types, and
5238 return a vector containing the innermost resulting template
5239 arguments. If any error occurs, return error_mark_node. Error and
5240 warning messages are issued under control of COMPLAIN.
5242 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5243 for arguments not specified in ARGS. Otherwise, if
5244 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5245 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5246 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5250 coerce_template_parms (tree parms,
5253 tsubst_flags_t complain,
5254 bool require_all_args,
5255 bool use_default_args)
5257 int nparms, nargs, parm_idx, arg_idx, lost = 0;
5260 tree new_inner_args;
5261 bool saved_skip_evaluation;
5263 /* When used as a boolean value, indicates whether this is a
5264 variadic template parameter list. Since it's an int, we can also
5265 subtract it from nparms to get the number of non-variadic
5269 nparms = TREE_VEC_LENGTH (parms);
5271 /* Determine if there are any parameter packs. */
5272 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
5274 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
5275 if (template_parameter_pack_p (tparm))
5279 inner_args = INNERMOST_TEMPLATE_ARGS (args);
5280 /* If there are 0 or 1 parameter packs, we need to expand any argument
5281 packs so that we can deduce a parameter pack from some non-packed args
5282 followed by an argument pack, as in variadic85.C. If there are more
5283 than that, we need to leave argument packs intact so the arguments are
5284 assigned to the right parameter packs. This should only happen when
5285 dealing with a nested class inside a partial specialization of a class
5286 template, as in variadic92.C. */
5287 if (variadic_p <= 1)
5288 inner_args = expand_template_argument_pack (inner_args);
5290 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5291 if ((nargs > nparms && !variadic_p)
5292 || (nargs < nparms - variadic_p
5294 && (!use_default_args
5295 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
5296 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
5298 if (complain & tf_error)
5300 const char *or_more = "";
5303 or_more = " or more";
5307 error ("wrong number of template arguments (%d, should be %d%s)",
5308 nargs, nparms, or_more);
5311 error ("provided for %q+D", in_decl);
5314 return error_mark_node;
5317 /* We need to evaluate the template arguments, even though this
5318 template-id may be nested within a "sizeof". */
5319 saved_skip_evaluation = skip_evaluation;
5320 skip_evaluation = false;
5321 new_inner_args = make_tree_vec (nparms);
5322 new_args = add_outermost_template_args (args, new_inner_args);
5323 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
5328 /* Get the Ith template parameter. */
5329 parm = TREE_VEC_ELT (parms, parm_idx);
5331 if (parm == error_mark_node)
5333 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
5337 /* Calculate the next argument. */
5338 if (arg_idx < nargs)
5339 arg = TREE_VEC_ELT (inner_args, arg_idx);
5343 if (template_parameter_pack_p (TREE_VALUE (parm))
5344 && !(arg && ARGUMENT_PACK_P (arg)))
5346 /* All remaining arguments will be placed in the
5347 template parameter pack PARM. */
5348 arg = coerce_template_parameter_pack (parms, parm_idx, args,
5349 inner_args, arg_idx,
5353 /* Store this argument. */
5354 if (arg == error_mark_node)
5356 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
5358 /* We are done with all of the arguments. */
5365 if (PACK_EXPANSION_P (arg))
5367 if (complain & tf_error)
5369 /* FIXME this restriction was removed by N2555; see
5371 /* If ARG is a pack expansion, but PARM is not a
5372 template parameter pack (if it were, we would have
5373 handled it above), we're trying to expand into a
5374 fixed-length argument list. */
5375 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5376 sorry ("cannot expand %<%E%> into a fixed-length "
5377 "argument list", arg);
5379 sorry ("cannot expand %<%T%> into a fixed-length "
5380 "argument list", arg);
5382 return error_mark_node;
5385 else if (require_all_args)
5386 /* There must be a default arg in this case. */
5387 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
5392 if (arg == error_mark_node)
5394 if (complain & tf_error)
5395 error ("template argument %d is invalid", arg_idx + 1);
5398 /* This only occurs if there was an error in the template
5399 parameter list itself (which we would already have
5400 reported) that we are trying to recover from, e.g., a class
5401 template with a parameter list such as
5402 template<typename..., typename>. */
5403 return error_mark_node;
5405 arg = convert_template_argument (TREE_VALUE (parm),
5406 arg, new_args, complain,
5409 if (arg == error_mark_node)
5411 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
5413 skip_evaluation = saved_skip_evaluation;
5416 return error_mark_node;
5418 return new_inner_args;
5421 /* Returns 1 if template args OT and NT are equivalent. */
5424 template_args_equal (tree ot, tree nt)
5429 if (TREE_CODE (nt) == TREE_VEC)
5430 /* For member templates */
5431 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
5432 else if (PACK_EXPANSION_P (ot))
5433 return PACK_EXPANSION_P (nt)
5434 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
5435 PACK_EXPANSION_PATTERN (nt));
5436 else if (ARGUMENT_PACK_P (ot))
5441 if (!ARGUMENT_PACK_P (nt))
5444 opack = ARGUMENT_PACK_ARGS (ot);
5445 npack = ARGUMENT_PACK_ARGS (nt);
5446 len = TREE_VEC_LENGTH (opack);
5447 if (TREE_VEC_LENGTH (npack) != len)
5449 for (i = 0; i < len; ++i)
5450 if (!template_args_equal (TREE_VEC_ELT (opack, i),
5451 TREE_VEC_ELT (npack, i)))
5455 else if (TYPE_P (nt))
5456 return TYPE_P (ot) && same_type_p (ot, nt);
5457 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
5460 return cp_tree_equal (ot, nt);
5463 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
5464 of template arguments. Returns 0 otherwise. */
5467 comp_template_args (tree oldargs, tree newargs)
5471 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
5474 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
5476 tree nt = TREE_VEC_ELT (newargs, i);
5477 tree ot = TREE_VEC_ELT (oldargs, i);
5479 if (! template_args_equal (ot, nt))
5486 add_pending_template (tree d)
5488 tree ti = (TYPE_P (d)
5489 ? CLASSTYPE_TEMPLATE_INFO (d)
5490 : DECL_TEMPLATE_INFO (d));
5491 struct pending_template *pt;
5494 if (TI_PENDING_TEMPLATE_FLAG (ti))
5497 /* We are called both from instantiate_decl, where we've already had a
5498 tinst_level pushed, and instantiate_template, where we haven't.
5500 level = !current_tinst_level || current_tinst_level->decl != d;
5503 push_tinst_level (d);
5505 pt = GGC_NEW (struct pending_template);
5507 pt->tinst = current_tinst_level;
5508 if (last_pending_template)
5509 last_pending_template->next = pt;
5511 pending_templates = pt;
5513 last_pending_template = pt;
5515 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
5522 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
5523 ARGLIST. Valid choices for FNS are given in the cp-tree.def
5524 documentation for TEMPLATE_ID_EXPR. */
5527 lookup_template_function (tree fns, tree arglist)
5531 if (fns == error_mark_node || arglist == error_mark_node)
5532 return error_mark_node;
5534 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
5535 gcc_assert (fns && (is_overloaded_fn (fns)
5536 || TREE_CODE (fns) == IDENTIFIER_NODE));
5538 if (BASELINK_P (fns))
5540 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
5542 BASELINK_FUNCTIONS (fns),
5547 type = TREE_TYPE (fns);
5548 if (TREE_CODE (fns) == OVERLOAD || !type)
5549 type = unknown_type_node;
5551 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
5554 /* Within the scope of a template class S<T>, the name S gets bound
5555 (in build_self_reference) to a TYPE_DECL for the class, not a
5556 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
5557 or one of its enclosing classes, and that type is a template,
5558 return the associated TEMPLATE_DECL. Otherwise, the original
5559 DECL is returned. */
5562 maybe_get_template_decl_from_type_decl (tree decl)
5564 return (decl != NULL_TREE
5565 && TREE_CODE (decl) == TYPE_DECL
5566 && DECL_ARTIFICIAL (decl)
5567 && CLASS_TYPE_P (TREE_TYPE (decl))
5568 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
5569 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
5572 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
5573 parameters, find the desired type.
5575 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
5577 IN_DECL, if non-NULL, is the template declaration we are trying to
5580 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
5581 the class we are looking up.
5583 Issue error and warning messages under control of COMPLAIN.
5585 If the template class is really a local class in a template
5586 function, then the FUNCTION_CONTEXT is the function in which it is
5589 ??? Note that this function is currently called *twice* for each
5590 template-id: the first time from the parser, while creating the
5591 incomplete type (finish_template_type), and the second type during the
5592 real instantiation (instantiate_template_class). This is surely something
5593 that we want to avoid. It also causes some problems with argument
5594 coercion (see convert_nontype_argument for more information on this). */
5597 lookup_template_class (tree d1,
5602 tsubst_flags_t complain)
5604 tree templ = NULL_TREE, parmlist;
5607 timevar_push (TV_NAME_LOOKUP);
5609 if (TREE_CODE (d1) == IDENTIFIER_NODE)
5611 tree value = innermost_non_namespace_value (d1);
5612 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
5617 push_decl_namespace (context);
5618 templ = lookup_name (d1);
5619 templ = maybe_get_template_decl_from_type_decl (templ);
5621 pop_decl_namespace ();
5624 context = DECL_CONTEXT (templ);
5626 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
5628 tree type = TREE_TYPE (d1);
5630 /* If we are declaring a constructor, say A<T>::A<T>, we will get
5631 an implicit typename for the second A. Deal with it. */
5632 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
5633 type = TREE_TYPE (type);
5635 if (CLASSTYPE_TEMPLATE_INFO (type))
5637 templ = CLASSTYPE_TI_TEMPLATE (type);
5638 d1 = DECL_NAME (templ);
5641 else if (TREE_CODE (d1) == ENUMERAL_TYPE
5642 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
5644 templ = TYPE_TI_TEMPLATE (d1);
5645 d1 = DECL_NAME (templ);
5647 else if (TREE_CODE (d1) == TEMPLATE_DECL
5648 && DECL_TEMPLATE_RESULT (d1)
5649 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
5652 d1 = DECL_NAME (templ);
5653 context = DECL_CONTEXT (templ);
5656 /* Issue an error message if we didn't find a template. */
5659 if (complain & tf_error)
5660 error ("%qT is not a template", d1);
5661 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5664 if (TREE_CODE (templ) != TEMPLATE_DECL
5665 /* Make sure it's a user visible template, if it was named by
5667 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
5668 && !PRIMARY_TEMPLATE_P (templ)))
5670 if (complain & tf_error)
5672 error ("non-template type %qT used as a template", d1);
5674 error ("for template declaration %q+D", in_decl);
5676 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5679 complain &= ~tf_user;
5681 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
5683 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
5684 template arguments */
5690 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
5692 /* Consider an example where a template template parameter declared as
5694 template <class T, class U = std::allocator<T> > class TT
5696 The template parameter level of T and U are one level larger than
5697 of TT. To proper process the default argument of U, say when an
5698 instantiation `TT<int>' is seen, we need to build the full
5699 arguments containing {int} as the innermost level. Outer levels,
5700 available when not appearing as default template argument, can be
5701 obtained from the arguments of the enclosing template.
5703 Suppose that TT is later substituted with std::vector. The above
5704 instantiation is `TT<int, std::allocator<T> >' with TT at
5705 level 1, and T at level 2, while the template arguments at level 1
5706 becomes {std::vector} and the inner level 2 is {int}. */
5708 outer = DECL_CONTEXT (templ);
5710 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
5711 else if (current_template_parms)
5712 /* This is an argument of the current template, so we haven't set
5713 DECL_CONTEXT yet. */
5714 outer = current_template_args ();
5717 arglist = add_to_template_args (outer, arglist);
5719 arglist2 = coerce_template_parms (parmlist, arglist, templ,
5721 /*require_all_args=*/true,
5722 /*use_default_args=*/true);
5723 if (arglist2 == error_mark_node
5724 || (!uses_template_parms (arglist2)
5725 && check_instantiated_args (templ, arglist2, complain)))
5726 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5728 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
5729 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
5733 tree template_type = TREE_TYPE (templ);
5736 tree found = NULL_TREE;
5739 int is_partial_instantiation;
5741 gen_tmpl = most_general_template (templ);
5742 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
5743 parm_depth = TMPL_PARMS_DEPTH (parmlist);
5744 arg_depth = TMPL_ARGS_DEPTH (arglist);
5746 if (arg_depth == 1 && parm_depth > 1)
5748 /* We've been given an incomplete set of template arguments.
5751 template <class T> struct S1 {
5752 template <class U> struct S2 {};
5753 template <class U> struct S2<U*> {};
5756 we will be called with an ARGLIST of `U*', but the
5757 TEMPLATE will be `template <class T> template
5758 <class U> struct S1<T>::S2'. We must fill in the missing
5761 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
5763 arg_depth = TMPL_ARGS_DEPTH (arglist);
5766 /* Now we should have enough arguments. */
5767 gcc_assert (parm_depth == arg_depth);
5769 /* From here on, we're only interested in the most general
5773 /* Calculate the BOUND_ARGS. These will be the args that are
5774 actually tsubst'd into the definition to create the
5778 /* We have multiple levels of arguments to coerce, at once. */
5780 int saved_depth = TMPL_ARGS_DEPTH (arglist);
5782 tree bound_args = make_tree_vec (parm_depth);
5784 for (i = saved_depth,
5785 t = DECL_TEMPLATE_PARMS (templ);
5786 i > 0 && t != NULL_TREE;
5787 --i, t = TREE_CHAIN (t))
5789 tree a = coerce_template_parms (TREE_VALUE (t),
5792 /*require_all_args=*/true,
5793 /*use_default_args=*/true);
5795 /* Don't process further if one of the levels fails. */
5796 if (a == error_mark_node)
5798 /* Restore the ARGLIST to its full size. */
5799 TREE_VEC_LENGTH (arglist) = saved_depth;
5800 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5803 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
5805 /* We temporarily reduce the length of the ARGLIST so
5806 that coerce_template_parms will see only the arguments
5807 corresponding to the template parameters it is
5809 TREE_VEC_LENGTH (arglist)--;
5812 /* Restore the ARGLIST to its full size. */
5813 TREE_VEC_LENGTH (arglist) = saved_depth;
5815 arglist = bound_args;
5819 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
5820 INNERMOST_TEMPLATE_ARGS (arglist),
5823 /*require_all_args=*/true,
5824 /*use_default_args=*/true);
5826 if (arglist == error_mark_node)
5827 /* We were unable to bind the arguments. */
5828 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5830 /* In the scope of a template class, explicit references to the
5831 template class refer to the type of the template, not any
5832 instantiation of it. For example, in:
5834 template <class T> class C { void f(C<T>); }
5836 the `C<T>' is just the same as `C'. Outside of the
5837 class, however, such a reference is an instantiation. */
5838 if (comp_template_args (TYPE_TI_ARGS (template_type),
5841 found = template_type;
5843 if (!entering_scope && PRIMARY_TEMPLATE_P (templ))
5847 for (ctx = current_class_type;
5848 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
5850 ? TYPE_CONTEXT (ctx)
5851 : DECL_CONTEXT (ctx)))
5852 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
5855 /* We're not in the scope of the class, so the
5856 TEMPLATE_TYPE is not the type we want after all. */
5862 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5864 /* If we already have this specialization, return it. */
5865 found = retrieve_specialization (templ, arglist,
5866 /*class_specializations_p=*/false);
5868 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5870 /* This type is a "partial instantiation" if any of the template
5871 arguments still involve template parameters. Note that we set
5872 IS_PARTIAL_INSTANTIATION for partial specializations as
5874 is_partial_instantiation = uses_template_parms (arglist);
5876 /* If the deduced arguments are invalid, then the binding
5878 if (!is_partial_instantiation
5879 && check_instantiated_args (templ,
5880 INNERMOST_TEMPLATE_ARGS (arglist),
5882 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5884 if (!is_partial_instantiation
5885 && !PRIMARY_TEMPLATE_P (templ)
5886 && TREE_CODE (CP_DECL_CONTEXT (templ)) == NAMESPACE_DECL)
5888 found = xref_tag_from_type (TREE_TYPE (templ),
5890 /*tag_scope=*/ts_global);
5891 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5894 context = tsubst (DECL_CONTEXT (templ), arglist,
5897 context = global_namespace;
5899 /* Create the type. */
5900 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
5902 if (!is_partial_instantiation)
5904 set_current_access_from_decl (TYPE_NAME (template_type));
5905 t = start_enum (TYPE_IDENTIFIER (template_type),
5906 tsubst (ENUM_UNDERLYING_TYPE (template_type),
5907 arglist, complain, in_decl),
5908 SCOPED_ENUM_P (template_type));
5912 /* We don't want to call start_enum for this type, since
5913 the values for the enumeration constants may involve
5914 template parameters. And, no one should be interested
5915 in the enumeration constants for such a type. */
5916 t = make_node (ENUMERAL_TYPE);
5917 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
5922 t = make_class_type (TREE_CODE (template_type));
5923 CLASSTYPE_DECLARED_CLASS (t)
5924 = CLASSTYPE_DECLARED_CLASS (template_type);
5925 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
5926 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
5928 /* A local class. Make sure the decl gets registered properly. */
5929 if (context == current_function_decl)
5930 pushtag (DECL_NAME (templ), t, /*tag_scope=*/ts_current);
5932 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
5933 /* This instantiation is another name for the primary
5934 template type. Set the TYPE_CANONICAL field
5936 TYPE_CANONICAL (t) = template_type;
5937 else if (any_template_arguments_need_structural_equality_p (arglist))
5938 /* Some of the template arguments require structural
5939 equality testing, so this template class requires
5940 structural equality testing. */
5941 SET_TYPE_STRUCTURAL_EQUALITY (t);
5944 /* If we called start_enum or pushtag above, this information
5945 will already be set up. */
5948 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5950 type_decl = create_implicit_typedef (DECL_NAME (templ), t);
5951 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
5952 TYPE_STUB_DECL (t) = type_decl;
5953 DECL_SOURCE_LOCATION (type_decl)
5954 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
5957 type_decl = TYPE_NAME (t);
5959 TREE_PRIVATE (type_decl)
5960 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
5961 TREE_PROTECTED (type_decl)
5962 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
5963 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
5965 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
5966 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
5969 /* Set up the template information. We have to figure out which
5970 template is the immediate parent if this is a full
5972 if (parm_depth == 1 || is_partial_instantiation
5973 || !PRIMARY_TEMPLATE_P (templ))
5974 /* This case is easy; there are no member templates involved. */
5978 /* This is a full instantiation of a member template. Look
5979 for a partial instantiation of which this is an instance. */
5981 for (found = DECL_TEMPLATE_INSTANTIATIONS (templ);
5982 found; found = TREE_CHAIN (found))
5985 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
5987 /* We only want partial instantiations, here, not
5988 specializations or full instantiations. */
5989 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
5990 || !uses_template_parms (TREE_VALUE (found)))
5993 /* Temporarily reduce by one the number of levels in the
5994 ARGLIST and in FOUND so as to avoid comparing the
5995 last set of arguments. */
5996 TREE_VEC_LENGTH (arglist)--;
5997 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
5999 /* See if the arguments match. If they do, then TMPL is
6000 the partial instantiation we want. */
6001 success = comp_template_args (TREE_PURPOSE (found), arglist);
6003 /* Restore the argument vectors to their full size. */
6004 TREE_VEC_LENGTH (arglist)++;
6005 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
6016 /* There was no partial instantiation. This happens
6017 where C<T> is a member template of A<T> and it's used
6020 template <typename T> struct B { A<T>::C<int> m; };
6023 Create the partial instantiation.
6025 TREE_VEC_LENGTH (arglist)--;
6026 found = tsubst (templ, arglist, complain, NULL_TREE);
6027 TREE_VEC_LENGTH (arglist)++;
6031 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
6032 DECL_TEMPLATE_INSTANTIATIONS (templ)
6033 = tree_cons (arglist, t,
6034 DECL_TEMPLATE_INSTANTIATIONS (templ));
6036 if (TREE_CODE (t) == ENUMERAL_TYPE
6037 && !is_partial_instantiation)
6038 /* Now that the type has been registered on the instantiations
6039 list, we set up the enumerators. Because the enumeration
6040 constants may involve the enumeration type itself, we make
6041 sure to register the type first, and then create the
6042 constants. That way, doing tsubst_expr for the enumeration
6043 constants won't result in recursive calls here; we'll find
6044 the instantiation and exit above. */
6045 tsubst_enum (template_type, t, arglist);
6047 if (is_partial_instantiation)
6048 /* If the type makes use of template parameters, the
6049 code that generates debugging information will crash. */
6050 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
6052 /* Possibly limit visibility based on template args. */
6053 TREE_PUBLIC (type_decl) = 1;
6054 determine_visibility (type_decl);
6056 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6058 timevar_pop (TV_NAME_LOOKUP);
6065 /* True when we should also visit template parameters that occur in
6066 non-deduced contexts. */
6067 bool include_nondeduced_p;
6068 struct pointer_set_t *visited;
6071 /* Called from for_each_template_parm via walk_tree. */
6074 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
6077 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
6078 tree_fn_t fn = pfd->fn;
6079 void *data = pfd->data;
6082 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
6083 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
6084 pfd->include_nondeduced_p))
6085 return error_mark_node;
6087 switch (TREE_CODE (t))
6090 if (TYPE_PTRMEMFUNC_P (t))
6096 if (!TYPE_TEMPLATE_INFO (t))
6098 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
6099 fn, data, pfd->visited,
6100 pfd->include_nondeduced_p))
6101 return error_mark_node;
6105 if (for_each_template_parm (TYPE_MIN_VALUE (t),
6106 fn, data, pfd->visited,
6107 pfd->include_nondeduced_p)
6108 || for_each_template_parm (TYPE_MAX_VALUE (t),
6109 fn, data, pfd->visited,
6110 pfd->include_nondeduced_p))
6111 return error_mark_node;
6115 /* Since we're not going to walk subtrees, we have to do this
6117 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
6118 pfd->visited, pfd->include_nondeduced_p))
6119 return error_mark_node;
6123 /* Check the return type. */
6124 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6125 pfd->include_nondeduced_p))
6126 return error_mark_node;
6128 /* Check the parameter types. Since default arguments are not
6129 instantiated until they are needed, the TYPE_ARG_TYPES may
6130 contain expressions that involve template parameters. But,
6131 no-one should be looking at them yet. And, once they're
6132 instantiated, they don't contain template parameters, so
6133 there's no point in looking at them then, either. */
6137 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
6138 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
6139 pfd->visited, pfd->include_nondeduced_p))
6140 return error_mark_node;
6142 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6143 want walk_tree walking into them itself. */
6149 if (pfd->include_nondeduced_p
6150 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
6152 pfd->include_nondeduced_p))
6153 return error_mark_node;
6158 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
6159 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
6160 pfd->visited, pfd->include_nondeduced_p))
6161 return error_mark_node;
6166 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
6167 && for_each_template_parm (DECL_INITIAL (t), fn, data,
6168 pfd->visited, pfd->include_nondeduced_p))
6169 return error_mark_node;
6170 if (DECL_CONTEXT (t)
6171 && pfd->include_nondeduced_p
6172 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
6173 pfd->visited, pfd->include_nondeduced_p))
6174 return error_mark_node;
6177 case BOUND_TEMPLATE_TEMPLATE_PARM:
6178 /* Record template parameters such as `T' inside `TT<T>'. */
6179 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
6180 pfd->include_nondeduced_p))
6181 return error_mark_node;
6184 case TEMPLATE_TEMPLATE_PARM:
6185 case TEMPLATE_TYPE_PARM:
6186 case TEMPLATE_PARM_INDEX:
6187 if (fn && (*fn)(t, data))
6188 return error_mark_node;
6190 return error_mark_node;
6194 /* A template template parameter is encountered. */
6195 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
6196 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6197 pfd->include_nondeduced_p))
6198 return error_mark_node;
6200 /* Already substituted template template parameter */
6206 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
6208 pfd->include_nondeduced_p))
6209 return error_mark_node;
6213 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
6214 && pfd->include_nondeduced_p
6215 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
6216 (TREE_TYPE (t)), fn, data,
6217 pfd->visited, pfd->include_nondeduced_p))
6218 return error_mark_node;
6223 /* If there's no type, then this thing must be some expression
6224 involving template parameters. */
6225 if (!fn && !TREE_TYPE (t))
6226 return error_mark_node;
6231 case REINTERPRET_CAST_EXPR:
6232 case CONST_CAST_EXPR:
6233 case STATIC_CAST_EXPR:
6234 case DYNAMIC_CAST_EXPR:
6238 case PSEUDO_DTOR_EXPR:
6240 return error_mark_node;
6247 /* We didn't find any template parameters we liked. */
6251 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
6252 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
6253 call FN with the parameter and the DATA.
6254 If FN returns nonzero, the iteration is terminated, and
6255 for_each_template_parm returns 1. Otherwise, the iteration
6256 continues. If FN never returns a nonzero value, the value
6257 returned by for_each_template_parm is 0. If FN is NULL, it is
6258 considered to be the function which always returns 1.
6260 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
6261 parameters that occur in non-deduced contexts. When false, only
6262 visits those template parameters that can be deduced. */
6265 for_each_template_parm (tree t, tree_fn_t fn, void* data,
6266 struct pointer_set_t *visited,
6267 bool include_nondeduced_p)
6269 struct pair_fn_data pfd;
6275 pfd.include_nondeduced_p = include_nondeduced_p;
6277 /* Walk the tree. (Conceptually, we would like to walk without
6278 duplicates, but for_each_template_parm_r recursively calls
6279 for_each_template_parm, so we would need to reorganize a fair
6280 bit to use walk_tree_without_duplicates, so we keep our own
6283 pfd.visited = visited;
6285 pfd.visited = pointer_set_create ();
6286 result = cp_walk_tree (&t,
6287 for_each_template_parm_r,
6289 pfd.visited) != NULL_TREE;
6294 pointer_set_destroy (pfd.visited);
6301 /* Returns true if T depends on any template parameter. */
6304 uses_template_parms (tree t)
6307 int saved_processing_template_decl;
6309 saved_processing_template_decl = processing_template_decl;
6310 if (!saved_processing_template_decl)
6311 processing_template_decl = 1;
6313 dependent_p = dependent_type_p (t);
6314 else if (TREE_CODE (t) == TREE_VEC)
6315 dependent_p = any_dependent_template_arguments_p (t);
6316 else if (TREE_CODE (t) == TREE_LIST)
6317 dependent_p = (uses_template_parms (TREE_VALUE (t))
6318 || uses_template_parms (TREE_CHAIN (t)));
6319 else if (TREE_CODE (t) == TYPE_DECL)
6320 dependent_p = dependent_type_p (TREE_TYPE (t));
6323 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
6324 || TREE_CODE (t) == OVERLOAD
6325 || TREE_CODE (t) == BASELINK
6326 || TREE_CODE (t) == IDENTIFIER_NODE
6327 || TREE_CODE (t) == TRAIT_EXPR
6328 || TREE_CODE (t) == CONSTRUCTOR
6329 || CONSTANT_CLASS_P (t))
6330 dependent_p = (type_dependent_expression_p (t)
6331 || value_dependent_expression_p (t));
6334 gcc_assert (t == error_mark_node);
6335 dependent_p = false;
6338 processing_template_decl = saved_processing_template_decl;
6343 /* Returns true if T depends on any template parameter with level LEVEL. */
6346 uses_template_parms_level (tree t, int level)
6348 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
6349 /*include_nondeduced_p=*/true);
6352 static int tinst_depth;
6353 extern int max_tinst_depth;
6354 #ifdef GATHER_STATISTICS
6357 static int tinst_level_tick;
6358 static int last_template_error_tick;
6360 /* We're starting to instantiate D; record the template instantiation context
6361 for diagnostics and to restore it later. */
6364 push_tinst_level (tree d)
6366 struct tinst_level *new_level;
6368 if (tinst_depth >= max_tinst_depth)
6370 /* If the instantiation in question still has unbound template parms,
6371 we don't really care if we can't instantiate it, so just return.
6372 This happens with base instantiation for implicit `typename'. */
6373 if (uses_template_parms (d))
6376 last_template_error_tick = tinst_level_tick;
6377 error ("template instantiation depth exceeds maximum of %d (use "
6378 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
6379 max_tinst_depth, d);
6381 print_instantiation_context ();
6386 new_level = GGC_NEW (struct tinst_level);
6387 new_level->decl = d;
6388 new_level->locus = input_location;
6389 new_level->in_system_header_p = in_system_header;
6390 new_level->next = current_tinst_level;
6391 current_tinst_level = new_level;
6394 #ifdef GATHER_STATISTICS
6395 if (tinst_depth > depth_reached)
6396 depth_reached = tinst_depth;
6403 /* We're done instantiating this template; return to the instantiation
6407 pop_tinst_level (void)
6409 /* Restore the filename and line number stashed away when we started
6410 this instantiation. */
6411 input_location = current_tinst_level->locus;
6412 current_tinst_level = current_tinst_level->next;
6417 /* We're instantiating a deferred template; restore the template
6418 instantiation context in which the instantiation was requested, which
6419 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
6422 reopen_tinst_level (struct tinst_level *level)
6424 struct tinst_level *t;
6427 for (t = level; t; t = t->next)
6430 current_tinst_level = level;
6435 /* Returns the TINST_LEVEL which gives the original instantiation
6438 struct tinst_level *
6439 outermost_tinst_level (void)
6441 struct tinst_level *level = current_tinst_level;
6444 level = level->next;
6448 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
6451 parameter_of_template_p (tree parm, tree templ)
6456 if (!parm || !templ)
6459 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
6460 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
6462 parms = DECL_TEMPLATE_PARMS (templ);
6463 parms = INNERMOST_TEMPLATE_PARMS (parms);
6465 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
6466 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
6472 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
6473 vector of template arguments, as for tsubst.
6475 Returns an appropriate tsubst'd friend declaration. */
6478 tsubst_friend_function (tree decl, tree args)
6482 if (TREE_CODE (decl) == FUNCTION_DECL
6483 && DECL_TEMPLATE_INSTANTIATION (decl)
6484 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
6485 /* This was a friend declared with an explicit template
6486 argument list, e.g.:
6490 to indicate that f was a template instantiation, not a new
6491 function declaration. Now, we have to figure out what
6492 instantiation of what template. */
6494 tree template_id, arglist, fns;
6497 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
6499 /* Friend functions are looked up in the containing namespace scope.
6500 We must enter that scope, to avoid finding member functions of the
6501 current class with same name. */
6502 push_nested_namespace (ns);
6503 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
6504 tf_warning_or_error, NULL_TREE,
6505 /*integral_constant_expression_p=*/false);
6506 pop_nested_namespace (ns);
6507 arglist = tsubst (DECL_TI_ARGS (decl), args,
6508 tf_warning_or_error, NULL_TREE);
6509 template_id = lookup_template_function (fns, arglist);
6511 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6512 tmpl = determine_specialization (template_id, new_friend,
6514 /*need_member_template=*/0,
6515 TREE_VEC_LENGTH (args),
6517 return instantiate_template (tmpl, new_args, tf_error);
6520 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6522 /* The NEW_FRIEND will look like an instantiation, to the
6523 compiler, but is not an instantiation from the point of view of
6524 the language. For example, we might have had:
6526 template <class T> struct S {
6527 template <class U> friend void f(T, U);
6530 Then, in S<int>, template <class U> void f(int, U) is not an
6531 instantiation of anything. */
6532 if (new_friend == error_mark_node)
6533 return error_mark_node;
6535 DECL_USE_TEMPLATE (new_friend) = 0;
6536 if (TREE_CODE (decl) == TEMPLATE_DECL)
6538 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
6539 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
6540 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
6543 /* The mangled name for the NEW_FRIEND is incorrect. The function
6544 is not a template instantiation and should not be mangled like
6545 one. Therefore, we forget the mangling here; we'll recompute it
6546 later if we need it. */
6547 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
6549 SET_DECL_RTL (new_friend, NULL_RTX);
6550 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
6553 if (DECL_NAMESPACE_SCOPE_P (new_friend))
6556 tree new_friend_template_info;
6557 tree new_friend_result_template_info;
6559 int new_friend_is_defn;
6561 /* We must save some information from NEW_FRIEND before calling
6562 duplicate decls since that function will free NEW_FRIEND if
6564 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
6565 new_friend_is_defn =
6566 (DECL_INITIAL (DECL_TEMPLATE_RESULT
6567 (template_for_substitution (new_friend)))
6569 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
6571 /* This declaration is a `primary' template. */
6572 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
6574 new_friend_result_template_info
6575 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
6578 new_friend_result_template_info = NULL_TREE;
6580 /* Make the init_value nonzero so pushdecl knows this is a defn. */
6581 if (new_friend_is_defn)
6582 DECL_INITIAL (new_friend) = error_mark_node;
6584 /* Inside pushdecl_namespace_level, we will push into the
6585 current namespace. However, the friend function should go
6586 into the namespace of the template. */
6587 ns = decl_namespace_context (new_friend);
6588 push_nested_namespace (ns);
6589 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
6590 pop_nested_namespace (ns);
6592 if (old_decl == error_mark_node)
6593 return error_mark_node;
6595 if (old_decl != new_friend)
6597 /* This new friend declaration matched an existing
6598 declaration. For example, given:
6600 template <class T> void f(T);
6601 template <class U> class C {
6602 template <class T> friend void f(T) {}
6605 the friend declaration actually provides the definition
6606 of `f', once C has been instantiated for some type. So,
6607 old_decl will be the out-of-class template declaration,
6608 while new_friend is the in-class definition.
6610 But, if `f' was called before this point, the
6611 instantiation of `f' will have DECL_TI_ARGS corresponding
6612 to `T' but not to `U', references to which might appear
6613 in the definition of `f'. Previously, the most general
6614 template for an instantiation of `f' was the out-of-class
6615 version; now it is the in-class version. Therefore, we
6616 run through all specialization of `f', adding to their
6617 DECL_TI_ARGS appropriately. In particular, they need a
6618 new set of outer arguments, corresponding to the
6619 arguments for this class instantiation.
6621 The same situation can arise with something like this:
6624 template <class T> class C {
6628 when `C<int>' is instantiated. Now, `f(int)' is defined
6631 if (!new_friend_is_defn)
6632 /* On the other hand, if the in-class declaration does
6633 *not* provide a definition, then we don't want to alter
6634 existing definitions. We can just leave everything
6639 /* Overwrite whatever template info was there before, if
6640 any, with the new template information pertaining to
6642 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
6644 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
6645 reregister_specialization (new_friend,
6646 most_general_template (old_decl),
6651 tree new_friend_args;
6653 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
6654 = new_friend_result_template_info;
6656 new_friend_args = TI_ARGS (new_friend_template_info);
6657 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
6661 tree spec = TREE_VALUE (t);
6664 = add_outermost_template_args (new_friend_args,
6665 DECL_TI_ARGS (spec));
6668 /* Now, since specializations are always supposed to
6669 hang off of the most general template, we must move
6671 t = most_general_template (old_decl);
6674 DECL_TEMPLATE_SPECIALIZATIONS (t)
6675 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
6676 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
6677 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
6682 /* The information from NEW_FRIEND has been merged into OLD_DECL
6683 by duplicate_decls. */
6684 new_friend = old_decl;
6689 tree context = DECL_CONTEXT (new_friend);
6693 template <class T> class C {
6694 template <class U> friend void C1<U>::f (); // case 1
6695 friend void C2<T>::f (); // case 2
6697 we only need to make sure CONTEXT is a complete type for
6698 case 2. To distinguish between the two cases, we note that
6699 CONTEXT of case 1 remains dependent type after tsubst while
6700 this isn't true for case 2. */
6701 ++processing_template_decl;
6702 dependent_p = dependent_type_p (context);
6703 --processing_template_decl;
6706 && !complete_type_or_else (context, NULL_TREE))
6707 return error_mark_node;
6709 if (COMPLETE_TYPE_P (context))
6711 /* Check to see that the declaration is really present, and,
6712 possibly obtain an improved declaration. */
6713 tree fn = check_classfn (context,
6714 new_friend, NULL_TREE);
6724 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
6725 template arguments, as for tsubst.
6727 Returns an appropriate tsubst'd friend type or error_mark_node on
6731 tsubst_friend_class (tree friend_tmpl, tree args)
6737 context = DECL_CONTEXT (friend_tmpl);
6741 if (TREE_CODE (context) == NAMESPACE_DECL)
6742 push_nested_namespace (context);
6744 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
6747 /* Look for a class template declaration. We look for hidden names
6748 because two friend declarations of the same template are the
6749 same. For example, in:
6752 template <typename> friend class F;
6754 template <typename> struct B {
6755 template <typename> friend class F;
6758 both F templates are the same. */
6759 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
6760 /*block_p=*/true, 0,
6761 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
6763 /* But, if we don't find one, it might be because we're in a
6764 situation like this:
6772 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
6773 for `S<int>', not the TEMPLATE_DECL. */
6774 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
6776 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
6777 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
6780 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
6782 /* The friend template has already been declared. Just
6783 check to see that the declarations match, and install any new
6784 default parameters. We must tsubst the default parameters,
6785 of course. We only need the innermost template parameters
6786 because that is all that redeclare_class_template will look
6788 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
6789 > TMPL_ARGS_DEPTH (args))
6792 location_t saved_input_location;
6793 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
6794 args, tf_warning_or_error);
6796 saved_input_location = input_location;
6797 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
6798 redeclare_class_template (TREE_TYPE (tmpl), parms);
6799 input_location = saved_input_location;
6803 friend_type = TREE_TYPE (tmpl);
6807 /* The friend template has not already been declared. In this
6808 case, the instantiation of the template class will cause the
6809 injection of this template into the global scope. */
6810 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
6811 if (tmpl == error_mark_node)
6812 return error_mark_node;
6814 /* The new TMPL is not an instantiation of anything, so we
6815 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
6816 the new type because that is supposed to be the corresponding
6817 template decl, i.e., TMPL. */
6818 DECL_USE_TEMPLATE (tmpl) = 0;
6819 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
6820 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
6821 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
6822 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
6824 /* Inject this template into the global scope. */
6825 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
6830 if (TREE_CODE (context) == NAMESPACE_DECL)
6831 pop_nested_namespace (context);
6833 pop_nested_class ();
6839 /* Returns zero if TYPE cannot be completed later due to circularity.
6840 Otherwise returns one. */
6843 can_complete_type_without_circularity (tree type)
6845 if (type == NULL_TREE || type == error_mark_node)
6847 else if (COMPLETE_TYPE_P (type))
6849 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
6850 return can_complete_type_without_circularity (TREE_TYPE (type));
6851 else if (CLASS_TYPE_P (type)
6852 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
6858 /* Apply any attributes which had to be deferred until instantiation
6859 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
6860 ARGS, COMPLAIN, IN_DECL are as tsubst. */
6863 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
6864 tree args, tsubst_flags_t complain, tree in_decl)
6866 tree last_dep = NULL_TREE;
6870 for (t = attributes; t; t = TREE_CHAIN (t))
6871 if (ATTR_IS_DEPENDENT (t))
6874 attributes = copy_list (attributes);
6878 if (DECL_P (*decl_p))
6880 if (TREE_TYPE (*decl_p) == error_mark_node)
6882 p = &DECL_ATTRIBUTES (*decl_p);
6885 p = &TYPE_ATTRIBUTES (*decl_p);
6889 tree late_attrs = NULL_TREE;
6890 tree *q = &late_attrs;
6892 for (*p = attributes; *p; )
6895 if (ATTR_IS_DEPENDENT (t))
6897 *p = TREE_CHAIN (t);
6898 TREE_CHAIN (t) = NULL_TREE;
6899 /* If the first attribute argument is an identifier, don't
6900 pass it through tsubst. Attributes like mode, format,
6901 cleanup and several target specific attributes expect it
6904 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
6905 && TREE_VALUE (TREE_VALUE (t))
6906 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
6907 == IDENTIFIER_NODE))
6910 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
6912 /*integral_constant_expression_p=*/false);
6913 if (chain != TREE_CHAIN (TREE_VALUE (t)))
6915 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
6920 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
6921 /*integral_constant_expression_p=*/false);
6923 q = &TREE_CHAIN (t);
6926 p = &TREE_CHAIN (t);
6929 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
6933 /* Perform (or defer) access check for typedefs that were referenced
6934 from within the template TMPL code.
6935 This is a subroutine of instantiate_template and instantiate_class_template.
6936 TMPL is the template to consider and TARGS is the list of arguments of
6940 perform_typedefs_access_check (tree tmpl, tree targs)
6944 if (!tmpl || TREE_CODE (tmpl) != TEMPLATE_DECL)
6947 for (t = MEMBER_TYPES_NEEDING_ACCESS_CHECK (tmpl); t; t = TREE_CHAIN (t))
6949 tree type_decl = TREE_PURPOSE (t);
6950 tree type_scope = TREE_VALUE (t);
6952 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
6955 if (uses_template_parms (type_decl))
6956 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
6957 if (uses_template_parms (type_scope))
6958 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
6960 perform_or_defer_access_check (TYPE_BINFO (type_scope), type_decl, type_decl);
6965 instantiate_class_template (tree type)
6967 tree templ, args, pattern, t, member;
6972 if (type == error_mark_node)
6973 return error_mark_node;
6975 if (TYPE_BEING_DEFINED (type)
6976 || COMPLETE_TYPE_P (type)
6977 || dependent_type_p (type))
6980 /* Figure out which template is being instantiated. */
6981 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
6982 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
6984 /* Determine what specialization of the original template to
6986 t = most_specialized_class (type, templ);
6987 if (t == error_mark_node)
6989 TYPE_BEING_DEFINED (type) = 1;
6990 return error_mark_node;
6994 /* This TYPE is actually an instantiation of a partial
6995 specialization. We replace the innermost set of ARGS with
6996 the arguments appropriate for substitution. For example,
6999 template <class T> struct S {};
7000 template <class T> struct S<T*> {};
7002 and supposing that we are instantiating S<int*>, ARGS will
7003 presently be {int*} -- but we need {int}. */
7004 pattern = TREE_TYPE (t);
7005 args = TREE_PURPOSE (t);
7009 pattern = TREE_TYPE (templ);
7010 args = CLASSTYPE_TI_ARGS (type);
7013 /* If the template we're instantiating is incomplete, then clearly
7014 there's nothing we can do. */
7015 if (!COMPLETE_TYPE_P (pattern))
7018 /* If we've recursively instantiated too many templates, stop. */
7019 if (! push_tinst_level (type))
7022 /* Now we're really doing the instantiation. Mark the type as in
7023 the process of being defined. */
7024 TYPE_BEING_DEFINED (type) = 1;
7026 /* We may be in the middle of deferred access check. Disable
7028 push_deferring_access_checks (dk_no_deferred);
7030 push_to_top_level ();
7032 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
7034 /* Set the input location to the template definition. This is needed
7035 if tsubsting causes an error. */
7036 typedecl = TYPE_MAIN_DECL (type);
7037 input_location = DECL_SOURCE_LOCATION (typedecl);
7039 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
7040 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
7041 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
7042 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
7043 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
7044 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
7045 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
7046 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
7047 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
7048 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
7049 TYPE_PACKED (type) = TYPE_PACKED (pattern);
7050 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
7051 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
7052 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
7053 if (ANON_AGGR_TYPE_P (pattern))
7054 SET_ANON_AGGR_TYPE_P (type);
7055 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
7057 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
7058 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
7061 pbinfo = TYPE_BINFO (pattern);
7063 /* We should never instantiate a nested class before its enclosing
7064 class; we need to look up the nested class by name before we can
7065 instantiate it, and that lookup should instantiate the enclosing
7067 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
7068 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
7069 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
7071 base_list = NULL_TREE;
7072 if (BINFO_N_BASE_BINFOS (pbinfo))
7075 tree context = TYPE_CONTEXT (type);
7079 /* We must enter the scope containing the type, as that is where
7080 the accessibility of types named in dependent bases are
7082 pushed_scope = push_scope (context ? context : global_namespace);
7084 /* Substitute into each of the bases to determine the actual
7086 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
7089 tree access = BINFO_BASE_ACCESS (pbinfo, i);
7090 tree expanded_bases = NULL_TREE;
7093 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
7096 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
7097 args, tf_error, NULL_TREE);
7098 if (expanded_bases == error_mark_node)
7101 len = TREE_VEC_LENGTH (expanded_bases);
7104 for (idx = 0; idx < len; idx++)
7107 /* Extract the already-expanded base class. */
7108 base = TREE_VEC_ELT (expanded_bases, idx);
7110 /* Substitute to figure out the base class. */
7111 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
7114 if (base == error_mark_node)
7117 base_list = tree_cons (access, base, base_list);
7118 if (BINFO_VIRTUAL_P (pbase_binfo))
7119 TREE_TYPE (base_list) = integer_type_node;
7123 /* The list is now in reverse order; correct that. */
7124 base_list = nreverse (base_list);
7127 pop_scope (pushed_scope);
7129 /* Now call xref_basetypes to set up all the base-class
7131 xref_basetypes (type, base_list);
7133 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
7134 (int) ATTR_FLAG_TYPE_IN_PLACE,
7135 args, tf_error, NULL_TREE);
7137 /* Now that our base classes are set up, enter the scope of the
7138 class, so that name lookups into base classes, etc. will work
7139 correctly. This is precisely analogous to what we do in
7140 begin_class_definition when defining an ordinary non-template
7141 class, except we also need to push the enclosing classes. */
7142 push_nested_class (type);
7144 /* Now members are processed in the order of declaration. */
7145 for (member = CLASSTYPE_DECL_LIST (pattern);
7146 member; member = TREE_CHAIN (member))
7148 tree t = TREE_VALUE (member);
7150 if (TREE_PURPOSE (member))
7154 /* Build new CLASSTYPE_NESTED_UTDS. */
7157 bool class_template_p;
7159 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
7160 && TYPE_LANG_SPECIFIC (t)
7161 && CLASSTYPE_IS_TEMPLATE (t));
7162 /* If the member is a class template, then -- even after
7163 substitution -- there may be dependent types in the
7164 template argument list for the class. We increment
7165 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7166 that function will assume that no types are dependent
7167 when outside of a template. */
7168 if (class_template_p)
7169 ++processing_template_decl;
7170 newtag = tsubst (t, args, tf_error, NULL_TREE);
7171 if (class_template_p)
7172 --processing_template_decl;
7173 if (newtag == error_mark_node)
7176 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
7178 tree name = TYPE_IDENTIFIER (t);
7180 if (class_template_p)
7181 /* Unfortunately, lookup_template_class sets
7182 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
7183 instantiation (i.e., for the type of a member
7184 template class nested within a template class.)
7185 This behavior is required for
7186 maybe_process_partial_specialization to work
7187 correctly, but is not accurate in this case;
7188 the TAG is not an instantiation of anything.
7189 (The corresponding TEMPLATE_DECL is an
7190 instantiation, but the TYPE is not.) */
7191 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
7193 /* Now, we call pushtag to put this NEWTAG into the scope of
7194 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
7195 pushtag calling push_template_decl. We don't have to do
7196 this for enums because it will already have been done in
7199 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
7200 pushtag (name, newtag, /*tag_scope=*/ts_current);
7203 else if (TREE_CODE (t) == FUNCTION_DECL
7204 || DECL_FUNCTION_TEMPLATE_P (t))
7206 /* Build new TYPE_METHODS. */
7209 if (TREE_CODE (t) == TEMPLATE_DECL)
7210 ++processing_template_decl;
7211 r = tsubst (t, args, tf_error, NULL_TREE);
7212 if (TREE_CODE (t) == TEMPLATE_DECL)
7213 --processing_template_decl;
7214 set_current_access_from_decl (r);
7215 finish_member_declaration (r);
7219 /* Build new TYPE_FIELDS. */
7220 if (TREE_CODE (t) == STATIC_ASSERT)
7223 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
7224 tf_warning_or_error, NULL_TREE,
7225 /*integral_constant_expression_p=*/true);
7226 finish_static_assert (condition,
7227 STATIC_ASSERT_MESSAGE (t),
7228 STATIC_ASSERT_SOURCE_LOCATION (t),
7231 else if (TREE_CODE (t) != CONST_DECL)
7235 /* The file and line for this declaration, to
7236 assist in error message reporting. Since we
7237 called push_tinst_level above, we don't need to
7239 input_location = DECL_SOURCE_LOCATION (t);
7241 if (TREE_CODE (t) == TEMPLATE_DECL)
7242 ++processing_template_decl;
7243 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
7244 if (TREE_CODE (t) == TEMPLATE_DECL)
7245 --processing_template_decl;
7246 if (TREE_CODE (r) == VAR_DECL)
7250 [t]he initialization (and any associated
7251 side-effects) of a static data member does
7252 not occur unless the static data member is
7253 itself used in a way that requires the
7254 definition of the static data member to
7257 Therefore, we do not substitute into the
7258 initialized for the static data member here. */
7259 finish_static_data_member_decl
7262 /*init_const_expr_p=*/false,
7263 /*asmspec_tree=*/NULL_TREE,
7265 if (DECL_INITIALIZED_IN_CLASS_P (r))
7266 check_static_variable_definition (r, TREE_TYPE (r));
7268 else if (TREE_CODE (r) == FIELD_DECL)
7270 /* Determine whether R has a valid type and can be
7271 completed later. If R is invalid, then it is
7272 replaced by error_mark_node so that it will not be
7273 added to TYPE_FIELDS. */
7274 tree rtype = TREE_TYPE (r);
7275 if (can_complete_type_without_circularity (rtype))
7276 complete_type (rtype);
7278 if (!COMPLETE_TYPE_P (rtype))
7280 cxx_incomplete_type_error (r, rtype);
7281 r = error_mark_node;
7285 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
7286 such a thing will already have been added to the field
7287 list by tsubst_enum in finish_member_declaration in the
7288 CLASSTYPE_NESTED_UTDS case above. */
7289 if (!(TREE_CODE (r) == TYPE_DECL
7290 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
7291 && DECL_ARTIFICIAL (r)))
7293 set_current_access_from_decl (r);
7294 finish_member_declaration (r);
7301 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
7303 /* Build new CLASSTYPE_FRIEND_CLASSES. */
7305 tree friend_type = t;
7306 bool adjust_processing_template_decl = false;
7308 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7310 /* template <class T> friend class C; */
7311 friend_type = tsubst_friend_class (friend_type, args);
7312 adjust_processing_template_decl = true;
7314 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
7316 /* template <class T> friend class C::D; */
7317 friend_type = tsubst (friend_type, args,
7318 tf_warning_or_error, NULL_TREE);
7319 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7320 friend_type = TREE_TYPE (friend_type);
7321 adjust_processing_template_decl = true;
7323 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
7325 /* This could be either
7329 when dependent_type_p is false or
7331 template <class U> friend class T::C;
7334 friend_type = tsubst (friend_type, args,
7335 tf_warning_or_error, NULL_TREE);
7336 /* Bump processing_template_decl for correct
7337 dependent_type_p calculation. */
7338 ++processing_template_decl;
7339 if (dependent_type_p (friend_type))
7340 adjust_processing_template_decl = true;
7341 --processing_template_decl;
7343 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
7344 && hidden_name_p (TYPE_NAME (friend_type)))
7348 where C hasn't been declared yet. Let's lookup name
7349 from namespace scope directly, bypassing any name that
7350 come from dependent base class. */
7351 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
7353 /* The call to xref_tag_from_type does injection for friend
7355 push_nested_namespace (ns);
7357 xref_tag_from_type (friend_type, NULL_TREE,
7358 /*tag_scope=*/ts_current);
7359 pop_nested_namespace (ns);
7361 else if (uses_template_parms (friend_type))
7362 /* friend class C<T>; */
7363 friend_type = tsubst (friend_type, args,
7364 tf_warning_or_error, NULL_TREE);
7369 where C is already declared or
7371 friend class C<int>;
7373 We don't have to do anything in these cases. */
7375 if (adjust_processing_template_decl)
7376 /* Trick make_friend_class into realizing that the friend
7377 we're adding is a template, not an ordinary class. It's
7378 important that we use make_friend_class since it will
7379 perform some error-checking and output cross-reference
7381 ++processing_template_decl;
7383 if (friend_type != error_mark_node)
7384 make_friend_class (type, friend_type, /*complain=*/false);
7386 if (adjust_processing_template_decl)
7387 --processing_template_decl;
7391 /* Build new DECL_FRIENDLIST. */
7394 /* The file and line for this declaration, to
7395 assist in error message reporting. Since we
7396 called push_tinst_level above, we don't need to
7398 input_location = DECL_SOURCE_LOCATION (t);
7400 if (TREE_CODE (t) == TEMPLATE_DECL)
7402 ++processing_template_decl;
7403 push_deferring_access_checks (dk_no_check);
7406 r = tsubst_friend_function (t, args);
7407 add_friend (type, r, /*complain=*/false);
7408 if (TREE_CODE (t) == TEMPLATE_DECL)
7410 pop_deferring_access_checks ();
7411 --processing_template_decl;
7417 /* Set the file and line number information to whatever is given for
7418 the class itself. This puts error messages involving generated
7419 implicit functions at a predictable point, and the same point
7420 that would be used for non-template classes. */
7421 input_location = DECL_SOURCE_LOCATION (typedecl);
7423 unreverse_member_declarations (type);
7424 finish_struct_1 (type);
7425 TYPE_BEING_DEFINED (type) = 0;
7427 /* Now that the class is complete, instantiate default arguments for
7428 any member functions. We don't do this earlier because the
7429 default arguments may reference members of the class. */
7430 if (!PRIMARY_TEMPLATE_P (templ))
7431 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
7432 if (TREE_CODE (t) == FUNCTION_DECL
7433 /* Implicitly generated member functions will not have template
7434 information; they are not instantiations, but instead are
7435 created "fresh" for each instantiation. */
7436 && DECL_TEMPLATE_INFO (t))
7437 tsubst_default_arguments (t);
7439 /* Some typedefs referenced from within the template code need to be access
7440 checked at template instantiation time, i.e now. These types were
7441 added to the template at parsing time. Let's get those and perform
7442 the acces checks then. */
7443 perform_typedefs_access_check (templ, args);
7444 perform_deferred_access_checks ();
7445 pop_nested_class ();
7446 pop_from_top_level ();
7447 pop_deferring_access_checks ();
7450 /* The vtable for a template class can be emitted in any translation
7451 unit in which the class is instantiated. When there is no key
7452 method, however, finish_struct_1 will already have added TYPE to
7453 the keyed_classes list. */
7454 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
7455 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
7461 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7467 else if (TYPE_P (t))
7468 r = tsubst (t, args, complain, in_decl);
7471 r = tsubst_expr (t, args, complain, in_decl,
7472 /*integral_constant_expression_p=*/true);
7473 r = fold_non_dependent_expr (r);
7478 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
7479 NONTYPE_ARGUMENT_PACK. */
7482 make_fnparm_pack (tree spec_parm)
7484 /* Collect all of the extra "packed" parameters into an
7488 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
7489 tree argtypepack = make_node (TYPE_ARGUMENT_PACK);
7490 int i, len = list_length (spec_parm);
7492 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
7493 parmvec = make_tree_vec (len);
7494 parmtypevec = make_tree_vec (len);
7495 for (i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
7497 TREE_VEC_ELT (parmvec, i) = spec_parm;
7498 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
7501 /* Build the argument packs. */
7502 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
7503 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
7504 TREE_TYPE (argpack) = argtypepack;
7509 /* Substitute ARGS into T, which is an pack expansion
7510 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
7511 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
7512 (if only a partial substitution could be performed) or
7513 ERROR_MARK_NODE if there was an error. */
7515 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
7519 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
7520 tree first_arg_pack; int i, len = -1;
7523 bool very_local_specializations = false;
7525 gcc_assert (PACK_EXPANSION_P (t));
7526 pattern = PACK_EXPANSION_PATTERN (t);
7528 /* Determine the argument packs that will instantiate the parameter
7529 packs used in the expansion expression. While we're at it,
7530 compute the number of arguments to be expanded and make sure it
7532 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
7533 pack = TREE_CHAIN (pack))
7535 tree parm_pack = TREE_VALUE (pack);
7536 tree arg_pack = NULL_TREE;
7537 tree orig_arg = NULL_TREE;
7539 if (TREE_CODE (parm_pack) == PARM_DECL)
7541 arg_pack = retrieve_local_specialization (parm_pack);
7542 if (arg_pack == NULL_TREE)
7544 /* This can happen for a parameter name used later in a function
7545 declaration (such as in a late-specified return type). Just
7546 make a dummy decl, since it's only used for its type. */
7547 gcc_assert (skip_evaluation);
7548 arg_pack = tsubst_decl (parm_pack, args, complain);
7549 arg_pack = make_fnparm_pack (arg_pack);
7554 int level, idx, levels;
7555 template_parm_level_and_index (parm_pack, &level, &idx);
7557 levels = TMPL_ARGS_DEPTH (args);
7558 if (level <= levels)
7559 arg_pack = TMPL_ARG (args, level, idx);
7562 orig_arg = arg_pack;
7563 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
7564 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
7566 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
7567 /* This can only happen if we forget to expand an argument
7568 pack somewhere else. Just return an error, silently. */
7570 result = make_tree_vec (1);
7571 TREE_VEC_ELT (result, 0) = error_mark_node;
7576 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
7577 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
7579 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
7580 tree pattern = PACK_EXPANSION_PATTERN (expansion);
7581 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
7582 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
7583 /* The argument pack that the parameter maps to is just an
7584 expansion of the parameter itself, such as one would
7585 find in the implicit typedef of a class inside the
7586 class itself. Consider this parameter "unsubstituted",
7587 so that we will maintain the outer pack expansion. */
7588 arg_pack = NULL_TREE;
7594 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
7596 /* It's all-or-nothing with incomplete argument packs. */
7597 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7598 return error_mark_node;
7600 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7606 first_arg_pack = arg_pack;
7608 else if (len != my_len)
7610 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
7611 error ("mismatched argument pack lengths while expanding "
7615 error ("mismatched argument pack lengths while expanding "
7618 return error_mark_node;
7621 /* Keep track of the parameter packs and their corresponding
7623 packs = tree_cons (parm_pack, arg_pack, packs);
7624 TREE_TYPE (packs) = orig_arg;
7627 /* We can't substitute for this parameter pack. */
7628 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
7630 unsubstituted_packs);
7633 /* We cannot expand this expansion expression, because we don't have
7634 all of the argument packs we need. Substitute into the pattern
7635 and return a PACK_EXPANSION_*. The caller will need to deal with
7637 if (unsubstituted_packs)
7638 return make_pack_expansion (tsubst (pattern, args, complain,
7641 /* We could not find any argument packs that work. */
7643 return error_mark_node;
7645 if (!local_specializations)
7647 /* We're in a late-specified return type, so we don't have a local
7648 specializations table. Create one for doing this expansion. */
7649 very_local_specializations = true;
7650 local_specializations = htab_create (37,
7651 hash_local_specialization,
7652 eq_local_specializations,
7656 /* For each argument in each argument pack, substitute into the
7658 result = make_tree_vec (len + incomplete);
7659 for (i = 0; i < len + incomplete; ++i)
7661 /* For parameter pack, change the substitution of the parameter
7662 pack to the ith argument in its argument pack, then expand
7664 for (pack = packs; pack; pack = TREE_CHAIN (pack))
7666 tree parm = TREE_PURPOSE (pack);
7668 if (TREE_CODE (parm) == PARM_DECL)
7670 /* Select the Ith argument from the pack. */
7671 tree arg = make_node (ARGUMENT_PACK_SELECT);
7672 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
7673 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
7675 register_local_specialization (arg, parm);
7681 template_parm_level_and_index (parm, &level, &idx);
7685 /* Select the Ith argument from the pack. */
7686 value = make_node (ARGUMENT_PACK_SELECT);
7687 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
7688 ARGUMENT_PACK_SELECT_INDEX (value) = i;
7691 /* Update the corresponding argument. */
7692 TMPL_ARG (args, level, idx) = value;
7696 /* Substitute into the PATTERN with the altered arguments. */
7697 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
7698 TREE_VEC_ELT (result, i) =
7699 tsubst_expr (pattern, args, complain, in_decl,
7700 /*integral_constant_expression_p=*/false);
7702 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
7705 /* When we have incomplete argument packs, the last "expanded"
7706 result is itself a pack expansion, which allows us
7707 to deduce more arguments. */
7708 TREE_VEC_ELT (result, i) =
7709 make_pack_expansion (TREE_VEC_ELT (result, i));
7711 if (TREE_VEC_ELT (result, i) == error_mark_node)
7713 result = error_mark_node;
7718 /* Update ARGS to restore the substitution from parameter packs to
7719 their argument packs. */
7720 for (pack = packs; pack; pack = TREE_CHAIN (pack))
7722 tree parm = TREE_PURPOSE (pack);
7724 if (TREE_CODE (parm) == PARM_DECL)
7725 register_local_specialization (TREE_TYPE (pack), parm);
7729 template_parm_level_and_index (parm, &level, &idx);
7731 /* Update the corresponding argument. */
7732 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
7733 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
7736 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
7740 if (very_local_specializations)
7742 htab_delete (local_specializations);
7743 local_specializations = NULL;
7749 /* Substitute ARGS into the vector or list of template arguments T. */
7752 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7755 int len = TREE_VEC_LENGTH (t);
7756 int need_new = 0, i, expanded_len_adjust = 0, out;
7757 tree *elts = (tree *) alloca (len * sizeof (tree));
7759 for (i = 0; i < len; i++)
7761 tree orig_arg = TREE_VEC_ELT (t, i);
7764 if (TREE_CODE (orig_arg) == TREE_VEC)
7765 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
7766 else if (PACK_EXPANSION_P (orig_arg))
7768 /* Substitute into an expansion expression. */
7769 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
7771 if (TREE_CODE (new_arg) == TREE_VEC)
7772 /* Add to the expanded length adjustment the number of
7773 expanded arguments. We subtract one from this
7774 measurement, because the argument pack expression
7775 itself is already counted as 1 in
7776 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
7777 the argument pack is empty. */
7778 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
7780 else if (ARGUMENT_PACK_P (orig_arg))
7782 /* Substitute into each of the arguments. */
7783 new_arg = make_node (TREE_CODE (orig_arg));
7785 SET_ARGUMENT_PACK_ARGS (
7787 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
7788 args, complain, in_decl));
7790 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
7791 new_arg = error_mark_node;
7793 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
7794 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
7796 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
7798 if (TREE_TYPE (new_arg) == error_mark_node)
7799 new_arg = error_mark_node;
7803 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
7805 if (new_arg == error_mark_node)
7806 return error_mark_node;
7809 if (new_arg != orig_arg)
7816 /* Make space for the expanded arguments coming from template
7818 t = make_tree_vec (len + expanded_len_adjust);
7819 for (i = 0, out = 0; i < len; i++)
7821 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
7822 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
7823 && TREE_CODE (elts[i]) == TREE_VEC)
7827 /* Now expand the template argument pack "in place". */
7828 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
7829 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
7833 TREE_VEC_ELT (t, out) = elts[i];
7841 /* Return the result of substituting ARGS into the template parameters
7842 given by PARMS. If there are m levels of ARGS and m + n levels of
7843 PARMS, then the result will contain n levels of PARMS. For
7844 example, if PARMS is `template <class T> template <class U>
7845 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
7846 result will be `template <int*, double, class V>'. */
7849 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
7854 /* When substituting into a template, we must set
7855 PROCESSING_TEMPLATE_DECL as the template parameters may be
7856 dependent if they are based on one-another, and the dependency
7857 predicates are short-circuit outside of templates. */
7858 ++processing_template_decl;
7860 for (new_parms = &r;
7861 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
7862 new_parms = &(TREE_CHAIN (*new_parms)),
7863 parms = TREE_CHAIN (parms))
7866 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
7869 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
7875 if (parms == error_mark_node)
7878 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
7880 if (tuple == error_mark_node)
7883 default_value = TREE_PURPOSE (tuple);
7884 parm_decl = TREE_VALUE (tuple);
7886 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
7887 if (TREE_CODE (parm_decl) == PARM_DECL
7888 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
7889 parm_decl = error_mark_node;
7890 default_value = tsubst_template_arg (default_value, args,
7891 complain, NULL_TREE);
7893 tuple = build_tree_list (default_value, parm_decl);
7894 TREE_VEC_ELT (new_vec, i) = tuple;
7898 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
7899 - TMPL_ARGS_DEPTH (args)),
7900 new_vec, NULL_TREE);
7903 --processing_template_decl;
7908 /* Substitute the ARGS into the indicated aggregate (or enumeration)
7909 type T. If T is not an aggregate or enumeration type, it is
7910 handled as if by tsubst. IN_DECL is as for tsubst. If
7911 ENTERING_SCOPE is nonzero, T is the context for a template which
7912 we are presently tsubst'ing. Return the substituted value. */
7915 tsubst_aggr_type (tree t,
7917 tsubst_flags_t complain,
7924 switch (TREE_CODE (t))
7927 if (TYPE_PTRMEMFUNC_P (t))
7928 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
7930 /* Else fall through. */
7933 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
7938 bool saved_skip_evaluation;
7940 /* In "sizeof(X<I>)" we need to evaluate "I". */
7941 saved_skip_evaluation = skip_evaluation;
7942 skip_evaluation = false;
7944 /* First, determine the context for the type we are looking
7946 context = TYPE_CONTEXT (t);
7949 context = tsubst_aggr_type (context, args, complain,
7950 in_decl, /*entering_scope=*/1);
7951 /* If context is a nested class inside a class template,
7952 it may still need to be instantiated (c++/33959). */
7953 if (TYPE_P (context))
7954 context = complete_type (context);
7957 /* Then, figure out what arguments are appropriate for the
7958 type we are trying to find. For example, given:
7960 template <class T> struct S;
7961 template <class T, class U> void f(T, U) { S<U> su; }
7963 and supposing that we are instantiating f<int, double>,
7964 then our ARGS will be {int, double}, but, when looking up
7965 S we only want {double}. */
7966 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
7968 if (argvec == error_mark_node)
7969 r = error_mark_node;
7972 r = lookup_template_class (t, argvec, in_decl, context,
7973 entering_scope, complain);
7974 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
7977 skip_evaluation = saved_skip_evaluation;
7982 /* This is not a template type, so there's nothing to do. */
7986 return tsubst (t, args, complain, in_decl);
7990 /* Substitute into the default argument ARG (a default argument for
7991 FN), which has the indicated TYPE. */
7994 tsubst_default_argument (tree fn, tree type, tree arg)
7996 tree saved_class_ptr = NULL_TREE;
7997 tree saved_class_ref = NULL_TREE;
7999 /* This default argument came from a template. Instantiate the
8000 default argument here, not in tsubst. In the case of
8009 we must be careful to do name lookup in the scope of S<T>,
8010 rather than in the current class. */
8011 push_access_scope (fn);
8012 /* The "this" pointer is not valid in a default argument. */
8015 saved_class_ptr = current_class_ptr;
8016 cp_function_chain->x_current_class_ptr = NULL_TREE;
8017 saved_class_ref = current_class_ref;
8018 cp_function_chain->x_current_class_ref = NULL_TREE;
8021 push_deferring_access_checks(dk_no_deferred);
8022 /* The default argument expression may cause implicitly defined
8023 member functions to be synthesized, which will result in garbage
8024 collection. We must treat this situation as if we were within
8025 the body of function so as to avoid collecting live data on the
8028 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
8029 tf_warning_or_error, NULL_TREE,
8030 /*integral_constant_expression_p=*/false);
8032 pop_deferring_access_checks();
8034 /* Restore the "this" pointer. */
8037 cp_function_chain->x_current_class_ptr = saved_class_ptr;
8038 cp_function_chain->x_current_class_ref = saved_class_ref;
8041 pop_access_scope (fn);
8043 /* Make sure the default argument is reasonable. */
8044 arg = check_default_argument (type, arg);
8049 /* Substitute into all the default arguments for FN. */
8052 tsubst_default_arguments (tree fn)
8057 tmpl_args = DECL_TI_ARGS (fn);
8059 /* If this function is not yet instantiated, we certainly don't need
8060 its default arguments. */
8061 if (uses_template_parms (tmpl_args))
8064 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
8066 arg = TREE_CHAIN (arg))
8067 if (TREE_PURPOSE (arg))
8068 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
8070 TREE_PURPOSE (arg));
8073 /* Substitute the ARGS into the T, which is a _DECL. Return the
8074 result of the substitution. Issue error and warning messages under
8075 control of COMPLAIN. */
8078 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
8080 location_t saved_loc;
8084 /* Set the filename and linenumber to improve error-reporting. */
8085 saved_loc = input_location;
8086 input_location = DECL_SOURCE_LOCATION (t);
8088 switch (TREE_CODE (t))
8092 /* We can get here when processing a member function template,
8093 member class template, and template template parameter of
8094 a template class. */
8095 tree decl = DECL_TEMPLATE_RESULT (t);
8100 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8102 /* Template template parameter is treated here. */
8103 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8104 if (new_type == error_mark_node)
8105 return error_mark_node;
8108 TREE_CHAIN (r) = NULL_TREE;
8109 TREE_TYPE (r) = new_type;
8110 DECL_TEMPLATE_RESULT (r)
8111 = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
8112 DECL_TEMPLATE_PARMS (r)
8113 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8115 TYPE_NAME (new_type) = r;
8119 /* We might already have an instance of this template.
8120 The ARGS are for the surrounding class type, so the
8121 full args contain the tsubst'd args for the context,
8122 plus the innermost args from the template decl. */
8123 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
8124 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
8125 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
8126 /* Because this is a template, the arguments will still be
8127 dependent, even after substitution. If
8128 PROCESSING_TEMPLATE_DECL is not set, the dependency
8129 predicates will short-circuit. */
8130 ++processing_template_decl;
8131 full_args = tsubst_template_args (tmpl_args, args,
8133 --processing_template_decl;
8134 if (full_args == error_mark_node)
8135 return error_mark_node;
8137 /* tsubst_template_args doesn't copy the vector if
8138 nothing changed. But, *something* should have
8140 gcc_assert (full_args != tmpl_args);
8142 spec = retrieve_specialization (t, full_args,
8143 /*class_specializations_p=*/true);
8144 if (spec != NULL_TREE)
8150 /* Make a new template decl. It will be similar to the
8151 original, but will record the current template arguments.
8152 We also create a new function declaration, which is just
8153 like the old one, but points to this new template, rather
8154 than the old one. */
8156 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
8157 TREE_CHAIN (r) = NULL_TREE;
8159 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
8161 if (TREE_CODE (decl) == TYPE_DECL)
8164 ++processing_template_decl;
8165 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8166 --processing_template_decl;
8167 if (new_type == error_mark_node)
8168 return error_mark_node;
8170 TREE_TYPE (r) = new_type;
8171 CLASSTYPE_TI_TEMPLATE (new_type) = r;
8172 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
8173 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
8174 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
8179 ++processing_template_decl;
8180 new_decl = tsubst (decl, args, complain, in_decl);
8181 --processing_template_decl;
8182 if (new_decl == error_mark_node)
8183 return error_mark_node;
8185 DECL_TEMPLATE_RESULT (r) = new_decl;
8186 DECL_TI_TEMPLATE (new_decl) = r;
8187 TREE_TYPE (r) = TREE_TYPE (new_decl);
8188 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
8189 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
8192 SET_DECL_IMPLICIT_INSTANTIATION (r);
8193 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
8194 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
8196 /* The template parameters for this new template are all the
8197 template parameters for the old template, except the
8198 outermost level of parameters. */
8199 DECL_TEMPLATE_PARMS (r)
8200 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8203 if (PRIMARY_TEMPLATE_P (t))
8204 DECL_PRIMARY_TEMPLATE (r) = r;
8206 if (TREE_CODE (decl) != TYPE_DECL)
8207 /* Record this non-type partial instantiation. */
8208 register_specialization (r, t,
8209 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
8217 tree argvec = NULL_TREE;
8225 /* Nobody should be tsubst'ing into non-template functions. */
8226 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
8228 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
8233 /* If T is not dependent, just return it. We have to
8234 increment PROCESSING_TEMPLATE_DECL because
8235 value_dependent_expression_p assumes that nothing is
8236 dependent when PROCESSING_TEMPLATE_DECL is zero. */
8237 ++processing_template_decl;
8238 dependent_p = value_dependent_expression_p (t);
8239 --processing_template_decl;
8243 /* Calculate the most general template of which R is a
8244 specialization, and the complete set of arguments used to
8246 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
8247 argvec = tsubst_template_args (DECL_TI_ARGS
8248 (DECL_TEMPLATE_RESULT (gen_tmpl)),
8249 args, complain, in_decl);
8251 /* Check to see if we already have this specialization. */
8252 spec = retrieve_specialization (gen_tmpl, argvec,
8253 /*class_specializations_p=*/false);
8261 /* We can see more levels of arguments than parameters if
8262 there was a specialization of a member template, like
8265 template <class T> struct S { template <class U> void f(); }
8266 template <> template <class U> void S<int>::f(U);
8268 Here, we'll be substituting into the specialization,
8269 because that's where we can find the code we actually
8270 want to generate, but we'll have enough arguments for
8271 the most general template.
8273 We also deal with the peculiar case:
8275 template <class T> struct S {
8276 template <class U> friend void f();
8278 template <class U> void f() {}
8280 template void f<double>();
8282 Here, the ARGS for the instantiation of will be {int,
8283 double}. But, we only need as many ARGS as there are
8284 levels of template parameters in CODE_PATTERN. We are
8285 careful not to get fooled into reducing the ARGS in
8288 template <class T> struct S { template <class U> void f(U); }
8289 template <class T> template <> void S<T>::f(int) {}
8291 which we can spot because the pattern will be a
8292 specialization in this case. */
8293 args_depth = TMPL_ARGS_DEPTH (args);
8295 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
8296 if (args_depth > parms_depth
8297 && !DECL_TEMPLATE_SPECIALIZATION (t))
8298 args = get_innermost_template_args (args, parms_depth);
8302 /* This special case arises when we have something like this:
8304 template <class T> struct S {
8305 friend void f<int>(int, double);
8308 Here, the DECL_TI_TEMPLATE for the friend declaration
8309 will be an IDENTIFIER_NODE. We are being called from
8310 tsubst_friend_function, and we want only to create a
8311 new decl (R) with appropriate types so that we can call
8312 determine_specialization. */
8313 gen_tmpl = NULL_TREE;
8316 if (DECL_CLASS_SCOPE_P (t))
8318 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
8322 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
8323 complain, t, /*entering_scope=*/1);
8328 ctx = DECL_CONTEXT (t);
8330 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8331 if (type == error_mark_node)
8332 return error_mark_node;
8334 /* We do NOT check for matching decls pushed separately at this
8335 point, as they may not represent instantiations of this
8336 template, and in any case are considered separate under the
8339 DECL_USE_TEMPLATE (r) = 0;
8340 TREE_TYPE (r) = type;
8341 /* Clear out the mangled name and RTL for the instantiation. */
8342 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
8343 SET_DECL_RTL (r, NULL_RTX);
8344 /* Leave DECL_INITIAL set on deleted instantiations. */
8345 if (!DECL_DELETED_FN (r))
8346 DECL_INITIAL (r) = NULL_TREE;
8347 DECL_CONTEXT (r) = ctx;
8349 if (member && DECL_CONV_FN_P (r))
8350 /* Type-conversion operator. Reconstruct the name, in
8351 case it's the name of one of the template's parameters. */
8352 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
8354 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
8356 DECL_RESULT (r) = NULL_TREE;
8358 TREE_STATIC (r) = 0;
8359 TREE_PUBLIC (r) = TREE_PUBLIC (t);
8360 DECL_EXTERNAL (r) = 1;
8361 /* If this is an instantiation of a function with internal
8362 linkage, we already know what object file linkage will be
8363 assigned to the instantiation. */
8364 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
8365 DECL_DEFER_OUTPUT (r) = 0;
8366 TREE_CHAIN (r) = NULL_TREE;
8367 DECL_PENDING_INLINE_INFO (r) = 0;
8368 DECL_PENDING_INLINE_P (r) = 0;
8369 DECL_SAVED_TREE (r) = NULL_TREE;
8370 DECL_STRUCT_FUNCTION (r) = NULL;
8372 if (DECL_CLONED_FUNCTION (r))
8374 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
8376 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
8377 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
8380 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
8381 this in the special friend case mentioned above where
8382 GEN_TMPL is NULL. */
8385 DECL_TEMPLATE_INFO (r)
8386 = tree_cons (gen_tmpl, argvec, NULL_TREE);
8387 SET_DECL_IMPLICIT_INSTANTIATION (r);
8388 register_specialization (r, gen_tmpl, argvec, false);
8390 /* We're not supposed to instantiate default arguments
8391 until they are called, for a template. But, for a
8394 template <class T> void f ()
8395 { extern void g(int i = T()); }
8397 we should do the substitution when the template is
8398 instantiated. We handle the member function case in
8399 instantiate_class_template since the default arguments
8400 might refer to other members of the class. */
8402 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8403 && !uses_template_parms (argvec))
8404 tsubst_default_arguments (r);
8407 DECL_TEMPLATE_INFO (r) = NULL_TREE;
8409 /* Copy the list of befriending classes. */
8410 for (friends = &DECL_BEFRIENDING_CLASSES (r);
8412 friends = &TREE_CHAIN (*friends))
8414 *friends = copy_node (*friends);
8415 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
8420 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
8422 maybe_retrofit_in_chrg (r);
8423 if (DECL_CONSTRUCTOR_P (r))
8424 grok_ctor_properties (ctx, r);
8425 /* If this is an instantiation of a member template, clone it.
8426 If it isn't, that'll be handled by
8427 clone_constructors_and_destructors. */
8428 if (PRIMARY_TEMPLATE_P (gen_tmpl))
8429 clone_function_decl (r, /*update_method_vec_p=*/0);
8431 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))
8432 && !grok_op_properties (r, (complain & tf_error) != 0))
8433 return error_mark_node;
8435 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
8436 SET_DECL_FRIEND_CONTEXT (r,
8437 tsubst (DECL_FRIEND_CONTEXT (t),
8438 args, complain, in_decl));
8440 /* Possibly limit visibility based on template args. */
8441 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
8442 if (DECL_VISIBILITY_SPECIFIED (t))
8444 DECL_VISIBILITY_SPECIFIED (r) = 0;
8446 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
8448 determine_visibility (r);
8450 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8451 args, complain, in_decl);
8457 tree type = NULL_TREE;
8459 tree expanded_types = NULL_TREE;
8460 tree prev_r = NULL_TREE;
8461 tree first_r = NULL_TREE;
8463 if (FUNCTION_PARAMETER_PACK_P (t))
8465 /* If there is a local specialization that isn't a
8466 parameter pack, it means that we're doing a "simple"
8467 substitution from inside tsubst_pack_expansion. Just
8468 return the local specialization (which will be a single
8470 tree spec = retrieve_local_specialization (t);
8472 && TREE_CODE (spec) == PARM_DECL
8473 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
8476 /* Expand the TYPE_PACK_EXPANSION that provides the types for
8477 the parameters in this function parameter pack. */
8478 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
8480 if (TREE_CODE (expanded_types) == TREE_VEC)
8482 len = TREE_VEC_LENGTH (expanded_types);
8484 /* Zero-length parameter packs are boring. Just substitute
8487 return tsubst (TREE_CHAIN (t), args, complain,
8492 /* All we did was update the type. Make a note of that. */
8493 type = expanded_types;
8494 expanded_types = NULL_TREE;
8498 /* Loop through all of the parameter's we'll build. When T is
8499 a function parameter pack, LEN is the number of expanded
8500 types in EXPANDED_TYPES; otherwise, LEN is 1. */
8502 for (i = 0; i < len; ++i)
8506 if (DECL_TEMPLATE_PARM_P (t))
8507 SET_DECL_TEMPLATE_PARM_P (r);
8510 /* We're on the Ith parameter of the function parameter
8513 /* Get the Ith type. */
8514 type = TREE_VEC_ELT (expanded_types, i);
8517 /* Rename the parameter to include the index. */
8519 make_ith_pack_parameter_name (DECL_NAME (r), i);
8522 /* We're dealing with a normal parameter. */
8523 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8525 type = type_decays_to (type);
8526 TREE_TYPE (r) = type;
8527 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8529 if (DECL_INITIAL (r))
8531 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
8532 DECL_INITIAL (r) = TREE_TYPE (r);
8534 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
8538 DECL_CONTEXT (r) = NULL_TREE;
8540 if (!DECL_TEMPLATE_PARM_P (r))
8541 DECL_ARG_TYPE (r) = type_passed_as (type);
8543 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8544 args, complain, in_decl);
8546 /* Keep track of the first new parameter we
8547 generate. That's what will be returned to the
8552 /* Build a proper chain of parameters when substituting
8553 into a function parameter pack. */
8555 TREE_CHAIN (prev_r) = r;
8559 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
8560 complain, TREE_CHAIN (t));
8562 /* FIRST_R contains the start of the chain we've built. */
8572 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8573 if (type == error_mark_node)
8574 return error_mark_node;
8575 TREE_TYPE (r) = type;
8576 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8578 /* DECL_INITIAL gives the number of bits in a bit-field. */
8580 = tsubst_expr (DECL_INITIAL (t), args,
8582 /*integral_constant_expression_p=*/true);
8583 /* We don't have to set DECL_CONTEXT here; it is set by
8584 finish_member_declaration. */
8585 TREE_CHAIN (r) = NULL_TREE;
8586 if (VOID_TYPE_P (type))
8587 error ("instantiation of %q+D as type %qT", r, type);
8589 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8590 args, complain, in_decl);
8595 /* We reach here only for member using decls. */
8596 if (DECL_DEPENDENT_P (t))
8598 r = do_class_using_decl
8599 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
8600 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
8602 r = error_mark_node;
8605 TREE_PROTECTED (r) = TREE_PROTECTED (t);
8606 TREE_PRIVATE (r) = TREE_PRIVATE (t);
8612 TREE_CHAIN (r) = NULL_TREE;
8619 tree argvec = NULL_TREE;
8620 tree gen_tmpl = NULL_TREE;
8622 tree tmpl = NULL_TREE;
8624 tree type = NULL_TREE;
8627 if (TREE_CODE (t) == TYPE_DECL
8628 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
8630 /* If this is the canonical decl, we don't have to
8631 mess with instantiations, and often we can't (for
8632 typename, template type parms and such). Note that
8633 TYPE_NAME is not correct for the above test if
8634 we've copied the type for a typedef. */
8635 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8636 if (type == error_mark_node)
8637 return error_mark_node;
8638 r = TYPE_NAME (type);
8642 /* Check to see if we already have the specialization we
8645 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
8647 /* T is a static data member or namespace-scope entity.
8648 We have to substitute into namespace-scope variables
8649 (even though such entities are never templates) because
8652 template <class T> void f() { extern T t; }
8654 where the entity referenced is not known until
8655 instantiation time. */
8657 ctx = DECL_CONTEXT (t);
8658 if (DECL_CLASS_SCOPE_P (t))
8660 ctx = tsubst_aggr_type (ctx, args,
8662 in_decl, /*entering_scope=*/1);
8663 /* If CTX is unchanged, then T is in fact the
8664 specialization we want. That situation occurs when
8665 referencing a static data member within in its own
8666 class. We can use pointer equality, rather than
8667 same_type_p, because DECL_CONTEXT is always
8669 if (ctx == DECL_CONTEXT (t))
8675 tmpl = DECL_TI_TEMPLATE (t);
8676 gen_tmpl = most_general_template (tmpl);
8677 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
8678 spec = (retrieve_specialization
8680 /*class_specializations_p=*/false));
8685 /* A local variable. */
8687 /* Subsequent calls to pushdecl will fill this in. */
8689 spec = retrieve_local_specialization (t);
8691 /* If we already have the specialization we need, there is
8692 nothing more to do. */
8699 /* Create a new node for the specialization we need. */
8701 if (type == NULL_TREE)
8702 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8703 if (TREE_CODE (r) == VAR_DECL)
8705 /* Even if the original location is out of scope, the
8706 newly substituted one is not. */
8707 DECL_DEAD_FOR_LOCAL (r) = 0;
8708 DECL_INITIALIZED_P (r) = 0;
8709 DECL_TEMPLATE_INSTANTIATED (r) = 0;
8710 if (type == error_mark_node)
8711 return error_mark_node;
8712 if (TREE_CODE (type) == FUNCTION_TYPE)
8714 /* It may seem that this case cannot occur, since:
8719 declares a function, not a variable. However:
8722 template <typename T> void g() { T t; }
8723 template void g<f>();
8725 is an attempt to declare a variable with function
8727 error ("variable %qD has function type",
8728 /* R is not yet sufficiently initialized, so we
8729 just use its name. */
8731 return error_mark_node;
8733 type = complete_type (type);
8734 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
8735 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
8736 type = check_var_type (DECL_NAME (r), type);
8738 if (DECL_HAS_VALUE_EXPR_P (t))
8740 tree ve = DECL_VALUE_EXPR (t);
8741 ve = tsubst_expr (ve, args, complain, in_decl,
8742 /*constant_expression_p=*/false);
8743 SET_DECL_VALUE_EXPR (r, ve);
8746 else if (DECL_SELF_REFERENCE_P (t))
8747 SET_DECL_SELF_REFERENCE_P (r);
8748 TREE_TYPE (r) = type;
8749 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8750 DECL_CONTEXT (r) = ctx;
8751 /* Clear out the mangled name and RTL for the instantiation. */
8752 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
8753 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
8754 SET_DECL_RTL (r, NULL_RTX);
8755 /* The initializer must not be expanded until it is required;
8757 DECL_INITIAL (r) = NULL_TREE;
8758 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
8759 SET_DECL_RTL (r, NULL_RTX);
8760 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
8761 if (TREE_CODE (r) == VAR_DECL)
8763 /* Possibly limit visibility based on template args. */
8764 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
8765 if (DECL_VISIBILITY_SPECIFIED (t))
8767 DECL_VISIBILITY_SPECIFIED (r) = 0;
8769 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
8771 determine_visibility (r);
8773 /* Preserve a typedef that names a type. */
8774 else if (TREE_CODE (r) == TYPE_DECL
8775 && DECL_ORIGINAL_TYPE (t)
8776 && type != error_mark_node)
8778 DECL_ORIGINAL_TYPE (r) = tsubst (DECL_ORIGINAL_TYPE (t),
8779 args, complain, in_decl);
8780 TREE_TYPE (r) = type = build_variant_type_copy (type);
8781 TYPE_NAME (type) = r;
8786 /* A static data member declaration is always marked
8787 external when it is declared in-class, even if an
8788 initializer is present. We mimic the non-template
8790 DECL_EXTERNAL (r) = 1;
8792 register_specialization (r, gen_tmpl, argvec, false);
8793 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
8794 SET_DECL_IMPLICIT_INSTANTIATION (r);
8797 register_local_specialization (r, t);
8799 TREE_CHAIN (r) = NULL_TREE;
8801 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
8802 (int) ATTR_FLAG_TYPE_IN_PLACE,
8803 args, complain, in_decl);
8812 /* Restore the file and line information. */
8813 input_location = saved_loc;
8818 /* Substitute into the ARG_TYPES of a function type. */
8821 tsubst_arg_types (tree arg_types,
8823 tsubst_flags_t complain,
8826 tree remaining_arg_types;
8827 tree type = NULL_TREE;
8829 tree expanded_args = NULL_TREE;
8832 if (!arg_types || arg_types == void_list_node)
8835 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
8836 args, complain, in_decl);
8837 if (remaining_arg_types == error_mark_node)
8838 return error_mark_node;
8840 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
8842 /* For a pack expansion, perform substitution on the
8843 entire expression. Later on, we'll handle the arguments
8845 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
8846 args, complain, in_decl);
8848 if (TREE_CODE (expanded_args) == TREE_VEC)
8849 /* So that we'll spin through the parameters, one by one. */
8850 i = TREE_VEC_LENGTH (expanded_args);
8853 /* We only partially substituted into the parameter
8854 pack. Our type is TYPE_PACK_EXPANSION. */
8855 type = expanded_args;
8856 expanded_args = NULL_TREE;
8864 type = TREE_VEC_ELT (expanded_args, i);
8866 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
8868 if (type == error_mark_node)
8869 return error_mark_node;
8870 if (VOID_TYPE_P (type))
8872 if (complain & tf_error)
8874 error ("invalid parameter type %qT", type);
8876 error ("in declaration %q+D", in_decl);
8878 return error_mark_node;
8881 /* Do array-to-pointer, function-to-pointer conversion, and ignore
8882 top-level qualifiers as required. */
8883 type = TYPE_MAIN_VARIANT (type_decays_to (type));
8885 /* We do not substitute into default arguments here. The standard
8886 mandates that they be instantiated only when needed, which is
8887 done in build_over_call. */
8888 default_arg = TREE_PURPOSE (arg_types);
8890 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
8892 /* We've instantiated a template before its default arguments
8893 have been parsed. This can happen for a nested template
8894 class, and is not an error unless we require the default
8895 argument in a call of this function. */
8896 remaining_arg_types =
8897 tree_cons (default_arg, type, remaining_arg_types);
8898 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
8899 remaining_arg_types);
8902 remaining_arg_types =
8903 hash_tree_cons (default_arg, type, remaining_arg_types);
8906 return remaining_arg_types;
8909 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
8910 *not* handle the exception-specification for FNTYPE, because the
8911 initial substitution of explicitly provided template parameters
8912 during argument deduction forbids substitution into the
8913 exception-specification:
8917 All references in the function type of the function template to the
8918 corresponding template parameters are replaced by the specified tem-
8919 plate argument values. If a substitution in a template parameter or
8920 in the function type of the function template results in an invalid
8921 type, type deduction fails. [Note: The equivalent substitution in
8922 exception specifications is done only when the function is instanti-
8923 ated, at which point a program is ill-formed if the substitution
8924 results in an invalid type.] */
8927 tsubst_function_type (tree t,
8929 tsubst_flags_t complain,
8936 /* The TYPE_CONTEXT is not used for function/method types. */
8937 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
8939 /* Substitute the return type. */
8940 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8941 if (return_type == error_mark_node)
8942 return error_mark_node;
8943 /* The standard does not presently indicate that creation of a
8944 function type with an invalid return type is a deduction failure.
8945 However, that is clearly analogous to creating an array of "void"
8946 or a reference to a reference. This is core issue #486. */
8947 if (TREE_CODE (return_type) == ARRAY_TYPE
8948 || TREE_CODE (return_type) == FUNCTION_TYPE)
8950 if (complain & tf_error)
8952 if (TREE_CODE (return_type) == ARRAY_TYPE)
8953 error ("function returning an array");
8955 error ("function returning a function");
8957 return error_mark_node;
8960 /* Substitute the argument types. */
8961 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
8963 if (arg_types == error_mark_node)
8964 return error_mark_node;
8966 /* Construct a new type node and return it. */
8967 if (TREE_CODE (t) == FUNCTION_TYPE)
8968 fntype = build_function_type (return_type, arg_types);
8971 tree r = TREE_TYPE (TREE_VALUE (arg_types));
8972 if (! MAYBE_CLASS_TYPE_P (r))
8976 Type deduction may fail for any of the following
8979 -- Attempting to create "pointer to member of T" when T
8980 is not a class type. */
8981 if (complain & tf_error)
8982 error ("creating pointer to member function of non-class type %qT",
8984 return error_mark_node;
8987 fntype = build_method_type_directly (r, return_type,
8988 TREE_CHAIN (arg_types));
8990 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
8991 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
8996 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
8997 ARGS into that specification, and return the substituted
8998 specification. If there is no specification, return NULL_TREE. */
9001 tsubst_exception_specification (tree fntype,
9003 tsubst_flags_t complain,
9009 specs = TYPE_RAISES_EXCEPTIONS (fntype);
9010 new_specs = NULL_TREE;
9013 if (! TREE_VALUE (specs))
9020 tree expanded_specs = NULL_TREE;
9022 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
9024 /* Expand the pack expansion type. */
9025 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
9029 if (expanded_specs == error_mark_node)
9030 return error_mark_node;
9031 else if (TREE_CODE (expanded_specs) == TREE_VEC)
9032 len = TREE_VEC_LENGTH (expanded_specs);
9035 /* We're substituting into a member template, so
9036 we got a TYPE_PACK_EXPANSION back. Add that
9037 expansion and move on. */
9038 gcc_assert (TREE_CODE (expanded_specs)
9039 == TYPE_PACK_EXPANSION);
9040 new_specs = add_exception_specifier (new_specs,
9043 specs = TREE_CHAIN (specs);
9048 for (i = 0; i < len; ++i)
9051 spec = TREE_VEC_ELT (expanded_specs, i);
9053 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
9054 if (spec == error_mark_node)
9056 new_specs = add_exception_specifier (new_specs, spec,
9060 specs = TREE_CHAIN (specs);
9066 /* Take the tree structure T and replace template parameters used
9067 therein with the argument vector ARGS. IN_DECL is an associated
9068 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
9069 Issue error and warning messages under control of COMPLAIN. Note
9070 that we must be relatively non-tolerant of extensions here, in
9071 order to preserve conformance; if we allow substitutions that
9072 should not be allowed, we may allow argument deductions that should
9073 not succeed, and therefore report ambiguous overload situations
9074 where there are none. In theory, we could allow the substitution,
9075 but indicate that it should have failed, and allow our caller to
9076 make sure that the right thing happens, but we don't try to do this
9079 This function is used for dealing with types, decls and the like;
9080 for expressions, use tsubst_expr or tsubst_copy. */
9083 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9087 if (t == NULL_TREE || t == error_mark_node
9088 || t == integer_type_node
9089 || t == void_type_node
9090 || t == char_type_node
9091 || t == unknown_type_node
9092 || TREE_CODE (t) == NAMESPACE_DECL)
9096 return tsubst_decl (t, args, complain);
9098 if (args == NULL_TREE)
9101 if (TREE_CODE (t) == IDENTIFIER_NODE)
9102 type = IDENTIFIER_TYPE_VALUE (t);
9104 type = TREE_TYPE (t);
9106 gcc_assert (type != unknown_type_node);
9108 /* Reuse typedefs. We need to do this to handle dependent attributes,
9109 such as attribute aligned. */
9112 && TYPE_NAME (t) != TYPE_MAIN_DECL (t))
9114 tree decl = TYPE_NAME (t);
9116 if (DECL_CLASS_SCOPE_P (decl)
9117 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
9118 && uses_template_parms (DECL_CONTEXT (decl)))
9120 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
9121 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
9122 r = retrieve_specialization (tmpl, gen_args, false);
9124 else if (DECL_FUNCTION_SCOPE_P (decl)
9125 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
9126 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
9127 r = retrieve_local_specialization (decl);
9129 /* The typedef is from a non-template context. */
9135 r = cp_build_qualified_type_real
9136 (r, cp_type_quals (t) | cp_type_quals (r),
9137 complain | tf_ignore_bad_quals);
9140 /* Else we must be instantiating the typedef, so fall through. */
9144 && TREE_CODE (t) != TYPENAME_TYPE
9145 && TREE_CODE (t) != IDENTIFIER_NODE
9146 && TREE_CODE (t) != FUNCTION_TYPE
9147 && TREE_CODE (t) != METHOD_TYPE)
9148 type = tsubst (type, args, complain, in_decl);
9149 if (type == error_mark_node)
9150 return error_mark_node;
9152 switch (TREE_CODE (t))
9157 return tsubst_aggr_type (t, args, complain, in_decl,
9158 /*entering_scope=*/0);
9161 case IDENTIFIER_NODE:
9173 if (t == integer_type_node)
9176 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
9177 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
9181 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
9183 max = tsubst_expr (omax, args, complain, in_decl,
9184 /*integral_constant_expression_p=*/false);
9186 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
9188 if (TREE_CODE (max) == NOP_EXPR
9189 && TREE_SIDE_EFFECTS (omax)
9190 && !TREE_TYPE (max))
9191 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
9193 max = fold_decl_constant_value (max);
9195 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
9196 with TREE_SIDE_EFFECTS that indicates this is not an integral
9197 constant expression. */
9198 if (processing_template_decl
9199 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
9201 gcc_assert (TREE_CODE (max) == NOP_EXPR);
9202 TREE_SIDE_EFFECTS (max) = 1;
9205 if (TREE_CODE (max) != INTEGER_CST
9206 && !at_function_scope_p ()
9207 && !TREE_SIDE_EFFECTS (max)
9208 && !value_dependent_expression_p (max))
9210 if (complain & tf_error)
9211 error ("array bound is not an integer constant");
9212 return error_mark_node;
9217 Type deduction may fail for any of the following
9220 Attempting to create an array with a size that is
9221 zero or negative. */
9222 if (integer_zerop (max) && !(complain & tf_error))
9223 /* We must fail if performing argument deduction (as
9224 indicated by the state of complain), so that
9225 another substitution can be found. */
9226 return error_mark_node;
9227 else if (TREE_CODE (max) == INTEGER_CST
9228 && INT_CST_LT (max, integer_zero_node))
9230 if (complain & tf_error)
9231 error ("creating array with negative size (%qE)", max);
9233 return error_mark_node;
9236 return compute_array_index_type (NULL_TREE, max);
9239 case TEMPLATE_TYPE_PARM:
9240 case TEMPLATE_TEMPLATE_PARM:
9241 case BOUND_TEMPLATE_TEMPLATE_PARM:
9242 case TEMPLATE_PARM_INDEX:
9247 tree arg = NULL_TREE;
9251 gcc_assert (TREE_VEC_LENGTH (args) > 0);
9252 template_parm_level_and_index (t, &level, &idx);
9254 levels = TMPL_ARGS_DEPTH (args);
9255 if (level <= levels)
9257 arg = TMPL_ARG (args, level, idx);
9259 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
9260 /* See through ARGUMENT_PACK_SELECT arguments. */
9261 arg = ARGUMENT_PACK_SELECT_ARG (arg);
9264 if (arg == error_mark_node)
9265 return error_mark_node;
9266 else if (arg != NULL_TREE)
9268 if (ARGUMENT_PACK_P (arg))
9269 /* If ARG is an argument pack, we don't actually want to
9270 perform a substitution here, because substitutions
9271 for argument packs are only done
9272 element-by-element. We can get to this point when
9273 substituting the type of a non-type template
9274 parameter pack, when that type actually contains
9275 template parameter packs from an outer template, e.g.,
9277 template<typename... Types> struct A {
9278 template<Types... Values> struct B { };
9282 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
9285 gcc_assert (TYPE_P (arg));
9287 /* cv-quals from the template are discarded when
9288 substituting in a function or reference type. */
9289 if (TREE_CODE (arg) == FUNCTION_TYPE
9290 || TREE_CODE (arg) == METHOD_TYPE
9291 || TREE_CODE (arg) == REFERENCE_TYPE)
9292 quals = cp_type_quals (arg);
9294 quals = cp_type_quals (arg) | cp_type_quals (t);
9296 return cp_build_qualified_type_real
9297 (arg, quals, complain | tf_ignore_bad_quals);
9299 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9301 /* We are processing a type constructed from a
9302 template template parameter. */
9303 tree argvec = tsubst (TYPE_TI_ARGS (t),
9304 args, complain, in_decl);
9305 if (argvec == error_mark_node)
9306 return error_mark_node;
9308 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
9309 are resolving nested-types in the signature of a
9310 member function templates. Otherwise ARG is a
9311 TEMPLATE_DECL and is the real template to be
9313 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
9314 arg = TYPE_NAME (arg);
9316 r = lookup_template_class (arg,
9319 /*entering_scope=*/0,
9321 return cp_build_qualified_type_real
9322 (r, TYPE_QUALS (t), complain);
9325 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
9330 /* This can happen during the attempted tsubst'ing in
9331 unify. This means that we don't yet have any information
9332 about the template parameter in question. */
9335 /* If we get here, we must have been looking at a parm for a
9336 more deeply nested template. Make a new version of this
9337 template parameter, but with a lower level. */
9338 switch (TREE_CODE (t))
9340 case TEMPLATE_TYPE_PARM:
9341 case TEMPLATE_TEMPLATE_PARM:
9342 case BOUND_TEMPLATE_TEMPLATE_PARM:
9343 if (cp_type_quals (t))
9345 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
9346 r = cp_build_qualified_type_real
9347 (r, cp_type_quals (t),
9348 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
9349 ? tf_ignore_bad_quals : 0));
9354 TEMPLATE_TYPE_PARM_INDEX (r)
9355 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
9356 r, levels, args, complain);
9357 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
9358 TYPE_MAIN_VARIANT (r) = r;
9359 TYPE_POINTER_TO (r) = NULL_TREE;
9360 TYPE_REFERENCE_TO (r) = NULL_TREE;
9362 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
9363 /* We have reduced the level of the template
9364 template parameter, but not the levels of its
9365 template parameters, so canonical_type_parameter
9366 will not be able to find the canonical template
9367 template parameter for this level. Thus, we
9368 require structural equality checking to compare
9369 TEMPLATE_TEMPLATE_PARMs. */
9370 SET_TYPE_STRUCTURAL_EQUALITY (r);
9371 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
9372 SET_TYPE_STRUCTURAL_EQUALITY (r);
9374 TYPE_CANONICAL (r) = canonical_type_parameter (r);
9376 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9378 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
9380 if (argvec == error_mark_node)
9381 return error_mark_node;
9383 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
9384 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
9389 case TEMPLATE_PARM_INDEX:
9390 r = reduce_template_parm_level (t, type, levels, args, complain);
9402 tree purpose, value, chain;
9404 if (t == void_list_node)
9407 purpose = TREE_PURPOSE (t);
9410 purpose = tsubst (purpose, args, complain, in_decl);
9411 if (purpose == error_mark_node)
9412 return error_mark_node;
9414 value = TREE_VALUE (t);
9417 value = tsubst (value, args, complain, in_decl);
9418 if (value == error_mark_node)
9419 return error_mark_node;
9421 chain = TREE_CHAIN (t);
9422 if (chain && chain != void_type_node)
9424 chain = tsubst (chain, args, complain, in_decl);
9425 if (chain == error_mark_node)
9426 return error_mark_node;
9428 if (purpose == TREE_PURPOSE (t)
9429 && value == TREE_VALUE (t)
9430 && chain == TREE_CHAIN (t))
9432 return hash_tree_cons (purpose, value, chain);
9436 /* We should never be tsubsting a binfo. */
9440 /* A vector of template arguments. */
9442 return tsubst_template_args (t, args, complain, in_decl);
9445 case REFERENCE_TYPE:
9447 enum tree_code code;
9449 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
9452 code = TREE_CODE (t);
9457 Type deduction may fail for any of the following
9460 -- Attempting to create a pointer to reference type.
9461 -- Attempting to create a reference to a reference type or
9462 a reference to void.
9464 Core issue 106 says that creating a reference to a reference
9465 during instantiation is no longer a cause for failure. We
9466 only enforce this check in strict C++98 mode. */
9467 if ((TREE_CODE (type) == REFERENCE_TYPE
9468 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
9469 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
9471 static location_t last_loc;
9473 /* We keep track of the last time we issued this error
9474 message to avoid spewing a ton of messages during a
9475 single bad template instantiation. */
9476 if (complain & tf_error
9477 && last_loc != input_location)
9479 if (TREE_CODE (type) == VOID_TYPE)
9480 error ("forming reference to void");
9482 error ("forming %s to reference type %qT",
9483 (code == POINTER_TYPE) ? "pointer" : "reference",
9485 last_loc = input_location;
9488 return error_mark_node;
9490 else if (code == POINTER_TYPE)
9492 r = build_pointer_type (type);
9493 if (TREE_CODE (type) == METHOD_TYPE)
9494 r = build_ptrmemfunc_type (r);
9496 else if (TREE_CODE (type) == REFERENCE_TYPE)
9497 /* In C++0x, during template argument substitution, when there is an
9498 attempt to create a reference to a reference type, reference
9499 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
9501 "If a template-argument for a template-parameter T names a type
9502 that is a reference to a type A, an attempt to create the type
9503 'lvalue reference to cv T' creates the type 'lvalue reference to
9504 A,' while an attempt to create the type type rvalue reference to
9505 cv T' creates the type T"
9507 r = cp_build_reference_type
9509 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
9511 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
9512 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
9514 if (r != error_mark_node)
9515 /* Will this ever be needed for TYPE_..._TO values? */
9522 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
9523 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
9527 Type deduction may fail for any of the following
9530 -- Attempting to create "pointer to member of T" when T
9531 is not a class type. */
9532 if (complain & tf_error)
9533 error ("creating pointer to member of non-class type %qT", r);
9534 return error_mark_node;
9536 if (TREE_CODE (type) == REFERENCE_TYPE)
9538 if (complain & tf_error)
9539 error ("creating pointer to member reference type %qT", type);
9540 return error_mark_node;
9542 if (TREE_CODE (type) == VOID_TYPE)
9544 if (complain & tf_error)
9545 error ("creating pointer to member of type void");
9546 return error_mark_node;
9548 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
9549 if (TREE_CODE (type) == FUNCTION_TYPE)
9551 /* The type of the implicit object parameter gets its
9552 cv-qualifiers from the FUNCTION_TYPE. */
9554 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
9555 cp_type_quals (type));
9557 method_type = build_method_type_directly (this_type,
9559 TYPE_ARG_TYPES (type));
9560 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
9561 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
9565 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
9574 fntype = tsubst_function_type (t, args, complain, in_decl);
9575 if (fntype == error_mark_node)
9576 return error_mark_node;
9578 /* Substitute the exception specification. */
9579 specs = tsubst_exception_specification (t, args, complain,
9581 if (specs == error_mark_node)
9582 return error_mark_node;
9584 fntype = build_exception_variant (fntype, specs);
9589 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
9590 if (domain == error_mark_node)
9591 return error_mark_node;
9593 /* As an optimization, we avoid regenerating the array type if
9594 it will obviously be the same as T. */
9595 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
9598 /* These checks should match the ones in grokdeclarator.
9602 The deduction may fail for any of the following reasons:
9604 -- Attempting to create an array with an element type that
9605 is void, a function type, or a reference type, or [DR337]
9606 an abstract class type. */
9607 if (TREE_CODE (type) == VOID_TYPE
9608 || TREE_CODE (type) == FUNCTION_TYPE
9609 || TREE_CODE (type) == REFERENCE_TYPE)
9611 if (complain & tf_error)
9612 error ("creating array of %qT", type);
9613 return error_mark_node;
9615 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
9617 if (complain & tf_error)
9618 error ("creating array of %qT, which is an abstract class type",
9620 return error_mark_node;
9623 r = build_cplus_array_type (type, domain);
9625 if (TYPE_USER_ALIGN (t))
9627 TYPE_ALIGN (r) = TYPE_ALIGN (t);
9628 TYPE_USER_ALIGN (r) = 1;
9637 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9638 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
9640 if (e1 == error_mark_node || e2 == error_mark_node)
9641 return error_mark_node;
9643 return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
9649 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9650 if (e == error_mark_node)
9651 return error_mark_node;
9653 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
9658 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
9659 in_decl, /*entering_scope=*/1);
9660 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
9663 if (ctx == error_mark_node || f == error_mark_node)
9664 return error_mark_node;
9666 if (!MAYBE_CLASS_TYPE_P (ctx))
9668 if (complain & tf_error)
9669 error ("%qT is not a class, struct, or union type", ctx);
9670 return error_mark_node;
9672 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
9674 /* Normally, make_typename_type does not require that the CTX
9675 have complete type in order to allow things like:
9677 template <class T> struct S { typename S<T>::X Y; };
9679 But, such constructs have already been resolved by this
9680 point, so here CTX really should have complete type, unless
9681 it's a partial instantiation. */
9682 ctx = complete_type (ctx);
9683 if (!COMPLETE_TYPE_P (ctx))
9685 if (complain & tf_error)
9686 cxx_incomplete_type_error (NULL_TREE, ctx);
9687 return error_mark_node;
9691 f = make_typename_type (ctx, f, typename_type,
9692 (complain & tf_error) | tf_keep_type_decl);
9693 if (f == error_mark_node)
9695 if (TREE_CODE (f) == TYPE_DECL)
9697 complain |= tf_ignore_bad_quals;
9701 if (TREE_CODE (f) != TYPENAME_TYPE)
9703 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
9704 error ("%qT resolves to %qT, which is not an enumeration type",
9706 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
9707 error ("%qT resolves to %qT, which is is not a class type",
9711 return cp_build_qualified_type_real
9712 (f, cp_type_quals (f) | cp_type_quals (t), complain);
9715 case UNBOUND_CLASS_TEMPLATE:
9717 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
9718 in_decl, /*entering_scope=*/1);
9719 tree name = TYPE_IDENTIFIER (t);
9720 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
9722 if (ctx == error_mark_node || name == error_mark_node)
9723 return error_mark_node;
9726 parm_list = tsubst_template_parms (parm_list, args, complain);
9727 return make_unbound_class_template (ctx, name, parm_list, complain);
9737 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9738 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
9739 /*integral_constant_expression_p=*/false);
9740 if (e1 == error_mark_node || e2 == error_mark_node)
9741 return error_mark_node;
9743 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
9748 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9749 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
9750 if (e1 == error_mark_node || e2 == error_mark_node)
9751 return error_mark_node;
9753 return build_qualified_name (/*type=*/NULL_TREE,
9754 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
9761 type = finish_typeof (tsubst_expr
9762 (TYPEOF_TYPE_EXPR (t), args,
9764 /*integral_constant_expression_p=*/false));
9765 return cp_build_qualified_type_real (type,
9767 | cp_type_quals (type),
9777 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
9779 /*integral_constant_expression_p=*/false);
9784 finish_decltype_type (type,
9785 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
9786 return cp_build_qualified_type_real (type,
9788 | cp_type_quals (type),
9792 case TYPE_ARGUMENT_PACK:
9793 case NONTYPE_ARGUMENT_PACK:
9795 tree r = make_node (TREE_CODE (t));
9797 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
9801 SET_ARGUMENT_PACK_ARGS (r, packed_out);
9803 /* For template nontype argument packs, also substitute into
9805 if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
9806 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
9813 sorry ("use of %qs in template",
9814 tree_code_name [(int) TREE_CODE (t)]);
9815 return error_mark_node;
9819 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
9820 type of the expression on the left-hand side of the "." or "->"
9824 tsubst_baselink (tree baselink, tree object_type,
9825 tree args, tsubst_flags_t complain, tree in_decl)
9828 tree qualifying_scope;
9831 tree template_args = 0;
9832 bool template_id_p = false;
9834 /* A baselink indicates a function from a base class. Both the
9835 BASELINK_ACCESS_BINFO and the base class referenced may
9836 indicate bases of the template class, rather than the
9837 instantiated class. In addition, lookups that were not
9838 ambiguous before may be ambiguous now. Therefore, we perform
9839 the lookup again. */
9840 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
9841 qualifying_scope = tsubst (qualifying_scope, args,
9843 fns = BASELINK_FUNCTIONS (baselink);
9844 optype = BASELINK_OPTYPE (baselink);
9845 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
9847 template_id_p = true;
9848 template_args = TREE_OPERAND (fns, 1);
9849 fns = TREE_OPERAND (fns, 0);
9851 template_args = tsubst_template_args (template_args, args,
9854 name = DECL_NAME (get_first_fn (fns));
9855 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
9857 /* If lookup found a single function, mark it as used at this
9858 point. (If it lookup found multiple functions the one selected
9859 later by overload resolution will be marked as used at that
9861 if (BASELINK_P (baselink))
9862 fns = BASELINK_FUNCTIONS (baselink);
9863 if (!template_id_p && !really_overloaded_fn (fns))
9864 mark_used (OVL_CURRENT (fns));
9866 /* Add back the template arguments, if present. */
9867 if (BASELINK_P (baselink) && template_id_p)
9868 BASELINK_FUNCTIONS (baselink)
9869 = build_nt (TEMPLATE_ID_EXPR,
9870 BASELINK_FUNCTIONS (baselink),
9872 /* Update the conversion operator type. */
9873 BASELINK_OPTYPE (baselink)
9874 = tsubst (optype, args, complain, in_decl);
9877 object_type = current_class_type;
9878 return adjust_result_of_qualified_name_lookup (baselink,
9883 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
9884 true if the qualified-id will be a postfix-expression in-and-of
9885 itself; false if more of the postfix-expression follows the
9886 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
9890 tsubst_qualified_id (tree qualified_id, tree args,
9891 tsubst_flags_t complain, tree in_decl,
9892 bool done, bool address_p)
9900 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
9902 /* Figure out what name to look up. */
9903 name = TREE_OPERAND (qualified_id, 1);
9904 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
9907 template_args = TREE_OPERAND (name, 1);
9909 template_args = tsubst_template_args (template_args, args,
9911 name = TREE_OPERAND (name, 0);
9915 is_template = false;
9916 template_args = NULL_TREE;
9919 /* Substitute into the qualifying scope. When there are no ARGS, we
9920 are just trying to simplify a non-dependent expression. In that
9921 case the qualifying scope may be dependent, and, in any case,
9922 substituting will not help. */
9923 scope = TREE_OPERAND (qualified_id, 0);
9926 scope = tsubst (scope, args, complain, in_decl);
9927 expr = tsubst_copy (name, args, complain, in_decl);
9932 if (dependent_type_p (scope))
9933 return build_qualified_name (/*type=*/NULL_TREE,
9935 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
9937 if (!BASELINK_P (name) && !DECL_P (expr))
9939 if (TREE_CODE (expr) == BIT_NOT_EXPR)
9940 /* If this were actually a destructor call, it would have been
9941 parsed as such by the parser. */
9942 expr = error_mark_node;
9944 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
9945 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
9946 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
9948 if (complain & tf_error)
9950 error ("dependent-name %qE is parsed as a non-type, but "
9951 "instantiation yields a type", qualified_id);
9952 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
9954 return error_mark_node;
9960 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
9962 /* Remember that there was a reference to this entity. */
9966 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
9968 if (complain & tf_error)
9969 qualified_name_lookup_error (scope,
9970 TREE_OPERAND (qualified_id, 1),
9971 expr, input_location);
9972 return error_mark_node;
9976 expr = lookup_template_function (expr, template_args);
9978 if (expr == error_mark_node && complain & tf_error)
9979 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
9980 expr, input_location);
9981 else if (TYPE_P (scope))
9983 expr = (adjust_result_of_qualified_name_lookup
9984 (expr, scope, current_class_type));
9985 expr = (finish_qualified_id_expr
9986 (scope, expr, done, address_p,
9987 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
9988 /*template_arg_p=*/false));
9991 /* Expressions do not generally have reference type. */
9992 if (TREE_CODE (expr) != SCOPE_REF
9993 /* However, if we're about to form a pointer-to-member, we just
9994 want the referenced member referenced. */
9995 && TREE_CODE (expr) != OFFSET_REF)
9996 expr = convert_from_reference (expr);
10001 /* Like tsubst, but deals with expressions. This function just replaces
10002 template parms; to finish processing the resultant expression, use
10006 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10008 enum tree_code code;
10011 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
10014 code = TREE_CODE (t);
10019 r = retrieve_local_specialization (t);
10024 /* This can happen for a parameter name used later in a function
10025 declaration (such as in a late-specified return type). Just
10026 make a dummy decl, since it's only used for its type. */
10027 gcc_assert (skip_evaluation);
10028 /* We copy T because want to tsubst the PARM_DECL only,
10029 not the following PARM_DECLs that are chained to T. */
10031 r = tsubst_decl (c, args, complain);
10032 /* Give it the template pattern as its context; its true context
10033 hasn't been instantiated yet and this is good enough for
10035 DECL_CONTEXT (r) = DECL_CONTEXT (t);
10038 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
10039 r = ARGUMENT_PACK_SELECT_ARG (r);
10048 if (DECL_TEMPLATE_PARM_P (t))
10049 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
10050 /* There is no need to substitute into namespace-scope
10052 if (DECL_NAMESPACE_SCOPE_P (t))
10054 /* If ARGS is NULL, then T is known to be non-dependent. */
10055 if (args == NULL_TREE)
10056 return integral_constant_value (t);
10058 /* Unfortunately, we cannot just call lookup_name here.
10061 template <int I> int f() {
10063 struct S { void g() { E e = a; } };
10066 When we instantiate f<7>::S::g(), say, lookup_name is not
10067 clever enough to find f<7>::a. */
10069 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
10070 /*entering_scope=*/0);
10072 for (v = TYPE_VALUES (enum_type);
10074 v = TREE_CHAIN (v))
10075 if (TREE_PURPOSE (v) == DECL_NAME (t))
10076 return TREE_VALUE (v);
10078 /* We didn't find the name. That should never happen; if
10079 name-lookup found it during preliminary parsing, we
10080 should find it again here during instantiation. */
10081 gcc_unreachable ();
10086 if (DECL_CONTEXT (t))
10090 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
10091 /*entering_scope=*/1);
10092 if (ctx != DECL_CONTEXT (t))
10094 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
10097 if (complain & tf_error)
10098 error ("using invalid field %qD", t);
10099 return error_mark_node;
10108 case FUNCTION_DECL:
10109 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10110 || local_variable_p (t))
10111 t = tsubst (t, args, complain, in_decl);
10116 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
10118 case TEMPLATE_DECL:
10119 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10120 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
10121 args, complain, in_decl);
10122 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
10123 return tsubst (t, args, complain, in_decl);
10124 else if (DECL_CLASS_SCOPE_P (t)
10125 && uses_template_parms (DECL_CONTEXT (t)))
10127 /* Template template argument like the following example need
10130 template <template <class> class TT> struct C {};
10131 template <class T> struct D {
10132 template <class U> struct E {};
10137 We are processing the template argument `E' in #1 for
10138 the template instantiation #2. Originally, `E' is a
10139 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
10140 have to substitute this with one having context `D<int>'. */
10142 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
10143 return lookup_field (context, DECL_NAME(t), 0, false);
10146 /* Ordinary template template argument. */
10150 case REINTERPRET_CAST_EXPR:
10151 case CONST_CAST_EXPR:
10152 case STATIC_CAST_EXPR:
10153 case DYNAMIC_CAST_EXPR:
10156 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10157 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10160 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
10162 /* We only want to compute the number of arguments. */
10163 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
10164 complain, in_decl);
10167 if (TREE_CODE (expanded) == TREE_VEC)
10168 len = TREE_VEC_LENGTH (expanded);
10170 if (expanded == error_mark_node)
10171 return error_mark_node;
10172 else if (PACK_EXPANSION_P (expanded)
10173 || (TREE_CODE (expanded) == TREE_VEC
10175 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
10177 if (TREE_CODE (expanded) == TREE_VEC)
10178 expanded = TREE_VEC_ELT (expanded, len - 1);
10180 if (TYPE_P (expanded))
10181 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
10182 complain & tf_error);
10184 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
10185 complain & tf_error);
10188 return build_int_cst (size_type_node, len);
10194 case TRUTH_NOT_EXPR:
10197 case UNARY_PLUS_EXPR: /* Unary + */
10202 case REALPART_EXPR:
10203 case IMAGPART_EXPR:
10205 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10206 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10208 case COMPONENT_REF:
10213 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
10214 name = TREE_OPERAND (t, 1);
10215 if (TREE_CODE (name) == BIT_NOT_EXPR)
10217 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10218 complain, in_decl);
10219 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10221 else if (TREE_CODE (name) == SCOPE_REF
10222 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
10224 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
10225 complain, in_decl);
10226 name = TREE_OPERAND (name, 1);
10227 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10228 complain, in_decl);
10229 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10230 name = build_qualified_name (/*type=*/NULL_TREE,
10232 /*template_p=*/false);
10234 else if (TREE_CODE (name) == BASELINK)
10235 name = tsubst_baselink (name,
10236 non_reference (TREE_TYPE (object)),
10240 name = tsubst_copy (name, args, complain, in_decl);
10241 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
10247 case TRUNC_DIV_EXPR:
10248 case CEIL_DIV_EXPR:
10249 case FLOOR_DIV_EXPR:
10250 case ROUND_DIV_EXPR:
10251 case EXACT_DIV_EXPR:
10255 case TRUNC_MOD_EXPR:
10256 case FLOOR_MOD_EXPR:
10257 case TRUTH_ANDIF_EXPR:
10258 case TRUTH_ORIF_EXPR:
10259 case TRUTH_AND_EXPR:
10260 case TRUTH_OR_EXPR:
10273 case COMPOUND_EXPR:
10276 case PREDECREMENT_EXPR:
10277 case PREINCREMENT_EXPR:
10278 case POSTDECREMENT_EXPR:
10279 case POSTINCREMENT_EXPR:
10281 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10282 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10285 return build_qualified_name (/*type=*/NULL_TREE,
10286 tsubst_copy (TREE_OPERAND (t, 0),
10287 args, complain, in_decl),
10288 tsubst_copy (TREE_OPERAND (t, 1),
10289 args, complain, in_decl),
10290 QUALIFIED_NAME_IS_TEMPLATE (t));
10295 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10296 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10297 NULL_TREE, NULL_TREE);
10301 int n = VL_EXP_OPERAND_LENGTH (t);
10302 tree result = build_vl_exp (CALL_EXPR, n);
10304 for (i = 0; i < n; i++)
10305 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
10306 complain, in_decl);
10312 case PSEUDO_DTOR_EXPR:
10315 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10316 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10317 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10318 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
10325 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10326 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10327 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10328 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
10335 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10336 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10337 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
10338 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
10342 case TEMPLATE_ID_EXPR:
10344 /* Substituted template arguments */
10345 tree fn = TREE_OPERAND (t, 0);
10346 tree targs = TREE_OPERAND (t, 1);
10348 fn = tsubst_copy (fn, args, complain, in_decl);
10350 targs = tsubst_template_args (targs, args, complain, in_decl);
10352 return lookup_template_function (fn, targs);
10357 tree purpose, value, chain;
10359 if (t == void_list_node)
10362 purpose = TREE_PURPOSE (t);
10364 purpose = tsubst_copy (purpose, args, complain, in_decl);
10365 value = TREE_VALUE (t);
10367 value = tsubst_copy (value, args, complain, in_decl);
10368 chain = TREE_CHAIN (t);
10369 if (chain && chain != void_type_node)
10370 chain = tsubst_copy (chain, args, complain, in_decl);
10371 if (purpose == TREE_PURPOSE (t)
10372 && value == TREE_VALUE (t)
10373 && chain == TREE_CHAIN (t))
10375 return tree_cons (purpose, value, chain);
10380 case ENUMERAL_TYPE:
10382 case TEMPLATE_TYPE_PARM:
10383 case TEMPLATE_TEMPLATE_PARM:
10384 case BOUND_TEMPLATE_TEMPLATE_PARM:
10385 case TEMPLATE_PARM_INDEX:
10387 case REFERENCE_TYPE:
10389 case FUNCTION_TYPE:
10392 case TYPENAME_TYPE:
10393 case UNBOUND_CLASS_TEMPLATE:
10395 case DECLTYPE_TYPE:
10397 return tsubst (t, args, complain, in_decl);
10399 case IDENTIFIER_NODE:
10400 if (IDENTIFIER_TYPENAME_P (t))
10402 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10403 return mangle_conv_op_name_for_type (new_type);
10409 /* This is handled by tsubst_copy_and_build. */
10410 gcc_unreachable ();
10413 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
10415 tsubst (TREE_TYPE (t), args, complain, in_decl));
10417 case CLEANUP_POINT_EXPR:
10418 /* We shouldn't have built any of these during initial template
10419 generation. Instead, they should be built during instantiation
10420 in response to the saved STMT_IS_FULL_EXPR_P setting. */
10421 gcc_unreachable ();
10424 mark_used (TREE_OPERAND (t, 1));
10427 case EXPR_PACK_EXPANSION:
10428 error ("invalid use of pack expansion expression");
10429 return error_mark_node;
10431 case NONTYPE_ARGUMENT_PACK:
10432 error ("use %<...%> to expand argument pack");
10433 return error_mark_node;
10440 /* Like tsubst_copy, but specifically for OpenMP clauses. */
10443 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
10446 tree new_clauses = NULL, nc, oc;
10448 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
10450 nc = copy_node (oc);
10451 OMP_CLAUSE_CHAIN (nc) = new_clauses;
10454 switch (OMP_CLAUSE_CODE (nc))
10456 case OMP_CLAUSE_LASTPRIVATE:
10457 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
10459 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
10460 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
10461 in_decl, /*integral_constant_expression_p=*/false);
10462 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
10463 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
10466 case OMP_CLAUSE_PRIVATE:
10467 case OMP_CLAUSE_SHARED:
10468 case OMP_CLAUSE_FIRSTPRIVATE:
10469 case OMP_CLAUSE_REDUCTION:
10470 case OMP_CLAUSE_COPYIN:
10471 case OMP_CLAUSE_COPYPRIVATE:
10472 case OMP_CLAUSE_IF:
10473 case OMP_CLAUSE_NUM_THREADS:
10474 case OMP_CLAUSE_SCHEDULE:
10475 case OMP_CLAUSE_COLLAPSE:
10476 OMP_CLAUSE_OPERAND (nc, 0)
10477 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
10478 in_decl, /*integral_constant_expression_p=*/false);
10480 case OMP_CLAUSE_NOWAIT:
10481 case OMP_CLAUSE_ORDERED:
10482 case OMP_CLAUSE_DEFAULT:
10483 case OMP_CLAUSE_UNTIED:
10486 gcc_unreachable ();
10490 return finish_omp_clauses (nreverse (new_clauses));
10493 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
10496 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
10499 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
10501 tree purpose, value, chain;
10506 if (TREE_CODE (t) != TREE_LIST)
10507 return tsubst_copy_and_build (t, args, complain, in_decl,
10508 /*function_p=*/false,
10509 /*integral_constant_expression_p=*/false);
10511 if (t == void_list_node)
10514 purpose = TREE_PURPOSE (t);
10516 purpose = RECUR (purpose);
10517 value = TREE_VALUE (t);
10519 value = RECUR (value);
10520 chain = TREE_CHAIN (t);
10521 if (chain && chain != void_type_node)
10522 chain = RECUR (chain);
10523 return tree_cons (purpose, value, chain);
10527 /* Substitute one OMP_FOR iterator. */
10530 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
10531 tree condv, tree incrv, tree *clauses,
10532 tree args, tsubst_flags_t complain, tree in_decl,
10533 bool integral_constant_expression_p)
10535 #define RECUR(NODE) \
10536 tsubst_expr ((NODE), args, complain, in_decl, \
10537 integral_constant_expression_p)
10538 tree decl, init, cond, incr, auto_node;
10540 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
10541 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
10542 decl = RECUR (TREE_OPERAND (init, 0));
10543 init = TREE_OPERAND (init, 1);
10544 auto_node = type_uses_auto (TREE_TYPE (decl));
10545 if (auto_node && init)
10547 tree init_expr = init;
10548 if (TREE_CODE (init_expr) == DECL_EXPR)
10549 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
10550 init_expr = RECUR (init_expr);
10552 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
10554 gcc_assert (!type_dependent_expression_p (decl));
10556 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
10558 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
10559 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
10560 if (TREE_CODE (incr) == MODIFY_EXPR)
10561 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
10562 RECUR (TREE_OPERAND (incr, 1)),
10565 incr = RECUR (incr);
10566 TREE_VEC_ELT (declv, i) = decl;
10567 TREE_VEC_ELT (initv, i) = init;
10568 TREE_VEC_ELT (condv, i) = cond;
10569 TREE_VEC_ELT (incrv, i) = incr;
10573 if (init && TREE_CODE (init) != DECL_EXPR)
10576 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10578 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
10579 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
10580 && OMP_CLAUSE_DECL (c) == decl)
10582 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
10583 && OMP_CLAUSE_DECL (c) == decl)
10584 error ("iteration variable %qD should not be firstprivate", decl);
10585 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
10586 && OMP_CLAUSE_DECL (c) == decl)
10587 error ("iteration variable %qD should not be reduction", decl);
10591 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
10592 OMP_CLAUSE_DECL (c) = decl;
10593 c = finish_omp_clauses (c);
10596 OMP_CLAUSE_CHAIN (c) = *clauses;
10601 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
10602 if (COMPARISON_CLASS_P (cond))
10603 cond = build2 (TREE_CODE (cond), boolean_type_node,
10604 RECUR (TREE_OPERAND (cond, 0)),
10605 RECUR (TREE_OPERAND (cond, 1)));
10607 cond = RECUR (cond);
10608 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
10609 switch (TREE_CODE (incr))
10611 case PREINCREMENT_EXPR:
10612 case PREDECREMENT_EXPR:
10613 case POSTINCREMENT_EXPR:
10614 case POSTDECREMENT_EXPR:
10615 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
10616 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
10619 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
10620 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
10622 tree rhs = TREE_OPERAND (incr, 1);
10623 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
10624 RECUR (TREE_OPERAND (incr, 0)),
10625 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
10626 RECUR (TREE_OPERAND (rhs, 0)),
10627 RECUR (TREE_OPERAND (rhs, 1))));
10630 incr = RECUR (incr);
10633 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
10634 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
10636 tree lhs = RECUR (TREE_OPERAND (incr, 0));
10637 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
10638 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
10639 TREE_TYPE (decl), lhs,
10640 RECUR (TREE_OPERAND (incr, 2))));
10642 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
10643 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
10644 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
10646 tree rhs = TREE_OPERAND (incr, 2);
10647 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
10648 RECUR (TREE_OPERAND (incr, 0)),
10649 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
10650 RECUR (TREE_OPERAND (rhs, 0)),
10651 RECUR (TREE_OPERAND (rhs, 1))));
10654 incr = RECUR (incr);
10657 incr = RECUR (incr);
10661 TREE_VEC_ELT (declv, i) = decl;
10662 TREE_VEC_ELT (initv, i) = init;
10663 TREE_VEC_ELT (condv, i) = cond;
10664 TREE_VEC_ELT (incrv, i) = incr;
10668 /* Like tsubst_copy for expressions, etc. but also does semantic
10672 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
10673 bool integral_constant_expression_p)
10675 #define RECUR(NODE) \
10676 tsubst_expr ((NODE), args, complain, in_decl, \
10677 integral_constant_expression_p)
10681 if (t == NULL_TREE || t == error_mark_node)
10684 if (EXPR_HAS_LOCATION (t))
10685 input_location = EXPR_LOCATION (t);
10686 if (STATEMENT_CODE_P (TREE_CODE (t)))
10687 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
10689 switch (TREE_CODE (t))
10691 case STATEMENT_LIST:
10693 tree_stmt_iterator i;
10694 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
10695 RECUR (tsi_stmt (i));
10699 case CTOR_INITIALIZER:
10700 finish_mem_initializers (tsubst_initializer_list
10701 (TREE_OPERAND (t, 0), args));
10705 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
10709 tmp = RECUR (EXPR_STMT_EXPR (t));
10710 if (EXPR_STMT_STMT_EXPR_RESULT (t))
10711 finish_stmt_expr_expr (tmp, cur_stmt_expr);
10713 finish_expr_stmt (tmp);
10717 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
10725 decl = DECL_EXPR_DECL (t);
10726 if (TREE_CODE (decl) == LABEL_DECL)
10727 finish_label_decl (DECL_NAME (decl));
10728 else if (TREE_CODE (decl) == USING_DECL)
10730 tree scope = USING_DECL_SCOPE (decl);
10731 tree name = DECL_NAME (decl);
10734 scope = RECUR (scope);
10735 decl = lookup_qualified_name (scope, name,
10736 /*is_type_p=*/false,
10737 /*complain=*/false);
10738 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
10739 qualified_name_lookup_error (scope, name, decl, input_location);
10741 do_local_using_decl (decl, scope, name);
10745 init = DECL_INITIAL (decl);
10746 decl = tsubst (decl, args, complain, in_decl);
10747 if (decl != error_mark_node)
10749 /* By marking the declaration as instantiated, we avoid
10750 trying to instantiate it. Since instantiate_decl can't
10751 handle local variables, and since we've already done
10752 all that needs to be done, that's the right thing to
10754 if (TREE_CODE (decl) == VAR_DECL)
10755 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
10756 if (TREE_CODE (decl) == VAR_DECL
10757 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
10758 /* Anonymous aggregates are a special case. */
10759 finish_anon_union (decl);
10762 maybe_push_decl (decl);
10763 if (TREE_CODE (decl) == VAR_DECL
10764 && DECL_PRETTY_FUNCTION_P (decl))
10766 /* For __PRETTY_FUNCTION__ we have to adjust the
10768 const char *const name
10769 = cxx_printable_name (current_function_decl, 2);
10770 init = cp_fname_init (name, &TREE_TYPE (decl));
10774 tree t = RECUR (init);
10777 /* If we had an initializer but it
10778 instantiated to nothing,
10779 value-initialize the object. This will
10780 only occur when the initializer was a
10781 pack expansion where the parameter packs
10782 used in that expansion were of length
10784 init = build_value_init (TREE_TYPE (decl));
10789 finish_decl (decl, init, NULL_TREE, NULL_TREE);
10794 /* A DECL_EXPR can also be used as an expression, in the condition
10795 clause of an if/for/while construct. */
10800 stmt = begin_for_stmt ();
10801 RECUR (FOR_INIT_STMT (t));
10802 finish_for_init_stmt (stmt);
10803 tmp = RECUR (FOR_COND (t));
10804 finish_for_cond (tmp, stmt);
10805 tmp = RECUR (FOR_EXPR (t));
10806 finish_for_expr (tmp, stmt);
10807 RECUR (FOR_BODY (t));
10808 finish_for_stmt (stmt);
10812 stmt = begin_while_stmt ();
10813 tmp = RECUR (WHILE_COND (t));
10814 finish_while_stmt_cond (tmp, stmt);
10815 RECUR (WHILE_BODY (t));
10816 finish_while_stmt (stmt);
10820 stmt = begin_do_stmt ();
10821 RECUR (DO_BODY (t));
10822 finish_do_body (stmt);
10823 tmp = RECUR (DO_COND (t));
10824 finish_do_stmt (tmp, stmt);
10828 stmt = begin_if_stmt ();
10829 tmp = RECUR (IF_COND (t));
10830 finish_if_stmt_cond (tmp, stmt);
10831 RECUR (THEN_CLAUSE (t));
10832 finish_then_clause (stmt);
10834 if (ELSE_CLAUSE (t))
10836 begin_else_clause (stmt);
10837 RECUR (ELSE_CLAUSE (t));
10838 finish_else_clause (stmt);
10841 finish_if_stmt (stmt);
10845 if (BIND_EXPR_BODY_BLOCK (t))
10846 stmt = begin_function_body ();
10848 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
10849 ? BCS_TRY_BLOCK : 0);
10851 RECUR (BIND_EXPR_BODY (t));
10853 if (BIND_EXPR_BODY_BLOCK (t))
10854 finish_function_body (stmt);
10856 finish_compound_stmt (stmt);
10860 finish_break_stmt ();
10863 case CONTINUE_STMT:
10864 finish_continue_stmt ();
10868 stmt = begin_switch_stmt ();
10869 tmp = RECUR (SWITCH_STMT_COND (t));
10870 finish_switch_cond (tmp, stmt);
10871 RECUR (SWITCH_STMT_BODY (t));
10872 finish_switch_stmt (stmt);
10875 case CASE_LABEL_EXPR:
10876 finish_case_label (RECUR (CASE_LOW (t)),
10877 RECUR (CASE_HIGH (t)));
10881 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
10885 tmp = GOTO_DESTINATION (t);
10886 if (TREE_CODE (tmp) != LABEL_DECL)
10887 /* Computed goto's must be tsubst'd into. On the other hand,
10888 non-computed gotos must not be; the identifier in question
10889 will have no binding. */
10892 tmp = DECL_NAME (tmp);
10893 finish_goto_stmt (tmp);
10897 tmp = finish_asm_stmt
10898 (ASM_VOLATILE_P (t),
10899 RECUR (ASM_STRING (t)),
10900 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
10901 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
10902 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
10904 tree asm_expr = tmp;
10905 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
10906 asm_expr = TREE_OPERAND (asm_expr, 0);
10907 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
10914 stmt = begin_try_block ();
10915 RECUR (TRY_STMTS (t));
10916 finish_cleanup_try_block (stmt);
10917 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
10921 tree compound_stmt = NULL_TREE;
10923 if (FN_TRY_BLOCK_P (t))
10924 stmt = begin_function_try_block (&compound_stmt);
10926 stmt = begin_try_block ();
10928 RECUR (TRY_STMTS (t));
10930 if (FN_TRY_BLOCK_P (t))
10931 finish_function_try_block (stmt);
10933 finish_try_block (stmt);
10935 RECUR (TRY_HANDLERS (t));
10936 if (FN_TRY_BLOCK_P (t))
10937 finish_function_handler_sequence (stmt, compound_stmt);
10939 finish_handler_sequence (stmt);
10945 tree decl = HANDLER_PARMS (t);
10949 decl = tsubst (decl, args, complain, in_decl);
10950 /* Prevent instantiate_decl from trying to instantiate
10951 this variable. We've already done all that needs to be
10953 if (decl != error_mark_node)
10954 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
10956 stmt = begin_handler ();
10957 finish_handler_parms (decl, stmt);
10958 RECUR (HANDLER_BODY (t));
10959 finish_handler (stmt);
10964 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
10967 case STATIC_ASSERT:
10970 tsubst_expr (STATIC_ASSERT_CONDITION (t),
10973 /*integral_constant_expression_p=*/true);
10974 finish_static_assert (condition,
10975 STATIC_ASSERT_MESSAGE (t),
10976 STATIC_ASSERT_SOURCE_LOCATION (t),
10977 /*member_p=*/false);
10982 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
10983 args, complain, in_decl);
10984 stmt = begin_omp_parallel ();
10985 RECUR (OMP_PARALLEL_BODY (t));
10986 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
10987 = OMP_PARALLEL_COMBINED (t);
10991 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
10992 args, complain, in_decl);
10993 stmt = begin_omp_task ();
10994 RECUR (OMP_TASK_BODY (t));
10995 finish_omp_task (tmp, stmt);
11000 tree clauses, body, pre_body;
11001 tree declv, initv, condv, incrv;
11004 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
11005 args, complain, in_decl);
11006 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11007 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11008 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11009 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11011 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
11012 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
11013 &clauses, args, complain, in_decl,
11014 integral_constant_expression_p);
11016 stmt = begin_omp_structured_block ();
11018 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
11019 if (TREE_VEC_ELT (initv, i) == NULL
11020 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
11021 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
11022 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
11024 tree init = RECUR (TREE_VEC_ELT (initv, i));
11025 gcc_assert (init == TREE_VEC_ELT (declv, i));
11026 TREE_VEC_ELT (initv, i) = NULL_TREE;
11030 tree decl_expr = TREE_VEC_ELT (initv, i);
11031 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
11032 gcc_assert (init != NULL);
11033 TREE_VEC_ELT (initv, i) = RECUR (init);
11034 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
11036 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
11039 pre_body = push_stmt_list ();
11040 RECUR (OMP_FOR_PRE_BODY (t));
11041 pre_body = pop_stmt_list (pre_body);
11043 body = push_stmt_list ();
11044 RECUR (OMP_FOR_BODY (t));
11045 body = pop_stmt_list (body);
11047 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
11048 body, pre_body, clauses);
11050 add_stmt (finish_omp_structured_block (stmt));
11056 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
11057 stmt = push_stmt_list ();
11058 RECUR (OMP_BODY (t));
11059 stmt = pop_stmt_list (stmt);
11062 OMP_BODY (t) = stmt;
11063 OMP_CLAUSES (t) = tmp;
11071 stmt = push_stmt_list ();
11072 RECUR (OMP_BODY (t));
11073 stmt = pop_stmt_list (stmt);
11076 OMP_BODY (t) = stmt;
11081 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
11083 tree op1 = TREE_OPERAND (t, 1);
11084 tree lhs = RECUR (TREE_OPERAND (op1, 0));
11085 tree rhs = RECUR (TREE_OPERAND (op1, 1));
11086 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
11090 case EXPR_PACK_EXPANSION:
11091 error ("invalid use of pack expansion expression");
11092 return error_mark_node;
11094 case NONTYPE_ARGUMENT_PACK:
11095 error ("use %<...%> to expand argument pack");
11096 return error_mark_node;
11099 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
11101 return tsubst_copy_and_build (t, args, complain, in_decl,
11102 /*function_p=*/false,
11103 integral_constant_expression_p);
11110 /* T is a postfix-expression that is not being used in a function
11111 call. Return the substituted version of T. */
11114 tsubst_non_call_postfix_expression (tree t, tree args,
11115 tsubst_flags_t complain,
11118 if (TREE_CODE (t) == SCOPE_REF)
11119 t = tsubst_qualified_id (t, args, complain, in_decl,
11120 /*done=*/false, /*address_p=*/false);
11122 t = tsubst_copy_and_build (t, args, complain, in_decl,
11123 /*function_p=*/false,
11124 /*integral_constant_expression_p=*/false);
11129 /* Like tsubst but deals with expressions and performs semantic
11130 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
11133 tsubst_copy_and_build (tree t,
11135 tsubst_flags_t complain,
11138 bool integral_constant_expression_p)
11140 #define RECUR(NODE) \
11141 tsubst_copy_and_build (NODE, args, complain, in_decl, \
11142 /*function_p=*/false, \
11143 integral_constant_expression_p)
11147 if (t == NULL_TREE || t == error_mark_node)
11150 switch (TREE_CODE (t))
11154 /* Fall through. */
11155 case IDENTIFIER_NODE:
11159 bool non_integral_constant_expression_p;
11160 const char *error_msg;
11162 if (IDENTIFIER_TYPENAME_P (t))
11164 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11165 t = mangle_conv_op_name_for_type (new_type);
11168 /* Look up the name. */
11169 decl = lookup_name (t);
11171 /* By convention, expressions use ERROR_MARK_NODE to indicate
11172 failure, not NULL_TREE. */
11173 if (decl == NULL_TREE)
11174 decl = error_mark_node;
11176 decl = finish_id_expression (t, decl, NULL_TREE,
11178 integral_constant_expression_p,
11179 /*allow_non_integral_constant_expression_p=*/false,
11180 &non_integral_constant_expression_p,
11181 /*template_p=*/false,
11183 /*address_p=*/false,
11184 /*template_arg_p=*/false,
11189 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
11190 decl = unqualified_name_lookup_error (decl);
11194 case TEMPLATE_ID_EXPR:
11197 tree templ = RECUR (TREE_OPERAND (t, 0));
11198 tree targs = TREE_OPERAND (t, 1);
11201 targs = tsubst_template_args (targs, args, complain, in_decl);
11203 if (TREE_CODE (templ) == COMPONENT_REF)
11205 object = TREE_OPERAND (templ, 0);
11206 templ = TREE_OPERAND (templ, 1);
11209 object = NULL_TREE;
11210 templ = lookup_template_function (templ, targs);
11213 return build3 (COMPONENT_REF, TREE_TYPE (templ),
11214 object, templ, NULL_TREE);
11216 return baselink_for_fns (templ);
11221 tree r = RECUR (TREE_OPERAND (t, 0));
11223 if (REFERENCE_REF_P (t))
11225 /* A type conversion to reference type will be enclosed in
11226 such an indirect ref, but the substitution of the cast
11227 will have also added such an indirect ref. */
11228 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
11229 r = convert_from_reference (r);
11232 r = build_x_indirect_ref (r, "unary *", complain);
11238 (tsubst (TREE_TYPE (t), args, complain, in_decl),
11239 RECUR (TREE_OPERAND (t, 0)));
11242 case REINTERPRET_CAST_EXPR:
11243 case CONST_CAST_EXPR:
11244 case DYNAMIC_CAST_EXPR:
11245 case STATIC_CAST_EXPR:
11250 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11251 if (integral_constant_expression_p
11252 && !cast_valid_in_integral_constant_expression_p (type))
11254 if (complain & tf_error)
11255 error ("a cast to a type other than an integral or "
11256 "enumeration type cannot appear in a constant-expression");
11257 return error_mark_node;
11260 op = RECUR (TREE_OPERAND (t, 0));
11262 switch (TREE_CODE (t))
11265 return build_functional_cast (type, op, complain);
11266 case REINTERPRET_CAST_EXPR:
11267 return build_reinterpret_cast (type, op, complain);
11268 case CONST_CAST_EXPR:
11269 return build_const_cast (type, op, complain);
11270 case DYNAMIC_CAST_EXPR:
11271 return build_dynamic_cast (type, op, complain);
11272 case STATIC_CAST_EXPR:
11273 return build_static_cast (type, op, complain);
11275 gcc_unreachable ();
11279 case POSTDECREMENT_EXPR:
11280 case POSTINCREMENT_EXPR:
11281 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11282 args, complain, in_decl);
11283 return build_x_unary_op (TREE_CODE (t), op1, complain);
11285 case PREDECREMENT_EXPR:
11286 case PREINCREMENT_EXPR:
11290 case TRUTH_NOT_EXPR:
11291 case UNARY_PLUS_EXPR: /* Unary + */
11292 case REALPART_EXPR:
11293 case IMAGPART_EXPR:
11294 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
11298 op1 = TREE_OPERAND (t, 0);
11299 if (TREE_CODE (op1) == SCOPE_REF)
11300 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
11301 /*done=*/true, /*address_p=*/true);
11303 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
11305 if (TREE_CODE (op1) == LABEL_DECL)
11306 return finish_label_address_expr (DECL_NAME (op1),
11307 EXPR_LOCATION (op1));
11308 return build_x_unary_op (ADDR_EXPR, op1, complain);
11313 case TRUNC_DIV_EXPR:
11314 case CEIL_DIV_EXPR:
11315 case FLOOR_DIV_EXPR:
11316 case ROUND_DIV_EXPR:
11317 case EXACT_DIV_EXPR:
11321 case TRUNC_MOD_EXPR:
11322 case FLOOR_MOD_EXPR:
11323 case TRUTH_ANDIF_EXPR:
11324 case TRUTH_ORIF_EXPR:
11325 case TRUTH_AND_EXPR:
11326 case TRUTH_OR_EXPR:
11341 return build_x_binary_op
11343 RECUR (TREE_OPERAND (t, 0)),
11344 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
11346 : TREE_CODE (TREE_OPERAND (t, 0))),
11347 RECUR (TREE_OPERAND (t, 1)),
11348 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
11350 : TREE_CODE (TREE_OPERAND (t, 1))),
11351 /*overloaded_p=*/NULL,
11355 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
11356 /*address_p=*/false);
11358 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11359 args, complain, in_decl);
11360 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
11363 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
11364 return tsubst_copy (t, args, complain, in_decl);
11368 op1 = TREE_OPERAND (t, 0);
11371 /* When there are no ARGS, we are trying to evaluate a
11372 non-dependent expression from the parser. Trying to do
11373 the substitutions may not work. */
11375 op1 = TREE_TYPE (op1);
11380 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
11381 /*function_p=*/false,
11382 /*integral_constant_expression_p=*/false);
11386 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
11387 complain & tf_error);
11389 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
11390 complain & tf_error);
11394 tree r = build_x_modify_expr
11395 (RECUR (TREE_OPERAND (t, 0)),
11396 TREE_CODE (TREE_OPERAND (t, 1)),
11397 RECUR (TREE_OPERAND (t, 2)),
11399 /* TREE_NO_WARNING must be set if either the expression was
11400 parenthesized or it uses an operator such as >>= rather
11401 than plain assignment. In the former case, it was already
11402 set and must be copied. In the latter case,
11403 build_x_modify_expr sets it and it must not be reset
11405 if (TREE_NO_WARNING (t))
11406 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
11411 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11412 args, complain, in_decl);
11413 /* Remember that there was a reference to this entity. */
11416 return build_x_arrow (op1);
11420 tree init = RECUR (TREE_OPERAND (t, 3));
11422 if (TREE_OPERAND (t, 3) && !init)
11423 /* If there was an initializer in the original tree, but
11424 it instantiated to an empty list, then we should pass on
11425 VOID_ZERO_NODE to tell build_new that it was an empty
11426 initializer () rather than no initializer. This can only
11427 happen when the initializer is a pack expansion whose
11428 parameter packs are of length zero. */
11429 init = void_zero_node;
11432 (RECUR (TREE_OPERAND (t, 0)),
11433 RECUR (TREE_OPERAND (t, 1)),
11434 RECUR (TREE_OPERAND (t, 2)),
11436 NEW_EXPR_USE_GLOBAL (t),
11441 return delete_sanity
11442 (RECUR (TREE_OPERAND (t, 0)),
11443 RECUR (TREE_OPERAND (t, 1)),
11444 DELETE_EXPR_USE_VEC (t),
11445 DELETE_EXPR_USE_GLOBAL (t));
11447 case COMPOUND_EXPR:
11448 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
11449 RECUR (TREE_OPERAND (t, 1)),
11459 function = CALL_EXPR_FN (t);
11460 /* When we parsed the expression, we determined whether or
11461 not Koenig lookup should be performed. */
11462 koenig_p = KOENIG_LOOKUP_P (t);
11463 if (TREE_CODE (function) == SCOPE_REF)
11465 qualified_p = true;
11466 function = tsubst_qualified_id (function, args, complain, in_decl,
11468 /*address_p=*/false);
11472 if (TREE_CODE (function) == COMPONENT_REF)
11474 tree op = TREE_OPERAND (function, 1);
11476 qualified_p = (TREE_CODE (op) == SCOPE_REF
11477 || (BASELINK_P (op)
11478 && BASELINK_QUALIFIED_P (op)));
11481 qualified_p = false;
11483 function = tsubst_copy_and_build (function, args, complain,
11486 integral_constant_expression_p);
11488 if (BASELINK_P (function))
11489 qualified_p = true;
11492 /* FIXME: Rewrite this so as not to construct an arglist. */
11493 call_args = RECUR (CALL_EXPR_ARGS (t));
11495 /* We do not perform argument-dependent lookup if normal
11496 lookup finds a non-function, in accordance with the
11497 expected resolution of DR 218. */
11499 && ((is_overloaded_fn (function)
11500 /* If lookup found a member function, the Koenig lookup is
11501 not appropriate, even if an unqualified-name was used
11502 to denote the function. */
11503 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
11504 || TREE_CODE (function) == IDENTIFIER_NODE)
11505 /* Only do this when substitution turns a dependent call
11506 into a non-dependent call. */
11507 && type_dependent_expression_p_push (t)
11508 && !any_type_dependent_arguments_p (call_args))
11509 function = perform_koenig_lookup (function, call_args);
11511 if (TREE_CODE (function) == IDENTIFIER_NODE)
11513 unqualified_name_lookup_error (function);
11514 return error_mark_node;
11517 /* Remember that there was a reference to this entity. */
11518 if (DECL_P (function))
11519 mark_used (function);
11521 if (TREE_CODE (function) == OFFSET_REF)
11522 return build_offset_ref_call_from_tree (function, call_args);
11523 if (TREE_CODE (function) == COMPONENT_REF)
11525 if (!BASELINK_P (TREE_OPERAND (function, 1)))
11526 return finish_call_expr (function, call_args,
11527 /*disallow_virtual=*/false,
11528 /*koenig_p=*/false,
11531 return (build_new_method_call
11532 (TREE_OPERAND (function, 0),
11533 TREE_OPERAND (function, 1),
11534 call_args, NULL_TREE,
11535 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
11539 return finish_call_expr (function, call_args,
11540 /*disallow_virtual=*/qualified_p,
11546 return build_x_conditional_expr
11547 (RECUR (TREE_OPERAND (t, 0)),
11548 RECUR (TREE_OPERAND (t, 1)),
11549 RECUR (TREE_OPERAND (t, 2)),
11552 case PSEUDO_DTOR_EXPR:
11553 return finish_pseudo_destructor_expr
11554 (RECUR (TREE_OPERAND (t, 0)),
11555 RECUR (TREE_OPERAND (t, 1)),
11556 RECUR (TREE_OPERAND (t, 2)));
11560 tree purpose, value, chain;
11562 if (t == void_list_node)
11565 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
11566 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
11568 /* We have pack expansions, so expand those and
11569 create a new list out of it. */
11570 tree purposevec = NULL_TREE;
11571 tree valuevec = NULL_TREE;
11575 /* Expand the argument expressions. */
11576 if (TREE_PURPOSE (t))
11577 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
11578 complain, in_decl);
11579 if (TREE_VALUE (t))
11580 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
11581 complain, in_decl);
11583 /* Build the rest of the list. */
11584 chain = TREE_CHAIN (t);
11585 if (chain && chain != void_type_node)
11586 chain = RECUR (chain);
11588 /* Determine the number of arguments. */
11589 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
11591 len = TREE_VEC_LENGTH (purposevec);
11592 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
11594 else if (TREE_CODE (valuevec) == TREE_VEC)
11595 len = TREE_VEC_LENGTH (valuevec);
11598 /* Since we only performed a partial substitution into
11599 the argument pack, we only return a single list
11601 if (purposevec == TREE_PURPOSE (t)
11602 && valuevec == TREE_VALUE (t)
11603 && chain == TREE_CHAIN (t))
11606 return tree_cons (purposevec, valuevec, chain);
11609 /* Convert the argument vectors into a TREE_LIST */
11613 /* Grab the Ith values. */
11615 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
11618 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
11621 /* Build the list (backwards). */
11622 chain = tree_cons (purpose, value, chain);
11628 purpose = TREE_PURPOSE (t);
11630 purpose = RECUR (purpose);
11631 value = TREE_VALUE (t);
11633 value = RECUR (value);
11634 chain = TREE_CHAIN (t);
11635 if (chain && chain != void_type_node)
11636 chain = RECUR (chain);
11637 if (purpose == TREE_PURPOSE (t)
11638 && value == TREE_VALUE (t)
11639 && chain == TREE_CHAIN (t))
11641 return tree_cons (purpose, value, chain);
11644 case COMPONENT_REF:
11650 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11651 args, complain, in_decl);
11652 /* Remember that there was a reference to this entity. */
11653 if (DECL_P (object))
11654 mark_used (object);
11655 object_type = TREE_TYPE (object);
11657 member = TREE_OPERAND (t, 1);
11658 if (BASELINK_P (member))
11659 member = tsubst_baselink (member,
11660 non_reference (TREE_TYPE (object)),
11661 args, complain, in_decl);
11663 member = tsubst_copy (member, args, complain, in_decl);
11664 if (member == error_mark_node)
11665 return error_mark_node;
11667 if (object_type && !CLASS_TYPE_P (object_type))
11669 if (SCALAR_TYPE_P (object_type))
11671 tree s = NULL_TREE;
11672 tree dtor = member;
11674 if (TREE_CODE (dtor) == SCOPE_REF)
11676 s = TREE_OPERAND (dtor, 0);
11677 dtor = TREE_OPERAND (dtor, 1);
11679 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
11681 dtor = TREE_OPERAND (dtor, 0);
11683 return finish_pseudo_destructor_expr (object, s, dtor);
11687 else if (TREE_CODE (member) == SCOPE_REF
11688 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
11693 /* Lookup the template functions now that we know what the
11695 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
11696 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
11697 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
11698 /*is_type_p=*/false,
11699 /*complain=*/false);
11700 if (BASELINK_P (member))
11702 BASELINK_FUNCTIONS (member)
11703 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
11705 member = (adjust_result_of_qualified_name_lookup
11706 (member, BINFO_TYPE (BASELINK_BINFO (member)),
11711 qualified_name_lookup_error (object_type, tmpl, member,
11713 return error_mark_node;
11716 else if (TREE_CODE (member) == SCOPE_REF
11717 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
11718 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
11720 if (complain & tf_error)
11722 if (TYPE_P (TREE_OPERAND (member, 0)))
11723 error ("%qT is not a class or namespace",
11724 TREE_OPERAND (member, 0));
11726 error ("%qD is not a class or namespace",
11727 TREE_OPERAND (member, 0));
11729 return error_mark_node;
11731 else if (TREE_CODE (member) == FIELD_DECL)
11732 return finish_non_static_data_member (member, object, NULL_TREE);
11734 return finish_class_member_access_expr (object, member,
11735 /*template_p=*/false,
11741 (RECUR (TREE_OPERAND (t, 0)));
11745 VEC(constructor_elt,gc) *n;
11746 constructor_elt *ce;
11747 unsigned HOST_WIDE_INT idx;
11748 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11749 bool process_index_p;
11751 bool need_copy_p = false;
11754 if (type == error_mark_node)
11755 return error_mark_node;
11757 /* digest_init will do the wrong thing if we let it. */
11758 if (type && TYPE_PTRMEMFUNC_P (type))
11761 /* We do not want to process the index of aggregate
11762 initializers as they are identifier nodes which will be
11763 looked up by digest_init. */
11764 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
11766 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
11767 newlen = VEC_length (constructor_elt, n);
11768 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
11770 if (ce->index && process_index_p)
11771 ce->index = RECUR (ce->index);
11773 if (PACK_EXPANSION_P (ce->value))
11775 /* Substitute into the pack expansion. */
11776 ce->value = tsubst_pack_expansion (ce->value, args, complain,
11779 if (ce->value == error_mark_node)
11781 else if (TREE_VEC_LENGTH (ce->value) == 1)
11782 /* Just move the argument into place. */
11783 ce->value = TREE_VEC_ELT (ce->value, 0);
11786 /* Update the length of the final CONSTRUCTOR
11787 arguments vector, and note that we will need to
11789 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
11790 need_copy_p = true;
11794 ce->value = RECUR (ce->value);
11799 VEC(constructor_elt,gc) *old_n = n;
11801 n = VEC_alloc (constructor_elt, gc, newlen);
11802 for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
11805 if (TREE_CODE (ce->value) == TREE_VEC)
11807 int i, len = TREE_VEC_LENGTH (ce->value);
11808 for (i = 0; i < len; ++i)
11809 CONSTRUCTOR_APPEND_ELT (n, 0,
11810 TREE_VEC_ELT (ce->value, i));
11813 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
11817 r = build_constructor (init_list_type_node, n);
11818 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
11820 if (TREE_HAS_CONSTRUCTOR (t))
11821 return finish_compound_literal (type, r);
11828 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
11829 if (TYPE_P (operand_0))
11830 return get_typeid (operand_0);
11831 return build_typeid (operand_0);
11841 tree r = tsubst_copy (t, args, complain, in_decl);
11843 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
11844 /* If the original type was a reference, we'll be wrapped in
11845 the appropriate INDIRECT_REF. */
11846 r = convert_from_reference (r);
11851 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
11852 tsubst_copy (TREE_TYPE (t), args, complain,
11855 case OFFSETOF_EXPR:
11856 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
11860 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
11861 complain, in_decl);
11863 tree type2 = TRAIT_EXPR_TYPE2 (t);
11865 type2 = tsubst_copy (type2, args, complain, in_decl);
11867 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
11872 tree old_stmt_expr = cur_stmt_expr;
11873 tree stmt_expr = begin_stmt_expr ();
11875 cur_stmt_expr = stmt_expr;
11876 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
11877 integral_constant_expression_p);
11878 stmt_expr = finish_stmt_expr (stmt_expr, false);
11879 cur_stmt_expr = old_stmt_expr;
11885 t = tsubst_copy (t, args, complain, in_decl);
11886 /* As in finish_id_expression, we resolve enumeration constants
11887 to their underlying values. */
11888 if (TREE_CODE (t) == CONST_DECL)
11890 used_types_insert (TREE_TYPE (t));
11891 return DECL_INITIAL (t);
11896 /* Handle Objective-C++ constructs, if appropriate. */
11899 = objcp_tsubst_copy_and_build (t, args, complain,
11900 in_decl, /*function_p=*/false);
11904 return tsubst_copy (t, args, complain, in_decl);
11910 /* Verify that the instantiated ARGS are valid. For type arguments,
11911 make sure that the type's linkage is ok. For non-type arguments,
11912 make sure they are constants if they are integral or enumerations.
11913 Emit an error under control of COMPLAIN, and return TRUE on error. */
11916 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
11918 if (ARGUMENT_PACK_P (t))
11920 tree vec = ARGUMENT_PACK_ARGS (t);
11921 int len = TREE_VEC_LENGTH (vec);
11922 bool result = false;
11925 for (i = 0; i < len; ++i)
11926 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
11930 else if (TYPE_P (t))
11932 /* [basic.link]: A name with no linkage (notably, the name
11933 of a class or enumeration declared in a local scope)
11934 shall not be used to declare an entity with linkage.
11935 This implies that names with no linkage cannot be used as
11936 template arguments. */
11937 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
11941 /* DR 488 makes use of a type with no linkage cause
11942 type deduction to fail. */
11943 if (complain & tf_error)
11945 if (TYPE_ANONYMOUS_P (nt))
11946 error ("%qT is/uses anonymous type", t);
11948 error ("template argument for %qD uses local type %qT",
11953 /* In order to avoid all sorts of complications, we do not
11954 allow variably-modified types as template arguments. */
11955 else if (variably_modified_type_p (t, NULL_TREE))
11957 if (complain & tf_error)
11958 error ("%qT is a variably modified type", t);
11962 /* A non-type argument of integral or enumerated type must be a
11964 else if (TREE_TYPE (t)
11965 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
11966 && !TREE_CONSTANT (t))
11968 if (complain & tf_error)
11969 error ("integral expression %qE is not constant", t);
11976 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
11978 int ix, len = DECL_NTPARMS (tmpl);
11979 bool result = false;
11981 for (ix = 0; ix != len; ix++)
11983 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
11986 if (result && (complain & tf_error))
11987 error (" trying to instantiate %qD", tmpl);
11991 /* Instantiate the indicated variable or function template TMPL with
11992 the template arguments in TARG_PTR. */
11995 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
12000 HOST_WIDE_INT saved_processing_template_decl;
12002 if (tmpl == error_mark_node)
12003 return error_mark_node;
12005 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
12007 /* If this function is a clone, handle it specially. */
12008 if (DECL_CLONED_FUNCTION_P (tmpl))
12013 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
12015 if (spec == error_mark_node)
12016 return error_mark_node;
12018 /* Look for the clone. */
12019 FOR_EACH_CLONE (clone, spec)
12020 if (DECL_NAME (clone) == DECL_NAME (tmpl))
12022 /* We should always have found the clone by now. */
12023 gcc_unreachable ();
12027 /* Check to see if we already have this specialization. */
12028 spec = retrieve_specialization (tmpl, targ_ptr,
12029 /*class_specializations_p=*/false);
12030 if (spec != NULL_TREE)
12033 gen_tmpl = most_general_template (tmpl);
12034 if (tmpl != gen_tmpl)
12036 /* The TMPL is a partial instantiation. To get a full set of
12037 arguments we must add the arguments used to perform the
12038 partial instantiation. */
12039 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
12042 /* Check to see if we already have this specialization. */
12043 spec = retrieve_specialization (gen_tmpl, targ_ptr,
12044 /*class_specializations_p=*/false);
12045 if (spec != NULL_TREE)
12049 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
12051 return error_mark_node;
12053 /* We are building a FUNCTION_DECL, during which the access of its
12054 parameters and return types have to be checked. However this
12055 FUNCTION_DECL which is the desired context for access checking
12056 is not built yet. We solve this chicken-and-egg problem by
12057 deferring all checks until we have the FUNCTION_DECL. */
12058 push_deferring_access_checks (dk_deferred);
12060 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
12061 (because, for example, we have encountered a non-dependent
12062 function call in the body of a template function and must now
12063 determine which of several overloaded functions will be called),
12064 within the instantiation itself we are not processing a
12066 saved_processing_template_decl = processing_template_decl;
12067 processing_template_decl = 0;
12068 /* Substitute template parameters to obtain the specialization. */
12069 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
12070 targ_ptr, complain, gen_tmpl);
12071 processing_template_decl = saved_processing_template_decl;
12072 if (fndecl == error_mark_node)
12073 return error_mark_node;
12075 /* Now we know the specialization, compute access previously
12077 push_access_scope (fndecl);
12079 /* Some typedefs referenced from within the template code need to be access
12080 checked at template instantiation time, i.e now. These types were
12081 added to the template at parsing time. Let's get those and perfom
12082 the acces checks then. */
12083 perform_typedefs_access_check (tmpl, targ_ptr);
12084 perform_deferred_access_checks ();
12085 pop_access_scope (fndecl);
12086 pop_deferring_access_checks ();
12088 /* The DECL_TI_TEMPLATE should always be the immediate parent
12089 template, not the most general template. */
12090 DECL_TI_TEMPLATE (fndecl) = tmpl;
12092 /* If we've just instantiated the main entry point for a function,
12093 instantiate all the alternate entry points as well. We do this
12094 by cloning the instantiation of the main entry point, not by
12095 instantiating the template clones. */
12096 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
12097 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
12102 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
12103 arguments that are being used when calling it. TARGS is a vector
12104 into which the deduced template arguments are placed.
12106 Return zero for success, 2 for an incomplete match that doesn't resolve
12107 all the types, and 1 for complete failure. An error message will be
12108 printed only for an incomplete match.
12110 If FN is a conversion operator, or we are trying to produce a specific
12111 specialization, RETURN_TYPE is the return type desired.
12113 The EXPLICIT_TARGS are explicit template arguments provided via a
12116 The parameter STRICT is one of:
12119 We are deducing arguments for a function call, as in
12120 [temp.deduct.call].
12123 We are deducing arguments for a conversion function, as in
12124 [temp.deduct.conv].
12127 We are deducing arguments when doing an explicit instantiation
12128 as in [temp.explicit], when determining an explicit specialization
12129 as in [temp.expl.spec], or when taking the address of a function
12130 template, as in [temp.deduct.funcaddr]. */
12133 fn_type_unification (tree fn,
12134 tree explicit_targs,
12138 unification_kind_t strict,
12144 bool incomplete_argument_packs_p = false;
12146 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
12148 fntype = TREE_TYPE (fn);
12149 if (explicit_targs)
12153 The specified template arguments must match the template
12154 parameters in kind (i.e., type, nontype, template), and there
12155 must not be more arguments than there are parameters;
12156 otherwise type deduction fails.
12158 Nontype arguments must match the types of the corresponding
12159 nontype template parameters, or must be convertible to the
12160 types of the corresponding nontype parameters as specified in
12161 _temp.arg.nontype_, otherwise type deduction fails.
12163 All references in the function type of the function template
12164 to the corresponding template parameters are replaced by the
12165 specified template argument values. If a substitution in a
12166 template parameter or in the function type of the function
12167 template results in an invalid type, type deduction fails. */
12168 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
12169 int i, len = TREE_VEC_LENGTH (tparms);
12170 tree converted_args;
12171 bool incomplete = false;
12173 if (explicit_targs == error_mark_node)
12177 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
12178 /*require_all_args=*/false,
12179 /*use_default_args=*/false));
12180 if (converted_args == error_mark_node)
12183 /* Substitute the explicit args into the function type. This is
12184 necessary so that, for instance, explicitly declared function
12185 arguments can match null pointed constants. If we were given
12186 an incomplete set of explicit args, we must not do semantic
12187 processing during substitution as we could create partial
12189 for (i = 0; i < len; i++)
12191 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
12192 bool parameter_pack = false;
12194 /* Dig out the actual parm. */
12195 if (TREE_CODE (parm) == TYPE_DECL
12196 || TREE_CODE (parm) == TEMPLATE_DECL)
12198 parm = TREE_TYPE (parm);
12199 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
12201 else if (TREE_CODE (parm) == PARM_DECL)
12203 parm = DECL_INITIAL (parm);
12204 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
12207 if (parameter_pack)
12211 template_parm_level_and_index (parm, &level, &idx);
12213 /* Mark the argument pack as "incomplete". We could
12214 still deduce more arguments during unification. */
12215 targ = TMPL_ARG (converted_args, level, idx);
12218 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
12219 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
12220 = ARGUMENT_PACK_ARGS (targ);
12223 /* We have some incomplete argument packs. */
12224 incomplete_argument_packs_p = true;
12228 if (incomplete_argument_packs_p)
12229 /* Any substitution is guaranteed to be incomplete if there
12230 are incomplete argument packs, because we can still deduce
12234 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
12236 processing_template_decl += incomplete;
12237 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
12238 processing_template_decl -= incomplete;
12240 if (fntype == error_mark_node)
12243 /* Place the explicitly specified arguments in TARGS. */
12244 for (i = NUM_TMPL_ARGS (converted_args); i--;)
12245 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
12248 /* Never do unification on the 'this' parameter. */
12249 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
12253 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
12254 args = tree_cons (NULL_TREE, return_type, args);
12257 /* We allow incomplete unification without an error message here
12258 because the standard doesn't seem to explicitly prohibit it. Our
12259 callers must be ready to deal with unification failures in any
12261 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
12262 targs, parms, args, /*subr=*/0,
12265 if (result == 0 && incomplete_argument_packs_p)
12267 int i, len = NUM_TMPL_ARGS (targs);
12269 /* Clear the "incomplete" flags on all argument packs. */
12270 for (i = 0; i < len; i++)
12272 tree arg = TREE_VEC_ELT (targs, i);
12273 if (ARGUMENT_PACK_P (arg))
12275 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
12276 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
12281 /* Now that we have bindings for all of the template arguments,
12282 ensure that the arguments deduced for the template template
12283 parameters have compatible template parameter lists. We cannot
12284 check this property before we have deduced all template
12285 arguments, because the template parameter types of a template
12286 template parameter might depend on prior template parameters
12287 deduced after the template template parameter. The following
12288 ill-formed example illustrates this issue:
12290 template<typename T, template<T> class C> void f(C<5>, T);
12292 template<int N> struct X {};
12295 f(X<5>(), 5l); // error: template argument deduction fails
12298 The template parameter list of 'C' depends on the template type
12299 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
12300 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
12301 time that we deduce 'C'. */
12303 && !template_template_parm_bindings_ok_p
12304 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
12308 /* All is well so far. Now, check:
12312 When all template arguments have been deduced, all uses of
12313 template parameters in nondeduced contexts are replaced with
12314 the corresponding deduced argument values. If the
12315 substitution results in an invalid type, as described above,
12316 type deduction fails. */
12318 tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
12319 if (substed == error_mark_node)
12322 /* If we're looking for an exact match, check that what we got
12323 is indeed an exact match. It might not be if some template
12324 parameters are used in non-deduced contexts. */
12325 if (strict == DEDUCE_EXACT)
12328 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
12331 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
12332 for (; arg && sarg;
12333 arg = TREE_CHAIN (arg), sarg = TREE_CHAIN (sarg))
12334 if (!same_type_p (TREE_VALUE (arg), TREE_VALUE (sarg)))
12342 /* Adjust types before performing type deduction, as described in
12343 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
12344 sections are symmetric. PARM is the type of a function parameter
12345 or the return type of the conversion function. ARG is the type of
12346 the argument passed to the call, or the type of the value
12347 initialized with the result of the conversion function.
12348 ARG_EXPR is the original argument expression, which may be null. */
12351 maybe_adjust_types_for_deduction (unification_kind_t strict,
12365 /* Swap PARM and ARG throughout the remainder of this
12366 function; the handling is precisely symmetric since PARM
12367 will initialize ARG rather than vice versa. */
12375 /* There is nothing to do in this case. */
12379 gcc_unreachable ();
12382 if (TREE_CODE (*parm) != REFERENCE_TYPE)
12384 /* [temp.deduct.call]
12386 If P is not a reference type:
12388 --If A is an array type, the pointer type produced by the
12389 array-to-pointer standard conversion (_conv.array_) is
12390 used in place of A for type deduction; otherwise,
12392 --If A is a function type, the pointer type produced by
12393 the function-to-pointer standard conversion
12394 (_conv.func_) is used in place of A for type deduction;
12397 --If A is a cv-qualified type, the top level
12398 cv-qualifiers of A's type are ignored for type
12400 if (TREE_CODE (*arg) == ARRAY_TYPE)
12401 *arg = build_pointer_type (TREE_TYPE (*arg));
12402 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
12403 *arg = build_pointer_type (*arg);
12405 *arg = TYPE_MAIN_VARIANT (*arg);
12408 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
12409 of the form T&&, where T is a template parameter, and the argument
12410 is an lvalue, T is deduced as A& */
12411 if (TREE_CODE (*parm) == REFERENCE_TYPE
12412 && TYPE_REF_IS_RVALUE (*parm)
12413 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
12414 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
12415 && arg_expr && real_lvalue_p (arg_expr))
12416 *arg = build_reference_type (*arg);
12418 /* [temp.deduct.call]
12420 If P is a cv-qualified type, the top level cv-qualifiers
12421 of P's type are ignored for type deduction. If P is a
12422 reference type, the type referred to by P is used for
12424 *parm = TYPE_MAIN_VARIANT (*parm);
12425 if (TREE_CODE (*parm) == REFERENCE_TYPE)
12427 *parm = TREE_TYPE (*parm);
12428 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
12431 /* DR 322. For conversion deduction, remove a reference type on parm
12432 too (which has been swapped into ARG). */
12433 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
12434 *arg = TREE_TYPE (*arg);
12439 /* Most parms like fn_type_unification.
12441 If SUBR is 1, we're being called recursively (to unify the
12442 arguments of a function or method parameter of a function
12446 type_unification_real (tree tparms,
12451 unification_kind_t strict,
12454 tree parm, arg, arg_expr;
12456 int ntparms = TREE_VEC_LENGTH (tparms);
12458 int saw_undeduced = 0;
12461 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
12462 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
12463 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
12464 gcc_assert (ntparms > 0);
12469 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
12470 | UNIFY_ALLOW_DERIVED);
12474 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
12478 sub_strict = UNIFY_ALLOW_NONE;
12482 gcc_unreachable ();
12489 while (parms && parms != void_list_node
12490 && args && args != void_list_node)
12492 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
12495 parm = TREE_VALUE (parms);
12496 parms = TREE_CHAIN (parms);
12497 arg = TREE_VALUE (args);
12498 args = TREE_CHAIN (args);
12501 if (arg == error_mark_node)
12503 if (arg == unknown_type_node)
12504 /* We can't deduce anything from this, but we might get all the
12505 template args from other function args. */
12508 /* Conversions will be performed on a function argument that
12509 corresponds with a function parameter that contains only
12510 non-deducible template parameters and explicitly specified
12511 template parameters. */
12512 if (!uses_template_parms (parm))
12517 type = TREE_TYPE (arg);
12521 if (same_type_p (parm, type))
12523 if (strict != DEDUCE_EXACT
12524 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
12533 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
12534 if (type_unknown_p (arg))
12536 /* [temp.deduct.type]
12538 A template-argument can be deduced from a pointer to
12539 function or pointer to member function argument if
12540 the set of overloaded functions does not contain
12541 function templates and at most one of a set of
12542 overloaded functions provides a unique match. */
12543 if (resolve_overloaded_unification
12544 (tparms, targs, parm, arg, strict, sub_strict))
12550 arg = unlowered_expr_type (arg);
12551 if (arg == error_mark_node)
12556 int arg_strict = sub_strict;
12559 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
12562 if (arg == init_list_type_node && arg_expr)
12564 if (unify (tparms, targs, parm, arg, arg_strict))
12571 && parms != void_list_node
12572 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
12574 /* Unify the remaining arguments with the pack expansion type. */
12576 tree parmvec = make_tree_vec (1);
12580 /* Count the number of arguments that remain. */
12581 for (t = args; t && t != void_list_node; t = TREE_CHAIN (t))
12584 /* Allocate a TREE_VEC and copy in all of the arguments */
12585 argvec = make_tree_vec (len);
12586 for (i = 0; args && args != void_list_node; args = TREE_CHAIN (args))
12588 TREE_VEC_ELT (argvec, i) = TREE_VALUE (args);
12592 /* Copy the parameter into parmvec. */
12593 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
12594 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
12595 /*call_args_p=*/true, /*subr=*/subr))
12598 /* Advance to the end of the list of parameters. */
12599 parms = TREE_CHAIN (parms);
12602 /* Fail if we've reached the end of the parm list, and more args
12603 are present, and the parm list isn't variadic. */
12604 if (args && args != void_list_node && parms == void_list_node)
12606 /* Fail if parms are left and they don't have default values. */
12607 if (parms && parms != void_list_node
12608 && TREE_PURPOSE (parms) == NULL_TREE)
12612 for (i = 0; i < ntparms; i++)
12613 if (!TREE_VEC_ELT (targs, i))
12617 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
12620 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
12622 /* If this is an undeduced nontype parameter that depends on
12623 a type parameter, try another pass; its type may have been
12624 deduced from a later argument than the one from which
12625 this parameter can be deduced. */
12626 if (TREE_CODE (tparm) == PARM_DECL
12627 && uses_template_parms (TREE_TYPE (tparm))
12628 && !saw_undeduced++)
12631 /* Core issue #226 (C++0x) [temp.deduct]:
12633 If a template argument has not been deduced, its
12634 default template argument, if any, is used.
12636 When we are in C++98 mode, TREE_PURPOSE will either
12637 be NULL_TREE or ERROR_MARK_NODE, so we do not need
12638 to explicitly check cxx_dialect here. */
12639 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
12641 tree arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)),
12642 targs, tf_none, NULL_TREE);
12643 if (arg == error_mark_node)
12647 TREE_VEC_ELT (targs, i) = arg;
12652 /* If the type parameter is a parameter pack, then it will
12653 be deduced to an empty parameter pack. */
12654 if (template_parameter_pack_p (tparm))
12658 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
12660 arg = make_node (NONTYPE_ARGUMENT_PACK);
12661 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
12662 TREE_CONSTANT (arg) = 1;
12665 arg = make_node (TYPE_ARGUMENT_PACK);
12667 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
12669 TREE_VEC_ELT (targs, i) = arg;
12679 /* Subroutine of type_unification_real. Args are like the variables
12680 at the call site. ARG is an overloaded function (or template-id);
12681 we try deducing template args from each of the overloads, and if
12682 only one succeeds, we go with that. Modifies TARGS and returns
12683 true on success. */
12686 resolve_overloaded_unification (tree tparms,
12690 unification_kind_t strict,
12693 tree tempargs = copy_node (targs);
12695 tree goodfn = NULL_TREE;
12698 if (TREE_CODE (arg) == ADDR_EXPR)
12700 arg = TREE_OPERAND (arg, 0);
12706 if (TREE_CODE (arg) == COMPONENT_REF)
12707 /* Handle `&x' where `x' is some static or non-static member
12709 arg = TREE_OPERAND (arg, 1);
12711 if (TREE_CODE (arg) == OFFSET_REF)
12712 arg = TREE_OPERAND (arg, 1);
12714 /* Strip baselink information. */
12715 if (BASELINK_P (arg))
12716 arg = BASELINK_FUNCTIONS (arg);
12718 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
12720 /* If we got some explicit template args, we need to plug them into
12721 the affected templates before we try to unify, in case the
12722 explicit args will completely resolve the templates in question. */
12724 tree expl_subargs = TREE_OPERAND (arg, 1);
12725 arg = TREE_OPERAND (arg, 0);
12727 for (; arg; arg = OVL_NEXT (arg))
12729 tree fn = OVL_CURRENT (arg);
12730 tree subargs, elem;
12732 if (TREE_CODE (fn) != TEMPLATE_DECL)
12735 ++processing_template_decl;
12736 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
12737 expl_subargs, /*check_ret=*/false);
12740 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
12741 if (try_one_overload (tparms, targs, tempargs, parm,
12742 elem, strict, sub_strict, addr_p)
12743 && (!goodfn || !decls_match (goodfn, elem)))
12749 --processing_template_decl;
12752 else if (TREE_CODE (arg) != OVERLOAD
12753 && TREE_CODE (arg) != FUNCTION_DECL)
12754 /* If ARG is, for example, "(0, &f)" then its type will be unknown
12755 -- but the deduction does not succeed because the expression is
12756 not just the function on its own. */
12759 for (; arg; arg = OVL_NEXT (arg))
12760 if (try_one_overload (tparms, targs, tempargs, parm,
12761 TREE_TYPE (OVL_CURRENT (arg)),
12762 strict, sub_strict, addr_p)
12763 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
12765 goodfn = OVL_CURRENT (arg);
12769 /* [temp.deduct.type] A template-argument can be deduced from a pointer
12770 to function or pointer to member function argument if the set of
12771 overloaded functions does not contain function templates and at most
12772 one of a set of overloaded functions provides a unique match.
12774 So if we found multiple possibilities, we return success but don't
12775 deduce anything. */
12779 int i = TREE_VEC_LENGTH (targs);
12781 if (TREE_VEC_ELT (tempargs, i))
12782 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
12790 /* Subroutine of resolve_overloaded_unification; does deduction for a single
12791 overload. Fills TARGS with any deduced arguments, or error_mark_node if
12792 different overloads deduce different arguments for a given parm.
12793 ADDR_P is true if the expression for which deduction is being
12794 performed was of the form "& fn" rather than simply "fn".
12796 Returns 1 on success. */
12799 try_one_overload (tree tparms,
12804 unification_kind_t strict,
12812 /* [temp.deduct.type] A template-argument can be deduced from a pointer
12813 to function or pointer to member function argument if the set of
12814 overloaded functions does not contain function templates and at most
12815 one of a set of overloaded functions provides a unique match.
12817 So if this is a template, just return success. */
12819 if (uses_template_parms (arg))
12822 if (TREE_CODE (arg) == METHOD_TYPE)
12823 arg = build_ptrmemfunc_type (build_pointer_type (arg));
12825 arg = build_pointer_type (arg);
12827 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
12829 /* We don't copy orig_targs for this because if we have already deduced
12830 some template args from previous args, unify would complain when we
12831 try to deduce a template parameter for the same argument, even though
12832 there isn't really a conflict. */
12833 nargs = TREE_VEC_LENGTH (targs);
12834 tempargs = make_tree_vec (nargs);
12836 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
12839 /* First make sure we didn't deduce anything that conflicts with
12840 explicitly specified args. */
12841 for (i = nargs; i--; )
12843 tree elt = TREE_VEC_ELT (tempargs, i);
12844 tree oldelt = TREE_VEC_ELT (orig_targs, i);
12848 else if (uses_template_parms (elt))
12849 /* Since we're unifying against ourselves, we will fill in
12850 template args used in the function parm list with our own
12851 template parms. Discard them. */
12852 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
12853 else if (oldelt && !template_args_equal (oldelt, elt))
12857 for (i = nargs; i--; )
12859 tree elt = TREE_VEC_ELT (tempargs, i);
12862 TREE_VEC_ELT (targs, i) = elt;
12868 /* PARM is a template class (perhaps with unbound template
12869 parameters). ARG is a fully instantiated type. If ARG can be
12870 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
12871 TARGS are as for unify. */
12874 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
12876 tree copy_of_targs;
12878 if (!CLASSTYPE_TEMPLATE_INFO (arg)
12879 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
12880 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
12883 /* We need to make a new template argument vector for the call to
12884 unify. If we used TARGS, we'd clutter it up with the result of
12885 the attempted unification, even if this class didn't work out.
12886 We also don't want to commit ourselves to all the unifications
12887 we've already done, since unification is supposed to be done on
12888 an argument-by-argument basis. In other words, consider the
12889 following pathological case:
12891 template <int I, int J, int K>
12894 template <int I, int J>
12895 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
12897 template <int I, int J, int K>
12898 void f(S<I, J, K>, S<I, I, I>);
12907 Now, by the time we consider the unification involving `s2', we
12908 already know that we must have `f<0, 0, 0>'. But, even though
12909 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
12910 because there are two ways to unify base classes of S<0, 1, 2>
12911 with S<I, I, I>. If we kept the already deduced knowledge, we
12912 would reject the possibility I=1. */
12913 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
12915 /* If unification failed, we're done. */
12916 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
12917 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
12923 /* Given a template type PARM and a class type ARG, find the unique
12924 base type in ARG that is an instance of PARM. We do not examine
12925 ARG itself; only its base-classes. If there is not exactly one
12926 appropriate base class, return NULL_TREE. PARM may be the type of
12927 a partial specialization, as well as a plain template type. Used
12931 get_template_base (tree tparms, tree targs, tree parm, tree arg)
12933 tree rval = NULL_TREE;
12936 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
12938 binfo = TYPE_BINFO (complete_type (arg));
12940 /* The type could not be completed. */
12943 /* Walk in inheritance graph order. The search order is not
12944 important, and this avoids multiple walks of virtual bases. */
12945 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
12947 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
12951 /* If there is more than one satisfactory baseclass, then:
12955 If they yield more than one possible deduced A, the type
12959 if (rval && !same_type_p (r, rval))
12969 /* Returns the level of DECL, which declares a template parameter. */
12972 template_decl_level (tree decl)
12974 switch (TREE_CODE (decl))
12977 case TEMPLATE_DECL:
12978 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
12981 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
12984 gcc_unreachable ();
12989 /* Decide whether ARG can be unified with PARM, considering only the
12990 cv-qualifiers of each type, given STRICT as documented for unify.
12991 Returns nonzero iff the unification is OK on that basis. */
12994 check_cv_quals_for_unify (int strict, tree arg, tree parm)
12996 int arg_quals = cp_type_quals (arg);
12997 int parm_quals = cp_type_quals (parm);
12999 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13000 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
13002 /* Although a CVR qualifier is ignored when being applied to a
13003 substituted template parameter ([8.3.2]/1 for example), that
13004 does not apply during deduction [14.8.2.4]/1, (even though
13005 that is not explicitly mentioned, [14.8.2.4]/9 indicates
13006 this). Except when we're allowing additional CV qualifiers
13007 at the outer level [14.8.2.1]/3,1st bullet. */
13008 if ((TREE_CODE (arg) == REFERENCE_TYPE
13009 || TREE_CODE (arg) == FUNCTION_TYPE
13010 || TREE_CODE (arg) == METHOD_TYPE)
13011 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
13014 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
13015 && (parm_quals & TYPE_QUAL_RESTRICT))
13019 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
13020 && (arg_quals & parm_quals) != parm_quals)
13023 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
13024 && (parm_quals & arg_quals) != arg_quals)
13030 /* Determines the LEVEL and INDEX for the template parameter PARM. */
13032 template_parm_level_and_index (tree parm, int* level, int* index)
13034 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13035 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13036 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13038 *index = TEMPLATE_TYPE_IDX (parm);
13039 *level = TEMPLATE_TYPE_LEVEL (parm);
13043 *index = TEMPLATE_PARM_IDX (parm);
13044 *level = TEMPLATE_PARM_LEVEL (parm);
13048 /* Unifies the remaining arguments in PACKED_ARGS with the pack
13049 expansion at the end of PACKED_PARMS. Returns 0 if the type
13050 deduction succeeds, 1 otherwise. STRICT is the same as in
13051 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
13052 call argument list. We'll need to adjust the arguments to make them
13053 types. SUBR tells us if this is from a recursive call to
13054 type_unification_real. */
13056 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
13057 tree packed_args, int strict, bool call_args_p,
13061 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
13062 tree pattern = PACK_EXPANSION_PATTERN (parm);
13063 tree pack, packs = NULL_TREE;
13064 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
13065 int len = TREE_VEC_LENGTH (packed_args);
13067 /* Determine the parameter packs we will be deducing from the
13068 pattern, and record their current deductions. */
13069 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
13070 pack; pack = TREE_CHAIN (pack))
13072 tree parm_pack = TREE_VALUE (pack);
13075 /* Determine the index and level of this parameter pack. */
13076 template_parm_level_and_index (parm_pack, &level, &idx);
13078 /* Keep track of the parameter packs and their corresponding
13080 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
13081 TREE_TYPE (packs) = make_tree_vec (len - start);
13084 /* Loop through all of the arguments that have not yet been
13085 unified and unify each with the pattern. */
13086 for (i = start; i < len; i++)
13088 tree parm = pattern;
13090 /* For each parameter pack, clear out the deduced value so that
13091 we can deduce it again. */
13092 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13095 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13097 TMPL_ARG (targs, level, idx) = NULL_TREE;
13100 /* Unify the pattern with the current argument. */
13102 tree arg = TREE_VEC_ELT (packed_args, i);
13103 tree arg_expr = NULL_TREE;
13104 int arg_strict = strict;
13105 bool skip_arg_p = false;
13111 /* This mirrors what we do in type_unification_real. */
13115 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
13116 | UNIFY_ALLOW_MORE_CV_QUAL
13117 | UNIFY_ALLOW_DERIVED);
13121 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13125 sub_strict = UNIFY_ALLOW_NONE;
13129 gcc_unreachable ();
13134 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
13135 if (type_unknown_p (arg))
13137 /* [temp.deduct.type] A template-argument can be
13138 deduced from a pointer to function or pointer
13139 to member function argument if the set of
13140 overloaded functions does not contain function
13141 templates and at most one of a set of
13142 overloaded functions provides a unique
13145 if (resolve_overloaded_unification
13146 (tparms, targs, parm, arg,
13147 (unification_kind_t) strict,
13157 arg = unlowered_expr_type (arg);
13158 if (arg == error_mark_node)
13163 arg_strict = sub_strict;
13167 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
13168 &parm, &arg, arg_expr);
13173 if (unify (tparms, targs, parm, arg, arg_strict))
13178 /* For each parameter pack, collect the deduced value. */
13179 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13182 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13184 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
13185 TMPL_ARG (targs, level, idx);
13189 /* Verify that the results of unification with the parameter packs
13190 produce results consistent with what we've seen before, and make
13191 the deduced argument packs available. */
13192 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13194 tree old_pack = TREE_VALUE (pack);
13195 tree new_args = TREE_TYPE (pack);
13196 int i, len = TREE_VEC_LENGTH (new_args);
13197 bool nondeduced_p = false;
13199 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
13200 actually deduce anything. */
13201 for (i = 0; i < len && !nondeduced_p; ++i)
13202 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
13203 nondeduced_p = true;
13207 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
13209 /* Prepend the explicit arguments onto NEW_ARGS. */
13210 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
13211 tree old_args = new_args;
13212 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
13213 int len = explicit_len + TREE_VEC_LENGTH (old_args);
13215 /* Copy the explicit arguments. */
13216 new_args = make_tree_vec (len);
13217 for (i = 0; i < explicit_len; i++)
13218 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
13220 /* Copy the deduced arguments. */
13221 for (; i < len; i++)
13222 TREE_VEC_ELT (new_args, i) =
13223 TREE_VEC_ELT (old_args, i - explicit_len);
13231 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13233 /* Build the deduced *_ARGUMENT_PACK. */
13234 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
13236 result = make_node (NONTYPE_ARGUMENT_PACK);
13237 TREE_TYPE (result) =
13238 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
13239 TREE_CONSTANT (result) = 1;
13242 result = make_node (TYPE_ARGUMENT_PACK);
13244 SET_ARGUMENT_PACK_ARGS (result, new_args);
13246 /* Note the deduced argument packs for this parameter
13248 TMPL_ARG (targs, level, idx) = result;
13250 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
13251 && (ARGUMENT_PACK_ARGS (old_pack)
13252 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
13254 /* We only had the explicitly-provided arguments before, but
13255 now we have a complete set of arguments. */
13257 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
13258 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13260 /* Keep the original deduced argument pack. */
13261 TMPL_ARG (targs, level, idx) = old_pack;
13263 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
13264 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
13265 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
13267 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
13269 /* Inconsistent unification of this parameter pack. */
13275 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13277 /* Keep the original deduced argument pack. */
13278 TMPL_ARG (targs, level, idx) = old_pack;
13285 /* Deduce the value of template parameters. TPARMS is the (innermost)
13286 set of template parameters to a template. TARGS is the bindings
13287 for those template parameters, as determined thus far; TARGS may
13288 include template arguments for outer levels of template parameters
13289 as well. PARM is a parameter to a template function, or a
13290 subcomponent of that parameter; ARG is the corresponding argument.
13291 This function attempts to match PARM with ARG in a manner
13292 consistent with the existing assignments in TARGS. If more values
13293 are deduced, then TARGS is updated.
13295 Returns 0 if the type deduction succeeds, 1 otherwise. The
13296 parameter STRICT is a bitwise or of the following flags:
13299 Require an exact match between PARM and ARG.
13300 UNIFY_ALLOW_MORE_CV_QUAL:
13301 Allow the deduced ARG to be more cv-qualified (by qualification
13302 conversion) than ARG.
13303 UNIFY_ALLOW_LESS_CV_QUAL:
13304 Allow the deduced ARG to be less cv-qualified than ARG.
13305 UNIFY_ALLOW_DERIVED:
13306 Allow the deduced ARG to be a template base class of ARG,
13307 or a pointer to a template base class of the type pointed to by
13309 UNIFY_ALLOW_INTEGER:
13310 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
13311 case for more information.
13312 UNIFY_ALLOW_OUTER_LEVEL:
13313 This is the outermost level of a deduction. Used to determine validity
13314 of qualification conversions. A valid qualification conversion must
13315 have const qualified pointers leading up to the inner type which
13316 requires additional CV quals, except at the outer level, where const
13317 is not required [conv.qual]. It would be normal to set this flag in
13318 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
13319 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
13320 This is the outermost level of a deduction, and PARM can be more CV
13321 qualified at this point.
13322 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
13323 This is the outermost level of a deduction, and PARM can be less CV
13324 qualified at this point. */
13327 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
13332 int strict_in = strict;
13334 /* I don't think this will do the right thing with respect to types.
13335 But the only case I've seen it in so far has been array bounds, where
13336 signedness is the only information lost, and I think that will be
13338 while (TREE_CODE (parm) == NOP_EXPR)
13339 parm = TREE_OPERAND (parm, 0);
13341 if (arg == error_mark_node)
13343 if (arg == unknown_type_node
13344 || arg == init_list_type_node)
13345 /* We can't deduce anything from this, but we might get all the
13346 template args from other function args. */
13349 /* If PARM uses template parameters, then we can't bail out here,
13350 even if ARG == PARM, since we won't record unifications for the
13351 template parameters. We might need them if we're trying to
13352 figure out which of two things is more specialized. */
13353 if (arg == parm && !uses_template_parms (parm))
13356 /* Handle init lists early, so the rest of the function can assume
13357 we're dealing with a type. */
13358 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
13363 if (!is_std_init_list (parm))
13364 /* We can only deduce from an initializer list argument if the
13365 parameter is std::initializer_list; otherwise this is a
13366 non-deduced context. */
13369 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
13371 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
13373 int elt_strict = strict;
13374 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
13376 tree type = TREE_TYPE (elt);
13377 /* It should only be possible to get here for a call. */
13378 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
13379 elt_strict |= maybe_adjust_types_for_deduction
13380 (DEDUCE_CALL, &elttype, &type, elt);
13384 if (unify (tparms, targs, elttype, elt, elt_strict))
13390 /* Immediately reject some pairs that won't unify because of
13391 cv-qualification mismatches. */
13392 if (TREE_CODE (arg) == TREE_CODE (parm)
13394 /* It is the elements of the array which hold the cv quals of an array
13395 type, and the elements might be template type parms. We'll check
13396 when we recurse. */
13397 && TREE_CODE (arg) != ARRAY_TYPE
13398 /* We check the cv-qualifiers when unifying with template type
13399 parameters below. We want to allow ARG `const T' to unify with
13400 PARM `T' for example, when computing which of two templates
13401 is more specialized, for example. */
13402 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
13403 && !check_cv_quals_for_unify (strict_in, arg, parm))
13406 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
13407 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
13408 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
13409 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
13410 strict &= ~UNIFY_ALLOW_DERIVED;
13411 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
13412 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
13414 switch (TREE_CODE (parm))
13416 case TYPENAME_TYPE:
13418 case UNBOUND_CLASS_TEMPLATE:
13419 /* In a type which contains a nested-name-specifier, template
13420 argument values cannot be deduced for template parameters used
13421 within the nested-name-specifier. */
13424 case TEMPLATE_TYPE_PARM:
13425 case TEMPLATE_TEMPLATE_PARM:
13426 case BOUND_TEMPLATE_TEMPLATE_PARM:
13427 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
13428 if (tparm == error_mark_node)
13431 if (TEMPLATE_TYPE_LEVEL (parm)
13432 != template_decl_level (tparm))
13433 /* The PARM is not one we're trying to unify. Just check
13434 to see if it matches ARG. */
13435 return (TREE_CODE (arg) == TREE_CODE (parm)
13436 && same_type_p (parm, arg)) ? 0 : 1;
13437 idx = TEMPLATE_TYPE_IDX (parm);
13438 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
13439 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
13441 /* Check for mixed types and values. */
13442 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13443 && TREE_CODE (tparm) != TYPE_DECL)
13444 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13445 && TREE_CODE (tparm) != TEMPLATE_DECL))
13448 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13450 /* ARG must be constructed from a template class or a template
13451 template parameter. */
13452 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
13453 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
13457 tree parmvec = TYPE_TI_ARGS (parm);
13458 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
13460 = DECL_INNERMOST_TEMPLATE_PARMS
13461 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
13463 int parm_variadic_p = 0;
13465 /* The resolution to DR150 makes clear that default
13466 arguments for an N-argument may not be used to bind T
13467 to a template template parameter with fewer than N
13468 parameters. It is not safe to permit the binding of
13469 default arguments as an extension, as that may change
13470 the meaning of a conforming program. Consider:
13472 struct Dense { static const unsigned int dim = 1; };
13474 template <template <typename> class View,
13476 void operator+(float, View<Block> const&);
13478 template <typename Block,
13479 unsigned int Dim = Block::dim>
13480 struct Lvalue_proxy { operator float() const; };
13484 Lvalue_proxy<Dense> p;
13489 Here, if Lvalue_proxy is permitted to bind to View, then
13490 the global operator+ will be used; if they are not, the
13491 Lvalue_proxy will be converted to float. */
13492 if (coerce_template_parms (parm_parms,
13494 TYPE_TI_TEMPLATE (parm),
13496 /*require_all_args=*/true,
13497 /*use_default_args=*/false)
13498 == error_mark_node)
13501 /* Deduce arguments T, i from TT<T> or TT<i>.
13502 We check each element of PARMVEC and ARGVEC individually
13503 rather than the whole TREE_VEC since they can have
13504 different number of elements. */
13506 parmvec = expand_template_argument_pack (parmvec);
13507 argvec = expand_template_argument_pack (argvec);
13509 len = TREE_VEC_LENGTH (parmvec);
13511 /* Check if the parameters end in a pack, making them
13514 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
13515 parm_variadic_p = 1;
13517 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
13520 for (i = 0; i < len - parm_variadic_p; ++i)
13522 if (unify (tparms, targs,
13523 TREE_VEC_ELT (parmvec, i),
13524 TREE_VEC_ELT (argvec, i),
13529 if (parm_variadic_p
13530 && unify_pack_expansion (tparms, targs,
13533 /*call_args_p=*/false,
13537 arg = TYPE_TI_TEMPLATE (arg);
13539 /* Fall through to deduce template name. */
13542 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13543 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13545 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
13547 /* Simple cases: Value already set, does match or doesn't. */
13548 if (targ != NULL_TREE && template_args_equal (targ, arg))
13555 /* If PARM is `const T' and ARG is only `int', we don't have
13556 a match unless we are allowing additional qualification.
13557 If ARG is `const int' and PARM is just `T' that's OK;
13558 that binds `const int' to `T'. */
13559 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
13563 /* Consider the case where ARG is `const volatile int' and
13564 PARM is `const T'. Then, T should be `volatile int'. */
13565 arg = cp_build_qualified_type_real
13566 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
13567 if (arg == error_mark_node)
13570 /* Simple cases: Value already set, does match or doesn't. */
13571 if (targ != NULL_TREE && same_type_p (targ, arg))
13576 /* Make sure that ARG is not a variable-sized array. (Note
13577 that were talking about variable-sized arrays (like
13578 `int[n]'), rather than arrays of unknown size (like
13579 `int[]').) We'll get very confused by such a type since
13580 the bound of the array will not be computable in an
13581 instantiation. Besides, such types are not allowed in
13582 ISO C++, so we can do as we please here. */
13583 if (variably_modified_type_p (arg, NULL_TREE))
13586 /* Strip typedefs as in convert_template_argument. */
13587 arg = canonical_type_variant (arg);
13590 /* If ARG is a parameter pack or an expansion, we cannot unify
13591 against it unless PARM is also a parameter pack. */
13592 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
13593 && !template_parameter_pack_p (parm))
13596 /* If the argument deduction results is a METHOD_TYPE,
13597 then there is a problem.
13598 METHOD_TYPE doesn't map to any real C++ type the result of
13599 the deduction can not be of that type. */
13600 if (TREE_CODE (arg) == METHOD_TYPE)
13603 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
13606 case TEMPLATE_PARM_INDEX:
13607 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
13608 if (tparm == error_mark_node)
13611 if (TEMPLATE_PARM_LEVEL (parm)
13612 != template_decl_level (tparm))
13613 /* The PARM is not one we're trying to unify. Just check
13614 to see if it matches ARG. */
13615 return !(TREE_CODE (arg) == TREE_CODE (parm)
13616 && cp_tree_equal (parm, arg));
13618 idx = TEMPLATE_PARM_IDX (parm);
13619 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
13622 return !cp_tree_equal (targ, arg);
13624 /* [temp.deduct.type] If, in the declaration of a function template
13625 with a non-type template-parameter, the non-type
13626 template-parameter is used in an expression in the function
13627 parameter-list and, if the corresponding template-argument is
13628 deduced, the template-argument type shall match the type of the
13629 template-parameter exactly, except that a template-argument
13630 deduced from an array bound may be of any integral type.
13631 The non-type parameter might use already deduced type parameters. */
13632 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
13633 if (!TREE_TYPE (arg))
13634 /* Template-parameter dependent expression. Just accept it for now.
13635 It will later be processed in convert_template_argument. */
13637 else if (same_type_p (TREE_TYPE (arg), tparm))
13639 else if ((strict & UNIFY_ALLOW_INTEGER)
13640 && (TREE_CODE (tparm) == INTEGER_TYPE
13641 || TREE_CODE (tparm) == BOOLEAN_TYPE))
13642 /* Convert the ARG to the type of PARM; the deduced non-type
13643 template argument must exactly match the types of the
13644 corresponding parameter. */
13645 arg = fold (build_nop (tparm, arg));
13646 else if (uses_template_parms (tparm))
13647 /* We haven't deduced the type of this parameter yet. Try again
13653 /* If ARG is a parameter pack or an expansion, we cannot unify
13654 against it unless PARM is also a parameter pack. */
13655 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
13656 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
13659 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
13664 /* A pointer-to-member constant can be unified only with
13665 another constant. */
13666 if (TREE_CODE (arg) != PTRMEM_CST)
13669 /* Just unify the class member. It would be useless (and possibly
13670 wrong, depending on the strict flags) to unify also
13671 PTRMEM_CST_CLASS, because we want to be sure that both parm and
13672 arg refer to the same variable, even if through different
13673 classes. For instance:
13675 struct A { int x; };
13678 Unification of &A::x and &B::x must succeed. */
13679 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
13680 PTRMEM_CST_MEMBER (arg), strict);
13685 if (TREE_CODE (arg) != POINTER_TYPE)
13688 /* [temp.deduct.call]
13690 A can be another pointer or pointer to member type that can
13691 be converted to the deduced A via a qualification
13692 conversion (_conv.qual_).
13694 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
13695 This will allow for additional cv-qualification of the
13696 pointed-to types if appropriate. */
13698 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
13699 /* The derived-to-base conversion only persists through one
13700 level of pointers. */
13701 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
13703 return unify (tparms, targs, TREE_TYPE (parm),
13704 TREE_TYPE (arg), strict);
13707 case REFERENCE_TYPE:
13708 if (TREE_CODE (arg) != REFERENCE_TYPE)
13710 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
13711 strict & UNIFY_ALLOW_MORE_CV_QUAL);
13714 if (TREE_CODE (arg) != ARRAY_TYPE)
13716 if ((TYPE_DOMAIN (parm) == NULL_TREE)
13717 != (TYPE_DOMAIN (arg) == NULL_TREE))
13719 if (TYPE_DOMAIN (parm) != NULL_TREE)
13726 /* Our representation of array types uses "N - 1" as the
13727 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
13728 not an integer constant. We cannot unify arbitrarily
13729 complex expressions, so we eliminate the MINUS_EXPRs
13731 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
13732 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
13735 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
13736 parm_max = TREE_OPERAND (parm_max, 0);
13738 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
13739 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
13742 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
13743 trying to unify the type of a variable with the type
13744 of a template parameter. For example:
13746 template <unsigned int N>
13747 void f (char (&) [N]);
13754 Here, the type of the ARG will be "int [g(i)]", and
13755 may be a SAVE_EXPR, etc. */
13756 if (TREE_CODE (arg_max) != MINUS_EXPR)
13758 arg_max = TREE_OPERAND (arg_max, 0);
13761 /* If only one of the bounds used a MINUS_EXPR, compensate
13762 by adding one to the other bound. */
13763 if (parm_cst && !arg_cst)
13764 parm_max = fold_build2 (PLUS_EXPR,
13768 else if (arg_cst && !parm_cst)
13769 arg_max = fold_build2 (PLUS_EXPR,
13774 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
13777 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
13778 strict & UNIFY_ALLOW_MORE_CV_QUAL);
13785 case ENUMERAL_TYPE:
13787 if (TREE_CODE (arg) != TREE_CODE (parm))
13790 /* We have already checked cv-qualification at the top of the
13792 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
13795 /* As far as unification is concerned, this wins. Later checks
13796 will invalidate it if necessary. */
13799 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
13800 /* Type INTEGER_CST can come from ordinary constant template args. */
13802 while (TREE_CODE (arg) == NOP_EXPR)
13803 arg = TREE_OPERAND (arg, 0);
13805 if (TREE_CODE (arg) != INTEGER_CST)
13807 return !tree_int_cst_equal (parm, arg);
13812 if (TREE_CODE (arg) != TREE_VEC)
13814 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
13816 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
13817 if (unify (tparms, targs,
13818 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
13826 if (TREE_CODE (arg) != TREE_CODE (parm))
13829 if (TYPE_PTRMEMFUNC_P (parm))
13831 if (!TYPE_PTRMEMFUNC_P (arg))
13834 return unify (tparms, targs,
13835 TYPE_PTRMEMFUNC_FN_TYPE (parm),
13836 TYPE_PTRMEMFUNC_FN_TYPE (arg),
13840 if (CLASSTYPE_TEMPLATE_INFO (parm))
13842 tree t = NULL_TREE;
13844 if (strict_in & UNIFY_ALLOW_DERIVED)
13846 /* First, we try to unify the PARM and ARG directly. */
13847 t = try_class_unification (tparms, targs,
13852 /* Fallback to the special case allowed in
13853 [temp.deduct.call]:
13855 If P is a class, and P has the form
13856 template-id, then A can be a derived class of
13857 the deduced A. Likewise, if P is a pointer to
13858 a class of the form template-id, A can be a
13859 pointer to a derived class pointed to by the
13861 t = get_template_base (tparms, targs, parm, arg);
13867 else if (CLASSTYPE_TEMPLATE_INFO (arg)
13868 && (CLASSTYPE_TI_TEMPLATE (parm)
13869 == CLASSTYPE_TI_TEMPLATE (arg)))
13870 /* Perhaps PARM is something like S<U> and ARG is S<int>.
13871 Then, we should unify `int' and `U'. */
13874 /* There's no chance of unification succeeding. */
13877 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
13878 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
13880 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
13885 case FUNCTION_TYPE:
13886 if (TREE_CODE (arg) != TREE_CODE (parm))
13889 /* CV qualifications for methods can never be deduced, they must
13890 match exactly. We need to check them explicitly here,
13891 because type_unification_real treats them as any other
13892 cv-qualified parameter. */
13893 if (TREE_CODE (parm) == METHOD_TYPE
13894 && (!check_cv_quals_for_unify
13896 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
13897 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
13900 if (unify (tparms, targs, TREE_TYPE (parm),
13901 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
13903 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
13904 TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT,
13908 /* Unify a pointer to member with a pointer to member function, which
13909 deduces the type of the member as a function type. */
13910 if (TYPE_PTRMEMFUNC_P (arg))
13914 cp_cv_quals cv_quals;
13916 /* Check top-level cv qualifiers */
13917 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
13920 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
13921 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
13924 /* Determine the type of the function we are unifying against. */
13925 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
13927 build_function_type (TREE_TYPE (method_type),
13928 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
13930 /* Extract the cv-qualifiers of the member function from the
13931 implicit object parameter and place them on the function
13932 type to be restored later. */
13934 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
13935 fntype = build_qualified_type (fntype, cv_quals);
13936 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
13939 if (TREE_CODE (arg) != OFFSET_TYPE)
13941 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
13942 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
13944 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
13948 if (DECL_TEMPLATE_PARM_P (parm))
13949 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
13950 if (arg != integral_constant_value (parm))
13955 case TEMPLATE_DECL:
13956 /* Matched cases are handled by the ARG == PARM test above. */
13959 case TYPE_ARGUMENT_PACK:
13960 case NONTYPE_ARGUMENT_PACK:
13962 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
13963 tree packed_args = ARGUMENT_PACK_ARGS (arg);
13964 int i, len = TREE_VEC_LENGTH (packed_parms);
13965 int argslen = TREE_VEC_LENGTH (packed_args);
13966 int parm_variadic_p = 0;
13968 for (i = 0; i < len; ++i)
13970 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
13973 /* We can unify against something with a trailing
13975 parm_variadic_p = 1;
13977 /* Since there is something following the pack
13978 expansion, we cannot unify this template argument
13985 /* If we don't have enough arguments to satisfy the parameters
13986 (not counting the pack expression at the end), or we have
13987 too many arguments for a parameter list that doesn't end in
13988 a pack expression, we can't unify. */
13989 if (argslen < (len - parm_variadic_p)
13990 || (argslen > len && !parm_variadic_p))
13993 /* Unify all of the parameters that precede the (optional)
13994 pack expression. */
13995 for (i = 0; i < len - parm_variadic_p; ++i)
13997 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
13998 TREE_VEC_ELT (packed_args, i), strict))
14002 if (parm_variadic_p)
14003 return unify_pack_expansion (tparms, targs,
14004 packed_parms, packed_args,
14005 strict, /*call_args_p=*/false,
14013 case DECLTYPE_TYPE:
14014 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
14019 /* Unification fails if we hit an error node. */
14023 gcc_assert (EXPR_P (parm));
14025 /* We must be looking at an expression. This can happen with
14029 void foo(S<I>, S<I + 2>);
14031 This is a "nondeduced context":
14035 The nondeduced contexts are:
14037 --A type that is a template-id in which one or more of
14038 the template-arguments is an expression that references
14039 a template-parameter.
14041 In these cases, we assume deduction succeeded, but don't
14042 actually infer any unifications. */
14044 if (!uses_template_parms (parm)
14045 && !template_args_equal (parm, arg))
14052 /* Note that DECL can be defined in this translation unit, if
14056 mark_definable (tree decl)
14059 DECL_NOT_REALLY_EXTERN (decl) = 1;
14060 FOR_EACH_CLONE (clone, decl)
14061 DECL_NOT_REALLY_EXTERN (clone) = 1;
14064 /* Called if RESULT is explicitly instantiated, or is a member of an
14065 explicitly instantiated class. */
14068 mark_decl_instantiated (tree result, int extern_p)
14070 SET_DECL_EXPLICIT_INSTANTIATION (result);
14072 /* If this entity has already been written out, it's too late to
14073 make any modifications. */
14074 if (TREE_ASM_WRITTEN (result))
14077 if (TREE_CODE (result) != FUNCTION_DECL)
14078 /* The TREE_PUBLIC flag for function declarations will have been
14079 set correctly by tsubst. */
14080 TREE_PUBLIC (result) = 1;
14082 /* This might have been set by an earlier implicit instantiation. */
14083 DECL_COMDAT (result) = 0;
14086 DECL_NOT_REALLY_EXTERN (result) = 0;
14089 mark_definable (result);
14090 /* Always make artificials weak. */
14091 if (DECL_ARTIFICIAL (result) && flag_weak)
14092 comdat_linkage (result);
14093 /* For WIN32 we also want to put explicit instantiations in
14094 linkonce sections. */
14095 else if (TREE_PUBLIC (result))
14096 maybe_make_one_only (result);
14099 /* If EXTERN_P, then this function will not be emitted -- unless
14100 followed by an explicit instantiation, at which point its linkage
14101 will be adjusted. If !EXTERN_P, then this function will be
14102 emitted here. In neither circumstance do we want
14103 import_export_decl to adjust the linkage. */
14104 DECL_INTERFACE_KNOWN (result) = 1;
14107 /* Given two function templates PAT1 and PAT2, return:
14109 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
14110 -1 if PAT2 is more specialized than PAT1.
14111 0 if neither is more specialized.
14113 LEN indicates the number of parameters we should consider
14114 (defaulted parameters should not be considered).
14116 The 1998 std underspecified function template partial ordering, and
14117 DR214 addresses the issue. We take pairs of arguments, one from
14118 each of the templates, and deduce them against each other. One of
14119 the templates will be more specialized if all the *other*
14120 template's arguments deduce against its arguments and at least one
14121 of its arguments *does* *not* deduce against the other template's
14122 corresponding argument. Deduction is done as for class templates.
14123 The arguments used in deduction have reference and top level cv
14124 qualifiers removed. Iff both arguments were originally reference
14125 types *and* deduction succeeds in both directions, the template
14126 with the more cv-qualified argument wins for that pairing (if
14127 neither is more cv-qualified, they both are equal). Unlike regular
14128 deduction, after all the arguments have been deduced in this way,
14129 we do *not* verify the deduced template argument values can be
14130 substituted into non-deduced contexts, nor do we have to verify
14131 that all template arguments have been deduced. */
14134 more_specialized_fn (tree pat1, tree pat2, int len)
14136 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
14137 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
14138 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
14139 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
14140 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
14141 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
14142 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
14143 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
14147 /* Remove the this parameter from non-static member functions. If
14148 one is a non-static member function and the other is not a static
14149 member function, remove the first parameter from that function
14150 also. This situation occurs for operator functions where we
14151 locate both a member function (with this pointer) and non-member
14152 operator (with explicit first operand). */
14153 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
14155 len--; /* LEN is the number of significant arguments for DECL1 */
14156 args1 = TREE_CHAIN (args1);
14157 if (!DECL_STATIC_FUNCTION_P (decl2))
14158 args2 = TREE_CHAIN (args2);
14160 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
14162 args2 = TREE_CHAIN (args2);
14163 if (!DECL_STATIC_FUNCTION_P (decl1))
14166 args1 = TREE_CHAIN (args1);
14170 /* If only one is a conversion operator, they are unordered. */
14171 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
14174 /* Consider the return type for a conversion function */
14175 if (DECL_CONV_FN_P (decl1))
14177 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
14178 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
14182 processing_template_decl++;
14185 /* Stop when an ellipsis is seen. */
14186 && args1 != NULL_TREE && args2 != NULL_TREE)
14188 tree arg1 = TREE_VALUE (args1);
14189 tree arg2 = TREE_VALUE (args2);
14190 int deduce1, deduce2;
14194 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
14195 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14197 /* When both arguments are pack expansions, we need only
14198 unify the patterns themselves. */
14199 arg1 = PACK_EXPANSION_PATTERN (arg1);
14200 arg2 = PACK_EXPANSION_PATTERN (arg2);
14202 /* This is the last comparison we need to do. */
14206 if (TREE_CODE (arg1) == REFERENCE_TYPE)
14208 arg1 = TREE_TYPE (arg1);
14209 quals1 = cp_type_quals (arg1);
14212 if (TREE_CODE (arg2) == REFERENCE_TYPE)
14214 arg2 = TREE_TYPE (arg2);
14215 quals2 = cp_type_quals (arg2);
14218 if ((quals1 < 0) != (quals2 < 0))
14220 /* Only of the args is a reference, see if we should apply
14221 array/function pointer decay to it. This is not part of
14222 DR214, but is, IMHO, consistent with the deduction rules
14223 for the function call itself, and with our earlier
14224 implementation of the underspecified partial ordering
14225 rules. (nathan). */
14228 switch (TREE_CODE (arg1))
14231 arg1 = TREE_TYPE (arg1);
14233 case FUNCTION_TYPE:
14234 arg1 = build_pointer_type (arg1);
14243 switch (TREE_CODE (arg2))
14246 arg2 = TREE_TYPE (arg2);
14248 case FUNCTION_TYPE:
14249 arg2 = build_pointer_type (arg2);
14258 arg1 = TYPE_MAIN_VARIANT (arg1);
14259 arg2 = TYPE_MAIN_VARIANT (arg2);
14261 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
14263 int i, len2 = list_length (args2);
14264 tree parmvec = make_tree_vec (1);
14265 tree argvec = make_tree_vec (len2);
14268 /* Setup the parameter vector, which contains only ARG1. */
14269 TREE_VEC_ELT (parmvec, 0) = arg1;
14271 /* Setup the argument vector, which contains the remaining
14273 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
14274 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
14276 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
14277 argvec, UNIFY_ALLOW_NONE,
14278 /*call_args_p=*/false,
14281 /* We cannot deduce in the other direction, because ARG1 is
14282 a pack expansion but ARG2 is not. */
14285 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14287 int i, len1 = list_length (args1);
14288 tree parmvec = make_tree_vec (1);
14289 tree argvec = make_tree_vec (len1);
14292 /* Setup the parameter vector, which contains only ARG1. */
14293 TREE_VEC_ELT (parmvec, 0) = arg2;
14295 /* Setup the argument vector, which contains the remaining
14297 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
14298 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
14300 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
14301 argvec, UNIFY_ALLOW_NONE,
14302 /*call_args_p=*/false,
14305 /* We cannot deduce in the other direction, because ARG2 is
14306 a pack expansion but ARG1 is not.*/
14312 /* The normal case, where neither argument is a pack
14314 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
14315 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
14322 if (better1 < 0 && better2 < 0)
14323 /* We've failed to deduce something in either direction.
14324 These must be unordered. */
14327 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
14329 /* Deduces in both directions, see if quals can
14330 disambiguate. Pretend the worse one failed to deduce. */
14331 if ((quals1 & quals2) == quals2)
14333 if ((quals1 & quals2) == quals1)
14336 if (deduce1 && !deduce2 && !better2)
14338 if (deduce2 && !deduce1 && !better1)
14341 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
14342 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14343 /* We have already processed all of the arguments in our
14344 handing of the pack expansion type. */
14347 args1 = TREE_CHAIN (args1);
14348 args2 = TREE_CHAIN (args2);
14351 processing_template_decl--;
14353 /* All things being equal, if the next argument is a pack expansion
14354 for one function but not for the other, prefer the
14355 non-variadic function. */
14356 if ((better1 > 0) - (better2 > 0) == 0
14357 && args1 && TREE_VALUE (args1)
14358 && args2 && TREE_VALUE (args2))
14360 if (TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION)
14361 return TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION ? 0 : -1;
14362 else if (TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION)
14366 return (better1 > 0) - (better2 > 0);
14369 /* Determine which of two partial specializations is more specialized.
14371 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
14372 to the first partial specialization. The TREE_VALUE is the
14373 innermost set of template parameters for the partial
14374 specialization. PAT2 is similar, but for the second template.
14376 Return 1 if the first partial specialization is more specialized;
14377 -1 if the second is more specialized; 0 if neither is more
14380 See [temp.class.order] for information about determining which of
14381 two templates is more specialized. */
14384 more_specialized_class (tree pat1, tree pat2)
14389 bool any_deductions = false;
14391 tmpl1 = TREE_TYPE (pat1);
14392 tmpl2 = TREE_TYPE (pat2);
14394 /* Just like what happens for functions, if we are ordering between
14395 different class template specializations, we may encounter dependent
14396 types in the arguments, and we need our dependency check functions
14397 to behave correctly. */
14398 ++processing_template_decl;
14399 targs = get_class_bindings (TREE_VALUE (pat1),
14400 CLASSTYPE_TI_ARGS (tmpl1),
14401 CLASSTYPE_TI_ARGS (tmpl2));
14405 any_deductions = true;
14408 targs = get_class_bindings (TREE_VALUE (pat2),
14409 CLASSTYPE_TI_ARGS (tmpl2),
14410 CLASSTYPE_TI_ARGS (tmpl1));
14414 any_deductions = true;
14416 --processing_template_decl;
14418 /* In the case of a tie where at least one of the class templates
14419 has a parameter pack at the end, the template with the most
14420 non-packed parameters wins. */
14423 && (template_args_variadic_p (TREE_PURPOSE (pat1))
14424 || template_args_variadic_p (TREE_PURPOSE (pat2))))
14426 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
14427 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
14428 int len1 = TREE_VEC_LENGTH (args1);
14429 int len2 = TREE_VEC_LENGTH (args2);
14431 /* We don't count the pack expansion at the end. */
14432 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
14434 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
14439 else if (len1 < len2)
14446 /* Return the template arguments that will produce the function signature
14447 DECL from the function template FN, with the explicit template
14448 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
14449 also match. Return NULL_TREE if no satisfactory arguments could be
14453 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
14455 int ntparms = DECL_NTPARMS (fn);
14456 tree targs = make_tree_vec (ntparms);
14458 tree decl_arg_types;
14460 /* Substitute the explicit template arguments into the type of DECL.
14461 The call to fn_type_unification will handle substitution into the
14463 decl_type = TREE_TYPE (decl);
14464 if (explicit_args && uses_template_parms (decl_type))
14467 tree converted_args;
14469 if (DECL_TEMPLATE_INFO (decl))
14470 tmpl = DECL_TI_TEMPLATE (decl);
14472 /* We can get here for some invalid specializations. */
14476 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
14477 explicit_args, NULL_TREE,
14479 /*require_all_args=*/false,
14480 /*use_default_args=*/false);
14481 if (converted_args == error_mark_node)
14484 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
14485 if (decl_type == error_mark_node)
14489 /* Never do unification on the 'this' parameter. */
14490 decl_arg_types = skip_artificial_parms_for (decl,
14491 TYPE_ARG_TYPES (decl_type));
14493 if (fn_type_unification (fn, explicit_args, targs,
14495 (check_rettype || DECL_CONV_FN_P (fn)
14496 ? TREE_TYPE (decl_type) : NULL_TREE),
14497 DEDUCE_EXACT, LOOKUP_NORMAL))
14503 /* Return the innermost template arguments that, when applied to a
14504 template specialization whose innermost template parameters are
14505 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
14508 For example, suppose we have:
14510 template <class T, class U> struct S {};
14511 template <class T> struct S<T*, int> {};
14513 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
14514 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
14515 int}. The resulting vector will be {double}, indicating that `T'
14516 is bound to `double'. */
14519 get_class_bindings (tree tparms, tree spec_args, tree args)
14521 int i, ntparms = TREE_VEC_LENGTH (tparms);
14523 tree innermost_deduced_args;
14525 innermost_deduced_args = make_tree_vec (ntparms);
14526 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
14528 deduced_args = copy_node (args);
14529 SET_TMPL_ARGS_LEVEL (deduced_args,
14530 TMPL_ARGS_DEPTH (deduced_args),
14531 innermost_deduced_args);
14534 deduced_args = innermost_deduced_args;
14536 if (unify (tparms, deduced_args,
14537 INNERMOST_TEMPLATE_ARGS (spec_args),
14538 INNERMOST_TEMPLATE_ARGS (args),
14542 for (i = 0; i < ntparms; ++i)
14543 if (! TREE_VEC_ELT (innermost_deduced_args, i))
14546 /* Verify that nondeduced template arguments agree with the type
14547 obtained from argument deduction.
14551 struct A { typedef int X; };
14552 template <class T, class U> struct C {};
14553 template <class T> struct C<T, typename T::X> {};
14555 Then with the instantiation `C<A, int>', we can deduce that
14556 `T' is `A' but unify () does not check whether `typename T::X'
14558 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
14559 if (spec_args == error_mark_node
14560 /* We only need to check the innermost arguments; the other
14561 arguments will always agree. */
14562 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
14563 INNERMOST_TEMPLATE_ARGS (args)))
14566 /* Now that we have bindings for all of the template arguments,
14567 ensure that the arguments deduced for the template template
14568 parameters have compatible template parameter lists. See the use
14569 of template_template_parm_bindings_ok_p in fn_type_unification
14570 for more information. */
14571 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
14574 return deduced_args;
14577 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
14578 Return the TREE_LIST node with the most specialized template, if
14579 any. If there is no most specialized template, the error_mark_node
14582 Note that this function does not look at, or modify, the
14583 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
14584 returned is one of the elements of INSTANTIATIONS, callers may
14585 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
14586 and retrieve it from the value returned. */
14589 most_specialized_instantiation (tree templates)
14593 ++processing_template_decl;
14596 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
14600 if (get_bindings (TREE_VALUE (champ),
14601 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
14602 NULL_TREE, /*check_ret=*/false))
14605 if (get_bindings (TREE_VALUE (fn),
14606 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
14607 NULL_TREE, /*check_ret=*/false))
14614 /* Equally specialized, move to next function. If there
14615 is no next function, nothing's most specialized. */
14616 fn = TREE_CHAIN (fn);
14624 /* Now verify that champ is better than everything earlier in the
14625 instantiation list. */
14626 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
14627 if (get_bindings (TREE_VALUE (champ),
14628 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
14629 NULL_TREE, /*check_ret=*/false)
14630 || !get_bindings (TREE_VALUE (fn),
14631 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
14632 NULL_TREE, /*check_ret=*/false))
14638 processing_template_decl--;
14641 return error_mark_node;
14646 /* If DECL is a specialization of some template, return the most
14647 general such template. Otherwise, returns NULL_TREE.
14649 For example, given:
14651 template <class T> struct S { template <class U> void f(U); };
14653 if TMPL is `template <class U> void S<int>::f(U)' this will return
14654 the full template. This function will not trace past partial
14655 specializations, however. For example, given in addition:
14657 template <class T> struct S<T*> { template <class U> void f(U); };
14659 if TMPL is `template <class U> void S<int*>::f(U)' this will return
14660 `template <class T> template <class U> S<T*>::f(U)'. */
14663 most_general_template (tree decl)
14665 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
14666 an immediate specialization. */
14667 if (TREE_CODE (decl) == FUNCTION_DECL)
14669 if (DECL_TEMPLATE_INFO (decl)) {
14670 decl = DECL_TI_TEMPLATE (decl);
14672 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
14673 template friend. */
14674 if (TREE_CODE (decl) != TEMPLATE_DECL)
14680 /* Look for more and more general templates. */
14681 while (DECL_TEMPLATE_INFO (decl))
14683 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
14684 (See cp-tree.h for details.) */
14685 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
14688 if (CLASS_TYPE_P (TREE_TYPE (decl))
14689 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
14692 /* Stop if we run into an explicitly specialized class template. */
14693 if (!DECL_NAMESPACE_SCOPE_P (decl)
14694 && DECL_CONTEXT (decl)
14695 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
14698 decl = DECL_TI_TEMPLATE (decl);
14704 /* Return the most specialized of the class template partial
14705 specializations of TMPL which can produce TYPE, a specialization of
14706 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
14707 a _TYPE node corresponding to the partial specialization, while the
14708 TREE_PURPOSE is the set of template arguments that must be
14709 substituted into the TREE_TYPE in order to generate TYPE.
14711 If the choice of partial specialization is ambiguous, a diagnostic
14712 is issued, and the error_mark_node is returned. If there are no
14713 partial specializations of TMPL matching TYPE, then NULL_TREE is
14717 most_specialized_class (tree type, tree tmpl)
14719 tree list = NULL_TREE;
14725 tree outer_args = NULL_TREE;
14727 tmpl = most_general_template (tmpl);
14728 args = CLASSTYPE_TI_ARGS (type);
14730 /* For determining which partial specialization to use, only the
14731 innermost args are interesting. */
14732 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
14734 outer_args = strip_innermost_template_args (args, 1);
14735 args = INNERMOST_TEMPLATE_ARGS (args);
14738 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
14740 tree partial_spec_args;
14742 tree parms = TREE_VALUE (t);
14744 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
14749 ++processing_template_decl;
14751 /* Discard the outer levels of args, and then substitute in the
14752 template args from the enclosing class. */
14753 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
14754 partial_spec_args = tsubst_template_args
14755 (partial_spec_args, outer_args, tf_none, NULL_TREE);
14757 /* PARMS already refers to just the innermost parms, but the
14758 template parms in partial_spec_args had their levels lowered
14759 by tsubst, so we need to do the same for the parm list. We
14760 can't just tsubst the TREE_VEC itself, as tsubst wants to
14761 treat a TREE_VEC as an argument vector. */
14762 parms = copy_node (parms);
14763 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
14764 TREE_VEC_ELT (parms, i) =
14765 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
14767 --processing_template_decl;
14769 spec_args = get_class_bindings (parms,
14775 spec_args = add_to_template_args (outer_args, spec_args);
14776 list = tree_cons (spec_args, TREE_VALUE (t), list);
14777 TREE_TYPE (list) = TREE_TYPE (t);
14784 ambiguous_p = false;
14787 t = TREE_CHAIN (t);
14788 for (; t; t = TREE_CHAIN (t))
14790 fate = more_specialized_class (champ, t);
14797 t = TREE_CHAIN (t);
14800 ambiguous_p = true;
14809 for (t = list; t && t != champ; t = TREE_CHAIN (t))
14811 fate = more_specialized_class (champ, t);
14814 ambiguous_p = true;
14821 const char *str = "candidates are:";
14822 error ("ambiguous class template instantiation for %q#T", type);
14823 for (t = list; t; t = TREE_CHAIN (t))
14825 error ("%s %+#T", str, TREE_TYPE (t));
14828 return error_mark_node;
14834 /* Explicitly instantiate DECL. */
14837 do_decl_instantiation (tree decl, tree storage)
14839 tree result = NULL_TREE;
14842 if (!decl || decl == error_mark_node)
14843 /* An error occurred, for which grokdeclarator has already issued
14844 an appropriate message. */
14846 else if (! DECL_LANG_SPECIFIC (decl))
14848 error ("explicit instantiation of non-template %q#D", decl);
14851 else if (TREE_CODE (decl) == VAR_DECL)
14853 /* There is an asymmetry here in the way VAR_DECLs and
14854 FUNCTION_DECLs are handled by grokdeclarator. In the case of
14855 the latter, the DECL we get back will be marked as a
14856 template instantiation, and the appropriate
14857 DECL_TEMPLATE_INFO will be set up. This does not happen for
14858 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
14859 should handle VAR_DECLs as it currently handles
14861 if (!DECL_CLASS_SCOPE_P (decl))
14863 error ("%qD is not a static data member of a class template", decl);
14866 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
14867 if (!result || TREE_CODE (result) != VAR_DECL)
14869 error ("no matching template for %qD found", decl);
14872 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
14874 error ("type %qT for explicit instantiation %qD does not match "
14875 "declared type %qT", TREE_TYPE (result), decl,
14880 else if (TREE_CODE (decl) != FUNCTION_DECL)
14882 error ("explicit instantiation of %q#D", decl);
14888 /* Check for various error cases. Note that if the explicit
14889 instantiation is valid the RESULT will currently be marked as an
14890 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
14891 until we get here. */
14893 if (DECL_TEMPLATE_SPECIALIZATION (result))
14895 /* DR 259 [temp.spec].
14897 Both an explicit instantiation and a declaration of an explicit
14898 specialization shall not appear in a program unless the explicit
14899 instantiation follows a declaration of the explicit specialization.
14901 For a given set of template parameters, if an explicit
14902 instantiation of a template appears after a declaration of an
14903 explicit specialization for that template, the explicit
14904 instantiation has no effect. */
14907 else if (DECL_EXPLICIT_INSTANTIATION (result))
14911 No program shall explicitly instantiate any template more
14914 We check DECL_NOT_REALLY_EXTERN so as not to complain when
14915 the first instantiation was `extern' and the second is not,
14916 and EXTERN_P for the opposite case. */
14917 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
14918 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
14919 /* If an "extern" explicit instantiation follows an ordinary
14920 explicit instantiation, the template is instantiated. */
14924 else if (!DECL_IMPLICIT_INSTANTIATION (result))
14926 error ("no matching template for %qD found", result);
14929 else if (!DECL_TEMPLATE_INFO (result))
14931 permerror (input_location, "explicit instantiation of non-template %q#D", result);
14935 if (storage == NULL_TREE)
14937 else if (storage == ridpointers[(int) RID_EXTERN])
14939 if (!in_system_header && (cxx_dialect == cxx98))
14940 pedwarn (input_location, OPT_pedantic,
14941 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
14946 error ("storage class %qD applied to template instantiation", storage);
14948 check_explicit_instantiation_namespace (result);
14949 mark_decl_instantiated (result, extern_p);
14951 instantiate_decl (result, /*defer_ok=*/1,
14952 /*expl_inst_class_mem_p=*/false);
14956 mark_class_instantiated (tree t, int extern_p)
14958 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
14959 SET_CLASSTYPE_INTERFACE_KNOWN (t);
14960 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
14961 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
14964 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
14965 rest_of_type_compilation (t, 1);
14969 /* Called from do_type_instantiation through binding_table_foreach to
14970 do recursive instantiation for the type bound in ENTRY. */
14972 bt_instantiate_type_proc (binding_entry entry, void *data)
14974 tree storage = *(tree *) data;
14976 if (MAYBE_CLASS_TYPE_P (entry->type)
14977 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
14978 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
14981 /* Called from do_type_instantiation to instantiate a member
14982 (a member function or a static member variable) of an
14983 explicitly instantiated class template. */
14985 instantiate_class_member (tree decl, int extern_p)
14987 mark_decl_instantiated (decl, extern_p);
14989 instantiate_decl (decl, /*defer_ok=*/1,
14990 /*expl_inst_class_mem_p=*/true);
14993 /* Perform an explicit instantiation of template class T. STORAGE, if
14994 non-null, is the RID for extern, inline or static. COMPLAIN is
14995 nonzero if this is called from the parser, zero if called recursively,
14996 since the standard is unclear (as detailed below). */
14999 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
15004 int previous_instantiation_extern_p = 0;
15006 if (TREE_CODE (t) == TYPE_DECL)
15009 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
15011 error ("explicit instantiation of non-template type %qT", t);
15017 if (!COMPLETE_TYPE_P (t))
15019 if (complain & tf_error)
15020 error ("explicit instantiation of %q#T before definition of template",
15025 if (storage != NULL_TREE)
15027 if (!in_system_header)
15029 if (storage == ridpointers[(int) RID_EXTERN])
15031 if (cxx_dialect == cxx98)
15032 pedwarn (input_location, OPT_pedantic,
15033 "ISO C++ 1998 forbids the use of %<extern%> on "
15034 "explicit instantiations");
15037 pedwarn (input_location, OPT_pedantic,
15038 "ISO C++ forbids the use of %qE"
15039 " on explicit instantiations", storage);
15042 if (storage == ridpointers[(int) RID_INLINE])
15044 else if (storage == ridpointers[(int) RID_EXTERN])
15046 else if (storage == ridpointers[(int) RID_STATIC])
15050 error ("storage class %qD applied to template instantiation",
15056 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
15058 /* DR 259 [temp.spec].
15060 Both an explicit instantiation and a declaration of an explicit
15061 specialization shall not appear in a program unless the explicit
15062 instantiation follows a declaration of the explicit specialization.
15064 For a given set of template parameters, if an explicit
15065 instantiation of a template appears after a declaration of an
15066 explicit specialization for that template, the explicit
15067 instantiation has no effect. */
15070 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
15074 No program shall explicitly instantiate any template more
15077 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
15078 instantiation was `extern'. If EXTERN_P then the second is.
15079 These cases are OK. */
15080 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
15082 if (!previous_instantiation_extern_p && !extern_p
15083 && (complain & tf_error))
15084 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
15086 /* If we've already instantiated the template, just return now. */
15087 if (!CLASSTYPE_INTERFACE_ONLY (t))
15091 check_explicit_instantiation_namespace (TYPE_NAME (t));
15092 mark_class_instantiated (t, extern_p);
15100 /* In contrast to implicit instantiation, where only the
15101 declarations, and not the definitions, of members are
15102 instantiated, we have here:
15106 The explicit instantiation of a class template specialization
15107 implies the instantiation of all of its members not
15108 previously explicitly specialized in the translation unit
15109 containing the explicit instantiation.
15111 Of course, we can't instantiate member template classes, since
15112 we don't have any arguments for them. Note that the standard
15113 is unclear on whether the instantiation of the members are
15114 *explicit* instantiations or not. However, the most natural
15115 interpretation is that it should be an explicit instantiation. */
15118 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
15119 if (TREE_CODE (tmp) == FUNCTION_DECL
15120 && DECL_TEMPLATE_INSTANTIATION (tmp))
15121 instantiate_class_member (tmp, extern_p);
15123 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
15124 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
15125 instantiate_class_member (tmp, extern_p);
15127 if (CLASSTYPE_NESTED_UTDS (t))
15128 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
15129 bt_instantiate_type_proc, &storage);
15133 /* Given a function DECL, which is a specialization of TMPL, modify
15134 DECL to be a re-instantiation of TMPL with the same template
15135 arguments. TMPL should be the template into which tsubst'ing
15136 should occur for DECL, not the most general template.
15138 One reason for doing this is a scenario like this:
15141 void f(const T&, int i);
15143 void g() { f(3, 7); }
15146 void f(const T& t, const int i) { }
15148 Note that when the template is first instantiated, with
15149 instantiate_template, the resulting DECL will have no name for the
15150 first parameter, and the wrong type for the second. So, when we go
15151 to instantiate the DECL, we regenerate it. */
15154 regenerate_decl_from_template (tree decl, tree tmpl)
15156 /* The arguments used to instantiate DECL, from the most general
15161 args = DECL_TI_ARGS (decl);
15162 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
15164 /* Make sure that we can see identifiers, and compute access
15166 push_access_scope (decl);
15168 if (TREE_CODE (decl) == FUNCTION_DECL)
15176 args_depth = TMPL_ARGS_DEPTH (args);
15177 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
15178 if (args_depth > parms_depth)
15179 args = get_innermost_template_args (args, parms_depth);
15181 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
15182 args, tf_error, NULL_TREE);
15184 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
15187 /* Merge parameter declarations. */
15188 decl_parm = skip_artificial_parms_for (decl,
15189 DECL_ARGUMENTS (decl));
15191 = skip_artificial_parms_for (code_pattern,
15192 DECL_ARGUMENTS (code_pattern));
15193 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
15198 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
15199 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
15200 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
15202 parm_type = type_decays_to (parm_type);
15203 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
15204 TREE_TYPE (decl_parm) = parm_type;
15205 attributes = DECL_ATTRIBUTES (pattern_parm);
15206 if (DECL_ATTRIBUTES (decl_parm) != attributes)
15208 DECL_ATTRIBUTES (decl_parm) = attributes;
15209 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
15211 decl_parm = TREE_CHAIN (decl_parm);
15212 pattern_parm = TREE_CHAIN (pattern_parm);
15214 /* Merge any parameters that match with the function parameter
15216 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
15219 tree expanded_types;
15220 /* Expand the TYPE_PACK_EXPANSION that provides the types for
15221 the parameters in this function parameter pack. */
15222 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
15223 args, tf_error, NULL_TREE);
15224 len = TREE_VEC_LENGTH (expanded_types);
15225 for (i = 0; i < len; i++)
15230 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
15231 /* Rename the parameter to include the index. */
15232 DECL_NAME (decl_parm) =
15233 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
15234 parm_type = TREE_VEC_ELT (expanded_types, i);
15235 parm_type = type_decays_to (parm_type);
15236 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
15237 TREE_TYPE (decl_parm) = parm_type;
15238 attributes = DECL_ATTRIBUTES (pattern_parm);
15239 if (DECL_ATTRIBUTES (decl_parm) != attributes)
15241 DECL_ATTRIBUTES (decl_parm) = attributes;
15242 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
15244 decl_parm = TREE_CHAIN (decl_parm);
15247 /* Merge additional specifiers from the CODE_PATTERN. */
15248 if (DECL_DECLARED_INLINE_P (code_pattern)
15249 && !DECL_DECLARED_INLINE_P (decl))
15250 DECL_DECLARED_INLINE_P (decl) = 1;
15252 else if (TREE_CODE (decl) == VAR_DECL)
15253 DECL_INITIAL (decl) =
15254 tsubst_expr (DECL_INITIAL (code_pattern), args,
15255 tf_error, DECL_TI_TEMPLATE (decl),
15256 /*integral_constant_expression_p=*/false);
15258 gcc_unreachable ();
15260 pop_access_scope (decl);
15263 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
15264 substituted to get DECL. */
15267 template_for_substitution (tree decl)
15269 tree tmpl = DECL_TI_TEMPLATE (decl);
15271 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
15272 for the instantiation. This is not always the most general
15273 template. Consider, for example:
15276 struct S { template <class U> void f();
15277 template <> void f<int>(); };
15279 and an instantiation of S<double>::f<int>. We want TD to be the
15280 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
15281 while (/* An instantiation cannot have a definition, so we need a
15282 more general template. */
15283 DECL_TEMPLATE_INSTANTIATION (tmpl)
15284 /* We must also deal with friend templates. Given:
15286 template <class T> struct S {
15287 template <class U> friend void f() {};
15290 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
15291 so far as the language is concerned, but that's still
15292 where we get the pattern for the instantiation from. On
15293 other hand, if the definition comes outside the class, say:
15295 template <class T> struct S {
15296 template <class U> friend void f();
15298 template <class U> friend void f() {}
15300 we don't need to look any further. That's what the check for
15301 DECL_INITIAL is for. */
15302 || (TREE_CODE (decl) == FUNCTION_DECL
15303 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
15304 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
15306 /* The present template, TD, should not be a definition. If it
15307 were a definition, we should be using it! Note that we
15308 cannot restructure the loop to just keep going until we find
15309 a template with a definition, since that might go too far if
15310 a specialization was declared, but not defined. */
15311 gcc_assert (TREE_CODE (decl) != VAR_DECL
15312 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
15314 /* Fetch the more general template. */
15315 tmpl = DECL_TI_TEMPLATE (tmpl);
15321 /* Produce the definition of D, a _DECL generated from a template. If
15322 DEFER_OK is nonzero, then we don't have to actually do the
15323 instantiation now; we just have to do it sometime. Normally it is
15324 an error if this is an explicit instantiation but D is undefined.
15325 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
15326 explicitly instantiated class template. */
15329 instantiate_decl (tree d, int defer_ok,
15330 bool expl_inst_class_mem_p)
15332 tree tmpl = DECL_TI_TEMPLATE (d);
15339 bool pattern_defined;
15341 location_t saved_loc = input_location;
15344 /* This function should only be used to instantiate templates for
15345 functions and static member variables. */
15346 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
15347 || TREE_CODE (d) == VAR_DECL);
15349 /* Variables are never deferred; if instantiation is required, they
15350 are instantiated right away. That allows for better code in the
15351 case that an expression refers to the value of the variable --
15352 if the variable has a constant value the referring expression can
15353 take advantage of that fact. */
15354 if (TREE_CODE (d) == VAR_DECL)
15357 /* Don't instantiate cloned functions. Instead, instantiate the
15358 functions they cloned. */
15359 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
15360 d = DECL_CLONED_FUNCTION (d);
15362 if (DECL_TEMPLATE_INSTANTIATED (d))
15363 /* D has already been instantiated. It might seem reasonable to
15364 check whether or not D is an explicit instantiation, and, if so,
15365 stop here. But when an explicit instantiation is deferred
15366 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
15367 is set, even though we still need to do the instantiation. */
15370 /* If we already have a specialization of this declaration, then
15371 there's no reason to instantiate it. Note that
15372 retrieve_specialization gives us both instantiations and
15373 specializations, so we must explicitly check
15374 DECL_TEMPLATE_SPECIALIZATION. */
15375 gen_tmpl = most_general_template (tmpl);
15376 gen_args = DECL_TI_ARGS (d);
15377 spec = retrieve_specialization (gen_tmpl, gen_args,
15378 /*class_specializations_p=*/false);
15379 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
15382 /* This needs to happen before any tsubsting. */
15383 if (! push_tinst_level (d))
15386 timevar_push (TV_PARSE);
15388 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
15389 for the instantiation. */
15390 td = template_for_substitution (d);
15391 code_pattern = DECL_TEMPLATE_RESULT (td);
15393 /* We should never be trying to instantiate a member of a class
15394 template or partial specialization. */
15395 gcc_assert (d != code_pattern);
15397 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
15398 || DECL_TEMPLATE_SPECIALIZATION (td))
15399 /* In the case of a friend template whose definition is provided
15400 outside the class, we may have too many arguments. Drop the
15401 ones we don't need. The same is true for specializations. */
15402 args = get_innermost_template_args
15403 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
15407 if (TREE_CODE (d) == FUNCTION_DECL)
15408 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
15410 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
15412 /* We may be in the middle of deferred access check. Disable it now. */
15413 push_deferring_access_checks (dk_no_deferred);
15415 /* Unless an explicit instantiation directive has already determined
15416 the linkage of D, remember that a definition is available for
15418 if (pattern_defined
15419 && !DECL_INTERFACE_KNOWN (d)
15420 && !DECL_NOT_REALLY_EXTERN (d))
15421 mark_definable (d);
15423 input_location = DECL_SOURCE_LOCATION (d);
15425 /* If D is a member of an explicitly instantiated class template,
15426 and no definition is available, treat it like an implicit
15428 if (!pattern_defined && expl_inst_class_mem_p
15429 && DECL_EXPLICIT_INSTANTIATION (d))
15431 DECL_NOT_REALLY_EXTERN (d) = 0;
15432 DECL_INTERFACE_KNOWN (d) = 0;
15433 SET_DECL_IMPLICIT_INSTANTIATION (d);
15438 /* Recheck the substitutions to obtain any warning messages
15439 about ignoring cv qualifiers. */
15440 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
15441 tree type = TREE_TYPE (gen);
15443 /* Make sure that we can see identifiers, and compute access
15444 correctly. D is already the target FUNCTION_DECL with the
15446 push_access_scope (d);
15448 if (TREE_CODE (gen) == FUNCTION_DECL)
15450 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
15451 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
15453 /* Don't simply tsubst the function type, as that will give
15454 duplicate warnings about poor parameter qualifications.
15455 The function arguments are the same as the decl_arguments
15456 without the top level cv qualifiers. */
15457 type = TREE_TYPE (type);
15459 tsubst (type, gen_args, tf_warning_or_error, d);
15461 pop_access_scope (d);
15464 /* Check to see whether we know that this template will be
15465 instantiated in some other file, as with "extern template"
15467 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
15468 /* In general, we do not instantiate such templates... */
15470 /* ... but we instantiate inline functions so that we can inline
15471 them. An explicit instantiation declaration prohibits implicit
15472 instantiation of non-inline functions. With high levels of
15473 optimization, we would normally inline non-inline functions
15474 -- but we're not allowed to do that for "extern template" functions.
15475 Therefore, we check DECL_DECLARED_INLINE_P, rather than
15476 possibly_inlined_p. And ... */
15477 && ! (TREE_CODE (d) == FUNCTION_DECL
15478 && DECL_DECLARED_INLINE_P (d))
15479 /* ... we instantiate static data members whose values are
15480 needed in integral constant expressions. */
15481 && ! (TREE_CODE (d) == VAR_DECL
15482 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
15484 /* Defer all other templates, unless we have been explicitly
15485 forbidden from doing so. */
15486 if (/* If there is no definition, we cannot instantiate the
15489 /* If it's OK to postpone instantiation, do so. */
15491 /* If this is a static data member that will be defined
15492 elsewhere, we don't want to instantiate the entire data
15493 member, but we do want to instantiate the initializer so that
15494 we can substitute that elsewhere. */
15495 || (external_p && TREE_CODE (d) == VAR_DECL))
15497 /* The definition of the static data member is now required so
15498 we must substitute the initializer. */
15499 if (TREE_CODE (d) == VAR_DECL
15500 && !DECL_INITIAL (d)
15501 && DECL_INITIAL (code_pattern))
15506 ns = decl_namespace_context (d);
15507 push_nested_namespace (ns);
15508 push_nested_class (DECL_CONTEXT (d));
15509 init = tsubst_expr (DECL_INITIAL (code_pattern),
15511 tf_warning_or_error, NULL_TREE,
15512 /*integral_constant_expression_p=*/false);
15513 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
15514 /*asmspec_tree=*/NULL_TREE,
15515 LOOKUP_ONLYCONVERTING);
15516 pop_nested_class ();
15517 pop_nested_namespace (ns);
15520 /* We restore the source position here because it's used by
15521 add_pending_template. */
15522 input_location = saved_loc;
15524 if (at_eof && !pattern_defined
15525 && DECL_EXPLICIT_INSTANTIATION (d)
15526 && DECL_NOT_REALLY_EXTERN (d))
15529 The definition of a non-exported function template, a
15530 non-exported member function template, or a non-exported
15531 member function or static data member of a class template
15532 shall be present in every translation unit in which it is
15533 explicitly instantiated. */
15534 permerror (input_location, "explicit instantiation of %qD "
15535 "but no definition available", d);
15537 /* ??? Historically, we have instantiated inline functions, even
15538 when marked as "extern template". */
15539 if (!(external_p && TREE_CODE (d) == VAR_DECL))
15540 add_pending_template (d);
15543 /* Tell the repository that D is available in this translation unit
15544 -- and see if it is supposed to be instantiated here. */
15545 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
15547 /* In a PCH file, despite the fact that the repository hasn't
15548 requested instantiation in the PCH it is still possible that
15549 an instantiation will be required in a file that includes the
15552 add_pending_template (d);
15553 /* Instantiate inline functions so that the inliner can do its
15554 job, even though we'll not be emitting a copy of this
15556 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
15560 need_push = !cfun || !global_bindings_p ();
15562 push_to_top_level ();
15564 /* Mark D as instantiated so that recursive calls to
15565 instantiate_decl do not try to instantiate it again. */
15566 DECL_TEMPLATE_INSTANTIATED (d) = 1;
15568 /* Regenerate the declaration in case the template has been modified
15569 by a subsequent redeclaration. */
15570 regenerate_decl_from_template (d, td);
15572 /* We already set the file and line above. Reset them now in case
15573 they changed as a result of calling regenerate_decl_from_template. */
15574 input_location = DECL_SOURCE_LOCATION (d);
15576 if (TREE_CODE (d) == VAR_DECL)
15580 /* Clear out DECL_RTL; whatever was there before may not be right
15581 since we've reset the type of the declaration. */
15582 SET_DECL_RTL (d, NULL_RTX);
15583 DECL_IN_AGGR_P (d) = 0;
15585 /* The initializer is placed in DECL_INITIAL by
15586 regenerate_decl_from_template. Pull it out so that
15587 finish_decl can process it. */
15588 init = DECL_INITIAL (d);
15589 DECL_INITIAL (d) = NULL_TREE;
15590 DECL_INITIALIZED_P (d) = 0;
15592 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
15593 initializer. That function will defer actual emission until
15594 we have a chance to determine linkage. */
15595 DECL_EXTERNAL (d) = 0;
15597 /* Enter the scope of D so that access-checking works correctly. */
15598 push_nested_class (DECL_CONTEXT (d));
15599 finish_decl (d, init, NULL_TREE, NULL_TREE);
15600 pop_nested_class ();
15602 else if (TREE_CODE (d) == FUNCTION_DECL)
15604 htab_t saved_local_specializations;
15609 /* Save away the current list, in case we are instantiating one
15610 template from within the body of another. */
15611 saved_local_specializations = local_specializations;
15613 /* Set up the list of local specializations. */
15614 local_specializations = htab_create (37,
15615 hash_local_specialization,
15616 eq_local_specializations,
15619 /* Set up context. */
15620 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
15622 /* Create substitution entries for the parameters. */
15623 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
15624 tmpl_parm = DECL_ARGUMENTS (subst_decl);
15625 spec_parm = DECL_ARGUMENTS (d);
15626 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
15628 register_local_specialization (spec_parm, tmpl_parm);
15629 spec_parm = skip_artificial_parms_for (d, spec_parm);
15630 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
15632 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
15634 register_local_specialization (spec_parm, tmpl_parm);
15635 tmpl_parm = TREE_CHAIN (tmpl_parm);
15636 spec_parm = TREE_CHAIN (spec_parm);
15638 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
15640 /* Register the (value) argument pack as a specialization of
15641 TMPL_PARM, then move on. */
15642 tree argpack = make_fnparm_pack (spec_parm);
15643 register_local_specialization (argpack, tmpl_parm);
15644 tmpl_parm = TREE_CHAIN (tmpl_parm);
15645 spec_parm = NULL_TREE;
15647 gcc_assert (!spec_parm);
15649 /* Substitute into the body of the function. */
15650 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
15651 tf_warning_or_error, tmpl,
15652 /*integral_constant_expression_p=*/false);
15654 /* Set the current input_location to the end of the function
15655 so that finish_function knows where we are. */
15656 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
15658 /* We don't need the local specializations any more. */
15659 htab_delete (local_specializations);
15660 local_specializations = saved_local_specializations;
15662 /* Finish the function. */
15663 d = finish_function (0);
15664 expand_or_defer_fn (d);
15667 /* We're not deferring instantiation any more. */
15668 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
15671 pop_from_top_level ();
15674 input_location = saved_loc;
15675 pop_deferring_access_checks ();
15676 pop_tinst_level ();
15678 timevar_pop (TV_PARSE);
15683 /* Run through the list of templates that we wish we could
15684 instantiate, and instantiate any we can. RETRIES is the
15685 number of times we retry pending template instantiation. */
15688 instantiate_pending_templates (int retries)
15691 location_t saved_loc = input_location;
15693 /* Instantiating templates may trigger vtable generation. This in turn
15694 may require further template instantiations. We place a limit here
15695 to avoid infinite loop. */
15696 if (pending_templates && retries >= max_tinst_depth)
15698 tree decl = pending_templates->tinst->decl;
15700 error ("template instantiation depth exceeds maximum of %d"
15701 " instantiating %q+D, possibly from virtual table generation"
15702 " (use -ftemplate-depth-NN to increase the maximum)",
15703 max_tinst_depth, decl);
15704 if (TREE_CODE (decl) == FUNCTION_DECL)
15705 /* Pretend that we defined it. */
15706 DECL_INITIAL (decl) = error_mark_node;
15712 struct pending_template **t = &pending_templates;
15713 struct pending_template *last = NULL;
15717 tree instantiation = reopen_tinst_level ((*t)->tinst);
15718 bool complete = false;
15720 if (TYPE_P (instantiation))
15724 if (!COMPLETE_TYPE_P (instantiation))
15726 instantiate_class_template (instantiation);
15727 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
15728 for (fn = TYPE_METHODS (instantiation);
15730 fn = TREE_CHAIN (fn))
15731 if (! DECL_ARTIFICIAL (fn))
15732 instantiate_decl (fn,
15734 /*expl_inst_class_mem_p=*/false);
15735 if (COMPLETE_TYPE_P (instantiation))
15739 complete = COMPLETE_TYPE_P (instantiation);
15743 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
15744 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
15747 = instantiate_decl (instantiation,
15749 /*expl_inst_class_mem_p=*/false);
15750 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
15754 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
15755 || DECL_TEMPLATE_INSTANTIATED (instantiation));
15759 /* If INSTANTIATION has been instantiated, then we don't
15760 need to consider it again in the future. */
15768 current_tinst_level = NULL;
15770 last_pending_template = last;
15772 while (reconsider);
15774 input_location = saved_loc;
15777 /* Substitute ARGVEC into T, which is a list of initializers for
15778 either base class or a non-static data member. The TREE_PURPOSEs
15779 are DECLs, and the TREE_VALUEs are the initializer values. Used by
15780 instantiate_decl. */
15783 tsubst_initializer_list (tree t, tree argvec)
15785 tree inits = NULL_TREE;
15787 for (; t; t = TREE_CHAIN (t))
15791 tree expanded_bases = NULL_TREE;
15792 tree expanded_arguments = NULL_TREE;
15795 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
15800 /* Expand the base class expansion type into separate base
15802 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
15803 tf_warning_or_error,
15805 if (expanded_bases == error_mark_node)
15808 /* We'll be building separate TREE_LISTs of arguments for
15810 len = TREE_VEC_LENGTH (expanded_bases);
15811 expanded_arguments = make_tree_vec (len);
15812 for (i = 0; i < len; i++)
15813 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
15815 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
15816 expand each argument in the TREE_VALUE of t. */
15817 expr = make_node (EXPR_PACK_EXPANSION);
15818 PACK_EXPANSION_PARAMETER_PACKS (expr) =
15819 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
15821 if (TREE_VALUE (t) == void_type_node)
15822 /* VOID_TYPE_NODE is used to indicate
15823 value-initialization. */
15825 for (i = 0; i < len; i++)
15826 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
15830 /* Substitute parameter packs into each argument in the
15832 in_base_initializer = 1;
15833 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
15835 tree expanded_exprs;
15837 /* Expand the argument. */
15838 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
15840 = tsubst_pack_expansion (expr, argvec,
15841 tf_warning_or_error,
15843 if (expanded_exprs == error_mark_node)
15846 /* Prepend each of the expanded expressions to the
15847 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
15848 for (i = 0; i < len; i++)
15850 TREE_VEC_ELT (expanded_arguments, i) =
15851 tree_cons (NULL_TREE,
15852 TREE_VEC_ELT (expanded_exprs, i),
15853 TREE_VEC_ELT (expanded_arguments, i));
15856 in_base_initializer = 0;
15858 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
15859 since we built them backwards. */
15860 for (i = 0; i < len; i++)
15862 TREE_VEC_ELT (expanded_arguments, i) =
15863 nreverse (TREE_VEC_ELT (expanded_arguments, i));
15868 for (i = 0; i < len; ++i)
15870 if (expanded_bases)
15872 decl = TREE_VEC_ELT (expanded_bases, i);
15873 decl = expand_member_init (decl);
15874 init = TREE_VEC_ELT (expanded_arguments, i);
15878 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
15879 tf_warning_or_error, NULL_TREE);
15881 decl = expand_member_init (decl);
15882 if (decl && !DECL_P (decl))
15883 in_base_initializer = 1;
15885 init = tsubst_expr (TREE_VALUE (t), argvec,
15886 tf_warning_or_error, NULL_TREE,
15887 /*integral_constant_expression_p=*/false);
15888 in_base_initializer = 0;
15893 init = build_tree_list (decl, init);
15894 TREE_CHAIN (init) = inits;
15902 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
15905 set_current_access_from_decl (tree decl)
15907 if (TREE_PRIVATE (decl))
15908 current_access_specifier = access_private_node;
15909 else if (TREE_PROTECTED (decl))
15910 current_access_specifier = access_protected_node;
15912 current_access_specifier = access_public_node;
15915 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
15916 is the instantiation (which should have been created with
15917 start_enum) and ARGS are the template arguments to use. */
15920 tsubst_enum (tree tag, tree newtag, tree args)
15924 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
15929 decl = TREE_VALUE (e);
15930 /* Note that in a template enum, the TREE_VALUE is the
15931 CONST_DECL, not the corresponding INTEGER_CST. */
15932 value = tsubst_expr (DECL_INITIAL (decl),
15933 args, tf_warning_or_error, NULL_TREE,
15934 /*integral_constant_expression_p=*/true);
15936 /* Give this enumeration constant the correct access. */
15937 set_current_access_from_decl (decl);
15939 /* Actually build the enumerator itself. */
15940 build_enumerator (DECL_NAME (decl), value, newtag);
15943 finish_enum (newtag);
15944 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
15945 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
15948 /* DECL is a FUNCTION_DECL that is a template specialization. Return
15949 its type -- but without substituting the innermost set of template
15950 arguments. So, innermost set of template parameters will appear in
15954 get_mostly_instantiated_function_type (tree decl)
15962 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15963 targs = DECL_TI_ARGS (decl);
15964 tparms = DECL_TEMPLATE_PARMS (tmpl);
15965 parm_depth = TMPL_PARMS_DEPTH (tparms);
15967 /* There should be as many levels of arguments as there are levels
15969 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
15971 fn_type = TREE_TYPE (tmpl);
15973 if (parm_depth == 1)
15974 /* No substitution is necessary. */
15978 int i, save_access_control;
15981 /* Replace the innermost level of the TARGS with NULL_TREEs to
15982 let tsubst know not to substitute for those parameters. */
15983 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
15984 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
15985 SET_TMPL_ARGS_LEVEL (partial_args, i,
15986 TMPL_ARGS_LEVEL (targs, i));
15987 SET_TMPL_ARGS_LEVEL (partial_args,
15988 TMPL_ARGS_DEPTH (targs),
15989 make_tree_vec (DECL_NTPARMS (tmpl)));
15991 /* Disable access control as this function is used only during
15993 save_access_control = flag_access_control;
15994 flag_access_control = 0;
15996 ++processing_template_decl;
15997 /* Now, do the (partial) substitution to figure out the
15998 appropriate function type. */
15999 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
16000 --processing_template_decl;
16002 /* Substitute into the template parameters to obtain the real
16003 innermost set of parameters. This step is important if the
16004 innermost set of template parameters contains value
16005 parameters whose types depend on outer template parameters. */
16006 TREE_VEC_LENGTH (partial_args)--;
16007 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
16009 flag_access_control = save_access_control;
16015 /* Return truthvalue if we're processing a template different from
16016 the last one involved in diagnostics. */
16018 problematic_instantiation_changed (void)
16020 return last_template_error_tick != tinst_level_tick;
16023 /* Remember current template involved in diagnostics. */
16025 record_last_problematic_instantiation (void)
16027 last_template_error_tick = tinst_level_tick;
16030 struct tinst_level *
16031 current_instantiation (void)
16033 return current_tinst_level;
16036 /* [temp.param] Check that template non-type parm TYPE is of an allowable
16037 type. Return zero for ok, nonzero for disallowed. Issue error and
16038 warning messages under control of COMPLAIN. */
16041 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
16043 if (INTEGRAL_TYPE_P (type))
16045 else if (POINTER_TYPE_P (type))
16047 else if (TYPE_PTR_TO_MEMBER_P (type))
16049 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
16051 else if (TREE_CODE (type) == TYPENAME_TYPE)
16054 if (complain & tf_error)
16055 error ("%q#T is not a valid type for a template constant parameter", type);
16059 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
16060 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
16063 dependent_type_p_r (tree type)
16069 A type is dependent if it is:
16071 -- a template parameter. Template template parameters are types
16072 for us (since TYPE_P holds true for them) so we handle
16074 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16075 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
16077 /* -- a qualified-id with a nested-name-specifier which contains a
16078 class-name that names a dependent type or whose unqualified-id
16079 names a dependent type. */
16080 if (TREE_CODE (type) == TYPENAME_TYPE)
16082 /* -- a cv-qualified type where the cv-unqualified type is
16084 type = TYPE_MAIN_VARIANT (type);
16085 /* -- a compound type constructed from any dependent type. */
16086 if (TYPE_PTR_TO_MEMBER_P (type))
16087 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
16088 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
16090 else if (TREE_CODE (type) == POINTER_TYPE
16091 || TREE_CODE (type) == REFERENCE_TYPE)
16092 return dependent_type_p (TREE_TYPE (type));
16093 else if (TREE_CODE (type) == FUNCTION_TYPE
16094 || TREE_CODE (type) == METHOD_TYPE)
16098 if (dependent_type_p (TREE_TYPE (type)))
16100 for (arg_type = TYPE_ARG_TYPES (type);
16102 arg_type = TREE_CHAIN (arg_type))
16103 if (dependent_type_p (TREE_VALUE (arg_type)))
16107 /* -- an array type constructed from any dependent type or whose
16108 size is specified by a constant expression that is
16109 value-dependent. */
16110 if (TREE_CODE (type) == ARRAY_TYPE)
16112 if (TYPE_DOMAIN (type)
16113 && dependent_type_p (TYPE_DOMAIN (type)))
16115 return dependent_type_p (TREE_TYPE (type));
16117 else if (TREE_CODE (type) == INTEGER_TYPE
16118 && !TREE_CONSTANT (TYPE_MAX_VALUE (type)))
16120 /* If this is the TYPE_DOMAIN of an array type, consider it
16121 dependent. We already checked for value-dependence in
16122 compute_array_index_type. */
16123 return type_dependent_expression_p (TYPE_MAX_VALUE (type));
16126 /* -- a template-id in which either the template name is a template
16128 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
16130 /* ... or any of the template arguments is a dependent type or
16131 an expression that is type-dependent or value-dependent. */
16132 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
16133 && (any_dependent_template_arguments_p
16134 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
16137 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
16138 argument of the `typeof' expression is not type-dependent, then
16139 it should already been have resolved. */
16140 if (TREE_CODE (type) == TYPEOF_TYPE
16141 || TREE_CODE (type) == DECLTYPE_TYPE)
16144 /* A template argument pack is dependent if any of its packed
16146 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
16148 tree args = ARGUMENT_PACK_ARGS (type);
16149 int i, len = TREE_VEC_LENGTH (args);
16150 for (i = 0; i < len; ++i)
16151 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
16155 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
16156 be template parameters. */
16157 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
16160 /* The standard does not specifically mention types that are local
16161 to template functions or local classes, but they should be
16162 considered dependent too. For example:
16164 template <int I> void f() {
16169 The size of `E' cannot be known until the value of `I' has been
16170 determined. Therefore, `E' must be considered dependent. */
16171 scope = TYPE_CONTEXT (type);
16172 if (scope && TYPE_P (scope))
16173 return dependent_type_p (scope);
16174 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
16175 return type_dependent_expression_p (scope);
16177 /* Other types are non-dependent. */
16181 /* Returns TRUE if TYPE is dependent, in the sense of
16182 [temp.dep.type]. */
16185 dependent_type_p (tree type)
16187 /* If there are no template parameters in scope, then there can't be
16188 any dependent types. */
16189 if (!processing_template_decl)
16191 /* If we are not processing a template, then nobody should be
16192 providing us with a dependent type. */
16194 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
16198 /* If the type is NULL, we have not computed a type for the entity
16199 in question; in that case, the type is dependent. */
16203 /* Erroneous types can be considered non-dependent. */
16204 if (type == error_mark_node)
16207 /* If we have not already computed the appropriate value for TYPE,
16209 if (!TYPE_DEPENDENT_P_VALID (type))
16211 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
16212 TYPE_DEPENDENT_P_VALID (type) = 1;
16215 return TYPE_DEPENDENT_P (type);
16218 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
16219 lookup. In other words, a dependent type that is not the current
16223 dependent_scope_p (tree scope)
16225 return (scope && TYPE_P (scope) && dependent_type_p (scope)
16226 && !currently_open_class (scope));
16229 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
16232 dependent_scope_ref_p (tree expression, bool criterion (tree))
16237 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
16239 if (!TYPE_P (TREE_OPERAND (expression, 0)))
16242 scope = TREE_OPERAND (expression, 0);
16243 name = TREE_OPERAND (expression, 1);
16247 An id-expression is type-dependent if it contains a
16248 nested-name-specifier that contains a class-name that names a
16250 /* The suggested resolution to Core Issue 224 implies that if the
16251 qualifying type is the current class, then we must peek
16254 && currently_open_class (scope)
16255 && !criterion (name))
16257 if (dependent_type_p (scope))
16263 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
16264 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
16268 value_dependent_expression_p (tree expression)
16270 if (!processing_template_decl)
16273 /* A name declared with a dependent type. */
16274 if (DECL_P (expression) && type_dependent_expression_p (expression))
16277 switch (TREE_CODE (expression))
16279 case IDENTIFIER_NODE:
16280 /* A name that has not been looked up -- must be dependent. */
16283 case TEMPLATE_PARM_INDEX:
16284 /* A non-type template parm. */
16288 /* A non-type template parm. */
16289 if (DECL_TEMPLATE_PARM_P (expression))
16291 return value_dependent_expression_p (DECL_INITIAL (expression));
16294 /* A constant with integral or enumeration type and is initialized
16295 with an expression that is value-dependent. */
16296 if (DECL_INITIAL (expression)
16297 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
16298 && value_dependent_expression_p (DECL_INITIAL (expression)))
16302 case DYNAMIC_CAST_EXPR:
16303 case STATIC_CAST_EXPR:
16304 case CONST_CAST_EXPR:
16305 case REINTERPRET_CAST_EXPR:
16307 /* These expressions are value-dependent if the type to which
16308 the cast occurs is dependent or the expression being casted
16309 is value-dependent. */
16311 tree type = TREE_TYPE (expression);
16313 if (dependent_type_p (type))
16316 /* A functional cast has a list of operands. */
16317 expression = TREE_OPERAND (expression, 0);
16320 /* If there are no operands, it must be an expression such
16321 as "int()". This should not happen for aggregate types
16322 because it would form non-constant expressions. */
16323 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
16328 if (TREE_CODE (expression) == TREE_LIST)
16329 return any_value_dependent_elements_p (expression);
16331 return value_dependent_expression_p (expression);
16336 /* A `sizeof' expression is value-dependent if the operand is
16337 type-dependent or is a pack expansion. */
16338 expression = TREE_OPERAND (expression, 0);
16339 if (PACK_EXPANSION_P (expression))
16341 else if (TYPE_P (expression))
16342 return dependent_type_p (expression);
16343 return type_dependent_expression_p (expression);
16346 return dependent_scope_ref_p (expression, value_dependent_expression_p);
16348 case COMPONENT_REF:
16349 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
16350 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
16353 /* A CALL_EXPR may appear in a constant expression if it is a
16354 call to a builtin function, e.g., __builtin_constant_p. All
16355 such calls are value-dependent. */
16358 case NONTYPE_ARGUMENT_PACK:
16359 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
16360 is value-dependent. */
16362 tree values = ARGUMENT_PACK_ARGS (expression);
16363 int i, len = TREE_VEC_LENGTH (values);
16365 for (i = 0; i < len; ++i)
16366 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
16374 tree type2 = TRAIT_EXPR_TYPE2 (expression);
16375 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
16376 || (type2 ? dependent_type_p (type2) : false));
16380 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
16381 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
16384 /* A constant expression is value-dependent if any subexpression is
16385 value-dependent. */
16386 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
16388 case tcc_reference:
16390 return (value_dependent_expression_p
16391 (TREE_OPERAND (expression, 0)));
16393 case tcc_comparison:
16395 return ((value_dependent_expression_p
16396 (TREE_OPERAND (expression, 0)))
16397 || (value_dependent_expression_p
16398 (TREE_OPERAND (expression, 1))));
16400 case tcc_expression:
16404 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
16405 /* In some cases, some of the operands may be missing.
16406 (For example, in the case of PREDECREMENT_EXPR, the
16407 amount to increment by may be missing.) That doesn't
16408 make the expression dependent. */
16409 if (TREE_OPERAND (expression, i)
16410 && (value_dependent_expression_p
16411 (TREE_OPERAND (expression, i))))
16421 /* The expression is not value-dependent. */
16425 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
16426 [temp.dep.expr]. */
16429 type_dependent_expression_p (tree expression)
16431 if (!processing_template_decl)
16434 if (expression == error_mark_node)
16437 /* An unresolved name is always dependent. */
16438 if (TREE_CODE (expression) == IDENTIFIER_NODE
16439 || TREE_CODE (expression) == USING_DECL)
16442 /* Some expression forms are never type-dependent. */
16443 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
16444 || TREE_CODE (expression) == SIZEOF_EXPR
16445 || TREE_CODE (expression) == ALIGNOF_EXPR
16446 || TREE_CODE (expression) == TRAIT_EXPR
16447 || TREE_CODE (expression) == TYPEID_EXPR
16448 || TREE_CODE (expression) == DELETE_EXPR
16449 || TREE_CODE (expression) == VEC_DELETE_EXPR
16450 || TREE_CODE (expression) == THROW_EXPR)
16453 /* The types of these expressions depends only on the type to which
16454 the cast occurs. */
16455 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
16456 || TREE_CODE (expression) == STATIC_CAST_EXPR
16457 || TREE_CODE (expression) == CONST_CAST_EXPR
16458 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
16459 || TREE_CODE (expression) == CAST_EXPR)
16460 return dependent_type_p (TREE_TYPE (expression));
16462 /* The types of these expressions depends only on the type created
16463 by the expression. */
16464 if (TREE_CODE (expression) == NEW_EXPR
16465 || TREE_CODE (expression) == VEC_NEW_EXPR)
16467 /* For NEW_EXPR tree nodes created inside a template, either
16468 the object type itself or a TREE_LIST may appear as the
16470 tree type = TREE_OPERAND (expression, 1);
16471 if (TREE_CODE (type) == TREE_LIST)
16472 /* This is an array type. We need to check array dimensions
16474 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
16475 || value_dependent_expression_p
16476 (TREE_OPERAND (TREE_VALUE (type), 1));
16478 return dependent_type_p (type);
16481 if (TREE_CODE (expression) == SCOPE_REF
16482 && dependent_scope_ref_p (expression,
16483 type_dependent_expression_p))
16486 if (TREE_CODE (expression) == FUNCTION_DECL
16487 && DECL_LANG_SPECIFIC (expression)
16488 && DECL_TEMPLATE_INFO (expression)
16489 && (any_dependent_template_arguments_p
16490 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
16493 if (TREE_CODE (expression) == TEMPLATE_DECL
16494 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
16497 if (TREE_CODE (expression) == STMT_EXPR)
16498 expression = stmt_expr_value_expr (expression);
16500 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
16505 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
16507 if (type_dependent_expression_p (elt))
16513 if (TREE_TYPE (expression) == unknown_type_node)
16515 if (TREE_CODE (expression) == ADDR_EXPR)
16516 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
16517 if (TREE_CODE (expression) == COMPONENT_REF
16518 || TREE_CODE (expression) == OFFSET_REF)
16520 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
16522 expression = TREE_OPERAND (expression, 1);
16523 if (TREE_CODE (expression) == IDENTIFIER_NODE)
16526 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
16527 if (TREE_CODE (expression) == SCOPE_REF)
16530 if (TREE_CODE (expression) == BASELINK)
16531 expression = BASELINK_FUNCTIONS (expression);
16533 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
16535 if (any_dependent_template_arguments_p
16536 (TREE_OPERAND (expression, 1)))
16538 expression = TREE_OPERAND (expression, 0);
16540 gcc_assert (TREE_CODE (expression) == OVERLOAD
16541 || TREE_CODE (expression) == FUNCTION_DECL);
16545 if (type_dependent_expression_p (OVL_CURRENT (expression)))
16547 expression = OVL_NEXT (expression);
16552 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
16554 return (dependent_type_p (TREE_TYPE (expression)));
16557 /* Like type_dependent_expression_p, but it also works while not processing
16558 a template definition, i.e. during substitution or mangling. */
16561 type_dependent_expression_p_push (tree expr)
16564 ++processing_template_decl;
16565 b = type_dependent_expression_p (expr);
16566 --processing_template_decl;
16570 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
16571 contains a type-dependent expression. */
16574 any_type_dependent_arguments_p (const_tree args)
16578 tree arg = TREE_VALUE (args);
16580 if (type_dependent_expression_p (arg))
16582 args = TREE_CHAIN (args);
16587 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
16588 expressions) contains any value-dependent expressions. */
16591 any_value_dependent_elements_p (const_tree list)
16593 for (; list; list = TREE_CHAIN (list))
16594 if (value_dependent_expression_p (TREE_VALUE (list)))
16600 /* Returns TRUE if the ARG (a template argument) is dependent. */
16603 dependent_template_arg_p (tree arg)
16605 if (!processing_template_decl)
16608 if (TREE_CODE (arg) == TEMPLATE_DECL
16609 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
16610 return dependent_template_p (arg);
16611 else if (ARGUMENT_PACK_P (arg))
16613 tree args = ARGUMENT_PACK_ARGS (arg);
16614 int i, len = TREE_VEC_LENGTH (args);
16615 for (i = 0; i < len; ++i)
16617 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
16623 else if (TYPE_P (arg))
16624 return dependent_type_p (arg);
16626 return (type_dependent_expression_p (arg)
16627 || value_dependent_expression_p (arg));
16630 /* Returns true if ARGS (a collection of template arguments) contains
16631 any types that require structural equality testing. */
16634 any_template_arguments_need_structural_equality_p (tree args)
16641 if (args == error_mark_node)
16644 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
16646 tree level = TMPL_ARGS_LEVEL (args, i + 1);
16647 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
16649 tree arg = TREE_VEC_ELT (level, j);
16650 tree packed_args = NULL_TREE;
16653 if (ARGUMENT_PACK_P (arg))
16655 /* Look inside the argument pack. */
16656 packed_args = ARGUMENT_PACK_ARGS (arg);
16657 len = TREE_VEC_LENGTH (packed_args);
16660 for (k = 0; k < len; ++k)
16663 arg = TREE_VEC_ELT (packed_args, k);
16665 if (error_operand_p (arg))
16667 else if (TREE_CODE (arg) == TEMPLATE_DECL
16668 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
16670 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
16672 else if (!TYPE_P (arg) && TREE_TYPE (arg)
16673 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
16682 /* Returns true if ARGS (a collection of template arguments) contains
16683 any dependent arguments. */
16686 any_dependent_template_arguments_p (const_tree args)
16693 if (args == error_mark_node)
16696 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
16698 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
16699 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
16700 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
16707 /* Returns TRUE if the template TMPL is dependent. */
16710 dependent_template_p (tree tmpl)
16712 if (TREE_CODE (tmpl) == OVERLOAD)
16716 if (dependent_template_p (OVL_FUNCTION (tmpl)))
16718 tmpl = OVL_CHAIN (tmpl);
16723 /* Template template parameters are dependent. */
16724 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
16725 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
16727 /* So are names that have not been looked up. */
16728 if (TREE_CODE (tmpl) == SCOPE_REF
16729 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
16731 /* So are member templates of dependent classes. */
16732 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
16733 return dependent_type_p (DECL_CONTEXT (tmpl));
16737 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
16740 dependent_template_id_p (tree tmpl, tree args)
16742 return (dependent_template_p (tmpl)
16743 || any_dependent_template_arguments_p (args));
16746 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
16750 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
16754 if (!processing_template_decl)
16757 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
16759 tree decl = TREE_VEC_ELT (declv, i);
16760 tree init = TREE_VEC_ELT (initv, i);
16761 tree cond = TREE_VEC_ELT (condv, i);
16762 tree incr = TREE_VEC_ELT (incrv, i);
16764 if (type_dependent_expression_p (decl))
16767 if (init && type_dependent_expression_p (init))
16770 if (type_dependent_expression_p (cond))
16773 if (COMPARISON_CLASS_P (cond)
16774 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
16775 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
16778 if (TREE_CODE (incr) == MODOP_EXPR)
16780 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
16781 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
16784 else if (type_dependent_expression_p (incr))
16786 else if (TREE_CODE (incr) == MODIFY_EXPR)
16788 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
16790 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
16792 tree t = TREE_OPERAND (incr, 1);
16793 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
16794 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
16803 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
16804 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
16805 no such TYPE can be found. Note that this function peers inside
16806 uninstantiated templates and therefore should be used only in
16807 extremely limited situations. ONLY_CURRENT_P restricts this
16808 peering to the currently open classes hierarchy (which is required
16809 when comparing types). */
16812 resolve_typename_type (tree type, bool only_current_p)
16821 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
16823 scope = TYPE_CONTEXT (type);
16824 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
16825 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
16826 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
16827 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
16828 identifier of the TYPENAME_TYPE anymore.
16829 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
16830 TYPENAME_TYPE instead, we avoid messing up with a possible
16831 typedef variant case. */
16832 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
16834 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
16835 it first before we can figure out what NAME refers to. */
16836 if (TREE_CODE (scope) == TYPENAME_TYPE)
16837 scope = resolve_typename_type (scope, only_current_p);
16838 /* If we don't know what SCOPE refers to, then we cannot resolve the
16840 if (TREE_CODE (scope) == TYPENAME_TYPE)
16842 /* If the SCOPE is a template type parameter, we have no way of
16843 resolving the name. */
16844 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
16846 /* If the SCOPE is not the current instantiation, there's no reason
16847 to look inside it. */
16848 if (only_current_p && !currently_open_class (scope))
16850 /* If SCOPE isn't the template itself, it will not have a valid
16851 TYPE_FIELDS list. */
16852 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
16853 /* scope is either the template itself or a compatible instantiation
16854 like X<T>, so look up the name in the original template. */
16855 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
16857 /* scope is a partial instantiation, so we can't do the lookup or we
16858 will lose the template arguments. */
16860 /* Enter the SCOPE so that name lookup will be resolved as if we
16861 were in the class definition. In particular, SCOPE will no
16862 longer be considered a dependent type. */
16863 pushed_scope = push_scope (scope);
16864 /* Look up the declaration. */
16865 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
16867 result = NULL_TREE;
16869 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
16870 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
16873 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
16874 && TREE_CODE (decl) == TYPE_DECL)
16876 result = TREE_TYPE (decl);
16877 if (result == error_mark_node)
16878 result = NULL_TREE;
16880 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
16881 && DECL_CLASS_TEMPLATE_P (decl))
16885 /* Obtain the template and the arguments. */
16886 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
16887 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
16888 /* Instantiate the template. */
16889 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
16890 /*entering_scope=*/0,
16891 tf_error | tf_user);
16892 if (result == error_mark_node)
16893 result = NULL_TREE;
16896 /* Leave the SCOPE. */
16898 pop_scope (pushed_scope);
16900 /* If we failed to resolve it, return the original typename. */
16904 /* If lookup found a typename type, resolve that too. */
16905 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
16907 /* Ill-formed programs can cause infinite recursion here, so we
16908 must catch that. */
16909 TYPENAME_IS_RESOLVING_P (type) = 1;
16910 result = resolve_typename_type (result, only_current_p);
16911 TYPENAME_IS_RESOLVING_P (type) = 0;
16914 /* Qualify the resulting type. */
16915 quals = cp_type_quals (type);
16917 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
16922 /* EXPR is an expression which is not type-dependent. Return a proxy
16923 for EXPR that can be used to compute the types of larger
16924 expressions containing EXPR. */
16927 build_non_dependent_expr (tree expr)
16931 /* Preserve null pointer constants so that the type of things like
16932 "p == 0" where "p" is a pointer can be determined. */
16933 if (null_ptr_cst_p (expr))
16935 /* Preserve OVERLOADs; the functions must be available to resolve
16938 if (TREE_CODE (inner_expr) == STMT_EXPR)
16939 inner_expr = stmt_expr_value_expr (inner_expr);
16940 if (TREE_CODE (inner_expr) == ADDR_EXPR)
16941 inner_expr = TREE_OPERAND (inner_expr, 0);
16942 if (TREE_CODE (inner_expr) == COMPONENT_REF)
16943 inner_expr = TREE_OPERAND (inner_expr, 1);
16944 if (is_overloaded_fn (inner_expr)
16945 || TREE_CODE (inner_expr) == OFFSET_REF)
16947 /* There is no need to return a proxy for a variable. */
16948 if (TREE_CODE (expr) == VAR_DECL)
16950 /* Preserve string constants; conversions from string constants to
16951 "char *" are allowed, even though normally a "const char *"
16952 cannot be used to initialize a "char *". */
16953 if (TREE_CODE (expr) == STRING_CST)
16955 /* Preserve arithmetic constants, as an optimization -- there is no
16956 reason to create a new node. */
16957 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
16959 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
16960 There is at least one place where we want to know that a
16961 particular expression is a throw-expression: when checking a ?:
16962 expression, there are special rules if the second or third
16963 argument is a throw-expression. */
16964 if (TREE_CODE (expr) == THROW_EXPR)
16967 if (TREE_CODE (expr) == COND_EXPR)
16968 return build3 (COND_EXPR,
16970 TREE_OPERAND (expr, 0),
16971 (TREE_OPERAND (expr, 1)
16972 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
16973 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
16974 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
16975 if (TREE_CODE (expr) == COMPOUND_EXPR
16976 && !COMPOUND_EXPR_OVERLOADED (expr))
16977 return build2 (COMPOUND_EXPR,
16979 TREE_OPERAND (expr, 0),
16980 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
16982 /* If the type is unknown, it can't really be non-dependent */
16983 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
16985 /* Otherwise, build a NON_DEPENDENT_EXPR.
16987 REFERENCE_TYPEs are not stripped for expressions in templates
16988 because doing so would play havoc with mangling. Consider, for
16991 template <typename T> void f<T& g>() { g(); }
16993 In the body of "f", the expression for "g" will have
16994 REFERENCE_TYPE, even though the standard says that it should
16995 not. The reason is that we must preserve the syntactic form of
16996 the expression so that mangling (say) "f<g>" inside the body of
16997 "f" works out correctly. Therefore, the REFERENCE_TYPE is
16999 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
17002 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
17003 Return a new TREE_LIST with the various arguments replaced with
17004 equivalent non-dependent expressions. */
17007 build_non_dependent_args (tree args)
17012 new_args = NULL_TREE;
17013 for (a = args; a; a = TREE_CHAIN (a))
17014 new_args = tree_cons (NULL_TREE,
17015 build_non_dependent_expr (TREE_VALUE (a)),
17017 return nreverse (new_args);
17020 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
17021 with a level one deeper than the actual template parms. */
17028 /* ??? Is it worth caching this for multiple autos at the same level? */
17029 au = cxx_make_type (TEMPLATE_TYPE_PARM);
17030 TYPE_NAME (au) = build_decl (TYPE_DECL, get_identifier ("auto"), au);
17031 TYPE_STUB_DECL (au) = TYPE_NAME (au);
17032 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
17033 (0, processing_template_decl + 1, processing_template_decl + 1,
17034 TYPE_NAME (au), NULL_TREE);
17035 TYPE_CANONICAL (au) = canonical_type_parameter (au);
17036 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
17037 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
17042 /* Replace auto in TYPE with std::initializer_list<auto>. */
17045 listify_autos (tree type, tree auto_node)
17047 tree std_init_list = namespace_binding
17048 (get_identifier ("initializer_list"), std_node);
17051 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
17053 error ("deducing auto from brace-enclosed initializer list requires "
17054 "#include <initializer_list>");
17055 return error_mark_node;
17057 argvec = make_tree_vec (1);
17058 TREE_VEC_ELT (argvec, 0) = auto_node;
17059 init_auto = lookup_template_class (std_init_list, argvec, NULL_TREE,
17060 NULL_TREE, 0, tf_warning_or_error);
17062 TREE_VEC_ELT (argvec, 0) = init_auto;
17063 if (processing_template_decl)
17064 argvec = add_to_template_args (current_template_args (), argvec);
17065 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
17068 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
17069 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
17072 do_auto_deduction (tree type, tree init, tree auto_node)
17074 tree parms, args, tparms, targs;
17077 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
17078 with either a new invented type template parameter U or, if the
17079 initializer is a braced-init-list (8.5.4), with
17080 std::initializer_list<U>. */
17081 if (BRACE_ENCLOSED_INITIALIZER_P (init))
17082 type = listify_autos (type, auto_node);
17084 parms = build_tree_list (NULL_TREE, type);
17085 args = build_tree_list (NULL_TREE, init);
17086 tparms = make_tree_vec (1);
17087 targs = make_tree_vec (1);
17088 TREE_VEC_ELT (tparms, 0)
17089 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
17090 val = type_unification_real (tparms, targs, parms, args, 0,
17091 DEDUCE_CALL, LOOKUP_NORMAL);
17094 error ("unable to deduce %qT from %qE", type, init);
17095 return error_mark_node;
17098 if (processing_template_decl)
17099 targs = add_to_template_args (current_template_args (), targs);
17100 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
17103 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
17107 splice_late_return_type (tree type, tree late_return_type)
17111 if (late_return_type == NULL_TREE)
17113 argvec = make_tree_vec (1);
17114 TREE_VEC_ELT (argvec, 0) = late_return_type;
17115 if (processing_template_decl)
17116 argvec = add_to_template_args (current_template_args (), argvec);
17117 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
17120 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
17123 is_auto (const_tree type)
17125 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17126 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
17132 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
17133 appear as a type-specifier for the declaration in question, we don't
17134 have to look through the whole type. */
17137 type_uses_auto (tree type)
17139 enum tree_code code;
17140 if (is_auto (type))
17143 code = TREE_CODE (type);
17145 if (code == POINTER_TYPE || code == REFERENCE_TYPE
17146 || code == OFFSET_TYPE || code == FUNCTION_TYPE
17147 || code == METHOD_TYPE || code == ARRAY_TYPE)
17148 return type_uses_auto (TREE_TYPE (type));
17150 if (TYPE_PTRMEMFUNC_P (type))
17151 return type_uses_auto (TREE_TYPE (TREE_TYPE
17152 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17157 /* Append TYPE_DECL to the template TEMPL.
17158 TEMPL is either a class type or a FUNCTION_DECL associated
17159 to a TEMPLATE_DECL.
17160 At TEMPL instanciation time, TYPE_DECL will be checked to see
17161 if it can be accessed through SCOPE. */
17164 append_type_to_template_for_access_check (tree templ,
17168 tree node, templ_decl;
17171 && get_template_info (templ)
17172 && TI_TEMPLATE (get_template_info (templ))
17174 && (TREE_CODE (type_decl) == TYPE_DECL));
17176 templ_decl = TI_TEMPLATE (get_template_info (templ));
17177 gcc_assert (templ_decl);
17179 /* Make sure we don't append the type to the template twice.
17180 If this appears to be too slow, the
17181 MEMBER_TYPE_NEEDING_ACCESS_CHECK property
17182 of templ should be a hash table instead. */
17183 for (node = MEMBER_TYPES_NEEDING_ACCESS_CHECK (templ_decl);
17185 node = TREE_CHAIN (node))
17187 tree decl = TREE_PURPOSE (node);
17188 tree type_scope = TREE_VALUE (node);
17190 if (decl == type_decl && type_scope == scope)
17194 MEMBER_TYPES_NEEDING_ACCESS_CHECK (templ_decl) =
17195 tree_cons (type_decl, scope,
17196 MEMBER_TYPES_NEEDING_ACCESS_CHECK (templ_decl));
17199 #include "gt-cp-pt.h"