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, 2010
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"
38 #include "c-family/c-common.h"
39 #include "cp-objcp-common.h"
40 #include "tree-inline.h"
45 #include "tree-iterator.h"
48 /* The type of functions taking a tree, and some additional data, and
50 typedef int (*tree_fn_t) (tree, void*);
52 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
53 instantiations have been deferred, either because their definitions
54 were not yet available, or because we were putting off doing the work. */
55 struct GTY (()) pending_template {
56 struct pending_template *next;
57 struct tinst_level *tinst;
60 static GTY(()) struct pending_template *pending_templates;
61 static GTY(()) struct pending_template *last_pending_template;
63 int processing_template_parmlist;
64 static int template_header_count;
66 static GTY(()) tree saved_trees;
67 static VEC(int,heap) *inline_parm_levels;
69 static GTY(()) struct tinst_level *current_tinst_level;
71 static GTY(()) tree saved_access_scope;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr;
78 /* A map from local variable declarations in the body of the template
79 presently being instantiated to the corresponding instantiated
81 static htab_t local_specializations;
83 typedef struct GTY(()) spec_entry
90 static GTY ((param_is (spec_entry)))
91 htab_t decl_specializations;
93 static GTY ((param_is (spec_entry)))
94 htab_t type_specializations;
96 /* Contains canonical template parameter types. The vector is indexed by
97 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
98 TREE_LIST, whose TREE_VALUEs contain the canonical template
99 parameters of various types and levels. */
100 static GTY(()) VEC(tree,gc) *canonical_template_parms;
102 #define UNIFY_ALLOW_NONE 0
103 #define UNIFY_ALLOW_MORE_CV_QUAL 1
104 #define UNIFY_ALLOW_LESS_CV_QUAL 2
105 #define UNIFY_ALLOW_DERIVED 4
106 #define UNIFY_ALLOW_INTEGER 8
107 #define UNIFY_ALLOW_OUTER_LEVEL 16
108 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
109 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
111 static void push_access_scope (tree);
112 static void pop_access_scope (tree);
113 static bool resolve_overloaded_unification (tree, tree, tree, tree,
114 unification_kind_t, int);
115 static int try_one_overload (tree, tree, tree, tree, tree,
116 unification_kind_t, int, bool);
117 static int unify (tree, tree, tree, tree, int);
118 static void add_pending_template (tree);
119 static tree reopen_tinst_level (struct tinst_level *);
120 static tree tsubst_initializer_list (tree, tree);
121 static tree get_class_bindings (tree, tree, tree);
122 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
124 static void tsubst_enum (tree, tree, tree);
125 static tree add_to_template_args (tree, tree);
126 static tree add_outermost_template_args (tree, tree);
127 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
128 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
130 static int type_unification_real (tree, tree, tree, const tree *,
131 unsigned int, int, unification_kind_t, int);
132 static void note_template_header (int);
133 static tree convert_nontype_argument_function (tree, tree);
134 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
135 static tree convert_template_argument (tree, tree, tree,
136 tsubst_flags_t, int, tree);
137 static int for_each_template_parm (tree, tree_fn_t, void*,
138 struct pointer_set_t*, bool);
139 static tree expand_template_argument_pack (tree);
140 static tree build_template_parm_index (int, int, int, tree, tree);
141 static bool inline_needs_template_parms (tree);
142 static void push_inline_template_parms_recursive (tree, int);
143 static tree retrieve_local_specialization (tree);
144 static void register_local_specialization (tree, tree);
145 static hashval_t hash_specialization (const void *p);
146 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
147 static int mark_template_parm (tree, void *);
148 static int template_parm_this_level_p (tree, void *);
149 static tree tsubst_friend_function (tree, tree);
150 static tree tsubst_friend_class (tree, tree);
151 static int can_complete_type_without_circularity (tree);
152 static tree get_bindings (tree, tree, tree, bool);
153 static int template_decl_level (tree);
154 static int check_cv_quals_for_unify (int, tree, tree);
155 static void template_parm_level_and_index (tree, int*, int*);
156 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
157 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
158 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
159 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
160 static void regenerate_decl_from_template (tree, tree);
161 static tree most_specialized_class (tree, tree, tsubst_flags_t);
162 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
163 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
164 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
165 static bool check_specialization_scope (void);
166 static tree process_partial_specialization (tree);
167 static void set_current_access_from_decl (tree);
168 static tree get_template_base (tree, tree, tree, tree);
169 static tree try_class_unification (tree, tree, tree, tree);
170 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
172 static bool template_template_parm_bindings_ok_p (tree, tree);
173 static int template_args_equal (tree, tree);
174 static void tsubst_default_arguments (tree);
175 static tree for_each_template_parm_r (tree *, int *, void *);
176 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
177 static void copy_default_args_to_explicit_spec (tree);
178 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
179 static int eq_local_specializations (const void *, const void *);
180 static bool dependent_template_arg_p (tree);
181 static bool any_template_arguments_need_structural_equality_p (tree);
182 static bool dependent_type_p_r (tree);
183 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
184 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
185 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
186 static tree tsubst_decl (tree, tree, tsubst_flags_t);
187 static void perform_typedefs_access_check (tree tmpl, tree targs);
188 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
190 static hashval_t iterative_hash_template_arg (tree arg, hashval_t val);
191 static tree listify (tree);
192 static tree listify_autos (tree, tree);
194 /* Make the current scope suitable for access checking when we are
195 processing T. T can be FUNCTION_DECL for instantiated function
196 template, or VAR_DECL for static member variable (need by
197 instantiate_decl). */
200 push_access_scope (tree t)
202 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
203 || TREE_CODE (t) == VAR_DECL);
205 if (DECL_FRIEND_CONTEXT (t))
206 push_nested_class (DECL_FRIEND_CONTEXT (t));
207 else if (DECL_CLASS_SCOPE_P (t))
208 push_nested_class (DECL_CONTEXT (t));
210 push_to_top_level ();
212 if (TREE_CODE (t) == FUNCTION_DECL)
214 saved_access_scope = tree_cons
215 (NULL_TREE, current_function_decl, saved_access_scope);
216 current_function_decl = t;
220 /* Restore the scope set up by push_access_scope. T is the node we
224 pop_access_scope (tree t)
226 if (TREE_CODE (t) == FUNCTION_DECL)
228 current_function_decl = TREE_VALUE (saved_access_scope);
229 saved_access_scope = TREE_CHAIN (saved_access_scope);
232 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
235 pop_from_top_level ();
238 /* Do any processing required when DECL (a member template
239 declaration) is finished. Returns the TEMPLATE_DECL corresponding
240 to DECL, unless it is a specialization, in which case the DECL
241 itself is returned. */
244 finish_member_template_decl (tree decl)
246 if (decl == error_mark_node)
247 return error_mark_node;
249 gcc_assert (DECL_P (decl));
251 if (TREE_CODE (decl) == TYPE_DECL)
255 type = TREE_TYPE (decl);
256 if (type == error_mark_node)
257 return error_mark_node;
258 if (MAYBE_CLASS_TYPE_P (type)
259 && CLASSTYPE_TEMPLATE_INFO (type)
260 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
262 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
263 check_member_template (tmpl);
268 else if (TREE_CODE (decl) == FIELD_DECL)
269 error ("data member %qD cannot be a member template", decl);
270 else if (DECL_TEMPLATE_INFO (decl))
272 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
274 check_member_template (DECL_TI_TEMPLATE (decl));
275 return DECL_TI_TEMPLATE (decl);
281 error ("invalid member template declaration %qD", decl);
283 return error_mark_node;
286 /* Create a template info node. */
289 build_template_info (tree template_decl, tree template_args)
291 tree result = make_node (TEMPLATE_INFO);
292 TI_TEMPLATE (result) = template_decl;
293 TI_ARGS (result) = template_args;
297 /* Return the template info node corresponding to T, whatever T is. */
300 get_template_info (const_tree t)
302 tree tinfo = NULL_TREE;
304 if (!t || t == error_mark_node)
307 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
308 tinfo = DECL_TEMPLATE_INFO (t);
310 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
313 if (TAGGED_TYPE_P (t))
314 tinfo = TYPE_TEMPLATE_INFO (t);
315 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
316 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
321 /* Returns the template nesting level of the indicated class TYPE.
331 A<T>::B<U> has depth two, while A<T> has depth one.
332 Both A<T>::B<int> and A<int>::B<U> have depth one, if
333 they are instantiations, not specializations.
335 This function is guaranteed to return 0 if passed NULL_TREE so
336 that, for example, `template_class_depth (current_class_type)' is
340 template_class_depth (tree type)
345 type && TREE_CODE (type) != NAMESPACE_DECL;
346 type = (TREE_CODE (type) == FUNCTION_DECL)
347 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
349 tree tinfo = get_template_info (type);
351 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
352 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
359 /* Subroutine of maybe_begin_member_template_processing.
360 Returns true if processing DECL needs us to push template parms. */
363 inline_needs_template_parms (tree decl)
365 if (! DECL_TEMPLATE_INFO (decl))
368 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
369 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
372 /* Subroutine of maybe_begin_member_template_processing.
373 Push the template parms in PARMS, starting from LEVELS steps into the
374 chain, and ending at the beginning, since template parms are listed
378 push_inline_template_parms_recursive (tree parmlist, int levels)
380 tree parms = TREE_VALUE (parmlist);
384 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
386 ++processing_template_decl;
387 current_template_parms
388 = tree_cons (size_int (processing_template_decl),
389 parms, current_template_parms);
390 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
392 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
394 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
396 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
398 if (parm == error_mark_node)
401 gcc_assert (DECL_P (parm));
403 switch (TREE_CODE (parm))
412 /* Make a CONST_DECL as is done in process_template_parm.
413 It is ugly that we recreate this here; the original
414 version built in process_template_parm is no longer
416 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
417 CONST_DECL, DECL_NAME (parm),
419 DECL_ARTIFICIAL (decl) = 1;
420 TREE_CONSTANT (decl) = 1;
421 TREE_READONLY (decl) = 1;
422 DECL_INITIAL (decl) = DECL_INITIAL (parm);
423 SET_DECL_TEMPLATE_PARM_P (decl);
434 /* Restore the template parameter context for a member template or
435 a friend template defined in a class definition. */
438 maybe_begin_member_template_processing (tree decl)
443 if (inline_needs_template_parms (decl))
445 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
446 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
448 if (DECL_TEMPLATE_SPECIALIZATION (decl))
451 parms = TREE_CHAIN (parms);
454 push_inline_template_parms_recursive (parms, levels);
457 /* Remember how many levels of template parameters we pushed so that
458 we can pop them later. */
459 VEC_safe_push (int, heap, inline_parm_levels, levels);
462 /* Undo the effects of maybe_begin_member_template_processing. */
465 maybe_end_member_template_processing (void)
470 if (VEC_length (int, inline_parm_levels) == 0)
473 last = VEC_pop (int, inline_parm_levels);
474 for (i = 0; i < last; ++i)
476 --processing_template_decl;
477 current_template_parms = TREE_CHAIN (current_template_parms);
482 /* Return a new template argument vector which contains all of ARGS,
483 but has as its innermost set of arguments the EXTRA_ARGS. */
486 add_to_template_args (tree args, tree extra_args)
493 if (args == NULL_TREE || extra_args == error_mark_node)
496 extra_depth = TMPL_ARGS_DEPTH (extra_args);
497 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
499 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
500 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
502 for (j = 1; j <= extra_depth; ++j, ++i)
503 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
508 /* Like add_to_template_args, but only the outermost ARGS are added to
509 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
510 (EXTRA_ARGS) levels are added. This function is used to combine
511 the template arguments from a partial instantiation with the
512 template arguments used to attain the full instantiation from the
513 partial instantiation. */
516 add_outermost_template_args (tree args, tree extra_args)
520 /* If there are more levels of EXTRA_ARGS than there are ARGS,
521 something very fishy is going on. */
522 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
524 /* If *all* the new arguments will be the EXTRA_ARGS, just return
526 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
529 /* For the moment, we make ARGS look like it contains fewer levels. */
530 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
532 new_args = add_to_template_args (args, extra_args);
534 /* Now, we restore ARGS to its full dimensions. */
535 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
540 /* Return the N levels of innermost template arguments from the ARGS. */
543 get_innermost_template_args (tree args, int n)
551 /* If N is 1, just return the innermost set of template arguments. */
553 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
555 /* If we're not removing anything, just return the arguments we were
557 extra_levels = TMPL_ARGS_DEPTH (args) - n;
558 gcc_assert (extra_levels >= 0);
559 if (extra_levels == 0)
562 /* Make a new set of arguments, not containing the outer arguments. */
563 new_args = make_tree_vec (n);
564 for (i = 1; i <= n; ++i)
565 SET_TMPL_ARGS_LEVEL (new_args, i,
566 TMPL_ARGS_LEVEL (args, i + extra_levels));
571 /* The inverse of get_innermost_template_args: Return all but the innermost
572 EXTRA_LEVELS levels of template arguments from the ARGS. */
575 strip_innermost_template_args (tree args, int extra_levels)
578 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
583 /* If N is 1, just return the outermost set of template arguments. */
585 return TMPL_ARGS_LEVEL (args, 1);
587 /* If we're not removing anything, just return the arguments we were
589 gcc_assert (extra_levels >= 0);
590 if (extra_levels == 0)
593 /* Make a new set of arguments, not containing the inner arguments. */
594 new_args = make_tree_vec (n);
595 for (i = 1; i <= n; ++i)
596 SET_TMPL_ARGS_LEVEL (new_args, i,
597 TMPL_ARGS_LEVEL (args, i));
602 /* We've got a template header coming up; push to a new level for storing
606 begin_template_parm_list (void)
608 /* We use a non-tag-transparent scope here, which causes pushtag to
609 put tags in this scope, rather than in the enclosing class or
610 namespace scope. This is the right thing, since we want
611 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
612 global template class, push_template_decl handles putting the
613 TEMPLATE_DECL into top-level scope. For a nested template class,
616 template <class T> struct S1 {
617 template <class T> struct S2 {};
620 pushtag contains special code to call pushdecl_with_scope on the
621 TEMPLATE_DECL for S2. */
622 begin_scope (sk_template_parms, NULL);
623 ++processing_template_decl;
624 ++processing_template_parmlist;
625 note_template_header (0);
628 /* This routine is called when a specialization is declared. If it is
629 invalid to declare a specialization here, an error is reported and
630 false is returned, otherwise this routine will return true. */
633 check_specialization_scope (void)
635 tree scope = current_scope ();
639 An explicit specialization shall be declared in the namespace of
640 which the template is a member, or, for member templates, in the
641 namespace of which the enclosing class or enclosing class
642 template is a member. An explicit specialization of a member
643 function, member class or static data member of a class template
644 shall be declared in the namespace of which the class template
646 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
648 error ("explicit specialization in non-namespace scope %qD", scope);
654 In an explicit specialization declaration for a member of a class
655 template or a member template that appears in namespace scope,
656 the member template and some of its enclosing class templates may
657 remain unspecialized, except that the declaration shall not
658 explicitly specialize a class member template if its enclosing
659 class templates are not explicitly specialized as well. */
660 if (current_template_parms)
662 error ("enclosing class templates are not explicitly specialized");
669 /* We've just seen template <>. */
672 begin_specialization (void)
674 begin_scope (sk_template_spec, NULL);
675 note_template_header (1);
676 return check_specialization_scope ();
679 /* Called at then end of processing a declaration preceded by
683 end_specialization (void)
686 reset_specialization ();
689 /* Any template <>'s that we have seen thus far are not referring to a
690 function specialization. */
693 reset_specialization (void)
695 processing_specialization = 0;
696 template_header_count = 0;
699 /* We've just seen a template header. If SPECIALIZATION is nonzero,
700 it was of the form template <>. */
703 note_template_header (int specialization)
705 processing_specialization = specialization;
706 template_header_count++;
709 /* We're beginning an explicit instantiation. */
712 begin_explicit_instantiation (void)
714 gcc_assert (!processing_explicit_instantiation);
715 processing_explicit_instantiation = true;
720 end_explicit_instantiation (void)
722 gcc_assert (processing_explicit_instantiation);
723 processing_explicit_instantiation = false;
726 /* An explicit specialization or partial specialization TMPL is being
727 declared. Check that the namespace in which the specialization is
728 occurring is permissible. Returns false iff it is invalid to
729 specialize TMPL in the current namespace. */
732 check_specialization_namespace (tree tmpl)
734 tree tpl_ns = decl_namespace_context (tmpl);
738 An explicit specialization shall be declared in the namespace of
739 which the template is a member, or, for member templates, in the
740 namespace of which the enclosing class or enclosing class
741 template is a member. An explicit specialization of a member
742 function, member class or static data member of a class template
743 shall be declared in the namespace of which the class template is
745 if (current_scope() != DECL_CONTEXT (tmpl)
746 && !at_namespace_scope_p ())
748 error ("specialization of %qD must appear at namespace scope", tmpl);
751 if (is_associated_namespace (current_namespace, tpl_ns))
752 /* Same or super-using namespace. */
756 permerror (input_location, "specialization of %qD in different namespace", tmpl);
757 permerror (input_location, " from definition of %q+#D", tmpl);
762 /* SPEC is an explicit instantiation. Check that it is valid to
763 perform this explicit instantiation in the current namespace. */
766 check_explicit_instantiation_namespace (tree spec)
770 /* DR 275: An explicit instantiation shall appear in an enclosing
771 namespace of its template. */
772 ns = decl_namespace_context (spec);
773 if (!is_ancestor (current_namespace, ns))
774 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
775 "(which does not enclose namespace %qD)",
776 spec, current_namespace, ns);
779 /* The TYPE is being declared. If it is a template type, that means it
780 is a partial specialization. Do appropriate error-checking. */
783 maybe_process_partial_specialization (tree type)
787 if (type == error_mark_node)
788 return error_mark_node;
790 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
792 error ("name of class shadows template template parameter %qD",
794 return error_mark_node;
797 context = TYPE_CONTEXT (type);
799 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
801 /* This is for ordinary explicit specialization and partial
802 specialization of a template class such as:
804 template <> class C<int>;
808 template <class T> class C<T*>;
810 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
812 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
813 && !COMPLETE_TYPE_P (type))
815 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
816 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
817 if (processing_template_decl)
819 if (push_template_decl (TYPE_MAIN_DECL (type))
821 return error_mark_node;
824 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
825 error ("specialization of %qT after instantiation", type);
827 else if (CLASS_TYPE_P (type)
828 && !CLASSTYPE_USE_TEMPLATE (type)
829 && CLASSTYPE_TEMPLATE_INFO (type)
830 && context && CLASS_TYPE_P (context)
831 && CLASSTYPE_TEMPLATE_INFO (context))
833 /* This is for an explicit specialization of member class
834 template according to [temp.expl.spec/18]:
836 template <> template <class U> class C<int>::D;
838 The context `C<int>' must be an implicit instantiation.
839 Otherwise this is just a member class template declared
842 template <> class C<int> { template <class U> class D; };
843 template <> template <class U> class C<int>::D;
845 In the first case, `C<int>::D' is a specialization of `C<T>::D'
846 while in the second case, `C<int>::D' is a primary template
847 and `C<T>::D' may not exist. */
849 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
850 && !COMPLETE_TYPE_P (type))
853 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
855 if (current_namespace
856 != decl_namespace_context (tmpl))
858 permerror (input_location, "specializing %q#T in different namespace", type);
859 permerror (input_location, " from definition of %q+#D", tmpl);
862 /* Check for invalid specialization after instantiation:
864 template <> template <> class C<int>::D<int>;
865 template <> template <class U> class C<int>::D; */
867 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
868 t; t = TREE_CHAIN (t))
870 tree inst = TREE_VALUE (t);
871 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
873 /* We already have a full specialization of this partial
874 instantiation. Reassign it to the new member
875 specialization template. */
879 elt.tmpl = most_general_template (tmpl);
880 elt.args = CLASSTYPE_TI_ARGS (inst);
883 htab_remove_elt (type_specializations, &elt);
886 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
888 slot = (spec_entry **)
889 htab_find_slot (type_specializations, &elt, INSERT);
890 *slot = ggc_alloc_spec_entry ();
893 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
894 /* But if we've had an implicit instantiation, that's a
895 problem ([temp.expl.spec]/6). */
896 error ("specialization %qT after instantiation %qT",
900 /* Mark TYPE as a specialization. And as a result, we only
901 have one level of template argument for the innermost
903 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
904 CLASSTYPE_TI_ARGS (type)
905 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
908 else if (processing_specialization)
910 error ("explicit specialization of non-template %qT", type);
911 return error_mark_node;
917 /* Returns nonzero if we can optimize the retrieval of specializations
918 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
919 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
922 optimize_specialization_lookup_p (tree tmpl)
924 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
925 && DECL_CLASS_SCOPE_P (tmpl)
926 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
928 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
929 /* The optimized lookup depends on the fact that the
930 template arguments for the member function template apply
931 purely to the containing class, which is not true if the
932 containing class is an explicit or partial
934 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
935 && !DECL_MEMBER_TEMPLATE_P (tmpl)
936 && !DECL_CONV_FN_P (tmpl)
937 /* It is possible to have a template that is not a member
938 template and is not a member of a template class:
940 template <typename T>
941 struct S { friend A::f(); };
943 Here, the friend function is a template, but the context does
944 not have template information. The optimized lookup relies
945 on having ARGS be the template arguments for both the class
946 and the function template. */
947 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
950 /* Retrieve the specialization (in the sense of [temp.spec] - a
951 specialization is either an instantiation or an explicit
952 specialization) of TMPL for the given template ARGS. If there is
953 no such specialization, return NULL_TREE. The ARGS are a vector of
954 arguments, or a vector of vectors of arguments, in the case of
955 templates with more than one level of parameters.
957 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
958 then we search for a partial specialization matching ARGS. This
959 parameter is ignored if TMPL is not a class template. */
962 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
964 if (args == error_mark_node)
967 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
969 /* There should be as many levels of arguments as there are
970 levels of parameters. */
971 gcc_assert (TMPL_ARGS_DEPTH (args)
972 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
974 if (optimize_specialization_lookup_p (tmpl))
977 tree class_specialization;
978 VEC(tree,gc) *methods;
982 /* The template arguments actually apply to the containing
983 class. Find the class specialization with those
985 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
987 = retrieve_specialization (class_template, args, 0);
988 if (!class_specialization)
990 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
991 for the specialization. */
992 idx = class_method_index_for_fn (class_specialization, tmpl);
995 /* Iterate through the methods with the indicated name, looking
996 for the one that has an instance of TMPL. */
997 methods = CLASSTYPE_METHOD_VEC (class_specialization);
998 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
1000 tree fn = OVL_CURRENT (fns);
1001 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1002 /* using-declarations can add base methods to the method vec,
1003 and we don't want those here. */
1004 && DECL_CONTEXT (fn) == class_specialization)
1013 htab_t specializations;
1017 elt.spec = NULL_TREE;
1019 if (DECL_CLASS_TEMPLATE_P (tmpl))
1020 specializations = type_specializations;
1022 specializations = decl_specializations;
1025 hash = hash_specialization (&elt);
1026 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1034 /* Like retrieve_specialization, but for local declarations. */
1037 retrieve_local_specialization (tree tmpl)
1041 if (local_specializations == NULL)
1044 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
1045 htab_hash_pointer (tmpl));
1046 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
1049 /* Returns nonzero iff DECL is a specialization of TMPL. */
1052 is_specialization_of (tree decl, tree tmpl)
1056 if (TREE_CODE (decl) == FUNCTION_DECL)
1060 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1066 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1068 for (t = TREE_TYPE (decl);
1070 t = CLASSTYPE_USE_TEMPLATE (t)
1071 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1072 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1079 /* Returns nonzero iff DECL is a specialization of friend declaration
1080 FRIEND_DECL according to [temp.friend]. */
1083 is_specialization_of_friend (tree decl, tree friend_decl)
1085 bool need_template = true;
1088 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1089 || TREE_CODE (decl) == TYPE_DECL);
1091 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1092 of a template class, we want to check if DECL is a specialization
1094 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1095 && DECL_TEMPLATE_INFO (friend_decl)
1096 && !DECL_USE_TEMPLATE (friend_decl))
1098 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1099 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1100 need_template = false;
1102 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1103 && !PRIMARY_TEMPLATE_P (friend_decl))
1104 need_template = false;
1106 /* There is nothing to do if this is not a template friend. */
1107 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1110 if (is_specialization_of (decl, friend_decl))
1114 A member of a class template may be declared to be a friend of a
1115 non-template class. In this case, the corresponding member of
1116 every specialization of the class template is a friend of the
1117 class granting friendship.
1119 For example, given a template friend declaration
1121 template <class T> friend void A<T>::f();
1123 the member function below is considered a friend
1125 template <> struct A<int> {
1129 For this type of template friend, TEMPLATE_DEPTH below will be
1130 nonzero. To determine if DECL is a friend of FRIEND, we first
1131 check if the enclosing class is a specialization of another. */
1133 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1135 && DECL_CLASS_SCOPE_P (decl)
1136 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1137 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1139 /* Next, we check the members themselves. In order to handle
1140 a few tricky cases, such as when FRIEND_DECL's are
1142 template <class T> friend void A<T>::g(T t);
1143 template <class T> template <T t> friend void A<T>::h();
1147 void A<int>::g(int);
1148 template <int> void A<int>::h();
1150 we need to figure out ARGS, the template arguments from
1151 the context of DECL. This is required for template substitution
1152 of `T' in the function parameter of `g' and template parameter
1153 of `h' in the above examples. Here ARGS corresponds to `int'. */
1155 tree context = DECL_CONTEXT (decl);
1156 tree args = NULL_TREE;
1157 int current_depth = 0;
1159 while (current_depth < template_depth)
1161 if (CLASSTYPE_TEMPLATE_INFO (context))
1163 if (current_depth == 0)
1164 args = TYPE_TI_ARGS (context);
1166 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1169 context = TYPE_CONTEXT (context);
1172 if (TREE_CODE (decl) == FUNCTION_DECL)
1177 tree friend_args_type;
1178 tree decl_args_type;
1180 /* Make sure that both DECL and FRIEND_DECL are templates or
1182 is_template = DECL_TEMPLATE_INFO (decl)
1183 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1184 if (need_template ^ is_template)
1186 else if (is_template)
1188 /* If both are templates, check template parameter list. */
1190 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1192 if (!comp_template_parms
1193 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1197 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1200 decl_type = TREE_TYPE (decl);
1202 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1203 tf_none, NULL_TREE);
1204 if (friend_type == error_mark_node)
1207 /* Check if return types match. */
1208 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1211 /* Check if function parameter types match, ignoring the
1212 `this' parameter. */
1213 friend_args_type = TYPE_ARG_TYPES (friend_type);
1214 decl_args_type = TYPE_ARG_TYPES (decl_type);
1215 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1216 friend_args_type = TREE_CHAIN (friend_args_type);
1217 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1218 decl_args_type = TREE_CHAIN (decl_args_type);
1220 return compparms (decl_args_type, friend_args_type);
1224 /* DECL is a TYPE_DECL */
1226 tree decl_type = TREE_TYPE (decl);
1228 /* Make sure that both DECL and FRIEND_DECL are templates or
1231 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1232 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1234 if (need_template ^ is_template)
1236 else if (is_template)
1239 /* If both are templates, check the name of the two
1240 TEMPLATE_DECL's first because is_friend didn't. */
1241 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1242 != DECL_NAME (friend_decl))
1245 /* Now check template parameter list. */
1247 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1249 return comp_template_parms
1250 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1254 return (DECL_NAME (decl)
1255 == DECL_NAME (friend_decl));
1261 /* Register the specialization SPEC as a specialization of TMPL with
1262 the indicated ARGS. IS_FRIEND indicates whether the specialization
1263 is actually just a friend declaration. Returns SPEC, or an
1264 equivalent prior declaration, if available. */
1267 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1271 spec_entry **slot = NULL;
1274 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1276 if (TREE_CODE (spec) == FUNCTION_DECL
1277 && uses_template_parms (DECL_TI_ARGS (spec)))
1278 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1279 register it; we want the corresponding TEMPLATE_DECL instead.
1280 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1281 the more obvious `uses_template_parms (spec)' to avoid problems
1282 with default function arguments. In particular, given
1283 something like this:
1285 template <class T> void f(T t1, T t = T())
1287 the default argument expression is not substituted for in an
1288 instantiation unless and until it is actually needed. */
1291 if (optimize_specialization_lookup_p (tmpl))
1292 /* We don't put these specializations in the hash table, but we might
1293 want to give an error about a mismatch. */
1294 fn = retrieve_specialization (tmpl, args, 0);
1302 hash = hash_specialization (&elt);
1304 slot = (spec_entry **)
1305 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1312 /* We can sometimes try to re-register a specialization that we've
1313 already got. In particular, regenerate_decl_from_template calls
1314 duplicate_decls which will update the specialization list. But,
1315 we'll still get called again here anyhow. It's more convenient
1316 to simply allow this than to try to prevent it. */
1319 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1321 if (DECL_TEMPLATE_INSTANTIATION (fn))
1323 if (DECL_ODR_USED (fn)
1324 || DECL_EXPLICIT_INSTANTIATION (fn))
1326 error ("specialization of %qD after instantiation",
1328 return error_mark_node;
1333 /* This situation should occur only if the first
1334 specialization is an implicit instantiation, the
1335 second is an explicit specialization, and the
1336 implicit instantiation has not yet been used. That
1337 situation can occur if we have implicitly
1338 instantiated a member function and then specialized
1341 We can also wind up here if a friend declaration that
1342 looked like an instantiation turns out to be a
1345 template <class T> void foo(T);
1346 class S { friend void foo<>(int) };
1347 template <> void foo(int);
1349 We transform the existing DECL in place so that any
1350 pointers to it become pointers to the updated
1353 If there was a definition for the template, but not
1354 for the specialization, we want this to look as if
1355 there were no definition, and vice versa. */
1356 DECL_INITIAL (fn) = NULL_TREE;
1357 duplicate_decls (spec, fn, is_friend);
1358 /* The call to duplicate_decls will have applied
1361 An explicit specialization of a function template
1362 is inline only if it is explicitly declared to be,
1363 and independently of whether its function template
1366 to the primary function; now copy the inline bits to
1367 the various clones. */
1368 FOR_EACH_CLONE (clone, fn)
1370 DECL_DECLARED_INLINE_P (clone)
1371 = DECL_DECLARED_INLINE_P (fn);
1372 DECL_SOURCE_LOCATION (clone)
1373 = DECL_SOURCE_LOCATION (fn);
1375 check_specialization_namespace (fn);
1380 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1382 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1383 /* Dup decl failed, but this is a new definition. Set the
1384 line number so any errors match this new
1386 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1392 return duplicate_decls (spec, fn, is_friend);
1394 /* A specialization must be declared in the same namespace as the
1395 template it is specializing. */
1396 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1397 && !check_specialization_namespace (tmpl))
1398 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1400 if (!optimize_specialization_lookup_p (tmpl))
1402 gcc_assert (tmpl && args && spec);
1403 *slot = ggc_alloc_spec_entry ();
1405 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1406 && PRIMARY_TEMPLATE_P (tmpl)
1407 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1408 /* TMPL is a forward declaration of a template function; keep a list
1409 of all specializations in case we need to reassign them to a friend
1410 template later in tsubst_friend_function. */
1411 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1412 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1418 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1419 TMPL and ARGS members, ignores SPEC. */
1422 eq_specializations (const void *p1, const void *p2)
1424 const spec_entry *e1 = (const spec_entry *)p1;
1425 const spec_entry *e2 = (const spec_entry *)p2;
1427 return (e1->tmpl == e2->tmpl
1428 && comp_template_args (e1->args, e2->args));
1431 /* Returns a hash for a template TMPL and template arguments ARGS. */
1434 hash_tmpl_and_args (tree tmpl, tree args)
1436 hashval_t val = DECL_UID (tmpl);
1437 return iterative_hash_template_arg (args, val);
1440 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1444 hash_specialization (const void *p)
1446 const spec_entry *e = (const spec_entry *)p;
1447 return hash_tmpl_and_args (e->tmpl, e->args);
1450 /* Recursively calculate a hash value for a template argument ARG, for use
1451 in the hash tables of template specializations. */
1454 iterative_hash_template_arg (tree arg, hashval_t val)
1456 unsigned HOST_WIDE_INT i;
1457 enum tree_code code;
1460 if (arg == NULL_TREE)
1461 return iterative_hash_object (arg, val);
1466 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1467 /* We can get one of these when re-hashing a previous entry in the middle
1468 of substituting into a pack expansion. Just look through it. */
1469 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1471 code = TREE_CODE (arg);
1472 tclass = TREE_CODE_CLASS (code);
1474 val = iterative_hash_object (code, val);
1481 case IDENTIFIER_NODE:
1482 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1486 int i, len = TREE_VEC_LENGTH (arg);
1487 for (i = 0; i < len; ++i)
1488 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1492 case TYPE_PACK_EXPANSION:
1493 case EXPR_PACK_EXPANSION:
1494 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1496 case TYPE_ARGUMENT_PACK:
1497 case NONTYPE_ARGUMENT_PACK:
1498 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1501 for (; arg; arg = TREE_CHAIN (arg))
1502 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1506 for (; arg; arg = OVL_CHAIN (arg))
1507 val = iterative_hash_template_arg (OVL_FUNCTION (arg), val);
1513 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1515 val = iterative_hash_template_arg (field, val);
1516 val = iterative_hash_template_arg (value, val);
1522 if (!DECL_ARTIFICIAL (arg))
1523 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1524 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1527 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1530 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1531 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1533 case TEMPLATE_PARM_INDEX:
1534 val = iterative_hash_template_arg
1535 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1536 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1537 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1540 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1541 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1542 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1545 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1547 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1551 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1552 code = TREE_CODE (TREE_OPERAND (arg, 1));
1553 val = iterative_hash_object (code, val);
1554 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1557 /* layout_type sets structural equality for arrays of
1558 incomplete type, so we can't rely on the canonical type
1560 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1561 return iterative_hash_template_arg (TYPE_DOMAIN (arg), val);
1564 /* A lambda can't appear in a template arg, but don't crash on
1566 gcc_assert (seen_error ());
1570 case STATIC_CAST_EXPR:
1571 case REINTERPRET_CAST_EXPR:
1572 case CONST_CAST_EXPR:
1573 case DYNAMIC_CAST_EXPR:
1575 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1576 /* Now hash operands as usual. */
1586 if (TYPE_CANONICAL (arg))
1587 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1589 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1590 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1591 /* Otherwise just compare the types during lookup. */
1594 case tcc_declaration:
1596 return iterative_hash_expr (arg, val);
1599 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1601 unsigned n = TREE_OPERAND_LENGTH (arg);
1602 for (i = 0; i < n; ++i)
1603 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1611 /* Unregister the specialization SPEC as a specialization of TMPL.
1612 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1613 if the SPEC was listed as a specialization of TMPL.
1615 Note that SPEC has been ggc_freed, so we can't look inside it. */
1618 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1623 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1624 elt.args = TI_ARGS (tinfo);
1625 elt.spec = NULL_TREE;
1627 slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
1630 gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
1631 gcc_assert (new_spec != NULL_TREE);
1632 (*slot)->spec = new_spec;
1639 /* Compare an entry in the local specializations hash table P1 (which
1640 is really a pointer to a TREE_LIST) with P2 (which is really a
1644 eq_local_specializations (const void *p1, const void *p2)
1646 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1649 /* Hash P1, an entry in the local specializations table. */
1652 hash_local_specialization (const void* p1)
1654 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1657 /* Like register_specialization, but for local declarations. We are
1658 registering SPEC, an instantiation of TMPL. */
1661 register_local_specialization (tree spec, tree tmpl)
1665 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1666 htab_hash_pointer (tmpl), INSERT);
1667 *slot = build_tree_list (spec, tmpl);
1670 /* TYPE is a class type. Returns true if TYPE is an explicitly
1671 specialized class. */
1674 explicit_class_specialization_p (tree type)
1676 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1678 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1681 /* Print the list of functions at FNS, going through all the overloads
1682 for each element of the list. Alternatively, FNS can not be a
1683 TREE_LIST, in which case it will be printed together with all the
1686 MORE and *STR should respectively be FALSE and NULL when the function
1687 is called from the outside. They are used internally on recursive
1688 calls. print_candidates manages the two parameters and leaves NULL
1689 in *STR when it ends. */
1692 print_candidates_1 (tree fns, bool more, const char **str)
1695 char *spaces = NULL;
1697 for (fn = fns; fn; fn = OVL_NEXT (fn))
1698 if (TREE_CODE (fn) == TREE_LIST)
1700 gcc_assert (!OVL_NEXT (fn) && !is_overloaded_fn (fn));
1701 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1702 print_candidates_1 (TREE_VALUE (fn2),
1703 TREE_CHAIN (fn2) || more, str);
1709 /* Pick the prefix string. */
1710 if (!more && !OVL_NEXT (fns))
1712 error ("candidate is: %+#D", OVL_CURRENT (fn));
1716 *str = _("candidates are:");
1717 spaces = get_spaces (*str);
1719 error ("%s %+#D", *str, OVL_CURRENT (fn));
1720 *str = spaces ? spaces : *str;
1730 /* Print the list of candidate FNS in an error message. */
1733 print_candidates (tree fns)
1735 const char *str = NULL;
1736 print_candidates_1 (fns, false, &str);
1737 gcc_assert (str == NULL);
1740 /* Returns the template (one of the functions given by TEMPLATE_ID)
1741 which can be specialized to match the indicated DECL with the
1742 explicit template args given in TEMPLATE_ID. The DECL may be
1743 NULL_TREE if none is available. In that case, the functions in
1744 TEMPLATE_ID are non-members.
1746 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1747 specialization of a member template.
1749 The TEMPLATE_COUNT is the number of references to qualifying
1750 template classes that appeared in the name of the function. See
1751 check_explicit_specialization for a more accurate description.
1753 TSK indicates what kind of template declaration (if any) is being
1754 declared. TSK_TEMPLATE indicates that the declaration given by
1755 DECL, though a FUNCTION_DECL, has template parameters, and is
1756 therefore a template function.
1758 The template args (those explicitly specified and those deduced)
1759 are output in a newly created vector *TARGS_OUT.
1761 If it is impossible to determine the result, an error message is
1762 issued. The error_mark_node is returned to indicate failure. */
1765 determine_specialization (tree template_id,
1768 int need_member_template,
1774 tree explicit_targs;
1775 tree candidates = NULL_TREE;
1776 /* A TREE_LIST of templates of which DECL may be a specialization.
1777 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1778 corresponding TREE_PURPOSE is the set of template arguments that,
1779 when used to instantiate the template, would produce a function
1780 with the signature of DECL. */
1781 tree templates = NULL_TREE;
1783 struct cp_binding_level *b;
1785 *targs_out = NULL_TREE;
1787 if (template_id == error_mark_node || decl == error_mark_node)
1788 return error_mark_node;
1790 fns = TREE_OPERAND (template_id, 0);
1791 explicit_targs = TREE_OPERAND (template_id, 1);
1793 if (fns == error_mark_node)
1794 return error_mark_node;
1796 /* Check for baselinks. */
1797 if (BASELINK_P (fns))
1798 fns = BASELINK_FUNCTIONS (fns);
1800 if (!is_overloaded_fn (fns))
1802 error ("%qD is not a function template", fns);
1803 return error_mark_node;
1806 /* Count the number of template headers specified for this
1809 for (b = current_binding_level;
1810 b->kind == sk_template_parms;
1814 for (; fns; fns = OVL_NEXT (fns))
1816 tree fn = OVL_CURRENT (fns);
1818 if (TREE_CODE (fn) == TEMPLATE_DECL)
1820 tree decl_arg_types;
1823 /* In case of explicit specialization, we need to check if
1824 the number of template headers appearing in the specialization
1825 is correct. This is usually done in check_explicit_specialization,
1826 but the check done there cannot be exhaustive when specializing
1827 member functions. Consider the following code:
1829 template <> void A<int>::f(int);
1830 template <> template <> void A<int>::f(int);
1832 Assuming that A<int> is not itself an explicit specialization
1833 already, the first line specializes "f" which is a non-template
1834 member function, whilst the second line specializes "f" which
1835 is a template member function. So both lines are syntactically
1836 correct, and check_explicit_specialization does not reject
1839 Here, we can do better, as we are matching the specialization
1840 against the declarations. We count the number of template
1841 headers, and we check if they match TEMPLATE_COUNT + 1
1842 (TEMPLATE_COUNT is the number of qualifying template classes,
1843 plus there must be another header for the member template
1846 Notice that if header_count is zero, this is not a
1847 specialization but rather a template instantiation, so there
1848 is no check we can perform here. */
1849 if (header_count && header_count != template_count + 1)
1852 /* Check that the number of template arguments at the
1853 innermost level for DECL is the same as for FN. */
1854 if (current_binding_level->kind == sk_template_parms
1855 && !current_binding_level->explicit_spec_p
1856 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1857 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1858 (current_template_parms))))
1861 /* DECL might be a specialization of FN. */
1862 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1863 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1865 /* For a non-static member function, we need to make sure
1866 that the const qualification is the same. Since
1867 get_bindings does not try to merge the "this" parameter,
1868 we must do the comparison explicitly. */
1869 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1870 && !same_type_p (TREE_VALUE (fn_arg_types),
1871 TREE_VALUE (decl_arg_types)))
1874 /* Skip the "this" parameter and, for constructors of
1875 classes with virtual bases, the VTT parameter. A
1876 full specialization of a constructor will have a VTT
1877 parameter, but a template never will. */
1879 = skip_artificial_parms_for (decl, decl_arg_types);
1881 = skip_artificial_parms_for (fn, fn_arg_types);
1883 /* Check that the number of function parameters matches.
1885 template <class T> void f(int i = 0);
1886 template <> void f<int>();
1887 The specialization f<int> is invalid but is not caught
1888 by get_bindings below. */
1889 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1892 /* Function templates cannot be specializations; there are
1893 no partial specializations of functions. Therefore, if
1894 the type of DECL does not match FN, there is no
1896 if (tsk == tsk_template)
1898 if (compparms (fn_arg_types, decl_arg_types))
1899 candidates = tree_cons (NULL_TREE, fn, candidates);
1903 /* See whether this function might be a specialization of this
1905 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1908 /* We cannot deduce template arguments that when used to
1909 specialize TMPL will produce DECL. */
1912 /* Save this template, and the arguments deduced. */
1913 templates = tree_cons (targs, fn, templates);
1915 else if (need_member_template)
1916 /* FN is an ordinary member function, and we need a
1917 specialization of a member template. */
1919 else if (TREE_CODE (fn) != FUNCTION_DECL)
1920 /* We can get IDENTIFIER_NODEs here in certain erroneous
1923 else if (!DECL_FUNCTION_MEMBER_P (fn))
1924 /* This is just an ordinary non-member function. Nothing can
1925 be a specialization of that. */
1927 else if (DECL_ARTIFICIAL (fn))
1928 /* Cannot specialize functions that are created implicitly. */
1932 tree decl_arg_types;
1934 /* This is an ordinary member function. However, since
1935 we're here, we can assume it's enclosing class is a
1936 template class. For example,
1938 template <typename T> struct S { void f(); };
1939 template <> void S<int>::f() {}
1941 Here, S<int>::f is a non-template, but S<int> is a
1942 template class. If FN has the same type as DECL, we
1943 might be in business. */
1945 if (!DECL_TEMPLATE_INFO (fn))
1946 /* Its enclosing class is an explicit specialization
1947 of a template class. This is not a candidate. */
1950 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1951 TREE_TYPE (TREE_TYPE (fn))))
1952 /* The return types differ. */
1955 /* Adjust the type of DECL in case FN is a static member. */
1956 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1957 if (DECL_STATIC_FUNCTION_P (fn)
1958 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1959 decl_arg_types = TREE_CHAIN (decl_arg_types);
1961 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1964 candidates = tree_cons (NULL_TREE, fn, candidates);
1968 if (templates && TREE_CHAIN (templates))
1974 It is possible for a specialization with a given function
1975 signature to be instantiated from more than one function
1976 template. In such cases, explicit specification of the
1977 template arguments must be used to uniquely identify the
1978 function template specialization being specialized.
1980 Note that here, there's no suggestion that we're supposed to
1981 determine which of the candidate templates is most
1982 specialized. However, we, also have:
1986 Partial ordering of overloaded function template
1987 declarations is used in the following contexts to select
1988 the function template to which a function template
1989 specialization refers:
1991 -- when an explicit specialization refers to a function
1994 So, we do use the partial ordering rules, at least for now.
1995 This extension can only serve to make invalid programs valid,
1996 so it's safe. And, there is strong anecdotal evidence that
1997 the committee intended the partial ordering rules to apply;
1998 the EDG front end has that behavior, and John Spicer claims
1999 that the committee simply forgot to delete the wording in
2000 [temp.expl.spec]. */
2001 tree tmpl = most_specialized_instantiation (templates);
2002 if (tmpl != error_mark_node)
2005 TREE_CHAIN (templates) = NULL_TREE;
2009 if (templates == NULL_TREE && candidates == NULL_TREE)
2011 error ("template-id %qD for %q+D does not match any template "
2012 "declaration", template_id, decl);
2013 if (header_count && header_count != template_count + 1)
2014 inform (input_location, "saw %d %<template<>%>, need %d for "
2015 "specializing a member function template",
2016 header_count, template_count + 1);
2017 return error_mark_node;
2019 else if ((templates && TREE_CHAIN (templates))
2020 || (candidates && TREE_CHAIN (candidates))
2021 || (templates && candidates))
2023 error ("ambiguous template specialization %qD for %q+D",
2025 candidates = chainon (candidates, templates);
2026 print_candidates (candidates);
2027 return error_mark_node;
2030 /* We have one, and exactly one, match. */
2033 tree fn = TREE_VALUE (candidates);
2034 *targs_out = copy_node (DECL_TI_ARGS (fn));
2035 /* DECL is a re-declaration or partial instantiation of a template
2037 if (TREE_CODE (fn) == TEMPLATE_DECL)
2039 /* It was a specialization of an ordinary member function in a
2041 return DECL_TI_TEMPLATE (fn);
2044 /* It was a specialization of a template. */
2045 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2046 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2048 *targs_out = copy_node (targs);
2049 SET_TMPL_ARGS_LEVEL (*targs_out,
2050 TMPL_ARGS_DEPTH (*targs_out),
2051 TREE_PURPOSE (templates));
2054 *targs_out = TREE_PURPOSE (templates);
2055 return TREE_VALUE (templates);
2058 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2059 but with the default argument values filled in from those in the
2063 copy_default_args_to_explicit_spec_1 (tree spec_types,
2066 tree new_spec_types;
2071 if (spec_types == void_list_node)
2072 return void_list_node;
2074 /* Substitute into the rest of the list. */
2076 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2077 TREE_CHAIN (tmpl_types));
2079 /* Add the default argument for this parameter. */
2080 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2081 TREE_VALUE (spec_types),
2085 /* DECL is an explicit specialization. Replicate default arguments
2086 from the template it specializes. (That way, code like:
2088 template <class T> void f(T = 3);
2089 template <> void f(double);
2092 works, as required.) An alternative approach would be to look up
2093 the correct default arguments at the call-site, but this approach
2094 is consistent with how implicit instantiations are handled. */
2097 copy_default_args_to_explicit_spec (tree decl)
2102 tree new_spec_types;
2106 tree object_type = NULL_TREE;
2107 tree in_charge = NULL_TREE;
2108 tree vtt = NULL_TREE;
2110 /* See if there's anything we need to do. */
2111 tmpl = DECL_TI_TEMPLATE (decl);
2112 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2113 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2114 if (TREE_PURPOSE (t))
2119 old_type = TREE_TYPE (decl);
2120 spec_types = TYPE_ARG_TYPES (old_type);
2122 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2124 /* Remove the this pointer, but remember the object's type for
2126 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2127 spec_types = TREE_CHAIN (spec_types);
2128 tmpl_types = TREE_CHAIN (tmpl_types);
2130 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2132 /* DECL may contain more parameters than TMPL due to the extra
2133 in-charge parameter in constructors and destructors. */
2134 in_charge = spec_types;
2135 spec_types = TREE_CHAIN (spec_types);
2137 if (DECL_HAS_VTT_PARM_P (decl))
2140 spec_types = TREE_CHAIN (spec_types);
2144 /* Compute the merged default arguments. */
2146 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2148 /* Compute the new FUNCTION_TYPE. */
2152 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2157 /* Put the in-charge parameter back. */
2158 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2159 TREE_VALUE (in_charge),
2162 new_type = build_method_type_directly (object_type,
2163 TREE_TYPE (old_type),
2167 new_type = build_function_type (TREE_TYPE (old_type),
2169 new_type = cp_build_type_attribute_variant (new_type,
2170 TYPE_ATTRIBUTES (old_type));
2171 new_type = build_exception_variant (new_type,
2172 TYPE_RAISES_EXCEPTIONS (old_type));
2173 TREE_TYPE (decl) = new_type;
2176 /* Check to see if the function just declared, as indicated in
2177 DECLARATOR, and in DECL, is a specialization of a function
2178 template. We may also discover that the declaration is an explicit
2179 instantiation at this point.
2181 Returns DECL, or an equivalent declaration that should be used
2182 instead if all goes well. Issues an error message if something is
2183 amiss. Returns error_mark_node if the error is not easily
2186 FLAGS is a bitmask consisting of the following flags:
2188 2: The function has a definition.
2189 4: The function is a friend.
2191 The TEMPLATE_COUNT is the number of references to qualifying
2192 template classes that appeared in the name of the function. For
2195 template <class T> struct S { void f(); };
2198 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2199 classes are not counted in the TEMPLATE_COUNT, so that in
2201 template <class T> struct S {};
2202 template <> struct S<int> { void f(); }
2203 template <> void S<int>::f();
2205 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2206 invalid; there should be no template <>.)
2208 If the function is a specialization, it is marked as such via
2209 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2210 is set up correctly, and it is added to the list of specializations
2211 for that template. */
2214 check_explicit_specialization (tree declarator,
2219 int have_def = flags & 2;
2220 int is_friend = flags & 4;
2221 int specialization = 0;
2222 int explicit_instantiation = 0;
2223 int member_specialization = 0;
2224 tree ctype = DECL_CLASS_CONTEXT (decl);
2225 tree dname = DECL_NAME (decl);
2230 if (!processing_specialization)
2233 tsk = tsk_excessive_parms;
2236 tsk = current_tmpl_spec_kind (template_count);
2241 if (processing_specialization)
2244 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2246 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2249 /* This could be something like:
2251 template <class T> void f(T);
2252 class S { friend void f<>(int); } */
2256 /* This case handles bogus declarations like template <>
2257 template <class T> void f<int>(); */
2259 error ("template-id %qD in declaration of primary template",
2266 case tsk_invalid_member_spec:
2267 /* The error has already been reported in
2268 check_specialization_scope. */
2269 return error_mark_node;
2271 case tsk_invalid_expl_inst:
2272 error ("template parameter list used in explicit instantiation");
2278 error ("definition provided for explicit instantiation");
2280 explicit_instantiation = 1;
2283 case tsk_excessive_parms:
2284 case tsk_insufficient_parms:
2285 if (tsk == tsk_excessive_parms)
2286 error ("too many template parameter lists in declaration of %qD",
2288 else if (template_header_count)
2289 error("too few template parameter lists in declaration of %qD", decl);
2291 error("explicit specialization of %qD must be introduced by "
2292 "%<template <>%>", decl);
2296 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2298 member_specialization = 1;
2304 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2306 /* This case handles bogus declarations like template <>
2307 template <class T> void f<int>(); */
2309 if (uses_template_parms (declarator))
2310 error ("function template partial specialization %qD "
2311 "is not allowed", declarator);
2313 error ("template-id %qD in declaration of primary template",
2318 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2319 /* This is a specialization of a member template, without
2320 specialization the containing class. Something like:
2322 template <class T> struct S {
2323 template <class U> void f (U);
2325 template <> template <class U> void S<int>::f(U) {}
2327 That's a specialization -- but of the entire template. */
2335 if (specialization || member_specialization)
2337 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2338 for (; t; t = TREE_CHAIN (t))
2339 if (TREE_PURPOSE (t))
2341 permerror (input_location,
2342 "default argument specified in explicit specialization");
2347 if (specialization || member_specialization || explicit_instantiation)
2349 tree tmpl = NULL_TREE;
2350 tree targs = NULL_TREE;
2352 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2353 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2357 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2362 /* If there is no class context, the explicit instantiation
2363 must be at namespace scope. */
2364 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2366 /* Find the namespace binding, using the declaration
2368 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2370 if (fns == error_mark_node || !is_overloaded_fn (fns))
2372 error ("%qD is not a template function", dname);
2373 fns = error_mark_node;
2377 tree fn = OVL_CURRENT (fns);
2378 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2379 CP_DECL_CONTEXT (fn)))
2380 error ("%qD is not declared in %qD",
2381 decl, current_namespace);
2385 declarator = lookup_template_function (fns, NULL_TREE);
2388 if (declarator == error_mark_node)
2389 return error_mark_node;
2391 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2393 if (!explicit_instantiation)
2394 /* A specialization in class scope. This is invalid,
2395 but the error will already have been flagged by
2396 check_specialization_scope. */
2397 return error_mark_node;
2400 /* It's not valid to write an explicit instantiation in
2403 class C { template void f(); }
2405 This case is caught by the parser. However, on
2408 template class C { void f(); };
2410 (which is invalid) we can get here. The error will be
2417 else if (ctype != NULL_TREE
2418 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2421 /* Find the list of functions in ctype that have the same
2422 name as the declared function. */
2423 tree name = TREE_OPERAND (declarator, 0);
2424 tree fns = NULL_TREE;
2427 if (constructor_name_p (name, ctype))
2429 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2431 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2432 : !CLASSTYPE_DESTRUCTORS (ctype))
2434 /* From [temp.expl.spec]:
2436 If such an explicit specialization for the member
2437 of a class template names an implicitly-declared
2438 special member function (clause _special_), the
2439 program is ill-formed.
2441 Similar language is found in [temp.explicit]. */
2442 error ("specialization of implicitly-declared special member function");
2443 return error_mark_node;
2446 name = is_constructor ? ctor_identifier : dtor_identifier;
2449 if (!DECL_CONV_FN_P (decl))
2451 idx = lookup_fnfields_1 (ctype, name);
2453 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2457 VEC(tree,gc) *methods;
2460 /* For a type-conversion operator, we cannot do a
2461 name-based lookup. We might be looking for `operator
2462 int' which will be a specialization of `operator T'.
2463 So, we find *all* the conversion operators, and then
2464 select from them. */
2467 methods = CLASSTYPE_METHOD_VEC (ctype);
2469 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2470 VEC_iterate (tree, methods, idx, ovl);
2473 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2474 /* There are no more conversion functions. */
2477 /* Glue all these conversion functions together
2478 with those we already have. */
2479 for (; ovl; ovl = OVL_NEXT (ovl))
2480 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2484 if (fns == NULL_TREE)
2486 error ("no member function %qD declared in %qT", name, ctype);
2487 return error_mark_node;
2490 TREE_OPERAND (declarator, 0) = fns;
2493 /* Figure out what exactly is being specialized at this point.
2494 Note that for an explicit instantiation, even one for a
2495 member function, we cannot tell apriori whether the
2496 instantiation is for a member template, or just a member
2497 function of a template class. Even if a member template is
2498 being instantiated, the member template arguments may be
2499 elided if they can be deduced from the rest of the
2501 tmpl = determine_specialization (declarator, decl,
2503 member_specialization,
2507 if (!tmpl || tmpl == error_mark_node)
2508 /* We couldn't figure out what this declaration was
2510 return error_mark_node;
2513 tree gen_tmpl = most_general_template (tmpl);
2515 if (explicit_instantiation)
2517 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2518 is done by do_decl_instantiation later. */
2520 int arg_depth = TMPL_ARGS_DEPTH (targs);
2521 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2523 if (arg_depth > parm_depth)
2525 /* If TMPL is not the most general template (for
2526 example, if TMPL is a friend template that is
2527 injected into namespace scope), then there will
2528 be too many levels of TARGS. Remove some of them
2533 new_targs = make_tree_vec (parm_depth);
2534 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2535 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2536 = TREE_VEC_ELT (targs, i);
2540 return instantiate_template (tmpl, targs, tf_error);
2543 /* If we thought that the DECL was a member function, but it
2544 turns out to be specializing a static member function,
2545 make DECL a static member function as well. */
2546 if (DECL_STATIC_FUNCTION_P (tmpl)
2547 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2548 revert_static_member_fn (decl);
2550 /* If this is a specialization of a member template of a
2551 template class, we want to return the TEMPLATE_DECL, not
2552 the specialization of it. */
2553 if (tsk == tsk_template)
2555 tree result = DECL_TEMPLATE_RESULT (tmpl);
2556 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2557 DECL_INITIAL (result) = NULL_TREE;
2561 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2562 DECL_SOURCE_LOCATION (result)
2563 = DECL_SOURCE_LOCATION (decl);
2564 /* We want to use the argument list specified in the
2565 definition, not in the original declaration. */
2566 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2567 for (parm = DECL_ARGUMENTS (result); parm;
2568 parm = DECL_CHAIN (parm))
2569 DECL_CONTEXT (parm) = result;
2571 return register_specialization (tmpl, gen_tmpl, targs,
2575 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2576 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2578 /* Inherit default function arguments from the template
2579 DECL is specializing. */
2580 copy_default_args_to_explicit_spec (decl);
2582 /* This specialization has the same protection as the
2583 template it specializes. */
2584 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2585 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2587 /* 7.1.1-1 [dcl.stc]
2589 A storage-class-specifier shall not be specified in an
2590 explicit specialization...
2592 The parser rejects these, so unless action is taken here,
2593 explicit function specializations will always appear with
2596 The action recommended by the C++ CWG in response to C++
2597 defect report 605 is to make the storage class and linkage
2598 of the explicit specialization match the templated function:
2600 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2602 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2604 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2605 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2607 /* This specialization has the same linkage and visibility as
2608 the function template it specializes. */
2609 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2610 if (! TREE_PUBLIC (decl))
2612 DECL_INTERFACE_KNOWN (decl) = 1;
2613 DECL_NOT_REALLY_EXTERN (decl) = 1;
2615 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2616 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2618 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2619 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2623 /* If DECL is a friend declaration, declared using an
2624 unqualified name, the namespace associated with DECL may
2625 have been set incorrectly. For example, in:
2627 template <typename T> void f(T);
2629 struct S { friend void f<int>(int); }
2632 we will have set the DECL_CONTEXT for the friend
2633 declaration to N, rather than to the global namespace. */
2634 if (DECL_NAMESPACE_SCOPE_P (decl))
2635 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2637 if (is_friend && !have_def)
2638 /* This is not really a declaration of a specialization.
2639 It's just the name of an instantiation. But, it's not
2640 a request for an instantiation, either. */
2641 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2642 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2643 /* This is indeed a specialization. In case of constructors
2644 and destructors, we need in-charge and not-in-charge
2645 versions in V3 ABI. */
2646 clone_function_decl (decl, /*update_method_vec_p=*/0);
2648 /* Register this specialization so that we can find it
2650 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2657 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2658 parameters. These are represented in the same format used for
2659 DECL_TEMPLATE_PARMS. */
2662 comp_template_parms (const_tree parms1, const_tree parms2)
2667 if (parms1 == parms2)
2670 for (p1 = parms1, p2 = parms2;
2671 p1 != NULL_TREE && p2 != NULL_TREE;
2672 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2674 tree t1 = TREE_VALUE (p1);
2675 tree t2 = TREE_VALUE (p2);
2678 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2679 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2681 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2684 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2686 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2687 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2689 /* If either of the template parameters are invalid, assume
2690 they match for the sake of error recovery. */
2691 if (parm1 == error_mark_node || parm2 == error_mark_node)
2694 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2697 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2698 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2699 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2701 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2706 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2707 /* One set of parameters has more parameters lists than the
2714 /* Determine whether PARM is a parameter pack. */
2717 template_parameter_pack_p (const_tree parm)
2719 /* Determine if we have a non-type template parameter pack. */
2720 if (TREE_CODE (parm) == PARM_DECL)
2721 return (DECL_TEMPLATE_PARM_P (parm)
2722 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2724 /* If this is a list of template parameters, we could get a
2725 TYPE_DECL or a TEMPLATE_DECL. */
2726 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2727 parm = TREE_TYPE (parm);
2729 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2730 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2731 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2734 /* Determine if T is a function parameter pack. */
2737 function_parameter_pack_p (const_tree t)
2739 if (t && TREE_CODE (t) == PARM_DECL)
2740 return FUNCTION_PARAMETER_PACK_P (t);
2744 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2745 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2748 get_function_template_decl (const_tree primary_func_tmpl_inst)
2750 if (! primary_func_tmpl_inst
2751 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2752 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2755 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2758 /* Return true iff the function parameter PARAM_DECL was expanded
2759 from the function parameter pack PACK. */
2762 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2764 if (DECL_ARTIFICIAL (param_decl)
2765 || !function_parameter_pack_p (pack))
2768 /* The parameter pack and its pack arguments have the same
2770 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2773 /* Determine whether ARGS describes a variadic template args list,
2774 i.e., one that is terminated by a template argument pack. */
2777 template_args_variadic_p (tree args)
2782 if (args == NULL_TREE)
2785 args = INNERMOST_TEMPLATE_ARGS (args);
2786 nargs = TREE_VEC_LENGTH (args);
2791 last_parm = TREE_VEC_ELT (args, nargs - 1);
2793 return ARGUMENT_PACK_P (last_parm);
2796 /* Generate a new name for the parameter pack name NAME (an
2797 IDENTIFIER_NODE) that incorporates its */
2800 make_ith_pack_parameter_name (tree name, int i)
2802 /* Munge the name to include the parameter index. */
2803 #define NUMBUF_LEN 128
2804 char numbuf[NUMBUF_LEN];
2808 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2809 newname_len = IDENTIFIER_LENGTH (name)
2810 + strlen (numbuf) + 2;
2811 newname = (char*)alloca (newname_len);
2812 snprintf (newname, newname_len,
2813 "%s#%i", IDENTIFIER_POINTER (name), i);
2814 return get_identifier (newname);
2817 /* Return true if T is a primary function
2818 or class template instantiation. */
2821 primary_template_instantiation_p (const_tree t)
2826 if (TREE_CODE (t) == FUNCTION_DECL)
2827 return DECL_LANG_SPECIFIC (t)
2828 && DECL_TEMPLATE_INSTANTIATION (t)
2829 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2830 else if (CLASS_TYPE_P (t))
2831 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2832 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2836 /* Return true if PARM is a template template parameter. */
2839 template_template_parameter_p (const_tree parm)
2841 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2844 /* Return the template parameters of T if T is a
2845 primary template instantiation, NULL otherwise. */
2848 get_primary_template_innermost_parameters (const_tree t)
2850 tree parms = NULL, template_info = NULL;
2852 if ((template_info = get_template_info (t))
2853 && primary_template_instantiation_p (t))
2854 parms = INNERMOST_TEMPLATE_PARMS
2855 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2860 /* Return the template parameters of the LEVELth level from the full list
2861 of template parameters PARMS. */
2864 get_template_parms_at_level (tree parms, int level)
2868 || TREE_CODE (parms) != TREE_LIST
2869 || level > TMPL_PARMS_DEPTH (parms))
2872 for (p = parms; p; p = TREE_CHAIN (p))
2873 if (TMPL_PARMS_DEPTH (p) == level)
2879 /* Returns the template arguments of T if T is a template instantiation,
2883 get_template_innermost_arguments (const_tree t)
2885 tree args = NULL, template_info = NULL;
2887 if ((template_info = get_template_info (t))
2888 && TI_ARGS (template_info))
2889 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2894 /* Return the argument pack elements of T if T is a template argument pack,
2898 get_template_argument_pack_elems (const_tree t)
2900 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2901 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2904 return ARGUMENT_PACK_ARGS (t);
2907 /* Structure used to track the progress of find_parameter_packs_r. */
2908 struct find_parameter_pack_data
2910 /* TREE_LIST that will contain all of the parameter packs found by
2912 tree* parameter_packs;
2914 /* Set of AST nodes that have been visited by the traversal. */
2915 struct pointer_set_t *visited;
2918 /* Identifies all of the argument packs that occur in a template
2919 argument and appends them to the TREE_LIST inside DATA, which is a
2920 find_parameter_pack_data structure. This is a subroutine of
2921 make_pack_expansion and uses_parameter_packs. */
2923 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2926 struct find_parameter_pack_data* ppd =
2927 (struct find_parameter_pack_data*)data;
2928 bool parameter_pack_p = false;
2930 /* Identify whether this is a parameter pack or not. */
2931 switch (TREE_CODE (t))
2933 case TEMPLATE_PARM_INDEX:
2934 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2935 parameter_pack_p = true;
2938 case TEMPLATE_TYPE_PARM:
2939 case TEMPLATE_TEMPLATE_PARM:
2940 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2941 parameter_pack_p = true;
2945 if (FUNCTION_PARAMETER_PACK_P (t))
2947 /* We don't want to walk into the type of a PARM_DECL,
2948 because we don't want to see the type parameter pack. */
2950 parameter_pack_p = true;
2955 /* Not a parameter pack. */
2959 if (parameter_pack_p)
2961 /* Add this parameter pack to the list. */
2962 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2966 cp_walk_tree (&TYPE_CONTEXT (t),
2967 &find_parameter_packs_r, ppd, ppd->visited);
2969 /* This switch statement will return immediately if we don't find a
2971 switch (TREE_CODE (t))
2973 case TEMPLATE_PARM_INDEX:
2976 case BOUND_TEMPLATE_TEMPLATE_PARM:
2977 /* Check the template itself. */
2978 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
2979 &find_parameter_packs_r, ppd, ppd->visited);
2980 /* Check the template arguments. */
2981 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2986 case TEMPLATE_TYPE_PARM:
2987 case TEMPLATE_TEMPLATE_PARM:
2994 if (TYPE_PTRMEMFUNC_P (t))
3000 if (TYPE_TEMPLATE_INFO (t))
3001 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
3002 &find_parameter_packs_r, ppd, ppd->visited);
3008 cp_walk_tree (&TREE_TYPE (t),
3009 &find_parameter_packs_r, ppd, ppd->visited);
3013 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3018 case TYPE_PACK_EXPANSION:
3019 case EXPR_PACK_EXPANSION:
3024 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3029 case IDENTIFIER_NODE:
3030 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3042 /* Determines if the expression or type T uses any parameter packs. */
3044 uses_parameter_packs (tree t)
3046 tree parameter_packs = NULL_TREE;
3047 struct find_parameter_pack_data ppd;
3048 ppd.parameter_packs = ¶meter_packs;
3049 ppd.visited = pointer_set_create ();
3050 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3051 pointer_set_destroy (ppd.visited);
3052 return parameter_packs != NULL_TREE;
3055 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3056 representation a base-class initializer into a parameter pack
3057 expansion. If all goes well, the resulting node will be an
3058 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3061 make_pack_expansion (tree arg)
3064 tree parameter_packs = NULL_TREE;
3065 bool for_types = false;
3066 struct find_parameter_pack_data ppd;
3068 if (!arg || arg == error_mark_node)
3071 if (TREE_CODE (arg) == TREE_LIST)
3073 /* The only time we will see a TREE_LIST here is for a base
3074 class initializer. In this case, the TREE_PURPOSE will be a
3075 _TYPE node (representing the base class expansion we're
3076 initializing) and the TREE_VALUE will be a TREE_LIST
3077 containing the initialization arguments.
3079 The resulting expansion looks somewhat different from most
3080 expansions. Rather than returning just one _EXPANSION, we
3081 return a TREE_LIST whose TREE_PURPOSE is a
3082 TYPE_PACK_EXPANSION containing the bases that will be
3083 initialized. The TREE_VALUE will be identical to the
3084 original TREE_VALUE, which is a list of arguments that will
3085 be passed to each base. We do not introduce any new pack
3086 expansion nodes into the TREE_VALUE (although it is possible
3087 that some already exist), because the TREE_PURPOSE and
3088 TREE_VALUE all need to be expanded together with the same
3089 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3090 resulting TREE_PURPOSE will mention the parameter packs in
3091 both the bases and the arguments to the bases. */
3094 tree parameter_packs = NULL_TREE;
3096 /* Determine which parameter packs will be used by the base
3098 ppd.visited = pointer_set_create ();
3099 ppd.parameter_packs = ¶meter_packs;
3100 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3103 if (parameter_packs == NULL_TREE)
3105 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3106 pointer_set_destroy (ppd.visited);
3107 return error_mark_node;
3110 if (TREE_VALUE (arg) != void_type_node)
3112 /* Collect the sets of parameter packs used in each of the
3113 initialization arguments. */
3114 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3116 /* Determine which parameter packs will be expanded in this
3118 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3123 pointer_set_destroy (ppd.visited);
3125 /* Create the pack expansion type for the base type. */
3126 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3127 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3128 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3130 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3131 they will rarely be compared to anything. */
3132 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3134 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3137 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3140 /* Build the PACK_EXPANSION_* node. */
3142 ? cxx_make_type (TYPE_PACK_EXPANSION)
3143 : make_node (EXPR_PACK_EXPANSION);
3144 SET_PACK_EXPANSION_PATTERN (result, arg);
3145 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3147 /* Propagate type and const-expression information. */
3148 TREE_TYPE (result) = TREE_TYPE (arg);
3149 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3152 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3153 they will rarely be compared to anything. */
3154 SET_TYPE_STRUCTURAL_EQUALITY (result);
3156 /* Determine which parameter packs will be expanded. */
3157 ppd.parameter_packs = ¶meter_packs;
3158 ppd.visited = pointer_set_create ();
3159 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3160 pointer_set_destroy (ppd.visited);
3162 /* Make sure we found some parameter packs. */
3163 if (parameter_packs == NULL_TREE)
3166 error ("expansion pattern %<%T%> contains no argument packs", arg);
3168 error ("expansion pattern %<%E%> contains no argument packs", arg);
3169 return error_mark_node;
3171 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3176 /* Checks T for any "bare" parameter packs, which have not yet been
3177 expanded, and issues an error if any are found. This operation can
3178 only be done on full expressions or types (e.g., an expression
3179 statement, "if" condition, etc.), because we could have expressions like:
3181 foo(f(g(h(args)))...)
3183 where "args" is a parameter pack. check_for_bare_parameter_packs
3184 should not be called for the subexpressions args, h(args),
3185 g(h(args)), or f(g(h(args))), because we would produce erroneous
3188 Returns TRUE and emits an error if there were bare parameter packs,
3189 returns FALSE otherwise. */
3191 check_for_bare_parameter_packs (tree t)
3193 tree parameter_packs = NULL_TREE;
3194 struct find_parameter_pack_data ppd;
3196 if (!processing_template_decl || !t || t == error_mark_node)
3199 if (TREE_CODE (t) == TYPE_DECL)
3202 ppd.parameter_packs = ¶meter_packs;
3203 ppd.visited = pointer_set_create ();
3204 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3205 pointer_set_destroy (ppd.visited);
3207 if (parameter_packs)
3209 error ("parameter packs not expanded with %<...%>:");
3210 while (parameter_packs)
3212 tree pack = TREE_VALUE (parameter_packs);
3213 tree name = NULL_TREE;
3215 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3216 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3217 name = TYPE_NAME (pack);
3218 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3219 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3221 name = DECL_NAME (pack);
3224 inform (input_location, " %qD", name);
3226 inform (input_location, " <anonymous>");
3228 parameter_packs = TREE_CHAIN (parameter_packs);
3237 /* Expand any parameter packs that occur in the template arguments in
3240 expand_template_argument_pack (tree args)
3242 tree result_args = NULL_TREE;
3243 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3244 int num_result_args = -1;
3245 int non_default_args_count = -1;
3247 /* First, determine if we need to expand anything, and the number of
3248 slots we'll need. */
3249 for (in_arg = 0; in_arg < nargs; ++in_arg)
3251 tree arg = TREE_VEC_ELT (args, in_arg);
3252 if (arg == NULL_TREE)
3254 if (ARGUMENT_PACK_P (arg))
3256 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3257 if (num_result_args < 0)
3258 num_result_args = in_arg + num_packed;
3260 num_result_args += num_packed;
3264 if (num_result_args >= 0)
3269 /* If no expansion is necessary, we're done. */
3270 if (num_result_args < 0)
3273 /* Expand arguments. */
3274 result_args = make_tree_vec (num_result_args);
3275 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3276 non_default_args_count =
3277 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3278 for (in_arg = 0; in_arg < nargs; ++in_arg)
3280 tree arg = TREE_VEC_ELT (args, in_arg);
3281 if (ARGUMENT_PACK_P (arg))
3283 tree packed = ARGUMENT_PACK_ARGS (arg);
3284 int i, num_packed = TREE_VEC_LENGTH (packed);
3285 for (i = 0; i < num_packed; ++i, ++out_arg)
3286 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3287 if (non_default_args_count > 0)
3288 non_default_args_count += num_packed;
3292 TREE_VEC_ELT (result_args, out_arg) = arg;
3296 if (non_default_args_count >= 0)
3297 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3301 /* Checks if DECL shadows a template parameter.
3303 [temp.local]: A template-parameter shall not be redeclared within its
3304 scope (including nested scopes).
3306 Emits an error and returns TRUE if the DECL shadows a parameter,
3307 returns FALSE otherwise. */
3310 check_template_shadow (tree decl)
3314 /* If we're not in a template, we can't possibly shadow a template
3316 if (!current_template_parms)
3319 /* Figure out what we're shadowing. */
3320 if (TREE_CODE (decl) == OVERLOAD)
3321 decl = OVL_CURRENT (decl);
3322 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3324 /* If there's no previous binding for this name, we're not shadowing
3325 anything, let alone a template parameter. */
3329 /* If we're not shadowing a template parameter, we're done. Note
3330 that OLDDECL might be an OVERLOAD (or perhaps even an
3331 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3333 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3336 /* We check for decl != olddecl to avoid bogus errors for using a
3337 name inside a class. We check TPFI to avoid duplicate errors for
3338 inline member templates. */
3340 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3343 error ("declaration of %q+#D", decl);
3344 error (" shadows template parm %q+#D", olddecl);
3348 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3349 ORIG_LEVEL, DECL, and TYPE. */
3352 build_template_parm_index (int index,
3358 tree t = make_node (TEMPLATE_PARM_INDEX);
3359 TEMPLATE_PARM_IDX (t) = index;
3360 TEMPLATE_PARM_LEVEL (t) = level;
3361 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3362 TEMPLATE_PARM_DECL (t) = decl;
3363 TREE_TYPE (t) = type;
3364 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3365 TREE_READONLY (t) = TREE_READONLY (decl);
3370 /* Find the canonical type parameter for the given template type
3371 parameter. Returns the canonical type parameter, which may be TYPE
3372 if no such parameter existed. */
3374 canonical_type_parameter (tree type)
3377 int idx = TEMPLATE_TYPE_IDX (type);
3378 if (!canonical_template_parms)
3379 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3381 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3382 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3384 list = VEC_index (tree, canonical_template_parms, idx);
3385 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3386 list = TREE_CHAIN (list);
3389 return TREE_VALUE (list);
3392 VEC_replace(tree, canonical_template_parms, idx,
3393 tree_cons (NULL_TREE, type,
3394 VEC_index (tree, canonical_template_parms, idx)));
3399 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3400 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3401 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3402 new one is created. */
3405 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3406 tsubst_flags_t complain)
3408 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3409 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3410 != TEMPLATE_PARM_LEVEL (index) - levels)
3411 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3413 tree orig_decl = TEMPLATE_PARM_DECL (index);
3416 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3417 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3418 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3419 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3420 DECL_ARTIFICIAL (decl) = 1;
3421 SET_DECL_TEMPLATE_PARM_P (decl);
3423 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3424 TEMPLATE_PARM_LEVEL (index) - levels,
3425 TEMPLATE_PARM_ORIG_LEVEL (index),
3427 TEMPLATE_PARM_DESCENDANTS (index) = t;
3428 TEMPLATE_PARM_PARAMETER_PACK (t)
3429 = TEMPLATE_PARM_PARAMETER_PACK (index);
3431 /* Template template parameters need this. */
3432 if (TREE_CODE (decl) == TEMPLATE_DECL)
3433 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3434 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3438 return TEMPLATE_PARM_DESCENDANTS (index);
3441 /* Process information from new template parameter PARM and append it to the
3442 LIST being built. This new parameter is a non-type parameter iff
3443 IS_NON_TYPE is true. This new parameter is a parameter
3444 pack iff IS_PARAMETER_PACK is true. The location of PARM is in
3448 process_template_parm (tree list, location_t parm_loc, tree parm, bool is_non_type,
3449 bool is_parameter_pack)
3456 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3457 defval = TREE_PURPOSE (parm);
3461 tree p = tree_last (list);
3463 if (p && TREE_VALUE (p) != error_mark_node)
3466 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3467 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3469 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3479 parm = TREE_VALUE (parm);
3481 SET_DECL_TEMPLATE_PARM_P (parm);
3483 if (TREE_TYPE (parm) == error_mark_node)
3485 err_parm_list = build_tree_list (defval, parm);
3486 TREE_VALUE (err_parm_list) = error_mark_node;
3487 return chainon (list, err_parm_list);
3493 The top-level cv-qualifiers on the template-parameter are
3494 ignored when determining its type. */
3495 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3496 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3498 err_parm_list = build_tree_list (defval, parm);
3499 TREE_VALUE (err_parm_list) = error_mark_node;
3500 return chainon (list, err_parm_list);
3503 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3505 /* This template parameter is not a parameter pack, but it
3506 should be. Complain about "bare" parameter packs. */
3507 check_for_bare_parameter_packs (TREE_TYPE (parm));
3509 /* Recover by calling this a parameter pack. */
3510 is_parameter_pack = true;
3514 /* A template parameter is not modifiable. */
3515 TREE_CONSTANT (parm) = 1;
3516 TREE_READONLY (parm) = 1;
3517 decl = build_decl (parm_loc,
3518 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3519 TREE_CONSTANT (decl) = 1;
3520 TREE_READONLY (decl) = 1;
3521 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3522 = build_template_parm_index (idx, processing_template_decl,
3523 processing_template_decl,
3524 decl, TREE_TYPE (parm));
3526 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3527 = is_parameter_pack;
3532 parm = TREE_VALUE (TREE_VALUE (parm));
3534 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3536 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3537 /* This is for distinguishing between real templates and template
3538 template parameters */
3539 TREE_TYPE (parm) = t;
3540 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3545 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3546 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3547 decl = build_decl (parm_loc,
3548 TYPE_DECL, parm, t);
3551 TYPE_NAME (t) = decl;
3552 TYPE_STUB_DECL (t) = decl;
3554 TEMPLATE_TYPE_PARM_INDEX (t)
3555 = build_template_parm_index (idx, processing_template_decl,
3556 processing_template_decl,
3557 decl, TREE_TYPE (parm));
3558 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3559 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3561 DECL_ARTIFICIAL (decl) = 1;
3562 SET_DECL_TEMPLATE_PARM_P (decl);
3564 parm = build_tree_list (defval, parm);
3565 return chainon (list, parm);
3568 /* The end of a template parameter list has been reached. Process the
3569 tree list into a parameter vector, converting each parameter into a more
3570 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3574 end_template_parm_list (tree parms)
3578 tree saved_parmlist = make_tree_vec (list_length (parms));
3580 current_template_parms
3581 = tree_cons (size_int (processing_template_decl),
3582 saved_parmlist, current_template_parms);
3584 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3586 next = TREE_CHAIN (parm);
3587 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3588 TREE_CHAIN (parm) = NULL_TREE;
3589 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
3590 TEMPLATE_TYPE_PARM_SIBLING_PARMS (TREE_TYPE (TREE_VALUE (parm))) =
3591 current_template_parms;
3594 --processing_template_parmlist;
3596 return saved_parmlist;
3599 /* end_template_decl is called after a template declaration is seen. */
3602 end_template_decl (void)
3604 reset_specialization ();
3606 if (! processing_template_decl)
3609 /* This matches the pushlevel in begin_template_parm_list. */
3612 --processing_template_decl;
3613 current_template_parms = TREE_CHAIN (current_template_parms);
3616 /* Within the declaration of a template, return all levels of template
3617 parameters that apply. The template parameters are represented as
3618 a TREE_VEC, in the form documented in cp-tree.h for template
3622 current_template_args (void)
3625 tree args = NULL_TREE;
3626 int length = TMPL_PARMS_DEPTH (current_template_parms);
3629 /* If there is only one level of template parameters, we do not
3630 create a TREE_VEC of TREE_VECs. Instead, we return a single
3631 TREE_VEC containing the arguments. */
3633 args = make_tree_vec (length);
3635 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3637 tree a = copy_node (TREE_VALUE (header));
3640 TREE_TYPE (a) = NULL_TREE;
3641 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3643 tree t = TREE_VEC_ELT (a, i);
3645 /* T will be a list if we are called from within a
3646 begin/end_template_parm_list pair, but a vector directly
3647 if within a begin/end_member_template_processing pair. */
3648 if (TREE_CODE (t) == TREE_LIST)
3652 if (!error_operand_p (t))
3654 if (TREE_CODE (t) == TYPE_DECL
3655 || TREE_CODE (t) == TEMPLATE_DECL)
3659 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3661 /* Turn this argument into a TYPE_ARGUMENT_PACK
3662 with a single element, which expands T. */
3663 tree vec = make_tree_vec (1);
3664 #ifdef ENABLE_CHECKING
3665 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3666 (vec, TREE_VEC_LENGTH (vec));
3668 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3670 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3671 SET_ARGUMENT_PACK_ARGS (t, vec);
3676 t = DECL_INITIAL (t);
3678 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3680 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3681 with a single element, which expands T. */
3682 tree vec = make_tree_vec (1);
3683 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3684 #ifdef ENABLE_CHECKING
3685 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3686 (vec, TREE_VEC_LENGTH (vec));
3688 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3690 t = make_node (NONTYPE_ARGUMENT_PACK);
3691 SET_ARGUMENT_PACK_ARGS (t, vec);
3692 TREE_TYPE (t) = type;
3695 TREE_VEC_ELT (a, i) = t;
3700 #ifdef ENABLE_CHECKING
3701 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3705 TREE_VEC_ELT (args, --l) = a;
3713 /* Update the declared TYPE by doing any lookups which were thought to be
3714 dependent, but are not now that we know the SCOPE of the declarator. */
3717 maybe_update_decl_type (tree orig_type, tree scope)
3719 tree type = orig_type;
3721 if (type == NULL_TREE)
3724 if (TREE_CODE (orig_type) == TYPE_DECL)
3725 type = TREE_TYPE (type);
3727 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3728 && dependent_type_p (type)
3729 /* Don't bother building up the args in this case. */
3730 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3732 /* tsubst in the args corresponding to the template parameters,
3733 including auto if present. Most things will be unchanged, but
3734 make_typename_type and tsubst_qualified_id will resolve
3735 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3736 tree args = current_template_args ();
3737 tree auto_node = type_uses_auto (type);
3741 tree auto_vec = make_tree_vec (1);
3742 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3743 args = add_to_template_args (args, auto_vec);
3745 pushed = push_scope (scope);
3746 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3751 if (type == error_mark_node)
3754 if (TREE_CODE (orig_type) == TYPE_DECL)
3756 if (same_type_p (type, TREE_TYPE (orig_type)))
3759 type = TYPE_NAME (type);
3764 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3765 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3766 a member template. Used by push_template_decl below. */
3769 build_template_decl (tree decl, tree parms, bool member_template_p)
3771 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3772 DECL_TEMPLATE_PARMS (tmpl) = parms;
3773 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3774 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3779 struct template_parm_data
3781 /* The level of the template parameters we are currently
3785 /* The index of the specialization argument we are currently
3789 /* An array whose size is the number of template parameters. The
3790 elements are nonzero if the parameter has been used in any one
3791 of the arguments processed so far. */
3794 /* An array whose size is the number of template arguments. The
3795 elements are nonzero if the argument makes use of template
3796 parameters of this level. */
3797 int* arg_uses_template_parms;
3800 /* Subroutine of push_template_decl used to see if each template
3801 parameter in a partial specialization is used in the explicit
3802 argument list. If T is of the LEVEL given in DATA (which is
3803 treated as a template_parm_data*), then DATA->PARMS is marked
3807 mark_template_parm (tree t, void* data)
3811 struct template_parm_data* tpd = (struct template_parm_data*) data;
3813 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3815 level = TEMPLATE_PARM_LEVEL (t);
3816 idx = TEMPLATE_PARM_IDX (t);
3820 level = TEMPLATE_TYPE_LEVEL (t);
3821 idx = TEMPLATE_TYPE_IDX (t);
3824 if (level == tpd->level)
3826 tpd->parms[idx] = 1;
3827 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3830 /* Return zero so that for_each_template_parm will continue the
3831 traversal of the tree; we want to mark *every* template parm. */
3835 /* Process the partial specialization DECL. */
3838 process_partial_specialization (tree decl)
3840 tree type = TREE_TYPE (decl);
3841 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3842 tree specargs = CLASSTYPE_TI_ARGS (type);
3843 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3844 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3847 int nargs = TREE_VEC_LENGTH (inner_args);
3850 bool did_error_intro = false;
3851 struct template_parm_data tpd;
3852 struct template_parm_data tpd2;
3854 gcc_assert (current_template_parms);
3856 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3857 ntparms = TREE_VEC_LENGTH (inner_parms);
3859 /* We check that each of the template parameters given in the
3860 partial specialization is used in the argument list to the
3861 specialization. For example:
3863 template <class T> struct S;
3864 template <class T> struct S<T*>;
3866 The second declaration is OK because `T*' uses the template
3867 parameter T, whereas
3869 template <class T> struct S<int>;
3871 is no good. Even trickier is:
3882 The S2<T> declaration is actually invalid; it is a
3883 full-specialization. Of course,
3886 struct S2<T (*)(U)>;
3888 or some such would have been OK. */
3889 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3890 tpd.parms = XALLOCAVEC (int, ntparms);
3891 memset (tpd.parms, 0, sizeof (int) * ntparms);
3893 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
3894 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3895 for (i = 0; i < nargs; ++i)
3897 tpd.current_arg = i;
3898 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3899 &mark_template_parm,
3902 /*include_nondeduced_p=*/false);
3904 for (i = 0; i < ntparms; ++i)
3905 if (tpd.parms[i] == 0)
3907 /* One of the template parms was not used in the
3909 if (!did_error_intro)
3911 error ("template parameters not used in partial specialization:");
3912 did_error_intro = true;
3915 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3918 if (did_error_intro)
3919 return error_mark_node;
3921 /* [temp.class.spec]
3923 The argument list of the specialization shall not be identical to
3924 the implicit argument list of the primary template. */
3925 if (comp_template_args
3927 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3929 error ("partial specialization %qT does not specialize any template arguments", type);
3931 /* [temp.class.spec]
3933 A partially specialized non-type argument expression shall not
3934 involve template parameters of the partial specialization except
3935 when the argument expression is a simple identifier.
3937 The type of a template parameter corresponding to a specialized
3938 non-type argument shall not be dependent on a parameter of the
3941 Also, we verify that pack expansions only occur at the
3942 end of the argument list. */
3943 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3945 for (i = 0; i < nargs; ++i)
3947 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3948 tree arg = TREE_VEC_ELT (inner_args, i);
3949 tree packed_args = NULL_TREE;
3952 if (ARGUMENT_PACK_P (arg))
3954 /* Extract the arguments from the argument pack. We'll be
3955 iterating over these in the following loop. */
3956 packed_args = ARGUMENT_PACK_ARGS (arg);
3957 len = TREE_VEC_LENGTH (packed_args);
3960 for (j = 0; j < len; j++)
3963 /* Get the Jth argument in the parameter pack. */
3964 arg = TREE_VEC_ELT (packed_args, j);
3966 if (PACK_EXPANSION_P (arg))
3968 /* Pack expansions must come at the end of the
3970 if ((packed_args && j < len - 1)
3971 || (!packed_args && i < nargs - 1))
3973 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3974 error ("parameter pack argument %qE must be at the "
3975 "end of the template argument list", arg);
3977 error ("parameter pack argument %qT must be at the "
3978 "end of the template argument list", arg);
3982 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3983 /* We only care about the pattern. */
3984 arg = PACK_EXPANSION_PATTERN (arg);
3986 if (/* These first two lines are the `non-type' bit. */
3988 && TREE_CODE (arg) != TEMPLATE_DECL
3989 /* This next line is the `argument expression is not just a
3990 simple identifier' condition and also the `specialized
3991 non-type argument' bit. */
3992 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3994 if ((!packed_args && tpd.arg_uses_template_parms[i])
3995 || (packed_args && uses_template_parms (arg)))
3996 error ("template argument %qE involves template parameter(s)",
4000 /* Look at the corresponding template parameter,
4001 marking which template parameters its type depends
4003 tree type = TREE_TYPE (parm);
4007 /* We haven't yet initialized TPD2. Do so now. */
4008 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4009 /* The number of parameters here is the number in the
4010 main template, which, as checked in the assertion
4012 tpd2.parms = XALLOCAVEC (int, nargs);
4014 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4017 /* Mark the template parameters. But this time, we're
4018 looking for the template parameters of the main
4019 template, not in the specialization. */
4020 tpd2.current_arg = i;
4021 tpd2.arg_uses_template_parms[i] = 0;
4022 memset (tpd2.parms, 0, sizeof (int) * nargs);
4023 for_each_template_parm (type,
4024 &mark_template_parm,
4027 /*include_nondeduced_p=*/false);
4029 if (tpd2.arg_uses_template_parms [i])
4031 /* The type depended on some template parameters.
4032 If they are fully specialized in the
4033 specialization, that's OK. */
4036 for (j = 0; j < nargs; ++j)
4037 if (tpd2.parms[j] != 0
4038 && tpd.arg_uses_template_parms [j])
4041 error_n (input_location, count,
4042 "type %qT of template argument %qE depends "
4043 "on a template parameter",
4044 "type %qT of template argument %qE depends "
4045 "on template parameters",
4054 /* We should only get here once. */
4055 gcc_assert (!COMPLETE_TYPE_P (type));
4057 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4058 = tree_cons (specargs, inner_parms,
4059 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4060 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4062 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4063 inst = TREE_CHAIN (inst))
4065 tree inst_type = TREE_VALUE (inst);
4066 if (COMPLETE_TYPE_P (inst_type)
4067 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4069 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4070 if (spec && TREE_TYPE (spec) == type)
4071 permerror (input_location,
4072 "partial specialization of %qT after instantiation "
4073 "of %qT", type, inst_type);
4080 /* Check that a template declaration's use of default arguments and
4081 parameter packs is not invalid. Here, PARMS are the template
4082 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4083 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4087 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4088 declaration (but not a definition); 1 indicates a declaration, 2
4089 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4090 emitted for extraneous default arguments.
4092 Returns TRUE if there were no errors found, FALSE otherwise. */
4095 check_default_tmpl_args (tree decl, tree parms, int is_primary,
4096 int is_partial, int is_friend_decl)
4099 int last_level_to_check;
4101 bool no_errors = true;
4105 A default template-argument shall not be specified in a
4106 function template declaration or a function template definition, nor
4107 in the template-parameter-list of the definition of a member of a
4110 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4111 /* You can't have a function template declaration in a local
4112 scope, nor you can you define a member of a class template in a
4116 if (current_class_type
4117 && !TYPE_BEING_DEFINED (current_class_type)
4118 && DECL_LANG_SPECIFIC (decl)
4119 && DECL_DECLARES_FUNCTION_P (decl)
4120 /* If this is either a friend defined in the scope of the class
4121 or a member function. */
4122 && (DECL_FUNCTION_MEMBER_P (decl)
4123 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4124 : DECL_FRIEND_CONTEXT (decl)
4125 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4127 /* And, if it was a member function, it really was defined in
4128 the scope of the class. */
4129 && (!DECL_FUNCTION_MEMBER_P (decl)
4130 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4131 /* We already checked these parameters when the template was
4132 declared, so there's no need to do it again now. This function
4133 was defined in class scope, but we're processing it's body now
4134 that the class is complete. */
4137 /* Core issue 226 (C++0x only): the following only applies to class
4139 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
4143 If a template-parameter has a default template-argument, all
4144 subsequent template-parameters shall have a default
4145 template-argument supplied. */
4146 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4148 tree inner_parms = TREE_VALUE (parm_level);
4149 int ntparms = TREE_VEC_LENGTH (inner_parms);
4150 int seen_def_arg_p = 0;
4153 for (i = 0; i < ntparms; ++i)
4155 tree parm = TREE_VEC_ELT (inner_parms, i);
4157 if (parm == error_mark_node)
4160 if (TREE_PURPOSE (parm))
4162 else if (seen_def_arg_p
4163 && !template_parameter_pack_p (TREE_VALUE (parm)))
4165 error ("no default argument for %qD", TREE_VALUE (parm));
4166 /* For better subsequent error-recovery, we indicate that
4167 there should have been a default argument. */
4168 TREE_PURPOSE (parm) = error_mark_node;
4174 /* Don't complain about an enclosing partial
4176 && parm_level == parms
4177 && TREE_CODE (decl) == TYPE_DECL
4179 && template_parameter_pack_p (TREE_VALUE (parm)))
4181 /* A primary class template can only have one
4182 parameter pack, at the end of the template
4185 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4186 error ("parameter pack %qE must be at the end of the"
4187 " template parameter list", TREE_VALUE (parm));
4189 error ("parameter pack %qT must be at the end of the"
4190 " template parameter list",
4191 TREE_TYPE (TREE_VALUE (parm)));
4193 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4201 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4205 /* For an ordinary class template, default template arguments are
4206 allowed at the innermost level, e.g.:
4207 template <class T = int>
4209 but, in a partial specialization, they're not allowed even
4210 there, as we have in [temp.class.spec]:
4212 The template parameter list of a specialization shall not
4213 contain default template argument values.
4215 So, for a partial specialization, or for a function template
4216 (in C++98/C++03), we look at all of them. */
4219 /* But, for a primary class template that is not a partial
4220 specialization we look at all template parameters except the
4222 parms = TREE_CHAIN (parms);
4224 /* Figure out what error message to issue. */
4225 if (is_friend_decl == 2)
4226 msg = G_("default template arguments may not be used in function template "
4227 "friend re-declaration");
4228 else if (is_friend_decl)
4229 msg = G_("default template arguments may not be used in function template "
4230 "friend declarations");
4231 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4232 msg = G_("default template arguments may not be used in function templates "
4233 "without -std=c++0x or -std=gnu++0x");
4234 else if (is_partial)
4235 msg = G_("default template arguments may not be used in "
4236 "partial specializations");
4238 msg = G_("default argument for template parameter for class enclosing %qD");
4240 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4241 /* If we're inside a class definition, there's no need to
4242 examine the parameters to the class itself. On the one
4243 hand, they will be checked when the class is defined, and,
4244 on the other, default arguments are valid in things like:
4245 template <class T = double>
4246 struct S { template <class U> void f(U); };
4247 Here the default argument for `S' has no bearing on the
4248 declaration of `f'. */
4249 last_level_to_check = template_class_depth (current_class_type) + 1;
4251 /* Check everything. */
4252 last_level_to_check = 0;
4254 for (parm_level = parms;
4255 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4256 parm_level = TREE_CHAIN (parm_level))
4258 tree inner_parms = TREE_VALUE (parm_level);
4262 ntparms = TREE_VEC_LENGTH (inner_parms);
4263 for (i = 0; i < ntparms; ++i)
4265 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4268 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4273 if (is_friend_decl == 2)
4280 /* Clear out the default argument so that we are not
4282 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4286 /* At this point, if we're still interested in issuing messages,
4287 they must apply to classes surrounding the object declared. */
4289 msg = G_("default argument for template parameter for class "
4296 /* Worker for push_template_decl_real, called via
4297 for_each_template_parm. DATA is really an int, indicating the
4298 level of the parameters we are interested in. If T is a template
4299 parameter of that level, return nonzero. */
4302 template_parm_this_level_p (tree t, void* data)
4304 int this_level = *(int *)data;
4307 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4308 level = TEMPLATE_PARM_LEVEL (t);
4310 level = TEMPLATE_TYPE_LEVEL (t);
4311 return level == this_level;
4314 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4315 parameters given by current_template_args, or reuses a
4316 previously existing one, if appropriate. Returns the DECL, or an
4317 equivalent one, if it is replaced via a call to duplicate_decls.
4319 If IS_FRIEND is true, DECL is a friend declaration. */
4322 push_template_decl_real (tree decl, bool is_friend)
4330 int new_template_p = 0;
4331 /* True if the template is a member template, in the sense of
4333 bool member_template_p = false;
4335 if (decl == error_mark_node || !current_template_parms)
4336 return error_mark_node;
4338 /* See if this is a partial specialization. */
4339 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4340 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4341 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4343 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4347 /* For a friend, we want the context of the friend function, not
4348 the type of which it is a friend. */
4349 ctx = CP_DECL_CONTEXT (decl);
4350 else if (CP_DECL_CONTEXT (decl)
4351 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4352 /* In the case of a virtual function, we want the class in which
4354 ctx = CP_DECL_CONTEXT (decl);
4356 /* Otherwise, if we're currently defining some class, the DECL
4357 is assumed to be a member of the class. */
4358 ctx = current_scope ();
4360 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4363 if (!DECL_CONTEXT (decl))
4364 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4366 /* See if this is a primary template. */
4367 if (is_friend && ctx)
4368 /* A friend template that specifies a class context, i.e.
4369 template <typename T> friend void A<T>::f();
4373 primary = template_parm_scope_p ();
4377 if (DECL_CLASS_SCOPE_P (decl))
4378 member_template_p = true;
4379 if (TREE_CODE (decl) == TYPE_DECL
4380 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4382 error ("template class without a name");
4383 return error_mark_node;
4385 else if (TREE_CODE (decl) == FUNCTION_DECL)
4387 if (DECL_DESTRUCTOR_P (decl))
4391 A destructor shall not be a member template. */
4392 error ("destructor %qD declared as member template", decl);
4393 return error_mark_node;
4395 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4396 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
4397 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4398 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4399 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4400 == void_list_node)))
4402 /* [basic.stc.dynamic.allocation]
4404 An allocation function can be a function
4405 template. ... Template allocation functions shall
4406 have two or more parameters. */
4407 error ("invalid template declaration of %qD", decl);
4408 return error_mark_node;
4411 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4412 && CLASS_TYPE_P (TREE_TYPE (decl)))
4416 error ("template declaration of %q#D", decl);
4417 return error_mark_node;
4421 /* Check to see that the rules regarding the use of default
4422 arguments are not being violated. */
4423 check_default_tmpl_args (decl, current_template_parms,
4424 primary, is_partial, /*is_friend_decl=*/0);
4426 /* Ensure that there are no parameter packs in the type of this
4427 declaration that have not been expanded. */
4428 if (TREE_CODE (decl) == FUNCTION_DECL)
4430 /* Check each of the arguments individually to see if there are
4431 any bare parameter packs. */
4432 tree type = TREE_TYPE (decl);
4433 tree arg = DECL_ARGUMENTS (decl);
4434 tree argtype = TYPE_ARG_TYPES (type);
4436 while (arg && argtype)
4438 if (!FUNCTION_PARAMETER_PACK_P (arg)
4439 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4441 /* This is a PARM_DECL that contains unexpanded parameter
4442 packs. We have already complained about this in the
4443 check_for_bare_parameter_packs call, so just replace
4444 these types with ERROR_MARK_NODE. */
4445 TREE_TYPE (arg) = error_mark_node;
4446 TREE_VALUE (argtype) = error_mark_node;
4449 arg = DECL_CHAIN (arg);
4450 argtype = TREE_CHAIN (argtype);
4453 /* Check for bare parameter packs in the return type and the
4454 exception specifiers. */
4455 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4456 /* Errors were already issued, set return type to int
4457 as the frontend doesn't expect error_mark_node as
4459 TREE_TYPE (type) = integer_type_node;
4460 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4461 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4463 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
4465 TREE_TYPE (decl) = error_mark_node;
4466 return error_mark_node;
4470 return process_partial_specialization (decl);
4472 args = current_template_args ();
4475 || TREE_CODE (ctx) == FUNCTION_DECL
4476 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4477 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4479 if (DECL_LANG_SPECIFIC (decl)
4480 && DECL_TEMPLATE_INFO (decl)
4481 && DECL_TI_TEMPLATE (decl))
4482 tmpl = DECL_TI_TEMPLATE (decl);
4483 /* If DECL is a TYPE_DECL for a class-template, then there won't
4484 be DECL_LANG_SPECIFIC. The information equivalent to
4485 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4486 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4487 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4488 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4490 /* Since a template declaration already existed for this
4491 class-type, we must be redeclaring it here. Make sure
4492 that the redeclaration is valid. */
4493 redeclare_class_template (TREE_TYPE (decl),
4494 current_template_parms);
4495 /* We don't need to create a new TEMPLATE_DECL; just use the
4496 one we already had. */
4497 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4501 tmpl = build_template_decl (decl, current_template_parms,
4505 if (DECL_LANG_SPECIFIC (decl)
4506 && DECL_TEMPLATE_SPECIALIZATION (decl))
4508 /* A specialization of a member template of a template
4510 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4511 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4512 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4518 tree a, t, current, parms;
4520 tree tinfo = get_template_info (decl);
4524 error ("template definition of non-template %q#D", decl);
4525 return error_mark_node;
4528 tmpl = TI_TEMPLATE (tinfo);
4530 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4531 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4532 && DECL_TEMPLATE_SPECIALIZATION (decl)
4533 && DECL_MEMBER_TEMPLATE_P (tmpl))
4537 /* The declaration is a specialization of a member
4538 template, declared outside the class. Therefore, the
4539 innermost template arguments will be NULL, so we
4540 replace them with the arguments determined by the
4541 earlier call to check_explicit_specialization. */
4542 args = DECL_TI_ARGS (decl);
4545 = build_template_decl (decl, current_template_parms,
4547 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4548 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4549 DECL_TI_TEMPLATE (decl) = new_tmpl;
4550 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4551 DECL_TEMPLATE_INFO (new_tmpl)
4552 = build_template_info (tmpl, args);
4554 register_specialization (new_tmpl,
4555 most_general_template (tmpl),
4561 /* Make sure the template headers we got make sense. */
4563 parms = DECL_TEMPLATE_PARMS (tmpl);
4564 i = TMPL_PARMS_DEPTH (parms);
4565 if (TMPL_ARGS_DEPTH (args) != i)
4567 error ("expected %d levels of template parms for %q#D, got %d",
4568 i, decl, TMPL_ARGS_DEPTH (args));
4571 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4573 a = TMPL_ARGS_LEVEL (args, i);
4574 t = INNERMOST_TEMPLATE_PARMS (parms);
4576 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4578 if (current == decl)
4579 error ("got %d template parameters for %q#D",
4580 TREE_VEC_LENGTH (a), decl);
4582 error ("got %d template parameters for %q#T",
4583 TREE_VEC_LENGTH (a), current);
4584 error (" but %d required", TREE_VEC_LENGTH (t));
4585 return error_mark_node;
4588 if (current == decl)
4590 else if (current == NULL_TREE)
4591 /* Can happen in erroneous input. */
4594 current = (TYPE_P (current)
4595 ? TYPE_CONTEXT (current)
4596 : DECL_CONTEXT (current));
4599 /* Check that the parms are used in the appropriate qualifying scopes
4600 in the declarator. */
4601 if (!comp_template_args
4603 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4606 template arguments to %qD do not match original template %qD",
4607 decl, DECL_TEMPLATE_RESULT (tmpl));
4608 if (!uses_template_parms (TI_ARGS (tinfo)))
4609 inform (input_location, "use template<> for an explicit specialization");
4610 /* Avoid crash in import_export_decl. */
4611 DECL_INTERFACE_KNOWN (decl) = 1;
4612 return error_mark_node;
4616 DECL_TEMPLATE_RESULT (tmpl) = decl;
4617 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4619 /* Push template declarations for global functions and types. Note
4620 that we do not try to push a global template friend declared in a
4621 template class; such a thing may well depend on the template
4622 parameters of the class. */
4623 if (new_template_p && !ctx
4624 && !(is_friend && template_class_depth (current_class_type) > 0))
4626 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4627 if (tmpl == error_mark_node)
4628 return error_mark_node;
4630 /* Hide template friend classes that haven't been declared yet. */
4631 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4633 DECL_ANTICIPATED (tmpl) = 1;
4634 DECL_FRIEND_P (tmpl) = 1;
4640 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4643 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4644 if (DECL_CONV_FN_P (tmpl))
4646 int depth = TMPL_PARMS_DEPTH (parms);
4648 /* It is a conversion operator. See if the type converted to
4649 depends on innermost template operands. */
4651 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4653 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4656 /* Give template template parms a DECL_CONTEXT of the template
4657 for which they are a parameter. */
4658 parms = INNERMOST_TEMPLATE_PARMS (parms);
4659 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4661 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4662 if (TREE_CODE (parm) == TEMPLATE_DECL)
4663 DECL_CONTEXT (parm) = tmpl;
4667 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4668 back to its most general template. If TMPL is a specialization,
4669 ARGS may only have the innermost set of arguments. Add the missing
4670 argument levels if necessary. */
4671 if (DECL_TEMPLATE_INFO (tmpl))
4672 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4674 info = build_template_info (tmpl, args);
4676 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4677 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4678 else if (DECL_LANG_SPECIFIC (decl))
4679 DECL_TEMPLATE_INFO (decl) = info;
4681 return DECL_TEMPLATE_RESULT (tmpl);
4685 push_template_decl (tree decl)
4687 return push_template_decl_real (decl, false);
4690 /* Called when a class template TYPE is redeclared with the indicated
4691 template PARMS, e.g.:
4693 template <class T> struct S;
4694 template <class T> struct S {}; */
4697 redeclare_class_template (tree type, tree parms)
4703 if (!TYPE_TEMPLATE_INFO (type))
4705 error ("%qT is not a template type", type);
4709 tmpl = TYPE_TI_TEMPLATE (type);
4710 if (!PRIMARY_TEMPLATE_P (tmpl))
4711 /* The type is nested in some template class. Nothing to worry
4712 about here; there are no new template parameters for the nested
4718 error ("template specifiers not specified in declaration of %qD",
4723 parms = INNERMOST_TEMPLATE_PARMS (parms);
4724 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4726 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4728 error_n (input_location, TREE_VEC_LENGTH (parms),
4729 "redeclared with %d template parameter",
4730 "redeclared with %d template parameters",
4731 TREE_VEC_LENGTH (parms));
4732 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
4733 "previous declaration %q+D used %d template parameter",
4734 "previous declaration %q+D used %d template parameters",
4735 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4739 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4746 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4747 || TREE_VEC_ELT (parms, i) == error_mark_node)
4750 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4751 if (tmpl_parm == error_mark_node)
4754 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4755 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4756 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4758 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4760 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4761 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4762 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4763 || (TREE_CODE (tmpl_parm) != PARM_DECL
4764 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4765 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4766 || (TREE_CODE (tmpl_parm) == PARM_DECL
4767 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4768 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
4770 error ("template parameter %q+#D", tmpl_parm);
4771 error ("redeclared here as %q#D", parm);
4775 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4777 /* We have in [temp.param]:
4779 A template-parameter may not be given default arguments
4780 by two different declarations in the same scope. */
4781 error_at (input_location, "redefinition of default argument for %q#D", parm);
4782 inform (DECL_SOURCE_LOCATION (tmpl_parm),
4783 "original definition appeared here");
4787 if (parm_default != NULL_TREE)
4788 /* Update the previous template parameters (which are the ones
4789 that will really count) with the new default value. */
4790 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4791 else if (tmpl_default != NULL_TREE)
4792 /* Update the new parameters, too; they'll be used as the
4793 parameters for any members. */
4794 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4800 /* Simplify EXPR if it is a non-dependent expression. Returns the
4801 (possibly simplified) expression. */
4804 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
4806 if (expr == NULL_TREE)
4809 /* If we're in a template, but EXPR isn't value dependent, simplify
4810 it. We're supposed to treat:
4812 template <typename T> void f(T[1 + 1]);
4813 template <typename T> void f(T[2]);
4815 as two declarations of the same function, for example. */
4816 if (processing_template_decl
4817 && !type_dependent_expression_p (expr)
4818 && !value_dependent_expression_p (expr))
4820 HOST_WIDE_INT saved_processing_template_decl;
4822 saved_processing_template_decl = processing_template_decl;
4823 processing_template_decl = 0;
4824 expr = tsubst_copy_and_build (expr,
4827 /*in_decl=*/NULL_TREE,
4828 /*function_p=*/false,
4829 /*integral_constant_expression_p=*/true);
4830 processing_template_decl = saved_processing_template_decl;
4836 fold_non_dependent_expr (tree expr)
4838 return fold_non_dependent_expr_sfinae (expr, tf_error);
4841 /* EXPR is an expression which is used in a constant-expression context.
4842 For instance, it could be a VAR_DECL with a constant initializer.
4843 Extract the innermost constant expression.
4845 This is basically a more powerful version of
4846 integral_constant_value, which can be used also in templates where
4847 initializers can maintain a syntactic rather than semantic form
4848 (even if they are non-dependent, for access-checking purposes). */
4851 fold_decl_constant_value (tree expr)
4853 tree const_expr = expr;
4856 expr = fold_non_dependent_expr (const_expr);
4857 const_expr = integral_constant_value (expr);
4859 while (expr != const_expr);
4864 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4865 must be a function or a pointer-to-function type, as specified
4866 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4867 and check that the resulting function has external linkage. */
4870 convert_nontype_argument_function (tree type, tree expr)
4875 fn = instantiate_type (type, fns, tf_none);
4876 if (fn == error_mark_node)
4877 return error_mark_node;
4880 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4881 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4882 if (TREE_CODE (fn_no_ptr) == BASELINK)
4883 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4885 /* [temp.arg.nontype]/1
4887 A template-argument for a non-type, non-template template-parameter
4890 -- the address of an object or function with external linkage. */
4891 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4893 error ("%qE is not a valid template argument for type %qT "
4894 "because function %qD has not external linkage",
4895 expr, type, fn_no_ptr);
4902 /* Subroutine of convert_nontype_argument.
4903 Check if EXPR of type TYPE is a valid pointer-to-member constant.
4904 Emit an error otherwise. */
4907 check_valid_ptrmem_cst_expr (tree type, tree expr)
4910 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
4912 error ("%qE is not a valid template argument for type %qT",
4914 error ("it must be a pointer-to-member of the form `&X::Y'");
4918 /* Returns TRUE iff the address of OP is value-dependent.
4920 14.6.2.4 [temp.dep.temp]:
4921 A non-integral non-type template-argument is dependent if its type is
4922 dependent or it has either of the following forms
4925 and contains a nested-name-specifier which specifies a class-name that
4926 names a dependent type.
4928 We generalize this to just say that the address of a member of a
4929 dependent class is value-dependent; the above doesn't cover the
4930 address of a static data member named with an unqualified-id. */
4933 has_value_dependent_address (tree op)
4935 /* We could use get_inner_reference here, but there's no need;
4936 this is only relevant for template non-type arguments, which
4937 can only be expressed as &id-expression. */
4940 tree ctx = CP_DECL_CONTEXT (op);
4941 if (TYPE_P (ctx) && dependent_type_p (ctx))
4948 /* Attempt to convert the non-type template parameter EXPR to the
4949 indicated TYPE. If the conversion is successful, return the
4950 converted value. If the conversion is unsuccessful, return
4951 NULL_TREE if we issued an error message, or error_mark_node if we
4952 did not. We issue error messages for out-and-out bad template
4953 parameters, but not simply because the conversion failed, since we
4954 might be just trying to do argument deduction. Both TYPE and EXPR
4955 must be non-dependent.
4957 The conversion follows the special rules described in
4958 [temp.arg.nontype], and it is much more strict than an implicit
4961 This function is called twice for each template argument (see
4962 lookup_template_class for a more accurate description of this
4963 problem). This means that we need to handle expressions which
4964 are not valid in a C++ source, but can be created from the
4965 first call (for instance, casts to perform conversions). These
4966 hacks can go away after we fix the double coercion problem. */
4969 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
4973 /* Detect immediately string literals as invalid non-type argument.
4974 This special-case is not needed for correctness (we would easily
4975 catch this later), but only to provide better diagnostic for this
4976 common user mistake. As suggested by DR 100, we do not mention
4977 linkage issues in the diagnostic as this is not the point. */
4978 /* FIXME we're making this OK. */
4979 if (TREE_CODE (expr) == STRING_CST)
4981 if (complain & tf_error)
4982 error ("%qE is not a valid template argument for type %qT "
4983 "because string literals can never be used in this context",
4988 /* Add the ADDR_EXPR now for the benefit of
4989 value_dependent_expression_p. */
4990 if (TYPE_PTROBV_P (type))
4991 expr = decay_conversion (expr);
4993 /* If we are in a template, EXPR may be non-dependent, but still
4994 have a syntactic, rather than semantic, form. For example, EXPR
4995 might be a SCOPE_REF, rather than the VAR_DECL to which the
4996 SCOPE_REF refers. Preserving the qualifying scope is necessary
4997 so that access checking can be performed when the template is
4998 instantiated -- but here we need the resolved form so that we can
4999 convert the argument. */
5000 if (TYPE_REF_OBJ_P (type)
5001 && has_value_dependent_address (expr))
5002 /* If we want the address and it's value-dependent, don't fold. */;
5003 else if (!type_unknown_p (expr))
5004 expr = fold_non_dependent_expr_sfinae (expr, complain);
5005 if (error_operand_p (expr))
5006 return error_mark_node;
5007 expr_type = TREE_TYPE (expr);
5008 if (TREE_CODE (type) == REFERENCE_TYPE)
5009 expr = mark_lvalue_use (expr);
5011 expr = mark_rvalue_use (expr);
5013 /* HACK: Due to double coercion, we can get a
5014 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5015 which is the tree that we built on the first call (see
5016 below when coercing to reference to object or to reference to
5017 function). We just strip everything and get to the arg.
5018 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5020 if (TREE_CODE (expr) == NOP_EXPR)
5022 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5024 /* ??? Maybe we could use convert_from_reference here, but we
5025 would need to relax its constraints because the NOP_EXPR
5026 could actually change the type to something more cv-qualified,
5027 and this is not folded by convert_from_reference. */
5028 tree addr = TREE_OPERAND (expr, 0);
5029 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
5030 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
5031 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
5032 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5033 (TREE_TYPE (expr_type),
5034 TREE_TYPE (TREE_TYPE (addr))));
5036 expr = TREE_OPERAND (addr, 0);
5037 expr_type = TREE_TYPE (expr);
5040 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5041 parameter is a pointer to object, through decay and
5042 qualification conversion. Let's strip everything. */
5043 else if (TYPE_PTROBV_P (type))
5046 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
5047 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
5048 /* Skip the ADDR_EXPR only if it is part of the decay for
5049 an array. Otherwise, it is part of the original argument
5050 in the source code. */
5051 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
5052 expr = TREE_OPERAND (expr, 0);
5053 expr_type = TREE_TYPE (expr);
5057 /* [temp.arg.nontype]/5, bullet 1
5059 For a non-type template-parameter of integral or enumeration type,
5060 integral promotions (_conv.prom_) and integral conversions
5061 (_conv.integral_) are applied. */
5062 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5064 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type))
5065 return error_mark_node;
5067 expr = fold_decl_constant_value (expr);
5068 /* Notice that there are constant expressions like '4 % 0' which
5069 do not fold into integer constants. */
5070 if (TREE_CODE (expr) != INTEGER_CST)
5072 if (complain & tf_error)
5073 error ("%qE is not a valid template argument for type %qT "
5074 "because it is a non-constant expression", expr, type);
5078 /* At this point, an implicit conversion does what we want,
5079 because we already know that the expression is of integral
5081 expr = perform_implicit_conversion (type, expr, complain);
5082 if (expr == error_mark_node)
5083 return error_mark_node;
5085 /* Conversion was allowed: fold it to a bare integer constant. */
5088 /* [temp.arg.nontype]/5, bullet 2
5090 For a non-type template-parameter of type pointer to object,
5091 qualification conversions (_conv.qual_) and the array-to-pointer
5092 conversion (_conv.array_) are applied. */
5093 else if (TYPE_PTROBV_P (type))
5095 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5097 A template-argument for a non-type, non-template template-parameter
5098 shall be one of: [...]
5100 -- the name of a non-type template-parameter;
5101 -- the address of an object or function with external linkage, [...]
5102 expressed as "& id-expression" where the & is optional if the name
5103 refers to a function or array, or if the corresponding
5104 template-parameter is a reference.
5106 Here, we do not care about functions, as they are invalid anyway
5107 for a parameter of type pointer-to-object. */
5109 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5110 /* Non-type template parameters are OK. */
5112 else if (TREE_CODE (expr) != ADDR_EXPR
5113 && TREE_CODE (expr_type) != ARRAY_TYPE)
5115 if (TREE_CODE (expr) == VAR_DECL)
5117 error ("%qD is not a valid template argument "
5118 "because %qD is a variable, not the address of "
5123 /* Other values, like integer constants, might be valid
5124 non-type arguments of some other type. */
5125 return error_mark_node;
5131 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5132 ? TREE_OPERAND (expr, 0) : expr);
5133 if (TREE_CODE (decl) != VAR_DECL)
5135 error ("%qE is not a valid template argument of type %qT "
5136 "because %qE is not a variable",
5140 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
5142 error ("%qE is not a valid template argument of type %qT "
5143 "because %qD does not have external linkage",
5149 expr = decay_conversion (expr);
5150 if (expr == error_mark_node)
5151 return error_mark_node;
5153 expr = perform_qualification_conversions (type, expr);
5154 if (expr == error_mark_node)
5155 return error_mark_node;
5157 /* [temp.arg.nontype]/5, bullet 3
5159 For a non-type template-parameter of type reference to object, no
5160 conversions apply. The type referred to by the reference may be more
5161 cv-qualified than the (otherwise identical) type of the
5162 template-argument. The template-parameter is bound directly to the
5163 template-argument, which must be an lvalue. */
5164 else if (TYPE_REF_OBJ_P (type))
5166 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5168 return error_mark_node;
5170 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5172 error ("%qE is not a valid template argument for type %qT "
5173 "because of conflicts in cv-qualification", expr, type);
5177 if (!real_lvalue_p (expr))
5179 error ("%qE is not a valid template argument for type %qT "
5180 "because it is not an lvalue", expr, type);
5184 /* [temp.arg.nontype]/1
5186 A template-argument for a non-type, non-template template-parameter
5187 shall be one of: [...]
5189 -- the address of an object or function with external linkage. */
5190 if (TREE_CODE (expr) == INDIRECT_REF
5191 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5193 expr = TREE_OPERAND (expr, 0);
5196 error ("%q#D is not a valid template argument for type %qT "
5197 "because a reference variable does not have a constant "
5198 "address", expr, type);
5205 error ("%qE is not a valid template argument for type %qT "
5206 "because it is not an object with external linkage",
5211 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5213 error ("%qE is not a valid template argument for type %qT "
5214 "because object %qD has not external linkage",
5219 expr = build_nop (type, build_address (expr));
5221 /* [temp.arg.nontype]/5, bullet 4
5223 For a non-type template-parameter of type pointer to function, only
5224 the function-to-pointer conversion (_conv.func_) is applied. If the
5225 template-argument represents a set of overloaded functions (or a
5226 pointer to such), the matching function is selected from the set
5228 else if (TYPE_PTRFN_P (type))
5230 /* If the argument is a template-id, we might not have enough
5231 context information to decay the pointer. */
5232 if (!type_unknown_p (expr_type))
5234 expr = decay_conversion (expr);
5235 if (expr == error_mark_node)
5236 return error_mark_node;
5239 expr = convert_nontype_argument_function (type, expr);
5240 if (!expr || expr == error_mark_node)
5243 if (TREE_CODE (expr) != ADDR_EXPR)
5245 error ("%qE is not a valid template argument for type %qT", expr, type);
5246 error ("it must be the address of a function with external linkage");
5250 /* [temp.arg.nontype]/5, bullet 5
5252 For a non-type template-parameter of type reference to function, no
5253 conversions apply. If the template-argument represents a set of
5254 overloaded functions, the matching function is selected from the set
5256 else if (TYPE_REFFN_P (type))
5258 if (TREE_CODE (expr) == ADDR_EXPR)
5260 error ("%qE is not a valid template argument for type %qT "
5261 "because it is a pointer", expr, type);
5262 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5266 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5267 if (!expr || expr == error_mark_node)
5270 expr = build_nop (type, build_address (expr));
5272 /* [temp.arg.nontype]/5, bullet 6
5274 For a non-type template-parameter of type pointer to member function,
5275 no conversions apply. If the template-argument represents a set of
5276 overloaded member functions, the matching member function is selected
5277 from the set (_over.over_). */
5278 else if (TYPE_PTRMEMFUNC_P (type))
5280 expr = instantiate_type (type, expr, tf_none);
5281 if (expr == error_mark_node)
5282 return error_mark_node;
5284 /* [temp.arg.nontype] bullet 1 says the pointer to member
5285 expression must be a pointer-to-member constant. */
5286 if (!check_valid_ptrmem_cst_expr (type, expr))
5287 return error_mark_node;
5289 /* There is no way to disable standard conversions in
5290 resolve_address_of_overloaded_function (called by
5291 instantiate_type). It is possible that the call succeeded by
5292 converting &B::I to &D::I (where B is a base of D), so we need
5293 to reject this conversion here.
5295 Actually, even if there was a way to disable standard conversions,
5296 it would still be better to reject them here so that we can
5297 provide a superior diagnostic. */
5298 if (!same_type_p (TREE_TYPE (expr), type))
5300 error ("%qE is not a valid template argument for type %qT "
5301 "because it is of type %qT", expr, type,
5303 /* If we are just one standard conversion off, explain. */
5304 if (can_convert (type, TREE_TYPE (expr)))
5305 inform (input_location,
5306 "standard conversions are not allowed in this context");
5310 /* [temp.arg.nontype]/5, bullet 7
5312 For a non-type template-parameter of type pointer to data member,
5313 qualification conversions (_conv.qual_) are applied. */
5314 else if (TYPE_PTRMEM_P (type))
5316 /* [temp.arg.nontype] bullet 1 says the pointer to member
5317 expression must be a pointer-to-member constant. */
5318 if (!check_valid_ptrmem_cst_expr (type, expr))
5319 return error_mark_node;
5321 expr = perform_qualification_conversions (type, expr);
5322 if (expr == error_mark_node)
5325 /* A template non-type parameter must be one of the above. */
5329 /* Sanity check: did we actually convert the argument to the
5331 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5332 (type, TREE_TYPE (expr)));
5336 /* Subroutine of coerce_template_template_parms, which returns 1 if
5337 PARM_PARM and ARG_PARM match using the rule for the template
5338 parameters of template template parameters. Both PARM and ARG are
5339 template parameters; the rest of the arguments are the same as for
5340 coerce_template_template_parms.
5343 coerce_template_template_parm (tree parm,
5345 tsubst_flags_t complain,
5349 if (arg == NULL_TREE || arg == error_mark_node
5350 || parm == NULL_TREE || parm == error_mark_node)
5353 if (TREE_CODE (arg) != TREE_CODE (parm))
5356 switch (TREE_CODE (parm))
5359 /* We encounter instantiations of templates like
5360 template <template <template <class> class> class TT>
5363 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5364 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5366 if (!coerce_template_template_parms
5367 (parmparm, argparm, complain, in_decl, outer_args))
5373 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5374 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5375 /* Argument is a parameter pack but parameter is not. */
5380 /* The tsubst call is used to handle cases such as
5382 template <int> class C {};
5383 template <class T, template <T> class TT> class D {};
5386 i.e. the parameter list of TT depends on earlier parameters. */
5387 if (!uses_template_parms (TREE_TYPE (arg))
5389 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5393 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5394 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5395 /* Argument is a parameter pack but parameter is not. */
5408 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5409 template template parameters. Both PARM_PARMS and ARG_PARMS are
5410 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5413 Consider the example:
5414 template <class T> class A;
5415 template<template <class U> class TT> class B;
5417 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5418 the parameters to A, and OUTER_ARGS contains A. */
5421 coerce_template_template_parms (tree parm_parms,
5423 tsubst_flags_t complain,
5427 int nparms, nargs, i;
5431 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5432 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5434 nparms = TREE_VEC_LENGTH (parm_parms);
5435 nargs = TREE_VEC_LENGTH (arg_parms);
5437 /* Determine whether we have a parameter pack at the end of the
5438 template template parameter's template parameter list. */
5439 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5441 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5443 if (parm == error_mark_node)
5446 switch (TREE_CODE (parm))
5450 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5455 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5465 && !(variadic_p && nargs >= nparms - 1))
5468 /* Check all of the template parameters except the parameter pack at
5469 the end (if any). */
5470 for (i = 0; i < nparms - variadic_p; ++i)
5472 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5473 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5476 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5477 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5479 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5487 /* Check each of the template parameters in the template
5488 argument against the template parameter pack at the end of
5489 the template template parameter. */
5490 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
5493 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5495 for (; i < nargs; ++i)
5497 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5500 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5502 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5511 /* Verifies that the deduced template arguments (in TARGS) for the
5512 template template parameters (in TPARMS) represent valid bindings,
5513 by comparing the template parameter list of each template argument
5514 to the template parameter list of its corresponding template
5515 template parameter, in accordance with DR150. This
5516 routine can only be called after all template arguments have been
5517 deduced. It will return TRUE if all of the template template
5518 parameter bindings are okay, FALSE otherwise. */
5520 template_template_parm_bindings_ok_p (tree tparms, tree targs)
5522 int i, ntparms = TREE_VEC_LENGTH (tparms);
5525 /* We're dealing with template parms in this process. */
5526 ++processing_template_decl;
5528 targs = INNERMOST_TEMPLATE_ARGS (targs);
5530 for (i = 0; i < ntparms; ++i)
5532 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
5533 tree targ = TREE_VEC_ELT (targs, i);
5535 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
5537 tree packed_args = NULL_TREE;
5540 if (ARGUMENT_PACK_P (targ))
5542 /* Look inside the argument pack. */
5543 packed_args = ARGUMENT_PACK_ARGS (targ);
5544 len = TREE_VEC_LENGTH (packed_args);
5547 for (idx = 0; idx < len; ++idx)
5549 tree targ_parms = NULL_TREE;
5552 /* Extract the next argument from the argument
5554 targ = TREE_VEC_ELT (packed_args, idx);
5556 if (PACK_EXPANSION_P (targ))
5557 /* Look at the pattern of the pack expansion. */
5558 targ = PACK_EXPANSION_PATTERN (targ);
5560 /* Extract the template parameters from the template
5562 if (TREE_CODE (targ) == TEMPLATE_DECL)
5563 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
5564 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
5565 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
5567 /* Verify that we can coerce the template template
5568 parameters from the template argument to the template
5569 parameter. This requires an exact match. */
5571 && !coerce_template_template_parms
5572 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
5587 --processing_template_decl;
5591 /* Convert the indicated template ARG as necessary to match the
5592 indicated template PARM. Returns the converted ARG, or
5593 error_mark_node if the conversion was unsuccessful. Error and
5594 warning messages are issued under control of COMPLAIN. This
5595 conversion is for the Ith parameter in the parameter list. ARGS is
5596 the full set of template arguments deduced so far. */
5599 convert_template_argument (tree parm,
5602 tsubst_flags_t complain,
5608 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
5610 if (TREE_CODE (arg) == TREE_LIST
5611 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
5613 /* The template argument was the name of some
5614 member function. That's usually
5615 invalid, but static members are OK. In any
5616 case, grab the underlying fields/functions
5617 and issue an error later if required. */
5618 orig_arg = TREE_VALUE (arg);
5619 TREE_TYPE (arg) = unknown_type_node;
5624 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
5625 requires_type = (TREE_CODE (parm) == TYPE_DECL
5626 || requires_tmpl_type);
5628 /* When determining whether an argument pack expansion is a template,
5629 look at the pattern. */
5630 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
5631 arg = PACK_EXPANSION_PATTERN (arg);
5633 /* Deal with an injected-class-name used as a template template arg. */
5634 if (requires_tmpl_type && CLASS_TYPE_P (arg))
5636 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
5637 if (TREE_CODE (t) == TEMPLATE_DECL)
5639 if (complain & tf_warning_or_error)
5640 pedwarn (input_location, OPT_pedantic, "injected-class-name %qD"
5641 " used as template template argument", TYPE_NAME (arg));
5642 else if (flag_pedantic_errors)
5650 ((TREE_CODE (arg) == TEMPLATE_DECL
5651 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
5652 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5653 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
5656 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5657 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
5658 arg = TYPE_STUB_DECL (arg);
5660 is_type = TYPE_P (arg) || is_tmpl_type;
5662 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
5663 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
5665 permerror (input_location, "to refer to a type member of a template parameter, "
5666 "use %<typename %E%>", orig_arg);
5668 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
5669 TREE_OPERAND (arg, 1),
5671 complain & tf_error);
5675 if (is_type != requires_type)
5679 if (complain & tf_error)
5681 error ("type/value mismatch at argument %d in template "
5682 "parameter list for %qD",
5685 error (" expected a constant of type %qT, got %qT",
5687 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
5688 else if (requires_tmpl_type)
5689 error (" expected a class template, got %qE", orig_arg);
5691 error (" expected a type, got %qE", orig_arg);
5694 return error_mark_node;
5696 if (is_tmpl_type ^ requires_tmpl_type)
5698 if (in_decl && (complain & tf_error))
5700 error ("type/value mismatch at argument %d in template "
5701 "parameter list for %qD",
5704 error (" expected a type, got %qT", DECL_NAME (arg));
5706 error (" expected a class template, got %qT", orig_arg);
5708 return error_mark_node;
5713 if (requires_tmpl_type)
5715 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
5716 /* The number of argument required is not known yet.
5717 Just accept it for now. */
5718 val = TREE_TYPE (arg);
5721 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5724 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5726 if (coerce_template_template_parms (parmparm, argparm,
5732 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5734 if (val != error_mark_node)
5736 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
5737 val = TREE_TYPE (val);
5738 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
5739 val = make_pack_expansion (val);
5744 if (in_decl && (complain & tf_error))
5746 error ("type/value mismatch at argument %d in "
5747 "template parameter list for %qD",
5749 error (" expected a template of type %qD, got %qT",
5753 val = error_mark_node;
5759 /* We only form one instance of each template specialization.
5760 Therefore, if we use a non-canonical variant (i.e., a
5761 typedef), any future messages referring to the type will use
5762 the typedef, which is confusing if those future uses do not
5763 themselves also use the typedef. */
5765 val = strip_typedefs (val);
5769 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
5771 if (invalid_nontype_parm_type_p (t, complain))
5772 return error_mark_node;
5774 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
5776 if (same_type_p (t, TREE_TYPE (orig_arg)))
5780 /* Not sure if this is reachable, but it doesn't hurt
5782 error ("type mismatch in nontype parameter pack");
5783 val = error_mark_node;
5786 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
5787 /* We used to call digest_init here. However, digest_init
5788 will report errors, which we don't want when complain
5789 is zero. More importantly, digest_init will try too
5790 hard to convert things: for example, `0' should not be
5791 converted to pointer type at this point according to
5792 the standard. Accepting this is not merely an
5793 extension, since deciding whether or not these
5794 conversions can occur is part of determining which
5795 function template to call, or whether a given explicit
5796 argument specification is valid. */
5797 val = convert_nontype_argument (t, orig_arg, complain);
5801 if (val == NULL_TREE)
5802 val = error_mark_node;
5803 else if (val == error_mark_node && (complain & tf_error))
5804 error ("could not convert template argument %qE to %qT", orig_arg, t);
5806 if (TREE_CODE (val) == SCOPE_REF)
5808 /* Strip typedefs from the SCOPE_REF. */
5809 tree type = strip_typedefs (TREE_TYPE (val));
5810 tree scope = strip_typedefs (TREE_OPERAND (val, 0));
5811 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
5812 QUALIFIED_NAME_IS_TEMPLATE (val));
5819 /* Coerces the remaining template arguments in INNER_ARGS (from
5820 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5821 Returns the coerced argument pack. PARM_IDX is the position of this
5822 parameter in the template parameter list. ARGS is the original
5823 template argument list. */
5825 coerce_template_parameter_pack (tree parms,
5833 tsubst_flags_t complain)
5835 tree parm = TREE_VEC_ELT (parms, parm_idx);
5836 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5839 tree packed_types = NULL_TREE;
5841 if (arg_idx > nargs)
5844 packed_args = make_tree_vec (nargs - arg_idx);
5846 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
5847 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
5849 /* When the template parameter is a non-type template
5850 parameter pack whose type uses parameter packs, we need
5851 to look at each of the template arguments
5852 separately. Build a vector of the types for these
5853 non-type template parameters in PACKED_TYPES. */
5855 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
5856 packed_types = tsubst_pack_expansion (expansion, args,
5859 if (packed_types == error_mark_node)
5860 return error_mark_node;
5862 /* Check that we have the right number of arguments. */
5864 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
5865 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
5868 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
5869 error ("wrong number of template arguments (%d, should be %d)",
5870 nargs, needed_parms);
5871 return error_mark_node;
5874 /* If we aren't able to check the actual arguments now
5875 (because they haven't been expanded yet), we can at least
5876 verify that all of the types used for the non-type
5877 template parameter pack are, in fact, valid for non-type
5878 template parameters. */
5880 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
5882 int j, len = TREE_VEC_LENGTH (packed_types);
5883 for (j = 0; j < len; ++j)
5885 tree t = TREE_VEC_ELT (packed_types, j);
5886 if (invalid_nontype_parm_type_p (t, complain))
5887 return error_mark_node;
5892 /* Convert the remaining arguments, which will be a part of the
5893 parameter pack "parm". */
5894 for (; arg_idx < nargs; ++arg_idx)
5896 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
5897 tree actual_parm = TREE_VALUE (parm);
5899 if (packed_types && !PACK_EXPANSION_P (arg))
5901 /* When we have a vector of types (corresponding to the
5902 non-type template parameter pack that uses parameter
5903 packs in its type, as mention above), and the
5904 argument is not an expansion (which expands to a
5905 currently unknown number of arguments), clone the
5906 parm and give it the next type in PACKED_TYPES. */
5907 actual_parm = copy_node (actual_parm);
5908 TREE_TYPE (actual_parm) =
5909 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
5912 if (arg != error_mark_node)
5913 arg = convert_template_argument (actual_parm,
5914 arg, new_args, complain, parm_idx,
5916 if (arg == error_mark_node)
5918 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
5921 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
5922 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
5923 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
5926 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
5927 TREE_TYPE (argument_pack)
5928 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
5929 TREE_CONSTANT (argument_pack) = 1;
5932 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
5933 #ifdef ENABLE_CHECKING
5934 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
5935 TREE_VEC_LENGTH (packed_args));
5937 return argument_pack;
5940 /* Convert all template arguments to their appropriate types, and
5941 return a vector containing the innermost resulting template
5942 arguments. If any error occurs, return error_mark_node. Error and
5943 warning messages are issued under control of COMPLAIN.
5945 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5946 for arguments not specified in ARGS. Otherwise, if
5947 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5948 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5949 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5953 coerce_template_parms (tree parms,
5956 tsubst_flags_t complain,
5957 bool require_all_args,
5958 bool use_default_args)
5960 int nparms, nargs, parm_idx, arg_idx, lost = 0;
5963 tree new_inner_args;
5964 int saved_unevaluated_operand;
5965 int saved_inhibit_evaluation_warnings;
5967 /* When used as a boolean value, indicates whether this is a
5968 variadic template parameter list. Since it's an int, we can also
5969 subtract it from nparms to get the number of non-variadic
5973 if (args == error_mark_node)
5974 return error_mark_node;
5976 nparms = TREE_VEC_LENGTH (parms);
5978 /* Determine if there are any parameter packs. */
5979 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
5981 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
5982 if (template_parameter_pack_p (tparm))
5986 inner_args = INNERMOST_TEMPLATE_ARGS (args);
5987 /* If there are 0 or 1 parameter packs, we need to expand any argument
5988 packs so that we can deduce a parameter pack from some non-packed args
5989 followed by an argument pack, as in variadic85.C. If there are more
5990 than that, we need to leave argument packs intact so the arguments are
5991 assigned to the right parameter packs. This should only happen when
5992 dealing with a nested class inside a partial specialization of a class
5993 template, as in variadic92.C. */
5994 if (variadic_p <= 1)
5995 inner_args = expand_template_argument_pack (inner_args);
5997 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5998 if ((nargs > nparms && !variadic_p)
5999 || (nargs < nparms - variadic_p
6001 && (!use_default_args
6002 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6003 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6005 if (complain & tf_error)
6010 error ("wrong number of template arguments "
6011 "(%d, should be %d or more)", nargs, nparms);
6014 error ("wrong number of template arguments "
6015 "(%d, should be %d)", nargs, nparms);
6018 error ("provided for %q+D", in_decl);
6021 return error_mark_node;
6024 /* We need to evaluate the template arguments, even though this
6025 template-id may be nested within a "sizeof". */
6026 saved_unevaluated_operand = cp_unevaluated_operand;
6027 cp_unevaluated_operand = 0;
6028 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6029 c_inhibit_evaluation_warnings = 0;
6030 new_inner_args = make_tree_vec (nparms);
6031 new_args = add_outermost_template_args (args, new_inner_args);
6032 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6037 /* Get the Ith template parameter. */
6038 parm = TREE_VEC_ELT (parms, parm_idx);
6040 if (parm == error_mark_node)
6042 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6046 /* Calculate the next argument. */
6047 if (arg_idx < nargs)
6048 arg = TREE_VEC_ELT (inner_args, arg_idx);
6052 if (template_parameter_pack_p (TREE_VALUE (parm))
6053 && !(arg && ARGUMENT_PACK_P (arg)))
6055 /* All remaining arguments will be placed in the
6056 template parameter pack PARM. */
6057 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6058 inner_args, arg_idx,
6062 /* Store this argument. */
6063 if (arg == error_mark_node)
6065 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6067 /* We are done with all of the arguments. */
6074 if (PACK_EXPANSION_P (arg))
6076 if (complain & tf_error)
6078 /* FIXME this restriction was removed by N2555; see
6080 /* If ARG is a pack expansion, but PARM is not a
6081 template parameter pack (if it were, we would have
6082 handled it above), we're trying to expand into a
6083 fixed-length argument list. */
6084 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
6085 sorry ("cannot expand %<%E%> into a fixed-length "
6086 "argument list", arg);
6088 sorry ("cannot expand %<%T%> into a fixed-length "
6089 "argument list", arg);
6091 return error_mark_node;
6094 else if (require_all_args)
6096 /* There must be a default arg in this case. */
6097 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6099 /* The position of the first default template argument,
6100 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6102 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6103 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6108 if (arg == error_mark_node)
6110 if (complain & tf_error)
6111 error ("template argument %d is invalid", arg_idx + 1);
6114 /* This only occurs if there was an error in the template
6115 parameter list itself (which we would already have
6116 reported) that we are trying to recover from, e.g., a class
6117 template with a parameter list such as
6118 template<typename..., typename>. */
6119 return error_mark_node;
6121 arg = convert_template_argument (TREE_VALUE (parm),
6122 arg, new_args, complain,
6125 if (arg == error_mark_node)
6127 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6129 cp_unevaluated_operand = saved_unevaluated_operand;
6130 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6133 return error_mark_node;
6135 #ifdef ENABLE_CHECKING
6136 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6137 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6138 TREE_VEC_LENGTH (new_inner_args));
6141 return new_inner_args;
6144 /* Returns 1 if template args OT and NT are equivalent. */
6147 template_args_equal (tree ot, tree nt)
6152 if (TREE_CODE (nt) == TREE_VEC)
6153 /* For member templates */
6154 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6155 else if (PACK_EXPANSION_P (ot))
6156 return PACK_EXPANSION_P (nt)
6157 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6158 PACK_EXPANSION_PATTERN (nt));
6159 else if (ARGUMENT_PACK_P (ot))
6164 if (!ARGUMENT_PACK_P (nt))
6167 opack = ARGUMENT_PACK_ARGS (ot);
6168 npack = ARGUMENT_PACK_ARGS (nt);
6169 len = TREE_VEC_LENGTH (opack);
6170 if (TREE_VEC_LENGTH (npack) != len)
6172 for (i = 0; i < len; ++i)
6173 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6174 TREE_VEC_ELT (npack, i)))
6178 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6180 /* We get here probably because we are in the middle of substituting
6181 into the pattern of a pack expansion. In that case the
6182 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6183 interested in. So we want to use the initial pack argument for
6185 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6186 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6187 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6188 return template_args_equal (ot, nt);
6190 else if (TYPE_P (nt))
6191 return TYPE_P (ot) && same_type_p (ot, nt);
6192 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6195 return cp_tree_equal (ot, nt);
6198 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6199 of template arguments. Returns 0 otherwise. */
6202 comp_template_args (tree oldargs, tree newargs)
6206 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6209 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6211 tree nt = TREE_VEC_ELT (newargs, i);
6212 tree ot = TREE_VEC_ELT (oldargs, i);
6214 if (! template_args_equal (ot, nt))
6221 add_pending_template (tree d)
6223 tree ti = (TYPE_P (d)
6224 ? CLASSTYPE_TEMPLATE_INFO (d)
6225 : DECL_TEMPLATE_INFO (d));
6226 struct pending_template *pt;
6229 if (TI_PENDING_TEMPLATE_FLAG (ti))
6232 /* We are called both from instantiate_decl, where we've already had a
6233 tinst_level pushed, and instantiate_template, where we haven't.
6235 level = !current_tinst_level || current_tinst_level->decl != d;
6238 push_tinst_level (d);
6240 pt = ggc_alloc_pending_template ();
6242 pt->tinst = current_tinst_level;
6243 if (last_pending_template)
6244 last_pending_template->next = pt;
6246 pending_templates = pt;
6248 last_pending_template = pt;
6250 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6257 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6258 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6259 documentation for TEMPLATE_ID_EXPR. */
6262 lookup_template_function (tree fns, tree arglist)
6266 if (fns == error_mark_node || arglist == error_mark_node)
6267 return error_mark_node;
6269 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6270 gcc_assert (fns && (is_overloaded_fn (fns)
6271 || TREE_CODE (fns) == IDENTIFIER_NODE));
6273 if (BASELINK_P (fns))
6275 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6277 BASELINK_FUNCTIONS (fns),
6282 type = TREE_TYPE (fns);
6283 if (TREE_CODE (fns) == OVERLOAD || !type)
6284 type = unknown_type_node;
6286 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6289 /* Within the scope of a template class S<T>, the name S gets bound
6290 (in build_self_reference) to a TYPE_DECL for the class, not a
6291 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6292 or one of its enclosing classes, and that type is a template,
6293 return the associated TEMPLATE_DECL. Otherwise, the original
6296 Also handle the case when DECL is a TREE_LIST of ambiguous
6297 injected-class-names from different bases. */
6300 maybe_get_template_decl_from_type_decl (tree decl)
6302 if (decl == NULL_TREE)
6305 /* DR 176: A lookup that finds an injected-class-name (10.2
6306 [class.member.lookup]) can result in an ambiguity in certain cases
6307 (for example, if it is found in more than one base class). If all of
6308 the injected-class-names that are found refer to specializations of
6309 the same class template, and if the name is followed by a
6310 template-argument-list, the reference refers to the class template
6311 itself and not a specialization thereof, and is not ambiguous. */
6312 if (TREE_CODE (decl) == TREE_LIST)
6314 tree t, tmpl = NULL_TREE;
6315 for (t = decl; t; t = TREE_CHAIN (t))
6317 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6320 else if (tmpl != elt)
6323 if (tmpl && t == NULL_TREE)
6329 return (decl != NULL_TREE
6330 && DECL_SELF_REFERENCE_P (decl)
6331 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6332 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
6335 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6336 parameters, find the desired type.
6338 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6340 IN_DECL, if non-NULL, is the template declaration we are trying to
6343 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6344 the class we are looking up.
6346 Issue error and warning messages under control of COMPLAIN.
6348 If the template class is really a local class in a template
6349 function, then the FUNCTION_CONTEXT is the function in which it is
6352 ??? Note that this function is currently called *twice* for each
6353 template-id: the first time from the parser, while creating the
6354 incomplete type (finish_template_type), and the second type during the
6355 real instantiation (instantiate_template_class). This is surely something
6356 that we want to avoid. It also causes some problems with argument
6357 coercion (see convert_nontype_argument for more information on this). */
6360 lookup_template_class (tree d1,
6365 tsubst_flags_t complain)
6367 tree templ = NULL_TREE, parmlist;
6374 timevar_push (TV_NAME_LOOKUP);
6376 if (TREE_CODE (d1) == IDENTIFIER_NODE)
6378 tree value = innermost_non_namespace_value (d1);
6379 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
6384 push_decl_namespace (context);
6385 templ = lookup_name (d1);
6386 templ = maybe_get_template_decl_from_type_decl (templ);
6388 pop_decl_namespace ();
6391 context = DECL_CONTEXT (templ);
6393 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
6395 tree type = TREE_TYPE (d1);
6397 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6398 an implicit typename for the second A. Deal with it. */
6399 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
6400 type = TREE_TYPE (type);
6402 if (CLASSTYPE_TEMPLATE_INFO (type))
6404 templ = CLASSTYPE_TI_TEMPLATE (type);
6405 d1 = DECL_NAME (templ);
6408 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6409 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
6411 templ = TYPE_TI_TEMPLATE (d1);
6412 d1 = DECL_NAME (templ);
6414 else if (TREE_CODE (d1) == TEMPLATE_DECL
6415 && DECL_TEMPLATE_RESULT (d1)
6416 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
6419 d1 = DECL_NAME (templ);
6420 context = DECL_CONTEXT (templ);
6423 /* Issue an error message if we didn't find a template. */
6426 if (complain & tf_error)
6427 error ("%qT is not a template", d1);
6428 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6431 if (TREE_CODE (templ) != TEMPLATE_DECL
6432 /* Make sure it's a user visible template, if it was named by
6434 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
6435 && !PRIMARY_TEMPLATE_P (templ)))
6437 if (complain & tf_error)
6439 error ("non-template type %qT used as a template", d1);
6441 error ("for template declaration %q+D", in_decl);
6443 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6446 complain &= ~tf_user;
6448 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
6450 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6451 template arguments */
6457 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6459 /* Consider an example where a template template parameter declared as
6461 template <class T, class U = std::allocator<T> > class TT
6463 The template parameter level of T and U are one level larger than
6464 of TT. To proper process the default argument of U, say when an
6465 instantiation `TT<int>' is seen, we need to build the full
6466 arguments containing {int} as the innermost level. Outer levels,
6467 available when not appearing as default template argument, can be
6468 obtained from the arguments of the enclosing template.
6470 Suppose that TT is later substituted with std::vector. The above
6471 instantiation is `TT<int, std::allocator<T> >' with TT at
6472 level 1, and T at level 2, while the template arguments at level 1
6473 becomes {std::vector} and the inner level 2 is {int}. */
6475 outer = DECL_CONTEXT (templ);
6477 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6478 else if (current_template_parms)
6479 /* This is an argument of the current template, so we haven't set
6480 DECL_CONTEXT yet. */
6481 outer = current_template_args ();
6484 arglist = add_to_template_args (outer, arglist);
6486 arglist2 = coerce_template_parms (parmlist, arglist, templ,
6488 /*require_all_args=*/true,
6489 /*use_default_args=*/true);
6490 if (arglist2 == error_mark_node
6491 || (!uses_template_parms (arglist2)
6492 && check_instantiated_args (templ, arglist2, complain)))
6493 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6495 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
6496 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
6500 tree template_type = TREE_TYPE (templ);
6503 tree found = NULL_TREE;
6506 int is_dependent_type;
6507 int use_partial_inst_tmpl = false;
6509 gen_tmpl = most_general_template (templ);
6510 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
6511 parm_depth = TMPL_PARMS_DEPTH (parmlist);
6512 arg_depth = TMPL_ARGS_DEPTH (arglist);
6514 if (arg_depth == 1 && parm_depth > 1)
6516 /* We've been given an incomplete set of template arguments.
6519 template <class T> struct S1 {
6520 template <class U> struct S2 {};
6521 template <class U> struct S2<U*> {};
6524 we will be called with an ARGLIST of `U*', but the
6525 TEMPLATE will be `template <class T> template
6526 <class U> struct S1<T>::S2'. We must fill in the missing
6529 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
6531 arg_depth = TMPL_ARGS_DEPTH (arglist);
6534 /* Now we should have enough arguments. */
6535 gcc_assert (parm_depth == arg_depth);
6537 /* From here on, we're only interested in the most general
6540 /* Calculate the BOUND_ARGS. These will be the args that are
6541 actually tsubst'd into the definition to create the
6545 /* We have multiple levels of arguments to coerce, at once. */
6547 int saved_depth = TMPL_ARGS_DEPTH (arglist);
6549 tree bound_args = make_tree_vec (parm_depth);
6551 for (i = saved_depth,
6552 t = DECL_TEMPLATE_PARMS (gen_tmpl);
6553 i > 0 && t != NULL_TREE;
6554 --i, t = TREE_CHAIN (t))
6557 if (i == saved_depth)
6558 a = coerce_template_parms (TREE_VALUE (t),
6561 /*require_all_args=*/true,
6562 /*use_default_args=*/true);
6564 /* Outer levels should have already been coerced. */
6565 a = TMPL_ARGS_LEVEL (arglist, i);
6567 /* Don't process further if one of the levels fails. */
6568 if (a == error_mark_node)
6570 /* Restore the ARGLIST to its full size. */
6571 TREE_VEC_LENGTH (arglist) = saved_depth;
6572 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6575 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
6577 /* We temporarily reduce the length of the ARGLIST so
6578 that coerce_template_parms will see only the arguments
6579 corresponding to the template parameters it is
6581 TREE_VEC_LENGTH (arglist)--;
6584 /* Restore the ARGLIST to its full size. */
6585 TREE_VEC_LENGTH (arglist) = saved_depth;
6587 arglist = bound_args;
6591 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
6592 INNERMOST_TEMPLATE_ARGS (arglist),
6595 /*require_all_args=*/true,
6596 /*use_default_args=*/true);
6598 if (arglist == error_mark_node)
6599 /* We were unable to bind the arguments. */
6600 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6602 /* In the scope of a template class, explicit references to the
6603 template class refer to the type of the template, not any
6604 instantiation of it. For example, in:
6606 template <class T> class C { void f(C<T>); }
6608 the `C<T>' is just the same as `C'. Outside of the
6609 class, however, such a reference is an instantiation. */
6611 || !PRIMARY_TEMPLATE_P (gen_tmpl)
6612 || currently_open_class (template_type))
6613 /* comp_template_args is expensive, check it last. */
6614 && comp_template_args (TYPE_TI_ARGS (template_type),
6616 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type);
6618 /* If we already have this specialization, return it. */
6619 elt.tmpl = gen_tmpl;
6621 hash = hash_specialization (&elt);
6622 entry = (spec_entry *) htab_find_with_hash (type_specializations,
6626 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec);
6628 is_dependent_type = uses_template_parms (arglist);
6630 /* If the deduced arguments are invalid, then the binding
6632 if (!is_dependent_type
6633 && check_instantiated_args (gen_tmpl,
6634 INNERMOST_TEMPLATE_ARGS (arglist),
6636 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6638 if (!is_dependent_type
6639 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6640 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
6641 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
6643 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
6644 DECL_NAME (gen_tmpl),
6645 /*tag_scope=*/ts_global);
6646 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
6649 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
6652 context = global_namespace;
6654 /* Create the type. */
6655 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
6657 if (!is_dependent_type)
6659 set_current_access_from_decl (TYPE_NAME (template_type));
6660 t = start_enum (TYPE_IDENTIFIER (template_type),
6661 tsubst (ENUM_UNDERLYING_TYPE (template_type),
6662 arglist, complain, in_decl),
6663 SCOPED_ENUM_P (template_type));
6667 /* We don't want to call start_enum for this type, since
6668 the values for the enumeration constants may involve
6669 template parameters. And, no one should be interested
6670 in the enumeration constants for such a type. */
6671 t = cxx_make_type (ENUMERAL_TYPE);
6672 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
6677 t = make_class_type (TREE_CODE (template_type));
6678 CLASSTYPE_DECLARED_CLASS (t)
6679 = CLASSTYPE_DECLARED_CLASS (template_type);
6680 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
6681 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
6683 /* A local class. Make sure the decl gets registered properly. */
6684 if (context == current_function_decl)
6685 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
6687 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
6688 /* This instantiation is another name for the primary
6689 template type. Set the TYPE_CANONICAL field
6691 TYPE_CANONICAL (t) = template_type;
6692 else if (any_template_arguments_need_structural_equality_p (arglist))
6693 /* Some of the template arguments require structural
6694 equality testing, so this template class requires
6695 structural equality testing. */
6696 SET_TYPE_STRUCTURAL_EQUALITY (t);
6699 /* If we called start_enum or pushtag above, this information
6700 will already be set up. */
6703 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
6705 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
6706 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
6707 DECL_SOURCE_LOCATION (type_decl)
6708 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
6711 type_decl = TYPE_NAME (t);
6713 TREE_PRIVATE (type_decl)
6714 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
6715 TREE_PROTECTED (type_decl)
6716 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
6717 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
6719 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
6720 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
6723 /* Let's consider the explicit specialization of a member
6724 of a class template specialization that is implicitely instantiated,
6729 template<class U> struct M {}; //#0
6734 struct S<int>::M<char> //#1
6738 [temp.expl.spec]/4 says this is valid.
6740 In this case, when we write:
6743 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
6746 When we encounter #1, we want to store the partial instantiation
6747 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
6749 For all cases other than this "explicit specialization of member of a
6750 class template", we just want to store the most general template into
6751 the CLASSTYPE_TI_TEMPLATE of M.
6753 This case of "explicit specialization of member of a class template"
6755 1/ the enclosing class is an instantiation of, and therefore not
6756 the same as, the context of the most general template, and
6757 2/ we aren't looking at the partial instantiation itself, i.e.
6758 the innermost arguments are not the same as the innermost parms of
6759 the most general template.
6761 So it's only when 1/ and 2/ happens that we want to use the partial
6762 instantiation of the member template in lieu of its most general
6765 if (PRIMARY_TEMPLATE_P (gen_tmpl)
6766 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
6767 /* the enclosing class must be an instantiation... */
6768 && CLASS_TYPE_P (context)
6769 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
6771 tree partial_inst_args;
6772 TREE_VEC_LENGTH (arglist)--;
6773 ++processing_template_decl;
6775 tsubst (INNERMOST_TEMPLATE_ARGS
6776 (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
6777 arglist, complain, NULL_TREE);
6778 --processing_template_decl;
6779 TREE_VEC_LENGTH (arglist)++;
6780 use_partial_inst_tmpl =
6781 /*...and we must not be looking at the partial instantiation
6783 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
6787 if (!use_partial_inst_tmpl)
6788 /* This case is easy; there are no member templates involved. */
6792 /* This is a full instantiation of a member template. Find
6793 the partial instantiation of which this is an instance. */
6795 /* Temporarily reduce by one the number of levels in the ARGLIST
6796 so as to avoid comparing the last set of arguments. */
6797 TREE_VEC_LENGTH (arglist)--;
6798 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
6799 TREE_VEC_LENGTH (arglist)++;
6800 found = CLASSTYPE_TI_TEMPLATE (found);
6803 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
6806 slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
6807 &elt, hash, INSERT);
6808 *slot = ggc_alloc_spec_entry ();
6811 /* Note this use of the partial instantiation so we can check it
6812 later in maybe_process_partial_specialization. */
6813 DECL_TEMPLATE_INSTANTIATIONS (templ)
6814 = tree_cons (arglist, t,
6815 DECL_TEMPLATE_INSTANTIATIONS (templ));
6817 if (TREE_CODE (t) == ENUMERAL_TYPE && !is_dependent_type)
6818 /* Now that the type has been registered on the instantiations
6819 list, we set up the enumerators. Because the enumeration
6820 constants may involve the enumeration type itself, we make
6821 sure to register the type first, and then create the
6822 constants. That way, doing tsubst_expr for the enumeration
6823 constants won't result in recursive calls here; we'll find
6824 the instantiation and exit above. */
6825 tsubst_enum (template_type, t, arglist);
6827 if (is_dependent_type)
6828 /* If the type makes use of template parameters, the
6829 code that generates debugging information will crash. */
6830 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
6832 /* Possibly limit visibility based on template args. */
6833 TREE_PUBLIC (type_decl) = 1;
6834 determine_visibility (type_decl);
6836 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6838 timevar_pop (TV_NAME_LOOKUP);
6845 /* True when we should also visit template parameters that occur in
6846 non-deduced contexts. */
6847 bool include_nondeduced_p;
6848 struct pointer_set_t *visited;
6851 /* Called from for_each_template_parm via walk_tree. */
6854 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
6857 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
6858 tree_fn_t fn = pfd->fn;
6859 void *data = pfd->data;
6862 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
6863 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
6864 pfd->include_nondeduced_p))
6865 return error_mark_node;
6867 switch (TREE_CODE (t))
6870 if (TYPE_PTRMEMFUNC_P (t))
6876 if (!TYPE_TEMPLATE_INFO (t))
6878 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
6879 fn, data, pfd->visited,
6880 pfd->include_nondeduced_p))
6881 return error_mark_node;
6885 if (for_each_template_parm (TYPE_MIN_VALUE (t),
6886 fn, data, pfd->visited,
6887 pfd->include_nondeduced_p)
6888 || for_each_template_parm (TYPE_MAX_VALUE (t),
6889 fn, data, pfd->visited,
6890 pfd->include_nondeduced_p))
6891 return error_mark_node;
6895 /* Since we're not going to walk subtrees, we have to do this
6897 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
6898 pfd->visited, pfd->include_nondeduced_p))
6899 return error_mark_node;
6903 /* Check the return type. */
6904 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6905 pfd->include_nondeduced_p))
6906 return error_mark_node;
6908 /* Check the parameter types. Since default arguments are not
6909 instantiated until they are needed, the TYPE_ARG_TYPES may
6910 contain expressions that involve template parameters. But,
6911 no-one should be looking at them yet. And, once they're
6912 instantiated, they don't contain template parameters, so
6913 there's no point in looking at them then, either. */
6917 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
6918 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
6919 pfd->visited, pfd->include_nondeduced_p))
6920 return error_mark_node;
6922 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6923 want walk_tree walking into them itself. */
6929 if (pfd->include_nondeduced_p
6930 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
6932 pfd->include_nondeduced_p))
6933 return error_mark_node;
6938 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
6939 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
6940 pfd->visited, pfd->include_nondeduced_p))
6941 return error_mark_node;
6946 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
6947 && for_each_template_parm (DECL_INITIAL (t), fn, data,
6948 pfd->visited, pfd->include_nondeduced_p))
6949 return error_mark_node;
6950 if (DECL_CONTEXT (t)
6951 && pfd->include_nondeduced_p
6952 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
6953 pfd->visited, pfd->include_nondeduced_p))
6954 return error_mark_node;
6957 case BOUND_TEMPLATE_TEMPLATE_PARM:
6958 /* Record template parameters such as `T' inside `TT<T>'. */
6959 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
6960 pfd->include_nondeduced_p))
6961 return error_mark_node;
6964 case TEMPLATE_TEMPLATE_PARM:
6965 case TEMPLATE_TYPE_PARM:
6966 case TEMPLATE_PARM_INDEX:
6967 if (fn && (*fn)(t, data))
6968 return error_mark_node;
6970 return error_mark_node;
6974 /* A template template parameter is encountered. */
6975 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
6976 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6977 pfd->include_nondeduced_p))
6978 return error_mark_node;
6980 /* Already substituted template template parameter */
6986 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
6988 pfd->include_nondeduced_p))
6989 return error_mark_node;
6993 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
6994 && pfd->include_nondeduced_p
6995 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
6996 (TREE_TYPE (t)), fn, data,
6997 pfd->visited, pfd->include_nondeduced_p))
6998 return error_mark_node;
7003 /* If there's no type, then this thing must be some expression
7004 involving template parameters. */
7005 if (!fn && !TREE_TYPE (t))
7006 return error_mark_node;
7011 case REINTERPRET_CAST_EXPR:
7012 case CONST_CAST_EXPR:
7013 case STATIC_CAST_EXPR:
7014 case DYNAMIC_CAST_EXPR:
7018 case PSEUDO_DTOR_EXPR:
7020 return error_mark_node;
7027 /* We didn't find any template parameters we liked. */
7031 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7032 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7033 call FN with the parameter and the DATA.
7034 If FN returns nonzero, the iteration is terminated, and
7035 for_each_template_parm returns 1. Otherwise, the iteration
7036 continues. If FN never returns a nonzero value, the value
7037 returned by for_each_template_parm is 0. If FN is NULL, it is
7038 considered to be the function which always returns 1.
7040 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7041 parameters that occur in non-deduced contexts. When false, only
7042 visits those template parameters that can be deduced. */
7045 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7046 struct pointer_set_t *visited,
7047 bool include_nondeduced_p)
7049 struct pair_fn_data pfd;
7055 pfd.include_nondeduced_p = include_nondeduced_p;
7057 /* Walk the tree. (Conceptually, we would like to walk without
7058 duplicates, but for_each_template_parm_r recursively calls
7059 for_each_template_parm, so we would need to reorganize a fair
7060 bit to use walk_tree_without_duplicates, so we keep our own
7063 pfd.visited = visited;
7065 pfd.visited = pointer_set_create ();
7066 result = cp_walk_tree (&t,
7067 for_each_template_parm_r,
7069 pfd.visited) != NULL_TREE;
7074 pointer_set_destroy (pfd.visited);
7081 /* Returns true if T depends on any template parameter. */
7084 uses_template_parms (tree t)
7087 int saved_processing_template_decl;
7089 saved_processing_template_decl = processing_template_decl;
7090 if (!saved_processing_template_decl)
7091 processing_template_decl = 1;
7093 dependent_p = dependent_type_p (t);
7094 else if (TREE_CODE (t) == TREE_VEC)
7095 dependent_p = any_dependent_template_arguments_p (t);
7096 else if (TREE_CODE (t) == TREE_LIST)
7097 dependent_p = (uses_template_parms (TREE_VALUE (t))
7098 || uses_template_parms (TREE_CHAIN (t)));
7099 else if (TREE_CODE (t) == TYPE_DECL)
7100 dependent_p = dependent_type_p (TREE_TYPE (t));
7103 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7104 || TREE_CODE (t) == OVERLOAD
7105 || TREE_CODE (t) == BASELINK
7106 || TREE_CODE (t) == IDENTIFIER_NODE
7107 || TREE_CODE (t) == TRAIT_EXPR
7108 || TREE_CODE (t) == CONSTRUCTOR
7109 || CONSTANT_CLASS_P (t))
7110 dependent_p = (type_dependent_expression_p (t)
7111 || value_dependent_expression_p (t));
7114 gcc_assert (t == error_mark_node);
7115 dependent_p = false;
7118 processing_template_decl = saved_processing_template_decl;
7123 /* Returns true if T depends on any template parameter with level LEVEL. */
7126 uses_template_parms_level (tree t, int level)
7128 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
7129 /*include_nondeduced_p=*/true);
7132 static int tinst_depth;
7133 extern int max_tinst_depth;
7134 #ifdef GATHER_STATISTICS
7137 static int tinst_level_tick;
7138 static int last_template_error_tick;
7140 /* We're starting to instantiate D; record the template instantiation context
7141 for diagnostics and to restore it later. */
7144 push_tinst_level (tree d)
7146 struct tinst_level *new_level;
7148 if (tinst_depth >= max_tinst_depth)
7150 /* If the instantiation in question still has unbound template parms,
7151 we don't really care if we can't instantiate it, so just return.
7152 This happens with base instantiation for implicit `typename'. */
7153 if (uses_template_parms (d))
7156 last_template_error_tick = tinst_level_tick;
7157 error ("template instantiation depth exceeds maximum of %d (use "
7158 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7159 max_tinst_depth, d);
7161 print_instantiation_context ();
7166 new_level = ggc_alloc_tinst_level ();
7167 new_level->decl = d;
7168 new_level->locus = input_location;
7169 new_level->in_system_header_p = in_system_header;
7170 new_level->next = current_tinst_level;
7171 current_tinst_level = new_level;
7174 #ifdef GATHER_STATISTICS
7175 if (tinst_depth > depth_reached)
7176 depth_reached = tinst_depth;
7183 /* We're done instantiating this template; return to the instantiation
7187 pop_tinst_level (void)
7189 /* Restore the filename and line number stashed away when we started
7190 this instantiation. */
7191 input_location = current_tinst_level->locus;
7192 current_tinst_level = current_tinst_level->next;
7197 /* We're instantiating a deferred template; restore the template
7198 instantiation context in which the instantiation was requested, which
7199 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7202 reopen_tinst_level (struct tinst_level *level)
7204 struct tinst_level *t;
7207 for (t = level; t; t = t->next)
7210 current_tinst_level = level;
7215 /* Returns the TINST_LEVEL which gives the original instantiation
7218 struct tinst_level *
7219 outermost_tinst_level (void)
7221 struct tinst_level *level = current_tinst_level;
7224 level = level->next;
7228 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
7231 parameter_of_template_p (tree parm, tree templ)
7236 if (!parm || !templ)
7239 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
7240 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7242 parms = DECL_TEMPLATE_PARMS (templ);
7243 parms = INNERMOST_TEMPLATE_PARMS (parms);
7245 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
7246 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
7252 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7253 vector of template arguments, as for tsubst.
7255 Returns an appropriate tsubst'd friend declaration. */
7258 tsubst_friend_function (tree decl, tree args)
7262 if (TREE_CODE (decl) == FUNCTION_DECL
7263 && DECL_TEMPLATE_INSTANTIATION (decl)
7264 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
7265 /* This was a friend declared with an explicit template
7266 argument list, e.g.:
7270 to indicate that f was a template instantiation, not a new
7271 function declaration. Now, we have to figure out what
7272 instantiation of what template. */
7274 tree template_id, arglist, fns;
7277 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
7279 /* Friend functions are looked up in the containing namespace scope.
7280 We must enter that scope, to avoid finding member functions of the
7281 current class with same name. */
7282 push_nested_namespace (ns);
7283 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
7284 tf_warning_or_error, NULL_TREE,
7285 /*integral_constant_expression_p=*/false);
7286 pop_nested_namespace (ns);
7287 arglist = tsubst (DECL_TI_ARGS (decl), args,
7288 tf_warning_or_error, NULL_TREE);
7289 template_id = lookup_template_function (fns, arglist);
7291 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7292 tmpl = determine_specialization (template_id, new_friend,
7294 /*need_member_template=*/0,
7295 TREE_VEC_LENGTH (args),
7297 return instantiate_template (tmpl, new_args, tf_error);
7300 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7302 /* The NEW_FRIEND will look like an instantiation, to the
7303 compiler, but is not an instantiation from the point of view of
7304 the language. For example, we might have had:
7306 template <class T> struct S {
7307 template <class U> friend void f(T, U);
7310 Then, in S<int>, template <class U> void f(int, U) is not an
7311 instantiation of anything. */
7312 if (new_friend == error_mark_node)
7313 return error_mark_node;
7315 DECL_USE_TEMPLATE (new_friend) = 0;
7316 if (TREE_CODE (decl) == TEMPLATE_DECL)
7318 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
7319 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
7320 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
7323 /* The mangled name for the NEW_FRIEND is incorrect. The function
7324 is not a template instantiation and should not be mangled like
7325 one. Therefore, we forget the mangling here; we'll recompute it
7326 later if we need it. */
7327 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
7329 SET_DECL_RTL (new_friend, NULL);
7330 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
7333 if (DECL_NAMESPACE_SCOPE_P (new_friend))
7336 tree new_friend_template_info;
7337 tree new_friend_result_template_info;
7339 int new_friend_is_defn;
7341 /* We must save some information from NEW_FRIEND before calling
7342 duplicate decls since that function will free NEW_FRIEND if
7344 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
7345 new_friend_is_defn =
7346 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7347 (template_for_substitution (new_friend)))
7349 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
7351 /* This declaration is a `primary' template. */
7352 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
7354 new_friend_result_template_info
7355 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
7358 new_friend_result_template_info = NULL_TREE;
7360 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7361 if (new_friend_is_defn)
7362 DECL_INITIAL (new_friend) = error_mark_node;
7364 /* Inside pushdecl_namespace_level, we will push into the
7365 current namespace. However, the friend function should go
7366 into the namespace of the template. */
7367 ns = decl_namespace_context (new_friend);
7368 push_nested_namespace (ns);
7369 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
7370 pop_nested_namespace (ns);
7372 if (old_decl == error_mark_node)
7373 return error_mark_node;
7375 if (old_decl != new_friend)
7377 /* This new friend declaration matched an existing
7378 declaration. For example, given:
7380 template <class T> void f(T);
7381 template <class U> class C {
7382 template <class T> friend void f(T) {}
7385 the friend declaration actually provides the definition
7386 of `f', once C has been instantiated for some type. So,
7387 old_decl will be the out-of-class template declaration,
7388 while new_friend is the in-class definition.
7390 But, if `f' was called before this point, the
7391 instantiation of `f' will have DECL_TI_ARGS corresponding
7392 to `T' but not to `U', references to which might appear
7393 in the definition of `f'. Previously, the most general
7394 template for an instantiation of `f' was the out-of-class
7395 version; now it is the in-class version. Therefore, we
7396 run through all specialization of `f', adding to their
7397 DECL_TI_ARGS appropriately. In particular, they need a
7398 new set of outer arguments, corresponding to the
7399 arguments for this class instantiation.
7401 The same situation can arise with something like this:
7404 template <class T> class C {
7408 when `C<int>' is instantiated. Now, `f(int)' is defined
7411 if (!new_friend_is_defn)
7412 /* On the other hand, if the in-class declaration does
7413 *not* provide a definition, then we don't want to alter
7414 existing definitions. We can just leave everything
7419 tree new_template = TI_TEMPLATE (new_friend_template_info);
7420 tree new_args = TI_ARGS (new_friend_template_info);
7422 /* Overwrite whatever template info was there before, if
7423 any, with the new template information pertaining to
7425 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
7427 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
7429 /* We should have called reregister_specialization in
7431 gcc_assert (retrieve_specialization (new_template,
7435 /* Instantiate it if the global has already been used. */
7436 if (DECL_ODR_USED (old_decl))
7437 instantiate_decl (old_decl, /*defer_ok=*/true,
7438 /*expl_inst_class_mem_p=*/false);
7444 /* Indicate that the old function template is a partial
7446 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
7447 = new_friend_result_template_info;
7449 gcc_assert (new_template
7450 == most_general_template (new_template));
7451 gcc_assert (new_template != old_decl);
7453 /* Reassign any specializations already in the hash table
7454 to the new more general template, and add the
7455 additional template args. */
7456 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
7460 tree spec = TREE_VALUE (t);
7463 elt.tmpl = old_decl;
7464 elt.args = DECL_TI_ARGS (spec);
7465 elt.spec = NULL_TREE;
7467 htab_remove_elt (decl_specializations, &elt);
7470 = add_outermost_template_args (new_args,
7471 DECL_TI_ARGS (spec));
7473 register_specialization
7474 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
7477 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
7481 /* The information from NEW_FRIEND has been merged into OLD_DECL
7482 by duplicate_decls. */
7483 new_friend = old_decl;
7488 tree context = DECL_CONTEXT (new_friend);
7492 template <class T> class C {
7493 template <class U> friend void C1<U>::f (); // case 1
7494 friend void C2<T>::f (); // case 2
7496 we only need to make sure CONTEXT is a complete type for
7497 case 2. To distinguish between the two cases, we note that
7498 CONTEXT of case 1 remains dependent type after tsubst while
7499 this isn't true for case 2. */
7500 ++processing_template_decl;
7501 dependent_p = dependent_type_p (context);
7502 --processing_template_decl;
7505 && !complete_type_or_else (context, NULL_TREE))
7506 return error_mark_node;
7508 if (COMPLETE_TYPE_P (context))
7510 /* Check to see that the declaration is really present, and,
7511 possibly obtain an improved declaration. */
7512 tree fn = check_classfn (context,
7513 new_friend, NULL_TREE);
7523 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7524 template arguments, as for tsubst.
7526 Returns an appropriate tsubst'd friend type or error_mark_node on
7530 tsubst_friend_class (tree friend_tmpl, tree args)
7536 context = CP_DECL_CONTEXT (friend_tmpl);
7538 if (context != global_namespace)
7540 if (TREE_CODE (context) == NAMESPACE_DECL)
7541 push_nested_namespace (context);
7543 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
7546 /* Look for a class template declaration. We look for hidden names
7547 because two friend declarations of the same template are the
7548 same. For example, in:
7551 template <typename> friend class F;
7553 template <typename> struct B {
7554 template <typename> friend class F;
7557 both F templates are the same. */
7558 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
7559 /*block_p=*/true, 0,
7560 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
7562 /* But, if we don't find one, it might be because we're in a
7563 situation like this:
7571 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7572 for `S<int>', not the TEMPLATE_DECL. */
7573 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
7575 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
7576 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
7579 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
7581 /* The friend template has already been declared. Just
7582 check to see that the declarations match, and install any new
7583 default parameters. We must tsubst the default parameters,
7584 of course. We only need the innermost template parameters
7585 because that is all that redeclare_class_template will look
7587 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
7588 > TMPL_ARGS_DEPTH (args))
7591 location_t saved_input_location;
7592 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
7593 args, tf_warning_or_error);
7595 saved_input_location = input_location;
7596 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
7597 redeclare_class_template (TREE_TYPE (tmpl), parms);
7598 input_location = saved_input_location;
7602 friend_type = TREE_TYPE (tmpl);
7606 /* The friend template has not already been declared. In this
7607 case, the instantiation of the template class will cause the
7608 injection of this template into the global scope. */
7609 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
7610 if (tmpl == error_mark_node)
7611 return error_mark_node;
7613 /* The new TMPL is not an instantiation of anything, so we
7614 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7615 the new type because that is supposed to be the corresponding
7616 template decl, i.e., TMPL. */
7617 DECL_USE_TEMPLATE (tmpl) = 0;
7618 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
7619 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
7620 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
7621 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
7623 /* Inject this template into the global scope. */
7624 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
7627 if (context != global_namespace)
7629 if (TREE_CODE (context) == NAMESPACE_DECL)
7630 pop_nested_namespace (context);
7632 pop_nested_class ();
7638 /* Returns zero if TYPE cannot be completed later due to circularity.
7639 Otherwise returns one. */
7642 can_complete_type_without_circularity (tree type)
7644 if (type == NULL_TREE || type == error_mark_node)
7646 else if (COMPLETE_TYPE_P (type))
7648 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
7649 return can_complete_type_without_circularity (TREE_TYPE (type));
7650 else if (CLASS_TYPE_P (type)
7651 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
7657 /* Apply any attributes which had to be deferred until instantiation
7658 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7659 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7662 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
7663 tree args, tsubst_flags_t complain, tree in_decl)
7665 tree last_dep = NULL_TREE;
7669 for (t = attributes; t; t = TREE_CHAIN (t))
7670 if (ATTR_IS_DEPENDENT (t))
7673 attributes = copy_list (attributes);
7677 if (DECL_P (*decl_p))
7679 if (TREE_TYPE (*decl_p) == error_mark_node)
7681 p = &DECL_ATTRIBUTES (*decl_p);
7684 p = &TYPE_ATTRIBUTES (*decl_p);
7688 tree late_attrs = NULL_TREE;
7689 tree *q = &late_attrs;
7691 for (*p = attributes; *p; )
7694 if (ATTR_IS_DEPENDENT (t))
7696 *p = TREE_CHAIN (t);
7697 TREE_CHAIN (t) = NULL_TREE;
7698 /* If the first attribute argument is an identifier, don't
7699 pass it through tsubst. Attributes like mode, format,
7700 cleanup and several target specific attributes expect it
7703 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
7704 && TREE_VALUE (TREE_VALUE (t))
7705 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
7706 == IDENTIFIER_NODE))
7709 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
7711 /*integral_constant_expression_p=*/false);
7712 if (chain != TREE_CHAIN (TREE_VALUE (t)))
7714 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
7719 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
7720 /*integral_constant_expression_p=*/false);
7722 q = &TREE_CHAIN (t);
7725 p = &TREE_CHAIN (t);
7728 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
7732 /* Perform (or defer) access check for typedefs that were referenced
7733 from within the template TMPL code.
7734 This is a subroutine of instantiate_template and instantiate_class_template.
7735 TMPL is the template to consider and TARGS is the list of arguments of
7739 perform_typedefs_access_check (tree tmpl, tree targs)
7741 location_t saved_location;
7743 qualified_typedef_usage_t *iter;
7746 || (!CLASS_TYPE_P (tmpl)
7747 && TREE_CODE (tmpl) != FUNCTION_DECL))
7750 saved_location = input_location;
7751 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
7752 get_types_needing_access_check (tmpl),
7755 tree type_decl = iter->typedef_decl;
7756 tree type_scope = iter->context;
7758 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
7761 if (uses_template_parms (type_decl))
7762 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
7763 if (uses_template_parms (type_scope))
7764 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
7766 /* Make access check error messages point to the location
7767 of the use of the typedef. */
7768 input_location = iter->locus;
7769 perform_or_defer_access_check (TYPE_BINFO (type_scope),
7770 type_decl, type_decl);
7772 input_location = saved_location;
7776 instantiate_class_template (tree type)
7778 tree templ, args, pattern, t, member;
7782 unsigned int saved_maximum_field_alignment;
7784 if (type == error_mark_node)
7785 return error_mark_node;
7787 if (COMPLETE_OR_OPEN_TYPE_P (type)
7788 || uses_template_parms (type))
7791 /* Figure out which template is being instantiated. */
7792 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
7793 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7795 /* Determine what specialization of the original template to
7797 t = most_specialized_class (type, templ, tf_warning_or_error);
7798 if (t == error_mark_node)
7800 TYPE_BEING_DEFINED (type) = 1;
7801 return error_mark_node;
7805 /* This TYPE is actually an instantiation of a partial
7806 specialization. We replace the innermost set of ARGS with
7807 the arguments appropriate for substitution. For example,
7810 template <class T> struct S {};
7811 template <class T> struct S<T*> {};
7813 and supposing that we are instantiating S<int*>, ARGS will
7814 presently be {int*} -- but we need {int}. */
7815 pattern = TREE_TYPE (t);
7816 args = TREE_PURPOSE (t);
7820 pattern = TREE_TYPE (templ);
7821 args = CLASSTYPE_TI_ARGS (type);
7824 /* If the template we're instantiating is incomplete, then clearly
7825 there's nothing we can do. */
7826 if (!COMPLETE_TYPE_P (pattern))
7829 /* If we've recursively instantiated too many templates, stop. */
7830 if (! push_tinst_level (type))
7833 /* Now we're really doing the instantiation. Mark the type as in
7834 the process of being defined. */
7835 TYPE_BEING_DEFINED (type) = 1;
7837 /* We may be in the middle of deferred access check. Disable
7839 push_deferring_access_checks (dk_no_deferred);
7841 push_to_top_level ();
7842 /* Use #pragma pack from the template context. */
7843 saved_maximum_field_alignment = maximum_field_alignment;
7844 maximum_field_alignment = TYPE_PRECISION (pattern);
7846 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
7848 /* Set the input location to the most specialized template definition.
7849 This is needed if tsubsting causes an error. */
7850 typedecl = TYPE_MAIN_DECL (pattern);
7851 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
7852 DECL_SOURCE_LOCATION (typedecl);
7854 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
7855 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
7856 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
7857 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
7858 TYPE_HAS_COPY_ASSIGN (type) = TYPE_HAS_COPY_ASSIGN (pattern);
7859 TYPE_HAS_CONST_COPY_ASSIGN (type) = TYPE_HAS_CONST_COPY_ASSIGN (pattern);
7860 TYPE_HAS_COPY_CTOR (type) = TYPE_HAS_COPY_CTOR (pattern);
7861 TYPE_HAS_CONST_COPY_CTOR (type) = TYPE_HAS_CONST_COPY_CTOR (pattern);
7862 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
7863 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
7864 TYPE_PACKED (type) = TYPE_PACKED (pattern);
7865 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
7866 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
7867 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
7868 if (ANON_AGGR_TYPE_P (pattern))
7869 SET_ANON_AGGR_TYPE_P (type);
7870 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
7872 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
7873 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
7876 pbinfo = TYPE_BINFO (pattern);
7878 /* We should never instantiate a nested class before its enclosing
7879 class; we need to look up the nested class by name before we can
7880 instantiate it, and that lookup should instantiate the enclosing
7882 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
7883 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
7885 base_list = NULL_TREE;
7886 if (BINFO_N_BASE_BINFOS (pbinfo))
7892 /* We must enter the scope containing the type, as that is where
7893 the accessibility of types named in dependent bases are
7895 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
7897 /* Substitute into each of the bases to determine the actual
7899 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
7902 tree access = BINFO_BASE_ACCESS (pbinfo, i);
7903 tree expanded_bases = NULL_TREE;
7906 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
7909 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
7910 args, tf_error, NULL_TREE);
7911 if (expanded_bases == error_mark_node)
7914 len = TREE_VEC_LENGTH (expanded_bases);
7917 for (idx = 0; idx < len; idx++)
7920 /* Extract the already-expanded base class. */
7921 base = TREE_VEC_ELT (expanded_bases, idx);
7923 /* Substitute to figure out the base class. */
7924 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
7927 if (base == error_mark_node)
7930 base_list = tree_cons (access, base, base_list);
7931 if (BINFO_VIRTUAL_P (pbase_binfo))
7932 TREE_TYPE (base_list) = integer_type_node;
7936 /* The list is now in reverse order; correct that. */
7937 base_list = nreverse (base_list);
7940 pop_scope (pushed_scope);
7942 /* Now call xref_basetypes to set up all the base-class
7944 xref_basetypes (type, base_list);
7946 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
7947 (int) ATTR_FLAG_TYPE_IN_PLACE,
7948 args, tf_error, NULL_TREE);
7949 fixup_attribute_variants (type);
7951 /* Now that our base classes are set up, enter the scope of the
7952 class, so that name lookups into base classes, etc. will work
7953 correctly. This is precisely analogous to what we do in
7954 begin_class_definition when defining an ordinary non-template
7955 class, except we also need to push the enclosing classes. */
7956 push_nested_class (type);
7958 /* Now members are processed in the order of declaration. */
7959 for (member = CLASSTYPE_DECL_LIST (pattern);
7960 member; member = TREE_CHAIN (member))
7962 tree t = TREE_VALUE (member);
7964 if (TREE_PURPOSE (member))
7968 /* Build new CLASSTYPE_NESTED_UTDS. */
7971 bool class_template_p;
7973 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
7974 && TYPE_LANG_SPECIFIC (t)
7975 && CLASSTYPE_IS_TEMPLATE (t));
7976 /* If the member is a class template, then -- even after
7977 substitution -- there may be dependent types in the
7978 template argument list for the class. We increment
7979 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7980 that function will assume that no types are dependent
7981 when outside of a template. */
7982 if (class_template_p)
7983 ++processing_template_decl;
7984 newtag = tsubst (t, args, tf_error, NULL_TREE);
7985 if (class_template_p)
7986 --processing_template_decl;
7987 if (newtag == error_mark_node)
7990 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
7992 tree name = TYPE_IDENTIFIER (t);
7994 if (class_template_p)
7995 /* Unfortunately, lookup_template_class sets
7996 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
7997 instantiation (i.e., for the type of a member
7998 template class nested within a template class.)
7999 This behavior is required for
8000 maybe_process_partial_specialization to work
8001 correctly, but is not accurate in this case;
8002 the TAG is not an instantiation of anything.
8003 (The corresponding TEMPLATE_DECL is an
8004 instantiation, but the TYPE is not.) */
8005 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8007 /* Now, we call pushtag to put this NEWTAG into the scope of
8008 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8009 pushtag calling push_template_decl. We don't have to do
8010 this for enums because it will already have been done in
8013 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8014 pushtag (name, newtag, /*tag_scope=*/ts_current);
8017 else if (TREE_CODE (t) == FUNCTION_DECL
8018 || DECL_FUNCTION_TEMPLATE_P (t))
8020 /* Build new TYPE_METHODS. */
8023 if (TREE_CODE (t) == TEMPLATE_DECL)
8024 ++processing_template_decl;
8025 r = tsubst (t, args, tf_error, NULL_TREE);
8026 if (TREE_CODE (t) == TEMPLATE_DECL)
8027 --processing_template_decl;
8028 set_current_access_from_decl (r);
8029 finish_member_declaration (r);
8033 /* Build new TYPE_FIELDS. */
8034 if (TREE_CODE (t) == STATIC_ASSERT)
8037 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8038 tf_warning_or_error, NULL_TREE,
8039 /*integral_constant_expression_p=*/true);
8040 finish_static_assert (condition,
8041 STATIC_ASSERT_MESSAGE (t),
8042 STATIC_ASSERT_SOURCE_LOCATION (t),
8045 else if (TREE_CODE (t) != CONST_DECL)
8049 /* The file and line for this declaration, to
8050 assist in error message reporting. Since we
8051 called push_tinst_level above, we don't need to
8053 input_location = DECL_SOURCE_LOCATION (t);
8055 if (TREE_CODE (t) == TEMPLATE_DECL)
8056 ++processing_template_decl;
8057 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8058 if (TREE_CODE (t) == TEMPLATE_DECL)
8059 --processing_template_decl;
8060 if (TREE_CODE (r) == VAR_DECL)
8064 [t]he initialization (and any associated
8065 side-effects) of a static data member does
8066 not occur unless the static data member is
8067 itself used in a way that requires the
8068 definition of the static data member to
8071 Therefore, we do not substitute into the
8072 initialized for the static data member here. */
8073 finish_static_data_member_decl
8076 /*init_const_expr_p=*/false,
8077 /*asmspec_tree=*/NULL_TREE,
8079 if (DECL_INITIALIZED_IN_CLASS_P (r))
8080 check_static_variable_definition (r, TREE_TYPE (r));
8082 else if (TREE_CODE (r) == FIELD_DECL)
8084 /* Determine whether R has a valid type and can be
8085 completed later. If R is invalid, then it is
8086 replaced by error_mark_node so that it will not be
8087 added to TYPE_FIELDS. */
8088 tree rtype = TREE_TYPE (r);
8089 if (can_complete_type_without_circularity (rtype))
8090 complete_type (rtype);
8092 if (!COMPLETE_TYPE_P (rtype))
8094 cxx_incomplete_type_error (r, rtype);
8095 r = error_mark_node;
8099 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8100 such a thing will already have been added to the field
8101 list by tsubst_enum in finish_member_declaration in the
8102 CLASSTYPE_NESTED_UTDS case above. */
8103 if (!(TREE_CODE (r) == TYPE_DECL
8104 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
8105 && DECL_ARTIFICIAL (r)))
8107 set_current_access_from_decl (r);
8108 finish_member_declaration (r);
8115 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
8117 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8119 tree friend_type = t;
8120 bool adjust_processing_template_decl = false;
8122 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8124 /* template <class T> friend class C; */
8125 friend_type = tsubst_friend_class (friend_type, args);
8126 adjust_processing_template_decl = true;
8128 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
8130 /* template <class T> friend class C::D; */
8131 friend_type = tsubst (friend_type, args,
8132 tf_warning_or_error, NULL_TREE);
8133 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8134 friend_type = TREE_TYPE (friend_type);
8135 adjust_processing_template_decl = true;
8137 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
8139 /* This could be either
8143 when dependent_type_p is false or
8145 template <class U> friend class T::C;
8148 friend_type = tsubst (friend_type, args,
8149 tf_warning_or_error, NULL_TREE);
8150 /* Bump processing_template_decl for correct
8151 dependent_type_p calculation. */
8152 ++processing_template_decl;
8153 if (dependent_type_p (friend_type))
8154 adjust_processing_template_decl = true;
8155 --processing_template_decl;
8157 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
8158 && hidden_name_p (TYPE_NAME (friend_type)))
8162 where C hasn't been declared yet. Let's lookup name
8163 from namespace scope directly, bypassing any name that
8164 come from dependent base class. */
8165 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
8167 /* The call to xref_tag_from_type does injection for friend
8169 push_nested_namespace (ns);
8171 xref_tag_from_type (friend_type, NULL_TREE,
8172 /*tag_scope=*/ts_current);
8173 pop_nested_namespace (ns);
8175 else if (uses_template_parms (friend_type))
8176 /* friend class C<T>; */
8177 friend_type = tsubst (friend_type, args,
8178 tf_warning_or_error, NULL_TREE);
8183 where C is already declared or
8185 friend class C<int>;
8187 We don't have to do anything in these cases. */
8189 if (adjust_processing_template_decl)
8190 /* Trick make_friend_class into realizing that the friend
8191 we're adding is a template, not an ordinary class. It's
8192 important that we use make_friend_class since it will
8193 perform some error-checking and output cross-reference
8195 ++processing_template_decl;
8197 if (friend_type != error_mark_node)
8198 make_friend_class (type, friend_type, /*complain=*/false);
8200 if (adjust_processing_template_decl)
8201 --processing_template_decl;
8205 /* Build new DECL_FRIENDLIST. */
8208 /* The file and line for this declaration, to
8209 assist in error message reporting. Since we
8210 called push_tinst_level above, we don't need to
8212 input_location = DECL_SOURCE_LOCATION (t);
8214 if (TREE_CODE (t) == TEMPLATE_DECL)
8216 ++processing_template_decl;
8217 push_deferring_access_checks (dk_no_check);
8220 r = tsubst_friend_function (t, args);
8221 add_friend (type, r, /*complain=*/false);
8222 if (TREE_CODE (t) == TEMPLATE_DECL)
8224 pop_deferring_access_checks ();
8225 --processing_template_decl;
8231 /* Set the file and line number information to whatever is given for
8232 the class itself. This puts error messages involving generated
8233 implicit functions at a predictable point, and the same point
8234 that would be used for non-template classes. */
8235 input_location = DECL_SOURCE_LOCATION (typedecl);
8237 unreverse_member_declarations (type);
8238 finish_struct_1 (type);
8239 TYPE_BEING_DEFINED (type) = 0;
8241 /* We don't instantiate default arguments for member functions. 14.7.1:
8243 The implicit instantiation of a class template specialization causes
8244 the implicit instantiation of the declarations, but not of the
8245 definitions or default arguments, of the class member functions,
8246 member classes, static data members and member templates.... */
8248 /* Some typedefs referenced from within the template code need to be access
8249 checked at template instantiation time, i.e now. These types were
8250 added to the template at parsing time. Let's get those and perform
8251 the access checks then. */
8252 perform_typedefs_access_check (pattern, args);
8253 perform_deferred_access_checks ();
8254 pop_nested_class ();
8255 maximum_field_alignment = saved_maximum_field_alignment;
8256 pop_from_top_level ();
8257 pop_deferring_access_checks ();
8260 /* The vtable for a template class can be emitted in any translation
8261 unit in which the class is instantiated. When there is no key
8262 method, however, finish_struct_1 will already have added TYPE to
8263 the keyed_classes list. */
8264 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
8265 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
8271 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8277 else if (TYPE_P (t))
8278 r = tsubst (t, args, complain, in_decl);
8281 if (!(complain & tf_warning))
8282 ++c_inhibit_evaluation_warnings;
8283 r = tsubst_expr (t, args, complain, in_decl,
8284 /*integral_constant_expression_p=*/true);
8285 if (!(complain & tf_warning))
8286 --c_inhibit_evaluation_warnings;
8291 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8292 NONTYPE_ARGUMENT_PACK. */
8295 make_fnparm_pack (tree spec_parm)
8297 /* Collect all of the extra "packed" parameters into an
8301 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
8302 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
8303 int i, len = list_length (spec_parm);
8305 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8306 parmvec = make_tree_vec (len);
8307 parmtypevec = make_tree_vec (len);
8308 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
8310 TREE_VEC_ELT (parmvec, i) = spec_parm;
8311 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
8314 /* Build the argument packs. */
8315 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
8316 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
8317 TREE_TYPE (argpack) = argtypepack;
8322 /* Substitute ARGS into T, which is an pack expansion
8323 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8324 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8325 (if only a partial substitution could be performed) or
8326 ERROR_MARK_NODE if there was an error. */
8328 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
8332 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
8336 htab_t saved_local_specializations = NULL;
8338 gcc_assert (PACK_EXPANSION_P (t));
8339 pattern = PACK_EXPANSION_PATTERN (t);
8341 /* Determine the argument packs that will instantiate the parameter
8342 packs used in the expansion expression. While we're at it,
8343 compute the number of arguments to be expanded and make sure it
8345 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
8346 pack = TREE_CHAIN (pack))
8348 tree parm_pack = TREE_VALUE (pack);
8349 tree arg_pack = NULL_TREE;
8350 tree orig_arg = NULL_TREE;
8352 if (TREE_CODE (parm_pack) == PARM_DECL)
8354 if (!cp_unevaluated_operand)
8355 arg_pack = retrieve_local_specialization (parm_pack);
8358 /* We can't rely on local_specializations for a parameter
8359 name used later in a function declaration (such as in a
8360 late-specified return type). Even if it exists, it might
8361 have the wrong value for a recursive call. Just make a
8362 dummy decl, since it's only used for its type. */
8363 arg_pack = tsubst_decl (parm_pack, args, complain);
8364 arg_pack = make_fnparm_pack (arg_pack);
8369 int level, idx, levels;
8370 template_parm_level_and_index (parm_pack, &level, &idx);
8372 levels = TMPL_ARGS_DEPTH (args);
8373 if (level <= levels)
8374 arg_pack = TMPL_ARG (args, level, idx);
8377 orig_arg = arg_pack;
8378 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
8379 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
8381 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
8382 /* This can only happen if we forget to expand an argument
8383 pack somewhere else. Just return an error, silently. */
8385 result = make_tree_vec (1);
8386 TREE_VEC_ELT (result, 0) = error_mark_node;
8391 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
8392 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
8394 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
8395 tree pattern = PACK_EXPANSION_PATTERN (expansion);
8396 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
8397 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
8398 /* The argument pack that the parameter maps to is just an
8399 expansion of the parameter itself, such as one would
8400 find in the implicit typedef of a class inside the
8401 class itself. Consider this parameter "unsubstituted",
8402 so that we will maintain the outer pack expansion. */
8403 arg_pack = NULL_TREE;
8409 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
8411 /* It's all-or-nothing with incomplete argument packs. */
8412 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8413 return error_mark_node;
8415 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8420 else if (len != my_len)
8423 /* We got explicit args for some packs but not others;
8424 do nothing now and try again after deduction. */
8426 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
8427 error ("mismatched argument pack lengths while expanding "
8431 error ("mismatched argument pack lengths while expanding "
8434 return error_mark_node;
8437 /* Keep track of the parameter packs and their corresponding
8439 packs = tree_cons (parm_pack, arg_pack, packs);
8440 TREE_TYPE (packs) = orig_arg;
8443 /* We can't substitute for this parameter pack. */
8444 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
8446 unsubstituted_packs);
8449 /* We cannot expand this expansion expression, because we don't have
8450 all of the argument packs we need. Substitute into the pattern
8451 and return a PACK_EXPANSION_*. The caller will need to deal with
8453 if (unsubstituted_packs)
8456 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8457 new_pat = tsubst_expr (pattern, args, complain, in_decl,
8458 /*integral_constant_expression_p=*/false);
8460 new_pat = tsubst (pattern, args, complain, in_decl);
8461 return make_pack_expansion (new_pat);
8464 /* We could not find any argument packs that work. */
8466 return error_mark_node;
8468 if (cp_unevaluated_operand)
8470 /* We're in a late-specified return type, so create our own local
8471 specializations table; the current table is either NULL or (in the
8472 case of recursive unification) might have bindings that we don't
8473 want to use or alter. */
8474 saved_local_specializations = local_specializations;
8475 local_specializations = htab_create (37,
8476 hash_local_specialization,
8477 eq_local_specializations,
8481 /* For each argument in each argument pack, substitute into the
8483 result = make_tree_vec (len + incomplete);
8484 for (i = 0; i < len + incomplete; ++i)
8486 /* For parameter pack, change the substitution of the parameter
8487 pack to the ith argument in its argument pack, then expand
8489 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8491 tree parm = TREE_PURPOSE (pack);
8493 if (TREE_CODE (parm) == PARM_DECL)
8495 /* Select the Ith argument from the pack. */
8496 tree arg = make_node (ARGUMENT_PACK_SELECT);
8497 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
8498 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
8500 register_local_specialization (arg, parm);
8506 template_parm_level_and_index (parm, &level, &idx);
8510 /* Select the Ith argument from the pack. */
8511 value = make_node (ARGUMENT_PACK_SELECT);
8512 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
8513 ARGUMENT_PACK_SELECT_INDEX (value) = i;
8516 /* Update the corresponding argument. */
8517 TMPL_ARG (args, level, idx) = value;
8521 /* Substitute into the PATTERN with the altered arguments. */
8522 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8523 TREE_VEC_ELT (result, i) =
8524 tsubst_expr (pattern, args, complain, in_decl,
8525 /*integral_constant_expression_p=*/false);
8527 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
8530 /* When we have incomplete argument packs, the last "expanded"
8531 result is itself a pack expansion, which allows us
8532 to deduce more arguments. */
8533 TREE_VEC_ELT (result, i) =
8534 make_pack_expansion (TREE_VEC_ELT (result, i));
8536 if (TREE_VEC_ELT (result, i) == error_mark_node)
8538 result = error_mark_node;
8543 /* Update ARGS to restore the substitution from parameter packs to
8544 their argument packs. */
8545 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8547 tree parm = TREE_PURPOSE (pack);
8549 if (TREE_CODE (parm) == PARM_DECL)
8550 register_local_specialization (TREE_TYPE (pack), parm);
8554 template_parm_level_and_index (parm, &level, &idx);
8556 /* Update the corresponding argument. */
8557 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
8558 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
8561 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
8565 if (saved_local_specializations)
8567 htab_delete (local_specializations);
8568 local_specializations = saved_local_specializations;
8574 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8575 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8576 parameter packs; all parms generated from a function parameter pack will
8577 have the same DECL_PARM_INDEX. */
8580 get_pattern_parm (tree parm, tree tmpl)
8582 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
8585 if (DECL_ARTIFICIAL (parm))
8587 for (patparm = DECL_ARGUMENTS (pattern);
8588 patparm; patparm = DECL_CHAIN (patparm))
8589 if (DECL_ARTIFICIAL (patparm)
8590 && DECL_NAME (parm) == DECL_NAME (patparm))
8595 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
8596 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
8597 gcc_assert (DECL_PARM_INDEX (patparm)
8598 == DECL_PARM_INDEX (parm));
8604 /* Substitute ARGS into the vector or list of template arguments T. */
8607 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8610 int len = TREE_VEC_LENGTH (t);
8611 int need_new = 0, i, expanded_len_adjust = 0, out;
8612 tree *elts = XALLOCAVEC (tree, len);
8614 for (i = 0; i < len; i++)
8616 tree orig_arg = TREE_VEC_ELT (t, i);
8619 if (TREE_CODE (orig_arg) == TREE_VEC)
8620 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
8621 else if (PACK_EXPANSION_P (orig_arg))
8623 /* Substitute into an expansion expression. */
8624 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
8626 if (TREE_CODE (new_arg) == TREE_VEC)
8627 /* Add to the expanded length adjustment the number of
8628 expanded arguments. We subtract one from this
8629 measurement, because the argument pack expression
8630 itself is already counted as 1 in
8631 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
8632 the argument pack is empty. */
8633 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
8635 else if (ARGUMENT_PACK_P (orig_arg))
8637 /* Substitute into each of the arguments. */
8638 new_arg = TYPE_P (orig_arg)
8639 ? cxx_make_type (TREE_CODE (orig_arg))
8640 : make_node (TREE_CODE (orig_arg));
8642 SET_ARGUMENT_PACK_ARGS (
8644 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
8645 args, complain, in_decl));
8647 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
8648 new_arg = error_mark_node;
8650 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
8651 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
8653 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
8655 if (TREE_TYPE (new_arg) == error_mark_node)
8656 new_arg = error_mark_node;
8660 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
8662 if (new_arg == error_mark_node)
8663 return error_mark_node;
8666 if (new_arg != orig_arg)
8673 /* Make space for the expanded arguments coming from template
8675 t = make_tree_vec (len + expanded_len_adjust);
8676 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
8677 arguments for a member template.
8678 In that case each TREE_VEC in ORIG_T represents a level of template
8679 arguments, and ORIG_T won't carry any non defaulted argument count.
8680 It will rather be the nested TREE_VECs that will carry one.
8681 In other words, ORIG_T carries a non defaulted argument count only
8682 if it doesn't contain any nested TREE_VEC. */
8683 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
8685 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
8686 count += expanded_len_adjust;
8687 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
8689 for (i = 0, out = 0; i < len; i++)
8691 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
8692 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
8693 && TREE_CODE (elts[i]) == TREE_VEC)
8697 /* Now expand the template argument pack "in place". */
8698 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
8699 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
8703 TREE_VEC_ELT (t, out) = elts[i];
8711 /* Return the result of substituting ARGS into the template parameters
8712 given by PARMS. If there are m levels of ARGS and m + n levels of
8713 PARMS, then the result will contain n levels of PARMS. For
8714 example, if PARMS is `template <class T> template <class U>
8715 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
8716 result will be `template <int*, double, class V>'. */
8719 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
8724 /* When substituting into a template, we must set
8725 PROCESSING_TEMPLATE_DECL as the template parameters may be
8726 dependent if they are based on one-another, and the dependency
8727 predicates are short-circuit outside of templates. */
8728 ++processing_template_decl;
8730 for (new_parms = &r;
8731 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
8732 new_parms = &(TREE_CHAIN (*new_parms)),
8733 parms = TREE_CHAIN (parms))
8736 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
8739 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
8745 if (parms == error_mark_node)
8748 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
8750 if (tuple == error_mark_node)
8753 default_value = TREE_PURPOSE (tuple);
8754 parm_decl = TREE_VALUE (tuple);
8756 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
8757 if (TREE_CODE (parm_decl) == PARM_DECL
8758 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
8759 parm_decl = error_mark_node;
8760 default_value = tsubst_template_arg (default_value, args,
8761 complain, NULL_TREE);
8763 tuple = build_tree_list (default_value, parm_decl);
8764 TREE_VEC_ELT (new_vec, i) = tuple;
8768 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
8769 - TMPL_ARGS_DEPTH (args)),
8770 new_vec, NULL_TREE);
8773 --processing_template_decl;
8778 /* Substitute the ARGS into the indicated aggregate (or enumeration)
8779 type T. If T is not an aggregate or enumeration type, it is
8780 handled as if by tsubst. IN_DECL is as for tsubst. If
8781 ENTERING_SCOPE is nonzero, T is the context for a template which
8782 we are presently tsubst'ing. Return the substituted value. */
8785 tsubst_aggr_type (tree t,
8787 tsubst_flags_t complain,
8794 switch (TREE_CODE (t))
8797 if (TYPE_PTRMEMFUNC_P (t))
8798 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
8800 /* Else fall through. */
8803 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
8808 int saved_unevaluated_operand;
8809 int saved_inhibit_evaluation_warnings;
8811 /* In "sizeof(X<I>)" we need to evaluate "I". */
8812 saved_unevaluated_operand = cp_unevaluated_operand;
8813 cp_unevaluated_operand = 0;
8814 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8815 c_inhibit_evaluation_warnings = 0;
8817 /* First, determine the context for the type we are looking
8819 context = TYPE_CONTEXT (t);
8822 context = tsubst_aggr_type (context, args, complain,
8823 in_decl, /*entering_scope=*/1);
8824 /* If context is a nested class inside a class template,
8825 it may still need to be instantiated (c++/33959). */
8826 if (TYPE_P (context))
8827 context = complete_type (context);
8830 /* Then, figure out what arguments are appropriate for the
8831 type we are trying to find. For example, given:
8833 template <class T> struct S;
8834 template <class T, class U> void f(T, U) { S<U> su; }
8836 and supposing that we are instantiating f<int, double>,
8837 then our ARGS will be {int, double}, but, when looking up
8838 S we only want {double}. */
8839 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
8841 if (argvec == error_mark_node)
8842 r = error_mark_node;
8845 r = lookup_template_class (t, argvec, in_decl, context,
8846 entering_scope, complain);
8847 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
8850 cp_unevaluated_operand = saved_unevaluated_operand;
8851 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8856 /* This is not a template type, so there's nothing to do. */
8860 return tsubst (t, args, complain, in_decl);
8864 /* Substitute into the default argument ARG (a default argument for
8865 FN), which has the indicated TYPE. */
8868 tsubst_default_argument (tree fn, tree type, tree arg)
8870 tree saved_class_ptr = NULL_TREE;
8871 tree saved_class_ref = NULL_TREE;
8873 /* This can happen in invalid code. */
8874 if (TREE_CODE (arg) == DEFAULT_ARG)
8877 /* This default argument came from a template. Instantiate the
8878 default argument here, not in tsubst. In the case of
8887 we must be careful to do name lookup in the scope of S<T>,
8888 rather than in the current class. */
8889 push_access_scope (fn);
8890 /* The "this" pointer is not valid in a default argument. */
8893 saved_class_ptr = current_class_ptr;
8894 cp_function_chain->x_current_class_ptr = NULL_TREE;
8895 saved_class_ref = current_class_ref;
8896 cp_function_chain->x_current_class_ref = NULL_TREE;
8899 push_deferring_access_checks(dk_no_deferred);
8900 /* The default argument expression may cause implicitly defined
8901 member functions to be synthesized, which will result in garbage
8902 collection. We must treat this situation as if we were within
8903 the body of function so as to avoid collecting live data on the
8906 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
8907 tf_warning_or_error, NULL_TREE,
8908 /*integral_constant_expression_p=*/false);
8910 pop_deferring_access_checks();
8912 /* Restore the "this" pointer. */
8915 cp_function_chain->x_current_class_ptr = saved_class_ptr;
8916 cp_function_chain->x_current_class_ref = saved_class_ref;
8919 /* Make sure the default argument is reasonable. */
8920 arg = check_default_argument (type, arg);
8922 pop_access_scope (fn);
8927 /* Substitute into all the default arguments for FN. */
8930 tsubst_default_arguments (tree fn)
8935 tmpl_args = DECL_TI_ARGS (fn);
8937 /* If this function is not yet instantiated, we certainly don't need
8938 its default arguments. */
8939 if (uses_template_parms (tmpl_args))
8941 /* Don't do this again for clones. */
8942 if (DECL_CLONED_FUNCTION_P (fn))
8945 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
8947 arg = TREE_CHAIN (arg))
8948 if (TREE_PURPOSE (arg))
8949 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
8951 TREE_PURPOSE (arg));
8954 /* Substitute the ARGS into the T, which is a _DECL. Return the
8955 result of the substitution. Issue error and warning messages under
8956 control of COMPLAIN. */
8959 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
8961 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
8962 location_t saved_loc;
8967 /* Set the filename and linenumber to improve error-reporting. */
8968 saved_loc = input_location;
8969 input_location = DECL_SOURCE_LOCATION (t);
8971 switch (TREE_CODE (t))
8975 /* We can get here when processing a member function template,
8976 member class template, or template template parameter. */
8977 tree decl = DECL_TEMPLATE_RESULT (t);
8982 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8984 /* Template template parameter is treated here. */
8985 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8986 if (new_type == error_mark_node)
8987 RETURN (error_mark_node);
8990 DECL_CHAIN (r) = NULL_TREE;
8991 TREE_TYPE (r) = new_type;
8992 DECL_TEMPLATE_RESULT (r)
8993 = build_decl (DECL_SOURCE_LOCATION (decl),
8994 TYPE_DECL, DECL_NAME (decl), new_type);
8995 DECL_TEMPLATE_PARMS (r)
8996 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8998 TYPE_NAME (new_type) = r;
9002 /* We might already have an instance of this template.
9003 The ARGS are for the surrounding class type, so the
9004 full args contain the tsubst'd args for the context,
9005 plus the innermost args from the template decl. */
9006 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
9007 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
9008 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
9009 /* Because this is a template, the arguments will still be
9010 dependent, even after substitution. If
9011 PROCESSING_TEMPLATE_DECL is not set, the dependency
9012 predicates will short-circuit. */
9013 ++processing_template_decl;
9014 full_args = tsubst_template_args (tmpl_args, args,
9016 --processing_template_decl;
9017 if (full_args == error_mark_node)
9018 RETURN (error_mark_node);
9020 /* If this is a default template template argument,
9021 tsubst might not have changed anything. */
9022 if (full_args == tmpl_args)
9025 hash = hash_tmpl_and_args (t, full_args);
9026 spec = retrieve_specialization (t, full_args, hash);
9027 if (spec != NULL_TREE)
9033 /* Make a new template decl. It will be similar to the
9034 original, but will record the current template arguments.
9035 We also create a new function declaration, which is just
9036 like the old one, but points to this new template, rather
9037 than the old one. */
9039 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
9040 DECL_CHAIN (r) = NULL_TREE;
9042 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
9044 if (TREE_CODE (decl) == TYPE_DECL)
9047 ++processing_template_decl;
9048 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9049 --processing_template_decl;
9050 if (new_type == error_mark_node)
9051 RETURN (error_mark_node);
9053 TREE_TYPE (r) = new_type;
9054 CLASSTYPE_TI_TEMPLATE (new_type) = r;
9055 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
9056 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
9057 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
9062 ++processing_template_decl;
9063 new_decl = tsubst (decl, args, complain, in_decl);
9064 --processing_template_decl;
9065 if (new_decl == error_mark_node)
9066 RETURN (error_mark_node);
9068 DECL_TEMPLATE_RESULT (r) = new_decl;
9069 DECL_TI_TEMPLATE (new_decl) = r;
9070 TREE_TYPE (r) = TREE_TYPE (new_decl);
9071 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
9072 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
9075 SET_DECL_IMPLICIT_INSTANTIATION (r);
9076 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
9077 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
9079 /* The template parameters for this new template are all the
9080 template parameters for the old template, except the
9081 outermost level of parameters. */
9082 DECL_TEMPLATE_PARMS (r)
9083 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9086 if (PRIMARY_TEMPLATE_P (t))
9087 DECL_PRIMARY_TEMPLATE (r) = r;
9089 if (TREE_CODE (decl) != TYPE_DECL)
9090 /* Record this non-type partial instantiation. */
9091 register_specialization (r, t,
9092 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
9100 tree argvec = NULL_TREE;
9108 /* Nobody should be tsubst'ing into non-template functions. */
9109 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
9111 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
9116 /* If T is not dependent, just return it. We have to
9117 increment PROCESSING_TEMPLATE_DECL because
9118 value_dependent_expression_p assumes that nothing is
9119 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9120 ++processing_template_decl;
9121 dependent_p = value_dependent_expression_p (t);
9122 --processing_template_decl;
9126 /* Calculate the most general template of which R is a
9127 specialization, and the complete set of arguments used to
9129 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
9130 argvec = tsubst_template_args (DECL_TI_ARGS
9131 (DECL_TEMPLATE_RESULT
9132 (DECL_TI_TEMPLATE (t))),
9133 args, complain, in_decl);
9135 /* Check to see if we already have this specialization. */
9136 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9137 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9145 /* We can see more levels of arguments than parameters if
9146 there was a specialization of a member template, like
9149 template <class T> struct S { template <class U> void f(); }
9150 template <> template <class U> void S<int>::f(U);
9152 Here, we'll be substituting into the specialization,
9153 because that's where we can find the code we actually
9154 want to generate, but we'll have enough arguments for
9155 the most general template.
9157 We also deal with the peculiar case:
9159 template <class T> struct S {
9160 template <class U> friend void f();
9162 template <class U> void f() {}
9164 template void f<double>();
9166 Here, the ARGS for the instantiation of will be {int,
9167 double}. But, we only need as many ARGS as there are
9168 levels of template parameters in CODE_PATTERN. We are
9169 careful not to get fooled into reducing the ARGS in
9172 template <class T> struct S { template <class U> void f(U); }
9173 template <class T> template <> void S<T>::f(int) {}
9175 which we can spot because the pattern will be a
9176 specialization in this case. */
9177 args_depth = TMPL_ARGS_DEPTH (args);
9179 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
9180 if (args_depth > parms_depth
9181 && !DECL_TEMPLATE_SPECIALIZATION (t))
9182 args = get_innermost_template_args (args, parms_depth);
9186 /* This special case arises when we have something like this:
9188 template <class T> struct S {
9189 friend void f<int>(int, double);
9192 Here, the DECL_TI_TEMPLATE for the friend declaration
9193 will be an IDENTIFIER_NODE. We are being called from
9194 tsubst_friend_function, and we want only to create a
9195 new decl (R) with appropriate types so that we can call
9196 determine_specialization. */
9197 gen_tmpl = NULL_TREE;
9200 if (DECL_CLASS_SCOPE_P (t))
9202 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
9206 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
9207 complain, t, /*entering_scope=*/1);
9212 ctx = DECL_CONTEXT (t);
9214 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9215 if (type == error_mark_node)
9216 RETURN (error_mark_node);
9218 /* We do NOT check for matching decls pushed separately at this
9219 point, as they may not represent instantiations of this
9220 template, and in any case are considered separate under the
9223 DECL_USE_TEMPLATE (r) = 0;
9224 TREE_TYPE (r) = type;
9225 /* Clear out the mangled name and RTL for the instantiation. */
9226 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9227 SET_DECL_RTL (r, NULL);
9228 /* Leave DECL_INITIAL set on deleted instantiations. */
9229 if (!DECL_DELETED_FN (r))
9230 DECL_INITIAL (r) = NULL_TREE;
9231 DECL_CONTEXT (r) = ctx;
9233 if (member && DECL_CONV_FN_P (r))
9234 /* Type-conversion operator. Reconstruct the name, in
9235 case it's the name of one of the template's parameters. */
9236 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
9238 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
9240 DECL_RESULT (r) = NULL_TREE;
9242 TREE_STATIC (r) = 0;
9243 TREE_PUBLIC (r) = TREE_PUBLIC (t);
9244 DECL_EXTERNAL (r) = 1;
9245 /* If this is an instantiation of a function with internal
9246 linkage, we already know what object file linkage will be
9247 assigned to the instantiation. */
9248 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
9249 DECL_DEFER_OUTPUT (r) = 0;
9250 DECL_CHAIN (r) = NULL_TREE;
9251 DECL_PENDING_INLINE_INFO (r) = 0;
9252 DECL_PENDING_INLINE_P (r) = 0;
9253 DECL_SAVED_TREE (r) = NULL_TREE;
9254 DECL_STRUCT_FUNCTION (r) = NULL;
9256 /* We'll re-clone as appropriate in instantiate_template. */
9257 DECL_CLONED_FUNCTION (r) = NULL_TREE;
9259 /* If we aren't complaining now, return on error before we register
9260 the specialization so that we'll complain eventually. */
9261 if ((complain & tf_error) == 0
9262 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9263 && !grok_op_properties (r, /*complain=*/false))
9264 RETURN (error_mark_node);
9266 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
9267 this in the special friend case mentioned above where
9268 GEN_TMPL is NULL. */
9271 DECL_TEMPLATE_INFO (r)
9272 = build_template_info (gen_tmpl, argvec);
9273 SET_DECL_IMPLICIT_INSTANTIATION (r);
9274 register_specialization (r, gen_tmpl, argvec, false, hash);
9276 /* We're not supposed to instantiate default arguments
9277 until they are called, for a template. But, for a
9280 template <class T> void f ()
9281 { extern void g(int i = T()); }
9283 we should do the substitution when the template is
9284 instantiated. We handle the member function case in
9285 instantiate_class_template since the default arguments
9286 might refer to other members of the class. */
9288 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9289 && !uses_template_parms (argvec))
9290 tsubst_default_arguments (r);
9293 DECL_TEMPLATE_INFO (r) = NULL_TREE;
9295 /* Copy the list of befriending classes. */
9296 for (friends = &DECL_BEFRIENDING_CLASSES (r);
9298 friends = &TREE_CHAIN (*friends))
9300 *friends = copy_node (*friends);
9301 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
9306 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
9308 maybe_retrofit_in_chrg (r);
9309 if (DECL_CONSTRUCTOR_P (r))
9310 grok_ctor_properties (ctx, r);
9311 /* If this is an instantiation of a member template, clone it.
9312 If it isn't, that'll be handled by
9313 clone_constructors_and_destructors. */
9314 if (PRIMARY_TEMPLATE_P (gen_tmpl))
9315 clone_function_decl (r, /*update_method_vec_p=*/0);
9317 else if ((complain & tf_error) != 0
9318 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9319 && !grok_op_properties (r, /*complain=*/true))
9320 RETURN (error_mark_node);
9322 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
9323 SET_DECL_FRIEND_CONTEXT (r,
9324 tsubst (DECL_FRIEND_CONTEXT (t),
9325 args, complain, in_decl));
9327 /* Possibly limit visibility based on template args. */
9328 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9329 if (DECL_VISIBILITY_SPECIFIED (t))
9331 DECL_VISIBILITY_SPECIFIED (r) = 0;
9333 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9335 determine_visibility (r);
9336 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
9337 && !processing_template_decl)
9338 defaulted_late_check (r);
9340 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9341 args, complain, in_decl);
9347 tree type = NULL_TREE;
9349 tree expanded_types = NULL_TREE;
9350 tree prev_r = NULL_TREE;
9351 tree first_r = NULL_TREE;
9353 if (FUNCTION_PARAMETER_PACK_P (t))
9355 /* If there is a local specialization that isn't a
9356 parameter pack, it means that we're doing a "simple"
9357 substitution from inside tsubst_pack_expansion. Just
9358 return the local specialization (which will be a single
9360 tree spec = retrieve_local_specialization (t);
9362 && TREE_CODE (spec) == PARM_DECL
9363 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
9366 /* Expand the TYPE_PACK_EXPANSION that provides the types for
9367 the parameters in this function parameter pack. */
9368 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
9370 if (TREE_CODE (expanded_types) == TREE_VEC)
9372 len = TREE_VEC_LENGTH (expanded_types);
9374 /* Zero-length parameter packs are boring. Just substitute
9377 RETURN (tsubst (TREE_CHAIN (t), args, complain,
9382 /* All we did was update the type. Make a note of that. */
9383 type = expanded_types;
9384 expanded_types = NULL_TREE;
9388 /* Loop through all of the parameter's we'll build. When T is
9389 a function parameter pack, LEN is the number of expanded
9390 types in EXPANDED_TYPES; otherwise, LEN is 1. */
9392 for (i = 0; i < len; ++i)
9396 if (DECL_TEMPLATE_PARM_P (t))
9397 SET_DECL_TEMPLATE_PARM_P (r);
9399 /* An argument of a function parameter pack is not a parameter
9401 FUNCTION_PARAMETER_PACK_P (r) = false;
9404 /* We're on the Ith parameter of the function parameter
9407 /* Get the Ith type. */
9408 type = TREE_VEC_ELT (expanded_types, i);
9411 /* Rename the parameter to include the index. */
9413 make_ith_pack_parameter_name (DECL_NAME (r), i);
9416 /* We're dealing with a normal parameter. */
9417 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9419 type = type_decays_to (type);
9420 TREE_TYPE (r) = type;
9421 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9423 if (DECL_INITIAL (r))
9425 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
9426 DECL_INITIAL (r) = TREE_TYPE (r);
9428 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
9432 DECL_CONTEXT (r) = NULL_TREE;
9434 if (!DECL_TEMPLATE_PARM_P (r))
9435 DECL_ARG_TYPE (r) = type_passed_as (type);
9437 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9438 args, complain, in_decl);
9440 /* Keep track of the first new parameter we
9441 generate. That's what will be returned to the
9446 /* Build a proper chain of parameters when substituting
9447 into a function parameter pack. */
9449 DECL_CHAIN (prev_r) = r;
9453 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
9454 complain, DECL_CHAIN (t));
9456 /* FIRST_R contains the start of the chain we've built. */
9466 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9467 if (type == error_mark_node)
9468 RETURN (error_mark_node);
9469 TREE_TYPE (r) = type;
9470 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9472 /* DECL_INITIAL gives the number of bits in a bit-field. */
9474 = tsubst_expr (DECL_INITIAL (t), args,
9476 /*integral_constant_expression_p=*/true);
9477 /* We don't have to set DECL_CONTEXT here; it is set by
9478 finish_member_declaration. */
9479 DECL_CHAIN (r) = NULL_TREE;
9480 if (VOID_TYPE_P (type))
9481 error ("instantiation of %q+D as type %qT", r, type);
9483 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9484 args, complain, in_decl);
9489 /* We reach here only for member using decls. */
9490 if (DECL_DEPENDENT_P (t))
9492 r = do_class_using_decl
9493 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
9494 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
9496 r = error_mark_node;
9499 TREE_PROTECTED (r) = TREE_PROTECTED (t);
9500 TREE_PRIVATE (r) = TREE_PRIVATE (t);
9506 DECL_CHAIN (r) = NULL_TREE;
9513 tree argvec = NULL_TREE;
9514 tree gen_tmpl = NULL_TREE;
9516 tree tmpl = NULL_TREE;
9518 tree type = NULL_TREE;
9521 if (TREE_CODE (t) == TYPE_DECL
9522 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
9524 /* If this is the canonical decl, we don't have to
9525 mess with instantiations, and often we can't (for
9526 typename, template type parms and such). Note that
9527 TYPE_NAME is not correct for the above test if
9528 we've copied the type for a typedef. */
9529 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9530 if (type == error_mark_node)
9531 RETURN (error_mark_node);
9532 r = TYPE_NAME (type);
9536 /* Check to see if we already have the specialization we
9539 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
9541 /* T is a static data member or namespace-scope entity.
9542 We have to substitute into namespace-scope variables
9543 (even though such entities are never templates) because
9546 template <class T> void f() { extern T t; }
9548 where the entity referenced is not known until
9549 instantiation time. */
9551 ctx = DECL_CONTEXT (t);
9552 if (DECL_CLASS_SCOPE_P (t))
9554 ctx = tsubst_aggr_type (ctx, args,
9556 in_decl, /*entering_scope=*/1);
9557 /* If CTX is unchanged, then T is in fact the
9558 specialization we want. That situation occurs when
9559 referencing a static data member within in its own
9560 class. We can use pointer equality, rather than
9561 same_type_p, because DECL_CONTEXT is always
9563 if (ctx == DECL_CONTEXT (t))
9569 tmpl = DECL_TI_TEMPLATE (t);
9570 gen_tmpl = most_general_template (tmpl);
9571 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
9572 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9573 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9578 /* A local variable. */
9580 /* Subsequent calls to pushdecl will fill this in. */
9582 spec = retrieve_local_specialization (t);
9584 /* If we already have the specialization we need, there is
9585 nothing more to do. */
9592 /* Create a new node for the specialization we need. */
9594 if (type == NULL_TREE)
9596 if (is_typedef_decl (t))
9597 type = DECL_ORIGINAL_TYPE (t);
9599 type = TREE_TYPE (t);
9600 if (TREE_CODE (t) == VAR_DECL && VAR_HAD_UNKNOWN_BOUND (t))
9601 type = strip_array_domain (type);
9602 type = tsubst (type, args, complain, in_decl);
9604 if (TREE_CODE (r) == VAR_DECL)
9606 /* Even if the original location is out of scope, the
9607 newly substituted one is not. */
9608 DECL_DEAD_FOR_LOCAL (r) = 0;
9609 DECL_INITIALIZED_P (r) = 0;
9610 DECL_TEMPLATE_INSTANTIATED (r) = 0;
9611 if (type == error_mark_node)
9612 RETURN (error_mark_node);
9613 if (TREE_CODE (type) == FUNCTION_TYPE)
9615 /* It may seem that this case cannot occur, since:
9620 declares a function, not a variable. However:
9623 template <typename T> void g() { T t; }
9624 template void g<f>();
9626 is an attempt to declare a variable with function
9628 error ("variable %qD has function type",
9629 /* R is not yet sufficiently initialized, so we
9630 just use its name. */
9632 RETURN (error_mark_node);
9634 type = complete_type (type);
9635 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
9636 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
9637 type = check_var_type (DECL_NAME (r), type);
9639 if (DECL_HAS_VALUE_EXPR_P (t))
9641 tree ve = DECL_VALUE_EXPR (t);
9642 ve = tsubst_expr (ve, args, complain, in_decl,
9643 /*constant_expression_p=*/false);
9644 SET_DECL_VALUE_EXPR (r, ve);
9647 else if (DECL_SELF_REFERENCE_P (t))
9648 SET_DECL_SELF_REFERENCE_P (r);
9649 TREE_TYPE (r) = type;
9650 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9651 DECL_CONTEXT (r) = ctx;
9652 /* Clear out the mangled name and RTL for the instantiation. */
9653 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9654 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9655 SET_DECL_RTL (r, NULL);
9656 /* The initializer must not be expanded until it is required;
9658 DECL_INITIAL (r) = NULL_TREE;
9659 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9660 SET_DECL_RTL (r, NULL);
9661 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
9662 if (TREE_CODE (r) == VAR_DECL)
9664 /* Possibly limit visibility based on template args. */
9665 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9666 if (DECL_VISIBILITY_SPECIFIED (t))
9668 DECL_VISIBILITY_SPECIFIED (r) = 0;
9670 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9672 determine_visibility (r);
9677 /* A static data member declaration is always marked
9678 external when it is declared in-class, even if an
9679 initializer is present. We mimic the non-template
9681 DECL_EXTERNAL (r) = 1;
9683 register_specialization (r, gen_tmpl, argvec, false, hash);
9684 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
9685 SET_DECL_IMPLICIT_INSTANTIATION (r);
9687 else if (cp_unevaluated_operand)
9689 /* We're substituting this var in a decltype outside of its
9690 scope, such as for a lambda return type. Don't add it to
9691 local_specializations, do perform auto deduction. */
9692 tree auto_node = type_uses_auto (type);
9694 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
9695 /*constant_expression_p=*/false);
9697 if (auto_node && init && describable_type (init))
9699 type = do_auto_deduction (type, init, auto_node);
9700 TREE_TYPE (r) = type;
9704 register_local_specialization (r, t);
9706 DECL_CHAIN (r) = NULL_TREE;
9708 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
9710 args, complain, in_decl);
9712 /* Preserve a typedef that names a type. */
9713 if (is_typedef_decl (r))
9715 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
9716 set_underlying_type (r);
9729 /* Restore the file and line information. */
9730 input_location = saved_loc;
9735 /* Substitute into the ARG_TYPES of a function type. */
9738 tsubst_arg_types (tree arg_types,
9740 tsubst_flags_t complain,
9743 tree remaining_arg_types;
9744 tree type = NULL_TREE;
9746 tree expanded_args = NULL_TREE;
9749 if (!arg_types || arg_types == void_list_node)
9752 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
9753 args, complain, in_decl);
9754 if (remaining_arg_types == error_mark_node)
9755 return error_mark_node;
9757 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
9759 /* For a pack expansion, perform substitution on the
9760 entire expression. Later on, we'll handle the arguments
9762 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
9763 args, complain, in_decl);
9765 if (TREE_CODE (expanded_args) == TREE_VEC)
9766 /* So that we'll spin through the parameters, one by one. */
9767 i = TREE_VEC_LENGTH (expanded_args);
9770 /* We only partially substituted into the parameter
9771 pack. Our type is TYPE_PACK_EXPANSION. */
9772 type = expanded_args;
9773 expanded_args = NULL_TREE;
9781 type = TREE_VEC_ELT (expanded_args, i);
9783 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
9785 if (type == error_mark_node)
9786 return error_mark_node;
9787 if (VOID_TYPE_P (type))
9789 if (complain & tf_error)
9791 error ("invalid parameter type %qT", type);
9793 error ("in declaration %q+D", in_decl);
9795 return error_mark_node;
9798 /* Do array-to-pointer, function-to-pointer conversion, and ignore
9799 top-level qualifiers as required. */
9800 type = TYPE_MAIN_VARIANT (type_decays_to (type));
9802 /* We do not substitute into default arguments here. The standard
9803 mandates that they be instantiated only when needed, which is
9804 done in build_over_call. */
9805 default_arg = TREE_PURPOSE (arg_types);
9807 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
9809 /* We've instantiated a template before its default arguments
9810 have been parsed. This can happen for a nested template
9811 class, and is not an error unless we require the default
9812 argument in a call of this function. */
9813 remaining_arg_types =
9814 tree_cons (default_arg, type, remaining_arg_types);
9815 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
9816 remaining_arg_types);
9819 remaining_arg_types =
9820 hash_tree_cons (default_arg, type, remaining_arg_types);
9823 return remaining_arg_types;
9826 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
9827 *not* handle the exception-specification for FNTYPE, because the
9828 initial substitution of explicitly provided template parameters
9829 during argument deduction forbids substitution into the
9830 exception-specification:
9834 All references in the function type of the function template to the
9835 corresponding template parameters are replaced by the specified tem-
9836 plate argument values. If a substitution in a template parameter or
9837 in the function type of the function template results in an invalid
9838 type, type deduction fails. [Note: The equivalent substitution in
9839 exception specifications is done only when the function is instanti-
9840 ated, at which point a program is ill-formed if the substitution
9841 results in an invalid type.] */
9844 tsubst_function_type (tree t,
9846 tsubst_flags_t complain,
9853 /* The TYPE_CONTEXT is not used for function/method types. */
9854 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
9856 /* Substitute the return type. */
9857 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9858 if (return_type == error_mark_node)
9859 return error_mark_node;
9860 /* The standard does not presently indicate that creation of a
9861 function type with an invalid return type is a deduction failure.
9862 However, that is clearly analogous to creating an array of "void"
9863 or a reference to a reference. This is core issue #486. */
9864 if (TREE_CODE (return_type) == ARRAY_TYPE
9865 || TREE_CODE (return_type) == FUNCTION_TYPE)
9867 if (complain & tf_error)
9869 if (TREE_CODE (return_type) == ARRAY_TYPE)
9870 error ("function returning an array");
9872 error ("function returning a function");
9874 return error_mark_node;
9877 /* Substitute the argument types. */
9878 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
9880 if (arg_types == error_mark_node)
9881 return error_mark_node;
9883 /* Construct a new type node and return it. */
9884 if (TREE_CODE (t) == FUNCTION_TYPE)
9886 fntype = build_function_type (return_type, arg_types);
9887 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
9891 tree r = TREE_TYPE (TREE_VALUE (arg_types));
9892 if (! MAYBE_CLASS_TYPE_P (r))
9896 Type deduction may fail for any of the following
9899 -- Attempting to create "pointer to member of T" when T
9900 is not a class type. */
9901 if (complain & tf_error)
9902 error ("creating pointer to member function of non-class type %qT",
9904 return error_mark_node;
9907 fntype = build_method_type_directly (r, return_type,
9908 TREE_CHAIN (arg_types));
9910 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
9915 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
9916 ARGS into that specification, and return the substituted
9917 specification. If there is no specification, return NULL_TREE. */
9920 tsubst_exception_specification (tree fntype,
9922 tsubst_flags_t complain,
9928 specs = TYPE_RAISES_EXCEPTIONS (fntype);
9929 new_specs = NULL_TREE;
9930 if (specs && TREE_PURPOSE (specs))
9932 /* A noexcept-specifier. */
9933 new_specs = tsubst_copy_and_build
9934 (TREE_PURPOSE (specs), args, complain, in_decl, /*function_p=*/false,
9935 /*integral_constant_expression_p=*/true);
9936 new_specs = build_noexcept_spec (new_specs, complain);
9940 if (! TREE_VALUE (specs))
9947 tree expanded_specs = NULL_TREE;
9949 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
9951 /* Expand the pack expansion type. */
9952 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
9956 if (expanded_specs == error_mark_node)
9957 return error_mark_node;
9958 else if (TREE_CODE (expanded_specs) == TREE_VEC)
9959 len = TREE_VEC_LENGTH (expanded_specs);
9962 /* We're substituting into a member template, so
9963 we got a TYPE_PACK_EXPANSION back. Add that
9964 expansion and move on. */
9965 gcc_assert (TREE_CODE (expanded_specs)
9966 == TYPE_PACK_EXPANSION);
9967 new_specs = add_exception_specifier (new_specs,
9970 specs = TREE_CHAIN (specs);
9975 for (i = 0; i < len; ++i)
9978 spec = TREE_VEC_ELT (expanded_specs, i);
9980 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
9981 if (spec == error_mark_node)
9983 new_specs = add_exception_specifier (new_specs, spec,
9987 specs = TREE_CHAIN (specs);
9993 /* Take the tree structure T and replace template parameters used
9994 therein with the argument vector ARGS. IN_DECL is an associated
9995 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
9996 Issue error and warning messages under control of COMPLAIN. Note
9997 that we must be relatively non-tolerant of extensions here, in
9998 order to preserve conformance; if we allow substitutions that
9999 should not be allowed, we may allow argument deductions that should
10000 not succeed, and therefore report ambiguous overload situations
10001 where there are none. In theory, we could allow the substitution,
10002 but indicate that it should have failed, and allow our caller to
10003 make sure that the right thing happens, but we don't try to do this
10006 This function is used for dealing with types, decls and the like;
10007 for expressions, use tsubst_expr or tsubst_copy. */
10010 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10012 enum tree_code code;
10015 if (t == NULL_TREE || t == error_mark_node
10016 || t == integer_type_node
10017 || t == void_type_node
10018 || t == char_type_node
10019 || t == unknown_type_node
10020 || TREE_CODE (t) == NAMESPACE_DECL
10021 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
10025 return tsubst_decl (t, args, complain);
10027 if (args == NULL_TREE)
10030 code = TREE_CODE (t);
10032 if (code == IDENTIFIER_NODE)
10033 type = IDENTIFIER_TYPE_VALUE (t);
10035 type = TREE_TYPE (t);
10037 gcc_assert (type != unknown_type_node);
10039 /* Reuse typedefs. We need to do this to handle dependent attributes,
10040 such as attribute aligned. */
10042 && typedef_variant_p (t))
10044 tree decl = TYPE_NAME (t);
10046 if (DECL_CLASS_SCOPE_P (decl)
10047 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
10048 && uses_template_parms (DECL_CONTEXT (decl)))
10050 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10051 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
10052 r = retrieve_specialization (tmpl, gen_args, 0);
10054 else if (DECL_FUNCTION_SCOPE_P (decl)
10055 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
10056 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
10057 r = retrieve_local_specialization (decl);
10059 /* The typedef is from a non-template context. */
10065 r = cp_build_qualified_type_real
10066 (r, cp_type_quals (t) | cp_type_quals (r),
10067 complain | tf_ignore_bad_quals);
10070 /* Else we must be instantiating the typedef, so fall through. */
10074 && code != TYPENAME_TYPE
10075 && code != TEMPLATE_TYPE_PARM
10076 && code != IDENTIFIER_NODE
10077 && code != FUNCTION_TYPE
10078 && code != METHOD_TYPE)
10079 type = tsubst (type, args, complain, in_decl);
10080 if (type == error_mark_node)
10081 return error_mark_node;
10087 case ENUMERAL_TYPE:
10088 return tsubst_aggr_type (t, args, complain, in_decl,
10089 /*entering_scope=*/0);
10092 case IDENTIFIER_NODE:
10103 if (t == integer_type_node)
10106 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
10107 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
10111 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
10113 max = tsubst_expr (omax, args, complain, in_decl,
10114 /*integral_constant_expression_p=*/false);
10116 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
10118 if (TREE_CODE (max) == NOP_EXPR
10119 && TREE_SIDE_EFFECTS (omax)
10120 && !TREE_TYPE (max))
10121 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
10123 max = mark_rvalue_use (max);
10124 max = fold_decl_constant_value (max);
10126 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
10127 with TREE_SIDE_EFFECTS that indicates this is not an integral
10128 constant expression. */
10129 if (processing_template_decl
10130 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
10132 gcc_assert (TREE_CODE (max) == NOP_EXPR);
10133 TREE_SIDE_EFFECTS (max) = 1;
10136 if (TREE_CODE (max) != INTEGER_CST
10137 && !at_function_scope_p ()
10138 && !TREE_SIDE_EFFECTS (max)
10139 && !value_dependent_expression_p (max))
10141 if (complain & tf_error)
10142 error ("array bound is not an integer constant");
10143 return error_mark_node;
10148 Type deduction may fail for any of the following
10151 Attempting to create an array with a size that is
10152 zero or negative. */
10153 if (integer_zerop (max) && !(complain & tf_error))
10154 /* We must fail if performing argument deduction (as
10155 indicated by the state of complain), so that
10156 another substitution can be found. */
10157 return error_mark_node;
10158 else if (TREE_CODE (max) == INTEGER_CST
10159 && INT_CST_LT (max, integer_zero_node))
10161 if (complain & tf_error)
10162 error ("creating array with negative size (%qE)", max);
10164 return error_mark_node;
10167 return compute_array_index_type (NULL_TREE, max);
10170 case TEMPLATE_TYPE_PARM:
10171 case TEMPLATE_TEMPLATE_PARM:
10172 case BOUND_TEMPLATE_TEMPLATE_PARM:
10173 case TEMPLATE_PARM_INDEX:
10178 tree arg = NULL_TREE;
10182 gcc_assert (TREE_VEC_LENGTH (args) > 0);
10183 template_parm_level_and_index (t, &level, &idx);
10185 levels = TMPL_ARGS_DEPTH (args);
10186 if (level <= levels)
10188 arg = TMPL_ARG (args, level, idx);
10190 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
10191 /* See through ARGUMENT_PACK_SELECT arguments. */
10192 arg = ARGUMENT_PACK_SELECT_ARG (arg);
10195 if (arg == error_mark_node)
10196 return error_mark_node;
10197 else if (arg != NULL_TREE)
10199 if (ARGUMENT_PACK_P (arg))
10200 /* If ARG is an argument pack, we don't actually want to
10201 perform a substitution here, because substitutions
10202 for argument packs are only done
10203 element-by-element. We can get to this point when
10204 substituting the type of a non-type template
10205 parameter pack, when that type actually contains
10206 template parameter packs from an outer template, e.g.,
10208 template<typename... Types> struct A {
10209 template<Types... Values> struct B { };
10213 if (code == TEMPLATE_TYPE_PARM)
10216 gcc_assert (TYPE_P (arg));
10218 quals = cp_type_quals (arg) | cp_type_quals (t);
10220 return cp_build_qualified_type_real
10221 (arg, quals, complain | tf_ignore_bad_quals);
10223 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
10225 /* We are processing a type constructed from a
10226 template template parameter. */
10227 tree argvec = tsubst (TYPE_TI_ARGS (t),
10228 args, complain, in_decl);
10229 if (argvec == error_mark_node)
10230 return error_mark_node;
10232 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
10233 are resolving nested-types in the signature of a
10234 member function templates. Otherwise ARG is a
10235 TEMPLATE_DECL and is the real template to be
10237 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
10238 arg = TYPE_NAME (arg);
10240 r = lookup_template_class (arg,
10242 DECL_CONTEXT (arg),
10243 /*entering_scope=*/0,
10245 return cp_build_qualified_type_real
10246 (r, cp_type_quals (t), complain);
10249 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
10250 return unshare_expr (arg);
10254 /* This can happen during the attempted tsubst'ing in
10255 unify. This means that we don't yet have any information
10256 about the template parameter in question. */
10259 /* If we get here, we must have been looking at a parm for a
10260 more deeply nested template. Make a new version of this
10261 template parameter, but with a lower level. */
10264 case TEMPLATE_TYPE_PARM:
10265 case TEMPLATE_TEMPLATE_PARM:
10266 case BOUND_TEMPLATE_TEMPLATE_PARM:
10267 if (cp_type_quals (t))
10269 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
10270 r = cp_build_qualified_type_real
10271 (r, cp_type_quals (t),
10272 complain | (code == TEMPLATE_TYPE_PARM
10273 ? tf_ignore_bad_quals : 0));
10278 TEMPLATE_TYPE_PARM_INDEX (r)
10279 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
10280 r, levels, args, complain);
10281 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
10282 TYPE_MAIN_VARIANT (r) = r;
10283 TYPE_POINTER_TO (r) = NULL_TREE;
10284 TYPE_REFERENCE_TO (r) = NULL_TREE;
10286 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
10287 /* We have reduced the level of the template
10288 template parameter, but not the levels of its
10289 template parameters, so canonical_type_parameter
10290 will not be able to find the canonical template
10291 template parameter for this level. Thus, we
10292 require structural equality checking to compare
10293 TEMPLATE_TEMPLATE_PARMs. */
10294 SET_TYPE_STRUCTURAL_EQUALITY (r);
10295 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
10296 SET_TYPE_STRUCTURAL_EQUALITY (r);
10298 TYPE_CANONICAL (r) = canonical_type_parameter (r);
10300 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
10302 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
10303 complain, in_decl);
10304 if (argvec == error_mark_node)
10305 return error_mark_node;
10307 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
10308 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
10313 case TEMPLATE_PARM_INDEX:
10314 r = reduce_template_parm_level (t, type, levels, args, complain);
10318 gcc_unreachable ();
10326 tree purpose, value, chain;
10328 if (t == void_list_node)
10331 purpose = TREE_PURPOSE (t);
10334 purpose = tsubst (purpose, args, complain, in_decl);
10335 if (purpose == error_mark_node)
10336 return error_mark_node;
10338 value = TREE_VALUE (t);
10341 value = tsubst (value, args, complain, in_decl);
10342 if (value == error_mark_node)
10343 return error_mark_node;
10345 chain = TREE_CHAIN (t);
10346 if (chain && chain != void_type_node)
10348 chain = tsubst (chain, args, complain, in_decl);
10349 if (chain == error_mark_node)
10350 return error_mark_node;
10352 if (purpose == TREE_PURPOSE (t)
10353 && value == TREE_VALUE (t)
10354 && chain == TREE_CHAIN (t))
10356 return hash_tree_cons (purpose, value, chain);
10360 /* We should never be tsubsting a binfo. */
10361 gcc_unreachable ();
10364 /* A vector of template arguments. */
10365 gcc_assert (!type);
10366 return tsubst_template_args (t, args, complain, in_decl);
10369 case REFERENCE_TYPE:
10371 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
10376 Type deduction may fail for any of the following
10379 -- Attempting to create a pointer to reference type.
10380 -- Attempting to create a reference to a reference type or
10381 a reference to void.
10383 Core issue 106 says that creating a reference to a reference
10384 during instantiation is no longer a cause for failure. We
10385 only enforce this check in strict C++98 mode. */
10386 if ((TREE_CODE (type) == REFERENCE_TYPE
10387 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
10388 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
10390 static location_t last_loc;
10392 /* We keep track of the last time we issued this error
10393 message to avoid spewing a ton of messages during a
10394 single bad template instantiation. */
10395 if (complain & tf_error
10396 && last_loc != input_location)
10398 if (TREE_CODE (type) == VOID_TYPE)
10399 error ("forming reference to void");
10400 else if (code == POINTER_TYPE)
10401 error ("forming pointer to reference type %qT", type);
10403 error ("forming reference to reference type %qT", type);
10404 last_loc = input_location;
10407 return error_mark_node;
10409 else if (code == POINTER_TYPE)
10411 r = build_pointer_type (type);
10412 if (TREE_CODE (type) == METHOD_TYPE)
10413 r = build_ptrmemfunc_type (r);
10415 else if (TREE_CODE (type) == REFERENCE_TYPE)
10416 /* In C++0x, during template argument substitution, when there is an
10417 attempt to create a reference to a reference type, reference
10418 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
10420 "If a template-argument for a template-parameter T names a type
10421 that is a reference to a type A, an attempt to create the type
10422 'lvalue reference to cv T' creates the type 'lvalue reference to
10423 A,' while an attempt to create the type type rvalue reference to
10424 cv T' creates the type T"
10426 r = cp_build_reference_type
10428 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
10430 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
10431 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10433 if (r != error_mark_node)
10434 /* Will this ever be needed for TYPE_..._TO values? */
10441 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
10442 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
10446 Type deduction may fail for any of the following
10449 -- Attempting to create "pointer to member of T" when T
10450 is not a class type. */
10451 if (complain & tf_error)
10452 error ("creating pointer to member of non-class type %qT", r);
10453 return error_mark_node;
10455 if (TREE_CODE (type) == REFERENCE_TYPE)
10457 if (complain & tf_error)
10458 error ("creating pointer to member reference type %qT", type);
10459 return error_mark_node;
10461 if (TREE_CODE (type) == VOID_TYPE)
10463 if (complain & tf_error)
10464 error ("creating pointer to member of type void");
10465 return error_mark_node;
10467 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
10468 if (TREE_CODE (type) == FUNCTION_TYPE)
10470 /* The type of the implicit object parameter gets its
10471 cv-qualifiers from the FUNCTION_TYPE. */
10473 tree method_type = build_memfn_type (type, r, type_memfn_quals (type));
10474 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
10475 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
10479 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
10483 case FUNCTION_TYPE:
10488 fntype = tsubst_function_type (t, args, complain, in_decl);
10489 if (fntype == error_mark_node)
10490 return error_mark_node;
10492 /* Substitute the exception specification. */
10493 specs = tsubst_exception_specification (t, args, complain,
10495 if (specs == error_mark_node)
10496 return error_mark_node;
10498 fntype = build_exception_variant (fntype, specs);
10503 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
10504 if (domain == error_mark_node)
10505 return error_mark_node;
10507 /* As an optimization, we avoid regenerating the array type if
10508 it will obviously be the same as T. */
10509 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
10512 /* These checks should match the ones in grokdeclarator.
10516 The deduction may fail for any of the following reasons:
10518 -- Attempting to create an array with an element type that
10519 is void, a function type, or a reference type, or [DR337]
10520 an abstract class type. */
10521 if (TREE_CODE (type) == VOID_TYPE
10522 || TREE_CODE (type) == FUNCTION_TYPE
10523 || TREE_CODE (type) == REFERENCE_TYPE)
10525 if (complain & tf_error)
10526 error ("creating array of %qT", type);
10527 return error_mark_node;
10529 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
10531 if (complain & tf_error)
10532 error ("creating array of %qT, which is an abstract class type",
10534 return error_mark_node;
10537 r = build_cplus_array_type (type, domain);
10539 if (TYPE_USER_ALIGN (t))
10541 TYPE_ALIGN (r) = TYPE_ALIGN (t);
10542 TYPE_USER_ALIGN (r) = 1;
10548 case TYPENAME_TYPE:
10550 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10551 in_decl, /*entering_scope=*/1);
10552 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
10553 complain, in_decl);
10555 if (ctx == error_mark_node || f == error_mark_node)
10556 return error_mark_node;
10558 if (!MAYBE_CLASS_TYPE_P (ctx))
10560 if (complain & tf_error)
10561 error ("%qT is not a class, struct, or union type", ctx);
10562 return error_mark_node;
10564 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
10566 /* Normally, make_typename_type does not require that the CTX
10567 have complete type in order to allow things like:
10569 template <class T> struct S { typename S<T>::X Y; };
10571 But, such constructs have already been resolved by this
10572 point, so here CTX really should have complete type, unless
10573 it's a partial instantiation. */
10574 if (!(complain & tf_no_class_instantiations))
10575 ctx = complete_type (ctx);
10576 if (!COMPLETE_TYPE_P (ctx))
10578 if (complain & tf_error)
10579 cxx_incomplete_type_error (NULL_TREE, ctx);
10580 return error_mark_node;
10584 f = make_typename_type (ctx, f, typename_type,
10585 (complain & tf_error) | tf_keep_type_decl);
10586 if (f == error_mark_node)
10588 if (TREE_CODE (f) == TYPE_DECL)
10590 complain |= tf_ignore_bad_quals;
10594 if (TREE_CODE (f) != TYPENAME_TYPE)
10596 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
10597 error ("%qT resolves to %qT, which is not an enumeration type",
10599 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
10600 error ("%qT resolves to %qT, which is is not a class type",
10604 return cp_build_qualified_type_real
10605 (f, cp_type_quals (f) | cp_type_quals (t), complain);
10608 case UNBOUND_CLASS_TEMPLATE:
10610 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10611 in_decl, /*entering_scope=*/1);
10612 tree name = TYPE_IDENTIFIER (t);
10613 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
10615 if (ctx == error_mark_node || name == error_mark_node)
10616 return error_mark_node;
10619 parm_list = tsubst_template_parms (parm_list, args, complain);
10620 return make_unbound_class_template (ctx, name, parm_list, complain);
10627 ++cp_unevaluated_operand;
10628 ++c_inhibit_evaluation_warnings;
10630 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
10632 /*integral_constant_expression_p=*/false);
10634 --cp_unevaluated_operand;
10635 --c_inhibit_evaluation_warnings;
10637 type = finish_typeof (type);
10638 return cp_build_qualified_type_real (type,
10640 | cp_type_quals (type),
10644 case DECLTYPE_TYPE:
10648 ++cp_unevaluated_operand;
10649 ++c_inhibit_evaluation_warnings;
10651 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
10653 /*integral_constant_expression_p=*/false);
10655 --cp_unevaluated_operand;
10656 --c_inhibit_evaluation_warnings;
10658 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
10659 type = lambda_capture_field_type (type);
10660 else if (DECLTYPE_FOR_LAMBDA_RETURN (t))
10661 type = lambda_return_type (type);
10663 type = finish_decltype_type
10664 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
10665 return cp_build_qualified_type_real (type,
10667 | cp_type_quals (type),
10671 case TYPE_ARGUMENT_PACK:
10672 case NONTYPE_ARGUMENT_PACK:
10674 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
10676 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
10680 SET_ARGUMENT_PACK_ARGS (r, packed_out);
10682 /* For template nontype argument packs, also substitute into
10684 if (code == NONTYPE_ARGUMENT_PACK)
10685 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
10703 /* We should use one of the expression tsubsts for these codes. */
10704 gcc_unreachable ();
10707 sorry ("use of %qs in template", tree_code_name [(int) code]);
10708 return error_mark_node;
10712 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
10713 type of the expression on the left-hand side of the "." or "->"
10717 tsubst_baselink (tree baselink, tree object_type,
10718 tree args, tsubst_flags_t complain, tree in_decl)
10721 tree qualifying_scope;
10724 tree template_args = 0;
10725 bool template_id_p = false;
10727 /* A baselink indicates a function from a base class. Both the
10728 BASELINK_ACCESS_BINFO and the base class referenced may
10729 indicate bases of the template class, rather than the
10730 instantiated class. In addition, lookups that were not
10731 ambiguous before may be ambiguous now. Therefore, we perform
10732 the lookup again. */
10733 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
10734 qualifying_scope = tsubst (qualifying_scope, args,
10735 complain, in_decl);
10736 fns = BASELINK_FUNCTIONS (baselink);
10737 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
10738 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
10740 template_id_p = true;
10741 template_args = TREE_OPERAND (fns, 1);
10742 fns = TREE_OPERAND (fns, 0);
10744 template_args = tsubst_template_args (template_args, args,
10745 complain, in_decl);
10747 name = DECL_NAME (get_first_fn (fns));
10748 if (IDENTIFIER_TYPENAME_P (name))
10749 name = mangle_conv_op_name_for_type (optype);
10750 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
10752 return error_mark_node;
10754 /* If lookup found a single function, mark it as used at this
10755 point. (If it lookup found multiple functions the one selected
10756 later by overload resolution will be marked as used at that
10758 if (BASELINK_P (baselink))
10759 fns = BASELINK_FUNCTIONS (baselink);
10760 if (!template_id_p && !really_overloaded_fn (fns))
10761 mark_used (OVL_CURRENT (fns));
10763 /* Add back the template arguments, if present. */
10764 if (BASELINK_P (baselink) && template_id_p)
10765 BASELINK_FUNCTIONS (baselink)
10766 = build_nt (TEMPLATE_ID_EXPR,
10767 BASELINK_FUNCTIONS (baselink),
10769 /* Update the conversion operator type. */
10770 BASELINK_OPTYPE (baselink) = optype;
10773 object_type = current_class_type;
10774 return adjust_result_of_qualified_name_lookup (baselink,
10779 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
10780 true if the qualified-id will be a postfix-expression in-and-of
10781 itself; false if more of the postfix-expression follows the
10782 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
10786 tsubst_qualified_id (tree qualified_id, tree args,
10787 tsubst_flags_t complain, tree in_decl,
10788 bool done, bool address_p)
10794 tree template_args;
10796 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
10798 /* Figure out what name to look up. */
10799 name = TREE_OPERAND (qualified_id, 1);
10800 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
10802 is_template = true;
10803 template_args = TREE_OPERAND (name, 1);
10805 template_args = tsubst_template_args (template_args, args,
10806 complain, in_decl);
10807 name = TREE_OPERAND (name, 0);
10811 is_template = false;
10812 template_args = NULL_TREE;
10815 /* Substitute into the qualifying scope. When there are no ARGS, we
10816 are just trying to simplify a non-dependent expression. In that
10817 case the qualifying scope may be dependent, and, in any case,
10818 substituting will not help. */
10819 scope = TREE_OPERAND (qualified_id, 0);
10822 scope = tsubst (scope, args, complain, in_decl);
10823 expr = tsubst_copy (name, args, complain, in_decl);
10828 if (dependent_scope_p (scope))
10829 return build_qualified_name (NULL_TREE, scope, expr,
10830 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
10832 if (!BASELINK_P (name) && !DECL_P (expr))
10834 if (TREE_CODE (expr) == BIT_NOT_EXPR)
10836 /* A BIT_NOT_EXPR is used to represent a destructor. */
10837 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
10839 error ("qualifying type %qT does not match destructor name ~%qT",
10840 scope, TREE_OPERAND (expr, 0));
10841 expr = error_mark_node;
10844 expr = lookup_qualified_name (scope, complete_dtor_identifier,
10845 /*is_type_p=*/0, false);
10848 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
10849 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
10850 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
10852 if (complain & tf_error)
10854 error ("dependent-name %qE is parsed as a non-type, but "
10855 "instantiation yields a type", qualified_id);
10856 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
10858 return error_mark_node;
10864 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
10866 /* Remember that there was a reference to this entity. */
10870 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
10872 if (complain & tf_error)
10873 qualified_name_lookup_error (scope,
10874 TREE_OPERAND (qualified_id, 1),
10875 expr, input_location);
10876 return error_mark_node;
10880 expr = lookup_template_function (expr, template_args);
10882 if (expr == error_mark_node && complain & tf_error)
10883 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
10884 expr, input_location);
10885 else if (TYPE_P (scope))
10887 expr = (adjust_result_of_qualified_name_lookup
10888 (expr, scope, current_class_type));
10889 expr = (finish_qualified_id_expr
10890 (scope, expr, done, address_p,
10891 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
10892 /*template_arg_p=*/false));
10895 /* Expressions do not generally have reference type. */
10896 if (TREE_CODE (expr) != SCOPE_REF
10897 /* However, if we're about to form a pointer-to-member, we just
10898 want the referenced member referenced. */
10899 && TREE_CODE (expr) != OFFSET_REF)
10900 expr = convert_from_reference (expr);
10905 /* Like tsubst, but deals with expressions. This function just replaces
10906 template parms; to finish processing the resultant expression, use
10910 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10912 enum tree_code code;
10915 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
10918 code = TREE_CODE (t);
10923 r = retrieve_local_specialization (t);
10928 /* This can happen for a parameter name used later in a function
10929 declaration (such as in a late-specified return type). Just
10930 make a dummy decl, since it's only used for its type. */
10931 gcc_assert (cp_unevaluated_operand != 0);
10932 /* We copy T because want to tsubst the PARM_DECL only,
10933 not the following PARM_DECLs that are chained to T. */
10935 r = tsubst_decl (c, args, complain);
10936 /* Give it the template pattern as its context; its true context
10937 hasn't been instantiated yet and this is good enough for
10939 DECL_CONTEXT (r) = DECL_CONTEXT (t);
10942 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
10943 r = ARGUMENT_PACK_SELECT_ARG (r);
10952 if (DECL_TEMPLATE_PARM_P (t))
10953 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
10954 /* There is no need to substitute into namespace-scope
10956 if (DECL_NAMESPACE_SCOPE_P (t))
10958 /* If ARGS is NULL, then T is known to be non-dependent. */
10959 if (args == NULL_TREE)
10960 return integral_constant_value (t);
10962 /* Unfortunately, we cannot just call lookup_name here.
10965 template <int I> int f() {
10967 struct S { void g() { E e = a; } };
10970 When we instantiate f<7>::S::g(), say, lookup_name is not
10971 clever enough to find f<7>::a. */
10973 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
10974 /*entering_scope=*/0);
10976 for (v = TYPE_VALUES (enum_type);
10978 v = TREE_CHAIN (v))
10979 if (TREE_PURPOSE (v) == DECL_NAME (t))
10980 return TREE_VALUE (v);
10982 /* We didn't find the name. That should never happen; if
10983 name-lookup found it during preliminary parsing, we
10984 should find it again here during instantiation. */
10985 gcc_unreachable ();
10990 if (DECL_CONTEXT (t))
10994 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
10995 /*entering_scope=*/1);
10996 if (ctx != DECL_CONTEXT (t))
10998 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
11001 if (complain & tf_error)
11002 error ("using invalid field %qD", t);
11003 return error_mark_node;
11012 case FUNCTION_DECL:
11013 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
11014 || local_variable_p (t))
11015 t = tsubst (t, args, complain, in_decl);
11020 /* An OVERLOAD will always be a non-dependent overload set; an
11021 overload set from function scope will just be represented with an
11022 IDENTIFIER_NODE, and from class scope with a BASELINK. */
11023 gcc_assert (!uses_template_parms (t));
11027 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
11029 case TEMPLATE_DECL:
11030 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11031 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
11032 args, complain, in_decl);
11033 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
11034 return tsubst (t, args, complain, in_decl);
11035 else if (DECL_CLASS_SCOPE_P (t)
11036 && uses_template_parms (DECL_CONTEXT (t)))
11038 /* Template template argument like the following example need
11041 template <template <class> class TT> struct C {};
11042 template <class T> struct D {
11043 template <class U> struct E {};
11048 We are processing the template argument `E' in #1 for
11049 the template instantiation #2. Originally, `E' is a
11050 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
11051 have to substitute this with one having context `D<int>'. */
11053 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
11054 return lookup_field (context, DECL_NAME(t), 0, false);
11057 /* Ordinary template template argument. */
11061 case REINTERPRET_CAST_EXPR:
11062 case CONST_CAST_EXPR:
11063 case STATIC_CAST_EXPR:
11064 case DYNAMIC_CAST_EXPR:
11067 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11068 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11071 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
11073 /* We only want to compute the number of arguments. */
11074 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
11075 complain, in_decl);
11078 if (TREE_CODE (expanded) == TREE_VEC)
11079 len = TREE_VEC_LENGTH (expanded);
11081 if (expanded == error_mark_node)
11082 return error_mark_node;
11083 else if (PACK_EXPANSION_P (expanded)
11084 || (TREE_CODE (expanded) == TREE_VEC
11086 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
11088 if (TREE_CODE (expanded) == TREE_VEC)
11089 expanded = TREE_VEC_ELT (expanded, len - 1);
11091 if (TYPE_P (expanded))
11092 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
11093 complain & tf_error);
11095 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
11096 complain & tf_error);
11099 return build_int_cst (size_type_node, len);
11105 case TRUTH_NOT_EXPR:
11108 case UNARY_PLUS_EXPR: /* Unary + */
11110 case AT_ENCODE_EXPR:
11114 case REALPART_EXPR:
11115 case IMAGPART_EXPR:
11117 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11118 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11120 case COMPONENT_REF:
11125 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
11126 name = TREE_OPERAND (t, 1);
11127 if (TREE_CODE (name) == BIT_NOT_EXPR)
11129 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11130 complain, in_decl);
11131 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11133 else if (TREE_CODE (name) == SCOPE_REF
11134 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
11136 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
11137 complain, in_decl);
11138 name = TREE_OPERAND (name, 1);
11139 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11140 complain, in_decl);
11141 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11142 name = build_qualified_name (/*type=*/NULL_TREE,
11144 /*template_p=*/false);
11146 else if (TREE_CODE (name) == BASELINK)
11147 name = tsubst_baselink (name,
11148 non_reference (TREE_TYPE (object)),
11152 name = tsubst_copy (name, args, complain, in_decl);
11153 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
11159 case TRUNC_DIV_EXPR:
11160 case CEIL_DIV_EXPR:
11161 case FLOOR_DIV_EXPR:
11162 case ROUND_DIV_EXPR:
11163 case EXACT_DIV_EXPR:
11167 case TRUNC_MOD_EXPR:
11168 case FLOOR_MOD_EXPR:
11169 case TRUTH_ANDIF_EXPR:
11170 case TRUTH_ORIF_EXPR:
11171 case TRUTH_AND_EXPR:
11172 case TRUTH_OR_EXPR:
11185 case COMPOUND_EXPR:
11188 case PREDECREMENT_EXPR:
11189 case PREINCREMENT_EXPR:
11190 case POSTDECREMENT_EXPR:
11191 case POSTINCREMENT_EXPR:
11193 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11194 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11197 return build_qualified_name (/*type=*/NULL_TREE,
11198 tsubst_copy (TREE_OPERAND (t, 0),
11199 args, complain, in_decl),
11200 tsubst_copy (TREE_OPERAND (t, 1),
11201 args, complain, in_decl),
11202 QUALIFIED_NAME_IS_TEMPLATE (t));
11207 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11208 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11209 NULL_TREE, NULL_TREE);
11213 int n = VL_EXP_OPERAND_LENGTH (t);
11214 tree result = build_vl_exp (CALL_EXPR, n);
11216 for (i = 0; i < n; i++)
11217 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
11218 complain, in_decl);
11224 case PSEUDO_DTOR_EXPR:
11227 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11228 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11229 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
11230 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
11237 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11238 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11239 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
11240 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
11247 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11248 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11249 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
11250 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
11254 case TEMPLATE_ID_EXPR:
11256 /* Substituted template arguments */
11257 tree fn = TREE_OPERAND (t, 0);
11258 tree targs = TREE_OPERAND (t, 1);
11260 fn = tsubst_copy (fn, args, complain, in_decl);
11262 targs = tsubst_template_args (targs, args, complain, in_decl);
11264 return lookup_template_function (fn, targs);
11269 tree purpose, value, chain;
11271 if (t == void_list_node)
11274 purpose = TREE_PURPOSE (t);
11276 purpose = tsubst_copy (purpose, args, complain, in_decl);
11277 value = TREE_VALUE (t);
11279 value = tsubst_copy (value, args, complain, in_decl);
11280 chain = TREE_CHAIN (t);
11281 if (chain && chain != void_type_node)
11282 chain = tsubst_copy (chain, args, complain, in_decl);
11283 if (purpose == TREE_PURPOSE (t)
11284 && value == TREE_VALUE (t)
11285 && chain == TREE_CHAIN (t))
11287 return tree_cons (purpose, value, chain);
11292 case ENUMERAL_TYPE:
11294 case TEMPLATE_TYPE_PARM:
11295 case TEMPLATE_TEMPLATE_PARM:
11296 case BOUND_TEMPLATE_TEMPLATE_PARM:
11297 case TEMPLATE_PARM_INDEX:
11299 case REFERENCE_TYPE:
11301 case FUNCTION_TYPE:
11304 case TYPENAME_TYPE:
11305 case UNBOUND_CLASS_TEMPLATE:
11307 case DECLTYPE_TYPE:
11309 return tsubst (t, args, complain, in_decl);
11311 case IDENTIFIER_NODE:
11312 if (IDENTIFIER_TYPENAME_P (t))
11314 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11315 return mangle_conv_op_name_for_type (new_type);
11321 /* This is handled by tsubst_copy_and_build. */
11322 gcc_unreachable ();
11325 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
11327 tsubst (TREE_TYPE (t), args, complain, in_decl));
11329 case CLEANUP_POINT_EXPR:
11330 /* We shouldn't have built any of these during initial template
11331 generation. Instead, they should be built during instantiation
11332 in response to the saved STMT_IS_FULL_EXPR_P setting. */
11333 gcc_unreachable ();
11337 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11338 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11339 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11340 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
11341 mark_used (TREE_OPERAND (r, 1));
11344 case EXPR_PACK_EXPANSION:
11345 error ("invalid use of pack expansion expression");
11346 return error_mark_node;
11348 case NONTYPE_ARGUMENT_PACK:
11349 error ("use %<...%> to expand argument pack");
11350 return error_mark_node;
11356 /* Instantiate any typedefs in the type. */
11357 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11358 r = fold_convert (type, t);
11359 gcc_assert (TREE_CODE (r) == code);
11364 /* These can sometimes show up in a partial instantiation, but never
11365 involve template parms. */
11366 gcc_assert (!uses_template_parms (t));
11370 gcc_unreachable ();
11374 /* Like tsubst_copy, but specifically for OpenMP clauses. */
11377 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
11380 tree new_clauses = NULL, nc, oc;
11382 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
11384 nc = copy_node (oc);
11385 OMP_CLAUSE_CHAIN (nc) = new_clauses;
11388 switch (OMP_CLAUSE_CODE (nc))
11390 case OMP_CLAUSE_LASTPRIVATE:
11391 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
11393 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
11394 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
11395 in_decl, /*integral_constant_expression_p=*/false);
11396 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
11397 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
11400 case OMP_CLAUSE_PRIVATE:
11401 case OMP_CLAUSE_SHARED:
11402 case OMP_CLAUSE_FIRSTPRIVATE:
11403 case OMP_CLAUSE_REDUCTION:
11404 case OMP_CLAUSE_COPYIN:
11405 case OMP_CLAUSE_COPYPRIVATE:
11406 case OMP_CLAUSE_IF:
11407 case OMP_CLAUSE_NUM_THREADS:
11408 case OMP_CLAUSE_SCHEDULE:
11409 case OMP_CLAUSE_COLLAPSE:
11410 OMP_CLAUSE_OPERAND (nc, 0)
11411 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
11412 in_decl, /*integral_constant_expression_p=*/false);
11414 case OMP_CLAUSE_NOWAIT:
11415 case OMP_CLAUSE_ORDERED:
11416 case OMP_CLAUSE_DEFAULT:
11417 case OMP_CLAUSE_UNTIED:
11420 gcc_unreachable ();
11424 return finish_omp_clauses (nreverse (new_clauses));
11427 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
11430 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
11433 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
11435 tree purpose, value, chain;
11440 if (TREE_CODE (t) != TREE_LIST)
11441 return tsubst_copy_and_build (t, args, complain, in_decl,
11442 /*function_p=*/false,
11443 /*integral_constant_expression_p=*/false);
11445 if (t == void_list_node)
11448 purpose = TREE_PURPOSE (t);
11450 purpose = RECUR (purpose);
11451 value = TREE_VALUE (t);
11452 if (value && TREE_CODE (value) != LABEL_DECL)
11453 value = RECUR (value);
11454 chain = TREE_CHAIN (t);
11455 if (chain && chain != void_type_node)
11456 chain = RECUR (chain);
11457 return tree_cons (purpose, value, chain);
11461 /* Substitute one OMP_FOR iterator. */
11464 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
11465 tree condv, tree incrv, tree *clauses,
11466 tree args, tsubst_flags_t complain, tree in_decl,
11467 bool integral_constant_expression_p)
11469 #define RECUR(NODE) \
11470 tsubst_expr ((NODE), args, complain, in_decl, \
11471 integral_constant_expression_p)
11472 tree decl, init, cond, incr, auto_node;
11474 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
11475 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
11476 decl = RECUR (TREE_OPERAND (init, 0));
11477 init = TREE_OPERAND (init, 1);
11478 auto_node = type_uses_auto (TREE_TYPE (decl));
11479 if (auto_node && init)
11481 tree init_expr = init;
11482 if (TREE_CODE (init_expr) == DECL_EXPR)
11483 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
11484 init_expr = RECUR (init_expr);
11486 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
11488 gcc_assert (!type_dependent_expression_p (decl));
11490 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
11492 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
11493 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11494 if (TREE_CODE (incr) == MODIFY_EXPR)
11495 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
11496 RECUR (TREE_OPERAND (incr, 1)),
11499 incr = RECUR (incr);
11500 TREE_VEC_ELT (declv, i) = decl;
11501 TREE_VEC_ELT (initv, i) = init;
11502 TREE_VEC_ELT (condv, i) = cond;
11503 TREE_VEC_ELT (incrv, i) = incr;
11507 if (init && TREE_CODE (init) != DECL_EXPR)
11510 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11512 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
11513 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
11514 && OMP_CLAUSE_DECL (c) == decl)
11516 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
11517 && OMP_CLAUSE_DECL (c) == decl)
11518 error ("iteration variable %qD should not be firstprivate", decl);
11519 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
11520 && OMP_CLAUSE_DECL (c) == decl)
11521 error ("iteration variable %qD should not be reduction", decl);
11525 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
11526 OMP_CLAUSE_DECL (c) = decl;
11527 c = finish_omp_clauses (c);
11530 OMP_CLAUSE_CHAIN (c) = *clauses;
11535 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
11536 if (COMPARISON_CLASS_P (cond))
11537 cond = build2 (TREE_CODE (cond), boolean_type_node,
11538 RECUR (TREE_OPERAND (cond, 0)),
11539 RECUR (TREE_OPERAND (cond, 1)));
11541 cond = RECUR (cond);
11542 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11543 switch (TREE_CODE (incr))
11545 case PREINCREMENT_EXPR:
11546 case PREDECREMENT_EXPR:
11547 case POSTINCREMENT_EXPR:
11548 case POSTDECREMENT_EXPR:
11549 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
11550 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
11553 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11554 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11556 tree rhs = TREE_OPERAND (incr, 1);
11557 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11558 RECUR (TREE_OPERAND (incr, 0)),
11559 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11560 RECUR (TREE_OPERAND (rhs, 0)),
11561 RECUR (TREE_OPERAND (rhs, 1))));
11564 incr = RECUR (incr);
11567 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11568 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11570 tree lhs = RECUR (TREE_OPERAND (incr, 0));
11571 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
11572 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
11573 TREE_TYPE (decl), lhs,
11574 RECUR (TREE_OPERAND (incr, 2))));
11576 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
11577 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
11578 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
11580 tree rhs = TREE_OPERAND (incr, 2);
11581 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11582 RECUR (TREE_OPERAND (incr, 0)),
11583 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11584 RECUR (TREE_OPERAND (rhs, 0)),
11585 RECUR (TREE_OPERAND (rhs, 1))));
11588 incr = RECUR (incr);
11591 incr = RECUR (incr);
11595 TREE_VEC_ELT (declv, i) = decl;
11596 TREE_VEC_ELT (initv, i) = init;
11597 TREE_VEC_ELT (condv, i) = cond;
11598 TREE_VEC_ELT (incrv, i) = incr;
11602 /* Like tsubst_copy for expressions, etc. but also does semantic
11606 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
11607 bool integral_constant_expression_p)
11609 #define RECUR(NODE) \
11610 tsubst_expr ((NODE), args, complain, in_decl, \
11611 integral_constant_expression_p)
11615 if (t == NULL_TREE || t == error_mark_node)
11618 if (EXPR_HAS_LOCATION (t))
11619 input_location = EXPR_LOCATION (t);
11620 if (STATEMENT_CODE_P (TREE_CODE (t)))
11621 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
11623 switch (TREE_CODE (t))
11625 case STATEMENT_LIST:
11627 tree_stmt_iterator i;
11628 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
11629 RECUR (tsi_stmt (i));
11633 case CTOR_INITIALIZER:
11634 finish_mem_initializers (tsubst_initializer_list
11635 (TREE_OPERAND (t, 0), args));
11639 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
11643 tmp = RECUR (EXPR_STMT_EXPR (t));
11644 if (EXPR_STMT_STMT_EXPR_RESULT (t))
11645 finish_stmt_expr_expr (tmp, cur_stmt_expr);
11647 finish_expr_stmt (tmp);
11651 do_using_directive (USING_STMT_NAMESPACE (t));
11659 decl = DECL_EXPR_DECL (t);
11660 if (TREE_CODE (decl) == LABEL_DECL)
11661 finish_label_decl (DECL_NAME (decl));
11662 else if (TREE_CODE (decl) == USING_DECL)
11664 tree scope = USING_DECL_SCOPE (decl);
11665 tree name = DECL_NAME (decl);
11668 scope = tsubst (scope, args, complain, in_decl);
11669 decl = lookup_qualified_name (scope, name,
11670 /*is_type_p=*/false,
11671 /*complain=*/false);
11672 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
11673 qualified_name_lookup_error (scope, name, decl, input_location);
11675 do_local_using_decl (decl, scope, name);
11679 init = DECL_INITIAL (decl);
11680 decl = tsubst (decl, args, complain, in_decl);
11681 if (decl != error_mark_node)
11683 /* By marking the declaration as instantiated, we avoid
11684 trying to instantiate it. Since instantiate_decl can't
11685 handle local variables, and since we've already done
11686 all that needs to be done, that's the right thing to
11688 if (TREE_CODE (decl) == VAR_DECL)
11689 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11690 if (TREE_CODE (decl) == VAR_DECL
11691 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
11692 /* Anonymous aggregates are a special case. */
11693 finish_anon_union (decl);
11696 maybe_push_decl (decl);
11697 if (TREE_CODE (decl) == VAR_DECL
11698 && DECL_PRETTY_FUNCTION_P (decl))
11700 /* For __PRETTY_FUNCTION__ we have to adjust the
11702 const char *const name
11703 = cxx_printable_name (current_function_decl, 2);
11704 init = cp_fname_init (name, &TREE_TYPE (decl));
11708 tree t = RECUR (init);
11712 /* If we had an initializer but it
11713 instantiated to nothing,
11714 value-initialize the object. This will
11715 only occur when the initializer was a
11716 pack expansion where the parameter packs
11717 used in that expansion were of length
11719 init = build_value_init (TREE_TYPE (decl),
11721 if (TREE_CODE (init) == AGGR_INIT_EXPR)
11722 init = get_target_expr (init);
11728 cp_finish_decl (decl, init, false, NULL_TREE, 0);
11733 /* A DECL_EXPR can also be used as an expression, in the condition
11734 clause of an if/for/while construct. */
11739 stmt = begin_for_stmt ();
11740 RECUR (FOR_INIT_STMT (t));
11741 finish_for_init_stmt (stmt);
11742 tmp = RECUR (FOR_COND (t));
11743 finish_for_cond (tmp, stmt);
11744 tmp = RECUR (FOR_EXPR (t));
11745 finish_for_expr (tmp, stmt);
11746 RECUR (FOR_BODY (t));
11747 finish_for_stmt (stmt);
11750 case RANGE_FOR_STMT:
11753 stmt = begin_for_stmt ();
11754 decl = RANGE_FOR_DECL (t);
11755 decl = tsubst (decl, args, complain, in_decl);
11756 maybe_push_decl (decl);
11757 expr = RECUR (RANGE_FOR_EXPR (t));
11758 stmt = cp_convert_range_for (stmt, decl, expr);
11759 RECUR (RANGE_FOR_BODY (t));
11760 finish_for_stmt (stmt);
11765 stmt = begin_while_stmt ();
11766 tmp = RECUR (WHILE_COND (t));
11767 finish_while_stmt_cond (tmp, stmt);
11768 RECUR (WHILE_BODY (t));
11769 finish_while_stmt (stmt);
11773 stmt = begin_do_stmt ();
11774 RECUR (DO_BODY (t));
11775 finish_do_body (stmt);
11776 tmp = RECUR (DO_COND (t));
11777 finish_do_stmt (tmp, stmt);
11781 stmt = begin_if_stmt ();
11782 tmp = RECUR (IF_COND (t));
11783 finish_if_stmt_cond (tmp, stmt);
11784 RECUR (THEN_CLAUSE (t));
11785 finish_then_clause (stmt);
11787 if (ELSE_CLAUSE (t))
11789 begin_else_clause (stmt);
11790 RECUR (ELSE_CLAUSE (t));
11791 finish_else_clause (stmt);
11794 finish_if_stmt (stmt);
11798 if (BIND_EXPR_BODY_BLOCK (t))
11799 stmt = begin_function_body ();
11801 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
11802 ? BCS_TRY_BLOCK : 0);
11804 RECUR (BIND_EXPR_BODY (t));
11806 if (BIND_EXPR_BODY_BLOCK (t))
11807 finish_function_body (stmt);
11809 finish_compound_stmt (stmt);
11813 finish_break_stmt ();
11816 case CONTINUE_STMT:
11817 finish_continue_stmt ();
11821 stmt = begin_switch_stmt ();
11822 tmp = RECUR (SWITCH_STMT_COND (t));
11823 finish_switch_cond (tmp, stmt);
11824 RECUR (SWITCH_STMT_BODY (t));
11825 finish_switch_stmt (stmt);
11828 case CASE_LABEL_EXPR:
11829 finish_case_label (EXPR_LOCATION (t),
11830 RECUR (CASE_LOW (t)),
11831 RECUR (CASE_HIGH (t)));
11836 tree decl = LABEL_EXPR_LABEL (t);
11839 label = finish_label_stmt (DECL_NAME (decl));
11840 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
11841 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
11846 tmp = GOTO_DESTINATION (t);
11847 if (TREE_CODE (tmp) != LABEL_DECL)
11848 /* Computed goto's must be tsubst'd into. On the other hand,
11849 non-computed gotos must not be; the identifier in question
11850 will have no binding. */
11853 tmp = DECL_NAME (tmp);
11854 finish_goto_stmt (tmp);
11858 tmp = finish_asm_stmt
11859 (ASM_VOLATILE_P (t),
11860 RECUR (ASM_STRING (t)),
11861 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
11862 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
11863 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
11864 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
11866 tree asm_expr = tmp;
11867 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
11868 asm_expr = TREE_OPERAND (asm_expr, 0);
11869 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
11876 stmt = begin_try_block ();
11877 RECUR (TRY_STMTS (t));
11878 finish_cleanup_try_block (stmt);
11879 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
11883 tree compound_stmt = NULL_TREE;
11885 if (FN_TRY_BLOCK_P (t))
11886 stmt = begin_function_try_block (&compound_stmt);
11888 stmt = begin_try_block ();
11890 RECUR (TRY_STMTS (t));
11892 if (FN_TRY_BLOCK_P (t))
11893 finish_function_try_block (stmt);
11895 finish_try_block (stmt);
11897 RECUR (TRY_HANDLERS (t));
11898 if (FN_TRY_BLOCK_P (t))
11899 finish_function_handler_sequence (stmt, compound_stmt);
11901 finish_handler_sequence (stmt);
11907 tree decl = HANDLER_PARMS (t);
11911 decl = tsubst (decl, args, complain, in_decl);
11912 /* Prevent instantiate_decl from trying to instantiate
11913 this variable. We've already done all that needs to be
11915 if (decl != error_mark_node)
11916 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11918 stmt = begin_handler ();
11919 finish_handler_parms (decl, stmt);
11920 RECUR (HANDLER_BODY (t));
11921 finish_handler (stmt);
11926 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
11929 case STATIC_ASSERT:
11932 tsubst_expr (STATIC_ASSERT_CONDITION (t),
11935 /*integral_constant_expression_p=*/true);
11936 finish_static_assert (condition,
11937 STATIC_ASSERT_MESSAGE (t),
11938 STATIC_ASSERT_SOURCE_LOCATION (t),
11939 /*member_p=*/false);
11944 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
11945 args, complain, in_decl);
11946 stmt = begin_omp_parallel ();
11947 RECUR (OMP_PARALLEL_BODY (t));
11948 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
11949 = OMP_PARALLEL_COMBINED (t);
11953 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
11954 args, complain, in_decl);
11955 stmt = begin_omp_task ();
11956 RECUR (OMP_TASK_BODY (t));
11957 finish_omp_task (tmp, stmt);
11962 tree clauses, body, pre_body;
11963 tree declv, initv, condv, incrv;
11966 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
11967 args, complain, in_decl);
11968 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11969 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11970 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11971 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11973 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
11974 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
11975 &clauses, args, complain, in_decl,
11976 integral_constant_expression_p);
11978 stmt = begin_omp_structured_block ();
11980 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
11981 if (TREE_VEC_ELT (initv, i) == NULL
11982 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
11983 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
11984 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
11986 tree init = RECUR (TREE_VEC_ELT (initv, i));
11987 gcc_assert (init == TREE_VEC_ELT (declv, i));
11988 TREE_VEC_ELT (initv, i) = NULL_TREE;
11992 tree decl_expr = TREE_VEC_ELT (initv, i);
11993 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
11994 gcc_assert (init != NULL);
11995 TREE_VEC_ELT (initv, i) = RECUR (init);
11996 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
11998 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
12001 pre_body = push_stmt_list ();
12002 RECUR (OMP_FOR_PRE_BODY (t));
12003 pre_body = pop_stmt_list (pre_body);
12005 body = push_stmt_list ();
12006 RECUR (OMP_FOR_BODY (t));
12007 body = pop_stmt_list (body);
12009 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
12010 body, pre_body, clauses);
12012 add_stmt (finish_omp_structured_block (stmt));
12018 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
12019 stmt = push_stmt_list ();
12020 RECUR (OMP_BODY (t));
12021 stmt = pop_stmt_list (stmt);
12024 OMP_BODY (t) = stmt;
12025 OMP_CLAUSES (t) = tmp;
12033 stmt = push_stmt_list ();
12034 RECUR (OMP_BODY (t));
12035 stmt = pop_stmt_list (stmt);
12038 OMP_BODY (t) = stmt;
12043 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
12045 tree op1 = TREE_OPERAND (t, 1);
12046 tree lhs = RECUR (TREE_OPERAND (op1, 0));
12047 tree rhs = RECUR (TREE_OPERAND (op1, 1));
12048 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
12052 case EXPR_PACK_EXPANSION:
12053 error ("invalid use of pack expansion expression");
12054 return error_mark_node;
12056 case NONTYPE_ARGUMENT_PACK:
12057 error ("use %<...%> to expand argument pack");
12058 return error_mark_node;
12061 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
12063 return tsubst_copy_and_build (t, args, complain, in_decl,
12064 /*function_p=*/false,
12065 integral_constant_expression_p);
12072 /* T is a postfix-expression that is not being used in a function
12073 call. Return the substituted version of T. */
12076 tsubst_non_call_postfix_expression (tree t, tree args,
12077 tsubst_flags_t complain,
12080 if (TREE_CODE (t) == SCOPE_REF)
12081 t = tsubst_qualified_id (t, args, complain, in_decl,
12082 /*done=*/false, /*address_p=*/false);
12084 t = tsubst_copy_and_build (t, args, complain, in_decl,
12085 /*function_p=*/false,
12086 /*integral_constant_expression_p=*/false);
12091 /* Like tsubst but deals with expressions and performs semantic
12092 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
12095 tsubst_copy_and_build (tree t,
12097 tsubst_flags_t complain,
12100 bool integral_constant_expression_p)
12102 #define RECUR(NODE) \
12103 tsubst_copy_and_build (NODE, args, complain, in_decl, \
12104 /*function_p=*/false, \
12105 integral_constant_expression_p)
12109 if (t == NULL_TREE || t == error_mark_node)
12112 switch (TREE_CODE (t))
12116 /* Fall through. */
12117 case IDENTIFIER_NODE:
12121 bool non_integral_constant_expression_p;
12122 const char *error_msg;
12124 if (IDENTIFIER_TYPENAME_P (t))
12126 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12127 t = mangle_conv_op_name_for_type (new_type);
12130 /* Look up the name. */
12131 decl = lookup_name (t);
12133 /* By convention, expressions use ERROR_MARK_NODE to indicate
12134 failure, not NULL_TREE. */
12135 if (decl == NULL_TREE)
12136 decl = error_mark_node;
12138 decl = finish_id_expression (t, decl, NULL_TREE,
12140 integral_constant_expression_p,
12141 /*allow_non_integral_constant_expression_p=*/false,
12142 &non_integral_constant_expression_p,
12143 /*template_p=*/false,
12145 /*address_p=*/false,
12146 /*template_arg_p=*/false,
12151 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
12152 decl = unqualified_name_lookup_error (decl);
12156 case TEMPLATE_ID_EXPR:
12159 tree templ = RECUR (TREE_OPERAND (t, 0));
12160 tree targs = TREE_OPERAND (t, 1);
12163 targs = tsubst_template_args (targs, args, complain, in_decl);
12165 if (TREE_CODE (templ) == COMPONENT_REF)
12167 object = TREE_OPERAND (templ, 0);
12168 templ = TREE_OPERAND (templ, 1);
12171 object = NULL_TREE;
12172 templ = lookup_template_function (templ, targs);
12175 return build3 (COMPONENT_REF, TREE_TYPE (templ),
12176 object, templ, NULL_TREE);
12178 return baselink_for_fns (templ);
12183 tree r = RECUR (TREE_OPERAND (t, 0));
12185 if (REFERENCE_REF_P (t))
12187 /* A type conversion to reference type will be enclosed in
12188 such an indirect ref, but the substitution of the cast
12189 will have also added such an indirect ref. */
12190 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
12191 r = convert_from_reference (r);
12194 r = build_x_indirect_ref (r, RO_UNARY_STAR, complain);
12200 (tsubst (TREE_TYPE (t), args, complain, in_decl),
12201 RECUR (TREE_OPERAND (t, 0)));
12204 case REINTERPRET_CAST_EXPR:
12205 case CONST_CAST_EXPR:
12206 case DYNAMIC_CAST_EXPR:
12207 case STATIC_CAST_EXPR:
12212 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12213 if (integral_constant_expression_p
12214 && !cast_valid_in_integral_constant_expression_p (type))
12216 if (complain & tf_error)
12217 error ("a cast to a type other than an integral or "
12218 "enumeration type cannot appear in a constant-expression");
12219 return error_mark_node;
12222 op = RECUR (TREE_OPERAND (t, 0));
12224 switch (TREE_CODE (t))
12227 return build_functional_cast (type, op, complain);
12228 case REINTERPRET_CAST_EXPR:
12229 return build_reinterpret_cast (type, op, complain);
12230 case CONST_CAST_EXPR:
12231 return build_const_cast (type, op, complain);
12232 case DYNAMIC_CAST_EXPR:
12233 return build_dynamic_cast (type, op, complain);
12234 case STATIC_CAST_EXPR:
12235 return build_static_cast (type, op, complain);
12237 gcc_unreachable ();
12241 case POSTDECREMENT_EXPR:
12242 case POSTINCREMENT_EXPR:
12243 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12244 args, complain, in_decl);
12245 return build_x_unary_op (TREE_CODE (t), op1, complain);
12247 case PREDECREMENT_EXPR:
12248 case PREINCREMENT_EXPR:
12252 case TRUTH_NOT_EXPR:
12253 case UNARY_PLUS_EXPR: /* Unary + */
12254 case REALPART_EXPR:
12255 case IMAGPART_EXPR:
12256 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
12260 op1 = TREE_OPERAND (t, 0);
12261 if (TREE_CODE (op1) == LABEL_DECL)
12262 return finish_label_address_expr (DECL_NAME (op1),
12263 EXPR_LOCATION (op1));
12264 if (TREE_CODE (op1) == SCOPE_REF)
12265 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
12266 /*done=*/true, /*address_p=*/true);
12268 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
12270 return build_x_unary_op (ADDR_EXPR, op1, complain);
12275 case TRUNC_DIV_EXPR:
12276 case CEIL_DIV_EXPR:
12277 case FLOOR_DIV_EXPR:
12278 case ROUND_DIV_EXPR:
12279 case EXACT_DIV_EXPR:
12283 case TRUNC_MOD_EXPR:
12284 case FLOOR_MOD_EXPR:
12285 case TRUTH_ANDIF_EXPR:
12286 case TRUTH_ORIF_EXPR:
12287 case TRUTH_AND_EXPR:
12288 case TRUTH_OR_EXPR:
12303 return build_x_binary_op
12305 RECUR (TREE_OPERAND (t, 0)),
12306 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
12308 : TREE_CODE (TREE_OPERAND (t, 0))),
12309 RECUR (TREE_OPERAND (t, 1)),
12310 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
12312 : TREE_CODE (TREE_OPERAND (t, 1))),
12313 /*overloaded_p=*/NULL,
12317 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
12318 /*address_p=*/false);
12320 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12321 args, complain, in_decl);
12322 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
12325 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12326 return tsubst_copy (t, args, complain, in_decl);
12330 op1 = TREE_OPERAND (t, 0);
12333 /* When there are no ARGS, we are trying to evaluate a
12334 non-dependent expression from the parser. Trying to do
12335 the substitutions may not work. */
12337 op1 = TREE_TYPE (op1);
12341 ++cp_unevaluated_operand;
12342 ++c_inhibit_evaluation_warnings;
12343 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12344 /*function_p=*/false,
12345 /*integral_constant_expression_p=*/false);
12346 --cp_unevaluated_operand;
12347 --c_inhibit_evaluation_warnings;
12350 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
12351 complain & tf_error);
12353 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
12354 complain & tf_error);
12356 case AT_ENCODE_EXPR:
12358 op1 = TREE_OPERAND (t, 0);
12359 ++cp_unevaluated_operand;
12360 ++c_inhibit_evaluation_warnings;
12361 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12362 /*function_p=*/false,
12363 /*integral_constant_expression_p=*/false);
12364 --cp_unevaluated_operand;
12365 --c_inhibit_evaluation_warnings;
12366 return objc_build_encode_expr (op1);
12369 case NOEXCEPT_EXPR:
12370 op1 = TREE_OPERAND (t, 0);
12371 ++cp_unevaluated_operand;
12372 ++c_inhibit_evaluation_warnings;
12373 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12374 /*function_p=*/false,
12375 /*integral_constant_expression_p=*/false);
12376 --cp_unevaluated_operand;
12377 --c_inhibit_evaluation_warnings;
12378 return finish_noexcept_expr (op1, complain);
12382 tree r = build_x_modify_expr
12383 (RECUR (TREE_OPERAND (t, 0)),
12384 TREE_CODE (TREE_OPERAND (t, 1)),
12385 RECUR (TREE_OPERAND (t, 2)),
12387 /* TREE_NO_WARNING must be set if either the expression was
12388 parenthesized or it uses an operator such as >>= rather
12389 than plain assignment. In the former case, it was already
12390 set and must be copied. In the latter case,
12391 build_x_modify_expr sets it and it must not be reset
12393 if (TREE_NO_WARNING (t))
12394 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12399 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12400 args, complain, in_decl);
12401 /* Remember that there was a reference to this entity. */
12404 return build_x_arrow (op1);
12408 tree placement = RECUR (TREE_OPERAND (t, 0));
12409 tree init = RECUR (TREE_OPERAND (t, 3));
12410 VEC(tree,gc) *placement_vec;
12411 VEC(tree,gc) *init_vec;
12414 if (placement == NULL_TREE)
12415 placement_vec = NULL;
12418 placement_vec = make_tree_vector ();
12419 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
12420 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
12423 /* If there was an initializer in the original tree, but it
12424 instantiated to an empty list, then we should pass a
12425 non-NULL empty vector to tell build_new that it was an
12426 empty initializer() rather than no initializer. This can
12427 only happen when the initializer is a pack expansion whose
12428 parameter packs are of length zero. */
12429 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
12433 init_vec = make_tree_vector ();
12434 if (init == void_zero_node)
12435 gcc_assert (init_vec != NULL);
12438 for (; init != NULL_TREE; init = TREE_CHAIN (init))
12439 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
12443 ret = build_new (&placement_vec,
12444 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
12445 RECUR (TREE_OPERAND (t, 2)),
12447 NEW_EXPR_USE_GLOBAL (t),
12450 if (placement_vec != NULL)
12451 release_tree_vector (placement_vec);
12452 if (init_vec != NULL)
12453 release_tree_vector (init_vec);
12459 return delete_sanity
12460 (RECUR (TREE_OPERAND (t, 0)),
12461 RECUR (TREE_OPERAND (t, 1)),
12462 DELETE_EXPR_USE_VEC (t),
12463 DELETE_EXPR_USE_GLOBAL (t));
12465 case COMPOUND_EXPR:
12466 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
12467 RECUR (TREE_OPERAND (t, 1)),
12473 VEC(tree,gc) *call_args;
12474 unsigned int nargs, i;
12479 function = CALL_EXPR_FN (t);
12480 /* When we parsed the expression, we determined whether or
12481 not Koenig lookup should be performed. */
12482 koenig_p = KOENIG_LOOKUP_P (t);
12483 if (TREE_CODE (function) == SCOPE_REF)
12485 qualified_p = true;
12486 function = tsubst_qualified_id (function, args, complain, in_decl,
12488 /*address_p=*/false);
12492 if (TREE_CODE (function) == COMPONENT_REF)
12494 tree op = TREE_OPERAND (function, 1);
12496 qualified_p = (TREE_CODE (op) == SCOPE_REF
12497 || (BASELINK_P (op)
12498 && BASELINK_QUALIFIED_P (op)));
12501 qualified_p = false;
12503 function = tsubst_copy_and_build (function, args, complain,
12506 integral_constant_expression_p);
12508 if (BASELINK_P (function))
12509 qualified_p = true;
12512 nargs = call_expr_nargs (t);
12513 call_args = make_tree_vector ();
12514 for (i = 0; i < nargs; ++i)
12516 tree arg = CALL_EXPR_ARG (t, i);
12518 if (!PACK_EXPANSION_P (arg))
12519 VEC_safe_push (tree, gc, call_args,
12520 RECUR (CALL_EXPR_ARG (t, i)));
12523 /* Expand the pack expansion and push each entry onto
12525 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
12526 if (TREE_CODE (arg) == TREE_VEC)
12528 unsigned int len, j;
12530 len = TREE_VEC_LENGTH (arg);
12531 for (j = 0; j < len; ++j)
12533 tree value = TREE_VEC_ELT (arg, j);
12534 if (value != NULL_TREE)
12535 value = convert_from_reference (value);
12536 VEC_safe_push (tree, gc, call_args, value);
12541 /* A partial substitution. Add one entry. */
12542 VEC_safe_push (tree, gc, call_args, arg);
12547 /* We do not perform argument-dependent lookup if normal
12548 lookup finds a non-function, in accordance with the
12549 expected resolution of DR 218. */
12551 && ((is_overloaded_fn (function)
12552 /* If lookup found a member function, the Koenig lookup is
12553 not appropriate, even if an unqualified-name was used
12554 to denote the function. */
12555 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
12556 || TREE_CODE (function) == IDENTIFIER_NODE)
12557 /* Only do this when substitution turns a dependent call
12558 into a non-dependent call. */
12559 && type_dependent_expression_p_push (t)
12560 && !any_type_dependent_arguments_p (call_args))
12561 function = perform_koenig_lookup (function, call_args, false);
12563 if (TREE_CODE (function) == IDENTIFIER_NODE)
12565 unqualified_name_lookup_error (function);
12566 release_tree_vector (call_args);
12567 return error_mark_node;
12570 /* Remember that there was a reference to this entity. */
12571 if (DECL_P (function))
12572 mark_used (function);
12574 if (TREE_CODE (function) == OFFSET_REF)
12575 ret = build_offset_ref_call_from_tree (function, &call_args);
12576 else if (TREE_CODE (function) == COMPONENT_REF)
12578 tree instance = TREE_OPERAND (function, 0);
12579 tree fn = TREE_OPERAND (function, 1);
12581 if (processing_template_decl
12582 && (type_dependent_expression_p (instance)
12583 || (!BASELINK_P (fn)
12584 && TREE_CODE (fn) != FIELD_DECL)
12585 || type_dependent_expression_p (fn)
12586 || any_type_dependent_arguments_p (call_args)))
12587 ret = build_nt_call_vec (function, call_args);
12588 else if (!BASELINK_P (fn))
12589 ret = finish_call_expr (function, &call_args,
12590 /*disallow_virtual=*/false,
12591 /*koenig_p=*/false,
12594 ret = (build_new_method_call
12596 &call_args, NULL_TREE,
12597 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
12602 ret = finish_call_expr (function, &call_args,
12603 /*disallow_virtual=*/qualified_p,
12607 release_tree_vector (call_args);
12613 return build_x_conditional_expr
12614 (RECUR (TREE_OPERAND (t, 0)),
12615 RECUR (TREE_OPERAND (t, 1)),
12616 RECUR (TREE_OPERAND (t, 2)),
12619 case PSEUDO_DTOR_EXPR:
12620 return finish_pseudo_destructor_expr
12621 (RECUR (TREE_OPERAND (t, 0)),
12622 RECUR (TREE_OPERAND (t, 1)),
12623 RECUR (TREE_OPERAND (t, 2)));
12627 tree purpose, value, chain;
12629 if (t == void_list_node)
12632 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
12633 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
12635 /* We have pack expansions, so expand those and
12636 create a new list out of it. */
12637 tree purposevec = NULL_TREE;
12638 tree valuevec = NULL_TREE;
12642 /* Expand the argument expressions. */
12643 if (TREE_PURPOSE (t))
12644 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
12645 complain, in_decl);
12646 if (TREE_VALUE (t))
12647 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
12648 complain, in_decl);
12650 /* Build the rest of the list. */
12651 chain = TREE_CHAIN (t);
12652 if (chain && chain != void_type_node)
12653 chain = RECUR (chain);
12655 /* Determine the number of arguments. */
12656 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
12658 len = TREE_VEC_LENGTH (purposevec);
12659 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
12661 else if (TREE_CODE (valuevec) == TREE_VEC)
12662 len = TREE_VEC_LENGTH (valuevec);
12665 /* Since we only performed a partial substitution into
12666 the argument pack, we only return a single list
12668 if (purposevec == TREE_PURPOSE (t)
12669 && valuevec == TREE_VALUE (t)
12670 && chain == TREE_CHAIN (t))
12673 return tree_cons (purposevec, valuevec, chain);
12676 /* Convert the argument vectors into a TREE_LIST */
12680 /* Grab the Ith values. */
12682 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
12685 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
12688 /* Build the list (backwards). */
12689 chain = tree_cons (purpose, value, chain);
12695 purpose = TREE_PURPOSE (t);
12697 purpose = RECUR (purpose);
12698 value = TREE_VALUE (t);
12700 value = RECUR (value);
12701 chain = TREE_CHAIN (t);
12702 if (chain && chain != void_type_node)
12703 chain = RECUR (chain);
12704 if (purpose == TREE_PURPOSE (t)
12705 && value == TREE_VALUE (t)
12706 && chain == TREE_CHAIN (t))
12708 return tree_cons (purpose, value, chain);
12711 case COMPONENT_REF:
12717 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12718 args, complain, in_decl);
12719 /* Remember that there was a reference to this entity. */
12720 if (DECL_P (object))
12721 mark_used (object);
12722 object_type = TREE_TYPE (object);
12724 member = TREE_OPERAND (t, 1);
12725 if (BASELINK_P (member))
12726 member = tsubst_baselink (member,
12727 non_reference (TREE_TYPE (object)),
12728 args, complain, in_decl);
12730 member = tsubst_copy (member, args, complain, in_decl);
12731 if (member == error_mark_node)
12732 return error_mark_node;
12734 if (object_type && !CLASS_TYPE_P (object_type))
12736 if (SCALAR_TYPE_P (object_type))
12738 tree s = NULL_TREE;
12739 tree dtor = member;
12741 if (TREE_CODE (dtor) == SCOPE_REF)
12743 s = TREE_OPERAND (dtor, 0);
12744 dtor = TREE_OPERAND (dtor, 1);
12746 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
12748 dtor = TREE_OPERAND (dtor, 0);
12750 return finish_pseudo_destructor_expr (object, s, dtor);
12754 else if (TREE_CODE (member) == SCOPE_REF
12755 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
12760 /* Lookup the template functions now that we know what the
12762 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
12763 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
12764 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
12765 /*is_type_p=*/false,
12766 /*complain=*/false);
12767 if (BASELINK_P (member))
12769 BASELINK_FUNCTIONS (member)
12770 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
12772 member = (adjust_result_of_qualified_name_lookup
12773 (member, BINFO_TYPE (BASELINK_BINFO (member)),
12778 qualified_name_lookup_error (object_type, tmpl, member,
12780 return error_mark_node;
12783 else if (TREE_CODE (member) == SCOPE_REF
12784 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
12785 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
12787 if (complain & tf_error)
12789 if (TYPE_P (TREE_OPERAND (member, 0)))
12790 error ("%qT is not a class or namespace",
12791 TREE_OPERAND (member, 0));
12793 error ("%qD is not a class or namespace",
12794 TREE_OPERAND (member, 0));
12796 return error_mark_node;
12798 else if (TREE_CODE (member) == FIELD_DECL)
12799 return finish_non_static_data_member (member, object, NULL_TREE);
12801 return finish_class_member_access_expr (object, member,
12802 /*template_p=*/false,
12808 (RECUR (TREE_OPERAND (t, 0)));
12812 VEC(constructor_elt,gc) *n;
12813 constructor_elt *ce;
12814 unsigned HOST_WIDE_INT idx;
12815 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12816 bool process_index_p;
12818 bool need_copy_p = false;
12821 if (type == error_mark_node)
12822 return error_mark_node;
12824 /* digest_init will do the wrong thing if we let it. */
12825 if (type && TYPE_PTRMEMFUNC_P (type))
12828 /* We do not want to process the index of aggregate
12829 initializers as they are identifier nodes which will be
12830 looked up by digest_init. */
12831 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
12833 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
12834 newlen = VEC_length (constructor_elt, n);
12835 FOR_EACH_VEC_ELT (constructor_elt, n, idx, ce)
12837 if (ce->index && process_index_p)
12838 ce->index = RECUR (ce->index);
12840 if (PACK_EXPANSION_P (ce->value))
12842 /* Substitute into the pack expansion. */
12843 ce->value = tsubst_pack_expansion (ce->value, args, complain,
12846 if (ce->value == error_mark_node)
12848 else if (TREE_VEC_LENGTH (ce->value) == 1)
12849 /* Just move the argument into place. */
12850 ce->value = TREE_VEC_ELT (ce->value, 0);
12853 /* Update the length of the final CONSTRUCTOR
12854 arguments vector, and note that we will need to
12856 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
12857 need_copy_p = true;
12861 ce->value = RECUR (ce->value);
12866 VEC(constructor_elt,gc) *old_n = n;
12868 n = VEC_alloc (constructor_elt, gc, newlen);
12869 FOR_EACH_VEC_ELT (constructor_elt, old_n, idx, ce)
12871 if (TREE_CODE (ce->value) == TREE_VEC)
12873 int i, len = TREE_VEC_LENGTH (ce->value);
12874 for (i = 0; i < len; ++i)
12875 CONSTRUCTOR_APPEND_ELT (n, 0,
12876 TREE_VEC_ELT (ce->value, i));
12879 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
12883 r = build_constructor (init_list_type_node, n);
12884 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
12886 if (TREE_HAS_CONSTRUCTOR (t))
12887 return finish_compound_literal (type, r);
12894 tree operand_0 = TREE_OPERAND (t, 0);
12895 if (TYPE_P (operand_0))
12897 operand_0 = tsubst (operand_0, args, complain, in_decl);
12898 return get_typeid (operand_0);
12902 operand_0 = RECUR (operand_0);
12903 return build_typeid (operand_0);
12914 tree r = tsubst_copy (t, args, complain, in_decl);
12916 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
12917 /* If the original type was a reference, we'll be wrapped in
12918 the appropriate INDIRECT_REF. */
12919 r = convert_from_reference (r);
12924 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
12925 tsubst_copy (TREE_TYPE (t), args, complain,
12928 case OFFSETOF_EXPR:
12929 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
12933 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
12934 complain, in_decl);
12936 tree type2 = TRAIT_EXPR_TYPE2 (t);
12938 type2 = tsubst_copy (type2, args, complain, in_decl);
12940 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
12945 tree old_stmt_expr = cur_stmt_expr;
12946 tree stmt_expr = begin_stmt_expr ();
12948 cur_stmt_expr = stmt_expr;
12949 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
12950 integral_constant_expression_p);
12951 stmt_expr = finish_stmt_expr (stmt_expr, false);
12952 cur_stmt_expr = old_stmt_expr;
12954 /* If the resulting list of expression statement is empty,
12955 fold it further into void_zero_node. */
12956 if (empty_expr_stmt_p (stmt_expr))
12957 stmt_expr = void_zero_node;
12963 t = tsubst_copy (t, args, complain, in_decl);
12964 /* As in finish_id_expression, we resolve enumeration constants
12965 to their underlying values. */
12966 if (TREE_CODE (t) == CONST_DECL)
12968 used_types_insert (TREE_TYPE (t));
12969 return DECL_INITIAL (t);
12975 tree r = build_lambda_expr ();
12977 tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12978 TREE_TYPE (r) = type;
12979 CLASSTYPE_LAMBDA_EXPR (type) = r;
12981 LAMBDA_EXPR_LOCATION (r)
12982 = LAMBDA_EXPR_LOCATION (t);
12983 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
12984 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
12985 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
12986 LAMBDA_EXPR_DISCRIMINATOR (r)
12987 = (LAMBDA_EXPR_DISCRIMINATOR (t));
12988 LAMBDA_EXPR_CAPTURE_LIST (r)
12989 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
12990 LAMBDA_EXPR_THIS_CAPTURE (r)
12991 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t));
12992 LAMBDA_EXPR_EXTRA_SCOPE (r)
12993 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
12995 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
12996 determine_visibility (TYPE_NAME (type));
12997 /* Now that we know visibility, instantiate the type so we have a
12998 declaration of the op() for later calls to lambda_function. */
12999 complete_type (type);
13001 type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
13003 apply_lambda_return_type (r, type);
13005 return build_lambda_object (r);
13009 /* Handle Objective-C++ constructs, if appropriate. */
13012 = objcp_tsubst_copy_and_build (t, args, complain,
13013 in_decl, /*function_p=*/false);
13017 return tsubst_copy (t, args, complain, in_decl);
13023 /* Verify that the instantiated ARGS are valid. For type arguments,
13024 make sure that the type's linkage is ok. For non-type arguments,
13025 make sure they are constants if they are integral or enumerations.
13026 Emit an error under control of COMPLAIN, and return TRUE on error. */
13029 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
13031 if (ARGUMENT_PACK_P (t))
13033 tree vec = ARGUMENT_PACK_ARGS (t);
13034 int len = TREE_VEC_LENGTH (vec);
13035 bool result = false;
13038 for (i = 0; i < len; ++i)
13039 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
13043 else if (TYPE_P (t))
13045 /* [basic.link]: A name with no linkage (notably, the name
13046 of a class or enumeration declared in a local scope)
13047 shall not be used to declare an entity with linkage.
13048 This implies that names with no linkage cannot be used as
13051 DR 757 relaxes this restriction for C++0x. */
13052 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
13053 : no_linkage_check (t, /*relaxed_p=*/false));
13057 /* DR 488 makes use of a type with no linkage cause
13058 type deduction to fail. */
13059 if (complain & tf_error)
13061 if (TYPE_ANONYMOUS_P (nt))
13062 error ("%qT is/uses anonymous type", t);
13064 error ("template argument for %qD uses local type %qT",
13069 /* In order to avoid all sorts of complications, we do not
13070 allow variably-modified types as template arguments. */
13071 else if (variably_modified_type_p (t, NULL_TREE))
13073 if (complain & tf_error)
13074 error ("%qT is a variably modified type", t);
13078 /* A non-type argument of integral or enumerated type must be a
13080 else if (TREE_TYPE (t)
13081 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
13082 && !TREE_CONSTANT (t))
13084 if (complain & tf_error)
13085 error ("integral expression %qE is not constant", t);
13092 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
13094 int ix, len = DECL_NTPARMS (tmpl);
13095 bool result = false;
13097 for (ix = 0; ix != len; ix++)
13099 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
13102 if (result && (complain & tf_error))
13103 error (" trying to instantiate %qD", tmpl);
13107 /* Instantiate the indicated variable or function template TMPL with
13108 the template arguments in TARG_PTR. */
13111 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
13113 tree targ_ptr = orig_args;
13117 HOST_WIDE_INT saved_processing_template_decl;
13119 if (tmpl == error_mark_node)
13120 return error_mark_node;
13122 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
13124 /* If this function is a clone, handle it specially. */
13125 if (DECL_CLONED_FUNCTION_P (tmpl))
13130 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
13131 DECL_CLONED_FUNCTION. */
13132 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
13133 targ_ptr, complain);
13134 if (spec == error_mark_node)
13135 return error_mark_node;
13137 /* Look for the clone. */
13138 FOR_EACH_CLONE (clone, spec)
13139 if (DECL_NAME (clone) == DECL_NAME (tmpl))
13141 /* We should always have found the clone by now. */
13142 gcc_unreachable ();
13146 /* Check to see if we already have this specialization. */
13147 gen_tmpl = most_general_template (tmpl);
13148 if (tmpl != gen_tmpl)
13149 /* The TMPL is a partial instantiation. To get a full set of
13150 arguments we must add the arguments used to perform the
13151 partial instantiation. */
13152 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
13155 /* It would be nice to avoid hashing here and then again in tsubst_decl,
13156 but it doesn't seem to be on the hot path. */
13157 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
13159 gcc_assert (tmpl == gen_tmpl
13160 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
13162 || fndecl == NULL_TREE);
13164 if (spec != NULL_TREE)
13167 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
13169 return error_mark_node;
13171 /* We are building a FUNCTION_DECL, during which the access of its
13172 parameters and return types have to be checked. However this
13173 FUNCTION_DECL which is the desired context for access checking
13174 is not built yet. We solve this chicken-and-egg problem by
13175 deferring all checks until we have the FUNCTION_DECL. */
13176 push_deferring_access_checks (dk_deferred);
13178 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
13179 (because, for example, we have encountered a non-dependent
13180 function call in the body of a template function and must now
13181 determine which of several overloaded functions will be called),
13182 within the instantiation itself we are not processing a
13184 saved_processing_template_decl = processing_template_decl;
13185 processing_template_decl = 0;
13186 /* Substitute template parameters to obtain the specialization. */
13187 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
13188 targ_ptr, complain, gen_tmpl);
13189 processing_template_decl = saved_processing_template_decl;
13190 if (fndecl == error_mark_node)
13191 return error_mark_node;
13193 /* Now we know the specialization, compute access previously
13195 push_access_scope (fndecl);
13197 /* Some typedefs referenced from within the template code need to be access
13198 checked at template instantiation time, i.e now. These types were
13199 added to the template at parsing time. Let's get those and perfom
13200 the acces checks then. */
13201 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
13202 perform_deferred_access_checks ();
13203 pop_access_scope (fndecl);
13204 pop_deferring_access_checks ();
13206 /* The DECL_TI_TEMPLATE should always be the immediate parent
13207 template, not the most general template. */
13208 DECL_TI_TEMPLATE (fndecl) = tmpl;
13210 /* If we've just instantiated the main entry point for a function,
13211 instantiate all the alternate entry points as well. We do this
13212 by cloning the instantiation of the main entry point, not by
13213 instantiating the template clones. */
13214 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
13215 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
13220 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
13221 NARGS elements of the arguments that are being used when calling
13222 it. TARGS is a vector into which the deduced template arguments
13225 Return zero for success, 2 for an incomplete match that doesn't resolve
13226 all the types, and 1 for complete failure. An error message will be
13227 printed only for an incomplete match.
13229 If FN is a conversion operator, or we are trying to produce a specific
13230 specialization, RETURN_TYPE is the return type desired.
13232 The EXPLICIT_TARGS are explicit template arguments provided via a
13235 The parameter STRICT is one of:
13238 We are deducing arguments for a function call, as in
13239 [temp.deduct.call].
13242 We are deducing arguments for a conversion function, as in
13243 [temp.deduct.conv].
13246 We are deducing arguments when doing an explicit instantiation
13247 as in [temp.explicit], when determining an explicit specialization
13248 as in [temp.expl.spec], or when taking the address of a function
13249 template, as in [temp.deduct.funcaddr]. */
13252 fn_type_unification (tree fn,
13253 tree explicit_targs,
13256 unsigned int nargs,
13258 unification_kind_t strict,
13264 bool incomplete_argument_packs_p = false;
13266 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
13268 fntype = TREE_TYPE (fn);
13269 if (explicit_targs)
13273 The specified template arguments must match the template
13274 parameters in kind (i.e., type, nontype, template), and there
13275 must not be more arguments than there are parameters;
13276 otherwise type deduction fails.
13278 Nontype arguments must match the types of the corresponding
13279 nontype template parameters, or must be convertible to the
13280 types of the corresponding nontype parameters as specified in
13281 _temp.arg.nontype_, otherwise type deduction fails.
13283 All references in the function type of the function template
13284 to the corresponding template parameters are replaced by the
13285 specified template argument values. If a substitution in a
13286 template parameter or in the function type of the function
13287 template results in an invalid type, type deduction fails. */
13288 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
13289 int i, len = TREE_VEC_LENGTH (tparms);
13290 tree converted_args;
13291 bool incomplete = false;
13293 if (explicit_targs == error_mark_node)
13297 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
13298 /*require_all_args=*/false,
13299 /*use_default_args=*/false));
13300 if (converted_args == error_mark_node)
13303 /* Substitute the explicit args into the function type. This is
13304 necessary so that, for instance, explicitly declared function
13305 arguments can match null pointed constants. If we were given
13306 an incomplete set of explicit args, we must not do semantic
13307 processing during substitution as we could create partial
13309 for (i = 0; i < len; i++)
13311 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13312 bool parameter_pack = false;
13314 /* Dig out the actual parm. */
13315 if (TREE_CODE (parm) == TYPE_DECL
13316 || TREE_CODE (parm) == TEMPLATE_DECL)
13318 parm = TREE_TYPE (parm);
13319 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
13321 else if (TREE_CODE (parm) == PARM_DECL)
13323 parm = DECL_INITIAL (parm);
13324 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
13327 if (parameter_pack)
13331 template_parm_level_and_index (parm, &level, &idx);
13333 /* Mark the argument pack as "incomplete". We could
13334 still deduce more arguments during unification. */
13335 targ = TMPL_ARG (converted_args, level, idx);
13338 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
13339 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
13340 = ARGUMENT_PACK_ARGS (targ);
13343 /* We have some incomplete argument packs. */
13344 incomplete_argument_packs_p = true;
13348 if (incomplete_argument_packs_p)
13349 /* Any substitution is guaranteed to be incomplete if there
13350 are incomplete argument packs, because we can still deduce
13354 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
13356 processing_template_decl += incomplete;
13357 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
13358 processing_template_decl -= incomplete;
13360 if (fntype == error_mark_node)
13363 /* Place the explicitly specified arguments in TARGS. */
13364 for (i = NUM_TMPL_ARGS (converted_args); i--;)
13365 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
13368 /* Never do unification on the 'this' parameter. */
13369 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
13375 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
13376 new_args = XALLOCAVEC (tree, nargs + 1);
13377 new_args[0] = return_type;
13378 memcpy (new_args + 1, args, nargs * sizeof (tree));
13383 /* We allow incomplete unification without an error message here
13384 because the standard doesn't seem to explicitly prohibit it. Our
13385 callers must be ready to deal with unification failures in any
13387 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
13388 targs, parms, args, nargs, /*subr=*/0,
13391 if (result == 0 && incomplete_argument_packs_p)
13393 int i, len = NUM_TMPL_ARGS (targs);
13395 /* Clear the "incomplete" flags on all argument packs. */
13396 for (i = 0; i < len; i++)
13398 tree arg = TREE_VEC_ELT (targs, i);
13399 if (ARGUMENT_PACK_P (arg))
13401 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
13402 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
13407 /* Now that we have bindings for all of the template arguments,
13408 ensure that the arguments deduced for the template template
13409 parameters have compatible template parameter lists. We cannot
13410 check this property before we have deduced all template
13411 arguments, because the template parameter types of a template
13412 template parameter might depend on prior template parameters
13413 deduced after the template template parameter. The following
13414 ill-formed example illustrates this issue:
13416 template<typename T, template<T> class C> void f(C<5>, T);
13418 template<int N> struct X {};
13421 f(X<5>(), 5l); // error: template argument deduction fails
13424 The template parameter list of 'C' depends on the template type
13425 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
13426 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
13427 time that we deduce 'C'. */
13429 && !template_template_parm_bindings_ok_p
13430 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
13434 /* All is well so far. Now, check:
13438 When all template arguments have been deduced, all uses of
13439 template parameters in nondeduced contexts are replaced with
13440 the corresponding deduced argument values. If the
13441 substitution results in an invalid type, as described above,
13442 type deduction fails. */
13444 tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
13445 if (substed == error_mark_node)
13448 /* If we're looking for an exact match, check that what we got
13449 is indeed an exact match. It might not be if some template
13450 parameters are used in non-deduced contexts. */
13451 if (strict == DEDUCE_EXACT)
13456 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
13458 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
13459 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
13460 if (!same_type_p (args[i], TREE_VALUE (sarg)))
13468 /* Adjust types before performing type deduction, as described in
13469 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
13470 sections are symmetric. PARM is the type of a function parameter
13471 or the return type of the conversion function. ARG is the type of
13472 the argument passed to the call, or the type of the value
13473 initialized with the result of the conversion function.
13474 ARG_EXPR is the original argument expression, which may be null. */
13477 maybe_adjust_types_for_deduction (unification_kind_t strict,
13491 /* Swap PARM and ARG throughout the remainder of this
13492 function; the handling is precisely symmetric since PARM
13493 will initialize ARG rather than vice versa. */
13501 /* Core issue #873: Do the DR606 thing (see below) for these cases,
13502 too, but here handle it by stripping the reference from PARM
13503 rather than by adding it to ARG. */
13504 if (TREE_CODE (*parm) == REFERENCE_TYPE
13505 && TYPE_REF_IS_RVALUE (*parm)
13506 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13507 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13508 && TREE_CODE (*arg) == REFERENCE_TYPE
13509 && !TYPE_REF_IS_RVALUE (*arg))
13510 *parm = TREE_TYPE (*parm);
13511 /* Nothing else to do in this case. */
13515 gcc_unreachable ();
13518 if (TREE_CODE (*parm) != REFERENCE_TYPE)
13520 /* [temp.deduct.call]
13522 If P is not a reference type:
13524 --If A is an array type, the pointer type produced by the
13525 array-to-pointer standard conversion (_conv.array_) is
13526 used in place of A for type deduction; otherwise,
13528 --If A is a function type, the pointer type produced by
13529 the function-to-pointer standard conversion
13530 (_conv.func_) is used in place of A for type deduction;
13533 --If A is a cv-qualified type, the top level
13534 cv-qualifiers of A's type are ignored for type
13536 if (TREE_CODE (*arg) == ARRAY_TYPE)
13537 *arg = build_pointer_type (TREE_TYPE (*arg));
13538 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
13539 *arg = build_pointer_type (*arg);
13541 *arg = TYPE_MAIN_VARIANT (*arg);
13544 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
13545 of the form T&&, where T is a template parameter, and the argument
13546 is an lvalue, T is deduced as A& */
13547 if (TREE_CODE (*parm) == REFERENCE_TYPE
13548 && TYPE_REF_IS_RVALUE (*parm)
13549 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13550 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13551 && arg_expr && real_lvalue_p (arg_expr))
13552 *arg = build_reference_type (*arg);
13554 /* [temp.deduct.call]
13556 If P is a cv-qualified type, the top level cv-qualifiers
13557 of P's type are ignored for type deduction. If P is a
13558 reference type, the type referred to by P is used for
13560 *parm = TYPE_MAIN_VARIANT (*parm);
13561 if (TREE_CODE (*parm) == REFERENCE_TYPE)
13563 *parm = TREE_TYPE (*parm);
13564 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
13567 /* DR 322. For conversion deduction, remove a reference type on parm
13568 too (which has been swapped into ARG). */
13569 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
13570 *arg = TREE_TYPE (*arg);
13575 /* Most parms like fn_type_unification.
13577 If SUBR is 1, we're being called recursively (to unify the
13578 arguments of a function or method parameter of a function
13582 type_unification_real (tree tparms,
13586 unsigned int xnargs,
13588 unification_kind_t strict,
13591 tree parm, arg, arg_expr;
13593 int ntparms = TREE_VEC_LENGTH (tparms);
13595 int saw_undeduced = 0;
13598 unsigned int nargs;
13601 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
13602 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
13603 gcc_assert (ntparms > 0);
13605 /* Reset the number of non-defaulted template arguments contained
13607 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
13612 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
13613 | UNIFY_ALLOW_DERIVED);
13617 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13621 sub_strict = UNIFY_ALLOW_NONE;
13625 gcc_unreachable ();
13634 while (parms && parms != void_list_node
13637 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13640 parm = TREE_VALUE (parms);
13641 parms = TREE_CHAIN (parms);
13646 if (arg == error_mark_node)
13648 if (arg == unknown_type_node)
13649 /* We can't deduce anything from this, but we might get all the
13650 template args from other function args. */
13653 /* Conversions will be performed on a function argument that
13654 corresponds with a function parameter that contains only
13655 non-deducible template parameters and explicitly specified
13656 template parameters. */
13657 if (!uses_template_parms (parm))
13662 type = TREE_TYPE (arg);
13666 if (same_type_p (parm, type))
13668 if (strict != DEDUCE_EXACT
13669 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
13678 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
13679 if (type_unknown_p (arg))
13681 /* [temp.deduct.type]
13683 A template-argument can be deduced from a pointer to
13684 function or pointer to member function argument if
13685 the set of overloaded functions does not contain
13686 function templates and at most one of a set of
13687 overloaded functions provides a unique match. */
13688 if (resolve_overloaded_unification
13689 (tparms, targs, parm, arg, strict, sub_strict))
13695 arg = unlowered_expr_type (arg);
13696 if (arg == error_mark_node)
13701 int arg_strict = sub_strict;
13704 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
13707 if (arg == init_list_type_node && arg_expr)
13709 if (unify (tparms, targs, parm, arg, arg_strict))
13716 && parms != void_list_node
13717 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13719 /* Unify the remaining arguments with the pack expansion type. */
13721 tree parmvec = make_tree_vec (1);
13723 /* Allocate a TREE_VEC and copy in all of the arguments */
13724 argvec = make_tree_vec (nargs - ia);
13725 for (i = 0; ia < nargs; ++ia, ++i)
13726 TREE_VEC_ELT (argvec, i) = args[ia];
13728 /* Copy the parameter into parmvec. */
13729 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
13730 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
13731 /*call_args_p=*/true, /*subr=*/subr))
13734 /* Advance to the end of the list of parameters. */
13735 parms = TREE_CHAIN (parms);
13738 /* Fail if we've reached the end of the parm list, and more args
13739 are present, and the parm list isn't variadic. */
13740 if (ia < nargs && parms == void_list_node)
13742 /* Fail if parms are left and they don't have default values. */
13743 if (parms && parms != void_list_node
13744 && TREE_PURPOSE (parms) == NULL_TREE)
13748 for (i = 0; i < ntparms; i++)
13749 if (!TREE_VEC_ELT (targs, i))
13753 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
13756 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13758 /* If this is an undeduced nontype parameter that depends on
13759 a type parameter, try another pass; its type may have been
13760 deduced from a later argument than the one from which
13761 this parameter can be deduced. */
13762 if (TREE_CODE (tparm) == PARM_DECL
13763 && uses_template_parms (TREE_TYPE (tparm))
13764 && !saw_undeduced++)
13767 /* Core issue #226 (C++0x) [temp.deduct]:
13769 If a template argument has not been deduced, its
13770 default template argument, if any, is used.
13772 When we are in C++98 mode, TREE_PURPOSE will either
13773 be NULL_TREE or ERROR_MARK_NODE, so we do not need
13774 to explicitly check cxx_dialect here. */
13775 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
13777 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13778 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
13779 arg = tsubst_template_arg (arg, targs, tf_none, NULL_TREE);
13780 arg = convert_template_argument (parm, arg, targs, tf_none,
13782 if (arg == error_mark_node)
13786 TREE_VEC_ELT (targs, i) = arg;
13787 /* The position of the first default template argument,
13788 is also the number of non-defaulted arguments in TARGS.
13790 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
13791 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
13796 /* If the type parameter is a parameter pack, then it will
13797 be deduced to an empty parameter pack. */
13798 if (template_parameter_pack_p (tparm))
13802 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
13804 arg = make_node (NONTYPE_ARGUMENT_PACK);
13805 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
13806 TREE_CONSTANT (arg) = 1;
13809 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
13811 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
13813 TREE_VEC_ELT (targs, i) = arg;
13819 #ifdef ENABLE_CHECKING
13820 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
13821 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
13827 /* Subroutine of type_unification_real. Args are like the variables
13828 at the call site. ARG is an overloaded function (or template-id);
13829 we try deducing template args from each of the overloads, and if
13830 only one succeeds, we go with that. Modifies TARGS and returns
13831 true on success. */
13834 resolve_overloaded_unification (tree tparms,
13838 unification_kind_t strict,
13841 tree tempargs = copy_node (targs);
13843 tree goodfn = NULL_TREE;
13846 if (TREE_CODE (arg) == ADDR_EXPR)
13848 arg = TREE_OPERAND (arg, 0);
13854 if (TREE_CODE (arg) == COMPONENT_REF)
13855 /* Handle `&x' where `x' is some static or non-static member
13857 arg = TREE_OPERAND (arg, 1);
13859 if (TREE_CODE (arg) == OFFSET_REF)
13860 arg = TREE_OPERAND (arg, 1);
13862 /* Strip baselink information. */
13863 if (BASELINK_P (arg))
13864 arg = BASELINK_FUNCTIONS (arg);
13866 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
13868 /* If we got some explicit template args, we need to plug them into
13869 the affected templates before we try to unify, in case the
13870 explicit args will completely resolve the templates in question. */
13872 tree expl_subargs = TREE_OPERAND (arg, 1);
13873 arg = TREE_OPERAND (arg, 0);
13875 for (; arg; arg = OVL_NEXT (arg))
13877 tree fn = OVL_CURRENT (arg);
13878 tree subargs, elem;
13880 if (TREE_CODE (fn) != TEMPLATE_DECL)
13883 ++processing_template_decl;
13884 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13885 expl_subargs, /*check_ret=*/false);
13888 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
13889 if (try_one_overload (tparms, targs, tempargs, parm,
13890 elem, strict, sub_strict, addr_p)
13891 && (!goodfn || !decls_match (goodfn, elem)))
13897 --processing_template_decl;
13900 else if (TREE_CODE (arg) != OVERLOAD
13901 && TREE_CODE (arg) != FUNCTION_DECL)
13902 /* If ARG is, for example, "(0, &f)" then its type will be unknown
13903 -- but the deduction does not succeed because the expression is
13904 not just the function on its own. */
13907 for (; arg; arg = OVL_NEXT (arg))
13908 if (try_one_overload (tparms, targs, tempargs, parm,
13909 TREE_TYPE (OVL_CURRENT (arg)),
13910 strict, sub_strict, addr_p)
13911 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
13913 goodfn = OVL_CURRENT (arg);
13917 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13918 to function or pointer to member function argument if the set of
13919 overloaded functions does not contain function templates and at most
13920 one of a set of overloaded functions provides a unique match.
13922 So if we found multiple possibilities, we return success but don't
13923 deduce anything. */
13927 int i = TREE_VEC_LENGTH (targs);
13929 if (TREE_VEC_ELT (tempargs, i))
13930 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
13938 /* Core DR 115: In contexts where deduction is done and fails, or in
13939 contexts where deduction is not done, if a template argument list is
13940 specified and it, along with any default template arguments, identifies
13941 a single function template specialization, then the template-id is an
13942 lvalue for the function template specialization. */
13945 resolve_nondeduced_context (tree orig_expr)
13947 tree expr, offset, baselink;
13950 if (!type_unknown_p (orig_expr))
13955 offset = NULL_TREE;
13956 baselink = NULL_TREE;
13958 if (TREE_CODE (expr) == ADDR_EXPR)
13960 expr = TREE_OPERAND (expr, 0);
13963 if (TREE_CODE (expr) == OFFSET_REF)
13966 expr = TREE_OPERAND (expr, 1);
13968 if (TREE_CODE (expr) == BASELINK)
13971 expr = BASELINK_FUNCTIONS (expr);
13974 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
13977 tree goodfn = NULL_TREE;
13979 /* If we got some explicit template args, we need to plug them into
13980 the affected templates before we try to unify, in case the
13981 explicit args will completely resolve the templates in question. */
13983 tree expl_subargs = TREE_OPERAND (expr, 1);
13984 tree arg = TREE_OPERAND (expr, 0);
13985 tree badfn = NULL_TREE;
13986 tree badargs = NULL_TREE;
13988 for (; arg; arg = OVL_NEXT (arg))
13990 tree fn = OVL_CURRENT (arg);
13991 tree subargs, elem;
13993 if (TREE_CODE (fn) != TEMPLATE_DECL)
13996 ++processing_template_decl;
13997 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13998 expl_subargs, /*check_ret=*/false);
13999 if (subargs && !any_dependent_template_arguments_p (subargs))
14001 elem = instantiate_template (fn, subargs, tf_none);
14002 if (elem == error_mark_node)
14007 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
14013 --processing_template_decl;
14019 expr = build_baselink (BASELINK_BINFO (baselink),
14020 BASELINK_ACCESS_BINFO (baselink),
14021 expr, BASELINK_OPTYPE (baselink));
14023 expr = build2 (OFFSET_REF, TREE_TYPE (expr),
14024 TREE_OPERAND (offset, 0), expr);
14026 expr = build_address (expr);
14029 else if (good == 0 && badargs)
14030 /* There were no good options and at least one bad one, so let the
14031 user know what the problem is. */
14032 instantiate_template (badfn, badargs, tf_warning_or_error);
14037 /* Subroutine of resolve_overloaded_unification; does deduction for a single
14038 overload. Fills TARGS with any deduced arguments, or error_mark_node if
14039 different overloads deduce different arguments for a given parm.
14040 ADDR_P is true if the expression for which deduction is being
14041 performed was of the form "& fn" rather than simply "fn".
14043 Returns 1 on success. */
14046 try_one_overload (tree tparms,
14051 unification_kind_t strict,
14059 /* [temp.deduct.type] A template-argument can be deduced from a pointer
14060 to function or pointer to member function argument if the set of
14061 overloaded functions does not contain function templates and at most
14062 one of a set of overloaded functions provides a unique match.
14064 So if this is a template, just return success. */
14066 if (uses_template_parms (arg))
14069 if (TREE_CODE (arg) == METHOD_TYPE)
14070 arg = build_ptrmemfunc_type (build_pointer_type (arg));
14072 arg = build_pointer_type (arg);
14074 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
14076 /* We don't copy orig_targs for this because if we have already deduced
14077 some template args from previous args, unify would complain when we
14078 try to deduce a template parameter for the same argument, even though
14079 there isn't really a conflict. */
14080 nargs = TREE_VEC_LENGTH (targs);
14081 tempargs = make_tree_vec (nargs);
14083 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
14086 /* First make sure we didn't deduce anything that conflicts with
14087 explicitly specified args. */
14088 for (i = nargs; i--; )
14090 tree elt = TREE_VEC_ELT (tempargs, i);
14091 tree oldelt = TREE_VEC_ELT (orig_targs, i);
14095 else if (uses_template_parms (elt))
14096 /* Since we're unifying against ourselves, we will fill in
14097 template args used in the function parm list with our own
14098 template parms. Discard them. */
14099 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
14100 else if (oldelt && !template_args_equal (oldelt, elt))
14104 for (i = nargs; i--; )
14106 tree elt = TREE_VEC_ELT (tempargs, i);
14109 TREE_VEC_ELT (targs, i) = elt;
14115 /* PARM is a template class (perhaps with unbound template
14116 parameters). ARG is a fully instantiated type. If ARG can be
14117 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
14118 TARGS are as for unify. */
14121 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
14123 tree copy_of_targs;
14125 if (!CLASSTYPE_TEMPLATE_INFO (arg)
14126 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
14127 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
14130 /* We need to make a new template argument vector for the call to
14131 unify. If we used TARGS, we'd clutter it up with the result of
14132 the attempted unification, even if this class didn't work out.
14133 We also don't want to commit ourselves to all the unifications
14134 we've already done, since unification is supposed to be done on
14135 an argument-by-argument basis. In other words, consider the
14136 following pathological case:
14138 template <int I, int J, int K>
14141 template <int I, int J>
14142 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
14144 template <int I, int J, int K>
14145 void f(S<I, J, K>, S<I, I, I>);
14154 Now, by the time we consider the unification involving `s2', we
14155 already know that we must have `f<0, 0, 0>'. But, even though
14156 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
14157 because there are two ways to unify base classes of S<0, 1, 2>
14158 with S<I, I, I>. If we kept the already deduced knowledge, we
14159 would reject the possibility I=1. */
14160 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
14162 /* If unification failed, we're done. */
14163 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
14164 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
14170 /* Given a template type PARM and a class type ARG, find the unique
14171 base type in ARG that is an instance of PARM. We do not examine
14172 ARG itself; only its base-classes. If there is not exactly one
14173 appropriate base class, return NULL_TREE. PARM may be the type of
14174 a partial specialization, as well as a plain template type. Used
14178 get_template_base (tree tparms, tree targs, tree parm, tree arg)
14180 tree rval = NULL_TREE;
14183 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
14185 binfo = TYPE_BINFO (complete_type (arg));
14187 /* The type could not be completed. */
14190 /* Walk in inheritance graph order. The search order is not
14191 important, and this avoids multiple walks of virtual bases. */
14192 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
14194 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
14198 /* If there is more than one satisfactory baseclass, then:
14202 If they yield more than one possible deduced A, the type
14206 if (rval && !same_type_p (r, rval))
14216 /* Returns the level of DECL, which declares a template parameter. */
14219 template_decl_level (tree decl)
14221 switch (TREE_CODE (decl))
14224 case TEMPLATE_DECL:
14225 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
14228 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
14231 gcc_unreachable ();
14236 /* Decide whether ARG can be unified with PARM, considering only the
14237 cv-qualifiers of each type, given STRICT as documented for unify.
14238 Returns nonzero iff the unification is OK on that basis. */
14241 check_cv_quals_for_unify (int strict, tree arg, tree parm)
14243 int arg_quals = cp_type_quals (arg);
14244 int parm_quals = cp_type_quals (parm);
14246 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14247 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
14249 /* Although a CVR qualifier is ignored when being applied to a
14250 substituted template parameter ([8.3.2]/1 for example), that
14251 does not allow us to unify "const T" with "int&" because both
14252 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
14253 It is ok when we're allowing additional CV qualifiers
14254 at the outer level [14.8.2.1]/3,1st bullet. */
14255 if ((TREE_CODE (arg) == REFERENCE_TYPE
14256 || TREE_CODE (arg) == FUNCTION_TYPE
14257 || TREE_CODE (arg) == METHOD_TYPE)
14258 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
14261 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
14262 && (parm_quals & TYPE_QUAL_RESTRICT))
14266 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
14267 && (arg_quals & parm_quals) != parm_quals)
14270 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
14271 && (parm_quals & arg_quals) != arg_quals)
14277 /* Determines the LEVEL and INDEX for the template parameter PARM. */
14279 template_parm_level_and_index (tree parm, int* level, int* index)
14281 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14282 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14283 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14285 *index = TEMPLATE_TYPE_IDX (parm);
14286 *level = TEMPLATE_TYPE_LEVEL (parm);
14290 *index = TEMPLATE_PARM_IDX (parm);
14291 *level = TEMPLATE_PARM_LEVEL (parm);
14295 /* Unifies the remaining arguments in PACKED_ARGS with the pack
14296 expansion at the end of PACKED_PARMS. Returns 0 if the type
14297 deduction succeeds, 1 otherwise. STRICT is the same as in
14298 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
14299 call argument list. We'll need to adjust the arguments to make them
14300 types. SUBR tells us if this is from a recursive call to
14301 type_unification_real. */
14303 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
14304 tree packed_args, int strict, bool call_args_p,
14308 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
14309 tree pattern = PACK_EXPANSION_PATTERN (parm);
14310 tree pack, packs = NULL_TREE;
14311 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
14312 int len = TREE_VEC_LENGTH (packed_args);
14314 /* Determine the parameter packs we will be deducing from the
14315 pattern, and record their current deductions. */
14316 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
14317 pack; pack = TREE_CHAIN (pack))
14319 tree parm_pack = TREE_VALUE (pack);
14322 /* Determine the index and level of this parameter pack. */
14323 template_parm_level_and_index (parm_pack, &level, &idx);
14325 /* Keep track of the parameter packs and their corresponding
14327 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
14328 TREE_TYPE (packs) = make_tree_vec (len - start);
14331 /* Loop through all of the arguments that have not yet been
14332 unified and unify each with the pattern. */
14333 for (i = start; i < len; i++)
14335 tree parm = pattern;
14337 /* For each parameter pack, clear out the deduced value so that
14338 we can deduce it again. */
14339 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14342 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14344 TMPL_ARG (targs, level, idx) = NULL_TREE;
14347 /* Unify the pattern with the current argument. */
14349 tree arg = TREE_VEC_ELT (packed_args, i);
14350 tree arg_expr = NULL_TREE;
14351 int arg_strict = strict;
14352 bool skip_arg_p = false;
14358 /* This mirrors what we do in type_unification_real. */
14362 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
14363 | UNIFY_ALLOW_MORE_CV_QUAL
14364 | UNIFY_ALLOW_DERIVED);
14368 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
14372 sub_strict = UNIFY_ALLOW_NONE;
14376 gcc_unreachable ();
14381 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
14382 if (type_unknown_p (arg))
14384 /* [temp.deduct.type] A template-argument can be
14385 deduced from a pointer to function or pointer
14386 to member function argument if the set of
14387 overloaded functions does not contain function
14388 templates and at most one of a set of
14389 overloaded functions provides a unique
14392 if (resolve_overloaded_unification
14393 (tparms, targs, parm, arg,
14394 (unification_kind_t) strict,
14404 arg = unlowered_expr_type (arg);
14405 if (arg == error_mark_node)
14410 arg_strict = sub_strict;
14414 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
14415 &parm, &arg, arg_expr);
14420 /* For deduction from an init-list we need the actual list. */
14421 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
14423 if (unify (tparms, targs, parm, arg, arg_strict))
14428 /* For each parameter pack, collect the deduced value. */
14429 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14432 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14434 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
14435 TMPL_ARG (targs, level, idx);
14439 /* Verify that the results of unification with the parameter packs
14440 produce results consistent with what we've seen before, and make
14441 the deduced argument packs available. */
14442 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14444 tree old_pack = TREE_VALUE (pack);
14445 tree new_args = TREE_TYPE (pack);
14446 int i, len = TREE_VEC_LENGTH (new_args);
14448 bool nondeduced_p = false;
14450 /* By default keep the original deduced argument pack.
14451 If necessary, more specific code is going to update the
14452 resulting deduced argument later down in this function. */
14453 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14454 TMPL_ARG (targs, level, idx) = old_pack;
14456 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
14457 actually deduce anything. */
14458 for (i = 0; i < len && !nondeduced_p; ++i)
14459 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
14460 nondeduced_p = true;
14464 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
14466 /* Prepend the explicit arguments onto NEW_ARGS. */
14467 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14468 tree old_args = new_args;
14469 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
14470 int len = explicit_len + TREE_VEC_LENGTH (old_args);
14472 /* Copy the explicit arguments. */
14473 new_args = make_tree_vec (len);
14474 for (i = 0; i < explicit_len; i++)
14475 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
14477 /* Copy the deduced arguments. */
14478 for (; i < len; i++)
14479 TREE_VEC_ELT (new_args, i) =
14480 TREE_VEC_ELT (old_args, i - explicit_len);
14486 /* Build the deduced *_ARGUMENT_PACK. */
14487 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
14489 result = make_node (NONTYPE_ARGUMENT_PACK);
14490 TREE_TYPE (result) =
14491 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
14492 TREE_CONSTANT (result) = 1;
14495 result = cxx_make_type (TYPE_ARGUMENT_PACK);
14497 SET_ARGUMENT_PACK_ARGS (result, new_args);
14499 /* Note the deduced argument packs for this parameter
14501 TMPL_ARG (targs, level, idx) = result;
14503 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
14504 && (ARGUMENT_PACK_ARGS (old_pack)
14505 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
14507 /* We only had the explicitly-provided arguments before, but
14508 now we have a complete set of arguments. */
14509 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14511 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
14512 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
14513 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
14515 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
14517 /* Inconsistent unification of this parameter pack. */
14524 /* Deduce the value of template parameters. TPARMS is the (innermost)
14525 set of template parameters to a template. TARGS is the bindings
14526 for those template parameters, as determined thus far; TARGS may
14527 include template arguments for outer levels of template parameters
14528 as well. PARM is a parameter to a template function, or a
14529 subcomponent of that parameter; ARG is the corresponding argument.
14530 This function attempts to match PARM with ARG in a manner
14531 consistent with the existing assignments in TARGS. If more values
14532 are deduced, then TARGS is updated.
14534 Returns 0 if the type deduction succeeds, 1 otherwise. The
14535 parameter STRICT is a bitwise or of the following flags:
14538 Require an exact match between PARM and ARG.
14539 UNIFY_ALLOW_MORE_CV_QUAL:
14540 Allow the deduced ARG to be more cv-qualified (by qualification
14541 conversion) than ARG.
14542 UNIFY_ALLOW_LESS_CV_QUAL:
14543 Allow the deduced ARG to be less cv-qualified than ARG.
14544 UNIFY_ALLOW_DERIVED:
14545 Allow the deduced ARG to be a template base class of ARG,
14546 or a pointer to a template base class of the type pointed to by
14548 UNIFY_ALLOW_INTEGER:
14549 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
14550 case for more information.
14551 UNIFY_ALLOW_OUTER_LEVEL:
14552 This is the outermost level of a deduction. Used to determine validity
14553 of qualification conversions. A valid qualification conversion must
14554 have const qualified pointers leading up to the inner type which
14555 requires additional CV quals, except at the outer level, where const
14556 is not required [conv.qual]. It would be normal to set this flag in
14557 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
14558 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
14559 This is the outermost level of a deduction, and PARM can be more CV
14560 qualified at this point.
14561 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
14562 This is the outermost level of a deduction, and PARM can be less CV
14563 qualified at this point. */
14566 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
14571 int strict_in = strict;
14573 /* I don't think this will do the right thing with respect to types.
14574 But the only case I've seen it in so far has been array bounds, where
14575 signedness is the only information lost, and I think that will be
14577 while (TREE_CODE (parm) == NOP_EXPR)
14578 parm = TREE_OPERAND (parm, 0);
14580 if (arg == error_mark_node)
14582 if (arg == unknown_type_node
14583 || arg == init_list_type_node)
14584 /* We can't deduce anything from this, but we might get all the
14585 template args from other function args. */
14588 /* If PARM uses template parameters, then we can't bail out here,
14589 even if ARG == PARM, since we won't record unifications for the
14590 template parameters. We might need them if we're trying to
14591 figure out which of two things is more specialized. */
14592 if (arg == parm && !uses_template_parms (parm))
14595 /* Handle init lists early, so the rest of the function can assume
14596 we're dealing with a type. */
14597 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
14601 tree orig_parm = parm;
14603 /* Replace T with std::initializer_list<T> for deduction. */
14604 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14605 && flag_deduce_init_list)
14606 parm = listify (parm);
14608 if (!is_std_init_list (parm))
14609 /* We can only deduce from an initializer list argument if the
14610 parameter is std::initializer_list; otherwise this is a
14611 non-deduced context. */
14614 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
14616 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
14618 int elt_strict = strict;
14620 if (elt == error_mark_node)
14623 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
14625 tree type = TREE_TYPE (elt);
14626 /* It should only be possible to get here for a call. */
14627 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
14628 elt_strict |= maybe_adjust_types_for_deduction
14629 (DEDUCE_CALL, &elttype, &type, elt);
14633 if (unify (tparms, targs, elttype, elt, elt_strict))
14637 /* If the std::initializer_list<T> deduction worked, replace the
14638 deduced A with std::initializer_list<A>. */
14639 if (orig_parm != parm)
14641 idx = TEMPLATE_TYPE_IDX (orig_parm);
14642 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14643 targ = listify (targ);
14644 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
14649 /* Immediately reject some pairs that won't unify because of
14650 cv-qualification mismatches. */
14651 if (TREE_CODE (arg) == TREE_CODE (parm)
14653 /* It is the elements of the array which hold the cv quals of an array
14654 type, and the elements might be template type parms. We'll check
14655 when we recurse. */
14656 && TREE_CODE (arg) != ARRAY_TYPE
14657 /* We check the cv-qualifiers when unifying with template type
14658 parameters below. We want to allow ARG `const T' to unify with
14659 PARM `T' for example, when computing which of two templates
14660 is more specialized, for example. */
14661 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
14662 && !check_cv_quals_for_unify (strict_in, arg, parm))
14665 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
14666 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
14667 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
14668 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
14669 strict &= ~UNIFY_ALLOW_DERIVED;
14670 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
14671 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
14673 switch (TREE_CODE (parm))
14675 case TYPENAME_TYPE:
14677 case UNBOUND_CLASS_TEMPLATE:
14678 /* In a type which contains a nested-name-specifier, template
14679 argument values cannot be deduced for template parameters used
14680 within the nested-name-specifier. */
14683 case TEMPLATE_TYPE_PARM:
14684 case TEMPLATE_TEMPLATE_PARM:
14685 case BOUND_TEMPLATE_TEMPLATE_PARM:
14686 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14687 if (tparm == error_mark_node)
14690 if (TEMPLATE_TYPE_LEVEL (parm)
14691 != template_decl_level (tparm))
14692 /* The PARM is not one we're trying to unify. Just check
14693 to see if it matches ARG. */
14694 return (TREE_CODE (arg) == TREE_CODE (parm)
14695 && same_type_p (parm, arg)) ? 0 : 1;
14696 idx = TEMPLATE_TYPE_IDX (parm);
14697 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14698 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
14700 /* Check for mixed types and values. */
14701 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14702 && TREE_CODE (tparm) != TYPE_DECL)
14703 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14704 && TREE_CODE (tparm) != TEMPLATE_DECL))
14707 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14709 /* ARG must be constructed from a template class or a template
14710 template parameter. */
14711 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
14712 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
14716 tree parmvec = TYPE_TI_ARGS (parm);
14717 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
14719 = DECL_INNERMOST_TEMPLATE_PARMS
14720 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
14722 int parm_variadic_p = 0;
14724 /* The resolution to DR150 makes clear that default
14725 arguments for an N-argument may not be used to bind T
14726 to a template template parameter with fewer than N
14727 parameters. It is not safe to permit the binding of
14728 default arguments as an extension, as that may change
14729 the meaning of a conforming program. Consider:
14731 struct Dense { static const unsigned int dim = 1; };
14733 template <template <typename> class View,
14735 void operator+(float, View<Block> const&);
14737 template <typename Block,
14738 unsigned int Dim = Block::dim>
14739 struct Lvalue_proxy { operator float() const; };
14743 Lvalue_proxy<Dense> p;
14748 Here, if Lvalue_proxy is permitted to bind to View, then
14749 the global operator+ will be used; if they are not, the
14750 Lvalue_proxy will be converted to float. */
14751 if (coerce_template_parms (parm_parms,
14753 TYPE_TI_TEMPLATE (parm),
14755 /*require_all_args=*/true,
14756 /*use_default_args=*/false)
14757 == error_mark_node)
14760 /* Deduce arguments T, i from TT<T> or TT<i>.
14761 We check each element of PARMVEC and ARGVEC individually
14762 rather than the whole TREE_VEC since they can have
14763 different number of elements. */
14765 parmvec = expand_template_argument_pack (parmvec);
14766 argvec = expand_template_argument_pack (argvec);
14768 len = TREE_VEC_LENGTH (parmvec);
14770 /* Check if the parameters end in a pack, making them
14773 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
14774 parm_variadic_p = 1;
14776 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
14779 for (i = 0; i < len - parm_variadic_p; ++i)
14781 if (unify (tparms, targs,
14782 TREE_VEC_ELT (parmvec, i),
14783 TREE_VEC_ELT (argvec, i),
14788 if (parm_variadic_p
14789 && unify_pack_expansion (tparms, targs,
14792 /*call_args_p=*/false,
14796 arg = TYPE_TI_TEMPLATE (arg);
14798 /* Fall through to deduce template name. */
14801 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14802 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14804 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
14806 /* Simple cases: Value already set, does match or doesn't. */
14807 if (targ != NULL_TREE && template_args_equal (targ, arg))
14814 /* If PARM is `const T' and ARG is only `int', we don't have
14815 a match unless we are allowing additional qualification.
14816 If ARG is `const int' and PARM is just `T' that's OK;
14817 that binds `const int' to `T'. */
14818 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
14822 /* Consider the case where ARG is `const volatile int' and
14823 PARM is `const T'. Then, T should be `volatile int'. */
14824 arg = cp_build_qualified_type_real
14825 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
14826 if (arg == error_mark_node)
14829 /* Simple cases: Value already set, does match or doesn't. */
14830 if (targ != NULL_TREE && same_type_p (targ, arg))
14835 /* Make sure that ARG is not a variable-sized array. (Note
14836 that were talking about variable-sized arrays (like
14837 `int[n]'), rather than arrays of unknown size (like
14838 `int[]').) We'll get very confused by such a type since
14839 the bound of the array will not be computable in an
14840 instantiation. Besides, such types are not allowed in
14841 ISO C++, so we can do as we please here. */
14842 if (variably_modified_type_p (arg, NULL_TREE))
14845 /* Strip typedefs as in convert_template_argument. */
14846 arg = strip_typedefs (arg);
14849 /* If ARG is a parameter pack or an expansion, we cannot unify
14850 against it unless PARM is also a parameter pack. */
14851 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14852 && !template_parameter_pack_p (parm))
14855 /* If the argument deduction results is a METHOD_TYPE,
14856 then there is a problem.
14857 METHOD_TYPE doesn't map to any real C++ type the result of
14858 the deduction can not be of that type. */
14859 if (TREE_CODE (arg) == METHOD_TYPE)
14862 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14865 case TEMPLATE_PARM_INDEX:
14866 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14867 if (tparm == error_mark_node)
14870 if (TEMPLATE_PARM_LEVEL (parm)
14871 != template_decl_level (tparm))
14872 /* The PARM is not one we're trying to unify. Just check
14873 to see if it matches ARG. */
14874 return !(TREE_CODE (arg) == TREE_CODE (parm)
14875 && cp_tree_equal (parm, arg));
14877 idx = TEMPLATE_PARM_IDX (parm);
14878 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14881 return !cp_tree_equal (targ, arg);
14883 /* [temp.deduct.type] If, in the declaration of a function template
14884 with a non-type template-parameter, the non-type
14885 template-parameter is used in an expression in the function
14886 parameter-list and, if the corresponding template-argument is
14887 deduced, the template-argument type shall match the type of the
14888 template-parameter exactly, except that a template-argument
14889 deduced from an array bound may be of any integral type.
14890 The non-type parameter might use already deduced type parameters. */
14891 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
14892 if (!TREE_TYPE (arg))
14893 /* Template-parameter dependent expression. Just accept it for now.
14894 It will later be processed in convert_template_argument. */
14896 else if (same_type_p (TREE_TYPE (arg), tparm))
14898 else if ((strict & UNIFY_ALLOW_INTEGER)
14899 && (TREE_CODE (tparm) == INTEGER_TYPE
14900 || TREE_CODE (tparm) == BOOLEAN_TYPE))
14901 /* Convert the ARG to the type of PARM; the deduced non-type
14902 template argument must exactly match the types of the
14903 corresponding parameter. */
14904 arg = fold (build_nop (tparm, arg));
14905 else if (uses_template_parms (tparm))
14906 /* We haven't deduced the type of this parameter yet. Try again
14912 /* If ARG is a parameter pack or an expansion, we cannot unify
14913 against it unless PARM is also a parameter pack. */
14914 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14915 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
14918 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14923 /* A pointer-to-member constant can be unified only with
14924 another constant. */
14925 if (TREE_CODE (arg) != PTRMEM_CST)
14928 /* Just unify the class member. It would be useless (and possibly
14929 wrong, depending on the strict flags) to unify also
14930 PTRMEM_CST_CLASS, because we want to be sure that both parm and
14931 arg refer to the same variable, even if through different
14932 classes. For instance:
14934 struct A { int x; };
14937 Unification of &A::x and &B::x must succeed. */
14938 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
14939 PTRMEM_CST_MEMBER (arg), strict);
14944 if (TREE_CODE (arg) != POINTER_TYPE)
14947 /* [temp.deduct.call]
14949 A can be another pointer or pointer to member type that can
14950 be converted to the deduced A via a qualification
14951 conversion (_conv.qual_).
14953 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
14954 This will allow for additional cv-qualification of the
14955 pointed-to types if appropriate. */
14957 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
14958 /* The derived-to-base conversion only persists through one
14959 level of pointers. */
14960 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
14962 return unify (tparms, targs, TREE_TYPE (parm),
14963 TREE_TYPE (arg), strict);
14966 case REFERENCE_TYPE:
14967 if (TREE_CODE (arg) != REFERENCE_TYPE)
14969 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14970 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14973 if (TREE_CODE (arg) != ARRAY_TYPE)
14975 if ((TYPE_DOMAIN (parm) == NULL_TREE)
14976 != (TYPE_DOMAIN (arg) == NULL_TREE))
14978 if (TYPE_DOMAIN (parm) != NULL_TREE)
14985 /* Our representation of array types uses "N - 1" as the
14986 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
14987 not an integer constant. We cannot unify arbitrarily
14988 complex expressions, so we eliminate the MINUS_EXPRs
14990 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
14991 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
14994 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
14995 parm_max = TREE_OPERAND (parm_max, 0);
14997 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
14998 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
15001 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
15002 trying to unify the type of a variable with the type
15003 of a template parameter. For example:
15005 template <unsigned int N>
15006 void f (char (&) [N]);
15013 Here, the type of the ARG will be "int [g(i)]", and
15014 may be a SAVE_EXPR, etc. */
15015 if (TREE_CODE (arg_max) != MINUS_EXPR)
15017 arg_max = TREE_OPERAND (arg_max, 0);
15020 /* If only one of the bounds used a MINUS_EXPR, compensate
15021 by adding one to the other bound. */
15022 if (parm_cst && !arg_cst)
15023 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
15027 else if (arg_cst && !parm_cst)
15028 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
15033 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
15036 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
15037 strict & UNIFY_ALLOW_MORE_CV_QUAL);
15044 case ENUMERAL_TYPE:
15046 if (TREE_CODE (arg) != TREE_CODE (parm))
15049 /* We have already checked cv-qualification at the top of the
15051 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
15054 /* As far as unification is concerned, this wins. Later checks
15055 will invalidate it if necessary. */
15058 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
15059 /* Type INTEGER_CST can come from ordinary constant template args. */
15061 while (TREE_CODE (arg) == NOP_EXPR)
15062 arg = TREE_OPERAND (arg, 0);
15064 if (TREE_CODE (arg) != INTEGER_CST)
15066 return !tree_int_cst_equal (parm, arg);
15071 if (TREE_CODE (arg) != TREE_VEC)
15073 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
15075 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
15076 if (unify (tparms, targs,
15077 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
15085 if (TREE_CODE (arg) != TREE_CODE (parm))
15088 if (TYPE_PTRMEMFUNC_P (parm))
15090 if (!TYPE_PTRMEMFUNC_P (arg))
15093 return unify (tparms, targs,
15094 TYPE_PTRMEMFUNC_FN_TYPE (parm),
15095 TYPE_PTRMEMFUNC_FN_TYPE (arg),
15099 if (CLASSTYPE_TEMPLATE_INFO (parm))
15101 tree t = NULL_TREE;
15103 if (strict_in & UNIFY_ALLOW_DERIVED)
15105 /* First, we try to unify the PARM and ARG directly. */
15106 t = try_class_unification (tparms, targs,
15111 /* Fallback to the special case allowed in
15112 [temp.deduct.call]:
15114 If P is a class, and P has the form
15115 template-id, then A can be a derived class of
15116 the deduced A. Likewise, if P is a pointer to
15117 a class of the form template-id, A can be a
15118 pointer to a derived class pointed to by the
15120 t = get_template_base (tparms, targs, parm, arg);
15126 else if (CLASSTYPE_TEMPLATE_INFO (arg)
15127 && (CLASSTYPE_TI_TEMPLATE (parm)
15128 == CLASSTYPE_TI_TEMPLATE (arg)))
15129 /* Perhaps PARM is something like S<U> and ARG is S<int>.
15130 Then, we should unify `int' and `U'. */
15133 /* There's no chance of unification succeeding. */
15136 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
15137 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
15139 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
15144 case FUNCTION_TYPE:
15146 unsigned int nargs;
15151 if (TREE_CODE (arg) != TREE_CODE (parm))
15154 /* CV qualifications for methods can never be deduced, they must
15155 match exactly. We need to check them explicitly here,
15156 because type_unification_real treats them as any other
15157 cv-qualified parameter. */
15158 if (TREE_CODE (parm) == METHOD_TYPE
15159 && (!check_cv_quals_for_unify
15161 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
15162 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
15165 if (unify (tparms, targs, TREE_TYPE (parm),
15166 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
15169 nargs = list_length (TYPE_ARG_TYPES (arg));
15170 args = XALLOCAVEC (tree, nargs);
15171 for (a = TYPE_ARG_TYPES (arg), i = 0;
15172 a != NULL_TREE && a != void_list_node;
15173 a = TREE_CHAIN (a), ++i)
15174 args[i] = TREE_VALUE (a);
15177 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
15178 args, nargs, 1, DEDUCE_EXACT,
15183 /* Unify a pointer to member with a pointer to member function, which
15184 deduces the type of the member as a function type. */
15185 if (TYPE_PTRMEMFUNC_P (arg))
15190 /* Check top-level cv qualifiers */
15191 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
15194 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
15195 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
15198 /* Determine the type of the function we are unifying against. */
15199 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
15201 build_function_type (TREE_TYPE (method_type),
15202 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
15204 /* Extract the cv-qualifiers of the member function from the
15205 implicit object parameter and place them on the function
15206 type to be restored later. */
15207 fntype = apply_memfn_quals (fntype, type_memfn_quals (method_type));
15208 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
15211 if (TREE_CODE (arg) != OFFSET_TYPE)
15213 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
15214 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
15216 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
15220 if (DECL_TEMPLATE_PARM_P (parm))
15221 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
15222 if (arg != integral_constant_value (parm))
15227 case TEMPLATE_DECL:
15228 /* Matched cases are handled by the ARG == PARM test above. */
15232 /* A non-type template parameter that is a variable should be a
15233 an integral constant, in which case, it whould have been
15234 folded into its (constant) value. So we should not be getting
15235 a variable here. */
15236 gcc_unreachable ();
15238 case TYPE_ARGUMENT_PACK:
15239 case NONTYPE_ARGUMENT_PACK:
15241 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
15242 tree packed_args = ARGUMENT_PACK_ARGS (arg);
15243 int i, len = TREE_VEC_LENGTH (packed_parms);
15244 int argslen = TREE_VEC_LENGTH (packed_args);
15245 int parm_variadic_p = 0;
15247 for (i = 0; i < len; ++i)
15249 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
15252 /* We can unify against something with a trailing
15254 parm_variadic_p = 1;
15256 /* Since there is something following the pack
15257 expansion, we cannot unify this template argument
15264 /* If we don't have enough arguments to satisfy the parameters
15265 (not counting the pack expression at the end), or we have
15266 too many arguments for a parameter list that doesn't end in
15267 a pack expression, we can't unify. */
15268 if (argslen < (len - parm_variadic_p)
15269 || (argslen > len && !parm_variadic_p))
15272 /* Unify all of the parameters that precede the (optional)
15273 pack expression. */
15274 for (i = 0; i < len - parm_variadic_p; ++i)
15276 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
15277 TREE_VEC_ELT (packed_args, i), strict))
15281 if (parm_variadic_p)
15282 return unify_pack_expansion (tparms, targs,
15283 packed_parms, packed_args,
15284 strict, /*call_args_p=*/false,
15292 case DECLTYPE_TYPE:
15293 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
15298 /* Unification fails if we hit an error node. */
15302 gcc_assert (EXPR_P (parm));
15304 /* We must be looking at an expression. This can happen with
15308 void foo(S<I>, S<I + 2>);
15310 This is a "nondeduced context":
15314 The nondeduced contexts are:
15316 --A type that is a template-id in which one or more of
15317 the template-arguments is an expression that references
15318 a template-parameter.
15320 In these cases, we assume deduction succeeded, but don't
15321 actually infer any unifications. */
15323 if (!uses_template_parms (parm)
15324 && !template_args_equal (parm, arg))
15331 /* Note that DECL can be defined in this translation unit, if
15335 mark_definable (tree decl)
15338 DECL_NOT_REALLY_EXTERN (decl) = 1;
15339 FOR_EACH_CLONE (clone, decl)
15340 DECL_NOT_REALLY_EXTERN (clone) = 1;
15343 /* Called if RESULT is explicitly instantiated, or is a member of an
15344 explicitly instantiated class. */
15347 mark_decl_instantiated (tree result, int extern_p)
15349 SET_DECL_EXPLICIT_INSTANTIATION (result);
15351 /* If this entity has already been written out, it's too late to
15352 make any modifications. */
15353 if (TREE_ASM_WRITTEN (result))
15356 if (TREE_CODE (result) != FUNCTION_DECL)
15357 /* The TREE_PUBLIC flag for function declarations will have been
15358 set correctly by tsubst. */
15359 TREE_PUBLIC (result) = 1;
15361 /* This might have been set by an earlier implicit instantiation. */
15362 DECL_COMDAT (result) = 0;
15365 DECL_NOT_REALLY_EXTERN (result) = 0;
15368 mark_definable (result);
15369 /* Always make artificials weak. */
15370 if (DECL_ARTIFICIAL (result) && flag_weak)
15371 comdat_linkage (result);
15372 /* For WIN32 we also want to put explicit instantiations in
15373 linkonce sections. */
15374 else if (TREE_PUBLIC (result))
15375 maybe_make_one_only (result);
15378 /* If EXTERN_P, then this function will not be emitted -- unless
15379 followed by an explicit instantiation, at which point its linkage
15380 will be adjusted. If !EXTERN_P, then this function will be
15381 emitted here. In neither circumstance do we want
15382 import_export_decl to adjust the linkage. */
15383 DECL_INTERFACE_KNOWN (result) = 1;
15386 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
15387 important template arguments. If any are missing, we check whether
15388 they're important by using error_mark_node for substituting into any
15389 args that were used for partial ordering (the ones between ARGS and END)
15390 and seeing if it bubbles up. */
15393 check_undeduced_parms (tree targs, tree args, tree end)
15395 bool found = false;
15397 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
15398 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
15401 TREE_VEC_ELT (targs, i) = error_mark_node;
15405 for (; args != end; args = TREE_CHAIN (args))
15407 tree substed = tsubst (TREE_VALUE (args), targs, tf_none, NULL_TREE);
15408 if (substed == error_mark_node)
15415 /* Given two function templates PAT1 and PAT2, return:
15417 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
15418 -1 if PAT2 is more specialized than PAT1.
15419 0 if neither is more specialized.
15421 LEN indicates the number of parameters we should consider
15422 (defaulted parameters should not be considered).
15424 The 1998 std underspecified function template partial ordering, and
15425 DR214 addresses the issue. We take pairs of arguments, one from
15426 each of the templates, and deduce them against each other. One of
15427 the templates will be more specialized if all the *other*
15428 template's arguments deduce against its arguments and at least one
15429 of its arguments *does* *not* deduce against the other template's
15430 corresponding argument. Deduction is done as for class templates.
15431 The arguments used in deduction have reference and top level cv
15432 qualifiers removed. Iff both arguments were originally reference
15433 types *and* deduction succeeds in both directions, the template
15434 with the more cv-qualified argument wins for that pairing (if
15435 neither is more cv-qualified, they both are equal). Unlike regular
15436 deduction, after all the arguments have been deduced in this way,
15437 we do *not* verify the deduced template argument values can be
15438 substituted into non-deduced contexts.
15440 The logic can be a bit confusing here, because we look at deduce1 and
15441 targs1 to see if pat2 is at least as specialized, and vice versa; if we
15442 can find template arguments for pat1 to make arg1 look like arg2, that
15443 means that arg2 is at least as specialized as arg1. */
15446 more_specialized_fn (tree pat1, tree pat2, int len)
15448 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
15449 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
15450 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
15451 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
15452 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
15453 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
15454 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
15455 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
15456 tree origs1, origs2;
15457 bool lose1 = false;
15458 bool lose2 = false;
15460 /* Remove the this parameter from non-static member functions. If
15461 one is a non-static member function and the other is not a static
15462 member function, remove the first parameter from that function
15463 also. This situation occurs for operator functions where we
15464 locate both a member function (with this pointer) and non-member
15465 operator (with explicit first operand). */
15466 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
15468 len--; /* LEN is the number of significant arguments for DECL1 */
15469 args1 = TREE_CHAIN (args1);
15470 if (!DECL_STATIC_FUNCTION_P (decl2))
15471 args2 = TREE_CHAIN (args2);
15473 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
15475 args2 = TREE_CHAIN (args2);
15476 if (!DECL_STATIC_FUNCTION_P (decl1))
15479 args1 = TREE_CHAIN (args1);
15483 /* If only one is a conversion operator, they are unordered. */
15484 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
15487 /* Consider the return type for a conversion function */
15488 if (DECL_CONV_FN_P (decl1))
15490 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
15491 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
15495 processing_template_decl++;
15501 /* Stop when an ellipsis is seen. */
15502 && args1 != NULL_TREE && args2 != NULL_TREE)
15504 tree arg1 = TREE_VALUE (args1);
15505 tree arg2 = TREE_VALUE (args2);
15506 int deduce1, deduce2;
15510 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15511 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15513 /* When both arguments are pack expansions, we need only
15514 unify the patterns themselves. */
15515 arg1 = PACK_EXPANSION_PATTERN (arg1);
15516 arg2 = PACK_EXPANSION_PATTERN (arg2);
15518 /* This is the last comparison we need to do. */
15522 if (TREE_CODE (arg1) == REFERENCE_TYPE)
15524 arg1 = TREE_TYPE (arg1);
15525 quals1 = cp_type_quals (arg1);
15528 if (TREE_CODE (arg2) == REFERENCE_TYPE)
15530 arg2 = TREE_TYPE (arg2);
15531 quals2 = cp_type_quals (arg2);
15534 if ((quals1 < 0) != (quals2 < 0))
15536 /* Only of the args is a reference, see if we should apply
15537 array/function pointer decay to it. This is not part of
15538 DR214, but is, IMHO, consistent with the deduction rules
15539 for the function call itself, and with our earlier
15540 implementation of the underspecified partial ordering
15541 rules. (nathan). */
15544 switch (TREE_CODE (arg1))
15547 arg1 = TREE_TYPE (arg1);
15549 case FUNCTION_TYPE:
15550 arg1 = build_pointer_type (arg1);
15559 switch (TREE_CODE (arg2))
15562 arg2 = TREE_TYPE (arg2);
15564 case FUNCTION_TYPE:
15565 arg2 = build_pointer_type (arg2);
15574 arg1 = TYPE_MAIN_VARIANT (arg1);
15575 arg2 = TYPE_MAIN_VARIANT (arg2);
15577 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
15579 int i, len2 = list_length (args2);
15580 tree parmvec = make_tree_vec (1);
15581 tree argvec = make_tree_vec (len2);
15584 /* Setup the parameter vector, which contains only ARG1. */
15585 TREE_VEC_ELT (parmvec, 0) = arg1;
15587 /* Setup the argument vector, which contains the remaining
15589 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
15590 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15592 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
15593 argvec, UNIFY_ALLOW_NONE,
15594 /*call_args_p=*/false,
15597 /* We cannot deduce in the other direction, because ARG1 is
15598 a pack expansion but ARG2 is not. */
15601 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15603 int i, len1 = list_length (args1);
15604 tree parmvec = make_tree_vec (1);
15605 tree argvec = make_tree_vec (len1);
15608 /* Setup the parameter vector, which contains only ARG1. */
15609 TREE_VEC_ELT (parmvec, 0) = arg2;
15611 /* Setup the argument vector, which contains the remaining
15613 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
15614 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15616 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
15617 argvec, UNIFY_ALLOW_NONE,
15618 /*call_args_p=*/false,
15621 /* We cannot deduce in the other direction, because ARG2 is
15622 a pack expansion but ARG1 is not.*/
15628 /* The normal case, where neither argument is a pack
15630 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
15631 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
15634 /* If we couldn't deduce arguments for tparms1 to make arg1 match
15635 arg2, then arg2 is not as specialized as arg1. */
15641 /* "If, for a given type, deduction succeeds in both directions
15642 (i.e., the types are identical after the transformations above)
15643 and if the type from the argument template is more cv-qualified
15644 than the type from the parameter template (as described above)
15645 that type is considered to be more specialized than the other. If
15646 neither type is more cv-qualified than the other then neither type
15647 is more specialized than the other." */
15649 if (deduce1 && deduce2
15650 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
15652 if ((quals1 & quals2) == quals2)
15654 if ((quals1 & quals2) == quals1)
15658 if (lose1 && lose2)
15659 /* We've failed to deduce something in either direction.
15660 These must be unordered. */
15663 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15664 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15665 /* We have already processed all of the arguments in our
15666 handing of the pack expansion type. */
15669 args1 = TREE_CHAIN (args1);
15670 args2 = TREE_CHAIN (args2);
15673 /* "In most cases, all template parameters must have values in order for
15674 deduction to succeed, but for partial ordering purposes a template
15675 parameter may remain without a value provided it is not used in the
15676 types being used for partial ordering."
15678 Thus, if we are missing any of the targs1 we need to substitute into
15679 origs1, then pat2 is not as specialized as pat1. This can happen when
15680 there is a nondeduced context. */
15681 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
15683 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
15686 processing_template_decl--;
15688 /* All things being equal, if the next argument is a pack expansion
15689 for one function but not for the other, prefer the
15690 non-variadic function. FIXME this is bogus; see c++/41958. */
15692 && args1 && TREE_VALUE (args1)
15693 && args2 && TREE_VALUE (args2))
15695 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
15696 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
15699 if (lose1 == lose2)
15707 /* Determine which of two partial specializations is more specialized.
15709 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
15710 to the first partial specialization. The TREE_VALUE is the
15711 innermost set of template parameters for the partial
15712 specialization. PAT2 is similar, but for the second template.
15714 Return 1 if the first partial specialization is more specialized;
15715 -1 if the second is more specialized; 0 if neither is more
15718 See [temp.class.order] for information about determining which of
15719 two templates is more specialized. */
15722 more_specialized_class (tree pat1, tree pat2)
15727 bool any_deductions = false;
15729 tmpl1 = TREE_TYPE (pat1);
15730 tmpl2 = TREE_TYPE (pat2);
15732 /* Just like what happens for functions, if we are ordering between
15733 different class template specializations, we may encounter dependent
15734 types in the arguments, and we need our dependency check functions
15735 to behave correctly. */
15736 ++processing_template_decl;
15737 targs = get_class_bindings (TREE_VALUE (pat1),
15738 CLASSTYPE_TI_ARGS (tmpl1),
15739 CLASSTYPE_TI_ARGS (tmpl2));
15743 any_deductions = true;
15746 targs = get_class_bindings (TREE_VALUE (pat2),
15747 CLASSTYPE_TI_ARGS (tmpl2),
15748 CLASSTYPE_TI_ARGS (tmpl1));
15752 any_deductions = true;
15754 --processing_template_decl;
15756 /* In the case of a tie where at least one of the class templates
15757 has a parameter pack at the end, the template with the most
15758 non-packed parameters wins. */
15761 && (template_args_variadic_p (TREE_PURPOSE (pat1))
15762 || template_args_variadic_p (TREE_PURPOSE (pat2))))
15764 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
15765 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
15766 int len1 = TREE_VEC_LENGTH (args1);
15767 int len2 = TREE_VEC_LENGTH (args2);
15769 /* We don't count the pack expansion at the end. */
15770 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
15772 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
15777 else if (len1 < len2)
15784 /* Return the template arguments that will produce the function signature
15785 DECL from the function template FN, with the explicit template
15786 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
15787 also match. Return NULL_TREE if no satisfactory arguments could be
15791 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
15793 int ntparms = DECL_NTPARMS (fn);
15794 tree targs = make_tree_vec (ntparms);
15796 tree decl_arg_types;
15798 unsigned int nargs, ix;
15801 /* Substitute the explicit template arguments into the type of DECL.
15802 The call to fn_type_unification will handle substitution into the
15804 decl_type = TREE_TYPE (decl);
15805 if (explicit_args && uses_template_parms (decl_type))
15808 tree converted_args;
15810 if (DECL_TEMPLATE_INFO (decl))
15811 tmpl = DECL_TI_TEMPLATE (decl);
15813 /* We can get here for some invalid specializations. */
15817 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
15818 explicit_args, NULL_TREE,
15820 /*require_all_args=*/false,
15821 /*use_default_args=*/false);
15822 if (converted_args == error_mark_node)
15825 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
15826 if (decl_type == error_mark_node)
15830 /* Never do unification on the 'this' parameter. */
15831 decl_arg_types = skip_artificial_parms_for (decl,
15832 TYPE_ARG_TYPES (decl_type));
15834 nargs = list_length (decl_arg_types);
15835 args = XALLOCAVEC (tree, nargs);
15836 for (arg = decl_arg_types, ix = 0;
15837 arg != NULL_TREE && arg != void_list_node;
15838 arg = TREE_CHAIN (arg), ++ix)
15839 args[ix] = TREE_VALUE (arg);
15841 if (fn_type_unification (fn, explicit_args, targs,
15843 (check_rettype || DECL_CONV_FN_P (fn)
15844 ? TREE_TYPE (decl_type) : NULL_TREE),
15845 DEDUCE_EXACT, LOOKUP_NORMAL))
15851 /* Return the innermost template arguments that, when applied to a
15852 template specialization whose innermost template parameters are
15853 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
15856 For example, suppose we have:
15858 template <class T, class U> struct S {};
15859 template <class T> struct S<T*, int> {};
15861 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
15862 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
15863 int}. The resulting vector will be {double}, indicating that `T'
15864 is bound to `double'. */
15867 get_class_bindings (tree tparms, tree spec_args, tree args)
15869 int i, ntparms = TREE_VEC_LENGTH (tparms);
15871 tree innermost_deduced_args;
15873 innermost_deduced_args = make_tree_vec (ntparms);
15874 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15876 deduced_args = copy_node (args);
15877 SET_TMPL_ARGS_LEVEL (deduced_args,
15878 TMPL_ARGS_DEPTH (deduced_args),
15879 innermost_deduced_args);
15882 deduced_args = innermost_deduced_args;
15884 if (unify (tparms, deduced_args,
15885 INNERMOST_TEMPLATE_ARGS (spec_args),
15886 INNERMOST_TEMPLATE_ARGS (args),
15890 for (i = 0; i < ntparms; ++i)
15891 if (! TREE_VEC_ELT (innermost_deduced_args, i))
15894 /* Verify that nondeduced template arguments agree with the type
15895 obtained from argument deduction.
15899 struct A { typedef int X; };
15900 template <class T, class U> struct C {};
15901 template <class T> struct C<T, typename T::X> {};
15903 Then with the instantiation `C<A, int>', we can deduce that
15904 `T' is `A' but unify () does not check whether `typename T::X'
15906 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
15907 if (spec_args == error_mark_node
15908 /* We only need to check the innermost arguments; the other
15909 arguments will always agree. */
15910 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
15911 INNERMOST_TEMPLATE_ARGS (args)))
15914 /* Now that we have bindings for all of the template arguments,
15915 ensure that the arguments deduced for the template template
15916 parameters have compatible template parameter lists. See the use
15917 of template_template_parm_bindings_ok_p in fn_type_unification
15918 for more information. */
15919 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
15922 return deduced_args;
15925 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
15926 Return the TREE_LIST node with the most specialized template, if
15927 any. If there is no most specialized template, the error_mark_node
15930 Note that this function does not look at, or modify, the
15931 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
15932 returned is one of the elements of INSTANTIATIONS, callers may
15933 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
15934 and retrieve it from the value returned. */
15937 most_specialized_instantiation (tree templates)
15941 ++processing_template_decl;
15944 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
15948 if (get_bindings (TREE_VALUE (champ),
15949 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15950 NULL_TREE, /*check_ret=*/false))
15953 if (get_bindings (TREE_VALUE (fn),
15954 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15955 NULL_TREE, /*check_ret=*/false))
15962 /* Equally specialized, move to next function. If there
15963 is no next function, nothing's most specialized. */
15964 fn = TREE_CHAIN (fn);
15972 /* Now verify that champ is better than everything earlier in the
15973 instantiation list. */
15974 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
15975 if (get_bindings (TREE_VALUE (champ),
15976 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15977 NULL_TREE, /*check_ret=*/false)
15978 || !get_bindings (TREE_VALUE (fn),
15979 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15980 NULL_TREE, /*check_ret=*/false))
15986 processing_template_decl--;
15989 return error_mark_node;
15994 /* If DECL is a specialization of some template, return the most
15995 general such template. Otherwise, returns NULL_TREE.
15997 For example, given:
15999 template <class T> struct S { template <class U> void f(U); };
16001 if TMPL is `template <class U> void S<int>::f(U)' this will return
16002 the full template. This function will not trace past partial
16003 specializations, however. For example, given in addition:
16005 template <class T> struct S<T*> { template <class U> void f(U); };
16007 if TMPL is `template <class U> void S<int*>::f(U)' this will return
16008 `template <class T> template <class U> S<T*>::f(U)'. */
16011 most_general_template (tree decl)
16013 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
16014 an immediate specialization. */
16015 if (TREE_CODE (decl) == FUNCTION_DECL)
16017 if (DECL_TEMPLATE_INFO (decl)) {
16018 decl = DECL_TI_TEMPLATE (decl);
16020 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
16021 template friend. */
16022 if (TREE_CODE (decl) != TEMPLATE_DECL)
16028 /* Look for more and more general templates. */
16029 while (DECL_TEMPLATE_INFO (decl))
16031 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
16032 (See cp-tree.h for details.) */
16033 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
16036 if (CLASS_TYPE_P (TREE_TYPE (decl))
16037 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
16040 /* Stop if we run into an explicitly specialized class template. */
16041 if (!DECL_NAMESPACE_SCOPE_P (decl)
16042 && DECL_CONTEXT (decl)
16043 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
16046 decl = DECL_TI_TEMPLATE (decl);
16052 /* Return the most specialized of the class template partial
16053 specializations of TMPL which can produce TYPE, a specialization of
16054 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
16055 a _TYPE node corresponding to the partial specialization, while the
16056 TREE_PURPOSE is the set of template arguments that must be
16057 substituted into the TREE_TYPE in order to generate TYPE.
16059 If the choice of partial specialization is ambiguous, a diagnostic
16060 is issued, and the error_mark_node is returned. If there are no
16061 partial specializations of TMPL matching TYPE, then NULL_TREE is
16065 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
16067 tree list = NULL_TREE;
16073 tree outer_args = NULL_TREE;
16075 tmpl = most_general_template (tmpl);
16076 args = CLASSTYPE_TI_ARGS (type);
16078 /* For determining which partial specialization to use, only the
16079 innermost args are interesting. */
16080 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
16082 outer_args = strip_innermost_template_args (args, 1);
16083 args = INNERMOST_TEMPLATE_ARGS (args);
16086 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
16088 tree partial_spec_args;
16090 tree parms = TREE_VALUE (t);
16092 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
16094 ++processing_template_decl;
16100 /* Discard the outer levels of args, and then substitute in the
16101 template args from the enclosing class. */
16102 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
16103 partial_spec_args = tsubst_template_args
16104 (partial_spec_args, outer_args, tf_none, NULL_TREE);
16106 /* PARMS already refers to just the innermost parms, but the
16107 template parms in partial_spec_args had their levels lowered
16108 by tsubst, so we need to do the same for the parm list. We
16109 can't just tsubst the TREE_VEC itself, as tsubst wants to
16110 treat a TREE_VEC as an argument vector. */
16111 parms = copy_node (parms);
16112 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
16113 TREE_VEC_ELT (parms, i) =
16114 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
16118 partial_spec_args =
16119 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
16120 add_to_template_args (outer_args,
16121 partial_spec_args),
16123 /*require_all_args=*/true,
16124 /*use_default_args=*/true);
16126 --processing_template_decl;
16128 if (partial_spec_args == error_mark_node)
16129 return error_mark_node;
16131 spec_args = get_class_bindings (parms,
16137 spec_args = add_to_template_args (outer_args, spec_args);
16138 list = tree_cons (spec_args, TREE_VALUE (t), list);
16139 TREE_TYPE (list) = TREE_TYPE (t);
16146 ambiguous_p = false;
16149 t = TREE_CHAIN (t);
16150 for (; t; t = TREE_CHAIN (t))
16152 fate = more_specialized_class (champ, t);
16159 t = TREE_CHAIN (t);
16162 ambiguous_p = true;
16171 for (t = list; t && t != champ; t = TREE_CHAIN (t))
16173 fate = more_specialized_class (champ, t);
16176 ambiguous_p = true;
16184 char *spaces = NULL;
16185 if (!(complain & tf_error))
16186 return error_mark_node;
16187 error ("ambiguous class template instantiation for %q#T", type);
16188 str = TREE_CHAIN (list) ? _("candidates are:") : _("candidate is:");
16189 for (t = list; t; t = TREE_CHAIN (t))
16191 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
16192 spaces = spaces ? spaces : get_spaces (str);
16195 return error_mark_node;
16201 /* Explicitly instantiate DECL. */
16204 do_decl_instantiation (tree decl, tree storage)
16206 tree result = NULL_TREE;
16209 if (!decl || decl == error_mark_node)
16210 /* An error occurred, for which grokdeclarator has already issued
16211 an appropriate message. */
16213 else if (! DECL_LANG_SPECIFIC (decl))
16215 error ("explicit instantiation of non-template %q#D", decl);
16218 else if (TREE_CODE (decl) == VAR_DECL)
16220 /* There is an asymmetry here in the way VAR_DECLs and
16221 FUNCTION_DECLs are handled by grokdeclarator. In the case of
16222 the latter, the DECL we get back will be marked as a
16223 template instantiation, and the appropriate
16224 DECL_TEMPLATE_INFO will be set up. This does not happen for
16225 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
16226 should handle VAR_DECLs as it currently handles
16228 if (!DECL_CLASS_SCOPE_P (decl))
16230 error ("%qD is not a static data member of a class template", decl);
16233 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
16234 if (!result || TREE_CODE (result) != VAR_DECL)
16236 error ("no matching template for %qD found", decl);
16239 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
16241 error ("type %qT for explicit instantiation %qD does not match "
16242 "declared type %qT", TREE_TYPE (result), decl,
16247 else if (TREE_CODE (decl) != FUNCTION_DECL)
16249 error ("explicit instantiation of %q#D", decl);
16255 /* Check for various error cases. Note that if the explicit
16256 instantiation is valid the RESULT will currently be marked as an
16257 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
16258 until we get here. */
16260 if (DECL_TEMPLATE_SPECIALIZATION (result))
16262 /* DR 259 [temp.spec].
16264 Both an explicit instantiation and a declaration of an explicit
16265 specialization shall not appear in a program unless the explicit
16266 instantiation follows a declaration of the explicit specialization.
16268 For a given set of template parameters, if an explicit
16269 instantiation of a template appears after a declaration of an
16270 explicit specialization for that template, the explicit
16271 instantiation has no effect. */
16274 else if (DECL_EXPLICIT_INSTANTIATION (result))
16278 No program shall explicitly instantiate any template more
16281 We check DECL_NOT_REALLY_EXTERN so as not to complain when
16282 the first instantiation was `extern' and the second is not,
16283 and EXTERN_P for the opposite case. */
16284 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
16285 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
16286 /* If an "extern" explicit instantiation follows an ordinary
16287 explicit instantiation, the template is instantiated. */
16291 else if (!DECL_IMPLICIT_INSTANTIATION (result))
16293 error ("no matching template for %qD found", result);
16296 else if (!DECL_TEMPLATE_INFO (result))
16298 permerror (input_location, "explicit instantiation of non-template %q#D", result);
16302 if (storage == NULL_TREE)
16304 else if (storage == ridpointers[(int) RID_EXTERN])
16306 if (!in_system_header && (cxx_dialect == cxx98))
16307 pedwarn (input_location, OPT_pedantic,
16308 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
16313 error ("storage class %qD applied to template instantiation", storage);
16315 check_explicit_instantiation_namespace (result);
16316 mark_decl_instantiated (result, extern_p);
16318 instantiate_decl (result, /*defer_ok=*/1,
16319 /*expl_inst_class_mem_p=*/false);
16323 mark_class_instantiated (tree t, int extern_p)
16325 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
16326 SET_CLASSTYPE_INTERFACE_KNOWN (t);
16327 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
16328 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
16331 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
16332 rest_of_type_compilation (t, 1);
16336 /* Called from do_type_instantiation through binding_table_foreach to
16337 do recursive instantiation for the type bound in ENTRY. */
16339 bt_instantiate_type_proc (binding_entry entry, void *data)
16341 tree storage = *(tree *) data;
16343 if (MAYBE_CLASS_TYPE_P (entry->type)
16344 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
16345 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
16348 /* Called from do_type_instantiation to instantiate a member
16349 (a member function or a static member variable) of an
16350 explicitly instantiated class template. */
16352 instantiate_class_member (tree decl, int extern_p)
16354 mark_decl_instantiated (decl, extern_p);
16356 instantiate_decl (decl, /*defer_ok=*/1,
16357 /*expl_inst_class_mem_p=*/true);
16360 /* Perform an explicit instantiation of template class T. STORAGE, if
16361 non-null, is the RID for extern, inline or static. COMPLAIN is
16362 nonzero if this is called from the parser, zero if called recursively,
16363 since the standard is unclear (as detailed below). */
16366 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
16371 int previous_instantiation_extern_p = 0;
16373 if (TREE_CODE (t) == TYPE_DECL)
16376 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
16378 error ("explicit instantiation of non-template type %qT", t);
16384 if (!COMPLETE_TYPE_P (t))
16386 if (complain & tf_error)
16387 error ("explicit instantiation of %q#T before definition of template",
16392 if (storage != NULL_TREE)
16394 if (!in_system_header)
16396 if (storage == ridpointers[(int) RID_EXTERN])
16398 if (cxx_dialect == cxx98)
16399 pedwarn (input_location, OPT_pedantic,
16400 "ISO C++ 1998 forbids the use of %<extern%> on "
16401 "explicit instantiations");
16404 pedwarn (input_location, OPT_pedantic,
16405 "ISO C++ forbids the use of %qE"
16406 " on explicit instantiations", storage);
16409 if (storage == ridpointers[(int) RID_INLINE])
16411 else if (storage == ridpointers[(int) RID_EXTERN])
16413 else if (storage == ridpointers[(int) RID_STATIC])
16417 error ("storage class %qD applied to template instantiation",
16423 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
16425 /* DR 259 [temp.spec].
16427 Both an explicit instantiation and a declaration of an explicit
16428 specialization shall not appear in a program unless the explicit
16429 instantiation follows a declaration of the explicit specialization.
16431 For a given set of template parameters, if an explicit
16432 instantiation of a template appears after a declaration of an
16433 explicit specialization for that template, the explicit
16434 instantiation has no effect. */
16437 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
16441 No program shall explicitly instantiate any template more
16444 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
16445 instantiation was `extern'. If EXTERN_P then the second is.
16446 These cases are OK. */
16447 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
16449 if (!previous_instantiation_extern_p && !extern_p
16450 && (complain & tf_error))
16451 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
16453 /* If we've already instantiated the template, just return now. */
16454 if (!CLASSTYPE_INTERFACE_ONLY (t))
16458 check_explicit_instantiation_namespace (TYPE_NAME (t));
16459 mark_class_instantiated (t, extern_p);
16467 /* In contrast to implicit instantiation, where only the
16468 declarations, and not the definitions, of members are
16469 instantiated, we have here:
16473 The explicit instantiation of a class template specialization
16474 implies the instantiation of all of its members not
16475 previously explicitly specialized in the translation unit
16476 containing the explicit instantiation.
16478 Of course, we can't instantiate member template classes, since
16479 we don't have any arguments for them. Note that the standard
16480 is unclear on whether the instantiation of the members are
16481 *explicit* instantiations or not. However, the most natural
16482 interpretation is that it should be an explicit instantiation. */
16485 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
16486 if (TREE_CODE (tmp) == FUNCTION_DECL
16487 && DECL_TEMPLATE_INSTANTIATION (tmp))
16488 instantiate_class_member (tmp, extern_p);
16490 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
16491 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
16492 instantiate_class_member (tmp, extern_p);
16494 if (CLASSTYPE_NESTED_UTDS (t))
16495 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
16496 bt_instantiate_type_proc, &storage);
16500 /* Given a function DECL, which is a specialization of TMPL, modify
16501 DECL to be a re-instantiation of TMPL with the same template
16502 arguments. TMPL should be the template into which tsubst'ing
16503 should occur for DECL, not the most general template.
16505 One reason for doing this is a scenario like this:
16508 void f(const T&, int i);
16510 void g() { f(3, 7); }
16513 void f(const T& t, const int i) { }
16515 Note that when the template is first instantiated, with
16516 instantiate_template, the resulting DECL will have no name for the
16517 first parameter, and the wrong type for the second. So, when we go
16518 to instantiate the DECL, we regenerate it. */
16521 regenerate_decl_from_template (tree decl, tree tmpl)
16523 /* The arguments used to instantiate DECL, from the most general
16528 args = DECL_TI_ARGS (decl);
16529 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
16531 /* Make sure that we can see identifiers, and compute access
16533 push_access_scope (decl);
16535 if (TREE_CODE (decl) == FUNCTION_DECL)
16543 args_depth = TMPL_ARGS_DEPTH (args);
16544 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
16545 if (args_depth > parms_depth)
16546 args = get_innermost_template_args (args, parms_depth);
16548 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
16549 args, tf_error, NULL_TREE);
16551 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
16554 /* Merge parameter declarations. */
16555 decl_parm = skip_artificial_parms_for (decl,
16556 DECL_ARGUMENTS (decl));
16558 = skip_artificial_parms_for (code_pattern,
16559 DECL_ARGUMENTS (code_pattern));
16560 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
16565 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16566 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
16567 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
16569 parm_type = type_decays_to (parm_type);
16570 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16571 TREE_TYPE (decl_parm) = parm_type;
16572 attributes = DECL_ATTRIBUTES (pattern_parm);
16573 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16575 DECL_ATTRIBUTES (decl_parm) = attributes;
16576 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16578 decl_parm = DECL_CHAIN (decl_parm);
16579 pattern_parm = DECL_CHAIN (pattern_parm);
16581 /* Merge any parameters that match with the function parameter
16583 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
16586 tree expanded_types;
16587 /* Expand the TYPE_PACK_EXPANSION that provides the types for
16588 the parameters in this function parameter pack. */
16589 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
16590 args, tf_error, NULL_TREE);
16591 len = TREE_VEC_LENGTH (expanded_types);
16592 for (i = 0; i < len; i++)
16597 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16598 /* Rename the parameter to include the index. */
16599 DECL_NAME (decl_parm) =
16600 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
16601 parm_type = TREE_VEC_ELT (expanded_types, i);
16602 parm_type = type_decays_to (parm_type);
16603 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16604 TREE_TYPE (decl_parm) = parm_type;
16605 attributes = DECL_ATTRIBUTES (pattern_parm);
16606 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16608 DECL_ATTRIBUTES (decl_parm) = attributes;
16609 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16611 decl_parm = DECL_CHAIN (decl_parm);
16614 /* Merge additional specifiers from the CODE_PATTERN. */
16615 if (DECL_DECLARED_INLINE_P (code_pattern)
16616 && !DECL_DECLARED_INLINE_P (decl))
16617 DECL_DECLARED_INLINE_P (decl) = 1;
16619 else if (TREE_CODE (decl) == VAR_DECL)
16621 DECL_INITIAL (decl) =
16622 tsubst_expr (DECL_INITIAL (code_pattern), args,
16623 tf_error, DECL_TI_TEMPLATE (decl),
16624 /*integral_constant_expression_p=*/false);
16625 if (VAR_HAD_UNKNOWN_BOUND (decl))
16626 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
16627 tf_error, DECL_TI_TEMPLATE (decl));
16630 gcc_unreachable ();
16632 pop_access_scope (decl);
16635 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
16636 substituted to get DECL. */
16639 template_for_substitution (tree decl)
16641 tree tmpl = DECL_TI_TEMPLATE (decl);
16643 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
16644 for the instantiation. This is not always the most general
16645 template. Consider, for example:
16648 struct S { template <class U> void f();
16649 template <> void f<int>(); };
16651 and an instantiation of S<double>::f<int>. We want TD to be the
16652 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
16653 while (/* An instantiation cannot have a definition, so we need a
16654 more general template. */
16655 DECL_TEMPLATE_INSTANTIATION (tmpl)
16656 /* We must also deal with friend templates. Given:
16658 template <class T> struct S {
16659 template <class U> friend void f() {};
16662 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
16663 so far as the language is concerned, but that's still
16664 where we get the pattern for the instantiation from. On
16665 other hand, if the definition comes outside the class, say:
16667 template <class T> struct S {
16668 template <class U> friend void f();
16670 template <class U> friend void f() {}
16672 we don't need to look any further. That's what the check for
16673 DECL_INITIAL is for. */
16674 || (TREE_CODE (decl) == FUNCTION_DECL
16675 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
16676 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
16678 /* The present template, TD, should not be a definition. If it
16679 were a definition, we should be using it! Note that we
16680 cannot restructure the loop to just keep going until we find
16681 a template with a definition, since that might go too far if
16682 a specialization was declared, but not defined. */
16683 gcc_assert (TREE_CODE (decl) != VAR_DECL
16684 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
16686 /* Fetch the more general template. */
16687 tmpl = DECL_TI_TEMPLATE (tmpl);
16693 /* Returns true if we need to instantiate this template instance even if we
16694 know we aren't going to emit it.. */
16697 always_instantiate_p (tree decl)
16699 /* We always instantiate inline functions so that we can inline them. An
16700 explicit instantiation declaration prohibits implicit instantiation of
16701 non-inline functions. With high levels of optimization, we would
16702 normally inline non-inline functions -- but we're not allowed to do
16703 that for "extern template" functions. Therefore, we check
16704 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
16705 return ((TREE_CODE (decl) == FUNCTION_DECL
16706 && DECL_DECLARED_INLINE_P (decl))
16707 /* And we need to instantiate static data members so that
16708 their initializers are available in integral constant
16710 || (TREE_CODE (decl) == VAR_DECL
16711 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)));
16714 /* Produce the definition of D, a _DECL generated from a template. If
16715 DEFER_OK is nonzero, then we don't have to actually do the
16716 instantiation now; we just have to do it sometime. Normally it is
16717 an error if this is an explicit instantiation but D is undefined.
16718 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
16719 explicitly instantiated class template. */
16722 instantiate_decl (tree d, int defer_ok,
16723 bool expl_inst_class_mem_p)
16725 tree tmpl = DECL_TI_TEMPLATE (d);
16732 bool pattern_defined;
16734 location_t saved_loc = input_location;
16737 /* This function should only be used to instantiate templates for
16738 functions and static member variables. */
16739 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
16740 || TREE_CODE (d) == VAR_DECL);
16742 /* Variables are never deferred; if instantiation is required, they
16743 are instantiated right away. That allows for better code in the
16744 case that an expression refers to the value of the variable --
16745 if the variable has a constant value the referring expression can
16746 take advantage of that fact. */
16747 if (TREE_CODE (d) == VAR_DECL)
16750 /* Don't instantiate cloned functions. Instead, instantiate the
16751 functions they cloned. */
16752 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
16753 d = DECL_CLONED_FUNCTION (d);
16755 if (DECL_TEMPLATE_INSTANTIATED (d)
16756 || DECL_TEMPLATE_SPECIALIZATION (d))
16757 /* D has already been instantiated or explicitly specialized, so
16758 there's nothing for us to do here.
16760 It might seem reasonable to check whether or not D is an explicit
16761 instantiation, and, if so, stop here. But when an explicit
16762 instantiation is deferred until the end of the compilation,
16763 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
16764 the instantiation. */
16767 /* Check to see whether we know that this template will be
16768 instantiated in some other file, as with "extern template"
16770 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
16772 /* In general, we do not instantiate such templates. */
16773 if (external_p && !always_instantiate_p (d))
16776 gen_tmpl = most_general_template (tmpl);
16777 gen_args = DECL_TI_ARGS (d);
16779 if (tmpl != gen_tmpl)
16780 /* We should already have the extra args. */
16781 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
16782 == TMPL_ARGS_DEPTH (gen_args));
16783 /* And what's in the hash table should match D. */
16784 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
16785 || spec == NULL_TREE);
16787 /* This needs to happen before any tsubsting. */
16788 if (! push_tinst_level (d))
16791 timevar_push (TV_PARSE);
16793 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
16794 for the instantiation. */
16795 td = template_for_substitution (d);
16796 code_pattern = DECL_TEMPLATE_RESULT (td);
16798 /* We should never be trying to instantiate a member of a class
16799 template or partial specialization. */
16800 gcc_assert (d != code_pattern);
16802 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
16803 || DECL_TEMPLATE_SPECIALIZATION (td))
16804 /* In the case of a friend template whose definition is provided
16805 outside the class, we may have too many arguments. Drop the
16806 ones we don't need. The same is true for specializations. */
16807 args = get_innermost_template_args
16808 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
16812 if (TREE_CODE (d) == FUNCTION_DECL)
16813 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
16815 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
16817 /* We may be in the middle of deferred access check. Disable it now. */
16818 push_deferring_access_checks (dk_no_deferred);
16820 /* Unless an explicit instantiation directive has already determined
16821 the linkage of D, remember that a definition is available for
16823 if (pattern_defined
16824 && !DECL_INTERFACE_KNOWN (d)
16825 && !DECL_NOT_REALLY_EXTERN (d))
16826 mark_definable (d);
16828 input_location = DECL_SOURCE_LOCATION (d);
16830 /* If D is a member of an explicitly instantiated class template,
16831 and no definition is available, treat it like an implicit
16833 if (!pattern_defined && expl_inst_class_mem_p
16834 && DECL_EXPLICIT_INSTANTIATION (d))
16836 DECL_NOT_REALLY_EXTERN (d) = 0;
16837 DECL_INTERFACE_KNOWN (d) = 0;
16838 SET_DECL_IMPLICIT_INSTANTIATION (d);
16841 /* Recheck the substitutions to obtain any warning messages
16842 about ignoring cv qualifiers. Don't do this for artificial decls,
16843 as it breaks the context-sensitive substitution for lambda op(). */
16844 if (!defer_ok && !DECL_ARTIFICIAL (d))
16846 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
16847 tree type = TREE_TYPE (gen);
16849 /* Make sure that we can see identifiers, and compute access
16850 correctly. D is already the target FUNCTION_DECL with the
16852 push_access_scope (d);
16854 if (TREE_CODE (gen) == FUNCTION_DECL)
16856 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
16857 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
16859 /* Don't simply tsubst the function type, as that will give
16860 duplicate warnings about poor parameter qualifications.
16861 The function arguments are the same as the decl_arguments
16862 without the top level cv qualifiers. */
16863 type = TREE_TYPE (type);
16865 tsubst (type, gen_args, tf_warning_or_error, d);
16867 pop_access_scope (d);
16870 /* Defer all other templates, unless we have been explicitly
16871 forbidden from doing so. */
16872 if (/* If there is no definition, we cannot instantiate the
16875 /* If it's OK to postpone instantiation, do so. */
16877 /* If this is a static data member that will be defined
16878 elsewhere, we don't want to instantiate the entire data
16879 member, but we do want to instantiate the initializer so that
16880 we can substitute that elsewhere. */
16881 || (external_p && TREE_CODE (d) == VAR_DECL))
16883 /* The definition of the static data member is now required so
16884 we must substitute the initializer. */
16885 if (TREE_CODE (d) == VAR_DECL
16886 && !DECL_INITIAL (d)
16887 && DECL_INITIAL (code_pattern))
16892 ns = decl_namespace_context (d);
16893 push_nested_namespace (ns);
16894 push_nested_class (DECL_CONTEXT (d));
16895 init = tsubst_expr (DECL_INITIAL (code_pattern),
16897 tf_warning_or_error, NULL_TREE,
16898 /*integral_constant_expression_p=*/false);
16899 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
16900 /*asmspec_tree=*/NULL_TREE,
16901 LOOKUP_ONLYCONVERTING);
16902 pop_nested_class ();
16903 pop_nested_namespace (ns);
16906 /* We restore the source position here because it's used by
16907 add_pending_template. */
16908 input_location = saved_loc;
16910 if (at_eof && !pattern_defined
16911 && DECL_EXPLICIT_INSTANTIATION (d)
16912 && DECL_NOT_REALLY_EXTERN (d))
16915 The definition of a non-exported function template, a
16916 non-exported member function template, or a non-exported
16917 member function or static data member of a class template
16918 shall be present in every translation unit in which it is
16919 explicitly instantiated. */
16920 permerror (input_location, "explicit instantiation of %qD "
16921 "but no definition available", d);
16923 /* ??? Historically, we have instantiated inline functions, even
16924 when marked as "extern template". */
16925 if (!(external_p && TREE_CODE (d) == VAR_DECL))
16926 add_pending_template (d);
16929 /* Tell the repository that D is available in this translation unit
16930 -- and see if it is supposed to be instantiated here. */
16931 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
16933 /* In a PCH file, despite the fact that the repository hasn't
16934 requested instantiation in the PCH it is still possible that
16935 an instantiation will be required in a file that includes the
16938 add_pending_template (d);
16939 /* Instantiate inline functions so that the inliner can do its
16940 job, even though we'll not be emitting a copy of this
16942 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
16946 need_push = !cfun || !global_bindings_p ();
16948 push_to_top_level ();
16950 /* Mark D as instantiated so that recursive calls to
16951 instantiate_decl do not try to instantiate it again. */
16952 DECL_TEMPLATE_INSTANTIATED (d) = 1;
16954 /* Regenerate the declaration in case the template has been modified
16955 by a subsequent redeclaration. */
16956 regenerate_decl_from_template (d, td);
16958 /* We already set the file and line above. Reset them now in case
16959 they changed as a result of calling regenerate_decl_from_template. */
16960 input_location = DECL_SOURCE_LOCATION (d);
16962 if (TREE_CODE (d) == VAR_DECL)
16966 /* Clear out DECL_RTL; whatever was there before may not be right
16967 since we've reset the type of the declaration. */
16968 SET_DECL_RTL (d, NULL);
16969 DECL_IN_AGGR_P (d) = 0;
16971 /* The initializer is placed in DECL_INITIAL by
16972 regenerate_decl_from_template. Pull it out so that
16973 cp_finish_decl can process it. */
16974 init = DECL_INITIAL (d);
16975 DECL_INITIAL (d) = NULL_TREE;
16976 DECL_INITIALIZED_P (d) = 0;
16978 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
16979 initializer. That function will defer actual emission until
16980 we have a chance to determine linkage. */
16981 DECL_EXTERNAL (d) = 0;
16983 /* Enter the scope of D so that access-checking works correctly. */
16984 push_nested_class (DECL_CONTEXT (d));
16985 cp_finish_decl (d, init, false, NULL_TREE, 0);
16986 pop_nested_class ();
16988 else if (TREE_CODE (d) == FUNCTION_DECL)
16990 htab_t saved_local_specializations;
16995 /* Save away the current list, in case we are instantiating one
16996 template from within the body of another. */
16997 saved_local_specializations = local_specializations;
16999 /* Set up the list of local specializations. */
17000 local_specializations = htab_create (37,
17001 hash_local_specialization,
17002 eq_local_specializations,
17005 /* Set up context. */
17006 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
17008 /* Create substitution entries for the parameters. */
17009 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
17010 tmpl_parm = DECL_ARGUMENTS (subst_decl);
17011 spec_parm = DECL_ARGUMENTS (d);
17012 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
17014 register_local_specialization (spec_parm, tmpl_parm);
17015 spec_parm = skip_artificial_parms_for (d, spec_parm);
17016 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
17018 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
17020 register_local_specialization (spec_parm, tmpl_parm);
17021 tmpl_parm = DECL_CHAIN (tmpl_parm);
17022 spec_parm = DECL_CHAIN (spec_parm);
17024 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
17026 /* Register the (value) argument pack as a specialization of
17027 TMPL_PARM, then move on. */
17028 tree argpack = make_fnparm_pack (spec_parm);
17029 register_local_specialization (argpack, tmpl_parm);
17030 tmpl_parm = DECL_CHAIN (tmpl_parm);
17031 spec_parm = NULL_TREE;
17033 gcc_assert (!spec_parm);
17035 /* Substitute into the body of the function. */
17036 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
17037 tf_warning_or_error, tmpl,
17038 /*integral_constant_expression_p=*/false);
17040 /* Set the current input_location to the end of the function
17041 so that finish_function knows where we are. */
17042 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
17044 /* We don't need the local specializations any more. */
17045 htab_delete (local_specializations);
17046 local_specializations = saved_local_specializations;
17048 /* Finish the function. */
17049 d = finish_function (0);
17050 expand_or_defer_fn (d);
17053 /* We're not deferring instantiation any more. */
17054 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
17057 pop_from_top_level ();
17060 input_location = saved_loc;
17061 pop_deferring_access_checks ();
17062 pop_tinst_level ();
17064 timevar_pop (TV_PARSE);
17069 /* Run through the list of templates that we wish we could
17070 instantiate, and instantiate any we can. RETRIES is the
17071 number of times we retry pending template instantiation. */
17074 instantiate_pending_templates (int retries)
17077 location_t saved_loc = input_location;
17079 /* Instantiating templates may trigger vtable generation. This in turn
17080 may require further template instantiations. We place a limit here
17081 to avoid infinite loop. */
17082 if (pending_templates && retries >= max_tinst_depth)
17084 tree decl = pending_templates->tinst->decl;
17086 error ("template instantiation depth exceeds maximum of %d"
17087 " instantiating %q+D, possibly from virtual table generation"
17088 " (use -ftemplate-depth= to increase the maximum)",
17089 max_tinst_depth, decl);
17090 if (TREE_CODE (decl) == FUNCTION_DECL)
17091 /* Pretend that we defined it. */
17092 DECL_INITIAL (decl) = error_mark_node;
17098 struct pending_template **t = &pending_templates;
17099 struct pending_template *last = NULL;
17103 tree instantiation = reopen_tinst_level ((*t)->tinst);
17104 bool complete = false;
17106 if (TYPE_P (instantiation))
17110 if (!COMPLETE_TYPE_P (instantiation))
17112 instantiate_class_template (instantiation);
17113 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
17114 for (fn = TYPE_METHODS (instantiation);
17116 fn = TREE_CHAIN (fn))
17117 if (! DECL_ARTIFICIAL (fn))
17118 instantiate_decl (fn,
17120 /*expl_inst_class_mem_p=*/false);
17121 if (COMPLETE_TYPE_P (instantiation))
17125 complete = COMPLETE_TYPE_P (instantiation);
17129 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
17130 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
17133 = instantiate_decl (instantiation,
17135 /*expl_inst_class_mem_p=*/false);
17136 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
17140 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
17141 || DECL_TEMPLATE_INSTANTIATED (instantiation));
17145 /* If INSTANTIATION has been instantiated, then we don't
17146 need to consider it again in the future. */
17154 current_tinst_level = NULL;
17156 last_pending_template = last;
17158 while (reconsider);
17160 input_location = saved_loc;
17163 /* Substitute ARGVEC into T, which is a list of initializers for
17164 either base class or a non-static data member. The TREE_PURPOSEs
17165 are DECLs, and the TREE_VALUEs are the initializer values. Used by
17166 instantiate_decl. */
17169 tsubst_initializer_list (tree t, tree argvec)
17171 tree inits = NULL_TREE;
17173 for (; t; t = TREE_CHAIN (t))
17177 tree expanded_bases = NULL_TREE;
17178 tree expanded_arguments = NULL_TREE;
17181 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
17186 /* Expand the base class expansion type into separate base
17188 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
17189 tf_warning_or_error,
17191 if (expanded_bases == error_mark_node)
17194 /* We'll be building separate TREE_LISTs of arguments for
17196 len = TREE_VEC_LENGTH (expanded_bases);
17197 expanded_arguments = make_tree_vec (len);
17198 for (i = 0; i < len; i++)
17199 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
17201 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
17202 expand each argument in the TREE_VALUE of t. */
17203 expr = make_node (EXPR_PACK_EXPANSION);
17204 PACK_EXPANSION_PARAMETER_PACKS (expr) =
17205 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
17207 if (TREE_VALUE (t) == void_type_node)
17208 /* VOID_TYPE_NODE is used to indicate
17209 value-initialization. */
17211 for (i = 0; i < len; i++)
17212 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
17216 /* Substitute parameter packs into each argument in the
17218 in_base_initializer = 1;
17219 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
17221 tree expanded_exprs;
17223 /* Expand the argument. */
17224 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
17226 = tsubst_pack_expansion (expr, argvec,
17227 tf_warning_or_error,
17229 if (expanded_exprs == error_mark_node)
17232 /* Prepend each of the expanded expressions to the
17233 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
17234 for (i = 0; i < len; i++)
17236 TREE_VEC_ELT (expanded_arguments, i) =
17237 tree_cons (NULL_TREE,
17238 TREE_VEC_ELT (expanded_exprs, i),
17239 TREE_VEC_ELT (expanded_arguments, i));
17242 in_base_initializer = 0;
17244 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
17245 since we built them backwards. */
17246 for (i = 0; i < len; i++)
17248 TREE_VEC_ELT (expanded_arguments, i) =
17249 nreverse (TREE_VEC_ELT (expanded_arguments, i));
17254 for (i = 0; i < len; ++i)
17256 if (expanded_bases)
17258 decl = TREE_VEC_ELT (expanded_bases, i);
17259 decl = expand_member_init (decl);
17260 init = TREE_VEC_ELT (expanded_arguments, i);
17264 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
17265 tf_warning_or_error, NULL_TREE);
17267 decl = expand_member_init (decl);
17268 if (decl && !DECL_P (decl))
17269 in_base_initializer = 1;
17271 init = TREE_VALUE (t);
17272 if (init != void_type_node)
17273 init = tsubst_expr (init, argvec,
17274 tf_warning_or_error, NULL_TREE,
17275 /*integral_constant_expression_p=*/false);
17276 in_base_initializer = 0;
17281 init = build_tree_list (decl, init);
17282 TREE_CHAIN (init) = inits;
17290 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
17293 set_current_access_from_decl (tree decl)
17295 if (TREE_PRIVATE (decl))
17296 current_access_specifier = access_private_node;
17297 else if (TREE_PROTECTED (decl))
17298 current_access_specifier = access_protected_node;
17300 current_access_specifier = access_public_node;
17303 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
17304 is the instantiation (which should have been created with
17305 start_enum) and ARGS are the template arguments to use. */
17308 tsubst_enum (tree tag, tree newtag, tree args)
17312 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
17317 decl = TREE_VALUE (e);
17318 /* Note that in a template enum, the TREE_VALUE is the
17319 CONST_DECL, not the corresponding INTEGER_CST. */
17320 value = tsubst_expr (DECL_INITIAL (decl),
17321 args, tf_warning_or_error, NULL_TREE,
17322 /*integral_constant_expression_p=*/true);
17324 /* Give this enumeration constant the correct access. */
17325 set_current_access_from_decl (decl);
17327 /* Actually build the enumerator itself. */
17329 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
17332 finish_enum (newtag);
17333 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
17334 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
17337 /* DECL is a FUNCTION_DECL that is a template specialization. Return
17338 its type -- but without substituting the innermost set of template
17339 arguments. So, innermost set of template parameters will appear in
17343 get_mostly_instantiated_function_type (tree decl)
17351 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
17352 targs = DECL_TI_ARGS (decl);
17353 tparms = DECL_TEMPLATE_PARMS (tmpl);
17354 parm_depth = TMPL_PARMS_DEPTH (tparms);
17356 /* There should be as many levels of arguments as there are levels
17358 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
17360 fn_type = TREE_TYPE (tmpl);
17362 if (parm_depth == 1)
17363 /* No substitution is necessary. */
17367 int i, save_access_control;
17370 /* Replace the innermost level of the TARGS with NULL_TREEs to
17371 let tsubst know not to substitute for those parameters. */
17372 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
17373 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
17374 SET_TMPL_ARGS_LEVEL (partial_args, i,
17375 TMPL_ARGS_LEVEL (targs, i));
17376 SET_TMPL_ARGS_LEVEL (partial_args,
17377 TMPL_ARGS_DEPTH (targs),
17378 make_tree_vec (DECL_NTPARMS (tmpl)));
17380 /* Disable access control as this function is used only during
17382 save_access_control = flag_access_control;
17383 flag_access_control = 0;
17385 ++processing_template_decl;
17386 /* Now, do the (partial) substitution to figure out the
17387 appropriate function type. */
17388 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
17389 --processing_template_decl;
17391 /* Substitute into the template parameters to obtain the real
17392 innermost set of parameters. This step is important if the
17393 innermost set of template parameters contains value
17394 parameters whose types depend on outer template parameters. */
17395 TREE_VEC_LENGTH (partial_args)--;
17396 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
17398 flag_access_control = save_access_control;
17404 /* Return truthvalue if we're processing a template different from
17405 the last one involved in diagnostics. */
17407 problematic_instantiation_changed (void)
17409 return last_template_error_tick != tinst_level_tick;
17412 /* Remember current template involved in diagnostics. */
17414 record_last_problematic_instantiation (void)
17416 last_template_error_tick = tinst_level_tick;
17419 struct tinst_level *
17420 current_instantiation (void)
17422 return current_tinst_level;
17425 /* [temp.param] Check that template non-type parm TYPE is of an allowable
17426 type. Return zero for ok, nonzero for disallowed. Issue error and
17427 warning messages under control of COMPLAIN. */
17430 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
17432 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
17434 else if (POINTER_TYPE_P (type))
17436 else if (TYPE_PTR_TO_MEMBER_P (type))
17438 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
17440 else if (TREE_CODE (type) == TYPENAME_TYPE)
17443 if (complain & tf_error)
17444 error ("%q#T is not a valid type for a template constant parameter", type);
17448 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
17449 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
17452 dependent_type_p_r (tree type)
17458 A type is dependent if it is:
17460 -- a template parameter. Template template parameters are types
17461 for us (since TYPE_P holds true for them) so we handle
17463 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17464 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
17466 /* -- a qualified-id with a nested-name-specifier which contains a
17467 class-name that names a dependent type or whose unqualified-id
17468 names a dependent type. */
17469 if (TREE_CODE (type) == TYPENAME_TYPE)
17471 /* -- a cv-qualified type where the cv-unqualified type is
17473 type = TYPE_MAIN_VARIANT (type);
17474 /* -- a compound type constructed from any dependent type. */
17475 if (TYPE_PTR_TO_MEMBER_P (type))
17476 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
17477 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
17479 else if (TREE_CODE (type) == POINTER_TYPE
17480 || TREE_CODE (type) == REFERENCE_TYPE)
17481 return dependent_type_p (TREE_TYPE (type));
17482 else if (TREE_CODE (type) == FUNCTION_TYPE
17483 || TREE_CODE (type) == METHOD_TYPE)
17487 if (dependent_type_p (TREE_TYPE (type)))
17489 for (arg_type = TYPE_ARG_TYPES (type);
17491 arg_type = TREE_CHAIN (arg_type))
17492 if (dependent_type_p (TREE_VALUE (arg_type)))
17496 /* -- an array type constructed from any dependent type or whose
17497 size is specified by a constant expression that is
17500 We checked for type- and value-dependence of the bounds in
17501 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
17502 if (TREE_CODE (type) == ARRAY_TYPE)
17504 if (TYPE_DOMAIN (type)
17505 && dependent_type_p (TYPE_DOMAIN (type)))
17507 return dependent_type_p (TREE_TYPE (type));
17510 /* -- a template-id in which either the template name is a template
17512 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
17514 /* ... or any of the template arguments is a dependent type or
17515 an expression that is type-dependent or value-dependent. */
17516 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
17517 && (any_dependent_template_arguments_p
17518 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
17521 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
17522 argument of the `typeof' expression is not type-dependent, then
17523 it should already been have resolved. */
17524 if (TREE_CODE (type) == TYPEOF_TYPE
17525 || TREE_CODE (type) == DECLTYPE_TYPE)
17528 /* A template argument pack is dependent if any of its packed
17530 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
17532 tree args = ARGUMENT_PACK_ARGS (type);
17533 int i, len = TREE_VEC_LENGTH (args);
17534 for (i = 0; i < len; ++i)
17535 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17539 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
17540 be template parameters. */
17541 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
17544 /* The standard does not specifically mention types that are local
17545 to template functions or local classes, but they should be
17546 considered dependent too. For example:
17548 template <int I> void f() {
17553 The size of `E' cannot be known until the value of `I' has been
17554 determined. Therefore, `E' must be considered dependent. */
17555 scope = TYPE_CONTEXT (type);
17556 if (scope && TYPE_P (scope))
17557 return dependent_type_p (scope);
17558 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
17559 return type_dependent_expression_p (scope);
17561 /* Other types are non-dependent. */
17565 /* Returns TRUE if TYPE is dependent, in the sense of
17566 [temp.dep.type]. */
17569 dependent_type_p (tree type)
17571 /* If there are no template parameters in scope, then there can't be
17572 any dependent types. */
17573 if (!processing_template_decl)
17575 /* If we are not processing a template, then nobody should be
17576 providing us with a dependent type. */
17578 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
17582 /* If the type is NULL, we have not computed a type for the entity
17583 in question; in that case, the type is dependent. */
17587 /* Erroneous types can be considered non-dependent. */
17588 if (type == error_mark_node)
17591 /* If we have not already computed the appropriate value for TYPE,
17593 if (!TYPE_DEPENDENT_P_VALID (type))
17595 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
17596 TYPE_DEPENDENT_P_VALID (type) = 1;
17599 return TYPE_DEPENDENT_P (type);
17602 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
17603 lookup. In other words, a dependent type that is not the current
17607 dependent_scope_p (tree scope)
17609 return (scope && TYPE_P (scope) && dependent_type_p (scope)
17610 && !currently_open_class (scope));
17613 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
17614 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
17618 value_dependent_expression_p (tree expression)
17620 if (!processing_template_decl)
17623 /* A name declared with a dependent type. */
17624 if (DECL_P (expression) && type_dependent_expression_p (expression))
17627 switch (TREE_CODE (expression))
17629 case IDENTIFIER_NODE:
17630 /* A name that has not been looked up -- must be dependent. */
17633 case TEMPLATE_PARM_INDEX:
17634 /* A non-type template parm. */
17638 /* A non-type template parm. */
17639 if (DECL_TEMPLATE_PARM_P (expression))
17641 return value_dependent_expression_p (DECL_INITIAL (expression));
17644 /* A constant with integral or enumeration type and is initialized
17645 with an expression that is value-dependent. */
17646 if (DECL_INITIAL (expression)
17647 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
17648 && value_dependent_expression_p (DECL_INITIAL (expression)))
17652 case DYNAMIC_CAST_EXPR:
17653 case STATIC_CAST_EXPR:
17654 case CONST_CAST_EXPR:
17655 case REINTERPRET_CAST_EXPR:
17657 /* These expressions are value-dependent if the type to which
17658 the cast occurs is dependent or the expression being casted
17659 is value-dependent. */
17661 tree type = TREE_TYPE (expression);
17663 if (dependent_type_p (type))
17666 /* A functional cast has a list of operands. */
17667 expression = TREE_OPERAND (expression, 0);
17670 /* If there are no operands, it must be an expression such
17671 as "int()". This should not happen for aggregate types
17672 because it would form non-constant expressions. */
17673 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
17678 if (TREE_CODE (expression) == TREE_LIST)
17679 return any_value_dependent_elements_p (expression);
17681 return value_dependent_expression_p (expression);
17686 /* A `sizeof' expression is value-dependent if the operand is
17687 type-dependent or is a pack expansion. */
17688 expression = TREE_OPERAND (expression, 0);
17689 if (PACK_EXPANSION_P (expression))
17691 else if (TYPE_P (expression))
17692 return dependent_type_p (expression);
17693 return type_dependent_expression_p (expression);
17695 case AT_ENCODE_EXPR:
17696 /* An 'encode' expression is value-dependent if the operand is
17698 expression = TREE_OPERAND (expression, 0);
17699 return dependent_type_p (expression);
17701 case NOEXCEPT_EXPR:
17702 expression = TREE_OPERAND (expression, 0);
17703 /* FIXME why check value-dependency? */
17704 return (type_dependent_expression_p (expression)
17705 || value_dependent_expression_p (expression));
17709 tree name = TREE_OPERAND (expression, 1);
17710 return value_dependent_expression_p (name);
17713 case COMPONENT_REF:
17714 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
17715 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
17718 /* A CALL_EXPR may appear in a constant expression if it is a
17719 call to a builtin function, e.g., __builtin_constant_p. All
17720 such calls are value-dependent. */
17723 case NONTYPE_ARGUMENT_PACK:
17724 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
17725 is value-dependent. */
17727 tree values = ARGUMENT_PACK_ARGS (expression);
17728 int i, len = TREE_VEC_LENGTH (values);
17730 for (i = 0; i < len; ++i)
17731 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
17739 tree type2 = TRAIT_EXPR_TYPE2 (expression);
17740 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
17741 || (type2 ? dependent_type_p (type2) : false));
17745 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
17746 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
17750 tree op = TREE_OPERAND (expression, 0);
17751 return (value_dependent_expression_p (op)
17752 || has_value_dependent_address (op));
17756 /* A constant expression is value-dependent if any subexpression is
17757 value-dependent. */
17758 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
17760 case tcc_reference:
17762 return (value_dependent_expression_p
17763 (TREE_OPERAND (expression, 0)));
17765 case tcc_comparison:
17767 return ((value_dependent_expression_p
17768 (TREE_OPERAND (expression, 0)))
17769 || (value_dependent_expression_p
17770 (TREE_OPERAND (expression, 1))));
17772 case tcc_expression:
17776 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
17777 /* In some cases, some of the operands may be missing.
17778 (For example, in the case of PREDECREMENT_EXPR, the
17779 amount to increment by may be missing.) That doesn't
17780 make the expression dependent. */
17781 if (TREE_OPERAND (expression, i)
17782 && (value_dependent_expression_p
17783 (TREE_OPERAND (expression, i))))
17793 /* The expression is not value-dependent. */
17797 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
17798 [temp.dep.expr]. */
17801 type_dependent_expression_p (tree expression)
17803 if (!processing_template_decl)
17806 if (expression == error_mark_node)
17809 /* An unresolved name is always dependent. */
17810 if (TREE_CODE (expression) == IDENTIFIER_NODE
17811 || TREE_CODE (expression) == USING_DECL)
17814 /* Some expression forms are never type-dependent. */
17815 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
17816 || TREE_CODE (expression) == SIZEOF_EXPR
17817 || TREE_CODE (expression) == ALIGNOF_EXPR
17818 || TREE_CODE (expression) == AT_ENCODE_EXPR
17819 || TREE_CODE (expression) == NOEXCEPT_EXPR
17820 || TREE_CODE (expression) == TRAIT_EXPR
17821 || TREE_CODE (expression) == TYPEID_EXPR
17822 || TREE_CODE (expression) == DELETE_EXPR
17823 || TREE_CODE (expression) == VEC_DELETE_EXPR
17824 || TREE_CODE (expression) == THROW_EXPR)
17827 /* The types of these expressions depends only on the type to which
17828 the cast occurs. */
17829 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
17830 || TREE_CODE (expression) == STATIC_CAST_EXPR
17831 || TREE_CODE (expression) == CONST_CAST_EXPR
17832 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
17833 || TREE_CODE (expression) == CAST_EXPR)
17834 return dependent_type_p (TREE_TYPE (expression));
17836 /* The types of these expressions depends only on the type created
17837 by the expression. */
17838 if (TREE_CODE (expression) == NEW_EXPR
17839 || TREE_CODE (expression) == VEC_NEW_EXPR)
17841 /* For NEW_EXPR tree nodes created inside a template, either
17842 the object type itself or a TREE_LIST may appear as the
17844 tree type = TREE_OPERAND (expression, 1);
17845 if (TREE_CODE (type) == TREE_LIST)
17846 /* This is an array type. We need to check array dimensions
17848 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
17849 || value_dependent_expression_p
17850 (TREE_OPERAND (TREE_VALUE (type), 1));
17852 return dependent_type_p (type);
17855 if (TREE_CODE (expression) == SCOPE_REF)
17857 tree scope = TREE_OPERAND (expression, 0);
17858 tree name = TREE_OPERAND (expression, 1);
17860 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
17861 contains an identifier associated by name lookup with one or more
17862 declarations declared with a dependent type, or...a
17863 nested-name-specifier or qualified-id that names a member of an
17864 unknown specialization. */
17865 return (type_dependent_expression_p (name)
17866 || dependent_scope_p (scope));
17869 if (TREE_CODE (expression) == FUNCTION_DECL
17870 && DECL_LANG_SPECIFIC (expression)
17871 && DECL_TEMPLATE_INFO (expression)
17872 && (any_dependent_template_arguments_p
17873 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
17876 if (TREE_CODE (expression) == TEMPLATE_DECL
17877 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
17880 if (TREE_CODE (expression) == STMT_EXPR)
17881 expression = stmt_expr_value_expr (expression);
17883 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
17888 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
17890 if (type_dependent_expression_p (elt))
17896 /* A static data member of the current instantiation with incomplete
17897 array type is type-dependent, as the definition and specializations
17898 can have different bounds. */
17899 if (TREE_CODE (expression) == VAR_DECL
17900 && DECL_CLASS_SCOPE_P (expression)
17901 && dependent_type_p (DECL_CONTEXT (expression))
17902 && VAR_HAD_UNKNOWN_BOUND (expression))
17905 if (TREE_TYPE (expression) == unknown_type_node)
17907 if (TREE_CODE (expression) == ADDR_EXPR)
17908 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
17909 if (TREE_CODE (expression) == COMPONENT_REF
17910 || TREE_CODE (expression) == OFFSET_REF)
17912 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
17914 expression = TREE_OPERAND (expression, 1);
17915 if (TREE_CODE (expression) == IDENTIFIER_NODE)
17918 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
17919 if (TREE_CODE (expression) == SCOPE_REF)
17922 if (TREE_CODE (expression) == BASELINK)
17923 expression = BASELINK_FUNCTIONS (expression);
17925 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
17927 if (any_dependent_template_arguments_p
17928 (TREE_OPERAND (expression, 1)))
17930 expression = TREE_OPERAND (expression, 0);
17932 gcc_assert (TREE_CODE (expression) == OVERLOAD
17933 || TREE_CODE (expression) == FUNCTION_DECL);
17937 if (type_dependent_expression_p (OVL_CURRENT (expression)))
17939 expression = OVL_NEXT (expression);
17944 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
17946 return (dependent_type_p (TREE_TYPE (expression)));
17949 /* Like type_dependent_expression_p, but it also works while not processing
17950 a template definition, i.e. during substitution or mangling. */
17953 type_dependent_expression_p_push (tree expr)
17956 ++processing_template_decl;
17957 b = type_dependent_expression_p (expr);
17958 --processing_template_decl;
17962 /* Returns TRUE if ARGS contains a type-dependent expression. */
17965 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
17970 FOR_EACH_VEC_ELT (tree, args, i, arg)
17972 if (type_dependent_expression_p (arg))
17978 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
17979 expressions) contains any value-dependent expressions. */
17982 any_value_dependent_elements_p (const_tree list)
17984 for (; list; list = TREE_CHAIN (list))
17985 if (value_dependent_expression_p (TREE_VALUE (list)))
17991 /* Returns TRUE if the ARG (a template argument) is dependent. */
17994 dependent_template_arg_p (tree arg)
17996 if (!processing_template_decl)
17999 if (TREE_CODE (arg) == TEMPLATE_DECL
18000 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
18001 return dependent_template_p (arg);
18002 else if (ARGUMENT_PACK_P (arg))
18004 tree args = ARGUMENT_PACK_ARGS (arg);
18005 int i, len = TREE_VEC_LENGTH (args);
18006 for (i = 0; i < len; ++i)
18008 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
18014 else if (TYPE_P (arg))
18015 return dependent_type_p (arg);
18017 return (type_dependent_expression_p (arg)
18018 || value_dependent_expression_p (arg));
18021 /* Returns true if ARGS (a collection of template arguments) contains
18022 any types that require structural equality testing. */
18025 any_template_arguments_need_structural_equality_p (tree args)
18032 if (args == error_mark_node)
18035 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
18037 tree level = TMPL_ARGS_LEVEL (args, i + 1);
18038 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
18040 tree arg = TREE_VEC_ELT (level, j);
18041 tree packed_args = NULL_TREE;
18044 if (ARGUMENT_PACK_P (arg))
18046 /* Look inside the argument pack. */
18047 packed_args = ARGUMENT_PACK_ARGS (arg);
18048 len = TREE_VEC_LENGTH (packed_args);
18051 for (k = 0; k < len; ++k)
18054 arg = TREE_VEC_ELT (packed_args, k);
18056 if (error_operand_p (arg))
18058 else if (TREE_CODE (arg) == TEMPLATE_DECL
18059 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
18061 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
18063 else if (!TYPE_P (arg) && TREE_TYPE (arg)
18064 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
18073 /* Returns true if ARGS (a collection of template arguments) contains
18074 any dependent arguments. */
18077 any_dependent_template_arguments_p (const_tree args)
18084 if (args == error_mark_node)
18087 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
18089 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
18090 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
18091 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
18098 /* Returns TRUE if the template TMPL is dependent. */
18101 dependent_template_p (tree tmpl)
18103 if (TREE_CODE (tmpl) == OVERLOAD)
18107 if (dependent_template_p (OVL_FUNCTION (tmpl)))
18109 tmpl = OVL_CHAIN (tmpl);
18114 /* Template template parameters are dependent. */
18115 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
18116 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
18118 /* So are names that have not been looked up. */
18119 if (TREE_CODE (tmpl) == SCOPE_REF
18120 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
18122 /* So are member templates of dependent classes. */
18123 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
18124 return dependent_type_p (DECL_CONTEXT (tmpl));
18128 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
18131 dependent_template_id_p (tree tmpl, tree args)
18133 return (dependent_template_p (tmpl)
18134 || any_dependent_template_arguments_p (args));
18137 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
18141 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
18145 if (!processing_template_decl)
18148 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
18150 tree decl = TREE_VEC_ELT (declv, i);
18151 tree init = TREE_VEC_ELT (initv, i);
18152 tree cond = TREE_VEC_ELT (condv, i);
18153 tree incr = TREE_VEC_ELT (incrv, i);
18155 if (type_dependent_expression_p (decl))
18158 if (init && type_dependent_expression_p (init))
18161 if (type_dependent_expression_p (cond))
18164 if (COMPARISON_CLASS_P (cond)
18165 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
18166 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
18169 if (TREE_CODE (incr) == MODOP_EXPR)
18171 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
18172 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
18175 else if (type_dependent_expression_p (incr))
18177 else if (TREE_CODE (incr) == MODIFY_EXPR)
18179 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
18181 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
18183 tree t = TREE_OPERAND (incr, 1);
18184 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
18185 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
18194 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
18195 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
18196 no such TYPE can be found. Note that this function peers inside
18197 uninstantiated templates and therefore should be used only in
18198 extremely limited situations. ONLY_CURRENT_P restricts this
18199 peering to the currently open classes hierarchy (which is required
18200 when comparing types). */
18203 resolve_typename_type (tree type, bool only_current_p)
18212 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
18214 scope = TYPE_CONTEXT (type);
18215 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
18216 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
18217 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
18218 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
18219 identifier of the TYPENAME_TYPE anymore.
18220 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
18221 TYPENAME_TYPE instead, we avoid messing up with a possible
18222 typedef variant case. */
18223 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
18225 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
18226 it first before we can figure out what NAME refers to. */
18227 if (TREE_CODE (scope) == TYPENAME_TYPE)
18228 scope = resolve_typename_type (scope, only_current_p);
18229 /* If we don't know what SCOPE refers to, then we cannot resolve the
18231 if (TREE_CODE (scope) == TYPENAME_TYPE)
18233 /* If the SCOPE is a template type parameter, we have no way of
18234 resolving the name. */
18235 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
18237 /* If the SCOPE is not the current instantiation, there's no reason
18238 to look inside it. */
18239 if (only_current_p && !currently_open_class (scope))
18241 /* If this is a typedef, we don't want to look inside (c++/11987). */
18242 if (typedef_variant_p (type))
18244 /* If SCOPE isn't the template itself, it will not have a valid
18245 TYPE_FIELDS list. */
18246 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
18247 /* scope is either the template itself or a compatible instantiation
18248 like X<T>, so look up the name in the original template. */
18249 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
18251 /* scope is a partial instantiation, so we can't do the lookup or we
18252 will lose the template arguments. */
18254 /* Enter the SCOPE so that name lookup will be resolved as if we
18255 were in the class definition. In particular, SCOPE will no
18256 longer be considered a dependent type. */
18257 pushed_scope = push_scope (scope);
18258 /* Look up the declaration. */
18259 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
18261 result = NULL_TREE;
18263 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
18264 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
18267 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
18268 && TREE_CODE (decl) == TYPE_DECL)
18270 result = TREE_TYPE (decl);
18271 if (result == error_mark_node)
18272 result = NULL_TREE;
18274 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
18275 && DECL_CLASS_TEMPLATE_P (decl))
18279 /* Obtain the template and the arguments. */
18280 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
18281 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
18282 /* Instantiate the template. */
18283 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
18284 /*entering_scope=*/0,
18285 tf_error | tf_user);
18286 if (result == error_mark_node)
18287 result = NULL_TREE;
18290 /* Leave the SCOPE. */
18292 pop_scope (pushed_scope);
18294 /* If we failed to resolve it, return the original typename. */
18298 /* If lookup found a typename type, resolve that too. */
18299 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
18301 /* Ill-formed programs can cause infinite recursion here, so we
18302 must catch that. */
18303 TYPENAME_IS_RESOLVING_P (type) = 1;
18304 result = resolve_typename_type (result, only_current_p);
18305 TYPENAME_IS_RESOLVING_P (type) = 0;
18308 /* Qualify the resulting type. */
18309 quals = cp_type_quals (type);
18311 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
18316 /* EXPR is an expression which is not type-dependent. Return a proxy
18317 for EXPR that can be used to compute the types of larger
18318 expressions containing EXPR. */
18321 build_non_dependent_expr (tree expr)
18325 /* Preserve null pointer constants so that the type of things like
18326 "p == 0" where "p" is a pointer can be determined. */
18327 if (null_ptr_cst_p (expr))
18329 /* Preserve OVERLOADs; the functions must be available to resolve
18332 if (TREE_CODE (inner_expr) == STMT_EXPR)
18333 inner_expr = stmt_expr_value_expr (inner_expr);
18334 if (TREE_CODE (inner_expr) == ADDR_EXPR)
18335 inner_expr = TREE_OPERAND (inner_expr, 0);
18336 if (TREE_CODE (inner_expr) == COMPONENT_REF)
18337 inner_expr = TREE_OPERAND (inner_expr, 1);
18338 if (is_overloaded_fn (inner_expr)
18339 || TREE_CODE (inner_expr) == OFFSET_REF)
18341 /* There is no need to return a proxy for a variable. */
18342 if (TREE_CODE (expr) == VAR_DECL)
18344 /* Preserve string constants; conversions from string constants to
18345 "char *" are allowed, even though normally a "const char *"
18346 cannot be used to initialize a "char *". */
18347 if (TREE_CODE (expr) == STRING_CST)
18349 /* Preserve arithmetic constants, as an optimization -- there is no
18350 reason to create a new node. */
18351 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
18353 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
18354 There is at least one place where we want to know that a
18355 particular expression is a throw-expression: when checking a ?:
18356 expression, there are special rules if the second or third
18357 argument is a throw-expression. */
18358 if (TREE_CODE (expr) == THROW_EXPR)
18361 if (TREE_CODE (expr) == COND_EXPR)
18362 return build3 (COND_EXPR,
18364 TREE_OPERAND (expr, 0),
18365 (TREE_OPERAND (expr, 1)
18366 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
18367 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
18368 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
18369 if (TREE_CODE (expr) == COMPOUND_EXPR
18370 && !COMPOUND_EXPR_OVERLOADED (expr))
18371 return build2 (COMPOUND_EXPR,
18373 TREE_OPERAND (expr, 0),
18374 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
18376 /* If the type is unknown, it can't really be non-dependent */
18377 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
18379 /* Otherwise, build a NON_DEPENDENT_EXPR.
18381 REFERENCE_TYPEs are not stripped for expressions in templates
18382 because doing so would play havoc with mangling. Consider, for
18385 template <typename T> void f<T& g>() { g(); }
18387 In the body of "f", the expression for "g" will have
18388 REFERENCE_TYPE, even though the standard says that it should
18389 not. The reason is that we must preserve the syntactic form of
18390 the expression so that mangling (say) "f<g>" inside the body of
18391 "f" works out correctly. Therefore, the REFERENCE_TYPE is
18393 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
18396 /* ARGS is a vector of expressions as arguments to a function call.
18397 Replace the arguments with equivalent non-dependent expressions.
18398 This modifies ARGS in place. */
18401 make_args_non_dependent (VEC(tree,gc) *args)
18406 FOR_EACH_VEC_ELT (tree, args, ix, arg)
18408 tree newarg = build_non_dependent_expr (arg);
18410 VEC_replace (tree, args, ix, newarg);
18414 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
18415 with a level one deeper than the actual template parms. */
18420 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
18421 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
18422 TYPE_DECL, get_identifier ("auto"), au);
18423 TYPE_STUB_DECL (au) = TYPE_NAME (au);
18424 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
18425 (0, processing_template_decl + 1, processing_template_decl + 1,
18426 TYPE_NAME (au), NULL_TREE);
18427 TYPE_CANONICAL (au) = canonical_type_parameter (au);
18428 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
18429 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
18434 /* Given type ARG, return std::initializer_list<ARG>. */
18439 tree std_init_list = namespace_binding
18440 (get_identifier ("initializer_list"), std_node);
18442 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
18444 error ("deducing from brace-enclosed initializer list requires "
18445 "#include <initializer_list>");
18446 return error_mark_node;
18448 argvec = make_tree_vec (1);
18449 TREE_VEC_ELT (argvec, 0) = arg;
18450 return lookup_template_class (std_init_list, argvec, NULL_TREE,
18451 NULL_TREE, 0, tf_warning_or_error);
18454 /* Replace auto in TYPE with std::initializer_list<auto>. */
18457 listify_autos (tree type, tree auto_node)
18459 tree init_auto = listify (auto_node);
18460 tree argvec = make_tree_vec (1);
18461 TREE_VEC_ELT (argvec, 0) = init_auto;
18462 if (processing_template_decl)
18463 argvec = add_to_template_args (current_template_args (), argvec);
18464 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18467 /* walk_tree helper for do_auto_deduction. */
18470 contains_auto_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
18473 /* Is this a variable with the type we're looking for? */
18475 && TREE_TYPE (*tp) == type)
18481 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
18482 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
18485 do_auto_deduction (tree type, tree init, tree auto_node)
18487 tree parms, tparms, targs;
18492 /* The name of the object being declared shall not appear in the
18493 initializer expression. */
18494 decl = cp_walk_tree_without_duplicates (&init, contains_auto_r, type);
18497 error ("variable %q#D with %<auto%> type used in its own "
18498 "initializer", decl);
18499 return error_mark_node;
18502 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
18503 with either a new invented type template parameter U or, if the
18504 initializer is a braced-init-list (8.5.4), with
18505 std::initializer_list<U>. */
18506 if (BRACE_ENCLOSED_INITIALIZER_P (init))
18507 type = listify_autos (type, auto_node);
18509 parms = build_tree_list (NULL_TREE, type);
18511 tparms = make_tree_vec (1);
18512 targs = make_tree_vec (1);
18513 TREE_VEC_ELT (tparms, 0)
18514 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
18515 val = type_unification_real (tparms, targs, parms, args, 1, 0,
18516 DEDUCE_CALL, LOOKUP_NORMAL);
18519 error ("unable to deduce %qT from %qE", type, init);
18520 return error_mark_node;
18523 /* If the list of declarators contains more than one declarator, the type
18524 of each declared variable is determined as described above. If the
18525 type deduced for the template parameter U is not the same in each
18526 deduction, the program is ill-formed. */
18527 if (TREE_TYPE (auto_node)
18528 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
18530 error ("inconsistent deduction for %qT: %qT and then %qT",
18531 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
18532 return error_mark_node;
18534 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
18536 if (processing_template_decl)
18537 targs = add_to_template_args (current_template_args (), targs);
18538 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
18541 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
18545 splice_late_return_type (tree type, tree late_return_type)
18549 if (late_return_type == NULL_TREE)
18551 argvec = make_tree_vec (1);
18552 TREE_VEC_ELT (argvec, 0) = late_return_type;
18553 if (processing_template_decl)
18554 argvec = add_to_template_args (current_template_args (), argvec);
18555 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18558 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
18561 is_auto (const_tree type)
18563 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18564 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
18570 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
18571 appear as a type-specifier for the declaration in question, we don't
18572 have to look through the whole type. */
18575 type_uses_auto (tree type)
18577 enum tree_code code;
18578 if (is_auto (type))
18581 code = TREE_CODE (type);
18583 if (code == POINTER_TYPE || code == REFERENCE_TYPE
18584 || code == OFFSET_TYPE || code == FUNCTION_TYPE
18585 || code == METHOD_TYPE || code == ARRAY_TYPE)
18586 return type_uses_auto (TREE_TYPE (type));
18588 if (TYPE_PTRMEMFUNC_P (type))
18589 return type_uses_auto (TREE_TYPE (TREE_TYPE
18590 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
18595 /* For a given template T, return the vector of typedefs referenced
18596 in T for which access check is needed at T instantiation time.
18597 T is either a FUNCTION_DECL or a RECORD_TYPE.
18598 Those typedefs were added to T by the function
18599 append_type_to_template_for_access_check. */
18601 VEC(qualified_typedef_usage_t,gc)*
18602 get_types_needing_access_check (tree t)
18605 VEC(qualified_typedef_usage_t,gc) *result = NULL;
18607 if (!t || t == error_mark_node)
18610 if (!(ti = get_template_info (t)))
18613 if (CLASS_TYPE_P (t)
18614 || TREE_CODE (t) == FUNCTION_DECL)
18616 if (!TI_TEMPLATE (ti))
18619 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
18625 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
18626 tied to T. That list of typedefs will be access checked at
18627 T instantiation time.
18628 T is either a FUNCTION_DECL or a RECORD_TYPE.
18629 TYPE_DECL is a TYPE_DECL node representing a typedef.
18630 SCOPE is the scope through which TYPE_DECL is accessed.
18631 LOCATION is the location of the usage point of TYPE_DECL.
18633 This function is a subroutine of
18634 append_type_to_template_for_access_check. */
18637 append_type_to_template_for_access_check_1 (tree t,
18640 location_t location)
18642 qualified_typedef_usage_t typedef_usage;
18645 if (!t || t == error_mark_node)
18648 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
18649 || CLASS_TYPE_P (t))
18651 && TREE_CODE (type_decl) == TYPE_DECL
18654 if (!(ti = get_template_info (t)))
18657 gcc_assert (TI_TEMPLATE (ti));
18659 typedef_usage.typedef_decl = type_decl;
18660 typedef_usage.context = scope;
18661 typedef_usage.locus = location;
18663 VEC_safe_push (qualified_typedef_usage_t, gc,
18664 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
18668 /* Append TYPE_DECL to the template TEMPL.
18669 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
18670 At TEMPL instanciation time, TYPE_DECL will be checked to see
18671 if it can be accessed through SCOPE.
18672 LOCATION is the location of the usage point of TYPE_DECL.
18674 e.g. consider the following code snippet:
18681 template<class U> struct S
18683 C::myint mi; // <-- usage point of the typedef C::myint
18688 At S<char> instantiation time, we need to check the access of C::myint
18689 In other words, we need to check the access of the myint typedef through
18690 the C scope. For that purpose, this function will add the myint typedef
18691 and the scope C through which its being accessed to a list of typedefs
18692 tied to the template S. That list will be walked at template instantiation
18693 time and access check performed on each typedefs it contains.
18694 Note that this particular code snippet should yield an error because
18695 myint is private to C. */
18698 append_type_to_template_for_access_check (tree templ,
18701 location_t location)
18703 qualified_typedef_usage_t *iter;
18706 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
18708 /* Make sure we don't append the type to the template twice. */
18709 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
18710 get_types_needing_access_check (templ),
18712 if (iter->typedef_decl == type_decl && scope == iter->context)
18715 append_type_to_template_for_access_check_1 (templ, type_decl,
18719 /* Set up the hash tables for template instantiations. */
18722 init_template_processing (void)
18724 decl_specializations = htab_create_ggc (37,
18725 hash_specialization,
18726 eq_specializations,
18728 type_specializations = htab_create_ggc (37,
18729 hash_specialization,
18730 eq_specializations,
18734 /* Print stats about the template hash tables for -fstats. */
18737 print_template_statistics (void)
18739 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
18740 "%f collisions\n", (long) htab_size (decl_specializations),
18741 (long) htab_elements (decl_specializations),
18742 htab_collisions (decl_specializations));
18743 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
18744 "%f collisions\n", (long) htab_size (type_specializations),
18745 (long) htab_elements (type_specializations),
18746 htab_collisions (type_specializations));
18749 #include "gt-cp-pt.h"