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, 2011
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 "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
41 #include "tree-inline.h"
46 #include "tree-iterator.h"
49 /* The type of functions taking a tree, and some additional data, and
51 typedef int (*tree_fn_t) (tree, void*);
53 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
54 instantiations have been deferred, either because their definitions
55 were not yet available, or because we were putting off doing the work. */
56 struct GTY ((chain_next ("%h.next"))) pending_template {
57 struct pending_template *next;
58 struct tinst_level *tinst;
61 static GTY(()) struct pending_template *pending_templates;
62 static GTY(()) struct pending_template *last_pending_template;
64 int processing_template_parmlist;
65 static int template_header_count;
67 static GTY(()) tree saved_trees;
68 static VEC(int,heap) *inline_parm_levels;
70 static GTY(()) struct tinst_level *current_tinst_level;
72 static GTY(()) tree saved_access_scope;
74 /* Live only within one (recursive) call to tsubst_expr. We use
75 this to pass the statement expression node from the STMT_EXPR
76 to the EXPR_STMT that is its result. */
77 static tree cur_stmt_expr;
79 /* A map from local variable declarations in the body of the template
80 presently being instantiated to the corresponding instantiated
82 static htab_t local_specializations;
84 typedef struct GTY(()) spec_entry
91 static GTY ((param_is (spec_entry)))
92 htab_t decl_specializations;
94 static GTY ((param_is (spec_entry)))
95 htab_t type_specializations;
97 /* Contains canonical template parameter types. The vector is indexed by
98 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
99 TREE_LIST, whose TREE_VALUEs contain the canonical template
100 parameters of various types and levels. */
101 static GTY(()) VEC(tree,gc) *canonical_template_parms;
103 #define UNIFY_ALLOW_NONE 0
104 #define UNIFY_ALLOW_MORE_CV_QUAL 1
105 #define UNIFY_ALLOW_LESS_CV_QUAL 2
106 #define UNIFY_ALLOW_DERIVED 4
107 #define UNIFY_ALLOW_INTEGER 8
108 #define UNIFY_ALLOW_OUTER_LEVEL 16
109 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
110 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
112 static void push_access_scope (tree);
113 static void pop_access_scope (tree);
114 static bool resolve_overloaded_unification (tree, tree, tree, tree,
115 unification_kind_t, int);
116 static int try_one_overload (tree, tree, tree, tree, tree,
117 unification_kind_t, int, bool);
118 static int unify (tree, tree, tree, tree, int);
119 static void add_pending_template (tree);
120 static tree reopen_tinst_level (struct tinst_level *);
121 static tree tsubst_initializer_list (tree, tree);
122 static tree get_class_bindings (tree, tree, tree);
123 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
125 static void tsubst_enum (tree, tree, tree);
126 static tree add_to_template_args (tree, tree);
127 static tree add_outermost_template_args (tree, tree);
128 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
129 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
131 static int type_unification_real (tree, tree, tree, const tree *,
132 unsigned int, int, unification_kind_t, int);
133 static void note_template_header (int);
134 static tree convert_nontype_argument_function (tree, tree);
135 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
136 static tree convert_template_argument (tree, tree, tree,
137 tsubst_flags_t, int, tree);
138 static int for_each_template_parm (tree, tree_fn_t, void*,
139 struct pointer_set_t*, bool);
140 static tree expand_template_argument_pack (tree);
141 static tree build_template_parm_index (int, int, int, int, tree, tree);
142 static bool inline_needs_template_parms (tree);
143 static void push_inline_template_parms_recursive (tree, int);
144 static tree retrieve_local_specialization (tree);
145 static void register_local_specialization (tree, tree);
146 static hashval_t hash_specialization (const void *p);
147 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
148 static int mark_template_parm (tree, void *);
149 static int template_parm_this_level_p (tree, void *);
150 static tree tsubst_friend_function (tree, tree);
151 static tree tsubst_friend_class (tree, tree);
152 static int can_complete_type_without_circularity (tree);
153 static tree get_bindings (tree, tree, tree, bool);
154 static int template_decl_level (tree);
155 static int check_cv_quals_for_unify (int, tree, tree);
156 static void template_parm_level_and_index (tree, int*, int*);
157 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
158 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
159 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
160 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
161 static void regenerate_decl_from_template (tree, tree);
162 static tree most_specialized_class (tree, tree, tsubst_flags_t);
163 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
164 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
165 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
166 static bool check_specialization_scope (void);
167 static tree process_partial_specialization (tree);
168 static void set_current_access_from_decl (tree);
169 static tree get_template_base (tree, tree, tree, tree);
170 static tree try_class_unification (tree, tree, tree, tree);
171 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
173 static bool template_template_parm_bindings_ok_p (tree, tree);
174 static int template_args_equal (tree, tree);
175 static void tsubst_default_arguments (tree);
176 static tree for_each_template_parm_r (tree *, int *, void *);
177 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
178 static void copy_default_args_to_explicit_spec (tree);
179 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
180 static int eq_local_specializations (const void *, const void *);
181 static bool dependent_template_arg_p (tree);
182 static bool any_template_arguments_need_structural_equality_p (tree);
183 static bool dependent_type_p_r (tree);
184 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
185 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
186 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
187 static tree tsubst_decl (tree, tree, tsubst_flags_t);
188 static void perform_typedefs_access_check (tree tmpl, tree targs);
189 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
191 static tree listify (tree);
192 static tree listify_autos (tree, tree);
193 static tree template_parm_to_arg (tree t);
194 static tree current_template_args (void);
195 static tree fixup_template_type_parm_type (tree, int);
196 static tree fixup_template_parm_index (tree, tree, int);
197 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
199 /* Make the current scope suitable for access checking when we are
200 processing T. T can be FUNCTION_DECL for instantiated function
201 template, or VAR_DECL for static member variable (need by
202 instantiate_decl). */
205 push_access_scope (tree t)
207 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
208 || TREE_CODE (t) == VAR_DECL);
210 if (DECL_FRIEND_CONTEXT (t))
211 push_nested_class (DECL_FRIEND_CONTEXT (t));
212 else if (DECL_CLASS_SCOPE_P (t))
213 push_nested_class (DECL_CONTEXT (t));
215 push_to_top_level ();
217 if (TREE_CODE (t) == FUNCTION_DECL)
219 saved_access_scope = tree_cons
220 (NULL_TREE, current_function_decl, saved_access_scope);
221 current_function_decl = t;
225 /* Restore the scope set up by push_access_scope. T is the node we
229 pop_access_scope (tree t)
231 if (TREE_CODE (t) == FUNCTION_DECL)
233 current_function_decl = TREE_VALUE (saved_access_scope);
234 saved_access_scope = TREE_CHAIN (saved_access_scope);
237 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
240 pop_from_top_level ();
243 /* Do any processing required when DECL (a member template
244 declaration) is finished. Returns the TEMPLATE_DECL corresponding
245 to DECL, unless it is a specialization, in which case the DECL
246 itself is returned. */
249 finish_member_template_decl (tree decl)
251 if (decl == error_mark_node)
252 return error_mark_node;
254 gcc_assert (DECL_P (decl));
256 if (TREE_CODE (decl) == TYPE_DECL)
260 type = TREE_TYPE (decl);
261 if (type == error_mark_node)
262 return error_mark_node;
263 if (MAYBE_CLASS_TYPE_P (type)
264 && CLASSTYPE_TEMPLATE_INFO (type)
265 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
267 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
268 check_member_template (tmpl);
273 else if (TREE_CODE (decl) == FIELD_DECL)
274 error ("data member %qD cannot be a member template", decl);
275 else if (DECL_TEMPLATE_INFO (decl))
277 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
279 check_member_template (DECL_TI_TEMPLATE (decl));
280 return DECL_TI_TEMPLATE (decl);
286 error ("invalid member template declaration %qD", decl);
288 return error_mark_node;
291 /* Create a template info node. */
294 build_template_info (tree template_decl, tree template_args)
296 tree result = make_node (TEMPLATE_INFO);
297 TI_TEMPLATE (result) = template_decl;
298 TI_ARGS (result) = template_args;
302 /* Return the template info node corresponding to T, whatever T is. */
305 get_template_info (const_tree t)
307 tree tinfo = NULL_TREE;
309 if (!t || t == error_mark_node)
312 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
313 tinfo = DECL_TEMPLATE_INFO (t);
315 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
318 if (TAGGED_TYPE_P (t))
319 tinfo = TYPE_TEMPLATE_INFO (t);
320 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
321 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
326 /* Returns the template nesting level of the indicated class TYPE.
336 A<T>::B<U> has depth two, while A<T> has depth one.
337 Both A<T>::B<int> and A<int>::B<U> have depth one, if
338 they are instantiations, not specializations.
340 This function is guaranteed to return 0 if passed NULL_TREE so
341 that, for example, `template_class_depth (current_class_type)' is
345 template_class_depth (tree type)
350 type && TREE_CODE (type) != NAMESPACE_DECL;
351 type = (TREE_CODE (type) == FUNCTION_DECL)
352 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
354 tree tinfo = get_template_info (type);
356 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
357 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
364 /* Subroutine of maybe_begin_member_template_processing.
365 Returns true if processing DECL needs us to push template parms. */
368 inline_needs_template_parms (tree decl)
370 if (! DECL_TEMPLATE_INFO (decl))
373 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
374 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
377 /* Subroutine of maybe_begin_member_template_processing.
378 Push the template parms in PARMS, starting from LEVELS steps into the
379 chain, and ending at the beginning, since template parms are listed
383 push_inline_template_parms_recursive (tree parmlist, int levels)
385 tree parms = TREE_VALUE (parmlist);
389 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
391 ++processing_template_decl;
392 current_template_parms
393 = tree_cons (size_int (processing_template_decl),
394 parms, current_template_parms);
395 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
397 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
399 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
401 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
403 if (parm == error_mark_node)
406 gcc_assert (DECL_P (parm));
408 switch (TREE_CODE (parm))
417 /* Make a CONST_DECL as is done in process_template_parm.
418 It is ugly that we recreate this here; the original
419 version built in process_template_parm is no longer
421 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
422 CONST_DECL, DECL_NAME (parm),
424 DECL_ARTIFICIAL (decl) = 1;
425 TREE_CONSTANT (decl) = 1;
426 TREE_READONLY (decl) = 1;
427 DECL_INITIAL (decl) = DECL_INITIAL (parm);
428 SET_DECL_TEMPLATE_PARM_P (decl);
439 /* Restore the template parameter context for a member template or
440 a friend template defined in a class definition. */
443 maybe_begin_member_template_processing (tree decl)
448 if (inline_needs_template_parms (decl))
450 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
451 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
453 if (DECL_TEMPLATE_SPECIALIZATION (decl))
456 parms = TREE_CHAIN (parms);
459 push_inline_template_parms_recursive (parms, levels);
462 /* Remember how many levels of template parameters we pushed so that
463 we can pop them later. */
464 VEC_safe_push (int, heap, inline_parm_levels, levels);
467 /* Undo the effects of maybe_begin_member_template_processing. */
470 maybe_end_member_template_processing (void)
475 if (VEC_length (int, inline_parm_levels) == 0)
478 last = VEC_pop (int, inline_parm_levels);
479 for (i = 0; i < last; ++i)
481 --processing_template_decl;
482 current_template_parms = TREE_CHAIN (current_template_parms);
487 /* Return a new template argument vector which contains all of ARGS,
488 but has as its innermost set of arguments the EXTRA_ARGS. */
491 add_to_template_args (tree args, tree extra_args)
498 if (args == NULL_TREE || extra_args == error_mark_node)
501 extra_depth = TMPL_ARGS_DEPTH (extra_args);
502 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
504 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
505 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
507 for (j = 1; j <= extra_depth; ++j, ++i)
508 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
513 /* Like add_to_template_args, but only the outermost ARGS are added to
514 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
515 (EXTRA_ARGS) levels are added. This function is used to combine
516 the template arguments from a partial instantiation with the
517 template arguments used to attain the full instantiation from the
518 partial instantiation. */
521 add_outermost_template_args (tree args, tree extra_args)
525 /* If there are more levels of EXTRA_ARGS than there are ARGS,
526 something very fishy is going on. */
527 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
529 /* If *all* the new arguments will be the EXTRA_ARGS, just return
531 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
534 /* For the moment, we make ARGS look like it contains fewer levels. */
535 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
537 new_args = add_to_template_args (args, extra_args);
539 /* Now, we restore ARGS to its full dimensions. */
540 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
545 /* Return the N levels of innermost template arguments from the ARGS. */
548 get_innermost_template_args (tree args, int n)
556 /* If N is 1, just return the innermost set of template arguments. */
558 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
560 /* If we're not removing anything, just return the arguments we were
562 extra_levels = TMPL_ARGS_DEPTH (args) - n;
563 gcc_assert (extra_levels >= 0);
564 if (extra_levels == 0)
567 /* Make a new set of arguments, not containing the outer arguments. */
568 new_args = make_tree_vec (n);
569 for (i = 1; i <= n; ++i)
570 SET_TMPL_ARGS_LEVEL (new_args, i,
571 TMPL_ARGS_LEVEL (args, i + extra_levels));
576 /* The inverse of get_innermost_template_args: Return all but the innermost
577 EXTRA_LEVELS levels of template arguments from the ARGS. */
580 strip_innermost_template_args (tree args, int extra_levels)
583 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
588 /* If N is 1, just return the outermost set of template arguments. */
590 return TMPL_ARGS_LEVEL (args, 1);
592 /* If we're not removing anything, just return the arguments we were
594 gcc_assert (extra_levels >= 0);
595 if (extra_levels == 0)
598 /* Make a new set of arguments, not containing the inner arguments. */
599 new_args = make_tree_vec (n);
600 for (i = 1; i <= n; ++i)
601 SET_TMPL_ARGS_LEVEL (new_args, i,
602 TMPL_ARGS_LEVEL (args, i));
607 /* We've got a template header coming up; push to a new level for storing
611 begin_template_parm_list (void)
613 /* We use a non-tag-transparent scope here, which causes pushtag to
614 put tags in this scope, rather than in the enclosing class or
615 namespace scope. This is the right thing, since we want
616 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
617 global template class, push_template_decl handles putting the
618 TEMPLATE_DECL into top-level scope. For a nested template class,
621 template <class T> struct S1 {
622 template <class T> struct S2 {};
625 pushtag contains special code to call pushdecl_with_scope on the
626 TEMPLATE_DECL for S2. */
627 begin_scope (sk_template_parms, NULL);
628 ++processing_template_decl;
629 ++processing_template_parmlist;
630 note_template_header (0);
633 /* This routine is called when a specialization is declared. If it is
634 invalid to declare a specialization here, an error is reported and
635 false is returned, otherwise this routine will return true. */
638 check_specialization_scope (void)
640 tree scope = current_scope ();
644 An explicit specialization shall be declared in the namespace of
645 which the template is a member, or, for member templates, in the
646 namespace of which the enclosing class or enclosing class
647 template is a member. An explicit specialization of a member
648 function, member class or static data member of a class template
649 shall be declared in the namespace of which the class template
651 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
653 error ("explicit specialization in non-namespace scope %qD", scope);
659 In an explicit specialization declaration for a member of a class
660 template or a member template that appears in namespace scope,
661 the member template and some of its enclosing class templates may
662 remain unspecialized, except that the declaration shall not
663 explicitly specialize a class member template if its enclosing
664 class templates are not explicitly specialized as well. */
665 if (current_template_parms)
667 error ("enclosing class templates are not explicitly specialized");
674 /* We've just seen template <>. */
677 begin_specialization (void)
679 begin_scope (sk_template_spec, NULL);
680 note_template_header (1);
681 return check_specialization_scope ();
684 /* Called at then end of processing a declaration preceded by
688 end_specialization (void)
691 reset_specialization ();
694 /* Any template <>'s that we have seen thus far are not referring to a
695 function specialization. */
698 reset_specialization (void)
700 processing_specialization = 0;
701 template_header_count = 0;
704 /* We've just seen a template header. If SPECIALIZATION is nonzero,
705 it was of the form template <>. */
708 note_template_header (int specialization)
710 processing_specialization = specialization;
711 template_header_count++;
714 /* We're beginning an explicit instantiation. */
717 begin_explicit_instantiation (void)
719 gcc_assert (!processing_explicit_instantiation);
720 processing_explicit_instantiation = true;
725 end_explicit_instantiation (void)
727 gcc_assert (processing_explicit_instantiation);
728 processing_explicit_instantiation = false;
731 /* An explicit specialization or partial specialization TMPL is being
732 declared. Check that the namespace in which the specialization is
733 occurring is permissible. Returns false iff it is invalid to
734 specialize TMPL in the current namespace. */
737 check_specialization_namespace (tree tmpl)
739 tree tpl_ns = decl_namespace_context (tmpl);
743 An explicit specialization shall be declared in the namespace of
744 which the template is a member, or, for member templates, in the
745 namespace of which the enclosing class or enclosing class
746 template is a member. An explicit specialization of a member
747 function, member class or static data member of a class template
748 shall be declared in the namespace of which the class template is
750 if (current_scope() != DECL_CONTEXT (tmpl)
751 && !at_namespace_scope_p ())
753 error ("specialization of %qD must appear at namespace scope", tmpl);
756 if (is_associated_namespace (current_namespace, tpl_ns))
757 /* Same or super-using namespace. */
761 permerror (input_location, "specialization of %qD in different namespace", tmpl);
762 permerror (input_location, " from definition of %q+#D", tmpl);
767 /* SPEC is an explicit instantiation. Check that it is valid to
768 perform this explicit instantiation in the current namespace. */
771 check_explicit_instantiation_namespace (tree spec)
775 /* DR 275: An explicit instantiation shall appear in an enclosing
776 namespace of its template. */
777 ns = decl_namespace_context (spec);
778 if (!is_ancestor (current_namespace, ns))
779 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
780 "(which does not enclose namespace %qD)",
781 spec, current_namespace, ns);
784 /* The TYPE is being declared. If it is a template type, that means it
785 is a partial specialization. Do appropriate error-checking. */
788 maybe_process_partial_specialization (tree type)
792 if (type == error_mark_node)
793 return error_mark_node;
795 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
797 error ("name of class shadows template template parameter %qD",
799 return error_mark_node;
802 context = TYPE_CONTEXT (type);
804 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
806 /* This is for ordinary explicit specialization and partial
807 specialization of a template class such as:
809 template <> class C<int>;
813 template <class T> class C<T*>;
815 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
817 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
818 && !COMPLETE_TYPE_P (type))
820 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
821 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
822 if (processing_template_decl)
824 if (push_template_decl (TYPE_MAIN_DECL (type))
826 return error_mark_node;
829 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
830 error ("specialization of %qT after instantiation", type);
832 else if (CLASS_TYPE_P (type)
833 && !CLASSTYPE_USE_TEMPLATE (type)
834 && CLASSTYPE_TEMPLATE_INFO (type)
835 && context && CLASS_TYPE_P (context)
836 && CLASSTYPE_TEMPLATE_INFO (context))
838 /* This is for an explicit specialization of member class
839 template according to [temp.expl.spec/18]:
841 template <> template <class U> class C<int>::D;
843 The context `C<int>' must be an implicit instantiation.
844 Otherwise this is just a member class template declared
847 template <> class C<int> { template <class U> class D; };
848 template <> template <class U> class C<int>::D;
850 In the first case, `C<int>::D' is a specialization of `C<T>::D'
851 while in the second case, `C<int>::D' is a primary template
852 and `C<T>::D' may not exist. */
854 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
855 && !COMPLETE_TYPE_P (type))
858 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
860 if (current_namespace
861 != decl_namespace_context (tmpl))
863 permerror (input_location, "specializing %q#T in different namespace", type);
864 permerror (input_location, " from definition of %q+#D", tmpl);
867 /* Check for invalid specialization after instantiation:
869 template <> template <> class C<int>::D<int>;
870 template <> template <class U> class C<int>::D; */
872 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
873 t; t = TREE_CHAIN (t))
875 tree inst = TREE_VALUE (t);
876 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
878 /* We already have a full specialization of this partial
879 instantiation. Reassign it to the new member
880 specialization template. */
884 elt.tmpl = most_general_template (tmpl);
885 elt.args = CLASSTYPE_TI_ARGS (inst);
888 htab_remove_elt (type_specializations, &elt);
891 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
893 slot = (spec_entry **)
894 htab_find_slot (type_specializations, &elt, INSERT);
895 *slot = ggc_alloc_spec_entry ();
898 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
899 /* But if we've had an implicit instantiation, that's a
900 problem ([temp.expl.spec]/6). */
901 error ("specialization %qT after instantiation %qT",
905 /* Mark TYPE as a specialization. And as a result, we only
906 have one level of template argument for the innermost
908 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
909 CLASSTYPE_TI_ARGS (type)
910 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
913 else if (processing_specialization)
915 /* Someday C++0x may allow for enum template specialization. */
916 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
917 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
918 pedwarn (input_location, OPT_pedantic, "template specialization "
919 "of %qD not allowed by ISO C++", type);
922 error ("explicit specialization of non-template %qT", type);
923 return error_mark_node;
930 /* Returns nonzero if we can optimize the retrieval of specializations
931 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
932 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
935 optimize_specialization_lookup_p (tree tmpl)
937 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
938 && DECL_CLASS_SCOPE_P (tmpl)
939 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
941 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
942 /* The optimized lookup depends on the fact that the
943 template arguments for the member function template apply
944 purely to the containing class, which is not true if the
945 containing class is an explicit or partial
947 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
948 && !DECL_MEMBER_TEMPLATE_P (tmpl)
949 && !DECL_CONV_FN_P (tmpl)
950 /* It is possible to have a template that is not a member
951 template and is not a member of a template class:
953 template <typename T>
954 struct S { friend A::f(); };
956 Here, the friend function is a template, but the context does
957 not have template information. The optimized lookup relies
958 on having ARGS be the template arguments for both the class
959 and the function template. */
960 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
963 /* Retrieve the specialization (in the sense of [temp.spec] - a
964 specialization is either an instantiation or an explicit
965 specialization) of TMPL for the given template ARGS. If there is
966 no such specialization, return NULL_TREE. The ARGS are a vector of
967 arguments, or a vector of vectors of arguments, in the case of
968 templates with more than one level of parameters.
970 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
971 then we search for a partial specialization matching ARGS. This
972 parameter is ignored if TMPL is not a class template. */
975 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
977 if (args == error_mark_node)
980 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
982 /* There should be as many levels of arguments as there are
983 levels of parameters. */
984 gcc_assert (TMPL_ARGS_DEPTH (args)
985 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
987 if (optimize_specialization_lookup_p (tmpl))
990 tree class_specialization;
991 VEC(tree,gc) *methods;
995 /* The template arguments actually apply to the containing
996 class. Find the class specialization with those
998 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1000 = retrieve_specialization (class_template, args, 0);
1001 if (!class_specialization)
1003 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1004 for the specialization. */
1005 idx = class_method_index_for_fn (class_specialization, tmpl);
1008 /* Iterate through the methods with the indicated name, looking
1009 for the one that has an instance of TMPL. */
1010 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1011 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
1013 tree fn = OVL_CURRENT (fns);
1014 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1015 /* using-declarations can add base methods to the method vec,
1016 and we don't want those here. */
1017 && DECL_CONTEXT (fn) == class_specialization)
1026 htab_t specializations;
1030 elt.spec = NULL_TREE;
1032 if (DECL_CLASS_TEMPLATE_P (tmpl))
1033 specializations = type_specializations;
1035 specializations = decl_specializations;
1038 hash = hash_specialization (&elt);
1039 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1047 /* Like retrieve_specialization, but for local declarations. */
1050 retrieve_local_specialization (tree tmpl)
1054 if (local_specializations == NULL)
1057 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
1058 htab_hash_pointer (tmpl));
1059 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
1062 /* Returns nonzero iff DECL is a specialization of TMPL. */
1065 is_specialization_of (tree decl, tree tmpl)
1069 if (TREE_CODE (decl) == FUNCTION_DECL)
1073 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1079 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1081 for (t = TREE_TYPE (decl);
1083 t = CLASSTYPE_USE_TEMPLATE (t)
1084 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1085 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1092 /* Returns nonzero iff DECL is a specialization of friend declaration
1093 FRIEND_DECL according to [temp.friend]. */
1096 is_specialization_of_friend (tree decl, tree friend_decl)
1098 bool need_template = true;
1101 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1102 || TREE_CODE (decl) == TYPE_DECL);
1104 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1105 of a template class, we want to check if DECL is a specialization
1107 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1108 && DECL_TEMPLATE_INFO (friend_decl)
1109 && !DECL_USE_TEMPLATE (friend_decl))
1111 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1112 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1113 need_template = false;
1115 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1116 && !PRIMARY_TEMPLATE_P (friend_decl))
1117 need_template = false;
1119 /* There is nothing to do if this is not a template friend. */
1120 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1123 if (is_specialization_of (decl, friend_decl))
1127 A member of a class template may be declared to be a friend of a
1128 non-template class. In this case, the corresponding member of
1129 every specialization of the class template is a friend of the
1130 class granting friendship.
1132 For example, given a template friend declaration
1134 template <class T> friend void A<T>::f();
1136 the member function below is considered a friend
1138 template <> struct A<int> {
1142 For this type of template friend, TEMPLATE_DEPTH below will be
1143 nonzero. To determine if DECL is a friend of FRIEND, we first
1144 check if the enclosing class is a specialization of another. */
1146 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1148 && DECL_CLASS_SCOPE_P (decl)
1149 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1150 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1152 /* Next, we check the members themselves. In order to handle
1153 a few tricky cases, such as when FRIEND_DECL's are
1155 template <class T> friend void A<T>::g(T t);
1156 template <class T> template <T t> friend void A<T>::h();
1160 void A<int>::g(int);
1161 template <int> void A<int>::h();
1163 we need to figure out ARGS, the template arguments from
1164 the context of DECL. This is required for template substitution
1165 of `T' in the function parameter of `g' and template parameter
1166 of `h' in the above examples. Here ARGS corresponds to `int'. */
1168 tree context = DECL_CONTEXT (decl);
1169 tree args = NULL_TREE;
1170 int current_depth = 0;
1172 while (current_depth < template_depth)
1174 if (CLASSTYPE_TEMPLATE_INFO (context))
1176 if (current_depth == 0)
1177 args = TYPE_TI_ARGS (context);
1179 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1182 context = TYPE_CONTEXT (context);
1185 if (TREE_CODE (decl) == FUNCTION_DECL)
1190 tree friend_args_type;
1191 tree decl_args_type;
1193 /* Make sure that both DECL and FRIEND_DECL are templates or
1195 is_template = DECL_TEMPLATE_INFO (decl)
1196 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1197 if (need_template ^ is_template)
1199 else if (is_template)
1201 /* If both are templates, check template parameter list. */
1203 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1205 if (!comp_template_parms
1206 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1210 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1213 decl_type = TREE_TYPE (decl);
1215 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1216 tf_none, NULL_TREE);
1217 if (friend_type == error_mark_node)
1220 /* Check if return types match. */
1221 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1224 /* Check if function parameter types match, ignoring the
1225 `this' parameter. */
1226 friend_args_type = TYPE_ARG_TYPES (friend_type);
1227 decl_args_type = TYPE_ARG_TYPES (decl_type);
1228 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1229 friend_args_type = TREE_CHAIN (friend_args_type);
1230 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1231 decl_args_type = TREE_CHAIN (decl_args_type);
1233 return compparms (decl_args_type, friend_args_type);
1237 /* DECL is a TYPE_DECL */
1239 tree decl_type = TREE_TYPE (decl);
1241 /* Make sure that both DECL and FRIEND_DECL are templates or
1244 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1245 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1247 if (need_template ^ is_template)
1249 else if (is_template)
1252 /* If both are templates, check the name of the two
1253 TEMPLATE_DECL's first because is_friend didn't. */
1254 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1255 != DECL_NAME (friend_decl))
1258 /* Now check template parameter list. */
1260 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1262 return comp_template_parms
1263 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1267 return (DECL_NAME (decl)
1268 == DECL_NAME (friend_decl));
1274 /* Register the specialization SPEC as a specialization of TMPL with
1275 the indicated ARGS. IS_FRIEND indicates whether the specialization
1276 is actually just a friend declaration. Returns SPEC, or an
1277 equivalent prior declaration, if available. */
1280 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1284 spec_entry **slot = NULL;
1287 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1289 if (TREE_CODE (spec) == FUNCTION_DECL
1290 && uses_template_parms (DECL_TI_ARGS (spec)))
1291 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1292 register it; we want the corresponding TEMPLATE_DECL instead.
1293 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1294 the more obvious `uses_template_parms (spec)' to avoid problems
1295 with default function arguments. In particular, given
1296 something like this:
1298 template <class T> void f(T t1, T t = T())
1300 the default argument expression is not substituted for in an
1301 instantiation unless and until it is actually needed. */
1304 if (optimize_specialization_lookup_p (tmpl))
1305 /* We don't put these specializations in the hash table, but we might
1306 want to give an error about a mismatch. */
1307 fn = retrieve_specialization (tmpl, args, 0);
1315 hash = hash_specialization (&elt);
1317 slot = (spec_entry **)
1318 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1325 /* We can sometimes try to re-register a specialization that we've
1326 already got. In particular, regenerate_decl_from_template calls
1327 duplicate_decls which will update the specialization list. But,
1328 we'll still get called again here anyhow. It's more convenient
1329 to simply allow this than to try to prevent it. */
1332 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1334 if (DECL_TEMPLATE_INSTANTIATION (fn))
1336 if (DECL_ODR_USED (fn)
1337 || DECL_EXPLICIT_INSTANTIATION (fn))
1339 error ("specialization of %qD after instantiation",
1341 return error_mark_node;
1346 /* This situation should occur only if the first
1347 specialization is an implicit instantiation, the
1348 second is an explicit specialization, and the
1349 implicit instantiation has not yet been used. That
1350 situation can occur if we have implicitly
1351 instantiated a member function and then specialized
1354 We can also wind up here if a friend declaration that
1355 looked like an instantiation turns out to be a
1358 template <class T> void foo(T);
1359 class S { friend void foo<>(int) };
1360 template <> void foo(int);
1362 We transform the existing DECL in place so that any
1363 pointers to it become pointers to the updated
1366 If there was a definition for the template, but not
1367 for the specialization, we want this to look as if
1368 there were no definition, and vice versa. */
1369 DECL_INITIAL (fn) = NULL_TREE;
1370 duplicate_decls (spec, fn, is_friend);
1371 /* The call to duplicate_decls will have applied
1374 An explicit specialization of a function template
1375 is inline only if it is explicitly declared to be,
1376 and independently of whether its function template
1379 to the primary function; now copy the inline bits to
1380 the various clones. */
1381 FOR_EACH_CLONE (clone, fn)
1383 DECL_DECLARED_INLINE_P (clone)
1384 = DECL_DECLARED_INLINE_P (fn);
1385 DECL_SOURCE_LOCATION (clone)
1386 = DECL_SOURCE_LOCATION (fn);
1388 check_specialization_namespace (fn);
1393 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1395 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1396 /* Dup decl failed, but this is a new definition. Set the
1397 line number so any errors match this new
1399 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1405 return duplicate_decls (spec, fn, is_friend);
1407 /* A specialization must be declared in the same namespace as the
1408 template it is specializing. */
1409 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1410 && !check_specialization_namespace (tmpl))
1411 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1413 if (!optimize_specialization_lookup_p (tmpl))
1415 gcc_assert (tmpl && args && spec);
1416 *slot = ggc_alloc_spec_entry ();
1418 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1419 && PRIMARY_TEMPLATE_P (tmpl)
1420 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1421 /* TMPL is a forward declaration of a template function; keep a list
1422 of all specializations in case we need to reassign them to a friend
1423 template later in tsubst_friend_function. */
1424 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1425 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1431 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1432 TMPL and ARGS members, ignores SPEC. */
1435 eq_specializations (const void *p1, const void *p2)
1437 const spec_entry *e1 = (const spec_entry *)p1;
1438 const spec_entry *e2 = (const spec_entry *)p2;
1440 return (e1->tmpl == e2->tmpl
1441 && comp_template_args (e1->args, e2->args));
1444 /* Returns a hash for a template TMPL and template arguments ARGS. */
1447 hash_tmpl_and_args (tree tmpl, tree args)
1449 hashval_t val = DECL_UID (tmpl);
1450 return iterative_hash_template_arg (args, val);
1453 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1457 hash_specialization (const void *p)
1459 const spec_entry *e = (const spec_entry *)p;
1460 return hash_tmpl_and_args (e->tmpl, e->args);
1463 /* Recursively calculate a hash value for a template argument ARG, for use
1464 in the hash tables of template specializations. */
1467 iterative_hash_template_arg (tree arg, hashval_t val)
1469 unsigned HOST_WIDE_INT i;
1470 enum tree_code code;
1473 if (arg == NULL_TREE)
1474 return iterative_hash_object (arg, val);
1479 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1480 /* We can get one of these when re-hashing a previous entry in the middle
1481 of substituting into a pack expansion. Just look through it. */
1482 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1484 code = TREE_CODE (arg);
1485 tclass = TREE_CODE_CLASS (code);
1487 val = iterative_hash_object (code, val);
1494 case IDENTIFIER_NODE:
1495 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1499 int i, len = TREE_VEC_LENGTH (arg);
1500 for (i = 0; i < len; ++i)
1501 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1505 case TYPE_PACK_EXPANSION:
1506 case EXPR_PACK_EXPANSION:
1507 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1509 case TYPE_ARGUMENT_PACK:
1510 case NONTYPE_ARGUMENT_PACK:
1511 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1514 for (; arg; arg = TREE_CHAIN (arg))
1515 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1519 for (; arg; arg = OVL_CHAIN (arg))
1520 val = iterative_hash_template_arg (OVL_FUNCTION (arg), val);
1526 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1528 val = iterative_hash_template_arg (field, val);
1529 val = iterative_hash_template_arg (value, val);
1535 if (!DECL_ARTIFICIAL (arg))
1537 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1538 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1540 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1543 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1546 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1547 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1549 case TEMPLATE_PARM_INDEX:
1550 val = iterative_hash_template_arg
1551 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1552 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1553 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1556 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1557 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1558 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1561 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1563 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1567 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1568 code = TREE_CODE (TREE_OPERAND (arg, 1));
1569 val = iterative_hash_object (code, val);
1570 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1573 /* layout_type sets structural equality for arrays of
1574 incomplete type, so we can't rely on the canonical type
1576 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1577 return iterative_hash_template_arg (TYPE_DOMAIN (arg), val);
1580 /* A lambda can't appear in a template arg, but don't crash on
1582 gcc_assert (seen_error ());
1586 case STATIC_CAST_EXPR:
1587 case REINTERPRET_CAST_EXPR:
1588 case CONST_CAST_EXPR:
1589 case DYNAMIC_CAST_EXPR:
1591 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1592 /* Now hash operands as usual. */
1602 if (TYPE_CANONICAL (arg))
1603 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1605 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1606 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1607 /* Otherwise just compare the types during lookup. */
1610 case tcc_declaration:
1612 return iterative_hash_expr (arg, val);
1615 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1617 unsigned n = TREE_OPERAND_LENGTH (arg);
1618 for (i = 0; i < n; ++i)
1619 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1627 /* Unregister the specialization SPEC as a specialization of TMPL.
1628 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1629 if the SPEC was listed as a specialization of TMPL.
1631 Note that SPEC has been ggc_freed, so we can't look inside it. */
1634 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1639 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1640 elt.args = TI_ARGS (tinfo);
1641 elt.spec = NULL_TREE;
1643 slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
1646 gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
1647 gcc_assert (new_spec != NULL_TREE);
1648 (*slot)->spec = new_spec;
1655 /* Compare an entry in the local specializations hash table P1 (which
1656 is really a pointer to a TREE_LIST) with P2 (which is really a
1660 eq_local_specializations (const void *p1, const void *p2)
1662 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1665 /* Hash P1, an entry in the local specializations table. */
1668 hash_local_specialization (const void* p1)
1670 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1673 /* Like register_specialization, but for local declarations. We are
1674 registering SPEC, an instantiation of TMPL. */
1677 register_local_specialization (tree spec, tree tmpl)
1681 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1682 htab_hash_pointer (tmpl), INSERT);
1683 *slot = build_tree_list (spec, tmpl);
1686 /* TYPE is a class type. Returns true if TYPE is an explicitly
1687 specialized class. */
1690 explicit_class_specialization_p (tree type)
1692 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1694 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1697 /* Print the list of functions at FNS, going through all the overloads
1698 for each element of the list. Alternatively, FNS can not be a
1699 TREE_LIST, in which case it will be printed together with all the
1702 MORE and *STR should respectively be FALSE and NULL when the function
1703 is called from the outside. They are used internally on recursive
1704 calls. print_candidates manages the two parameters and leaves NULL
1705 in *STR when it ends. */
1708 print_candidates_1 (tree fns, bool more, const char **str)
1711 char *spaces = NULL;
1713 for (fn = fns; fn; fn = OVL_NEXT (fn))
1714 if (TREE_CODE (fn) == TREE_LIST)
1716 gcc_assert (!OVL_NEXT (fn) && !is_overloaded_fn (fn));
1717 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1718 print_candidates_1 (TREE_VALUE (fn2),
1719 TREE_CHAIN (fn2) || more, str);
1725 /* Pick the prefix string. */
1726 if (!more && !OVL_NEXT (fns))
1728 error ("candidate is: %+#D", OVL_CURRENT (fn));
1732 *str = _("candidates are:");
1733 spaces = get_spaces (*str);
1735 error ("%s %+#D", *str, OVL_CURRENT (fn));
1736 *str = spaces ? spaces : *str;
1746 /* Print the list of candidate FNS in an error message. */
1749 print_candidates (tree fns)
1751 const char *str = NULL;
1752 print_candidates_1 (fns, false, &str);
1753 gcc_assert (str == NULL);
1756 /* Returns the template (one of the functions given by TEMPLATE_ID)
1757 which can be specialized to match the indicated DECL with the
1758 explicit template args given in TEMPLATE_ID. The DECL may be
1759 NULL_TREE if none is available. In that case, the functions in
1760 TEMPLATE_ID are non-members.
1762 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1763 specialization of a member template.
1765 The TEMPLATE_COUNT is the number of references to qualifying
1766 template classes that appeared in the name of the function. See
1767 check_explicit_specialization for a more accurate description.
1769 TSK indicates what kind of template declaration (if any) is being
1770 declared. TSK_TEMPLATE indicates that the declaration given by
1771 DECL, though a FUNCTION_DECL, has template parameters, and is
1772 therefore a template function.
1774 The template args (those explicitly specified and those deduced)
1775 are output in a newly created vector *TARGS_OUT.
1777 If it is impossible to determine the result, an error message is
1778 issued. The error_mark_node is returned to indicate failure. */
1781 determine_specialization (tree template_id,
1784 int need_member_template,
1790 tree explicit_targs;
1791 tree candidates = NULL_TREE;
1792 /* A TREE_LIST of templates of which DECL may be a specialization.
1793 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1794 corresponding TREE_PURPOSE is the set of template arguments that,
1795 when used to instantiate the template, would produce a function
1796 with the signature of DECL. */
1797 tree templates = NULL_TREE;
1799 struct cp_binding_level *b;
1801 *targs_out = NULL_TREE;
1803 if (template_id == error_mark_node || decl == error_mark_node)
1804 return error_mark_node;
1806 fns = TREE_OPERAND (template_id, 0);
1807 explicit_targs = TREE_OPERAND (template_id, 1);
1809 if (fns == error_mark_node)
1810 return error_mark_node;
1812 /* Check for baselinks. */
1813 if (BASELINK_P (fns))
1814 fns = BASELINK_FUNCTIONS (fns);
1816 if (!is_overloaded_fn (fns))
1818 error ("%qD is not a function template", fns);
1819 return error_mark_node;
1822 /* Count the number of template headers specified for this
1825 for (b = current_binding_level;
1826 b->kind == sk_template_parms;
1830 for (; fns; fns = OVL_NEXT (fns))
1832 tree fn = OVL_CURRENT (fns);
1834 if (TREE_CODE (fn) == TEMPLATE_DECL)
1836 tree decl_arg_types;
1839 /* In case of explicit specialization, we need to check if
1840 the number of template headers appearing in the specialization
1841 is correct. This is usually done in check_explicit_specialization,
1842 but the check done there cannot be exhaustive when specializing
1843 member functions. Consider the following code:
1845 template <> void A<int>::f(int);
1846 template <> template <> void A<int>::f(int);
1848 Assuming that A<int> is not itself an explicit specialization
1849 already, the first line specializes "f" which is a non-template
1850 member function, whilst the second line specializes "f" which
1851 is a template member function. So both lines are syntactically
1852 correct, and check_explicit_specialization does not reject
1855 Here, we can do better, as we are matching the specialization
1856 against the declarations. We count the number of template
1857 headers, and we check if they match TEMPLATE_COUNT + 1
1858 (TEMPLATE_COUNT is the number of qualifying template classes,
1859 plus there must be another header for the member template
1862 Notice that if header_count is zero, this is not a
1863 specialization but rather a template instantiation, so there
1864 is no check we can perform here. */
1865 if (header_count && header_count != template_count + 1)
1868 /* Check that the number of template arguments at the
1869 innermost level for DECL is the same as for FN. */
1870 if (current_binding_level->kind == sk_template_parms
1871 && !current_binding_level->explicit_spec_p
1872 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1873 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1874 (current_template_parms))))
1877 /* DECL might be a specialization of FN. */
1878 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1879 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1881 /* For a non-static member function, we need to make sure
1882 that the const qualification is the same. Since
1883 get_bindings does not try to merge the "this" parameter,
1884 we must do the comparison explicitly. */
1885 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1886 && !same_type_p (TREE_VALUE (fn_arg_types),
1887 TREE_VALUE (decl_arg_types)))
1890 /* Skip the "this" parameter and, for constructors of
1891 classes with virtual bases, the VTT parameter. A
1892 full specialization of a constructor will have a VTT
1893 parameter, but a template never will. */
1895 = skip_artificial_parms_for (decl, decl_arg_types);
1897 = skip_artificial_parms_for (fn, fn_arg_types);
1899 /* Check that the number of function parameters matches.
1901 template <class T> void f(int i = 0);
1902 template <> void f<int>();
1903 The specialization f<int> is invalid but is not caught
1904 by get_bindings below. */
1905 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1908 /* Function templates cannot be specializations; there are
1909 no partial specializations of functions. Therefore, if
1910 the type of DECL does not match FN, there is no
1912 if (tsk == tsk_template)
1914 if (compparms (fn_arg_types, decl_arg_types))
1915 candidates = tree_cons (NULL_TREE, fn, candidates);
1919 /* See whether this function might be a specialization of this
1921 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1924 /* We cannot deduce template arguments that when used to
1925 specialize TMPL will produce DECL. */
1928 /* Save this template, and the arguments deduced. */
1929 templates = tree_cons (targs, fn, templates);
1931 else if (need_member_template)
1932 /* FN is an ordinary member function, and we need a
1933 specialization of a member template. */
1935 else if (TREE_CODE (fn) != FUNCTION_DECL)
1936 /* We can get IDENTIFIER_NODEs here in certain erroneous
1939 else if (!DECL_FUNCTION_MEMBER_P (fn))
1940 /* This is just an ordinary non-member function. Nothing can
1941 be a specialization of that. */
1943 else if (DECL_ARTIFICIAL (fn))
1944 /* Cannot specialize functions that are created implicitly. */
1948 tree decl_arg_types;
1950 /* This is an ordinary member function. However, since
1951 we're here, we can assume it's enclosing class is a
1952 template class. For example,
1954 template <typename T> struct S { void f(); };
1955 template <> void S<int>::f() {}
1957 Here, S<int>::f is a non-template, but S<int> is a
1958 template class. If FN has the same type as DECL, we
1959 might be in business. */
1961 if (!DECL_TEMPLATE_INFO (fn))
1962 /* Its enclosing class is an explicit specialization
1963 of a template class. This is not a candidate. */
1966 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1967 TREE_TYPE (TREE_TYPE (fn))))
1968 /* The return types differ. */
1971 /* Adjust the type of DECL in case FN is a static member. */
1972 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1973 if (DECL_STATIC_FUNCTION_P (fn)
1974 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1975 decl_arg_types = TREE_CHAIN (decl_arg_types);
1977 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1980 candidates = tree_cons (NULL_TREE, fn, candidates);
1984 if (templates && TREE_CHAIN (templates))
1990 It is possible for a specialization with a given function
1991 signature to be instantiated from more than one function
1992 template. In such cases, explicit specification of the
1993 template arguments must be used to uniquely identify the
1994 function template specialization being specialized.
1996 Note that here, there's no suggestion that we're supposed to
1997 determine which of the candidate templates is most
1998 specialized. However, we, also have:
2002 Partial ordering of overloaded function template
2003 declarations is used in the following contexts to select
2004 the function template to which a function template
2005 specialization refers:
2007 -- when an explicit specialization refers to a function
2010 So, we do use the partial ordering rules, at least for now.
2011 This extension can only serve to make invalid programs valid,
2012 so it's safe. And, there is strong anecdotal evidence that
2013 the committee intended the partial ordering rules to apply;
2014 the EDG front end has that behavior, and John Spicer claims
2015 that the committee simply forgot to delete the wording in
2016 [temp.expl.spec]. */
2017 tree tmpl = most_specialized_instantiation (templates);
2018 if (tmpl != error_mark_node)
2021 TREE_CHAIN (templates) = NULL_TREE;
2025 if (templates == NULL_TREE && candidates == NULL_TREE)
2027 error ("template-id %qD for %q+D does not match any template "
2028 "declaration", template_id, decl);
2029 if (header_count && header_count != template_count + 1)
2030 inform (input_location, "saw %d %<template<>%>, need %d for "
2031 "specializing a member function template",
2032 header_count, template_count + 1);
2033 return error_mark_node;
2035 else if ((templates && TREE_CHAIN (templates))
2036 || (candidates && TREE_CHAIN (candidates))
2037 || (templates && candidates))
2039 error ("ambiguous template specialization %qD for %q+D",
2041 candidates = chainon (candidates, templates);
2042 print_candidates (candidates);
2043 return error_mark_node;
2046 /* We have one, and exactly one, match. */
2049 tree fn = TREE_VALUE (candidates);
2050 *targs_out = copy_node (DECL_TI_ARGS (fn));
2051 /* DECL is a re-declaration or partial instantiation of a template
2053 if (TREE_CODE (fn) == TEMPLATE_DECL)
2055 /* It was a specialization of an ordinary member function in a
2057 return DECL_TI_TEMPLATE (fn);
2060 /* It was a specialization of a template. */
2061 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2062 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2064 *targs_out = copy_node (targs);
2065 SET_TMPL_ARGS_LEVEL (*targs_out,
2066 TMPL_ARGS_DEPTH (*targs_out),
2067 TREE_PURPOSE (templates));
2070 *targs_out = TREE_PURPOSE (templates);
2071 return TREE_VALUE (templates);
2074 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2075 but with the default argument values filled in from those in the
2079 copy_default_args_to_explicit_spec_1 (tree spec_types,
2082 tree new_spec_types;
2087 if (spec_types == void_list_node)
2088 return void_list_node;
2090 /* Substitute into the rest of the list. */
2092 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2093 TREE_CHAIN (tmpl_types));
2095 /* Add the default argument for this parameter. */
2096 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2097 TREE_VALUE (spec_types),
2101 /* DECL is an explicit specialization. Replicate default arguments
2102 from the template it specializes. (That way, code like:
2104 template <class T> void f(T = 3);
2105 template <> void f(double);
2108 works, as required.) An alternative approach would be to look up
2109 the correct default arguments at the call-site, but this approach
2110 is consistent with how implicit instantiations are handled. */
2113 copy_default_args_to_explicit_spec (tree decl)
2118 tree new_spec_types;
2122 tree object_type = NULL_TREE;
2123 tree in_charge = NULL_TREE;
2124 tree vtt = NULL_TREE;
2126 /* See if there's anything we need to do. */
2127 tmpl = DECL_TI_TEMPLATE (decl);
2128 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2129 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2130 if (TREE_PURPOSE (t))
2135 old_type = TREE_TYPE (decl);
2136 spec_types = TYPE_ARG_TYPES (old_type);
2138 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2140 /* Remove the this pointer, but remember the object's type for
2142 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2143 spec_types = TREE_CHAIN (spec_types);
2144 tmpl_types = TREE_CHAIN (tmpl_types);
2146 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2148 /* DECL may contain more parameters than TMPL due to the extra
2149 in-charge parameter in constructors and destructors. */
2150 in_charge = spec_types;
2151 spec_types = TREE_CHAIN (spec_types);
2153 if (DECL_HAS_VTT_PARM_P (decl))
2156 spec_types = TREE_CHAIN (spec_types);
2160 /* Compute the merged default arguments. */
2162 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2164 /* Compute the new FUNCTION_TYPE. */
2168 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2173 /* Put the in-charge parameter back. */
2174 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2175 TREE_VALUE (in_charge),
2178 new_type = build_method_type_directly (object_type,
2179 TREE_TYPE (old_type),
2183 new_type = build_function_type (TREE_TYPE (old_type),
2185 new_type = cp_build_type_attribute_variant (new_type,
2186 TYPE_ATTRIBUTES (old_type));
2187 new_type = build_exception_variant (new_type,
2188 TYPE_RAISES_EXCEPTIONS (old_type));
2189 TREE_TYPE (decl) = new_type;
2192 /* Check to see if the function just declared, as indicated in
2193 DECLARATOR, and in DECL, is a specialization of a function
2194 template. We may also discover that the declaration is an explicit
2195 instantiation at this point.
2197 Returns DECL, or an equivalent declaration that should be used
2198 instead if all goes well. Issues an error message if something is
2199 amiss. Returns error_mark_node if the error is not easily
2202 FLAGS is a bitmask consisting of the following flags:
2204 2: The function has a definition.
2205 4: The function is a friend.
2207 The TEMPLATE_COUNT is the number of references to qualifying
2208 template classes that appeared in the name of the function. For
2211 template <class T> struct S { void f(); };
2214 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2215 classes are not counted in the TEMPLATE_COUNT, so that in
2217 template <class T> struct S {};
2218 template <> struct S<int> { void f(); }
2219 template <> void S<int>::f();
2221 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2222 invalid; there should be no template <>.)
2224 If the function is a specialization, it is marked as such via
2225 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2226 is set up correctly, and it is added to the list of specializations
2227 for that template. */
2230 check_explicit_specialization (tree declarator,
2235 int have_def = flags & 2;
2236 int is_friend = flags & 4;
2237 int specialization = 0;
2238 int explicit_instantiation = 0;
2239 int member_specialization = 0;
2240 tree ctype = DECL_CLASS_CONTEXT (decl);
2241 tree dname = DECL_NAME (decl);
2246 if (!processing_specialization)
2249 tsk = tsk_excessive_parms;
2252 tsk = current_tmpl_spec_kind (template_count);
2257 if (processing_specialization)
2260 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2262 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2265 /* This could be something like:
2267 template <class T> void f(T);
2268 class S { friend void f<>(int); } */
2272 /* This case handles bogus declarations like template <>
2273 template <class T> void f<int>(); */
2275 error ("template-id %qD in declaration of primary template",
2282 case tsk_invalid_member_spec:
2283 /* The error has already been reported in
2284 check_specialization_scope. */
2285 return error_mark_node;
2287 case tsk_invalid_expl_inst:
2288 error ("template parameter list used in explicit instantiation");
2294 error ("definition provided for explicit instantiation");
2296 explicit_instantiation = 1;
2299 case tsk_excessive_parms:
2300 case tsk_insufficient_parms:
2301 if (tsk == tsk_excessive_parms)
2302 error ("too many template parameter lists in declaration of %qD",
2304 else if (template_header_count)
2305 error("too few template parameter lists in declaration of %qD", decl);
2307 error("explicit specialization of %qD must be introduced by "
2308 "%<template <>%>", decl);
2312 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2314 member_specialization = 1;
2320 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2322 /* This case handles bogus declarations like template <>
2323 template <class T> void f<int>(); */
2325 if (uses_template_parms (declarator))
2326 error ("function template partial specialization %qD "
2327 "is not allowed", declarator);
2329 error ("template-id %qD in declaration of primary template",
2334 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2335 /* This is a specialization of a member template, without
2336 specialization the containing class. Something like:
2338 template <class T> struct S {
2339 template <class U> void f (U);
2341 template <> template <class U> void S<int>::f(U) {}
2343 That's a specialization -- but of the entire template. */
2351 if (specialization || member_specialization)
2353 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2354 for (; t; t = TREE_CHAIN (t))
2355 if (TREE_PURPOSE (t))
2357 permerror (input_location,
2358 "default argument specified in explicit specialization");
2363 if (specialization || member_specialization || explicit_instantiation)
2365 tree tmpl = NULL_TREE;
2366 tree targs = NULL_TREE;
2368 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2369 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2373 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2378 /* If there is no class context, the explicit instantiation
2379 must be at namespace scope. */
2380 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2382 /* Find the namespace binding, using the declaration
2384 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2386 if (fns == error_mark_node || !is_overloaded_fn (fns))
2388 error ("%qD is not a template function", dname);
2389 fns = error_mark_node;
2393 tree fn = OVL_CURRENT (fns);
2394 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2395 CP_DECL_CONTEXT (fn)))
2396 error ("%qD is not declared in %qD",
2397 decl, current_namespace);
2401 declarator = lookup_template_function (fns, NULL_TREE);
2404 if (declarator == error_mark_node)
2405 return error_mark_node;
2407 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2409 if (!explicit_instantiation)
2410 /* A specialization in class scope. This is invalid,
2411 but the error will already have been flagged by
2412 check_specialization_scope. */
2413 return error_mark_node;
2416 /* It's not valid to write an explicit instantiation in
2419 class C { template void f(); }
2421 This case is caught by the parser. However, on
2424 template class C { void f(); };
2426 (which is invalid) we can get here. The error will be
2433 else if (ctype != NULL_TREE
2434 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2437 /* Find the list of functions in ctype that have the same
2438 name as the declared function. */
2439 tree name = TREE_OPERAND (declarator, 0);
2440 tree fns = NULL_TREE;
2443 if (constructor_name_p (name, ctype))
2445 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2447 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2448 : !CLASSTYPE_DESTRUCTORS (ctype))
2450 /* From [temp.expl.spec]:
2452 If such an explicit specialization for the member
2453 of a class template names an implicitly-declared
2454 special member function (clause _special_), the
2455 program is ill-formed.
2457 Similar language is found in [temp.explicit]. */
2458 error ("specialization of implicitly-declared special member function");
2459 return error_mark_node;
2462 name = is_constructor ? ctor_identifier : dtor_identifier;
2465 if (!DECL_CONV_FN_P (decl))
2467 idx = lookup_fnfields_1 (ctype, name);
2469 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2473 VEC(tree,gc) *methods;
2476 /* For a type-conversion operator, we cannot do a
2477 name-based lookup. We might be looking for `operator
2478 int' which will be a specialization of `operator T'.
2479 So, we find *all* the conversion operators, and then
2480 select from them. */
2483 methods = CLASSTYPE_METHOD_VEC (ctype);
2485 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2486 VEC_iterate (tree, methods, idx, ovl);
2489 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2490 /* There are no more conversion functions. */
2493 /* Glue all these conversion functions together
2494 with those we already have. */
2495 for (; ovl; ovl = OVL_NEXT (ovl))
2496 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2500 if (fns == NULL_TREE)
2502 error ("no member function %qD declared in %qT", name, ctype);
2503 return error_mark_node;
2506 TREE_OPERAND (declarator, 0) = fns;
2509 /* Figure out what exactly is being specialized at this point.
2510 Note that for an explicit instantiation, even one for a
2511 member function, we cannot tell apriori whether the
2512 instantiation is for a member template, or just a member
2513 function of a template class. Even if a member template is
2514 being instantiated, the member template arguments may be
2515 elided if they can be deduced from the rest of the
2517 tmpl = determine_specialization (declarator, decl,
2519 member_specialization,
2523 if (!tmpl || tmpl == error_mark_node)
2524 /* We couldn't figure out what this declaration was
2526 return error_mark_node;
2529 tree gen_tmpl = most_general_template (tmpl);
2531 if (explicit_instantiation)
2533 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2534 is done by do_decl_instantiation later. */
2536 int arg_depth = TMPL_ARGS_DEPTH (targs);
2537 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2539 if (arg_depth > parm_depth)
2541 /* If TMPL is not the most general template (for
2542 example, if TMPL is a friend template that is
2543 injected into namespace scope), then there will
2544 be too many levels of TARGS. Remove some of them
2549 new_targs = make_tree_vec (parm_depth);
2550 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2551 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2552 = TREE_VEC_ELT (targs, i);
2556 return instantiate_template (tmpl, targs, tf_error);
2559 /* If we thought that the DECL was a member function, but it
2560 turns out to be specializing a static member function,
2561 make DECL a static member function as well. */
2562 if (DECL_STATIC_FUNCTION_P (tmpl)
2563 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2564 revert_static_member_fn (decl);
2566 /* If this is a specialization of a member template of a
2567 template class, we want to return the TEMPLATE_DECL, not
2568 the specialization of it. */
2569 if (tsk == tsk_template)
2571 tree result = DECL_TEMPLATE_RESULT (tmpl);
2572 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2573 DECL_INITIAL (result) = NULL_TREE;
2577 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2578 DECL_SOURCE_LOCATION (result)
2579 = DECL_SOURCE_LOCATION (decl);
2580 /* We want to use the argument list specified in the
2581 definition, not in the original declaration. */
2582 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2583 for (parm = DECL_ARGUMENTS (result); parm;
2584 parm = DECL_CHAIN (parm))
2585 DECL_CONTEXT (parm) = result;
2587 return register_specialization (tmpl, gen_tmpl, targs,
2591 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2592 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2594 /* Inherit default function arguments from the template
2595 DECL is specializing. */
2596 copy_default_args_to_explicit_spec (decl);
2598 /* This specialization has the same protection as the
2599 template it specializes. */
2600 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2601 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2603 /* 7.1.1-1 [dcl.stc]
2605 A storage-class-specifier shall not be specified in an
2606 explicit specialization...
2608 The parser rejects these, so unless action is taken here,
2609 explicit function specializations will always appear with
2612 The action recommended by the C++ CWG in response to C++
2613 defect report 605 is to make the storage class and linkage
2614 of the explicit specialization match the templated function:
2616 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2618 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2620 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2621 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2623 /* This specialization has the same linkage and visibility as
2624 the function template it specializes. */
2625 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2626 if (! TREE_PUBLIC (decl))
2628 DECL_INTERFACE_KNOWN (decl) = 1;
2629 DECL_NOT_REALLY_EXTERN (decl) = 1;
2631 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2632 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2634 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2635 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2639 /* If DECL is a friend declaration, declared using an
2640 unqualified name, the namespace associated with DECL may
2641 have been set incorrectly. For example, in:
2643 template <typename T> void f(T);
2645 struct S { friend void f<int>(int); }
2648 we will have set the DECL_CONTEXT for the friend
2649 declaration to N, rather than to the global namespace. */
2650 if (DECL_NAMESPACE_SCOPE_P (decl))
2651 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2653 if (is_friend && !have_def)
2654 /* This is not really a declaration of a specialization.
2655 It's just the name of an instantiation. But, it's not
2656 a request for an instantiation, either. */
2657 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2658 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2659 /* This is indeed a specialization. In case of constructors
2660 and destructors, we need in-charge and not-in-charge
2661 versions in V3 ABI. */
2662 clone_function_decl (decl, /*update_method_vec_p=*/0);
2664 /* Register this specialization so that we can find it
2666 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2673 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2674 parameters. These are represented in the same format used for
2675 DECL_TEMPLATE_PARMS. */
2678 comp_template_parms (const_tree parms1, const_tree parms2)
2683 if (parms1 == parms2)
2686 for (p1 = parms1, p2 = parms2;
2687 p1 != NULL_TREE && p2 != NULL_TREE;
2688 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2690 tree t1 = TREE_VALUE (p1);
2691 tree t2 = TREE_VALUE (p2);
2694 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2695 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2697 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2700 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2702 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2703 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2705 /* If either of the template parameters are invalid, assume
2706 they match for the sake of error recovery. */
2707 if (parm1 == error_mark_node || parm2 == error_mark_node)
2710 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2713 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2714 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2715 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2717 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2722 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2723 /* One set of parameters has more parameters lists than the
2730 /* Determine whether PARM is a parameter pack. */
2733 template_parameter_pack_p (const_tree parm)
2735 /* Determine if we have a non-type template parameter pack. */
2736 if (TREE_CODE (parm) == PARM_DECL)
2737 return (DECL_TEMPLATE_PARM_P (parm)
2738 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2740 /* If this is a list of template parameters, we could get a
2741 TYPE_DECL or a TEMPLATE_DECL. */
2742 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2743 parm = TREE_TYPE (parm);
2745 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2746 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2747 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2750 /* Determine if T is a function parameter pack. */
2753 function_parameter_pack_p (const_tree t)
2755 if (t && TREE_CODE (t) == PARM_DECL)
2756 return FUNCTION_PARAMETER_PACK_P (t);
2760 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2761 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2764 get_function_template_decl (const_tree primary_func_tmpl_inst)
2766 if (! primary_func_tmpl_inst
2767 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2768 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2771 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2774 /* Return true iff the function parameter PARAM_DECL was expanded
2775 from the function parameter pack PACK. */
2778 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2780 if (DECL_ARTIFICIAL (param_decl)
2781 || !function_parameter_pack_p (pack))
2784 /* The parameter pack and its pack arguments have the same
2786 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2789 /* Determine whether ARGS describes a variadic template args list,
2790 i.e., one that is terminated by a template argument pack. */
2793 template_args_variadic_p (tree args)
2798 if (args == NULL_TREE)
2801 args = INNERMOST_TEMPLATE_ARGS (args);
2802 nargs = TREE_VEC_LENGTH (args);
2807 last_parm = TREE_VEC_ELT (args, nargs - 1);
2809 return ARGUMENT_PACK_P (last_parm);
2812 /* Generate a new name for the parameter pack name NAME (an
2813 IDENTIFIER_NODE) that incorporates its */
2816 make_ith_pack_parameter_name (tree name, int i)
2818 /* Munge the name to include the parameter index. */
2819 #define NUMBUF_LEN 128
2820 char numbuf[NUMBUF_LEN];
2824 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2825 newname_len = IDENTIFIER_LENGTH (name)
2826 + strlen (numbuf) + 2;
2827 newname = (char*)alloca (newname_len);
2828 snprintf (newname, newname_len,
2829 "%s#%i", IDENTIFIER_POINTER (name), i);
2830 return get_identifier (newname);
2833 /* Return true if T is a primary function
2834 or class template instantiation. */
2837 primary_template_instantiation_p (const_tree t)
2842 if (TREE_CODE (t) == FUNCTION_DECL)
2843 return DECL_LANG_SPECIFIC (t)
2844 && DECL_TEMPLATE_INSTANTIATION (t)
2845 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2846 else if (CLASS_TYPE_P (t))
2847 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2848 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2852 /* Return true if PARM is a template template parameter. */
2855 template_template_parameter_p (const_tree parm)
2857 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2860 /* Return the template parameters of T if T is a
2861 primary template instantiation, NULL otherwise. */
2864 get_primary_template_innermost_parameters (const_tree t)
2866 tree parms = NULL, template_info = NULL;
2868 if ((template_info = get_template_info (t))
2869 && primary_template_instantiation_p (t))
2870 parms = INNERMOST_TEMPLATE_PARMS
2871 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2876 /* Return the template parameters of the LEVELth level from the full list
2877 of template parameters PARMS. */
2880 get_template_parms_at_level (tree parms, int level)
2884 || TREE_CODE (parms) != TREE_LIST
2885 || level > TMPL_PARMS_DEPTH (parms))
2888 for (p = parms; p; p = TREE_CHAIN (p))
2889 if (TMPL_PARMS_DEPTH (p) == level)
2895 /* Returns the template arguments of T if T is a template instantiation,
2899 get_template_innermost_arguments (const_tree t)
2901 tree args = NULL, template_info = NULL;
2903 if ((template_info = get_template_info (t))
2904 && TI_ARGS (template_info))
2905 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2910 /* Return the argument pack elements of T if T is a template argument pack,
2914 get_template_argument_pack_elems (const_tree t)
2916 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2917 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2920 return ARGUMENT_PACK_ARGS (t);
2923 /* Structure used to track the progress of find_parameter_packs_r. */
2924 struct find_parameter_pack_data
2926 /* TREE_LIST that will contain all of the parameter packs found by
2928 tree* parameter_packs;
2930 /* Set of AST nodes that have been visited by the traversal. */
2931 struct pointer_set_t *visited;
2934 /* Identifies all of the argument packs that occur in a template
2935 argument and appends them to the TREE_LIST inside DATA, which is a
2936 find_parameter_pack_data structure. This is a subroutine of
2937 make_pack_expansion and uses_parameter_packs. */
2939 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2942 struct find_parameter_pack_data* ppd =
2943 (struct find_parameter_pack_data*)data;
2944 bool parameter_pack_p = false;
2946 /* Identify whether this is a parameter pack or not. */
2947 switch (TREE_CODE (t))
2949 case TEMPLATE_PARM_INDEX:
2950 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2951 parameter_pack_p = true;
2954 case TEMPLATE_TYPE_PARM:
2955 case TEMPLATE_TEMPLATE_PARM:
2956 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2957 parameter_pack_p = true;
2961 if (FUNCTION_PARAMETER_PACK_P (t))
2963 /* We don't want to walk into the type of a PARM_DECL,
2964 because we don't want to see the type parameter pack. */
2966 parameter_pack_p = true;
2971 /* Not a parameter pack. */
2975 if (parameter_pack_p)
2977 /* Add this parameter pack to the list. */
2978 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2982 cp_walk_tree (&TYPE_CONTEXT (t),
2983 &find_parameter_packs_r, ppd, ppd->visited);
2985 /* This switch statement will return immediately if we don't find a
2987 switch (TREE_CODE (t))
2989 case TEMPLATE_PARM_INDEX:
2992 case BOUND_TEMPLATE_TEMPLATE_PARM:
2993 /* Check the template itself. */
2994 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
2995 &find_parameter_packs_r, ppd, ppd->visited);
2996 /* Check the template arguments. */
2997 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3002 case TEMPLATE_TYPE_PARM:
3003 case TEMPLATE_TEMPLATE_PARM:
3010 if (TYPE_PTRMEMFUNC_P (t))
3016 if (TYPE_TEMPLATE_INFO (t))
3017 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
3018 &find_parameter_packs_r, ppd, ppd->visited);
3024 cp_walk_tree (&TREE_TYPE (t),
3025 &find_parameter_packs_r, ppd, ppd->visited);
3029 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3034 case TYPE_PACK_EXPANSION:
3035 case EXPR_PACK_EXPANSION:
3040 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3045 case IDENTIFIER_NODE:
3046 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3058 /* Determines if the expression or type T uses any parameter packs. */
3060 uses_parameter_packs (tree t)
3062 tree parameter_packs = NULL_TREE;
3063 struct find_parameter_pack_data ppd;
3064 ppd.parameter_packs = ¶meter_packs;
3065 ppd.visited = pointer_set_create ();
3066 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3067 pointer_set_destroy (ppd.visited);
3068 return parameter_packs != NULL_TREE;
3071 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3072 representation a base-class initializer into a parameter pack
3073 expansion. If all goes well, the resulting node will be an
3074 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3077 make_pack_expansion (tree arg)
3080 tree parameter_packs = NULL_TREE;
3081 bool for_types = false;
3082 struct find_parameter_pack_data ppd;
3084 if (!arg || arg == error_mark_node)
3087 if (TREE_CODE (arg) == TREE_LIST)
3089 /* The only time we will see a TREE_LIST here is for a base
3090 class initializer. In this case, the TREE_PURPOSE will be a
3091 _TYPE node (representing the base class expansion we're
3092 initializing) and the TREE_VALUE will be a TREE_LIST
3093 containing the initialization arguments.
3095 The resulting expansion looks somewhat different from most
3096 expansions. Rather than returning just one _EXPANSION, we
3097 return a TREE_LIST whose TREE_PURPOSE is a
3098 TYPE_PACK_EXPANSION containing the bases that will be
3099 initialized. The TREE_VALUE will be identical to the
3100 original TREE_VALUE, which is a list of arguments that will
3101 be passed to each base. We do not introduce any new pack
3102 expansion nodes into the TREE_VALUE (although it is possible
3103 that some already exist), because the TREE_PURPOSE and
3104 TREE_VALUE all need to be expanded together with the same
3105 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3106 resulting TREE_PURPOSE will mention the parameter packs in
3107 both the bases and the arguments to the bases. */
3110 tree parameter_packs = NULL_TREE;
3112 /* Determine which parameter packs will be used by the base
3114 ppd.visited = pointer_set_create ();
3115 ppd.parameter_packs = ¶meter_packs;
3116 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3119 if (parameter_packs == NULL_TREE)
3121 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3122 pointer_set_destroy (ppd.visited);
3123 return error_mark_node;
3126 if (TREE_VALUE (arg) != void_type_node)
3128 /* Collect the sets of parameter packs used in each of the
3129 initialization arguments. */
3130 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3132 /* Determine which parameter packs will be expanded in this
3134 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3139 pointer_set_destroy (ppd.visited);
3141 /* Create the pack expansion type for the base type. */
3142 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3143 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3144 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3146 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3147 they will rarely be compared to anything. */
3148 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3150 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3153 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3156 /* Build the PACK_EXPANSION_* node. */
3158 ? cxx_make_type (TYPE_PACK_EXPANSION)
3159 : make_node (EXPR_PACK_EXPANSION);
3160 SET_PACK_EXPANSION_PATTERN (result, arg);
3161 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3163 /* Propagate type and const-expression information. */
3164 TREE_TYPE (result) = TREE_TYPE (arg);
3165 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3168 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3169 they will rarely be compared to anything. */
3170 SET_TYPE_STRUCTURAL_EQUALITY (result);
3172 /* Determine which parameter packs will be expanded. */
3173 ppd.parameter_packs = ¶meter_packs;
3174 ppd.visited = pointer_set_create ();
3175 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3176 pointer_set_destroy (ppd.visited);
3178 /* Make sure we found some parameter packs. */
3179 if (parameter_packs == NULL_TREE)
3182 error ("expansion pattern %<%T%> contains no argument packs", arg);
3184 error ("expansion pattern %<%E%> contains no argument packs", arg);
3185 return error_mark_node;
3187 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3192 /* Checks T for any "bare" parameter packs, which have not yet been
3193 expanded, and issues an error if any are found. This operation can
3194 only be done on full expressions or types (e.g., an expression
3195 statement, "if" condition, etc.), because we could have expressions like:
3197 foo(f(g(h(args)))...)
3199 where "args" is a parameter pack. check_for_bare_parameter_packs
3200 should not be called for the subexpressions args, h(args),
3201 g(h(args)), or f(g(h(args))), because we would produce erroneous
3204 Returns TRUE and emits an error if there were bare parameter packs,
3205 returns FALSE otherwise. */
3207 check_for_bare_parameter_packs (tree t)
3209 tree parameter_packs = NULL_TREE;
3210 struct find_parameter_pack_data ppd;
3212 if (!processing_template_decl || !t || t == error_mark_node)
3215 if (TREE_CODE (t) == TYPE_DECL)
3218 ppd.parameter_packs = ¶meter_packs;
3219 ppd.visited = pointer_set_create ();
3220 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3221 pointer_set_destroy (ppd.visited);
3223 if (parameter_packs)
3225 error ("parameter packs not expanded with %<...%>:");
3226 while (parameter_packs)
3228 tree pack = TREE_VALUE (parameter_packs);
3229 tree name = NULL_TREE;
3231 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3232 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3233 name = TYPE_NAME (pack);
3234 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3235 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3237 name = DECL_NAME (pack);
3240 inform (input_location, " %qD", name);
3242 inform (input_location, " <anonymous>");
3244 parameter_packs = TREE_CHAIN (parameter_packs);
3253 /* Expand any parameter packs that occur in the template arguments in
3256 expand_template_argument_pack (tree args)
3258 tree result_args = NULL_TREE;
3259 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3260 int num_result_args = -1;
3261 int non_default_args_count = -1;
3263 /* First, determine if we need to expand anything, and the number of
3264 slots we'll need. */
3265 for (in_arg = 0; in_arg < nargs; ++in_arg)
3267 tree arg = TREE_VEC_ELT (args, in_arg);
3268 if (arg == NULL_TREE)
3270 if (ARGUMENT_PACK_P (arg))
3272 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3273 if (num_result_args < 0)
3274 num_result_args = in_arg + num_packed;
3276 num_result_args += num_packed;
3280 if (num_result_args >= 0)
3285 /* If no expansion is necessary, we're done. */
3286 if (num_result_args < 0)
3289 /* Expand arguments. */
3290 result_args = make_tree_vec (num_result_args);
3291 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3292 non_default_args_count =
3293 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3294 for (in_arg = 0; in_arg < nargs; ++in_arg)
3296 tree arg = TREE_VEC_ELT (args, in_arg);
3297 if (ARGUMENT_PACK_P (arg))
3299 tree packed = ARGUMENT_PACK_ARGS (arg);
3300 int i, num_packed = TREE_VEC_LENGTH (packed);
3301 for (i = 0; i < num_packed; ++i, ++out_arg)
3302 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3303 if (non_default_args_count > 0)
3304 non_default_args_count += num_packed;
3308 TREE_VEC_ELT (result_args, out_arg) = arg;
3312 if (non_default_args_count >= 0)
3313 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3317 /* Checks if DECL shadows a template parameter.
3319 [temp.local]: A template-parameter shall not be redeclared within its
3320 scope (including nested scopes).
3322 Emits an error and returns TRUE if the DECL shadows a parameter,
3323 returns FALSE otherwise. */
3326 check_template_shadow (tree decl)
3330 /* If we're not in a template, we can't possibly shadow a template
3332 if (!current_template_parms)
3335 /* Figure out what we're shadowing. */
3336 if (TREE_CODE (decl) == OVERLOAD)
3337 decl = OVL_CURRENT (decl);
3338 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3340 /* If there's no previous binding for this name, we're not shadowing
3341 anything, let alone a template parameter. */
3345 /* If we're not shadowing a template parameter, we're done. Note
3346 that OLDDECL might be an OVERLOAD (or perhaps even an
3347 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3349 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3352 /* We check for decl != olddecl to avoid bogus errors for using a
3353 name inside a class. We check TPFI to avoid duplicate errors for
3354 inline member templates. */
3356 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3359 error ("declaration of %q+#D", decl);
3360 error (" shadows template parm %q+#D", olddecl);
3364 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3365 ORIG_LEVEL, DECL, and TYPE. NUM_SIBLINGS is the total number of
3366 template parameters. */
3369 build_template_parm_index (int index,
3376 tree t = make_node (TEMPLATE_PARM_INDEX);
3377 TEMPLATE_PARM_IDX (t) = index;
3378 TEMPLATE_PARM_LEVEL (t) = level;
3379 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3380 TEMPLATE_PARM_NUM_SIBLINGS (t) = num_siblings;
3381 TEMPLATE_PARM_DECL (t) = decl;
3382 TREE_TYPE (t) = type;
3383 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3384 TREE_READONLY (t) = TREE_READONLY (decl);
3389 /* Find the canonical type parameter for the given template type
3390 parameter. Returns the canonical type parameter, which may be TYPE
3391 if no such parameter existed. */
3394 canonical_type_parameter (tree type)
3397 int idx = TEMPLATE_TYPE_IDX (type);
3398 if (!canonical_template_parms)
3399 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3401 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3402 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3404 list = VEC_index (tree, canonical_template_parms, idx);
3405 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3406 list = TREE_CHAIN (list);
3409 return TREE_VALUE (list);
3412 VEC_replace(tree, canonical_template_parms, idx,
3413 tree_cons (NULL_TREE, type,
3414 VEC_index (tree, canonical_template_parms, idx)));
3419 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3420 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3421 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3422 new one is created. */
3425 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3426 tsubst_flags_t complain)
3428 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3429 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3430 != TEMPLATE_PARM_LEVEL (index) - levels)
3431 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3433 tree orig_decl = TEMPLATE_PARM_DECL (index);
3436 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3437 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3438 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3439 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3440 DECL_ARTIFICIAL (decl) = 1;
3441 SET_DECL_TEMPLATE_PARM_P (decl);
3443 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3444 TEMPLATE_PARM_LEVEL (index) - levels,
3445 TEMPLATE_PARM_ORIG_LEVEL (index),
3446 TEMPLATE_PARM_NUM_SIBLINGS (index),
3448 TEMPLATE_PARM_DESCENDANTS (index) = t;
3449 TEMPLATE_PARM_PARAMETER_PACK (t)
3450 = TEMPLATE_PARM_PARAMETER_PACK (index);
3452 /* Template template parameters need this. */
3453 if (TREE_CODE (decl) == TEMPLATE_DECL)
3454 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3455 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3459 return TEMPLATE_PARM_DESCENDANTS (index);
3462 /* Process information from new template parameter PARM and append it
3463 to the LIST being built. This new parameter is a non-type
3464 parameter iff IS_NON_TYPE is true. This new parameter is a
3465 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3466 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3467 parameter list PARM belongs to. This is used used to create a
3468 proper canonical type for the type of PARM that is to be created,
3469 iff PARM is a type. If the size is not known, this parameter shall
3473 process_template_parm (tree list, location_t parm_loc, tree parm,
3474 bool is_non_type, bool is_parameter_pack,
3475 unsigned num_template_parms)
3482 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3483 defval = TREE_PURPOSE (parm);
3487 tree p = tree_last (list);
3489 if (p && TREE_VALUE (p) != error_mark_node)
3492 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3493 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3495 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3505 parm = TREE_VALUE (parm);
3507 SET_DECL_TEMPLATE_PARM_P (parm);
3509 if (TREE_TYPE (parm) == error_mark_node)
3511 err_parm_list = build_tree_list (defval, parm);
3512 TREE_VALUE (err_parm_list) = error_mark_node;
3513 return chainon (list, err_parm_list);
3519 The top-level cv-qualifiers on the template-parameter are
3520 ignored when determining its type. */
3521 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3522 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3524 err_parm_list = build_tree_list (defval, parm);
3525 TREE_VALUE (err_parm_list) = error_mark_node;
3526 return chainon (list, err_parm_list);
3529 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3531 /* This template parameter is not a parameter pack, but it
3532 should be. Complain about "bare" parameter packs. */
3533 check_for_bare_parameter_packs (TREE_TYPE (parm));
3535 /* Recover by calling this a parameter pack. */
3536 is_parameter_pack = true;
3540 /* A template parameter is not modifiable. */
3541 TREE_CONSTANT (parm) = 1;
3542 TREE_READONLY (parm) = 1;
3543 decl = build_decl (parm_loc,
3544 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3545 TREE_CONSTANT (decl) = 1;
3546 TREE_READONLY (decl) = 1;
3547 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3548 = build_template_parm_index (idx, processing_template_decl,
3549 processing_template_decl,
3551 decl, TREE_TYPE (parm));
3553 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3554 = is_parameter_pack;
3559 parm = TREE_VALUE (TREE_VALUE (parm));
3561 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3563 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3564 /* This is for distinguishing between real templates and template
3565 template parameters */
3566 TREE_TYPE (parm) = t;
3567 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3572 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3573 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3574 decl = build_decl (parm_loc,
3575 TYPE_DECL, parm, t);
3578 TYPE_NAME (t) = decl;
3579 TYPE_STUB_DECL (t) = decl;
3581 TEMPLATE_TYPE_PARM_INDEX (t)
3582 = build_template_parm_index (idx, processing_template_decl,
3583 processing_template_decl,
3585 decl, TREE_TYPE (parm));
3586 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3587 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3589 DECL_ARTIFICIAL (decl) = 1;
3590 SET_DECL_TEMPLATE_PARM_P (decl);
3592 parm = build_tree_list (defval, parm);
3593 return chainon (list, parm);
3596 /* The end of a template parameter list has been reached. Process the
3597 tree list into a parameter vector, converting each parameter into a more
3598 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3602 end_template_parm_list (tree parms)
3606 tree saved_parmlist = make_tree_vec (list_length (parms));
3608 current_template_parms
3609 = tree_cons (size_int (processing_template_decl),
3610 saved_parmlist, current_template_parms);
3612 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3614 next = TREE_CHAIN (parm);
3615 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3616 TREE_CHAIN (parm) = NULL_TREE;
3619 --processing_template_parmlist;
3621 return saved_parmlist;
3624 /* Create a new type almost identical to TYPE but which has the
3625 following differences:
3627 1/ T has a new TEMPLATE_PARM_INDEX that carries the new number of
3628 template sibling parameters of T.
3630 2/ T has a new canonical type that matches the new number
3633 3/ From now on, T is going to be what lookups referring to the
3634 name of TYPE will return. No lookup should return TYPE anymore.
3636 NUM_PARMS is the new number of sibling parms TYPE belongs to.
3638 This is a subroutine of fixup_template_parms. */
3641 fixup_template_type_parm_type (tree type, int num_parms)
3643 tree orig_idx = TEMPLATE_TYPE_PARM_INDEX (type), idx;
3645 /* This is the decl which name is inserted into the symbol table for
3646 the template parm type. So whenever we lookup the type name, this
3647 is the DECL we get. */
3650 /* Do not fix up the type twice. */
3651 if (orig_idx && TEMPLATE_PARM_NUM_SIBLINGS (orig_idx) != 0)
3654 t = copy_type (type);
3655 decl = TYPE_NAME (t);
3657 TYPE_MAIN_VARIANT (t) = t;
3658 TYPE_NEXT_VARIANT (t)= NULL_TREE;
3659 TYPE_POINTER_TO (t) = 0;
3660 TYPE_REFERENCE_TO (t) = 0;
3662 idx = build_template_parm_index (TEMPLATE_PARM_IDX (orig_idx),
3663 TEMPLATE_PARM_LEVEL (orig_idx),
3664 TEMPLATE_PARM_ORIG_LEVEL (orig_idx),
3667 TEMPLATE_PARM_DESCENDANTS (idx) = TEMPLATE_PARM_DESCENDANTS (orig_idx);
3668 TEMPLATE_PARM_PARAMETER_PACK (idx) = TEMPLATE_PARM_PARAMETER_PACK (orig_idx);
3669 TEMPLATE_TYPE_PARM_INDEX (t) = idx;
3671 TYPE_STUB_DECL (t) = decl;
3672 TEMPLATE_TYPE_DECL (t) = decl;
3673 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
3674 TREE_TYPE (DECL_TEMPLATE_RESULT (decl)) = t;
3676 /* Update the type associated to the type name stored in the symbol
3677 table. Now, whenever the type name is looked up, the resulting
3678 type is properly fixed up. */
3679 TREE_TYPE (decl) = t;
3681 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3686 /* Create and return a new TEMPLATE_PARM_INDEX that is almost
3687 identical to I, but that is fixed up as to:
3689 1/ carry the number of sibling parms (NUM_PARMS) of the template
3690 parm represented by I.
3692 2/ replace all references to template parm types declared before I
3693 (in the same template parm list as I) by references to template
3694 parm types contained in ARGS. ARGS should contain the list of
3695 template parms that have been fixed up so far, in a form suitable
3696 to be passed to tsubst.
3698 This is a subroutine of fixup_template_parms. */
3701 fixup_template_parm_index (tree i, tree args, int num_parms)
3703 tree index, decl, type;
3706 || TREE_CODE (i) != TEMPLATE_PARM_INDEX
3707 /* Do not fix up the index twice. */
3708 || (TEMPLATE_PARM_NUM_SIBLINGS (i) != 0))
3711 decl = TEMPLATE_PARM_DECL (i);
3712 type = TREE_TYPE (decl);
3714 index = build_template_parm_index (TEMPLATE_PARM_IDX (i),
3715 TEMPLATE_PARM_LEVEL (i),
3716 TEMPLATE_PARM_ORIG_LEVEL (i),
3720 TEMPLATE_PARM_DESCENDANTS (index) = TEMPLATE_PARM_DESCENDANTS (i);
3721 TEMPLATE_PARM_PARAMETER_PACK (index) = TEMPLATE_PARM_PARAMETER_PACK (i);
3723 type = tsubst (type, args, tf_none, NULL_TREE);
3725 TREE_TYPE (decl) = type;
3726 TREE_TYPE (index) = type;
3732 This is a subroutine of fixup_template_parms.
3734 It computes the canonical type of the type of the template
3735 parameter PARM_DESC and update all references to that type so that
3736 they use the newly computed canonical type. No access check is
3737 performed during the fixup. PARM_DESC is a TREE_LIST which
3738 TREE_VALUE is the template parameter and its TREE_PURPOSE is the
3739 default argument of the template parm if any. IDX is the index of
3740 the template parameter, starting at 0. NUM_PARMS is the number of
3741 template parameters in the set PARM_DESC belongs to. ARGLIST is a
3742 TREE_VEC containing the full set of template parameters in a form
3743 suitable to be passed to substs functions as their ARGS
3744 argument. This is what current_template_args returns for a given
3745 template. The innermost vector of args in ARGLIST is the set of
3746 template parms that have been fixed up so far. This function adds
3747 the fixed up parameter into that vector. */
3750 fixup_template_parm (tree parm_desc,
3755 tree parm = TREE_VALUE (parm_desc);
3756 tree fixedup_args = INNERMOST_TEMPLATE_ARGS (arglist);
3758 push_deferring_access_checks (dk_no_check);
3760 if (TREE_CODE (parm) == TYPE_DECL)
3762 /* PARM is a template type parameter. Fix up its type, add
3763 the fixed-up template parm to the vector of fixed-up
3764 template parms so far, and substitute the fixed-up
3765 template parms into the default argument of this
3768 fixup_template_type_parm_type (TREE_TYPE (parm), num_parms);
3769 TREE_TYPE (parm) = t;
3771 TREE_VEC_ELT (fixedup_args, idx) = template_parm_to_arg (parm_desc);
3773 else if (TREE_CODE (parm) == TEMPLATE_DECL)
3775 /* PARM is a template template parameter. This is going to
3777 tree tparms, targs, innermost_args, t;
3780 /* First, fix up the parms of the template template parm
3781 because the parms are involved in defining the new canonical
3782 type of the template template parm. */
3784 /* So we need to substitute the template parm types that have
3785 been fixed up so far into the template parms of this template
3786 template parm. E.g, consider this:
3788 template<class T, template<T u> class TT> class S;
3790 In this case we want to substitute T into the
3791 template parameters of TT.
3793 So let's walk the template parms of PARM here, and
3794 tsubst ARGLIST into into each of the template
3797 /* For this substitution we need to build the full set of
3798 template parameters and use that as arguments for the
3799 tsubsting function. */
3800 tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
3802 /* This will contain the innermost parms of PARM into which
3803 we have substituted so far. */
3804 innermost_args = make_tree_vec (TREE_VEC_LENGTH (tparms));
3805 targs = add_to_template_args (arglist, innermost_args);
3806 for (j = 0; j < TREE_VEC_LENGTH (tparms); ++j)
3810 parameter = TREE_VEC_ELT (tparms, j);
3812 /* INNERMOST_ARGS needs to have at least the same number
3813 of elements as the index PARAMETER, ortherwise
3814 tsubsting into PARAMETER will result in partially
3815 instantiating it, reducing its tempate parm
3816 level. Let's tactically fill INNERMOST_ARGS for that
3818 TREE_VEC_ELT (innermost_args, j) =
3819 template_parm_to_arg (parameter);
3821 fixup_template_parm (parameter, j,
3822 TREE_VEC_LENGTH (tparms),
3826 /* Now fix up the type of the template template parm. */
3828 t = fixup_template_type_parm_type (TREE_TYPE (parm), num_parms);
3829 TREE_TYPE (parm) = t;
3831 TREE_VEC_ELT (fixedup_args, idx) =
3832 template_parm_to_arg (parm_desc);
3834 else if (TREE_CODE (parm) == PARM_DECL)
3836 /* PARM is a non-type template parameter. We need to:
3838 * Fix up its TEMPLATE_PARM_INDEX to make it carry the
3839 proper number of sibling parameters.
3841 * Make lookups of the template parameter return a reference
3842 to the fixed-up index. No lookup should return references
3843 to the former index anymore.
3845 * Substitute the template parms that got fixed up so far
3847 * into the type of PARM. */
3849 tree index = DECL_INITIAL (parm);
3851 /* PUSHED_DECL is the decl added to the symbol table with
3852 the name of the parameter. E,g:
3854 template<class T, T u> //#0
3855 auto my_function(T t) -> decltype(u); //#1
3857 Here, when looking up u at //#1, we get the decl of u
3858 resulting from the declaration in #0. This is what
3859 PUSHED_DECL is. We need to replace the reference to the
3860 old TEMPLATE_PARM_INDEX carried by PUSHED_DECL by the
3861 fixed-up TEMPLATE_PARM_INDEX. */
3862 tree pushed_decl = TEMPLATE_PARM_DECL (index);
3864 /* Let's fix up the TEMPLATE_PARM_INDEX then. Note that we must
3865 fixup the type of PUSHED_DECL as well and luckily
3866 fixup_template_parm_index does it for us too. */
3867 tree fixed_up_index =
3868 fixup_template_parm_index (index, arglist, num_parms);
3870 DECL_INITIAL (pushed_decl) = DECL_INITIAL (parm) = fixed_up_index;
3872 /* Add this fixed up PARM to the template parms we've fixed
3873 up so far and use that to substitute the fixed-up
3874 template parms into the type of PARM. */
3875 TREE_VEC_ELT (fixedup_args, idx) =
3876 template_parm_to_arg (parm_desc);
3877 TREE_TYPE (parm) = tsubst (TREE_TYPE (parm), arglist,
3878 tf_none, NULL_TREE);
3881 TREE_PURPOSE (parm_desc) =
3882 tsubst_template_arg (TREE_PURPOSE (parm_desc),
3883 arglist, tf_none, parm);
3885 pop_deferring_access_checks ();
3888 /* Walk the current template parms and properly compute the canonical
3889 types of the dependent types created during
3890 cp_parser_template_parameter_list. */
3893 fixup_template_parms (void)
3900 parameter_vec = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3901 if (parameter_vec == NULL_TREE)
3904 num_parms = TREE_VEC_LENGTH (parameter_vec);
3906 /* This vector contains the current innermost template parms that
3907 have been fixed up so far. The form of FIXEDUP_ARGS is suitable
3908 to be passed to tsubst* functions as their ARGS argument. */
3909 fixedup_args = make_tree_vec (num_parms);
3911 /* This vector contains the full set of template parms in a form
3912 suitable to be passed to substs functions as their ARGS
3914 arglist = current_template_args ();
3915 arglist = add_outermost_template_args (arglist, fixedup_args);
3917 /* Let's do the proper fixup now. */
3918 for (i = 0; i < num_parms; ++i)
3919 fixup_template_parm (TREE_VEC_ELT (parameter_vec, i),
3920 i, num_parms, arglist);
3923 /* end_template_decl is called after a template declaration is seen. */
3926 end_template_decl (void)
3928 reset_specialization ();
3930 if (! processing_template_decl)
3933 /* This matches the pushlevel in begin_template_parm_list. */
3936 --processing_template_decl;
3937 current_template_parms = TREE_CHAIN (current_template_parms);
3940 /* Takes a TREE_LIST representing a template parameter and convert it
3941 into an argument suitable to be passed to the type substitution
3942 functions. Note that If the TREE_LIST contains an error_mark
3943 node, the returned argument is error_mark_node. */
3946 template_parm_to_arg (tree t)
3950 || TREE_CODE (t) != TREE_LIST)
3953 if (error_operand_p (TREE_VALUE (t)))
3954 return error_mark_node;
3958 if (TREE_CODE (t) == TYPE_DECL
3959 || TREE_CODE (t) == TEMPLATE_DECL)
3963 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3965 /* Turn this argument into a TYPE_ARGUMENT_PACK
3966 with a single element, which expands T. */
3967 tree vec = make_tree_vec (1);
3968 #ifdef ENABLE_CHECKING
3969 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3970 (vec, TREE_VEC_LENGTH (vec));
3972 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3974 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3975 SET_ARGUMENT_PACK_ARGS (t, vec);
3980 t = DECL_INITIAL (t);
3982 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3984 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3985 with a single element, which expands T. */
3986 tree vec = make_tree_vec (1);
3987 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3988 #ifdef ENABLE_CHECKING
3989 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3990 (vec, TREE_VEC_LENGTH (vec));
3992 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3994 t = make_node (NONTYPE_ARGUMENT_PACK);
3995 SET_ARGUMENT_PACK_ARGS (t, vec);
3996 TREE_TYPE (t) = type;
4002 /* Within the declaration of a template, return all levels of template
4003 parameters that apply. The template parameters are represented as
4004 a TREE_VEC, in the form documented in cp-tree.h for template
4008 current_template_args (void)
4011 tree args = NULL_TREE;
4012 int length = TMPL_PARMS_DEPTH (current_template_parms);
4015 /* If there is only one level of template parameters, we do not
4016 create a TREE_VEC of TREE_VECs. Instead, we return a single
4017 TREE_VEC containing the arguments. */
4019 args = make_tree_vec (length);
4021 for (header = current_template_parms; header; header = TREE_CHAIN (header))
4023 tree a = copy_node (TREE_VALUE (header));
4026 TREE_TYPE (a) = NULL_TREE;
4027 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4028 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4030 #ifdef ENABLE_CHECKING
4031 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4035 TREE_VEC_ELT (args, --l) = a;
4040 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
4041 /* This can happen for template parms of a template template
4044 template<template<class T, class U> class TT> struct S;
4046 Consider the level of the parms of TT; T and U both have
4047 level 2; TT has no template parm of level 1. So in this case
4048 the first element of full_template_args is NULL_TREE. If we
4049 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
4050 of 2. This will make tsubst wrongly consider that T and U
4051 have level 1. Instead, let's create a dummy vector as the
4052 first element of full_template_args so that TMPL_ARG_DEPTH
4053 returns the correct depth for args. */
4054 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
4058 /* Update the declared TYPE by doing any lookups which were thought to be
4059 dependent, but are not now that we know the SCOPE of the declarator. */
4062 maybe_update_decl_type (tree orig_type, tree scope)
4064 tree type = orig_type;
4066 if (type == NULL_TREE)
4069 if (TREE_CODE (orig_type) == TYPE_DECL)
4070 type = TREE_TYPE (type);
4072 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4073 && dependent_type_p (type)
4074 /* Don't bother building up the args in this case. */
4075 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4077 /* tsubst in the args corresponding to the template parameters,
4078 including auto if present. Most things will be unchanged, but
4079 make_typename_type and tsubst_qualified_id will resolve
4080 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4081 tree args = current_template_args ();
4082 tree auto_node = type_uses_auto (type);
4086 tree auto_vec = make_tree_vec (1);
4087 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4088 args = add_to_template_args (args, auto_vec);
4090 pushed = push_scope (scope);
4091 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4096 if (type == error_mark_node)
4099 if (TREE_CODE (orig_type) == TYPE_DECL)
4101 if (same_type_p (type, TREE_TYPE (orig_type)))
4104 type = TYPE_NAME (type);
4109 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4110 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4111 a member template. Used by push_template_decl below. */
4114 build_template_decl (tree decl, tree parms, bool member_template_p)
4116 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4117 DECL_TEMPLATE_PARMS (tmpl) = parms;
4118 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4119 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4124 struct template_parm_data
4126 /* The level of the template parameters we are currently
4130 /* The index of the specialization argument we are currently
4134 /* An array whose size is the number of template parameters. The
4135 elements are nonzero if the parameter has been used in any one
4136 of the arguments processed so far. */
4139 /* An array whose size is the number of template arguments. The
4140 elements are nonzero if the argument makes use of template
4141 parameters of this level. */
4142 int* arg_uses_template_parms;
4145 /* Subroutine of push_template_decl used to see if each template
4146 parameter in a partial specialization is used in the explicit
4147 argument list. If T is of the LEVEL given in DATA (which is
4148 treated as a template_parm_data*), then DATA->PARMS is marked
4152 mark_template_parm (tree t, void* data)
4156 struct template_parm_data* tpd = (struct template_parm_data*) data;
4158 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4160 level = TEMPLATE_PARM_LEVEL (t);
4161 idx = TEMPLATE_PARM_IDX (t);
4165 level = TEMPLATE_TYPE_LEVEL (t);
4166 idx = TEMPLATE_TYPE_IDX (t);
4169 if (level == tpd->level)
4171 tpd->parms[idx] = 1;
4172 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4175 /* Return zero so that for_each_template_parm will continue the
4176 traversal of the tree; we want to mark *every* template parm. */
4180 /* Process the partial specialization DECL. */
4183 process_partial_specialization (tree decl)
4185 tree type = TREE_TYPE (decl);
4186 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4187 tree specargs = CLASSTYPE_TI_ARGS (type);
4188 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4189 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4192 int nargs = TREE_VEC_LENGTH (inner_args);
4195 bool did_error_intro = false;
4196 struct template_parm_data tpd;
4197 struct template_parm_data tpd2;
4199 gcc_assert (current_template_parms);
4201 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4202 ntparms = TREE_VEC_LENGTH (inner_parms);
4204 /* We check that each of the template parameters given in the
4205 partial specialization is used in the argument list to the
4206 specialization. For example:
4208 template <class T> struct S;
4209 template <class T> struct S<T*>;
4211 The second declaration is OK because `T*' uses the template
4212 parameter T, whereas
4214 template <class T> struct S<int>;
4216 is no good. Even trickier is:
4227 The S2<T> declaration is actually invalid; it is a
4228 full-specialization. Of course,
4231 struct S2<T (*)(U)>;
4233 or some such would have been OK. */
4234 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4235 tpd.parms = XALLOCAVEC (int, ntparms);
4236 memset (tpd.parms, 0, sizeof (int) * ntparms);
4238 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4239 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4240 for (i = 0; i < nargs; ++i)
4242 tpd.current_arg = i;
4243 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4244 &mark_template_parm,
4247 /*include_nondeduced_p=*/false);
4249 for (i = 0; i < ntparms; ++i)
4250 if (tpd.parms[i] == 0)
4252 /* One of the template parms was not used in the
4254 if (!did_error_intro)
4256 error ("template parameters not used in partial specialization:");
4257 did_error_intro = true;
4260 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4263 if (did_error_intro)
4264 return error_mark_node;
4266 /* [temp.class.spec]
4268 The argument list of the specialization shall not be identical to
4269 the implicit argument list of the primary template. */
4270 if (comp_template_args
4272 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4274 error ("partial specialization %qT does not specialize any template arguments", type);
4276 /* [temp.class.spec]
4278 A partially specialized non-type argument expression shall not
4279 involve template parameters of the partial specialization except
4280 when the argument expression is a simple identifier.
4282 The type of a template parameter corresponding to a specialized
4283 non-type argument shall not be dependent on a parameter of the
4286 Also, we verify that pack expansions only occur at the
4287 end of the argument list. */
4288 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4290 for (i = 0; i < nargs; ++i)
4292 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4293 tree arg = TREE_VEC_ELT (inner_args, i);
4294 tree packed_args = NULL_TREE;
4297 if (ARGUMENT_PACK_P (arg))
4299 /* Extract the arguments from the argument pack. We'll be
4300 iterating over these in the following loop. */
4301 packed_args = ARGUMENT_PACK_ARGS (arg);
4302 len = TREE_VEC_LENGTH (packed_args);
4305 for (j = 0; j < len; j++)
4308 /* Get the Jth argument in the parameter pack. */
4309 arg = TREE_VEC_ELT (packed_args, j);
4311 if (PACK_EXPANSION_P (arg))
4313 /* Pack expansions must come at the end of the
4315 if ((packed_args && j < len - 1)
4316 || (!packed_args && i < nargs - 1))
4318 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4319 error ("parameter pack argument %qE must be at the "
4320 "end of the template argument list", arg);
4322 error ("parameter pack argument %qT must be at the "
4323 "end of the template argument list", arg);
4327 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4328 /* We only care about the pattern. */
4329 arg = PACK_EXPANSION_PATTERN (arg);
4331 if (/* These first two lines are the `non-type' bit. */
4333 && TREE_CODE (arg) != TEMPLATE_DECL
4334 /* This next line is the `argument expression is not just a
4335 simple identifier' condition and also the `specialized
4336 non-type argument' bit. */
4337 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4339 if ((!packed_args && tpd.arg_uses_template_parms[i])
4340 || (packed_args && uses_template_parms (arg)))
4341 error ("template argument %qE involves template parameter(s)",
4345 /* Look at the corresponding template parameter,
4346 marking which template parameters its type depends
4348 tree type = TREE_TYPE (parm);
4352 /* We haven't yet initialized TPD2. Do so now. */
4353 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4354 /* The number of parameters here is the number in the
4355 main template, which, as checked in the assertion
4357 tpd2.parms = XALLOCAVEC (int, nargs);
4359 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4362 /* Mark the template parameters. But this time, we're
4363 looking for the template parameters of the main
4364 template, not in the specialization. */
4365 tpd2.current_arg = i;
4366 tpd2.arg_uses_template_parms[i] = 0;
4367 memset (tpd2.parms, 0, sizeof (int) * nargs);
4368 for_each_template_parm (type,
4369 &mark_template_parm,
4372 /*include_nondeduced_p=*/false);
4374 if (tpd2.arg_uses_template_parms [i])
4376 /* The type depended on some template parameters.
4377 If they are fully specialized in the
4378 specialization, that's OK. */
4381 for (j = 0; j < nargs; ++j)
4382 if (tpd2.parms[j] != 0
4383 && tpd.arg_uses_template_parms [j])
4386 error_n (input_location, count,
4387 "type %qT of template argument %qE depends "
4388 "on a template parameter",
4389 "type %qT of template argument %qE depends "
4390 "on template parameters",
4399 /* We should only get here once. */
4400 gcc_assert (!COMPLETE_TYPE_P (type));
4402 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4403 = tree_cons (specargs, inner_parms,
4404 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4405 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4407 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4408 inst = TREE_CHAIN (inst))
4410 tree inst_type = TREE_VALUE (inst);
4411 if (COMPLETE_TYPE_P (inst_type)
4412 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4414 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4415 if (spec && TREE_TYPE (spec) == type)
4416 permerror (input_location,
4417 "partial specialization of %qT after instantiation "
4418 "of %qT", type, inst_type);
4425 /* Check that a template declaration's use of default arguments and
4426 parameter packs is not invalid. Here, PARMS are the template
4427 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4428 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4432 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4433 declaration (but not a definition); 1 indicates a declaration, 2
4434 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4435 emitted for extraneous default arguments.
4437 Returns TRUE if there were no errors found, FALSE otherwise. */
4440 check_default_tmpl_args (tree decl, tree parms, int is_primary,
4441 int is_partial, int is_friend_decl)
4444 int last_level_to_check;
4446 bool no_errors = true;
4450 A default template-argument shall not be specified in a
4451 function template declaration or a function template definition, nor
4452 in the template-parameter-list of the definition of a member of a
4455 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4456 /* You can't have a function template declaration in a local
4457 scope, nor you can you define a member of a class template in a
4461 if (current_class_type
4462 && !TYPE_BEING_DEFINED (current_class_type)
4463 && DECL_LANG_SPECIFIC (decl)
4464 && DECL_DECLARES_FUNCTION_P (decl)
4465 /* If this is either a friend defined in the scope of the class
4466 or a member function. */
4467 && (DECL_FUNCTION_MEMBER_P (decl)
4468 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4469 : DECL_FRIEND_CONTEXT (decl)
4470 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4472 /* And, if it was a member function, it really was defined in
4473 the scope of the class. */
4474 && (!DECL_FUNCTION_MEMBER_P (decl)
4475 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4476 /* We already checked these parameters when the template was
4477 declared, so there's no need to do it again now. This function
4478 was defined in class scope, but we're processing it's body now
4479 that the class is complete. */
4482 /* Core issue 226 (C++0x only): the following only applies to class
4484 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
4488 If a template-parameter has a default template-argument, all
4489 subsequent template-parameters shall have a default
4490 template-argument supplied. */
4491 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4493 tree inner_parms = TREE_VALUE (parm_level);
4494 int ntparms = TREE_VEC_LENGTH (inner_parms);
4495 int seen_def_arg_p = 0;
4498 for (i = 0; i < ntparms; ++i)
4500 tree parm = TREE_VEC_ELT (inner_parms, i);
4502 if (parm == error_mark_node)
4505 if (TREE_PURPOSE (parm))
4507 else if (seen_def_arg_p
4508 && !template_parameter_pack_p (TREE_VALUE (parm)))
4510 error ("no default argument for %qD", TREE_VALUE (parm));
4511 /* For better subsequent error-recovery, we indicate that
4512 there should have been a default argument. */
4513 TREE_PURPOSE (parm) = error_mark_node;
4519 /* Don't complain about an enclosing partial
4521 && parm_level == parms
4522 && TREE_CODE (decl) == TYPE_DECL
4524 && template_parameter_pack_p (TREE_VALUE (parm)))
4526 /* A primary class template can only have one
4527 parameter pack, at the end of the template
4530 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4531 error ("parameter pack %qE must be at the end of the"
4532 " template parameter list", TREE_VALUE (parm));
4534 error ("parameter pack %qT must be at the end of the"
4535 " template parameter list",
4536 TREE_TYPE (TREE_VALUE (parm)));
4538 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4546 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4550 /* For an ordinary class template, default template arguments are
4551 allowed at the innermost level, e.g.:
4552 template <class T = int>
4554 but, in a partial specialization, they're not allowed even
4555 there, as we have in [temp.class.spec]:
4557 The template parameter list of a specialization shall not
4558 contain default template argument values.
4560 So, for a partial specialization, or for a function template
4561 (in C++98/C++03), we look at all of them. */
4564 /* But, for a primary class template that is not a partial
4565 specialization we look at all template parameters except the
4567 parms = TREE_CHAIN (parms);
4569 /* Figure out what error message to issue. */
4570 if (is_friend_decl == 2)
4571 msg = G_("default template arguments may not be used in function template "
4572 "friend re-declaration");
4573 else if (is_friend_decl)
4574 msg = G_("default template arguments may not be used in function template "
4575 "friend declarations");
4576 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4577 msg = G_("default template arguments may not be used in function templates "
4578 "without -std=c++0x or -std=gnu++0x");
4579 else if (is_partial)
4580 msg = G_("default template arguments may not be used in "
4581 "partial specializations");
4583 msg = G_("default argument for template parameter for class enclosing %qD");
4585 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4586 /* If we're inside a class definition, there's no need to
4587 examine the parameters to the class itself. On the one
4588 hand, they will be checked when the class is defined, and,
4589 on the other, default arguments are valid in things like:
4590 template <class T = double>
4591 struct S { template <class U> void f(U); };
4592 Here the default argument for `S' has no bearing on the
4593 declaration of `f'. */
4594 last_level_to_check = template_class_depth (current_class_type) + 1;
4596 /* Check everything. */
4597 last_level_to_check = 0;
4599 for (parm_level = parms;
4600 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4601 parm_level = TREE_CHAIN (parm_level))
4603 tree inner_parms = TREE_VALUE (parm_level);
4607 ntparms = TREE_VEC_LENGTH (inner_parms);
4608 for (i = 0; i < ntparms; ++i)
4610 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4613 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4618 if (is_friend_decl == 2)
4625 /* Clear out the default argument so that we are not
4627 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4631 /* At this point, if we're still interested in issuing messages,
4632 they must apply to classes surrounding the object declared. */
4634 msg = G_("default argument for template parameter for class "
4641 /* Worker for push_template_decl_real, called via
4642 for_each_template_parm. DATA is really an int, indicating the
4643 level of the parameters we are interested in. If T is a template
4644 parameter of that level, return nonzero. */
4647 template_parm_this_level_p (tree t, void* data)
4649 int this_level = *(int *)data;
4652 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4653 level = TEMPLATE_PARM_LEVEL (t);
4655 level = TEMPLATE_TYPE_LEVEL (t);
4656 return level == this_level;
4659 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4660 parameters given by current_template_args, or reuses a
4661 previously existing one, if appropriate. Returns the DECL, or an
4662 equivalent one, if it is replaced via a call to duplicate_decls.
4664 If IS_FRIEND is true, DECL is a friend declaration. */
4667 push_template_decl_real (tree decl, bool is_friend)
4675 int new_template_p = 0;
4676 /* True if the template is a member template, in the sense of
4678 bool member_template_p = false;
4680 if (decl == error_mark_node || !current_template_parms)
4681 return error_mark_node;
4683 /* See if this is a partial specialization. */
4684 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4685 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4686 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4688 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4692 /* For a friend, we want the context of the friend function, not
4693 the type of which it is a friend. */
4694 ctx = CP_DECL_CONTEXT (decl);
4695 else if (CP_DECL_CONTEXT (decl)
4696 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4697 /* In the case of a virtual function, we want the class in which
4699 ctx = CP_DECL_CONTEXT (decl);
4701 /* Otherwise, if we're currently defining some class, the DECL
4702 is assumed to be a member of the class. */
4703 ctx = current_scope ();
4705 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4708 if (!DECL_CONTEXT (decl))
4709 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4711 /* See if this is a primary template. */
4712 if (is_friend && ctx)
4713 /* A friend template that specifies a class context, i.e.
4714 template <typename T> friend void A<T>::f();
4718 primary = template_parm_scope_p ();
4722 if (DECL_CLASS_SCOPE_P (decl))
4723 member_template_p = true;
4724 if (TREE_CODE (decl) == TYPE_DECL
4725 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4727 error ("template class without a name");
4728 return error_mark_node;
4730 else if (TREE_CODE (decl) == FUNCTION_DECL)
4732 if (DECL_DESTRUCTOR_P (decl))
4736 A destructor shall not be a member template. */
4737 error ("destructor %qD declared as member template", decl);
4738 return error_mark_node;
4740 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4741 && (!prototype_p (TREE_TYPE (decl))
4742 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4743 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4744 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4745 == void_list_node)))
4747 /* [basic.stc.dynamic.allocation]
4749 An allocation function can be a function
4750 template. ... Template allocation functions shall
4751 have two or more parameters. */
4752 error ("invalid template declaration of %qD", decl);
4753 return error_mark_node;
4756 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4757 && CLASS_TYPE_P (TREE_TYPE (decl)))
4761 error ("template declaration of %q#D", decl);
4762 return error_mark_node;
4766 /* Check to see that the rules regarding the use of default
4767 arguments are not being violated. */
4768 check_default_tmpl_args (decl, current_template_parms,
4769 primary, is_partial, /*is_friend_decl=*/0);
4771 /* Ensure that there are no parameter packs in the type of this
4772 declaration that have not been expanded. */
4773 if (TREE_CODE (decl) == FUNCTION_DECL)
4775 /* Check each of the arguments individually to see if there are
4776 any bare parameter packs. */
4777 tree type = TREE_TYPE (decl);
4778 tree arg = DECL_ARGUMENTS (decl);
4779 tree argtype = TYPE_ARG_TYPES (type);
4781 while (arg && argtype)
4783 if (!FUNCTION_PARAMETER_PACK_P (arg)
4784 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4786 /* This is a PARM_DECL that contains unexpanded parameter
4787 packs. We have already complained about this in the
4788 check_for_bare_parameter_packs call, so just replace
4789 these types with ERROR_MARK_NODE. */
4790 TREE_TYPE (arg) = error_mark_node;
4791 TREE_VALUE (argtype) = error_mark_node;
4794 arg = DECL_CHAIN (arg);
4795 argtype = TREE_CHAIN (argtype);
4798 /* Check for bare parameter packs in the return type and the
4799 exception specifiers. */
4800 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4801 /* Errors were already issued, set return type to int
4802 as the frontend doesn't expect error_mark_node as
4804 TREE_TYPE (type) = integer_type_node;
4805 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4806 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4808 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
4810 TREE_TYPE (decl) = error_mark_node;
4811 return error_mark_node;
4815 return process_partial_specialization (decl);
4817 args = current_template_args ();
4820 || TREE_CODE (ctx) == FUNCTION_DECL
4821 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4822 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4824 if (DECL_LANG_SPECIFIC (decl)
4825 && DECL_TEMPLATE_INFO (decl)
4826 && DECL_TI_TEMPLATE (decl))
4827 tmpl = DECL_TI_TEMPLATE (decl);
4828 /* If DECL is a TYPE_DECL for a class-template, then there won't
4829 be DECL_LANG_SPECIFIC. The information equivalent to
4830 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4831 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4832 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4833 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4835 /* Since a template declaration already existed for this
4836 class-type, we must be redeclaring it here. Make sure
4837 that the redeclaration is valid. */
4838 redeclare_class_template (TREE_TYPE (decl),
4839 current_template_parms);
4840 /* We don't need to create a new TEMPLATE_DECL; just use the
4841 one we already had. */
4842 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4846 tmpl = build_template_decl (decl, current_template_parms,
4850 if (DECL_LANG_SPECIFIC (decl)
4851 && DECL_TEMPLATE_SPECIALIZATION (decl))
4853 /* A specialization of a member template of a template
4855 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4856 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4857 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4863 tree a, t, current, parms;
4865 tree tinfo = get_template_info (decl);
4869 error ("template definition of non-template %q#D", decl);
4870 return error_mark_node;
4873 tmpl = TI_TEMPLATE (tinfo);
4875 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4876 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4877 && DECL_TEMPLATE_SPECIALIZATION (decl)
4878 && DECL_MEMBER_TEMPLATE_P (tmpl))
4882 /* The declaration is a specialization of a member
4883 template, declared outside the class. Therefore, the
4884 innermost template arguments will be NULL, so we
4885 replace them with the arguments determined by the
4886 earlier call to check_explicit_specialization. */
4887 args = DECL_TI_ARGS (decl);
4890 = build_template_decl (decl, current_template_parms,
4892 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4893 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4894 DECL_TI_TEMPLATE (decl) = new_tmpl;
4895 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4896 DECL_TEMPLATE_INFO (new_tmpl)
4897 = build_template_info (tmpl, args);
4899 register_specialization (new_tmpl,
4900 most_general_template (tmpl),
4906 /* Make sure the template headers we got make sense. */
4908 parms = DECL_TEMPLATE_PARMS (tmpl);
4909 i = TMPL_PARMS_DEPTH (parms);
4910 if (TMPL_ARGS_DEPTH (args) != i)
4912 error ("expected %d levels of template parms for %q#D, got %d",
4913 i, decl, TMPL_ARGS_DEPTH (args));
4916 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4918 a = TMPL_ARGS_LEVEL (args, i);
4919 t = INNERMOST_TEMPLATE_PARMS (parms);
4921 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4923 if (current == decl)
4924 error ("got %d template parameters for %q#D",
4925 TREE_VEC_LENGTH (a), decl);
4927 error ("got %d template parameters for %q#T",
4928 TREE_VEC_LENGTH (a), current);
4929 error (" but %d required", TREE_VEC_LENGTH (t));
4930 return error_mark_node;
4933 if (current == decl)
4935 else if (current == NULL_TREE)
4936 /* Can happen in erroneous input. */
4939 current = (TYPE_P (current)
4940 ? TYPE_CONTEXT (current)
4941 : DECL_CONTEXT (current));
4944 /* Check that the parms are used in the appropriate qualifying scopes
4945 in the declarator. */
4946 if (!comp_template_args
4948 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4951 template arguments to %qD do not match original template %qD",
4952 decl, DECL_TEMPLATE_RESULT (tmpl));
4953 if (!uses_template_parms (TI_ARGS (tinfo)))
4954 inform (input_location, "use template<> for an explicit specialization");
4955 /* Avoid crash in import_export_decl. */
4956 DECL_INTERFACE_KNOWN (decl) = 1;
4957 return error_mark_node;
4961 DECL_TEMPLATE_RESULT (tmpl) = decl;
4962 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4964 /* Push template declarations for global functions and types. Note
4965 that we do not try to push a global template friend declared in a
4966 template class; such a thing may well depend on the template
4967 parameters of the class. */
4968 if (new_template_p && !ctx
4969 && !(is_friend && template_class_depth (current_class_type) > 0))
4971 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4972 if (tmpl == error_mark_node)
4973 return error_mark_node;
4975 /* Hide template friend classes that haven't been declared yet. */
4976 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4978 DECL_ANTICIPATED (tmpl) = 1;
4979 DECL_FRIEND_P (tmpl) = 1;
4985 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4988 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4989 if (DECL_CONV_FN_P (tmpl))
4991 int depth = TMPL_PARMS_DEPTH (parms);
4993 /* It is a conversion operator. See if the type converted to
4994 depends on innermost template operands. */
4996 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4998 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5001 /* Give template template parms a DECL_CONTEXT of the template
5002 for which they are a parameter. */
5003 parms = INNERMOST_TEMPLATE_PARMS (parms);
5004 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5006 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5007 if (TREE_CODE (parm) == TEMPLATE_DECL)
5008 DECL_CONTEXT (parm) = tmpl;
5012 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5013 back to its most general template. If TMPL is a specialization,
5014 ARGS may only have the innermost set of arguments. Add the missing
5015 argument levels if necessary. */
5016 if (DECL_TEMPLATE_INFO (tmpl))
5017 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5019 info = build_template_info (tmpl, args);
5021 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5022 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5023 else if (DECL_LANG_SPECIFIC (decl))
5024 DECL_TEMPLATE_INFO (decl) = info;
5026 return DECL_TEMPLATE_RESULT (tmpl);
5030 push_template_decl (tree decl)
5032 return push_template_decl_real (decl, false);
5035 /* Called when a class template TYPE is redeclared with the indicated
5036 template PARMS, e.g.:
5038 template <class T> struct S;
5039 template <class T> struct S {}; */
5042 redeclare_class_template (tree type, tree parms)
5048 if (!TYPE_TEMPLATE_INFO (type))
5050 error ("%qT is not a template type", type);
5054 tmpl = TYPE_TI_TEMPLATE (type);
5055 if (!PRIMARY_TEMPLATE_P (tmpl))
5056 /* The type is nested in some template class. Nothing to worry
5057 about here; there are no new template parameters for the nested
5063 error ("template specifiers not specified in declaration of %qD",
5068 parms = INNERMOST_TEMPLATE_PARMS (parms);
5069 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5071 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5073 error_n (input_location, TREE_VEC_LENGTH (parms),
5074 "redeclared with %d template parameter",
5075 "redeclared with %d template parameters",
5076 TREE_VEC_LENGTH (parms));
5077 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5078 "previous declaration %q+D used %d template parameter",
5079 "previous declaration %q+D used %d template parameters",
5080 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5084 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5091 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5092 || TREE_VEC_ELT (parms, i) == error_mark_node)
5095 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5096 if (tmpl_parm == error_mark_node)
5099 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5100 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5101 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5103 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5105 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5106 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5107 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5108 || (TREE_CODE (tmpl_parm) != PARM_DECL
5109 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5110 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5111 || (TREE_CODE (tmpl_parm) == PARM_DECL
5112 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5113 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5115 error ("template parameter %q+#D", tmpl_parm);
5116 error ("redeclared here as %q#D", parm);
5120 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5122 /* We have in [temp.param]:
5124 A template-parameter may not be given default arguments
5125 by two different declarations in the same scope. */
5126 error_at (input_location, "redefinition of default argument for %q#D", parm);
5127 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5128 "original definition appeared here");
5132 if (parm_default != NULL_TREE)
5133 /* Update the previous template parameters (which are the ones
5134 that will really count) with the new default value. */
5135 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5136 else if (tmpl_default != NULL_TREE)
5137 /* Update the new parameters, too; they'll be used as the
5138 parameters for any members. */
5139 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5145 /* Simplify EXPR if it is a non-dependent expression. Returns the
5146 (possibly simplified) expression. */
5149 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5151 if (expr == NULL_TREE)
5154 /* If we're in a template, but EXPR isn't value dependent, simplify
5155 it. We're supposed to treat:
5157 template <typename T> void f(T[1 + 1]);
5158 template <typename T> void f(T[2]);
5160 as two declarations of the same function, for example. */
5161 if (processing_template_decl
5162 && !type_dependent_expression_p (expr)
5163 && potential_constant_expression (expr)
5164 && !value_dependent_expression_p (expr))
5166 HOST_WIDE_INT saved_processing_template_decl;
5168 saved_processing_template_decl = processing_template_decl;
5169 processing_template_decl = 0;
5170 expr = tsubst_copy_and_build (expr,
5173 /*in_decl=*/NULL_TREE,
5174 /*function_p=*/false,
5175 /*integral_constant_expression_p=*/true);
5176 processing_template_decl = saved_processing_template_decl;
5182 fold_non_dependent_expr (tree expr)
5184 return fold_non_dependent_expr_sfinae (expr, tf_error);
5187 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5188 must be a function or a pointer-to-function type, as specified
5189 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5190 and check that the resulting function has external linkage. */
5193 convert_nontype_argument_function (tree type, tree expr)
5198 fn = instantiate_type (type, fns, tf_none);
5199 if (fn == error_mark_node)
5200 return error_mark_node;
5203 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5204 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5205 if (TREE_CODE (fn_no_ptr) == BASELINK)
5206 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5208 /* [temp.arg.nontype]/1
5210 A template-argument for a non-type, non-template template-parameter
5213 -- the address of an object or function with external linkage. */
5214 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
5216 error ("%qE is not a valid template argument for type %qT "
5217 "because function %qD has not external linkage",
5218 expr, type, fn_no_ptr);
5225 /* Subroutine of convert_nontype_argument.
5226 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5227 Emit an error otherwise. */
5230 check_valid_ptrmem_cst_expr (tree type, tree expr,
5231 tsubst_flags_t complain)
5234 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5236 if (complain & tf_error)
5238 error ("%qE is not a valid template argument for type %qT",
5240 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5245 /* Returns TRUE iff the address of OP is value-dependent.
5247 14.6.2.4 [temp.dep.temp]:
5248 A non-integral non-type template-argument is dependent if its type is
5249 dependent or it has either of the following forms
5252 and contains a nested-name-specifier which specifies a class-name that
5253 names a dependent type.
5255 We generalize this to just say that the address of a member of a
5256 dependent class is value-dependent; the above doesn't cover the
5257 address of a static data member named with an unqualified-id. */
5260 has_value_dependent_address (tree op)
5262 /* We could use get_inner_reference here, but there's no need;
5263 this is only relevant for template non-type arguments, which
5264 can only be expressed as &id-expression. */
5267 tree ctx = CP_DECL_CONTEXT (op);
5268 if (TYPE_P (ctx) && dependent_type_p (ctx))
5275 /* Attempt to convert the non-type template parameter EXPR to the
5276 indicated TYPE. If the conversion is successful, return the
5277 converted value. If the conversion is unsuccessful, return
5278 NULL_TREE if we issued an error message, or error_mark_node if we
5279 did not. We issue error messages for out-and-out bad template
5280 parameters, but not simply because the conversion failed, since we
5281 might be just trying to do argument deduction. Both TYPE and EXPR
5282 must be non-dependent.
5284 The conversion follows the special rules described in
5285 [temp.arg.nontype], and it is much more strict than an implicit
5288 This function is called twice for each template argument (see
5289 lookup_template_class for a more accurate description of this
5290 problem). This means that we need to handle expressions which
5291 are not valid in a C++ source, but can be created from the
5292 first call (for instance, casts to perform conversions). These
5293 hacks can go away after we fix the double coercion problem. */
5296 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5300 /* Detect immediately string literals as invalid non-type argument.
5301 This special-case is not needed for correctness (we would easily
5302 catch this later), but only to provide better diagnostic for this
5303 common user mistake. As suggested by DR 100, we do not mention
5304 linkage issues in the diagnostic as this is not the point. */
5305 /* FIXME we're making this OK. */
5306 if (TREE_CODE (expr) == STRING_CST)
5308 if (complain & tf_error)
5309 error ("%qE is not a valid template argument for type %qT "
5310 "because string literals can never be used in this context",
5315 /* Add the ADDR_EXPR now for the benefit of
5316 value_dependent_expression_p. */
5317 if (TYPE_PTROBV_P (type))
5318 expr = decay_conversion (expr);
5320 /* If we are in a template, EXPR may be non-dependent, but still
5321 have a syntactic, rather than semantic, form. For example, EXPR
5322 might be a SCOPE_REF, rather than the VAR_DECL to which the
5323 SCOPE_REF refers. Preserving the qualifying scope is necessary
5324 so that access checking can be performed when the template is
5325 instantiated -- but here we need the resolved form so that we can
5326 convert the argument. */
5327 if (TYPE_REF_OBJ_P (type)
5328 && has_value_dependent_address (expr))
5329 /* If we want the address and it's value-dependent, don't fold. */;
5330 else if (!type_unknown_p (expr))
5331 expr = fold_non_dependent_expr_sfinae (expr, complain);
5332 if (error_operand_p (expr))
5333 return error_mark_node;
5334 expr_type = TREE_TYPE (expr);
5335 if (TREE_CODE (type) == REFERENCE_TYPE)
5336 expr = mark_lvalue_use (expr);
5338 expr = mark_rvalue_use (expr);
5340 /* HACK: Due to double coercion, we can get a
5341 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5342 which is the tree that we built on the first call (see
5343 below when coercing to reference to object or to reference to
5344 function). We just strip everything and get to the arg.
5345 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5347 if (TREE_CODE (expr) == NOP_EXPR)
5349 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5351 /* ??? Maybe we could use convert_from_reference here, but we
5352 would need to relax its constraints because the NOP_EXPR
5353 could actually change the type to something more cv-qualified,
5354 and this is not folded by convert_from_reference. */
5355 tree addr = TREE_OPERAND (expr, 0);
5356 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
5357 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
5358 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
5359 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5360 (TREE_TYPE (expr_type),
5361 TREE_TYPE (TREE_TYPE (addr))));
5363 expr = TREE_OPERAND (addr, 0);
5364 expr_type = TREE_TYPE (expr);
5367 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5368 parameter is a pointer to object, through decay and
5369 qualification conversion. Let's strip everything. */
5370 else if (TYPE_PTROBV_P (type))
5373 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
5374 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
5375 /* Skip the ADDR_EXPR only if it is part of the decay for
5376 an array. Otherwise, it is part of the original argument
5377 in the source code. */
5378 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
5379 expr = TREE_OPERAND (expr, 0);
5380 expr_type = TREE_TYPE (expr);
5384 /* [temp.arg.nontype]/5, bullet 1
5386 For a non-type template-parameter of integral or enumeration type,
5387 integral promotions (_conv.prom_) and integral conversions
5388 (_conv.integral_) are applied. */
5389 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5391 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5392 t = maybe_constant_value (t);
5393 if (t != error_mark_node)
5396 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5397 return error_mark_node;
5399 /* Notice that there are constant expressions like '4 % 0' which
5400 do not fold into integer constants. */
5401 if (TREE_CODE (expr) != INTEGER_CST)
5403 if (complain & tf_error)
5405 int errs = errorcount, warns = warningcount;
5406 expr = cxx_constant_value (expr);
5407 if (errorcount > errs || warningcount > warns)
5408 inform (EXPR_LOC_OR_HERE (expr),
5409 "in template argument for type %qT ", type);
5410 if (expr == error_mark_node)
5412 /* else cxx_constant_value complained but gave us
5413 a real constant, so go ahead. */
5414 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5420 /* [temp.arg.nontype]/5, bullet 2
5422 For a non-type template-parameter of type pointer to object,
5423 qualification conversions (_conv.qual_) and the array-to-pointer
5424 conversion (_conv.array_) are applied. */
5425 else if (TYPE_PTROBV_P (type))
5427 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5429 A template-argument for a non-type, non-template template-parameter
5430 shall be one of: [...]
5432 -- the name of a non-type template-parameter;
5433 -- the address of an object or function with external linkage, [...]
5434 expressed as "& id-expression" where the & is optional if the name
5435 refers to a function or array, or if the corresponding
5436 template-parameter is a reference.
5438 Here, we do not care about functions, as they are invalid anyway
5439 for a parameter of type pointer-to-object. */
5441 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5442 /* Non-type template parameters are OK. */
5444 else if (TREE_CODE (expr) != ADDR_EXPR
5445 && TREE_CODE (expr_type) != ARRAY_TYPE)
5447 if (TREE_CODE (expr) == VAR_DECL)
5449 error ("%qD is not a valid template argument "
5450 "because %qD is a variable, not the address of "
5455 /* Other values, like integer constants, might be valid
5456 non-type arguments of some other type. */
5457 return error_mark_node;
5463 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5464 ? TREE_OPERAND (expr, 0) : expr);
5465 if (TREE_CODE (decl) != VAR_DECL)
5467 error ("%qE is not a valid template argument of type %qT "
5468 "because %qE is not a variable",
5472 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
5474 error ("%qE is not a valid template argument of type %qT "
5475 "because %qD does not have external linkage",
5481 expr = decay_conversion (expr);
5482 if (expr == error_mark_node)
5483 return error_mark_node;
5485 expr = perform_qualification_conversions (type, expr);
5486 if (expr == error_mark_node)
5487 return error_mark_node;
5489 /* [temp.arg.nontype]/5, bullet 3
5491 For a non-type template-parameter of type reference to object, no
5492 conversions apply. The type referred to by the reference may be more
5493 cv-qualified than the (otherwise identical) type of the
5494 template-argument. The template-parameter is bound directly to the
5495 template-argument, which must be an lvalue. */
5496 else if (TYPE_REF_OBJ_P (type))
5498 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5500 return error_mark_node;
5502 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5504 error ("%qE is not a valid template argument for type %qT "
5505 "because of conflicts in cv-qualification", expr, type);
5509 if (!real_lvalue_p (expr))
5511 error ("%qE is not a valid template argument for type %qT "
5512 "because it is not an lvalue", expr, type);
5516 /* [temp.arg.nontype]/1
5518 A template-argument for a non-type, non-template template-parameter
5519 shall be one of: [...]
5521 -- the address of an object or function with external linkage. */
5522 if (TREE_CODE (expr) == INDIRECT_REF
5523 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5525 expr = TREE_OPERAND (expr, 0);
5528 error ("%q#D is not a valid template argument for type %qT "
5529 "because a reference variable does not have a constant "
5530 "address", expr, type);
5537 error ("%qE is not a valid template argument for type %qT "
5538 "because it is not an object with external linkage",
5543 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5545 error ("%qE is not a valid template argument for type %qT "
5546 "because object %qD has not external linkage",
5551 expr = build_nop (type, build_address (expr));
5553 /* [temp.arg.nontype]/5, bullet 4
5555 For a non-type template-parameter of type pointer to function, only
5556 the function-to-pointer conversion (_conv.func_) is applied. If the
5557 template-argument represents a set of overloaded functions (or a
5558 pointer to such), the matching function is selected from the set
5560 else if (TYPE_PTRFN_P (type))
5562 /* If the argument is a template-id, we might not have enough
5563 context information to decay the pointer. */
5564 if (!type_unknown_p (expr_type))
5566 expr = decay_conversion (expr);
5567 if (expr == error_mark_node)
5568 return error_mark_node;
5571 expr = convert_nontype_argument_function (type, expr);
5572 if (!expr || expr == error_mark_node)
5575 if (TREE_CODE (expr) != ADDR_EXPR)
5577 error ("%qE is not a valid template argument for type %qT", expr, type);
5578 error ("it must be the address of a function with external linkage");
5582 /* [temp.arg.nontype]/5, bullet 5
5584 For a non-type template-parameter of type reference to function, no
5585 conversions apply. If the template-argument represents a set of
5586 overloaded functions, the matching function is selected from the set
5588 else if (TYPE_REFFN_P (type))
5590 if (TREE_CODE (expr) == ADDR_EXPR)
5592 error ("%qE is not a valid template argument for type %qT "
5593 "because it is a pointer", expr, type);
5594 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5598 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5599 if (!expr || expr == error_mark_node)
5602 expr = build_nop (type, build_address (expr));
5604 /* [temp.arg.nontype]/5, bullet 6
5606 For a non-type template-parameter of type pointer to member function,
5607 no conversions apply. If the template-argument represents a set of
5608 overloaded member functions, the matching member function is selected
5609 from the set (_over.over_). */
5610 else if (TYPE_PTRMEMFUNC_P (type))
5612 expr = instantiate_type (type, expr, tf_none);
5613 if (expr == error_mark_node)
5614 return error_mark_node;
5616 /* [temp.arg.nontype] bullet 1 says the pointer to member
5617 expression must be a pointer-to-member constant. */
5618 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5619 return error_mark_node;
5621 /* There is no way to disable standard conversions in
5622 resolve_address_of_overloaded_function (called by
5623 instantiate_type). It is possible that the call succeeded by
5624 converting &B::I to &D::I (where B is a base of D), so we need
5625 to reject this conversion here.
5627 Actually, even if there was a way to disable standard conversions,
5628 it would still be better to reject them here so that we can
5629 provide a superior diagnostic. */
5630 if (!same_type_p (TREE_TYPE (expr), type))
5632 error ("%qE is not a valid template argument for type %qT "
5633 "because it is of type %qT", expr, type,
5635 /* If we are just one standard conversion off, explain. */
5636 if (can_convert (type, TREE_TYPE (expr)))
5637 inform (input_location,
5638 "standard conversions are not allowed in this context");
5642 /* [temp.arg.nontype]/5, bullet 7
5644 For a non-type template-parameter of type pointer to data member,
5645 qualification conversions (_conv.qual_) are applied. */
5646 else if (TYPE_PTRMEM_P (type))
5648 /* [temp.arg.nontype] bullet 1 says the pointer to member
5649 expression must be a pointer-to-member constant. */
5650 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5651 return error_mark_node;
5653 expr = perform_qualification_conversions (type, expr);
5654 if (expr == error_mark_node)
5657 /* A template non-type parameter must be one of the above. */
5661 /* Sanity check: did we actually convert the argument to the
5663 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5664 (type, TREE_TYPE (expr)));
5668 /* Subroutine of coerce_template_template_parms, which returns 1 if
5669 PARM_PARM and ARG_PARM match using the rule for the template
5670 parameters of template template parameters. Both PARM and ARG are
5671 template parameters; the rest of the arguments are the same as for
5672 coerce_template_template_parms.
5675 coerce_template_template_parm (tree parm,
5677 tsubst_flags_t complain,
5681 if (arg == NULL_TREE || arg == error_mark_node
5682 || parm == NULL_TREE || parm == error_mark_node)
5685 if (TREE_CODE (arg) != TREE_CODE (parm))
5688 switch (TREE_CODE (parm))
5691 /* We encounter instantiations of templates like
5692 template <template <template <class> class> class TT>
5695 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5696 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5698 if (!coerce_template_template_parms
5699 (parmparm, argparm, complain, in_decl, outer_args))
5705 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5706 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5707 /* Argument is a parameter pack but parameter is not. */
5712 /* The tsubst call is used to handle cases such as
5714 template <int> class C {};
5715 template <class T, template <T> class TT> class D {};
5718 i.e. the parameter list of TT depends on earlier parameters. */
5719 if (!uses_template_parms (TREE_TYPE (arg))
5721 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5725 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5726 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5727 /* Argument is a parameter pack but parameter is not. */
5740 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5741 template template parameters. Both PARM_PARMS and ARG_PARMS are
5742 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5745 Consider the example:
5746 template <class T> class A;
5747 template<template <class U> class TT> class B;
5749 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5750 the parameters to A, and OUTER_ARGS contains A. */
5753 coerce_template_template_parms (tree parm_parms,
5755 tsubst_flags_t complain,
5759 int nparms, nargs, i;
5763 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5764 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5766 nparms = TREE_VEC_LENGTH (parm_parms);
5767 nargs = TREE_VEC_LENGTH (arg_parms);
5769 /* Determine whether we have a parameter pack at the end of the
5770 template template parameter's template parameter list. */
5771 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5773 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5775 if (parm == error_mark_node)
5778 switch (TREE_CODE (parm))
5782 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5787 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5797 && !(variadic_p && nargs >= nparms - 1))
5800 /* Check all of the template parameters except the parameter pack at
5801 the end (if any). */
5802 for (i = 0; i < nparms - variadic_p; ++i)
5804 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5805 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5808 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5809 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5811 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5819 /* Check each of the template parameters in the template
5820 argument against the template parameter pack at the end of
5821 the template template parameter. */
5822 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
5825 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5827 for (; i < nargs; ++i)
5829 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5832 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5834 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5843 /* Verifies that the deduced template arguments (in TARGS) for the
5844 template template parameters (in TPARMS) represent valid bindings,
5845 by comparing the template parameter list of each template argument
5846 to the template parameter list of its corresponding template
5847 template parameter, in accordance with DR150. This
5848 routine can only be called after all template arguments have been
5849 deduced. It will return TRUE if all of the template template
5850 parameter bindings are okay, FALSE otherwise. */
5852 template_template_parm_bindings_ok_p (tree tparms, tree targs)
5854 int i, ntparms = TREE_VEC_LENGTH (tparms);
5857 /* We're dealing with template parms in this process. */
5858 ++processing_template_decl;
5860 targs = INNERMOST_TEMPLATE_ARGS (targs);
5862 for (i = 0; i < ntparms; ++i)
5864 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
5865 tree targ = TREE_VEC_ELT (targs, i);
5867 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
5869 tree packed_args = NULL_TREE;
5872 if (ARGUMENT_PACK_P (targ))
5874 /* Look inside the argument pack. */
5875 packed_args = ARGUMENT_PACK_ARGS (targ);
5876 len = TREE_VEC_LENGTH (packed_args);
5879 for (idx = 0; idx < len; ++idx)
5881 tree targ_parms = NULL_TREE;
5884 /* Extract the next argument from the argument
5886 targ = TREE_VEC_ELT (packed_args, idx);
5888 if (PACK_EXPANSION_P (targ))
5889 /* Look at the pattern of the pack expansion. */
5890 targ = PACK_EXPANSION_PATTERN (targ);
5892 /* Extract the template parameters from the template
5894 if (TREE_CODE (targ) == TEMPLATE_DECL)
5895 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
5896 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
5897 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
5899 /* Verify that we can coerce the template template
5900 parameters from the template argument to the template
5901 parameter. This requires an exact match. */
5903 && !coerce_template_template_parms
5904 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
5919 --processing_template_decl;
5923 /* Convert the indicated template ARG as necessary to match the
5924 indicated template PARM. Returns the converted ARG, or
5925 error_mark_node if the conversion was unsuccessful. Error and
5926 warning messages are issued under control of COMPLAIN. This
5927 conversion is for the Ith parameter in the parameter list. ARGS is
5928 the full set of template arguments deduced so far. */
5931 convert_template_argument (tree parm,
5934 tsubst_flags_t complain,
5940 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
5942 if (TREE_CODE (arg) == TREE_LIST
5943 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
5945 /* The template argument was the name of some
5946 member function. That's usually
5947 invalid, but static members are OK. In any
5948 case, grab the underlying fields/functions
5949 and issue an error later if required. */
5950 orig_arg = TREE_VALUE (arg);
5951 TREE_TYPE (arg) = unknown_type_node;
5956 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
5957 requires_type = (TREE_CODE (parm) == TYPE_DECL
5958 || requires_tmpl_type);
5960 /* When determining whether an argument pack expansion is a template,
5961 look at the pattern. */
5962 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
5963 arg = PACK_EXPANSION_PATTERN (arg);
5965 /* Deal with an injected-class-name used as a template template arg. */
5966 if (requires_tmpl_type && CLASS_TYPE_P (arg))
5968 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
5969 if (TREE_CODE (t) == TEMPLATE_DECL)
5971 if (complain & tf_warning_or_error)
5972 pedwarn (input_location, OPT_pedantic, "injected-class-name %qD"
5973 " used as template template argument", TYPE_NAME (arg));
5974 else if (flag_pedantic_errors)
5982 ((TREE_CODE (arg) == TEMPLATE_DECL
5983 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
5984 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5985 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
5988 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5989 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
5990 arg = TYPE_STUB_DECL (arg);
5992 is_type = TYPE_P (arg) || is_tmpl_type;
5994 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
5995 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
5997 permerror (input_location, "to refer to a type member of a template parameter, "
5998 "use %<typename %E%>", orig_arg);
6000 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6001 TREE_OPERAND (arg, 1),
6003 complain & tf_error);
6007 if (is_type != requires_type)
6011 if (complain & tf_error)
6013 error ("type/value mismatch at argument %d in template "
6014 "parameter list for %qD",
6017 error (" expected a constant of type %qT, got %qT",
6019 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6020 else if (requires_tmpl_type)
6021 error (" expected a class template, got %qE", orig_arg);
6023 error (" expected a type, got %qE", orig_arg);
6026 return error_mark_node;
6028 if (is_tmpl_type ^ requires_tmpl_type)
6030 if (in_decl && (complain & tf_error))
6032 error ("type/value mismatch at argument %d in template "
6033 "parameter list for %qD",
6036 error (" expected a type, got %qT", DECL_NAME (arg));
6038 error (" expected a class template, got %qT", orig_arg);
6040 return error_mark_node;
6045 if (requires_tmpl_type)
6047 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6048 /* The number of argument required is not known yet.
6049 Just accept it for now. */
6050 val = TREE_TYPE (arg);
6053 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6056 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6058 if (coerce_template_template_parms (parmparm, argparm,
6064 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6066 if (val != error_mark_node)
6068 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6069 val = TREE_TYPE (val);
6070 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6071 val = make_pack_expansion (val);
6076 if (in_decl && (complain & tf_error))
6078 error ("type/value mismatch at argument %d in "
6079 "template parameter list for %qD",
6081 error (" expected a template of type %qD, got %qT",
6085 val = error_mark_node;
6091 /* We only form one instance of each template specialization.
6092 Therefore, if we use a non-canonical variant (i.e., a
6093 typedef), any future messages referring to the type will use
6094 the typedef, which is confusing if those future uses do not
6095 themselves also use the typedef. */
6097 val = strip_typedefs (val);
6101 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6103 if (invalid_nontype_parm_type_p (t, complain))
6104 return error_mark_node;
6106 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6108 if (same_type_p (t, TREE_TYPE (orig_arg)))
6112 /* Not sure if this is reachable, but it doesn't hurt
6114 error ("type mismatch in nontype parameter pack");
6115 val = error_mark_node;
6118 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
6119 /* We used to call digest_init here. However, digest_init
6120 will report errors, which we don't want when complain
6121 is zero. More importantly, digest_init will try too
6122 hard to convert things: for example, `0' should not be
6123 converted to pointer type at this point according to
6124 the standard. Accepting this is not merely an
6125 extension, since deciding whether or not these
6126 conversions can occur is part of determining which
6127 function template to call, or whether a given explicit
6128 argument specification is valid. */
6129 val = convert_nontype_argument (t, orig_arg, complain);
6133 if (val == NULL_TREE)
6134 val = error_mark_node;
6135 else if (val == error_mark_node && (complain & tf_error))
6136 error ("could not convert template argument %qE to %qT", orig_arg, t);
6138 if (TREE_CODE (val) == SCOPE_REF)
6140 /* Strip typedefs from the SCOPE_REF. */
6141 tree type = strip_typedefs (TREE_TYPE (val));
6142 tree scope = strip_typedefs (TREE_OPERAND (val, 0));
6143 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6144 QUALIFIED_NAME_IS_TEMPLATE (val));
6151 /* Coerces the remaining template arguments in INNER_ARGS (from
6152 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6153 Returns the coerced argument pack. PARM_IDX is the position of this
6154 parameter in the template parameter list. ARGS is the original
6155 template argument list. */
6157 coerce_template_parameter_pack (tree parms,
6165 tsubst_flags_t complain)
6167 tree parm = TREE_VEC_ELT (parms, parm_idx);
6168 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6171 tree packed_types = NULL_TREE;
6173 if (arg_idx > nargs)
6176 packed_args = make_tree_vec (nargs - arg_idx);
6178 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6179 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6181 /* When the template parameter is a non-type template
6182 parameter pack whose type uses parameter packs, we need
6183 to look at each of the template arguments
6184 separately. Build a vector of the types for these
6185 non-type template parameters in PACKED_TYPES. */
6187 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6188 packed_types = tsubst_pack_expansion (expansion, args,
6191 if (packed_types == error_mark_node)
6192 return error_mark_node;
6194 /* Check that we have the right number of arguments. */
6196 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6197 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6200 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6201 error ("wrong number of template arguments (%d, should be %d)",
6202 nargs, needed_parms);
6203 return error_mark_node;
6206 /* If we aren't able to check the actual arguments now
6207 (because they haven't been expanded yet), we can at least
6208 verify that all of the types used for the non-type
6209 template parameter pack are, in fact, valid for non-type
6210 template parameters. */
6212 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6214 int j, len = TREE_VEC_LENGTH (packed_types);
6215 for (j = 0; j < len; ++j)
6217 tree t = TREE_VEC_ELT (packed_types, j);
6218 if (invalid_nontype_parm_type_p (t, complain))
6219 return error_mark_node;
6224 /* Convert the remaining arguments, which will be a part of the
6225 parameter pack "parm". */
6226 for (; arg_idx < nargs; ++arg_idx)
6228 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6229 tree actual_parm = TREE_VALUE (parm);
6231 if (packed_types && !PACK_EXPANSION_P (arg))
6233 /* When we have a vector of types (corresponding to the
6234 non-type template parameter pack that uses parameter
6235 packs in its type, as mention above), and the
6236 argument is not an expansion (which expands to a
6237 currently unknown number of arguments), clone the
6238 parm and give it the next type in PACKED_TYPES. */
6239 actual_parm = copy_node (actual_parm);
6240 TREE_TYPE (actual_parm) =
6241 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6244 if (arg != error_mark_node)
6245 arg = convert_template_argument (actual_parm,
6246 arg, new_args, complain, parm_idx,
6248 if (arg == error_mark_node)
6250 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6253 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6254 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6255 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6258 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6259 TREE_TYPE (argument_pack)
6260 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6261 TREE_CONSTANT (argument_pack) = 1;
6264 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6265 #ifdef ENABLE_CHECKING
6266 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6267 TREE_VEC_LENGTH (packed_args));
6269 return argument_pack;
6272 /* Convert all template arguments to their appropriate types, and
6273 return a vector containing the innermost resulting template
6274 arguments. If any error occurs, return error_mark_node. Error and
6275 warning messages are issued under control of COMPLAIN.
6277 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6278 for arguments not specified in ARGS. Otherwise, if
6279 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6280 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6281 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6285 coerce_template_parms (tree parms,
6288 tsubst_flags_t complain,
6289 bool require_all_args,
6290 bool use_default_args)
6292 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6295 tree new_inner_args;
6296 int saved_unevaluated_operand;
6297 int saved_inhibit_evaluation_warnings;
6299 /* When used as a boolean value, indicates whether this is a
6300 variadic template parameter list. Since it's an int, we can also
6301 subtract it from nparms to get the number of non-variadic
6305 if (args == error_mark_node)
6306 return error_mark_node;
6308 nparms = TREE_VEC_LENGTH (parms);
6310 /* Determine if there are any parameter packs. */
6311 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6313 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6314 if (template_parameter_pack_p (tparm))
6318 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6319 /* If there are 0 or 1 parameter packs, we need to expand any argument
6320 packs so that we can deduce a parameter pack from some non-packed args
6321 followed by an argument pack, as in variadic85.C. If there are more
6322 than that, we need to leave argument packs intact so the arguments are
6323 assigned to the right parameter packs. This should only happen when
6324 dealing with a nested class inside a partial specialization of a class
6325 template, as in variadic92.C. */
6326 if (variadic_p <= 1)
6327 inner_args = expand_template_argument_pack (inner_args);
6329 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6330 if ((nargs > nparms && !variadic_p)
6331 || (nargs < nparms - variadic_p
6333 && (!use_default_args
6334 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6335 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6337 if (complain & tf_error)
6342 error ("wrong number of template arguments "
6343 "(%d, should be %d or more)", nargs, nparms);
6346 error ("wrong number of template arguments "
6347 "(%d, should be %d)", nargs, nparms);
6350 error ("provided for %q+D", in_decl);
6353 return error_mark_node;
6356 /* We need to evaluate the template arguments, even though this
6357 template-id may be nested within a "sizeof". */
6358 saved_unevaluated_operand = cp_unevaluated_operand;
6359 cp_unevaluated_operand = 0;
6360 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6361 c_inhibit_evaluation_warnings = 0;
6362 new_inner_args = make_tree_vec (nparms);
6363 new_args = add_outermost_template_args (args, new_inner_args);
6364 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6369 /* Get the Ith template parameter. */
6370 parm = TREE_VEC_ELT (parms, parm_idx);
6372 if (parm == error_mark_node)
6374 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6378 /* Calculate the next argument. */
6379 if (arg_idx < nargs)
6380 arg = TREE_VEC_ELT (inner_args, arg_idx);
6384 if (template_parameter_pack_p (TREE_VALUE (parm))
6385 && !(arg && ARGUMENT_PACK_P (arg)))
6387 /* All remaining arguments will be placed in the
6388 template parameter pack PARM. */
6389 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6390 inner_args, arg_idx,
6394 /* Store this argument. */
6395 if (arg == error_mark_node)
6397 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6399 /* We are done with all of the arguments. */
6406 if (PACK_EXPANSION_P (arg))
6408 if (complain & tf_error)
6410 /* FIXME this restriction was removed by N2555; see
6412 /* If ARG is a pack expansion, but PARM is not a
6413 template parameter pack (if it were, we would have
6414 handled it above), we're trying to expand into a
6415 fixed-length argument list. */
6416 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
6417 sorry ("cannot expand %<%E%> into a fixed-length "
6418 "argument list", arg);
6420 sorry ("cannot expand %<%T%> into a fixed-length "
6421 "argument list", arg);
6426 else if (require_all_args)
6428 /* There must be a default arg in this case. */
6429 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6431 /* The position of the first default template argument,
6432 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6434 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6435 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6440 if (arg == error_mark_node)
6442 if (complain & tf_error)
6443 error ("template argument %d is invalid", arg_idx + 1);
6446 /* This only occurs if there was an error in the template
6447 parameter list itself (which we would already have
6448 reported) that we are trying to recover from, e.g., a class
6449 template with a parameter list such as
6450 template<typename..., typename>. */
6453 arg = convert_template_argument (TREE_VALUE (parm),
6454 arg, new_args, complain,
6457 if (arg == error_mark_node)
6459 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6461 cp_unevaluated_operand = saved_unevaluated_operand;
6462 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6465 return error_mark_node;
6467 #ifdef ENABLE_CHECKING
6468 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6469 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6470 TREE_VEC_LENGTH (new_inner_args));
6473 return new_inner_args;
6476 /* Returns 1 if template args OT and NT are equivalent. */
6479 template_args_equal (tree ot, tree nt)
6484 if (TREE_CODE (nt) == TREE_VEC)
6485 /* For member templates */
6486 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6487 else if (PACK_EXPANSION_P (ot))
6488 return PACK_EXPANSION_P (nt)
6489 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6490 PACK_EXPANSION_PATTERN (nt));
6491 else if (ARGUMENT_PACK_P (ot))
6496 if (!ARGUMENT_PACK_P (nt))
6499 opack = ARGUMENT_PACK_ARGS (ot);
6500 npack = ARGUMENT_PACK_ARGS (nt);
6501 len = TREE_VEC_LENGTH (opack);
6502 if (TREE_VEC_LENGTH (npack) != len)
6504 for (i = 0; i < len; ++i)
6505 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6506 TREE_VEC_ELT (npack, i)))
6510 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6512 /* We get here probably because we are in the middle of substituting
6513 into the pattern of a pack expansion. In that case the
6514 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6515 interested in. So we want to use the initial pack argument for
6517 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6518 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6519 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6520 return template_args_equal (ot, nt);
6522 else if (TYPE_P (nt))
6523 return TYPE_P (ot) && same_type_p (ot, nt);
6524 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6527 return cp_tree_equal (ot, nt);
6530 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6531 of template arguments. Returns 0 otherwise. */
6534 comp_template_args (tree oldargs, tree newargs)
6538 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6541 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6543 tree nt = TREE_VEC_ELT (newargs, i);
6544 tree ot = TREE_VEC_ELT (oldargs, i);
6546 if (! template_args_equal (ot, nt))
6553 add_pending_template (tree d)
6555 tree ti = (TYPE_P (d)
6556 ? CLASSTYPE_TEMPLATE_INFO (d)
6557 : DECL_TEMPLATE_INFO (d));
6558 struct pending_template *pt;
6561 if (TI_PENDING_TEMPLATE_FLAG (ti))
6564 /* We are called both from instantiate_decl, where we've already had a
6565 tinst_level pushed, and instantiate_template, where we haven't.
6567 level = !current_tinst_level || current_tinst_level->decl != d;
6570 push_tinst_level (d);
6572 pt = ggc_alloc_pending_template ();
6574 pt->tinst = current_tinst_level;
6575 if (last_pending_template)
6576 last_pending_template->next = pt;
6578 pending_templates = pt;
6580 last_pending_template = pt;
6582 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6589 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6590 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6591 documentation for TEMPLATE_ID_EXPR. */
6594 lookup_template_function (tree fns, tree arglist)
6598 if (fns == error_mark_node || arglist == error_mark_node)
6599 return error_mark_node;
6601 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6602 gcc_assert (fns && (is_overloaded_fn (fns)
6603 || TREE_CODE (fns) == IDENTIFIER_NODE));
6605 if (BASELINK_P (fns))
6607 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6609 BASELINK_FUNCTIONS (fns),
6614 type = TREE_TYPE (fns);
6615 if (TREE_CODE (fns) == OVERLOAD || !type)
6616 type = unknown_type_node;
6618 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6621 /* Within the scope of a template class S<T>, the name S gets bound
6622 (in build_self_reference) to a TYPE_DECL for the class, not a
6623 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6624 or one of its enclosing classes, and that type is a template,
6625 return the associated TEMPLATE_DECL. Otherwise, the original
6628 Also handle the case when DECL is a TREE_LIST of ambiguous
6629 injected-class-names from different bases. */
6632 maybe_get_template_decl_from_type_decl (tree decl)
6634 if (decl == NULL_TREE)
6637 /* DR 176: A lookup that finds an injected-class-name (10.2
6638 [class.member.lookup]) can result in an ambiguity in certain cases
6639 (for example, if it is found in more than one base class). If all of
6640 the injected-class-names that are found refer to specializations of
6641 the same class template, and if the name is followed by a
6642 template-argument-list, the reference refers to the class template
6643 itself and not a specialization thereof, and is not ambiguous. */
6644 if (TREE_CODE (decl) == TREE_LIST)
6646 tree t, tmpl = NULL_TREE;
6647 for (t = decl; t; t = TREE_CHAIN (t))
6649 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6652 else if (tmpl != elt)
6655 if (tmpl && t == NULL_TREE)
6661 return (decl != NULL_TREE
6662 && DECL_SELF_REFERENCE_P (decl)
6663 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6664 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
6667 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6668 parameters, find the desired type.
6670 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6672 IN_DECL, if non-NULL, is the template declaration we are trying to
6675 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6676 the class we are looking up.
6678 Issue error and warning messages under control of COMPLAIN.
6680 If the template class is really a local class in a template
6681 function, then the FUNCTION_CONTEXT is the function in which it is
6684 ??? Note that this function is currently called *twice* for each
6685 template-id: the first time from the parser, while creating the
6686 incomplete type (finish_template_type), and the second type during the
6687 real instantiation (instantiate_template_class). This is surely something
6688 that we want to avoid. It also causes some problems with argument
6689 coercion (see convert_nontype_argument for more information on this). */
6692 lookup_template_class (tree d1,
6697 tsubst_flags_t complain)
6699 tree templ = NULL_TREE, parmlist;
6706 timevar_push (TV_NAME_LOOKUP);
6708 if (TREE_CODE (d1) == IDENTIFIER_NODE)
6710 tree value = innermost_non_namespace_value (d1);
6711 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
6716 push_decl_namespace (context);
6717 templ = lookup_name (d1);
6718 templ = maybe_get_template_decl_from_type_decl (templ);
6720 pop_decl_namespace ();
6723 context = DECL_CONTEXT (templ);
6725 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
6727 tree type = TREE_TYPE (d1);
6729 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6730 an implicit typename for the second A. Deal with it. */
6731 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
6732 type = TREE_TYPE (type);
6734 if (CLASSTYPE_TEMPLATE_INFO (type))
6736 templ = CLASSTYPE_TI_TEMPLATE (type);
6737 d1 = DECL_NAME (templ);
6740 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6741 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
6743 templ = TYPE_TI_TEMPLATE (d1);
6744 d1 = DECL_NAME (templ);
6746 else if (TREE_CODE (d1) == TEMPLATE_DECL
6747 && DECL_TEMPLATE_RESULT (d1)
6748 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
6751 d1 = DECL_NAME (templ);
6752 context = DECL_CONTEXT (templ);
6755 /* Issue an error message if we didn't find a template. */
6758 if (complain & tf_error)
6759 error ("%qT is not a template", d1);
6760 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6763 if (TREE_CODE (templ) != TEMPLATE_DECL
6764 /* Make sure it's a user visible template, if it was named by
6766 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
6767 && !PRIMARY_TEMPLATE_P (templ)))
6769 if (complain & tf_error)
6771 error ("non-template type %qT used as a template", d1);
6773 error ("for template declaration %q+D", in_decl);
6775 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6778 complain &= ~tf_user;
6780 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
6782 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6783 template arguments */
6789 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6791 /* Consider an example where a template template parameter declared as
6793 template <class T, class U = std::allocator<T> > class TT
6795 The template parameter level of T and U are one level larger than
6796 of TT. To proper process the default argument of U, say when an
6797 instantiation `TT<int>' is seen, we need to build the full
6798 arguments containing {int} as the innermost level. Outer levels,
6799 available when not appearing as default template argument, can be
6800 obtained from the arguments of the enclosing template.
6802 Suppose that TT is later substituted with std::vector. The above
6803 instantiation is `TT<int, std::allocator<T> >' with TT at
6804 level 1, and T at level 2, while the template arguments at level 1
6805 becomes {std::vector} and the inner level 2 is {int}. */
6807 outer = DECL_CONTEXT (templ);
6809 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6810 else if (current_template_parms)
6811 /* This is an argument of the current template, so we haven't set
6812 DECL_CONTEXT yet. */
6813 outer = current_template_args ();
6816 arglist = add_to_template_args (outer, arglist);
6818 arglist2 = coerce_template_parms (parmlist, arglist, templ,
6820 /*require_all_args=*/true,
6821 /*use_default_args=*/true);
6822 if (arglist2 == error_mark_node
6823 || (!uses_template_parms (arglist2)
6824 && check_instantiated_args (templ, arglist2, complain)))
6825 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6827 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
6828 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
6832 tree template_type = TREE_TYPE (templ);
6835 tree found = NULL_TREE;
6838 int is_dependent_type;
6839 int use_partial_inst_tmpl = false;
6841 gen_tmpl = most_general_template (templ);
6842 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
6843 parm_depth = TMPL_PARMS_DEPTH (parmlist);
6844 arg_depth = TMPL_ARGS_DEPTH (arglist);
6846 if (arg_depth == 1 && parm_depth > 1)
6848 /* We've been given an incomplete set of template arguments.
6851 template <class T> struct S1 {
6852 template <class U> struct S2 {};
6853 template <class U> struct S2<U*> {};
6856 we will be called with an ARGLIST of `U*', but the
6857 TEMPLATE will be `template <class T> template
6858 <class U> struct S1<T>::S2'. We must fill in the missing
6861 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
6863 arg_depth = TMPL_ARGS_DEPTH (arglist);
6866 /* Now we should have enough arguments. */
6867 gcc_assert (parm_depth == arg_depth);
6869 /* From here on, we're only interested in the most general
6872 /* Calculate the BOUND_ARGS. These will be the args that are
6873 actually tsubst'd into the definition to create the
6877 /* We have multiple levels of arguments to coerce, at once. */
6879 int saved_depth = TMPL_ARGS_DEPTH (arglist);
6881 tree bound_args = make_tree_vec (parm_depth);
6883 for (i = saved_depth,
6884 t = DECL_TEMPLATE_PARMS (gen_tmpl);
6885 i > 0 && t != NULL_TREE;
6886 --i, t = TREE_CHAIN (t))
6889 if (i == saved_depth)
6890 a = coerce_template_parms (TREE_VALUE (t),
6893 /*require_all_args=*/true,
6894 /*use_default_args=*/true);
6896 /* Outer levels should have already been coerced. */
6897 a = TMPL_ARGS_LEVEL (arglist, i);
6899 /* Don't process further if one of the levels fails. */
6900 if (a == error_mark_node)
6902 /* Restore the ARGLIST to its full size. */
6903 TREE_VEC_LENGTH (arglist) = saved_depth;
6904 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6907 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
6909 /* We temporarily reduce the length of the ARGLIST so
6910 that coerce_template_parms will see only the arguments
6911 corresponding to the template parameters it is
6913 TREE_VEC_LENGTH (arglist)--;
6916 /* Restore the ARGLIST to its full size. */
6917 TREE_VEC_LENGTH (arglist) = saved_depth;
6919 arglist = bound_args;
6923 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
6924 INNERMOST_TEMPLATE_ARGS (arglist),
6927 /*require_all_args=*/true,
6928 /*use_default_args=*/true);
6930 if (arglist == error_mark_node)
6931 /* We were unable to bind the arguments. */
6932 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6934 /* In the scope of a template class, explicit references to the
6935 template class refer to the type of the template, not any
6936 instantiation of it. For example, in:
6938 template <class T> class C { void f(C<T>); }
6940 the `C<T>' is just the same as `C'. Outside of the
6941 class, however, such a reference is an instantiation. */
6943 || !PRIMARY_TEMPLATE_P (gen_tmpl)
6944 || currently_open_class (template_type))
6945 /* comp_template_args is expensive, check it last. */
6946 && comp_template_args (TYPE_TI_ARGS (template_type),
6948 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type);
6950 /* If we already have this specialization, return it. */
6951 elt.tmpl = gen_tmpl;
6953 hash = hash_specialization (&elt);
6954 entry = (spec_entry *) htab_find_with_hash (type_specializations,
6958 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec);
6960 is_dependent_type = uses_template_parms (arglist);
6962 /* If the deduced arguments are invalid, then the binding
6964 if (!is_dependent_type
6965 && check_instantiated_args (gen_tmpl,
6966 INNERMOST_TEMPLATE_ARGS (arglist),
6968 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6970 if (!is_dependent_type
6971 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6972 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
6973 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
6975 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
6976 DECL_NAME (gen_tmpl),
6977 /*tag_scope=*/ts_global);
6978 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
6981 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
6984 context = global_namespace;
6986 /* Create the type. */
6987 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
6989 if (!is_dependent_type)
6991 set_current_access_from_decl (TYPE_NAME (template_type));
6992 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
6993 tsubst (ENUM_UNDERLYING_TYPE (template_type),
6994 arglist, complain, in_decl),
6995 SCOPED_ENUM_P (template_type), NULL);
6999 /* We don't want to call start_enum for this type, since
7000 the values for the enumeration constants may involve
7001 template parameters. And, no one should be interested
7002 in the enumeration constants for such a type. */
7003 t = cxx_make_type (ENUMERAL_TYPE);
7004 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7006 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7007 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7008 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7012 t = make_class_type (TREE_CODE (template_type));
7013 CLASSTYPE_DECLARED_CLASS (t)
7014 = CLASSTYPE_DECLARED_CLASS (template_type);
7015 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7016 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7018 /* A local class. Make sure the decl gets registered properly. */
7019 if (context == current_function_decl)
7020 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7022 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7023 /* This instantiation is another name for the primary
7024 template type. Set the TYPE_CANONICAL field
7026 TYPE_CANONICAL (t) = template_type;
7027 else if (any_template_arguments_need_structural_equality_p (arglist))
7028 /* Some of the template arguments require structural
7029 equality testing, so this template class requires
7030 structural equality testing. */
7031 SET_TYPE_STRUCTURAL_EQUALITY (t);
7034 /* If we called start_enum or pushtag above, this information
7035 will already be set up. */
7038 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7040 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7041 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7042 DECL_SOURCE_LOCATION (type_decl)
7043 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7046 type_decl = TYPE_NAME (t);
7048 TREE_PRIVATE (type_decl)
7049 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
7050 TREE_PROTECTED (type_decl)
7051 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
7052 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7054 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7055 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7058 /* Let's consider the explicit specialization of a member
7059 of a class template specialization that is implicitely instantiated,
7064 template<class U> struct M {}; //#0
7069 struct S<int>::M<char> //#1
7073 [temp.expl.spec]/4 says this is valid.
7075 In this case, when we write:
7078 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7081 When we encounter #1, we want to store the partial instantiation
7082 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7084 For all cases other than this "explicit specialization of member of a
7085 class template", we just want to store the most general template into
7086 the CLASSTYPE_TI_TEMPLATE of M.
7088 This case of "explicit specialization of member of a class template"
7090 1/ the enclosing class is an instantiation of, and therefore not
7091 the same as, the context of the most general template, and
7092 2/ we aren't looking at the partial instantiation itself, i.e.
7093 the innermost arguments are not the same as the innermost parms of
7094 the most general template.
7096 So it's only when 1/ and 2/ happens that we want to use the partial
7097 instantiation of the member template in lieu of its most general
7100 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7101 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7102 /* the enclosing class must be an instantiation... */
7103 && CLASS_TYPE_P (context)
7104 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7106 tree partial_inst_args;
7107 TREE_VEC_LENGTH (arglist)--;
7108 ++processing_template_decl;
7110 tsubst (INNERMOST_TEMPLATE_ARGS
7111 (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7112 arglist, complain, NULL_TREE);
7113 --processing_template_decl;
7114 TREE_VEC_LENGTH (arglist)++;
7115 use_partial_inst_tmpl =
7116 /*...and we must not be looking at the partial instantiation
7118 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7122 if (!use_partial_inst_tmpl)
7123 /* This case is easy; there are no member templates involved. */
7127 /* This is a full instantiation of a member template. Find
7128 the partial instantiation of which this is an instance. */
7130 /* Temporarily reduce by one the number of levels in the ARGLIST
7131 so as to avoid comparing the last set of arguments. */
7132 TREE_VEC_LENGTH (arglist)--;
7133 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7134 TREE_VEC_LENGTH (arglist)++;
7135 found = CLASSTYPE_TI_TEMPLATE (found);
7138 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7141 slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
7142 &elt, hash, INSERT);
7143 *slot = ggc_alloc_spec_entry ();
7146 /* Note this use of the partial instantiation so we can check it
7147 later in maybe_process_partial_specialization. */
7148 DECL_TEMPLATE_INSTANTIATIONS (templ)
7149 = tree_cons (arglist, t,
7150 DECL_TEMPLATE_INSTANTIATIONS (templ));
7152 if (TREE_CODE (t) == ENUMERAL_TYPE && !is_dependent_type)
7153 /* Now that the type has been registered on the instantiations
7154 list, we set up the enumerators. Because the enumeration
7155 constants may involve the enumeration type itself, we make
7156 sure to register the type first, and then create the
7157 constants. That way, doing tsubst_expr for the enumeration
7158 constants won't result in recursive calls here; we'll find
7159 the instantiation and exit above. */
7160 tsubst_enum (template_type, t, arglist);
7162 if (is_dependent_type)
7163 /* If the type makes use of template parameters, the
7164 code that generates debugging information will crash. */
7165 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
7167 /* Possibly limit visibility based on template args. */
7168 TREE_PUBLIC (type_decl) = 1;
7169 determine_visibility (type_decl);
7171 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
7173 timevar_pop (TV_NAME_LOOKUP);
7180 /* True when we should also visit template parameters that occur in
7181 non-deduced contexts. */
7182 bool include_nondeduced_p;
7183 struct pointer_set_t *visited;
7186 /* Called from for_each_template_parm via walk_tree. */
7189 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7192 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7193 tree_fn_t fn = pfd->fn;
7194 void *data = pfd->data;
7197 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7198 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7199 pfd->include_nondeduced_p))
7200 return error_mark_node;
7202 switch (TREE_CODE (t))
7205 if (TYPE_PTRMEMFUNC_P (t))
7211 if (!TYPE_TEMPLATE_INFO (t))
7213 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
7214 fn, data, pfd->visited,
7215 pfd->include_nondeduced_p))
7216 return error_mark_node;
7220 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7221 fn, data, pfd->visited,
7222 pfd->include_nondeduced_p)
7223 || for_each_template_parm (TYPE_MAX_VALUE (t),
7224 fn, data, pfd->visited,
7225 pfd->include_nondeduced_p))
7226 return error_mark_node;
7230 /* Since we're not going to walk subtrees, we have to do this
7232 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7233 pfd->visited, pfd->include_nondeduced_p))
7234 return error_mark_node;
7238 /* Check the return type. */
7239 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7240 pfd->include_nondeduced_p))
7241 return error_mark_node;
7243 /* Check the parameter types. Since default arguments are not
7244 instantiated until they are needed, the TYPE_ARG_TYPES may
7245 contain expressions that involve template parameters. But,
7246 no-one should be looking at them yet. And, once they're
7247 instantiated, they don't contain template parameters, so
7248 there's no point in looking at them then, either. */
7252 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7253 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7254 pfd->visited, pfd->include_nondeduced_p))
7255 return error_mark_node;
7257 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7258 want walk_tree walking into them itself. */
7264 if (pfd->include_nondeduced_p
7265 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7267 pfd->include_nondeduced_p))
7268 return error_mark_node;
7273 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7274 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7275 pfd->visited, pfd->include_nondeduced_p))
7276 return error_mark_node;
7281 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7282 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7283 pfd->visited, pfd->include_nondeduced_p))
7284 return error_mark_node;
7285 if (DECL_CONTEXT (t)
7286 && pfd->include_nondeduced_p
7287 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7288 pfd->visited, pfd->include_nondeduced_p))
7289 return error_mark_node;
7292 case BOUND_TEMPLATE_TEMPLATE_PARM:
7293 /* Record template parameters such as `T' inside `TT<T>'. */
7294 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7295 pfd->include_nondeduced_p))
7296 return error_mark_node;
7299 case TEMPLATE_TEMPLATE_PARM:
7300 case TEMPLATE_TYPE_PARM:
7301 case TEMPLATE_PARM_INDEX:
7302 if (fn && (*fn)(t, data))
7303 return error_mark_node;
7305 return error_mark_node;
7309 /* A template template parameter is encountered. */
7310 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7311 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7312 pfd->include_nondeduced_p))
7313 return error_mark_node;
7315 /* Already substituted template template parameter */
7321 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7323 pfd->include_nondeduced_p))
7324 return error_mark_node;
7328 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7329 && pfd->include_nondeduced_p
7330 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7331 (TREE_TYPE (t)), fn, data,
7332 pfd->visited, pfd->include_nondeduced_p))
7333 return error_mark_node;
7338 /* If there's no type, then this thing must be some expression
7339 involving template parameters. */
7340 if (!fn && !TREE_TYPE (t))
7341 return error_mark_node;
7346 case REINTERPRET_CAST_EXPR:
7347 case CONST_CAST_EXPR:
7348 case STATIC_CAST_EXPR:
7349 case DYNAMIC_CAST_EXPR:
7353 case PSEUDO_DTOR_EXPR:
7355 return error_mark_node;
7362 /* We didn't find any template parameters we liked. */
7366 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7367 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7368 call FN with the parameter and the DATA.
7369 If FN returns nonzero, the iteration is terminated, and
7370 for_each_template_parm returns 1. Otherwise, the iteration
7371 continues. If FN never returns a nonzero value, the value
7372 returned by for_each_template_parm is 0. If FN is NULL, it is
7373 considered to be the function which always returns 1.
7375 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7376 parameters that occur in non-deduced contexts. When false, only
7377 visits those template parameters that can be deduced. */
7380 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7381 struct pointer_set_t *visited,
7382 bool include_nondeduced_p)
7384 struct pair_fn_data pfd;
7390 pfd.include_nondeduced_p = include_nondeduced_p;
7392 /* Walk the tree. (Conceptually, we would like to walk without
7393 duplicates, but for_each_template_parm_r recursively calls
7394 for_each_template_parm, so we would need to reorganize a fair
7395 bit to use walk_tree_without_duplicates, so we keep our own
7398 pfd.visited = visited;
7400 pfd.visited = pointer_set_create ();
7401 result = cp_walk_tree (&t,
7402 for_each_template_parm_r,
7404 pfd.visited) != NULL_TREE;
7409 pointer_set_destroy (pfd.visited);
7416 /* Returns true if T depends on any template parameter. */
7419 uses_template_parms (tree t)
7422 int saved_processing_template_decl;
7424 saved_processing_template_decl = processing_template_decl;
7425 if (!saved_processing_template_decl)
7426 processing_template_decl = 1;
7428 dependent_p = dependent_type_p (t);
7429 else if (TREE_CODE (t) == TREE_VEC)
7430 dependent_p = any_dependent_template_arguments_p (t);
7431 else if (TREE_CODE (t) == TREE_LIST)
7432 dependent_p = (uses_template_parms (TREE_VALUE (t))
7433 || uses_template_parms (TREE_CHAIN (t)));
7434 else if (TREE_CODE (t) == TYPE_DECL)
7435 dependent_p = dependent_type_p (TREE_TYPE (t));
7438 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7439 || TREE_CODE (t) == OVERLOAD
7440 || TREE_CODE (t) == BASELINK
7441 || TREE_CODE (t) == IDENTIFIER_NODE
7442 || TREE_CODE (t) == TRAIT_EXPR
7443 || TREE_CODE (t) == CONSTRUCTOR
7444 || CONSTANT_CLASS_P (t))
7445 dependent_p = (type_dependent_expression_p (t)
7446 || value_dependent_expression_p (t));
7449 gcc_assert (t == error_mark_node);
7450 dependent_p = false;
7453 processing_template_decl = saved_processing_template_decl;
7458 /* Returns true if T depends on any template parameter with level LEVEL. */
7461 uses_template_parms_level (tree t, int level)
7463 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
7464 /*include_nondeduced_p=*/true);
7467 static int tinst_depth;
7468 extern int max_tinst_depth;
7469 #ifdef GATHER_STATISTICS
7472 static int tinst_level_tick;
7473 static int last_template_error_tick;
7475 /* We're starting to instantiate D; record the template instantiation context
7476 for diagnostics and to restore it later. */
7479 push_tinst_level (tree d)
7481 struct tinst_level *new_level;
7483 if (tinst_depth >= max_tinst_depth)
7485 /* If the instantiation in question still has unbound template parms,
7486 we don't really care if we can't instantiate it, so just return.
7487 This happens with base instantiation for implicit `typename'. */
7488 if (uses_template_parms (d))
7491 last_template_error_tick = tinst_level_tick;
7492 error ("template instantiation depth exceeds maximum of %d (use "
7493 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7494 max_tinst_depth, d);
7496 print_instantiation_context ();
7501 new_level = ggc_alloc_tinst_level ();
7502 new_level->decl = d;
7503 new_level->locus = input_location;
7504 new_level->in_system_header_p = in_system_header;
7505 new_level->next = current_tinst_level;
7506 current_tinst_level = new_level;
7509 #ifdef GATHER_STATISTICS
7510 if (tinst_depth > depth_reached)
7511 depth_reached = tinst_depth;
7518 /* We're done instantiating this template; return to the instantiation
7522 pop_tinst_level (void)
7524 /* Restore the filename and line number stashed away when we started
7525 this instantiation. */
7526 input_location = current_tinst_level->locus;
7527 current_tinst_level = current_tinst_level->next;
7532 /* We're instantiating a deferred template; restore the template
7533 instantiation context in which the instantiation was requested, which
7534 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7537 reopen_tinst_level (struct tinst_level *level)
7539 struct tinst_level *t;
7542 for (t = level; t; t = t->next)
7545 current_tinst_level = level;
7550 /* Returns the TINST_LEVEL which gives the original instantiation
7553 struct tinst_level *
7554 outermost_tinst_level (void)
7556 struct tinst_level *level = current_tinst_level;
7559 level = level->next;
7563 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
7566 parameter_of_template_p (tree parm, tree templ)
7571 if (!parm || !templ)
7574 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
7575 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7577 parms = DECL_TEMPLATE_PARMS (templ);
7578 parms = INNERMOST_TEMPLATE_PARMS (parms);
7580 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
7581 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
7587 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7588 vector of template arguments, as for tsubst.
7590 Returns an appropriate tsubst'd friend declaration. */
7593 tsubst_friend_function (tree decl, tree args)
7597 if (TREE_CODE (decl) == FUNCTION_DECL
7598 && DECL_TEMPLATE_INSTANTIATION (decl)
7599 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
7600 /* This was a friend declared with an explicit template
7601 argument list, e.g.:
7605 to indicate that f was a template instantiation, not a new
7606 function declaration. Now, we have to figure out what
7607 instantiation of what template. */
7609 tree template_id, arglist, fns;
7612 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
7614 /* Friend functions are looked up in the containing namespace scope.
7615 We must enter that scope, to avoid finding member functions of the
7616 current class with same name. */
7617 push_nested_namespace (ns);
7618 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
7619 tf_warning_or_error, NULL_TREE,
7620 /*integral_constant_expression_p=*/false);
7621 pop_nested_namespace (ns);
7622 arglist = tsubst (DECL_TI_ARGS (decl), args,
7623 tf_warning_or_error, NULL_TREE);
7624 template_id = lookup_template_function (fns, arglist);
7626 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7627 tmpl = determine_specialization (template_id, new_friend,
7629 /*need_member_template=*/0,
7630 TREE_VEC_LENGTH (args),
7632 return instantiate_template (tmpl, new_args, tf_error);
7635 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7637 /* The NEW_FRIEND will look like an instantiation, to the
7638 compiler, but is not an instantiation from the point of view of
7639 the language. For example, we might have had:
7641 template <class T> struct S {
7642 template <class U> friend void f(T, U);
7645 Then, in S<int>, template <class U> void f(int, U) is not an
7646 instantiation of anything. */
7647 if (new_friend == error_mark_node)
7648 return error_mark_node;
7650 DECL_USE_TEMPLATE (new_friend) = 0;
7651 if (TREE_CODE (decl) == TEMPLATE_DECL)
7653 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
7654 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
7655 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
7658 /* The mangled name for the NEW_FRIEND is incorrect. The function
7659 is not a template instantiation and should not be mangled like
7660 one. Therefore, we forget the mangling here; we'll recompute it
7661 later if we need it. */
7662 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
7664 SET_DECL_RTL (new_friend, NULL);
7665 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
7668 if (DECL_NAMESPACE_SCOPE_P (new_friend))
7671 tree new_friend_template_info;
7672 tree new_friend_result_template_info;
7674 int new_friend_is_defn;
7676 /* We must save some information from NEW_FRIEND before calling
7677 duplicate decls since that function will free NEW_FRIEND if
7679 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
7680 new_friend_is_defn =
7681 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7682 (template_for_substitution (new_friend)))
7684 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
7686 /* This declaration is a `primary' template. */
7687 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
7689 new_friend_result_template_info
7690 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
7693 new_friend_result_template_info = NULL_TREE;
7695 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7696 if (new_friend_is_defn)
7697 DECL_INITIAL (new_friend) = error_mark_node;
7699 /* Inside pushdecl_namespace_level, we will push into the
7700 current namespace. However, the friend function should go
7701 into the namespace of the template. */
7702 ns = decl_namespace_context (new_friend);
7703 push_nested_namespace (ns);
7704 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
7705 pop_nested_namespace (ns);
7707 if (old_decl == error_mark_node)
7708 return error_mark_node;
7710 if (old_decl != new_friend)
7712 /* This new friend declaration matched an existing
7713 declaration. For example, given:
7715 template <class T> void f(T);
7716 template <class U> class C {
7717 template <class T> friend void f(T) {}
7720 the friend declaration actually provides the definition
7721 of `f', once C has been instantiated for some type. So,
7722 old_decl will be the out-of-class template declaration,
7723 while new_friend is the in-class definition.
7725 But, if `f' was called before this point, the
7726 instantiation of `f' will have DECL_TI_ARGS corresponding
7727 to `T' but not to `U', references to which might appear
7728 in the definition of `f'. Previously, the most general
7729 template for an instantiation of `f' was the out-of-class
7730 version; now it is the in-class version. Therefore, we
7731 run through all specialization of `f', adding to their
7732 DECL_TI_ARGS appropriately. In particular, they need a
7733 new set of outer arguments, corresponding to the
7734 arguments for this class instantiation.
7736 The same situation can arise with something like this:
7739 template <class T> class C {
7743 when `C<int>' is instantiated. Now, `f(int)' is defined
7746 if (!new_friend_is_defn)
7747 /* On the other hand, if the in-class declaration does
7748 *not* provide a definition, then we don't want to alter
7749 existing definitions. We can just leave everything
7754 tree new_template = TI_TEMPLATE (new_friend_template_info);
7755 tree new_args = TI_ARGS (new_friend_template_info);
7757 /* Overwrite whatever template info was there before, if
7758 any, with the new template information pertaining to
7760 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
7762 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
7764 /* We should have called reregister_specialization in
7766 gcc_assert (retrieve_specialization (new_template,
7770 /* Instantiate it if the global has already been used. */
7771 if (DECL_ODR_USED (old_decl))
7772 instantiate_decl (old_decl, /*defer_ok=*/true,
7773 /*expl_inst_class_mem_p=*/false);
7779 /* Indicate that the old function template is a partial
7781 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
7782 = new_friend_result_template_info;
7784 gcc_assert (new_template
7785 == most_general_template (new_template));
7786 gcc_assert (new_template != old_decl);
7788 /* Reassign any specializations already in the hash table
7789 to the new more general template, and add the
7790 additional template args. */
7791 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
7795 tree spec = TREE_VALUE (t);
7798 elt.tmpl = old_decl;
7799 elt.args = DECL_TI_ARGS (spec);
7800 elt.spec = NULL_TREE;
7802 htab_remove_elt (decl_specializations, &elt);
7805 = add_outermost_template_args (new_args,
7806 DECL_TI_ARGS (spec));
7808 register_specialization
7809 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
7812 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
7816 /* The information from NEW_FRIEND has been merged into OLD_DECL
7817 by duplicate_decls. */
7818 new_friend = old_decl;
7823 tree context = DECL_CONTEXT (new_friend);
7827 template <class T> class C {
7828 template <class U> friend void C1<U>::f (); // case 1
7829 friend void C2<T>::f (); // case 2
7831 we only need to make sure CONTEXT is a complete type for
7832 case 2. To distinguish between the two cases, we note that
7833 CONTEXT of case 1 remains dependent type after tsubst while
7834 this isn't true for case 2. */
7835 ++processing_template_decl;
7836 dependent_p = dependent_type_p (context);
7837 --processing_template_decl;
7840 && !complete_type_or_else (context, NULL_TREE))
7841 return error_mark_node;
7843 if (COMPLETE_TYPE_P (context))
7845 /* Check to see that the declaration is really present, and,
7846 possibly obtain an improved declaration. */
7847 tree fn = check_classfn (context,
7848 new_friend, NULL_TREE);
7858 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7859 template arguments, as for tsubst.
7861 Returns an appropriate tsubst'd friend type or error_mark_node on
7865 tsubst_friend_class (tree friend_tmpl, tree args)
7871 context = CP_DECL_CONTEXT (friend_tmpl);
7873 if (context != global_namespace)
7875 if (TREE_CODE (context) == NAMESPACE_DECL)
7876 push_nested_namespace (context);
7878 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
7881 /* Look for a class template declaration. We look for hidden names
7882 because two friend declarations of the same template are the
7883 same. For example, in:
7886 template <typename> friend class F;
7888 template <typename> struct B {
7889 template <typename> friend class F;
7892 both F templates are the same. */
7893 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
7894 /*block_p=*/true, 0,
7895 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
7897 /* But, if we don't find one, it might be because we're in a
7898 situation like this:
7906 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7907 for `S<int>', not the TEMPLATE_DECL. */
7908 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
7910 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
7911 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
7914 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
7916 /* The friend template has already been declared. Just
7917 check to see that the declarations match, and install any new
7918 default parameters. We must tsubst the default parameters,
7919 of course. We only need the innermost template parameters
7920 because that is all that redeclare_class_template will look
7922 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
7923 > TMPL_ARGS_DEPTH (args))
7926 location_t saved_input_location;
7927 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
7928 args, tf_warning_or_error);
7930 saved_input_location = input_location;
7931 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
7932 redeclare_class_template (TREE_TYPE (tmpl), parms);
7933 input_location = saved_input_location;
7937 friend_type = TREE_TYPE (tmpl);
7941 /* The friend template has not already been declared. In this
7942 case, the instantiation of the template class will cause the
7943 injection of this template into the global scope. */
7944 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
7945 if (tmpl == error_mark_node)
7946 return error_mark_node;
7948 /* The new TMPL is not an instantiation of anything, so we
7949 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7950 the new type because that is supposed to be the corresponding
7951 template decl, i.e., TMPL. */
7952 DECL_USE_TEMPLATE (tmpl) = 0;
7953 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
7954 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
7955 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
7956 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
7958 /* Inject this template into the global scope. */
7959 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
7962 if (context != global_namespace)
7964 if (TREE_CODE (context) == NAMESPACE_DECL)
7965 pop_nested_namespace (context);
7967 pop_nested_class ();
7973 /* Returns zero if TYPE cannot be completed later due to circularity.
7974 Otherwise returns one. */
7977 can_complete_type_without_circularity (tree type)
7979 if (type == NULL_TREE || type == error_mark_node)
7981 else if (COMPLETE_TYPE_P (type))
7983 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
7984 return can_complete_type_without_circularity (TREE_TYPE (type));
7985 else if (CLASS_TYPE_P (type)
7986 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
7992 /* Apply any attributes which had to be deferred until instantiation
7993 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7994 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7997 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
7998 tree args, tsubst_flags_t complain, tree in_decl)
8000 tree last_dep = NULL_TREE;
8004 for (t = attributes; t; t = TREE_CHAIN (t))
8005 if (ATTR_IS_DEPENDENT (t))
8008 attributes = copy_list (attributes);
8012 if (DECL_P (*decl_p))
8014 if (TREE_TYPE (*decl_p) == error_mark_node)
8016 p = &DECL_ATTRIBUTES (*decl_p);
8019 p = &TYPE_ATTRIBUTES (*decl_p);
8023 tree late_attrs = NULL_TREE;
8024 tree *q = &late_attrs;
8026 for (*p = attributes; *p; )
8029 if (ATTR_IS_DEPENDENT (t))
8031 *p = TREE_CHAIN (t);
8032 TREE_CHAIN (t) = NULL_TREE;
8033 /* If the first attribute argument is an identifier, don't
8034 pass it through tsubst. Attributes like mode, format,
8035 cleanup and several target specific attributes expect it
8038 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
8039 && TREE_VALUE (TREE_VALUE (t))
8040 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
8041 == IDENTIFIER_NODE))
8044 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8046 /*integral_constant_expression_p=*/false);
8047 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8049 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8054 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8055 /*integral_constant_expression_p=*/false);
8057 q = &TREE_CHAIN (t);
8060 p = &TREE_CHAIN (t);
8063 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8067 /* Perform (or defer) access check for typedefs that were referenced
8068 from within the template TMPL code.
8069 This is a subroutine of instantiate_template and instantiate_class_template.
8070 TMPL is the template to consider and TARGS is the list of arguments of
8074 perform_typedefs_access_check (tree tmpl, tree targs)
8076 location_t saved_location;
8078 qualified_typedef_usage_t *iter;
8081 || (!CLASS_TYPE_P (tmpl)
8082 && TREE_CODE (tmpl) != FUNCTION_DECL))
8085 saved_location = input_location;
8086 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
8087 get_types_needing_access_check (tmpl),
8090 tree type_decl = iter->typedef_decl;
8091 tree type_scope = iter->context;
8093 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8096 if (uses_template_parms (type_decl))
8097 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8098 if (uses_template_parms (type_scope))
8099 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8101 /* Make access check error messages point to the location
8102 of the use of the typedef. */
8103 input_location = iter->locus;
8104 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8105 type_decl, type_decl);
8107 input_location = saved_location;
8111 instantiate_class_template (tree type)
8113 tree templ, args, pattern, t, member;
8117 unsigned int saved_maximum_field_alignment;
8119 if (type == error_mark_node)
8120 return error_mark_node;
8122 if (COMPLETE_OR_OPEN_TYPE_P (type)
8123 || uses_template_parms (type))
8126 /* Figure out which template is being instantiated. */
8127 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8128 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8130 /* Determine what specialization of the original template to
8132 t = most_specialized_class (type, templ, tf_warning_or_error);
8133 if (t == error_mark_node)
8135 TYPE_BEING_DEFINED (type) = 1;
8136 return error_mark_node;
8140 /* This TYPE is actually an instantiation of a partial
8141 specialization. We replace the innermost set of ARGS with
8142 the arguments appropriate for substitution. For example,
8145 template <class T> struct S {};
8146 template <class T> struct S<T*> {};
8148 and supposing that we are instantiating S<int*>, ARGS will
8149 presently be {int*} -- but we need {int}. */
8150 pattern = TREE_TYPE (t);
8151 args = TREE_PURPOSE (t);
8155 pattern = TREE_TYPE (templ);
8156 args = CLASSTYPE_TI_ARGS (type);
8159 /* If the template we're instantiating is incomplete, then clearly
8160 there's nothing we can do. */
8161 if (!COMPLETE_TYPE_P (pattern))
8164 /* If we've recursively instantiated too many templates, stop. */
8165 if (! push_tinst_level (type))
8168 /* Now we're really doing the instantiation. Mark the type as in
8169 the process of being defined. */
8170 TYPE_BEING_DEFINED (type) = 1;
8172 /* We may be in the middle of deferred access check. Disable
8174 push_deferring_access_checks (dk_no_deferred);
8176 push_to_top_level ();
8177 /* Use #pragma pack from the template context. */
8178 saved_maximum_field_alignment = maximum_field_alignment;
8179 maximum_field_alignment = TYPE_PRECISION (pattern);
8181 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8183 /* Set the input location to the most specialized template definition.
8184 This is needed if tsubsting causes an error. */
8185 typedecl = TYPE_MAIN_DECL (pattern);
8186 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8187 DECL_SOURCE_LOCATION (typedecl);
8189 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
8190 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
8191 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
8192 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
8193 TYPE_HAS_COPY_ASSIGN (type) = TYPE_HAS_COPY_ASSIGN (pattern);
8194 TYPE_HAS_CONST_COPY_ASSIGN (type) = TYPE_HAS_CONST_COPY_ASSIGN (pattern);
8195 TYPE_HAS_COPY_CTOR (type) = TYPE_HAS_COPY_CTOR (pattern);
8196 TYPE_HAS_CONST_COPY_CTOR (type) = TYPE_HAS_CONST_COPY_CTOR (pattern);
8197 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
8198 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
8199 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8200 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8201 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8202 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8203 if (ANON_AGGR_TYPE_P (pattern))
8204 SET_ANON_AGGR_TYPE_P (type);
8205 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8207 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8208 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8211 pbinfo = TYPE_BINFO (pattern);
8213 /* We should never instantiate a nested class before its enclosing
8214 class; we need to look up the nested class by name before we can
8215 instantiate it, and that lookup should instantiate the enclosing
8217 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8218 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8220 base_list = NULL_TREE;
8221 if (BINFO_N_BASE_BINFOS (pbinfo))
8227 /* We must enter the scope containing the type, as that is where
8228 the accessibility of types named in dependent bases are
8230 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8232 /* Substitute into each of the bases to determine the actual
8234 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8237 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8238 tree expanded_bases = NULL_TREE;
8241 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8244 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8245 args, tf_error, NULL_TREE);
8246 if (expanded_bases == error_mark_node)
8249 len = TREE_VEC_LENGTH (expanded_bases);
8252 for (idx = 0; idx < len; idx++)
8255 /* Extract the already-expanded base class. */
8256 base = TREE_VEC_ELT (expanded_bases, idx);
8258 /* Substitute to figure out the base class. */
8259 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8262 if (base == error_mark_node)
8265 base_list = tree_cons (access, base, base_list);
8266 if (BINFO_VIRTUAL_P (pbase_binfo))
8267 TREE_TYPE (base_list) = integer_type_node;
8271 /* The list is now in reverse order; correct that. */
8272 base_list = nreverse (base_list);
8275 pop_scope (pushed_scope);
8277 /* Now call xref_basetypes to set up all the base-class
8279 xref_basetypes (type, base_list);
8281 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8282 (int) ATTR_FLAG_TYPE_IN_PLACE,
8283 args, tf_error, NULL_TREE);
8284 fixup_attribute_variants (type);
8286 /* Now that our base classes are set up, enter the scope of the
8287 class, so that name lookups into base classes, etc. will work
8288 correctly. This is precisely analogous to what we do in
8289 begin_class_definition when defining an ordinary non-template
8290 class, except we also need to push the enclosing classes. */
8291 push_nested_class (type);
8293 /* Now members are processed in the order of declaration. */
8294 for (member = CLASSTYPE_DECL_LIST (pattern);
8295 member; member = TREE_CHAIN (member))
8297 tree t = TREE_VALUE (member);
8299 if (TREE_PURPOSE (member))
8303 /* Build new CLASSTYPE_NESTED_UTDS. */
8306 bool class_template_p;
8308 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8309 && TYPE_LANG_SPECIFIC (t)
8310 && CLASSTYPE_IS_TEMPLATE (t));
8311 /* If the member is a class template, then -- even after
8312 substitution -- there may be dependent types in the
8313 template argument list for the class. We increment
8314 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8315 that function will assume that no types are dependent
8316 when outside of a template. */
8317 if (class_template_p)
8318 ++processing_template_decl;
8319 newtag = tsubst (t, args, tf_error, NULL_TREE);
8320 if (class_template_p)
8321 --processing_template_decl;
8322 if (newtag == error_mark_node)
8325 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8327 tree name = TYPE_IDENTIFIER (t);
8329 if (class_template_p)
8330 /* Unfortunately, lookup_template_class sets
8331 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8332 instantiation (i.e., for the type of a member
8333 template class nested within a template class.)
8334 This behavior is required for
8335 maybe_process_partial_specialization to work
8336 correctly, but is not accurate in this case;
8337 the TAG is not an instantiation of anything.
8338 (The corresponding TEMPLATE_DECL is an
8339 instantiation, but the TYPE is not.) */
8340 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8342 /* Now, we call pushtag to put this NEWTAG into the scope of
8343 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8344 pushtag calling push_template_decl. We don't have to do
8345 this for enums because it will already have been done in
8348 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8349 pushtag (name, newtag, /*tag_scope=*/ts_current);
8352 else if (TREE_CODE (t) == FUNCTION_DECL
8353 || DECL_FUNCTION_TEMPLATE_P (t))
8355 /* Build new TYPE_METHODS. */
8358 if (TREE_CODE (t) == TEMPLATE_DECL)
8359 ++processing_template_decl;
8360 r = tsubst (t, args, tf_error, NULL_TREE);
8361 if (TREE_CODE (t) == TEMPLATE_DECL)
8362 --processing_template_decl;
8363 set_current_access_from_decl (r);
8364 finish_member_declaration (r);
8368 /* Build new TYPE_FIELDS. */
8369 if (TREE_CODE (t) == STATIC_ASSERT)
8372 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8373 tf_warning_or_error, NULL_TREE,
8374 /*integral_constant_expression_p=*/true);
8375 finish_static_assert (condition,
8376 STATIC_ASSERT_MESSAGE (t),
8377 STATIC_ASSERT_SOURCE_LOCATION (t),
8380 else if (TREE_CODE (t) != CONST_DECL)
8384 /* The file and line for this declaration, to
8385 assist in error message reporting. Since we
8386 called push_tinst_level above, we don't need to
8388 input_location = DECL_SOURCE_LOCATION (t);
8390 if (TREE_CODE (t) == TEMPLATE_DECL)
8391 ++processing_template_decl;
8392 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8393 if (TREE_CODE (t) == TEMPLATE_DECL)
8394 --processing_template_decl;
8395 if (TREE_CODE (r) == VAR_DECL)
8399 [t]he initialization (and any associated
8400 side-effects) of a static data member does
8401 not occur unless the static data member is
8402 itself used in a way that requires the
8403 definition of the static data member to
8406 Therefore, we do not substitute into the
8407 initialized for the static data member here. */
8408 finish_static_data_member_decl
8411 /*init_const_expr_p=*/false,
8412 /*asmspec_tree=*/NULL_TREE,
8414 if (DECL_INITIALIZED_IN_CLASS_P (r))
8415 check_static_variable_definition (r, TREE_TYPE (r));
8417 else if (TREE_CODE (r) == FIELD_DECL)
8419 /* Determine whether R has a valid type and can be
8420 completed later. If R is invalid, then it is
8421 replaced by error_mark_node so that it will not be
8422 added to TYPE_FIELDS. */
8423 tree rtype = TREE_TYPE (r);
8424 if (can_complete_type_without_circularity (rtype))
8425 complete_type (rtype);
8427 if (!COMPLETE_TYPE_P (rtype))
8429 cxx_incomplete_type_error (r, rtype);
8430 r = error_mark_node;
8434 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8435 such a thing will already have been added to the field
8436 list by tsubst_enum in finish_member_declaration in the
8437 CLASSTYPE_NESTED_UTDS case above. */
8438 if (!(TREE_CODE (r) == TYPE_DECL
8439 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
8440 && DECL_ARTIFICIAL (r)))
8442 set_current_access_from_decl (r);
8443 finish_member_declaration (r);
8450 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
8452 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8454 tree friend_type = t;
8455 bool adjust_processing_template_decl = false;
8457 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8459 /* template <class T> friend class C; */
8460 friend_type = tsubst_friend_class (friend_type, args);
8461 adjust_processing_template_decl = true;
8463 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
8465 /* template <class T> friend class C::D; */
8466 friend_type = tsubst (friend_type, args,
8467 tf_warning_or_error, NULL_TREE);
8468 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8469 friend_type = TREE_TYPE (friend_type);
8470 adjust_processing_template_decl = true;
8472 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
8474 /* This could be either
8478 when dependent_type_p is false or
8480 template <class U> friend class T::C;
8483 friend_type = tsubst (friend_type, args,
8484 tf_warning_or_error, NULL_TREE);
8485 /* Bump processing_template_decl for correct
8486 dependent_type_p calculation. */
8487 ++processing_template_decl;
8488 if (dependent_type_p (friend_type))
8489 adjust_processing_template_decl = true;
8490 --processing_template_decl;
8492 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
8493 && hidden_name_p (TYPE_NAME (friend_type)))
8497 where C hasn't been declared yet. Let's lookup name
8498 from namespace scope directly, bypassing any name that
8499 come from dependent base class. */
8500 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
8502 /* The call to xref_tag_from_type does injection for friend
8504 push_nested_namespace (ns);
8506 xref_tag_from_type (friend_type, NULL_TREE,
8507 /*tag_scope=*/ts_current);
8508 pop_nested_namespace (ns);
8510 else if (uses_template_parms (friend_type))
8511 /* friend class C<T>; */
8512 friend_type = tsubst (friend_type, args,
8513 tf_warning_or_error, NULL_TREE);
8518 where C is already declared or
8520 friend class C<int>;
8522 We don't have to do anything in these cases. */
8524 if (adjust_processing_template_decl)
8525 /* Trick make_friend_class into realizing that the friend
8526 we're adding is a template, not an ordinary class. It's
8527 important that we use make_friend_class since it will
8528 perform some error-checking and output cross-reference
8530 ++processing_template_decl;
8532 if (friend_type != error_mark_node)
8533 make_friend_class (type, friend_type, /*complain=*/false);
8535 if (adjust_processing_template_decl)
8536 --processing_template_decl;
8540 /* Build new DECL_FRIENDLIST. */
8543 /* The file and line for this declaration, to
8544 assist in error message reporting. Since we
8545 called push_tinst_level above, we don't need to
8547 input_location = DECL_SOURCE_LOCATION (t);
8549 if (TREE_CODE (t) == TEMPLATE_DECL)
8551 ++processing_template_decl;
8552 push_deferring_access_checks (dk_no_check);
8555 r = tsubst_friend_function (t, args);
8556 add_friend (type, r, /*complain=*/false);
8557 if (TREE_CODE (t) == TEMPLATE_DECL)
8559 pop_deferring_access_checks ();
8560 --processing_template_decl;
8566 /* Set the file and line number information to whatever is given for
8567 the class itself. This puts error messages involving generated
8568 implicit functions at a predictable point, and the same point
8569 that would be used for non-template classes. */
8570 input_location = DECL_SOURCE_LOCATION (typedecl);
8572 unreverse_member_declarations (type);
8573 finish_struct_1 (type);
8574 TYPE_BEING_DEFINED (type) = 0;
8576 /* We don't instantiate default arguments for member functions. 14.7.1:
8578 The implicit instantiation of a class template specialization causes
8579 the implicit instantiation of the declarations, but not of the
8580 definitions or default arguments, of the class member functions,
8581 member classes, static data members and member templates.... */
8583 /* Some typedefs referenced from within the template code need to be access
8584 checked at template instantiation time, i.e now. These types were
8585 added to the template at parsing time. Let's get those and perform
8586 the access checks then. */
8587 perform_typedefs_access_check (pattern, args);
8588 perform_deferred_access_checks ();
8589 pop_nested_class ();
8590 maximum_field_alignment = saved_maximum_field_alignment;
8591 pop_from_top_level ();
8592 pop_deferring_access_checks ();
8595 /* The vtable for a template class can be emitted in any translation
8596 unit in which the class is instantiated. When there is no key
8597 method, however, finish_struct_1 will already have added TYPE to
8598 the keyed_classes list. */
8599 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
8600 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
8606 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8612 else if (TYPE_P (t))
8613 r = tsubst (t, args, complain, in_decl);
8616 if (!(complain & tf_warning))
8617 ++c_inhibit_evaluation_warnings;
8618 r = tsubst_expr (t, args, complain, in_decl,
8619 /*integral_constant_expression_p=*/true);
8620 if (!(complain & tf_warning))
8621 --c_inhibit_evaluation_warnings;
8626 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8627 NONTYPE_ARGUMENT_PACK. */
8630 make_fnparm_pack (tree spec_parm)
8632 /* Collect all of the extra "packed" parameters into an
8636 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
8637 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
8638 int i, len = list_length (spec_parm);
8640 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8641 parmvec = make_tree_vec (len);
8642 parmtypevec = make_tree_vec (len);
8643 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
8645 TREE_VEC_ELT (parmvec, i) = spec_parm;
8646 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
8649 /* Build the argument packs. */
8650 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
8651 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
8652 TREE_TYPE (argpack) = argtypepack;
8657 /* Substitute ARGS into T, which is an pack expansion
8658 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8659 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8660 (if only a partial substitution could be performed) or
8661 ERROR_MARK_NODE if there was an error. */
8663 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
8667 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
8671 htab_t saved_local_specializations = NULL;
8673 gcc_assert (PACK_EXPANSION_P (t));
8674 pattern = PACK_EXPANSION_PATTERN (t);
8676 /* Determine the argument packs that will instantiate the parameter
8677 packs used in the expansion expression. While we're at it,
8678 compute the number of arguments to be expanded and make sure it
8680 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
8681 pack = TREE_CHAIN (pack))
8683 tree parm_pack = TREE_VALUE (pack);
8684 tree arg_pack = NULL_TREE;
8685 tree orig_arg = NULL_TREE;
8687 if (TREE_CODE (parm_pack) == PARM_DECL)
8689 if (!cp_unevaluated_operand)
8690 arg_pack = retrieve_local_specialization (parm_pack);
8693 /* We can't rely on local_specializations for a parameter
8694 name used later in a function declaration (such as in a
8695 late-specified return type). Even if it exists, it might
8696 have the wrong value for a recursive call. Just make a
8697 dummy decl, since it's only used for its type. */
8698 arg_pack = tsubst_decl (parm_pack, args, complain);
8699 arg_pack = make_fnparm_pack (arg_pack);
8704 int level, idx, levels;
8705 template_parm_level_and_index (parm_pack, &level, &idx);
8707 levels = TMPL_ARGS_DEPTH (args);
8708 if (level <= levels)
8709 arg_pack = TMPL_ARG (args, level, idx);
8712 orig_arg = arg_pack;
8713 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
8714 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
8716 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
8717 /* This can only happen if we forget to expand an argument
8718 pack somewhere else. Just return an error, silently. */
8720 result = make_tree_vec (1);
8721 TREE_VEC_ELT (result, 0) = error_mark_node;
8725 /* For clarity in the comments below let's use the
8726 representation 'argument_pack<elements>' to denote an
8727 argument pack and its elements.
8729 In the 'if' block below, we want to detect cases where
8730 ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to
8731 check if ARG_PACK is an argument pack which sole element is
8732 the expansion of PARM_PACK. That argument pack is typically
8733 created by template_parm_to_arg when passed a parameter
8736 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
8737 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
8739 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
8740 tree pattern = PACK_EXPANSION_PATTERN (expansion);
8741 /* So we have an argument_pack<P...>. We want to test if P
8742 is actually PARM_PACK. We will not use cp_tree_equal to
8743 test P and PARM_PACK because during type fixup (by
8744 fixup_template_parm) P can be a pre-fixup version of a
8745 type and PARM_PACK be its post-fixup version.
8746 cp_tree_equal would consider them as different even
8747 though we would want to consider them compatible for our
8748 precise purpose here.
8750 Thus we are going to consider that P and PARM_PACK are
8751 compatible if they have the same DECL. */
8752 if ((/* If ARG_PACK is a type parameter pack named by the
8753 same DECL as parm_pack ... */
8755 && TYPE_P (parm_pack)
8756 && TYPE_NAME (pattern) == TYPE_NAME (parm_pack))
8757 /* ... or if ARG_PACK is a non-type parameter
8758 named by the same DECL as parm_pack ... */
8759 || (TREE_CODE (pattern) == TEMPLATE_PARM_INDEX
8760 && TREE_CODE (parm_pack) == PARM_DECL
8761 && TEMPLATE_PARM_DECL (pattern)
8762 == TEMPLATE_PARM_DECL (DECL_INITIAL (parm_pack))))
8763 && template_parameter_pack_p (pattern))
8764 /* ... then the argument pack that the parameter maps to
8765 is just an expansion of the parameter itself, such as
8766 one would find in the implicit typedef of a class
8767 inside the class itself. Consider this parameter
8768 "unsubstituted", so that we will maintain the outer
8770 arg_pack = NULL_TREE;
8776 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
8778 /* It's all-or-nothing with incomplete argument packs. */
8779 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8780 return error_mark_node;
8782 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8787 else if (len != my_len)
8790 /* We got explicit args for some packs but not others;
8791 do nothing now and try again after deduction. */
8793 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
8794 error ("mismatched argument pack lengths while expanding "
8798 error ("mismatched argument pack lengths while expanding "
8801 return error_mark_node;
8804 /* Keep track of the parameter packs and their corresponding
8806 packs = tree_cons (parm_pack, arg_pack, packs);
8807 TREE_TYPE (packs) = orig_arg;
8810 /* We can't substitute for this parameter pack. */
8811 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
8813 unsubstituted_packs);
8816 /* We cannot expand this expansion expression, because we don't have
8817 all of the argument packs we need. Substitute into the pattern
8818 and return a PACK_EXPANSION_*. The caller will need to deal with
8820 if (unsubstituted_packs)
8823 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8824 new_pat = tsubst_expr (pattern, args, complain, in_decl,
8825 /*integral_constant_expression_p=*/false);
8827 new_pat = tsubst (pattern, args, complain, in_decl);
8828 return make_pack_expansion (new_pat);
8831 /* We could not find any argument packs that work. */
8833 return error_mark_node;
8835 if (cp_unevaluated_operand)
8837 /* We're in a late-specified return type, so create our own local
8838 specializations table; the current table is either NULL or (in the
8839 case of recursive unification) might have bindings that we don't
8840 want to use or alter. */
8841 saved_local_specializations = local_specializations;
8842 local_specializations = htab_create (37,
8843 hash_local_specialization,
8844 eq_local_specializations,
8848 /* For each argument in each argument pack, substitute into the
8850 result = make_tree_vec (len + incomplete);
8851 for (i = 0; i < len + incomplete; ++i)
8853 /* For parameter pack, change the substitution of the parameter
8854 pack to the ith argument in its argument pack, then expand
8856 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8858 tree parm = TREE_PURPOSE (pack);
8860 if (TREE_CODE (parm) == PARM_DECL)
8862 /* Select the Ith argument from the pack. */
8863 tree arg = make_node (ARGUMENT_PACK_SELECT);
8864 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
8865 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
8867 register_local_specialization (arg, parm);
8873 template_parm_level_and_index (parm, &level, &idx);
8877 /* Select the Ith argument from the pack. */
8878 value = make_node (ARGUMENT_PACK_SELECT);
8879 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
8880 ARGUMENT_PACK_SELECT_INDEX (value) = i;
8883 /* Update the corresponding argument. */
8884 TMPL_ARG (args, level, idx) = value;
8888 /* Substitute into the PATTERN with the altered arguments. */
8889 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8890 TREE_VEC_ELT (result, i) =
8891 tsubst_expr (pattern, args, complain, in_decl,
8892 /*integral_constant_expression_p=*/false);
8894 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
8897 /* When we have incomplete argument packs, the last "expanded"
8898 result is itself a pack expansion, which allows us
8899 to deduce more arguments. */
8900 TREE_VEC_ELT (result, i) =
8901 make_pack_expansion (TREE_VEC_ELT (result, i));
8903 if (TREE_VEC_ELT (result, i) == error_mark_node)
8905 result = error_mark_node;
8910 /* Update ARGS to restore the substitution from parameter packs to
8911 their argument packs. */
8912 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8914 tree parm = TREE_PURPOSE (pack);
8916 if (TREE_CODE (parm) == PARM_DECL)
8917 register_local_specialization (TREE_TYPE (pack), parm);
8921 template_parm_level_and_index (parm, &level, &idx);
8923 /* Update the corresponding argument. */
8924 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
8925 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
8928 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
8932 if (saved_local_specializations)
8934 htab_delete (local_specializations);
8935 local_specializations = saved_local_specializations;
8941 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8942 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8943 parameter packs; all parms generated from a function parameter pack will
8944 have the same DECL_PARM_INDEX. */
8947 get_pattern_parm (tree parm, tree tmpl)
8949 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
8952 if (DECL_ARTIFICIAL (parm))
8954 for (patparm = DECL_ARGUMENTS (pattern);
8955 patparm; patparm = DECL_CHAIN (patparm))
8956 if (DECL_ARTIFICIAL (patparm)
8957 && DECL_NAME (parm) == DECL_NAME (patparm))
8962 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
8963 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
8964 gcc_assert (DECL_PARM_INDEX (patparm)
8965 == DECL_PARM_INDEX (parm));
8971 /* Substitute ARGS into the vector or list of template arguments T. */
8974 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8977 int len, need_new = 0, i, expanded_len_adjust = 0, out;
8980 if (t == error_mark_node)
8981 return error_mark_node;
8983 len = TREE_VEC_LENGTH (t);
8984 elts = XALLOCAVEC (tree, len);
8986 for (i = 0; i < len; i++)
8988 tree orig_arg = TREE_VEC_ELT (t, i);
8991 if (TREE_CODE (orig_arg) == TREE_VEC)
8992 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
8993 else if (PACK_EXPANSION_P (orig_arg))
8995 /* Substitute into an expansion expression. */
8996 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
8998 if (TREE_CODE (new_arg) == TREE_VEC)
8999 /* Add to the expanded length adjustment the number of
9000 expanded arguments. We subtract one from this
9001 measurement, because the argument pack expression
9002 itself is already counted as 1 in
9003 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9004 the argument pack is empty. */
9005 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9007 else if (ARGUMENT_PACK_P (orig_arg))
9009 /* Substitute into each of the arguments. */
9010 new_arg = TYPE_P (orig_arg)
9011 ? cxx_make_type (TREE_CODE (orig_arg))
9012 : make_node (TREE_CODE (orig_arg));
9014 SET_ARGUMENT_PACK_ARGS (
9016 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9017 args, complain, in_decl));
9019 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9020 new_arg = error_mark_node;
9022 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9023 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9025 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9027 if (TREE_TYPE (new_arg) == error_mark_node)
9028 new_arg = error_mark_node;
9032 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9034 if (new_arg == error_mark_node)
9035 return error_mark_node;
9038 if (new_arg != orig_arg)
9045 /* Make space for the expanded arguments coming from template
9047 t = make_tree_vec (len + expanded_len_adjust);
9048 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9049 arguments for a member template.
9050 In that case each TREE_VEC in ORIG_T represents a level of template
9051 arguments, and ORIG_T won't carry any non defaulted argument count.
9052 It will rather be the nested TREE_VECs that will carry one.
9053 In other words, ORIG_T carries a non defaulted argument count only
9054 if it doesn't contain any nested TREE_VEC. */
9055 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9057 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9058 count += expanded_len_adjust;
9059 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9061 for (i = 0, out = 0; i < len; i++)
9063 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9064 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9065 && TREE_CODE (elts[i]) == TREE_VEC)
9069 /* Now expand the template argument pack "in place". */
9070 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9071 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9075 TREE_VEC_ELT (t, out) = elts[i];
9083 /* Return the result of substituting ARGS into the template parameters
9084 given by PARMS. If there are m levels of ARGS and m + n levels of
9085 PARMS, then the result will contain n levels of PARMS. For
9086 example, if PARMS is `template <class T> template <class U>
9087 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9088 result will be `template <int*, double, class V>'. */
9091 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9096 /* When substituting into a template, we must set
9097 PROCESSING_TEMPLATE_DECL as the template parameters may be
9098 dependent if they are based on one-another, and the dependency
9099 predicates are short-circuit outside of templates. */
9100 ++processing_template_decl;
9102 for (new_parms = &r;
9103 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9104 new_parms = &(TREE_CHAIN (*new_parms)),
9105 parms = TREE_CHAIN (parms))
9108 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9111 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9115 if (parms == error_mark_node)
9118 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9120 if (tuple == error_mark_node)
9123 TREE_VEC_ELT (new_vec, i) =
9124 tsubst_template_parm (tuple, args, complain);
9128 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9129 - TMPL_ARGS_DEPTH (args)),
9130 new_vec, NULL_TREE);
9133 --processing_template_decl;
9138 /* Return the result of substituting ARGS into one template parameter
9139 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9140 parameter and which TREE_PURPOSE is the default argument of the
9141 template parameter. */
9144 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9146 tree default_value, parm_decl;
9148 if (args == NULL_TREE
9150 || t == error_mark_node)
9153 gcc_assert (TREE_CODE (t) == TREE_LIST);
9155 default_value = TREE_PURPOSE (t);
9156 parm_decl = TREE_VALUE (t);
9158 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9159 if (TREE_CODE (parm_decl) == PARM_DECL
9160 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9161 parm_decl = error_mark_node;
9162 default_value = tsubst_template_arg (default_value, args,
9163 complain, NULL_TREE);
9165 return build_tree_list (default_value, parm_decl);
9168 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9169 type T. If T is not an aggregate or enumeration type, it is
9170 handled as if by tsubst. IN_DECL is as for tsubst. If
9171 ENTERING_SCOPE is nonzero, T is the context for a template which
9172 we are presently tsubst'ing. Return the substituted value. */
9175 tsubst_aggr_type (tree t,
9177 tsubst_flags_t complain,
9184 switch (TREE_CODE (t))
9187 if (TYPE_PTRMEMFUNC_P (t))
9188 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9190 /* Else fall through. */
9193 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9198 int saved_unevaluated_operand;
9199 int saved_inhibit_evaluation_warnings;
9201 /* In "sizeof(X<I>)" we need to evaluate "I". */
9202 saved_unevaluated_operand = cp_unevaluated_operand;
9203 cp_unevaluated_operand = 0;
9204 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9205 c_inhibit_evaluation_warnings = 0;
9207 /* First, determine the context for the type we are looking
9209 context = TYPE_CONTEXT (t);
9212 context = tsubst_aggr_type (context, args, complain,
9213 in_decl, /*entering_scope=*/1);
9214 /* If context is a nested class inside a class template,
9215 it may still need to be instantiated (c++/33959). */
9216 if (TYPE_P (context))
9217 context = complete_type (context);
9220 /* Then, figure out what arguments are appropriate for the
9221 type we are trying to find. For example, given:
9223 template <class T> struct S;
9224 template <class T, class U> void f(T, U) { S<U> su; }
9226 and supposing that we are instantiating f<int, double>,
9227 then our ARGS will be {int, double}, but, when looking up
9228 S we only want {double}. */
9229 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
9231 if (argvec == error_mark_node)
9232 r = error_mark_node;
9235 r = lookup_template_class (t, argvec, in_decl, context,
9236 entering_scope, complain);
9237 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
9240 cp_unevaluated_operand = saved_unevaluated_operand;
9241 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
9246 /* This is not a template type, so there's nothing to do. */
9250 return tsubst (t, args, complain, in_decl);
9254 /* Substitute into the default argument ARG (a default argument for
9255 FN), which has the indicated TYPE. */
9258 tsubst_default_argument (tree fn, tree type, tree arg)
9260 tree saved_class_ptr = NULL_TREE;
9261 tree saved_class_ref = NULL_TREE;
9263 /* This can happen in invalid code. */
9264 if (TREE_CODE (arg) == DEFAULT_ARG)
9267 /* This default argument came from a template. Instantiate the
9268 default argument here, not in tsubst. In the case of
9277 we must be careful to do name lookup in the scope of S<T>,
9278 rather than in the current class. */
9279 push_access_scope (fn);
9280 /* The "this" pointer is not valid in a default argument. */
9283 saved_class_ptr = current_class_ptr;
9284 cp_function_chain->x_current_class_ptr = NULL_TREE;
9285 saved_class_ref = current_class_ref;
9286 cp_function_chain->x_current_class_ref = NULL_TREE;
9289 push_deferring_access_checks(dk_no_deferred);
9290 /* The default argument expression may cause implicitly defined
9291 member functions to be synthesized, which will result in garbage
9292 collection. We must treat this situation as if we were within
9293 the body of function so as to avoid collecting live data on the
9296 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
9297 tf_warning_or_error, NULL_TREE,
9298 /*integral_constant_expression_p=*/false);
9300 pop_deferring_access_checks();
9302 /* Restore the "this" pointer. */
9305 cp_function_chain->x_current_class_ptr = saved_class_ptr;
9306 cp_function_chain->x_current_class_ref = saved_class_ref;
9309 /* Make sure the default argument is reasonable. */
9310 arg = check_default_argument (type, arg);
9312 pop_access_scope (fn);
9317 /* Substitute into all the default arguments for FN. */
9320 tsubst_default_arguments (tree fn)
9325 tmpl_args = DECL_TI_ARGS (fn);
9327 /* If this function is not yet instantiated, we certainly don't need
9328 its default arguments. */
9329 if (uses_template_parms (tmpl_args))
9331 /* Don't do this again for clones. */
9332 if (DECL_CLONED_FUNCTION_P (fn))
9335 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
9337 arg = TREE_CHAIN (arg))
9338 if (TREE_PURPOSE (arg))
9339 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
9341 TREE_PURPOSE (arg));
9344 /* Substitute the ARGS into the T, which is a _DECL. Return the
9345 result of the substitution. Issue error and warning messages under
9346 control of COMPLAIN. */
9349 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
9351 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9352 location_t saved_loc;
9357 /* Set the filename and linenumber to improve error-reporting. */
9358 saved_loc = input_location;
9359 input_location = DECL_SOURCE_LOCATION (t);
9361 switch (TREE_CODE (t))
9365 /* We can get here when processing a member function template,
9366 member class template, or template template parameter. */
9367 tree decl = DECL_TEMPLATE_RESULT (t);
9372 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9374 /* Template template parameter is treated here. */
9375 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9376 if (new_type == error_mark_node)
9377 RETURN (error_mark_node);
9380 DECL_CHAIN (r) = NULL_TREE;
9381 TREE_TYPE (r) = new_type;
9382 DECL_TEMPLATE_RESULT (r)
9383 = build_decl (DECL_SOURCE_LOCATION (decl),
9384 TYPE_DECL, DECL_NAME (decl), new_type);
9385 DECL_TEMPLATE_PARMS (r)
9386 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9388 TYPE_NAME (new_type) = r;
9392 /* We might already have an instance of this template.
9393 The ARGS are for the surrounding class type, so the
9394 full args contain the tsubst'd args for the context,
9395 plus the innermost args from the template decl. */
9396 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
9397 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
9398 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
9399 /* Because this is a template, the arguments will still be
9400 dependent, even after substitution. If
9401 PROCESSING_TEMPLATE_DECL is not set, the dependency
9402 predicates will short-circuit. */
9403 ++processing_template_decl;
9404 full_args = tsubst_template_args (tmpl_args, args,
9406 --processing_template_decl;
9407 if (full_args == error_mark_node)
9408 RETURN (error_mark_node);
9410 /* If this is a default template template argument,
9411 tsubst might not have changed anything. */
9412 if (full_args == tmpl_args)
9415 hash = hash_tmpl_and_args (t, full_args);
9416 spec = retrieve_specialization (t, full_args, hash);
9417 if (spec != NULL_TREE)
9423 /* Make a new template decl. It will be similar to the
9424 original, but will record the current template arguments.
9425 We also create a new function declaration, which is just
9426 like the old one, but points to this new template, rather
9427 than the old one. */
9429 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
9430 DECL_CHAIN (r) = NULL_TREE;
9432 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
9434 if (TREE_CODE (decl) == TYPE_DECL)
9437 ++processing_template_decl;
9438 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9439 --processing_template_decl;
9440 if (new_type == error_mark_node)
9441 RETURN (error_mark_node);
9443 TREE_TYPE (r) = new_type;
9444 CLASSTYPE_TI_TEMPLATE (new_type) = r;
9445 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
9446 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
9447 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
9452 ++processing_template_decl;
9453 new_decl = tsubst (decl, args, complain, in_decl);
9454 --processing_template_decl;
9455 if (new_decl == error_mark_node)
9456 RETURN (error_mark_node);
9458 DECL_TEMPLATE_RESULT (r) = new_decl;
9459 DECL_TI_TEMPLATE (new_decl) = r;
9460 TREE_TYPE (r) = TREE_TYPE (new_decl);
9461 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
9462 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
9465 SET_DECL_IMPLICIT_INSTANTIATION (r);
9466 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
9467 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
9469 /* The template parameters for this new template are all the
9470 template parameters for the old template, except the
9471 outermost level of parameters. */
9472 DECL_TEMPLATE_PARMS (r)
9473 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9476 if (PRIMARY_TEMPLATE_P (t))
9477 DECL_PRIMARY_TEMPLATE (r) = r;
9479 if (TREE_CODE (decl) != TYPE_DECL)
9480 /* Record this non-type partial instantiation. */
9481 register_specialization (r, t,
9482 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
9490 tree argvec = NULL_TREE;
9498 /* Nobody should be tsubst'ing into non-template functions. */
9499 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
9501 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
9506 /* If T is not dependent, just return it. We have to
9507 increment PROCESSING_TEMPLATE_DECL because
9508 value_dependent_expression_p assumes that nothing is
9509 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9510 ++processing_template_decl;
9511 dependent_p = value_dependent_expression_p (t);
9512 --processing_template_decl;
9516 /* Calculate the most general template of which R is a
9517 specialization, and the complete set of arguments used to
9519 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
9520 argvec = tsubst_template_args (DECL_TI_ARGS
9521 (DECL_TEMPLATE_RESULT
9522 (DECL_TI_TEMPLATE (t))),
9523 args, complain, in_decl);
9525 /* Check to see if we already have this specialization. */
9526 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9527 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9535 /* We can see more levels of arguments than parameters if
9536 there was a specialization of a member template, like
9539 template <class T> struct S { template <class U> void f(); }
9540 template <> template <class U> void S<int>::f(U);
9542 Here, we'll be substituting into the specialization,
9543 because that's where we can find the code we actually
9544 want to generate, but we'll have enough arguments for
9545 the most general template.
9547 We also deal with the peculiar case:
9549 template <class T> struct S {
9550 template <class U> friend void f();
9552 template <class U> void f() {}
9554 template void f<double>();
9556 Here, the ARGS for the instantiation of will be {int,
9557 double}. But, we only need as many ARGS as there are
9558 levels of template parameters in CODE_PATTERN. We are
9559 careful not to get fooled into reducing the ARGS in
9562 template <class T> struct S { template <class U> void f(U); }
9563 template <class T> template <> void S<T>::f(int) {}
9565 which we can spot because the pattern will be a
9566 specialization in this case. */
9567 args_depth = TMPL_ARGS_DEPTH (args);
9569 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
9570 if (args_depth > parms_depth
9571 && !DECL_TEMPLATE_SPECIALIZATION (t))
9572 args = get_innermost_template_args (args, parms_depth);
9576 /* This special case arises when we have something like this:
9578 template <class T> struct S {
9579 friend void f<int>(int, double);
9582 Here, the DECL_TI_TEMPLATE for the friend declaration
9583 will be an IDENTIFIER_NODE. We are being called from
9584 tsubst_friend_function, and we want only to create a
9585 new decl (R) with appropriate types so that we can call
9586 determine_specialization. */
9587 gen_tmpl = NULL_TREE;
9590 if (DECL_CLASS_SCOPE_P (t))
9592 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
9596 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
9597 complain, t, /*entering_scope=*/1);
9602 ctx = DECL_CONTEXT (t);
9604 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9605 if (type == error_mark_node)
9606 RETURN (error_mark_node);
9608 /* We do NOT check for matching decls pushed separately at this
9609 point, as they may not represent instantiations of this
9610 template, and in any case are considered separate under the
9613 DECL_USE_TEMPLATE (r) = 0;
9614 TREE_TYPE (r) = type;
9615 /* Clear out the mangled name and RTL for the instantiation. */
9616 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9617 SET_DECL_RTL (r, NULL);
9618 /* Leave DECL_INITIAL set on deleted instantiations. */
9619 if (!DECL_DELETED_FN (r))
9620 DECL_INITIAL (r) = NULL_TREE;
9621 DECL_CONTEXT (r) = ctx;
9623 if (member && DECL_CONV_FN_P (r))
9624 /* Type-conversion operator. Reconstruct the name, in
9625 case it's the name of one of the template's parameters. */
9626 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
9628 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
9630 DECL_RESULT (r) = NULL_TREE;
9632 TREE_STATIC (r) = 0;
9633 TREE_PUBLIC (r) = TREE_PUBLIC (t);
9634 DECL_EXTERNAL (r) = 1;
9635 /* If this is an instantiation of a function with internal
9636 linkage, we already know what object file linkage will be
9637 assigned to the instantiation. */
9638 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
9639 DECL_DEFER_OUTPUT (r) = 0;
9640 DECL_CHAIN (r) = NULL_TREE;
9641 DECL_PENDING_INLINE_INFO (r) = 0;
9642 DECL_PENDING_INLINE_P (r) = 0;
9643 DECL_SAVED_TREE (r) = NULL_TREE;
9644 DECL_STRUCT_FUNCTION (r) = NULL;
9646 /* We'll re-clone as appropriate in instantiate_template. */
9647 DECL_CLONED_FUNCTION (r) = NULL_TREE;
9649 /* If we aren't complaining now, return on error before we register
9650 the specialization so that we'll complain eventually. */
9651 if ((complain & tf_error) == 0
9652 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9653 && !grok_op_properties (r, /*complain=*/false))
9654 RETURN (error_mark_node);
9656 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
9657 this in the special friend case mentioned above where
9658 GEN_TMPL is NULL. */
9661 DECL_TEMPLATE_INFO (r)
9662 = build_template_info (gen_tmpl, argvec);
9663 SET_DECL_IMPLICIT_INSTANTIATION (r);
9664 register_specialization (r, gen_tmpl, argvec, false, hash);
9666 /* We're not supposed to instantiate default arguments
9667 until they are called, for a template. But, for a
9670 template <class T> void f ()
9671 { extern void g(int i = T()); }
9673 we should do the substitution when the template is
9674 instantiated. We handle the member function case in
9675 instantiate_class_template since the default arguments
9676 might refer to other members of the class. */
9678 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9679 && !uses_template_parms (argvec))
9680 tsubst_default_arguments (r);
9683 DECL_TEMPLATE_INFO (r) = NULL_TREE;
9685 /* Copy the list of befriending classes. */
9686 for (friends = &DECL_BEFRIENDING_CLASSES (r);
9688 friends = &TREE_CHAIN (*friends))
9690 *friends = copy_node (*friends);
9691 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
9696 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
9698 maybe_retrofit_in_chrg (r);
9699 if (DECL_CONSTRUCTOR_P (r))
9700 grok_ctor_properties (ctx, r);
9701 /* If this is an instantiation of a member template, clone it.
9702 If it isn't, that'll be handled by
9703 clone_constructors_and_destructors. */
9704 if (PRIMARY_TEMPLATE_P (gen_tmpl))
9705 clone_function_decl (r, /*update_method_vec_p=*/0);
9707 else if ((complain & tf_error) != 0
9708 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9709 && !grok_op_properties (r, /*complain=*/true))
9710 RETURN (error_mark_node);
9712 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
9713 SET_DECL_FRIEND_CONTEXT (r,
9714 tsubst (DECL_FRIEND_CONTEXT (t),
9715 args, complain, in_decl));
9717 /* Possibly limit visibility based on template args. */
9718 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9719 if (DECL_VISIBILITY_SPECIFIED (t))
9721 DECL_VISIBILITY_SPECIFIED (r) = 0;
9723 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9725 determine_visibility (r);
9726 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
9727 && !processing_template_decl)
9728 defaulted_late_check (r);
9730 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9731 args, complain, in_decl);
9737 tree type = NULL_TREE;
9739 tree expanded_types = NULL_TREE;
9740 tree prev_r = NULL_TREE;
9741 tree first_r = NULL_TREE;
9743 if (FUNCTION_PARAMETER_PACK_P (t))
9745 /* If there is a local specialization that isn't a
9746 parameter pack, it means that we're doing a "simple"
9747 substitution from inside tsubst_pack_expansion. Just
9748 return the local specialization (which will be a single
9750 tree spec = retrieve_local_specialization (t);
9752 && TREE_CODE (spec) == PARM_DECL
9753 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
9756 /* Expand the TYPE_PACK_EXPANSION that provides the types for
9757 the parameters in this function parameter pack. */
9758 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
9760 if (TREE_CODE (expanded_types) == TREE_VEC)
9762 len = TREE_VEC_LENGTH (expanded_types);
9764 /* Zero-length parameter packs are boring. Just substitute
9767 RETURN (tsubst (TREE_CHAIN (t), args, complain,
9772 /* All we did was update the type. Make a note of that. */
9773 type = expanded_types;
9774 expanded_types = NULL_TREE;
9778 /* Loop through all of the parameter's we'll build. When T is
9779 a function parameter pack, LEN is the number of expanded
9780 types in EXPANDED_TYPES; otherwise, LEN is 1. */
9782 for (i = 0; i < len; ++i)
9786 if (DECL_TEMPLATE_PARM_P (t))
9787 SET_DECL_TEMPLATE_PARM_P (r);
9789 /* An argument of a function parameter pack is not a parameter
9791 FUNCTION_PARAMETER_PACK_P (r) = false;
9794 /* We're on the Ith parameter of the function parameter
9797 /* Get the Ith type. */
9798 type = TREE_VEC_ELT (expanded_types, i);
9801 /* Rename the parameter to include the index. */
9803 make_ith_pack_parameter_name (DECL_NAME (r), i);
9806 /* We're dealing with a normal parameter. */
9807 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9809 type = type_decays_to (type);
9810 TREE_TYPE (r) = type;
9811 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9813 if (DECL_INITIAL (r))
9815 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
9816 DECL_INITIAL (r) = TREE_TYPE (r);
9818 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
9822 DECL_CONTEXT (r) = NULL_TREE;
9824 if (!DECL_TEMPLATE_PARM_P (r))
9825 DECL_ARG_TYPE (r) = type_passed_as (type);
9827 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9828 args, complain, in_decl);
9830 /* Keep track of the first new parameter we
9831 generate. That's what will be returned to the
9836 /* Build a proper chain of parameters when substituting
9837 into a function parameter pack. */
9839 DECL_CHAIN (prev_r) = r;
9843 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
9844 complain, DECL_CHAIN (t));
9846 /* FIRST_R contains the start of the chain we've built. */
9856 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9857 if (type == error_mark_node)
9858 RETURN (error_mark_node);
9859 TREE_TYPE (r) = type;
9860 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9862 /* DECL_INITIAL gives the number of bits in a bit-field. */
9864 = tsubst_expr (DECL_INITIAL (t), args,
9866 /*integral_constant_expression_p=*/true);
9867 /* We don't have to set DECL_CONTEXT here; it is set by
9868 finish_member_declaration. */
9869 DECL_CHAIN (r) = NULL_TREE;
9870 if (VOID_TYPE_P (type))
9871 error ("instantiation of %q+D as type %qT", r, type);
9873 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9874 args, complain, in_decl);
9879 /* We reach here only for member using decls. */
9880 if (DECL_DEPENDENT_P (t))
9882 r = do_class_using_decl
9883 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
9884 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
9886 r = error_mark_node;
9889 TREE_PROTECTED (r) = TREE_PROTECTED (t);
9890 TREE_PRIVATE (r) = TREE_PRIVATE (t);
9896 DECL_CHAIN (r) = NULL_TREE;
9903 tree argvec = NULL_TREE;
9904 tree gen_tmpl = NULL_TREE;
9906 tree tmpl = NULL_TREE;
9908 tree type = NULL_TREE;
9911 if (TREE_CODE (t) == TYPE_DECL
9912 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
9914 /* If this is the canonical decl, we don't have to
9915 mess with instantiations, and often we can't (for
9916 typename, template type parms and such). Note that
9917 TYPE_NAME is not correct for the above test if
9918 we've copied the type for a typedef. */
9919 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9920 if (type == error_mark_node)
9921 RETURN (error_mark_node);
9922 r = TYPE_NAME (type);
9926 /* Check to see if we already have the specialization we
9929 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
9931 /* T is a static data member or namespace-scope entity.
9932 We have to substitute into namespace-scope variables
9933 (even though such entities are never templates) because
9936 template <class T> void f() { extern T t; }
9938 where the entity referenced is not known until
9939 instantiation time. */
9941 ctx = DECL_CONTEXT (t);
9942 if (DECL_CLASS_SCOPE_P (t))
9944 ctx = tsubst_aggr_type (ctx, args,
9946 in_decl, /*entering_scope=*/1);
9947 /* If CTX is unchanged, then T is in fact the
9948 specialization we want. That situation occurs when
9949 referencing a static data member within in its own
9950 class. We can use pointer equality, rather than
9951 same_type_p, because DECL_CONTEXT is always
9953 if (ctx == DECL_CONTEXT (t))
9959 tmpl = DECL_TI_TEMPLATE (t);
9960 gen_tmpl = most_general_template (tmpl);
9961 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
9962 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9963 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9968 /* A local variable. */
9970 /* Subsequent calls to pushdecl will fill this in. */
9972 spec = retrieve_local_specialization (t);
9974 /* If we already have the specialization we need, there is
9975 nothing more to do. */
9982 /* Create a new node for the specialization we need. */
9984 if (type == NULL_TREE)
9986 if (is_typedef_decl (t))
9987 type = DECL_ORIGINAL_TYPE (t);
9989 type = TREE_TYPE (t);
9990 if (TREE_CODE (t) == VAR_DECL && VAR_HAD_UNKNOWN_BOUND (t))
9991 type = strip_array_domain (type);
9992 type = tsubst (type, args, complain, in_decl);
9994 if (TREE_CODE (r) == VAR_DECL)
9996 /* Even if the original location is out of scope, the
9997 newly substituted one is not. */
9998 DECL_DEAD_FOR_LOCAL (r) = 0;
9999 DECL_INITIALIZED_P (r) = 0;
10000 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10001 if (type == error_mark_node)
10002 RETURN (error_mark_node);
10003 if (TREE_CODE (type) == FUNCTION_TYPE)
10005 /* It may seem that this case cannot occur, since:
10010 declares a function, not a variable. However:
10013 template <typename T> void g() { T t; }
10014 template void g<f>();
10016 is an attempt to declare a variable with function
10018 error ("variable %qD has function type",
10019 /* R is not yet sufficiently initialized, so we
10020 just use its name. */
10022 RETURN (error_mark_node);
10024 type = complete_type (type);
10025 /* Wait until cp_finish_decl to set this again, to handle
10026 circular dependency (template/instantiate6.C). */
10027 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10028 type = check_var_type (DECL_NAME (r), type);
10030 if (DECL_HAS_VALUE_EXPR_P (t))
10032 tree ve = DECL_VALUE_EXPR (t);
10033 ve = tsubst_expr (ve, args, complain, in_decl,
10034 /*constant_expression_p=*/false);
10035 SET_DECL_VALUE_EXPR (r, ve);
10038 else if (DECL_SELF_REFERENCE_P (t))
10039 SET_DECL_SELF_REFERENCE_P (r);
10040 TREE_TYPE (r) = type;
10041 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10042 DECL_CONTEXT (r) = ctx;
10043 /* Clear out the mangled name and RTL for the instantiation. */
10044 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10045 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10046 SET_DECL_RTL (r, NULL);
10047 /* The initializer must not be expanded until it is required;
10048 see [temp.inst]. */
10049 DECL_INITIAL (r) = NULL_TREE;
10050 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10051 SET_DECL_RTL (r, NULL);
10052 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10053 if (TREE_CODE (r) == VAR_DECL)
10055 /* Possibly limit visibility based on template args. */
10056 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10057 if (DECL_VISIBILITY_SPECIFIED (t))
10059 DECL_VISIBILITY_SPECIFIED (r) = 0;
10060 DECL_ATTRIBUTES (r)
10061 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10063 determine_visibility (r);
10068 /* A static data member declaration is always marked
10069 external when it is declared in-class, even if an
10070 initializer is present. We mimic the non-template
10071 processing here. */
10072 DECL_EXTERNAL (r) = 1;
10074 register_specialization (r, gen_tmpl, argvec, false, hash);
10075 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10076 SET_DECL_IMPLICIT_INSTANTIATION (r);
10078 else if (cp_unevaluated_operand)
10080 /* We're substituting this var in a decltype outside of its
10081 scope, such as for a lambda return type. Don't add it to
10082 local_specializations, do perform auto deduction. */
10083 tree auto_node = type_uses_auto (type);
10085 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
10086 /*constant_expression_p=*/false);
10088 if (auto_node && init)
10090 init = resolve_nondeduced_context (init);
10091 if (describable_type (init))
10093 type = do_auto_deduction (type, init, auto_node);
10094 TREE_TYPE (r) = type;
10099 register_local_specialization (r, t);
10101 DECL_CHAIN (r) = NULL_TREE;
10103 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10105 args, complain, in_decl);
10107 /* Preserve a typedef that names a type. */
10108 if (is_typedef_decl (r))
10110 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
10111 set_underlying_type (r);
10114 layout_decl (r, 0);
10119 gcc_unreachable ();
10124 /* Restore the file and line information. */
10125 input_location = saved_loc;
10130 /* Substitute into the ARG_TYPES of a function type. */
10133 tsubst_arg_types (tree arg_types,
10135 tsubst_flags_t complain,
10138 tree remaining_arg_types;
10139 tree type = NULL_TREE;
10141 tree expanded_args = NULL_TREE;
10144 if (!arg_types || arg_types == void_list_node)
10147 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
10148 args, complain, in_decl);
10149 if (remaining_arg_types == error_mark_node)
10150 return error_mark_node;
10152 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
10154 /* For a pack expansion, perform substitution on the
10155 entire expression. Later on, we'll handle the arguments
10157 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
10158 args, complain, in_decl);
10160 if (TREE_CODE (expanded_args) == TREE_VEC)
10161 /* So that we'll spin through the parameters, one by one. */
10162 i = TREE_VEC_LENGTH (expanded_args);
10165 /* We only partially substituted into the parameter
10166 pack. Our type is TYPE_PACK_EXPANSION. */
10167 type = expanded_args;
10168 expanded_args = NULL_TREE;
10176 type = TREE_VEC_ELT (expanded_args, i);
10178 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
10180 if (type == error_mark_node)
10181 return error_mark_node;
10182 if (VOID_TYPE_P (type))
10184 if (complain & tf_error)
10186 error ("invalid parameter type %qT", type);
10188 error ("in declaration %q+D", in_decl);
10190 return error_mark_node;
10193 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10194 top-level qualifiers as required. */
10195 type = TYPE_MAIN_VARIANT (type_decays_to (type));
10197 /* We do not substitute into default arguments here. The standard
10198 mandates that they be instantiated only when needed, which is
10199 done in build_over_call. */
10200 default_arg = TREE_PURPOSE (arg_types);
10202 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
10204 /* We've instantiated a template before its default arguments
10205 have been parsed. This can happen for a nested template
10206 class, and is not an error unless we require the default
10207 argument in a call of this function. */
10208 remaining_arg_types =
10209 tree_cons (default_arg, type, remaining_arg_types);
10210 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
10211 remaining_arg_types);
10214 remaining_arg_types =
10215 hash_tree_cons (default_arg, type, remaining_arg_types);
10218 return remaining_arg_types;
10221 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10222 *not* handle the exception-specification for FNTYPE, because the
10223 initial substitution of explicitly provided template parameters
10224 during argument deduction forbids substitution into the
10225 exception-specification:
10229 All references in the function type of the function template to the
10230 corresponding template parameters are replaced by the specified tem-
10231 plate argument values. If a substitution in a template parameter or
10232 in the function type of the function template results in an invalid
10233 type, type deduction fails. [Note: The equivalent substitution in
10234 exception specifications is done only when the function is instanti-
10235 ated, at which point a program is ill-formed if the substitution
10236 results in an invalid type.] */
10239 tsubst_function_type (tree t,
10241 tsubst_flags_t complain,
10248 /* The TYPE_CONTEXT is not used for function/method types. */
10249 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
10251 /* Substitute the return type. */
10252 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10253 if (return_type == error_mark_node)
10254 return error_mark_node;
10255 /* The standard does not presently indicate that creation of a
10256 function type with an invalid return type is a deduction failure.
10257 However, that is clearly analogous to creating an array of "void"
10258 or a reference to a reference. This is core issue #486. */
10259 if (TREE_CODE (return_type) == ARRAY_TYPE
10260 || TREE_CODE (return_type) == FUNCTION_TYPE)
10262 if (complain & tf_error)
10264 if (TREE_CODE (return_type) == ARRAY_TYPE)
10265 error ("function returning an array");
10267 error ("function returning a function");
10269 return error_mark_node;
10272 /* Substitute the argument types. */
10273 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
10274 complain, in_decl);
10275 if (arg_types == error_mark_node)
10276 return error_mark_node;
10278 /* Construct a new type node and return it. */
10279 if (TREE_CODE (t) == FUNCTION_TYPE)
10281 fntype = build_function_type (return_type, arg_types);
10282 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
10286 tree r = TREE_TYPE (TREE_VALUE (arg_types));
10287 if (! MAYBE_CLASS_TYPE_P (r))
10291 Type deduction may fail for any of the following
10294 -- Attempting to create "pointer to member of T" when T
10295 is not a class type. */
10296 if (complain & tf_error)
10297 error ("creating pointer to member function of non-class type %qT",
10299 return error_mark_node;
10302 fntype = build_method_type_directly (r, return_type,
10303 TREE_CHAIN (arg_types));
10305 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
10310 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10311 ARGS into that specification, and return the substituted
10312 specification. If there is no specification, return NULL_TREE. */
10315 tsubst_exception_specification (tree fntype,
10317 tsubst_flags_t complain,
10323 specs = TYPE_RAISES_EXCEPTIONS (fntype);
10324 new_specs = NULL_TREE;
10325 if (specs && TREE_PURPOSE (specs))
10327 /* A noexcept-specifier. */
10328 new_specs = tsubst_copy_and_build
10329 (TREE_PURPOSE (specs), args, complain, in_decl, /*function_p=*/false,
10330 /*integral_constant_expression_p=*/true);
10331 new_specs = build_noexcept_spec (new_specs, complain);
10335 if (! TREE_VALUE (specs))
10342 tree expanded_specs = NULL_TREE;
10344 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
10346 /* Expand the pack expansion type. */
10347 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
10351 if (expanded_specs == error_mark_node)
10352 return error_mark_node;
10353 else if (TREE_CODE (expanded_specs) == TREE_VEC)
10354 len = TREE_VEC_LENGTH (expanded_specs);
10357 /* We're substituting into a member template, so
10358 we got a TYPE_PACK_EXPANSION back. Add that
10359 expansion and move on. */
10360 gcc_assert (TREE_CODE (expanded_specs)
10361 == TYPE_PACK_EXPANSION);
10362 new_specs = add_exception_specifier (new_specs,
10365 specs = TREE_CHAIN (specs);
10370 for (i = 0; i < len; ++i)
10372 if (expanded_specs)
10373 spec = TREE_VEC_ELT (expanded_specs, i);
10375 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
10376 if (spec == error_mark_node)
10378 new_specs = add_exception_specifier (new_specs, spec,
10382 specs = TREE_CHAIN (specs);
10388 /* Take the tree structure T and replace template parameters used
10389 therein with the argument vector ARGS. IN_DECL is an associated
10390 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
10391 Issue error and warning messages under control of COMPLAIN. Note
10392 that we must be relatively non-tolerant of extensions here, in
10393 order to preserve conformance; if we allow substitutions that
10394 should not be allowed, we may allow argument deductions that should
10395 not succeed, and therefore report ambiguous overload situations
10396 where there are none. In theory, we could allow the substitution,
10397 but indicate that it should have failed, and allow our caller to
10398 make sure that the right thing happens, but we don't try to do this
10401 This function is used for dealing with types, decls and the like;
10402 for expressions, use tsubst_expr or tsubst_copy. */
10405 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10407 enum tree_code code;
10410 if (t == NULL_TREE || t == error_mark_node
10411 || t == integer_type_node
10412 || t == void_type_node
10413 || t == char_type_node
10414 || t == unknown_type_node
10415 || TREE_CODE (t) == NAMESPACE_DECL
10416 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
10420 return tsubst_decl (t, args, complain);
10422 if (args == NULL_TREE)
10425 code = TREE_CODE (t);
10427 if (code == IDENTIFIER_NODE)
10428 type = IDENTIFIER_TYPE_VALUE (t);
10430 type = TREE_TYPE (t);
10432 gcc_assert (type != unknown_type_node);
10434 /* Reuse typedefs. We need to do this to handle dependent attributes,
10435 such as attribute aligned. */
10437 && typedef_variant_p (t))
10439 tree decl = TYPE_NAME (t);
10441 if (DECL_CLASS_SCOPE_P (decl)
10442 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
10443 && uses_template_parms (DECL_CONTEXT (decl)))
10445 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10446 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
10447 r = retrieve_specialization (tmpl, gen_args, 0);
10449 else if (DECL_FUNCTION_SCOPE_P (decl)
10450 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
10451 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
10452 r = retrieve_local_specialization (decl);
10454 /* The typedef is from a non-template context. */
10460 r = cp_build_qualified_type_real
10461 (r, cp_type_quals (t) | cp_type_quals (r),
10462 complain | tf_ignore_bad_quals);
10465 /* Else we must be instantiating the typedef, so fall through. */
10469 && code != TYPENAME_TYPE
10470 && code != TEMPLATE_TYPE_PARM
10471 && code != IDENTIFIER_NODE
10472 && code != FUNCTION_TYPE
10473 && code != METHOD_TYPE)
10474 type = tsubst (type, args, complain, in_decl);
10475 if (type == error_mark_node)
10476 return error_mark_node;
10482 case ENUMERAL_TYPE:
10483 return tsubst_aggr_type (t, args, complain, in_decl,
10484 /*entering_scope=*/0);
10487 case IDENTIFIER_NODE:
10498 if (t == integer_type_node)
10501 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
10502 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
10506 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
10508 max = tsubst_expr (omax, args, complain, in_decl,
10509 /*integral_constant_expression_p=*/false);
10511 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
10513 if (TREE_CODE (max) == NOP_EXPR
10514 && TREE_SIDE_EFFECTS (omax)
10515 && !TREE_TYPE (max))
10516 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
10518 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
10519 with TREE_SIDE_EFFECTS that indicates this is not an integral
10520 constant expression. */
10521 if (processing_template_decl
10522 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
10524 gcc_assert (TREE_CODE (max) == NOP_EXPR);
10525 TREE_SIDE_EFFECTS (max) = 1;
10528 return compute_array_index_type (NULL_TREE, max, complain);
10531 case TEMPLATE_TYPE_PARM:
10532 case TEMPLATE_TEMPLATE_PARM:
10533 case BOUND_TEMPLATE_TEMPLATE_PARM:
10534 case TEMPLATE_PARM_INDEX:
10539 tree arg = NULL_TREE;
10543 gcc_assert (TREE_VEC_LENGTH (args) > 0);
10544 template_parm_level_and_index (t, &level, &idx);
10546 levels = TMPL_ARGS_DEPTH (args);
10547 if (level <= levels)
10549 arg = TMPL_ARG (args, level, idx);
10551 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
10552 /* See through ARGUMENT_PACK_SELECT arguments. */
10553 arg = ARGUMENT_PACK_SELECT_ARG (arg);
10556 if (arg == error_mark_node)
10557 return error_mark_node;
10558 else if (arg != NULL_TREE)
10560 if (ARGUMENT_PACK_P (arg))
10561 /* If ARG is an argument pack, we don't actually want to
10562 perform a substitution here, because substitutions
10563 for argument packs are only done
10564 element-by-element. We can get to this point when
10565 substituting the type of a non-type template
10566 parameter pack, when that type actually contains
10567 template parameter packs from an outer template, e.g.,
10569 template<typename... Types> struct A {
10570 template<Types... Values> struct B { };
10574 if (code == TEMPLATE_TYPE_PARM)
10577 gcc_assert (TYPE_P (arg));
10579 quals = cp_type_quals (arg) | cp_type_quals (t);
10581 return cp_build_qualified_type_real
10582 (arg, quals, complain | tf_ignore_bad_quals);
10584 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
10586 /* We are processing a type constructed from a
10587 template template parameter. */
10588 tree argvec = tsubst (TYPE_TI_ARGS (t),
10589 args, complain, in_decl);
10590 if (argvec == error_mark_node)
10591 return error_mark_node;
10593 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
10594 are resolving nested-types in the signature of a
10595 member function templates. Otherwise ARG is a
10596 TEMPLATE_DECL and is the real template to be
10598 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
10599 arg = TYPE_NAME (arg);
10601 r = lookup_template_class (arg,
10603 DECL_CONTEXT (arg),
10604 /*entering_scope=*/0,
10606 return cp_build_qualified_type_real
10607 (r, cp_type_quals (t), complain);
10610 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
10611 return unshare_expr (arg);
10615 /* This can happen during the attempted tsubst'ing in
10616 unify. This means that we don't yet have any information
10617 about the template parameter in question. */
10620 /* If we get here, we must have been looking at a parm for a
10621 more deeply nested template. Make a new version of this
10622 template parameter, but with a lower level. */
10625 case TEMPLATE_TYPE_PARM:
10626 case TEMPLATE_TEMPLATE_PARM:
10627 case BOUND_TEMPLATE_TEMPLATE_PARM:
10628 if (cp_type_quals (t))
10630 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
10631 r = cp_build_qualified_type_real
10632 (r, cp_type_quals (t),
10633 complain | (code == TEMPLATE_TYPE_PARM
10634 ? tf_ignore_bad_quals : 0));
10639 TEMPLATE_TYPE_PARM_INDEX (r)
10640 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
10641 r, levels, args, complain);
10642 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
10643 TYPE_MAIN_VARIANT (r) = r;
10644 TYPE_POINTER_TO (r) = NULL_TREE;
10645 TYPE_REFERENCE_TO (r) = NULL_TREE;
10647 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
10648 /* We have reduced the level of the template
10649 template parameter, but not the levels of its
10650 template parameters, so canonical_type_parameter
10651 will not be able to find the canonical template
10652 template parameter for this level. Thus, we
10653 require structural equality checking to compare
10654 TEMPLATE_TEMPLATE_PARMs. */
10655 SET_TYPE_STRUCTURAL_EQUALITY (r);
10656 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
10657 SET_TYPE_STRUCTURAL_EQUALITY (r);
10659 TYPE_CANONICAL (r) = canonical_type_parameter (r);
10661 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
10663 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
10664 complain, in_decl);
10665 if (argvec == error_mark_node)
10666 return error_mark_node;
10668 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
10669 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
10674 case TEMPLATE_PARM_INDEX:
10675 r = reduce_template_parm_level (t, type, levels, args, complain);
10679 gcc_unreachable ();
10687 tree purpose, value, chain;
10689 if (t == void_list_node)
10692 purpose = TREE_PURPOSE (t);
10695 purpose = tsubst (purpose, args, complain, in_decl);
10696 if (purpose == error_mark_node)
10697 return error_mark_node;
10699 value = TREE_VALUE (t);
10702 value = tsubst (value, args, complain, in_decl);
10703 if (value == error_mark_node)
10704 return error_mark_node;
10706 chain = TREE_CHAIN (t);
10707 if (chain && chain != void_type_node)
10709 chain = tsubst (chain, args, complain, in_decl);
10710 if (chain == error_mark_node)
10711 return error_mark_node;
10713 if (purpose == TREE_PURPOSE (t)
10714 && value == TREE_VALUE (t)
10715 && chain == TREE_CHAIN (t))
10717 return hash_tree_cons (purpose, value, chain);
10721 /* We should never be tsubsting a binfo. */
10722 gcc_unreachable ();
10725 /* A vector of template arguments. */
10726 gcc_assert (!type);
10727 return tsubst_template_args (t, args, complain, in_decl);
10730 case REFERENCE_TYPE:
10732 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
10737 Type deduction may fail for any of the following
10740 -- Attempting to create a pointer to reference type.
10741 -- Attempting to create a reference to a reference type or
10742 a reference to void.
10744 Core issue 106 says that creating a reference to a reference
10745 during instantiation is no longer a cause for failure. We
10746 only enforce this check in strict C++98 mode. */
10747 if ((TREE_CODE (type) == REFERENCE_TYPE
10748 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
10749 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
10751 static location_t last_loc;
10753 /* We keep track of the last time we issued this error
10754 message to avoid spewing a ton of messages during a
10755 single bad template instantiation. */
10756 if (complain & tf_error
10757 && last_loc != input_location)
10759 if (TREE_CODE (type) == VOID_TYPE)
10760 error ("forming reference to void");
10761 else if (code == POINTER_TYPE)
10762 error ("forming pointer to reference type %qT", type);
10764 error ("forming reference to reference type %qT", type);
10765 last_loc = input_location;
10768 return error_mark_node;
10770 else if (code == POINTER_TYPE)
10772 r = build_pointer_type (type);
10773 if (TREE_CODE (type) == METHOD_TYPE)
10774 r = build_ptrmemfunc_type (r);
10776 else if (TREE_CODE (type) == REFERENCE_TYPE)
10777 /* In C++0x, during template argument substitution, when there is an
10778 attempt to create a reference to a reference type, reference
10779 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
10781 "If a template-argument for a template-parameter T names a type
10782 that is a reference to a type A, an attempt to create the type
10783 'lvalue reference to cv T' creates the type 'lvalue reference to
10784 A,' while an attempt to create the type type rvalue reference to
10785 cv T' creates the type T"
10787 r = cp_build_reference_type
10789 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
10791 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
10792 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10794 if (r != error_mark_node)
10795 /* Will this ever be needed for TYPE_..._TO values? */
10802 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
10803 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
10807 Type deduction may fail for any of the following
10810 -- Attempting to create "pointer to member of T" when T
10811 is not a class type. */
10812 if (complain & tf_error)
10813 error ("creating pointer to member of non-class type %qT", r);
10814 return error_mark_node;
10816 if (TREE_CODE (type) == REFERENCE_TYPE)
10818 if (complain & tf_error)
10819 error ("creating pointer to member reference type %qT", type);
10820 return error_mark_node;
10822 if (TREE_CODE (type) == VOID_TYPE)
10824 if (complain & tf_error)
10825 error ("creating pointer to member of type void");
10826 return error_mark_node;
10828 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
10829 if (TREE_CODE (type) == FUNCTION_TYPE)
10831 /* The type of the implicit object parameter gets its
10832 cv-qualifiers from the FUNCTION_TYPE. */
10834 tree method_type = build_memfn_type (type, r, type_memfn_quals (type));
10835 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
10836 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
10840 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
10844 case FUNCTION_TYPE:
10849 fntype = tsubst_function_type (t, args, complain, in_decl);
10850 if (fntype == error_mark_node)
10851 return error_mark_node;
10853 /* Substitute the exception specification. */
10854 specs = tsubst_exception_specification (t, args, complain,
10856 if (specs == error_mark_node)
10857 return error_mark_node;
10859 fntype = build_exception_variant (fntype, specs);
10864 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
10865 if (domain == error_mark_node)
10866 return error_mark_node;
10868 /* As an optimization, we avoid regenerating the array type if
10869 it will obviously be the same as T. */
10870 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
10873 /* These checks should match the ones in grokdeclarator.
10877 The deduction may fail for any of the following reasons:
10879 -- Attempting to create an array with an element type that
10880 is void, a function type, or a reference type, or [DR337]
10881 an abstract class type. */
10882 if (TREE_CODE (type) == VOID_TYPE
10883 || TREE_CODE (type) == FUNCTION_TYPE
10884 || TREE_CODE (type) == REFERENCE_TYPE)
10886 if (complain & tf_error)
10887 error ("creating array of %qT", type);
10888 return error_mark_node;
10890 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
10892 if (complain & tf_error)
10893 error ("creating array of %qT, which is an abstract class type",
10895 return error_mark_node;
10898 r = build_cplus_array_type (type, domain);
10900 if (TYPE_USER_ALIGN (t))
10902 TYPE_ALIGN (r) = TYPE_ALIGN (t);
10903 TYPE_USER_ALIGN (r) = 1;
10909 case TYPENAME_TYPE:
10911 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10912 in_decl, /*entering_scope=*/1);
10913 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
10914 complain, in_decl);
10916 if (ctx == error_mark_node || f == error_mark_node)
10917 return error_mark_node;
10919 if (!MAYBE_CLASS_TYPE_P (ctx))
10921 if (complain & tf_error)
10922 error ("%qT is not a class, struct, or union type", ctx);
10923 return error_mark_node;
10925 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
10927 /* Normally, make_typename_type does not require that the CTX
10928 have complete type in order to allow things like:
10930 template <class T> struct S { typename S<T>::X Y; };
10932 But, such constructs have already been resolved by this
10933 point, so here CTX really should have complete type, unless
10934 it's a partial instantiation. */
10935 ctx = complete_type (ctx);
10936 if (!COMPLETE_TYPE_P (ctx))
10938 if (complain & tf_error)
10939 cxx_incomplete_type_error (NULL_TREE, ctx);
10940 return error_mark_node;
10944 f = make_typename_type (ctx, f, typename_type,
10945 (complain & tf_error) | tf_keep_type_decl);
10946 if (f == error_mark_node)
10948 if (TREE_CODE (f) == TYPE_DECL)
10950 complain |= tf_ignore_bad_quals;
10954 if (TREE_CODE (f) != TYPENAME_TYPE)
10956 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
10957 error ("%qT resolves to %qT, which is not an enumeration type",
10959 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
10960 error ("%qT resolves to %qT, which is is not a class type",
10964 return cp_build_qualified_type_real
10965 (f, cp_type_quals (f) | cp_type_quals (t), complain);
10968 case UNBOUND_CLASS_TEMPLATE:
10970 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10971 in_decl, /*entering_scope=*/1);
10972 tree name = TYPE_IDENTIFIER (t);
10973 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
10975 if (ctx == error_mark_node || name == error_mark_node)
10976 return error_mark_node;
10979 parm_list = tsubst_template_parms (parm_list, args, complain);
10980 return make_unbound_class_template (ctx, name, parm_list, complain);
10987 ++cp_unevaluated_operand;
10988 ++c_inhibit_evaluation_warnings;
10990 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
10992 /*integral_constant_expression_p=*/false);
10994 --cp_unevaluated_operand;
10995 --c_inhibit_evaluation_warnings;
10997 type = finish_typeof (type);
10998 return cp_build_qualified_type_real (type,
11000 | cp_type_quals (type),
11004 case DECLTYPE_TYPE:
11008 ++cp_unevaluated_operand;
11009 ++c_inhibit_evaluation_warnings;
11011 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
11013 /*integral_constant_expression_p=*/false);
11015 --cp_unevaluated_operand;
11016 --c_inhibit_evaluation_warnings;
11018 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
11019 type = lambda_capture_field_type (type);
11020 else if (DECLTYPE_FOR_LAMBDA_RETURN (t))
11021 type = lambda_return_type (type);
11023 type = finish_decltype_type
11024 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
11025 return cp_build_qualified_type_real (type,
11027 | cp_type_quals (type),
11031 case TYPE_ARGUMENT_PACK:
11032 case NONTYPE_ARGUMENT_PACK:
11034 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
11036 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
11040 SET_ARGUMENT_PACK_ARGS (r, packed_out);
11042 /* For template nontype argument packs, also substitute into
11044 if (code == NONTYPE_ARGUMENT_PACK)
11045 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
11063 /* We should use one of the expression tsubsts for these codes. */
11064 gcc_unreachable ();
11067 sorry ("use of %qs in template", tree_code_name [(int) code]);
11068 return error_mark_node;
11072 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11073 type of the expression on the left-hand side of the "." or "->"
11077 tsubst_baselink (tree baselink, tree object_type,
11078 tree args, tsubst_flags_t complain, tree in_decl)
11081 tree qualifying_scope;
11084 tree template_args = 0;
11085 bool template_id_p = false;
11087 /* A baselink indicates a function from a base class. Both the
11088 BASELINK_ACCESS_BINFO and the base class referenced may
11089 indicate bases of the template class, rather than the
11090 instantiated class. In addition, lookups that were not
11091 ambiguous before may be ambiguous now. Therefore, we perform
11092 the lookup again. */
11093 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
11094 qualifying_scope = tsubst (qualifying_scope, args,
11095 complain, in_decl);
11096 fns = BASELINK_FUNCTIONS (baselink);
11097 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
11098 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
11100 template_id_p = true;
11101 template_args = TREE_OPERAND (fns, 1);
11102 fns = TREE_OPERAND (fns, 0);
11104 template_args = tsubst_template_args (template_args, args,
11105 complain, in_decl);
11107 name = DECL_NAME (get_first_fn (fns));
11108 if (IDENTIFIER_TYPENAME_P (name))
11109 name = mangle_conv_op_name_for_type (optype);
11110 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
11112 return error_mark_node;
11114 /* If lookup found a single function, mark it as used at this
11115 point. (If it lookup found multiple functions the one selected
11116 later by overload resolution will be marked as used at that
11118 if (BASELINK_P (baselink))
11119 fns = BASELINK_FUNCTIONS (baselink);
11120 if (!template_id_p && !really_overloaded_fn (fns))
11121 mark_used (OVL_CURRENT (fns));
11123 /* Add back the template arguments, if present. */
11124 if (BASELINK_P (baselink) && template_id_p)
11125 BASELINK_FUNCTIONS (baselink)
11126 = build_nt (TEMPLATE_ID_EXPR,
11127 BASELINK_FUNCTIONS (baselink),
11129 /* Update the conversion operator type. */
11130 BASELINK_OPTYPE (baselink) = optype;
11133 object_type = current_class_type;
11134 return adjust_result_of_qualified_name_lookup (baselink,
11139 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11140 true if the qualified-id will be a postfix-expression in-and-of
11141 itself; false if more of the postfix-expression follows the
11142 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11146 tsubst_qualified_id (tree qualified_id, tree args,
11147 tsubst_flags_t complain, tree in_decl,
11148 bool done, bool address_p)
11154 tree template_args;
11156 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
11158 /* Figure out what name to look up. */
11159 name = TREE_OPERAND (qualified_id, 1);
11160 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11162 is_template = true;
11163 template_args = TREE_OPERAND (name, 1);
11165 template_args = tsubst_template_args (template_args, args,
11166 complain, in_decl);
11167 name = TREE_OPERAND (name, 0);
11171 is_template = false;
11172 template_args = NULL_TREE;
11175 /* Substitute into the qualifying scope. When there are no ARGS, we
11176 are just trying to simplify a non-dependent expression. In that
11177 case the qualifying scope may be dependent, and, in any case,
11178 substituting will not help. */
11179 scope = TREE_OPERAND (qualified_id, 0);
11182 scope = tsubst (scope, args, complain, in_decl);
11183 expr = tsubst_copy (name, args, complain, in_decl);
11188 if (dependent_scope_p (scope))
11189 return build_qualified_name (NULL_TREE, scope, expr,
11190 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
11192 if (!BASELINK_P (name) && !DECL_P (expr))
11194 if (TREE_CODE (expr) == BIT_NOT_EXPR)
11196 /* A BIT_NOT_EXPR is used to represent a destructor. */
11197 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
11199 error ("qualifying type %qT does not match destructor name ~%qT",
11200 scope, TREE_OPERAND (expr, 0));
11201 expr = error_mark_node;
11204 expr = lookup_qualified_name (scope, complete_dtor_identifier,
11205 /*is_type_p=*/0, false);
11208 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
11209 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
11210 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
11212 if (complain & tf_error)
11214 error ("dependent-name %qE is parsed as a non-type, but "
11215 "instantiation yields a type", qualified_id);
11216 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
11218 return error_mark_node;
11224 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
11226 /* Remember that there was a reference to this entity. */
11230 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
11232 if (complain & tf_error)
11233 qualified_name_lookup_error (scope,
11234 TREE_OPERAND (qualified_id, 1),
11235 expr, input_location);
11236 return error_mark_node;
11240 expr = lookup_template_function (expr, template_args);
11242 if (expr == error_mark_node && complain & tf_error)
11243 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
11244 expr, input_location);
11245 else if (TYPE_P (scope))
11247 expr = (adjust_result_of_qualified_name_lookup
11248 (expr, scope, current_class_type));
11249 expr = (finish_qualified_id_expr
11250 (scope, expr, done, address_p,
11251 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
11252 /*template_arg_p=*/false));
11255 /* Expressions do not generally have reference type. */
11256 if (TREE_CODE (expr) != SCOPE_REF
11257 /* However, if we're about to form a pointer-to-member, we just
11258 want the referenced member referenced. */
11259 && TREE_CODE (expr) != OFFSET_REF)
11260 expr = convert_from_reference (expr);
11265 /* Like tsubst, but deals with expressions. This function just replaces
11266 template parms; to finish processing the resultant expression, use
11270 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11272 enum tree_code code;
11275 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
11278 code = TREE_CODE (t);
11283 r = retrieve_local_specialization (t);
11288 /* This can happen for a parameter name used later in a function
11289 declaration (such as in a late-specified return type). Just
11290 make a dummy decl, since it's only used for its type. */
11291 gcc_assert (cp_unevaluated_operand != 0);
11292 /* We copy T because want to tsubst the PARM_DECL only,
11293 not the following PARM_DECLs that are chained to T. */
11295 r = tsubst_decl (c, args, complain);
11296 /* Give it the template pattern as its context; its true context
11297 hasn't been instantiated yet and this is good enough for
11299 DECL_CONTEXT (r) = DECL_CONTEXT (t);
11302 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
11303 r = ARGUMENT_PACK_SELECT_ARG (r);
11312 if (DECL_TEMPLATE_PARM_P (t))
11313 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
11314 /* There is no need to substitute into namespace-scope
11316 if (DECL_NAMESPACE_SCOPE_P (t))
11318 /* If ARGS is NULL, then T is known to be non-dependent. */
11319 if (args == NULL_TREE)
11320 return integral_constant_value (t);
11322 /* Unfortunately, we cannot just call lookup_name here.
11325 template <int I> int f() {
11327 struct S { void g() { E e = a; } };
11330 When we instantiate f<7>::S::g(), say, lookup_name is not
11331 clever enough to find f<7>::a. */
11333 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
11334 /*entering_scope=*/0);
11336 for (v = TYPE_VALUES (enum_type);
11338 v = TREE_CHAIN (v))
11339 if (TREE_PURPOSE (v) == DECL_NAME (t))
11340 return TREE_VALUE (v);
11342 /* We didn't find the name. That should never happen; if
11343 name-lookup found it during preliminary parsing, we
11344 should find it again here during instantiation. */
11345 gcc_unreachable ();
11350 if (DECL_CONTEXT (t))
11354 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
11355 /*entering_scope=*/1);
11356 if (ctx != DECL_CONTEXT (t))
11358 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
11361 if (complain & tf_error)
11362 error ("using invalid field %qD", t);
11363 return error_mark_node;
11372 case FUNCTION_DECL:
11373 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
11374 || local_variable_p (t))
11375 t = tsubst (t, args, complain, in_decl);
11380 /* An OVERLOAD will always be a non-dependent overload set; an
11381 overload set from function scope will just be represented with an
11382 IDENTIFIER_NODE, and from class scope with a BASELINK. */
11383 gcc_assert (!uses_template_parms (t));
11387 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
11389 case TEMPLATE_DECL:
11390 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11391 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
11392 args, complain, in_decl);
11393 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
11394 return tsubst (t, args, complain, in_decl);
11395 else if (DECL_CLASS_SCOPE_P (t)
11396 && uses_template_parms (DECL_CONTEXT (t)))
11398 /* Template template argument like the following example need
11401 template <template <class> class TT> struct C {};
11402 template <class T> struct D {
11403 template <class U> struct E {};
11408 We are processing the template argument `E' in #1 for
11409 the template instantiation #2. Originally, `E' is a
11410 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
11411 have to substitute this with one having context `D<int>'. */
11413 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
11414 return lookup_field (context, DECL_NAME(t), 0, false);
11417 /* Ordinary template template argument. */
11421 case REINTERPRET_CAST_EXPR:
11422 case CONST_CAST_EXPR:
11423 case STATIC_CAST_EXPR:
11424 case DYNAMIC_CAST_EXPR:
11427 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11428 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11431 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
11437 ++cp_unevaluated_operand;
11438 ++c_inhibit_evaluation_warnings;
11439 /* We only want to compute the number of arguments. */
11440 expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
11441 complain, in_decl);
11442 --cp_unevaluated_operand;
11443 --c_inhibit_evaluation_warnings;
11445 if (TREE_CODE (expanded) == TREE_VEC)
11446 len = TREE_VEC_LENGTH (expanded);
11448 if (expanded == error_mark_node)
11449 return error_mark_node;
11450 else if (PACK_EXPANSION_P (expanded)
11451 || (TREE_CODE (expanded) == TREE_VEC
11453 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
11455 if (TREE_CODE (expanded) == TREE_VEC)
11456 expanded = TREE_VEC_ELT (expanded, len - 1);
11458 if (TYPE_P (expanded))
11459 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
11460 complain & tf_error);
11462 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
11463 complain & tf_error);
11466 return build_int_cst (size_type_node, len);
11472 case TRUTH_NOT_EXPR:
11475 case UNARY_PLUS_EXPR: /* Unary + */
11477 case AT_ENCODE_EXPR:
11481 case REALPART_EXPR:
11482 case IMAGPART_EXPR:
11484 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11485 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11487 case COMPONENT_REF:
11492 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
11493 name = TREE_OPERAND (t, 1);
11494 if (TREE_CODE (name) == BIT_NOT_EXPR)
11496 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11497 complain, in_decl);
11498 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11500 else if (TREE_CODE (name) == SCOPE_REF
11501 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
11503 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
11504 complain, in_decl);
11505 name = TREE_OPERAND (name, 1);
11506 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11507 complain, in_decl);
11508 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11509 name = build_qualified_name (/*type=*/NULL_TREE,
11511 /*template_p=*/false);
11513 else if (TREE_CODE (name) == BASELINK)
11514 name = tsubst_baselink (name,
11515 non_reference (TREE_TYPE (object)),
11519 name = tsubst_copy (name, args, complain, in_decl);
11520 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
11526 case TRUNC_DIV_EXPR:
11527 case CEIL_DIV_EXPR:
11528 case FLOOR_DIV_EXPR:
11529 case ROUND_DIV_EXPR:
11530 case EXACT_DIV_EXPR:
11534 case TRUNC_MOD_EXPR:
11535 case FLOOR_MOD_EXPR:
11536 case TRUTH_ANDIF_EXPR:
11537 case TRUTH_ORIF_EXPR:
11538 case TRUTH_AND_EXPR:
11539 case TRUTH_OR_EXPR:
11552 case COMPOUND_EXPR:
11555 case PREDECREMENT_EXPR:
11556 case PREINCREMENT_EXPR:
11557 case POSTDECREMENT_EXPR:
11558 case POSTINCREMENT_EXPR:
11560 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11561 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11564 return build_qualified_name (/*type=*/NULL_TREE,
11565 tsubst_copy (TREE_OPERAND (t, 0),
11566 args, complain, in_decl),
11567 tsubst_copy (TREE_OPERAND (t, 1),
11568 args, complain, in_decl),
11569 QUALIFIED_NAME_IS_TEMPLATE (t));
11574 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11575 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11576 NULL_TREE, NULL_TREE);
11580 int n = VL_EXP_OPERAND_LENGTH (t);
11581 tree result = build_vl_exp (CALL_EXPR, n);
11583 for (i = 0; i < n; i++)
11584 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
11585 complain, in_decl);
11591 case PSEUDO_DTOR_EXPR:
11594 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11595 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11596 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
11597 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
11604 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11605 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11606 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
11607 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
11614 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11615 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11616 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
11617 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
11621 case TEMPLATE_ID_EXPR:
11623 /* Substituted template arguments */
11624 tree fn = TREE_OPERAND (t, 0);
11625 tree targs = TREE_OPERAND (t, 1);
11627 fn = tsubst_copy (fn, args, complain, in_decl);
11629 targs = tsubst_template_args (targs, args, complain, in_decl);
11631 return lookup_template_function (fn, targs);
11636 tree purpose, value, chain;
11638 if (t == void_list_node)
11641 purpose = TREE_PURPOSE (t);
11643 purpose = tsubst_copy (purpose, args, complain, in_decl);
11644 value = TREE_VALUE (t);
11646 value = tsubst_copy (value, args, complain, in_decl);
11647 chain = TREE_CHAIN (t);
11648 if (chain && chain != void_type_node)
11649 chain = tsubst_copy (chain, args, complain, in_decl);
11650 if (purpose == TREE_PURPOSE (t)
11651 && value == TREE_VALUE (t)
11652 && chain == TREE_CHAIN (t))
11654 return tree_cons (purpose, value, chain);
11659 case ENUMERAL_TYPE:
11661 case TEMPLATE_TYPE_PARM:
11662 case TEMPLATE_TEMPLATE_PARM:
11663 case BOUND_TEMPLATE_TEMPLATE_PARM:
11664 case TEMPLATE_PARM_INDEX:
11666 case REFERENCE_TYPE:
11668 case FUNCTION_TYPE:
11671 case TYPENAME_TYPE:
11672 case UNBOUND_CLASS_TEMPLATE:
11674 case DECLTYPE_TYPE:
11676 return tsubst (t, args, complain, in_decl);
11678 case IDENTIFIER_NODE:
11679 if (IDENTIFIER_TYPENAME_P (t))
11681 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11682 return mangle_conv_op_name_for_type (new_type);
11688 /* This is handled by tsubst_copy_and_build. */
11689 gcc_unreachable ();
11692 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
11694 tsubst (TREE_TYPE (t), args, complain, in_decl));
11696 case CLEANUP_POINT_EXPR:
11697 /* We shouldn't have built any of these during initial template
11698 generation. Instead, they should be built during instantiation
11699 in response to the saved STMT_IS_FULL_EXPR_P setting. */
11700 gcc_unreachable ();
11704 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11705 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11706 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11707 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
11708 mark_used (TREE_OPERAND (r, 1));
11711 case EXPR_PACK_EXPANSION:
11712 error ("invalid use of pack expansion expression");
11713 return error_mark_node;
11715 case NONTYPE_ARGUMENT_PACK:
11716 error ("use %<...%> to expand argument pack");
11717 return error_mark_node;
11724 /* Instantiate any typedefs in the type. */
11725 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11726 r = fold_convert (type, t);
11727 gcc_assert (TREE_CODE (r) == code);
11732 /* These can sometimes show up in a partial instantiation, but never
11733 involve template parms. */
11734 gcc_assert (!uses_template_parms (t));
11738 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
11739 gcc_checking_assert (false);
11744 /* Like tsubst_copy, but specifically for OpenMP clauses. */
11747 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
11750 tree new_clauses = NULL, nc, oc;
11752 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
11754 nc = copy_node (oc);
11755 OMP_CLAUSE_CHAIN (nc) = new_clauses;
11758 switch (OMP_CLAUSE_CODE (nc))
11760 case OMP_CLAUSE_LASTPRIVATE:
11761 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
11763 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
11764 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
11765 in_decl, /*integral_constant_expression_p=*/false);
11766 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
11767 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
11770 case OMP_CLAUSE_PRIVATE:
11771 case OMP_CLAUSE_SHARED:
11772 case OMP_CLAUSE_FIRSTPRIVATE:
11773 case OMP_CLAUSE_REDUCTION:
11774 case OMP_CLAUSE_COPYIN:
11775 case OMP_CLAUSE_COPYPRIVATE:
11776 case OMP_CLAUSE_IF:
11777 case OMP_CLAUSE_NUM_THREADS:
11778 case OMP_CLAUSE_SCHEDULE:
11779 case OMP_CLAUSE_COLLAPSE:
11780 OMP_CLAUSE_OPERAND (nc, 0)
11781 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
11782 in_decl, /*integral_constant_expression_p=*/false);
11784 case OMP_CLAUSE_NOWAIT:
11785 case OMP_CLAUSE_ORDERED:
11786 case OMP_CLAUSE_DEFAULT:
11787 case OMP_CLAUSE_UNTIED:
11790 gcc_unreachable ();
11794 return finish_omp_clauses (nreverse (new_clauses));
11797 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
11800 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
11803 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
11805 tree purpose, value, chain;
11810 if (TREE_CODE (t) != TREE_LIST)
11811 return tsubst_copy_and_build (t, args, complain, in_decl,
11812 /*function_p=*/false,
11813 /*integral_constant_expression_p=*/false);
11815 if (t == void_list_node)
11818 purpose = TREE_PURPOSE (t);
11820 purpose = RECUR (purpose);
11821 value = TREE_VALUE (t);
11822 if (value && TREE_CODE (value) != LABEL_DECL)
11823 value = RECUR (value);
11824 chain = TREE_CHAIN (t);
11825 if (chain && chain != void_type_node)
11826 chain = RECUR (chain);
11827 return tree_cons (purpose, value, chain);
11831 /* Substitute one OMP_FOR iterator. */
11834 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
11835 tree condv, tree incrv, tree *clauses,
11836 tree args, tsubst_flags_t complain, tree in_decl,
11837 bool integral_constant_expression_p)
11839 #define RECUR(NODE) \
11840 tsubst_expr ((NODE), args, complain, in_decl, \
11841 integral_constant_expression_p)
11842 tree decl, init, cond, incr, auto_node;
11844 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
11845 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
11846 decl = RECUR (TREE_OPERAND (init, 0));
11847 init = TREE_OPERAND (init, 1);
11848 auto_node = type_uses_auto (TREE_TYPE (decl));
11849 if (auto_node && init)
11851 tree init_expr = init;
11852 if (TREE_CODE (init_expr) == DECL_EXPR)
11853 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
11854 init_expr = RECUR (init_expr);
11856 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
11858 gcc_assert (!type_dependent_expression_p (decl));
11860 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
11862 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
11863 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11864 if (TREE_CODE (incr) == MODIFY_EXPR)
11865 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
11866 RECUR (TREE_OPERAND (incr, 1)),
11869 incr = RECUR (incr);
11870 TREE_VEC_ELT (declv, i) = decl;
11871 TREE_VEC_ELT (initv, i) = init;
11872 TREE_VEC_ELT (condv, i) = cond;
11873 TREE_VEC_ELT (incrv, i) = incr;
11877 if (init && TREE_CODE (init) != DECL_EXPR)
11880 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11882 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
11883 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
11884 && OMP_CLAUSE_DECL (c) == decl)
11886 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
11887 && OMP_CLAUSE_DECL (c) == decl)
11888 error ("iteration variable %qD should not be firstprivate", decl);
11889 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
11890 && OMP_CLAUSE_DECL (c) == decl)
11891 error ("iteration variable %qD should not be reduction", decl);
11895 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
11896 OMP_CLAUSE_DECL (c) = decl;
11897 c = finish_omp_clauses (c);
11900 OMP_CLAUSE_CHAIN (c) = *clauses;
11905 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
11906 if (COMPARISON_CLASS_P (cond))
11907 cond = build2 (TREE_CODE (cond), boolean_type_node,
11908 RECUR (TREE_OPERAND (cond, 0)),
11909 RECUR (TREE_OPERAND (cond, 1)));
11911 cond = RECUR (cond);
11912 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11913 switch (TREE_CODE (incr))
11915 case PREINCREMENT_EXPR:
11916 case PREDECREMENT_EXPR:
11917 case POSTINCREMENT_EXPR:
11918 case POSTDECREMENT_EXPR:
11919 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
11920 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
11923 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11924 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11926 tree rhs = TREE_OPERAND (incr, 1);
11927 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11928 RECUR (TREE_OPERAND (incr, 0)),
11929 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11930 RECUR (TREE_OPERAND (rhs, 0)),
11931 RECUR (TREE_OPERAND (rhs, 1))));
11934 incr = RECUR (incr);
11937 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11938 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11940 tree lhs = RECUR (TREE_OPERAND (incr, 0));
11941 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
11942 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
11943 TREE_TYPE (decl), lhs,
11944 RECUR (TREE_OPERAND (incr, 2))));
11946 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
11947 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
11948 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
11950 tree rhs = TREE_OPERAND (incr, 2);
11951 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11952 RECUR (TREE_OPERAND (incr, 0)),
11953 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11954 RECUR (TREE_OPERAND (rhs, 0)),
11955 RECUR (TREE_OPERAND (rhs, 1))));
11958 incr = RECUR (incr);
11961 incr = RECUR (incr);
11965 TREE_VEC_ELT (declv, i) = decl;
11966 TREE_VEC_ELT (initv, i) = init;
11967 TREE_VEC_ELT (condv, i) = cond;
11968 TREE_VEC_ELT (incrv, i) = incr;
11972 /* Like tsubst_copy for expressions, etc. but also does semantic
11976 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
11977 bool integral_constant_expression_p)
11979 #define RECUR(NODE) \
11980 tsubst_expr ((NODE), args, complain, in_decl, \
11981 integral_constant_expression_p)
11985 if (t == NULL_TREE || t == error_mark_node)
11988 if (EXPR_HAS_LOCATION (t))
11989 input_location = EXPR_LOCATION (t);
11990 if (STATEMENT_CODE_P (TREE_CODE (t)))
11991 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
11993 switch (TREE_CODE (t))
11995 case STATEMENT_LIST:
11997 tree_stmt_iterator i;
11998 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
11999 RECUR (tsi_stmt (i));
12003 case CTOR_INITIALIZER:
12004 finish_mem_initializers (tsubst_initializer_list
12005 (TREE_OPERAND (t, 0), args));
12009 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
12013 tmp = RECUR (EXPR_STMT_EXPR (t));
12014 if (EXPR_STMT_STMT_EXPR_RESULT (t))
12015 finish_stmt_expr_expr (tmp, cur_stmt_expr);
12017 finish_expr_stmt (tmp);
12021 do_using_directive (USING_STMT_NAMESPACE (t));
12026 tree decl, pattern_decl;
12029 pattern_decl = decl = DECL_EXPR_DECL (t);
12030 if (TREE_CODE (decl) == LABEL_DECL)
12031 finish_label_decl (DECL_NAME (decl));
12032 else if (TREE_CODE (decl) == USING_DECL)
12034 tree scope = USING_DECL_SCOPE (decl);
12035 tree name = DECL_NAME (decl);
12038 scope = tsubst (scope, args, complain, in_decl);
12039 decl = lookup_qualified_name (scope, name,
12040 /*is_type_p=*/false,
12041 /*complain=*/false);
12042 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
12043 qualified_name_lookup_error (scope, name, decl, input_location);
12045 do_local_using_decl (decl, scope, name);
12049 init = DECL_INITIAL (decl);
12050 decl = tsubst (decl, args, complain, in_decl);
12051 if (decl != error_mark_node)
12053 /* By marking the declaration as instantiated, we avoid
12054 trying to instantiate it. Since instantiate_decl can't
12055 handle local variables, and since we've already done
12056 all that needs to be done, that's the right thing to
12058 if (TREE_CODE (decl) == VAR_DECL)
12059 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12060 if (TREE_CODE (decl) == VAR_DECL
12061 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
12062 /* Anonymous aggregates are a special case. */
12063 finish_anon_union (decl);
12066 int const_init = false;
12067 maybe_push_decl (decl);
12068 if (TREE_CODE (decl) == VAR_DECL
12069 && DECL_PRETTY_FUNCTION_P (decl))
12071 /* For __PRETTY_FUNCTION__ we have to adjust the
12073 const char *const name
12074 = cxx_printable_name (current_function_decl, 2);
12075 init = cp_fname_init (name, &TREE_TYPE (decl));
12079 tree t = RECUR (init);
12083 /* If we had an initializer but it
12084 instantiated to nothing,
12085 value-initialize the object. This will
12086 only occur when the initializer was a
12087 pack expansion where the parameter packs
12088 used in that expansion were of length
12090 init = build_value_init (TREE_TYPE (decl),
12092 if (TREE_CODE (init) == AGGR_INIT_EXPR)
12093 init = get_target_expr (init);
12099 if (TREE_CODE (decl) == VAR_DECL)
12100 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12102 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
12107 /* A DECL_EXPR can also be used as an expression, in the condition
12108 clause of an if/for/while construct. */
12113 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12114 RECUR (FOR_INIT_STMT (t));
12115 finish_for_init_stmt (stmt);
12116 tmp = RECUR (FOR_COND (t));
12117 finish_for_cond (tmp, stmt);
12118 tmp = RECUR (FOR_EXPR (t));
12119 finish_for_expr (tmp, stmt);
12120 RECUR (FOR_BODY (t));
12121 finish_for_stmt (stmt);
12124 case RANGE_FOR_STMT:
12127 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12128 decl = RANGE_FOR_DECL (t);
12129 decl = tsubst (decl, args, complain, in_decl);
12130 maybe_push_decl (decl);
12131 expr = RECUR (RANGE_FOR_EXPR (t));
12132 stmt = cp_convert_range_for (stmt, decl, expr);
12133 RECUR (RANGE_FOR_BODY (t));
12134 finish_for_stmt (stmt);
12139 stmt = begin_while_stmt ();
12140 tmp = RECUR (WHILE_COND (t));
12141 finish_while_stmt_cond (tmp, stmt);
12142 RECUR (WHILE_BODY (t));
12143 finish_while_stmt (stmt);
12147 stmt = begin_do_stmt ();
12148 RECUR (DO_BODY (t));
12149 finish_do_body (stmt);
12150 tmp = RECUR (DO_COND (t));
12151 finish_do_stmt (tmp, stmt);
12155 stmt = begin_if_stmt ();
12156 tmp = RECUR (IF_COND (t));
12157 finish_if_stmt_cond (tmp, stmt);
12158 RECUR (THEN_CLAUSE (t));
12159 finish_then_clause (stmt);
12161 if (ELSE_CLAUSE (t))
12163 begin_else_clause (stmt);
12164 RECUR (ELSE_CLAUSE (t));
12165 finish_else_clause (stmt);
12168 finish_if_stmt (stmt);
12172 if (BIND_EXPR_BODY_BLOCK (t))
12173 stmt = begin_function_body ();
12175 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
12176 ? BCS_TRY_BLOCK : 0);
12178 RECUR (BIND_EXPR_BODY (t));
12180 if (BIND_EXPR_BODY_BLOCK (t))
12181 finish_function_body (stmt);
12183 finish_compound_stmt (stmt);
12187 finish_break_stmt ();
12190 case CONTINUE_STMT:
12191 finish_continue_stmt ();
12195 stmt = begin_switch_stmt ();
12196 tmp = RECUR (SWITCH_STMT_COND (t));
12197 finish_switch_cond (tmp, stmt);
12198 RECUR (SWITCH_STMT_BODY (t));
12199 finish_switch_stmt (stmt);
12202 case CASE_LABEL_EXPR:
12203 finish_case_label (EXPR_LOCATION (t),
12204 RECUR (CASE_LOW (t)),
12205 RECUR (CASE_HIGH (t)));
12210 tree decl = LABEL_EXPR_LABEL (t);
12213 label = finish_label_stmt (DECL_NAME (decl));
12214 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
12215 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
12220 tmp = GOTO_DESTINATION (t);
12221 if (TREE_CODE (tmp) != LABEL_DECL)
12222 /* Computed goto's must be tsubst'd into. On the other hand,
12223 non-computed gotos must not be; the identifier in question
12224 will have no binding. */
12227 tmp = DECL_NAME (tmp);
12228 finish_goto_stmt (tmp);
12232 tmp = finish_asm_stmt
12233 (ASM_VOLATILE_P (t),
12234 RECUR (ASM_STRING (t)),
12235 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
12236 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
12237 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
12238 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
12240 tree asm_expr = tmp;
12241 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
12242 asm_expr = TREE_OPERAND (asm_expr, 0);
12243 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
12250 stmt = begin_try_block ();
12251 RECUR (TRY_STMTS (t));
12252 finish_cleanup_try_block (stmt);
12253 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
12257 tree compound_stmt = NULL_TREE;
12259 if (FN_TRY_BLOCK_P (t))
12260 stmt = begin_function_try_block (&compound_stmt);
12262 stmt = begin_try_block ();
12264 RECUR (TRY_STMTS (t));
12266 if (FN_TRY_BLOCK_P (t))
12267 finish_function_try_block (stmt);
12269 finish_try_block (stmt);
12271 RECUR (TRY_HANDLERS (t));
12272 if (FN_TRY_BLOCK_P (t))
12273 finish_function_handler_sequence (stmt, compound_stmt);
12275 finish_handler_sequence (stmt);
12281 tree decl = HANDLER_PARMS (t);
12285 decl = tsubst (decl, args, complain, in_decl);
12286 /* Prevent instantiate_decl from trying to instantiate
12287 this variable. We've already done all that needs to be
12289 if (decl != error_mark_node)
12290 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12292 stmt = begin_handler ();
12293 finish_handler_parms (decl, stmt);
12294 RECUR (HANDLER_BODY (t));
12295 finish_handler (stmt);
12300 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12303 case STATIC_ASSERT:
12306 tsubst_expr (STATIC_ASSERT_CONDITION (t),
12309 /*integral_constant_expression_p=*/true);
12310 finish_static_assert (condition,
12311 STATIC_ASSERT_MESSAGE (t),
12312 STATIC_ASSERT_SOURCE_LOCATION (t),
12313 /*member_p=*/false);
12318 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
12319 args, complain, in_decl);
12320 stmt = begin_omp_parallel ();
12321 RECUR (OMP_PARALLEL_BODY (t));
12322 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
12323 = OMP_PARALLEL_COMBINED (t);
12327 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
12328 args, complain, in_decl);
12329 stmt = begin_omp_task ();
12330 RECUR (OMP_TASK_BODY (t));
12331 finish_omp_task (tmp, stmt);
12336 tree clauses, body, pre_body;
12337 tree declv, initv, condv, incrv;
12340 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
12341 args, complain, in_decl);
12342 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12343 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12344 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12345 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12347 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
12348 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
12349 &clauses, args, complain, in_decl,
12350 integral_constant_expression_p);
12352 stmt = begin_omp_structured_block ();
12354 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
12355 if (TREE_VEC_ELT (initv, i) == NULL
12356 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
12357 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
12358 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
12360 tree init = RECUR (TREE_VEC_ELT (initv, i));
12361 gcc_assert (init == TREE_VEC_ELT (declv, i));
12362 TREE_VEC_ELT (initv, i) = NULL_TREE;
12366 tree decl_expr = TREE_VEC_ELT (initv, i);
12367 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
12368 gcc_assert (init != NULL);
12369 TREE_VEC_ELT (initv, i) = RECUR (init);
12370 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
12372 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
12375 pre_body = push_stmt_list ();
12376 RECUR (OMP_FOR_PRE_BODY (t));
12377 pre_body = pop_stmt_list (pre_body);
12379 body = push_stmt_list ();
12380 RECUR (OMP_FOR_BODY (t));
12381 body = pop_stmt_list (body);
12383 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
12384 body, pre_body, clauses);
12386 add_stmt (finish_omp_structured_block (stmt));
12392 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
12393 stmt = push_stmt_list ();
12394 RECUR (OMP_BODY (t));
12395 stmt = pop_stmt_list (stmt);
12398 OMP_BODY (t) = stmt;
12399 OMP_CLAUSES (t) = tmp;
12407 stmt = push_stmt_list ();
12408 RECUR (OMP_BODY (t));
12409 stmt = pop_stmt_list (stmt);
12412 OMP_BODY (t) = stmt;
12417 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
12419 tree op1 = TREE_OPERAND (t, 1);
12420 tree lhs = RECUR (TREE_OPERAND (op1, 0));
12421 tree rhs = RECUR (TREE_OPERAND (op1, 1));
12422 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
12426 case EXPR_PACK_EXPANSION:
12427 error ("invalid use of pack expansion expression");
12428 return error_mark_node;
12430 case NONTYPE_ARGUMENT_PACK:
12431 error ("use %<...%> to expand argument pack");
12432 return error_mark_node;
12435 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
12437 return tsubst_copy_and_build (t, args, complain, in_decl,
12438 /*function_p=*/false,
12439 integral_constant_expression_p);
12446 /* T is a postfix-expression that is not being used in a function
12447 call. Return the substituted version of T. */
12450 tsubst_non_call_postfix_expression (tree t, tree args,
12451 tsubst_flags_t complain,
12454 if (TREE_CODE (t) == SCOPE_REF)
12455 t = tsubst_qualified_id (t, args, complain, in_decl,
12456 /*done=*/false, /*address_p=*/false);
12458 t = tsubst_copy_and_build (t, args, complain, in_decl,
12459 /*function_p=*/false,
12460 /*integral_constant_expression_p=*/false);
12465 /* Like tsubst but deals with expressions and performs semantic
12466 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
12469 tsubst_copy_and_build (tree t,
12471 tsubst_flags_t complain,
12474 bool integral_constant_expression_p)
12476 #define RECUR(NODE) \
12477 tsubst_copy_and_build (NODE, args, complain, in_decl, \
12478 /*function_p=*/false, \
12479 integral_constant_expression_p)
12483 if (t == NULL_TREE || t == error_mark_node)
12486 switch (TREE_CODE (t))
12490 /* Fall through. */
12491 case IDENTIFIER_NODE:
12495 bool non_integral_constant_expression_p;
12496 const char *error_msg;
12498 if (IDENTIFIER_TYPENAME_P (t))
12500 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12501 t = mangle_conv_op_name_for_type (new_type);
12504 /* Look up the name. */
12505 decl = lookup_name (t);
12507 /* By convention, expressions use ERROR_MARK_NODE to indicate
12508 failure, not NULL_TREE. */
12509 if (decl == NULL_TREE)
12510 decl = error_mark_node;
12512 decl = finish_id_expression (t, decl, NULL_TREE,
12514 integral_constant_expression_p,
12515 /*allow_non_integral_constant_expression_p=*/false,
12516 &non_integral_constant_expression_p,
12517 /*template_p=*/false,
12519 /*address_p=*/false,
12520 /*template_arg_p=*/false,
12525 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
12526 decl = unqualified_name_lookup_error (decl);
12530 case TEMPLATE_ID_EXPR:
12533 tree templ = RECUR (TREE_OPERAND (t, 0));
12534 tree targs = TREE_OPERAND (t, 1);
12537 targs = tsubst_template_args (targs, args, complain, in_decl);
12539 if (TREE_CODE (templ) == COMPONENT_REF)
12541 object = TREE_OPERAND (templ, 0);
12542 templ = TREE_OPERAND (templ, 1);
12545 object = NULL_TREE;
12546 templ = lookup_template_function (templ, targs);
12549 return build3 (COMPONENT_REF, TREE_TYPE (templ),
12550 object, templ, NULL_TREE);
12552 return baselink_for_fns (templ);
12557 tree r = RECUR (TREE_OPERAND (t, 0));
12559 if (REFERENCE_REF_P (t))
12561 /* A type conversion to reference type will be enclosed in
12562 such an indirect ref, but the substitution of the cast
12563 will have also added such an indirect ref. */
12564 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
12565 r = convert_from_reference (r);
12568 r = build_x_indirect_ref (r, RO_UNARY_STAR, complain);
12574 (tsubst (TREE_TYPE (t), args, complain, in_decl),
12575 RECUR (TREE_OPERAND (t, 0)));
12578 case REINTERPRET_CAST_EXPR:
12579 case CONST_CAST_EXPR:
12580 case DYNAMIC_CAST_EXPR:
12581 case STATIC_CAST_EXPR:
12586 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12587 if (integral_constant_expression_p
12588 && !cast_valid_in_integral_constant_expression_p (type))
12590 if (complain & tf_error)
12591 error ("a cast to a type other than an integral or "
12592 "enumeration type cannot appear in a constant-expression");
12593 return error_mark_node;
12596 op = RECUR (TREE_OPERAND (t, 0));
12598 switch (TREE_CODE (t))
12601 return build_functional_cast (type, op, complain);
12602 case REINTERPRET_CAST_EXPR:
12603 return build_reinterpret_cast (type, op, complain);
12604 case CONST_CAST_EXPR:
12605 return build_const_cast (type, op, complain);
12606 case DYNAMIC_CAST_EXPR:
12607 return build_dynamic_cast (type, op, complain);
12608 case STATIC_CAST_EXPR:
12609 return build_static_cast (type, op, complain);
12611 gcc_unreachable ();
12615 case POSTDECREMENT_EXPR:
12616 case POSTINCREMENT_EXPR:
12617 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12618 args, complain, in_decl);
12619 return build_x_unary_op (TREE_CODE (t), op1, complain);
12621 case PREDECREMENT_EXPR:
12622 case PREINCREMENT_EXPR:
12626 case TRUTH_NOT_EXPR:
12627 case UNARY_PLUS_EXPR: /* Unary + */
12628 case REALPART_EXPR:
12629 case IMAGPART_EXPR:
12630 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
12634 op1 = TREE_OPERAND (t, 0);
12635 if (TREE_CODE (op1) == LABEL_DECL)
12636 return finish_label_address_expr (DECL_NAME (op1),
12637 EXPR_LOCATION (op1));
12638 if (TREE_CODE (op1) == SCOPE_REF)
12639 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
12640 /*done=*/true, /*address_p=*/true);
12642 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
12644 return build_x_unary_op (ADDR_EXPR, op1, complain);
12649 case TRUNC_DIV_EXPR:
12650 case CEIL_DIV_EXPR:
12651 case FLOOR_DIV_EXPR:
12652 case ROUND_DIV_EXPR:
12653 case EXACT_DIV_EXPR:
12657 case TRUNC_MOD_EXPR:
12658 case FLOOR_MOD_EXPR:
12659 case TRUTH_ANDIF_EXPR:
12660 case TRUTH_ORIF_EXPR:
12661 case TRUTH_AND_EXPR:
12662 case TRUTH_OR_EXPR:
12677 return build_x_binary_op
12679 RECUR (TREE_OPERAND (t, 0)),
12680 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
12682 : TREE_CODE (TREE_OPERAND (t, 0))),
12683 RECUR (TREE_OPERAND (t, 1)),
12684 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
12686 : TREE_CODE (TREE_OPERAND (t, 1))),
12687 /*overloaded_p=*/NULL,
12691 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
12692 /*address_p=*/false);
12694 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12695 args, complain, in_decl);
12696 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
12699 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12700 return tsubst_copy (t, args, complain, in_decl);
12704 op1 = TREE_OPERAND (t, 0);
12707 /* When there are no ARGS, we are trying to evaluate a
12708 non-dependent expression from the parser. Trying to do
12709 the substitutions may not work. */
12711 op1 = TREE_TYPE (op1);
12715 ++cp_unevaluated_operand;
12716 ++c_inhibit_evaluation_warnings;
12717 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12718 /*function_p=*/false,
12719 /*integral_constant_expression_p=*/false);
12720 --cp_unevaluated_operand;
12721 --c_inhibit_evaluation_warnings;
12724 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
12725 complain & tf_error);
12727 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
12728 complain & tf_error);
12730 case AT_ENCODE_EXPR:
12732 op1 = TREE_OPERAND (t, 0);
12733 ++cp_unevaluated_operand;
12734 ++c_inhibit_evaluation_warnings;
12735 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12736 /*function_p=*/false,
12737 /*integral_constant_expression_p=*/false);
12738 --cp_unevaluated_operand;
12739 --c_inhibit_evaluation_warnings;
12740 return objc_build_encode_expr (op1);
12743 case NOEXCEPT_EXPR:
12744 op1 = TREE_OPERAND (t, 0);
12745 ++cp_unevaluated_operand;
12746 ++c_inhibit_evaluation_warnings;
12747 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12748 /*function_p=*/false,
12749 /*integral_constant_expression_p=*/false);
12750 --cp_unevaluated_operand;
12751 --c_inhibit_evaluation_warnings;
12752 return finish_noexcept_expr (op1, complain);
12756 tree r = build_x_modify_expr
12757 (RECUR (TREE_OPERAND (t, 0)),
12758 TREE_CODE (TREE_OPERAND (t, 1)),
12759 RECUR (TREE_OPERAND (t, 2)),
12761 /* TREE_NO_WARNING must be set if either the expression was
12762 parenthesized or it uses an operator such as >>= rather
12763 than plain assignment. In the former case, it was already
12764 set and must be copied. In the latter case,
12765 build_x_modify_expr sets it and it must not be reset
12767 if (TREE_NO_WARNING (t))
12768 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12773 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12774 args, complain, in_decl);
12775 /* Remember that there was a reference to this entity. */
12778 return build_x_arrow (op1);
12782 tree placement = RECUR (TREE_OPERAND (t, 0));
12783 tree init = RECUR (TREE_OPERAND (t, 3));
12784 VEC(tree,gc) *placement_vec;
12785 VEC(tree,gc) *init_vec;
12788 if (placement == NULL_TREE)
12789 placement_vec = NULL;
12792 placement_vec = make_tree_vector ();
12793 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
12794 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
12797 /* If there was an initializer in the original tree, but it
12798 instantiated to an empty list, then we should pass a
12799 non-NULL empty vector to tell build_new that it was an
12800 empty initializer() rather than no initializer. This can
12801 only happen when the initializer is a pack expansion whose
12802 parameter packs are of length zero. */
12803 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
12807 init_vec = make_tree_vector ();
12808 if (init == void_zero_node)
12809 gcc_assert (init_vec != NULL);
12812 for (; init != NULL_TREE; init = TREE_CHAIN (init))
12813 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
12817 ret = build_new (&placement_vec,
12818 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
12819 RECUR (TREE_OPERAND (t, 2)),
12821 NEW_EXPR_USE_GLOBAL (t),
12824 if (placement_vec != NULL)
12825 release_tree_vector (placement_vec);
12826 if (init_vec != NULL)
12827 release_tree_vector (init_vec);
12833 return delete_sanity
12834 (RECUR (TREE_OPERAND (t, 0)),
12835 RECUR (TREE_OPERAND (t, 1)),
12836 DELETE_EXPR_USE_VEC (t),
12837 DELETE_EXPR_USE_GLOBAL (t));
12839 case COMPOUND_EXPR:
12840 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
12841 RECUR (TREE_OPERAND (t, 1)),
12847 VEC(tree,gc) *call_args;
12848 unsigned int nargs, i;
12853 function = CALL_EXPR_FN (t);
12854 /* When we parsed the expression, we determined whether or
12855 not Koenig lookup should be performed. */
12856 koenig_p = KOENIG_LOOKUP_P (t);
12857 if (TREE_CODE (function) == SCOPE_REF)
12859 qualified_p = true;
12860 function = tsubst_qualified_id (function, args, complain, in_decl,
12862 /*address_p=*/false);
12866 if (TREE_CODE (function) == COMPONENT_REF)
12868 tree op = TREE_OPERAND (function, 1);
12870 qualified_p = (TREE_CODE (op) == SCOPE_REF
12871 || (BASELINK_P (op)
12872 && BASELINK_QUALIFIED_P (op)));
12875 qualified_p = false;
12877 function = tsubst_copy_and_build (function, args, complain,
12880 integral_constant_expression_p);
12882 if (BASELINK_P (function))
12883 qualified_p = true;
12886 nargs = call_expr_nargs (t);
12887 call_args = make_tree_vector ();
12888 for (i = 0; i < nargs; ++i)
12890 tree arg = CALL_EXPR_ARG (t, i);
12892 if (!PACK_EXPANSION_P (arg))
12893 VEC_safe_push (tree, gc, call_args,
12894 RECUR (CALL_EXPR_ARG (t, i)));
12897 /* Expand the pack expansion and push each entry onto
12899 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
12900 if (TREE_CODE (arg) == TREE_VEC)
12902 unsigned int len, j;
12904 len = TREE_VEC_LENGTH (arg);
12905 for (j = 0; j < len; ++j)
12907 tree value = TREE_VEC_ELT (arg, j);
12908 if (value != NULL_TREE)
12909 value = convert_from_reference (value);
12910 VEC_safe_push (tree, gc, call_args, value);
12915 /* A partial substitution. Add one entry. */
12916 VEC_safe_push (tree, gc, call_args, arg);
12921 /* We do not perform argument-dependent lookup if normal
12922 lookup finds a non-function, in accordance with the
12923 expected resolution of DR 218. */
12925 && ((is_overloaded_fn (function)
12926 /* If lookup found a member function, the Koenig lookup is
12927 not appropriate, even if an unqualified-name was used
12928 to denote the function. */
12929 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
12930 || TREE_CODE (function) == IDENTIFIER_NODE)
12931 /* Only do this when substitution turns a dependent call
12932 into a non-dependent call. */
12933 && type_dependent_expression_p_push (t)
12934 && !any_type_dependent_arguments_p (call_args))
12935 function = perform_koenig_lookup (function, call_args, false);
12937 if (TREE_CODE (function) == IDENTIFIER_NODE)
12939 unqualified_name_lookup_error (function);
12940 release_tree_vector (call_args);
12941 return error_mark_node;
12944 /* Remember that there was a reference to this entity. */
12945 if (DECL_P (function))
12946 mark_used (function);
12948 if (TREE_CODE (function) == OFFSET_REF)
12949 ret = build_offset_ref_call_from_tree (function, &call_args);
12950 else if (TREE_CODE (function) == COMPONENT_REF)
12952 tree instance = TREE_OPERAND (function, 0);
12953 tree fn = TREE_OPERAND (function, 1);
12955 if (processing_template_decl
12956 && (type_dependent_expression_p (instance)
12957 || (!BASELINK_P (fn)
12958 && TREE_CODE (fn) != FIELD_DECL)
12959 || type_dependent_expression_p (fn)
12960 || any_type_dependent_arguments_p (call_args)))
12961 ret = build_nt_call_vec (function, call_args);
12962 else if (!BASELINK_P (fn))
12963 ret = finish_call_expr (function, &call_args,
12964 /*disallow_virtual=*/false,
12965 /*koenig_p=*/false,
12968 ret = (build_new_method_call
12970 &call_args, NULL_TREE,
12971 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
12976 ret = finish_call_expr (function, &call_args,
12977 /*disallow_virtual=*/qualified_p,
12981 release_tree_vector (call_args);
12987 return build_x_conditional_expr
12988 (RECUR (TREE_OPERAND (t, 0)),
12989 RECUR (TREE_OPERAND (t, 1)),
12990 RECUR (TREE_OPERAND (t, 2)),
12993 case PSEUDO_DTOR_EXPR:
12994 return finish_pseudo_destructor_expr
12995 (RECUR (TREE_OPERAND (t, 0)),
12996 RECUR (TREE_OPERAND (t, 1)),
12997 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl));
13001 tree purpose, value, chain;
13003 if (t == void_list_node)
13006 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
13007 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
13009 /* We have pack expansions, so expand those and
13010 create a new list out of it. */
13011 tree purposevec = NULL_TREE;
13012 tree valuevec = NULL_TREE;
13016 /* Expand the argument expressions. */
13017 if (TREE_PURPOSE (t))
13018 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
13019 complain, in_decl);
13020 if (TREE_VALUE (t))
13021 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
13022 complain, in_decl);
13024 /* Build the rest of the list. */
13025 chain = TREE_CHAIN (t);
13026 if (chain && chain != void_type_node)
13027 chain = RECUR (chain);
13029 /* Determine the number of arguments. */
13030 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
13032 len = TREE_VEC_LENGTH (purposevec);
13033 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
13035 else if (TREE_CODE (valuevec) == TREE_VEC)
13036 len = TREE_VEC_LENGTH (valuevec);
13039 /* Since we only performed a partial substitution into
13040 the argument pack, we only return a single list
13042 if (purposevec == TREE_PURPOSE (t)
13043 && valuevec == TREE_VALUE (t)
13044 && chain == TREE_CHAIN (t))
13047 return tree_cons (purposevec, valuevec, chain);
13050 /* Convert the argument vectors into a TREE_LIST */
13054 /* Grab the Ith values. */
13056 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
13059 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
13062 /* Build the list (backwards). */
13063 chain = tree_cons (purpose, value, chain);
13069 purpose = TREE_PURPOSE (t);
13071 purpose = RECUR (purpose);
13072 value = TREE_VALUE (t);
13074 value = RECUR (value);
13075 chain = TREE_CHAIN (t);
13076 if (chain && chain != void_type_node)
13077 chain = RECUR (chain);
13078 if (purpose == TREE_PURPOSE (t)
13079 && value == TREE_VALUE (t)
13080 && chain == TREE_CHAIN (t))
13082 return tree_cons (purpose, value, chain);
13085 case COMPONENT_REF:
13091 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13092 args, complain, in_decl);
13093 /* Remember that there was a reference to this entity. */
13094 if (DECL_P (object))
13095 mark_used (object);
13096 object_type = TREE_TYPE (object);
13098 member = TREE_OPERAND (t, 1);
13099 if (BASELINK_P (member))
13100 member = tsubst_baselink (member,
13101 non_reference (TREE_TYPE (object)),
13102 args, complain, in_decl);
13104 member = tsubst_copy (member, args, complain, in_decl);
13105 if (member == error_mark_node)
13106 return error_mark_node;
13108 if (object_type && !CLASS_TYPE_P (object_type))
13110 if (SCALAR_TYPE_P (object_type))
13112 tree s = NULL_TREE;
13113 tree dtor = member;
13115 if (TREE_CODE (dtor) == SCOPE_REF)
13117 s = TREE_OPERAND (dtor, 0);
13118 dtor = TREE_OPERAND (dtor, 1);
13120 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
13122 dtor = TREE_OPERAND (dtor, 0);
13124 return finish_pseudo_destructor_expr (object, s, dtor);
13128 else if (TREE_CODE (member) == SCOPE_REF
13129 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
13134 /* Lookup the template functions now that we know what the
13136 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
13137 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
13138 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
13139 /*is_type_p=*/false,
13140 /*complain=*/false);
13141 if (BASELINK_P (member))
13143 BASELINK_FUNCTIONS (member)
13144 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
13146 member = (adjust_result_of_qualified_name_lookup
13147 (member, BINFO_TYPE (BASELINK_BINFO (member)),
13152 qualified_name_lookup_error (object_type, tmpl, member,
13154 return error_mark_node;
13157 else if (TREE_CODE (member) == SCOPE_REF
13158 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
13159 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
13161 if (complain & tf_error)
13163 if (TYPE_P (TREE_OPERAND (member, 0)))
13164 error ("%qT is not a class or namespace",
13165 TREE_OPERAND (member, 0));
13167 error ("%qD is not a class or namespace",
13168 TREE_OPERAND (member, 0));
13170 return error_mark_node;
13172 else if (TREE_CODE (member) == FIELD_DECL)
13173 return finish_non_static_data_member (member, object, NULL_TREE);
13175 return finish_class_member_access_expr (object, member,
13176 /*template_p=*/false,
13182 (RECUR (TREE_OPERAND (t, 0)));
13186 VEC(constructor_elt,gc) *n;
13187 constructor_elt *ce;
13188 unsigned HOST_WIDE_INT idx;
13189 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13190 bool process_index_p;
13192 bool need_copy_p = false;
13195 if (type == error_mark_node)
13196 return error_mark_node;
13198 /* digest_init will do the wrong thing if we let it. */
13199 if (type && TYPE_PTRMEMFUNC_P (type))
13202 /* We do not want to process the index of aggregate
13203 initializers as they are identifier nodes which will be
13204 looked up by digest_init. */
13205 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
13207 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
13208 newlen = VEC_length (constructor_elt, n);
13209 FOR_EACH_VEC_ELT (constructor_elt, n, idx, ce)
13211 if (ce->index && process_index_p)
13212 ce->index = RECUR (ce->index);
13214 if (PACK_EXPANSION_P (ce->value))
13216 /* Substitute into the pack expansion. */
13217 ce->value = tsubst_pack_expansion (ce->value, args, complain,
13220 if (ce->value == error_mark_node)
13222 else if (TREE_VEC_LENGTH (ce->value) == 1)
13223 /* Just move the argument into place. */
13224 ce->value = TREE_VEC_ELT (ce->value, 0);
13227 /* Update the length of the final CONSTRUCTOR
13228 arguments vector, and note that we will need to
13230 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
13231 need_copy_p = true;
13235 ce->value = RECUR (ce->value);
13240 VEC(constructor_elt,gc) *old_n = n;
13242 n = VEC_alloc (constructor_elt, gc, newlen);
13243 FOR_EACH_VEC_ELT (constructor_elt, old_n, idx, ce)
13245 if (TREE_CODE (ce->value) == TREE_VEC)
13247 int i, len = TREE_VEC_LENGTH (ce->value);
13248 for (i = 0; i < len; ++i)
13249 CONSTRUCTOR_APPEND_ELT (n, 0,
13250 TREE_VEC_ELT (ce->value, i));
13253 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
13257 r = build_constructor (init_list_type_node, n);
13258 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
13260 if (TREE_HAS_CONSTRUCTOR (t))
13261 return finish_compound_literal (type, r);
13263 TREE_TYPE (r) = type;
13269 tree operand_0 = TREE_OPERAND (t, 0);
13270 if (TYPE_P (operand_0))
13272 operand_0 = tsubst (operand_0, args, complain, in_decl);
13273 return get_typeid (operand_0);
13277 operand_0 = RECUR (operand_0);
13278 return build_typeid (operand_0);
13289 tree r = tsubst_copy (t, args, complain, in_decl);
13291 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
13292 /* If the original type was a reference, we'll be wrapped in
13293 the appropriate INDIRECT_REF. */
13294 r = convert_from_reference (r);
13299 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
13300 tsubst (TREE_TYPE (t), args, complain, in_decl));
13302 case OFFSETOF_EXPR:
13303 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
13307 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
13308 complain, in_decl);
13310 tree type2 = TRAIT_EXPR_TYPE2 (t);
13312 type2 = tsubst_copy (type2, args, complain, in_decl);
13314 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
13319 tree old_stmt_expr = cur_stmt_expr;
13320 tree stmt_expr = begin_stmt_expr ();
13322 cur_stmt_expr = stmt_expr;
13323 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
13324 integral_constant_expression_p);
13325 stmt_expr = finish_stmt_expr (stmt_expr, false);
13326 cur_stmt_expr = old_stmt_expr;
13328 /* If the resulting list of expression statement is empty,
13329 fold it further into void_zero_node. */
13330 if (empty_expr_stmt_p (stmt_expr))
13331 stmt_expr = void_zero_node;
13337 t = tsubst_copy (t, args, complain, in_decl);
13338 /* As in finish_id_expression, we resolve enumeration constants
13339 to their underlying values. */
13340 if (TREE_CODE (t) == CONST_DECL)
13342 used_types_insert (TREE_TYPE (t));
13343 return DECL_INITIAL (t);
13349 tree r = build_lambda_expr ();
13351 tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13352 TREE_TYPE (r) = type;
13353 CLASSTYPE_LAMBDA_EXPR (type) = r;
13355 LAMBDA_EXPR_LOCATION (r)
13356 = LAMBDA_EXPR_LOCATION (t);
13357 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
13358 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
13359 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
13360 LAMBDA_EXPR_DISCRIMINATOR (r)
13361 = (LAMBDA_EXPR_DISCRIMINATOR (t));
13362 LAMBDA_EXPR_CAPTURE_LIST (r)
13363 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
13364 LAMBDA_EXPR_THIS_CAPTURE (r)
13365 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t));
13366 LAMBDA_EXPR_EXTRA_SCOPE (r)
13367 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
13369 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
13370 determine_visibility (TYPE_NAME (type));
13371 /* Now that we know visibility, instantiate the type so we have a
13372 declaration of the op() for later calls to lambda_function. */
13373 complete_type (type);
13375 type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
13377 apply_lambda_return_type (r, type);
13379 return build_lambda_object (r);
13383 /* We can get here for a constant initializer of non-dependent type.
13384 FIXME stop folding in cp_parser_initializer_clause. */
13385 gcc_assert (TREE_CONSTANT (t));
13387 tree r = get_target_expr (RECUR (TARGET_EXPR_INITIAL (t)));
13388 TREE_CONSTANT (r) = true;
13393 /* Handle Objective-C++ constructs, if appropriate. */
13396 = objcp_tsubst_copy_and_build (t, args, complain,
13397 in_decl, /*function_p=*/false);
13401 return tsubst_copy (t, args, complain, in_decl);
13407 /* Verify that the instantiated ARGS are valid. For type arguments,
13408 make sure that the type's linkage is ok. For non-type arguments,
13409 make sure they are constants if they are integral or enumerations.
13410 Emit an error under control of COMPLAIN, and return TRUE on error. */
13413 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
13415 if (ARGUMENT_PACK_P (t))
13417 tree vec = ARGUMENT_PACK_ARGS (t);
13418 int len = TREE_VEC_LENGTH (vec);
13419 bool result = false;
13422 for (i = 0; i < len; ++i)
13423 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
13427 else if (TYPE_P (t))
13429 /* [basic.link]: A name with no linkage (notably, the name
13430 of a class or enumeration declared in a local scope)
13431 shall not be used to declare an entity with linkage.
13432 This implies that names with no linkage cannot be used as
13435 DR 757 relaxes this restriction for C++0x. */
13436 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
13437 : no_linkage_check (t, /*relaxed_p=*/false));
13441 /* DR 488 makes use of a type with no linkage cause
13442 type deduction to fail. */
13443 if (complain & tf_error)
13445 if (TYPE_ANONYMOUS_P (nt))
13446 error ("%qT is/uses anonymous type", t);
13448 error ("template argument for %qD uses local type %qT",
13453 /* In order to avoid all sorts of complications, we do not
13454 allow variably-modified types as template arguments. */
13455 else if (variably_modified_type_p (t, NULL_TREE))
13457 if (complain & tf_error)
13458 error ("%qT is a variably modified type", t);
13462 /* A non-type argument of integral or enumerated type must be a
13464 else if (TREE_TYPE (t)
13465 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
13466 && !TREE_CONSTANT (t))
13468 if (complain & tf_error)
13469 error ("integral expression %qE is not constant", t);
13476 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
13478 int ix, len = DECL_NTPARMS (tmpl);
13479 bool result = false;
13481 for (ix = 0; ix != len; ix++)
13483 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
13486 if (result && (complain & tf_error))
13487 error (" trying to instantiate %qD", tmpl);
13491 /* Instantiate the indicated variable or function template TMPL with
13492 the template arguments in TARG_PTR. */
13495 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
13497 tree targ_ptr = orig_args;
13501 HOST_WIDE_INT saved_processing_template_decl;
13503 if (tmpl == error_mark_node)
13504 return error_mark_node;
13506 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
13508 /* If this function is a clone, handle it specially. */
13509 if (DECL_CLONED_FUNCTION_P (tmpl))
13514 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
13515 DECL_CLONED_FUNCTION. */
13516 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
13517 targ_ptr, complain);
13518 if (spec == error_mark_node)
13519 return error_mark_node;
13521 /* Look for the clone. */
13522 FOR_EACH_CLONE (clone, spec)
13523 if (DECL_NAME (clone) == DECL_NAME (tmpl))
13525 /* We should always have found the clone by now. */
13526 gcc_unreachable ();
13530 /* Check to see if we already have this specialization. */
13531 gen_tmpl = most_general_template (tmpl);
13532 if (tmpl != gen_tmpl)
13533 /* The TMPL is a partial instantiation. To get a full set of
13534 arguments we must add the arguments used to perform the
13535 partial instantiation. */
13536 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
13539 /* It would be nice to avoid hashing here and then again in tsubst_decl,
13540 but it doesn't seem to be on the hot path. */
13541 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
13543 gcc_assert (tmpl == gen_tmpl
13544 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
13546 || fndecl == NULL_TREE);
13548 if (spec != NULL_TREE)
13551 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
13553 return error_mark_node;
13555 /* We are building a FUNCTION_DECL, during which the access of its
13556 parameters and return types have to be checked. However this
13557 FUNCTION_DECL which is the desired context for access checking
13558 is not built yet. We solve this chicken-and-egg problem by
13559 deferring all checks until we have the FUNCTION_DECL. */
13560 push_deferring_access_checks (dk_deferred);
13562 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
13563 (because, for example, we have encountered a non-dependent
13564 function call in the body of a template function and must now
13565 determine which of several overloaded functions will be called),
13566 within the instantiation itself we are not processing a
13568 saved_processing_template_decl = processing_template_decl;
13569 processing_template_decl = 0;
13570 /* Substitute template parameters to obtain the specialization. */
13571 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
13572 targ_ptr, complain, gen_tmpl);
13573 processing_template_decl = saved_processing_template_decl;
13574 if (fndecl == error_mark_node)
13575 return error_mark_node;
13577 /* Now we know the specialization, compute access previously
13579 push_access_scope (fndecl);
13581 /* Some typedefs referenced from within the template code need to be access
13582 checked at template instantiation time, i.e now. These types were
13583 added to the template at parsing time. Let's get those and perfom
13584 the acces checks then. */
13585 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
13586 perform_deferred_access_checks ();
13587 pop_access_scope (fndecl);
13588 pop_deferring_access_checks ();
13590 /* The DECL_TI_TEMPLATE should always be the immediate parent
13591 template, not the most general template. */
13592 DECL_TI_TEMPLATE (fndecl) = tmpl;
13594 /* If we've just instantiated the main entry point for a function,
13595 instantiate all the alternate entry points as well. We do this
13596 by cloning the instantiation of the main entry point, not by
13597 instantiating the template clones. */
13598 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
13599 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
13604 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
13605 NARGS elements of the arguments that are being used when calling
13606 it. TARGS is a vector into which the deduced template arguments
13609 Return zero for success, 2 for an incomplete match that doesn't resolve
13610 all the types, and 1 for complete failure. An error message will be
13611 printed only for an incomplete match.
13613 If FN is a conversion operator, or we are trying to produce a specific
13614 specialization, RETURN_TYPE is the return type desired.
13616 The EXPLICIT_TARGS are explicit template arguments provided via a
13619 The parameter STRICT is one of:
13622 We are deducing arguments for a function call, as in
13623 [temp.deduct.call].
13626 We are deducing arguments for a conversion function, as in
13627 [temp.deduct.conv].
13630 We are deducing arguments when doing an explicit instantiation
13631 as in [temp.explicit], when determining an explicit specialization
13632 as in [temp.expl.spec], or when taking the address of a function
13633 template, as in [temp.deduct.funcaddr]. */
13636 fn_type_unification (tree fn,
13637 tree explicit_targs,
13640 unsigned int nargs,
13642 unification_kind_t strict,
13648 bool incomplete_argument_packs_p = false;
13650 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
13652 fntype = TREE_TYPE (fn);
13653 if (explicit_targs)
13657 The specified template arguments must match the template
13658 parameters in kind (i.e., type, nontype, template), and there
13659 must not be more arguments than there are parameters;
13660 otherwise type deduction fails.
13662 Nontype arguments must match the types of the corresponding
13663 nontype template parameters, or must be convertible to the
13664 types of the corresponding nontype parameters as specified in
13665 _temp.arg.nontype_, otherwise type deduction fails.
13667 All references in the function type of the function template
13668 to the corresponding template parameters are replaced by the
13669 specified template argument values. If a substitution in a
13670 template parameter or in the function type of the function
13671 template results in an invalid type, type deduction fails. */
13672 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
13673 int i, len = TREE_VEC_LENGTH (tparms);
13674 tree converted_args;
13675 bool incomplete = false;
13677 if (explicit_targs == error_mark_node)
13681 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
13682 /*require_all_args=*/false,
13683 /*use_default_args=*/false));
13684 if (converted_args == error_mark_node)
13687 /* Substitute the explicit args into the function type. This is
13688 necessary so that, for instance, explicitly declared function
13689 arguments can match null pointed constants. If we were given
13690 an incomplete set of explicit args, we must not do semantic
13691 processing during substitution as we could create partial
13693 for (i = 0; i < len; i++)
13695 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13696 bool parameter_pack = false;
13698 /* Dig out the actual parm. */
13699 if (TREE_CODE (parm) == TYPE_DECL
13700 || TREE_CODE (parm) == TEMPLATE_DECL)
13702 parm = TREE_TYPE (parm);
13703 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
13705 else if (TREE_CODE (parm) == PARM_DECL)
13707 parm = DECL_INITIAL (parm);
13708 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
13711 if (parameter_pack)
13715 template_parm_level_and_index (parm, &level, &idx);
13717 /* Mark the argument pack as "incomplete". We could
13718 still deduce more arguments during unification. */
13719 targ = TMPL_ARG (converted_args, level, idx);
13722 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
13723 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
13724 = ARGUMENT_PACK_ARGS (targ);
13727 /* We have some incomplete argument packs. */
13728 incomplete_argument_packs_p = true;
13732 if (incomplete_argument_packs_p)
13733 /* Any substitution is guaranteed to be incomplete if there
13734 are incomplete argument packs, because we can still deduce
13738 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
13740 processing_template_decl += incomplete;
13741 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
13742 processing_template_decl -= incomplete;
13744 if (fntype == error_mark_node)
13747 /* Place the explicitly specified arguments in TARGS. */
13748 for (i = NUM_TMPL_ARGS (converted_args); i--;)
13749 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
13752 /* Never do unification on the 'this' parameter. */
13753 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
13759 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
13760 new_args = XALLOCAVEC (tree, nargs + 1);
13761 new_args[0] = return_type;
13762 memcpy (new_args + 1, args, nargs * sizeof (tree));
13767 /* We allow incomplete unification without an error message here
13768 because the standard doesn't seem to explicitly prohibit it. Our
13769 callers must be ready to deal with unification failures in any
13771 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
13772 targs, parms, args, nargs, /*subr=*/0,
13775 if (result == 0 && incomplete_argument_packs_p)
13777 int i, len = NUM_TMPL_ARGS (targs);
13779 /* Clear the "incomplete" flags on all argument packs. */
13780 for (i = 0; i < len; i++)
13782 tree arg = TREE_VEC_ELT (targs, i);
13783 if (ARGUMENT_PACK_P (arg))
13785 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
13786 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
13791 /* Now that we have bindings for all of the template arguments,
13792 ensure that the arguments deduced for the template template
13793 parameters have compatible template parameter lists. We cannot
13794 check this property before we have deduced all template
13795 arguments, because the template parameter types of a template
13796 template parameter might depend on prior template parameters
13797 deduced after the template template parameter. The following
13798 ill-formed example illustrates this issue:
13800 template<typename T, template<T> class C> void f(C<5>, T);
13802 template<int N> struct X {};
13805 f(X<5>(), 5l); // error: template argument deduction fails
13808 The template parameter list of 'C' depends on the template type
13809 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
13810 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
13811 time that we deduce 'C'. */
13813 && !template_template_parm_bindings_ok_p
13814 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
13818 /* All is well so far. Now, check:
13822 When all template arguments have been deduced, all uses of
13823 template parameters in nondeduced contexts are replaced with
13824 the corresponding deduced argument values. If the
13825 substitution results in an invalid type, as described above,
13826 type deduction fails. */
13828 tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
13829 if (substed == error_mark_node)
13832 /* If we're looking for an exact match, check that what we got
13833 is indeed an exact match. It might not be if some template
13834 parameters are used in non-deduced contexts. */
13835 if (strict == DEDUCE_EXACT)
13840 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
13842 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
13843 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
13844 if (!same_type_p (args[i], TREE_VALUE (sarg)))
13852 /* Adjust types before performing type deduction, as described in
13853 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
13854 sections are symmetric. PARM is the type of a function parameter
13855 or the return type of the conversion function. ARG is the type of
13856 the argument passed to the call, or the type of the value
13857 initialized with the result of the conversion function.
13858 ARG_EXPR is the original argument expression, which may be null. */
13861 maybe_adjust_types_for_deduction (unification_kind_t strict,
13875 /* Swap PARM and ARG throughout the remainder of this
13876 function; the handling is precisely symmetric since PARM
13877 will initialize ARG rather than vice versa. */
13885 /* Core issue #873: Do the DR606 thing (see below) for these cases,
13886 too, but here handle it by stripping the reference from PARM
13887 rather than by adding it to ARG. */
13888 if (TREE_CODE (*parm) == REFERENCE_TYPE
13889 && TYPE_REF_IS_RVALUE (*parm)
13890 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13891 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13892 && TREE_CODE (*arg) == REFERENCE_TYPE
13893 && !TYPE_REF_IS_RVALUE (*arg))
13894 *parm = TREE_TYPE (*parm);
13895 /* Nothing else to do in this case. */
13899 gcc_unreachable ();
13902 if (TREE_CODE (*parm) != REFERENCE_TYPE)
13904 /* [temp.deduct.call]
13906 If P is not a reference type:
13908 --If A is an array type, the pointer type produced by the
13909 array-to-pointer standard conversion (_conv.array_) is
13910 used in place of A for type deduction; otherwise,
13912 --If A is a function type, the pointer type produced by
13913 the function-to-pointer standard conversion
13914 (_conv.func_) is used in place of A for type deduction;
13917 --If A is a cv-qualified type, the top level
13918 cv-qualifiers of A's type are ignored for type
13920 if (TREE_CODE (*arg) == ARRAY_TYPE)
13921 *arg = build_pointer_type (TREE_TYPE (*arg));
13922 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
13923 *arg = build_pointer_type (*arg);
13925 *arg = TYPE_MAIN_VARIANT (*arg);
13928 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
13929 of the form T&&, where T is a template parameter, and the argument
13930 is an lvalue, T is deduced as A& */
13931 if (TREE_CODE (*parm) == REFERENCE_TYPE
13932 && TYPE_REF_IS_RVALUE (*parm)
13933 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13934 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13935 && arg_expr && real_lvalue_p (arg_expr))
13936 *arg = build_reference_type (*arg);
13938 /* [temp.deduct.call]
13940 If P is a cv-qualified type, the top level cv-qualifiers
13941 of P's type are ignored for type deduction. If P is a
13942 reference type, the type referred to by P is used for
13944 *parm = TYPE_MAIN_VARIANT (*parm);
13945 if (TREE_CODE (*parm) == REFERENCE_TYPE)
13947 *parm = TREE_TYPE (*parm);
13948 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
13951 /* DR 322. For conversion deduction, remove a reference type on parm
13952 too (which has been swapped into ARG). */
13953 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
13954 *arg = TREE_TYPE (*arg);
13959 /* Most parms like fn_type_unification.
13961 If SUBR is 1, we're being called recursively (to unify the
13962 arguments of a function or method parameter of a function
13966 type_unification_real (tree tparms,
13970 unsigned int xnargs,
13972 unification_kind_t strict,
13975 tree parm, arg, arg_expr;
13977 int ntparms = TREE_VEC_LENGTH (tparms);
13979 int saw_undeduced = 0;
13982 unsigned int nargs;
13985 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
13986 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
13987 gcc_assert (ntparms > 0);
13989 /* Reset the number of non-defaulted template arguments contained
13991 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
13996 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
13997 | UNIFY_ALLOW_DERIVED);
14001 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
14005 sub_strict = UNIFY_ALLOW_NONE;
14009 gcc_unreachable ();
14018 while (parms && parms != void_list_node
14021 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
14024 parm = TREE_VALUE (parms);
14025 parms = TREE_CHAIN (parms);
14030 if (arg == error_mark_node)
14032 if (arg == unknown_type_node)
14033 /* We can't deduce anything from this, but we might get all the
14034 template args from other function args. */
14037 /* Conversions will be performed on a function argument that
14038 corresponds with a function parameter that contains only
14039 non-deducible template parameters and explicitly specified
14040 template parameters. */
14041 if (!uses_template_parms (parm))
14046 type = TREE_TYPE (arg);
14050 if (same_type_p (parm, type))
14052 if (strict != DEDUCE_EXACT
14053 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
14062 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
14063 if (type_unknown_p (arg))
14065 /* [temp.deduct.type]
14067 A template-argument can be deduced from a pointer to
14068 function or pointer to member function argument if
14069 the set of overloaded functions does not contain
14070 function templates and at most one of a set of
14071 overloaded functions provides a unique match. */
14072 if (resolve_overloaded_unification
14073 (tparms, targs, parm, arg, strict, sub_strict))
14079 arg = unlowered_expr_type (arg);
14080 if (arg == error_mark_node)
14085 int arg_strict = sub_strict;
14088 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
14091 if (arg == init_list_type_node && arg_expr)
14093 if (unify (tparms, targs, parm, arg, arg_strict))
14100 && parms != void_list_node
14101 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
14103 /* Unify the remaining arguments with the pack expansion type. */
14105 tree parmvec = make_tree_vec (1);
14107 /* Allocate a TREE_VEC and copy in all of the arguments */
14108 argvec = make_tree_vec (nargs - ia);
14109 for (i = 0; ia < nargs; ++ia, ++i)
14110 TREE_VEC_ELT (argvec, i) = args[ia];
14112 /* Copy the parameter into parmvec. */
14113 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
14114 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
14115 /*call_args_p=*/true, /*subr=*/subr))
14118 /* Advance to the end of the list of parameters. */
14119 parms = TREE_CHAIN (parms);
14122 /* Fail if we've reached the end of the parm list, and more args
14123 are present, and the parm list isn't variadic. */
14124 if (ia < nargs && parms == void_list_node)
14126 /* Fail if parms are left and they don't have default values. */
14127 if (parms && parms != void_list_node
14128 && TREE_PURPOSE (parms) == NULL_TREE)
14132 for (i = 0; i < ntparms; i++)
14133 if (!TREE_VEC_ELT (targs, i))
14137 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
14140 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
14142 /* If this is an undeduced nontype parameter that depends on
14143 a type parameter, try another pass; its type may have been
14144 deduced from a later argument than the one from which
14145 this parameter can be deduced. */
14146 if (TREE_CODE (tparm) == PARM_DECL
14147 && uses_template_parms (TREE_TYPE (tparm))
14148 && !saw_undeduced++)
14151 /* Core issue #226 (C++0x) [temp.deduct]:
14153 If a template argument has not been deduced, its
14154 default template argument, if any, is used.
14156 When we are in C++98 mode, TREE_PURPOSE will either
14157 be NULL_TREE or ERROR_MARK_NODE, so we do not need
14158 to explicitly check cxx_dialect here. */
14159 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
14161 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
14162 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
14163 arg = tsubst_template_arg (arg, targs, tf_none, NULL_TREE);
14164 arg = convert_template_argument (parm, arg, targs, tf_none,
14166 if (arg == error_mark_node)
14170 TREE_VEC_ELT (targs, i) = arg;
14171 /* The position of the first default template argument,
14172 is also the number of non-defaulted arguments in TARGS.
14174 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
14175 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
14180 /* If the type parameter is a parameter pack, then it will
14181 be deduced to an empty parameter pack. */
14182 if (template_parameter_pack_p (tparm))
14186 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
14188 arg = make_node (NONTYPE_ARGUMENT_PACK);
14189 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
14190 TREE_CONSTANT (arg) = 1;
14193 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
14195 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
14197 TREE_VEC_ELT (targs, i) = arg;
14203 #ifdef ENABLE_CHECKING
14204 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
14205 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
14211 /* Subroutine of type_unification_real. Args are like the variables
14212 at the call site. ARG is an overloaded function (or template-id);
14213 we try deducing template args from each of the overloads, and if
14214 only one succeeds, we go with that. Modifies TARGS and returns
14215 true on success. */
14218 resolve_overloaded_unification (tree tparms,
14222 unification_kind_t strict,
14225 tree tempargs = copy_node (targs);
14227 tree goodfn = NULL_TREE;
14230 if (TREE_CODE (arg) == ADDR_EXPR)
14232 arg = TREE_OPERAND (arg, 0);
14238 if (TREE_CODE (arg) == COMPONENT_REF)
14239 /* Handle `&x' where `x' is some static or non-static member
14241 arg = TREE_OPERAND (arg, 1);
14243 if (TREE_CODE (arg) == OFFSET_REF)
14244 arg = TREE_OPERAND (arg, 1);
14246 /* Strip baselink information. */
14247 if (BASELINK_P (arg))
14248 arg = BASELINK_FUNCTIONS (arg);
14250 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
14252 /* If we got some explicit template args, we need to plug them into
14253 the affected templates before we try to unify, in case the
14254 explicit args will completely resolve the templates in question. */
14256 tree expl_subargs = TREE_OPERAND (arg, 1);
14257 arg = TREE_OPERAND (arg, 0);
14259 for (; arg; arg = OVL_NEXT (arg))
14261 tree fn = OVL_CURRENT (arg);
14262 tree subargs, elem;
14264 if (TREE_CODE (fn) != TEMPLATE_DECL)
14267 ++processing_template_decl;
14268 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
14269 expl_subargs, /*check_ret=*/false);
14272 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
14273 if (try_one_overload (tparms, targs, tempargs, parm,
14274 elem, strict, sub_strict, addr_p)
14275 && (!goodfn || !decls_match (goodfn, elem)))
14281 --processing_template_decl;
14284 else if (TREE_CODE (arg) != OVERLOAD
14285 && TREE_CODE (arg) != FUNCTION_DECL)
14286 /* If ARG is, for example, "(0, &f)" then its type will be unknown
14287 -- but the deduction does not succeed because the expression is
14288 not just the function on its own. */
14291 for (; arg; arg = OVL_NEXT (arg))
14292 if (try_one_overload (tparms, targs, tempargs, parm,
14293 TREE_TYPE (OVL_CURRENT (arg)),
14294 strict, sub_strict, addr_p)
14295 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
14297 goodfn = OVL_CURRENT (arg);
14301 /* [temp.deduct.type] A template-argument can be deduced from a pointer
14302 to function or pointer to member function argument if the set of
14303 overloaded functions does not contain function templates and at most
14304 one of a set of overloaded functions provides a unique match.
14306 So if we found multiple possibilities, we return success but don't
14307 deduce anything. */
14311 int i = TREE_VEC_LENGTH (targs);
14313 if (TREE_VEC_ELT (tempargs, i))
14314 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
14322 /* Core DR 115: In contexts where deduction is done and fails, or in
14323 contexts where deduction is not done, if a template argument list is
14324 specified and it, along with any default template arguments, identifies
14325 a single function template specialization, then the template-id is an
14326 lvalue for the function template specialization. */
14329 resolve_nondeduced_context (tree orig_expr)
14331 tree expr, offset, baselink;
14334 if (!type_unknown_p (orig_expr))
14339 offset = NULL_TREE;
14340 baselink = NULL_TREE;
14342 if (TREE_CODE (expr) == ADDR_EXPR)
14344 expr = TREE_OPERAND (expr, 0);
14347 if (TREE_CODE (expr) == OFFSET_REF)
14350 expr = TREE_OPERAND (expr, 1);
14352 if (TREE_CODE (expr) == BASELINK)
14355 expr = BASELINK_FUNCTIONS (expr);
14358 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
14361 tree goodfn = NULL_TREE;
14363 /* If we got some explicit template args, we need to plug them into
14364 the affected templates before we try to unify, in case the
14365 explicit args will completely resolve the templates in question. */
14367 tree expl_subargs = TREE_OPERAND (expr, 1);
14368 tree arg = TREE_OPERAND (expr, 0);
14369 tree badfn = NULL_TREE;
14370 tree badargs = NULL_TREE;
14372 for (; arg; arg = OVL_NEXT (arg))
14374 tree fn = OVL_CURRENT (arg);
14375 tree subargs, elem;
14377 if (TREE_CODE (fn) != TEMPLATE_DECL)
14380 ++processing_template_decl;
14381 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
14382 expl_subargs, /*check_ret=*/false);
14383 if (subargs && !any_dependent_template_arguments_p (subargs))
14385 elem = instantiate_template (fn, subargs, tf_none);
14386 if (elem == error_mark_node)
14391 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
14397 --processing_template_decl;
14403 expr = build_baselink (BASELINK_BINFO (baselink),
14404 BASELINK_ACCESS_BINFO (baselink),
14405 expr, BASELINK_OPTYPE (baselink));
14409 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
14410 expr = build_offset_ref (base, expr, addr);
14413 expr = cp_build_addr_expr (expr, tf_warning_or_error);
14416 else if (good == 0 && badargs)
14417 /* There were no good options and at least one bad one, so let the
14418 user know what the problem is. */
14419 instantiate_template (badfn, badargs, tf_warning_or_error);
14424 /* Subroutine of resolve_overloaded_unification; does deduction for a single
14425 overload. Fills TARGS with any deduced arguments, or error_mark_node if
14426 different overloads deduce different arguments for a given parm.
14427 ADDR_P is true if the expression for which deduction is being
14428 performed was of the form "& fn" rather than simply "fn".
14430 Returns 1 on success. */
14433 try_one_overload (tree tparms,
14438 unification_kind_t strict,
14446 /* [temp.deduct.type] A template-argument can be deduced from a pointer
14447 to function or pointer to member function argument if the set of
14448 overloaded functions does not contain function templates and at most
14449 one of a set of overloaded functions provides a unique match.
14451 So if this is a template, just return success. */
14453 if (uses_template_parms (arg))
14456 if (TREE_CODE (arg) == METHOD_TYPE)
14457 arg = build_ptrmemfunc_type (build_pointer_type (arg));
14459 arg = build_pointer_type (arg);
14461 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
14463 /* We don't copy orig_targs for this because if we have already deduced
14464 some template args from previous args, unify would complain when we
14465 try to deduce a template parameter for the same argument, even though
14466 there isn't really a conflict. */
14467 nargs = TREE_VEC_LENGTH (targs);
14468 tempargs = make_tree_vec (nargs);
14470 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
14473 /* First make sure we didn't deduce anything that conflicts with
14474 explicitly specified args. */
14475 for (i = nargs; i--; )
14477 tree elt = TREE_VEC_ELT (tempargs, i);
14478 tree oldelt = TREE_VEC_ELT (orig_targs, i);
14482 else if (uses_template_parms (elt))
14483 /* Since we're unifying against ourselves, we will fill in
14484 template args used in the function parm list with our own
14485 template parms. Discard them. */
14486 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
14487 else if (oldelt && !template_args_equal (oldelt, elt))
14491 for (i = nargs; i--; )
14493 tree elt = TREE_VEC_ELT (tempargs, i);
14496 TREE_VEC_ELT (targs, i) = elt;
14502 /* PARM is a template class (perhaps with unbound template
14503 parameters). ARG is a fully instantiated type. If ARG can be
14504 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
14505 TARGS are as for unify. */
14508 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
14510 tree copy_of_targs;
14512 if (!CLASSTYPE_TEMPLATE_INFO (arg)
14513 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
14514 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
14517 /* We need to make a new template argument vector for the call to
14518 unify. If we used TARGS, we'd clutter it up with the result of
14519 the attempted unification, even if this class didn't work out.
14520 We also don't want to commit ourselves to all the unifications
14521 we've already done, since unification is supposed to be done on
14522 an argument-by-argument basis. In other words, consider the
14523 following pathological case:
14525 template <int I, int J, int K>
14528 template <int I, int J>
14529 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
14531 template <int I, int J, int K>
14532 void f(S<I, J, K>, S<I, I, I>);
14541 Now, by the time we consider the unification involving `s2', we
14542 already know that we must have `f<0, 0, 0>'. But, even though
14543 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
14544 because there are two ways to unify base classes of S<0, 1, 2>
14545 with S<I, I, I>. If we kept the already deduced knowledge, we
14546 would reject the possibility I=1. */
14547 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
14549 /* If unification failed, we're done. */
14550 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
14551 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
14557 /* Given a template type PARM and a class type ARG, find the unique
14558 base type in ARG that is an instance of PARM. We do not examine
14559 ARG itself; only its base-classes. If there is not exactly one
14560 appropriate base class, return NULL_TREE. PARM may be the type of
14561 a partial specialization, as well as a plain template type. Used
14565 get_template_base (tree tparms, tree targs, tree parm, tree arg)
14567 tree rval = NULL_TREE;
14570 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
14572 binfo = TYPE_BINFO (complete_type (arg));
14574 /* The type could not be completed. */
14577 /* Walk in inheritance graph order. The search order is not
14578 important, and this avoids multiple walks of virtual bases. */
14579 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
14581 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
14585 /* If there is more than one satisfactory baseclass, then:
14589 If they yield more than one possible deduced A, the type
14593 if (rval && !same_type_p (r, rval))
14603 /* Returns the level of DECL, which declares a template parameter. */
14606 template_decl_level (tree decl)
14608 switch (TREE_CODE (decl))
14611 case TEMPLATE_DECL:
14612 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
14615 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
14618 gcc_unreachable ();
14623 /* Decide whether ARG can be unified with PARM, considering only the
14624 cv-qualifiers of each type, given STRICT as documented for unify.
14625 Returns nonzero iff the unification is OK on that basis. */
14628 check_cv_quals_for_unify (int strict, tree arg, tree parm)
14630 int arg_quals = cp_type_quals (arg);
14631 int parm_quals = cp_type_quals (parm);
14633 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14634 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
14636 /* Although a CVR qualifier is ignored when being applied to a
14637 substituted template parameter ([8.3.2]/1 for example), that
14638 does not allow us to unify "const T" with "int&" because both
14639 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
14640 It is ok when we're allowing additional CV qualifiers
14641 at the outer level [14.8.2.1]/3,1st bullet. */
14642 if ((TREE_CODE (arg) == REFERENCE_TYPE
14643 || TREE_CODE (arg) == FUNCTION_TYPE
14644 || TREE_CODE (arg) == METHOD_TYPE)
14645 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
14648 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
14649 && (parm_quals & TYPE_QUAL_RESTRICT))
14653 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
14654 && (arg_quals & parm_quals) != parm_quals)
14657 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
14658 && (parm_quals & arg_quals) != arg_quals)
14664 /* Determines the LEVEL and INDEX for the template parameter PARM. */
14666 template_parm_level_and_index (tree parm, int* level, int* index)
14668 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14669 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14670 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14672 *index = TEMPLATE_TYPE_IDX (parm);
14673 *level = TEMPLATE_TYPE_LEVEL (parm);
14677 *index = TEMPLATE_PARM_IDX (parm);
14678 *level = TEMPLATE_PARM_LEVEL (parm);
14682 /* Unifies the remaining arguments in PACKED_ARGS with the pack
14683 expansion at the end of PACKED_PARMS. Returns 0 if the type
14684 deduction succeeds, 1 otherwise. STRICT is the same as in
14685 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
14686 call argument list. We'll need to adjust the arguments to make them
14687 types. SUBR tells us if this is from a recursive call to
14688 type_unification_real. */
14690 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
14691 tree packed_args, int strict, bool call_args_p,
14695 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
14696 tree pattern = PACK_EXPANSION_PATTERN (parm);
14697 tree pack, packs = NULL_TREE;
14698 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
14699 int len = TREE_VEC_LENGTH (packed_args);
14701 /* Determine the parameter packs we will be deducing from the
14702 pattern, and record their current deductions. */
14703 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
14704 pack; pack = TREE_CHAIN (pack))
14706 tree parm_pack = TREE_VALUE (pack);
14709 /* Determine the index and level of this parameter pack. */
14710 template_parm_level_and_index (parm_pack, &level, &idx);
14712 /* Keep track of the parameter packs and their corresponding
14714 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
14715 TREE_TYPE (packs) = make_tree_vec (len - start);
14718 /* Loop through all of the arguments that have not yet been
14719 unified and unify each with the pattern. */
14720 for (i = start; i < len; i++)
14722 tree parm = pattern;
14724 /* For each parameter pack, clear out the deduced value so that
14725 we can deduce it again. */
14726 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14729 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14731 TMPL_ARG (targs, level, idx) = NULL_TREE;
14734 /* Unify the pattern with the current argument. */
14736 tree arg = TREE_VEC_ELT (packed_args, i);
14737 tree arg_expr = NULL_TREE;
14738 int arg_strict = strict;
14739 bool skip_arg_p = false;
14745 /* This mirrors what we do in type_unification_real. */
14749 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
14750 | UNIFY_ALLOW_MORE_CV_QUAL
14751 | UNIFY_ALLOW_DERIVED);
14755 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
14759 sub_strict = UNIFY_ALLOW_NONE;
14763 gcc_unreachable ();
14768 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
14769 if (type_unknown_p (arg))
14771 /* [temp.deduct.type] A template-argument can be
14772 deduced from a pointer to function or pointer
14773 to member function argument if the set of
14774 overloaded functions does not contain function
14775 templates and at most one of a set of
14776 overloaded functions provides a unique
14779 if (resolve_overloaded_unification
14780 (tparms, targs, parm, arg,
14781 (unification_kind_t) strict,
14791 arg = unlowered_expr_type (arg);
14792 if (arg == error_mark_node)
14797 arg_strict = sub_strict;
14801 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
14802 &parm, &arg, arg_expr);
14807 /* For deduction from an init-list we need the actual list. */
14808 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
14810 if (unify (tparms, targs, parm, arg, arg_strict))
14815 /* For each parameter pack, collect the deduced value. */
14816 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14819 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14821 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
14822 TMPL_ARG (targs, level, idx);
14826 /* Verify that the results of unification with the parameter packs
14827 produce results consistent with what we've seen before, and make
14828 the deduced argument packs available. */
14829 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14831 tree old_pack = TREE_VALUE (pack);
14832 tree new_args = TREE_TYPE (pack);
14833 int i, len = TREE_VEC_LENGTH (new_args);
14835 bool nondeduced_p = false;
14837 /* By default keep the original deduced argument pack.
14838 If necessary, more specific code is going to update the
14839 resulting deduced argument later down in this function. */
14840 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14841 TMPL_ARG (targs, level, idx) = old_pack;
14843 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
14844 actually deduce anything. */
14845 for (i = 0; i < len && !nondeduced_p; ++i)
14846 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
14847 nondeduced_p = true;
14851 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
14853 /* Prepend the explicit arguments onto NEW_ARGS. */
14854 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14855 tree old_args = new_args;
14856 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
14857 int len = explicit_len + TREE_VEC_LENGTH (old_args);
14859 /* Copy the explicit arguments. */
14860 new_args = make_tree_vec (len);
14861 for (i = 0; i < explicit_len; i++)
14862 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
14864 /* Copy the deduced arguments. */
14865 for (; i < len; i++)
14866 TREE_VEC_ELT (new_args, i) =
14867 TREE_VEC_ELT (old_args, i - explicit_len);
14873 /* Build the deduced *_ARGUMENT_PACK. */
14874 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
14876 result = make_node (NONTYPE_ARGUMENT_PACK);
14877 TREE_TYPE (result) =
14878 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
14879 TREE_CONSTANT (result) = 1;
14882 result = cxx_make_type (TYPE_ARGUMENT_PACK);
14884 SET_ARGUMENT_PACK_ARGS (result, new_args);
14886 /* Note the deduced argument packs for this parameter
14888 TMPL_ARG (targs, level, idx) = result;
14890 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
14891 && (ARGUMENT_PACK_ARGS (old_pack)
14892 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
14894 /* We only had the explicitly-provided arguments before, but
14895 now we have a complete set of arguments. */
14896 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14898 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
14899 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
14900 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
14902 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
14904 /* Inconsistent unification of this parameter pack. */
14911 /* Deduce the value of template parameters. TPARMS is the (innermost)
14912 set of template parameters to a template. TARGS is the bindings
14913 for those template parameters, as determined thus far; TARGS may
14914 include template arguments for outer levels of template parameters
14915 as well. PARM is a parameter to a template function, or a
14916 subcomponent of that parameter; ARG is the corresponding argument.
14917 This function attempts to match PARM with ARG in a manner
14918 consistent with the existing assignments in TARGS. If more values
14919 are deduced, then TARGS is updated.
14921 Returns 0 if the type deduction succeeds, 1 otherwise. The
14922 parameter STRICT is a bitwise or of the following flags:
14925 Require an exact match between PARM and ARG.
14926 UNIFY_ALLOW_MORE_CV_QUAL:
14927 Allow the deduced ARG to be more cv-qualified (by qualification
14928 conversion) than ARG.
14929 UNIFY_ALLOW_LESS_CV_QUAL:
14930 Allow the deduced ARG to be less cv-qualified than ARG.
14931 UNIFY_ALLOW_DERIVED:
14932 Allow the deduced ARG to be a template base class of ARG,
14933 or a pointer to a template base class of the type pointed to by
14935 UNIFY_ALLOW_INTEGER:
14936 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
14937 case for more information.
14938 UNIFY_ALLOW_OUTER_LEVEL:
14939 This is the outermost level of a deduction. Used to determine validity
14940 of qualification conversions. A valid qualification conversion must
14941 have const qualified pointers leading up to the inner type which
14942 requires additional CV quals, except at the outer level, where const
14943 is not required [conv.qual]. It would be normal to set this flag in
14944 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
14945 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
14946 This is the outermost level of a deduction, and PARM can be more CV
14947 qualified at this point.
14948 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
14949 This is the outermost level of a deduction, and PARM can be less CV
14950 qualified at this point. */
14953 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
14958 int strict_in = strict;
14960 /* I don't think this will do the right thing with respect to types.
14961 But the only case I've seen it in so far has been array bounds, where
14962 signedness is the only information lost, and I think that will be
14964 while (TREE_CODE (parm) == NOP_EXPR)
14965 parm = TREE_OPERAND (parm, 0);
14967 if (arg == error_mark_node)
14969 if (arg == unknown_type_node
14970 || arg == init_list_type_node)
14971 /* We can't deduce anything from this, but we might get all the
14972 template args from other function args. */
14975 /* If PARM uses template parameters, then we can't bail out here,
14976 even if ARG == PARM, since we won't record unifications for the
14977 template parameters. We might need them if we're trying to
14978 figure out which of two things is more specialized. */
14979 if (arg == parm && !uses_template_parms (parm))
14982 /* Handle init lists early, so the rest of the function can assume
14983 we're dealing with a type. */
14984 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
14988 tree orig_parm = parm;
14990 /* Replace T with std::initializer_list<T> for deduction. */
14991 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14992 && flag_deduce_init_list)
14993 parm = listify (parm);
14995 if (!is_std_init_list (parm))
14996 /* We can only deduce from an initializer list argument if the
14997 parameter is std::initializer_list; otherwise this is a
14998 non-deduced context. */
15001 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
15003 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
15005 int elt_strict = strict;
15007 if (elt == error_mark_node)
15010 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
15012 tree type = TREE_TYPE (elt);
15013 /* It should only be possible to get here for a call. */
15014 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
15015 elt_strict |= maybe_adjust_types_for_deduction
15016 (DEDUCE_CALL, &elttype, &type, elt);
15020 if (unify (tparms, targs, elttype, elt, elt_strict))
15024 /* If the std::initializer_list<T> deduction worked, replace the
15025 deduced A with std::initializer_list<A>. */
15026 if (orig_parm != parm)
15028 idx = TEMPLATE_TYPE_IDX (orig_parm);
15029 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
15030 targ = listify (targ);
15031 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
15036 /* Immediately reject some pairs that won't unify because of
15037 cv-qualification mismatches. */
15038 if (TREE_CODE (arg) == TREE_CODE (parm)
15040 /* It is the elements of the array which hold the cv quals of an array
15041 type, and the elements might be template type parms. We'll check
15042 when we recurse. */
15043 && TREE_CODE (arg) != ARRAY_TYPE
15044 /* We check the cv-qualifiers when unifying with template type
15045 parameters below. We want to allow ARG `const T' to unify with
15046 PARM `T' for example, when computing which of two templates
15047 is more specialized, for example. */
15048 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
15049 && !check_cv_quals_for_unify (strict_in, arg, parm))
15052 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
15053 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
15054 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
15055 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
15056 strict &= ~UNIFY_ALLOW_DERIVED;
15057 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15058 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
15060 switch (TREE_CODE (parm))
15062 case TYPENAME_TYPE:
15064 case UNBOUND_CLASS_TEMPLATE:
15065 /* In a type which contains a nested-name-specifier, template
15066 argument values cannot be deduced for template parameters used
15067 within the nested-name-specifier. */
15070 case TEMPLATE_TYPE_PARM:
15071 case TEMPLATE_TEMPLATE_PARM:
15072 case BOUND_TEMPLATE_TEMPLATE_PARM:
15073 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
15074 if (tparm == error_mark_node)
15077 if (TEMPLATE_TYPE_LEVEL (parm)
15078 != template_decl_level (tparm))
15079 /* The PARM is not one we're trying to unify. Just check
15080 to see if it matches ARG. */
15081 return (TREE_CODE (arg) == TREE_CODE (parm)
15082 && same_type_p (parm, arg)) ? 0 : 1;
15083 idx = TEMPLATE_TYPE_IDX (parm);
15084 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
15085 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
15087 /* Check for mixed types and values. */
15088 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15089 && TREE_CODE (tparm) != TYPE_DECL)
15090 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
15091 && TREE_CODE (tparm) != TEMPLATE_DECL))
15094 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
15096 /* ARG must be constructed from a template class or a template
15097 template parameter. */
15098 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
15099 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
15103 tree parmvec = TYPE_TI_ARGS (parm);
15104 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
15105 tree full_argvec = add_to_template_args (targs, argvec);
15107 = DECL_INNERMOST_TEMPLATE_PARMS
15108 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
15110 int parm_variadic_p = 0;
15112 /* The resolution to DR150 makes clear that default
15113 arguments for an N-argument may not be used to bind T
15114 to a template template parameter with fewer than N
15115 parameters. It is not safe to permit the binding of
15116 default arguments as an extension, as that may change
15117 the meaning of a conforming program. Consider:
15119 struct Dense { static const unsigned int dim = 1; };
15121 template <template <typename> class View,
15123 void operator+(float, View<Block> const&);
15125 template <typename Block,
15126 unsigned int Dim = Block::dim>
15127 struct Lvalue_proxy { operator float() const; };
15131 Lvalue_proxy<Dense> p;
15136 Here, if Lvalue_proxy is permitted to bind to View, then
15137 the global operator+ will be used; if they are not, the
15138 Lvalue_proxy will be converted to float. */
15139 if (coerce_template_parms (parm_parms,
15141 TYPE_TI_TEMPLATE (parm),
15143 /*require_all_args=*/true,
15144 /*use_default_args=*/false)
15145 == error_mark_node)
15148 /* Deduce arguments T, i from TT<T> or TT<i>.
15149 We check each element of PARMVEC and ARGVEC individually
15150 rather than the whole TREE_VEC since they can have
15151 different number of elements. */
15153 parmvec = expand_template_argument_pack (parmvec);
15154 argvec = expand_template_argument_pack (argvec);
15156 len = TREE_VEC_LENGTH (parmvec);
15158 /* Check if the parameters end in a pack, making them
15161 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
15162 parm_variadic_p = 1;
15164 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
15167 for (i = 0; i < len - parm_variadic_p; ++i)
15169 if (unify (tparms, targs,
15170 TREE_VEC_ELT (parmvec, i),
15171 TREE_VEC_ELT (argvec, i),
15176 if (parm_variadic_p
15177 && unify_pack_expansion (tparms, targs,
15180 /*call_args_p=*/false,
15184 arg = TYPE_TI_TEMPLATE (arg);
15186 /* Fall through to deduce template name. */
15189 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
15190 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
15192 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
15194 /* Simple cases: Value already set, does match or doesn't. */
15195 if (targ != NULL_TREE && template_args_equal (targ, arg))
15202 /* If PARM is `const T' and ARG is only `int', we don't have
15203 a match unless we are allowing additional qualification.
15204 If ARG is `const int' and PARM is just `T' that's OK;
15205 that binds `const int' to `T'. */
15206 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
15210 /* Consider the case where ARG is `const volatile int' and
15211 PARM is `const T'. Then, T should be `volatile int'. */
15212 arg = cp_build_qualified_type_real
15213 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
15214 if (arg == error_mark_node)
15217 /* Simple cases: Value already set, does match or doesn't. */
15218 if (targ != NULL_TREE && same_type_p (targ, arg))
15223 /* Make sure that ARG is not a variable-sized array. (Note
15224 that were talking about variable-sized arrays (like
15225 `int[n]'), rather than arrays of unknown size (like
15226 `int[]').) We'll get very confused by such a type since
15227 the bound of the array will not be computable in an
15228 instantiation. Besides, such types are not allowed in
15229 ISO C++, so we can do as we please here. */
15230 if (variably_modified_type_p (arg, NULL_TREE))
15233 /* Strip typedefs as in convert_template_argument. */
15234 arg = strip_typedefs (arg);
15237 /* If ARG is a parameter pack or an expansion, we cannot unify
15238 against it unless PARM is also a parameter pack. */
15239 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
15240 && !template_parameter_pack_p (parm))
15243 /* If the argument deduction results is a METHOD_TYPE,
15244 then there is a problem.
15245 METHOD_TYPE doesn't map to any real C++ type the result of
15246 the deduction can not be of that type. */
15247 if (TREE_CODE (arg) == METHOD_TYPE)
15250 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
15253 case TEMPLATE_PARM_INDEX:
15254 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
15255 if (tparm == error_mark_node)
15258 if (TEMPLATE_PARM_LEVEL (parm)
15259 != template_decl_level (tparm))
15260 /* The PARM is not one we're trying to unify. Just check
15261 to see if it matches ARG. */
15262 return !(TREE_CODE (arg) == TREE_CODE (parm)
15263 && cp_tree_equal (parm, arg));
15265 idx = TEMPLATE_PARM_IDX (parm);
15266 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
15269 return !cp_tree_equal (targ, arg);
15271 /* [temp.deduct.type] If, in the declaration of a function template
15272 with a non-type template-parameter, the non-type
15273 template-parameter is used in an expression in the function
15274 parameter-list and, if the corresponding template-argument is
15275 deduced, the template-argument type shall match the type of the
15276 template-parameter exactly, except that a template-argument
15277 deduced from an array bound may be of any integral type.
15278 The non-type parameter might use already deduced type parameters. */
15279 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
15280 if (!TREE_TYPE (arg))
15281 /* Template-parameter dependent expression. Just accept it for now.
15282 It will later be processed in convert_template_argument. */
15284 else if (same_type_p (TREE_TYPE (arg), tparm))
15286 else if ((strict & UNIFY_ALLOW_INTEGER)
15287 && (TREE_CODE (tparm) == INTEGER_TYPE
15288 || TREE_CODE (tparm) == BOOLEAN_TYPE))
15289 /* Convert the ARG to the type of PARM; the deduced non-type
15290 template argument must exactly match the types of the
15291 corresponding parameter. */
15292 arg = fold (build_nop (tparm, arg));
15293 else if (uses_template_parms (tparm))
15294 /* We haven't deduced the type of this parameter yet. Try again
15300 /* If ARG is a parameter pack or an expansion, we cannot unify
15301 against it unless PARM is also a parameter pack. */
15302 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
15303 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
15306 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
15311 /* A pointer-to-member constant can be unified only with
15312 another constant. */
15313 if (TREE_CODE (arg) != PTRMEM_CST)
15316 /* Just unify the class member. It would be useless (and possibly
15317 wrong, depending on the strict flags) to unify also
15318 PTRMEM_CST_CLASS, because we want to be sure that both parm and
15319 arg refer to the same variable, even if through different
15320 classes. For instance:
15322 struct A { int x; };
15325 Unification of &A::x and &B::x must succeed. */
15326 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
15327 PTRMEM_CST_MEMBER (arg), strict);
15332 if (TREE_CODE (arg) != POINTER_TYPE)
15335 /* [temp.deduct.call]
15337 A can be another pointer or pointer to member type that can
15338 be converted to the deduced A via a qualification
15339 conversion (_conv.qual_).
15341 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
15342 This will allow for additional cv-qualification of the
15343 pointed-to types if appropriate. */
15345 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
15346 /* The derived-to-base conversion only persists through one
15347 level of pointers. */
15348 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
15350 return unify (tparms, targs, TREE_TYPE (parm),
15351 TREE_TYPE (arg), strict);
15354 case REFERENCE_TYPE:
15355 if (TREE_CODE (arg) != REFERENCE_TYPE)
15357 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
15358 strict & UNIFY_ALLOW_MORE_CV_QUAL);
15361 if (TREE_CODE (arg) != ARRAY_TYPE)
15363 if ((TYPE_DOMAIN (parm) == NULL_TREE)
15364 != (TYPE_DOMAIN (arg) == NULL_TREE))
15366 if (TYPE_DOMAIN (parm) != NULL_TREE)
15373 /* Our representation of array types uses "N - 1" as the
15374 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
15375 not an integer constant. We cannot unify arbitrarily
15376 complex expressions, so we eliminate the MINUS_EXPRs
15378 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
15379 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
15382 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
15383 parm_max = TREE_OPERAND (parm_max, 0);
15385 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
15386 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
15389 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
15390 trying to unify the type of a variable with the type
15391 of a template parameter. For example:
15393 template <unsigned int N>
15394 void f (char (&) [N]);
15401 Here, the type of the ARG will be "int [g(i)]", and
15402 may be a SAVE_EXPR, etc. */
15403 if (TREE_CODE (arg_max) != MINUS_EXPR)
15405 arg_max = TREE_OPERAND (arg_max, 0);
15408 /* If only one of the bounds used a MINUS_EXPR, compensate
15409 by adding one to the other bound. */
15410 if (parm_cst && !arg_cst)
15411 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
15415 else if (arg_cst && !parm_cst)
15416 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
15421 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
15424 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
15425 strict & UNIFY_ALLOW_MORE_CV_QUAL);
15432 case ENUMERAL_TYPE:
15434 if (TREE_CODE (arg) != TREE_CODE (parm))
15437 /* We have already checked cv-qualification at the top of the
15439 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
15442 /* As far as unification is concerned, this wins. Later checks
15443 will invalidate it if necessary. */
15446 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
15447 /* Type INTEGER_CST can come from ordinary constant template args. */
15449 while (TREE_CODE (arg) == NOP_EXPR)
15450 arg = TREE_OPERAND (arg, 0);
15452 if (TREE_CODE (arg) != INTEGER_CST)
15454 return !tree_int_cst_equal (parm, arg);
15459 if (TREE_CODE (arg) != TREE_VEC)
15461 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
15463 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
15464 if (unify (tparms, targs,
15465 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
15473 if (TREE_CODE (arg) != TREE_CODE (parm))
15476 if (TYPE_PTRMEMFUNC_P (parm))
15478 if (!TYPE_PTRMEMFUNC_P (arg))
15481 return unify (tparms, targs,
15482 TYPE_PTRMEMFUNC_FN_TYPE (parm),
15483 TYPE_PTRMEMFUNC_FN_TYPE (arg),
15487 if (CLASSTYPE_TEMPLATE_INFO (parm))
15489 tree t = NULL_TREE;
15491 if (strict_in & UNIFY_ALLOW_DERIVED)
15493 /* First, we try to unify the PARM and ARG directly. */
15494 t = try_class_unification (tparms, targs,
15499 /* Fallback to the special case allowed in
15500 [temp.deduct.call]:
15502 If P is a class, and P has the form
15503 template-id, then A can be a derived class of
15504 the deduced A. Likewise, if P is a pointer to
15505 a class of the form template-id, A can be a
15506 pointer to a derived class pointed to by the
15508 t = get_template_base (tparms, targs, parm, arg);
15514 else if (CLASSTYPE_TEMPLATE_INFO (arg)
15515 && (CLASSTYPE_TI_TEMPLATE (parm)
15516 == CLASSTYPE_TI_TEMPLATE (arg)))
15517 /* Perhaps PARM is something like S<U> and ARG is S<int>.
15518 Then, we should unify `int' and `U'. */
15521 /* There's no chance of unification succeeding. */
15524 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
15525 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
15527 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
15532 case FUNCTION_TYPE:
15534 unsigned int nargs;
15539 if (TREE_CODE (arg) != TREE_CODE (parm))
15542 /* CV qualifications for methods can never be deduced, they must
15543 match exactly. We need to check them explicitly here,
15544 because type_unification_real treats them as any other
15545 cv-qualified parameter. */
15546 if (TREE_CODE (parm) == METHOD_TYPE
15547 && (!check_cv_quals_for_unify
15549 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
15550 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
15553 if (unify (tparms, targs, TREE_TYPE (parm),
15554 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
15557 nargs = list_length (TYPE_ARG_TYPES (arg));
15558 args = XALLOCAVEC (tree, nargs);
15559 for (a = TYPE_ARG_TYPES (arg), i = 0;
15560 a != NULL_TREE && a != void_list_node;
15561 a = TREE_CHAIN (a), ++i)
15562 args[i] = TREE_VALUE (a);
15565 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
15566 args, nargs, 1, DEDUCE_EXACT,
15571 /* Unify a pointer to member with a pointer to member function, which
15572 deduces the type of the member as a function type. */
15573 if (TYPE_PTRMEMFUNC_P (arg))
15578 /* Check top-level cv qualifiers */
15579 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
15582 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
15583 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
15586 /* Determine the type of the function we are unifying against. */
15587 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
15589 build_function_type (TREE_TYPE (method_type),
15590 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
15592 /* Extract the cv-qualifiers of the member function from the
15593 implicit object parameter and place them on the function
15594 type to be restored later. */
15595 fntype = apply_memfn_quals (fntype, type_memfn_quals (method_type));
15596 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
15599 if (TREE_CODE (arg) != OFFSET_TYPE)
15601 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
15602 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
15604 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
15608 if (DECL_TEMPLATE_PARM_P (parm))
15609 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
15610 if (arg != integral_constant_value (parm))
15615 case TEMPLATE_DECL:
15616 /* Matched cases are handled by the ARG == PARM test above. */
15620 /* A non-type template parameter that is a variable should be a
15621 an integral constant, in which case, it whould have been
15622 folded into its (constant) value. So we should not be getting
15623 a variable here. */
15624 gcc_unreachable ();
15626 case TYPE_ARGUMENT_PACK:
15627 case NONTYPE_ARGUMENT_PACK:
15629 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
15630 tree packed_args = ARGUMENT_PACK_ARGS (arg);
15631 int i, len = TREE_VEC_LENGTH (packed_parms);
15632 int argslen = TREE_VEC_LENGTH (packed_args);
15633 int parm_variadic_p = 0;
15635 for (i = 0; i < len; ++i)
15637 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
15640 /* We can unify against something with a trailing
15642 parm_variadic_p = 1;
15644 /* Since there is something following the pack
15645 expansion, we cannot unify this template argument
15652 /* If we don't have enough arguments to satisfy the parameters
15653 (not counting the pack expression at the end), or we have
15654 too many arguments for a parameter list that doesn't end in
15655 a pack expression, we can't unify. */
15656 if (argslen < (len - parm_variadic_p)
15657 || (argslen > len && !parm_variadic_p))
15660 /* Unify all of the parameters that precede the (optional)
15661 pack expression. */
15662 for (i = 0; i < len - parm_variadic_p; ++i)
15664 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
15665 TREE_VEC_ELT (packed_args, i), strict))
15669 if (parm_variadic_p)
15670 return unify_pack_expansion (tparms, targs,
15671 packed_parms, packed_args,
15672 strict, /*call_args_p=*/false,
15680 case DECLTYPE_TYPE:
15681 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
15686 /* Unification fails if we hit an error node. */
15690 gcc_assert (EXPR_P (parm));
15692 /* We must be looking at an expression. This can happen with
15696 void foo(S<I>, S<I + 2>);
15698 This is a "nondeduced context":
15702 The nondeduced contexts are:
15704 --A type that is a template-id in which one or more of
15705 the template-arguments is an expression that references
15706 a template-parameter.
15708 In these cases, we assume deduction succeeded, but don't
15709 actually infer any unifications. */
15711 if (!uses_template_parms (parm)
15712 && !template_args_equal (parm, arg))
15719 /* Note that DECL can be defined in this translation unit, if
15723 mark_definable (tree decl)
15726 DECL_NOT_REALLY_EXTERN (decl) = 1;
15727 FOR_EACH_CLONE (clone, decl)
15728 DECL_NOT_REALLY_EXTERN (clone) = 1;
15731 /* Called if RESULT is explicitly instantiated, or is a member of an
15732 explicitly instantiated class. */
15735 mark_decl_instantiated (tree result, int extern_p)
15737 SET_DECL_EXPLICIT_INSTANTIATION (result);
15739 /* If this entity has already been written out, it's too late to
15740 make any modifications. */
15741 if (TREE_ASM_WRITTEN (result))
15744 if (TREE_CODE (result) != FUNCTION_DECL)
15745 /* The TREE_PUBLIC flag for function declarations will have been
15746 set correctly by tsubst. */
15747 TREE_PUBLIC (result) = 1;
15749 /* This might have been set by an earlier implicit instantiation. */
15750 DECL_COMDAT (result) = 0;
15753 DECL_NOT_REALLY_EXTERN (result) = 0;
15756 mark_definable (result);
15757 /* Always make artificials weak. */
15758 if (DECL_ARTIFICIAL (result) && flag_weak)
15759 comdat_linkage (result);
15760 /* For WIN32 we also want to put explicit instantiations in
15761 linkonce sections. */
15762 else if (TREE_PUBLIC (result))
15763 maybe_make_one_only (result);
15766 /* If EXTERN_P, then this function will not be emitted -- unless
15767 followed by an explicit instantiation, at which point its linkage
15768 will be adjusted. If !EXTERN_P, then this function will be
15769 emitted here. In neither circumstance do we want
15770 import_export_decl to adjust the linkage. */
15771 DECL_INTERFACE_KNOWN (result) = 1;
15774 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
15775 important template arguments. If any are missing, we check whether
15776 they're important by using error_mark_node for substituting into any
15777 args that were used for partial ordering (the ones between ARGS and END)
15778 and seeing if it bubbles up. */
15781 check_undeduced_parms (tree targs, tree args, tree end)
15783 bool found = false;
15785 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
15786 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
15789 TREE_VEC_ELT (targs, i) = error_mark_node;
15793 for (; args != end; args = TREE_CHAIN (args))
15795 tree substed = tsubst (TREE_VALUE (args), targs, tf_none, NULL_TREE);
15796 if (substed == error_mark_node)
15803 /* Given two function templates PAT1 and PAT2, return:
15805 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
15806 -1 if PAT2 is more specialized than PAT1.
15807 0 if neither is more specialized.
15809 LEN indicates the number of parameters we should consider
15810 (defaulted parameters should not be considered).
15812 The 1998 std underspecified function template partial ordering, and
15813 DR214 addresses the issue. We take pairs of arguments, one from
15814 each of the templates, and deduce them against each other. One of
15815 the templates will be more specialized if all the *other*
15816 template's arguments deduce against its arguments and at least one
15817 of its arguments *does* *not* deduce against the other template's
15818 corresponding argument. Deduction is done as for class templates.
15819 The arguments used in deduction have reference and top level cv
15820 qualifiers removed. Iff both arguments were originally reference
15821 types *and* deduction succeeds in both directions, the template
15822 with the more cv-qualified argument wins for that pairing (if
15823 neither is more cv-qualified, they both are equal). Unlike regular
15824 deduction, after all the arguments have been deduced in this way,
15825 we do *not* verify the deduced template argument values can be
15826 substituted into non-deduced contexts.
15828 The logic can be a bit confusing here, because we look at deduce1 and
15829 targs1 to see if pat2 is at least as specialized, and vice versa; if we
15830 can find template arguments for pat1 to make arg1 look like arg2, that
15831 means that arg2 is at least as specialized as arg1. */
15834 more_specialized_fn (tree pat1, tree pat2, int len)
15836 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
15837 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
15838 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
15839 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
15840 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
15841 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
15842 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
15843 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
15844 tree origs1, origs2;
15845 bool lose1 = false;
15846 bool lose2 = false;
15848 /* Remove the this parameter from non-static member functions. If
15849 one is a non-static member function and the other is not a static
15850 member function, remove the first parameter from that function
15851 also. This situation occurs for operator functions where we
15852 locate both a member function (with this pointer) and non-member
15853 operator (with explicit first operand). */
15854 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
15856 len--; /* LEN is the number of significant arguments for DECL1 */
15857 args1 = TREE_CHAIN (args1);
15858 if (!DECL_STATIC_FUNCTION_P (decl2))
15859 args2 = TREE_CHAIN (args2);
15861 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
15863 args2 = TREE_CHAIN (args2);
15864 if (!DECL_STATIC_FUNCTION_P (decl1))
15867 args1 = TREE_CHAIN (args1);
15871 /* If only one is a conversion operator, they are unordered. */
15872 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
15875 /* Consider the return type for a conversion function */
15876 if (DECL_CONV_FN_P (decl1))
15878 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
15879 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
15883 processing_template_decl++;
15889 /* Stop when an ellipsis is seen. */
15890 && args1 != NULL_TREE && args2 != NULL_TREE)
15892 tree arg1 = TREE_VALUE (args1);
15893 tree arg2 = TREE_VALUE (args2);
15894 int deduce1, deduce2;
15898 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15899 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15901 /* When both arguments are pack expansions, we need only
15902 unify the patterns themselves. */
15903 arg1 = PACK_EXPANSION_PATTERN (arg1);
15904 arg2 = PACK_EXPANSION_PATTERN (arg2);
15906 /* This is the last comparison we need to do. */
15910 if (TREE_CODE (arg1) == REFERENCE_TYPE)
15912 arg1 = TREE_TYPE (arg1);
15913 quals1 = cp_type_quals (arg1);
15916 if (TREE_CODE (arg2) == REFERENCE_TYPE)
15918 arg2 = TREE_TYPE (arg2);
15919 quals2 = cp_type_quals (arg2);
15922 if ((quals1 < 0) != (quals2 < 0))
15924 /* Only of the args is a reference, see if we should apply
15925 array/function pointer decay to it. This is not part of
15926 DR214, but is, IMHO, consistent with the deduction rules
15927 for the function call itself, and with our earlier
15928 implementation of the underspecified partial ordering
15929 rules. (nathan). */
15932 switch (TREE_CODE (arg1))
15935 arg1 = TREE_TYPE (arg1);
15937 case FUNCTION_TYPE:
15938 arg1 = build_pointer_type (arg1);
15947 switch (TREE_CODE (arg2))
15950 arg2 = TREE_TYPE (arg2);
15952 case FUNCTION_TYPE:
15953 arg2 = build_pointer_type (arg2);
15962 arg1 = TYPE_MAIN_VARIANT (arg1);
15963 arg2 = TYPE_MAIN_VARIANT (arg2);
15965 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
15967 int i, len2 = list_length (args2);
15968 tree parmvec = make_tree_vec (1);
15969 tree argvec = make_tree_vec (len2);
15972 /* Setup the parameter vector, which contains only ARG1. */
15973 TREE_VEC_ELT (parmvec, 0) = arg1;
15975 /* Setup the argument vector, which contains the remaining
15977 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
15978 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15980 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
15981 argvec, UNIFY_ALLOW_NONE,
15982 /*call_args_p=*/false,
15985 /* We cannot deduce in the other direction, because ARG1 is
15986 a pack expansion but ARG2 is not. */
15989 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15991 int i, len1 = list_length (args1);
15992 tree parmvec = make_tree_vec (1);
15993 tree argvec = make_tree_vec (len1);
15996 /* Setup the parameter vector, which contains only ARG1. */
15997 TREE_VEC_ELT (parmvec, 0) = arg2;
15999 /* Setup the argument vector, which contains the remaining
16001 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
16002 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
16004 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
16005 argvec, UNIFY_ALLOW_NONE,
16006 /*call_args_p=*/false,
16009 /* We cannot deduce in the other direction, because ARG2 is
16010 a pack expansion but ARG1 is not.*/
16016 /* The normal case, where neither argument is a pack
16018 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
16019 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
16022 /* If we couldn't deduce arguments for tparms1 to make arg1 match
16023 arg2, then arg2 is not as specialized as arg1. */
16029 /* "If, for a given type, deduction succeeds in both directions
16030 (i.e., the types are identical after the transformations above)
16031 and if the type from the argument template is more cv-qualified
16032 than the type from the parameter template (as described above)
16033 that type is considered to be more specialized than the other. If
16034 neither type is more cv-qualified than the other then neither type
16035 is more specialized than the other." */
16037 if (deduce1 && deduce2
16038 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
16040 if ((quals1 & quals2) == quals2)
16042 if ((quals1 & quals2) == quals1)
16046 if (lose1 && lose2)
16047 /* We've failed to deduce something in either direction.
16048 These must be unordered. */
16051 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
16052 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
16053 /* We have already processed all of the arguments in our
16054 handing of the pack expansion type. */
16057 args1 = TREE_CHAIN (args1);
16058 args2 = TREE_CHAIN (args2);
16061 /* "In most cases, all template parameters must have values in order for
16062 deduction to succeed, but for partial ordering purposes a template
16063 parameter may remain without a value provided it is not used in the
16064 types being used for partial ordering."
16066 Thus, if we are missing any of the targs1 we need to substitute into
16067 origs1, then pat2 is not as specialized as pat1. This can happen when
16068 there is a nondeduced context. */
16069 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
16071 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
16074 processing_template_decl--;
16076 /* All things being equal, if the next argument is a pack expansion
16077 for one function but not for the other, prefer the
16078 non-variadic function. FIXME this is bogus; see c++/41958. */
16080 && args1 && TREE_VALUE (args1)
16081 && args2 && TREE_VALUE (args2))
16083 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
16084 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
16087 if (lose1 == lose2)
16095 /* Determine which of two partial specializations is more specialized.
16097 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
16098 to the first partial specialization. The TREE_VALUE is the
16099 innermost set of template parameters for the partial
16100 specialization. PAT2 is similar, but for the second template.
16102 Return 1 if the first partial specialization is more specialized;
16103 -1 if the second is more specialized; 0 if neither is more
16106 See [temp.class.order] for information about determining which of
16107 two templates is more specialized. */
16110 more_specialized_class (tree pat1, tree pat2)
16115 bool any_deductions = false;
16117 tmpl1 = TREE_TYPE (pat1);
16118 tmpl2 = TREE_TYPE (pat2);
16120 /* Just like what happens for functions, if we are ordering between
16121 different class template specializations, we may encounter dependent
16122 types in the arguments, and we need our dependency check functions
16123 to behave correctly. */
16124 ++processing_template_decl;
16125 targs = get_class_bindings (TREE_VALUE (pat1),
16126 CLASSTYPE_TI_ARGS (tmpl1),
16127 CLASSTYPE_TI_ARGS (tmpl2));
16131 any_deductions = true;
16134 targs = get_class_bindings (TREE_VALUE (pat2),
16135 CLASSTYPE_TI_ARGS (tmpl2),
16136 CLASSTYPE_TI_ARGS (tmpl1));
16140 any_deductions = true;
16142 --processing_template_decl;
16144 /* In the case of a tie where at least one of the class templates
16145 has a parameter pack at the end, the template with the most
16146 non-packed parameters wins. */
16149 && (template_args_variadic_p (TREE_PURPOSE (pat1))
16150 || template_args_variadic_p (TREE_PURPOSE (pat2))))
16152 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
16153 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
16154 int len1 = TREE_VEC_LENGTH (args1);
16155 int len2 = TREE_VEC_LENGTH (args2);
16157 /* We don't count the pack expansion at the end. */
16158 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
16160 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
16165 else if (len1 < len2)
16172 /* Return the template arguments that will produce the function signature
16173 DECL from the function template FN, with the explicit template
16174 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
16175 also match. Return NULL_TREE if no satisfactory arguments could be
16179 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
16181 int ntparms = DECL_NTPARMS (fn);
16182 tree targs = make_tree_vec (ntparms);
16184 tree decl_arg_types;
16186 unsigned int nargs, ix;
16189 /* Substitute the explicit template arguments into the type of DECL.
16190 The call to fn_type_unification will handle substitution into the
16192 decl_type = TREE_TYPE (decl);
16193 if (explicit_args && uses_template_parms (decl_type))
16196 tree converted_args;
16198 if (DECL_TEMPLATE_INFO (decl))
16199 tmpl = DECL_TI_TEMPLATE (decl);
16201 /* We can get here for some invalid specializations. */
16205 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
16206 explicit_args, NULL_TREE,
16208 /*require_all_args=*/false,
16209 /*use_default_args=*/false);
16210 if (converted_args == error_mark_node)
16213 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
16214 if (decl_type == error_mark_node)
16218 /* Never do unification on the 'this' parameter. */
16219 decl_arg_types = skip_artificial_parms_for (decl,
16220 TYPE_ARG_TYPES (decl_type));
16222 nargs = list_length (decl_arg_types);
16223 args = XALLOCAVEC (tree, nargs);
16224 for (arg = decl_arg_types, ix = 0;
16225 arg != NULL_TREE && arg != void_list_node;
16226 arg = TREE_CHAIN (arg), ++ix)
16227 args[ix] = TREE_VALUE (arg);
16229 if (fn_type_unification (fn, explicit_args, targs,
16231 (check_rettype || DECL_CONV_FN_P (fn)
16232 ? TREE_TYPE (decl_type) : NULL_TREE),
16233 DEDUCE_EXACT, LOOKUP_NORMAL))
16239 /* Return the innermost template arguments that, when applied to a
16240 template specialization whose innermost template parameters are
16241 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
16244 For example, suppose we have:
16246 template <class T, class U> struct S {};
16247 template <class T> struct S<T*, int> {};
16249 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
16250 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
16251 int}. The resulting vector will be {double}, indicating that `T'
16252 is bound to `double'. */
16255 get_class_bindings (tree tparms, tree spec_args, tree args)
16257 int i, ntparms = TREE_VEC_LENGTH (tparms);
16259 tree innermost_deduced_args;
16261 innermost_deduced_args = make_tree_vec (ntparms);
16262 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
16264 deduced_args = copy_node (args);
16265 SET_TMPL_ARGS_LEVEL (deduced_args,
16266 TMPL_ARGS_DEPTH (deduced_args),
16267 innermost_deduced_args);
16270 deduced_args = innermost_deduced_args;
16272 if (unify (tparms, deduced_args,
16273 INNERMOST_TEMPLATE_ARGS (spec_args),
16274 INNERMOST_TEMPLATE_ARGS (args),
16278 for (i = 0; i < ntparms; ++i)
16279 if (! TREE_VEC_ELT (innermost_deduced_args, i))
16282 /* Verify that nondeduced template arguments agree with the type
16283 obtained from argument deduction.
16287 struct A { typedef int X; };
16288 template <class T, class U> struct C {};
16289 template <class T> struct C<T, typename T::X> {};
16291 Then with the instantiation `C<A, int>', we can deduce that
16292 `T' is `A' but unify () does not check whether `typename T::X'
16294 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
16295 if (spec_args == error_mark_node
16296 /* We only need to check the innermost arguments; the other
16297 arguments will always agree. */
16298 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
16299 INNERMOST_TEMPLATE_ARGS (args)))
16302 /* Now that we have bindings for all of the template arguments,
16303 ensure that the arguments deduced for the template template
16304 parameters have compatible template parameter lists. See the use
16305 of template_template_parm_bindings_ok_p in fn_type_unification
16306 for more information. */
16307 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
16310 return deduced_args;
16313 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
16314 Return the TREE_LIST node with the most specialized template, if
16315 any. If there is no most specialized template, the error_mark_node
16318 Note that this function does not look at, or modify, the
16319 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
16320 returned is one of the elements of INSTANTIATIONS, callers may
16321 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
16322 and retrieve it from the value returned. */
16325 most_specialized_instantiation (tree templates)
16329 ++processing_template_decl;
16332 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
16336 if (get_bindings (TREE_VALUE (champ),
16337 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
16338 NULL_TREE, /*check_ret=*/false))
16341 if (get_bindings (TREE_VALUE (fn),
16342 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
16343 NULL_TREE, /*check_ret=*/false))
16350 /* Equally specialized, move to next function. If there
16351 is no next function, nothing's most specialized. */
16352 fn = TREE_CHAIN (fn);
16360 /* Now verify that champ is better than everything earlier in the
16361 instantiation list. */
16362 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
16363 if (get_bindings (TREE_VALUE (champ),
16364 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
16365 NULL_TREE, /*check_ret=*/false)
16366 || !get_bindings (TREE_VALUE (fn),
16367 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
16368 NULL_TREE, /*check_ret=*/false))
16374 processing_template_decl--;
16377 return error_mark_node;
16382 /* If DECL is a specialization of some template, return the most
16383 general such template. Otherwise, returns NULL_TREE.
16385 For example, given:
16387 template <class T> struct S { template <class U> void f(U); };
16389 if TMPL is `template <class U> void S<int>::f(U)' this will return
16390 the full template. This function will not trace past partial
16391 specializations, however. For example, given in addition:
16393 template <class T> struct S<T*> { template <class U> void f(U); };
16395 if TMPL is `template <class U> void S<int*>::f(U)' this will return
16396 `template <class T> template <class U> S<T*>::f(U)'. */
16399 most_general_template (tree decl)
16401 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
16402 an immediate specialization. */
16403 if (TREE_CODE (decl) == FUNCTION_DECL)
16405 if (DECL_TEMPLATE_INFO (decl)) {
16406 decl = DECL_TI_TEMPLATE (decl);
16408 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
16409 template friend. */
16410 if (TREE_CODE (decl) != TEMPLATE_DECL)
16416 /* Look for more and more general templates. */
16417 while (DECL_TEMPLATE_INFO (decl))
16419 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
16420 (See cp-tree.h for details.) */
16421 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
16424 if (CLASS_TYPE_P (TREE_TYPE (decl))
16425 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
16428 /* Stop if we run into an explicitly specialized class template. */
16429 if (!DECL_NAMESPACE_SCOPE_P (decl)
16430 && DECL_CONTEXT (decl)
16431 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
16434 decl = DECL_TI_TEMPLATE (decl);
16440 /* Return the most specialized of the class template partial
16441 specializations of TMPL which can produce TYPE, a specialization of
16442 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
16443 a _TYPE node corresponding to the partial specialization, while the
16444 TREE_PURPOSE is the set of template arguments that must be
16445 substituted into the TREE_TYPE in order to generate TYPE.
16447 If the choice of partial specialization is ambiguous, a diagnostic
16448 is issued, and the error_mark_node is returned. If there are no
16449 partial specializations of TMPL matching TYPE, then NULL_TREE is
16453 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
16455 tree list = NULL_TREE;
16461 tree outer_args = NULL_TREE;
16463 tmpl = most_general_template (tmpl);
16464 args = CLASSTYPE_TI_ARGS (type);
16466 /* For determining which partial specialization to use, only the
16467 innermost args are interesting. */
16468 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
16470 outer_args = strip_innermost_template_args (args, 1);
16471 args = INNERMOST_TEMPLATE_ARGS (args);
16474 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
16476 tree partial_spec_args;
16478 tree parms = TREE_VALUE (t);
16480 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
16482 ++processing_template_decl;
16488 /* Discard the outer levels of args, and then substitute in the
16489 template args from the enclosing class. */
16490 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
16491 partial_spec_args = tsubst_template_args
16492 (partial_spec_args, outer_args, tf_none, NULL_TREE);
16494 /* PARMS already refers to just the innermost parms, but the
16495 template parms in partial_spec_args had their levels lowered
16496 by tsubst, so we need to do the same for the parm list. We
16497 can't just tsubst the TREE_VEC itself, as tsubst wants to
16498 treat a TREE_VEC as an argument vector. */
16499 parms = copy_node (parms);
16500 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
16501 TREE_VEC_ELT (parms, i) =
16502 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
16506 partial_spec_args =
16507 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
16508 add_to_template_args (outer_args,
16509 partial_spec_args),
16511 /*require_all_args=*/true,
16512 /*use_default_args=*/true);
16514 --processing_template_decl;
16516 if (partial_spec_args == error_mark_node)
16517 return error_mark_node;
16519 spec_args = get_class_bindings (parms,
16525 spec_args = add_to_template_args (outer_args, spec_args);
16526 list = tree_cons (spec_args, TREE_VALUE (t), list);
16527 TREE_TYPE (list) = TREE_TYPE (t);
16534 ambiguous_p = false;
16537 t = TREE_CHAIN (t);
16538 for (; t; t = TREE_CHAIN (t))
16540 fate = more_specialized_class (champ, t);
16547 t = TREE_CHAIN (t);
16550 ambiguous_p = true;
16559 for (t = list; t && t != champ; t = TREE_CHAIN (t))
16561 fate = more_specialized_class (champ, t);
16564 ambiguous_p = true;
16572 char *spaces = NULL;
16573 if (!(complain & tf_error))
16574 return error_mark_node;
16575 error ("ambiguous class template instantiation for %q#T", type);
16576 str = ngettext ("candidate is:", "candidates are:", list_length (list));
16577 for (t = list; t; t = TREE_CHAIN (t))
16579 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
16580 spaces = spaces ? spaces : get_spaces (str);
16583 return error_mark_node;
16589 /* Explicitly instantiate DECL. */
16592 do_decl_instantiation (tree decl, tree storage)
16594 tree result = NULL_TREE;
16597 if (!decl || decl == error_mark_node)
16598 /* An error occurred, for which grokdeclarator has already issued
16599 an appropriate message. */
16601 else if (! DECL_LANG_SPECIFIC (decl))
16603 error ("explicit instantiation of non-template %q#D", decl);
16606 else if (TREE_CODE (decl) == VAR_DECL)
16608 /* There is an asymmetry here in the way VAR_DECLs and
16609 FUNCTION_DECLs are handled by grokdeclarator. In the case of
16610 the latter, the DECL we get back will be marked as a
16611 template instantiation, and the appropriate
16612 DECL_TEMPLATE_INFO will be set up. This does not happen for
16613 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
16614 should handle VAR_DECLs as it currently handles
16616 if (!DECL_CLASS_SCOPE_P (decl))
16618 error ("%qD is not a static data member of a class template", decl);
16621 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
16622 if (!result || TREE_CODE (result) != VAR_DECL)
16624 error ("no matching template for %qD found", decl);
16627 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
16629 error ("type %qT for explicit instantiation %qD does not match "
16630 "declared type %qT", TREE_TYPE (result), decl,
16635 else if (TREE_CODE (decl) != FUNCTION_DECL)
16637 error ("explicit instantiation of %q#D", decl);
16643 /* Check for various error cases. Note that if the explicit
16644 instantiation is valid the RESULT will currently be marked as an
16645 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
16646 until we get here. */
16648 if (DECL_TEMPLATE_SPECIALIZATION (result))
16650 /* DR 259 [temp.spec].
16652 Both an explicit instantiation and a declaration of an explicit
16653 specialization shall not appear in a program unless the explicit
16654 instantiation follows a declaration of the explicit specialization.
16656 For a given set of template parameters, if an explicit
16657 instantiation of a template appears after a declaration of an
16658 explicit specialization for that template, the explicit
16659 instantiation has no effect. */
16662 else if (DECL_EXPLICIT_INSTANTIATION (result))
16666 No program shall explicitly instantiate any template more
16669 We check DECL_NOT_REALLY_EXTERN so as not to complain when
16670 the first instantiation was `extern' and the second is not,
16671 and EXTERN_P for the opposite case. */
16672 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
16673 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
16674 /* If an "extern" explicit instantiation follows an ordinary
16675 explicit instantiation, the template is instantiated. */
16679 else if (!DECL_IMPLICIT_INSTANTIATION (result))
16681 error ("no matching template for %qD found", result);
16684 else if (!DECL_TEMPLATE_INFO (result))
16686 permerror (input_location, "explicit instantiation of non-template %q#D", result);
16690 if (storage == NULL_TREE)
16692 else if (storage == ridpointers[(int) RID_EXTERN])
16694 if (!in_system_header && (cxx_dialect == cxx98))
16695 pedwarn (input_location, OPT_pedantic,
16696 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
16701 error ("storage class %qD applied to template instantiation", storage);
16703 check_explicit_instantiation_namespace (result);
16704 mark_decl_instantiated (result, extern_p);
16706 instantiate_decl (result, /*defer_ok=*/1,
16707 /*expl_inst_class_mem_p=*/false);
16711 mark_class_instantiated (tree t, int extern_p)
16713 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
16714 SET_CLASSTYPE_INTERFACE_KNOWN (t);
16715 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
16716 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
16719 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
16720 rest_of_type_compilation (t, 1);
16724 /* Called from do_type_instantiation through binding_table_foreach to
16725 do recursive instantiation for the type bound in ENTRY. */
16727 bt_instantiate_type_proc (binding_entry entry, void *data)
16729 tree storage = *(tree *) data;
16731 if (MAYBE_CLASS_TYPE_P (entry->type)
16732 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
16733 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
16736 /* Called from do_type_instantiation to instantiate a member
16737 (a member function or a static member variable) of an
16738 explicitly instantiated class template. */
16740 instantiate_class_member (tree decl, int extern_p)
16742 mark_decl_instantiated (decl, extern_p);
16744 instantiate_decl (decl, /*defer_ok=*/1,
16745 /*expl_inst_class_mem_p=*/true);
16748 /* Perform an explicit instantiation of template class T. STORAGE, if
16749 non-null, is the RID for extern, inline or static. COMPLAIN is
16750 nonzero if this is called from the parser, zero if called recursively,
16751 since the standard is unclear (as detailed below). */
16754 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
16759 int previous_instantiation_extern_p = 0;
16761 if (TREE_CODE (t) == TYPE_DECL)
16764 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
16766 error ("explicit instantiation of non-template type %qT", t);
16772 if (!COMPLETE_TYPE_P (t))
16774 if (complain & tf_error)
16775 error ("explicit instantiation of %q#T before definition of template",
16780 if (storage != NULL_TREE)
16782 if (!in_system_header)
16784 if (storage == ridpointers[(int) RID_EXTERN])
16786 if (cxx_dialect == cxx98)
16787 pedwarn (input_location, OPT_pedantic,
16788 "ISO C++ 1998 forbids the use of %<extern%> on "
16789 "explicit instantiations");
16792 pedwarn (input_location, OPT_pedantic,
16793 "ISO C++ forbids the use of %qE"
16794 " on explicit instantiations", storage);
16797 if (storage == ridpointers[(int) RID_INLINE])
16799 else if (storage == ridpointers[(int) RID_EXTERN])
16801 else if (storage == ridpointers[(int) RID_STATIC])
16805 error ("storage class %qD applied to template instantiation",
16811 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
16813 /* DR 259 [temp.spec].
16815 Both an explicit instantiation and a declaration of an explicit
16816 specialization shall not appear in a program unless the explicit
16817 instantiation follows a declaration of the explicit specialization.
16819 For a given set of template parameters, if an explicit
16820 instantiation of a template appears after a declaration of an
16821 explicit specialization for that template, the explicit
16822 instantiation has no effect. */
16825 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
16829 No program shall explicitly instantiate any template more
16832 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
16833 instantiation was `extern'. If EXTERN_P then the second is.
16834 These cases are OK. */
16835 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
16837 if (!previous_instantiation_extern_p && !extern_p
16838 && (complain & tf_error))
16839 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
16841 /* If we've already instantiated the template, just return now. */
16842 if (!CLASSTYPE_INTERFACE_ONLY (t))
16846 check_explicit_instantiation_namespace (TYPE_NAME (t));
16847 mark_class_instantiated (t, extern_p);
16855 /* In contrast to implicit instantiation, where only the
16856 declarations, and not the definitions, of members are
16857 instantiated, we have here:
16861 The explicit instantiation of a class template specialization
16862 implies the instantiation of all of its members not
16863 previously explicitly specialized in the translation unit
16864 containing the explicit instantiation.
16866 Of course, we can't instantiate member template classes, since
16867 we don't have any arguments for them. Note that the standard
16868 is unclear on whether the instantiation of the members are
16869 *explicit* instantiations or not. However, the most natural
16870 interpretation is that it should be an explicit instantiation. */
16873 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
16874 if (TREE_CODE (tmp) == FUNCTION_DECL
16875 && DECL_TEMPLATE_INSTANTIATION (tmp))
16876 instantiate_class_member (tmp, extern_p);
16878 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
16879 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
16880 instantiate_class_member (tmp, extern_p);
16882 if (CLASSTYPE_NESTED_UTDS (t))
16883 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
16884 bt_instantiate_type_proc, &storage);
16888 /* Given a function DECL, which is a specialization of TMPL, modify
16889 DECL to be a re-instantiation of TMPL with the same template
16890 arguments. TMPL should be the template into which tsubst'ing
16891 should occur for DECL, not the most general template.
16893 One reason for doing this is a scenario like this:
16896 void f(const T&, int i);
16898 void g() { f(3, 7); }
16901 void f(const T& t, const int i) { }
16903 Note that when the template is first instantiated, with
16904 instantiate_template, the resulting DECL will have no name for the
16905 first parameter, and the wrong type for the second. So, when we go
16906 to instantiate the DECL, we regenerate it. */
16909 regenerate_decl_from_template (tree decl, tree tmpl)
16911 /* The arguments used to instantiate DECL, from the most general
16916 args = DECL_TI_ARGS (decl);
16917 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
16919 /* Make sure that we can see identifiers, and compute access
16921 push_access_scope (decl);
16923 if (TREE_CODE (decl) == FUNCTION_DECL)
16931 args_depth = TMPL_ARGS_DEPTH (args);
16932 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
16933 if (args_depth > parms_depth)
16934 args = get_innermost_template_args (args, parms_depth);
16936 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
16937 args, tf_error, NULL_TREE);
16939 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
16942 /* Merge parameter declarations. */
16943 decl_parm = skip_artificial_parms_for (decl,
16944 DECL_ARGUMENTS (decl));
16946 = skip_artificial_parms_for (code_pattern,
16947 DECL_ARGUMENTS (code_pattern));
16948 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
16953 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16954 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
16955 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
16957 parm_type = type_decays_to (parm_type);
16958 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16959 TREE_TYPE (decl_parm) = parm_type;
16960 attributes = DECL_ATTRIBUTES (pattern_parm);
16961 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16963 DECL_ATTRIBUTES (decl_parm) = attributes;
16964 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16966 decl_parm = DECL_CHAIN (decl_parm);
16967 pattern_parm = DECL_CHAIN (pattern_parm);
16969 /* Merge any parameters that match with the function parameter
16971 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
16974 tree expanded_types;
16975 /* Expand the TYPE_PACK_EXPANSION that provides the types for
16976 the parameters in this function parameter pack. */
16977 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
16978 args, tf_error, NULL_TREE);
16979 len = TREE_VEC_LENGTH (expanded_types);
16980 for (i = 0; i < len; i++)
16985 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16986 /* Rename the parameter to include the index. */
16987 DECL_NAME (decl_parm) =
16988 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
16989 parm_type = TREE_VEC_ELT (expanded_types, i);
16990 parm_type = type_decays_to (parm_type);
16991 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16992 TREE_TYPE (decl_parm) = parm_type;
16993 attributes = DECL_ATTRIBUTES (pattern_parm);
16994 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16996 DECL_ATTRIBUTES (decl_parm) = attributes;
16997 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16999 decl_parm = DECL_CHAIN (decl_parm);
17002 /* Merge additional specifiers from the CODE_PATTERN. */
17003 if (DECL_DECLARED_INLINE_P (code_pattern)
17004 && !DECL_DECLARED_INLINE_P (decl))
17005 DECL_DECLARED_INLINE_P (decl) = 1;
17007 else if (TREE_CODE (decl) == VAR_DECL)
17009 DECL_INITIAL (decl) =
17010 tsubst_expr (DECL_INITIAL (code_pattern), args,
17011 tf_error, DECL_TI_TEMPLATE (decl),
17012 /*integral_constant_expression_p=*/false);
17013 if (VAR_HAD_UNKNOWN_BOUND (decl))
17014 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
17015 tf_error, DECL_TI_TEMPLATE (decl));
17018 gcc_unreachable ();
17020 pop_access_scope (decl);
17023 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
17024 substituted to get DECL. */
17027 template_for_substitution (tree decl)
17029 tree tmpl = DECL_TI_TEMPLATE (decl);
17031 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
17032 for the instantiation. This is not always the most general
17033 template. Consider, for example:
17036 struct S { template <class U> void f();
17037 template <> void f<int>(); };
17039 and an instantiation of S<double>::f<int>. We want TD to be the
17040 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
17041 while (/* An instantiation cannot have a definition, so we need a
17042 more general template. */
17043 DECL_TEMPLATE_INSTANTIATION (tmpl)
17044 /* We must also deal with friend templates. Given:
17046 template <class T> struct S {
17047 template <class U> friend void f() {};
17050 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
17051 so far as the language is concerned, but that's still
17052 where we get the pattern for the instantiation from. On
17053 other hand, if the definition comes outside the class, say:
17055 template <class T> struct S {
17056 template <class U> friend void f();
17058 template <class U> friend void f() {}
17060 we don't need to look any further. That's what the check for
17061 DECL_INITIAL is for. */
17062 || (TREE_CODE (decl) == FUNCTION_DECL
17063 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
17064 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
17066 /* The present template, TD, should not be a definition. If it
17067 were a definition, we should be using it! Note that we
17068 cannot restructure the loop to just keep going until we find
17069 a template with a definition, since that might go too far if
17070 a specialization was declared, but not defined. */
17071 gcc_assert (TREE_CODE (decl) != VAR_DECL
17072 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
17074 /* Fetch the more general template. */
17075 tmpl = DECL_TI_TEMPLATE (tmpl);
17081 /* Returns true if we need to instantiate this template instance even if we
17082 know we aren't going to emit it.. */
17085 always_instantiate_p (tree decl)
17087 /* We always instantiate inline functions so that we can inline them. An
17088 explicit instantiation declaration prohibits implicit instantiation of
17089 non-inline functions. With high levels of optimization, we would
17090 normally inline non-inline functions -- but we're not allowed to do
17091 that for "extern template" functions. Therefore, we check
17092 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
17093 return ((TREE_CODE (decl) == FUNCTION_DECL
17094 && DECL_DECLARED_INLINE_P (decl))
17095 /* And we need to instantiate static data members so that
17096 their initializers are available in integral constant
17098 || (TREE_CODE (decl) == VAR_DECL
17099 && decl_maybe_constant_var_p (decl)));
17102 /* Produce the definition of D, a _DECL generated from a template. If
17103 DEFER_OK is nonzero, then we don't have to actually do the
17104 instantiation now; we just have to do it sometime. Normally it is
17105 an error if this is an explicit instantiation but D is undefined.
17106 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
17107 explicitly instantiated class template. */
17110 instantiate_decl (tree d, int defer_ok,
17111 bool expl_inst_class_mem_p)
17113 tree tmpl = DECL_TI_TEMPLATE (d);
17120 bool pattern_defined;
17122 location_t saved_loc = input_location;
17125 /* This function should only be used to instantiate templates for
17126 functions and static member variables. */
17127 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
17128 || TREE_CODE (d) == VAR_DECL);
17130 /* Variables are never deferred; if instantiation is required, they
17131 are instantiated right away. That allows for better code in the
17132 case that an expression refers to the value of the variable --
17133 if the variable has a constant value the referring expression can
17134 take advantage of that fact. */
17135 if (TREE_CODE (d) == VAR_DECL
17136 || DECL_DECLARED_CONSTEXPR_P (d))
17139 /* Don't instantiate cloned functions. Instead, instantiate the
17140 functions they cloned. */
17141 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
17142 d = DECL_CLONED_FUNCTION (d);
17144 if (DECL_TEMPLATE_INSTANTIATED (d)
17145 || DECL_TEMPLATE_SPECIALIZATION (d))
17146 /* D has already been instantiated or explicitly specialized, so
17147 there's nothing for us to do here.
17149 It might seem reasonable to check whether or not D is an explicit
17150 instantiation, and, if so, stop here. But when an explicit
17151 instantiation is deferred until the end of the compilation,
17152 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
17153 the instantiation. */
17156 /* Check to see whether we know that this template will be
17157 instantiated in some other file, as with "extern template"
17159 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
17161 /* In general, we do not instantiate such templates. */
17162 if (external_p && !always_instantiate_p (d))
17165 gen_tmpl = most_general_template (tmpl);
17166 gen_args = DECL_TI_ARGS (d);
17168 if (tmpl != gen_tmpl)
17169 /* We should already have the extra args. */
17170 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
17171 == TMPL_ARGS_DEPTH (gen_args));
17172 /* And what's in the hash table should match D. */
17173 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
17174 || spec == NULL_TREE);
17176 /* This needs to happen before any tsubsting. */
17177 if (! push_tinst_level (d))
17180 timevar_push (TV_PARSE);
17182 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
17183 for the instantiation. */
17184 td = template_for_substitution (d);
17185 code_pattern = DECL_TEMPLATE_RESULT (td);
17187 /* We should never be trying to instantiate a member of a class
17188 template or partial specialization. */
17189 gcc_assert (d != code_pattern);
17191 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
17192 || DECL_TEMPLATE_SPECIALIZATION (td))
17193 /* In the case of a friend template whose definition is provided
17194 outside the class, we may have too many arguments. Drop the
17195 ones we don't need. The same is true for specializations. */
17196 args = get_innermost_template_args
17197 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
17201 if (TREE_CODE (d) == FUNCTION_DECL)
17202 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
17204 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
17206 /* We may be in the middle of deferred access check. Disable it now. */
17207 push_deferring_access_checks (dk_no_deferred);
17209 /* Unless an explicit instantiation directive has already determined
17210 the linkage of D, remember that a definition is available for
17212 if (pattern_defined
17213 && !DECL_INTERFACE_KNOWN (d)
17214 && !DECL_NOT_REALLY_EXTERN (d))
17215 mark_definable (d);
17217 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
17218 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
17219 input_location = DECL_SOURCE_LOCATION (d);
17221 /* If D is a member of an explicitly instantiated class template,
17222 and no definition is available, treat it like an implicit
17224 if (!pattern_defined && expl_inst_class_mem_p
17225 && DECL_EXPLICIT_INSTANTIATION (d))
17227 DECL_NOT_REALLY_EXTERN (d) = 0;
17228 DECL_INTERFACE_KNOWN (d) = 0;
17229 SET_DECL_IMPLICIT_INSTANTIATION (d);
17232 /* Recheck the substitutions to obtain any warning messages
17233 about ignoring cv qualifiers. Don't do this for artificial decls,
17234 as it breaks the context-sensitive substitution for lambda op(). */
17235 if (!defer_ok && !DECL_ARTIFICIAL (d))
17237 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
17238 tree type = TREE_TYPE (gen);
17240 /* Make sure that we can see identifiers, and compute access
17241 correctly. D is already the target FUNCTION_DECL with the
17243 push_access_scope (d);
17245 if (TREE_CODE (gen) == FUNCTION_DECL)
17247 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
17248 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
17250 /* Don't simply tsubst the function type, as that will give
17251 duplicate warnings about poor parameter qualifications.
17252 The function arguments are the same as the decl_arguments
17253 without the top level cv qualifiers. */
17254 type = TREE_TYPE (type);
17256 tsubst (type, gen_args, tf_warning_or_error, d);
17258 pop_access_scope (d);
17261 /* Defer all other templates, unless we have been explicitly
17262 forbidden from doing so. */
17263 if (/* If there is no definition, we cannot instantiate the
17266 /* If it's OK to postpone instantiation, do so. */
17268 /* If this is a static data member that will be defined
17269 elsewhere, we don't want to instantiate the entire data
17270 member, but we do want to instantiate the initializer so that
17271 we can substitute that elsewhere. */
17272 || (external_p && TREE_CODE (d) == VAR_DECL))
17274 /* The definition of the static data member is now required so
17275 we must substitute the initializer. */
17276 if (TREE_CODE (d) == VAR_DECL
17277 && !DECL_INITIAL (d)
17278 && DECL_INITIAL (code_pattern))
17282 bool const_init = false;
17284 ns = decl_namespace_context (d);
17285 push_nested_namespace (ns);
17286 push_nested_class (DECL_CONTEXT (d));
17287 init = tsubst_expr (DECL_INITIAL (code_pattern),
17289 tf_warning_or_error, NULL_TREE,
17290 /*integral_constant_expression_p=*/false);
17291 /* Make sure the initializer is still constant, in case of
17292 circular dependency (template/instantiate6.C). */
17294 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
17295 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
17296 /*asmspec_tree=*/NULL_TREE,
17297 LOOKUP_ONLYCONVERTING);
17298 pop_nested_class ();
17299 pop_nested_namespace (ns);
17302 /* We restore the source position here because it's used by
17303 add_pending_template. */
17304 input_location = saved_loc;
17306 if (at_eof && !pattern_defined
17307 && DECL_EXPLICIT_INSTANTIATION (d)
17308 && DECL_NOT_REALLY_EXTERN (d))
17311 The definition of a non-exported function template, a
17312 non-exported member function template, or a non-exported
17313 member function or static data member of a class template
17314 shall be present in every translation unit in which it is
17315 explicitly instantiated. */
17316 permerror (input_location, "explicit instantiation of %qD "
17317 "but no definition available", d);
17319 /* If we're in unevaluated context, we just wanted to get the
17320 constant value; this isn't an odr use, so don't queue
17321 a full instantiation. */
17322 if (cp_unevaluated_operand != 0)
17324 /* ??? Historically, we have instantiated inline functions, even
17325 when marked as "extern template". */
17326 if (!(external_p && TREE_CODE (d) == VAR_DECL))
17327 add_pending_template (d);
17330 /* Tell the repository that D is available in this translation unit
17331 -- and see if it is supposed to be instantiated here. */
17332 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
17334 /* In a PCH file, despite the fact that the repository hasn't
17335 requested instantiation in the PCH it is still possible that
17336 an instantiation will be required in a file that includes the
17339 add_pending_template (d);
17340 /* Instantiate inline functions so that the inliner can do its
17341 job, even though we'll not be emitting a copy of this
17343 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
17347 need_push = !cfun || !global_bindings_p ();
17349 push_to_top_level ();
17351 /* Mark D as instantiated so that recursive calls to
17352 instantiate_decl do not try to instantiate it again. */
17353 DECL_TEMPLATE_INSTANTIATED (d) = 1;
17355 /* Regenerate the declaration in case the template has been modified
17356 by a subsequent redeclaration. */
17357 regenerate_decl_from_template (d, td);
17359 /* We already set the file and line above. Reset them now in case
17360 they changed as a result of calling regenerate_decl_from_template. */
17361 input_location = DECL_SOURCE_LOCATION (d);
17363 if (TREE_CODE (d) == VAR_DECL)
17366 bool const_init = false;
17368 /* Clear out DECL_RTL; whatever was there before may not be right
17369 since we've reset the type of the declaration. */
17370 SET_DECL_RTL (d, NULL);
17371 DECL_IN_AGGR_P (d) = 0;
17373 /* The initializer is placed in DECL_INITIAL by
17374 regenerate_decl_from_template so we don't need to
17375 push/pop_access_scope again here. Pull it out so that
17376 cp_finish_decl can process it. */
17377 init = DECL_INITIAL (d);
17378 DECL_INITIAL (d) = NULL_TREE;
17379 DECL_INITIALIZED_P (d) = 0;
17381 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
17382 initializer. That function will defer actual emission until
17383 we have a chance to determine linkage. */
17384 DECL_EXTERNAL (d) = 0;
17386 /* Enter the scope of D so that access-checking works correctly. */
17387 push_nested_class (DECL_CONTEXT (d));
17388 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
17389 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
17390 pop_nested_class ();
17392 else if (TREE_CODE (d) == FUNCTION_DECL)
17394 htab_t saved_local_specializations;
17399 /* Save away the current list, in case we are instantiating one
17400 template from within the body of another. */
17401 saved_local_specializations = local_specializations;
17403 /* Set up the list of local specializations. */
17404 local_specializations = htab_create (37,
17405 hash_local_specialization,
17406 eq_local_specializations,
17409 /* Set up context. */
17410 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
17412 /* Create substitution entries for the parameters. */
17413 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
17414 tmpl_parm = DECL_ARGUMENTS (subst_decl);
17415 spec_parm = DECL_ARGUMENTS (d);
17416 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
17418 register_local_specialization (spec_parm, tmpl_parm);
17419 spec_parm = skip_artificial_parms_for (d, spec_parm);
17420 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
17422 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
17424 register_local_specialization (spec_parm, tmpl_parm);
17425 tmpl_parm = DECL_CHAIN (tmpl_parm);
17426 spec_parm = DECL_CHAIN (spec_parm);
17428 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
17430 /* Register the (value) argument pack as a specialization of
17431 TMPL_PARM, then move on. */
17432 tree argpack = make_fnparm_pack (spec_parm);
17433 register_local_specialization (argpack, tmpl_parm);
17434 tmpl_parm = DECL_CHAIN (tmpl_parm);
17435 spec_parm = NULL_TREE;
17437 gcc_assert (!spec_parm);
17439 /* Substitute into the body of the function. */
17440 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
17441 tf_warning_or_error, tmpl,
17442 /*integral_constant_expression_p=*/false);
17444 /* Set the current input_location to the end of the function
17445 so that finish_function knows where we are. */
17446 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
17448 /* We don't need the local specializations any more. */
17449 htab_delete (local_specializations);
17450 local_specializations = saved_local_specializations;
17452 /* Finish the function. */
17453 d = finish_function (0);
17454 expand_or_defer_fn (d);
17457 /* We're not deferring instantiation any more. */
17458 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
17461 pop_from_top_level ();
17464 input_location = saved_loc;
17465 pop_deferring_access_checks ();
17466 pop_tinst_level ();
17468 timevar_pop (TV_PARSE);
17473 /* Run through the list of templates that we wish we could
17474 instantiate, and instantiate any we can. RETRIES is the
17475 number of times we retry pending template instantiation. */
17478 instantiate_pending_templates (int retries)
17481 location_t saved_loc = input_location;
17483 /* Instantiating templates may trigger vtable generation. This in turn
17484 may require further template instantiations. We place a limit here
17485 to avoid infinite loop. */
17486 if (pending_templates && retries >= max_tinst_depth)
17488 tree decl = pending_templates->tinst->decl;
17490 error ("template instantiation depth exceeds maximum of %d"
17491 " instantiating %q+D, possibly from virtual table generation"
17492 " (use -ftemplate-depth= to increase the maximum)",
17493 max_tinst_depth, decl);
17494 if (TREE_CODE (decl) == FUNCTION_DECL)
17495 /* Pretend that we defined it. */
17496 DECL_INITIAL (decl) = error_mark_node;
17502 struct pending_template **t = &pending_templates;
17503 struct pending_template *last = NULL;
17507 tree instantiation = reopen_tinst_level ((*t)->tinst);
17508 bool complete = false;
17510 if (TYPE_P (instantiation))
17514 if (!COMPLETE_TYPE_P (instantiation))
17516 instantiate_class_template (instantiation);
17517 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
17518 for (fn = TYPE_METHODS (instantiation);
17520 fn = TREE_CHAIN (fn))
17521 if (! DECL_ARTIFICIAL (fn))
17522 instantiate_decl (fn,
17524 /*expl_inst_class_mem_p=*/false);
17525 if (COMPLETE_TYPE_P (instantiation))
17529 complete = COMPLETE_TYPE_P (instantiation);
17533 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
17534 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
17537 = instantiate_decl (instantiation,
17539 /*expl_inst_class_mem_p=*/false);
17540 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
17544 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
17545 || DECL_TEMPLATE_INSTANTIATED (instantiation));
17549 /* If INSTANTIATION has been instantiated, then we don't
17550 need to consider it again in the future. */
17558 current_tinst_level = NULL;
17560 last_pending_template = last;
17562 while (reconsider);
17564 input_location = saved_loc;
17567 /* Substitute ARGVEC into T, which is a list of initializers for
17568 either base class or a non-static data member. The TREE_PURPOSEs
17569 are DECLs, and the TREE_VALUEs are the initializer values. Used by
17570 instantiate_decl. */
17573 tsubst_initializer_list (tree t, tree argvec)
17575 tree inits = NULL_TREE;
17577 for (; t; t = TREE_CHAIN (t))
17581 tree expanded_bases = NULL_TREE;
17582 tree expanded_arguments = NULL_TREE;
17585 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
17590 /* Expand the base class expansion type into separate base
17592 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
17593 tf_warning_or_error,
17595 if (expanded_bases == error_mark_node)
17598 /* We'll be building separate TREE_LISTs of arguments for
17600 len = TREE_VEC_LENGTH (expanded_bases);
17601 expanded_arguments = make_tree_vec (len);
17602 for (i = 0; i < len; i++)
17603 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
17605 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
17606 expand each argument in the TREE_VALUE of t. */
17607 expr = make_node (EXPR_PACK_EXPANSION);
17608 PACK_EXPANSION_PARAMETER_PACKS (expr) =
17609 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
17611 if (TREE_VALUE (t) == void_type_node)
17612 /* VOID_TYPE_NODE is used to indicate
17613 value-initialization. */
17615 for (i = 0; i < len; i++)
17616 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
17620 /* Substitute parameter packs into each argument in the
17622 in_base_initializer = 1;
17623 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
17625 tree expanded_exprs;
17627 /* Expand the argument. */
17628 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
17630 = tsubst_pack_expansion (expr, argvec,
17631 tf_warning_or_error,
17633 if (expanded_exprs == error_mark_node)
17636 /* Prepend each of the expanded expressions to the
17637 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
17638 for (i = 0; i < len; i++)
17640 TREE_VEC_ELT (expanded_arguments, i) =
17641 tree_cons (NULL_TREE,
17642 TREE_VEC_ELT (expanded_exprs, i),
17643 TREE_VEC_ELT (expanded_arguments, i));
17646 in_base_initializer = 0;
17648 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
17649 since we built them backwards. */
17650 for (i = 0; i < len; i++)
17652 TREE_VEC_ELT (expanded_arguments, i) =
17653 nreverse (TREE_VEC_ELT (expanded_arguments, i));
17658 for (i = 0; i < len; ++i)
17660 if (expanded_bases)
17662 decl = TREE_VEC_ELT (expanded_bases, i);
17663 decl = expand_member_init (decl);
17664 init = TREE_VEC_ELT (expanded_arguments, i);
17668 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
17669 tf_warning_or_error, NULL_TREE);
17671 decl = expand_member_init (decl);
17672 if (decl && !DECL_P (decl))
17673 in_base_initializer = 1;
17675 init = TREE_VALUE (t);
17676 if (init != void_type_node)
17677 init = tsubst_expr (init, argvec,
17678 tf_warning_or_error, NULL_TREE,
17679 /*integral_constant_expression_p=*/false);
17680 in_base_initializer = 0;
17685 init = build_tree_list (decl, init);
17686 TREE_CHAIN (init) = inits;
17694 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
17697 set_current_access_from_decl (tree decl)
17699 if (TREE_PRIVATE (decl))
17700 current_access_specifier = access_private_node;
17701 else if (TREE_PROTECTED (decl))
17702 current_access_specifier = access_protected_node;
17704 current_access_specifier = access_public_node;
17707 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
17708 is the instantiation (which should have been created with
17709 start_enum) and ARGS are the template arguments to use. */
17712 tsubst_enum (tree tag, tree newtag, tree args)
17716 if (SCOPED_ENUM_P (newtag))
17717 begin_scope (sk_scoped_enum, newtag);
17719 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
17724 decl = TREE_VALUE (e);
17725 /* Note that in a template enum, the TREE_VALUE is the
17726 CONST_DECL, not the corresponding INTEGER_CST. */
17727 value = tsubst_expr (DECL_INITIAL (decl),
17728 args, tf_warning_or_error, NULL_TREE,
17729 /*integral_constant_expression_p=*/true);
17731 /* Give this enumeration constant the correct access. */
17732 set_current_access_from_decl (decl);
17734 /* Actually build the enumerator itself. */
17736 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
17739 if (SCOPED_ENUM_P (newtag))
17742 finish_enum_value_list (newtag);
17743 finish_enum (newtag);
17745 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
17746 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
17749 /* DECL is a FUNCTION_DECL that is a template specialization. Return
17750 its type -- but without substituting the innermost set of template
17751 arguments. So, innermost set of template parameters will appear in
17755 get_mostly_instantiated_function_type (tree decl)
17763 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
17764 targs = DECL_TI_ARGS (decl);
17765 tparms = DECL_TEMPLATE_PARMS (tmpl);
17766 parm_depth = TMPL_PARMS_DEPTH (tparms);
17768 /* There should be as many levels of arguments as there are levels
17770 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
17772 fn_type = TREE_TYPE (tmpl);
17774 if (parm_depth == 1)
17775 /* No substitution is necessary. */
17779 int i, save_access_control;
17782 /* Replace the innermost level of the TARGS with NULL_TREEs to
17783 let tsubst know not to substitute for those parameters. */
17784 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
17785 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
17786 SET_TMPL_ARGS_LEVEL (partial_args, i,
17787 TMPL_ARGS_LEVEL (targs, i));
17788 SET_TMPL_ARGS_LEVEL (partial_args,
17789 TMPL_ARGS_DEPTH (targs),
17790 make_tree_vec (DECL_NTPARMS (tmpl)));
17792 /* Disable access control as this function is used only during
17794 save_access_control = flag_access_control;
17795 flag_access_control = 0;
17797 ++processing_template_decl;
17798 /* Now, do the (partial) substitution to figure out the
17799 appropriate function type. */
17800 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
17801 --processing_template_decl;
17803 /* Substitute into the template parameters to obtain the real
17804 innermost set of parameters. This step is important if the
17805 innermost set of template parameters contains value
17806 parameters whose types depend on outer template parameters. */
17807 TREE_VEC_LENGTH (partial_args)--;
17808 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
17810 flag_access_control = save_access_control;
17816 /* Return truthvalue if we're processing a template different from
17817 the last one involved in diagnostics. */
17819 problematic_instantiation_changed (void)
17821 return last_template_error_tick != tinst_level_tick;
17824 /* Remember current template involved in diagnostics. */
17826 record_last_problematic_instantiation (void)
17828 last_template_error_tick = tinst_level_tick;
17831 struct tinst_level *
17832 current_instantiation (void)
17834 return current_tinst_level;
17837 /* [temp.param] Check that template non-type parm TYPE is of an allowable
17838 type. Return zero for ok, nonzero for disallowed. Issue error and
17839 warning messages under control of COMPLAIN. */
17842 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
17844 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
17846 else if (POINTER_TYPE_P (type))
17848 else if (TYPE_PTR_TO_MEMBER_P (type))
17850 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
17852 else if (TREE_CODE (type) == TYPENAME_TYPE)
17855 if (complain & tf_error)
17856 error ("%q#T is not a valid type for a template constant parameter", type);
17860 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
17861 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
17864 dependent_type_p_r (tree type)
17870 A type is dependent if it is:
17872 -- a template parameter. Template template parameters are types
17873 for us (since TYPE_P holds true for them) so we handle
17875 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17876 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
17878 /* -- a qualified-id with a nested-name-specifier which contains a
17879 class-name that names a dependent type or whose unqualified-id
17880 names a dependent type. */
17881 if (TREE_CODE (type) == TYPENAME_TYPE)
17883 /* -- a cv-qualified type where the cv-unqualified type is
17885 type = TYPE_MAIN_VARIANT (type);
17886 /* -- a compound type constructed from any dependent type. */
17887 if (TYPE_PTR_TO_MEMBER_P (type))
17888 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
17889 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
17891 else if (TREE_CODE (type) == POINTER_TYPE
17892 || TREE_CODE (type) == REFERENCE_TYPE)
17893 return dependent_type_p (TREE_TYPE (type));
17894 else if (TREE_CODE (type) == FUNCTION_TYPE
17895 || TREE_CODE (type) == METHOD_TYPE)
17899 if (dependent_type_p (TREE_TYPE (type)))
17901 for (arg_type = TYPE_ARG_TYPES (type);
17903 arg_type = TREE_CHAIN (arg_type))
17904 if (dependent_type_p (TREE_VALUE (arg_type)))
17908 /* -- an array type constructed from any dependent type or whose
17909 size is specified by a constant expression that is
17912 We checked for type- and value-dependence of the bounds in
17913 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
17914 if (TREE_CODE (type) == ARRAY_TYPE)
17916 if (TYPE_DOMAIN (type)
17917 && dependent_type_p (TYPE_DOMAIN (type)))
17919 return dependent_type_p (TREE_TYPE (type));
17922 /* -- a template-id in which either the template name is a template
17924 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
17926 /* ... or any of the template arguments is a dependent type or
17927 an expression that is type-dependent or value-dependent. */
17928 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
17929 && (any_dependent_template_arguments_p
17930 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
17933 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
17934 argument of the `typeof' expression is not type-dependent, then
17935 it should already been have resolved. */
17936 if (TREE_CODE (type) == TYPEOF_TYPE
17937 || TREE_CODE (type) == DECLTYPE_TYPE)
17940 /* A template argument pack is dependent if any of its packed
17942 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
17944 tree args = ARGUMENT_PACK_ARGS (type);
17945 int i, len = TREE_VEC_LENGTH (args);
17946 for (i = 0; i < len; ++i)
17947 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17951 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
17952 be template parameters. */
17953 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
17956 /* The standard does not specifically mention types that are local
17957 to template functions or local classes, but they should be
17958 considered dependent too. For example:
17960 template <int I> void f() {
17965 The size of `E' cannot be known until the value of `I' has been
17966 determined. Therefore, `E' must be considered dependent. */
17967 scope = TYPE_CONTEXT (type);
17968 if (scope && TYPE_P (scope))
17969 return dependent_type_p (scope);
17970 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
17971 return type_dependent_expression_p (scope);
17973 /* Other types are non-dependent. */
17977 /* Returns TRUE if TYPE is dependent, in the sense of
17978 [temp.dep.type]. Note that a NULL type is considered dependent. */
17981 dependent_type_p (tree type)
17983 /* If there are no template parameters in scope, then there can't be
17984 any dependent types. */
17985 if (!processing_template_decl)
17987 /* If we are not processing a template, then nobody should be
17988 providing us with a dependent type. */
17990 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
17994 /* If the type is NULL, we have not computed a type for the entity
17995 in question; in that case, the type is dependent. */
17999 /* Erroneous types can be considered non-dependent. */
18000 if (type == error_mark_node)
18003 /* If we have not already computed the appropriate value for TYPE,
18005 if (!TYPE_DEPENDENT_P_VALID (type))
18007 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
18008 TYPE_DEPENDENT_P_VALID (type) = 1;
18011 return TYPE_DEPENDENT_P (type);
18014 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
18015 lookup. In other words, a dependent type that is not the current
18019 dependent_scope_p (tree scope)
18021 return (scope && TYPE_P (scope) && dependent_type_p (scope)
18022 && !currently_open_class (scope));
18025 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
18026 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
18029 /* Note that this predicate is not appropriate for general expressions;
18030 only constant expressions (that satisfy potential_constant_expression)
18031 can be tested for value dependence.
18033 We should really also have a predicate for "instantiation-dependent".
18035 fold_non_dependent_expr: fold if constant and not type-dependent and not value-dependent.
18036 (what about instantiation-dependent constant-expressions?)
18037 is_late_template_attribute: defer if instantiation-dependent.
18038 compute_array_index_type: proceed if constant and not t- or v-dependent
18039 if instantiation-dependent, need to remember full expression
18040 uses_template_parms: FIXME - need to audit callers
18041 tsubst_decl [function_decl]: Why is this using value_dependent_expression_p?
18042 dependent_type_p [array_type]: dependent if index type is dependent
18044 static_assert - instantiation-dependent */
18047 value_dependent_expression_p (tree expression)
18049 if (!processing_template_decl)
18052 /* A name declared with a dependent type. */
18053 if (DECL_P (expression) && type_dependent_expression_p (expression))
18056 switch (TREE_CODE (expression))
18058 case IDENTIFIER_NODE:
18059 /* A name that has not been looked up -- must be dependent. */
18062 case TEMPLATE_PARM_INDEX:
18063 /* A non-type template parm. */
18067 /* A non-type template parm. */
18068 if (DECL_TEMPLATE_PARM_P (expression))
18070 return value_dependent_expression_p (DECL_INITIAL (expression));
18073 /* A constant with integral or enumeration type and is initialized
18074 with an expression that is value-dependent. */
18075 if (DECL_INITIAL (expression)
18076 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
18077 && value_dependent_expression_p (DECL_INITIAL (expression)))
18081 case DYNAMIC_CAST_EXPR:
18082 case STATIC_CAST_EXPR:
18083 case CONST_CAST_EXPR:
18084 case REINTERPRET_CAST_EXPR:
18086 /* These expressions are value-dependent if the type to which
18087 the cast occurs is dependent or the expression being casted
18088 is value-dependent. */
18090 tree type = TREE_TYPE (expression);
18092 if (dependent_type_p (type))
18095 /* A functional cast has a list of operands. */
18096 expression = TREE_OPERAND (expression, 0);
18099 /* If there are no operands, it must be an expression such
18100 as "int()". This should not happen for aggregate types
18101 because it would form non-constant expressions. */
18102 gcc_assert (cxx_dialect >= cxx0x
18103 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
18108 if (TREE_CODE (expression) == TREE_LIST)
18109 return any_value_dependent_elements_p (expression);
18111 return value_dependent_expression_p (expression);
18117 /* A `sizeof' expression is value-dependent if the operand is
18118 type-dependent or is a pack expansion. */
18119 expression = TREE_OPERAND (expression, 0);
18120 if (PACK_EXPANSION_P (expression))
18122 else if (TYPE_P (expression))
18123 return dependent_type_p (expression);
18124 return type_dependent_expression_p (expression);
18126 case AT_ENCODE_EXPR:
18127 /* An 'encode' expression is value-dependent if the operand is
18129 expression = TREE_OPERAND (expression, 0);
18130 return dependent_type_p (expression);
18132 case NOEXCEPT_EXPR:
18133 expression = TREE_OPERAND (expression, 0);
18134 /* FIXME why check value-dependency? */
18135 return (type_dependent_expression_p (expression)
18136 || value_dependent_expression_p (expression));
18140 tree name = TREE_OPERAND (expression, 1);
18141 return value_dependent_expression_p (name);
18144 case COMPONENT_REF:
18145 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
18146 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
18148 case NONTYPE_ARGUMENT_PACK:
18149 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
18150 is value-dependent. */
18152 tree values = ARGUMENT_PACK_ARGS (expression);
18153 int i, len = TREE_VEC_LENGTH (values);
18155 for (i = 0; i < len; ++i)
18156 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
18164 tree type2 = TRAIT_EXPR_TYPE2 (expression);
18165 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
18166 || (type2 ? dependent_type_p (type2) : false));
18170 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
18171 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
18174 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
18175 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
18179 tree op = TREE_OPERAND (expression, 0);
18180 return (value_dependent_expression_p (op)
18181 || has_value_dependent_address (op));
18186 tree fn = get_callee_fndecl (expression);
18188 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
18190 nargs = call_expr_nargs (expression);
18191 for (i = 0; i < nargs; ++i)
18193 tree op = CALL_EXPR_ARG (expression, i);
18194 /* In a call to a constexpr member function, look through the
18195 implicit ADDR_EXPR on the object argument so that it doesn't
18196 cause the call to be considered value-dependent. We also
18197 look through it in potential_constant_expression. */
18198 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
18199 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
18200 && TREE_CODE (op) == ADDR_EXPR)
18201 op = TREE_OPERAND (op, 0);
18202 if (value_dependent_expression_p (op))
18209 /* A constant expression is value-dependent if any subexpression is
18210 value-dependent. */
18211 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
18213 case tcc_reference:
18215 return (value_dependent_expression_p
18216 (TREE_OPERAND (expression, 0)));
18218 case tcc_comparison:
18220 return ((value_dependent_expression_p
18221 (TREE_OPERAND (expression, 0)))
18222 || (value_dependent_expression_p
18223 (TREE_OPERAND (expression, 1))));
18225 case tcc_expression:
18229 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
18230 /* In some cases, some of the operands may be missing.
18231 (For example, in the case of PREDECREMENT_EXPR, the
18232 amount to increment by may be missing.) That doesn't
18233 make the expression dependent. */
18234 if (TREE_OPERAND (expression, i)
18235 && (value_dependent_expression_p
18236 (TREE_OPERAND (expression, i))))
18246 /* The expression is not value-dependent. */
18250 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
18251 [temp.dep.expr]. Note that an expression with no type is
18252 considered dependent. Other parts of the compiler arrange for an
18253 expression with type-dependent subexpressions to have no type, so
18254 this function doesn't have to be fully recursive. */
18257 type_dependent_expression_p (tree expression)
18259 if (!processing_template_decl)
18262 if (expression == error_mark_node)
18265 /* An unresolved name is always dependent. */
18266 if (TREE_CODE (expression) == IDENTIFIER_NODE
18267 || TREE_CODE (expression) == USING_DECL)
18270 /* Some expression forms are never type-dependent. */
18271 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
18272 || TREE_CODE (expression) == SIZEOF_EXPR
18273 || TREE_CODE (expression) == ALIGNOF_EXPR
18274 || TREE_CODE (expression) == AT_ENCODE_EXPR
18275 || TREE_CODE (expression) == NOEXCEPT_EXPR
18276 || TREE_CODE (expression) == TRAIT_EXPR
18277 || TREE_CODE (expression) == TYPEID_EXPR
18278 || TREE_CODE (expression) == DELETE_EXPR
18279 || TREE_CODE (expression) == VEC_DELETE_EXPR
18280 || TREE_CODE (expression) == THROW_EXPR)
18283 /* The types of these expressions depends only on the type to which
18284 the cast occurs. */
18285 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
18286 || TREE_CODE (expression) == STATIC_CAST_EXPR
18287 || TREE_CODE (expression) == CONST_CAST_EXPR
18288 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
18289 || TREE_CODE (expression) == CAST_EXPR)
18290 return dependent_type_p (TREE_TYPE (expression));
18292 /* The types of these expressions depends only on the type created
18293 by the expression. */
18294 if (TREE_CODE (expression) == NEW_EXPR
18295 || TREE_CODE (expression) == VEC_NEW_EXPR)
18297 /* For NEW_EXPR tree nodes created inside a template, either
18298 the object type itself or a TREE_LIST may appear as the
18300 tree type = TREE_OPERAND (expression, 1);
18301 if (TREE_CODE (type) == TREE_LIST)
18302 /* This is an array type. We need to check array dimensions
18304 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
18305 || value_dependent_expression_p
18306 (TREE_OPERAND (TREE_VALUE (type), 1));
18308 return dependent_type_p (type);
18311 if (TREE_CODE (expression) == SCOPE_REF)
18313 tree scope = TREE_OPERAND (expression, 0);
18314 tree name = TREE_OPERAND (expression, 1);
18316 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
18317 contains an identifier associated by name lookup with one or more
18318 declarations declared with a dependent type, or...a
18319 nested-name-specifier or qualified-id that names a member of an
18320 unknown specialization. */
18321 return (type_dependent_expression_p (name)
18322 || dependent_scope_p (scope));
18325 if (TREE_CODE (expression) == FUNCTION_DECL
18326 && DECL_LANG_SPECIFIC (expression)
18327 && DECL_TEMPLATE_INFO (expression)
18328 && (any_dependent_template_arguments_p
18329 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
18332 if (TREE_CODE (expression) == TEMPLATE_DECL
18333 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
18336 if (TREE_CODE (expression) == STMT_EXPR)
18337 expression = stmt_expr_value_expr (expression);
18339 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
18344 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
18346 if (type_dependent_expression_p (elt))
18352 /* A static data member of the current instantiation with incomplete
18353 array type is type-dependent, as the definition and specializations
18354 can have different bounds. */
18355 if (TREE_CODE (expression) == VAR_DECL
18356 && DECL_CLASS_SCOPE_P (expression)
18357 && dependent_type_p (DECL_CONTEXT (expression))
18358 && VAR_HAD_UNKNOWN_BOUND (expression))
18361 if (TREE_TYPE (expression) == unknown_type_node)
18363 if (TREE_CODE (expression) == ADDR_EXPR)
18364 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
18365 if (TREE_CODE (expression) == COMPONENT_REF
18366 || TREE_CODE (expression) == OFFSET_REF)
18368 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
18370 expression = TREE_OPERAND (expression, 1);
18371 if (TREE_CODE (expression) == IDENTIFIER_NODE)
18374 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
18375 if (TREE_CODE (expression) == SCOPE_REF)
18378 if (TREE_CODE (expression) == BASELINK)
18379 expression = BASELINK_FUNCTIONS (expression);
18381 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
18383 if (any_dependent_template_arguments_p
18384 (TREE_OPERAND (expression, 1)))
18386 expression = TREE_OPERAND (expression, 0);
18388 gcc_assert (TREE_CODE (expression) == OVERLOAD
18389 || TREE_CODE (expression) == FUNCTION_DECL);
18393 if (type_dependent_expression_p (OVL_CURRENT (expression)))
18395 expression = OVL_NEXT (expression);
18400 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
18402 return (dependent_type_p (TREE_TYPE (expression)));
18405 /* Like type_dependent_expression_p, but it also works while not processing
18406 a template definition, i.e. during substitution or mangling. */
18409 type_dependent_expression_p_push (tree expr)
18412 ++processing_template_decl;
18413 b = type_dependent_expression_p (expr);
18414 --processing_template_decl;
18418 /* Returns TRUE if ARGS contains a type-dependent expression. */
18421 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
18426 FOR_EACH_VEC_ELT (tree, args, i, arg)
18428 if (type_dependent_expression_p (arg))
18434 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
18435 expressions) contains any value-dependent expressions. */
18438 any_value_dependent_elements_p (const_tree list)
18440 for (; list; list = TREE_CHAIN (list))
18441 if (value_dependent_expression_p (TREE_VALUE (list)))
18447 /* Returns TRUE if the ARG (a template argument) is dependent. */
18450 dependent_template_arg_p (tree arg)
18452 if (!processing_template_decl)
18455 /* Assume a template argument that was wrongly written by the user
18456 is dependent. This is consistent with what
18457 any_dependent_template_arguments_p [that calls this function]
18459 if (arg == error_mark_node)
18462 if (TREE_CODE (arg) == TEMPLATE_DECL
18463 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
18464 return dependent_template_p (arg);
18465 else if (ARGUMENT_PACK_P (arg))
18467 tree args = ARGUMENT_PACK_ARGS (arg);
18468 int i, len = TREE_VEC_LENGTH (args);
18469 for (i = 0; i < len; ++i)
18471 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
18477 else if (TYPE_P (arg))
18478 return dependent_type_p (arg);
18480 return (type_dependent_expression_p (arg)
18481 || value_dependent_expression_p (arg));
18484 /* Returns true if ARGS (a collection of template arguments) contains
18485 any types that require structural equality testing. */
18488 any_template_arguments_need_structural_equality_p (tree args)
18495 if (args == error_mark_node)
18498 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
18500 tree level = TMPL_ARGS_LEVEL (args, i + 1);
18501 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
18503 tree arg = TREE_VEC_ELT (level, j);
18504 tree packed_args = NULL_TREE;
18507 if (ARGUMENT_PACK_P (arg))
18509 /* Look inside the argument pack. */
18510 packed_args = ARGUMENT_PACK_ARGS (arg);
18511 len = TREE_VEC_LENGTH (packed_args);
18514 for (k = 0; k < len; ++k)
18517 arg = TREE_VEC_ELT (packed_args, k);
18519 if (error_operand_p (arg))
18521 else if (TREE_CODE (arg) == TEMPLATE_DECL
18522 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
18524 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
18526 else if (!TYPE_P (arg) && TREE_TYPE (arg)
18527 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
18536 /* Returns true if ARGS (a collection of template arguments) contains
18537 any dependent arguments. */
18540 any_dependent_template_arguments_p (const_tree args)
18547 if (args == error_mark_node)
18550 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
18552 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
18553 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
18554 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
18561 /* Returns TRUE if the template TMPL is dependent. */
18564 dependent_template_p (tree tmpl)
18566 if (TREE_CODE (tmpl) == OVERLOAD)
18570 if (dependent_template_p (OVL_FUNCTION (tmpl)))
18572 tmpl = OVL_CHAIN (tmpl);
18577 /* Template template parameters are dependent. */
18578 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
18579 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
18581 /* So are names that have not been looked up. */
18582 if (TREE_CODE (tmpl) == SCOPE_REF
18583 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
18585 /* So are member templates of dependent classes. */
18586 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
18587 return dependent_type_p (DECL_CONTEXT (tmpl));
18591 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
18594 dependent_template_id_p (tree tmpl, tree args)
18596 return (dependent_template_p (tmpl)
18597 || any_dependent_template_arguments_p (args));
18600 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
18604 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
18608 if (!processing_template_decl)
18611 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
18613 tree decl = TREE_VEC_ELT (declv, i);
18614 tree init = TREE_VEC_ELT (initv, i);
18615 tree cond = TREE_VEC_ELT (condv, i);
18616 tree incr = TREE_VEC_ELT (incrv, i);
18618 if (type_dependent_expression_p (decl))
18621 if (init && type_dependent_expression_p (init))
18624 if (type_dependent_expression_p (cond))
18627 if (COMPARISON_CLASS_P (cond)
18628 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
18629 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
18632 if (TREE_CODE (incr) == MODOP_EXPR)
18634 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
18635 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
18638 else if (type_dependent_expression_p (incr))
18640 else if (TREE_CODE (incr) == MODIFY_EXPR)
18642 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
18644 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
18646 tree t = TREE_OPERAND (incr, 1);
18647 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
18648 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
18657 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
18658 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
18659 no such TYPE can be found. Note that this function peers inside
18660 uninstantiated templates and therefore should be used only in
18661 extremely limited situations. ONLY_CURRENT_P restricts this
18662 peering to the currently open classes hierarchy (which is required
18663 when comparing types). */
18666 resolve_typename_type (tree type, bool only_current_p)
18675 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
18677 scope = TYPE_CONTEXT (type);
18678 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
18679 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
18680 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
18681 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
18682 identifier of the TYPENAME_TYPE anymore.
18683 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
18684 TYPENAME_TYPE instead, we avoid messing up with a possible
18685 typedef variant case. */
18686 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
18688 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
18689 it first before we can figure out what NAME refers to. */
18690 if (TREE_CODE (scope) == TYPENAME_TYPE)
18691 scope = resolve_typename_type (scope, only_current_p);
18692 /* If we don't know what SCOPE refers to, then we cannot resolve the
18694 if (TREE_CODE (scope) == TYPENAME_TYPE)
18696 /* If the SCOPE is a template type parameter, we have no way of
18697 resolving the name. */
18698 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
18700 /* If the SCOPE is not the current instantiation, there's no reason
18701 to look inside it. */
18702 if (only_current_p && !currently_open_class (scope))
18704 /* If this is a typedef, we don't want to look inside (c++/11987). */
18705 if (typedef_variant_p (type))
18707 /* If SCOPE isn't the template itself, it will not have a valid
18708 TYPE_FIELDS list. */
18709 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
18710 /* scope is either the template itself or a compatible instantiation
18711 like X<T>, so look up the name in the original template. */
18712 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
18714 /* scope is a partial instantiation, so we can't do the lookup or we
18715 will lose the template arguments. */
18717 /* Enter the SCOPE so that name lookup will be resolved as if we
18718 were in the class definition. In particular, SCOPE will no
18719 longer be considered a dependent type. */
18720 pushed_scope = push_scope (scope);
18721 /* Look up the declaration. */
18722 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
18724 result = NULL_TREE;
18726 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
18727 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
18730 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
18731 && TREE_CODE (decl) == TYPE_DECL)
18733 result = TREE_TYPE (decl);
18734 if (result == error_mark_node)
18735 result = NULL_TREE;
18737 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
18738 && DECL_CLASS_TEMPLATE_P (decl))
18742 /* Obtain the template and the arguments. */
18743 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
18744 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
18745 /* Instantiate the template. */
18746 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
18747 /*entering_scope=*/0,
18748 tf_error | tf_user);
18749 if (result == error_mark_node)
18750 result = NULL_TREE;
18753 /* Leave the SCOPE. */
18755 pop_scope (pushed_scope);
18757 /* If we failed to resolve it, return the original typename. */
18761 /* If lookup found a typename type, resolve that too. */
18762 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
18764 /* Ill-formed programs can cause infinite recursion here, so we
18765 must catch that. */
18766 TYPENAME_IS_RESOLVING_P (type) = 1;
18767 result = resolve_typename_type (result, only_current_p);
18768 TYPENAME_IS_RESOLVING_P (type) = 0;
18771 /* Qualify the resulting type. */
18772 quals = cp_type_quals (type);
18774 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
18779 /* EXPR is an expression which is not type-dependent. Return a proxy
18780 for EXPR that can be used to compute the types of larger
18781 expressions containing EXPR. */
18784 build_non_dependent_expr (tree expr)
18788 /* Preserve null pointer constants so that the type of things like
18789 "p == 0" where "p" is a pointer can be determined. */
18790 if (null_ptr_cst_p (expr))
18792 /* Preserve OVERLOADs; the functions must be available to resolve
18795 if (TREE_CODE (inner_expr) == STMT_EXPR)
18796 inner_expr = stmt_expr_value_expr (inner_expr);
18797 if (TREE_CODE (inner_expr) == ADDR_EXPR)
18798 inner_expr = TREE_OPERAND (inner_expr, 0);
18799 if (TREE_CODE (inner_expr) == COMPONENT_REF)
18800 inner_expr = TREE_OPERAND (inner_expr, 1);
18801 if (is_overloaded_fn (inner_expr)
18802 || TREE_CODE (inner_expr) == OFFSET_REF)
18804 /* There is no need to return a proxy for a variable. */
18805 if (TREE_CODE (expr) == VAR_DECL)
18807 /* Preserve string constants; conversions from string constants to
18808 "char *" are allowed, even though normally a "const char *"
18809 cannot be used to initialize a "char *". */
18810 if (TREE_CODE (expr) == STRING_CST)
18812 /* Preserve arithmetic constants, as an optimization -- there is no
18813 reason to create a new node. */
18814 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
18816 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
18817 There is at least one place where we want to know that a
18818 particular expression is a throw-expression: when checking a ?:
18819 expression, there are special rules if the second or third
18820 argument is a throw-expression. */
18821 if (TREE_CODE (expr) == THROW_EXPR)
18824 if (TREE_CODE (expr) == COND_EXPR)
18825 return build3 (COND_EXPR,
18827 TREE_OPERAND (expr, 0),
18828 (TREE_OPERAND (expr, 1)
18829 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
18830 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
18831 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
18832 if (TREE_CODE (expr) == COMPOUND_EXPR
18833 && !COMPOUND_EXPR_OVERLOADED (expr))
18834 return build2 (COMPOUND_EXPR,
18836 TREE_OPERAND (expr, 0),
18837 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
18839 /* If the type is unknown, it can't really be non-dependent */
18840 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
18842 /* Otherwise, build a NON_DEPENDENT_EXPR.
18844 REFERENCE_TYPEs are not stripped for expressions in templates
18845 because doing so would play havoc with mangling. Consider, for
18848 template <typename T> void f<T& g>() { g(); }
18850 In the body of "f", the expression for "g" will have
18851 REFERENCE_TYPE, even though the standard says that it should
18852 not. The reason is that we must preserve the syntactic form of
18853 the expression so that mangling (say) "f<g>" inside the body of
18854 "f" works out correctly. Therefore, the REFERENCE_TYPE is
18856 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
18859 /* ARGS is a vector of expressions as arguments to a function call.
18860 Replace the arguments with equivalent non-dependent expressions.
18861 This modifies ARGS in place. */
18864 make_args_non_dependent (VEC(tree,gc) *args)
18869 FOR_EACH_VEC_ELT (tree, args, ix, arg)
18871 tree newarg = build_non_dependent_expr (arg);
18873 VEC_replace (tree, args, ix, newarg);
18877 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
18878 with a level one deeper than the actual template parms. */
18883 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
18884 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
18885 TYPE_DECL, get_identifier ("auto"), au);
18886 TYPE_STUB_DECL (au) = TYPE_NAME (au);
18887 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
18888 (0, processing_template_decl + 1, processing_template_decl + 1,
18889 0, TYPE_NAME (au), NULL_TREE);
18890 TYPE_CANONICAL (au) = canonical_type_parameter (au);
18891 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
18892 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
18897 /* Given type ARG, return std::initializer_list<ARG>. */
18902 tree std_init_list = namespace_binding
18903 (get_identifier ("initializer_list"), std_node);
18905 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
18907 error ("deducing from brace-enclosed initializer list requires "
18908 "#include <initializer_list>");
18909 return error_mark_node;
18911 argvec = make_tree_vec (1);
18912 TREE_VEC_ELT (argvec, 0) = arg;
18913 return lookup_template_class (std_init_list, argvec, NULL_TREE,
18914 NULL_TREE, 0, tf_warning_or_error);
18917 /* Replace auto in TYPE with std::initializer_list<auto>. */
18920 listify_autos (tree type, tree auto_node)
18922 tree init_auto = listify (auto_node);
18923 tree argvec = make_tree_vec (1);
18924 TREE_VEC_ELT (argvec, 0) = init_auto;
18925 if (processing_template_decl)
18926 argvec = add_to_template_args (current_template_args (), argvec);
18927 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18930 /* walk_tree helper for do_auto_deduction. */
18933 contains_auto_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
18936 /* Is this a variable with the type we're looking for? */
18938 && TREE_TYPE (*tp) == type)
18944 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
18945 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
18948 do_auto_deduction (tree type, tree init, tree auto_node)
18950 tree parms, tparms, targs;
18955 /* The name of the object being declared shall not appear in the
18956 initializer expression. */
18957 decl = cp_walk_tree_without_duplicates (&init, contains_auto_r, type);
18960 error ("variable %q#D with %<auto%> type used in its own "
18961 "initializer", decl);
18962 return error_mark_node;
18965 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
18966 with either a new invented type template parameter U or, if the
18967 initializer is a braced-init-list (8.5.4), with
18968 std::initializer_list<U>. */
18969 if (BRACE_ENCLOSED_INITIALIZER_P (init))
18970 type = listify_autos (type, auto_node);
18972 init = resolve_nondeduced_context (init);
18974 parms = build_tree_list (NULL_TREE, type);
18976 tparms = make_tree_vec (1);
18977 targs = make_tree_vec (1);
18978 TREE_VEC_ELT (tparms, 0)
18979 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
18980 val = type_unification_real (tparms, targs, parms, args, 1, 0,
18981 DEDUCE_CALL, LOOKUP_NORMAL);
18984 if (type && type != error_mark_node)
18985 /* If type is error_mark_node a diagnostic must have been
18986 emitted by now. Also, having a mention to '<type error>'
18987 in the diagnostic is not really useful to the user. */
18988 error ("unable to deduce %qT from %qE", type, init);
18989 return error_mark_node;
18992 /* If the list of declarators contains more than one declarator, the type
18993 of each declared variable is determined as described above. If the
18994 type deduced for the template parameter U is not the same in each
18995 deduction, the program is ill-formed. */
18996 if (TREE_TYPE (auto_node)
18997 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
18999 error ("inconsistent deduction for %qT: %qT and then %qT",
19000 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
19001 return error_mark_node;
19003 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
19005 if (processing_template_decl)
19006 targs = add_to_template_args (current_template_args (), targs);
19007 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
19010 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
19014 splice_late_return_type (tree type, tree late_return_type)
19018 if (late_return_type == NULL_TREE)
19020 argvec = make_tree_vec (1);
19021 TREE_VEC_ELT (argvec, 0) = late_return_type;
19022 if (processing_template_decl)
19023 argvec = add_to_template_args (current_template_args (), argvec);
19024 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
19027 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
19030 is_auto (const_tree type)
19032 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19033 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
19039 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
19040 appear as a type-specifier for the declaration in question, we don't
19041 have to look through the whole type. */
19044 type_uses_auto (tree type)
19046 enum tree_code code;
19047 if (is_auto (type))
19050 code = TREE_CODE (type);
19052 if (code == POINTER_TYPE || code == REFERENCE_TYPE
19053 || code == OFFSET_TYPE || code == FUNCTION_TYPE
19054 || code == METHOD_TYPE || code == ARRAY_TYPE)
19055 return type_uses_auto (TREE_TYPE (type));
19057 if (TYPE_PTRMEMFUNC_P (type))
19058 return type_uses_auto (TREE_TYPE (TREE_TYPE
19059 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
19064 /* For a given template T, return the vector of typedefs referenced
19065 in T for which access check is needed at T instantiation time.
19066 T is either a FUNCTION_DECL or a RECORD_TYPE.
19067 Those typedefs were added to T by the function
19068 append_type_to_template_for_access_check. */
19070 VEC(qualified_typedef_usage_t,gc)*
19071 get_types_needing_access_check (tree t)
19074 VEC(qualified_typedef_usage_t,gc) *result = NULL;
19076 if (!t || t == error_mark_node)
19079 if (!(ti = get_template_info (t)))
19082 if (CLASS_TYPE_P (t)
19083 || TREE_CODE (t) == FUNCTION_DECL)
19085 if (!TI_TEMPLATE (ti))
19088 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
19094 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
19095 tied to T. That list of typedefs will be access checked at
19096 T instantiation time.
19097 T is either a FUNCTION_DECL or a RECORD_TYPE.
19098 TYPE_DECL is a TYPE_DECL node representing a typedef.
19099 SCOPE is the scope through which TYPE_DECL is accessed.
19100 LOCATION is the location of the usage point of TYPE_DECL.
19102 This function is a subroutine of
19103 append_type_to_template_for_access_check. */
19106 append_type_to_template_for_access_check_1 (tree t,
19109 location_t location)
19111 qualified_typedef_usage_t typedef_usage;
19114 if (!t || t == error_mark_node)
19117 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
19118 || CLASS_TYPE_P (t))
19120 && TREE_CODE (type_decl) == TYPE_DECL
19123 if (!(ti = get_template_info (t)))
19126 gcc_assert (TI_TEMPLATE (ti));
19128 typedef_usage.typedef_decl = type_decl;
19129 typedef_usage.context = scope;
19130 typedef_usage.locus = location;
19132 VEC_safe_push (qualified_typedef_usage_t, gc,
19133 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
19137 /* Append TYPE_DECL to the template TEMPL.
19138 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
19139 At TEMPL instanciation time, TYPE_DECL will be checked to see
19140 if it can be accessed through SCOPE.
19141 LOCATION is the location of the usage point of TYPE_DECL.
19143 e.g. consider the following code snippet:
19150 template<class U> struct S
19152 C::myint mi; // <-- usage point of the typedef C::myint
19157 At S<char> instantiation time, we need to check the access of C::myint
19158 In other words, we need to check the access of the myint typedef through
19159 the C scope. For that purpose, this function will add the myint typedef
19160 and the scope C through which its being accessed to a list of typedefs
19161 tied to the template S. That list will be walked at template instantiation
19162 time and access check performed on each typedefs it contains.
19163 Note that this particular code snippet should yield an error because
19164 myint is private to C. */
19167 append_type_to_template_for_access_check (tree templ,
19170 location_t location)
19172 qualified_typedef_usage_t *iter;
19175 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
19177 /* Make sure we don't append the type to the template twice. */
19178 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
19179 get_types_needing_access_check (templ),
19181 if (iter->typedef_decl == type_decl && scope == iter->context)
19184 append_type_to_template_for_access_check_1 (templ, type_decl,
19188 /* Set up the hash tables for template instantiations. */
19191 init_template_processing (void)
19193 decl_specializations = htab_create_ggc (37,
19194 hash_specialization,
19195 eq_specializations,
19197 type_specializations = htab_create_ggc (37,
19198 hash_specialization,
19199 eq_specializations,
19203 /* Print stats about the template hash tables for -fstats. */
19206 print_template_statistics (void)
19208 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
19209 "%f collisions\n", (long) htab_size (decl_specializations),
19210 (long) htab_elements (decl_specializations),
19211 htab_collisions (decl_specializations));
19212 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
19213 "%f collisions\n", (long) htab_size (type_specializations),
19214 (long) htab_elements (type_specializations),
19215 htab_collisions (type_specializations));
19218 #include "gt-cp-pt.h"