1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
31 #include "coretypes.h"
36 #include "pointer-set.h"
40 #include "cp-objcp-common.h"
41 #include "tree-inline.h"
48 #include "tree-iterator.h"
51 /* The type of functions taking a tree, and some additional data, and
53 typedef int (*tree_fn_t) (tree, void*);
55 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
56 instantiations have been deferred, either because their definitions
57 were not yet available, or because we were putting off doing the work. */
58 struct GTY (()) pending_template {
59 struct pending_template *next;
60 struct tinst_level *tinst;
63 static GTY(()) struct pending_template *pending_templates;
64 static GTY(()) struct pending_template *last_pending_template;
66 int processing_template_parmlist;
67 static int template_header_count;
69 static GTY(()) tree saved_trees;
70 static VEC(int,heap) *inline_parm_levels;
72 static GTY(()) struct tinst_level *current_tinst_level;
74 static GTY(()) tree saved_access_scope;
76 /* Live only within one (recursive) call to tsubst_expr. We use
77 this to pass the statement expression node from the STMT_EXPR
78 to the EXPR_STMT that is its result. */
79 static tree cur_stmt_expr;
81 /* A map from local variable declarations in the body of the template
82 presently being instantiated to the corresponding instantiated
84 static htab_t local_specializations;
86 typedef struct GTY(()) spec_entry
93 static GTY ((param_is (spec_entry)))
94 htab_t decl_specializations;
96 static GTY ((param_is (spec_entry)))
97 htab_t type_specializations;
99 /* Contains canonical template parameter types. The vector is indexed by
100 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
101 TREE_LIST, whose TREE_VALUEs contain the canonical template
102 parameters of various types and levels. */
103 static GTY(()) VEC(tree,gc) *canonical_template_parms;
105 #define UNIFY_ALLOW_NONE 0
106 #define UNIFY_ALLOW_MORE_CV_QUAL 1
107 #define UNIFY_ALLOW_LESS_CV_QUAL 2
108 #define UNIFY_ALLOW_DERIVED 4
109 #define UNIFY_ALLOW_INTEGER 8
110 #define UNIFY_ALLOW_OUTER_LEVEL 16
111 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
112 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
114 static void push_access_scope (tree);
115 static void pop_access_scope (tree);
116 static bool resolve_overloaded_unification (tree, tree, tree, tree,
117 unification_kind_t, int);
118 static int try_one_overload (tree, tree, tree, tree, tree,
119 unification_kind_t, int, bool);
120 static int unify (tree, tree, tree, tree, int);
121 static void add_pending_template (tree);
122 static int push_tinst_level (tree);
123 static void pop_tinst_level (void);
124 static tree reopen_tinst_level (struct tinst_level *);
125 static tree tsubst_initializer_list (tree, tree);
126 static tree get_class_bindings (tree, tree, tree);
127 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
129 static void tsubst_enum (tree, tree, tree);
130 static tree add_to_template_args (tree, tree);
131 static tree add_outermost_template_args (tree, tree);
132 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
133 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
135 static int type_unification_real (tree, tree, tree, const tree *,
136 unsigned int, int, unification_kind_t, int);
137 static void note_template_header (int);
138 static tree convert_nontype_argument_function (tree, tree);
139 static tree convert_nontype_argument (tree, tree);
140 static tree convert_template_argument (tree, tree, tree,
141 tsubst_flags_t, int, tree);
142 static int for_each_template_parm (tree, tree_fn_t, void*,
143 struct pointer_set_t*, bool);
144 static tree expand_template_argument_pack (tree);
145 static tree build_template_parm_index (int, int, int, tree, tree);
146 static bool inline_needs_template_parms (tree);
147 static void push_inline_template_parms_recursive (tree, int);
148 static tree retrieve_local_specialization (tree);
149 static void register_local_specialization (tree, tree);
150 static hashval_t hash_specialization (const void *p);
151 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
152 static int mark_template_parm (tree, void *);
153 static int template_parm_this_level_p (tree, void *);
154 static tree tsubst_friend_function (tree, tree);
155 static tree tsubst_friend_class (tree, tree);
156 static int can_complete_type_without_circularity (tree);
157 static tree get_bindings (tree, tree, tree, bool);
158 static int template_decl_level (tree);
159 static int check_cv_quals_for_unify (int, tree, tree);
160 static void template_parm_level_and_index (tree, int*, int*);
161 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
162 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
163 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
164 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
165 static void regenerate_decl_from_template (tree, tree);
166 static tree most_specialized_class (tree, tree);
167 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
168 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
169 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
170 static bool check_specialization_scope (void);
171 static tree process_partial_specialization (tree);
172 static void set_current_access_from_decl (tree);
173 static tree get_template_base (tree, tree, tree, tree);
174 static tree try_class_unification (tree, tree, tree, tree);
175 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
177 static bool template_template_parm_bindings_ok_p (tree, tree);
178 static int template_args_equal (tree, tree);
179 static void tsubst_default_arguments (tree);
180 static tree for_each_template_parm_r (tree *, int *, void *);
181 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
182 static void copy_default_args_to_explicit_spec (tree);
183 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
184 static int eq_local_specializations (const void *, const void *);
185 static bool dependent_template_arg_p (tree);
186 static bool any_template_arguments_need_structural_equality_p (tree);
187 static bool dependent_type_p_r (tree);
188 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
189 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
190 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
191 static tree tsubst_decl (tree, tree, tsubst_flags_t);
192 static void perform_typedefs_access_check (tree tmpl, tree targs);
193 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
195 static hashval_t iterative_hash_template_arg (tree arg, hashval_t val);
196 static tree listify (tree);
197 static tree listify_autos (tree, tree);
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) : 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 extra_depth = TMPL_ARGS_DEPTH (extra_args);
499 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
501 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
502 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
504 for (j = 1; j <= extra_depth; ++j, ++i)
505 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
510 /* Like add_to_template_args, but only the outermost ARGS are added to
511 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
512 (EXTRA_ARGS) levels are added. This function is used to combine
513 the template arguments from a partial instantiation with the
514 template arguments used to attain the full instantiation from the
515 partial instantiation. */
518 add_outermost_template_args (tree args, tree extra_args)
522 /* If there are more levels of EXTRA_ARGS than there are ARGS,
523 something very fishy is going on. */
524 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
526 /* If *all* the new arguments will be the EXTRA_ARGS, just return
528 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
531 /* For the moment, we make ARGS look like it contains fewer levels. */
532 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
534 new_args = add_to_template_args (args, extra_args);
536 /* Now, we restore ARGS to its full dimensions. */
537 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
542 /* Return the N levels of innermost template arguments from the ARGS. */
545 get_innermost_template_args (tree args, int n)
553 /* If N is 1, just return the innermost set of template arguments. */
555 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
557 /* If we're not removing anything, just return the arguments we were
559 extra_levels = TMPL_ARGS_DEPTH (args) - n;
560 gcc_assert (extra_levels >= 0);
561 if (extra_levels == 0)
564 /* Make a new set of arguments, not containing the outer arguments. */
565 new_args = make_tree_vec (n);
566 for (i = 1; i <= n; ++i)
567 SET_TMPL_ARGS_LEVEL (new_args, i,
568 TMPL_ARGS_LEVEL (args, i + extra_levels));
573 /* The inverse of get_innermost_template_args: Return all but the innermost
574 EXTRA_LEVELS levels of template arguments from the ARGS. */
577 strip_innermost_template_args (tree args, int extra_levels)
580 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
585 /* If N is 1, just return the outermost set of template arguments. */
587 return TMPL_ARGS_LEVEL (args, 1);
589 /* If we're not removing anything, just return the arguments we were
591 gcc_assert (extra_levels >= 0);
592 if (extra_levels == 0)
595 /* Make a new set of arguments, not containing the inner arguments. */
596 new_args = make_tree_vec (n);
597 for (i = 1; i <= n; ++i)
598 SET_TMPL_ARGS_LEVEL (new_args, i,
599 TMPL_ARGS_LEVEL (args, i));
604 /* We've got a template header coming up; push to a new level for storing
608 begin_template_parm_list (void)
610 /* We use a non-tag-transparent scope here, which causes pushtag to
611 put tags in this scope, rather than in the enclosing class or
612 namespace scope. This is the right thing, since we want
613 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
614 global template class, push_template_decl handles putting the
615 TEMPLATE_DECL into top-level scope. For a nested template class,
618 template <class T> struct S1 {
619 template <class T> struct S2 {};
622 pushtag contains special code to call pushdecl_with_scope on the
623 TEMPLATE_DECL for S2. */
624 begin_scope (sk_template_parms, NULL);
625 ++processing_template_decl;
626 ++processing_template_parmlist;
627 note_template_header (0);
630 /* This routine is called when a specialization is declared. If it is
631 invalid to declare a specialization here, an error is reported and
632 false is returned, otherwise this routine will return true. */
635 check_specialization_scope (void)
637 tree scope = current_scope ();
641 An explicit specialization shall be declared in the namespace of
642 which the template is a member, or, for member templates, in the
643 namespace of which the enclosing class or enclosing class
644 template is a member. An explicit specialization of a member
645 function, member class or static data member of a class template
646 shall be declared in the namespace of which the class template
648 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
650 error ("explicit specialization in non-namespace scope %qD", scope);
656 In an explicit specialization declaration for a member of a class
657 template or a member template that appears in namespace scope,
658 the member template and some of its enclosing class templates may
659 remain unspecialized, except that the declaration shall not
660 explicitly specialize a class member template if its enclosing
661 class templates are not explicitly specialized as well. */
662 if (current_template_parms)
664 error ("enclosing class templates are not explicitly specialized");
671 /* We've just seen template <>. */
674 begin_specialization (void)
676 begin_scope (sk_template_spec, NULL);
677 note_template_header (1);
678 return check_specialization_scope ();
681 /* Called at then end of processing a declaration preceded by
685 end_specialization (void)
688 reset_specialization ();
691 /* Any template <>'s that we have seen thus far are not referring to a
692 function specialization. */
695 reset_specialization (void)
697 processing_specialization = 0;
698 template_header_count = 0;
701 /* We've just seen a template header. If SPECIALIZATION is nonzero,
702 it was of the form template <>. */
705 note_template_header (int specialization)
707 processing_specialization = specialization;
708 template_header_count++;
711 /* We're beginning an explicit instantiation. */
714 begin_explicit_instantiation (void)
716 gcc_assert (!processing_explicit_instantiation);
717 processing_explicit_instantiation = true;
722 end_explicit_instantiation (void)
724 gcc_assert (processing_explicit_instantiation);
725 processing_explicit_instantiation = false;
728 /* An explicit specialization or partial specialization TMPL is being
729 declared. Check that the namespace in which the specialization is
730 occurring is permissible. Returns false iff it is invalid to
731 specialize TMPL in the current namespace. */
734 check_specialization_namespace (tree tmpl)
736 tree tpl_ns = decl_namespace_context (tmpl);
740 An explicit specialization shall be declared in the namespace of
741 which the template is a member, or, for member templates, in the
742 namespace of which the enclosing class or enclosing class
743 template is a member. An explicit specialization of a member
744 function, member class or static data member of a class template
745 shall be declared in the namespace of which the class template is
747 if (current_scope() != DECL_CONTEXT (tmpl)
748 && !at_namespace_scope_p ())
750 error ("specialization of %qD must appear at namespace scope", tmpl);
753 if (is_associated_namespace (current_namespace, tpl_ns))
754 /* Same or super-using namespace. */
758 permerror (input_location, "specialization of %qD in different namespace", tmpl);
759 permerror (input_location, " from definition of %q+#D", tmpl);
764 /* SPEC is an explicit instantiation. Check that it is valid to
765 perform this explicit instantiation in the current namespace. */
768 check_explicit_instantiation_namespace (tree spec)
772 /* DR 275: An explicit instantiation shall appear in an enclosing
773 namespace of its template. */
774 ns = decl_namespace_context (spec);
775 if (!is_ancestor (current_namespace, ns))
776 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
777 "(which does not enclose namespace %qD)",
778 spec, current_namespace, ns);
781 /* The TYPE is being declared. If it is a template type, that means it
782 is a partial specialization. Do appropriate error-checking. */
785 maybe_process_partial_specialization (tree type)
789 if (type == error_mark_node)
790 return error_mark_node;
792 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
794 error ("name of class shadows template template parameter %qD",
796 return error_mark_node;
799 context = TYPE_CONTEXT (type);
801 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
803 /* This is for ordinary explicit specialization and partial
804 specialization of a template class such as:
806 template <> class C<int>;
810 template <class T> class C<T*>;
812 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
814 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
815 && !COMPLETE_TYPE_P (type))
817 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
818 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
819 if (processing_template_decl)
821 if (push_template_decl (TYPE_MAIN_DECL (type))
823 return error_mark_node;
826 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
827 error ("specialization of %qT after instantiation", type);
829 else if (CLASS_TYPE_P (type)
830 && !CLASSTYPE_USE_TEMPLATE (type)
831 && CLASSTYPE_TEMPLATE_INFO (type)
832 && context && CLASS_TYPE_P (context)
833 && CLASSTYPE_TEMPLATE_INFO (context))
835 /* This is for an explicit specialization of member class
836 template according to [temp.expl.spec/18]:
838 template <> template <class U> class C<int>::D;
840 The context `C<int>' must be an implicit instantiation.
841 Otherwise this is just a member class template declared
844 template <> class C<int> { template <class U> class D; };
845 template <> template <class U> class C<int>::D;
847 In the first case, `C<int>::D' is a specialization of `C<T>::D'
848 while in the second case, `C<int>::D' is a primary template
849 and `C<T>::D' may not exist. */
851 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
852 && !COMPLETE_TYPE_P (type))
855 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
857 if (current_namespace
858 != decl_namespace_context (tmpl))
860 permerror (input_location, "specializing %q#T in different namespace", type);
861 permerror (input_location, " from definition of %q+#D", tmpl);
864 /* Check for invalid specialization after instantiation:
866 template <> template <> class C<int>::D<int>;
867 template <> template <class U> class C<int>::D; */
869 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
870 t; t = TREE_CHAIN (t))
872 tree inst = TREE_VALUE (t);
873 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
875 /* We already have a full specialization of this partial
876 instantiation. Reassign it to the new member
877 specialization template. */
881 elt.tmpl = most_general_template (tmpl);
882 elt.args = CLASSTYPE_TI_ARGS (inst);
885 htab_remove_elt (type_specializations, &elt);
888 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
890 slot = (spec_entry **)
891 htab_find_slot (type_specializations, &elt, INSERT);
892 *slot = GGC_NEW (spec_entry);
895 else if (COMPLETE_TYPE_P (inst) || TYPE_BEING_DEFINED (inst))
896 /* But if we've had an implicit instantiation, that's a
897 problem ([temp.expl.spec]/6). */
898 error ("specialization %qT after instantiation %qT",
902 /* Mark TYPE as a specialization. And as a result, we only
903 have one level of template argument for the innermost
905 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
906 CLASSTYPE_TI_ARGS (type)
907 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
910 else if (processing_specialization)
912 error ("explicit specialization of non-template %qT", type);
913 return error_mark_node;
919 /* Returns nonzero if we can optimize the retrieval of specializations
920 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
921 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
924 optimize_specialization_lookup_p (tree tmpl)
926 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
927 && DECL_CLASS_SCOPE_P (tmpl)
928 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
930 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
931 /* The optimized lookup depends on the fact that the
932 template arguments for the member function template apply
933 purely to the containing class, which is not true if the
934 containing class is an explicit or partial
936 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
937 && !DECL_MEMBER_TEMPLATE_P (tmpl)
938 && !DECL_CONV_FN_P (tmpl)
939 /* It is possible to have a template that is not a member
940 template and is not a member of a template class:
942 template <typename T>
943 struct S { friend A::f(); };
945 Here, the friend function is a template, but the context does
946 not have template information. The optimized lookup relies
947 on having ARGS be the template arguments for both the class
948 and the function template. */
949 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
952 /* Retrieve the specialization (in the sense of [temp.spec] - a
953 specialization is either an instantiation or an explicit
954 specialization) of TMPL for the given template ARGS. If there is
955 no such specialization, return NULL_TREE. The ARGS are a vector of
956 arguments, or a vector of vectors of arguments, in the case of
957 templates with more than one level of parameters.
959 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
960 then we search for a partial specialization matching ARGS. This
961 parameter is ignored if TMPL is not a class template. */
964 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
966 if (args == error_mark_node)
969 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
971 /* There should be as many levels of arguments as there are
972 levels of parameters. */
973 gcc_assert (TMPL_ARGS_DEPTH (args)
974 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
976 if (optimize_specialization_lookup_p (tmpl))
979 tree class_specialization;
980 VEC(tree,gc) *methods;
984 /* The template arguments actually apply to the containing
985 class. Find the class specialization with those
987 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
989 = retrieve_specialization (class_template, args, 0);
990 if (!class_specialization)
992 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
993 for the specialization. */
994 idx = class_method_index_for_fn (class_specialization, tmpl);
997 /* Iterate through the methods with the indicated name, looking
998 for the one that has an instance of TMPL. */
999 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1000 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
1002 tree fn = OVL_CURRENT (fns);
1003 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1004 /* using-declarations can add base methods to the method vec,
1005 and we don't want those here. */
1006 && DECL_CONTEXT (fn) == class_specialization)
1015 htab_t specializations;
1019 elt.spec = NULL_TREE;
1021 if (DECL_CLASS_TEMPLATE_P (tmpl))
1022 specializations = type_specializations;
1024 specializations = decl_specializations;
1027 hash = hash_specialization (&elt);
1028 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1036 /* Like retrieve_specialization, but for local declarations. */
1039 retrieve_local_specialization (tree tmpl)
1043 if (local_specializations == NULL)
1046 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
1047 htab_hash_pointer (tmpl));
1048 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
1051 /* Returns nonzero iff DECL is a specialization of TMPL. */
1054 is_specialization_of (tree decl, tree tmpl)
1058 if (TREE_CODE (decl) == FUNCTION_DECL)
1062 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1068 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1070 for (t = TREE_TYPE (decl);
1072 t = CLASSTYPE_USE_TEMPLATE (t)
1073 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1074 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1081 /* Returns nonzero iff DECL is a specialization of friend declaration
1082 FRIEND_DECL according to [temp.friend]. */
1085 is_specialization_of_friend (tree decl, tree friend_decl)
1087 bool need_template = true;
1090 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1091 || TREE_CODE (decl) == TYPE_DECL);
1093 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1094 of a template class, we want to check if DECL is a specialization
1096 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1097 && DECL_TEMPLATE_INFO (friend_decl)
1098 && !DECL_USE_TEMPLATE (friend_decl))
1100 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1101 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1102 need_template = false;
1104 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1105 && !PRIMARY_TEMPLATE_P (friend_decl))
1106 need_template = false;
1108 /* There is nothing to do if this is not a template friend. */
1109 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1112 if (is_specialization_of (decl, friend_decl))
1116 A member of a class template may be declared to be a friend of a
1117 non-template class. In this case, the corresponding member of
1118 every specialization of the class template is a friend of the
1119 class granting friendship.
1121 For example, given a template friend declaration
1123 template <class T> friend void A<T>::f();
1125 the member function below is considered a friend
1127 template <> struct A<int> {
1131 For this type of template friend, TEMPLATE_DEPTH below will be
1132 nonzero. To determine if DECL is a friend of FRIEND, we first
1133 check if the enclosing class is a specialization of another. */
1135 template_depth = template_class_depth (DECL_CONTEXT (friend_decl));
1137 && DECL_CLASS_SCOPE_P (decl)
1138 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1139 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1141 /* Next, we check the members themselves. In order to handle
1142 a few tricky cases, such as when FRIEND_DECL's are
1144 template <class T> friend void A<T>::g(T t);
1145 template <class T> template <T t> friend void A<T>::h();
1149 void A<int>::g(int);
1150 template <int> void A<int>::h();
1152 we need to figure out ARGS, the template arguments from
1153 the context of DECL. This is required for template substitution
1154 of `T' in the function parameter of `g' and template parameter
1155 of `h' in the above examples. Here ARGS corresponds to `int'. */
1157 tree context = DECL_CONTEXT (decl);
1158 tree args = NULL_TREE;
1159 int current_depth = 0;
1161 while (current_depth < template_depth)
1163 if (CLASSTYPE_TEMPLATE_INFO (context))
1165 if (current_depth == 0)
1166 args = TYPE_TI_ARGS (context);
1168 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1171 context = TYPE_CONTEXT (context);
1174 if (TREE_CODE (decl) == FUNCTION_DECL)
1179 tree friend_args_type;
1180 tree decl_args_type;
1182 /* Make sure that both DECL and FRIEND_DECL are templates or
1184 is_template = DECL_TEMPLATE_INFO (decl)
1185 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1186 if (need_template ^ is_template)
1188 else if (is_template)
1190 /* If both are templates, check template parameter list. */
1192 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1194 if (!comp_template_parms
1195 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1199 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1202 decl_type = TREE_TYPE (decl);
1204 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1205 tf_none, NULL_TREE);
1206 if (friend_type == error_mark_node)
1209 /* Check if return types match. */
1210 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1213 /* Check if function parameter types match, ignoring the
1214 `this' parameter. */
1215 friend_args_type = TYPE_ARG_TYPES (friend_type);
1216 decl_args_type = TYPE_ARG_TYPES (decl_type);
1217 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1218 friend_args_type = TREE_CHAIN (friend_args_type);
1219 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1220 decl_args_type = TREE_CHAIN (decl_args_type);
1222 return compparms (decl_args_type, friend_args_type);
1226 /* DECL is a TYPE_DECL */
1228 tree decl_type = TREE_TYPE (decl);
1230 /* Make sure that both DECL and FRIEND_DECL are templates or
1233 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1234 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1236 if (need_template ^ is_template)
1238 else if (is_template)
1241 /* If both are templates, check the name of the two
1242 TEMPLATE_DECL's first because is_friend didn't. */
1243 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1244 != DECL_NAME (friend_decl))
1247 /* Now check template parameter list. */
1249 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1251 return comp_template_parms
1252 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1256 return (DECL_NAME (decl)
1257 == DECL_NAME (friend_decl));
1263 /* Register the specialization SPEC as a specialization of TMPL with
1264 the indicated ARGS. IS_FRIEND indicates whether the specialization
1265 is actually just a friend declaration. Returns SPEC, or an
1266 equivalent prior declaration, if available. */
1269 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1273 spec_entry **slot = NULL;
1276 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1278 if (TREE_CODE (spec) == FUNCTION_DECL
1279 && uses_template_parms (DECL_TI_ARGS (spec)))
1280 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1281 register it; we want the corresponding TEMPLATE_DECL instead.
1282 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1283 the more obvious `uses_template_parms (spec)' to avoid problems
1284 with default function arguments. In particular, given
1285 something like this:
1287 template <class T> void f(T t1, T t = T())
1289 the default argument expression is not substituted for in an
1290 instantiation unless and until it is actually needed. */
1293 if (optimize_specialization_lookup_p (tmpl))
1294 /* We don't put these specializations in the hash table, but we might
1295 want to give an error about a mismatch. */
1296 fn = retrieve_specialization (tmpl, args, 0);
1304 hash = hash_specialization (&elt);
1306 slot = (spec_entry **)
1307 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1314 /* We can sometimes try to re-register a specialization that we've
1315 already got. In particular, regenerate_decl_from_template calls
1316 duplicate_decls which will update the specialization list. But,
1317 we'll still get called again here anyhow. It's more convenient
1318 to simply allow this than to try to prevent it. */
1321 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1323 if (DECL_TEMPLATE_INSTANTIATION (fn))
1325 if (DECL_ODR_USED (fn)
1326 || DECL_EXPLICIT_INSTANTIATION (fn))
1328 error ("specialization of %qD after instantiation",
1330 return error_mark_node;
1335 /* This situation should occur only if the first
1336 specialization is an implicit instantiation, the
1337 second is an explicit specialization, and the
1338 implicit instantiation has not yet been used. That
1339 situation can occur if we have implicitly
1340 instantiated a member function and then specialized
1343 We can also wind up here if a friend declaration that
1344 looked like an instantiation turns out to be a
1347 template <class T> void foo(T);
1348 class S { friend void foo<>(int) };
1349 template <> void foo(int);
1351 We transform the existing DECL in place so that any
1352 pointers to it become pointers to the updated
1355 If there was a definition for the template, but not
1356 for the specialization, we want this to look as if
1357 there were no definition, and vice versa. */
1358 DECL_INITIAL (fn) = NULL_TREE;
1359 duplicate_decls (spec, fn, is_friend);
1360 /* The call to duplicate_decls will have applied
1363 An explicit specialization of a function template
1364 is inline only if it is explicitly declared to be,
1365 and independently of whether its function template
1368 to the primary function; now copy the inline bits to
1369 the various clones. */
1370 FOR_EACH_CLONE (clone, fn)
1372 DECL_DECLARED_INLINE_P (clone)
1373 = DECL_DECLARED_INLINE_P (fn);
1374 DECL_SOURCE_LOCATION (clone)
1375 = DECL_SOURCE_LOCATION (fn);
1377 check_specialization_namespace (fn);
1382 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1384 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1385 /* Dup decl failed, but this is a new definition. Set the
1386 line number so any errors match this new
1388 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1394 return duplicate_decls (spec, fn, is_friend);
1396 /* A specialization must be declared in the same namespace as the
1397 template it is specializing. */
1398 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1399 && !check_specialization_namespace (tmpl))
1400 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1402 if (!optimize_specialization_lookup_p (tmpl))
1404 gcc_assert (tmpl && args && spec);
1405 *slot = GGC_NEW (spec_entry);
1407 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1408 && PRIMARY_TEMPLATE_P (tmpl)
1409 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1410 /* TMPL is a forward declaration of a template function; keep a list
1411 of all specializations in case we need to reassign them to a friend
1412 template later in tsubst_friend_function. */
1413 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1414 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1420 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1421 TMPL and ARGS members, ignores SPEC. */
1424 eq_specializations (const void *p1, const void *p2)
1426 const spec_entry *e1 = (const spec_entry *)p1;
1427 const spec_entry *e2 = (const spec_entry *)p2;
1429 return (e1->tmpl == e2->tmpl
1430 && comp_template_args (e1->args, e2->args));
1433 /* Returns a hash for a template TMPL and template arguments ARGS. */
1436 hash_tmpl_and_args (tree tmpl, tree args)
1438 hashval_t val = DECL_UID (tmpl);
1439 return iterative_hash_template_arg (args, val);
1442 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1446 hash_specialization (const void *p)
1448 const spec_entry *e = (const spec_entry *)p;
1449 return hash_tmpl_and_args (e->tmpl, e->args);
1452 /* Recursively calculate a hash value for a template argument ARG, for use
1453 in the hash tables of template specializations. */
1456 iterative_hash_template_arg (tree arg, hashval_t val)
1458 unsigned HOST_WIDE_INT i;
1459 enum tree_code code;
1462 if (arg == NULL_TREE)
1463 return iterative_hash_object (arg, val);
1468 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1469 /* We can get one of these when re-hashing a previous entry in the middle
1470 of substituting into a pack expansion. Just look through it. */
1471 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1473 code = TREE_CODE (arg);
1474 tclass = TREE_CODE_CLASS (code);
1476 val = iterative_hash_object (code, val);
1483 case IDENTIFIER_NODE:
1484 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1488 int i, len = TREE_VEC_LENGTH (arg);
1489 for (i = 0; i < len; ++i)
1490 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1494 case TYPE_PACK_EXPANSION:
1495 case EXPR_PACK_EXPANSION:
1496 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1498 case TYPE_ARGUMENT_PACK:
1499 case NONTYPE_ARGUMENT_PACK:
1500 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1503 for (; arg; arg = TREE_CHAIN (arg))
1504 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1508 for (; arg; arg = OVL_CHAIN (arg))
1509 val = iterative_hash_template_arg (OVL_FUNCTION (arg), val);
1515 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1517 val = iterative_hash_template_arg (field, val);
1518 val = iterative_hash_template_arg (value, val);
1524 if (!DECL_ARTIFICIAL (arg))
1525 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1526 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1529 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1532 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1533 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1535 case TEMPLATE_PARM_INDEX:
1536 val = iterative_hash_template_arg
1537 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1538 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1539 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1542 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1543 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1544 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1547 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1549 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1553 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1554 code = TREE_CODE (TREE_OPERAND (arg, 1));
1555 val = iterative_hash_object (code, val);
1556 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1559 /* layout_type sets structural equality for arrays of
1560 incomplete type, so we can't rely on the canonical type
1562 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1563 return iterative_hash_template_arg (TYPE_DOMAIN (arg), val);
1569 if (TYPE_CANONICAL (arg))
1570 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1572 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1573 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1574 /* Otherwise just compare the types during lookup. */
1577 case tcc_declaration:
1579 return iterative_hash_expr (arg, val);
1582 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1584 unsigned n = TREE_OPERAND_LENGTH (arg);
1585 for (i = 0; i < n; ++i)
1586 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1595 /* Unregister the specialization SPEC as a specialization of TMPL.
1596 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1597 if the SPEC was listed as a specialization of TMPL.
1599 Note that SPEC has been ggc_freed, so we can't look inside it. */
1602 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1607 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1608 elt.args = TI_ARGS (tinfo);
1609 elt.spec = NULL_TREE;
1611 slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
1614 gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
1615 gcc_assert (new_spec != NULL_TREE);
1616 (*slot)->spec = new_spec;
1623 /* Compare an entry in the local specializations hash table P1 (which
1624 is really a pointer to a TREE_LIST) with P2 (which is really a
1628 eq_local_specializations (const void *p1, const void *p2)
1630 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1633 /* Hash P1, an entry in the local specializations table. */
1636 hash_local_specialization (const void* p1)
1638 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1641 /* Like register_specialization, but for local declarations. We are
1642 registering SPEC, an instantiation of TMPL. */
1645 register_local_specialization (tree spec, tree tmpl)
1649 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1650 htab_hash_pointer (tmpl), INSERT);
1651 *slot = build_tree_list (spec, tmpl);
1654 /* TYPE is a class type. Returns true if TYPE is an explicitly
1655 specialized class. */
1658 explicit_class_specialization_p (tree type)
1660 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1662 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1665 /* Print the list of functions at FNS, going through all the overloads
1666 for each element of the list. Alternatively, FNS can not be a
1667 TREE_LIST, in which case it will be printed together with all the
1670 MORE and *STR should respectively be FALSE and NULL when the function
1671 is called from the outside. They are used internally on recursive
1672 calls. print_candidates manages the two parameters and leaves NULL
1673 in *STR when it ends. */
1676 print_candidates_1 (tree fns, bool more, const char **str)
1679 char *spaces = NULL;
1681 for (fn = fns; fn; fn = OVL_NEXT (fn))
1682 if (TREE_CODE (fn) == TREE_LIST)
1684 gcc_assert (!OVL_NEXT (fn) && !is_overloaded_fn (fn));
1685 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1686 print_candidates_1 (TREE_VALUE (fn2),
1687 TREE_CHAIN (fn2) || more, str);
1693 /* Pick the prefix string. */
1694 if (!more && !OVL_NEXT (fns))
1696 error ("candidate is: %+#D", OVL_CURRENT (fn));
1700 *str = _("candidates are:");
1701 spaces = get_spaces (*str);
1703 error ("%s %+#D", *str, OVL_CURRENT (fn));
1704 *str = spaces ? spaces : *str;
1714 /* Print the list of candidate FNS in an error message. */
1717 print_candidates (tree fns)
1719 const char *str = NULL;
1720 print_candidates_1 (fns, false, &str);
1721 gcc_assert (str == NULL);
1724 /* Returns the template (one of the functions given by TEMPLATE_ID)
1725 which can be specialized to match the indicated DECL with the
1726 explicit template args given in TEMPLATE_ID. The DECL may be
1727 NULL_TREE if none is available. In that case, the functions in
1728 TEMPLATE_ID are non-members.
1730 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1731 specialization of a member template.
1733 The TEMPLATE_COUNT is the number of references to qualifying
1734 template classes that appeared in the name of the function. See
1735 check_explicit_specialization for a more accurate description.
1737 TSK indicates what kind of template declaration (if any) is being
1738 declared. TSK_TEMPLATE indicates that the declaration given by
1739 DECL, though a FUNCTION_DECL, has template parameters, and is
1740 therefore a template function.
1742 The template args (those explicitly specified and those deduced)
1743 are output in a newly created vector *TARGS_OUT.
1745 If it is impossible to determine the result, an error message is
1746 issued. The error_mark_node is returned to indicate failure. */
1749 determine_specialization (tree template_id,
1752 int need_member_template,
1758 tree explicit_targs;
1759 tree candidates = NULL_TREE;
1760 /* A TREE_LIST of templates of which DECL may be a specialization.
1761 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1762 corresponding TREE_PURPOSE is the set of template arguments that,
1763 when used to instantiate the template, would produce a function
1764 with the signature of DECL. */
1765 tree templates = NULL_TREE;
1767 struct cp_binding_level *b;
1769 *targs_out = NULL_TREE;
1771 if (template_id == error_mark_node || decl == error_mark_node)
1772 return error_mark_node;
1774 fns = TREE_OPERAND (template_id, 0);
1775 explicit_targs = TREE_OPERAND (template_id, 1);
1777 if (fns == error_mark_node)
1778 return error_mark_node;
1780 /* Check for baselinks. */
1781 if (BASELINK_P (fns))
1782 fns = BASELINK_FUNCTIONS (fns);
1784 if (!is_overloaded_fn (fns))
1786 error ("%qD is not a function template", fns);
1787 return error_mark_node;
1790 /* Count the number of template headers specified for this
1793 for (b = current_binding_level;
1794 b->kind == sk_template_parms;
1798 for (; fns; fns = OVL_NEXT (fns))
1800 tree fn = OVL_CURRENT (fns);
1802 if (TREE_CODE (fn) == TEMPLATE_DECL)
1804 tree decl_arg_types;
1807 /* In case of explicit specialization, we need to check if
1808 the number of template headers appearing in the specialization
1809 is correct. This is usually done in check_explicit_specialization,
1810 but the check done there cannot be exhaustive when specializing
1811 member functions. Consider the following code:
1813 template <> void A<int>::f(int);
1814 template <> template <> void A<int>::f(int);
1816 Assuming that A<int> is not itself an explicit specialization
1817 already, the first line specializes "f" which is a non-template
1818 member function, whilst the second line specializes "f" which
1819 is a template member function. So both lines are syntactically
1820 correct, and check_explicit_specialization does not reject
1823 Here, we can do better, as we are matching the specialization
1824 against the declarations. We count the number of template
1825 headers, and we check if they match TEMPLATE_COUNT + 1
1826 (TEMPLATE_COUNT is the number of qualifying template classes,
1827 plus there must be another header for the member template
1830 Notice that if header_count is zero, this is not a
1831 specialization but rather a template instantiation, so there
1832 is no check we can perform here. */
1833 if (header_count && header_count != template_count + 1)
1836 /* Check that the number of template arguments at the
1837 innermost level for DECL is the same as for FN. */
1838 if (current_binding_level->kind == sk_template_parms
1839 && !current_binding_level->explicit_spec_p
1840 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1841 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1842 (current_template_parms))))
1845 /* DECL might be a specialization of FN. */
1846 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1847 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1849 /* For a non-static member function, we need to make sure
1850 that the const qualification is the same. Since
1851 get_bindings does not try to merge the "this" parameter,
1852 we must do the comparison explicitly. */
1853 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1854 && !same_type_p (TREE_VALUE (fn_arg_types),
1855 TREE_VALUE (decl_arg_types)))
1858 /* Skip the "this" parameter and, for constructors of
1859 classes with virtual bases, the VTT parameter. A
1860 full specialization of a constructor will have a VTT
1861 parameter, but a template never will. */
1863 = skip_artificial_parms_for (decl, decl_arg_types);
1865 = skip_artificial_parms_for (fn, fn_arg_types);
1867 /* Check that the number of function parameters matches.
1869 template <class T> void f(int i = 0);
1870 template <> void f<int>();
1871 The specialization f<int> is invalid but is not caught
1872 by get_bindings below. */
1873 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1876 /* Function templates cannot be specializations; there are
1877 no partial specializations of functions. Therefore, if
1878 the type of DECL does not match FN, there is no
1880 if (tsk == tsk_template)
1882 if (compparms (fn_arg_types, decl_arg_types))
1883 candidates = tree_cons (NULL_TREE, fn, candidates);
1887 /* See whether this function might be a specialization of this
1889 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1892 /* We cannot deduce template arguments that when used to
1893 specialize TMPL will produce DECL. */
1896 /* Save this template, and the arguments deduced. */
1897 templates = tree_cons (targs, fn, templates);
1899 else if (need_member_template)
1900 /* FN is an ordinary member function, and we need a
1901 specialization of a member template. */
1903 else if (TREE_CODE (fn) != FUNCTION_DECL)
1904 /* We can get IDENTIFIER_NODEs here in certain erroneous
1907 else if (!DECL_FUNCTION_MEMBER_P (fn))
1908 /* This is just an ordinary non-member function. Nothing can
1909 be a specialization of that. */
1911 else if (DECL_ARTIFICIAL (fn))
1912 /* Cannot specialize functions that are created implicitly. */
1916 tree decl_arg_types;
1918 /* This is an ordinary member function. However, since
1919 we're here, we can assume it's enclosing class is a
1920 template class. For example,
1922 template <typename T> struct S { void f(); };
1923 template <> void S<int>::f() {}
1925 Here, S<int>::f is a non-template, but S<int> is a
1926 template class. If FN has the same type as DECL, we
1927 might be in business. */
1929 if (!DECL_TEMPLATE_INFO (fn))
1930 /* Its enclosing class is an explicit specialization
1931 of a template class. This is not a candidate. */
1934 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1935 TREE_TYPE (TREE_TYPE (fn))))
1936 /* The return types differ. */
1939 /* Adjust the type of DECL in case FN is a static member. */
1940 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1941 if (DECL_STATIC_FUNCTION_P (fn)
1942 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1943 decl_arg_types = TREE_CHAIN (decl_arg_types);
1945 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1948 candidates = tree_cons (NULL_TREE, fn, candidates);
1952 if (templates && TREE_CHAIN (templates))
1958 It is possible for a specialization with a given function
1959 signature to be instantiated from more than one function
1960 template. In such cases, explicit specification of the
1961 template arguments must be used to uniquely identify the
1962 function template specialization being specialized.
1964 Note that here, there's no suggestion that we're supposed to
1965 determine which of the candidate templates is most
1966 specialized. However, we, also have:
1970 Partial ordering of overloaded function template
1971 declarations is used in the following contexts to select
1972 the function template to which a function template
1973 specialization refers:
1975 -- when an explicit specialization refers to a function
1978 So, we do use the partial ordering rules, at least for now.
1979 This extension can only serve to make invalid programs valid,
1980 so it's safe. And, there is strong anecdotal evidence that
1981 the committee intended the partial ordering rules to apply;
1982 the EDG front end has that behavior, and John Spicer claims
1983 that the committee simply forgot to delete the wording in
1984 [temp.expl.spec]. */
1985 tree tmpl = most_specialized_instantiation (templates);
1986 if (tmpl != error_mark_node)
1989 TREE_CHAIN (templates) = NULL_TREE;
1993 if (templates == NULL_TREE && candidates == NULL_TREE)
1995 error ("template-id %qD for %q+D does not match any template "
1996 "declaration", template_id, decl);
1997 if (header_count && header_count != template_count + 1)
1998 inform (input_location, "saw %d %<template<>%>, need %d for "
1999 "specializing a member function template",
2000 header_count, template_count + 1);
2001 return error_mark_node;
2003 else if ((templates && TREE_CHAIN (templates))
2004 || (candidates && TREE_CHAIN (candidates))
2005 || (templates && candidates))
2007 error ("ambiguous template specialization %qD for %q+D",
2009 candidates = chainon (candidates, templates);
2010 print_candidates (candidates);
2011 return error_mark_node;
2014 /* We have one, and exactly one, match. */
2017 tree fn = TREE_VALUE (candidates);
2018 *targs_out = copy_node (DECL_TI_ARGS (fn));
2019 /* DECL is a re-declaration or partial instantiation of a template
2021 if (TREE_CODE (fn) == TEMPLATE_DECL)
2023 /* It was a specialization of an ordinary member function in a
2025 return DECL_TI_TEMPLATE (fn);
2028 /* It was a specialization of a template. */
2029 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2030 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2032 *targs_out = copy_node (targs);
2033 SET_TMPL_ARGS_LEVEL (*targs_out,
2034 TMPL_ARGS_DEPTH (*targs_out),
2035 TREE_PURPOSE (templates));
2038 *targs_out = TREE_PURPOSE (templates);
2039 return TREE_VALUE (templates);
2042 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2043 but with the default argument values filled in from those in the
2047 copy_default_args_to_explicit_spec_1 (tree spec_types,
2050 tree new_spec_types;
2055 if (spec_types == void_list_node)
2056 return void_list_node;
2058 /* Substitute into the rest of the list. */
2060 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2061 TREE_CHAIN (tmpl_types));
2063 /* Add the default argument for this parameter. */
2064 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2065 TREE_VALUE (spec_types),
2069 /* DECL is an explicit specialization. Replicate default arguments
2070 from the template it specializes. (That way, code like:
2072 template <class T> void f(T = 3);
2073 template <> void f(double);
2076 works, as required.) An alternative approach would be to look up
2077 the correct default arguments at the call-site, but this approach
2078 is consistent with how implicit instantiations are handled. */
2081 copy_default_args_to_explicit_spec (tree decl)
2086 tree new_spec_types;
2090 tree object_type = NULL_TREE;
2091 tree in_charge = NULL_TREE;
2092 tree vtt = NULL_TREE;
2094 /* See if there's anything we need to do. */
2095 tmpl = DECL_TI_TEMPLATE (decl);
2096 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2097 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2098 if (TREE_PURPOSE (t))
2103 old_type = TREE_TYPE (decl);
2104 spec_types = TYPE_ARG_TYPES (old_type);
2106 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2108 /* Remove the this pointer, but remember the object's type for
2110 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2111 spec_types = TREE_CHAIN (spec_types);
2112 tmpl_types = TREE_CHAIN (tmpl_types);
2114 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2116 /* DECL may contain more parameters than TMPL due to the extra
2117 in-charge parameter in constructors and destructors. */
2118 in_charge = spec_types;
2119 spec_types = TREE_CHAIN (spec_types);
2121 if (DECL_HAS_VTT_PARM_P (decl))
2124 spec_types = TREE_CHAIN (spec_types);
2128 /* Compute the merged default arguments. */
2130 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2132 /* Compute the new FUNCTION_TYPE. */
2136 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2141 /* Put the in-charge parameter back. */
2142 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2143 TREE_VALUE (in_charge),
2146 new_type = build_method_type_directly (object_type,
2147 TREE_TYPE (old_type),
2151 new_type = build_function_type (TREE_TYPE (old_type),
2153 new_type = cp_build_type_attribute_variant (new_type,
2154 TYPE_ATTRIBUTES (old_type));
2155 new_type = build_exception_variant (new_type,
2156 TYPE_RAISES_EXCEPTIONS (old_type));
2157 TREE_TYPE (decl) = new_type;
2160 /* Check to see if the function just declared, as indicated in
2161 DECLARATOR, and in DECL, is a specialization of a function
2162 template. We may also discover that the declaration is an explicit
2163 instantiation at this point.
2165 Returns DECL, or an equivalent declaration that should be used
2166 instead if all goes well. Issues an error message if something is
2167 amiss. Returns error_mark_node if the error is not easily
2170 FLAGS is a bitmask consisting of the following flags:
2172 2: The function has a definition.
2173 4: The function is a friend.
2175 The TEMPLATE_COUNT is the number of references to qualifying
2176 template classes that appeared in the name of the function. For
2179 template <class T> struct S { void f(); };
2182 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2183 classes are not counted in the TEMPLATE_COUNT, so that in
2185 template <class T> struct S {};
2186 template <> struct S<int> { void f(); }
2187 template <> void S<int>::f();
2189 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2190 invalid; there should be no template <>.)
2192 If the function is a specialization, it is marked as such via
2193 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2194 is set up correctly, and it is added to the list of specializations
2195 for that template. */
2198 check_explicit_specialization (tree declarator,
2203 int have_def = flags & 2;
2204 int is_friend = flags & 4;
2205 int specialization = 0;
2206 int explicit_instantiation = 0;
2207 int member_specialization = 0;
2208 tree ctype = DECL_CLASS_CONTEXT (decl);
2209 tree dname = DECL_NAME (decl);
2214 if (!processing_specialization)
2217 tsk = tsk_excessive_parms;
2220 tsk = current_tmpl_spec_kind (template_count);
2225 if (processing_specialization)
2228 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2230 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2233 /* This could be something like:
2235 template <class T> void f(T);
2236 class S { friend void f<>(int); } */
2240 /* This case handles bogus declarations like template <>
2241 template <class T> void f<int>(); */
2243 error ("template-id %qD in declaration of primary template",
2250 case tsk_invalid_member_spec:
2251 /* The error has already been reported in
2252 check_specialization_scope. */
2253 return error_mark_node;
2255 case tsk_invalid_expl_inst:
2256 error ("template parameter list used in explicit instantiation");
2262 error ("definition provided for explicit instantiation");
2264 explicit_instantiation = 1;
2267 case tsk_excessive_parms:
2268 case tsk_insufficient_parms:
2269 if (tsk == tsk_excessive_parms)
2270 error ("too many template parameter lists in declaration of %qD",
2272 else if (template_header_count)
2273 error("too few template parameter lists in declaration of %qD", decl);
2275 error("explicit specialization of %qD must be introduced by "
2276 "%<template <>%>", decl);
2280 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2282 member_specialization = 1;
2288 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2290 /* This case handles bogus declarations like template <>
2291 template <class T> void f<int>(); */
2293 if (uses_template_parms (declarator))
2294 error ("function template partial specialization %qD "
2295 "is not allowed", declarator);
2297 error ("template-id %qD in declaration of primary template",
2302 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2303 /* This is a specialization of a member template, without
2304 specialization the containing class. Something like:
2306 template <class T> struct S {
2307 template <class U> void f (U);
2309 template <> template <class U> void S<int>::f(U) {}
2311 That's a specialization -- but of the entire template. */
2319 if (specialization || member_specialization)
2321 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2322 for (; t; t = TREE_CHAIN (t))
2323 if (TREE_PURPOSE (t))
2325 permerror (input_location,
2326 "default argument specified in explicit specialization");
2331 if (specialization || member_specialization || explicit_instantiation)
2333 tree tmpl = NULL_TREE;
2334 tree targs = NULL_TREE;
2336 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2337 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2341 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2346 /* If there is no class context, the explicit instantiation
2347 must be at namespace scope. */
2348 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2350 /* Find the namespace binding, using the declaration
2352 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2354 if (fns == error_mark_node || !is_overloaded_fn (fns))
2356 error ("%qD is not a template function", dname);
2357 fns = error_mark_node;
2361 tree fn = OVL_CURRENT (fns);
2362 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2363 CP_DECL_CONTEXT (fn)))
2364 error ("%qD is not declared in %qD",
2365 decl, current_namespace);
2369 declarator = lookup_template_function (fns, NULL_TREE);
2372 if (declarator == error_mark_node)
2373 return error_mark_node;
2375 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2377 if (!explicit_instantiation)
2378 /* A specialization in class scope. This is invalid,
2379 but the error will already have been flagged by
2380 check_specialization_scope. */
2381 return error_mark_node;
2384 /* It's not valid to write an explicit instantiation in
2387 class C { template void f(); }
2389 This case is caught by the parser. However, on
2392 template class C { void f(); };
2394 (which is invalid) we can get here. The error will be
2401 else if (ctype != NULL_TREE
2402 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2405 /* Find the list of functions in ctype that have the same
2406 name as the declared function. */
2407 tree name = TREE_OPERAND (declarator, 0);
2408 tree fns = NULL_TREE;
2411 if (constructor_name_p (name, ctype))
2413 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2415 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2416 : !CLASSTYPE_DESTRUCTORS (ctype))
2418 /* From [temp.expl.spec]:
2420 If such an explicit specialization for the member
2421 of a class template names an implicitly-declared
2422 special member function (clause _special_), the
2423 program is ill-formed.
2425 Similar language is found in [temp.explicit]. */
2426 error ("specialization of implicitly-declared special member function");
2427 return error_mark_node;
2430 name = is_constructor ? ctor_identifier : dtor_identifier;
2433 if (!DECL_CONV_FN_P (decl))
2435 idx = lookup_fnfields_1 (ctype, name);
2437 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2441 VEC(tree,gc) *methods;
2444 /* For a type-conversion operator, we cannot do a
2445 name-based lookup. We might be looking for `operator
2446 int' which will be a specialization of `operator T'.
2447 So, we find *all* the conversion operators, and then
2448 select from them. */
2451 methods = CLASSTYPE_METHOD_VEC (ctype);
2453 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2454 VEC_iterate (tree, methods, idx, ovl);
2457 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2458 /* There are no more conversion functions. */
2461 /* Glue all these conversion functions together
2462 with those we already have. */
2463 for (; ovl; ovl = OVL_NEXT (ovl))
2464 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2468 if (fns == NULL_TREE)
2470 error ("no member function %qD declared in %qT", name, ctype);
2471 return error_mark_node;
2474 TREE_OPERAND (declarator, 0) = fns;
2477 /* Figure out what exactly is being specialized at this point.
2478 Note that for an explicit instantiation, even one for a
2479 member function, we cannot tell apriori whether the
2480 instantiation is for a member template, or just a member
2481 function of a template class. Even if a member template is
2482 being instantiated, the member template arguments may be
2483 elided if they can be deduced from the rest of the
2485 tmpl = determine_specialization (declarator, decl,
2487 member_specialization,
2491 if (!tmpl || tmpl == error_mark_node)
2492 /* We couldn't figure out what this declaration was
2494 return error_mark_node;
2497 tree gen_tmpl = most_general_template (tmpl);
2499 if (explicit_instantiation)
2501 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2502 is done by do_decl_instantiation later. */
2504 int arg_depth = TMPL_ARGS_DEPTH (targs);
2505 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2507 if (arg_depth > parm_depth)
2509 /* If TMPL is not the most general template (for
2510 example, if TMPL is a friend template that is
2511 injected into namespace scope), then there will
2512 be too many levels of TARGS. Remove some of them
2517 new_targs = make_tree_vec (parm_depth);
2518 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2519 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2520 = TREE_VEC_ELT (targs, i);
2524 return instantiate_template (tmpl, targs, tf_error);
2527 /* If we thought that the DECL was a member function, but it
2528 turns out to be specializing a static member function,
2529 make DECL a static member function as well. */
2530 if (DECL_STATIC_FUNCTION_P (tmpl)
2531 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2532 revert_static_member_fn (decl);
2534 /* If this is a specialization of a member template of a
2535 template class, we want to return the TEMPLATE_DECL, not
2536 the specialization of it. */
2537 if (tsk == tsk_template)
2539 tree result = DECL_TEMPLATE_RESULT (tmpl);
2540 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2541 DECL_INITIAL (result) = NULL_TREE;
2545 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2546 DECL_SOURCE_LOCATION (result)
2547 = DECL_SOURCE_LOCATION (decl);
2548 /* We want to use the argument list specified in the
2549 definition, not in the original declaration. */
2550 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2551 for (parm = DECL_ARGUMENTS (result); parm;
2552 parm = TREE_CHAIN (parm))
2553 DECL_CONTEXT (parm) = result;
2555 return register_specialization (tmpl, gen_tmpl, targs,
2559 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2560 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2562 /* Inherit default function arguments from the template
2563 DECL is specializing. */
2564 copy_default_args_to_explicit_spec (decl);
2566 /* This specialization has the same protection as the
2567 template it specializes. */
2568 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2569 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2571 /* 7.1.1-1 [dcl.stc]
2573 A storage-class-specifier shall not be specified in an
2574 explicit specialization...
2576 The parser rejects these, so unless action is taken here,
2577 explicit function specializations will always appear with
2580 The action recommended by the C++ CWG in response to C++
2581 defect report 605 is to make the storage class and linkage
2582 of the explicit specialization match the templated function:
2584 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2586 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2588 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2589 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2591 /* This specialization has the same linkage and visibility as
2592 the function template it specializes. */
2593 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2594 if (! TREE_PUBLIC (decl))
2596 DECL_INTERFACE_KNOWN (decl) = 1;
2597 DECL_NOT_REALLY_EXTERN (decl) = 1;
2599 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2600 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2602 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2603 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2607 /* If DECL is a friend declaration, declared using an
2608 unqualified name, the namespace associated with DECL may
2609 have been set incorrectly. For example, in:
2611 template <typename T> void f(T);
2613 struct S { friend void f<int>(int); }
2616 we will have set the DECL_CONTEXT for the friend
2617 declaration to N, rather than to the global namespace. */
2618 if (DECL_NAMESPACE_SCOPE_P (decl))
2619 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2621 if (is_friend && !have_def)
2622 /* This is not really a declaration of a specialization.
2623 It's just the name of an instantiation. But, it's not
2624 a request for an instantiation, either. */
2625 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2626 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2627 /* This is indeed a specialization. In case of constructors
2628 and destructors, we need in-charge and not-in-charge
2629 versions in V3 ABI. */
2630 clone_function_decl (decl, /*update_method_vec_p=*/0);
2632 /* Register this specialization so that we can find it
2634 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2641 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2642 parameters. These are represented in the same format used for
2643 DECL_TEMPLATE_PARMS. */
2646 comp_template_parms (const_tree parms1, const_tree parms2)
2651 if (parms1 == parms2)
2654 for (p1 = parms1, p2 = parms2;
2655 p1 != NULL_TREE && p2 != NULL_TREE;
2656 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2658 tree t1 = TREE_VALUE (p1);
2659 tree t2 = TREE_VALUE (p2);
2662 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2663 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2665 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2668 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2670 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2671 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2673 /* If either of the template parameters are invalid, assume
2674 they match for the sake of error recovery. */
2675 if (parm1 == error_mark_node || parm2 == error_mark_node)
2678 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2681 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2682 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2683 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2685 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2690 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2691 /* One set of parameters has more parameters lists than the
2698 /* Determine whether PARM is a parameter pack. */
2701 template_parameter_pack_p (const_tree parm)
2703 /* Determine if we have a non-type template parameter pack. */
2704 if (TREE_CODE (parm) == PARM_DECL)
2705 return (DECL_TEMPLATE_PARM_P (parm)
2706 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2708 /* If this is a list of template parameters, we could get a
2709 TYPE_DECL or a TEMPLATE_DECL. */
2710 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2711 parm = TREE_TYPE (parm);
2713 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2714 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2715 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2718 /* Determine if T is a function parameter pack. */
2721 function_parameter_pack_p (const_tree t)
2723 if (t && TREE_CODE (t) == PARM_DECL)
2724 return FUNCTION_PARAMETER_PACK_P (t);
2728 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2729 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2732 get_function_template_decl (const_tree primary_func_tmpl_inst)
2734 if (! primary_func_tmpl_inst
2735 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2736 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2739 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2742 /* Return true iff the function parameter PARAM_DECL was expanded
2743 from the function parameter pack PACK. */
2746 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2748 if (DECL_ARTIFICIAL (param_decl)
2749 || !function_parameter_pack_p (pack))
2752 /* The parameter pack and its pack arguments have the same
2754 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2757 /* Determine whether ARGS describes a variadic template args list,
2758 i.e., one that is terminated by a template argument pack. */
2761 template_args_variadic_p (tree args)
2766 if (args == NULL_TREE)
2769 args = INNERMOST_TEMPLATE_ARGS (args);
2770 nargs = TREE_VEC_LENGTH (args);
2775 last_parm = TREE_VEC_ELT (args, nargs - 1);
2777 return ARGUMENT_PACK_P (last_parm);
2780 /* Generate a new name for the parameter pack name NAME (an
2781 IDENTIFIER_NODE) that incorporates its */
2784 make_ith_pack_parameter_name (tree name, int i)
2786 /* Munge the name to include the parameter index. */
2787 #define NUMBUF_LEN 128
2788 char numbuf[NUMBUF_LEN];
2792 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2793 newname_len = IDENTIFIER_LENGTH (name)
2794 + strlen (numbuf) + 2;
2795 newname = (char*)alloca (newname_len);
2796 snprintf (newname, newname_len,
2797 "%s#%i", IDENTIFIER_POINTER (name), i);
2798 return get_identifier (newname);
2801 /* Return true if T is a primary function
2802 or class template instantiation. */
2805 primary_template_instantiation_p (const_tree t)
2810 if (TREE_CODE (t) == FUNCTION_DECL)
2811 return DECL_LANG_SPECIFIC (t)
2812 && DECL_TEMPLATE_INSTANTIATION (t)
2813 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2814 else if (CLASS_TYPE_P (t))
2815 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2816 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2820 /* Return true if PARM is a template template parameter. */
2823 template_template_parameter_p (const_tree parm)
2825 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2828 /* Return the template parameters of T if T is a
2829 primary template instantiation, NULL otherwise. */
2832 get_primary_template_innermost_parameters (const_tree t)
2834 tree parms = NULL, template_info = NULL;
2836 if ((template_info = get_template_info (t))
2837 && primary_template_instantiation_p (t))
2838 parms = INNERMOST_TEMPLATE_PARMS
2839 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2844 /* Returns the template arguments of T if T is a template instantiation,
2848 get_template_innermost_arguments (const_tree t)
2850 tree args = NULL, template_info = NULL;
2852 if ((template_info = get_template_info (t))
2853 && TI_ARGS (template_info))
2854 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2859 /* Return the argument pack elements of T if T is a template argument pack,
2863 get_template_argument_pack_elems (const_tree t)
2865 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2866 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2869 return ARGUMENT_PACK_ARGS (t);
2872 /* Structure used to track the progress of find_parameter_packs_r. */
2873 struct find_parameter_pack_data
2875 /* TREE_LIST that will contain all of the parameter packs found by
2877 tree* parameter_packs;
2879 /* Set of AST nodes that have been visited by the traversal. */
2880 struct pointer_set_t *visited;
2883 /* Identifies all of the argument packs that occur in a template
2884 argument and appends them to the TREE_LIST inside DATA, which is a
2885 find_parameter_pack_data structure. This is a subroutine of
2886 make_pack_expansion and uses_parameter_packs. */
2888 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2891 struct find_parameter_pack_data* ppd =
2892 (struct find_parameter_pack_data*)data;
2893 bool parameter_pack_p = false;
2895 /* Identify whether this is a parameter pack or not. */
2896 switch (TREE_CODE (t))
2898 case TEMPLATE_PARM_INDEX:
2899 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2900 parameter_pack_p = true;
2903 case TEMPLATE_TYPE_PARM:
2904 case TEMPLATE_TEMPLATE_PARM:
2905 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2906 parameter_pack_p = true;
2910 if (FUNCTION_PARAMETER_PACK_P (t))
2912 /* We don't want to walk into the type of a PARM_DECL,
2913 because we don't want to see the type parameter pack. */
2915 parameter_pack_p = true;
2920 /* Not a parameter pack. */
2924 if (parameter_pack_p)
2926 /* Add this parameter pack to the list. */
2927 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2931 cp_walk_tree (&TYPE_CONTEXT (t),
2932 &find_parameter_packs_r, ppd, ppd->visited);
2934 /* This switch statement will return immediately if we don't find a
2936 switch (TREE_CODE (t))
2938 case TEMPLATE_PARM_INDEX:
2941 case BOUND_TEMPLATE_TEMPLATE_PARM:
2942 /* Check the template itself. */
2943 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
2944 &find_parameter_packs_r, ppd, ppd->visited);
2945 /* Check the template arguments. */
2946 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2951 case TEMPLATE_TYPE_PARM:
2952 case TEMPLATE_TEMPLATE_PARM:
2959 if (TYPE_PTRMEMFUNC_P (t))
2965 if (TYPE_TEMPLATE_INFO (t))
2966 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
2967 &find_parameter_packs_r, ppd, ppd->visited);
2973 cp_walk_tree (&TREE_TYPE (t),
2974 &find_parameter_packs_r, ppd, ppd->visited);
2978 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
2983 case TYPE_PACK_EXPANSION:
2984 case EXPR_PACK_EXPANSION:
2989 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
2994 case IDENTIFIER_NODE:
2995 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3007 /* Determines if the expression or type T uses any parameter packs. */
3009 uses_parameter_packs (tree t)
3011 tree parameter_packs = NULL_TREE;
3012 struct find_parameter_pack_data ppd;
3013 ppd.parameter_packs = ¶meter_packs;
3014 ppd.visited = pointer_set_create ();
3015 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3016 pointer_set_destroy (ppd.visited);
3017 return parameter_packs != NULL_TREE;
3020 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3021 representation a base-class initializer into a parameter pack
3022 expansion. If all goes well, the resulting node will be an
3023 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3026 make_pack_expansion (tree arg)
3029 tree parameter_packs = NULL_TREE;
3030 bool for_types = false;
3031 struct find_parameter_pack_data ppd;
3033 if (!arg || arg == error_mark_node)
3036 if (TREE_CODE (arg) == TREE_LIST)
3038 /* The only time we will see a TREE_LIST here is for a base
3039 class initializer. In this case, the TREE_PURPOSE will be a
3040 _TYPE node (representing the base class expansion we're
3041 initializing) and the TREE_VALUE will be a TREE_LIST
3042 containing the initialization arguments.
3044 The resulting expansion looks somewhat different from most
3045 expansions. Rather than returning just one _EXPANSION, we
3046 return a TREE_LIST whose TREE_PURPOSE is a
3047 TYPE_PACK_EXPANSION containing the bases that will be
3048 initialized. The TREE_VALUE will be identical to the
3049 original TREE_VALUE, which is a list of arguments that will
3050 be passed to each base. We do not introduce any new pack
3051 expansion nodes into the TREE_VALUE (although it is possible
3052 that some already exist), because the TREE_PURPOSE and
3053 TREE_VALUE all need to be expanded together with the same
3054 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3055 resulting TREE_PURPOSE will mention the parameter packs in
3056 both the bases and the arguments to the bases. */
3059 tree parameter_packs = NULL_TREE;
3061 /* Determine which parameter packs will be used by the base
3063 ppd.visited = pointer_set_create ();
3064 ppd.parameter_packs = ¶meter_packs;
3065 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3068 if (parameter_packs == NULL_TREE)
3070 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3071 pointer_set_destroy (ppd.visited);
3072 return error_mark_node;
3075 if (TREE_VALUE (arg) != void_type_node)
3077 /* Collect the sets of parameter packs used in each of the
3078 initialization arguments. */
3079 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3081 /* Determine which parameter packs will be expanded in this
3083 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3088 pointer_set_destroy (ppd.visited);
3090 /* Create the pack expansion type for the base type. */
3091 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3092 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3093 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3095 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3096 they will rarely be compared to anything. */
3097 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3099 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3102 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3105 /* Build the PACK_EXPANSION_* node. */
3107 ? cxx_make_type (TYPE_PACK_EXPANSION)
3108 : make_node (EXPR_PACK_EXPANSION);
3109 SET_PACK_EXPANSION_PATTERN (result, arg);
3110 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3112 /* Propagate type and const-expression information. */
3113 TREE_TYPE (result) = TREE_TYPE (arg);
3114 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3117 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3118 they will rarely be compared to anything. */
3119 SET_TYPE_STRUCTURAL_EQUALITY (result);
3121 /* Determine which parameter packs will be expanded. */
3122 ppd.parameter_packs = ¶meter_packs;
3123 ppd.visited = pointer_set_create ();
3124 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3125 pointer_set_destroy (ppd.visited);
3127 /* Make sure we found some parameter packs. */
3128 if (parameter_packs == NULL_TREE)
3131 error ("expansion pattern %<%T%> contains no argument packs", arg);
3133 error ("expansion pattern %<%E%> contains no argument packs", arg);
3134 return error_mark_node;
3136 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3141 /* Checks T for any "bare" parameter packs, which have not yet been
3142 expanded, and issues an error if any are found. This operation can
3143 only be done on full expressions or types (e.g., an expression
3144 statement, "if" condition, etc.), because we could have expressions like:
3146 foo(f(g(h(args)))...)
3148 where "args" is a parameter pack. check_for_bare_parameter_packs
3149 should not be called for the subexpressions args, h(args),
3150 g(h(args)), or f(g(h(args))), because we would produce erroneous
3153 Returns TRUE and emits an error if there were bare parameter packs,
3154 returns FALSE otherwise. */
3156 check_for_bare_parameter_packs (tree t)
3158 tree parameter_packs = NULL_TREE;
3159 struct find_parameter_pack_data ppd;
3161 if (!processing_template_decl || !t || t == error_mark_node)
3164 if (TREE_CODE (t) == TYPE_DECL)
3167 ppd.parameter_packs = ¶meter_packs;
3168 ppd.visited = pointer_set_create ();
3169 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3170 pointer_set_destroy (ppd.visited);
3172 if (parameter_packs)
3174 error ("parameter packs not expanded with %<...%>:");
3175 while (parameter_packs)
3177 tree pack = TREE_VALUE (parameter_packs);
3178 tree name = NULL_TREE;
3180 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3181 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3182 name = TYPE_NAME (pack);
3183 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3184 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3186 name = DECL_NAME (pack);
3189 inform (input_location, " %qD", name);
3191 inform (input_location, " <anonymous>");
3193 parameter_packs = TREE_CHAIN (parameter_packs);
3202 /* Expand any parameter packs that occur in the template arguments in
3205 expand_template_argument_pack (tree args)
3207 tree result_args = NULL_TREE;
3208 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3209 int num_result_args = -1;
3211 /* First, determine if we need to expand anything, and the number of
3212 slots we'll need. */
3213 for (in_arg = 0; in_arg < nargs; ++in_arg)
3215 tree arg = TREE_VEC_ELT (args, in_arg);
3216 if (arg == NULL_TREE)
3218 if (ARGUMENT_PACK_P (arg))
3220 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3221 if (num_result_args < 0)
3222 num_result_args = in_arg + num_packed;
3224 num_result_args += num_packed;
3228 if (num_result_args >= 0)
3233 /* If no expansion is necessary, we're done. */
3234 if (num_result_args < 0)
3237 /* Expand arguments. */
3238 result_args = make_tree_vec (num_result_args);
3239 for (in_arg = 0; in_arg < nargs; ++in_arg)
3241 tree arg = TREE_VEC_ELT (args, in_arg);
3242 if (ARGUMENT_PACK_P (arg))
3244 tree packed = ARGUMENT_PACK_ARGS (arg);
3245 int i, num_packed = TREE_VEC_LENGTH (packed);
3246 for (i = 0; i < num_packed; ++i, ++out_arg)
3247 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3251 TREE_VEC_ELT (result_args, out_arg) = arg;
3259 /* Checks if DECL shadows a template parameter.
3261 [temp.local]: A template-parameter shall not be redeclared within its
3262 scope (including nested scopes).
3264 Emits an error and returns TRUE if the DECL shadows a parameter,
3265 returns FALSE otherwise. */
3268 check_template_shadow (tree decl)
3272 /* If we're not in a template, we can't possibly shadow a template
3274 if (!current_template_parms)
3277 /* Figure out what we're shadowing. */
3278 if (TREE_CODE (decl) == OVERLOAD)
3279 decl = OVL_CURRENT (decl);
3280 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3282 /* If there's no previous binding for this name, we're not shadowing
3283 anything, let alone a template parameter. */
3287 /* If we're not shadowing a template parameter, we're done. Note
3288 that OLDDECL might be an OVERLOAD (or perhaps even an
3289 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3291 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3294 /* We check for decl != olddecl to avoid bogus errors for using a
3295 name inside a class. We check TPFI to avoid duplicate errors for
3296 inline member templates. */
3298 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3301 error ("declaration of %q+#D", decl);
3302 error (" shadows template parm %q+#D", olddecl);
3306 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3307 ORIG_LEVEL, DECL, and TYPE. */
3310 build_template_parm_index (int index,
3316 tree t = make_node (TEMPLATE_PARM_INDEX);
3317 TEMPLATE_PARM_IDX (t) = index;
3318 TEMPLATE_PARM_LEVEL (t) = level;
3319 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3320 TEMPLATE_PARM_DECL (t) = decl;
3321 TREE_TYPE (t) = type;
3322 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3323 TREE_READONLY (t) = TREE_READONLY (decl);
3328 /* Find the canonical type parameter for the given template type
3329 parameter. Returns the canonical type parameter, which may be TYPE
3330 if no such parameter existed. */
3332 canonical_type_parameter (tree type)
3335 int idx = TEMPLATE_TYPE_IDX (type);
3336 if (!canonical_template_parms)
3337 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3339 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3340 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3342 list = VEC_index (tree, canonical_template_parms, idx);
3343 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3344 list = TREE_CHAIN (list);
3347 return TREE_VALUE (list);
3350 VEC_replace(tree, canonical_template_parms, idx,
3351 tree_cons (NULL_TREE, type,
3352 VEC_index (tree, canonical_template_parms, idx)));
3357 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3358 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3359 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3360 new one is created. */
3363 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3364 tsubst_flags_t complain)
3366 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3367 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3368 != TEMPLATE_PARM_LEVEL (index) - levels)
3369 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3371 tree orig_decl = TEMPLATE_PARM_DECL (index);
3374 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3375 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3376 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3377 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3378 DECL_ARTIFICIAL (decl) = 1;
3379 SET_DECL_TEMPLATE_PARM_P (decl);
3381 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3382 TEMPLATE_PARM_LEVEL (index) - levels,
3383 TEMPLATE_PARM_ORIG_LEVEL (index),
3385 TEMPLATE_PARM_DESCENDANTS (index) = t;
3386 TEMPLATE_PARM_PARAMETER_PACK (t)
3387 = TEMPLATE_PARM_PARAMETER_PACK (index);
3389 /* Template template parameters need this. */
3390 if (TREE_CODE (decl) == TEMPLATE_DECL)
3391 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3392 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3396 return TEMPLATE_PARM_DESCENDANTS (index);
3399 /* Process information from new template parameter PARM and append it to the
3400 LIST being built. This new parameter is a non-type parameter iff
3401 IS_NON_TYPE is true. This new parameter is a parameter
3402 pack iff IS_PARAMETER_PACK is true. The location of PARM is in
3406 process_template_parm (tree list, location_t parm_loc, tree parm, bool is_non_type,
3407 bool is_parameter_pack)
3414 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3415 defval = TREE_PURPOSE (parm);
3419 tree p = tree_last (list);
3421 if (p && TREE_VALUE (p) != error_mark_node)
3424 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3425 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3427 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3437 parm = TREE_VALUE (parm);
3439 SET_DECL_TEMPLATE_PARM_P (parm);
3441 if (TREE_TYPE (parm) == error_mark_node)
3443 err_parm_list = build_tree_list (defval, parm);
3444 TREE_VALUE (err_parm_list) = error_mark_node;
3445 return chainon (list, err_parm_list);
3451 The top-level cv-qualifiers on the template-parameter are
3452 ignored when determining its type. */
3453 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3454 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3456 err_parm_list = build_tree_list (defval, parm);
3457 TREE_VALUE (err_parm_list) = error_mark_node;
3458 return chainon (list, err_parm_list);
3461 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3463 /* This template parameter is not a parameter pack, but it
3464 should be. Complain about "bare" parameter packs. */
3465 check_for_bare_parameter_packs (TREE_TYPE (parm));
3467 /* Recover by calling this a parameter pack. */
3468 is_parameter_pack = true;
3472 /* A template parameter is not modifiable. */
3473 TREE_CONSTANT (parm) = 1;
3474 TREE_READONLY (parm) = 1;
3475 decl = build_decl (parm_loc,
3476 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3477 TREE_CONSTANT (decl) = 1;
3478 TREE_READONLY (decl) = 1;
3479 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3480 = build_template_parm_index (idx, processing_template_decl,
3481 processing_template_decl,
3482 decl, TREE_TYPE (parm));
3484 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3485 = is_parameter_pack;
3490 parm = TREE_VALUE (TREE_VALUE (parm));
3492 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3494 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3495 /* This is for distinguishing between real templates and template
3496 template parameters */
3497 TREE_TYPE (parm) = t;
3498 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3503 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3504 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3505 decl = build_decl (parm_loc,
3506 TYPE_DECL, parm, t);
3509 TYPE_NAME (t) = decl;
3510 TYPE_STUB_DECL (t) = decl;
3512 TEMPLATE_TYPE_PARM_INDEX (t)
3513 = build_template_parm_index (idx, processing_template_decl,
3514 processing_template_decl,
3515 decl, TREE_TYPE (parm));
3516 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3517 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3519 DECL_ARTIFICIAL (decl) = 1;
3520 SET_DECL_TEMPLATE_PARM_P (decl);
3522 parm = build_tree_list (defval, parm);
3523 return chainon (list, parm);
3526 /* The end of a template parameter list has been reached. Process the
3527 tree list into a parameter vector, converting each parameter into a more
3528 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3532 end_template_parm_list (tree parms)
3536 tree saved_parmlist = make_tree_vec (list_length (parms));
3538 current_template_parms
3539 = tree_cons (size_int (processing_template_decl),
3540 saved_parmlist, current_template_parms);
3542 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3544 next = TREE_CHAIN (parm);
3545 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3546 TREE_CHAIN (parm) = NULL_TREE;
3549 --processing_template_parmlist;
3551 return saved_parmlist;
3554 /* end_template_decl is called after a template declaration is seen. */
3557 end_template_decl (void)
3559 reset_specialization ();
3561 if (! processing_template_decl)
3564 /* This matches the pushlevel in begin_template_parm_list. */
3567 --processing_template_decl;
3568 current_template_parms = TREE_CHAIN (current_template_parms);
3571 /* Within the declaration of a template, return all levels of template
3572 parameters that apply. The template parameters are represented as
3573 a TREE_VEC, in the form documented in cp-tree.h for template
3577 current_template_args (void)
3580 tree args = NULL_TREE;
3581 int length = TMPL_PARMS_DEPTH (current_template_parms);
3584 /* If there is only one level of template parameters, we do not
3585 create a TREE_VEC of TREE_VECs. Instead, we return a single
3586 TREE_VEC containing the arguments. */
3588 args = make_tree_vec (length);
3590 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3592 tree a = copy_node (TREE_VALUE (header));
3595 TREE_TYPE (a) = NULL_TREE;
3596 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3598 tree t = TREE_VEC_ELT (a, i);
3600 /* T will be a list if we are called from within a
3601 begin/end_template_parm_list pair, but a vector directly
3602 if within a begin/end_member_template_processing pair. */
3603 if (TREE_CODE (t) == TREE_LIST)
3607 if (!error_operand_p (t))
3609 if (TREE_CODE (t) == TYPE_DECL
3610 || TREE_CODE (t) == TEMPLATE_DECL)
3614 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3616 /* Turn this argument into a TYPE_ARGUMENT_PACK
3617 with a single element, which expands T. */
3618 tree vec = make_tree_vec (1);
3619 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3621 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3622 SET_ARGUMENT_PACK_ARGS (t, vec);
3627 t = DECL_INITIAL (t);
3629 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3631 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3632 with a single element, which expands T. */
3633 tree vec = make_tree_vec (1);
3634 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3635 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3637 t = make_node (NONTYPE_ARGUMENT_PACK);
3638 SET_ARGUMENT_PACK_ARGS (t, vec);
3639 TREE_TYPE (t) = type;
3642 TREE_VEC_ELT (a, i) = t;
3648 TREE_VEC_ELT (args, --l) = a;
3656 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3657 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3658 a member template. Used by push_template_decl below. */
3661 build_template_decl (tree decl, tree parms, bool member_template_p)
3663 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3664 DECL_TEMPLATE_PARMS (tmpl) = parms;
3665 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3666 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3671 struct template_parm_data
3673 /* The level of the template parameters we are currently
3677 /* The index of the specialization argument we are currently
3681 /* An array whose size is the number of template parameters. The
3682 elements are nonzero if the parameter has been used in any one
3683 of the arguments processed so far. */
3686 /* An array whose size is the number of template arguments. The
3687 elements are nonzero if the argument makes use of template
3688 parameters of this level. */
3689 int* arg_uses_template_parms;
3692 /* Subroutine of push_template_decl used to see if each template
3693 parameter in a partial specialization is used in the explicit
3694 argument list. If T is of the LEVEL given in DATA (which is
3695 treated as a template_parm_data*), then DATA->PARMS is marked
3699 mark_template_parm (tree t, void* data)
3703 struct template_parm_data* tpd = (struct template_parm_data*) data;
3705 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3707 level = TEMPLATE_PARM_LEVEL (t);
3708 idx = TEMPLATE_PARM_IDX (t);
3712 level = TEMPLATE_TYPE_LEVEL (t);
3713 idx = TEMPLATE_TYPE_IDX (t);
3716 if (level == tpd->level)
3718 tpd->parms[idx] = 1;
3719 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3722 /* Return zero so that for_each_template_parm will continue the
3723 traversal of the tree; we want to mark *every* template parm. */
3727 /* Process the partial specialization DECL. */
3730 process_partial_specialization (tree decl)
3732 tree type = TREE_TYPE (decl);
3733 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3734 tree specargs = CLASSTYPE_TI_ARGS (type);
3735 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3736 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3738 int nargs = TREE_VEC_LENGTH (inner_args);
3741 int did_error_intro = 0;
3742 struct template_parm_data tpd;
3743 struct template_parm_data tpd2;
3745 gcc_assert (current_template_parms);
3747 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3748 ntparms = TREE_VEC_LENGTH (inner_parms);
3750 /* We check that each of the template parameters given in the
3751 partial specialization is used in the argument list to the
3752 specialization. For example:
3754 template <class T> struct S;
3755 template <class T> struct S<T*>;
3757 The second declaration is OK because `T*' uses the template
3758 parameter T, whereas
3760 template <class T> struct S<int>;
3762 is no good. Even trickier is:
3773 The S2<T> declaration is actually invalid; it is a
3774 full-specialization. Of course,
3777 struct S2<T (*)(U)>;
3779 or some such would have been OK. */
3780 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3781 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
3782 memset (tpd.parms, 0, sizeof (int) * ntparms);
3784 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
3785 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3786 for (i = 0; i < nargs; ++i)
3788 tpd.current_arg = i;
3789 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3790 &mark_template_parm,
3793 /*include_nondeduced_p=*/false);
3795 for (i = 0; i < ntparms; ++i)
3796 if (tpd.parms[i] == 0)
3798 /* One of the template parms was not used in the
3800 if (!did_error_intro)
3802 error ("template parameters not used in partial specialization:");
3803 did_error_intro = 1;
3806 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3809 /* [temp.class.spec]
3811 The argument list of the specialization shall not be identical to
3812 the implicit argument list of the primary template. */
3813 if (comp_template_args
3815 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3817 error ("partial specialization %qT does not specialize any template arguments", type);
3819 /* [temp.class.spec]
3821 A partially specialized non-type argument expression shall not
3822 involve template parameters of the partial specialization except
3823 when the argument expression is a simple identifier.
3825 The type of a template parameter corresponding to a specialized
3826 non-type argument shall not be dependent on a parameter of the
3829 Also, we verify that pack expansions only occur at the
3830 end of the argument list. */
3831 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3833 for (i = 0; i < nargs; ++i)
3835 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3836 tree arg = TREE_VEC_ELT (inner_args, i);
3837 tree packed_args = NULL_TREE;
3840 if (ARGUMENT_PACK_P (arg))
3842 /* Extract the arguments from the argument pack. We'll be
3843 iterating over these in the following loop. */
3844 packed_args = ARGUMENT_PACK_ARGS (arg);
3845 len = TREE_VEC_LENGTH (packed_args);
3848 for (j = 0; j < len; j++)
3851 /* Get the Jth argument in the parameter pack. */
3852 arg = TREE_VEC_ELT (packed_args, j);
3854 if (PACK_EXPANSION_P (arg))
3856 /* Pack expansions must come at the end of the
3858 if ((packed_args && j < len - 1)
3859 || (!packed_args && i < nargs - 1))
3861 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3862 error ("parameter pack argument %qE must be at the "
3863 "end of the template argument list", arg);
3865 error ("parameter pack argument %qT must be at the "
3866 "end of the template argument list", arg);
3870 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3871 /* We only care about the pattern. */
3872 arg = PACK_EXPANSION_PATTERN (arg);
3874 if (/* These first two lines are the `non-type' bit. */
3876 && TREE_CODE (arg) != TEMPLATE_DECL
3877 /* This next line is the `argument expression is not just a
3878 simple identifier' condition and also the `specialized
3879 non-type argument' bit. */
3880 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3882 if ((!packed_args && tpd.arg_uses_template_parms[i])
3883 || (packed_args && uses_template_parms (arg)))
3884 error ("template argument %qE involves template parameter(s)",
3888 /* Look at the corresponding template parameter,
3889 marking which template parameters its type depends
3891 tree type = TREE_TYPE (parm);
3895 /* We haven't yet initialized TPD2. Do so now. */
3896 tpd2.arg_uses_template_parms
3897 = (int *) alloca (sizeof (int) * nargs);
3898 /* The number of parameters here is the number in the
3899 main template, which, as checked in the assertion
3901 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
3903 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
3906 /* Mark the template parameters. But this time, we're
3907 looking for the template parameters of the main
3908 template, not in the specialization. */
3909 tpd2.current_arg = i;
3910 tpd2.arg_uses_template_parms[i] = 0;
3911 memset (tpd2.parms, 0, sizeof (int) * nargs);
3912 for_each_template_parm (type,
3913 &mark_template_parm,
3916 /*include_nondeduced_p=*/false);
3918 if (tpd2.arg_uses_template_parms [i])
3920 /* The type depended on some template parameters.
3921 If they are fully specialized in the
3922 specialization, that's OK. */
3924 for (j = 0; j < nargs; ++j)
3925 if (tpd2.parms[j] != 0
3926 && tpd.arg_uses_template_parms [j])
3928 error ("type %qT of template argument %qE depends "
3929 "on template parameter(s)",
3940 /* We should only get here once. */
3941 gcc_assert (!COMPLETE_TYPE_P (type));
3943 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
3944 = tree_cons (specargs, inner_parms,
3945 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
3946 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
3950 /* Check that a template declaration's use of default arguments and
3951 parameter packs is not invalid. Here, PARMS are the template
3952 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
3953 a primary template. IS_PARTIAL is nonzero if DECL is a partial
3957 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3958 declaration (but not a definition); 1 indicates a declaration, 2
3959 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3960 emitted for extraneous default arguments.
3962 Returns TRUE if there were no errors found, FALSE otherwise. */
3965 check_default_tmpl_args (tree decl, tree parms, int is_primary,
3966 int is_partial, int is_friend_decl)
3969 int last_level_to_check;
3971 bool no_errors = true;
3975 A default template-argument shall not be specified in a
3976 function template declaration or a function template definition, nor
3977 in the template-parameter-list of the definition of a member of a
3980 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
3981 /* You can't have a function template declaration in a local
3982 scope, nor you can you define a member of a class template in a
3986 if (current_class_type
3987 && !TYPE_BEING_DEFINED (current_class_type)
3988 && DECL_LANG_SPECIFIC (decl)
3989 && DECL_DECLARES_FUNCTION_P (decl)
3990 /* If this is either a friend defined in the scope of the class
3991 or a member function. */
3992 && (DECL_FUNCTION_MEMBER_P (decl)
3993 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
3994 : DECL_FRIEND_CONTEXT (decl)
3995 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
3997 /* And, if it was a member function, it really was defined in
3998 the scope of the class. */
3999 && (!DECL_FUNCTION_MEMBER_P (decl)
4000 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4001 /* We already checked these parameters when the template was
4002 declared, so there's no need to do it again now. This function
4003 was defined in class scope, but we're processing it's body now
4004 that the class is complete. */
4007 /* Core issue 226 (C++0x only): the following only applies to class
4009 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
4013 If a template-parameter has a default template-argument, all
4014 subsequent template-parameters shall have a default
4015 template-argument supplied. */
4016 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4018 tree inner_parms = TREE_VALUE (parm_level);
4019 int ntparms = TREE_VEC_LENGTH (inner_parms);
4020 int seen_def_arg_p = 0;
4023 for (i = 0; i < ntparms; ++i)
4025 tree parm = TREE_VEC_ELT (inner_parms, i);
4027 if (parm == error_mark_node)
4030 if (TREE_PURPOSE (parm))
4032 else if (seen_def_arg_p
4033 && !template_parameter_pack_p (TREE_VALUE (parm)))
4035 error ("no default argument for %qD", TREE_VALUE (parm));
4036 /* For better subsequent error-recovery, we indicate that
4037 there should have been a default argument. */
4038 TREE_PURPOSE (parm) = error_mark_node;
4044 /* Don't complain about an enclosing partial
4046 && parm_level == parms
4047 && TREE_CODE (decl) == TYPE_DECL
4049 && template_parameter_pack_p (TREE_VALUE (parm)))
4051 /* A primary class template can only have one
4052 parameter pack, at the end of the template
4055 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4056 error ("parameter pack %qE must be at the end of the"
4057 " template parameter list", TREE_VALUE (parm));
4059 error ("parameter pack %qT must be at the end of the"
4060 " template parameter list",
4061 TREE_TYPE (TREE_VALUE (parm)));
4063 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4071 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4075 /* For an ordinary class template, default template arguments are
4076 allowed at the innermost level, e.g.:
4077 template <class T = int>
4079 but, in a partial specialization, they're not allowed even
4080 there, as we have in [temp.class.spec]:
4082 The template parameter list of a specialization shall not
4083 contain default template argument values.
4085 So, for a partial specialization, or for a function template
4086 (in C++98/C++03), we look at all of them. */
4089 /* But, for a primary class template that is not a partial
4090 specialization we look at all template parameters except the
4092 parms = TREE_CHAIN (parms);
4094 /* Figure out what error message to issue. */
4095 if (is_friend_decl == 2)
4096 msg = "default template arguments may not be used in function template friend re-declaration";
4097 else if (is_friend_decl)
4098 msg = "default template arguments may not be used in function template friend declarations";
4099 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4100 msg = ("default template arguments may not be used in function templates "
4101 "without -std=c++0x or -std=gnu++0x");
4102 else if (is_partial)
4103 msg = "default template arguments may not be used in partial specializations";
4105 msg = "default argument for template parameter for class enclosing %qD";
4107 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4108 /* If we're inside a class definition, there's no need to
4109 examine the parameters to the class itself. On the one
4110 hand, they will be checked when the class is defined, and,
4111 on the other, default arguments are valid in things like:
4112 template <class T = double>
4113 struct S { template <class U> void f(U); };
4114 Here the default argument for `S' has no bearing on the
4115 declaration of `f'. */
4116 last_level_to_check = template_class_depth (current_class_type) + 1;
4118 /* Check everything. */
4119 last_level_to_check = 0;
4121 for (parm_level = parms;
4122 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4123 parm_level = TREE_CHAIN (parm_level))
4125 tree inner_parms = TREE_VALUE (parm_level);
4129 ntparms = TREE_VEC_LENGTH (inner_parms);
4130 for (i = 0; i < ntparms; ++i)
4132 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4135 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4140 if (is_friend_decl == 2)
4147 /* Clear out the default argument so that we are not
4149 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4153 /* At this point, if we're still interested in issuing messages,
4154 they must apply to classes surrounding the object declared. */
4156 msg = "default argument for template parameter for class enclosing %qD";
4162 /* Worker for push_template_decl_real, called via
4163 for_each_template_parm. DATA is really an int, indicating the
4164 level of the parameters we are interested in. If T is a template
4165 parameter of that level, return nonzero. */
4168 template_parm_this_level_p (tree t, void* data)
4170 int this_level = *(int *)data;
4173 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4174 level = TEMPLATE_PARM_LEVEL (t);
4176 level = TEMPLATE_TYPE_LEVEL (t);
4177 return level == this_level;
4180 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4181 parameters given by current_template_args, or reuses a
4182 previously existing one, if appropriate. Returns the DECL, or an
4183 equivalent one, if it is replaced via a call to duplicate_decls.
4185 If IS_FRIEND is true, DECL is a friend declaration. */
4188 push_template_decl_real (tree decl, bool is_friend)
4196 int new_template_p = 0;
4197 /* True if the template is a member template, in the sense of
4199 bool member_template_p = false;
4201 if (decl == error_mark_node || !current_template_parms)
4202 return error_mark_node;
4204 /* See if this is a partial specialization. */
4205 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4206 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4207 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4209 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4213 /* For a friend, we want the context of the friend function, not
4214 the type of which it is a friend. */
4215 ctx = DECL_CONTEXT (decl);
4216 else if (CP_DECL_CONTEXT (decl)
4217 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4218 /* In the case of a virtual function, we want the class in which
4220 ctx = CP_DECL_CONTEXT (decl);
4222 /* Otherwise, if we're currently defining some class, the DECL
4223 is assumed to be a member of the class. */
4224 ctx = current_scope ();
4226 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4229 if (!DECL_CONTEXT (decl))
4230 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4232 /* See if this is a primary template. */
4233 if (is_friend && ctx)
4234 /* A friend template that specifies a class context, i.e.
4235 template <typename T> friend void A<T>::f();
4239 primary = template_parm_scope_p ();
4243 if (DECL_CLASS_SCOPE_P (decl))
4244 member_template_p = true;
4245 if (TREE_CODE (decl) == TYPE_DECL
4246 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4248 error ("template class without a name");
4249 return error_mark_node;
4251 else if (TREE_CODE (decl) == FUNCTION_DECL)
4253 if (DECL_DESTRUCTOR_P (decl))
4257 A destructor shall not be a member template. */
4258 error ("destructor %qD declared as member template", decl);
4259 return error_mark_node;
4261 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4262 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
4263 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4264 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4265 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4266 == void_list_node)))
4268 /* [basic.stc.dynamic.allocation]
4270 An allocation function can be a function
4271 template. ... Template allocation functions shall
4272 have two or more parameters. */
4273 error ("invalid template declaration of %qD", decl);
4274 return error_mark_node;
4277 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4278 && CLASS_TYPE_P (TREE_TYPE (decl)))
4282 error ("template declaration of %q#D", decl);
4283 return error_mark_node;
4287 /* Check to see that the rules regarding the use of default
4288 arguments are not being violated. */
4289 check_default_tmpl_args (decl, current_template_parms,
4290 primary, is_partial, /*is_friend_decl=*/0);
4292 /* Ensure that there are no parameter packs in the type of this
4293 declaration that have not been expanded. */
4294 if (TREE_CODE (decl) == FUNCTION_DECL)
4296 /* Check each of the arguments individually to see if there are
4297 any bare parameter packs. */
4298 tree type = TREE_TYPE (decl);
4299 tree arg = DECL_ARGUMENTS (decl);
4300 tree argtype = TYPE_ARG_TYPES (type);
4302 while (arg && argtype)
4304 if (!FUNCTION_PARAMETER_PACK_P (arg)
4305 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4307 /* This is a PARM_DECL that contains unexpanded parameter
4308 packs. We have already complained about this in the
4309 check_for_bare_parameter_packs call, so just replace
4310 these types with ERROR_MARK_NODE. */
4311 TREE_TYPE (arg) = error_mark_node;
4312 TREE_VALUE (argtype) = error_mark_node;
4315 arg = TREE_CHAIN (arg);
4316 argtype = TREE_CHAIN (argtype);
4319 /* Check for bare parameter packs in the return type and the
4320 exception specifiers. */
4321 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4322 /* Errors were already issued, set return type to int
4323 as the frontend doesn't expect error_mark_node as
4325 TREE_TYPE (type) = integer_type_node;
4326 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4327 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4329 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
4331 TREE_TYPE (decl) = error_mark_node;
4332 return error_mark_node;
4336 return process_partial_specialization (decl);
4338 args = current_template_args ();
4341 || TREE_CODE (ctx) == FUNCTION_DECL
4342 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4343 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4345 if (DECL_LANG_SPECIFIC (decl)
4346 && DECL_TEMPLATE_INFO (decl)
4347 && DECL_TI_TEMPLATE (decl))
4348 tmpl = DECL_TI_TEMPLATE (decl);
4349 /* If DECL is a TYPE_DECL for a class-template, then there won't
4350 be DECL_LANG_SPECIFIC. The information equivalent to
4351 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4352 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4353 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4354 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4356 /* Since a template declaration already existed for this
4357 class-type, we must be redeclaring it here. Make sure
4358 that the redeclaration is valid. */
4359 redeclare_class_template (TREE_TYPE (decl),
4360 current_template_parms);
4361 /* We don't need to create a new TEMPLATE_DECL; just use the
4362 one we already had. */
4363 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4367 tmpl = build_template_decl (decl, current_template_parms,
4371 if (DECL_LANG_SPECIFIC (decl)
4372 && DECL_TEMPLATE_SPECIALIZATION (decl))
4374 /* A specialization of a member template of a template
4376 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4377 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4378 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4384 tree a, t, current, parms;
4386 tree tinfo = get_template_info (decl);
4390 error ("template definition of non-template %q#D", decl);
4391 return error_mark_node;
4394 tmpl = TI_TEMPLATE (tinfo);
4396 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4397 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4398 && DECL_TEMPLATE_SPECIALIZATION (decl)
4399 && DECL_MEMBER_TEMPLATE_P (tmpl))
4403 /* The declaration is a specialization of a member
4404 template, declared outside the class. Therefore, the
4405 innermost template arguments will be NULL, so we
4406 replace them with the arguments determined by the
4407 earlier call to check_explicit_specialization. */
4408 args = DECL_TI_ARGS (decl);
4411 = build_template_decl (decl, current_template_parms,
4413 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4414 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4415 DECL_TI_TEMPLATE (decl) = new_tmpl;
4416 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4417 DECL_TEMPLATE_INFO (new_tmpl)
4418 = build_template_info (tmpl, args);
4420 register_specialization (new_tmpl,
4421 most_general_template (tmpl),
4427 /* Make sure the template headers we got make sense. */
4429 parms = DECL_TEMPLATE_PARMS (tmpl);
4430 i = TMPL_PARMS_DEPTH (parms);
4431 if (TMPL_ARGS_DEPTH (args) != i)
4433 error ("expected %d levels of template parms for %q#D, got %d",
4434 i, decl, TMPL_ARGS_DEPTH (args));
4437 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4439 a = TMPL_ARGS_LEVEL (args, i);
4440 t = INNERMOST_TEMPLATE_PARMS (parms);
4442 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4444 if (current == decl)
4445 error ("got %d template parameters for %q#D",
4446 TREE_VEC_LENGTH (a), decl);
4448 error ("got %d template parameters for %q#T",
4449 TREE_VEC_LENGTH (a), current);
4450 error (" but %d required", TREE_VEC_LENGTH (t));
4451 return error_mark_node;
4454 if (current == decl)
4457 current = (TYPE_P (current)
4458 ? TYPE_CONTEXT (current)
4459 : DECL_CONTEXT (current));
4462 /* Check that the parms are used in the appropriate qualifying scopes
4463 in the declarator. */
4464 if (!comp_template_args
4466 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4469 template arguments to %qD do not match original template %qD",
4470 decl, DECL_TEMPLATE_RESULT (tmpl));
4471 if (!uses_template_parms (TI_ARGS (tinfo)))
4472 inform (input_location, "use template<> for an explicit specialization");
4473 /* Avoid crash in import_export_decl. */
4474 DECL_INTERFACE_KNOWN (decl) = 1;
4475 return error_mark_node;
4479 DECL_TEMPLATE_RESULT (tmpl) = decl;
4480 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4482 /* Push template declarations for global functions and types. Note
4483 that we do not try to push a global template friend declared in a
4484 template class; such a thing may well depend on the template
4485 parameters of the class. */
4486 if (new_template_p && !ctx
4487 && !(is_friend && template_class_depth (current_class_type) > 0))
4489 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4490 if (tmpl == error_mark_node)
4491 return error_mark_node;
4493 /* Hide template friend classes that haven't been declared yet. */
4494 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4496 DECL_ANTICIPATED (tmpl) = 1;
4497 DECL_FRIEND_P (tmpl) = 1;
4503 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4506 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4507 if (DECL_CONV_FN_P (tmpl))
4509 int depth = TMPL_PARMS_DEPTH (parms);
4511 /* It is a conversion operator. See if the type converted to
4512 depends on innermost template operands. */
4514 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4516 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4519 /* Give template template parms a DECL_CONTEXT of the template
4520 for which they are a parameter. */
4521 parms = INNERMOST_TEMPLATE_PARMS (parms);
4522 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4524 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4525 if (TREE_CODE (parm) == TEMPLATE_DECL)
4526 DECL_CONTEXT (parm) = tmpl;
4528 if (TREE_CODE (TREE_TYPE (parm)) == TEMPLATE_TYPE_PARM)
4529 DECL_CONTEXT (TYPE_NAME (TREE_TYPE (parm))) = tmpl;
4533 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4534 back to its most general template. If TMPL is a specialization,
4535 ARGS may only have the innermost set of arguments. Add the missing
4536 argument levels if necessary. */
4537 if (DECL_TEMPLATE_INFO (tmpl))
4538 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4540 info = build_template_info (tmpl, args);
4542 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4543 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4544 else if (DECL_LANG_SPECIFIC (decl))
4545 DECL_TEMPLATE_INFO (decl) = info;
4547 return DECL_TEMPLATE_RESULT (tmpl);
4551 push_template_decl (tree decl)
4553 return push_template_decl_real (decl, false);
4556 /* Called when a class template TYPE is redeclared with the indicated
4557 template PARMS, e.g.:
4559 template <class T> struct S;
4560 template <class T> struct S {}; */
4563 redeclare_class_template (tree type, tree parms)
4569 if (!TYPE_TEMPLATE_INFO (type))
4571 error ("%qT is not a template type", type);
4575 tmpl = TYPE_TI_TEMPLATE (type);
4576 if (!PRIMARY_TEMPLATE_P (tmpl))
4577 /* The type is nested in some template class. Nothing to worry
4578 about here; there are no new template parameters for the nested
4584 error ("template specifiers not specified in declaration of %qD",
4589 parms = INNERMOST_TEMPLATE_PARMS (parms);
4590 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4592 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4594 error ("redeclared with %d template parameter(s)",
4595 TREE_VEC_LENGTH (parms));
4596 inform (input_location, "previous declaration %q+D used %d template parameter(s)",
4597 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4601 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4608 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4609 || TREE_VEC_ELT (parms, i) == error_mark_node)
4612 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4613 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4614 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4615 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4617 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4619 if (tmpl_parm != error_mark_node
4620 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4621 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4622 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4623 || (TREE_CODE (tmpl_parm) != PARM_DECL
4624 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4625 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4626 || (TREE_CODE (tmpl_parm) == PARM_DECL
4627 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4628 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))))))
4630 error ("template parameter %q+#D", tmpl_parm);
4631 error ("redeclared here as %q#D", parm);
4635 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4637 /* We have in [temp.param]:
4639 A template-parameter may not be given default arguments
4640 by two different declarations in the same scope. */
4641 error_at (input_location, "redefinition of default argument for %q#D", parm);
4642 inform (DECL_SOURCE_LOCATION (tmpl_parm),
4643 "original definition appeared here");
4647 if (parm_default != NULL_TREE)
4648 /* Update the previous template parameters (which are the ones
4649 that will really count) with the new default value. */
4650 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4651 else if (tmpl_default != NULL_TREE)
4652 /* Update the new parameters, too; they'll be used as the
4653 parameters for any members. */
4654 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4660 /* Simplify EXPR if it is a non-dependent expression. Returns the
4661 (possibly simplified) expression. */
4664 fold_non_dependent_expr (tree expr)
4666 if (expr == NULL_TREE)
4669 /* If we're in a template, but EXPR isn't value dependent, simplify
4670 it. We're supposed to treat:
4672 template <typename T> void f(T[1 + 1]);
4673 template <typename T> void f(T[2]);
4675 as two declarations of the same function, for example. */
4676 if (processing_template_decl
4677 && !type_dependent_expression_p (expr)
4678 && !value_dependent_expression_p (expr))
4680 HOST_WIDE_INT saved_processing_template_decl;
4682 saved_processing_template_decl = processing_template_decl;
4683 processing_template_decl = 0;
4684 expr = tsubst_copy_and_build (expr,
4687 /*in_decl=*/NULL_TREE,
4688 /*function_p=*/false,
4689 /*integral_constant_expression_p=*/true);
4690 processing_template_decl = saved_processing_template_decl;
4695 /* EXPR is an expression which is used in a constant-expression context.
4696 For instance, it could be a VAR_DECL with a constant initializer.
4697 Extract the innermost constant expression.
4699 This is basically a more powerful version of
4700 integral_constant_value, which can be used also in templates where
4701 initializers can maintain a syntactic rather than semantic form
4702 (even if they are non-dependent, for access-checking purposes). */
4705 fold_decl_constant_value (tree expr)
4707 tree const_expr = expr;
4710 expr = fold_non_dependent_expr (const_expr);
4711 const_expr = integral_constant_value (expr);
4713 while (expr != const_expr);
4718 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4719 must be a function or a pointer-to-function type, as specified
4720 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4721 and check that the resulting function has external linkage. */
4724 convert_nontype_argument_function (tree type, tree expr)
4729 fn = instantiate_type (type, fns, tf_none);
4730 if (fn == error_mark_node)
4731 return error_mark_node;
4734 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4735 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4736 if (TREE_CODE (fn_no_ptr) == BASELINK)
4737 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4739 /* [temp.arg.nontype]/1
4741 A template-argument for a non-type, non-template template-parameter
4744 -- the address of an object or function with external linkage. */
4745 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4747 error ("%qE is not a valid template argument for type %qT "
4748 "because function %qD has not external linkage",
4749 expr, type, fn_no_ptr);
4756 /* Subroutine of convert_nontype_argument.
4757 Check if EXPR of type TYPE is a valid pointer-to-member constant.
4758 Emit an error otherwise. */
4761 check_valid_ptrmem_cst_expr (tree type, tree expr)
4764 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
4766 error ("%qE is not a valid template argument for type %qT",
4768 error ("it must be a pointer-to-member of the form `&X::Y'");
4772 /* Attempt to convert the non-type template parameter EXPR to the
4773 indicated TYPE. If the conversion is successful, return the
4774 converted value. If the conversion is unsuccessful, return
4775 NULL_TREE if we issued an error message, or error_mark_node if we
4776 did not. We issue error messages for out-and-out bad template
4777 parameters, but not simply because the conversion failed, since we
4778 might be just trying to do argument deduction. Both TYPE and EXPR
4779 must be non-dependent.
4781 The conversion follows the special rules described in
4782 [temp.arg.nontype], and it is much more strict than an implicit
4785 This function is called twice for each template argument (see
4786 lookup_template_class for a more accurate description of this
4787 problem). This means that we need to handle expressions which
4788 are not valid in a C++ source, but can be created from the
4789 first call (for instance, casts to perform conversions). These
4790 hacks can go away after we fix the double coercion problem. */
4793 convert_nontype_argument (tree type, tree expr)
4797 /* Detect immediately string literals as invalid non-type argument.
4798 This special-case is not needed for correctness (we would easily
4799 catch this later), but only to provide better diagnostic for this
4800 common user mistake. As suggested by DR 100, we do not mention
4801 linkage issues in the diagnostic as this is not the point. */
4802 if (TREE_CODE (expr) == STRING_CST)
4804 error ("%qE is not a valid template argument for type %qT "
4805 "because string literals can never be used in this context",
4810 /* If we are in a template, EXPR may be non-dependent, but still
4811 have a syntactic, rather than semantic, form. For example, EXPR
4812 might be a SCOPE_REF, rather than the VAR_DECL to which the
4813 SCOPE_REF refers. Preserving the qualifying scope is necessary
4814 so that access checking can be performed when the template is
4815 instantiated -- but here we need the resolved form so that we can
4816 convert the argument. */
4817 expr = fold_non_dependent_expr (expr);
4818 if (error_operand_p (expr))
4819 return error_mark_node;
4820 expr_type = TREE_TYPE (expr);
4822 /* HACK: Due to double coercion, we can get a
4823 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4824 which is the tree that we built on the first call (see
4825 below when coercing to reference to object or to reference to
4826 function). We just strip everything and get to the arg.
4827 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4829 if (TREE_CODE (expr) == NOP_EXPR)
4831 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
4833 /* ??? Maybe we could use convert_from_reference here, but we
4834 would need to relax its constraints because the NOP_EXPR
4835 could actually change the type to something more cv-qualified,
4836 and this is not folded by convert_from_reference. */
4837 tree addr = TREE_OPERAND (expr, 0);
4838 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
4839 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
4840 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
4841 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4842 (TREE_TYPE (expr_type),
4843 TREE_TYPE (TREE_TYPE (addr))));
4845 expr = TREE_OPERAND (addr, 0);
4846 expr_type = TREE_TYPE (expr);
4849 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4850 parameter is a pointer to object, through decay and
4851 qualification conversion. Let's strip everything. */
4852 else if (TYPE_PTROBV_P (type))
4855 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
4856 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
4857 /* Skip the ADDR_EXPR only if it is part of the decay for
4858 an array. Otherwise, it is part of the original argument
4859 in the source code. */
4860 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
4861 expr = TREE_OPERAND (expr, 0);
4862 expr_type = TREE_TYPE (expr);
4866 /* [temp.arg.nontype]/5, bullet 1
4868 For a non-type template-parameter of integral or enumeration type,
4869 integral promotions (_conv.prom_) and integral conversions
4870 (_conv.integral_) are applied. */
4871 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4873 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type))
4874 return error_mark_node;
4876 expr = fold_decl_constant_value (expr);
4877 /* Notice that there are constant expressions like '4 % 0' which
4878 do not fold into integer constants. */
4879 if (TREE_CODE (expr) != INTEGER_CST)
4881 error ("%qE is not a valid template argument for type %qT "
4882 "because it is a non-constant expression", expr, type);
4886 /* At this point, an implicit conversion does what we want,
4887 because we already know that the expression is of integral
4889 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
4890 if (expr == error_mark_node)
4891 return error_mark_node;
4893 /* Conversion was allowed: fold it to a bare integer constant. */
4896 /* [temp.arg.nontype]/5, bullet 2
4898 For a non-type template-parameter of type pointer to object,
4899 qualification conversions (_conv.qual_) and the array-to-pointer
4900 conversion (_conv.array_) are applied. */
4901 else if (TYPE_PTROBV_P (type))
4903 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
4905 A template-argument for a non-type, non-template template-parameter
4906 shall be one of: [...]
4908 -- the name of a non-type template-parameter;
4909 -- the address of an object or function with external linkage, [...]
4910 expressed as "& id-expression" where the & is optional if the name
4911 refers to a function or array, or if the corresponding
4912 template-parameter is a reference.
4914 Here, we do not care about functions, as they are invalid anyway
4915 for a parameter of type pointer-to-object. */
4917 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
4918 /* Non-type template parameters are OK. */
4920 else if (TREE_CODE (expr) != ADDR_EXPR
4921 && TREE_CODE (expr_type) != ARRAY_TYPE)
4923 if (TREE_CODE (expr) == VAR_DECL)
4925 error ("%qD is not a valid template argument "
4926 "because %qD is a variable, not the address of "
4931 /* Other values, like integer constants, might be valid
4932 non-type arguments of some other type. */
4933 return error_mark_node;
4939 decl = ((TREE_CODE (expr) == ADDR_EXPR)
4940 ? TREE_OPERAND (expr, 0) : expr);
4941 if (TREE_CODE (decl) != VAR_DECL)
4943 error ("%qE is not a valid template argument of type %qT "
4944 "because %qE is not a variable",
4948 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
4950 error ("%qE is not a valid template argument of type %qT "
4951 "because %qD does not have external linkage",
4957 expr = decay_conversion (expr);
4958 if (expr == error_mark_node)
4959 return error_mark_node;
4961 expr = perform_qualification_conversions (type, expr);
4962 if (expr == error_mark_node)
4963 return error_mark_node;
4965 /* [temp.arg.nontype]/5, bullet 3
4967 For a non-type template-parameter of type reference to object, no
4968 conversions apply. The type referred to by the reference may be more
4969 cv-qualified than the (otherwise identical) type of the
4970 template-argument. The template-parameter is bound directly to the
4971 template-argument, which must be an lvalue. */
4972 else if (TYPE_REF_OBJ_P (type))
4974 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
4976 return error_mark_node;
4978 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
4980 error ("%qE is not a valid template argument for type %qT "
4981 "because of conflicts in cv-qualification", expr, type);
4985 if (!real_lvalue_p (expr))
4987 error ("%qE is not a valid template argument for type %qT "
4988 "because it is not an lvalue", expr, type);
4992 /* [temp.arg.nontype]/1
4994 A template-argument for a non-type, non-template template-parameter
4995 shall be one of: [...]
4997 -- the address of an object or function with external linkage. */
4998 if (TREE_CODE (expr) == INDIRECT_REF
4999 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5001 expr = TREE_OPERAND (expr, 0);
5004 error ("%q#D is not a valid template argument for type %qT "
5005 "because a reference variable does not have a constant "
5006 "address", expr, type);
5013 error ("%qE is not a valid template argument for type %qT "
5014 "because it is not an object with external linkage",
5019 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5021 error ("%qE is not a valid template argument for type %qT "
5022 "because object %qD has not external linkage",
5027 expr = build_nop (type, build_address (expr));
5029 /* [temp.arg.nontype]/5, bullet 4
5031 For a non-type template-parameter of type pointer to function, only
5032 the function-to-pointer conversion (_conv.func_) is applied. If the
5033 template-argument represents a set of overloaded functions (or a
5034 pointer to such), the matching function is selected from the set
5036 else if (TYPE_PTRFN_P (type))
5038 /* If the argument is a template-id, we might not have enough
5039 context information to decay the pointer. */
5040 if (!type_unknown_p (expr_type))
5042 expr = decay_conversion (expr);
5043 if (expr == error_mark_node)
5044 return error_mark_node;
5047 expr = convert_nontype_argument_function (type, expr);
5048 if (!expr || expr == error_mark_node)
5051 if (TREE_CODE (expr) != ADDR_EXPR)
5053 error ("%qE is not a valid template argument for type %qT", expr, type);
5054 error ("it must be the address of a function with external linkage");
5058 /* [temp.arg.nontype]/5, bullet 5
5060 For a non-type template-parameter of type reference to function, no
5061 conversions apply. If the template-argument represents a set of
5062 overloaded functions, the matching function is selected from the set
5064 else if (TYPE_REFFN_P (type))
5066 if (TREE_CODE (expr) == ADDR_EXPR)
5068 error ("%qE is not a valid template argument for type %qT "
5069 "because it is a pointer", expr, type);
5070 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5074 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5075 if (!expr || expr == error_mark_node)
5078 expr = build_nop (type, build_address (expr));
5080 /* [temp.arg.nontype]/5, bullet 6
5082 For a non-type template-parameter of type pointer to member function,
5083 no conversions apply. If the template-argument represents a set of
5084 overloaded member functions, the matching member function is selected
5085 from the set (_over.over_). */
5086 else if (TYPE_PTRMEMFUNC_P (type))
5088 expr = instantiate_type (type, expr, tf_none);
5089 if (expr == error_mark_node)
5090 return error_mark_node;
5092 /* [temp.arg.nontype] bullet 1 says the pointer to member
5093 expression must be a pointer-to-member constant. */
5094 if (!check_valid_ptrmem_cst_expr (type, expr))
5095 return error_mark_node;
5097 /* There is no way to disable standard conversions in
5098 resolve_address_of_overloaded_function (called by
5099 instantiate_type). It is possible that the call succeeded by
5100 converting &B::I to &D::I (where B is a base of D), so we need
5101 to reject this conversion here.
5103 Actually, even if there was a way to disable standard conversions,
5104 it would still be better to reject them here so that we can
5105 provide a superior diagnostic. */
5106 if (!same_type_p (TREE_TYPE (expr), type))
5108 /* Make sure we are just one standard conversion off. */
5109 gcc_assert (can_convert (type, TREE_TYPE (expr)));
5110 error ("%qE is not a valid template argument for type %qT "
5111 "because it is of type %qT", expr, type,
5113 inform (input_location, "standard conversions are not allowed in this context");
5117 /* [temp.arg.nontype]/5, bullet 7
5119 For a non-type template-parameter of type pointer to data member,
5120 qualification conversions (_conv.qual_) are applied. */
5121 else if (TYPE_PTRMEM_P (type))
5123 /* [temp.arg.nontype] bullet 1 says the pointer to member
5124 expression must be a pointer-to-member constant. */
5125 if (!check_valid_ptrmem_cst_expr (type, expr))
5126 return error_mark_node;
5128 expr = perform_qualification_conversions (type, expr);
5129 if (expr == error_mark_node)
5132 /* A template non-type parameter must be one of the above. */
5136 /* Sanity check: did we actually convert the argument to the
5138 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
5142 /* Subroutine of coerce_template_template_parms, which returns 1 if
5143 PARM_PARM and ARG_PARM match using the rule for the template
5144 parameters of template template parameters. Both PARM and ARG are
5145 template parameters; the rest of the arguments are the same as for
5146 coerce_template_template_parms.
5149 coerce_template_template_parm (tree parm,
5151 tsubst_flags_t complain,
5155 if (arg == NULL_TREE || arg == error_mark_node
5156 || parm == NULL_TREE || parm == error_mark_node)
5159 if (TREE_CODE (arg) != TREE_CODE (parm))
5162 switch (TREE_CODE (parm))
5165 /* We encounter instantiations of templates like
5166 template <template <template <class> class> class TT>
5169 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5170 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5172 if (!coerce_template_template_parms
5173 (parmparm, argparm, complain, in_decl, outer_args))
5179 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5180 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5181 /* Argument is a parameter pack but parameter is not. */
5186 /* The tsubst call is used to handle cases such as
5188 template <int> class C {};
5189 template <class T, template <T> class TT> class D {};
5192 i.e. the parameter list of TT depends on earlier parameters. */
5193 if (!uses_template_parms (TREE_TYPE (arg))
5195 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5199 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5200 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5201 /* Argument is a parameter pack but parameter is not. */
5214 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5215 template template parameters. Both PARM_PARMS and ARG_PARMS are
5216 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5219 Consider the example:
5220 template <class T> class A;
5221 template<template <class U> class TT> class B;
5223 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5224 the parameters to A, and OUTER_ARGS contains A. */
5227 coerce_template_template_parms (tree parm_parms,
5229 tsubst_flags_t complain,
5233 int nparms, nargs, i;
5237 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5238 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5240 nparms = TREE_VEC_LENGTH (parm_parms);
5241 nargs = TREE_VEC_LENGTH (arg_parms);
5243 /* Determine whether we have a parameter pack at the end of the
5244 template template parameter's template parameter list. */
5245 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5247 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5249 if (parm == error_mark_node)
5252 switch (TREE_CODE (parm))
5256 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5261 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5271 && !(variadic_p && nargs >= nparms - 1))
5274 /* Check all of the template parameters except the parameter pack at
5275 the end (if any). */
5276 for (i = 0; i < nparms - variadic_p; ++i)
5278 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5279 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5282 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5283 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5285 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5293 /* Check each of the template parameters in the template
5294 argument against the template parameter pack at the end of
5295 the template template parameter. */
5296 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
5299 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5301 for (; i < nargs; ++i)
5303 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5306 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5308 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5317 /* Verifies that the deduced template arguments (in TARGS) for the
5318 template template parameters (in TPARMS) represent valid bindings,
5319 by comparing the template parameter list of each template argument
5320 to the template parameter list of its corresponding template
5321 template parameter, in accordance with DR150. This
5322 routine can only be called after all template arguments have been
5323 deduced. It will return TRUE if all of the template template
5324 parameter bindings are okay, FALSE otherwise. */
5326 template_template_parm_bindings_ok_p (tree tparms, tree targs)
5328 int i, ntparms = TREE_VEC_LENGTH (tparms);
5331 /* We're dealing with template parms in this process. */
5332 ++processing_template_decl;
5334 targs = INNERMOST_TEMPLATE_ARGS (targs);
5336 for (i = 0; i < ntparms; ++i)
5338 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
5339 tree targ = TREE_VEC_ELT (targs, i);
5341 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
5343 tree packed_args = NULL_TREE;
5346 if (ARGUMENT_PACK_P (targ))
5348 /* Look inside the argument pack. */
5349 packed_args = ARGUMENT_PACK_ARGS (targ);
5350 len = TREE_VEC_LENGTH (packed_args);
5353 for (idx = 0; idx < len; ++idx)
5355 tree targ_parms = NULL_TREE;
5358 /* Extract the next argument from the argument
5360 targ = TREE_VEC_ELT (packed_args, idx);
5362 if (PACK_EXPANSION_P (targ))
5363 /* Look at the pattern of the pack expansion. */
5364 targ = PACK_EXPANSION_PATTERN (targ);
5366 /* Extract the template parameters from the template
5368 if (TREE_CODE (targ) == TEMPLATE_DECL)
5369 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
5370 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
5371 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
5373 /* Verify that we can coerce the template template
5374 parameters from the template argument to the template
5375 parameter. This requires an exact match. */
5377 && !coerce_template_template_parms
5378 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
5393 --processing_template_decl;
5397 /* Convert the indicated template ARG as necessary to match the
5398 indicated template PARM. Returns the converted ARG, or
5399 error_mark_node if the conversion was unsuccessful. Error and
5400 warning messages are issued under control of COMPLAIN. This
5401 conversion is for the Ith parameter in the parameter list. ARGS is
5402 the full set of template arguments deduced so far. */
5405 convert_template_argument (tree parm,
5408 tsubst_flags_t complain,
5414 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
5416 if (TREE_CODE (arg) == TREE_LIST
5417 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
5419 /* The template argument was the name of some
5420 member function. That's usually
5421 invalid, but static members are OK. In any
5422 case, grab the underlying fields/functions
5423 and issue an error later if required. */
5424 orig_arg = TREE_VALUE (arg);
5425 TREE_TYPE (arg) = unknown_type_node;
5430 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
5431 requires_type = (TREE_CODE (parm) == TYPE_DECL
5432 || requires_tmpl_type);
5434 /* When determining whether an argument pack expansion is a template,
5435 look at the pattern. */
5436 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
5437 arg = PACK_EXPANSION_PATTERN (arg);
5439 /* Deal with an injected-class-name used as a template template arg. */
5440 if (requires_tmpl_type && CLASS_TYPE_P (arg))
5442 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
5443 if (TREE_CODE (t) == TEMPLATE_DECL)
5445 if (complain & tf_warning_or_error)
5446 pedwarn (input_location, OPT_pedantic, "injected-class-name %qD"
5447 " used as template template argument", TYPE_NAME (arg));
5448 else if (flag_pedantic_errors)
5456 ((TREE_CODE (arg) == TEMPLATE_DECL
5457 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
5458 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5459 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
5462 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5463 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
5464 arg = TYPE_STUB_DECL (arg);
5466 is_type = TYPE_P (arg) || is_tmpl_type;
5468 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
5469 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
5471 permerror (input_location, "to refer to a type member of a template parameter, "
5472 "use %<typename %E%>", orig_arg);
5474 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
5475 TREE_OPERAND (arg, 1),
5477 complain & tf_error);
5481 if (is_type != requires_type)
5485 if (complain & tf_error)
5487 error ("type/value mismatch at argument %d in template "
5488 "parameter list for %qD",
5491 error (" expected a constant of type %qT, got %qT",
5493 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
5494 else if (requires_tmpl_type)
5495 error (" expected a class template, got %qE", orig_arg);
5497 error (" expected a type, got %qE", orig_arg);
5500 return error_mark_node;
5502 if (is_tmpl_type ^ requires_tmpl_type)
5504 if (in_decl && (complain & tf_error))
5506 error ("type/value mismatch at argument %d in template "
5507 "parameter list for %qD",
5510 error (" expected a type, got %qT", DECL_NAME (arg));
5512 error (" expected a class template, got %qT", orig_arg);
5514 return error_mark_node;
5519 if (requires_tmpl_type)
5521 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
5522 /* The number of argument required is not known yet.
5523 Just accept it for now. */
5524 val = TREE_TYPE (arg);
5527 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5530 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5532 if (coerce_template_template_parms (parmparm, argparm,
5538 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5540 if (val != error_mark_node)
5542 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
5543 val = TREE_TYPE (val);
5544 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
5545 val = make_pack_expansion (val);
5550 if (in_decl && (complain & tf_error))
5552 error ("type/value mismatch at argument %d in "
5553 "template parameter list for %qD",
5555 error (" expected a template of type %qD, got %qT",
5559 val = error_mark_node;
5565 /* We only form one instance of each template specialization.
5566 Therefore, if we use a non-canonical variant (i.e., a
5567 typedef), any future messages referring to the type will use
5568 the typedef, which is confusing if those future uses do not
5569 themselves also use the typedef. */
5571 val = strip_typedefs (val);
5575 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
5577 if (invalid_nontype_parm_type_p (t, complain))
5578 return error_mark_node;
5580 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
5582 if (same_type_p (t, TREE_TYPE (orig_arg)))
5586 /* Not sure if this is reachable, but it doesn't hurt
5588 error ("type mismatch in nontype parameter pack");
5589 val = error_mark_node;
5592 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
5593 /* We used to call digest_init here. However, digest_init
5594 will report errors, which we don't want when complain
5595 is zero. More importantly, digest_init will try too
5596 hard to convert things: for example, `0' should not be
5597 converted to pointer type at this point according to
5598 the standard. Accepting this is not merely an
5599 extension, since deciding whether or not these
5600 conversions can occur is part of determining which
5601 function template to call, or whether a given explicit
5602 argument specification is valid. */
5603 val = convert_nontype_argument (t, orig_arg);
5607 if (val == NULL_TREE)
5608 val = error_mark_node;
5609 else if (val == error_mark_node && (complain & tf_error))
5610 error ("could not convert template argument %qE to %qT", orig_arg, t);
5612 if (TREE_CODE (val) == SCOPE_REF)
5614 /* Strip typedefs from the SCOPE_REF. */
5615 tree type = strip_typedefs (TREE_TYPE (val));
5616 tree scope = strip_typedefs (TREE_OPERAND (val, 0));
5617 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
5618 QUALIFIED_NAME_IS_TEMPLATE (val));
5625 /* Coerces the remaining template arguments in INNER_ARGS (from
5626 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5627 Returns the coerced argument pack. PARM_IDX is the position of this
5628 parameter in the template parameter list. ARGS is the original
5629 template argument list. */
5631 coerce_template_parameter_pack (tree parms,
5639 tsubst_flags_t complain)
5641 tree parm = TREE_VEC_ELT (parms, parm_idx);
5642 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5645 tree packed_types = NULL_TREE;
5647 if (arg_idx > nargs)
5650 packed_args = make_tree_vec (nargs - arg_idx);
5652 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
5653 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
5655 /* When the template parameter is a non-type template
5656 parameter pack whose type uses parameter packs, we need
5657 to look at each of the template arguments
5658 separately. Build a vector of the types for these
5659 non-type template parameters in PACKED_TYPES. */
5661 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
5662 packed_types = tsubst_pack_expansion (expansion, args,
5665 if (packed_types == error_mark_node)
5666 return error_mark_node;
5668 /* Check that we have the right number of arguments. */
5670 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
5671 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
5674 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
5675 error ("wrong number of template arguments (%d, should be %d)",
5676 nargs, needed_parms);
5677 return error_mark_node;
5680 /* If we aren't able to check the actual arguments now
5681 (because they haven't been expanded yet), we can at least
5682 verify that all of the types used for the non-type
5683 template parameter pack are, in fact, valid for non-type
5684 template parameters. */
5686 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
5688 int j, len = TREE_VEC_LENGTH (packed_types);
5689 for (j = 0; j < len; ++j)
5691 tree t = TREE_VEC_ELT (packed_types, j);
5692 if (invalid_nontype_parm_type_p (t, complain))
5693 return error_mark_node;
5698 /* Convert the remaining arguments, which will be a part of the
5699 parameter pack "parm". */
5700 for (; arg_idx < nargs; ++arg_idx)
5702 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
5703 tree actual_parm = TREE_VALUE (parm);
5705 if (packed_types && !PACK_EXPANSION_P (arg))
5707 /* When we have a vector of types (corresponding to the
5708 non-type template parameter pack that uses parameter
5709 packs in its type, as mention above), and the
5710 argument is not an expansion (which expands to a
5711 currently unknown number of arguments), clone the
5712 parm and give it the next type in PACKED_TYPES. */
5713 actual_parm = copy_node (actual_parm);
5714 TREE_TYPE (actual_parm) =
5715 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
5718 if (arg != error_mark_node)
5719 arg = convert_template_argument (actual_parm,
5720 arg, new_args, complain, parm_idx,
5722 if (arg == error_mark_node)
5724 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
5727 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
5728 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
5729 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
5732 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
5733 TREE_TYPE (argument_pack)
5734 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
5735 TREE_CONSTANT (argument_pack) = 1;
5738 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
5739 return argument_pack;
5742 /* Convert all template arguments to their appropriate types, and
5743 return a vector containing the innermost resulting template
5744 arguments. If any error occurs, return error_mark_node. Error and
5745 warning messages are issued under control of COMPLAIN.
5747 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5748 for arguments not specified in ARGS. Otherwise, if
5749 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5750 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5751 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5755 coerce_template_parms (tree parms,
5758 tsubst_flags_t complain,
5759 bool require_all_args,
5760 bool use_default_args)
5762 int nparms, nargs, parm_idx, arg_idx, lost = 0;
5765 tree new_inner_args;
5766 int saved_unevaluated_operand;
5767 int saved_inhibit_evaluation_warnings;
5769 /* When used as a boolean value, indicates whether this is a
5770 variadic template parameter list. Since it's an int, we can also
5771 subtract it from nparms to get the number of non-variadic
5775 nparms = TREE_VEC_LENGTH (parms);
5777 /* Determine if there are any parameter packs. */
5778 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
5780 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
5781 if (template_parameter_pack_p (tparm))
5785 inner_args = INNERMOST_TEMPLATE_ARGS (args);
5786 /* If there are 0 or 1 parameter packs, we need to expand any argument
5787 packs so that we can deduce a parameter pack from some non-packed args
5788 followed by an argument pack, as in variadic85.C. If there are more
5789 than that, we need to leave argument packs intact so the arguments are
5790 assigned to the right parameter packs. This should only happen when
5791 dealing with a nested class inside a partial specialization of a class
5792 template, as in variadic92.C. */
5793 if (variadic_p <= 1)
5794 inner_args = expand_template_argument_pack (inner_args);
5796 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5797 if ((nargs > nparms && !variadic_p)
5798 || (nargs < nparms - variadic_p
5800 && (!use_default_args
5801 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
5802 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
5804 if (complain & tf_error)
5806 const char *or_more = "";
5809 or_more = " or more";
5813 error ("wrong number of template arguments (%d, should be %d%s)",
5814 nargs, nparms, or_more);
5817 error ("provided for %q+D", in_decl);
5820 return error_mark_node;
5823 /* We need to evaluate the template arguments, even though this
5824 template-id may be nested within a "sizeof". */
5825 saved_unevaluated_operand = cp_unevaluated_operand;
5826 cp_unevaluated_operand = 0;
5827 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
5828 c_inhibit_evaluation_warnings = 0;
5829 new_inner_args = make_tree_vec (nparms);
5830 new_args = add_outermost_template_args (args, new_inner_args);
5831 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
5836 /* Get the Ith template parameter. */
5837 parm = TREE_VEC_ELT (parms, parm_idx);
5839 if (parm == error_mark_node)
5841 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
5845 /* Calculate the next argument. */
5846 if (arg_idx < nargs)
5847 arg = TREE_VEC_ELT (inner_args, arg_idx);
5851 if (template_parameter_pack_p (TREE_VALUE (parm))
5852 && !(arg && ARGUMENT_PACK_P (arg)))
5854 /* All remaining arguments will be placed in the
5855 template parameter pack PARM. */
5856 arg = coerce_template_parameter_pack (parms, parm_idx, args,
5857 inner_args, arg_idx,
5861 /* Store this argument. */
5862 if (arg == error_mark_node)
5864 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
5866 /* We are done with all of the arguments. */
5873 if (PACK_EXPANSION_P (arg))
5875 if (complain & tf_error)
5877 /* FIXME this restriction was removed by N2555; see
5879 /* If ARG is a pack expansion, but PARM is not a
5880 template parameter pack (if it were, we would have
5881 handled it above), we're trying to expand into a
5882 fixed-length argument list. */
5883 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5884 sorry ("cannot expand %<%E%> into a fixed-length "
5885 "argument list", arg);
5887 sorry ("cannot expand %<%T%> into a fixed-length "
5888 "argument list", arg);
5890 return error_mark_node;
5893 else if (require_all_args)
5894 /* There must be a default arg in this case. */
5895 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
5900 if (arg == error_mark_node)
5902 if (complain & tf_error)
5903 error ("template argument %d is invalid", arg_idx + 1);
5906 /* This only occurs if there was an error in the template
5907 parameter list itself (which we would already have
5908 reported) that we are trying to recover from, e.g., a class
5909 template with a parameter list such as
5910 template<typename..., typename>. */
5911 return error_mark_node;
5913 arg = convert_template_argument (TREE_VALUE (parm),
5914 arg, new_args, complain,
5917 if (arg == error_mark_node)
5919 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
5921 cp_unevaluated_operand = saved_unevaluated_operand;
5922 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
5925 return error_mark_node;
5927 return new_inner_args;
5930 /* Returns 1 if template args OT and NT are equivalent. */
5933 template_args_equal (tree ot, tree nt)
5938 if (TREE_CODE (nt) == TREE_VEC)
5939 /* For member templates */
5940 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
5941 else if (PACK_EXPANSION_P (ot))
5942 return PACK_EXPANSION_P (nt)
5943 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
5944 PACK_EXPANSION_PATTERN (nt));
5945 else if (ARGUMENT_PACK_P (ot))
5950 if (!ARGUMENT_PACK_P (nt))
5953 opack = ARGUMENT_PACK_ARGS (ot);
5954 npack = ARGUMENT_PACK_ARGS (nt);
5955 len = TREE_VEC_LENGTH (opack);
5956 if (TREE_VEC_LENGTH (npack) != len)
5958 for (i = 0; i < len; ++i)
5959 if (!template_args_equal (TREE_VEC_ELT (opack, i),
5960 TREE_VEC_ELT (npack, i)))
5964 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
5966 /* We get here probably because we are in the middle of substituting
5967 into the pattern of a pack expansion. In that case the
5968 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
5969 interested in. So we want to use the initial pack argument for
5971 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
5972 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
5973 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
5974 return template_args_equal (ot, nt);
5976 else if (TYPE_P (nt))
5977 return TYPE_P (ot) && same_type_p (ot, nt);
5978 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
5981 return cp_tree_equal (ot, nt);
5984 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
5985 of template arguments. Returns 0 otherwise. */
5988 comp_template_args (tree oldargs, tree newargs)
5992 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
5995 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
5997 tree nt = TREE_VEC_ELT (newargs, i);
5998 tree ot = TREE_VEC_ELT (oldargs, i);
6000 if (! template_args_equal (ot, nt))
6007 add_pending_template (tree d)
6009 tree ti = (TYPE_P (d)
6010 ? CLASSTYPE_TEMPLATE_INFO (d)
6011 : DECL_TEMPLATE_INFO (d));
6012 struct pending_template *pt;
6015 if (TI_PENDING_TEMPLATE_FLAG (ti))
6018 /* We are called both from instantiate_decl, where we've already had a
6019 tinst_level pushed, and instantiate_template, where we haven't.
6021 level = !current_tinst_level || current_tinst_level->decl != d;
6024 push_tinst_level (d);
6026 pt = GGC_NEW (struct pending_template);
6028 pt->tinst = current_tinst_level;
6029 if (last_pending_template)
6030 last_pending_template->next = pt;
6032 pending_templates = pt;
6034 last_pending_template = pt;
6036 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6043 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6044 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6045 documentation for TEMPLATE_ID_EXPR. */
6048 lookup_template_function (tree fns, tree arglist)
6052 if (fns == error_mark_node || arglist == error_mark_node)
6053 return error_mark_node;
6055 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6056 gcc_assert (fns && (is_overloaded_fn (fns)
6057 || TREE_CODE (fns) == IDENTIFIER_NODE));
6059 if (BASELINK_P (fns))
6061 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6063 BASELINK_FUNCTIONS (fns),
6068 type = TREE_TYPE (fns);
6069 if (TREE_CODE (fns) == OVERLOAD || !type)
6070 type = unknown_type_node;
6072 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6075 /* Within the scope of a template class S<T>, the name S gets bound
6076 (in build_self_reference) to a TYPE_DECL for the class, not a
6077 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6078 or one of its enclosing classes, and that type is a template,
6079 return the associated TEMPLATE_DECL. Otherwise, the original
6082 Also handle the case when DECL is a TREE_LIST of ambiguous
6083 injected-class-names from different bases. */
6086 maybe_get_template_decl_from_type_decl (tree decl)
6088 if (decl == NULL_TREE)
6091 /* DR 176: A lookup that finds an injected-class-name (10.2
6092 [class.member.lookup]) can result in an ambiguity in certain cases
6093 (for example, if it is found in more than one base class). If all of
6094 the injected-class-names that are found refer to specializations of
6095 the same class template, and if the name is followed by a
6096 template-argument-list, the reference refers to the class template
6097 itself and not a specialization thereof, and is not ambiguous. */
6098 if (TREE_CODE (decl) == TREE_LIST)
6100 tree t, tmpl = NULL_TREE;
6101 for (t = decl; t; t = TREE_CHAIN (t))
6103 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6106 else if (tmpl != elt)
6109 if (tmpl && t == NULL_TREE)
6115 return (decl != NULL_TREE
6116 && DECL_SELF_REFERENCE_P (decl)
6117 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6118 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
6121 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6122 parameters, find the desired type.
6124 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6126 IN_DECL, if non-NULL, is the template declaration we are trying to
6129 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6130 the class we are looking up.
6132 Issue error and warning messages under control of COMPLAIN.
6134 If the template class is really a local class in a template
6135 function, then the FUNCTION_CONTEXT is the function in which it is
6138 ??? Note that this function is currently called *twice* for each
6139 template-id: the first time from the parser, while creating the
6140 incomplete type (finish_template_type), and the second type during the
6141 real instantiation (instantiate_template_class). This is surely something
6142 that we want to avoid. It also causes some problems with argument
6143 coercion (see convert_nontype_argument for more information on this). */
6146 lookup_template_class (tree d1,
6151 tsubst_flags_t complain)
6153 tree templ = NULL_TREE, parmlist;
6160 timevar_push (TV_NAME_LOOKUP);
6162 if (TREE_CODE (d1) == IDENTIFIER_NODE)
6164 tree value = innermost_non_namespace_value (d1);
6165 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
6170 push_decl_namespace (context);
6171 templ = lookup_name (d1);
6172 templ = maybe_get_template_decl_from_type_decl (templ);
6174 pop_decl_namespace ();
6177 context = DECL_CONTEXT (templ);
6179 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
6181 tree type = TREE_TYPE (d1);
6183 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6184 an implicit typename for the second A. Deal with it. */
6185 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
6186 type = TREE_TYPE (type);
6188 if (CLASSTYPE_TEMPLATE_INFO (type))
6190 templ = CLASSTYPE_TI_TEMPLATE (type);
6191 d1 = DECL_NAME (templ);
6194 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6195 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
6197 templ = TYPE_TI_TEMPLATE (d1);
6198 d1 = DECL_NAME (templ);
6200 else if (TREE_CODE (d1) == TEMPLATE_DECL
6201 && DECL_TEMPLATE_RESULT (d1)
6202 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
6205 d1 = DECL_NAME (templ);
6206 context = DECL_CONTEXT (templ);
6209 /* Issue an error message if we didn't find a template. */
6212 if (complain & tf_error)
6213 error ("%qT is not a template", d1);
6214 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6217 if (TREE_CODE (templ) != TEMPLATE_DECL
6218 /* Make sure it's a user visible template, if it was named by
6220 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
6221 && !PRIMARY_TEMPLATE_P (templ)))
6223 if (complain & tf_error)
6225 error ("non-template type %qT used as a template", d1);
6227 error ("for template declaration %q+D", in_decl);
6229 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6232 complain &= ~tf_user;
6234 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
6236 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6237 template arguments */
6243 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6245 /* Consider an example where a template template parameter declared as
6247 template <class T, class U = std::allocator<T> > class TT
6249 The template parameter level of T and U are one level larger than
6250 of TT. To proper process the default argument of U, say when an
6251 instantiation `TT<int>' is seen, we need to build the full
6252 arguments containing {int} as the innermost level. Outer levels,
6253 available when not appearing as default template argument, can be
6254 obtained from the arguments of the enclosing template.
6256 Suppose that TT is later substituted with std::vector. The above
6257 instantiation is `TT<int, std::allocator<T> >' with TT at
6258 level 1, and T at level 2, while the template arguments at level 1
6259 becomes {std::vector} and the inner level 2 is {int}. */
6261 outer = DECL_CONTEXT (templ);
6263 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6264 else if (current_template_parms)
6265 /* This is an argument of the current template, so we haven't set
6266 DECL_CONTEXT yet. */
6267 outer = current_template_args ();
6270 arglist = add_to_template_args (outer, arglist);
6272 arglist2 = coerce_template_parms (parmlist, arglist, templ,
6274 /*require_all_args=*/true,
6275 /*use_default_args=*/true);
6276 if (arglist2 == error_mark_node
6277 || (!uses_template_parms (arglist2)
6278 && check_instantiated_args (templ, arglist2, complain)))
6279 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6281 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
6282 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
6286 tree template_type = TREE_TYPE (templ);
6289 tree found = NULL_TREE;
6292 int is_partial_instantiation;
6294 gen_tmpl = most_general_template (templ);
6295 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
6296 parm_depth = TMPL_PARMS_DEPTH (parmlist);
6297 arg_depth = TMPL_ARGS_DEPTH (arglist);
6299 if (arg_depth == 1 && parm_depth > 1)
6301 /* We've been given an incomplete set of template arguments.
6304 template <class T> struct S1 {
6305 template <class U> struct S2 {};
6306 template <class U> struct S2<U*> {};
6309 we will be called with an ARGLIST of `U*', but the
6310 TEMPLATE will be `template <class T> template
6311 <class U> struct S1<T>::S2'. We must fill in the missing
6314 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
6316 arg_depth = TMPL_ARGS_DEPTH (arglist);
6319 /* Now we should have enough arguments. */
6320 gcc_assert (parm_depth == arg_depth);
6322 /* From here on, we're only interested in the most general
6325 /* Calculate the BOUND_ARGS. These will be the args that are
6326 actually tsubst'd into the definition to create the
6330 /* We have multiple levels of arguments to coerce, at once. */
6332 int saved_depth = TMPL_ARGS_DEPTH (arglist);
6334 tree bound_args = make_tree_vec (parm_depth);
6336 for (i = saved_depth,
6337 t = DECL_TEMPLATE_PARMS (gen_tmpl);
6338 i > 0 && t != NULL_TREE;
6339 --i, t = TREE_CHAIN (t))
6341 tree a = coerce_template_parms (TREE_VALUE (t),
6344 /*require_all_args=*/true,
6345 /*use_default_args=*/true);
6347 /* Don't process further if one of the levels fails. */
6348 if (a == error_mark_node)
6350 /* Restore the ARGLIST to its full size. */
6351 TREE_VEC_LENGTH (arglist) = saved_depth;
6352 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6355 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
6357 /* We temporarily reduce the length of the ARGLIST so
6358 that coerce_template_parms will see only the arguments
6359 corresponding to the template parameters it is
6361 TREE_VEC_LENGTH (arglist)--;
6364 /* Restore the ARGLIST to its full size. */
6365 TREE_VEC_LENGTH (arglist) = saved_depth;
6367 arglist = bound_args;
6371 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
6372 INNERMOST_TEMPLATE_ARGS (arglist),
6375 /*require_all_args=*/true,
6376 /*use_default_args=*/true);
6378 if (arglist == error_mark_node)
6379 /* We were unable to bind the arguments. */
6380 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6382 /* In the scope of a template class, explicit references to the
6383 template class refer to the type of the template, not any
6384 instantiation of it. For example, in:
6386 template <class T> class C { void f(C<T>); }
6388 the `C<T>' is just the same as `C'. Outside of the
6389 class, however, such a reference is an instantiation. */
6391 || !PRIMARY_TEMPLATE_P (gen_tmpl)
6392 || currently_open_class (template_type))
6393 /* comp_template_args is expensive, check it last. */
6394 && comp_template_args (TYPE_TI_ARGS (template_type),
6396 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type);
6398 /* If we already have this specialization, return it. */
6399 elt.tmpl = gen_tmpl;
6401 hash = hash_specialization (&elt);
6402 entry = (spec_entry *) htab_find_with_hash (type_specializations,
6406 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec);
6408 /* This type is a "partial instantiation" if any of the template
6409 arguments still involve template parameters. Note that we set
6410 IS_PARTIAL_INSTANTIATION for partial specializations as
6412 is_partial_instantiation = uses_template_parms (arglist);
6414 /* If the deduced arguments are invalid, then the binding
6416 if (!is_partial_instantiation
6417 && check_instantiated_args (gen_tmpl,
6418 INNERMOST_TEMPLATE_ARGS (arglist),
6420 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6422 if (!is_partial_instantiation
6423 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6424 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
6425 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
6427 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
6428 DECL_NAME (gen_tmpl),
6429 /*tag_scope=*/ts_global);
6430 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
6433 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
6436 context = global_namespace;
6438 /* Create the type. */
6439 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
6441 if (!is_partial_instantiation)
6443 set_current_access_from_decl (TYPE_NAME (template_type));
6444 t = start_enum (TYPE_IDENTIFIER (template_type),
6445 tsubst (ENUM_UNDERLYING_TYPE (template_type),
6446 arglist, complain, in_decl),
6447 SCOPED_ENUM_P (template_type));
6451 /* We don't want to call start_enum for this type, since
6452 the values for the enumeration constants may involve
6453 template parameters. And, no one should be interested
6454 in the enumeration constants for such a type. */
6455 t = cxx_make_type (ENUMERAL_TYPE);
6456 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
6461 t = make_class_type (TREE_CODE (template_type));
6462 CLASSTYPE_DECLARED_CLASS (t)
6463 = CLASSTYPE_DECLARED_CLASS (template_type);
6464 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
6465 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
6467 /* A local class. Make sure the decl gets registered properly. */
6468 if (context == current_function_decl)
6469 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
6471 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
6472 /* This instantiation is another name for the primary
6473 template type. Set the TYPE_CANONICAL field
6475 TYPE_CANONICAL (t) = template_type;
6476 else if (any_template_arguments_need_structural_equality_p (arglist))
6477 /* Some of the template arguments require structural
6478 equality testing, so this template class requires
6479 structural equality testing. */
6480 SET_TYPE_STRUCTURAL_EQUALITY (t);
6483 /* If we called start_enum or pushtag above, this information
6484 will already be set up. */
6487 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
6489 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
6490 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
6491 DECL_SOURCE_LOCATION (type_decl)
6492 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
6495 type_decl = TYPE_NAME (t);
6497 TREE_PRIVATE (type_decl)
6498 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
6499 TREE_PROTECTED (type_decl)
6500 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
6501 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
6503 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
6504 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
6507 /* Set up the template information. We have to figure out which
6508 template is the immediate parent if this is a full
6510 if (parm_depth == 1 || is_partial_instantiation
6511 || !PRIMARY_TEMPLATE_P (gen_tmpl))
6512 /* This case is easy; there are no member templates involved. */
6516 /* This is a full instantiation of a member template. Find
6517 the partial instantiation of which this is an instance. */
6519 /* Temporarily reduce by one the number of levels in the ARGLIST
6520 so as to avoid comparing the last set of arguments. */
6521 TREE_VEC_LENGTH (arglist)--;
6522 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
6523 TREE_VEC_LENGTH (arglist)++;
6524 found = CLASSTYPE_TI_TEMPLATE (found);
6527 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
6530 slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
6531 &elt, hash, INSERT);
6532 *slot = GGC_NEW (spec_entry);
6535 /* Note this use of the partial instantiation so we can check it
6536 later in maybe_process_partial_specialization. */
6537 DECL_TEMPLATE_INSTANTIATIONS (templ)
6538 = tree_cons (arglist, t,
6539 DECL_TEMPLATE_INSTANTIATIONS (templ));
6541 if (TREE_CODE (t) == ENUMERAL_TYPE
6542 && !is_partial_instantiation)
6543 /* Now that the type has been registered on the instantiations
6544 list, we set up the enumerators. Because the enumeration
6545 constants may involve the enumeration type itself, we make
6546 sure to register the type first, and then create the
6547 constants. That way, doing tsubst_expr for the enumeration
6548 constants won't result in recursive calls here; we'll find
6549 the instantiation and exit above. */
6550 tsubst_enum (template_type, t, arglist);
6552 if (is_partial_instantiation)
6553 /* If the type makes use of template parameters, the
6554 code that generates debugging information will crash. */
6555 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
6557 /* Possibly limit visibility based on template args. */
6558 TREE_PUBLIC (type_decl) = 1;
6559 determine_visibility (type_decl);
6561 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6563 timevar_pop (TV_NAME_LOOKUP);
6570 /* True when we should also visit template parameters that occur in
6571 non-deduced contexts. */
6572 bool include_nondeduced_p;
6573 struct pointer_set_t *visited;
6576 /* Called from for_each_template_parm via walk_tree. */
6579 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
6582 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
6583 tree_fn_t fn = pfd->fn;
6584 void *data = pfd->data;
6587 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
6588 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
6589 pfd->include_nondeduced_p))
6590 return error_mark_node;
6592 switch (TREE_CODE (t))
6595 if (TYPE_PTRMEMFUNC_P (t))
6601 if (!TYPE_TEMPLATE_INFO (t))
6603 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
6604 fn, data, pfd->visited,
6605 pfd->include_nondeduced_p))
6606 return error_mark_node;
6610 if (for_each_template_parm (TYPE_MIN_VALUE (t),
6611 fn, data, pfd->visited,
6612 pfd->include_nondeduced_p)
6613 || for_each_template_parm (TYPE_MAX_VALUE (t),
6614 fn, data, pfd->visited,
6615 pfd->include_nondeduced_p))
6616 return error_mark_node;
6620 /* Since we're not going to walk subtrees, we have to do this
6622 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
6623 pfd->visited, pfd->include_nondeduced_p))
6624 return error_mark_node;
6628 /* Check the return type. */
6629 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6630 pfd->include_nondeduced_p))
6631 return error_mark_node;
6633 /* Check the parameter types. Since default arguments are not
6634 instantiated until they are needed, the TYPE_ARG_TYPES may
6635 contain expressions that involve template parameters. But,
6636 no-one should be looking at them yet. And, once they're
6637 instantiated, they don't contain template parameters, so
6638 there's no point in looking at them then, either. */
6642 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
6643 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
6644 pfd->visited, pfd->include_nondeduced_p))
6645 return error_mark_node;
6647 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6648 want walk_tree walking into them itself. */
6654 if (pfd->include_nondeduced_p
6655 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
6657 pfd->include_nondeduced_p))
6658 return error_mark_node;
6663 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
6664 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
6665 pfd->visited, pfd->include_nondeduced_p))
6666 return error_mark_node;
6671 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
6672 && for_each_template_parm (DECL_INITIAL (t), fn, data,
6673 pfd->visited, pfd->include_nondeduced_p))
6674 return error_mark_node;
6675 if (DECL_CONTEXT (t)
6676 && pfd->include_nondeduced_p
6677 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
6678 pfd->visited, pfd->include_nondeduced_p))
6679 return error_mark_node;
6682 case BOUND_TEMPLATE_TEMPLATE_PARM:
6683 /* Record template parameters such as `T' inside `TT<T>'. */
6684 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
6685 pfd->include_nondeduced_p))
6686 return error_mark_node;
6689 case TEMPLATE_TEMPLATE_PARM:
6690 case TEMPLATE_TYPE_PARM:
6691 case TEMPLATE_PARM_INDEX:
6692 if (fn && (*fn)(t, data))
6693 return error_mark_node;
6695 return error_mark_node;
6699 /* A template template parameter is encountered. */
6700 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
6701 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6702 pfd->include_nondeduced_p))
6703 return error_mark_node;
6705 /* Already substituted template template parameter */
6711 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
6713 pfd->include_nondeduced_p))
6714 return error_mark_node;
6718 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
6719 && pfd->include_nondeduced_p
6720 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
6721 (TREE_TYPE (t)), fn, data,
6722 pfd->visited, pfd->include_nondeduced_p))
6723 return error_mark_node;
6728 /* If there's no type, then this thing must be some expression
6729 involving template parameters. */
6730 if (!fn && !TREE_TYPE (t))
6731 return error_mark_node;
6736 case REINTERPRET_CAST_EXPR:
6737 case CONST_CAST_EXPR:
6738 case STATIC_CAST_EXPR:
6739 case DYNAMIC_CAST_EXPR:
6743 case PSEUDO_DTOR_EXPR:
6745 return error_mark_node;
6752 /* We didn't find any template parameters we liked. */
6756 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
6757 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
6758 call FN with the parameter and the DATA.
6759 If FN returns nonzero, the iteration is terminated, and
6760 for_each_template_parm returns 1. Otherwise, the iteration
6761 continues. If FN never returns a nonzero value, the value
6762 returned by for_each_template_parm is 0. If FN is NULL, it is
6763 considered to be the function which always returns 1.
6765 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
6766 parameters that occur in non-deduced contexts. When false, only
6767 visits those template parameters that can be deduced. */
6770 for_each_template_parm (tree t, tree_fn_t fn, void* data,
6771 struct pointer_set_t *visited,
6772 bool include_nondeduced_p)
6774 struct pair_fn_data pfd;
6780 pfd.include_nondeduced_p = include_nondeduced_p;
6782 /* Walk the tree. (Conceptually, we would like to walk without
6783 duplicates, but for_each_template_parm_r recursively calls
6784 for_each_template_parm, so we would need to reorganize a fair
6785 bit to use walk_tree_without_duplicates, so we keep our own
6788 pfd.visited = visited;
6790 pfd.visited = pointer_set_create ();
6791 result = cp_walk_tree (&t,
6792 for_each_template_parm_r,
6794 pfd.visited) != NULL_TREE;
6799 pointer_set_destroy (pfd.visited);
6806 /* Returns true if T depends on any template parameter. */
6809 uses_template_parms (tree t)
6812 int saved_processing_template_decl;
6814 saved_processing_template_decl = processing_template_decl;
6815 if (!saved_processing_template_decl)
6816 processing_template_decl = 1;
6818 dependent_p = dependent_type_p (t);
6819 else if (TREE_CODE (t) == TREE_VEC)
6820 dependent_p = any_dependent_template_arguments_p (t);
6821 else if (TREE_CODE (t) == TREE_LIST)
6822 dependent_p = (uses_template_parms (TREE_VALUE (t))
6823 || uses_template_parms (TREE_CHAIN (t)));
6824 else if (TREE_CODE (t) == TYPE_DECL)
6825 dependent_p = dependent_type_p (TREE_TYPE (t));
6828 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
6829 || TREE_CODE (t) == OVERLOAD
6830 || TREE_CODE (t) == BASELINK
6831 || TREE_CODE (t) == IDENTIFIER_NODE
6832 || TREE_CODE (t) == TRAIT_EXPR
6833 || TREE_CODE (t) == CONSTRUCTOR
6834 || CONSTANT_CLASS_P (t))
6835 dependent_p = (type_dependent_expression_p (t)
6836 || value_dependent_expression_p (t));
6839 gcc_assert (t == error_mark_node);
6840 dependent_p = false;
6843 processing_template_decl = saved_processing_template_decl;
6848 /* Returns true if T depends on any template parameter with level LEVEL. */
6851 uses_template_parms_level (tree t, int level)
6853 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
6854 /*include_nondeduced_p=*/true);
6857 static int tinst_depth;
6858 extern int max_tinst_depth;
6859 #ifdef GATHER_STATISTICS
6862 static int tinst_level_tick;
6863 static int last_template_error_tick;
6865 /* We're starting to instantiate D; record the template instantiation context
6866 for diagnostics and to restore it later. */
6869 push_tinst_level (tree d)
6871 struct tinst_level *new_level;
6873 if (tinst_depth >= max_tinst_depth)
6875 /* If the instantiation in question still has unbound template parms,
6876 we don't really care if we can't instantiate it, so just return.
6877 This happens with base instantiation for implicit `typename'. */
6878 if (uses_template_parms (d))
6881 last_template_error_tick = tinst_level_tick;
6882 error ("template instantiation depth exceeds maximum of %d (use "
6883 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
6884 max_tinst_depth, d);
6886 print_instantiation_context ();
6891 new_level = GGC_NEW (struct tinst_level);
6892 new_level->decl = d;
6893 new_level->locus = input_location;
6894 new_level->in_system_header_p = in_system_header;
6895 new_level->next = current_tinst_level;
6896 current_tinst_level = new_level;
6899 #ifdef GATHER_STATISTICS
6900 if (tinst_depth > depth_reached)
6901 depth_reached = tinst_depth;
6908 /* We're done instantiating this template; return to the instantiation
6912 pop_tinst_level (void)
6914 /* Restore the filename and line number stashed away when we started
6915 this instantiation. */
6916 input_location = current_tinst_level->locus;
6917 current_tinst_level = current_tinst_level->next;
6922 /* We're instantiating a deferred template; restore the template
6923 instantiation context in which the instantiation was requested, which
6924 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
6927 reopen_tinst_level (struct tinst_level *level)
6929 struct tinst_level *t;
6932 for (t = level; t; t = t->next)
6935 current_tinst_level = level;
6940 /* Returns the TINST_LEVEL which gives the original instantiation
6943 struct tinst_level *
6944 outermost_tinst_level (void)
6946 struct tinst_level *level = current_tinst_level;
6949 level = level->next;
6953 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
6956 parameter_of_template_p (tree parm, tree templ)
6961 if (!parm || !templ)
6964 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
6965 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
6967 parms = DECL_TEMPLATE_PARMS (templ);
6968 parms = INNERMOST_TEMPLATE_PARMS (parms);
6970 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
6971 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
6977 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
6978 vector of template arguments, as for tsubst.
6980 Returns an appropriate tsubst'd friend declaration. */
6983 tsubst_friend_function (tree decl, tree args)
6987 if (TREE_CODE (decl) == FUNCTION_DECL
6988 && DECL_TEMPLATE_INSTANTIATION (decl)
6989 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
6990 /* This was a friend declared with an explicit template
6991 argument list, e.g.:
6995 to indicate that f was a template instantiation, not a new
6996 function declaration. Now, we have to figure out what
6997 instantiation of what template. */
6999 tree template_id, arglist, fns;
7002 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
7004 /* Friend functions are looked up in the containing namespace scope.
7005 We must enter that scope, to avoid finding member functions of the
7006 current class with same name. */
7007 push_nested_namespace (ns);
7008 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
7009 tf_warning_or_error, NULL_TREE,
7010 /*integral_constant_expression_p=*/false);
7011 pop_nested_namespace (ns);
7012 arglist = tsubst (DECL_TI_ARGS (decl), args,
7013 tf_warning_or_error, NULL_TREE);
7014 template_id = lookup_template_function (fns, arglist);
7016 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7017 tmpl = determine_specialization (template_id, new_friend,
7019 /*need_member_template=*/0,
7020 TREE_VEC_LENGTH (args),
7022 return instantiate_template (tmpl, new_args, tf_error);
7025 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7027 /* The NEW_FRIEND will look like an instantiation, to the
7028 compiler, but is not an instantiation from the point of view of
7029 the language. For example, we might have had:
7031 template <class T> struct S {
7032 template <class U> friend void f(T, U);
7035 Then, in S<int>, template <class U> void f(int, U) is not an
7036 instantiation of anything. */
7037 if (new_friend == error_mark_node)
7038 return error_mark_node;
7040 DECL_USE_TEMPLATE (new_friend) = 0;
7041 if (TREE_CODE (decl) == TEMPLATE_DECL)
7043 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
7044 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
7045 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
7048 /* The mangled name for the NEW_FRIEND is incorrect. The function
7049 is not a template instantiation and should not be mangled like
7050 one. Therefore, we forget the mangling here; we'll recompute it
7051 later if we need it. */
7052 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
7054 SET_DECL_RTL (new_friend, NULL_RTX);
7055 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
7058 if (DECL_NAMESPACE_SCOPE_P (new_friend))
7061 tree new_friend_template_info;
7062 tree new_friend_result_template_info;
7064 int new_friend_is_defn;
7066 /* We must save some information from NEW_FRIEND before calling
7067 duplicate decls since that function will free NEW_FRIEND if
7069 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
7070 new_friend_is_defn =
7071 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7072 (template_for_substitution (new_friend)))
7074 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
7076 /* This declaration is a `primary' template. */
7077 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
7079 new_friend_result_template_info
7080 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
7083 new_friend_result_template_info = NULL_TREE;
7085 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7086 if (new_friend_is_defn)
7087 DECL_INITIAL (new_friend) = error_mark_node;
7089 /* Inside pushdecl_namespace_level, we will push into the
7090 current namespace. However, the friend function should go
7091 into the namespace of the template. */
7092 ns = decl_namespace_context (new_friend);
7093 push_nested_namespace (ns);
7094 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
7095 pop_nested_namespace (ns);
7097 if (old_decl == error_mark_node)
7098 return error_mark_node;
7100 if (old_decl != new_friend)
7102 /* This new friend declaration matched an existing
7103 declaration. For example, given:
7105 template <class T> void f(T);
7106 template <class U> class C {
7107 template <class T> friend void f(T) {}
7110 the friend declaration actually provides the definition
7111 of `f', once C has been instantiated for some type. So,
7112 old_decl will be the out-of-class template declaration,
7113 while new_friend is the in-class definition.
7115 But, if `f' was called before this point, the
7116 instantiation of `f' will have DECL_TI_ARGS corresponding
7117 to `T' but not to `U', references to which might appear
7118 in the definition of `f'. Previously, the most general
7119 template for an instantiation of `f' was the out-of-class
7120 version; now it is the in-class version. Therefore, we
7121 run through all specialization of `f', adding to their
7122 DECL_TI_ARGS appropriately. In particular, they need a
7123 new set of outer arguments, corresponding to the
7124 arguments for this class instantiation.
7126 The same situation can arise with something like this:
7129 template <class T> class C {
7133 when `C<int>' is instantiated. Now, `f(int)' is defined
7136 if (!new_friend_is_defn)
7137 /* On the other hand, if the in-class declaration does
7138 *not* provide a definition, then we don't want to alter
7139 existing definitions. We can just leave everything
7144 tree new_template = TI_TEMPLATE (new_friend_template_info);
7145 tree new_args = TI_ARGS (new_friend_template_info);
7147 /* Overwrite whatever template info was there before, if
7148 any, with the new template information pertaining to
7150 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
7152 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
7153 /* We should have called reregister_specialization in
7155 gcc_assert (retrieve_specialization (new_template,
7162 /* Indicate that the old function template is a partial
7164 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
7165 = new_friend_result_template_info;
7167 gcc_assert (new_template
7168 == most_general_template (new_template));
7169 gcc_assert (new_template != old_decl);
7171 /* Reassign any specializations already in the hash table
7172 to the new more general template, and add the
7173 additional template args. */
7174 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
7178 tree spec = TREE_VALUE (t);
7181 elt.tmpl = old_decl;
7182 elt.args = DECL_TI_ARGS (spec);
7183 elt.spec = NULL_TREE;
7185 htab_remove_elt (decl_specializations, &elt);
7188 = add_outermost_template_args (new_args,
7189 DECL_TI_ARGS (spec));
7191 register_specialization
7192 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
7195 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
7199 /* The information from NEW_FRIEND has been merged into OLD_DECL
7200 by duplicate_decls. */
7201 new_friend = old_decl;
7206 tree context = DECL_CONTEXT (new_friend);
7210 template <class T> class C {
7211 template <class U> friend void C1<U>::f (); // case 1
7212 friend void C2<T>::f (); // case 2
7214 we only need to make sure CONTEXT is a complete type for
7215 case 2. To distinguish between the two cases, we note that
7216 CONTEXT of case 1 remains dependent type after tsubst while
7217 this isn't true for case 2. */
7218 ++processing_template_decl;
7219 dependent_p = dependent_type_p (context);
7220 --processing_template_decl;
7223 && !complete_type_or_else (context, NULL_TREE))
7224 return error_mark_node;
7226 if (COMPLETE_TYPE_P (context))
7228 /* Check to see that the declaration is really present, and,
7229 possibly obtain an improved declaration. */
7230 tree fn = check_classfn (context,
7231 new_friend, NULL_TREE);
7241 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7242 template arguments, as for tsubst.
7244 Returns an appropriate tsubst'd friend type or error_mark_node on
7248 tsubst_friend_class (tree friend_tmpl, tree args)
7254 context = DECL_CONTEXT (friend_tmpl);
7258 if (TREE_CODE (context) == NAMESPACE_DECL)
7259 push_nested_namespace (context);
7261 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
7264 /* Look for a class template declaration. We look for hidden names
7265 because two friend declarations of the same template are the
7266 same. For example, in:
7269 template <typename> friend class F;
7271 template <typename> struct B {
7272 template <typename> friend class F;
7275 both F templates are the same. */
7276 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
7277 /*block_p=*/true, 0,
7278 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
7280 /* But, if we don't find one, it might be because we're in a
7281 situation like this:
7289 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7290 for `S<int>', not the TEMPLATE_DECL. */
7291 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
7293 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
7294 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
7297 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
7299 /* The friend template has already been declared. Just
7300 check to see that the declarations match, and install any new
7301 default parameters. We must tsubst the default parameters,
7302 of course. We only need the innermost template parameters
7303 because that is all that redeclare_class_template will look
7305 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
7306 > TMPL_ARGS_DEPTH (args))
7309 location_t saved_input_location;
7310 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
7311 args, tf_warning_or_error);
7313 saved_input_location = input_location;
7314 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
7315 redeclare_class_template (TREE_TYPE (tmpl), parms);
7316 input_location = saved_input_location;
7320 friend_type = TREE_TYPE (tmpl);
7324 /* The friend template has not already been declared. In this
7325 case, the instantiation of the template class will cause the
7326 injection of this template into the global scope. */
7327 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
7328 if (tmpl == error_mark_node)
7329 return error_mark_node;
7331 /* The new TMPL is not an instantiation of anything, so we
7332 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7333 the new type because that is supposed to be the corresponding
7334 template decl, i.e., TMPL. */
7335 DECL_USE_TEMPLATE (tmpl) = 0;
7336 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
7337 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
7338 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
7339 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
7341 /* Inject this template into the global scope. */
7342 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
7347 if (TREE_CODE (context) == NAMESPACE_DECL)
7348 pop_nested_namespace (context);
7350 pop_nested_class ();
7356 /* Returns zero if TYPE cannot be completed later due to circularity.
7357 Otherwise returns one. */
7360 can_complete_type_without_circularity (tree type)
7362 if (type == NULL_TREE || type == error_mark_node)
7364 else if (COMPLETE_TYPE_P (type))
7366 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
7367 return can_complete_type_without_circularity (TREE_TYPE (type));
7368 else if (CLASS_TYPE_P (type)
7369 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
7375 /* Apply any attributes which had to be deferred until instantiation
7376 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7377 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7380 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
7381 tree args, tsubst_flags_t complain, tree in_decl)
7383 tree last_dep = NULL_TREE;
7387 for (t = attributes; t; t = TREE_CHAIN (t))
7388 if (ATTR_IS_DEPENDENT (t))
7391 attributes = copy_list (attributes);
7395 if (DECL_P (*decl_p))
7397 if (TREE_TYPE (*decl_p) == error_mark_node)
7399 p = &DECL_ATTRIBUTES (*decl_p);
7402 p = &TYPE_ATTRIBUTES (*decl_p);
7406 tree late_attrs = NULL_TREE;
7407 tree *q = &late_attrs;
7409 for (*p = attributes; *p; )
7412 if (ATTR_IS_DEPENDENT (t))
7414 *p = TREE_CHAIN (t);
7415 TREE_CHAIN (t) = NULL_TREE;
7416 /* If the first attribute argument is an identifier, don't
7417 pass it through tsubst. Attributes like mode, format,
7418 cleanup and several target specific attributes expect it
7421 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
7422 && TREE_VALUE (TREE_VALUE (t))
7423 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
7424 == IDENTIFIER_NODE))
7427 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
7429 /*integral_constant_expression_p=*/false);
7430 if (chain != TREE_CHAIN (TREE_VALUE (t)))
7432 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
7437 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
7438 /*integral_constant_expression_p=*/false);
7440 q = &TREE_CHAIN (t);
7443 p = &TREE_CHAIN (t);
7446 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
7450 /* Perform (or defer) access check for typedefs that were referenced
7451 from within the template TMPL code.
7452 This is a subroutine of instantiate_template and instantiate_class_template.
7453 TMPL is the template to consider and TARGS is the list of arguments of
7457 perform_typedefs_access_check (tree tmpl, tree targs)
7459 location_t saved_location;
7461 qualified_typedef_usage_t *iter;
7464 || (!CLASS_TYPE_P (tmpl)
7465 && TREE_CODE (tmpl) != FUNCTION_DECL))
7468 saved_location = input_location;
7470 VEC_iterate (qualified_typedef_usage_t,
7471 get_types_needing_access_check (tmpl),
7475 tree type_decl = iter->typedef_decl;
7476 tree type_scope = iter->context;
7478 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
7481 if (uses_template_parms (type_decl))
7482 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
7483 if (uses_template_parms (type_scope))
7484 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
7486 /* Make access check error messages point to the location
7487 of the use of the typedef. */
7488 input_location = iter->locus;
7489 perform_or_defer_access_check (TYPE_BINFO (type_scope),
7490 type_decl, type_decl);
7492 input_location = saved_location;
7496 instantiate_class_template (tree type)
7498 tree templ, args, pattern, t, member;
7502 unsigned int saved_maximum_field_alignment;
7504 if (type == error_mark_node)
7505 return error_mark_node;
7507 if (TYPE_BEING_DEFINED (type)
7508 || COMPLETE_TYPE_P (type)
7509 || uses_template_parms (type))
7512 /* Figure out which template is being instantiated. */
7513 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
7514 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7516 /* Determine what specialization of the original template to
7518 t = most_specialized_class (type, templ);
7519 if (t == error_mark_node)
7521 TYPE_BEING_DEFINED (type) = 1;
7522 return error_mark_node;
7526 /* This TYPE is actually an instantiation of a partial
7527 specialization. We replace the innermost set of ARGS with
7528 the arguments appropriate for substitution. For example,
7531 template <class T> struct S {};
7532 template <class T> struct S<T*> {};
7534 and supposing that we are instantiating S<int*>, ARGS will
7535 presently be {int*} -- but we need {int}. */
7536 pattern = TREE_TYPE (t);
7537 args = TREE_PURPOSE (t);
7541 pattern = TREE_TYPE (templ);
7542 args = CLASSTYPE_TI_ARGS (type);
7545 /* If the template we're instantiating is incomplete, then clearly
7546 there's nothing we can do. */
7547 if (!COMPLETE_TYPE_P (pattern))
7550 /* If we've recursively instantiated too many templates, stop. */
7551 if (! push_tinst_level (type))
7554 /* Now we're really doing the instantiation. Mark the type as in
7555 the process of being defined. */
7556 TYPE_BEING_DEFINED (type) = 1;
7558 /* We may be in the middle of deferred access check. Disable
7560 push_deferring_access_checks (dk_no_deferred);
7562 push_to_top_level ();
7563 /* Use #pragma pack from the template context. */
7564 saved_maximum_field_alignment = maximum_field_alignment;
7565 maximum_field_alignment = TYPE_PRECISION (pattern);
7567 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
7569 /* Set the input location to the most specialized template definition.
7570 This is needed if tsubsting causes an error. */
7571 typedecl = TYPE_MAIN_DECL (pattern);
7572 input_location = DECL_SOURCE_LOCATION (typedecl);
7574 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
7575 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
7576 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
7577 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
7578 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
7579 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
7580 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
7581 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
7582 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
7583 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
7584 TYPE_PACKED (type) = TYPE_PACKED (pattern);
7585 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
7586 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
7587 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
7588 if (ANON_AGGR_TYPE_P (pattern))
7589 SET_ANON_AGGR_TYPE_P (type);
7590 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
7592 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
7593 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
7596 pbinfo = TYPE_BINFO (pattern);
7598 /* We should never instantiate a nested class before its enclosing
7599 class; we need to look up the nested class by name before we can
7600 instantiate it, and that lookup should instantiate the enclosing
7602 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
7603 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
7604 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
7606 base_list = NULL_TREE;
7607 if (BINFO_N_BASE_BINFOS (pbinfo))
7610 tree context = TYPE_CONTEXT (type);
7614 /* We must enter the scope containing the type, as that is where
7615 the accessibility of types named in dependent bases are
7617 pushed_scope = push_scope (context ? context : global_namespace);
7619 /* Substitute into each of the bases to determine the actual
7621 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
7624 tree access = BINFO_BASE_ACCESS (pbinfo, i);
7625 tree expanded_bases = NULL_TREE;
7628 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
7631 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
7632 args, tf_error, NULL_TREE);
7633 if (expanded_bases == error_mark_node)
7636 len = TREE_VEC_LENGTH (expanded_bases);
7639 for (idx = 0; idx < len; idx++)
7642 /* Extract the already-expanded base class. */
7643 base = TREE_VEC_ELT (expanded_bases, idx);
7645 /* Substitute to figure out the base class. */
7646 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
7649 if (base == error_mark_node)
7652 base_list = tree_cons (access, base, base_list);
7653 if (BINFO_VIRTUAL_P (pbase_binfo))
7654 TREE_TYPE (base_list) = integer_type_node;
7658 /* The list is now in reverse order; correct that. */
7659 base_list = nreverse (base_list);
7662 pop_scope (pushed_scope);
7664 /* Now call xref_basetypes to set up all the base-class
7666 xref_basetypes (type, base_list);
7668 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
7669 (int) ATTR_FLAG_TYPE_IN_PLACE,
7670 args, tf_error, NULL_TREE);
7672 /* Now that our base classes are set up, enter the scope of the
7673 class, so that name lookups into base classes, etc. will work
7674 correctly. This is precisely analogous to what we do in
7675 begin_class_definition when defining an ordinary non-template
7676 class, except we also need to push the enclosing classes. */
7677 push_nested_class (type);
7679 /* Now members are processed in the order of declaration. */
7680 for (member = CLASSTYPE_DECL_LIST (pattern);
7681 member; member = TREE_CHAIN (member))
7683 tree t = TREE_VALUE (member);
7685 if (TREE_PURPOSE (member))
7689 /* Build new CLASSTYPE_NESTED_UTDS. */
7692 bool class_template_p;
7694 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
7695 && TYPE_LANG_SPECIFIC (t)
7696 && CLASSTYPE_IS_TEMPLATE (t));
7697 /* If the member is a class template, then -- even after
7698 substitution -- there may be dependent types in the
7699 template argument list for the class. We increment
7700 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7701 that function will assume that no types are dependent
7702 when outside of a template. */
7703 if (class_template_p)
7704 ++processing_template_decl;
7705 newtag = tsubst (t, args, tf_error, NULL_TREE);
7706 if (class_template_p)
7707 --processing_template_decl;
7708 if (newtag == error_mark_node)
7711 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
7713 tree name = TYPE_IDENTIFIER (t);
7715 if (class_template_p)
7716 /* Unfortunately, lookup_template_class sets
7717 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
7718 instantiation (i.e., for the type of a member
7719 template class nested within a template class.)
7720 This behavior is required for
7721 maybe_process_partial_specialization to work
7722 correctly, but is not accurate in this case;
7723 the TAG is not an instantiation of anything.
7724 (The corresponding TEMPLATE_DECL is an
7725 instantiation, but the TYPE is not.) */
7726 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
7728 /* Now, we call pushtag to put this NEWTAG into the scope of
7729 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
7730 pushtag calling push_template_decl. We don't have to do
7731 this for enums because it will already have been done in
7734 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
7735 pushtag (name, newtag, /*tag_scope=*/ts_current);
7738 else if (TREE_CODE (t) == FUNCTION_DECL
7739 || DECL_FUNCTION_TEMPLATE_P (t))
7741 /* Build new TYPE_METHODS. */
7744 if (TREE_CODE (t) == TEMPLATE_DECL)
7745 ++processing_template_decl;
7746 r = tsubst (t, args, tf_error, NULL_TREE);
7747 if (TREE_CODE (t) == TEMPLATE_DECL)
7748 --processing_template_decl;
7749 set_current_access_from_decl (r);
7750 finish_member_declaration (r);
7754 /* Build new TYPE_FIELDS. */
7755 if (TREE_CODE (t) == STATIC_ASSERT)
7758 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
7759 tf_warning_or_error, NULL_TREE,
7760 /*integral_constant_expression_p=*/true);
7761 finish_static_assert (condition,
7762 STATIC_ASSERT_MESSAGE (t),
7763 STATIC_ASSERT_SOURCE_LOCATION (t),
7766 else if (TREE_CODE (t) != CONST_DECL)
7770 /* The file and line for this declaration, to
7771 assist in error message reporting. Since we
7772 called push_tinst_level above, we don't need to
7774 input_location = DECL_SOURCE_LOCATION (t);
7776 if (TREE_CODE (t) == TEMPLATE_DECL)
7777 ++processing_template_decl;
7778 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
7779 if (TREE_CODE (t) == TEMPLATE_DECL)
7780 --processing_template_decl;
7781 if (TREE_CODE (r) == VAR_DECL)
7785 [t]he initialization (and any associated
7786 side-effects) of a static data member does
7787 not occur unless the static data member is
7788 itself used in a way that requires the
7789 definition of the static data member to
7792 Therefore, we do not substitute into the
7793 initialized for the static data member here. */
7794 finish_static_data_member_decl
7797 /*init_const_expr_p=*/false,
7798 /*asmspec_tree=*/NULL_TREE,
7800 if (DECL_INITIALIZED_IN_CLASS_P (r))
7801 check_static_variable_definition (r, TREE_TYPE (r));
7803 else if (TREE_CODE (r) == FIELD_DECL)
7805 /* Determine whether R has a valid type and can be
7806 completed later. If R is invalid, then it is
7807 replaced by error_mark_node so that it will not be
7808 added to TYPE_FIELDS. */
7809 tree rtype = TREE_TYPE (r);
7810 if (can_complete_type_without_circularity (rtype))
7811 complete_type (rtype);
7813 if (!COMPLETE_TYPE_P (rtype))
7815 cxx_incomplete_type_error (r, rtype);
7816 r = error_mark_node;
7820 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
7821 such a thing will already have been added to the field
7822 list by tsubst_enum in finish_member_declaration in the
7823 CLASSTYPE_NESTED_UTDS case above. */
7824 if (!(TREE_CODE (r) == TYPE_DECL
7825 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
7826 && DECL_ARTIFICIAL (r)))
7828 set_current_access_from_decl (r);
7829 finish_member_declaration (r);
7836 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
7838 /* Build new CLASSTYPE_FRIEND_CLASSES. */
7840 tree friend_type = t;
7841 bool adjust_processing_template_decl = false;
7843 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7845 /* template <class T> friend class C; */
7846 friend_type = tsubst_friend_class (friend_type, args);
7847 adjust_processing_template_decl = true;
7849 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
7851 /* template <class T> friend class C::D; */
7852 friend_type = tsubst (friend_type, args,
7853 tf_warning_or_error, NULL_TREE);
7854 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7855 friend_type = TREE_TYPE (friend_type);
7856 adjust_processing_template_decl = true;
7858 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
7860 /* This could be either
7864 when dependent_type_p is false or
7866 template <class U> friend class T::C;
7869 friend_type = tsubst (friend_type, args,
7870 tf_warning_or_error, NULL_TREE);
7871 /* Bump processing_template_decl for correct
7872 dependent_type_p calculation. */
7873 ++processing_template_decl;
7874 if (dependent_type_p (friend_type))
7875 adjust_processing_template_decl = true;
7876 --processing_template_decl;
7878 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
7879 && hidden_name_p (TYPE_NAME (friend_type)))
7883 where C hasn't been declared yet. Let's lookup name
7884 from namespace scope directly, bypassing any name that
7885 come from dependent base class. */
7886 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
7888 /* The call to xref_tag_from_type does injection for friend
7890 push_nested_namespace (ns);
7892 xref_tag_from_type (friend_type, NULL_TREE,
7893 /*tag_scope=*/ts_current);
7894 pop_nested_namespace (ns);
7896 else if (uses_template_parms (friend_type))
7897 /* friend class C<T>; */
7898 friend_type = tsubst (friend_type, args,
7899 tf_warning_or_error, NULL_TREE);
7904 where C is already declared or
7906 friend class C<int>;
7908 We don't have to do anything in these cases. */
7910 if (adjust_processing_template_decl)
7911 /* Trick make_friend_class into realizing that the friend
7912 we're adding is a template, not an ordinary class. It's
7913 important that we use make_friend_class since it will
7914 perform some error-checking and output cross-reference
7916 ++processing_template_decl;
7918 if (friend_type != error_mark_node)
7919 make_friend_class (type, friend_type, /*complain=*/false);
7921 if (adjust_processing_template_decl)
7922 --processing_template_decl;
7926 /* Build new DECL_FRIENDLIST. */
7929 /* The file and line for this declaration, to
7930 assist in error message reporting. Since we
7931 called push_tinst_level above, we don't need to
7933 input_location = DECL_SOURCE_LOCATION (t);
7935 if (TREE_CODE (t) == TEMPLATE_DECL)
7937 ++processing_template_decl;
7938 push_deferring_access_checks (dk_no_check);
7941 r = tsubst_friend_function (t, args);
7942 add_friend (type, r, /*complain=*/false);
7943 if (TREE_CODE (t) == TEMPLATE_DECL)
7945 pop_deferring_access_checks ();
7946 --processing_template_decl;
7952 /* Set the file and line number information to whatever is given for
7953 the class itself. This puts error messages involving generated
7954 implicit functions at a predictable point, and the same point
7955 that would be used for non-template classes. */
7956 input_location = DECL_SOURCE_LOCATION (typedecl);
7958 unreverse_member_declarations (type);
7959 finish_struct_1 (type);
7960 TYPE_BEING_DEFINED (type) = 0;
7962 /* Now that the class is complete, instantiate default arguments for
7963 any member functions. We don't do this earlier because the
7964 default arguments may reference members of the class. */
7965 if (!PRIMARY_TEMPLATE_P (templ))
7966 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
7967 if (TREE_CODE (t) == FUNCTION_DECL
7968 /* Implicitly generated member functions will not have template
7969 information; they are not instantiations, but instead are
7970 created "fresh" for each instantiation. */
7971 && DECL_TEMPLATE_INFO (t))
7972 tsubst_default_arguments (t);
7974 /* Some typedefs referenced from within the template code need to be access
7975 checked at template instantiation time, i.e now. These types were
7976 added to the template at parsing time. Let's get those and perform
7977 the access checks then. */
7978 perform_typedefs_access_check (pattern, args);
7979 perform_deferred_access_checks ();
7980 pop_nested_class ();
7981 maximum_field_alignment = saved_maximum_field_alignment;
7982 pop_from_top_level ();
7983 pop_deferring_access_checks ();
7986 /* The vtable for a template class can be emitted in any translation
7987 unit in which the class is instantiated. When there is no key
7988 method, however, finish_struct_1 will already have added TYPE to
7989 the keyed_classes list. */
7990 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
7991 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
7997 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8003 else if (TYPE_P (t))
8004 r = tsubst (t, args, complain, in_decl);
8007 r = tsubst_expr (t, args, complain, in_decl,
8008 /*integral_constant_expression_p=*/true);
8009 r = fold_non_dependent_expr (r);
8014 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8015 NONTYPE_ARGUMENT_PACK. */
8018 make_fnparm_pack (tree spec_parm)
8020 /* Collect all of the extra "packed" parameters into an
8024 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
8025 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
8026 int i, len = list_length (spec_parm);
8028 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8029 parmvec = make_tree_vec (len);
8030 parmtypevec = make_tree_vec (len);
8031 for (i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
8033 TREE_VEC_ELT (parmvec, i) = spec_parm;
8034 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
8037 /* Build the argument packs. */
8038 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
8039 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
8040 TREE_TYPE (argpack) = argtypepack;
8045 /* Substitute ARGS into T, which is an pack expansion
8046 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8047 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8048 (if only a partial substitution could be performed) or
8049 ERROR_MARK_NODE if there was an error. */
8051 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
8055 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
8059 bool very_local_specializations = false;
8061 gcc_assert (PACK_EXPANSION_P (t));
8062 pattern = PACK_EXPANSION_PATTERN (t);
8064 /* Determine the argument packs that will instantiate the parameter
8065 packs used in the expansion expression. While we're at it,
8066 compute the number of arguments to be expanded and make sure it
8068 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
8069 pack = TREE_CHAIN (pack))
8071 tree parm_pack = TREE_VALUE (pack);
8072 tree arg_pack = NULL_TREE;
8073 tree orig_arg = NULL_TREE;
8075 if (TREE_CODE (parm_pack) == PARM_DECL)
8077 arg_pack = retrieve_local_specialization (parm_pack);
8078 if (arg_pack == NULL_TREE)
8080 /* This can happen for a parameter name used later in a function
8081 declaration (such as in a late-specified return type). Just
8082 make a dummy decl, since it's only used for its type. */
8083 gcc_assert (cp_unevaluated_operand != 0);
8084 arg_pack = tsubst_decl (parm_pack, args, complain);
8085 arg_pack = make_fnparm_pack (arg_pack);
8090 int level, idx, levels;
8091 template_parm_level_and_index (parm_pack, &level, &idx);
8093 levels = TMPL_ARGS_DEPTH (args);
8094 if (level <= levels)
8095 arg_pack = TMPL_ARG (args, level, idx);
8098 orig_arg = arg_pack;
8099 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
8100 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
8102 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
8103 /* This can only happen if we forget to expand an argument
8104 pack somewhere else. Just return an error, silently. */
8106 result = make_tree_vec (1);
8107 TREE_VEC_ELT (result, 0) = error_mark_node;
8112 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
8113 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
8115 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
8116 tree pattern = PACK_EXPANSION_PATTERN (expansion);
8117 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
8118 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
8119 /* The argument pack that the parameter maps to is just an
8120 expansion of the parameter itself, such as one would
8121 find in the implicit typedef of a class inside the
8122 class itself. Consider this parameter "unsubstituted",
8123 so that we will maintain the outer pack expansion. */
8124 arg_pack = NULL_TREE;
8130 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
8132 /* It's all-or-nothing with incomplete argument packs. */
8133 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8134 return error_mark_node;
8136 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8141 else if (len != my_len)
8144 /* We got explicit args for some packs but not others;
8145 do nothing now and try again after deduction. */
8147 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
8148 error ("mismatched argument pack lengths while expanding "
8152 error ("mismatched argument pack lengths while expanding "
8155 return error_mark_node;
8158 /* Keep track of the parameter packs and their corresponding
8160 packs = tree_cons (parm_pack, arg_pack, packs);
8161 TREE_TYPE (packs) = orig_arg;
8164 /* We can't substitute for this parameter pack. */
8165 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
8167 unsubstituted_packs);
8170 /* We cannot expand this expansion expression, because we don't have
8171 all of the argument packs we need. Substitute into the pattern
8172 and return a PACK_EXPANSION_*. The caller will need to deal with
8174 if (unsubstituted_packs)
8177 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8178 new_pat = tsubst_expr (pattern, args, complain, in_decl,
8179 /*integral_constant_expression_p=*/false);
8181 new_pat = tsubst (pattern, args, complain, in_decl);
8182 return make_pack_expansion (new_pat);
8185 /* We could not find any argument packs that work. */
8187 return error_mark_node;
8189 if (!local_specializations)
8191 /* We're in a late-specified return type, so we don't have a local
8192 specializations table. Create one for doing this expansion. */
8193 very_local_specializations = true;
8194 local_specializations = htab_create (37,
8195 hash_local_specialization,
8196 eq_local_specializations,
8200 /* For each argument in each argument pack, substitute into the
8202 result = make_tree_vec (len + incomplete);
8203 for (i = 0; i < len + incomplete; ++i)
8205 /* For parameter pack, change the substitution of the parameter
8206 pack to the ith argument in its argument pack, then expand
8208 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8210 tree parm = TREE_PURPOSE (pack);
8212 if (TREE_CODE (parm) == PARM_DECL)
8214 /* Select the Ith argument from the pack. */
8215 tree arg = make_node (ARGUMENT_PACK_SELECT);
8216 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
8217 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
8219 register_local_specialization (arg, parm);
8225 template_parm_level_and_index (parm, &level, &idx);
8229 /* Select the Ith argument from the pack. */
8230 value = make_node (ARGUMENT_PACK_SELECT);
8231 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
8232 ARGUMENT_PACK_SELECT_INDEX (value) = i;
8235 /* Update the corresponding argument. */
8236 TMPL_ARG (args, level, idx) = value;
8240 /* Substitute into the PATTERN with the altered arguments. */
8241 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8242 TREE_VEC_ELT (result, i) =
8243 tsubst_expr (pattern, args, complain, in_decl,
8244 /*integral_constant_expression_p=*/false);
8246 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
8249 /* When we have incomplete argument packs, the last "expanded"
8250 result is itself a pack expansion, which allows us
8251 to deduce more arguments. */
8252 TREE_VEC_ELT (result, i) =
8253 make_pack_expansion (TREE_VEC_ELT (result, i));
8255 if (TREE_VEC_ELT (result, i) == error_mark_node)
8257 result = error_mark_node;
8262 /* Update ARGS to restore the substitution from parameter packs to
8263 their argument packs. */
8264 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8266 tree parm = TREE_PURPOSE (pack);
8268 if (TREE_CODE (parm) == PARM_DECL)
8269 register_local_specialization (TREE_TYPE (pack), parm);
8273 template_parm_level_and_index (parm, &level, &idx);
8275 /* Update the corresponding argument. */
8276 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
8277 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
8280 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
8284 if (very_local_specializations)
8286 htab_delete (local_specializations);
8287 local_specializations = NULL;
8293 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8294 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8295 parameter packs; all parms generated from a function parameter pack will
8296 have the same DECL_PARM_INDEX. */
8299 get_pattern_parm (tree parm, tree tmpl)
8301 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
8304 if (DECL_ARTIFICIAL (parm))
8306 for (patparm = DECL_ARGUMENTS (pattern);
8307 patparm; patparm = TREE_CHAIN (patparm))
8308 if (DECL_ARTIFICIAL (patparm)
8309 && DECL_NAME (parm) == DECL_NAME (patparm))
8314 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
8315 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
8316 gcc_assert (DECL_PARM_INDEX (patparm)
8317 == DECL_PARM_INDEX (parm));
8323 /* Substitute ARGS into the vector or list of template arguments T. */
8326 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8329 int len = TREE_VEC_LENGTH (t);
8330 int need_new = 0, i, expanded_len_adjust = 0, out;
8331 tree *elts = (tree *) alloca (len * sizeof (tree));
8333 for (i = 0; i < len; i++)
8335 tree orig_arg = TREE_VEC_ELT (t, i);
8338 if (TREE_CODE (orig_arg) == TREE_VEC)
8339 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
8340 else if (PACK_EXPANSION_P (orig_arg))
8342 /* Substitute into an expansion expression. */
8343 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
8345 if (TREE_CODE (new_arg) == TREE_VEC)
8346 /* Add to the expanded length adjustment the number of
8347 expanded arguments. We subtract one from this
8348 measurement, because the argument pack expression
8349 itself is already counted as 1 in
8350 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
8351 the argument pack is empty. */
8352 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
8354 else if (ARGUMENT_PACK_P (orig_arg))
8356 /* Substitute into each of the arguments. */
8357 new_arg = TYPE_P (orig_arg)
8358 ? cxx_make_type (TREE_CODE (orig_arg))
8359 : make_node (TREE_CODE (orig_arg));
8361 SET_ARGUMENT_PACK_ARGS (
8363 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
8364 args, complain, in_decl));
8366 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
8367 new_arg = error_mark_node;
8369 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
8370 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
8372 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
8374 if (TREE_TYPE (new_arg) == error_mark_node)
8375 new_arg = error_mark_node;
8379 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
8381 if (new_arg == error_mark_node)
8382 return error_mark_node;
8385 if (new_arg != orig_arg)
8392 /* Make space for the expanded arguments coming from template
8394 t = make_tree_vec (len + expanded_len_adjust);
8395 for (i = 0, out = 0; i < len; i++)
8397 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
8398 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
8399 && TREE_CODE (elts[i]) == TREE_VEC)
8403 /* Now expand the template argument pack "in place". */
8404 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
8405 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
8409 TREE_VEC_ELT (t, out) = elts[i];
8417 /* Return the result of substituting ARGS into the template parameters
8418 given by PARMS. If there are m levels of ARGS and m + n levels of
8419 PARMS, then the result will contain n levels of PARMS. For
8420 example, if PARMS is `template <class T> template <class U>
8421 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
8422 result will be `template <int*, double, class V>'. */
8425 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
8430 /* When substituting into a template, we must set
8431 PROCESSING_TEMPLATE_DECL as the template parameters may be
8432 dependent if they are based on one-another, and the dependency
8433 predicates are short-circuit outside of templates. */
8434 ++processing_template_decl;
8436 for (new_parms = &r;
8437 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
8438 new_parms = &(TREE_CHAIN (*new_parms)),
8439 parms = TREE_CHAIN (parms))
8442 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
8445 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
8451 if (parms == error_mark_node)
8454 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
8456 if (tuple == error_mark_node)
8459 default_value = TREE_PURPOSE (tuple);
8460 parm_decl = TREE_VALUE (tuple);
8462 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
8463 if (TREE_CODE (parm_decl) == PARM_DECL
8464 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
8465 parm_decl = error_mark_node;
8466 default_value = tsubst_template_arg (default_value, args,
8467 complain, NULL_TREE);
8469 tuple = build_tree_list (default_value, parm_decl);
8470 TREE_VEC_ELT (new_vec, i) = tuple;
8474 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
8475 - TMPL_ARGS_DEPTH (args)),
8476 new_vec, NULL_TREE);
8479 --processing_template_decl;
8484 /* Substitute the ARGS into the indicated aggregate (or enumeration)
8485 type T. If T is not an aggregate or enumeration type, it is
8486 handled as if by tsubst. IN_DECL is as for tsubst. If
8487 ENTERING_SCOPE is nonzero, T is the context for a template which
8488 we are presently tsubst'ing. Return the substituted value. */
8491 tsubst_aggr_type (tree t,
8493 tsubst_flags_t complain,
8500 switch (TREE_CODE (t))
8503 if (TYPE_PTRMEMFUNC_P (t))
8504 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
8506 /* Else fall through. */
8509 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
8514 int saved_unevaluated_operand;
8515 int saved_inhibit_evaluation_warnings;
8517 /* In "sizeof(X<I>)" we need to evaluate "I". */
8518 saved_unevaluated_operand = cp_unevaluated_operand;
8519 cp_unevaluated_operand = 0;
8520 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8521 c_inhibit_evaluation_warnings = 0;
8523 /* First, determine the context for the type we are looking
8525 context = TYPE_CONTEXT (t);
8528 context = tsubst_aggr_type (context, args, complain,
8529 in_decl, /*entering_scope=*/1);
8530 /* If context is a nested class inside a class template,
8531 it may still need to be instantiated (c++/33959). */
8532 if (TYPE_P (context))
8533 context = complete_type (context);
8536 /* Then, figure out what arguments are appropriate for the
8537 type we are trying to find. For example, given:
8539 template <class T> struct S;
8540 template <class T, class U> void f(T, U) { S<U> su; }
8542 and supposing that we are instantiating f<int, double>,
8543 then our ARGS will be {int, double}, but, when looking up
8544 S we only want {double}. */
8545 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
8547 if (argvec == error_mark_node)
8548 r = error_mark_node;
8551 r = lookup_template_class (t, argvec, in_decl, context,
8552 entering_scope, complain);
8553 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
8556 cp_unevaluated_operand = saved_unevaluated_operand;
8557 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8562 /* This is not a template type, so there's nothing to do. */
8566 return tsubst (t, args, complain, in_decl);
8570 /* Substitute into the default argument ARG (a default argument for
8571 FN), which has the indicated TYPE. */
8574 tsubst_default_argument (tree fn, tree type, tree arg)
8576 tree saved_class_ptr = NULL_TREE;
8577 tree saved_class_ref = NULL_TREE;
8579 /* This default argument came from a template. Instantiate the
8580 default argument here, not in tsubst. In the case of
8589 we must be careful to do name lookup in the scope of S<T>,
8590 rather than in the current class. */
8591 push_access_scope (fn);
8592 /* The "this" pointer is not valid in a default argument. */
8595 saved_class_ptr = current_class_ptr;
8596 cp_function_chain->x_current_class_ptr = NULL_TREE;
8597 saved_class_ref = current_class_ref;
8598 cp_function_chain->x_current_class_ref = NULL_TREE;
8601 push_deferring_access_checks(dk_no_deferred);
8602 /* The default argument expression may cause implicitly defined
8603 member functions to be synthesized, which will result in garbage
8604 collection. We must treat this situation as if we were within
8605 the body of function so as to avoid collecting live data on the
8608 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
8609 tf_warning_or_error, NULL_TREE,
8610 /*integral_constant_expression_p=*/false);
8612 pop_deferring_access_checks();
8614 /* Restore the "this" pointer. */
8617 cp_function_chain->x_current_class_ptr = saved_class_ptr;
8618 cp_function_chain->x_current_class_ref = saved_class_ref;
8621 /* Make sure the default argument is reasonable. */
8622 arg = check_default_argument (type, arg);
8624 pop_access_scope (fn);
8629 /* Substitute into all the default arguments for FN. */
8632 tsubst_default_arguments (tree fn)
8637 tmpl_args = DECL_TI_ARGS (fn);
8639 /* If this function is not yet instantiated, we certainly don't need
8640 its default arguments. */
8641 if (uses_template_parms (tmpl_args))
8644 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
8646 arg = TREE_CHAIN (arg))
8647 if (TREE_PURPOSE (arg))
8648 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
8650 TREE_PURPOSE (arg));
8653 /* Substitute the ARGS into the T, which is a _DECL. Return the
8654 result of the substitution. Issue error and warning messages under
8655 control of COMPLAIN. */
8658 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
8660 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
8661 location_t saved_loc;
8666 /* Set the filename and linenumber to improve error-reporting. */
8667 saved_loc = input_location;
8668 input_location = DECL_SOURCE_LOCATION (t);
8670 switch (TREE_CODE (t))
8674 /* We can get here when processing a member function template,
8675 member class template, or template template parameter. */
8676 tree decl = DECL_TEMPLATE_RESULT (t);
8681 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8683 /* Template template parameter is treated here. */
8684 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8685 if (new_type == error_mark_node)
8686 RETURN (error_mark_node);
8689 TREE_CHAIN (r) = NULL_TREE;
8690 TREE_TYPE (r) = new_type;
8691 DECL_TEMPLATE_RESULT (r)
8692 = build_decl (DECL_SOURCE_LOCATION (decl),
8693 TYPE_DECL, DECL_NAME (decl), new_type);
8694 DECL_TEMPLATE_PARMS (r)
8695 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8697 TYPE_NAME (new_type) = r;
8701 /* We might already have an instance of this template.
8702 The ARGS are for the surrounding class type, so the
8703 full args contain the tsubst'd args for the context,
8704 plus the innermost args from the template decl. */
8705 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
8706 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
8707 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
8708 /* Because this is a template, the arguments will still be
8709 dependent, even after substitution. If
8710 PROCESSING_TEMPLATE_DECL is not set, the dependency
8711 predicates will short-circuit. */
8712 ++processing_template_decl;
8713 full_args = tsubst_template_args (tmpl_args, args,
8715 --processing_template_decl;
8716 if (full_args == error_mark_node)
8717 RETURN (error_mark_node);
8719 /* If this is a default template template argument,
8720 tsubst might not have changed anything. */
8721 if (full_args == tmpl_args)
8724 hash = hash_tmpl_and_args (t, full_args);
8725 spec = retrieve_specialization (t, full_args, hash);
8726 if (spec != NULL_TREE)
8732 /* Make a new template decl. It will be similar to the
8733 original, but will record the current template arguments.
8734 We also create a new function declaration, which is just
8735 like the old one, but points to this new template, rather
8736 than the old one. */
8738 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
8739 TREE_CHAIN (r) = NULL_TREE;
8741 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
8743 if (TREE_CODE (decl) == TYPE_DECL)
8746 ++processing_template_decl;
8747 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8748 --processing_template_decl;
8749 if (new_type == error_mark_node)
8750 RETURN (error_mark_node);
8752 TREE_TYPE (r) = new_type;
8753 CLASSTYPE_TI_TEMPLATE (new_type) = r;
8754 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
8755 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
8756 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
8761 ++processing_template_decl;
8762 new_decl = tsubst (decl, args, complain, in_decl);
8763 --processing_template_decl;
8764 if (new_decl == error_mark_node)
8765 RETURN (error_mark_node);
8767 DECL_TEMPLATE_RESULT (r) = new_decl;
8768 DECL_TI_TEMPLATE (new_decl) = r;
8769 TREE_TYPE (r) = TREE_TYPE (new_decl);
8770 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
8771 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
8774 SET_DECL_IMPLICIT_INSTANTIATION (r);
8775 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
8776 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
8778 /* The template parameters for this new template are all the
8779 template parameters for the old template, except the
8780 outermost level of parameters. */
8781 DECL_TEMPLATE_PARMS (r)
8782 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8785 if (PRIMARY_TEMPLATE_P (t))
8786 DECL_PRIMARY_TEMPLATE (r) = r;
8788 if (TREE_CODE (decl) != TYPE_DECL)
8789 /* Record this non-type partial instantiation. */
8790 register_specialization (r, t,
8791 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
8799 tree argvec = NULL_TREE;
8807 /* Nobody should be tsubst'ing into non-template functions. */
8808 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
8810 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
8815 /* If T is not dependent, just return it. We have to
8816 increment PROCESSING_TEMPLATE_DECL because
8817 value_dependent_expression_p assumes that nothing is
8818 dependent when PROCESSING_TEMPLATE_DECL is zero. */
8819 ++processing_template_decl;
8820 dependent_p = value_dependent_expression_p (t);
8821 --processing_template_decl;
8825 /* Calculate the most general template of which R is a
8826 specialization, and the complete set of arguments used to
8828 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
8829 argvec = tsubst_template_args (DECL_TI_ARGS
8830 (DECL_TEMPLATE_RESULT (gen_tmpl)),
8831 args, complain, in_decl);
8833 /* Check to see if we already have this specialization. */
8834 hash = hash_tmpl_and_args (gen_tmpl, argvec);
8835 spec = retrieve_specialization (gen_tmpl, argvec, hash);
8843 /* We can see more levels of arguments than parameters if
8844 there was a specialization of a member template, like
8847 template <class T> struct S { template <class U> void f(); }
8848 template <> template <class U> void S<int>::f(U);
8850 Here, we'll be substituting into the specialization,
8851 because that's where we can find the code we actually
8852 want to generate, but we'll have enough arguments for
8853 the most general template.
8855 We also deal with the peculiar case:
8857 template <class T> struct S {
8858 template <class U> friend void f();
8860 template <class U> void f() {}
8862 template void f<double>();
8864 Here, the ARGS for the instantiation of will be {int,
8865 double}. But, we only need as many ARGS as there are
8866 levels of template parameters in CODE_PATTERN. We are
8867 careful not to get fooled into reducing the ARGS in
8870 template <class T> struct S { template <class U> void f(U); }
8871 template <class T> template <> void S<T>::f(int) {}
8873 which we can spot because the pattern will be a
8874 specialization in this case. */
8875 args_depth = TMPL_ARGS_DEPTH (args);
8877 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
8878 if (args_depth > parms_depth
8879 && !DECL_TEMPLATE_SPECIALIZATION (t))
8880 args = get_innermost_template_args (args, parms_depth);
8884 /* This special case arises when we have something like this:
8886 template <class T> struct S {
8887 friend void f<int>(int, double);
8890 Here, the DECL_TI_TEMPLATE for the friend declaration
8891 will be an IDENTIFIER_NODE. We are being called from
8892 tsubst_friend_function, and we want only to create a
8893 new decl (R) with appropriate types so that we can call
8894 determine_specialization. */
8895 gen_tmpl = NULL_TREE;
8898 if (DECL_CLASS_SCOPE_P (t))
8900 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
8904 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
8905 complain, t, /*entering_scope=*/1);
8910 ctx = DECL_CONTEXT (t);
8912 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8913 if (type == error_mark_node)
8914 RETURN (error_mark_node);
8916 /* We do NOT check for matching decls pushed separately at this
8917 point, as they may not represent instantiations of this
8918 template, and in any case are considered separate under the
8921 DECL_USE_TEMPLATE (r) = 0;
8922 TREE_TYPE (r) = type;
8923 /* Clear out the mangled name and RTL for the instantiation. */
8924 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
8925 SET_DECL_RTL (r, NULL_RTX);
8926 /* Leave DECL_INITIAL set on deleted instantiations. */
8927 if (!DECL_DELETED_FN (r))
8928 DECL_INITIAL (r) = NULL_TREE;
8929 DECL_CONTEXT (r) = ctx;
8931 if (member && DECL_CONV_FN_P (r))
8932 /* Type-conversion operator. Reconstruct the name, in
8933 case it's the name of one of the template's parameters. */
8934 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
8936 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
8938 DECL_RESULT (r) = NULL_TREE;
8940 TREE_STATIC (r) = 0;
8941 TREE_PUBLIC (r) = TREE_PUBLIC (t);
8942 DECL_EXTERNAL (r) = 1;
8943 /* If this is an instantiation of a function with internal
8944 linkage, we already know what object file linkage will be
8945 assigned to the instantiation. */
8946 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
8947 DECL_DEFER_OUTPUT (r) = 0;
8948 TREE_CHAIN (r) = NULL_TREE;
8949 DECL_PENDING_INLINE_INFO (r) = 0;
8950 DECL_PENDING_INLINE_P (r) = 0;
8951 DECL_SAVED_TREE (r) = NULL_TREE;
8952 DECL_STRUCT_FUNCTION (r) = NULL;
8954 /* We'll re-clone as appropriate in instantiate_template. */
8955 DECL_CLONED_FUNCTION (r) = NULL_TREE;
8957 /* If we aren't complaining now, return on error before we register
8958 the specialization so that we'll complain eventually. */
8959 if ((complain & tf_error) == 0
8960 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
8961 && !grok_op_properties (r, /*complain=*/false))
8962 RETURN (error_mark_node);
8964 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
8965 this in the special friend case mentioned above where
8966 GEN_TMPL is NULL. */
8969 DECL_TEMPLATE_INFO (r)
8970 = build_template_info (gen_tmpl, argvec);
8971 SET_DECL_IMPLICIT_INSTANTIATION (r);
8972 register_specialization (r, gen_tmpl, argvec, false, hash);
8974 /* We're not supposed to instantiate default arguments
8975 until they are called, for a template. But, for a
8978 template <class T> void f ()
8979 { extern void g(int i = T()); }
8981 we should do the substitution when the template is
8982 instantiated. We handle the member function case in
8983 instantiate_class_template since the default arguments
8984 might refer to other members of the class. */
8986 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8987 && !uses_template_parms (argvec))
8988 tsubst_default_arguments (r);
8991 DECL_TEMPLATE_INFO (r) = NULL_TREE;
8993 /* Copy the list of befriending classes. */
8994 for (friends = &DECL_BEFRIENDING_CLASSES (r);
8996 friends = &TREE_CHAIN (*friends))
8998 *friends = copy_node (*friends);
8999 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
9004 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
9006 maybe_retrofit_in_chrg (r);
9007 if (DECL_CONSTRUCTOR_P (r))
9008 grok_ctor_properties (ctx, r);
9009 /* If this is an instantiation of a member template, clone it.
9010 If it isn't, that'll be handled by
9011 clone_constructors_and_destructors. */
9012 if (PRIMARY_TEMPLATE_P (gen_tmpl))
9013 clone_function_decl (r, /*update_method_vec_p=*/0);
9015 else if ((complain & tf_error) != 0
9016 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9017 && !grok_op_properties (r, /*complain=*/true))
9018 RETURN (error_mark_node);
9020 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
9021 SET_DECL_FRIEND_CONTEXT (r,
9022 tsubst (DECL_FRIEND_CONTEXT (t),
9023 args, complain, in_decl));
9025 /* Possibly limit visibility based on template args. */
9026 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9027 if (DECL_VISIBILITY_SPECIFIED (t))
9029 DECL_VISIBILITY_SPECIFIED (r) = 0;
9031 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9033 determine_visibility (r);
9034 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
9035 && !processing_template_decl)
9036 defaulted_late_check (r);
9038 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9039 args, complain, in_decl);
9045 tree type = NULL_TREE;
9047 tree expanded_types = NULL_TREE;
9048 tree prev_r = NULL_TREE;
9049 tree first_r = NULL_TREE;
9051 if (FUNCTION_PARAMETER_PACK_P (t))
9053 /* If there is a local specialization that isn't a
9054 parameter pack, it means that we're doing a "simple"
9055 substitution from inside tsubst_pack_expansion. Just
9056 return the local specialization (which will be a single
9058 tree spec = retrieve_local_specialization (t);
9060 && TREE_CODE (spec) == PARM_DECL
9061 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
9064 /* Expand the TYPE_PACK_EXPANSION that provides the types for
9065 the parameters in this function parameter pack. */
9066 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
9068 if (TREE_CODE (expanded_types) == TREE_VEC)
9070 len = TREE_VEC_LENGTH (expanded_types);
9072 /* Zero-length parameter packs are boring. Just substitute
9075 RETURN (tsubst (TREE_CHAIN (t), args, complain,
9080 /* All we did was update the type. Make a note of that. */
9081 type = expanded_types;
9082 expanded_types = NULL_TREE;
9086 /* Loop through all of the parameter's we'll build. When T is
9087 a function parameter pack, LEN is the number of expanded
9088 types in EXPANDED_TYPES; otherwise, LEN is 1. */
9090 for (i = 0; i < len; ++i)
9094 if (DECL_TEMPLATE_PARM_P (t))
9095 SET_DECL_TEMPLATE_PARM_P (r);
9097 /* An argument of a function parameter pack is not a parameter
9099 FUNCTION_PARAMETER_PACK_P (r) = false;
9102 /* We're on the Ith parameter of the function parameter
9105 /* Get the Ith type. */
9106 type = TREE_VEC_ELT (expanded_types, i);
9109 /* Rename the parameter to include the index. */
9111 make_ith_pack_parameter_name (DECL_NAME (r), i);
9114 /* We're dealing with a normal parameter. */
9115 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9117 type = type_decays_to (type);
9118 TREE_TYPE (r) = type;
9119 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9121 if (DECL_INITIAL (r))
9123 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
9124 DECL_INITIAL (r) = TREE_TYPE (r);
9126 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
9130 DECL_CONTEXT (r) = NULL_TREE;
9132 if (!DECL_TEMPLATE_PARM_P (r))
9133 DECL_ARG_TYPE (r) = type_passed_as (type);
9135 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9136 args, complain, in_decl);
9138 /* Keep track of the first new parameter we
9139 generate. That's what will be returned to the
9144 /* Build a proper chain of parameters when substituting
9145 into a function parameter pack. */
9147 TREE_CHAIN (prev_r) = r;
9151 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
9152 complain, TREE_CHAIN (t));
9154 /* FIRST_R contains the start of the chain we've built. */
9164 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9165 if (type == error_mark_node)
9166 RETURN (error_mark_node);
9167 TREE_TYPE (r) = type;
9168 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9170 /* DECL_INITIAL gives the number of bits in a bit-field. */
9172 = tsubst_expr (DECL_INITIAL (t), args,
9174 /*integral_constant_expression_p=*/true);
9175 /* We don't have to set DECL_CONTEXT here; it is set by
9176 finish_member_declaration. */
9177 TREE_CHAIN (r) = NULL_TREE;
9178 if (VOID_TYPE_P (type))
9179 error ("instantiation of %q+D as type %qT", r, type);
9181 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9182 args, complain, in_decl);
9187 /* We reach here only for member using decls. */
9188 if (DECL_DEPENDENT_P (t))
9190 r = do_class_using_decl
9191 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
9192 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
9194 r = error_mark_node;
9197 TREE_PROTECTED (r) = TREE_PROTECTED (t);
9198 TREE_PRIVATE (r) = TREE_PRIVATE (t);
9204 TREE_CHAIN (r) = NULL_TREE;
9211 tree argvec = NULL_TREE;
9212 tree gen_tmpl = NULL_TREE;
9214 tree tmpl = NULL_TREE;
9216 tree type = NULL_TREE;
9219 if (TREE_CODE (t) == TYPE_DECL
9220 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
9222 /* If this is the canonical decl, we don't have to
9223 mess with instantiations, and often we can't (for
9224 typename, template type parms and such). Note that
9225 TYPE_NAME is not correct for the above test if
9226 we've copied the type for a typedef. */
9227 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9228 if (type == error_mark_node)
9229 RETURN (error_mark_node);
9230 r = TYPE_NAME (type);
9234 /* Check to see if we already have the specialization we
9237 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
9239 /* T is a static data member or namespace-scope entity.
9240 We have to substitute into namespace-scope variables
9241 (even though such entities are never templates) because
9244 template <class T> void f() { extern T t; }
9246 where the entity referenced is not known until
9247 instantiation time. */
9249 ctx = DECL_CONTEXT (t);
9250 if (DECL_CLASS_SCOPE_P (t))
9252 ctx = tsubst_aggr_type (ctx, args,
9254 in_decl, /*entering_scope=*/1);
9255 /* If CTX is unchanged, then T is in fact the
9256 specialization we want. That situation occurs when
9257 referencing a static data member within in its own
9258 class. We can use pointer equality, rather than
9259 same_type_p, because DECL_CONTEXT is always
9261 if (ctx == DECL_CONTEXT (t))
9267 tmpl = DECL_TI_TEMPLATE (t);
9268 gen_tmpl = most_general_template (tmpl);
9269 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
9270 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9271 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9276 /* A local variable. */
9278 /* Subsequent calls to pushdecl will fill this in. */
9280 spec = retrieve_local_specialization (t);
9282 /* If we already have the specialization we need, there is
9283 nothing more to do. */
9290 /* Create a new node for the specialization we need. */
9292 if (type == NULL_TREE)
9294 if (is_typedef_decl (t))
9295 type = DECL_ORIGINAL_TYPE (t);
9297 type = TREE_TYPE (t);
9298 type = tsubst (type, args, complain, in_decl);
9300 if (TREE_CODE (r) == VAR_DECL)
9302 /* Even if the original location is out of scope, the
9303 newly substituted one is not. */
9304 DECL_DEAD_FOR_LOCAL (r) = 0;
9305 DECL_INITIALIZED_P (r) = 0;
9306 DECL_TEMPLATE_INSTANTIATED (r) = 0;
9307 if (type == error_mark_node)
9308 RETURN (error_mark_node);
9309 if (TREE_CODE (type) == FUNCTION_TYPE)
9311 /* It may seem that this case cannot occur, since:
9316 declares a function, not a variable. However:
9319 template <typename T> void g() { T t; }
9320 template void g<f>();
9322 is an attempt to declare a variable with function
9324 error ("variable %qD has function type",
9325 /* R is not yet sufficiently initialized, so we
9326 just use its name. */
9328 RETURN (error_mark_node);
9330 type = complete_type (type);
9331 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
9332 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
9333 type = check_var_type (DECL_NAME (r), type);
9335 if (DECL_HAS_VALUE_EXPR_P (t))
9337 tree ve = DECL_VALUE_EXPR (t);
9338 ve = tsubst_expr (ve, args, complain, in_decl,
9339 /*constant_expression_p=*/false);
9340 SET_DECL_VALUE_EXPR (r, ve);
9343 else if (DECL_SELF_REFERENCE_P (t))
9344 SET_DECL_SELF_REFERENCE_P (r);
9345 TREE_TYPE (r) = type;
9346 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9347 DECL_CONTEXT (r) = ctx;
9348 /* Clear out the mangled name and RTL for the instantiation. */
9349 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9350 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9351 SET_DECL_RTL (r, NULL_RTX);
9352 /* The initializer must not be expanded until it is required;
9354 DECL_INITIAL (r) = NULL_TREE;
9355 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9356 SET_DECL_RTL (r, NULL_RTX);
9357 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
9358 if (TREE_CODE (r) == VAR_DECL)
9360 /* Possibly limit visibility based on template args. */
9361 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9362 if (DECL_VISIBILITY_SPECIFIED (t))
9364 DECL_VISIBILITY_SPECIFIED (r) = 0;
9366 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9368 determine_visibility (r);
9373 /* A static data member declaration is always marked
9374 external when it is declared in-class, even if an
9375 initializer is present. We mimic the non-template
9377 DECL_EXTERNAL (r) = 1;
9379 register_specialization (r, gen_tmpl, argvec, false, hash);
9380 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
9381 SET_DECL_IMPLICIT_INSTANTIATION (r);
9383 else if (cp_unevaluated_operand)
9385 /* We're substituting this var in a decltype outside of its
9386 scope, such as for a lambda return type. Don't add it to
9387 local_specializations, do perform auto deduction. */
9388 tree auto_node = type_uses_auto (type);
9390 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
9391 /*constant_expression_p=*/false);
9393 if (auto_node && init && describable_type (init))
9395 type = do_auto_deduction (type, init, auto_node);
9396 TREE_TYPE (r) = type;
9400 register_local_specialization (r, t);
9402 TREE_CHAIN (r) = NULL_TREE;
9404 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
9406 args, complain, in_decl);
9408 /* Preserve a typedef that names a type. */
9409 if (is_typedef_decl (r))
9411 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
9412 set_underlying_type (r);
9425 /* Restore the file and line information. */
9426 input_location = saved_loc;
9431 /* Substitute into the ARG_TYPES of a function type. */
9434 tsubst_arg_types (tree arg_types,
9436 tsubst_flags_t complain,
9439 tree remaining_arg_types;
9440 tree type = NULL_TREE;
9442 tree expanded_args = NULL_TREE;
9445 if (!arg_types || arg_types == void_list_node)
9448 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
9449 args, complain, in_decl);
9450 if (remaining_arg_types == error_mark_node)
9451 return error_mark_node;
9453 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
9455 /* For a pack expansion, perform substitution on the
9456 entire expression. Later on, we'll handle the arguments
9458 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
9459 args, complain, in_decl);
9461 if (TREE_CODE (expanded_args) == TREE_VEC)
9462 /* So that we'll spin through the parameters, one by one. */
9463 i = TREE_VEC_LENGTH (expanded_args);
9466 /* We only partially substituted into the parameter
9467 pack. Our type is TYPE_PACK_EXPANSION. */
9468 type = expanded_args;
9469 expanded_args = NULL_TREE;
9477 type = TREE_VEC_ELT (expanded_args, i);
9479 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
9481 if (type == error_mark_node)
9482 return error_mark_node;
9483 if (VOID_TYPE_P (type))
9485 if (complain & tf_error)
9487 error ("invalid parameter type %qT", type);
9489 error ("in declaration %q+D", in_decl);
9491 return error_mark_node;
9494 /* Do array-to-pointer, function-to-pointer conversion, and ignore
9495 top-level qualifiers as required. */
9496 type = TYPE_MAIN_VARIANT (type_decays_to (type));
9498 /* We do not substitute into default arguments here. The standard
9499 mandates that they be instantiated only when needed, which is
9500 done in build_over_call. */
9501 default_arg = TREE_PURPOSE (arg_types);
9503 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
9505 /* We've instantiated a template before its default arguments
9506 have been parsed. This can happen for a nested template
9507 class, and is not an error unless we require the default
9508 argument in a call of this function. */
9509 remaining_arg_types =
9510 tree_cons (default_arg, type, remaining_arg_types);
9511 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
9512 remaining_arg_types);
9515 remaining_arg_types =
9516 hash_tree_cons (default_arg, type, remaining_arg_types);
9519 return remaining_arg_types;
9522 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
9523 *not* handle the exception-specification for FNTYPE, because the
9524 initial substitution of explicitly provided template parameters
9525 during argument deduction forbids substitution into the
9526 exception-specification:
9530 All references in the function type of the function template to the
9531 corresponding template parameters are replaced by the specified tem-
9532 plate argument values. If a substitution in a template parameter or
9533 in the function type of the function template results in an invalid
9534 type, type deduction fails. [Note: The equivalent substitution in
9535 exception specifications is done only when the function is instanti-
9536 ated, at which point a program is ill-formed if the substitution
9537 results in an invalid type.] */
9540 tsubst_function_type (tree t,
9542 tsubst_flags_t complain,
9549 /* The TYPE_CONTEXT is not used for function/method types. */
9550 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
9552 /* Substitute the return type. */
9553 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9554 if (return_type == error_mark_node)
9555 return error_mark_node;
9556 /* The standard does not presently indicate that creation of a
9557 function type with an invalid return type is a deduction failure.
9558 However, that is clearly analogous to creating an array of "void"
9559 or a reference to a reference. This is core issue #486. */
9560 if (TREE_CODE (return_type) == ARRAY_TYPE
9561 || TREE_CODE (return_type) == FUNCTION_TYPE)
9563 if (complain & tf_error)
9565 if (TREE_CODE (return_type) == ARRAY_TYPE)
9566 error ("function returning an array");
9568 error ("function returning a function");
9570 return error_mark_node;
9573 /* Substitute the argument types. */
9574 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
9576 if (arg_types == error_mark_node)
9577 return error_mark_node;
9579 /* Construct a new type node and return it. */
9580 if (TREE_CODE (t) == FUNCTION_TYPE)
9581 fntype = build_function_type (return_type, arg_types);
9584 tree r = TREE_TYPE (TREE_VALUE (arg_types));
9585 if (! MAYBE_CLASS_TYPE_P (r))
9589 Type deduction may fail for any of the following
9592 -- Attempting to create "pointer to member of T" when T
9593 is not a class type. */
9594 if (complain & tf_error)
9595 error ("creating pointer to member function of non-class type %qT",
9597 return error_mark_node;
9600 fntype = build_method_type_directly (r, return_type,
9601 TREE_CHAIN (arg_types));
9603 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
9604 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
9609 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
9610 ARGS into that specification, and return the substituted
9611 specification. If there is no specification, return NULL_TREE. */
9614 tsubst_exception_specification (tree fntype,
9616 tsubst_flags_t complain,
9622 specs = TYPE_RAISES_EXCEPTIONS (fntype);
9623 new_specs = NULL_TREE;
9626 if (! TREE_VALUE (specs))
9633 tree expanded_specs = NULL_TREE;
9635 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
9637 /* Expand the pack expansion type. */
9638 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
9642 if (expanded_specs == error_mark_node)
9643 return error_mark_node;
9644 else if (TREE_CODE (expanded_specs) == TREE_VEC)
9645 len = TREE_VEC_LENGTH (expanded_specs);
9648 /* We're substituting into a member template, so
9649 we got a TYPE_PACK_EXPANSION back. Add that
9650 expansion and move on. */
9651 gcc_assert (TREE_CODE (expanded_specs)
9652 == TYPE_PACK_EXPANSION);
9653 new_specs = add_exception_specifier (new_specs,
9656 specs = TREE_CHAIN (specs);
9661 for (i = 0; i < len; ++i)
9664 spec = TREE_VEC_ELT (expanded_specs, i);
9666 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
9667 if (spec == error_mark_node)
9669 new_specs = add_exception_specifier (new_specs, spec,
9673 specs = TREE_CHAIN (specs);
9679 /* Take the tree structure T and replace template parameters used
9680 therein with the argument vector ARGS. IN_DECL is an associated
9681 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
9682 Issue error and warning messages under control of COMPLAIN. Note
9683 that we must be relatively non-tolerant of extensions here, in
9684 order to preserve conformance; if we allow substitutions that
9685 should not be allowed, we may allow argument deductions that should
9686 not succeed, and therefore report ambiguous overload situations
9687 where there are none. In theory, we could allow the substitution,
9688 but indicate that it should have failed, and allow our caller to
9689 make sure that the right thing happens, but we don't try to do this
9692 This function is used for dealing with types, decls and the like;
9693 for expressions, use tsubst_expr or tsubst_copy. */
9696 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9700 if (t == NULL_TREE || t == error_mark_node
9701 || t == integer_type_node
9702 || t == void_type_node
9703 || t == char_type_node
9704 || t == unknown_type_node
9705 || TREE_CODE (t) == NAMESPACE_DECL)
9709 return tsubst_decl (t, args, complain);
9711 if (args == NULL_TREE)
9714 if (TREE_CODE (t) == IDENTIFIER_NODE)
9715 type = IDENTIFIER_TYPE_VALUE (t);
9717 type = TREE_TYPE (t);
9719 gcc_assert (type != unknown_type_node);
9721 /* Reuse typedefs. We need to do this to handle dependent attributes,
9722 such as attribute aligned. */
9725 && TYPE_NAME (t) != TYPE_MAIN_DECL (t))
9727 tree decl = TYPE_NAME (t);
9729 if (DECL_CLASS_SCOPE_P (decl)
9730 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
9731 && uses_template_parms (DECL_CONTEXT (decl)))
9733 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
9734 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
9735 r = retrieve_specialization (tmpl, gen_args, 0);
9737 else if (DECL_FUNCTION_SCOPE_P (decl)
9738 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
9739 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
9740 r = retrieve_local_specialization (decl);
9742 /* The typedef is from a non-template context. */
9748 r = cp_build_qualified_type_real
9749 (r, cp_type_quals (t) | cp_type_quals (r),
9750 complain | tf_ignore_bad_quals);
9753 /* Else we must be instantiating the typedef, so fall through. */
9757 && TREE_CODE (t) != TYPENAME_TYPE
9758 && TREE_CODE (t) != IDENTIFIER_NODE
9759 && TREE_CODE (t) != FUNCTION_TYPE
9760 && TREE_CODE (t) != METHOD_TYPE)
9761 type = tsubst (type, args, complain, in_decl);
9762 if (type == error_mark_node)
9763 return error_mark_node;
9765 switch (TREE_CODE (t))
9770 return tsubst_aggr_type (t, args, complain, in_decl,
9771 /*entering_scope=*/0);
9774 case IDENTIFIER_NODE:
9786 if (t == integer_type_node)
9789 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
9790 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
9794 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
9796 max = tsubst_expr (omax, args, complain, in_decl,
9797 /*integral_constant_expression_p=*/false);
9799 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
9801 if (TREE_CODE (max) == NOP_EXPR
9802 && TREE_SIDE_EFFECTS (omax)
9803 && !TREE_TYPE (max))
9804 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
9806 max = fold_decl_constant_value (max);
9808 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
9809 with TREE_SIDE_EFFECTS that indicates this is not an integral
9810 constant expression. */
9811 if (processing_template_decl
9812 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
9814 gcc_assert (TREE_CODE (max) == NOP_EXPR);
9815 TREE_SIDE_EFFECTS (max) = 1;
9818 if (TREE_CODE (max) != INTEGER_CST
9819 && !at_function_scope_p ()
9820 && !TREE_SIDE_EFFECTS (max)
9821 && !value_dependent_expression_p (max))
9823 if (complain & tf_error)
9824 error ("array bound is not an integer constant");
9825 return error_mark_node;
9830 Type deduction may fail for any of the following
9833 Attempting to create an array with a size that is
9834 zero or negative. */
9835 if (integer_zerop (max) && !(complain & tf_error))
9836 /* We must fail if performing argument deduction (as
9837 indicated by the state of complain), so that
9838 another substitution can be found. */
9839 return error_mark_node;
9840 else if (TREE_CODE (max) == INTEGER_CST
9841 && INT_CST_LT (max, integer_zero_node))
9843 if (complain & tf_error)
9844 error ("creating array with negative size (%qE)", max);
9846 return error_mark_node;
9849 return compute_array_index_type (NULL_TREE, max);
9852 case TEMPLATE_TYPE_PARM:
9853 case TEMPLATE_TEMPLATE_PARM:
9854 case BOUND_TEMPLATE_TEMPLATE_PARM:
9855 case TEMPLATE_PARM_INDEX:
9860 tree arg = NULL_TREE;
9864 gcc_assert (TREE_VEC_LENGTH (args) > 0);
9865 template_parm_level_and_index (t, &level, &idx);
9867 levels = TMPL_ARGS_DEPTH (args);
9868 if (level <= levels)
9870 arg = TMPL_ARG (args, level, idx);
9872 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
9873 /* See through ARGUMENT_PACK_SELECT arguments. */
9874 arg = ARGUMENT_PACK_SELECT_ARG (arg);
9877 if (arg == error_mark_node)
9878 return error_mark_node;
9879 else if (arg != NULL_TREE)
9881 if (ARGUMENT_PACK_P (arg))
9882 /* If ARG is an argument pack, we don't actually want to
9883 perform a substitution here, because substitutions
9884 for argument packs are only done
9885 element-by-element. We can get to this point when
9886 substituting the type of a non-type template
9887 parameter pack, when that type actually contains
9888 template parameter packs from an outer template, e.g.,
9890 template<typename... Types> struct A {
9891 template<Types... Values> struct B { };
9895 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
9898 gcc_assert (TYPE_P (arg));
9900 /* cv-quals from the template are discarded when
9901 substituting in a function or reference type. */
9902 if (TREE_CODE (arg) == FUNCTION_TYPE
9903 || TREE_CODE (arg) == METHOD_TYPE
9904 || TREE_CODE (arg) == REFERENCE_TYPE)
9905 quals = cp_type_quals (arg);
9907 quals = cp_type_quals (arg) | cp_type_quals (t);
9909 return cp_build_qualified_type_real
9910 (arg, quals, complain | tf_ignore_bad_quals);
9912 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9914 /* We are processing a type constructed from a
9915 template template parameter. */
9916 tree argvec = tsubst (TYPE_TI_ARGS (t),
9917 args, complain, in_decl);
9918 if (argvec == error_mark_node)
9919 return error_mark_node;
9921 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
9922 are resolving nested-types in the signature of a
9923 member function templates. Otherwise ARG is a
9924 TEMPLATE_DECL and is the real template to be
9926 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
9927 arg = TYPE_NAME (arg);
9929 r = lookup_template_class (arg,
9932 /*entering_scope=*/0,
9934 return cp_build_qualified_type_real
9935 (r, TYPE_QUALS (t), complain);
9938 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
9943 /* This can happen during the attempted tsubst'ing in
9944 unify. This means that we don't yet have any information
9945 about the template parameter in question. */
9948 /* If we get here, we must have been looking at a parm for a
9949 more deeply nested template. Make a new version of this
9950 template parameter, but with a lower level. */
9951 switch (TREE_CODE (t))
9953 case TEMPLATE_TYPE_PARM:
9954 case TEMPLATE_TEMPLATE_PARM:
9955 case BOUND_TEMPLATE_TEMPLATE_PARM:
9956 if (cp_type_quals (t))
9958 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
9959 r = cp_build_qualified_type_real
9960 (r, cp_type_quals (t),
9961 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
9962 ? tf_ignore_bad_quals : 0));
9967 TEMPLATE_TYPE_PARM_INDEX (r)
9968 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
9969 r, levels, args, complain);
9970 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
9971 TYPE_MAIN_VARIANT (r) = r;
9972 TYPE_POINTER_TO (r) = NULL_TREE;
9973 TYPE_REFERENCE_TO (r) = NULL_TREE;
9975 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
9976 /* We have reduced the level of the template
9977 template parameter, but not the levels of its
9978 template parameters, so canonical_type_parameter
9979 will not be able to find the canonical template
9980 template parameter for this level. Thus, we
9981 require structural equality checking to compare
9982 TEMPLATE_TEMPLATE_PARMs. */
9983 SET_TYPE_STRUCTURAL_EQUALITY (r);
9984 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
9985 SET_TYPE_STRUCTURAL_EQUALITY (r);
9987 TYPE_CANONICAL (r) = canonical_type_parameter (r);
9989 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9991 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
9993 if (argvec == error_mark_node)
9994 return error_mark_node;
9996 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
9997 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
10002 case TEMPLATE_PARM_INDEX:
10003 r = reduce_template_parm_level (t, type, levels, args, complain);
10007 gcc_unreachable ();
10015 tree purpose, value, chain;
10017 if (t == void_list_node)
10020 purpose = TREE_PURPOSE (t);
10023 purpose = tsubst (purpose, args, complain, in_decl);
10024 if (purpose == error_mark_node)
10025 return error_mark_node;
10027 value = TREE_VALUE (t);
10030 value = tsubst (value, args, complain, in_decl);
10031 if (value == error_mark_node)
10032 return error_mark_node;
10034 chain = TREE_CHAIN (t);
10035 if (chain && chain != void_type_node)
10037 chain = tsubst (chain, args, complain, in_decl);
10038 if (chain == error_mark_node)
10039 return error_mark_node;
10041 if (purpose == TREE_PURPOSE (t)
10042 && value == TREE_VALUE (t)
10043 && chain == TREE_CHAIN (t))
10045 return hash_tree_cons (purpose, value, chain);
10049 /* We should never be tsubsting a binfo. */
10050 gcc_unreachable ();
10053 /* A vector of template arguments. */
10054 gcc_assert (!type);
10055 return tsubst_template_args (t, args, complain, in_decl);
10058 case REFERENCE_TYPE:
10060 enum tree_code code;
10062 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
10065 code = TREE_CODE (t);
10070 Type deduction may fail for any of the following
10073 -- Attempting to create a pointer to reference type.
10074 -- Attempting to create a reference to a reference type or
10075 a reference to void.
10077 Core issue 106 says that creating a reference to a reference
10078 during instantiation is no longer a cause for failure. We
10079 only enforce this check in strict C++98 mode. */
10080 if ((TREE_CODE (type) == REFERENCE_TYPE
10081 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
10082 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
10084 static location_t last_loc;
10086 /* We keep track of the last time we issued this error
10087 message to avoid spewing a ton of messages during a
10088 single bad template instantiation. */
10089 if (complain & tf_error
10090 && last_loc != input_location)
10092 if (TREE_CODE (type) == VOID_TYPE)
10093 error ("forming reference to void");
10094 else if (code == POINTER_TYPE)
10095 error ("forming pointer to reference type %qT", type);
10097 error ("forming reference to reference type %qT", type);
10098 last_loc = input_location;
10101 return error_mark_node;
10103 else if (code == POINTER_TYPE)
10105 r = build_pointer_type (type);
10106 if (TREE_CODE (type) == METHOD_TYPE)
10107 r = build_ptrmemfunc_type (r);
10109 else if (TREE_CODE (type) == REFERENCE_TYPE)
10110 /* In C++0x, during template argument substitution, when there is an
10111 attempt to create a reference to a reference type, reference
10112 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
10114 "If a template-argument for a template-parameter T names a type
10115 that is a reference to a type A, an attempt to create the type
10116 'lvalue reference to cv T' creates the type 'lvalue reference to
10117 A,' while an attempt to create the type type rvalue reference to
10118 cv T' creates the type T"
10120 r = cp_build_reference_type
10122 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
10124 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
10125 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
10127 if (r != error_mark_node)
10128 /* Will this ever be needed for TYPE_..._TO values? */
10135 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
10136 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
10140 Type deduction may fail for any of the following
10143 -- Attempting to create "pointer to member of T" when T
10144 is not a class type. */
10145 if (complain & tf_error)
10146 error ("creating pointer to member of non-class type %qT", r);
10147 return error_mark_node;
10149 if (TREE_CODE (type) == REFERENCE_TYPE)
10151 if (complain & tf_error)
10152 error ("creating pointer to member reference type %qT", type);
10153 return error_mark_node;
10155 if (TREE_CODE (type) == VOID_TYPE)
10157 if (complain & tf_error)
10158 error ("creating pointer to member of type void");
10159 return error_mark_node;
10161 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
10162 if (TREE_CODE (type) == FUNCTION_TYPE)
10164 /* The type of the implicit object parameter gets its
10165 cv-qualifiers from the FUNCTION_TYPE. */
10167 tree method_type = build_memfn_type (type, r, cp_type_quals (type));
10168 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
10169 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
10173 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
10177 case FUNCTION_TYPE:
10182 fntype = tsubst_function_type (t, args, complain, in_decl);
10183 if (fntype == error_mark_node)
10184 return error_mark_node;
10186 /* Substitute the exception specification. */
10187 specs = tsubst_exception_specification (t, args, complain,
10189 if (specs == error_mark_node)
10190 return error_mark_node;
10192 fntype = build_exception_variant (fntype, specs);
10197 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
10198 if (domain == error_mark_node)
10199 return error_mark_node;
10201 /* As an optimization, we avoid regenerating the array type if
10202 it will obviously be the same as T. */
10203 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
10206 /* These checks should match the ones in grokdeclarator.
10210 The deduction may fail for any of the following reasons:
10212 -- Attempting to create an array with an element type that
10213 is void, a function type, or a reference type, or [DR337]
10214 an abstract class type. */
10215 if (TREE_CODE (type) == VOID_TYPE
10216 || TREE_CODE (type) == FUNCTION_TYPE
10217 || TREE_CODE (type) == REFERENCE_TYPE)
10219 if (complain & tf_error)
10220 error ("creating array of %qT", type);
10221 return error_mark_node;
10223 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
10225 if (complain & tf_error)
10226 error ("creating array of %qT, which is an abstract class type",
10228 return error_mark_node;
10231 r = build_cplus_array_type (type, domain);
10233 if (TYPE_USER_ALIGN (t))
10235 TYPE_ALIGN (r) = TYPE_ALIGN (t);
10236 TYPE_USER_ALIGN (r) = 1;
10245 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10246 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10248 if (e1 == error_mark_node || e2 == error_mark_node)
10249 return error_mark_node;
10251 return fold_build2_loc (input_location,
10252 TREE_CODE (t), TREE_TYPE (t), e1, e2);
10258 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10259 if (e == error_mark_node)
10260 return error_mark_node;
10262 return fold_build1_loc (input_location, TREE_CODE (t), TREE_TYPE (t), e);
10265 case TYPENAME_TYPE:
10267 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10268 in_decl, /*entering_scope=*/1);
10269 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
10270 complain, in_decl);
10272 if (ctx == error_mark_node || f == error_mark_node)
10273 return error_mark_node;
10275 if (!MAYBE_CLASS_TYPE_P (ctx))
10277 if (complain & tf_error)
10278 error ("%qT is not a class, struct, or union type", ctx);
10279 return error_mark_node;
10281 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
10283 /* Normally, make_typename_type does not require that the CTX
10284 have complete type in order to allow things like:
10286 template <class T> struct S { typename S<T>::X Y; };
10288 But, such constructs have already been resolved by this
10289 point, so here CTX really should have complete type, unless
10290 it's a partial instantiation. */
10291 if (!(complain & tf_no_class_instantiations))
10292 ctx = complete_type (ctx);
10293 if (!COMPLETE_TYPE_P (ctx))
10295 if (complain & tf_error)
10296 cxx_incomplete_type_error (NULL_TREE, ctx);
10297 return error_mark_node;
10301 f = make_typename_type (ctx, f, typename_type,
10302 (complain & tf_error) | tf_keep_type_decl);
10303 if (f == error_mark_node)
10305 if (TREE_CODE (f) == TYPE_DECL)
10307 complain |= tf_ignore_bad_quals;
10311 if (TREE_CODE (f) != TYPENAME_TYPE)
10313 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
10314 error ("%qT resolves to %qT, which is not an enumeration type",
10316 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
10317 error ("%qT resolves to %qT, which is is not a class type",
10321 return cp_build_qualified_type_real
10322 (f, cp_type_quals (f) | cp_type_quals (t), complain);
10325 case UNBOUND_CLASS_TEMPLATE:
10327 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10328 in_decl, /*entering_scope=*/1);
10329 tree name = TYPE_IDENTIFIER (t);
10330 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
10332 if (ctx == error_mark_node || name == error_mark_node)
10333 return error_mark_node;
10336 parm_list = tsubst_template_parms (parm_list, args, complain);
10337 return make_unbound_class_template (ctx, name, parm_list, complain);
10343 gcc_unreachable ();
10347 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10348 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
10349 /*integral_constant_expression_p=*/false);
10350 if (e1 == error_mark_node || e2 == error_mark_node)
10351 return error_mark_node;
10353 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
10358 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10359 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10360 if (e1 == error_mark_node || e2 == error_mark_node)
10361 return error_mark_node;
10363 return build_qualified_name (/*type=*/NULL_TREE,
10364 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
10371 ++cp_unevaluated_operand;
10372 ++c_inhibit_evaluation_warnings;
10374 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
10376 /*integral_constant_expression_p=*/false);
10378 --cp_unevaluated_operand;
10379 --c_inhibit_evaluation_warnings;
10381 type = finish_typeof (type);
10382 return cp_build_qualified_type_real (type,
10384 | cp_type_quals (type),
10388 case DECLTYPE_TYPE:
10392 ++cp_unevaluated_operand;
10393 ++c_inhibit_evaluation_warnings;
10395 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
10397 /*integral_constant_expression_p=*/false);
10399 --cp_unevaluated_operand;
10400 --c_inhibit_evaluation_warnings;
10402 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
10403 type = lambda_capture_field_type (type);
10404 else if (DECLTYPE_FOR_LAMBDA_RETURN (t))
10405 type = lambda_return_type (type);
10407 type = finish_decltype_type
10408 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
10409 return cp_build_qualified_type_real (type,
10411 | cp_type_quals (type),
10415 case TYPE_ARGUMENT_PACK:
10416 case NONTYPE_ARGUMENT_PACK:
10418 tree r = TYPE_P (t)
10419 ? cxx_make_type (TREE_CODE (t))
10420 : make_node (TREE_CODE (t));
10422 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
10426 SET_ARGUMENT_PACK_ARGS (r, packed_out);
10428 /* For template nontype argument packs, also substitute into
10430 if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
10431 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
10438 sorry ("use of %qs in template",
10439 tree_code_name [(int) TREE_CODE (t)]);
10440 return error_mark_node;
10444 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
10445 type of the expression on the left-hand side of the "." or "->"
10449 tsubst_baselink (tree baselink, tree object_type,
10450 tree args, tsubst_flags_t complain, tree in_decl)
10453 tree qualifying_scope;
10456 tree template_args = 0;
10457 bool template_id_p = false;
10459 /* A baselink indicates a function from a base class. Both the
10460 BASELINK_ACCESS_BINFO and the base class referenced may
10461 indicate bases of the template class, rather than the
10462 instantiated class. In addition, lookups that were not
10463 ambiguous before may be ambiguous now. Therefore, we perform
10464 the lookup again. */
10465 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
10466 qualifying_scope = tsubst (qualifying_scope, args,
10467 complain, in_decl);
10468 fns = BASELINK_FUNCTIONS (baselink);
10469 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
10470 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
10472 template_id_p = true;
10473 template_args = TREE_OPERAND (fns, 1);
10474 fns = TREE_OPERAND (fns, 0);
10476 template_args = tsubst_template_args (template_args, args,
10477 complain, in_decl);
10479 name = DECL_NAME (get_first_fn (fns));
10480 if (IDENTIFIER_TYPENAME_P (name))
10481 name = mangle_conv_op_name_for_type (optype);
10482 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
10484 /* If lookup found a single function, mark it as used at this
10485 point. (If it lookup found multiple functions the one selected
10486 later by overload resolution will be marked as used at that
10488 if (BASELINK_P (baselink))
10489 fns = BASELINK_FUNCTIONS (baselink);
10490 if (!template_id_p && !really_overloaded_fn (fns))
10491 mark_used (OVL_CURRENT (fns));
10493 /* Add back the template arguments, if present. */
10494 if (BASELINK_P (baselink) && template_id_p)
10495 BASELINK_FUNCTIONS (baselink)
10496 = build_nt (TEMPLATE_ID_EXPR,
10497 BASELINK_FUNCTIONS (baselink),
10499 /* Update the conversion operator type. */
10500 BASELINK_OPTYPE (baselink) = optype;
10503 object_type = current_class_type;
10504 return adjust_result_of_qualified_name_lookup (baselink,
10509 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
10510 true if the qualified-id will be a postfix-expression in-and-of
10511 itself; false if more of the postfix-expression follows the
10512 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
10516 tsubst_qualified_id (tree qualified_id, tree args,
10517 tsubst_flags_t complain, tree in_decl,
10518 bool done, bool address_p)
10524 tree template_args;
10526 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
10528 /* Figure out what name to look up. */
10529 name = TREE_OPERAND (qualified_id, 1);
10530 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
10532 is_template = true;
10533 template_args = TREE_OPERAND (name, 1);
10535 template_args = tsubst_template_args (template_args, args,
10536 complain, in_decl);
10537 name = TREE_OPERAND (name, 0);
10541 is_template = false;
10542 template_args = NULL_TREE;
10545 /* Substitute into the qualifying scope. When there are no ARGS, we
10546 are just trying to simplify a non-dependent expression. In that
10547 case the qualifying scope may be dependent, and, in any case,
10548 substituting will not help. */
10549 scope = TREE_OPERAND (qualified_id, 0);
10552 scope = tsubst (scope, args, complain, in_decl);
10553 expr = tsubst_copy (name, args, complain, in_decl);
10558 if (dependent_type_p (scope))
10560 tree type = NULL_TREE;
10561 if (DECL_P (expr) && !dependent_scope_p (scope))
10562 type = TREE_TYPE (expr);
10563 return build_qualified_name (type, scope, expr,
10564 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
10567 if (!BASELINK_P (name) && !DECL_P (expr))
10569 if (TREE_CODE (expr) == BIT_NOT_EXPR)
10571 /* A BIT_NOT_EXPR is used to represent a destructor. */
10572 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
10574 error ("qualifying type %qT does not match destructor name ~%qT",
10575 scope, TREE_OPERAND (expr, 0));
10576 expr = error_mark_node;
10579 expr = lookup_qualified_name (scope, complete_dtor_identifier,
10580 /*is_type_p=*/0, false);
10583 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
10584 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
10585 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
10587 if (complain & tf_error)
10589 error ("dependent-name %qE is parsed as a non-type, but "
10590 "instantiation yields a type", qualified_id);
10591 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
10593 return error_mark_node;
10599 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
10601 /* Remember that there was a reference to this entity. */
10605 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
10607 if (complain & tf_error)
10608 qualified_name_lookup_error (scope,
10609 TREE_OPERAND (qualified_id, 1),
10610 expr, input_location);
10611 return error_mark_node;
10615 expr = lookup_template_function (expr, template_args);
10617 if (expr == error_mark_node && complain & tf_error)
10618 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
10619 expr, input_location);
10620 else if (TYPE_P (scope))
10622 expr = (adjust_result_of_qualified_name_lookup
10623 (expr, scope, current_class_type));
10624 expr = (finish_qualified_id_expr
10625 (scope, expr, done, address_p,
10626 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
10627 /*template_arg_p=*/false));
10630 /* Expressions do not generally have reference type. */
10631 if (TREE_CODE (expr) != SCOPE_REF
10632 /* However, if we're about to form a pointer-to-member, we just
10633 want the referenced member referenced. */
10634 && TREE_CODE (expr) != OFFSET_REF)
10635 expr = convert_from_reference (expr);
10640 /* Like tsubst, but deals with expressions. This function just replaces
10641 template parms; to finish processing the resultant expression, use
10645 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10647 enum tree_code code;
10650 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
10653 code = TREE_CODE (t);
10658 r = retrieve_local_specialization (t);
10663 /* This can happen for a parameter name used later in a function
10664 declaration (such as in a late-specified return type). Just
10665 make a dummy decl, since it's only used for its type. */
10666 gcc_assert (cp_unevaluated_operand != 0);
10667 /* We copy T because want to tsubst the PARM_DECL only,
10668 not the following PARM_DECLs that are chained to T. */
10670 r = tsubst_decl (c, args, complain);
10671 /* Give it the template pattern as its context; its true context
10672 hasn't been instantiated yet and this is good enough for
10674 DECL_CONTEXT (r) = DECL_CONTEXT (t);
10677 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
10678 r = ARGUMENT_PACK_SELECT_ARG (r);
10687 if (DECL_TEMPLATE_PARM_P (t))
10688 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
10689 /* There is no need to substitute into namespace-scope
10691 if (DECL_NAMESPACE_SCOPE_P (t))
10693 /* If ARGS is NULL, then T is known to be non-dependent. */
10694 if (args == NULL_TREE)
10695 return integral_constant_value (t);
10697 /* Unfortunately, we cannot just call lookup_name here.
10700 template <int I> int f() {
10702 struct S { void g() { E e = a; } };
10705 When we instantiate f<7>::S::g(), say, lookup_name is not
10706 clever enough to find f<7>::a. */
10708 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
10709 /*entering_scope=*/0);
10711 for (v = TYPE_VALUES (enum_type);
10713 v = TREE_CHAIN (v))
10714 if (TREE_PURPOSE (v) == DECL_NAME (t))
10715 return TREE_VALUE (v);
10717 /* We didn't find the name. That should never happen; if
10718 name-lookup found it during preliminary parsing, we
10719 should find it again here during instantiation. */
10720 gcc_unreachable ();
10725 if (DECL_CONTEXT (t))
10729 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
10730 /*entering_scope=*/1);
10731 if (ctx != DECL_CONTEXT (t))
10733 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
10736 if (complain & tf_error)
10737 error ("using invalid field %qD", t);
10738 return error_mark_node;
10747 case FUNCTION_DECL:
10748 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10749 || local_variable_p (t))
10750 t = tsubst (t, args, complain, in_decl);
10755 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
10757 case TEMPLATE_DECL:
10758 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10759 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
10760 args, complain, in_decl);
10761 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
10762 return tsubst (t, args, complain, in_decl);
10763 else if (DECL_CLASS_SCOPE_P (t)
10764 && uses_template_parms (DECL_CONTEXT (t)))
10766 /* Template template argument like the following example need
10769 template <template <class> class TT> struct C {};
10770 template <class T> struct D {
10771 template <class U> struct E {};
10776 We are processing the template argument `E' in #1 for
10777 the template instantiation #2. Originally, `E' is a
10778 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
10779 have to substitute this with one having context `D<int>'. */
10781 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
10782 return lookup_field (context, DECL_NAME(t), 0, false);
10785 /* Ordinary template template argument. */
10789 case REINTERPRET_CAST_EXPR:
10790 case CONST_CAST_EXPR:
10791 case STATIC_CAST_EXPR:
10792 case DYNAMIC_CAST_EXPR:
10795 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10796 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10799 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
10801 /* We only want to compute the number of arguments. */
10802 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
10803 complain, in_decl);
10806 if (TREE_CODE (expanded) == TREE_VEC)
10807 len = TREE_VEC_LENGTH (expanded);
10809 if (expanded == error_mark_node)
10810 return error_mark_node;
10811 else if (PACK_EXPANSION_P (expanded)
10812 || (TREE_CODE (expanded) == TREE_VEC
10814 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
10816 if (TREE_CODE (expanded) == TREE_VEC)
10817 expanded = TREE_VEC_ELT (expanded, len - 1);
10819 if (TYPE_P (expanded))
10820 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
10821 complain & tf_error);
10823 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
10824 complain & tf_error);
10827 return build_int_cst (size_type_node, len);
10833 case TRUTH_NOT_EXPR:
10836 case UNARY_PLUS_EXPR: /* Unary + */
10841 case REALPART_EXPR:
10842 case IMAGPART_EXPR:
10844 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10845 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10847 case COMPONENT_REF:
10852 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
10853 name = TREE_OPERAND (t, 1);
10854 if (TREE_CODE (name) == BIT_NOT_EXPR)
10856 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10857 complain, in_decl);
10858 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10860 else if (TREE_CODE (name) == SCOPE_REF
10861 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
10863 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
10864 complain, in_decl);
10865 name = TREE_OPERAND (name, 1);
10866 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10867 complain, in_decl);
10868 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10869 name = build_qualified_name (/*type=*/NULL_TREE,
10871 /*template_p=*/false);
10873 else if (TREE_CODE (name) == BASELINK)
10874 name = tsubst_baselink (name,
10875 non_reference (TREE_TYPE (object)),
10879 name = tsubst_copy (name, args, complain, in_decl);
10880 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
10886 case TRUNC_DIV_EXPR:
10887 case CEIL_DIV_EXPR:
10888 case FLOOR_DIV_EXPR:
10889 case ROUND_DIV_EXPR:
10890 case EXACT_DIV_EXPR:
10894 case TRUNC_MOD_EXPR:
10895 case FLOOR_MOD_EXPR:
10896 case TRUTH_ANDIF_EXPR:
10897 case TRUTH_ORIF_EXPR:
10898 case TRUTH_AND_EXPR:
10899 case TRUTH_OR_EXPR:
10912 case COMPOUND_EXPR:
10915 case PREDECREMENT_EXPR:
10916 case PREINCREMENT_EXPR:
10917 case POSTDECREMENT_EXPR:
10918 case POSTINCREMENT_EXPR:
10920 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10921 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10924 return build_qualified_name (/*type=*/NULL_TREE,
10925 tsubst_copy (TREE_OPERAND (t, 0),
10926 args, complain, in_decl),
10927 tsubst_copy (TREE_OPERAND (t, 1),
10928 args, complain, in_decl),
10929 QUALIFIED_NAME_IS_TEMPLATE (t));
10934 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10935 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10936 NULL_TREE, NULL_TREE);
10940 int n = VL_EXP_OPERAND_LENGTH (t);
10941 tree result = build_vl_exp (CALL_EXPR, n);
10943 for (i = 0; i < n; i++)
10944 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
10945 complain, in_decl);
10951 case PSEUDO_DTOR_EXPR:
10954 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10955 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10956 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10957 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
10964 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10965 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10966 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10967 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
10974 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10975 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10976 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
10977 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
10981 case TEMPLATE_ID_EXPR:
10983 /* Substituted template arguments */
10984 tree fn = TREE_OPERAND (t, 0);
10985 tree targs = TREE_OPERAND (t, 1);
10987 fn = tsubst_copy (fn, args, complain, in_decl);
10989 targs = tsubst_template_args (targs, args, complain, in_decl);
10991 return lookup_template_function (fn, targs);
10996 tree purpose, value, chain;
10998 if (t == void_list_node)
11001 purpose = TREE_PURPOSE (t);
11003 purpose = tsubst_copy (purpose, args, complain, in_decl);
11004 value = TREE_VALUE (t);
11006 value = tsubst_copy (value, args, complain, in_decl);
11007 chain = TREE_CHAIN (t);
11008 if (chain && chain != void_type_node)
11009 chain = tsubst_copy (chain, args, complain, in_decl);
11010 if (purpose == TREE_PURPOSE (t)
11011 && value == TREE_VALUE (t)
11012 && chain == TREE_CHAIN (t))
11014 return tree_cons (purpose, value, chain);
11019 case ENUMERAL_TYPE:
11021 case TEMPLATE_TYPE_PARM:
11022 case TEMPLATE_TEMPLATE_PARM:
11023 case BOUND_TEMPLATE_TEMPLATE_PARM:
11024 case TEMPLATE_PARM_INDEX:
11026 case REFERENCE_TYPE:
11028 case FUNCTION_TYPE:
11031 case TYPENAME_TYPE:
11032 case UNBOUND_CLASS_TEMPLATE:
11034 case DECLTYPE_TYPE:
11036 return tsubst (t, args, complain, in_decl);
11038 case IDENTIFIER_NODE:
11039 if (IDENTIFIER_TYPENAME_P (t))
11041 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11042 return mangle_conv_op_name_for_type (new_type);
11048 /* This is handled by tsubst_copy_and_build. */
11049 gcc_unreachable ();
11052 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
11054 tsubst (TREE_TYPE (t), args, complain, in_decl));
11056 case CLEANUP_POINT_EXPR:
11057 /* We shouldn't have built any of these during initial template
11058 generation. Instead, they should be built during instantiation
11059 in response to the saved STMT_IS_FULL_EXPR_P setting. */
11060 gcc_unreachable ();
11063 mark_used (TREE_OPERAND (t, 1));
11066 case EXPR_PACK_EXPANSION:
11067 error ("invalid use of pack expansion expression");
11068 return error_mark_node;
11070 case NONTYPE_ARGUMENT_PACK:
11071 error ("use %<...%> to expand argument pack");
11072 return error_mark_node;
11079 /* Like tsubst_copy, but specifically for OpenMP clauses. */
11082 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
11085 tree new_clauses = NULL, nc, oc;
11087 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
11089 nc = copy_node (oc);
11090 OMP_CLAUSE_CHAIN (nc) = new_clauses;
11093 switch (OMP_CLAUSE_CODE (nc))
11095 case OMP_CLAUSE_LASTPRIVATE:
11096 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
11098 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
11099 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
11100 in_decl, /*integral_constant_expression_p=*/false);
11101 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
11102 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
11105 case OMP_CLAUSE_PRIVATE:
11106 case OMP_CLAUSE_SHARED:
11107 case OMP_CLAUSE_FIRSTPRIVATE:
11108 case OMP_CLAUSE_REDUCTION:
11109 case OMP_CLAUSE_COPYIN:
11110 case OMP_CLAUSE_COPYPRIVATE:
11111 case OMP_CLAUSE_IF:
11112 case OMP_CLAUSE_NUM_THREADS:
11113 case OMP_CLAUSE_SCHEDULE:
11114 case OMP_CLAUSE_COLLAPSE:
11115 OMP_CLAUSE_OPERAND (nc, 0)
11116 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
11117 in_decl, /*integral_constant_expression_p=*/false);
11119 case OMP_CLAUSE_NOWAIT:
11120 case OMP_CLAUSE_ORDERED:
11121 case OMP_CLAUSE_DEFAULT:
11122 case OMP_CLAUSE_UNTIED:
11125 gcc_unreachable ();
11129 return finish_omp_clauses (nreverse (new_clauses));
11132 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
11135 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
11138 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
11140 tree purpose, value, chain;
11145 if (TREE_CODE (t) != TREE_LIST)
11146 return tsubst_copy_and_build (t, args, complain, in_decl,
11147 /*function_p=*/false,
11148 /*integral_constant_expression_p=*/false);
11150 if (t == void_list_node)
11153 purpose = TREE_PURPOSE (t);
11155 purpose = RECUR (purpose);
11156 value = TREE_VALUE (t);
11157 if (value && TREE_CODE (value) != LABEL_DECL)
11158 value = RECUR (value);
11159 chain = TREE_CHAIN (t);
11160 if (chain && chain != void_type_node)
11161 chain = RECUR (chain);
11162 return tree_cons (purpose, value, chain);
11166 /* Substitute one OMP_FOR iterator. */
11169 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
11170 tree condv, tree incrv, tree *clauses,
11171 tree args, tsubst_flags_t complain, tree in_decl,
11172 bool integral_constant_expression_p)
11174 #define RECUR(NODE) \
11175 tsubst_expr ((NODE), args, complain, in_decl, \
11176 integral_constant_expression_p)
11177 tree decl, init, cond, incr, auto_node;
11179 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
11180 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
11181 decl = RECUR (TREE_OPERAND (init, 0));
11182 init = TREE_OPERAND (init, 1);
11183 auto_node = type_uses_auto (TREE_TYPE (decl));
11184 if (auto_node && init)
11186 tree init_expr = init;
11187 if (TREE_CODE (init_expr) == DECL_EXPR)
11188 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
11189 init_expr = RECUR (init_expr);
11191 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
11193 gcc_assert (!type_dependent_expression_p (decl));
11195 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
11197 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
11198 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11199 if (TREE_CODE (incr) == MODIFY_EXPR)
11200 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
11201 RECUR (TREE_OPERAND (incr, 1)),
11204 incr = RECUR (incr);
11205 TREE_VEC_ELT (declv, i) = decl;
11206 TREE_VEC_ELT (initv, i) = init;
11207 TREE_VEC_ELT (condv, i) = cond;
11208 TREE_VEC_ELT (incrv, i) = incr;
11212 if (init && TREE_CODE (init) != DECL_EXPR)
11215 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11217 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
11218 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
11219 && OMP_CLAUSE_DECL (c) == decl)
11221 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
11222 && OMP_CLAUSE_DECL (c) == decl)
11223 error ("iteration variable %qD should not be firstprivate", decl);
11224 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
11225 && OMP_CLAUSE_DECL (c) == decl)
11226 error ("iteration variable %qD should not be reduction", decl);
11230 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
11231 OMP_CLAUSE_DECL (c) = decl;
11232 c = finish_omp_clauses (c);
11235 OMP_CLAUSE_CHAIN (c) = *clauses;
11240 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
11241 if (COMPARISON_CLASS_P (cond))
11242 cond = build2 (TREE_CODE (cond), boolean_type_node,
11243 RECUR (TREE_OPERAND (cond, 0)),
11244 RECUR (TREE_OPERAND (cond, 1)));
11246 cond = RECUR (cond);
11247 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11248 switch (TREE_CODE (incr))
11250 case PREINCREMENT_EXPR:
11251 case PREDECREMENT_EXPR:
11252 case POSTINCREMENT_EXPR:
11253 case POSTDECREMENT_EXPR:
11254 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
11255 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
11258 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11259 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11261 tree rhs = TREE_OPERAND (incr, 1);
11262 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11263 RECUR (TREE_OPERAND (incr, 0)),
11264 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11265 RECUR (TREE_OPERAND (rhs, 0)),
11266 RECUR (TREE_OPERAND (rhs, 1))));
11269 incr = RECUR (incr);
11272 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11273 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11275 tree lhs = RECUR (TREE_OPERAND (incr, 0));
11276 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
11277 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
11278 TREE_TYPE (decl), lhs,
11279 RECUR (TREE_OPERAND (incr, 2))));
11281 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
11282 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
11283 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
11285 tree rhs = TREE_OPERAND (incr, 2);
11286 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11287 RECUR (TREE_OPERAND (incr, 0)),
11288 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11289 RECUR (TREE_OPERAND (rhs, 0)),
11290 RECUR (TREE_OPERAND (rhs, 1))));
11293 incr = RECUR (incr);
11296 incr = RECUR (incr);
11300 TREE_VEC_ELT (declv, i) = decl;
11301 TREE_VEC_ELT (initv, i) = init;
11302 TREE_VEC_ELT (condv, i) = cond;
11303 TREE_VEC_ELT (incrv, i) = incr;
11307 /* Like tsubst_copy for expressions, etc. but also does semantic
11311 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
11312 bool integral_constant_expression_p)
11314 #define RECUR(NODE) \
11315 tsubst_expr ((NODE), args, complain, in_decl, \
11316 integral_constant_expression_p)
11320 if (t == NULL_TREE || t == error_mark_node)
11323 if (EXPR_HAS_LOCATION (t))
11324 input_location = EXPR_LOCATION (t);
11325 if (STATEMENT_CODE_P (TREE_CODE (t)))
11326 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
11328 switch (TREE_CODE (t))
11330 case STATEMENT_LIST:
11332 tree_stmt_iterator i;
11333 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
11334 RECUR (tsi_stmt (i));
11338 case CTOR_INITIALIZER:
11339 finish_mem_initializers (tsubst_initializer_list
11340 (TREE_OPERAND (t, 0), args));
11344 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
11348 tmp = RECUR (EXPR_STMT_EXPR (t));
11349 if (EXPR_STMT_STMT_EXPR_RESULT (t))
11350 finish_stmt_expr_expr (tmp, cur_stmt_expr);
11352 finish_expr_stmt (tmp);
11356 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
11364 decl = DECL_EXPR_DECL (t);
11365 if (TREE_CODE (decl) == LABEL_DECL)
11366 finish_label_decl (DECL_NAME (decl));
11367 else if (TREE_CODE (decl) == USING_DECL)
11369 tree scope = USING_DECL_SCOPE (decl);
11370 tree name = DECL_NAME (decl);
11373 scope = RECUR (scope);
11374 decl = lookup_qualified_name (scope, name,
11375 /*is_type_p=*/false,
11376 /*complain=*/false);
11377 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
11378 qualified_name_lookup_error (scope, name, decl, input_location);
11380 do_local_using_decl (decl, scope, name);
11384 init = DECL_INITIAL (decl);
11385 decl = tsubst (decl, args, complain, in_decl);
11386 if (decl != error_mark_node)
11388 /* By marking the declaration as instantiated, we avoid
11389 trying to instantiate it. Since instantiate_decl can't
11390 handle local variables, and since we've already done
11391 all that needs to be done, that's the right thing to
11393 if (TREE_CODE (decl) == VAR_DECL)
11394 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11395 if (TREE_CODE (decl) == VAR_DECL
11396 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
11397 /* Anonymous aggregates are a special case. */
11398 finish_anon_union (decl);
11401 maybe_push_decl (decl);
11402 if (TREE_CODE (decl) == VAR_DECL
11403 && DECL_PRETTY_FUNCTION_P (decl))
11405 /* For __PRETTY_FUNCTION__ we have to adjust the
11407 const char *const name
11408 = cxx_printable_name (current_function_decl, 2);
11409 init = cp_fname_init (name, &TREE_TYPE (decl));
11413 tree t = RECUR (init);
11416 /* If we had an initializer but it
11417 instantiated to nothing,
11418 value-initialize the object. This will
11419 only occur when the initializer was a
11420 pack expansion where the parameter packs
11421 used in that expansion were of length
11423 init = build_value_init (TREE_TYPE (decl));
11428 cp_finish_decl (decl, init, false, NULL_TREE, 0);
11433 /* A DECL_EXPR can also be used as an expression, in the condition
11434 clause of an if/for/while construct. */
11439 stmt = begin_for_stmt ();
11440 RECUR (FOR_INIT_STMT (t));
11441 finish_for_init_stmt (stmt);
11442 tmp = RECUR (FOR_COND (t));
11443 finish_for_cond (tmp, stmt);
11444 tmp = RECUR (FOR_EXPR (t));
11445 finish_for_expr (tmp, stmt);
11446 RECUR (FOR_BODY (t));
11447 finish_for_stmt (stmt);
11451 stmt = begin_while_stmt ();
11452 tmp = RECUR (WHILE_COND (t));
11453 finish_while_stmt_cond (tmp, stmt);
11454 RECUR (WHILE_BODY (t));
11455 finish_while_stmt (stmt);
11459 stmt = begin_do_stmt ();
11460 RECUR (DO_BODY (t));
11461 finish_do_body (stmt);
11462 tmp = RECUR (DO_COND (t));
11463 finish_do_stmt (tmp, stmt);
11467 stmt = begin_if_stmt ();
11468 tmp = RECUR (IF_COND (t));
11469 finish_if_stmt_cond (tmp, stmt);
11470 RECUR (THEN_CLAUSE (t));
11471 finish_then_clause (stmt);
11473 if (ELSE_CLAUSE (t))
11475 begin_else_clause (stmt);
11476 RECUR (ELSE_CLAUSE (t));
11477 finish_else_clause (stmt);
11480 finish_if_stmt (stmt);
11484 if (BIND_EXPR_BODY_BLOCK (t))
11485 stmt = begin_function_body ();
11487 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
11488 ? BCS_TRY_BLOCK : 0);
11490 RECUR (BIND_EXPR_BODY (t));
11492 if (BIND_EXPR_BODY_BLOCK (t))
11493 finish_function_body (stmt);
11495 finish_compound_stmt (stmt);
11499 finish_break_stmt ();
11502 case CONTINUE_STMT:
11503 finish_continue_stmt ();
11507 stmt = begin_switch_stmt ();
11508 tmp = RECUR (SWITCH_STMT_COND (t));
11509 finish_switch_cond (tmp, stmt);
11510 RECUR (SWITCH_STMT_BODY (t));
11511 finish_switch_stmt (stmt);
11514 case CASE_LABEL_EXPR:
11515 finish_case_label (EXPR_LOCATION (t),
11516 RECUR (CASE_LOW (t)),
11517 RECUR (CASE_HIGH (t)));
11522 tree decl = LABEL_EXPR_LABEL (t);
11525 label = finish_label_stmt (DECL_NAME (decl));
11526 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
11527 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
11532 tmp = GOTO_DESTINATION (t);
11533 if (TREE_CODE (tmp) != LABEL_DECL)
11534 /* Computed goto's must be tsubst'd into. On the other hand,
11535 non-computed gotos must not be; the identifier in question
11536 will have no binding. */
11539 tmp = DECL_NAME (tmp);
11540 finish_goto_stmt (tmp);
11544 tmp = finish_asm_stmt
11545 (ASM_VOLATILE_P (t),
11546 RECUR (ASM_STRING (t)),
11547 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
11548 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
11549 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
11550 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
11552 tree asm_expr = tmp;
11553 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
11554 asm_expr = TREE_OPERAND (asm_expr, 0);
11555 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
11562 stmt = begin_try_block ();
11563 RECUR (TRY_STMTS (t));
11564 finish_cleanup_try_block (stmt);
11565 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
11569 tree compound_stmt = NULL_TREE;
11571 if (FN_TRY_BLOCK_P (t))
11572 stmt = begin_function_try_block (&compound_stmt);
11574 stmt = begin_try_block ();
11576 RECUR (TRY_STMTS (t));
11578 if (FN_TRY_BLOCK_P (t))
11579 finish_function_try_block (stmt);
11581 finish_try_block (stmt);
11583 RECUR (TRY_HANDLERS (t));
11584 if (FN_TRY_BLOCK_P (t))
11585 finish_function_handler_sequence (stmt, compound_stmt);
11587 finish_handler_sequence (stmt);
11593 tree decl = HANDLER_PARMS (t);
11597 decl = tsubst (decl, args, complain, in_decl);
11598 /* Prevent instantiate_decl from trying to instantiate
11599 this variable. We've already done all that needs to be
11601 if (decl != error_mark_node)
11602 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11604 stmt = begin_handler ();
11605 finish_handler_parms (decl, stmt);
11606 RECUR (HANDLER_BODY (t));
11607 finish_handler (stmt);
11612 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
11615 case STATIC_ASSERT:
11618 tsubst_expr (STATIC_ASSERT_CONDITION (t),
11621 /*integral_constant_expression_p=*/true);
11622 finish_static_assert (condition,
11623 STATIC_ASSERT_MESSAGE (t),
11624 STATIC_ASSERT_SOURCE_LOCATION (t),
11625 /*member_p=*/false);
11630 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
11631 args, complain, in_decl);
11632 stmt = begin_omp_parallel ();
11633 RECUR (OMP_PARALLEL_BODY (t));
11634 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
11635 = OMP_PARALLEL_COMBINED (t);
11639 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
11640 args, complain, in_decl);
11641 stmt = begin_omp_task ();
11642 RECUR (OMP_TASK_BODY (t));
11643 finish_omp_task (tmp, stmt);
11648 tree clauses, body, pre_body;
11649 tree declv, initv, condv, incrv;
11652 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
11653 args, complain, in_decl);
11654 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11655 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11656 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11657 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11659 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
11660 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
11661 &clauses, args, complain, in_decl,
11662 integral_constant_expression_p);
11664 stmt = begin_omp_structured_block ();
11666 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
11667 if (TREE_VEC_ELT (initv, i) == NULL
11668 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
11669 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
11670 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
11672 tree init = RECUR (TREE_VEC_ELT (initv, i));
11673 gcc_assert (init == TREE_VEC_ELT (declv, i));
11674 TREE_VEC_ELT (initv, i) = NULL_TREE;
11678 tree decl_expr = TREE_VEC_ELT (initv, i);
11679 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
11680 gcc_assert (init != NULL);
11681 TREE_VEC_ELT (initv, i) = RECUR (init);
11682 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
11684 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
11687 pre_body = push_stmt_list ();
11688 RECUR (OMP_FOR_PRE_BODY (t));
11689 pre_body = pop_stmt_list (pre_body);
11691 body = push_stmt_list ();
11692 RECUR (OMP_FOR_BODY (t));
11693 body = pop_stmt_list (body);
11695 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
11696 body, pre_body, clauses);
11698 add_stmt (finish_omp_structured_block (stmt));
11704 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
11705 stmt = push_stmt_list ();
11706 RECUR (OMP_BODY (t));
11707 stmt = pop_stmt_list (stmt);
11710 OMP_BODY (t) = stmt;
11711 OMP_CLAUSES (t) = tmp;
11719 stmt = push_stmt_list ();
11720 RECUR (OMP_BODY (t));
11721 stmt = pop_stmt_list (stmt);
11724 OMP_BODY (t) = stmt;
11729 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
11731 tree op1 = TREE_OPERAND (t, 1);
11732 tree lhs = RECUR (TREE_OPERAND (op1, 0));
11733 tree rhs = RECUR (TREE_OPERAND (op1, 1));
11734 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
11738 case EXPR_PACK_EXPANSION:
11739 error ("invalid use of pack expansion expression");
11740 return error_mark_node;
11742 case NONTYPE_ARGUMENT_PACK:
11743 error ("use %<...%> to expand argument pack");
11744 return error_mark_node;
11747 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
11749 return tsubst_copy_and_build (t, args, complain, in_decl,
11750 /*function_p=*/false,
11751 integral_constant_expression_p);
11758 /* T is a postfix-expression that is not being used in a function
11759 call. Return the substituted version of T. */
11762 tsubst_non_call_postfix_expression (tree t, tree args,
11763 tsubst_flags_t complain,
11766 if (TREE_CODE (t) == SCOPE_REF)
11767 t = tsubst_qualified_id (t, args, complain, in_decl,
11768 /*done=*/false, /*address_p=*/false);
11770 t = tsubst_copy_and_build (t, args, complain, in_decl,
11771 /*function_p=*/false,
11772 /*integral_constant_expression_p=*/false);
11777 /* Like tsubst but deals with expressions and performs semantic
11778 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
11781 tsubst_copy_and_build (tree t,
11783 tsubst_flags_t complain,
11786 bool integral_constant_expression_p)
11788 #define RECUR(NODE) \
11789 tsubst_copy_and_build (NODE, args, complain, in_decl, \
11790 /*function_p=*/false, \
11791 integral_constant_expression_p)
11795 if (t == NULL_TREE || t == error_mark_node)
11798 switch (TREE_CODE (t))
11802 /* Fall through. */
11803 case IDENTIFIER_NODE:
11807 bool non_integral_constant_expression_p;
11808 const char *error_msg;
11810 if (IDENTIFIER_TYPENAME_P (t))
11812 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11813 t = mangle_conv_op_name_for_type (new_type);
11816 /* Look up the name. */
11817 decl = lookup_name (t);
11819 /* By convention, expressions use ERROR_MARK_NODE to indicate
11820 failure, not NULL_TREE. */
11821 if (decl == NULL_TREE)
11822 decl = error_mark_node;
11824 decl = finish_id_expression (t, decl, NULL_TREE,
11826 integral_constant_expression_p,
11827 /*allow_non_integral_constant_expression_p=*/false,
11828 &non_integral_constant_expression_p,
11829 /*template_p=*/false,
11831 /*address_p=*/false,
11832 /*template_arg_p=*/false,
11837 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
11838 decl = unqualified_name_lookup_error (decl);
11842 case TEMPLATE_ID_EXPR:
11845 tree templ = RECUR (TREE_OPERAND (t, 0));
11846 tree targs = TREE_OPERAND (t, 1);
11849 targs = tsubst_template_args (targs, args, complain, in_decl);
11851 if (TREE_CODE (templ) == COMPONENT_REF)
11853 object = TREE_OPERAND (templ, 0);
11854 templ = TREE_OPERAND (templ, 1);
11857 object = NULL_TREE;
11858 templ = lookup_template_function (templ, targs);
11861 return build3 (COMPONENT_REF, TREE_TYPE (templ),
11862 object, templ, NULL_TREE);
11864 return baselink_for_fns (templ);
11869 tree r = RECUR (TREE_OPERAND (t, 0));
11871 if (REFERENCE_REF_P (t))
11873 /* A type conversion to reference type will be enclosed in
11874 such an indirect ref, but the substitution of the cast
11875 will have also added such an indirect ref. */
11876 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
11877 r = convert_from_reference (r);
11880 r = build_x_indirect_ref (r, RO_UNARY_STAR, complain);
11886 (tsubst (TREE_TYPE (t), args, complain, in_decl),
11887 RECUR (TREE_OPERAND (t, 0)));
11890 case REINTERPRET_CAST_EXPR:
11891 case CONST_CAST_EXPR:
11892 case DYNAMIC_CAST_EXPR:
11893 case STATIC_CAST_EXPR:
11898 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11899 if (integral_constant_expression_p
11900 && !cast_valid_in_integral_constant_expression_p (type))
11902 if (complain & tf_error)
11903 error ("a cast to a type other than an integral or "
11904 "enumeration type cannot appear in a constant-expression");
11905 return error_mark_node;
11908 op = RECUR (TREE_OPERAND (t, 0));
11910 switch (TREE_CODE (t))
11913 return build_functional_cast (type, op, complain);
11914 case REINTERPRET_CAST_EXPR:
11915 return build_reinterpret_cast (type, op, complain);
11916 case CONST_CAST_EXPR:
11917 return build_const_cast (type, op, complain);
11918 case DYNAMIC_CAST_EXPR:
11919 return build_dynamic_cast (type, op, complain);
11920 case STATIC_CAST_EXPR:
11921 return build_static_cast (type, op, complain);
11923 gcc_unreachable ();
11927 case POSTDECREMENT_EXPR:
11928 case POSTINCREMENT_EXPR:
11929 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11930 args, complain, in_decl);
11931 return build_x_unary_op (TREE_CODE (t), op1, complain);
11933 case PREDECREMENT_EXPR:
11934 case PREINCREMENT_EXPR:
11938 case TRUTH_NOT_EXPR:
11939 case UNARY_PLUS_EXPR: /* Unary + */
11940 case REALPART_EXPR:
11941 case IMAGPART_EXPR:
11942 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
11946 op1 = TREE_OPERAND (t, 0);
11947 if (TREE_CODE (op1) == SCOPE_REF)
11948 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
11949 /*done=*/true, /*address_p=*/true);
11951 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
11953 if (TREE_CODE (op1) == LABEL_DECL)
11954 return finish_label_address_expr (DECL_NAME (op1),
11955 EXPR_LOCATION (op1));
11956 return build_x_unary_op (ADDR_EXPR, op1, complain);
11961 case TRUNC_DIV_EXPR:
11962 case CEIL_DIV_EXPR:
11963 case FLOOR_DIV_EXPR:
11964 case ROUND_DIV_EXPR:
11965 case EXACT_DIV_EXPR:
11969 case TRUNC_MOD_EXPR:
11970 case FLOOR_MOD_EXPR:
11971 case TRUTH_ANDIF_EXPR:
11972 case TRUTH_ORIF_EXPR:
11973 case TRUTH_AND_EXPR:
11974 case TRUTH_OR_EXPR:
11989 return build_x_binary_op
11991 RECUR (TREE_OPERAND (t, 0)),
11992 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
11994 : TREE_CODE (TREE_OPERAND (t, 0))),
11995 RECUR (TREE_OPERAND (t, 1)),
11996 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
11998 : TREE_CODE (TREE_OPERAND (t, 1))),
11999 /*overloaded_p=*/NULL,
12003 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
12004 /*address_p=*/false);
12006 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12007 args, complain, in_decl);
12008 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
12011 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12012 return tsubst_copy (t, args, complain, in_decl);
12016 op1 = TREE_OPERAND (t, 0);
12019 /* When there are no ARGS, we are trying to evaluate a
12020 non-dependent expression from the parser. Trying to do
12021 the substitutions may not work. */
12023 op1 = TREE_TYPE (op1);
12027 ++cp_unevaluated_operand;
12028 ++c_inhibit_evaluation_warnings;
12029 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12030 /*function_p=*/false,
12031 /*integral_constant_expression_p=*/false);
12032 --cp_unevaluated_operand;
12033 --c_inhibit_evaluation_warnings;
12036 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
12037 complain & tf_error);
12039 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
12040 complain & tf_error);
12044 tree r = build_x_modify_expr
12045 (RECUR (TREE_OPERAND (t, 0)),
12046 TREE_CODE (TREE_OPERAND (t, 1)),
12047 RECUR (TREE_OPERAND (t, 2)),
12049 /* TREE_NO_WARNING must be set if either the expression was
12050 parenthesized or it uses an operator such as >>= rather
12051 than plain assignment. In the former case, it was already
12052 set and must be copied. In the latter case,
12053 build_x_modify_expr sets it and it must not be reset
12055 if (TREE_NO_WARNING (t))
12056 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12061 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12062 args, complain, in_decl);
12063 /* Remember that there was a reference to this entity. */
12066 return build_x_arrow (op1);
12070 tree placement = RECUR (TREE_OPERAND (t, 0));
12071 tree init = RECUR (TREE_OPERAND (t, 3));
12072 VEC(tree,gc) *placement_vec;
12073 VEC(tree,gc) *init_vec;
12076 if (placement == NULL_TREE)
12077 placement_vec = NULL;
12080 placement_vec = make_tree_vector ();
12081 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
12082 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
12085 /* If there was an initializer in the original tree, but it
12086 instantiated to an empty list, then we should pass a
12087 non-NULL empty vector to tell build_new that it was an
12088 empty initializer() rather than no initializer. This can
12089 only happen when the initializer is a pack expansion whose
12090 parameter packs are of length zero. */
12091 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
12095 init_vec = make_tree_vector ();
12096 if (init == void_zero_node)
12097 gcc_assert (init_vec != NULL);
12100 for (; init != NULL_TREE; init = TREE_CHAIN (init))
12101 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
12105 ret = build_new (&placement_vec,
12106 RECUR (TREE_OPERAND (t, 1)),
12107 RECUR (TREE_OPERAND (t, 2)),
12109 NEW_EXPR_USE_GLOBAL (t),
12112 if (placement_vec != NULL)
12113 release_tree_vector (placement_vec);
12114 if (init_vec != NULL)
12115 release_tree_vector (init_vec);
12121 return delete_sanity
12122 (RECUR (TREE_OPERAND (t, 0)),
12123 RECUR (TREE_OPERAND (t, 1)),
12124 DELETE_EXPR_USE_VEC (t),
12125 DELETE_EXPR_USE_GLOBAL (t));
12127 case COMPOUND_EXPR:
12128 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
12129 RECUR (TREE_OPERAND (t, 1)),
12135 VEC(tree,gc) *call_args;
12136 unsigned int nargs, i;
12141 function = CALL_EXPR_FN (t);
12142 /* When we parsed the expression, we determined whether or
12143 not Koenig lookup should be performed. */
12144 koenig_p = KOENIG_LOOKUP_P (t);
12145 if (TREE_CODE (function) == SCOPE_REF)
12147 qualified_p = true;
12148 function = tsubst_qualified_id (function, args, complain, in_decl,
12150 /*address_p=*/false);
12154 if (TREE_CODE (function) == COMPONENT_REF)
12156 tree op = TREE_OPERAND (function, 1);
12158 qualified_p = (TREE_CODE (op) == SCOPE_REF
12159 || (BASELINK_P (op)
12160 && BASELINK_QUALIFIED_P (op)));
12163 qualified_p = false;
12165 function = tsubst_copy_and_build (function, args, complain,
12168 integral_constant_expression_p);
12170 if (BASELINK_P (function))
12171 qualified_p = true;
12174 nargs = call_expr_nargs (t);
12175 call_args = make_tree_vector ();
12176 for (i = 0; i < nargs; ++i)
12178 tree arg = CALL_EXPR_ARG (t, i);
12180 if (!PACK_EXPANSION_P (arg))
12181 VEC_safe_push (tree, gc, call_args,
12182 RECUR (CALL_EXPR_ARG (t, i)));
12185 /* Expand the pack expansion and push each entry onto
12187 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
12188 if (TREE_CODE (arg) == TREE_VEC)
12190 unsigned int len, j;
12192 len = TREE_VEC_LENGTH (arg);
12193 for (j = 0; j < len; ++j)
12195 tree value = TREE_VEC_ELT (arg, j);
12196 if (value != NULL_TREE)
12197 value = convert_from_reference (value);
12198 VEC_safe_push (tree, gc, call_args, value);
12203 /* A partial substitution. Add one entry. */
12204 VEC_safe_push (tree, gc, call_args, arg);
12209 /* We do not perform argument-dependent lookup if normal
12210 lookup finds a non-function, in accordance with the
12211 expected resolution of DR 218. */
12213 && ((is_overloaded_fn (function)
12214 /* If lookup found a member function, the Koenig lookup is
12215 not appropriate, even if an unqualified-name was used
12216 to denote the function. */
12217 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
12218 || TREE_CODE (function) == IDENTIFIER_NODE)
12219 /* Only do this when substitution turns a dependent call
12220 into a non-dependent call. */
12221 && type_dependent_expression_p_push (t)
12222 && !any_type_dependent_arguments_p (call_args))
12223 function = perform_koenig_lookup (function, call_args);
12225 if (TREE_CODE (function) == IDENTIFIER_NODE)
12227 unqualified_name_lookup_error (function);
12228 release_tree_vector (call_args);
12229 return error_mark_node;
12232 /* Remember that there was a reference to this entity. */
12233 if (DECL_P (function))
12234 mark_used (function);
12236 if (TREE_CODE (function) == OFFSET_REF)
12237 ret = build_offset_ref_call_from_tree (function, &call_args);
12238 else if (TREE_CODE (function) == COMPONENT_REF)
12240 if (!BASELINK_P (TREE_OPERAND (function, 1)))
12241 ret = finish_call_expr (function, &call_args,
12242 /*disallow_virtual=*/false,
12243 /*koenig_p=*/false,
12246 ret = (build_new_method_call
12247 (TREE_OPERAND (function, 0),
12248 TREE_OPERAND (function, 1),
12249 &call_args, NULL_TREE,
12250 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
12255 ret = finish_call_expr (function, &call_args,
12256 /*disallow_virtual=*/qualified_p,
12260 release_tree_vector (call_args);
12266 return build_x_conditional_expr
12267 (RECUR (TREE_OPERAND (t, 0)),
12268 RECUR (TREE_OPERAND (t, 1)),
12269 RECUR (TREE_OPERAND (t, 2)),
12272 case PSEUDO_DTOR_EXPR:
12273 return finish_pseudo_destructor_expr
12274 (RECUR (TREE_OPERAND (t, 0)),
12275 RECUR (TREE_OPERAND (t, 1)),
12276 RECUR (TREE_OPERAND (t, 2)));
12280 tree purpose, value, chain;
12282 if (t == void_list_node)
12285 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
12286 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
12288 /* We have pack expansions, so expand those and
12289 create a new list out of it. */
12290 tree purposevec = NULL_TREE;
12291 tree valuevec = NULL_TREE;
12295 /* Expand the argument expressions. */
12296 if (TREE_PURPOSE (t))
12297 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
12298 complain, in_decl);
12299 if (TREE_VALUE (t))
12300 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
12301 complain, in_decl);
12303 /* Build the rest of the list. */
12304 chain = TREE_CHAIN (t);
12305 if (chain && chain != void_type_node)
12306 chain = RECUR (chain);
12308 /* Determine the number of arguments. */
12309 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
12311 len = TREE_VEC_LENGTH (purposevec);
12312 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
12314 else if (TREE_CODE (valuevec) == TREE_VEC)
12315 len = TREE_VEC_LENGTH (valuevec);
12318 /* Since we only performed a partial substitution into
12319 the argument pack, we only return a single list
12321 if (purposevec == TREE_PURPOSE (t)
12322 && valuevec == TREE_VALUE (t)
12323 && chain == TREE_CHAIN (t))
12326 return tree_cons (purposevec, valuevec, chain);
12329 /* Convert the argument vectors into a TREE_LIST */
12333 /* Grab the Ith values. */
12335 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
12338 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
12341 /* Build the list (backwards). */
12342 chain = tree_cons (purpose, value, chain);
12348 purpose = TREE_PURPOSE (t);
12350 purpose = RECUR (purpose);
12351 value = TREE_VALUE (t);
12353 value = RECUR (value);
12354 chain = TREE_CHAIN (t);
12355 if (chain && chain != void_type_node)
12356 chain = RECUR (chain);
12357 if (purpose == TREE_PURPOSE (t)
12358 && value == TREE_VALUE (t)
12359 && chain == TREE_CHAIN (t))
12361 return tree_cons (purpose, value, chain);
12364 case COMPONENT_REF:
12370 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12371 args, complain, in_decl);
12372 /* Remember that there was a reference to this entity. */
12373 if (DECL_P (object))
12374 mark_used (object);
12375 object_type = TREE_TYPE (object);
12377 member = TREE_OPERAND (t, 1);
12378 if (BASELINK_P (member))
12379 member = tsubst_baselink (member,
12380 non_reference (TREE_TYPE (object)),
12381 args, complain, in_decl);
12383 member = tsubst_copy (member, args, complain, in_decl);
12384 if (member == error_mark_node)
12385 return error_mark_node;
12387 if (object_type && !CLASS_TYPE_P (object_type))
12389 if (SCALAR_TYPE_P (object_type))
12391 tree s = NULL_TREE;
12392 tree dtor = member;
12394 if (TREE_CODE (dtor) == SCOPE_REF)
12396 s = TREE_OPERAND (dtor, 0);
12397 dtor = TREE_OPERAND (dtor, 1);
12399 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
12401 dtor = TREE_OPERAND (dtor, 0);
12403 return finish_pseudo_destructor_expr (object, s, dtor);
12407 else if (TREE_CODE (member) == SCOPE_REF
12408 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
12413 /* Lookup the template functions now that we know what the
12415 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
12416 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
12417 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
12418 /*is_type_p=*/false,
12419 /*complain=*/false);
12420 if (BASELINK_P (member))
12422 BASELINK_FUNCTIONS (member)
12423 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
12425 member = (adjust_result_of_qualified_name_lookup
12426 (member, BINFO_TYPE (BASELINK_BINFO (member)),
12431 qualified_name_lookup_error (object_type, tmpl, member,
12433 return error_mark_node;
12436 else if (TREE_CODE (member) == SCOPE_REF
12437 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
12438 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
12440 if (complain & tf_error)
12442 if (TYPE_P (TREE_OPERAND (member, 0)))
12443 error ("%qT is not a class or namespace",
12444 TREE_OPERAND (member, 0));
12446 error ("%qD is not a class or namespace",
12447 TREE_OPERAND (member, 0));
12449 return error_mark_node;
12451 else if (TREE_CODE (member) == FIELD_DECL)
12452 return finish_non_static_data_member (member, object, NULL_TREE);
12454 return finish_class_member_access_expr (object, member,
12455 /*template_p=*/false,
12461 (RECUR (TREE_OPERAND (t, 0)));
12465 VEC(constructor_elt,gc) *n;
12466 constructor_elt *ce;
12467 unsigned HOST_WIDE_INT idx;
12468 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12469 bool process_index_p;
12471 bool need_copy_p = false;
12474 if (type == error_mark_node)
12475 return error_mark_node;
12477 /* digest_init will do the wrong thing if we let it. */
12478 if (type && TYPE_PTRMEMFUNC_P (type))
12481 /* We do not want to process the index of aggregate
12482 initializers as they are identifier nodes which will be
12483 looked up by digest_init. */
12484 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
12486 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
12487 newlen = VEC_length (constructor_elt, n);
12488 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
12490 if (ce->index && process_index_p)
12491 ce->index = RECUR (ce->index);
12493 if (PACK_EXPANSION_P (ce->value))
12495 /* Substitute into the pack expansion. */
12496 ce->value = tsubst_pack_expansion (ce->value, args, complain,
12499 if (ce->value == error_mark_node)
12501 else if (TREE_VEC_LENGTH (ce->value) == 1)
12502 /* Just move the argument into place. */
12503 ce->value = TREE_VEC_ELT (ce->value, 0);
12506 /* Update the length of the final CONSTRUCTOR
12507 arguments vector, and note that we will need to
12509 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
12510 need_copy_p = true;
12514 ce->value = RECUR (ce->value);
12519 VEC(constructor_elt,gc) *old_n = n;
12521 n = VEC_alloc (constructor_elt, gc, newlen);
12522 for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
12525 if (TREE_CODE (ce->value) == TREE_VEC)
12527 int i, len = TREE_VEC_LENGTH (ce->value);
12528 for (i = 0; i < len; ++i)
12529 CONSTRUCTOR_APPEND_ELT (n, 0,
12530 TREE_VEC_ELT (ce->value, i));
12533 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
12537 r = build_constructor (init_list_type_node, n);
12538 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
12540 if (TREE_HAS_CONSTRUCTOR (t))
12541 return finish_compound_literal (type, r);
12548 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
12549 if (TYPE_P (operand_0))
12550 return get_typeid (operand_0);
12551 return build_typeid (operand_0);
12561 tree r = tsubst_copy (t, args, complain, in_decl);
12563 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
12564 /* If the original type was a reference, we'll be wrapped in
12565 the appropriate INDIRECT_REF. */
12566 r = convert_from_reference (r);
12571 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
12572 tsubst_copy (TREE_TYPE (t), args, complain,
12575 case OFFSETOF_EXPR:
12576 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
12580 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
12581 complain, in_decl);
12583 tree type2 = TRAIT_EXPR_TYPE2 (t);
12585 type2 = tsubst_copy (type2, args, complain, in_decl);
12587 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
12592 tree old_stmt_expr = cur_stmt_expr;
12593 tree stmt_expr = begin_stmt_expr ();
12595 cur_stmt_expr = stmt_expr;
12596 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
12597 integral_constant_expression_p);
12598 stmt_expr = finish_stmt_expr (stmt_expr, false);
12599 cur_stmt_expr = old_stmt_expr;
12601 /* If the resulting list of expression statement is empty,
12602 fold it further into void_zero_node. */
12603 if (empty_expr_stmt_p (stmt_expr))
12604 stmt_expr = void_zero_node;
12610 t = tsubst_copy (t, args, complain, in_decl);
12611 /* As in finish_id_expression, we resolve enumeration constants
12612 to their underlying values. */
12613 if (TREE_CODE (t) == CONST_DECL)
12615 used_types_insert (TREE_TYPE (t));
12616 return DECL_INITIAL (t);
12622 tree r = build_lambda_expr ();
12624 tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12625 TREE_TYPE (r) = type;
12626 CLASSTYPE_LAMBDA_EXPR (type) = r;
12628 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
12629 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
12630 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
12631 LAMBDA_EXPR_DISCRIMINATOR (r)
12632 = (LAMBDA_EXPR_DISCRIMINATOR (t));
12633 LAMBDA_EXPR_CAPTURE_LIST (r)
12634 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
12635 LAMBDA_EXPR_THIS_CAPTURE (r)
12636 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t));
12637 LAMBDA_EXPR_EXTRA_SCOPE (r)
12638 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
12640 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
12641 determine_visibility (TYPE_NAME (type));
12642 /* Now that we know visibility, instantiate the type so we have a
12643 declaration of the op() for later calls to lambda_function. */
12644 complete_type (type);
12646 type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
12648 apply_lambda_return_type (r, type);
12650 return build_lambda_object (r);
12654 /* Handle Objective-C++ constructs, if appropriate. */
12657 = objcp_tsubst_copy_and_build (t, args, complain,
12658 in_decl, /*function_p=*/false);
12662 return tsubst_copy (t, args, complain, in_decl);
12668 /* Verify that the instantiated ARGS are valid. For type arguments,
12669 make sure that the type's linkage is ok. For non-type arguments,
12670 make sure they are constants if they are integral or enumerations.
12671 Emit an error under control of COMPLAIN, and return TRUE on error. */
12674 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
12676 if (ARGUMENT_PACK_P (t))
12678 tree vec = ARGUMENT_PACK_ARGS (t);
12679 int len = TREE_VEC_LENGTH (vec);
12680 bool result = false;
12683 for (i = 0; i < len; ++i)
12684 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
12688 else if (TYPE_P (t))
12690 /* [basic.link]: A name with no linkage (notably, the name
12691 of a class or enumeration declared in a local scope)
12692 shall not be used to declare an entity with linkage.
12693 This implies that names with no linkage cannot be used as
12696 DR 757 relaxes this restriction for C++0x. */
12697 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
12698 : no_linkage_check (t, /*relaxed_p=*/false));
12702 /* DR 488 makes use of a type with no linkage cause
12703 type deduction to fail. */
12704 if (complain & tf_error)
12706 if (TYPE_ANONYMOUS_P (nt))
12707 error ("%qT is/uses anonymous type", t);
12709 error ("template argument for %qD uses local type %qT",
12714 /* In order to avoid all sorts of complications, we do not
12715 allow variably-modified types as template arguments. */
12716 else if (variably_modified_type_p (t, NULL_TREE))
12718 if (complain & tf_error)
12719 error ("%qT is a variably modified type", t);
12723 /* A non-type argument of integral or enumerated type must be a
12725 else if (TREE_TYPE (t)
12726 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
12727 && !TREE_CONSTANT (t))
12729 if (complain & tf_error)
12730 error ("integral expression %qE is not constant", t);
12737 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
12739 int ix, len = DECL_NTPARMS (tmpl);
12740 bool result = false;
12742 for (ix = 0; ix != len; ix++)
12744 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
12747 if (result && (complain & tf_error))
12748 error (" trying to instantiate %qD", tmpl);
12752 /* Instantiate the indicated variable or function template TMPL with
12753 the template arguments in TARG_PTR. */
12756 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
12758 tree targ_ptr = orig_args;
12762 HOST_WIDE_INT saved_processing_template_decl;
12764 if (tmpl == error_mark_node)
12765 return error_mark_node;
12767 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
12769 /* If this function is a clone, handle it specially. */
12770 if (DECL_CLONED_FUNCTION_P (tmpl))
12775 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
12776 DECL_CLONED_FUNCTION. */
12777 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
12778 targ_ptr, complain);
12779 if (spec == error_mark_node)
12780 return error_mark_node;
12782 /* Look for the clone. */
12783 FOR_EACH_CLONE (clone, spec)
12784 if (DECL_NAME (clone) == DECL_NAME (tmpl))
12786 /* We should always have found the clone by now. */
12787 gcc_unreachable ();
12791 /* Check to see if we already have this specialization. */
12792 gen_tmpl = most_general_template (tmpl);
12793 if (tmpl != gen_tmpl)
12794 /* The TMPL is a partial instantiation. To get a full set of
12795 arguments we must add the arguments used to perform the
12796 partial instantiation. */
12797 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
12800 /* It would be nice to avoid hashing here and then again in tsubst_decl,
12801 but it doesn't seem to be on the hot path. */
12802 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
12804 gcc_assert (tmpl == gen_tmpl
12805 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
12807 || fndecl == NULL_TREE);
12809 if (spec != NULL_TREE)
12812 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
12814 return error_mark_node;
12816 /* We are building a FUNCTION_DECL, during which the access of its
12817 parameters and return types have to be checked. However this
12818 FUNCTION_DECL which is the desired context for access checking
12819 is not built yet. We solve this chicken-and-egg problem by
12820 deferring all checks until we have the FUNCTION_DECL. */
12821 push_deferring_access_checks (dk_deferred);
12823 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
12824 (because, for example, we have encountered a non-dependent
12825 function call in the body of a template function and must now
12826 determine which of several overloaded functions will be called),
12827 within the instantiation itself we are not processing a
12829 saved_processing_template_decl = processing_template_decl;
12830 processing_template_decl = 0;
12831 /* Substitute template parameters to obtain the specialization. */
12832 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
12833 targ_ptr, complain, gen_tmpl);
12834 processing_template_decl = saved_processing_template_decl;
12835 if (fndecl == error_mark_node)
12836 return error_mark_node;
12838 /* Now we know the specialization, compute access previously
12840 push_access_scope (fndecl);
12842 /* Some typedefs referenced from within the template code need to be access
12843 checked at template instantiation time, i.e now. These types were
12844 added to the template at parsing time. Let's get those and perfom
12845 the acces checks then. */
12846 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
12847 perform_deferred_access_checks ();
12848 pop_access_scope (fndecl);
12849 pop_deferring_access_checks ();
12851 /* The DECL_TI_TEMPLATE should always be the immediate parent
12852 template, not the most general template. */
12853 DECL_TI_TEMPLATE (fndecl) = tmpl;
12855 /* If we've just instantiated the main entry point for a function,
12856 instantiate all the alternate entry points as well. We do this
12857 by cloning the instantiation of the main entry point, not by
12858 instantiating the template clones. */
12859 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
12860 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
12865 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
12866 NARGS elements of the arguments that are being used when calling
12867 it. TARGS is a vector into which the deduced template arguments
12870 Return zero for success, 2 for an incomplete match that doesn't resolve
12871 all the types, and 1 for complete failure. An error message will be
12872 printed only for an incomplete match.
12874 If FN is a conversion operator, or we are trying to produce a specific
12875 specialization, RETURN_TYPE is the return type desired.
12877 The EXPLICIT_TARGS are explicit template arguments provided via a
12880 The parameter STRICT is one of:
12883 We are deducing arguments for a function call, as in
12884 [temp.deduct.call].
12887 We are deducing arguments for a conversion function, as in
12888 [temp.deduct.conv].
12891 We are deducing arguments when doing an explicit instantiation
12892 as in [temp.explicit], when determining an explicit specialization
12893 as in [temp.expl.spec], or when taking the address of a function
12894 template, as in [temp.deduct.funcaddr]. */
12897 fn_type_unification (tree fn,
12898 tree explicit_targs,
12901 unsigned int nargs,
12903 unification_kind_t strict,
12909 bool incomplete_argument_packs_p = false;
12911 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
12913 fntype = TREE_TYPE (fn);
12914 if (explicit_targs)
12918 The specified template arguments must match the template
12919 parameters in kind (i.e., type, nontype, template), and there
12920 must not be more arguments than there are parameters;
12921 otherwise type deduction fails.
12923 Nontype arguments must match the types of the corresponding
12924 nontype template parameters, or must be convertible to the
12925 types of the corresponding nontype parameters as specified in
12926 _temp.arg.nontype_, otherwise type deduction fails.
12928 All references in the function type of the function template
12929 to the corresponding template parameters are replaced by the
12930 specified template argument values. If a substitution in a
12931 template parameter or in the function type of the function
12932 template results in an invalid type, type deduction fails. */
12933 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
12934 int i, len = TREE_VEC_LENGTH (tparms);
12935 tree converted_args;
12936 bool incomplete = false;
12938 if (explicit_targs == error_mark_node)
12942 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
12943 /*require_all_args=*/false,
12944 /*use_default_args=*/false));
12945 if (converted_args == error_mark_node)
12948 /* Substitute the explicit args into the function type. This is
12949 necessary so that, for instance, explicitly declared function
12950 arguments can match null pointed constants. If we were given
12951 an incomplete set of explicit args, we must not do semantic
12952 processing during substitution as we could create partial
12954 for (i = 0; i < len; i++)
12956 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
12957 bool parameter_pack = false;
12959 /* Dig out the actual parm. */
12960 if (TREE_CODE (parm) == TYPE_DECL
12961 || TREE_CODE (parm) == TEMPLATE_DECL)
12963 parm = TREE_TYPE (parm);
12964 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
12966 else if (TREE_CODE (parm) == PARM_DECL)
12968 parm = DECL_INITIAL (parm);
12969 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
12972 if (parameter_pack)
12976 template_parm_level_and_index (parm, &level, &idx);
12978 /* Mark the argument pack as "incomplete". We could
12979 still deduce more arguments during unification. */
12980 targ = TMPL_ARG (converted_args, level, idx);
12983 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
12984 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
12985 = ARGUMENT_PACK_ARGS (targ);
12988 /* We have some incomplete argument packs. */
12989 incomplete_argument_packs_p = true;
12993 if (incomplete_argument_packs_p)
12994 /* Any substitution is guaranteed to be incomplete if there
12995 are incomplete argument packs, because we can still deduce
12999 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
13001 processing_template_decl += incomplete;
13002 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
13003 processing_template_decl -= incomplete;
13005 if (fntype == error_mark_node)
13008 /* Place the explicitly specified arguments in TARGS. */
13009 for (i = NUM_TMPL_ARGS (converted_args); i--;)
13010 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
13013 /* Never do unification on the 'this' parameter. */
13014 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
13020 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
13021 new_args = XALLOCAVEC (tree, nargs + 1);
13022 new_args[0] = return_type;
13023 memcpy (new_args + 1, args, nargs * sizeof (tree));
13028 /* We allow incomplete unification without an error message here
13029 because the standard doesn't seem to explicitly prohibit it. Our
13030 callers must be ready to deal with unification failures in any
13032 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
13033 targs, parms, args, nargs, /*subr=*/0,
13036 if (result == 0 && incomplete_argument_packs_p)
13038 int i, len = NUM_TMPL_ARGS (targs);
13040 /* Clear the "incomplete" flags on all argument packs. */
13041 for (i = 0; i < len; i++)
13043 tree arg = TREE_VEC_ELT (targs, i);
13044 if (ARGUMENT_PACK_P (arg))
13046 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
13047 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
13052 /* Now that we have bindings for all of the template arguments,
13053 ensure that the arguments deduced for the template template
13054 parameters have compatible template parameter lists. We cannot
13055 check this property before we have deduced all template
13056 arguments, because the template parameter types of a template
13057 template parameter might depend on prior template parameters
13058 deduced after the template template parameter. The following
13059 ill-formed example illustrates this issue:
13061 template<typename T, template<T> class C> void f(C<5>, T);
13063 template<int N> struct X {};
13066 f(X<5>(), 5l); // error: template argument deduction fails
13069 The template parameter list of 'C' depends on the template type
13070 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
13071 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
13072 time that we deduce 'C'. */
13074 && !template_template_parm_bindings_ok_p
13075 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
13079 /* All is well so far. Now, check:
13083 When all template arguments have been deduced, all uses of
13084 template parameters in nondeduced contexts are replaced with
13085 the corresponding deduced argument values. If the
13086 substitution results in an invalid type, as described above,
13087 type deduction fails. */
13089 tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
13090 if (substed == error_mark_node)
13093 /* If we're looking for an exact match, check that what we got
13094 is indeed an exact match. It might not be if some template
13095 parameters are used in non-deduced contexts. */
13096 if (strict == DEDUCE_EXACT)
13101 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
13103 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
13104 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
13105 if (!same_type_p (args[i], TREE_VALUE (sarg)))
13113 /* Adjust types before performing type deduction, as described in
13114 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
13115 sections are symmetric. PARM is the type of a function parameter
13116 or the return type of the conversion function. ARG is the type of
13117 the argument passed to the call, or the type of the value
13118 initialized with the result of the conversion function.
13119 ARG_EXPR is the original argument expression, which may be null. */
13122 maybe_adjust_types_for_deduction (unification_kind_t strict,
13136 /* Swap PARM and ARG throughout the remainder of this
13137 function; the handling is precisely symmetric since PARM
13138 will initialize ARG rather than vice versa. */
13146 /* Core issue #873: Do the DR606 thing (see below) for these cases,
13147 too, but here handle it by stripping the reference from PARM
13148 rather than by adding it to ARG. */
13149 if (TREE_CODE (*parm) == REFERENCE_TYPE
13150 && TYPE_REF_IS_RVALUE (*parm)
13151 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13152 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13153 && TREE_CODE (*arg) == REFERENCE_TYPE
13154 && !TYPE_REF_IS_RVALUE (*arg))
13155 *parm = TREE_TYPE (*parm);
13156 /* Nothing else to do in this case. */
13160 gcc_unreachable ();
13163 if (TREE_CODE (*parm) != REFERENCE_TYPE)
13165 /* [temp.deduct.call]
13167 If P is not a reference type:
13169 --If A is an array type, the pointer type produced by the
13170 array-to-pointer standard conversion (_conv.array_) is
13171 used in place of A for type deduction; otherwise,
13173 --If A is a function type, the pointer type produced by
13174 the function-to-pointer standard conversion
13175 (_conv.func_) is used in place of A for type deduction;
13178 --If A is a cv-qualified type, the top level
13179 cv-qualifiers of A's type are ignored for type
13181 if (TREE_CODE (*arg) == ARRAY_TYPE)
13182 *arg = build_pointer_type (TREE_TYPE (*arg));
13183 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
13184 *arg = build_pointer_type (*arg);
13186 *arg = TYPE_MAIN_VARIANT (*arg);
13189 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
13190 of the form T&&, where T is a template parameter, and the argument
13191 is an lvalue, T is deduced as A& */
13192 if (TREE_CODE (*parm) == REFERENCE_TYPE
13193 && TYPE_REF_IS_RVALUE (*parm)
13194 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13195 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13196 && arg_expr && real_lvalue_p (arg_expr))
13197 *arg = build_reference_type (*arg);
13199 /* [temp.deduct.call]
13201 If P is a cv-qualified type, the top level cv-qualifiers
13202 of P's type are ignored for type deduction. If P is a
13203 reference type, the type referred to by P is used for
13205 *parm = TYPE_MAIN_VARIANT (*parm);
13206 if (TREE_CODE (*parm) == REFERENCE_TYPE)
13208 *parm = TREE_TYPE (*parm);
13209 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
13212 /* DR 322. For conversion deduction, remove a reference type on parm
13213 too (which has been swapped into ARG). */
13214 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
13215 *arg = TREE_TYPE (*arg);
13220 /* Most parms like fn_type_unification.
13222 If SUBR is 1, we're being called recursively (to unify the
13223 arguments of a function or method parameter of a function
13227 type_unification_real (tree tparms,
13231 unsigned int xnargs,
13233 unification_kind_t strict,
13236 tree parm, arg, arg_expr;
13238 int ntparms = TREE_VEC_LENGTH (tparms);
13240 int saw_undeduced = 0;
13243 unsigned int nargs;
13246 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
13247 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
13248 gcc_assert (ntparms > 0);
13253 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
13254 | UNIFY_ALLOW_DERIVED);
13258 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13262 sub_strict = UNIFY_ALLOW_NONE;
13266 gcc_unreachable ();
13275 while (parms && parms != void_list_node
13278 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13281 parm = TREE_VALUE (parms);
13282 parms = TREE_CHAIN (parms);
13287 if (arg == error_mark_node)
13289 if (arg == unknown_type_node)
13290 /* We can't deduce anything from this, but we might get all the
13291 template args from other function args. */
13294 /* Conversions will be performed on a function argument that
13295 corresponds with a function parameter that contains only
13296 non-deducible template parameters and explicitly specified
13297 template parameters. */
13298 if (!uses_template_parms (parm))
13303 type = TREE_TYPE (arg);
13307 if (same_type_p (parm, type))
13309 if (strict != DEDUCE_EXACT
13310 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
13319 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
13320 if (type_unknown_p (arg))
13322 /* [temp.deduct.type]
13324 A template-argument can be deduced from a pointer to
13325 function or pointer to member function argument if
13326 the set of overloaded functions does not contain
13327 function templates and at most one of a set of
13328 overloaded functions provides a unique match. */
13329 if (resolve_overloaded_unification
13330 (tparms, targs, parm, arg, strict, sub_strict))
13336 arg = unlowered_expr_type (arg);
13337 if (arg == error_mark_node)
13342 int arg_strict = sub_strict;
13345 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
13348 if (arg == init_list_type_node && arg_expr)
13350 if (unify (tparms, targs, parm, arg, arg_strict))
13357 && parms != void_list_node
13358 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13360 /* Unify the remaining arguments with the pack expansion type. */
13362 tree parmvec = make_tree_vec (1);
13364 /* Allocate a TREE_VEC and copy in all of the arguments */
13365 argvec = make_tree_vec (nargs - ia);
13366 for (i = 0; ia < nargs; ++ia, ++i)
13367 TREE_VEC_ELT (argvec, i) = args[ia];
13369 /* Copy the parameter into parmvec. */
13370 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
13371 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
13372 /*call_args_p=*/true, /*subr=*/subr))
13375 /* Advance to the end of the list of parameters. */
13376 parms = TREE_CHAIN (parms);
13379 /* Fail if we've reached the end of the parm list, and more args
13380 are present, and the parm list isn't variadic. */
13381 if (ia < nargs && parms == void_list_node)
13383 /* Fail if parms are left and they don't have default values. */
13384 if (parms && parms != void_list_node
13385 && TREE_PURPOSE (parms) == NULL_TREE)
13389 for (i = 0; i < ntparms; i++)
13390 if (!TREE_VEC_ELT (targs, i))
13394 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
13397 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13399 /* If this is an undeduced nontype parameter that depends on
13400 a type parameter, try another pass; its type may have been
13401 deduced from a later argument than the one from which
13402 this parameter can be deduced. */
13403 if (TREE_CODE (tparm) == PARM_DECL
13404 && uses_template_parms (TREE_TYPE (tparm))
13405 && !saw_undeduced++)
13408 /* Core issue #226 (C++0x) [temp.deduct]:
13410 If a template argument has not been deduced, its
13411 default template argument, if any, is used.
13413 When we are in C++98 mode, TREE_PURPOSE will either
13414 be NULL_TREE or ERROR_MARK_NODE, so we do not need
13415 to explicitly check cxx_dialect here. */
13416 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
13418 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13419 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
13420 arg = tsubst_template_arg (arg, targs, tf_none, NULL_TREE);
13421 arg = convert_template_argument (parm, arg, targs, tf_none,
13423 if (arg == error_mark_node)
13427 TREE_VEC_ELT (targs, i) = arg;
13432 /* If the type parameter is a parameter pack, then it will
13433 be deduced to an empty parameter pack. */
13434 if (template_parameter_pack_p (tparm))
13438 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
13440 arg = make_node (NONTYPE_ARGUMENT_PACK);
13441 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
13442 TREE_CONSTANT (arg) = 1;
13445 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
13447 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
13449 TREE_VEC_ELT (targs, i) = arg;
13459 /* Subroutine of type_unification_real. Args are like the variables
13460 at the call site. ARG is an overloaded function (or template-id);
13461 we try deducing template args from each of the overloads, and if
13462 only one succeeds, we go with that. Modifies TARGS and returns
13463 true on success. */
13466 resolve_overloaded_unification (tree tparms,
13470 unification_kind_t strict,
13473 tree tempargs = copy_node (targs);
13475 tree goodfn = NULL_TREE;
13478 if (TREE_CODE (arg) == ADDR_EXPR)
13480 arg = TREE_OPERAND (arg, 0);
13486 if (TREE_CODE (arg) == COMPONENT_REF)
13487 /* Handle `&x' where `x' is some static or non-static member
13489 arg = TREE_OPERAND (arg, 1);
13491 if (TREE_CODE (arg) == OFFSET_REF)
13492 arg = TREE_OPERAND (arg, 1);
13494 /* Strip baselink information. */
13495 if (BASELINK_P (arg))
13496 arg = BASELINK_FUNCTIONS (arg);
13498 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
13500 /* If we got some explicit template args, we need to plug them into
13501 the affected templates before we try to unify, in case the
13502 explicit args will completely resolve the templates in question. */
13504 tree expl_subargs = TREE_OPERAND (arg, 1);
13505 arg = TREE_OPERAND (arg, 0);
13507 for (; arg; arg = OVL_NEXT (arg))
13509 tree fn = OVL_CURRENT (arg);
13510 tree subargs, elem;
13512 if (TREE_CODE (fn) != TEMPLATE_DECL)
13515 ++processing_template_decl;
13516 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13517 expl_subargs, /*check_ret=*/false);
13520 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
13521 if (try_one_overload (tparms, targs, tempargs, parm,
13522 elem, strict, sub_strict, addr_p)
13523 && (!goodfn || !decls_match (goodfn, elem)))
13529 --processing_template_decl;
13532 else if (TREE_CODE (arg) != OVERLOAD
13533 && TREE_CODE (arg) != FUNCTION_DECL)
13534 /* If ARG is, for example, "(0, &f)" then its type will be unknown
13535 -- but the deduction does not succeed because the expression is
13536 not just the function on its own. */
13539 for (; arg; arg = OVL_NEXT (arg))
13540 if (try_one_overload (tparms, targs, tempargs, parm,
13541 TREE_TYPE (OVL_CURRENT (arg)),
13542 strict, sub_strict, addr_p)
13543 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
13545 goodfn = OVL_CURRENT (arg);
13549 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13550 to function or pointer to member function argument if the set of
13551 overloaded functions does not contain function templates and at most
13552 one of a set of overloaded functions provides a unique match.
13554 So if we found multiple possibilities, we return success but don't
13555 deduce anything. */
13559 int i = TREE_VEC_LENGTH (targs);
13561 if (TREE_VEC_ELT (tempargs, i))
13562 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
13570 /* Core DR 115: In contexts where deduction is done and fails, or in
13571 contexts where deduction is not done, if a template argument list is
13572 specified and it, along with any default template arguments, identifies
13573 a single function template specialization, then the template-id is an
13574 lvalue for the function template specialization. */
13577 resolve_nondeduced_context (tree orig_expr)
13579 tree expr, offset, baselink;
13582 if (!type_unknown_p (orig_expr))
13587 offset = NULL_TREE;
13588 baselink = NULL_TREE;
13590 if (TREE_CODE (expr) == ADDR_EXPR)
13592 expr = TREE_OPERAND (expr, 0);
13595 if (TREE_CODE (expr) == OFFSET_REF)
13598 expr = TREE_OPERAND (expr, 1);
13600 if (TREE_CODE (expr) == BASELINK)
13603 expr = BASELINK_FUNCTIONS (expr);
13606 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
13609 tree goodfn = NULL_TREE;
13611 /* If we got some explicit template args, we need to plug them into
13612 the affected templates before we try to unify, in case the
13613 explicit args will completely resolve the templates in question. */
13615 tree expl_subargs = TREE_OPERAND (expr, 1);
13616 tree arg = TREE_OPERAND (expr, 0);
13617 tree badfn = NULL_TREE;
13618 tree badargs = NULL_TREE;
13620 for (; arg; arg = OVL_NEXT (arg))
13622 tree fn = OVL_CURRENT (arg);
13623 tree subargs, elem;
13625 if (TREE_CODE (fn) != TEMPLATE_DECL)
13628 ++processing_template_decl;
13629 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13630 expl_subargs, /*check_ret=*/false);
13631 if (subargs && !any_dependent_template_arguments_p (subargs))
13633 elem = instantiate_template (fn, subargs, tf_none);
13634 if (elem == error_mark_node)
13639 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
13645 --processing_template_decl;
13651 expr = build_baselink (BASELINK_BINFO (baselink),
13652 BASELINK_ACCESS_BINFO (baselink),
13653 expr, BASELINK_OPTYPE (baselink));
13655 expr = build2 (OFFSET_REF, TREE_TYPE (expr),
13656 TREE_OPERAND (offset, 0), expr);
13658 expr = build_address (expr);
13661 else if (good == 0 && badargs)
13662 /* There were no good options and at least one bad one, so let the
13663 user know what the problem is. */
13664 instantiate_template (badfn, badargs, tf_warning_or_error);
13669 /* Subroutine of resolve_overloaded_unification; does deduction for a single
13670 overload. Fills TARGS with any deduced arguments, or error_mark_node if
13671 different overloads deduce different arguments for a given parm.
13672 ADDR_P is true if the expression for which deduction is being
13673 performed was of the form "& fn" rather than simply "fn".
13675 Returns 1 on success. */
13678 try_one_overload (tree tparms,
13683 unification_kind_t strict,
13691 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13692 to function or pointer to member function argument if the set of
13693 overloaded functions does not contain function templates and at most
13694 one of a set of overloaded functions provides a unique match.
13696 So if this is a template, just return success. */
13698 if (uses_template_parms (arg))
13701 if (TREE_CODE (arg) == METHOD_TYPE)
13702 arg = build_ptrmemfunc_type (build_pointer_type (arg));
13704 arg = build_pointer_type (arg);
13706 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
13708 /* We don't copy orig_targs for this because if we have already deduced
13709 some template args from previous args, unify would complain when we
13710 try to deduce a template parameter for the same argument, even though
13711 there isn't really a conflict. */
13712 nargs = TREE_VEC_LENGTH (targs);
13713 tempargs = make_tree_vec (nargs);
13715 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
13718 /* First make sure we didn't deduce anything that conflicts with
13719 explicitly specified args. */
13720 for (i = nargs; i--; )
13722 tree elt = TREE_VEC_ELT (tempargs, i);
13723 tree oldelt = TREE_VEC_ELT (orig_targs, i);
13727 else if (uses_template_parms (elt))
13728 /* Since we're unifying against ourselves, we will fill in
13729 template args used in the function parm list with our own
13730 template parms. Discard them. */
13731 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
13732 else if (oldelt && !template_args_equal (oldelt, elt))
13736 for (i = nargs; i--; )
13738 tree elt = TREE_VEC_ELT (tempargs, i);
13741 TREE_VEC_ELT (targs, i) = elt;
13747 /* PARM is a template class (perhaps with unbound template
13748 parameters). ARG is a fully instantiated type. If ARG can be
13749 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
13750 TARGS are as for unify. */
13753 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
13755 tree copy_of_targs;
13757 if (!CLASSTYPE_TEMPLATE_INFO (arg)
13758 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
13759 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
13762 /* We need to make a new template argument vector for the call to
13763 unify. If we used TARGS, we'd clutter it up with the result of
13764 the attempted unification, even if this class didn't work out.
13765 We also don't want to commit ourselves to all the unifications
13766 we've already done, since unification is supposed to be done on
13767 an argument-by-argument basis. In other words, consider the
13768 following pathological case:
13770 template <int I, int J, int K>
13773 template <int I, int J>
13774 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
13776 template <int I, int J, int K>
13777 void f(S<I, J, K>, S<I, I, I>);
13786 Now, by the time we consider the unification involving `s2', we
13787 already know that we must have `f<0, 0, 0>'. But, even though
13788 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
13789 because there are two ways to unify base classes of S<0, 1, 2>
13790 with S<I, I, I>. If we kept the already deduced knowledge, we
13791 would reject the possibility I=1. */
13792 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
13794 /* If unification failed, we're done. */
13795 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
13796 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
13802 /* Given a template type PARM and a class type ARG, find the unique
13803 base type in ARG that is an instance of PARM. We do not examine
13804 ARG itself; only its base-classes. If there is not exactly one
13805 appropriate base class, return NULL_TREE. PARM may be the type of
13806 a partial specialization, as well as a plain template type. Used
13810 get_template_base (tree tparms, tree targs, tree parm, tree arg)
13812 tree rval = NULL_TREE;
13815 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
13817 binfo = TYPE_BINFO (complete_type (arg));
13819 /* The type could not be completed. */
13822 /* Walk in inheritance graph order. The search order is not
13823 important, and this avoids multiple walks of virtual bases. */
13824 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
13826 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
13830 /* If there is more than one satisfactory baseclass, then:
13834 If they yield more than one possible deduced A, the type
13838 if (rval && !same_type_p (r, rval))
13848 /* Returns the level of DECL, which declares a template parameter. */
13851 template_decl_level (tree decl)
13853 switch (TREE_CODE (decl))
13856 case TEMPLATE_DECL:
13857 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
13860 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
13863 gcc_unreachable ();
13868 /* Decide whether ARG can be unified with PARM, considering only the
13869 cv-qualifiers of each type, given STRICT as documented for unify.
13870 Returns nonzero iff the unification is OK on that basis. */
13873 check_cv_quals_for_unify (int strict, tree arg, tree parm)
13875 int arg_quals = cp_type_quals (arg);
13876 int parm_quals = cp_type_quals (parm);
13878 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13879 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
13881 /* Although a CVR qualifier is ignored when being applied to a
13882 substituted template parameter ([8.3.2]/1 for example), that
13883 does not apply during deduction [14.8.2.4]/1, (even though
13884 that is not explicitly mentioned, [14.8.2.4]/9 indicates
13885 this). Except when we're allowing additional CV qualifiers
13886 at the outer level [14.8.2.1]/3,1st bullet. */
13887 if ((TREE_CODE (arg) == REFERENCE_TYPE
13888 || TREE_CODE (arg) == FUNCTION_TYPE
13889 || TREE_CODE (arg) == METHOD_TYPE)
13890 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
13893 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
13894 && (parm_quals & TYPE_QUAL_RESTRICT))
13898 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
13899 && (arg_quals & parm_quals) != parm_quals)
13902 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
13903 && (parm_quals & arg_quals) != arg_quals)
13909 /* Determines the LEVEL and INDEX for the template parameter PARM. */
13911 template_parm_level_and_index (tree parm, int* level, int* index)
13913 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13914 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13915 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13917 *index = TEMPLATE_TYPE_IDX (parm);
13918 *level = TEMPLATE_TYPE_LEVEL (parm);
13922 *index = TEMPLATE_PARM_IDX (parm);
13923 *level = TEMPLATE_PARM_LEVEL (parm);
13927 /* Unifies the remaining arguments in PACKED_ARGS with the pack
13928 expansion at the end of PACKED_PARMS. Returns 0 if the type
13929 deduction succeeds, 1 otherwise. STRICT is the same as in
13930 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
13931 call argument list. We'll need to adjust the arguments to make them
13932 types. SUBR tells us if this is from a recursive call to
13933 type_unification_real. */
13935 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
13936 tree packed_args, int strict, bool call_args_p,
13940 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
13941 tree pattern = PACK_EXPANSION_PATTERN (parm);
13942 tree pack, packs = NULL_TREE;
13943 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
13944 int len = TREE_VEC_LENGTH (packed_args);
13946 /* Determine the parameter packs we will be deducing from the
13947 pattern, and record their current deductions. */
13948 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
13949 pack; pack = TREE_CHAIN (pack))
13951 tree parm_pack = TREE_VALUE (pack);
13954 /* Determine the index and level of this parameter pack. */
13955 template_parm_level_and_index (parm_pack, &level, &idx);
13957 /* Keep track of the parameter packs and their corresponding
13959 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
13960 TREE_TYPE (packs) = make_tree_vec (len - start);
13963 /* Loop through all of the arguments that have not yet been
13964 unified and unify each with the pattern. */
13965 for (i = start; i < len; i++)
13967 tree parm = pattern;
13969 /* For each parameter pack, clear out the deduced value so that
13970 we can deduce it again. */
13971 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13974 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13976 TMPL_ARG (targs, level, idx) = NULL_TREE;
13979 /* Unify the pattern with the current argument. */
13981 tree arg = TREE_VEC_ELT (packed_args, i);
13982 tree arg_expr = NULL_TREE;
13983 int arg_strict = strict;
13984 bool skip_arg_p = false;
13990 /* This mirrors what we do in type_unification_real. */
13994 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
13995 | UNIFY_ALLOW_MORE_CV_QUAL
13996 | UNIFY_ALLOW_DERIVED);
14000 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
14004 sub_strict = UNIFY_ALLOW_NONE;
14008 gcc_unreachable ();
14013 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
14014 if (type_unknown_p (arg))
14016 /* [temp.deduct.type] A template-argument can be
14017 deduced from a pointer to function or pointer
14018 to member function argument if the set of
14019 overloaded functions does not contain function
14020 templates and at most one of a set of
14021 overloaded functions provides a unique
14024 if (resolve_overloaded_unification
14025 (tparms, targs, parm, arg,
14026 (unification_kind_t) strict,
14036 arg = unlowered_expr_type (arg);
14037 if (arg == error_mark_node)
14042 arg_strict = sub_strict;
14046 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
14047 &parm, &arg, arg_expr);
14052 /* For deduction from an init-list we need the actual list. */
14053 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
14055 if (unify (tparms, targs, parm, arg, arg_strict))
14060 /* For each parameter pack, collect the deduced value. */
14061 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14064 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14066 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
14067 TMPL_ARG (targs, level, idx);
14071 /* Verify that the results of unification with the parameter packs
14072 produce results consistent with what we've seen before, and make
14073 the deduced argument packs available. */
14074 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14076 tree old_pack = TREE_VALUE (pack);
14077 tree new_args = TREE_TYPE (pack);
14078 int i, len = TREE_VEC_LENGTH (new_args);
14080 bool nondeduced_p = false;
14082 /* By default keep the original deduced argument pack.
14083 If necessary, more specific code is going to update the
14084 resulting deduced argument later down in this function. */
14085 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14086 TMPL_ARG (targs, level, idx) = old_pack;
14088 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
14089 actually deduce anything. */
14090 for (i = 0; i < len && !nondeduced_p; ++i)
14091 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
14092 nondeduced_p = true;
14096 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
14098 /* Prepend the explicit arguments onto NEW_ARGS. */
14099 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14100 tree old_args = new_args;
14101 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
14102 int len = explicit_len + TREE_VEC_LENGTH (old_args);
14104 /* Copy the explicit arguments. */
14105 new_args = make_tree_vec (len);
14106 for (i = 0; i < explicit_len; i++)
14107 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
14109 /* Copy the deduced arguments. */
14110 for (; i < len; i++)
14111 TREE_VEC_ELT (new_args, i) =
14112 TREE_VEC_ELT (old_args, i - explicit_len);
14118 /* Build the deduced *_ARGUMENT_PACK. */
14119 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
14121 result = make_node (NONTYPE_ARGUMENT_PACK);
14122 TREE_TYPE (result) =
14123 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
14124 TREE_CONSTANT (result) = 1;
14127 result = cxx_make_type (TYPE_ARGUMENT_PACK);
14129 SET_ARGUMENT_PACK_ARGS (result, new_args);
14131 /* Note the deduced argument packs for this parameter
14133 TMPL_ARG (targs, level, idx) = result;
14135 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
14136 && (ARGUMENT_PACK_ARGS (old_pack)
14137 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
14139 /* We only had the explicitly-provided arguments before, but
14140 now we have a complete set of arguments. */
14141 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14143 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
14144 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
14145 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
14147 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
14149 /* Inconsistent unification of this parameter pack. */
14156 /* Deduce the value of template parameters. TPARMS is the (innermost)
14157 set of template parameters to a template. TARGS is the bindings
14158 for those template parameters, as determined thus far; TARGS may
14159 include template arguments for outer levels of template parameters
14160 as well. PARM is a parameter to a template function, or a
14161 subcomponent of that parameter; ARG is the corresponding argument.
14162 This function attempts to match PARM with ARG in a manner
14163 consistent with the existing assignments in TARGS. If more values
14164 are deduced, then TARGS is updated.
14166 Returns 0 if the type deduction succeeds, 1 otherwise. The
14167 parameter STRICT is a bitwise or of the following flags:
14170 Require an exact match between PARM and ARG.
14171 UNIFY_ALLOW_MORE_CV_QUAL:
14172 Allow the deduced ARG to be more cv-qualified (by qualification
14173 conversion) than ARG.
14174 UNIFY_ALLOW_LESS_CV_QUAL:
14175 Allow the deduced ARG to be less cv-qualified than ARG.
14176 UNIFY_ALLOW_DERIVED:
14177 Allow the deduced ARG to be a template base class of ARG,
14178 or a pointer to a template base class of the type pointed to by
14180 UNIFY_ALLOW_INTEGER:
14181 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
14182 case for more information.
14183 UNIFY_ALLOW_OUTER_LEVEL:
14184 This is the outermost level of a deduction. Used to determine validity
14185 of qualification conversions. A valid qualification conversion must
14186 have const qualified pointers leading up to the inner type which
14187 requires additional CV quals, except at the outer level, where const
14188 is not required [conv.qual]. It would be normal to set this flag in
14189 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
14190 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
14191 This is the outermost level of a deduction, and PARM can be more CV
14192 qualified at this point.
14193 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
14194 This is the outermost level of a deduction, and PARM can be less CV
14195 qualified at this point. */
14198 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
14203 int strict_in = strict;
14205 /* I don't think this will do the right thing with respect to types.
14206 But the only case I've seen it in so far has been array bounds, where
14207 signedness is the only information lost, and I think that will be
14209 while (TREE_CODE (parm) == NOP_EXPR)
14210 parm = TREE_OPERAND (parm, 0);
14212 if (arg == error_mark_node)
14214 if (arg == unknown_type_node
14215 || arg == init_list_type_node)
14216 /* We can't deduce anything from this, but we might get all the
14217 template args from other function args. */
14220 /* If PARM uses template parameters, then we can't bail out here,
14221 even if ARG == PARM, since we won't record unifications for the
14222 template parameters. We might need them if we're trying to
14223 figure out which of two things is more specialized. */
14224 if (arg == parm && !uses_template_parms (parm))
14227 /* Handle init lists early, so the rest of the function can assume
14228 we're dealing with a type. */
14229 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
14233 tree orig_parm = parm;
14235 /* Replace T with std::initializer_list<T> for deduction. */
14236 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14237 && flag_deduce_init_list)
14238 parm = listify (parm);
14240 if (!is_std_init_list (parm))
14241 /* We can only deduce from an initializer list argument if the
14242 parameter is std::initializer_list; otherwise this is a
14243 non-deduced context. */
14246 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
14248 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
14250 int elt_strict = strict;
14251 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
14253 tree type = TREE_TYPE (elt);
14254 /* It should only be possible to get here for a call. */
14255 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
14256 elt_strict |= maybe_adjust_types_for_deduction
14257 (DEDUCE_CALL, &elttype, &type, elt);
14261 if (unify (tparms, targs, elttype, elt, elt_strict))
14265 /* If the std::initializer_list<T> deduction worked, replace the
14266 deduced A with std::initializer_list<A>. */
14267 if (orig_parm != parm)
14269 idx = TEMPLATE_TYPE_IDX (orig_parm);
14270 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14271 targ = listify (targ);
14272 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
14277 /* Immediately reject some pairs that won't unify because of
14278 cv-qualification mismatches. */
14279 if (TREE_CODE (arg) == TREE_CODE (parm)
14281 /* It is the elements of the array which hold the cv quals of an array
14282 type, and the elements might be template type parms. We'll check
14283 when we recurse. */
14284 && TREE_CODE (arg) != ARRAY_TYPE
14285 /* We check the cv-qualifiers when unifying with template type
14286 parameters below. We want to allow ARG `const T' to unify with
14287 PARM `T' for example, when computing which of two templates
14288 is more specialized, for example. */
14289 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
14290 && !check_cv_quals_for_unify (strict_in, arg, parm))
14293 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
14294 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
14295 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
14296 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
14297 strict &= ~UNIFY_ALLOW_DERIVED;
14298 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
14299 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
14301 switch (TREE_CODE (parm))
14303 case TYPENAME_TYPE:
14305 case UNBOUND_CLASS_TEMPLATE:
14306 /* In a type which contains a nested-name-specifier, template
14307 argument values cannot be deduced for template parameters used
14308 within the nested-name-specifier. */
14311 case TEMPLATE_TYPE_PARM:
14312 case TEMPLATE_TEMPLATE_PARM:
14313 case BOUND_TEMPLATE_TEMPLATE_PARM:
14314 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14315 if (tparm == error_mark_node)
14318 if (TEMPLATE_TYPE_LEVEL (parm)
14319 != template_decl_level (tparm))
14320 /* The PARM is not one we're trying to unify. Just check
14321 to see if it matches ARG. */
14322 return (TREE_CODE (arg) == TREE_CODE (parm)
14323 && same_type_p (parm, arg)) ? 0 : 1;
14324 idx = TEMPLATE_TYPE_IDX (parm);
14325 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14326 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
14328 /* Check for mixed types and values. */
14329 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14330 && TREE_CODE (tparm) != TYPE_DECL)
14331 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14332 && TREE_CODE (tparm) != TEMPLATE_DECL))
14335 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14337 /* ARG must be constructed from a template class or a template
14338 template parameter. */
14339 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
14340 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
14344 tree parmvec = TYPE_TI_ARGS (parm);
14345 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
14347 = DECL_INNERMOST_TEMPLATE_PARMS
14348 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
14350 int parm_variadic_p = 0;
14352 /* The resolution to DR150 makes clear that default
14353 arguments for an N-argument may not be used to bind T
14354 to a template template parameter with fewer than N
14355 parameters. It is not safe to permit the binding of
14356 default arguments as an extension, as that may change
14357 the meaning of a conforming program. Consider:
14359 struct Dense { static const unsigned int dim = 1; };
14361 template <template <typename> class View,
14363 void operator+(float, View<Block> const&);
14365 template <typename Block,
14366 unsigned int Dim = Block::dim>
14367 struct Lvalue_proxy { operator float() const; };
14371 Lvalue_proxy<Dense> p;
14376 Here, if Lvalue_proxy is permitted to bind to View, then
14377 the global operator+ will be used; if they are not, the
14378 Lvalue_proxy will be converted to float. */
14379 if (coerce_template_parms (parm_parms,
14381 TYPE_TI_TEMPLATE (parm),
14383 /*require_all_args=*/true,
14384 /*use_default_args=*/false)
14385 == error_mark_node)
14388 /* Deduce arguments T, i from TT<T> or TT<i>.
14389 We check each element of PARMVEC and ARGVEC individually
14390 rather than the whole TREE_VEC since they can have
14391 different number of elements. */
14393 parmvec = expand_template_argument_pack (parmvec);
14394 argvec = expand_template_argument_pack (argvec);
14396 len = TREE_VEC_LENGTH (parmvec);
14398 /* Check if the parameters end in a pack, making them
14401 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
14402 parm_variadic_p = 1;
14404 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
14407 for (i = 0; i < len - parm_variadic_p; ++i)
14409 if (unify (tparms, targs,
14410 TREE_VEC_ELT (parmvec, i),
14411 TREE_VEC_ELT (argvec, i),
14416 if (parm_variadic_p
14417 && unify_pack_expansion (tparms, targs,
14420 /*call_args_p=*/false,
14424 arg = TYPE_TI_TEMPLATE (arg);
14426 /* Fall through to deduce template name. */
14429 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14430 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14432 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
14434 /* Simple cases: Value already set, does match or doesn't. */
14435 if (targ != NULL_TREE && template_args_equal (targ, arg))
14442 /* If PARM is `const T' and ARG is only `int', we don't have
14443 a match unless we are allowing additional qualification.
14444 If ARG is `const int' and PARM is just `T' that's OK;
14445 that binds `const int' to `T'. */
14446 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
14450 /* Consider the case where ARG is `const volatile int' and
14451 PARM is `const T'. Then, T should be `volatile int'. */
14452 arg = cp_build_qualified_type_real
14453 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
14454 if (arg == error_mark_node)
14457 /* Simple cases: Value already set, does match or doesn't. */
14458 if (targ != NULL_TREE && same_type_p (targ, arg))
14463 /* Make sure that ARG is not a variable-sized array. (Note
14464 that were talking about variable-sized arrays (like
14465 `int[n]'), rather than arrays of unknown size (like
14466 `int[]').) We'll get very confused by such a type since
14467 the bound of the array will not be computable in an
14468 instantiation. Besides, such types are not allowed in
14469 ISO C++, so we can do as we please here. */
14470 if (variably_modified_type_p (arg, NULL_TREE))
14473 /* Strip typedefs as in convert_template_argument. */
14474 arg = strip_typedefs (arg);
14477 /* If ARG is a parameter pack or an expansion, we cannot unify
14478 against it unless PARM is also a parameter pack. */
14479 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14480 && !template_parameter_pack_p (parm))
14483 /* If the argument deduction results is a METHOD_TYPE,
14484 then there is a problem.
14485 METHOD_TYPE doesn't map to any real C++ type the result of
14486 the deduction can not be of that type. */
14487 if (TREE_CODE (arg) == METHOD_TYPE)
14490 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14493 case TEMPLATE_PARM_INDEX:
14494 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14495 if (tparm == error_mark_node)
14498 if (TEMPLATE_PARM_LEVEL (parm)
14499 != template_decl_level (tparm))
14500 /* The PARM is not one we're trying to unify. Just check
14501 to see if it matches ARG. */
14502 return !(TREE_CODE (arg) == TREE_CODE (parm)
14503 && cp_tree_equal (parm, arg));
14505 idx = TEMPLATE_PARM_IDX (parm);
14506 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14509 return !cp_tree_equal (targ, arg);
14511 /* [temp.deduct.type] If, in the declaration of a function template
14512 with a non-type template-parameter, the non-type
14513 template-parameter is used in an expression in the function
14514 parameter-list and, if the corresponding template-argument is
14515 deduced, the template-argument type shall match the type of the
14516 template-parameter exactly, except that a template-argument
14517 deduced from an array bound may be of any integral type.
14518 The non-type parameter might use already deduced type parameters. */
14519 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
14520 if (!TREE_TYPE (arg))
14521 /* Template-parameter dependent expression. Just accept it for now.
14522 It will later be processed in convert_template_argument. */
14524 else if (same_type_p (TREE_TYPE (arg), tparm))
14526 else if ((strict & UNIFY_ALLOW_INTEGER)
14527 && (TREE_CODE (tparm) == INTEGER_TYPE
14528 || TREE_CODE (tparm) == BOOLEAN_TYPE))
14529 /* Convert the ARG to the type of PARM; the deduced non-type
14530 template argument must exactly match the types of the
14531 corresponding parameter. */
14532 arg = fold (build_nop (tparm, arg));
14533 else if (uses_template_parms (tparm))
14534 /* We haven't deduced the type of this parameter yet. Try again
14540 /* If ARG is a parameter pack or an expansion, we cannot unify
14541 against it unless PARM is also a parameter pack. */
14542 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14543 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
14546 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14551 /* A pointer-to-member constant can be unified only with
14552 another constant. */
14553 if (TREE_CODE (arg) != PTRMEM_CST)
14556 /* Just unify the class member. It would be useless (and possibly
14557 wrong, depending on the strict flags) to unify also
14558 PTRMEM_CST_CLASS, because we want to be sure that both parm and
14559 arg refer to the same variable, even if through different
14560 classes. For instance:
14562 struct A { int x; };
14565 Unification of &A::x and &B::x must succeed. */
14566 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
14567 PTRMEM_CST_MEMBER (arg), strict);
14572 if (TREE_CODE (arg) != POINTER_TYPE)
14575 /* [temp.deduct.call]
14577 A can be another pointer or pointer to member type that can
14578 be converted to the deduced A via a qualification
14579 conversion (_conv.qual_).
14581 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
14582 This will allow for additional cv-qualification of the
14583 pointed-to types if appropriate. */
14585 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
14586 /* The derived-to-base conversion only persists through one
14587 level of pointers. */
14588 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
14590 return unify (tparms, targs, TREE_TYPE (parm),
14591 TREE_TYPE (arg), strict);
14594 case REFERENCE_TYPE:
14595 if (TREE_CODE (arg) != REFERENCE_TYPE)
14597 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14598 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14601 if (TREE_CODE (arg) != ARRAY_TYPE)
14603 if ((TYPE_DOMAIN (parm) == NULL_TREE)
14604 != (TYPE_DOMAIN (arg) == NULL_TREE))
14606 if (TYPE_DOMAIN (parm) != NULL_TREE)
14613 /* Our representation of array types uses "N - 1" as the
14614 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
14615 not an integer constant. We cannot unify arbitrarily
14616 complex expressions, so we eliminate the MINUS_EXPRs
14618 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
14619 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
14622 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
14623 parm_max = TREE_OPERAND (parm_max, 0);
14625 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
14626 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
14629 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
14630 trying to unify the type of a variable with the type
14631 of a template parameter. For example:
14633 template <unsigned int N>
14634 void f (char (&) [N]);
14641 Here, the type of the ARG will be "int [g(i)]", and
14642 may be a SAVE_EXPR, etc. */
14643 if (TREE_CODE (arg_max) != MINUS_EXPR)
14645 arg_max = TREE_OPERAND (arg_max, 0);
14648 /* If only one of the bounds used a MINUS_EXPR, compensate
14649 by adding one to the other bound. */
14650 if (parm_cst && !arg_cst)
14651 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
14655 else if (arg_cst && !parm_cst)
14656 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
14661 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
14664 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14665 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14672 case ENUMERAL_TYPE:
14674 if (TREE_CODE (arg) != TREE_CODE (parm))
14677 /* We have already checked cv-qualification at the top of the
14679 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
14682 /* As far as unification is concerned, this wins. Later checks
14683 will invalidate it if necessary. */
14686 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
14687 /* Type INTEGER_CST can come from ordinary constant template args. */
14689 while (TREE_CODE (arg) == NOP_EXPR)
14690 arg = TREE_OPERAND (arg, 0);
14692 if (TREE_CODE (arg) != INTEGER_CST)
14694 return !tree_int_cst_equal (parm, arg);
14699 if (TREE_CODE (arg) != TREE_VEC)
14701 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
14703 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
14704 if (unify (tparms, targs,
14705 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
14713 if (TREE_CODE (arg) != TREE_CODE (parm))
14716 if (TYPE_PTRMEMFUNC_P (parm))
14718 if (!TYPE_PTRMEMFUNC_P (arg))
14721 return unify (tparms, targs,
14722 TYPE_PTRMEMFUNC_FN_TYPE (parm),
14723 TYPE_PTRMEMFUNC_FN_TYPE (arg),
14727 if (CLASSTYPE_TEMPLATE_INFO (parm))
14729 tree t = NULL_TREE;
14731 if (strict_in & UNIFY_ALLOW_DERIVED)
14733 /* First, we try to unify the PARM and ARG directly. */
14734 t = try_class_unification (tparms, targs,
14739 /* Fallback to the special case allowed in
14740 [temp.deduct.call]:
14742 If P is a class, and P has the form
14743 template-id, then A can be a derived class of
14744 the deduced A. Likewise, if P is a pointer to
14745 a class of the form template-id, A can be a
14746 pointer to a derived class pointed to by the
14748 t = get_template_base (tparms, targs, parm, arg);
14754 else if (CLASSTYPE_TEMPLATE_INFO (arg)
14755 && (CLASSTYPE_TI_TEMPLATE (parm)
14756 == CLASSTYPE_TI_TEMPLATE (arg)))
14757 /* Perhaps PARM is something like S<U> and ARG is S<int>.
14758 Then, we should unify `int' and `U'. */
14761 /* There's no chance of unification succeeding. */
14764 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
14765 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
14767 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
14772 case FUNCTION_TYPE:
14774 unsigned int nargs;
14779 if (TREE_CODE (arg) != TREE_CODE (parm))
14782 /* CV qualifications for methods can never be deduced, they must
14783 match exactly. We need to check them explicitly here,
14784 because type_unification_real treats them as any other
14785 cv-qualified parameter. */
14786 if (TREE_CODE (parm) == METHOD_TYPE
14787 && (!check_cv_quals_for_unify
14789 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
14790 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
14793 if (unify (tparms, targs, TREE_TYPE (parm),
14794 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
14797 nargs = list_length (TYPE_ARG_TYPES (arg));
14798 args = XALLOCAVEC (tree, nargs);
14799 for (a = TYPE_ARG_TYPES (arg), i = 0;
14800 a != NULL_TREE && a != void_list_node;
14801 a = TREE_CHAIN (a), ++i)
14802 args[i] = TREE_VALUE (a);
14805 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
14806 args, nargs, 1, DEDUCE_EXACT,
14811 /* Unify a pointer to member with a pointer to member function, which
14812 deduces the type of the member as a function type. */
14813 if (TYPE_PTRMEMFUNC_P (arg))
14817 cp_cv_quals cv_quals;
14819 /* Check top-level cv qualifiers */
14820 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
14823 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
14824 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
14827 /* Determine the type of the function we are unifying against. */
14828 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
14830 build_function_type (TREE_TYPE (method_type),
14831 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
14833 /* Extract the cv-qualifiers of the member function from the
14834 implicit object parameter and place them on the function
14835 type to be restored later. */
14837 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
14838 fntype = build_qualified_type (fntype, cv_quals);
14839 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
14842 if (TREE_CODE (arg) != OFFSET_TYPE)
14844 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
14845 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
14847 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14851 if (DECL_TEMPLATE_PARM_P (parm))
14852 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
14853 if (arg != integral_constant_value (parm))
14858 case TEMPLATE_DECL:
14859 /* Matched cases are handled by the ARG == PARM test above. */
14862 case TYPE_ARGUMENT_PACK:
14863 case NONTYPE_ARGUMENT_PACK:
14865 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
14866 tree packed_args = ARGUMENT_PACK_ARGS (arg);
14867 int i, len = TREE_VEC_LENGTH (packed_parms);
14868 int argslen = TREE_VEC_LENGTH (packed_args);
14869 int parm_variadic_p = 0;
14871 for (i = 0; i < len; ++i)
14873 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
14876 /* We can unify against something with a trailing
14878 parm_variadic_p = 1;
14880 /* Since there is something following the pack
14881 expansion, we cannot unify this template argument
14888 /* If we don't have enough arguments to satisfy the parameters
14889 (not counting the pack expression at the end), or we have
14890 too many arguments for a parameter list that doesn't end in
14891 a pack expression, we can't unify. */
14892 if (argslen < (len - parm_variadic_p)
14893 || (argslen > len && !parm_variadic_p))
14896 /* Unify all of the parameters that precede the (optional)
14897 pack expression. */
14898 for (i = 0; i < len - parm_variadic_p; ++i)
14900 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
14901 TREE_VEC_ELT (packed_args, i), strict))
14905 if (parm_variadic_p)
14906 return unify_pack_expansion (tparms, targs,
14907 packed_parms, packed_args,
14908 strict, /*call_args_p=*/false,
14916 case DECLTYPE_TYPE:
14917 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
14922 /* Unification fails if we hit an error node. */
14926 gcc_assert (EXPR_P (parm));
14928 /* We must be looking at an expression. This can happen with
14932 void foo(S<I>, S<I + 2>);
14934 This is a "nondeduced context":
14938 The nondeduced contexts are:
14940 --A type that is a template-id in which one or more of
14941 the template-arguments is an expression that references
14942 a template-parameter.
14944 In these cases, we assume deduction succeeded, but don't
14945 actually infer any unifications. */
14947 if (!uses_template_parms (parm)
14948 && !template_args_equal (parm, arg))
14955 /* Note that DECL can be defined in this translation unit, if
14959 mark_definable (tree decl)
14962 DECL_NOT_REALLY_EXTERN (decl) = 1;
14963 FOR_EACH_CLONE (clone, decl)
14964 DECL_NOT_REALLY_EXTERN (clone) = 1;
14967 /* Called if RESULT is explicitly instantiated, or is a member of an
14968 explicitly instantiated class. */
14971 mark_decl_instantiated (tree result, int extern_p)
14973 SET_DECL_EXPLICIT_INSTANTIATION (result);
14975 /* If this entity has already been written out, it's too late to
14976 make any modifications. */
14977 if (TREE_ASM_WRITTEN (result))
14980 if (TREE_CODE (result) != FUNCTION_DECL)
14981 /* The TREE_PUBLIC flag for function declarations will have been
14982 set correctly by tsubst. */
14983 TREE_PUBLIC (result) = 1;
14985 /* This might have been set by an earlier implicit instantiation. */
14986 DECL_COMDAT (result) = 0;
14989 DECL_NOT_REALLY_EXTERN (result) = 0;
14992 mark_definable (result);
14993 /* Always make artificials weak. */
14994 if (DECL_ARTIFICIAL (result) && flag_weak)
14995 comdat_linkage (result);
14996 /* For WIN32 we also want to put explicit instantiations in
14997 linkonce sections. */
14998 else if (TREE_PUBLIC (result))
14999 maybe_make_one_only (result);
15002 /* If EXTERN_P, then this function will not be emitted -- unless
15003 followed by an explicit instantiation, at which point its linkage
15004 will be adjusted. If !EXTERN_P, then this function will be
15005 emitted here. In neither circumstance do we want
15006 import_export_decl to adjust the linkage. */
15007 DECL_INTERFACE_KNOWN (result) = 1;
15010 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
15011 important template arguments. If any are missing, we check whether
15012 they're important by using error_mark_node for substituting into any
15013 args that were used for partial ordering (the ones between ARGS and END)
15014 and seeing if it bubbles up. */
15017 check_undeduced_parms (tree targs, tree args, tree end)
15019 bool found = false;
15021 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
15022 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
15025 TREE_VEC_ELT (targs, i) = error_mark_node;
15029 for (; args != end; args = TREE_CHAIN (args))
15031 tree substed = tsubst (TREE_VALUE (args), targs, tf_none, NULL_TREE);
15032 if (substed == error_mark_node)
15039 /* Given two function templates PAT1 and PAT2, return:
15041 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
15042 -1 if PAT2 is more specialized than PAT1.
15043 0 if neither is more specialized.
15045 LEN indicates the number of parameters we should consider
15046 (defaulted parameters should not be considered).
15048 The 1998 std underspecified function template partial ordering, and
15049 DR214 addresses the issue. We take pairs of arguments, one from
15050 each of the templates, and deduce them against each other. One of
15051 the templates will be more specialized if all the *other*
15052 template's arguments deduce against its arguments and at least one
15053 of its arguments *does* *not* deduce against the other template's
15054 corresponding argument. Deduction is done as for class templates.
15055 The arguments used in deduction have reference and top level cv
15056 qualifiers removed. Iff both arguments were originally reference
15057 types *and* deduction succeeds in both directions, the template
15058 with the more cv-qualified argument wins for that pairing (if
15059 neither is more cv-qualified, they both are equal). Unlike regular
15060 deduction, after all the arguments have been deduced in this way,
15061 we do *not* verify the deduced template argument values can be
15062 substituted into non-deduced contexts.
15064 The logic can be a bit confusing here, because we look at deduce1 and
15065 targs1 to see if pat2 is at least as specialized, and vice versa; if we
15066 can find template arguments for pat1 to make arg1 look like arg2, that
15067 means that arg2 is at least as specialized as arg1. */
15070 more_specialized_fn (tree pat1, tree pat2, int len)
15072 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
15073 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
15074 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
15075 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
15076 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
15077 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
15078 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
15079 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
15080 tree origs1, origs2;
15081 bool lose1 = false;
15082 bool lose2 = false;
15084 /* Remove the this parameter from non-static member functions. If
15085 one is a non-static member function and the other is not a static
15086 member function, remove the first parameter from that function
15087 also. This situation occurs for operator functions where we
15088 locate both a member function (with this pointer) and non-member
15089 operator (with explicit first operand). */
15090 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
15092 len--; /* LEN is the number of significant arguments for DECL1 */
15093 args1 = TREE_CHAIN (args1);
15094 if (!DECL_STATIC_FUNCTION_P (decl2))
15095 args2 = TREE_CHAIN (args2);
15097 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
15099 args2 = TREE_CHAIN (args2);
15100 if (!DECL_STATIC_FUNCTION_P (decl1))
15103 args1 = TREE_CHAIN (args1);
15107 /* If only one is a conversion operator, they are unordered. */
15108 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
15111 /* Consider the return type for a conversion function */
15112 if (DECL_CONV_FN_P (decl1))
15114 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
15115 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
15119 processing_template_decl++;
15125 /* Stop when an ellipsis is seen. */
15126 && args1 != NULL_TREE && args2 != NULL_TREE)
15128 tree arg1 = TREE_VALUE (args1);
15129 tree arg2 = TREE_VALUE (args2);
15130 int deduce1, deduce2;
15134 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15135 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15137 /* When both arguments are pack expansions, we need only
15138 unify the patterns themselves. */
15139 arg1 = PACK_EXPANSION_PATTERN (arg1);
15140 arg2 = PACK_EXPANSION_PATTERN (arg2);
15142 /* This is the last comparison we need to do. */
15146 if (TREE_CODE (arg1) == REFERENCE_TYPE)
15148 arg1 = TREE_TYPE (arg1);
15149 quals1 = cp_type_quals (arg1);
15152 if (TREE_CODE (arg2) == REFERENCE_TYPE)
15154 arg2 = TREE_TYPE (arg2);
15155 quals2 = cp_type_quals (arg2);
15158 if ((quals1 < 0) != (quals2 < 0))
15160 /* Only of the args is a reference, see if we should apply
15161 array/function pointer decay to it. This is not part of
15162 DR214, but is, IMHO, consistent with the deduction rules
15163 for the function call itself, and with our earlier
15164 implementation of the underspecified partial ordering
15165 rules. (nathan). */
15168 switch (TREE_CODE (arg1))
15171 arg1 = TREE_TYPE (arg1);
15173 case FUNCTION_TYPE:
15174 arg1 = build_pointer_type (arg1);
15183 switch (TREE_CODE (arg2))
15186 arg2 = TREE_TYPE (arg2);
15188 case FUNCTION_TYPE:
15189 arg2 = build_pointer_type (arg2);
15198 arg1 = TYPE_MAIN_VARIANT (arg1);
15199 arg2 = TYPE_MAIN_VARIANT (arg2);
15201 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
15203 int i, len2 = list_length (args2);
15204 tree parmvec = make_tree_vec (1);
15205 tree argvec = make_tree_vec (len2);
15208 /* Setup the parameter vector, which contains only ARG1. */
15209 TREE_VEC_ELT (parmvec, 0) = arg1;
15211 /* Setup the argument vector, which contains the remaining
15213 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
15214 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15216 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
15217 argvec, UNIFY_ALLOW_NONE,
15218 /*call_args_p=*/false,
15221 /* We cannot deduce in the other direction, because ARG1 is
15222 a pack expansion but ARG2 is not. */
15225 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15227 int i, len1 = list_length (args1);
15228 tree parmvec = make_tree_vec (1);
15229 tree argvec = make_tree_vec (len1);
15232 /* Setup the parameter vector, which contains only ARG1. */
15233 TREE_VEC_ELT (parmvec, 0) = arg2;
15235 /* Setup the argument vector, which contains the remaining
15237 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
15238 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15240 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
15241 argvec, UNIFY_ALLOW_NONE,
15242 /*call_args_p=*/false,
15245 /* We cannot deduce in the other direction, because ARG2 is
15246 a pack expansion but ARG1 is not.*/
15252 /* The normal case, where neither argument is a pack
15254 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
15255 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
15258 /* If we couldn't deduce arguments for tparms1 to make arg1 match
15259 arg2, then arg2 is not as specialized as arg1. */
15265 /* "If, for a given type, deduction succeeds in both directions
15266 (i.e., the types are identical after the transformations above)
15267 and if the type from the argument template is more cv-qualified
15268 than the type from the parameter template (as described above)
15269 that type is considered to be more specialized than the other. If
15270 neither type is more cv-qualified than the other then neither type
15271 is more specialized than the other."
15273 We check same_type_p explicitly because deduction can also succeed
15274 in both directions when there is a nondeduced context. */
15275 if (deduce1 && deduce2
15276 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0
15277 && same_type_p (arg1, arg2))
15279 if ((quals1 & quals2) == quals2)
15281 if ((quals1 & quals2) == quals1)
15285 if (lose1 && lose2)
15286 /* We've failed to deduce something in either direction.
15287 These must be unordered. */
15290 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15291 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15292 /* We have already processed all of the arguments in our
15293 handing of the pack expansion type. */
15296 args1 = TREE_CHAIN (args1);
15297 args2 = TREE_CHAIN (args2);
15300 /* "In most cases, all template parameters must have values in order for
15301 deduction to succeed, but for partial ordering purposes a template
15302 parameter may remain without a value provided it is not used in the
15303 types being used for partial ordering."
15305 Thus, if we are missing any of the targs1 we need to substitute into
15306 origs1, then pat2 is not as specialized as pat1. This can happen when
15307 there is a nondeduced context. */
15308 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
15310 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
15313 processing_template_decl--;
15315 /* All things being equal, if the next argument is a pack expansion
15316 for one function but not for the other, prefer the
15317 non-variadic function. FIXME this is bogus; see c++/41958. */
15319 && args1 && TREE_VALUE (args1)
15320 && args2 && TREE_VALUE (args2))
15322 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
15323 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
15326 if (lose1 == lose2)
15334 /* Determine which of two partial specializations is more specialized.
15336 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
15337 to the first partial specialization. The TREE_VALUE is the
15338 innermost set of template parameters for the partial
15339 specialization. PAT2 is similar, but for the second template.
15341 Return 1 if the first partial specialization is more specialized;
15342 -1 if the second is more specialized; 0 if neither is more
15345 See [temp.class.order] for information about determining which of
15346 two templates is more specialized. */
15349 more_specialized_class (tree pat1, tree pat2)
15354 bool any_deductions = false;
15356 tmpl1 = TREE_TYPE (pat1);
15357 tmpl2 = TREE_TYPE (pat2);
15359 /* Just like what happens for functions, if we are ordering between
15360 different class template specializations, we may encounter dependent
15361 types in the arguments, and we need our dependency check functions
15362 to behave correctly. */
15363 ++processing_template_decl;
15364 targs = get_class_bindings (TREE_VALUE (pat1),
15365 CLASSTYPE_TI_ARGS (tmpl1),
15366 CLASSTYPE_TI_ARGS (tmpl2));
15370 any_deductions = true;
15373 targs = get_class_bindings (TREE_VALUE (pat2),
15374 CLASSTYPE_TI_ARGS (tmpl2),
15375 CLASSTYPE_TI_ARGS (tmpl1));
15379 any_deductions = true;
15381 --processing_template_decl;
15383 /* In the case of a tie where at least one of the class templates
15384 has a parameter pack at the end, the template with the most
15385 non-packed parameters wins. */
15388 && (template_args_variadic_p (TREE_PURPOSE (pat1))
15389 || template_args_variadic_p (TREE_PURPOSE (pat2))))
15391 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
15392 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
15393 int len1 = TREE_VEC_LENGTH (args1);
15394 int len2 = TREE_VEC_LENGTH (args2);
15396 /* We don't count the pack expansion at the end. */
15397 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
15399 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
15404 else if (len1 < len2)
15411 /* Return the template arguments that will produce the function signature
15412 DECL from the function template FN, with the explicit template
15413 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
15414 also match. Return NULL_TREE if no satisfactory arguments could be
15418 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
15420 int ntparms = DECL_NTPARMS (fn);
15421 tree targs = make_tree_vec (ntparms);
15423 tree decl_arg_types;
15425 unsigned int nargs, ix;
15428 /* Substitute the explicit template arguments into the type of DECL.
15429 The call to fn_type_unification will handle substitution into the
15431 decl_type = TREE_TYPE (decl);
15432 if (explicit_args && uses_template_parms (decl_type))
15435 tree converted_args;
15437 if (DECL_TEMPLATE_INFO (decl))
15438 tmpl = DECL_TI_TEMPLATE (decl);
15440 /* We can get here for some invalid specializations. */
15444 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
15445 explicit_args, NULL_TREE,
15447 /*require_all_args=*/false,
15448 /*use_default_args=*/false);
15449 if (converted_args == error_mark_node)
15452 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
15453 if (decl_type == error_mark_node)
15457 /* Never do unification on the 'this' parameter. */
15458 decl_arg_types = skip_artificial_parms_for (decl,
15459 TYPE_ARG_TYPES (decl_type));
15461 nargs = list_length (decl_arg_types);
15462 args = XALLOCAVEC (tree, nargs);
15463 for (arg = decl_arg_types, ix = 0;
15464 arg != NULL_TREE && arg != void_list_node;
15465 arg = TREE_CHAIN (arg), ++ix)
15466 args[ix] = TREE_VALUE (arg);
15468 if (fn_type_unification (fn, explicit_args, targs,
15470 (check_rettype || DECL_CONV_FN_P (fn)
15471 ? TREE_TYPE (decl_type) : NULL_TREE),
15472 DEDUCE_EXACT, LOOKUP_NORMAL))
15478 /* Return the innermost template arguments that, when applied to a
15479 template specialization whose innermost template parameters are
15480 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
15483 For example, suppose we have:
15485 template <class T, class U> struct S {};
15486 template <class T> struct S<T*, int> {};
15488 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
15489 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
15490 int}. The resulting vector will be {double}, indicating that `T'
15491 is bound to `double'. */
15494 get_class_bindings (tree tparms, tree spec_args, tree args)
15496 int i, ntparms = TREE_VEC_LENGTH (tparms);
15498 tree innermost_deduced_args;
15500 innermost_deduced_args = make_tree_vec (ntparms);
15501 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15503 deduced_args = copy_node (args);
15504 SET_TMPL_ARGS_LEVEL (deduced_args,
15505 TMPL_ARGS_DEPTH (deduced_args),
15506 innermost_deduced_args);
15509 deduced_args = innermost_deduced_args;
15511 if (unify (tparms, deduced_args,
15512 INNERMOST_TEMPLATE_ARGS (spec_args),
15513 INNERMOST_TEMPLATE_ARGS (args),
15517 for (i = 0; i < ntparms; ++i)
15518 if (! TREE_VEC_ELT (innermost_deduced_args, i))
15521 /* Verify that nondeduced template arguments agree with the type
15522 obtained from argument deduction.
15526 struct A { typedef int X; };
15527 template <class T, class U> struct C {};
15528 template <class T> struct C<T, typename T::X> {};
15530 Then with the instantiation `C<A, int>', we can deduce that
15531 `T' is `A' but unify () does not check whether `typename T::X'
15533 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
15534 if (spec_args == error_mark_node
15535 /* We only need to check the innermost arguments; the other
15536 arguments will always agree. */
15537 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
15538 INNERMOST_TEMPLATE_ARGS (args)))
15541 /* Now that we have bindings for all of the template arguments,
15542 ensure that the arguments deduced for the template template
15543 parameters have compatible template parameter lists. See the use
15544 of template_template_parm_bindings_ok_p in fn_type_unification
15545 for more information. */
15546 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
15549 return deduced_args;
15552 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
15553 Return the TREE_LIST node with the most specialized template, if
15554 any. If there is no most specialized template, the error_mark_node
15557 Note that this function does not look at, or modify, the
15558 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
15559 returned is one of the elements of INSTANTIATIONS, callers may
15560 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
15561 and retrieve it from the value returned. */
15564 most_specialized_instantiation (tree templates)
15568 ++processing_template_decl;
15571 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
15575 if (get_bindings (TREE_VALUE (champ),
15576 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15577 NULL_TREE, /*check_ret=*/false))
15580 if (get_bindings (TREE_VALUE (fn),
15581 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15582 NULL_TREE, /*check_ret=*/false))
15589 /* Equally specialized, move to next function. If there
15590 is no next function, nothing's most specialized. */
15591 fn = TREE_CHAIN (fn);
15599 /* Now verify that champ is better than everything earlier in the
15600 instantiation list. */
15601 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
15602 if (get_bindings (TREE_VALUE (champ),
15603 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15604 NULL_TREE, /*check_ret=*/false)
15605 || !get_bindings (TREE_VALUE (fn),
15606 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15607 NULL_TREE, /*check_ret=*/false))
15613 processing_template_decl--;
15616 return error_mark_node;
15621 /* If DECL is a specialization of some template, return the most
15622 general such template. Otherwise, returns NULL_TREE.
15624 For example, given:
15626 template <class T> struct S { template <class U> void f(U); };
15628 if TMPL is `template <class U> void S<int>::f(U)' this will return
15629 the full template. This function will not trace past partial
15630 specializations, however. For example, given in addition:
15632 template <class T> struct S<T*> { template <class U> void f(U); };
15634 if TMPL is `template <class U> void S<int*>::f(U)' this will return
15635 `template <class T> template <class U> S<T*>::f(U)'. */
15638 most_general_template (tree decl)
15640 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
15641 an immediate specialization. */
15642 if (TREE_CODE (decl) == FUNCTION_DECL)
15644 if (DECL_TEMPLATE_INFO (decl)) {
15645 decl = DECL_TI_TEMPLATE (decl);
15647 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
15648 template friend. */
15649 if (TREE_CODE (decl) != TEMPLATE_DECL)
15655 /* Look for more and more general templates. */
15656 while (DECL_TEMPLATE_INFO (decl))
15658 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
15659 (See cp-tree.h for details.) */
15660 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
15663 if (CLASS_TYPE_P (TREE_TYPE (decl))
15664 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
15667 /* Stop if we run into an explicitly specialized class template. */
15668 if (!DECL_NAMESPACE_SCOPE_P (decl)
15669 && DECL_CONTEXT (decl)
15670 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
15673 decl = DECL_TI_TEMPLATE (decl);
15679 /* Return the most specialized of the class template partial
15680 specializations of TMPL which can produce TYPE, a specialization of
15681 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
15682 a _TYPE node corresponding to the partial specialization, while the
15683 TREE_PURPOSE is the set of template arguments that must be
15684 substituted into the TREE_TYPE in order to generate TYPE.
15686 If the choice of partial specialization is ambiguous, a diagnostic
15687 is issued, and the error_mark_node is returned. If there are no
15688 partial specializations of TMPL matching TYPE, then NULL_TREE is
15692 most_specialized_class (tree type, tree tmpl)
15694 tree list = NULL_TREE;
15700 tree outer_args = NULL_TREE;
15702 tmpl = most_general_template (tmpl);
15703 args = CLASSTYPE_TI_ARGS (type);
15705 /* For determining which partial specialization to use, only the
15706 innermost args are interesting. */
15707 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15709 outer_args = strip_innermost_template_args (args, 1);
15710 args = INNERMOST_TEMPLATE_ARGS (args);
15713 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
15715 tree partial_spec_args;
15717 tree parms = TREE_VALUE (t);
15719 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
15724 ++processing_template_decl;
15726 /* Discard the outer levels of args, and then substitute in the
15727 template args from the enclosing class. */
15728 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
15729 partial_spec_args = tsubst_template_args
15730 (partial_spec_args, outer_args, tf_none, NULL_TREE);
15732 /* PARMS already refers to just the innermost parms, but the
15733 template parms in partial_spec_args had their levels lowered
15734 by tsubst, so we need to do the same for the parm list. We
15735 can't just tsubst the TREE_VEC itself, as tsubst wants to
15736 treat a TREE_VEC as an argument vector. */
15737 parms = copy_node (parms);
15738 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
15739 TREE_VEC_ELT (parms, i) =
15740 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
15742 --processing_template_decl;
15744 spec_args = get_class_bindings (parms,
15750 spec_args = add_to_template_args (outer_args, spec_args);
15751 list = tree_cons (spec_args, TREE_VALUE (t), list);
15752 TREE_TYPE (list) = TREE_TYPE (t);
15759 ambiguous_p = false;
15762 t = TREE_CHAIN (t);
15763 for (; t; t = TREE_CHAIN (t))
15765 fate = more_specialized_class (champ, t);
15772 t = TREE_CHAIN (t);
15775 ambiguous_p = true;
15784 for (t = list; t && t != champ; t = TREE_CHAIN (t))
15786 fate = more_specialized_class (champ, t);
15789 ambiguous_p = true;
15797 char *spaces = NULL;
15798 error ("ambiguous class template instantiation for %q#T", type);
15799 str = TREE_CHAIN (list) ? _("candidates are:") : _("candidate is:");
15800 for (t = list; t; t = TREE_CHAIN (t))
15802 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
15803 spaces = spaces ? spaces : get_spaces (str);
15806 return error_mark_node;
15812 /* Explicitly instantiate DECL. */
15815 do_decl_instantiation (tree decl, tree storage)
15817 tree result = NULL_TREE;
15820 if (!decl || decl == error_mark_node)
15821 /* An error occurred, for which grokdeclarator has already issued
15822 an appropriate message. */
15824 else if (! DECL_LANG_SPECIFIC (decl))
15826 error ("explicit instantiation of non-template %q#D", decl);
15829 else if (TREE_CODE (decl) == VAR_DECL)
15831 /* There is an asymmetry here in the way VAR_DECLs and
15832 FUNCTION_DECLs are handled by grokdeclarator. In the case of
15833 the latter, the DECL we get back will be marked as a
15834 template instantiation, and the appropriate
15835 DECL_TEMPLATE_INFO will be set up. This does not happen for
15836 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
15837 should handle VAR_DECLs as it currently handles
15839 if (!DECL_CLASS_SCOPE_P (decl))
15841 error ("%qD is not a static data member of a class template", decl);
15844 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
15845 if (!result || TREE_CODE (result) != VAR_DECL)
15847 error ("no matching template for %qD found", decl);
15850 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
15852 error ("type %qT for explicit instantiation %qD does not match "
15853 "declared type %qT", TREE_TYPE (result), decl,
15858 else if (TREE_CODE (decl) != FUNCTION_DECL)
15860 error ("explicit instantiation of %q#D", decl);
15866 /* Check for various error cases. Note that if the explicit
15867 instantiation is valid the RESULT will currently be marked as an
15868 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
15869 until we get here. */
15871 if (DECL_TEMPLATE_SPECIALIZATION (result))
15873 /* DR 259 [temp.spec].
15875 Both an explicit instantiation and a declaration of an explicit
15876 specialization shall not appear in a program unless the explicit
15877 instantiation follows a declaration of the explicit specialization.
15879 For a given set of template parameters, if an explicit
15880 instantiation of a template appears after a declaration of an
15881 explicit specialization for that template, the explicit
15882 instantiation has no effect. */
15885 else if (DECL_EXPLICIT_INSTANTIATION (result))
15889 No program shall explicitly instantiate any template more
15892 We check DECL_NOT_REALLY_EXTERN so as not to complain when
15893 the first instantiation was `extern' and the second is not,
15894 and EXTERN_P for the opposite case. */
15895 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
15896 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
15897 /* If an "extern" explicit instantiation follows an ordinary
15898 explicit instantiation, the template is instantiated. */
15902 else if (!DECL_IMPLICIT_INSTANTIATION (result))
15904 error ("no matching template for %qD found", result);
15907 else if (!DECL_TEMPLATE_INFO (result))
15909 permerror (input_location, "explicit instantiation of non-template %q#D", result);
15913 if (storage == NULL_TREE)
15915 else if (storage == ridpointers[(int) RID_EXTERN])
15917 if (!in_system_header && (cxx_dialect == cxx98))
15918 pedwarn (input_location, OPT_pedantic,
15919 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
15924 error ("storage class %qD applied to template instantiation", storage);
15926 check_explicit_instantiation_namespace (result);
15927 mark_decl_instantiated (result, extern_p);
15929 instantiate_decl (result, /*defer_ok=*/1,
15930 /*expl_inst_class_mem_p=*/false);
15934 mark_class_instantiated (tree t, int extern_p)
15936 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
15937 SET_CLASSTYPE_INTERFACE_KNOWN (t);
15938 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
15939 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
15942 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
15943 rest_of_type_compilation (t, 1);
15947 /* Called from do_type_instantiation through binding_table_foreach to
15948 do recursive instantiation for the type bound in ENTRY. */
15950 bt_instantiate_type_proc (binding_entry entry, void *data)
15952 tree storage = *(tree *) data;
15954 if (MAYBE_CLASS_TYPE_P (entry->type)
15955 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
15956 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
15959 /* Called from do_type_instantiation to instantiate a member
15960 (a member function or a static member variable) of an
15961 explicitly instantiated class template. */
15963 instantiate_class_member (tree decl, int extern_p)
15965 mark_decl_instantiated (decl, extern_p);
15967 instantiate_decl (decl, /*defer_ok=*/1,
15968 /*expl_inst_class_mem_p=*/true);
15971 /* Perform an explicit instantiation of template class T. STORAGE, if
15972 non-null, is the RID for extern, inline or static. COMPLAIN is
15973 nonzero if this is called from the parser, zero if called recursively,
15974 since the standard is unclear (as detailed below). */
15977 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
15982 int previous_instantiation_extern_p = 0;
15984 if (TREE_CODE (t) == TYPE_DECL)
15987 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
15989 error ("explicit instantiation of non-template type %qT", t);
15995 if (!COMPLETE_TYPE_P (t))
15997 if (complain & tf_error)
15998 error ("explicit instantiation of %q#T before definition of template",
16003 if (storage != NULL_TREE)
16005 if (!in_system_header)
16007 if (storage == ridpointers[(int) RID_EXTERN])
16009 if (cxx_dialect == cxx98)
16010 pedwarn (input_location, OPT_pedantic,
16011 "ISO C++ 1998 forbids the use of %<extern%> on "
16012 "explicit instantiations");
16015 pedwarn (input_location, OPT_pedantic,
16016 "ISO C++ forbids the use of %qE"
16017 " on explicit instantiations", storage);
16020 if (storage == ridpointers[(int) RID_INLINE])
16022 else if (storage == ridpointers[(int) RID_EXTERN])
16024 else if (storage == ridpointers[(int) RID_STATIC])
16028 error ("storage class %qD applied to template instantiation",
16034 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
16036 /* DR 259 [temp.spec].
16038 Both an explicit instantiation and a declaration of an explicit
16039 specialization shall not appear in a program unless the explicit
16040 instantiation follows a declaration of the explicit specialization.
16042 For a given set of template parameters, if an explicit
16043 instantiation of a template appears after a declaration of an
16044 explicit specialization for that template, the explicit
16045 instantiation has no effect. */
16048 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
16052 No program shall explicitly instantiate any template more
16055 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
16056 instantiation was `extern'. If EXTERN_P then the second is.
16057 These cases are OK. */
16058 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
16060 if (!previous_instantiation_extern_p && !extern_p
16061 && (complain & tf_error))
16062 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
16064 /* If we've already instantiated the template, just return now. */
16065 if (!CLASSTYPE_INTERFACE_ONLY (t))
16069 check_explicit_instantiation_namespace (TYPE_NAME (t));
16070 mark_class_instantiated (t, extern_p);
16078 /* In contrast to implicit instantiation, where only the
16079 declarations, and not the definitions, of members are
16080 instantiated, we have here:
16084 The explicit instantiation of a class template specialization
16085 implies the instantiation of all of its members not
16086 previously explicitly specialized in the translation unit
16087 containing the explicit instantiation.
16089 Of course, we can't instantiate member template classes, since
16090 we don't have any arguments for them. Note that the standard
16091 is unclear on whether the instantiation of the members are
16092 *explicit* instantiations or not. However, the most natural
16093 interpretation is that it should be an explicit instantiation. */
16096 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
16097 if (TREE_CODE (tmp) == FUNCTION_DECL
16098 && DECL_TEMPLATE_INSTANTIATION (tmp))
16099 instantiate_class_member (tmp, extern_p);
16101 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
16102 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
16103 instantiate_class_member (tmp, extern_p);
16105 if (CLASSTYPE_NESTED_UTDS (t))
16106 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
16107 bt_instantiate_type_proc, &storage);
16111 /* Given a function DECL, which is a specialization of TMPL, modify
16112 DECL to be a re-instantiation of TMPL with the same template
16113 arguments. TMPL should be the template into which tsubst'ing
16114 should occur for DECL, not the most general template.
16116 One reason for doing this is a scenario like this:
16119 void f(const T&, int i);
16121 void g() { f(3, 7); }
16124 void f(const T& t, const int i) { }
16126 Note that when the template is first instantiated, with
16127 instantiate_template, the resulting DECL will have no name for the
16128 first parameter, and the wrong type for the second. So, when we go
16129 to instantiate the DECL, we regenerate it. */
16132 regenerate_decl_from_template (tree decl, tree tmpl)
16134 /* The arguments used to instantiate DECL, from the most general
16139 args = DECL_TI_ARGS (decl);
16140 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
16142 /* Make sure that we can see identifiers, and compute access
16144 push_access_scope (decl);
16146 if (TREE_CODE (decl) == FUNCTION_DECL)
16154 args_depth = TMPL_ARGS_DEPTH (args);
16155 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
16156 if (args_depth > parms_depth)
16157 args = get_innermost_template_args (args, parms_depth);
16159 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
16160 args, tf_error, NULL_TREE);
16162 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
16165 /* Merge parameter declarations. */
16166 decl_parm = skip_artificial_parms_for (decl,
16167 DECL_ARGUMENTS (decl));
16169 = skip_artificial_parms_for (code_pattern,
16170 DECL_ARGUMENTS (code_pattern));
16171 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
16176 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16177 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
16178 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
16180 parm_type = type_decays_to (parm_type);
16181 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16182 TREE_TYPE (decl_parm) = parm_type;
16183 attributes = DECL_ATTRIBUTES (pattern_parm);
16184 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16186 DECL_ATTRIBUTES (decl_parm) = attributes;
16187 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16189 decl_parm = TREE_CHAIN (decl_parm);
16190 pattern_parm = TREE_CHAIN (pattern_parm);
16192 /* Merge any parameters that match with the function parameter
16194 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
16197 tree expanded_types;
16198 /* Expand the TYPE_PACK_EXPANSION that provides the types for
16199 the parameters in this function parameter pack. */
16200 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
16201 args, tf_error, NULL_TREE);
16202 len = TREE_VEC_LENGTH (expanded_types);
16203 for (i = 0; i < len; i++)
16208 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16209 /* Rename the parameter to include the index. */
16210 DECL_NAME (decl_parm) =
16211 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
16212 parm_type = TREE_VEC_ELT (expanded_types, i);
16213 parm_type = type_decays_to (parm_type);
16214 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16215 TREE_TYPE (decl_parm) = parm_type;
16216 attributes = DECL_ATTRIBUTES (pattern_parm);
16217 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16219 DECL_ATTRIBUTES (decl_parm) = attributes;
16220 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16222 decl_parm = TREE_CHAIN (decl_parm);
16225 /* Merge additional specifiers from the CODE_PATTERN. */
16226 if (DECL_DECLARED_INLINE_P (code_pattern)
16227 && !DECL_DECLARED_INLINE_P (decl))
16228 DECL_DECLARED_INLINE_P (decl) = 1;
16230 else if (TREE_CODE (decl) == VAR_DECL)
16231 DECL_INITIAL (decl) =
16232 tsubst_expr (DECL_INITIAL (code_pattern), args,
16233 tf_error, DECL_TI_TEMPLATE (decl),
16234 /*integral_constant_expression_p=*/false);
16236 gcc_unreachable ();
16238 pop_access_scope (decl);
16241 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
16242 substituted to get DECL. */
16245 template_for_substitution (tree decl)
16247 tree tmpl = DECL_TI_TEMPLATE (decl);
16249 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
16250 for the instantiation. This is not always the most general
16251 template. Consider, for example:
16254 struct S { template <class U> void f();
16255 template <> void f<int>(); };
16257 and an instantiation of S<double>::f<int>. We want TD to be the
16258 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
16259 while (/* An instantiation cannot have a definition, so we need a
16260 more general template. */
16261 DECL_TEMPLATE_INSTANTIATION (tmpl)
16262 /* We must also deal with friend templates. Given:
16264 template <class T> struct S {
16265 template <class U> friend void f() {};
16268 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
16269 so far as the language is concerned, but that's still
16270 where we get the pattern for the instantiation from. On
16271 other hand, if the definition comes outside the class, say:
16273 template <class T> struct S {
16274 template <class U> friend void f();
16276 template <class U> friend void f() {}
16278 we don't need to look any further. That's what the check for
16279 DECL_INITIAL is for. */
16280 || (TREE_CODE (decl) == FUNCTION_DECL
16281 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
16282 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
16284 /* The present template, TD, should not be a definition. If it
16285 were a definition, we should be using it! Note that we
16286 cannot restructure the loop to just keep going until we find
16287 a template with a definition, since that might go too far if
16288 a specialization was declared, but not defined. */
16289 gcc_assert (TREE_CODE (decl) != VAR_DECL
16290 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
16292 /* Fetch the more general template. */
16293 tmpl = DECL_TI_TEMPLATE (tmpl);
16299 /* Returns true if we need to instantiate this template instance even if we
16300 know we aren't going to emit it.. */
16303 always_instantiate_p (tree decl)
16305 /* We always instantiate inline functions so that we can inline them. An
16306 explicit instantiation declaration prohibits implicit instantiation of
16307 non-inline functions. With high levels of optimization, we would
16308 normally inline non-inline functions -- but we're not allowed to do
16309 that for "extern template" functions. Therefore, we check
16310 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
16311 return ((TREE_CODE (decl) == FUNCTION_DECL
16312 && DECL_DECLARED_INLINE_P (decl))
16313 /* And we need to instantiate static data members so that
16314 their initializers are available in integral constant
16316 || (TREE_CODE (decl) == VAR_DECL
16317 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)));
16320 /* Produce the definition of D, a _DECL generated from a template. If
16321 DEFER_OK is nonzero, then we don't have to actually do the
16322 instantiation now; we just have to do it sometime. Normally it is
16323 an error if this is an explicit instantiation but D is undefined.
16324 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
16325 explicitly instantiated class template. */
16328 instantiate_decl (tree d, int defer_ok,
16329 bool expl_inst_class_mem_p)
16331 tree tmpl = DECL_TI_TEMPLATE (d);
16338 bool pattern_defined;
16340 location_t saved_loc = input_location;
16343 /* This function should only be used to instantiate templates for
16344 functions and static member variables. */
16345 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
16346 || TREE_CODE (d) == VAR_DECL);
16348 /* Variables are never deferred; if instantiation is required, they
16349 are instantiated right away. That allows for better code in the
16350 case that an expression refers to the value of the variable --
16351 if the variable has a constant value the referring expression can
16352 take advantage of that fact. */
16353 if (TREE_CODE (d) == VAR_DECL)
16356 /* Don't instantiate cloned functions. Instead, instantiate the
16357 functions they cloned. */
16358 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
16359 d = DECL_CLONED_FUNCTION (d);
16361 if (DECL_TEMPLATE_INSTANTIATED (d)
16362 || DECL_TEMPLATE_SPECIALIZATION (d))
16363 /* D has already been instantiated or explicitly specialized, so
16364 there's nothing for us to do here.
16366 It might seem reasonable to check whether or not D is an explicit
16367 instantiation, and, if so, stop here. But when an explicit
16368 instantiation is deferred until the end of the compilation,
16369 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
16370 the instantiation. */
16373 /* Check to see whether we know that this template will be
16374 instantiated in some other file, as with "extern template"
16376 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
16378 /* In general, we do not instantiate such templates. */
16379 if (external_p && !always_instantiate_p (d))
16382 gen_tmpl = most_general_template (tmpl);
16383 gen_args = DECL_TI_ARGS (d);
16385 if (tmpl != gen_tmpl)
16386 /* We should already have the extra args. */
16387 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
16388 == TMPL_ARGS_DEPTH (gen_args));
16389 /* And what's in the hash table should match D. */
16390 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
16391 || spec == NULL_TREE);
16393 /* This needs to happen before any tsubsting. */
16394 if (! push_tinst_level (d))
16397 timevar_push (TV_PARSE);
16399 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
16400 for the instantiation. */
16401 td = template_for_substitution (d);
16402 code_pattern = DECL_TEMPLATE_RESULT (td);
16404 /* We should never be trying to instantiate a member of a class
16405 template or partial specialization. */
16406 gcc_assert (d != code_pattern);
16408 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
16409 || DECL_TEMPLATE_SPECIALIZATION (td))
16410 /* In the case of a friend template whose definition is provided
16411 outside the class, we may have too many arguments. Drop the
16412 ones we don't need. The same is true for specializations. */
16413 args = get_innermost_template_args
16414 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
16418 if (TREE_CODE (d) == FUNCTION_DECL)
16419 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
16421 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
16423 /* We may be in the middle of deferred access check. Disable it now. */
16424 push_deferring_access_checks (dk_no_deferred);
16426 /* Unless an explicit instantiation directive has already determined
16427 the linkage of D, remember that a definition is available for
16429 if (pattern_defined
16430 && !DECL_INTERFACE_KNOWN (d)
16431 && !DECL_NOT_REALLY_EXTERN (d))
16432 mark_definable (d);
16434 input_location = DECL_SOURCE_LOCATION (d);
16436 /* If D is a member of an explicitly instantiated class template,
16437 and no definition is available, treat it like an implicit
16439 if (!pattern_defined && expl_inst_class_mem_p
16440 && DECL_EXPLICIT_INSTANTIATION (d))
16442 DECL_NOT_REALLY_EXTERN (d) = 0;
16443 DECL_INTERFACE_KNOWN (d) = 0;
16444 SET_DECL_IMPLICIT_INSTANTIATION (d);
16447 /* Recheck the substitutions to obtain any warning messages
16448 about ignoring cv qualifiers. Don't do this for artificial decls,
16449 as it breaks the context-sensitive substitution for lambda op(). */
16450 if (!defer_ok && !DECL_ARTIFICIAL (d))
16452 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
16453 tree type = TREE_TYPE (gen);
16455 /* Make sure that we can see identifiers, and compute access
16456 correctly. D is already the target FUNCTION_DECL with the
16458 push_access_scope (d);
16460 if (TREE_CODE (gen) == FUNCTION_DECL)
16462 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
16463 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
16465 /* Don't simply tsubst the function type, as that will give
16466 duplicate warnings about poor parameter qualifications.
16467 The function arguments are the same as the decl_arguments
16468 without the top level cv qualifiers. */
16469 type = TREE_TYPE (type);
16471 tsubst (type, gen_args, tf_warning_or_error, d);
16473 pop_access_scope (d);
16476 /* Defer all other templates, unless we have been explicitly
16477 forbidden from doing so. */
16478 if (/* If there is no definition, we cannot instantiate the
16481 /* If it's OK to postpone instantiation, do so. */
16483 /* If this is a static data member that will be defined
16484 elsewhere, we don't want to instantiate the entire data
16485 member, but we do want to instantiate the initializer so that
16486 we can substitute that elsewhere. */
16487 || (external_p && TREE_CODE (d) == VAR_DECL))
16489 /* The definition of the static data member is now required so
16490 we must substitute the initializer. */
16491 if (TREE_CODE (d) == VAR_DECL
16492 && !DECL_INITIAL (d)
16493 && DECL_INITIAL (code_pattern))
16498 ns = decl_namespace_context (d);
16499 push_nested_namespace (ns);
16500 push_nested_class (DECL_CONTEXT (d));
16501 init = tsubst_expr (DECL_INITIAL (code_pattern),
16503 tf_warning_or_error, NULL_TREE,
16504 /*integral_constant_expression_p=*/false);
16505 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
16506 /*asmspec_tree=*/NULL_TREE,
16507 LOOKUP_ONLYCONVERTING);
16508 pop_nested_class ();
16509 pop_nested_namespace (ns);
16512 /* We restore the source position here because it's used by
16513 add_pending_template. */
16514 input_location = saved_loc;
16516 if (at_eof && !pattern_defined
16517 && DECL_EXPLICIT_INSTANTIATION (d)
16518 && DECL_NOT_REALLY_EXTERN (d))
16521 The definition of a non-exported function template, a
16522 non-exported member function template, or a non-exported
16523 member function or static data member of a class template
16524 shall be present in every translation unit in which it is
16525 explicitly instantiated. */
16526 permerror (input_location, "explicit instantiation of %qD "
16527 "but no definition available", d);
16529 /* ??? Historically, we have instantiated inline functions, even
16530 when marked as "extern template". */
16531 if (!(external_p && TREE_CODE (d) == VAR_DECL))
16532 add_pending_template (d);
16535 /* Tell the repository that D is available in this translation unit
16536 -- and see if it is supposed to be instantiated here. */
16537 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
16539 /* In a PCH file, despite the fact that the repository hasn't
16540 requested instantiation in the PCH it is still possible that
16541 an instantiation will be required in a file that includes the
16544 add_pending_template (d);
16545 /* Instantiate inline functions so that the inliner can do its
16546 job, even though we'll not be emitting a copy of this
16548 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
16552 need_push = !cfun || !global_bindings_p ();
16554 push_to_top_level ();
16556 /* Mark D as instantiated so that recursive calls to
16557 instantiate_decl do not try to instantiate it again. */
16558 DECL_TEMPLATE_INSTANTIATED (d) = 1;
16560 /* Regenerate the declaration in case the template has been modified
16561 by a subsequent redeclaration. */
16562 regenerate_decl_from_template (d, td);
16564 /* We already set the file and line above. Reset them now in case
16565 they changed as a result of calling regenerate_decl_from_template. */
16566 input_location = DECL_SOURCE_LOCATION (d);
16568 if (TREE_CODE (d) == VAR_DECL)
16572 /* Clear out DECL_RTL; whatever was there before may not be right
16573 since we've reset the type of the declaration. */
16574 SET_DECL_RTL (d, NULL_RTX);
16575 DECL_IN_AGGR_P (d) = 0;
16577 /* The initializer is placed in DECL_INITIAL by
16578 regenerate_decl_from_template. Pull it out so that
16579 cp_finish_decl can process it. */
16580 init = DECL_INITIAL (d);
16581 DECL_INITIAL (d) = NULL_TREE;
16582 DECL_INITIALIZED_P (d) = 0;
16584 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
16585 initializer. That function will defer actual emission until
16586 we have a chance to determine linkage. */
16587 DECL_EXTERNAL (d) = 0;
16589 /* Enter the scope of D so that access-checking works correctly. */
16590 push_nested_class (DECL_CONTEXT (d));
16591 cp_finish_decl (d, init, false, NULL_TREE, 0);
16592 pop_nested_class ();
16594 else if (TREE_CODE (d) == FUNCTION_DECL)
16596 htab_t saved_local_specializations;
16601 /* Save away the current list, in case we are instantiating one
16602 template from within the body of another. */
16603 saved_local_specializations = local_specializations;
16605 /* Set up the list of local specializations. */
16606 local_specializations = htab_create (37,
16607 hash_local_specialization,
16608 eq_local_specializations,
16611 /* Set up context. */
16612 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
16614 /* Create substitution entries for the parameters. */
16615 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
16616 tmpl_parm = DECL_ARGUMENTS (subst_decl);
16617 spec_parm = DECL_ARGUMENTS (d);
16618 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
16620 register_local_specialization (spec_parm, tmpl_parm);
16621 spec_parm = skip_artificial_parms_for (d, spec_parm);
16622 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
16624 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
16626 register_local_specialization (spec_parm, tmpl_parm);
16627 tmpl_parm = TREE_CHAIN (tmpl_parm);
16628 spec_parm = TREE_CHAIN (spec_parm);
16630 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
16632 /* Register the (value) argument pack as a specialization of
16633 TMPL_PARM, then move on. */
16634 tree argpack = make_fnparm_pack (spec_parm);
16635 register_local_specialization (argpack, tmpl_parm);
16636 tmpl_parm = TREE_CHAIN (tmpl_parm);
16637 spec_parm = NULL_TREE;
16639 gcc_assert (!spec_parm);
16641 /* Substitute into the body of the function. */
16642 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
16643 tf_warning_or_error, tmpl,
16644 /*integral_constant_expression_p=*/false);
16646 /* Set the current input_location to the end of the function
16647 so that finish_function knows where we are. */
16648 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
16650 /* We don't need the local specializations any more. */
16651 htab_delete (local_specializations);
16652 local_specializations = saved_local_specializations;
16654 /* Finish the function. */
16655 d = finish_function (0);
16656 expand_or_defer_fn (d);
16659 /* We're not deferring instantiation any more. */
16660 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
16663 pop_from_top_level ();
16666 input_location = saved_loc;
16667 pop_deferring_access_checks ();
16668 pop_tinst_level ();
16670 timevar_pop (TV_PARSE);
16675 /* Run through the list of templates that we wish we could
16676 instantiate, and instantiate any we can. RETRIES is the
16677 number of times we retry pending template instantiation. */
16680 instantiate_pending_templates (int retries)
16683 location_t saved_loc = input_location;
16685 /* Instantiating templates may trigger vtable generation. This in turn
16686 may require further template instantiations. We place a limit here
16687 to avoid infinite loop. */
16688 if (pending_templates && retries >= max_tinst_depth)
16690 tree decl = pending_templates->tinst->decl;
16692 error ("template instantiation depth exceeds maximum of %d"
16693 " instantiating %q+D, possibly from virtual table generation"
16694 " (use -ftemplate-depth-NN to increase the maximum)",
16695 max_tinst_depth, decl);
16696 if (TREE_CODE (decl) == FUNCTION_DECL)
16697 /* Pretend that we defined it. */
16698 DECL_INITIAL (decl) = error_mark_node;
16704 struct pending_template **t = &pending_templates;
16705 struct pending_template *last = NULL;
16709 tree instantiation = reopen_tinst_level ((*t)->tinst);
16710 bool complete = false;
16712 if (TYPE_P (instantiation))
16716 if (!COMPLETE_TYPE_P (instantiation))
16718 instantiate_class_template (instantiation);
16719 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
16720 for (fn = TYPE_METHODS (instantiation);
16722 fn = TREE_CHAIN (fn))
16723 if (! DECL_ARTIFICIAL (fn))
16724 instantiate_decl (fn,
16726 /*expl_inst_class_mem_p=*/false);
16727 if (COMPLETE_TYPE_P (instantiation))
16731 complete = COMPLETE_TYPE_P (instantiation);
16735 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
16736 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
16739 = instantiate_decl (instantiation,
16741 /*expl_inst_class_mem_p=*/false);
16742 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
16746 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
16747 || DECL_TEMPLATE_INSTANTIATED (instantiation));
16751 /* If INSTANTIATION has been instantiated, then we don't
16752 need to consider it again in the future. */
16760 current_tinst_level = NULL;
16762 last_pending_template = last;
16764 while (reconsider);
16766 input_location = saved_loc;
16769 /* Substitute ARGVEC into T, which is a list of initializers for
16770 either base class or a non-static data member. The TREE_PURPOSEs
16771 are DECLs, and the TREE_VALUEs are the initializer values. Used by
16772 instantiate_decl. */
16775 tsubst_initializer_list (tree t, tree argvec)
16777 tree inits = NULL_TREE;
16779 for (; t; t = TREE_CHAIN (t))
16783 tree expanded_bases = NULL_TREE;
16784 tree expanded_arguments = NULL_TREE;
16787 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
16792 /* Expand the base class expansion type into separate base
16794 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
16795 tf_warning_or_error,
16797 if (expanded_bases == error_mark_node)
16800 /* We'll be building separate TREE_LISTs of arguments for
16802 len = TREE_VEC_LENGTH (expanded_bases);
16803 expanded_arguments = make_tree_vec (len);
16804 for (i = 0; i < len; i++)
16805 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
16807 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
16808 expand each argument in the TREE_VALUE of t. */
16809 expr = make_node (EXPR_PACK_EXPANSION);
16810 PACK_EXPANSION_PARAMETER_PACKS (expr) =
16811 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
16813 if (TREE_VALUE (t) == void_type_node)
16814 /* VOID_TYPE_NODE is used to indicate
16815 value-initialization. */
16817 for (i = 0; i < len; i++)
16818 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
16822 /* Substitute parameter packs into each argument in the
16824 in_base_initializer = 1;
16825 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
16827 tree expanded_exprs;
16829 /* Expand the argument. */
16830 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
16832 = tsubst_pack_expansion (expr, argvec,
16833 tf_warning_or_error,
16835 if (expanded_exprs == error_mark_node)
16838 /* Prepend each of the expanded expressions to the
16839 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
16840 for (i = 0; i < len; i++)
16842 TREE_VEC_ELT (expanded_arguments, i) =
16843 tree_cons (NULL_TREE,
16844 TREE_VEC_ELT (expanded_exprs, i),
16845 TREE_VEC_ELT (expanded_arguments, i));
16848 in_base_initializer = 0;
16850 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
16851 since we built them backwards. */
16852 for (i = 0; i < len; i++)
16854 TREE_VEC_ELT (expanded_arguments, i) =
16855 nreverse (TREE_VEC_ELT (expanded_arguments, i));
16860 for (i = 0; i < len; ++i)
16862 if (expanded_bases)
16864 decl = TREE_VEC_ELT (expanded_bases, i);
16865 decl = expand_member_init (decl);
16866 init = TREE_VEC_ELT (expanded_arguments, i);
16870 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
16871 tf_warning_or_error, NULL_TREE);
16873 decl = expand_member_init (decl);
16874 if (decl && !DECL_P (decl))
16875 in_base_initializer = 1;
16877 init = tsubst_expr (TREE_VALUE (t), argvec,
16878 tf_warning_or_error, NULL_TREE,
16879 /*integral_constant_expression_p=*/false);
16880 in_base_initializer = 0;
16885 init = build_tree_list (decl, init);
16886 TREE_CHAIN (init) = inits;
16894 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
16897 set_current_access_from_decl (tree decl)
16899 if (TREE_PRIVATE (decl))
16900 current_access_specifier = access_private_node;
16901 else if (TREE_PROTECTED (decl))
16902 current_access_specifier = access_protected_node;
16904 current_access_specifier = access_public_node;
16907 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
16908 is the instantiation (which should have been created with
16909 start_enum) and ARGS are the template arguments to use. */
16912 tsubst_enum (tree tag, tree newtag, tree args)
16916 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
16921 decl = TREE_VALUE (e);
16922 /* Note that in a template enum, the TREE_VALUE is the
16923 CONST_DECL, not the corresponding INTEGER_CST. */
16924 value = tsubst_expr (DECL_INITIAL (decl),
16925 args, tf_warning_or_error, NULL_TREE,
16926 /*integral_constant_expression_p=*/true);
16928 /* Give this enumeration constant the correct access. */
16929 set_current_access_from_decl (decl);
16931 /* Actually build the enumerator itself. */
16932 build_enumerator (DECL_NAME (decl), value, newtag);
16935 finish_enum (newtag);
16936 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
16937 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
16940 /* DECL is a FUNCTION_DECL that is a template specialization. Return
16941 its type -- but without substituting the innermost set of template
16942 arguments. So, innermost set of template parameters will appear in
16946 get_mostly_instantiated_function_type (tree decl)
16954 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
16955 targs = DECL_TI_ARGS (decl);
16956 tparms = DECL_TEMPLATE_PARMS (tmpl);
16957 parm_depth = TMPL_PARMS_DEPTH (tparms);
16959 /* There should be as many levels of arguments as there are levels
16961 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
16963 fn_type = TREE_TYPE (tmpl);
16965 if (parm_depth == 1)
16966 /* No substitution is necessary. */
16970 int i, save_access_control;
16973 /* Replace the innermost level of the TARGS with NULL_TREEs to
16974 let tsubst know not to substitute for those parameters. */
16975 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
16976 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
16977 SET_TMPL_ARGS_LEVEL (partial_args, i,
16978 TMPL_ARGS_LEVEL (targs, i));
16979 SET_TMPL_ARGS_LEVEL (partial_args,
16980 TMPL_ARGS_DEPTH (targs),
16981 make_tree_vec (DECL_NTPARMS (tmpl)));
16983 /* Disable access control as this function is used only during
16985 save_access_control = flag_access_control;
16986 flag_access_control = 0;
16988 ++processing_template_decl;
16989 /* Now, do the (partial) substitution to figure out the
16990 appropriate function type. */
16991 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
16992 --processing_template_decl;
16994 /* Substitute into the template parameters to obtain the real
16995 innermost set of parameters. This step is important if the
16996 innermost set of template parameters contains value
16997 parameters whose types depend on outer template parameters. */
16998 TREE_VEC_LENGTH (partial_args)--;
16999 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
17001 flag_access_control = save_access_control;
17007 /* Return truthvalue if we're processing a template different from
17008 the last one involved in diagnostics. */
17010 problematic_instantiation_changed (void)
17012 return last_template_error_tick != tinst_level_tick;
17015 /* Remember current template involved in diagnostics. */
17017 record_last_problematic_instantiation (void)
17019 last_template_error_tick = tinst_level_tick;
17022 struct tinst_level *
17023 current_instantiation (void)
17025 return current_tinst_level;
17028 /* [temp.param] Check that template non-type parm TYPE is of an allowable
17029 type. Return zero for ok, nonzero for disallowed. Issue error and
17030 warning messages under control of COMPLAIN. */
17033 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
17035 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
17037 else if (POINTER_TYPE_P (type))
17039 else if (TYPE_PTR_TO_MEMBER_P (type))
17041 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
17043 else if (TREE_CODE (type) == TYPENAME_TYPE)
17046 if (complain & tf_error)
17047 error ("%q#T is not a valid type for a template constant parameter", type);
17051 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
17052 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
17055 dependent_type_p_r (tree type)
17061 A type is dependent if it is:
17063 -- a template parameter. Template template parameters are types
17064 for us (since TYPE_P holds true for them) so we handle
17066 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17067 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
17069 /* -- a qualified-id with a nested-name-specifier which contains a
17070 class-name that names a dependent type or whose unqualified-id
17071 names a dependent type. */
17072 if (TREE_CODE (type) == TYPENAME_TYPE)
17074 /* -- a cv-qualified type where the cv-unqualified type is
17076 type = TYPE_MAIN_VARIANT (type);
17077 /* -- a compound type constructed from any dependent type. */
17078 if (TYPE_PTR_TO_MEMBER_P (type))
17079 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
17080 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
17082 else if (TREE_CODE (type) == POINTER_TYPE
17083 || TREE_CODE (type) == REFERENCE_TYPE)
17084 return dependent_type_p (TREE_TYPE (type));
17085 else if (TREE_CODE (type) == FUNCTION_TYPE
17086 || TREE_CODE (type) == METHOD_TYPE)
17090 if (dependent_type_p (TREE_TYPE (type)))
17092 for (arg_type = TYPE_ARG_TYPES (type);
17094 arg_type = TREE_CHAIN (arg_type))
17095 if (dependent_type_p (TREE_VALUE (arg_type)))
17099 /* -- an array type constructed from any dependent type or whose
17100 size is specified by a constant expression that is
17101 value-dependent. */
17102 if (TREE_CODE (type) == ARRAY_TYPE)
17104 if (TYPE_DOMAIN (type)
17105 && dependent_type_p (TYPE_DOMAIN (type)))
17107 return dependent_type_p (TREE_TYPE (type));
17109 else if (TREE_CODE (type) == INTEGER_TYPE
17110 && !TREE_CONSTANT (TYPE_MAX_VALUE (type)))
17112 /* If this is the TYPE_DOMAIN of an array type, consider it
17113 dependent. We already checked for value-dependence in
17114 compute_array_index_type. */
17115 return type_dependent_expression_p (TYPE_MAX_VALUE (type));
17118 /* -- a template-id in which either the template name is a template
17120 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
17122 /* ... or any of the template arguments is a dependent type or
17123 an expression that is type-dependent or value-dependent. */
17124 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
17125 && (any_dependent_template_arguments_p
17126 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
17129 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
17130 argument of the `typeof' expression is not type-dependent, then
17131 it should already been have resolved. */
17132 if (TREE_CODE (type) == TYPEOF_TYPE
17133 || TREE_CODE (type) == DECLTYPE_TYPE)
17136 /* A template argument pack is dependent if any of its packed
17138 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
17140 tree args = ARGUMENT_PACK_ARGS (type);
17141 int i, len = TREE_VEC_LENGTH (args);
17142 for (i = 0; i < len; ++i)
17143 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17147 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
17148 be template parameters. */
17149 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
17152 /* The standard does not specifically mention types that are local
17153 to template functions or local classes, but they should be
17154 considered dependent too. For example:
17156 template <int I> void f() {
17161 The size of `E' cannot be known until the value of `I' has been
17162 determined. Therefore, `E' must be considered dependent. */
17163 scope = TYPE_CONTEXT (type);
17164 if (scope && TYPE_P (scope))
17165 return dependent_type_p (scope);
17166 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
17167 return type_dependent_expression_p (scope);
17169 /* Other types are non-dependent. */
17173 /* Returns TRUE if TYPE is dependent, in the sense of
17174 [temp.dep.type]. */
17177 dependent_type_p (tree type)
17179 /* If there are no template parameters in scope, then there can't be
17180 any dependent types. */
17181 if (!processing_template_decl)
17183 /* If we are not processing a template, then nobody should be
17184 providing us with a dependent type. */
17186 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
17190 /* If the type is NULL, we have not computed a type for the entity
17191 in question; in that case, the type is dependent. */
17195 /* Erroneous types can be considered non-dependent. */
17196 if (type == error_mark_node)
17199 /* If we have not already computed the appropriate value for TYPE,
17201 if (!TYPE_DEPENDENT_P_VALID (type))
17203 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
17204 TYPE_DEPENDENT_P_VALID (type) = 1;
17207 return TYPE_DEPENDENT_P (type);
17210 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
17211 lookup. In other words, a dependent type that is not the current
17215 dependent_scope_p (tree scope)
17217 return (scope && TYPE_P (scope) && dependent_type_p (scope)
17218 && !currently_open_class (scope));
17221 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
17224 dependent_scope_ref_p (tree expression, bool criterion (tree))
17229 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
17231 if (!TYPE_P (TREE_OPERAND (expression, 0)))
17234 scope = TREE_OPERAND (expression, 0);
17235 name = TREE_OPERAND (expression, 1);
17239 An id-expression is type-dependent if it contains a
17240 nested-name-specifier that contains a class-name that names a
17242 /* The suggested resolution to Core Issue 224 implies that if the
17243 qualifying type is the current class, then we must peek
17246 && currently_open_class (scope)
17247 && !criterion (name))
17249 if (dependent_type_p (scope))
17255 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
17256 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
17260 value_dependent_expression_p (tree expression)
17262 if (!processing_template_decl)
17265 /* A name declared with a dependent type. */
17266 if (DECL_P (expression) && type_dependent_expression_p (expression))
17269 switch (TREE_CODE (expression))
17271 case IDENTIFIER_NODE:
17272 /* A name that has not been looked up -- must be dependent. */
17275 case TEMPLATE_PARM_INDEX:
17276 /* A non-type template parm. */
17280 /* A non-type template parm. */
17281 if (DECL_TEMPLATE_PARM_P (expression))
17283 return value_dependent_expression_p (DECL_INITIAL (expression));
17286 /* A constant with integral or enumeration type and is initialized
17287 with an expression that is value-dependent. */
17288 if (DECL_INITIAL (expression)
17289 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
17290 && value_dependent_expression_p (DECL_INITIAL (expression)))
17294 case DYNAMIC_CAST_EXPR:
17295 case STATIC_CAST_EXPR:
17296 case CONST_CAST_EXPR:
17297 case REINTERPRET_CAST_EXPR:
17299 /* These expressions are value-dependent if the type to which
17300 the cast occurs is dependent or the expression being casted
17301 is value-dependent. */
17303 tree type = TREE_TYPE (expression);
17305 if (dependent_type_p (type))
17308 /* A functional cast has a list of operands. */
17309 expression = TREE_OPERAND (expression, 0);
17312 /* If there are no operands, it must be an expression such
17313 as "int()". This should not happen for aggregate types
17314 because it would form non-constant expressions. */
17315 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
17320 if (TREE_CODE (expression) == TREE_LIST)
17321 return any_value_dependent_elements_p (expression);
17323 return value_dependent_expression_p (expression);
17328 /* A `sizeof' expression is value-dependent if the operand is
17329 type-dependent or is a pack expansion. */
17330 expression = TREE_OPERAND (expression, 0);
17331 if (PACK_EXPANSION_P (expression))
17333 else if (TYPE_P (expression))
17334 return dependent_type_p (expression);
17335 return type_dependent_expression_p (expression);
17338 return dependent_scope_ref_p (expression, value_dependent_expression_p);
17340 case COMPONENT_REF:
17341 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
17342 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
17345 /* A CALL_EXPR may appear in a constant expression if it is a
17346 call to a builtin function, e.g., __builtin_constant_p. All
17347 such calls are value-dependent. */
17350 case NONTYPE_ARGUMENT_PACK:
17351 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
17352 is value-dependent. */
17354 tree values = ARGUMENT_PACK_ARGS (expression);
17355 int i, len = TREE_VEC_LENGTH (values);
17357 for (i = 0; i < len; ++i)
17358 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
17366 tree type2 = TRAIT_EXPR_TYPE2 (expression);
17367 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
17368 || (type2 ? dependent_type_p (type2) : false));
17372 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
17373 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
17376 /* A constant expression is value-dependent if any subexpression is
17377 value-dependent. */
17378 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
17380 case tcc_reference:
17382 return (value_dependent_expression_p
17383 (TREE_OPERAND (expression, 0)));
17385 case tcc_comparison:
17387 return ((value_dependent_expression_p
17388 (TREE_OPERAND (expression, 0)))
17389 || (value_dependent_expression_p
17390 (TREE_OPERAND (expression, 1))));
17392 case tcc_expression:
17396 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
17397 /* In some cases, some of the operands may be missing.
17398 (For example, in the case of PREDECREMENT_EXPR, the
17399 amount to increment by may be missing.) That doesn't
17400 make the expression dependent. */
17401 if (TREE_OPERAND (expression, i)
17402 && (value_dependent_expression_p
17403 (TREE_OPERAND (expression, i))))
17413 /* The expression is not value-dependent. */
17417 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
17418 [temp.dep.expr]. */
17421 type_dependent_expression_p (tree expression)
17423 if (!processing_template_decl)
17426 if (expression == error_mark_node)
17429 /* An unresolved name is always dependent. */
17430 if (TREE_CODE (expression) == IDENTIFIER_NODE
17431 || TREE_CODE (expression) == USING_DECL)
17434 /* Some expression forms are never type-dependent. */
17435 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
17436 || TREE_CODE (expression) == SIZEOF_EXPR
17437 || TREE_CODE (expression) == ALIGNOF_EXPR
17438 || TREE_CODE (expression) == TRAIT_EXPR
17439 || TREE_CODE (expression) == TYPEID_EXPR
17440 || TREE_CODE (expression) == DELETE_EXPR
17441 || TREE_CODE (expression) == VEC_DELETE_EXPR
17442 || TREE_CODE (expression) == THROW_EXPR)
17445 /* The types of these expressions depends only on the type to which
17446 the cast occurs. */
17447 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
17448 || TREE_CODE (expression) == STATIC_CAST_EXPR
17449 || TREE_CODE (expression) == CONST_CAST_EXPR
17450 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
17451 || TREE_CODE (expression) == CAST_EXPR)
17452 return dependent_type_p (TREE_TYPE (expression));
17454 /* The types of these expressions depends only on the type created
17455 by the expression. */
17456 if (TREE_CODE (expression) == NEW_EXPR
17457 || TREE_CODE (expression) == VEC_NEW_EXPR)
17459 /* For NEW_EXPR tree nodes created inside a template, either
17460 the object type itself or a TREE_LIST may appear as the
17462 tree type = TREE_OPERAND (expression, 1);
17463 if (TREE_CODE (type) == TREE_LIST)
17464 /* This is an array type. We need to check array dimensions
17466 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
17467 || value_dependent_expression_p
17468 (TREE_OPERAND (TREE_VALUE (type), 1));
17470 return dependent_type_p (type);
17473 if (TREE_CODE (expression) == SCOPE_REF
17474 && dependent_scope_ref_p (expression,
17475 type_dependent_expression_p))
17478 if (TREE_CODE (expression) == FUNCTION_DECL
17479 && DECL_LANG_SPECIFIC (expression)
17480 && DECL_TEMPLATE_INFO (expression)
17481 && (any_dependent_template_arguments_p
17482 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
17485 if (TREE_CODE (expression) == TEMPLATE_DECL
17486 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
17489 if (TREE_CODE (expression) == STMT_EXPR)
17490 expression = stmt_expr_value_expr (expression);
17492 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
17497 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
17499 if (type_dependent_expression_p (elt))
17505 if (TREE_TYPE (expression) == unknown_type_node)
17507 if (TREE_CODE (expression) == ADDR_EXPR)
17508 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
17509 if (TREE_CODE (expression) == COMPONENT_REF
17510 || TREE_CODE (expression) == OFFSET_REF)
17512 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
17514 expression = TREE_OPERAND (expression, 1);
17515 if (TREE_CODE (expression) == IDENTIFIER_NODE)
17518 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
17519 if (TREE_CODE (expression) == SCOPE_REF)
17522 if (TREE_CODE (expression) == BASELINK)
17523 expression = BASELINK_FUNCTIONS (expression);
17525 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
17527 if (any_dependent_template_arguments_p
17528 (TREE_OPERAND (expression, 1)))
17530 expression = TREE_OPERAND (expression, 0);
17532 gcc_assert (TREE_CODE (expression) == OVERLOAD
17533 || TREE_CODE (expression) == FUNCTION_DECL);
17537 if (type_dependent_expression_p (OVL_CURRENT (expression)))
17539 expression = OVL_NEXT (expression);
17544 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
17546 return (dependent_type_p (TREE_TYPE (expression)));
17549 /* Like type_dependent_expression_p, but it also works while not processing
17550 a template definition, i.e. during substitution or mangling. */
17553 type_dependent_expression_p_push (tree expr)
17556 ++processing_template_decl;
17557 b = type_dependent_expression_p (expr);
17558 --processing_template_decl;
17562 /* Returns TRUE if ARGS contains a type-dependent expression. */
17565 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
17570 for (i = 0; VEC_iterate (tree, args, i, arg); ++i)
17572 if (type_dependent_expression_p (arg))
17578 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
17579 expressions) contains any value-dependent expressions. */
17582 any_value_dependent_elements_p (const_tree list)
17584 for (; list; list = TREE_CHAIN (list))
17585 if (value_dependent_expression_p (TREE_VALUE (list)))
17591 /* Returns TRUE if the ARG (a template argument) is dependent. */
17594 dependent_template_arg_p (tree arg)
17596 if (!processing_template_decl)
17599 if (TREE_CODE (arg) == TEMPLATE_DECL
17600 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17601 return dependent_template_p (arg);
17602 else if (ARGUMENT_PACK_P (arg))
17604 tree args = ARGUMENT_PACK_ARGS (arg);
17605 int i, len = TREE_VEC_LENGTH (args);
17606 for (i = 0; i < len; ++i)
17608 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17614 else if (TYPE_P (arg))
17615 return dependent_type_p (arg);
17617 return (type_dependent_expression_p (arg)
17618 || value_dependent_expression_p (arg));
17621 /* Returns true if ARGS (a collection of template arguments) contains
17622 any types that require structural equality testing. */
17625 any_template_arguments_need_structural_equality_p (tree args)
17632 if (args == error_mark_node)
17635 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17637 tree level = TMPL_ARGS_LEVEL (args, i + 1);
17638 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17640 tree arg = TREE_VEC_ELT (level, j);
17641 tree packed_args = NULL_TREE;
17644 if (ARGUMENT_PACK_P (arg))
17646 /* Look inside the argument pack. */
17647 packed_args = ARGUMENT_PACK_ARGS (arg);
17648 len = TREE_VEC_LENGTH (packed_args);
17651 for (k = 0; k < len; ++k)
17654 arg = TREE_VEC_ELT (packed_args, k);
17656 if (error_operand_p (arg))
17658 else if (TREE_CODE (arg) == TEMPLATE_DECL
17659 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17661 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
17663 else if (!TYPE_P (arg) && TREE_TYPE (arg)
17664 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
17673 /* Returns true if ARGS (a collection of template arguments) contains
17674 any dependent arguments. */
17677 any_dependent_template_arguments_p (const_tree args)
17684 if (args == error_mark_node)
17687 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17689 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
17690 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17691 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
17698 /* Returns TRUE if the template TMPL is dependent. */
17701 dependent_template_p (tree tmpl)
17703 if (TREE_CODE (tmpl) == OVERLOAD)
17707 if (dependent_template_p (OVL_FUNCTION (tmpl)))
17709 tmpl = OVL_CHAIN (tmpl);
17714 /* Template template parameters are dependent. */
17715 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
17716 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
17718 /* So are names that have not been looked up. */
17719 if (TREE_CODE (tmpl) == SCOPE_REF
17720 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
17722 /* So are member templates of dependent classes. */
17723 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
17724 return dependent_type_p (DECL_CONTEXT (tmpl));
17728 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
17731 dependent_template_id_p (tree tmpl, tree args)
17733 return (dependent_template_p (tmpl)
17734 || any_dependent_template_arguments_p (args));
17737 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
17741 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
17745 if (!processing_template_decl)
17748 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
17750 tree decl = TREE_VEC_ELT (declv, i);
17751 tree init = TREE_VEC_ELT (initv, i);
17752 tree cond = TREE_VEC_ELT (condv, i);
17753 tree incr = TREE_VEC_ELT (incrv, i);
17755 if (type_dependent_expression_p (decl))
17758 if (init && type_dependent_expression_p (init))
17761 if (type_dependent_expression_p (cond))
17764 if (COMPARISON_CLASS_P (cond)
17765 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
17766 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
17769 if (TREE_CODE (incr) == MODOP_EXPR)
17771 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
17772 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
17775 else if (type_dependent_expression_p (incr))
17777 else if (TREE_CODE (incr) == MODIFY_EXPR)
17779 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
17781 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
17783 tree t = TREE_OPERAND (incr, 1);
17784 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
17785 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
17794 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
17795 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
17796 no such TYPE can be found. Note that this function peers inside
17797 uninstantiated templates and therefore should be used only in
17798 extremely limited situations. ONLY_CURRENT_P restricts this
17799 peering to the currently open classes hierarchy (which is required
17800 when comparing types). */
17803 resolve_typename_type (tree type, bool only_current_p)
17812 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
17814 scope = TYPE_CONTEXT (type);
17815 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
17816 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
17817 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
17818 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
17819 identifier of the TYPENAME_TYPE anymore.
17820 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
17821 TYPENAME_TYPE instead, we avoid messing up with a possible
17822 typedef variant case. */
17823 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
17825 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
17826 it first before we can figure out what NAME refers to. */
17827 if (TREE_CODE (scope) == TYPENAME_TYPE)
17828 scope = resolve_typename_type (scope, only_current_p);
17829 /* If we don't know what SCOPE refers to, then we cannot resolve the
17831 if (TREE_CODE (scope) == TYPENAME_TYPE)
17833 /* If the SCOPE is a template type parameter, we have no way of
17834 resolving the name. */
17835 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
17837 /* If the SCOPE is not the current instantiation, there's no reason
17838 to look inside it. */
17839 if (only_current_p && !currently_open_class (scope))
17841 /* If this is a typedef, we don't want to look inside (c++/11987). */
17842 if (typedef_variant_p (type))
17844 /* If SCOPE isn't the template itself, it will not have a valid
17845 TYPE_FIELDS list. */
17846 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
17847 /* scope is either the template itself or a compatible instantiation
17848 like X<T>, so look up the name in the original template. */
17849 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
17851 /* scope is a partial instantiation, so we can't do the lookup or we
17852 will lose the template arguments. */
17854 /* Enter the SCOPE so that name lookup will be resolved as if we
17855 were in the class definition. In particular, SCOPE will no
17856 longer be considered a dependent type. */
17857 pushed_scope = push_scope (scope);
17858 /* Look up the declaration. */
17859 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
17861 result = NULL_TREE;
17863 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
17864 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
17867 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
17868 && TREE_CODE (decl) == TYPE_DECL)
17870 result = TREE_TYPE (decl);
17871 if (result == error_mark_node)
17872 result = NULL_TREE;
17874 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
17875 && DECL_CLASS_TEMPLATE_P (decl))
17879 /* Obtain the template and the arguments. */
17880 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
17881 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
17882 /* Instantiate the template. */
17883 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
17884 /*entering_scope=*/0,
17885 tf_error | tf_user);
17886 if (result == error_mark_node)
17887 result = NULL_TREE;
17890 /* Leave the SCOPE. */
17892 pop_scope (pushed_scope);
17894 /* If we failed to resolve it, return the original typename. */
17898 /* If lookup found a typename type, resolve that too. */
17899 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
17901 /* Ill-formed programs can cause infinite recursion here, so we
17902 must catch that. */
17903 TYPENAME_IS_RESOLVING_P (type) = 1;
17904 result = resolve_typename_type (result, only_current_p);
17905 TYPENAME_IS_RESOLVING_P (type) = 0;
17908 /* Qualify the resulting type. */
17909 quals = cp_type_quals (type);
17911 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
17916 /* EXPR is an expression which is not type-dependent. Return a proxy
17917 for EXPR that can be used to compute the types of larger
17918 expressions containing EXPR. */
17921 build_non_dependent_expr (tree expr)
17925 /* Preserve null pointer constants so that the type of things like
17926 "p == 0" where "p" is a pointer can be determined. */
17927 if (null_ptr_cst_p (expr))
17929 /* Preserve OVERLOADs; the functions must be available to resolve
17932 if (TREE_CODE (inner_expr) == STMT_EXPR)
17933 inner_expr = stmt_expr_value_expr (inner_expr);
17934 if (TREE_CODE (inner_expr) == ADDR_EXPR)
17935 inner_expr = TREE_OPERAND (inner_expr, 0);
17936 if (TREE_CODE (inner_expr) == COMPONENT_REF)
17937 inner_expr = TREE_OPERAND (inner_expr, 1);
17938 if (is_overloaded_fn (inner_expr)
17939 || TREE_CODE (inner_expr) == OFFSET_REF)
17941 /* There is no need to return a proxy for a variable. */
17942 if (TREE_CODE (expr) == VAR_DECL)
17944 /* Preserve string constants; conversions from string constants to
17945 "char *" are allowed, even though normally a "const char *"
17946 cannot be used to initialize a "char *". */
17947 if (TREE_CODE (expr) == STRING_CST)
17949 /* Preserve arithmetic constants, as an optimization -- there is no
17950 reason to create a new node. */
17951 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
17953 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
17954 There is at least one place where we want to know that a
17955 particular expression is a throw-expression: when checking a ?:
17956 expression, there are special rules if the second or third
17957 argument is a throw-expression. */
17958 if (TREE_CODE (expr) == THROW_EXPR)
17961 if (TREE_CODE (expr) == COND_EXPR)
17962 return build3 (COND_EXPR,
17964 TREE_OPERAND (expr, 0),
17965 (TREE_OPERAND (expr, 1)
17966 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
17967 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
17968 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
17969 if (TREE_CODE (expr) == COMPOUND_EXPR
17970 && !COMPOUND_EXPR_OVERLOADED (expr))
17971 return build2 (COMPOUND_EXPR,
17973 TREE_OPERAND (expr, 0),
17974 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
17976 /* If the type is unknown, it can't really be non-dependent */
17977 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
17979 /* Otherwise, build a NON_DEPENDENT_EXPR.
17981 REFERENCE_TYPEs are not stripped for expressions in templates
17982 because doing so would play havoc with mangling. Consider, for
17985 template <typename T> void f<T& g>() { g(); }
17987 In the body of "f", the expression for "g" will have
17988 REFERENCE_TYPE, even though the standard says that it should
17989 not. The reason is that we must preserve the syntactic form of
17990 the expression so that mangling (say) "f<g>" inside the body of
17991 "f" works out correctly. Therefore, the REFERENCE_TYPE is
17993 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
17996 /* ARGS is a vector of expressions as arguments to a function call.
17997 Replace the arguments with equivalent non-dependent expressions.
17998 This modifies ARGS in place. */
18001 make_args_non_dependent (VEC(tree,gc) *args)
18006 for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix)
18008 tree newarg = build_non_dependent_expr (arg);
18010 VEC_replace (tree, args, ix, newarg);
18014 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
18015 with a level one deeper than the actual template parms. */
18020 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
18021 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
18022 TYPE_DECL, get_identifier ("auto"), au);
18023 TYPE_STUB_DECL (au) = TYPE_NAME (au);
18024 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
18025 (0, processing_template_decl + 1, processing_template_decl + 1,
18026 TYPE_NAME (au), NULL_TREE);
18027 TYPE_CANONICAL (au) = canonical_type_parameter (au);
18028 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
18029 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
18034 /* Given type ARG, return std::initializer_list<ARG>. */
18039 tree std_init_list = namespace_binding
18040 (get_identifier ("initializer_list"), std_node);
18042 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
18044 error ("deducing from brace-enclosed initializer list requires "
18045 "#include <initializer_list>");
18046 return error_mark_node;
18048 argvec = make_tree_vec (1);
18049 TREE_VEC_ELT (argvec, 0) = arg;
18050 return lookup_template_class (std_init_list, argvec, NULL_TREE,
18051 NULL_TREE, 0, tf_warning_or_error);
18054 /* Replace auto in TYPE with std::initializer_list<auto>. */
18057 listify_autos (tree type, tree auto_node)
18059 tree init_auto = listify (auto_node);
18060 tree argvec = make_tree_vec (1);
18061 TREE_VEC_ELT (argvec, 0) = init_auto;
18062 if (processing_template_decl)
18063 argvec = add_to_template_args (current_template_args (), argvec);
18064 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18067 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
18068 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
18071 do_auto_deduction (tree type, tree init, tree auto_node)
18073 tree parms, tparms, targs;
18077 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
18078 with either a new invented type template parameter U or, if the
18079 initializer is a braced-init-list (8.5.4), with
18080 std::initializer_list<U>. */
18081 if (BRACE_ENCLOSED_INITIALIZER_P (init))
18082 type = listify_autos (type, auto_node);
18084 parms = build_tree_list (NULL_TREE, type);
18086 tparms = make_tree_vec (1);
18087 targs = make_tree_vec (1);
18088 TREE_VEC_ELT (tparms, 0)
18089 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
18090 val = type_unification_real (tparms, targs, parms, args, 1, 0,
18091 DEDUCE_CALL, LOOKUP_NORMAL);
18094 error ("unable to deduce %qT from %qE", type, init);
18095 return error_mark_node;
18098 /* If the list of declarators contains more than one declarator, the type
18099 of each declared variable is determined as described above. If the
18100 type deduced for the template parameter U is not the same in each
18101 deduction, the program is ill-formed. */
18102 if (TREE_TYPE (auto_node)
18103 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
18105 error ("inconsistent deduction for %qT: %qT and then %qT",
18106 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
18107 return error_mark_node;
18109 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
18111 if (processing_template_decl)
18112 targs = add_to_template_args (current_template_args (), targs);
18113 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
18116 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
18120 splice_late_return_type (tree type, tree late_return_type)
18124 if (late_return_type == NULL_TREE)
18126 argvec = make_tree_vec (1);
18127 TREE_VEC_ELT (argvec, 0) = late_return_type;
18128 if (processing_template_decl)
18129 argvec = add_to_template_args (current_template_args (), argvec);
18130 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18133 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
18136 is_auto (const_tree type)
18138 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18139 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
18145 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
18146 appear as a type-specifier for the declaration in question, we don't
18147 have to look through the whole type. */
18150 type_uses_auto (tree type)
18152 enum tree_code code;
18153 if (is_auto (type))
18156 code = TREE_CODE (type);
18158 if (code == POINTER_TYPE || code == REFERENCE_TYPE
18159 || code == OFFSET_TYPE || code == FUNCTION_TYPE
18160 || code == METHOD_TYPE || code == ARRAY_TYPE)
18161 return type_uses_auto (TREE_TYPE (type));
18163 if (TYPE_PTRMEMFUNC_P (type))
18164 return type_uses_auto (TREE_TYPE (TREE_TYPE
18165 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
18170 /* For a given template T, return the vector of typedefs referenced
18171 in T for which access check is needed at T instantiation time.
18172 T is either a FUNCTION_DECL or a RECORD_TYPE.
18173 Those typedefs were added to T by the function
18174 append_type_to_template_for_access_check. */
18176 VEC(qualified_typedef_usage_t,gc)*
18177 get_types_needing_access_check (tree t)
18180 VEC(qualified_typedef_usage_t,gc) *result = NULL;
18182 if (!t || t == error_mark_node)
18185 if (!(ti = get_template_info (t)))
18188 if (CLASS_TYPE_P (t)
18189 || TREE_CODE (t) == FUNCTION_DECL)
18191 if (!TI_TEMPLATE (ti))
18194 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
18200 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
18201 tied to T. That list of typedefs will be access checked at
18202 T instantiation time.
18203 T is either a FUNCTION_DECL or a RECORD_TYPE.
18204 TYPE_DECL is a TYPE_DECL node representing a typedef.
18205 SCOPE is the scope through which TYPE_DECL is accessed.
18206 LOCATION is the location of the usage point of TYPE_DECL.
18208 This function is a subroutine of
18209 append_type_to_template_for_access_check. */
18212 append_type_to_template_for_access_check_1 (tree t,
18215 location_t location)
18217 qualified_typedef_usage_t typedef_usage;
18220 if (!t || t == error_mark_node)
18223 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
18224 || CLASS_TYPE_P (t))
18226 && TREE_CODE (type_decl) == TYPE_DECL
18229 if (!(ti = get_template_info (t)))
18232 gcc_assert (TI_TEMPLATE (ti));
18234 typedef_usage.typedef_decl = type_decl;
18235 typedef_usage.context = scope;
18236 typedef_usage.locus = location;
18238 VEC_safe_push (qualified_typedef_usage_t, gc,
18239 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
18243 /* Append TYPE_DECL to the template TEMPL.
18244 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
18245 At TEMPL instanciation time, TYPE_DECL will be checked to see
18246 if it can be accessed through SCOPE.
18247 LOCATION is the location of the usage point of TYPE_DECL.
18249 e.g. consider the following code snippet:
18256 template<class U> struct S
18258 C::myint mi; // <-- usage point of the typedef C::myint
18263 At S<char> instantiation time, we need to check the access of C::myint
18264 In other words, we need to check the access of the myint typedef through
18265 the C scope. For that purpose, this function will add the myint typedef
18266 and the scope C through which its being accessed to a list of typedefs
18267 tied to the template S. That list will be walked at template instantiation
18268 time and access check performed on each typedefs it contains.
18269 Note that this particular code snippet should yield an error because
18270 myint is private to C. */
18273 append_type_to_template_for_access_check (tree templ,
18276 location_t location)
18278 qualified_typedef_usage_t *iter;
18281 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
18283 /* Make sure we don't append the type to the template twice. */
18285 VEC_iterate (qualified_typedef_usage_t,
18286 get_types_needing_access_check (templ),
18289 if (iter->typedef_decl == type_decl && scope == iter->context)
18292 append_type_to_template_for_access_check_1 (templ, type_decl,
18296 /* Set up the hash tables for template instantiations. */
18299 init_template_processing (void)
18301 decl_specializations = htab_create_ggc (37,
18302 hash_specialization,
18303 eq_specializations,
18305 type_specializations = htab_create_ggc (37,
18306 hash_specialization,
18307 eq_specializations,
18311 #include "gt-cp-pt.h"