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 tree reopen_tinst_level (struct tinst_level *);
123 static tree tsubst_initializer_list (tree, tree);
124 static tree get_class_bindings (tree, tree, tree);
125 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
127 static void tsubst_enum (tree, tree, tree);
128 static tree add_to_template_args (tree, tree);
129 static tree add_outermost_template_args (tree, tree);
130 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
131 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
133 static int type_unification_real (tree, tree, tree, const tree *,
134 unsigned int, int, unification_kind_t, int);
135 static void note_template_header (int);
136 static tree convert_nontype_argument_function (tree, tree);
137 static tree convert_nontype_argument (tree, tree);
138 static tree convert_template_argument (tree, tree, tree,
139 tsubst_flags_t, int, tree);
140 static int for_each_template_parm (tree, tree_fn_t, void*,
141 struct pointer_set_t*, bool);
142 static tree expand_template_argument_pack (tree);
143 static tree build_template_parm_index (int, int, int, tree, tree);
144 static bool inline_needs_template_parms (tree);
145 static void push_inline_template_parms_recursive (tree, int);
146 static tree retrieve_local_specialization (tree);
147 static void register_local_specialization (tree, tree);
148 static hashval_t hash_specialization (const void *p);
149 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
150 static int mark_template_parm (tree, void *);
151 static int template_parm_this_level_p (tree, void *);
152 static tree tsubst_friend_function (tree, tree);
153 static tree tsubst_friend_class (tree, tree);
154 static int can_complete_type_without_circularity (tree);
155 static tree get_bindings (tree, tree, tree, bool);
156 static int template_decl_level (tree);
157 static int check_cv_quals_for_unify (int, tree, tree);
158 static void template_parm_level_and_index (tree, int*, int*);
159 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
160 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
161 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
162 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
163 static void regenerate_decl_from_template (tree, tree);
164 static tree most_specialized_class (tree, tree);
165 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
166 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
167 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
168 static bool check_specialization_scope (void);
169 static tree process_partial_specialization (tree);
170 static void set_current_access_from_decl (tree);
171 static tree get_template_base (tree, tree, tree, tree);
172 static tree try_class_unification (tree, tree, tree, tree);
173 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
175 static bool template_template_parm_bindings_ok_p (tree, tree);
176 static int template_args_equal (tree, tree);
177 static void tsubst_default_arguments (tree);
178 static tree for_each_template_parm_r (tree *, int *, void *);
179 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
180 static void copy_default_args_to_explicit_spec (tree);
181 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
182 static int eq_local_specializations (const void *, const void *);
183 static bool dependent_template_arg_p (tree);
184 static bool any_template_arguments_need_structural_equality_p (tree);
185 static bool dependent_type_p_r (tree);
186 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
187 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
189 static tree tsubst_decl (tree, tree, tsubst_flags_t);
190 static void perform_typedefs_access_check (tree tmpl, tree targs);
191 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
193 static hashval_t iterative_hash_template_arg (tree arg, hashval_t val);
194 static tree listify (tree);
195 static tree listify_autos (tree, tree);
197 /* Make the current scope suitable for access checking when we are
198 processing T. T can be FUNCTION_DECL for instantiated function
199 template, or VAR_DECL for static member variable (need by
200 instantiate_decl). */
203 push_access_scope (tree t)
205 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
206 || TREE_CODE (t) == VAR_DECL);
208 if (DECL_FRIEND_CONTEXT (t))
209 push_nested_class (DECL_FRIEND_CONTEXT (t));
210 else if (DECL_CLASS_SCOPE_P (t))
211 push_nested_class (DECL_CONTEXT (t));
213 push_to_top_level ();
215 if (TREE_CODE (t) == FUNCTION_DECL)
217 saved_access_scope = tree_cons
218 (NULL_TREE, current_function_decl, saved_access_scope);
219 current_function_decl = t;
223 /* Restore the scope set up by push_access_scope. T is the node we
227 pop_access_scope (tree t)
229 if (TREE_CODE (t) == FUNCTION_DECL)
231 current_function_decl = TREE_VALUE (saved_access_scope);
232 saved_access_scope = TREE_CHAIN (saved_access_scope);
235 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
238 pop_from_top_level ();
241 /* Do any processing required when DECL (a member template
242 declaration) is finished. Returns the TEMPLATE_DECL corresponding
243 to DECL, unless it is a specialization, in which case the DECL
244 itself is returned. */
247 finish_member_template_decl (tree decl)
249 if (decl == error_mark_node)
250 return error_mark_node;
252 gcc_assert (DECL_P (decl));
254 if (TREE_CODE (decl) == TYPE_DECL)
258 type = TREE_TYPE (decl);
259 if (type == error_mark_node)
260 return error_mark_node;
261 if (MAYBE_CLASS_TYPE_P (type)
262 && CLASSTYPE_TEMPLATE_INFO (type)
263 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
265 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
266 check_member_template (tmpl);
271 else if (TREE_CODE (decl) == FIELD_DECL)
272 error ("data member %qD cannot be a member template", decl);
273 else if (DECL_TEMPLATE_INFO (decl))
275 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
277 check_member_template (DECL_TI_TEMPLATE (decl));
278 return DECL_TI_TEMPLATE (decl);
284 error ("invalid member template declaration %qD", decl);
286 return error_mark_node;
289 /* Create a template info node. */
292 build_template_info (tree template_decl, tree template_args)
294 tree result = make_node (TEMPLATE_INFO);
295 TI_TEMPLATE (result) = template_decl;
296 TI_ARGS (result) = template_args;
300 /* Return the template info node corresponding to T, whatever T is. */
303 get_template_info (const_tree t)
305 tree tinfo = NULL_TREE;
307 if (!t || t == error_mark_node)
310 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
311 tinfo = DECL_TEMPLATE_INFO (t);
313 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
316 if (TAGGED_TYPE_P (t))
317 tinfo = TYPE_TEMPLATE_INFO (t);
318 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
319 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
324 /* Returns the template nesting level of the indicated class TYPE.
334 A<T>::B<U> has depth two, while A<T> has depth one.
335 Both A<T>::B<int> and A<int>::B<U> have depth one, if
336 they are instantiations, not specializations.
338 This function is guaranteed to return 0 if passed NULL_TREE so
339 that, for example, `template_class_depth (current_class_type)' is
343 template_class_depth (tree type)
348 type && TREE_CODE (type) != NAMESPACE_DECL;
349 type = (TREE_CODE (type) == FUNCTION_DECL)
350 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
352 tree tinfo = get_template_info (type);
354 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
355 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
362 /* Subroutine of maybe_begin_member_template_processing.
363 Returns true if processing DECL needs us to push template parms. */
366 inline_needs_template_parms (tree decl)
368 if (! DECL_TEMPLATE_INFO (decl))
371 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
372 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
375 /* Subroutine of maybe_begin_member_template_processing.
376 Push the template parms in PARMS, starting from LEVELS steps into the
377 chain, and ending at the beginning, since template parms are listed
381 push_inline_template_parms_recursive (tree parmlist, int levels)
383 tree parms = TREE_VALUE (parmlist);
387 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
389 ++processing_template_decl;
390 current_template_parms
391 = tree_cons (size_int (processing_template_decl),
392 parms, current_template_parms);
393 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
395 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
397 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
399 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
401 if (parm == error_mark_node)
404 gcc_assert (DECL_P (parm));
406 switch (TREE_CODE (parm))
415 /* Make a CONST_DECL as is done in process_template_parm.
416 It is ugly that we recreate this here; the original
417 version built in process_template_parm is no longer
419 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
420 CONST_DECL, DECL_NAME (parm),
422 DECL_ARTIFICIAL (decl) = 1;
423 TREE_CONSTANT (decl) = 1;
424 TREE_READONLY (decl) = 1;
425 DECL_INITIAL (decl) = DECL_INITIAL (parm);
426 SET_DECL_TEMPLATE_PARM_P (decl);
437 /* Restore the template parameter context for a member template or
438 a friend template defined in a class definition. */
441 maybe_begin_member_template_processing (tree decl)
446 if (inline_needs_template_parms (decl))
448 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
449 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
451 if (DECL_TEMPLATE_SPECIALIZATION (decl))
454 parms = TREE_CHAIN (parms);
457 push_inline_template_parms_recursive (parms, levels);
460 /* Remember how many levels of template parameters we pushed so that
461 we can pop them later. */
462 VEC_safe_push (int, heap, inline_parm_levels, levels);
465 /* Undo the effects of maybe_begin_member_template_processing. */
468 maybe_end_member_template_processing (void)
473 if (VEC_length (int, inline_parm_levels) == 0)
476 last = VEC_pop (int, inline_parm_levels);
477 for (i = 0; i < last; ++i)
479 --processing_template_decl;
480 current_template_parms = TREE_CHAIN (current_template_parms);
485 /* Return a new template argument vector which contains all of ARGS,
486 but has as its innermost set of arguments the EXTRA_ARGS. */
489 add_to_template_args (tree args, tree extra_args)
496 if (args == NULL_TREE)
499 extra_depth = TMPL_ARGS_DEPTH (extra_args);
500 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
502 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
503 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
505 for (j = 1; j <= extra_depth; ++j, ++i)
506 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
511 /* Like add_to_template_args, but only the outermost ARGS are added to
512 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
513 (EXTRA_ARGS) levels are added. This function is used to combine
514 the template arguments from a partial instantiation with the
515 template arguments used to attain the full instantiation from the
516 partial instantiation. */
519 add_outermost_template_args (tree args, tree extra_args)
523 /* If there are more levels of EXTRA_ARGS than there are ARGS,
524 something very fishy is going on. */
525 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
527 /* If *all* the new arguments will be the EXTRA_ARGS, just return
529 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
532 /* For the moment, we make ARGS look like it contains fewer levels. */
533 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
535 new_args = add_to_template_args (args, extra_args);
537 /* Now, we restore ARGS to its full dimensions. */
538 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
543 /* Return the N levels of innermost template arguments from the ARGS. */
546 get_innermost_template_args (tree args, int n)
554 /* If N is 1, just return the innermost set of template arguments. */
556 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
558 /* If we're not removing anything, just return the arguments we were
560 extra_levels = TMPL_ARGS_DEPTH (args) - n;
561 gcc_assert (extra_levels >= 0);
562 if (extra_levels == 0)
565 /* Make a new set of arguments, not containing the outer arguments. */
566 new_args = make_tree_vec (n);
567 for (i = 1; i <= n; ++i)
568 SET_TMPL_ARGS_LEVEL (new_args, i,
569 TMPL_ARGS_LEVEL (args, i + extra_levels));
574 /* The inverse of get_innermost_template_args: Return all but the innermost
575 EXTRA_LEVELS levels of template arguments from the ARGS. */
578 strip_innermost_template_args (tree args, int extra_levels)
581 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
586 /* If N is 1, just return the outermost set of template arguments. */
588 return TMPL_ARGS_LEVEL (args, 1);
590 /* If we're not removing anything, just return the arguments we were
592 gcc_assert (extra_levels >= 0);
593 if (extra_levels == 0)
596 /* Make a new set of arguments, not containing the inner arguments. */
597 new_args = make_tree_vec (n);
598 for (i = 1; i <= n; ++i)
599 SET_TMPL_ARGS_LEVEL (new_args, i,
600 TMPL_ARGS_LEVEL (args, i));
605 /* We've got a template header coming up; push to a new level for storing
609 begin_template_parm_list (void)
611 /* We use a non-tag-transparent scope here, which causes pushtag to
612 put tags in this scope, rather than in the enclosing class or
613 namespace scope. This is the right thing, since we want
614 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
615 global template class, push_template_decl handles putting the
616 TEMPLATE_DECL into top-level scope. For a nested template class,
619 template <class T> struct S1 {
620 template <class T> struct S2 {};
623 pushtag contains special code to call pushdecl_with_scope on the
624 TEMPLATE_DECL for S2. */
625 begin_scope (sk_template_parms, NULL);
626 ++processing_template_decl;
627 ++processing_template_parmlist;
628 note_template_header (0);
631 /* This routine is called when a specialization is declared. If it is
632 invalid to declare a specialization here, an error is reported and
633 false is returned, otherwise this routine will return true. */
636 check_specialization_scope (void)
638 tree scope = current_scope ();
642 An explicit specialization shall be declared in the namespace of
643 which the template is a member, or, for member templates, in the
644 namespace of which the enclosing class or enclosing class
645 template is a member. An explicit specialization of a member
646 function, member class or static data member of a class template
647 shall be declared in the namespace of which the class template
649 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
651 error ("explicit specialization in non-namespace scope %qD", scope);
657 In an explicit specialization declaration for a member of a class
658 template or a member template that appears in namespace scope,
659 the member template and some of its enclosing class templates may
660 remain unspecialized, except that the declaration shall not
661 explicitly specialize a class member template if its enclosing
662 class templates are not explicitly specialized as well. */
663 if (current_template_parms)
665 error ("enclosing class templates are not explicitly specialized");
672 /* We've just seen template <>. */
675 begin_specialization (void)
677 begin_scope (sk_template_spec, NULL);
678 note_template_header (1);
679 return check_specialization_scope ();
682 /* Called at then end of processing a declaration preceded by
686 end_specialization (void)
689 reset_specialization ();
692 /* Any template <>'s that we have seen thus far are not referring to a
693 function specialization. */
696 reset_specialization (void)
698 processing_specialization = 0;
699 template_header_count = 0;
702 /* We've just seen a template header. If SPECIALIZATION is nonzero,
703 it was of the form template <>. */
706 note_template_header (int specialization)
708 processing_specialization = specialization;
709 template_header_count++;
712 /* We're beginning an explicit instantiation. */
715 begin_explicit_instantiation (void)
717 gcc_assert (!processing_explicit_instantiation);
718 processing_explicit_instantiation = true;
723 end_explicit_instantiation (void)
725 gcc_assert (processing_explicit_instantiation);
726 processing_explicit_instantiation = false;
729 /* An explicit specialization or partial specialization TMPL is being
730 declared. Check that the namespace in which the specialization is
731 occurring is permissible. Returns false iff it is invalid to
732 specialize TMPL in the current namespace. */
735 check_specialization_namespace (tree tmpl)
737 tree tpl_ns = decl_namespace_context (tmpl);
741 An explicit specialization shall be declared in the namespace of
742 which the template is a member, or, for member templates, in the
743 namespace of which the enclosing class or enclosing class
744 template is a member. An explicit specialization of a member
745 function, member class or static data member of a class template
746 shall be declared in the namespace of which the class template is
748 if (current_scope() != DECL_CONTEXT (tmpl)
749 && !at_namespace_scope_p ())
751 error ("specialization of %qD must appear at namespace scope", tmpl);
754 if (is_associated_namespace (current_namespace, tpl_ns))
755 /* Same or super-using namespace. */
759 permerror (input_location, "specialization of %qD in different namespace", tmpl);
760 permerror (input_location, " from definition of %q+#D", tmpl);
765 /* SPEC is an explicit instantiation. Check that it is valid to
766 perform this explicit instantiation in the current namespace. */
769 check_explicit_instantiation_namespace (tree spec)
773 /* DR 275: An explicit instantiation shall appear in an enclosing
774 namespace of its template. */
775 ns = decl_namespace_context (spec);
776 if (!is_ancestor (current_namespace, ns))
777 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
778 "(which does not enclose namespace %qD)",
779 spec, current_namespace, ns);
782 /* The TYPE is being declared. If it is a template type, that means it
783 is a partial specialization. Do appropriate error-checking. */
786 maybe_process_partial_specialization (tree type)
790 if (type == error_mark_node)
791 return error_mark_node;
793 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
795 error ("name of class shadows template template parameter %qD",
797 return error_mark_node;
800 context = TYPE_CONTEXT (type);
802 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
804 /* This is for ordinary explicit specialization and partial
805 specialization of a template class such as:
807 template <> class C<int>;
811 template <class T> class C<T*>;
813 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
815 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
816 && !COMPLETE_TYPE_P (type))
818 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
819 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
820 if (processing_template_decl)
822 if (push_template_decl (TYPE_MAIN_DECL (type))
824 return error_mark_node;
827 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
828 error ("specialization of %qT after instantiation", type);
830 else if (CLASS_TYPE_P (type)
831 && !CLASSTYPE_USE_TEMPLATE (type)
832 && CLASSTYPE_TEMPLATE_INFO (type)
833 && context && CLASS_TYPE_P (context)
834 && CLASSTYPE_TEMPLATE_INFO (context))
836 /* This is for an explicit specialization of member class
837 template according to [temp.expl.spec/18]:
839 template <> template <class U> class C<int>::D;
841 The context `C<int>' must be an implicit instantiation.
842 Otherwise this is just a member class template declared
845 template <> class C<int> { template <class U> class D; };
846 template <> template <class U> class C<int>::D;
848 In the first case, `C<int>::D' is a specialization of `C<T>::D'
849 while in the second case, `C<int>::D' is a primary template
850 and `C<T>::D' may not exist. */
852 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
853 && !COMPLETE_TYPE_P (type))
856 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
858 if (current_namespace
859 != decl_namespace_context (tmpl))
861 permerror (input_location, "specializing %q#T in different namespace", type);
862 permerror (input_location, " from definition of %q+#D", tmpl);
865 /* Check for invalid specialization after instantiation:
867 template <> template <> class C<int>::D<int>;
868 template <> template <class U> class C<int>::D; */
870 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
871 t; t = TREE_CHAIN (t))
873 tree inst = TREE_VALUE (t);
874 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
876 /* We already have a full specialization of this partial
877 instantiation. Reassign it to the new member
878 specialization template. */
882 elt.tmpl = most_general_template (tmpl);
883 elt.args = CLASSTYPE_TI_ARGS (inst);
886 htab_remove_elt (type_specializations, &elt);
889 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
891 slot = (spec_entry **)
892 htab_find_slot (type_specializations, &elt, INSERT);
893 *slot = GGC_NEW (spec_entry);
896 else if (COMPLETE_TYPE_P (inst) || TYPE_BEING_DEFINED (inst))
897 /* But if we've had an implicit instantiation, that's a
898 problem ([temp.expl.spec]/6). */
899 error ("specialization %qT after instantiation %qT",
903 /* Mark TYPE as a specialization. And as a result, we only
904 have one level of template argument for the innermost
906 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
907 CLASSTYPE_TI_ARGS (type)
908 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
911 else if (processing_specialization)
913 error ("explicit specialization of non-template %qT", type);
914 return error_mark_node;
920 /* Returns nonzero if we can optimize the retrieval of specializations
921 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
922 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
925 optimize_specialization_lookup_p (tree tmpl)
927 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
928 && DECL_CLASS_SCOPE_P (tmpl)
929 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
931 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
932 /* The optimized lookup depends on the fact that the
933 template arguments for the member function template apply
934 purely to the containing class, which is not true if the
935 containing class is an explicit or partial
937 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
938 && !DECL_MEMBER_TEMPLATE_P (tmpl)
939 && !DECL_CONV_FN_P (tmpl)
940 /* It is possible to have a template that is not a member
941 template and is not a member of a template class:
943 template <typename T>
944 struct S { friend A::f(); };
946 Here, the friend function is a template, but the context does
947 not have template information. The optimized lookup relies
948 on having ARGS be the template arguments for both the class
949 and the function template. */
950 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
953 /* Retrieve the specialization (in the sense of [temp.spec] - a
954 specialization is either an instantiation or an explicit
955 specialization) of TMPL for the given template ARGS. If there is
956 no such specialization, return NULL_TREE. The ARGS are a vector of
957 arguments, or a vector of vectors of arguments, in the case of
958 templates with more than one level of parameters.
960 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
961 then we search for a partial specialization matching ARGS. This
962 parameter is ignored if TMPL is not a class template. */
965 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
967 if (args == error_mark_node)
970 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
972 /* There should be as many levels of arguments as there are
973 levels of parameters. */
974 gcc_assert (TMPL_ARGS_DEPTH (args)
975 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
977 if (optimize_specialization_lookup_p (tmpl))
980 tree class_specialization;
981 VEC(tree,gc) *methods;
985 /* The template arguments actually apply to the containing
986 class. Find the class specialization with those
988 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
990 = retrieve_specialization (class_template, args, 0);
991 if (!class_specialization)
993 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
994 for the specialization. */
995 idx = class_method_index_for_fn (class_specialization, tmpl);
998 /* Iterate through the methods with the indicated name, looking
999 for the one that has an instance of TMPL. */
1000 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1001 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
1003 tree fn = OVL_CURRENT (fns);
1004 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1005 /* using-declarations can add base methods to the method vec,
1006 and we don't want those here. */
1007 && DECL_CONTEXT (fn) == class_specialization)
1016 htab_t specializations;
1020 elt.spec = NULL_TREE;
1022 if (DECL_CLASS_TEMPLATE_P (tmpl))
1023 specializations = type_specializations;
1025 specializations = decl_specializations;
1028 hash = hash_specialization (&elt);
1029 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1037 /* Like retrieve_specialization, but for local declarations. */
1040 retrieve_local_specialization (tree tmpl)
1044 if (local_specializations == NULL)
1047 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
1048 htab_hash_pointer (tmpl));
1049 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
1052 /* Returns nonzero iff DECL is a specialization of TMPL. */
1055 is_specialization_of (tree decl, tree tmpl)
1059 if (TREE_CODE (decl) == FUNCTION_DECL)
1063 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1069 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1071 for (t = TREE_TYPE (decl);
1073 t = CLASSTYPE_USE_TEMPLATE (t)
1074 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1075 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1082 /* Returns nonzero iff DECL is a specialization of friend declaration
1083 FRIEND_DECL according to [temp.friend]. */
1086 is_specialization_of_friend (tree decl, tree friend_decl)
1088 bool need_template = true;
1091 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1092 || TREE_CODE (decl) == TYPE_DECL);
1094 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1095 of a template class, we want to check if DECL is a specialization
1097 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1098 && DECL_TEMPLATE_INFO (friend_decl)
1099 && !DECL_USE_TEMPLATE (friend_decl))
1101 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1102 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1103 need_template = false;
1105 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1106 && !PRIMARY_TEMPLATE_P (friend_decl))
1107 need_template = false;
1109 /* There is nothing to do if this is not a template friend. */
1110 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1113 if (is_specialization_of (decl, friend_decl))
1117 A member of a class template may be declared to be a friend of a
1118 non-template class. In this case, the corresponding member of
1119 every specialization of the class template is a friend of the
1120 class granting friendship.
1122 For example, given a template friend declaration
1124 template <class T> friend void A<T>::f();
1126 the member function below is considered a friend
1128 template <> struct A<int> {
1132 For this type of template friend, TEMPLATE_DEPTH below will be
1133 nonzero. To determine if DECL is a friend of FRIEND, we first
1134 check if the enclosing class is a specialization of another. */
1136 template_depth = template_class_depth (DECL_CONTEXT (friend_decl));
1138 && DECL_CLASS_SCOPE_P (decl)
1139 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1140 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1142 /* Next, we check the members themselves. In order to handle
1143 a few tricky cases, such as when FRIEND_DECL's are
1145 template <class T> friend void A<T>::g(T t);
1146 template <class T> template <T t> friend void A<T>::h();
1150 void A<int>::g(int);
1151 template <int> void A<int>::h();
1153 we need to figure out ARGS, the template arguments from
1154 the context of DECL. This is required for template substitution
1155 of `T' in the function parameter of `g' and template parameter
1156 of `h' in the above examples. Here ARGS corresponds to `int'. */
1158 tree context = DECL_CONTEXT (decl);
1159 tree args = NULL_TREE;
1160 int current_depth = 0;
1162 while (current_depth < template_depth)
1164 if (CLASSTYPE_TEMPLATE_INFO (context))
1166 if (current_depth == 0)
1167 args = TYPE_TI_ARGS (context);
1169 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1172 context = TYPE_CONTEXT (context);
1175 if (TREE_CODE (decl) == FUNCTION_DECL)
1180 tree friend_args_type;
1181 tree decl_args_type;
1183 /* Make sure that both DECL and FRIEND_DECL are templates or
1185 is_template = DECL_TEMPLATE_INFO (decl)
1186 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1187 if (need_template ^ is_template)
1189 else if (is_template)
1191 /* If both are templates, check template parameter list. */
1193 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1195 if (!comp_template_parms
1196 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1200 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1203 decl_type = TREE_TYPE (decl);
1205 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1206 tf_none, NULL_TREE);
1207 if (friend_type == error_mark_node)
1210 /* Check if return types match. */
1211 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1214 /* Check if function parameter types match, ignoring the
1215 `this' parameter. */
1216 friend_args_type = TYPE_ARG_TYPES (friend_type);
1217 decl_args_type = TYPE_ARG_TYPES (decl_type);
1218 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1219 friend_args_type = TREE_CHAIN (friend_args_type);
1220 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1221 decl_args_type = TREE_CHAIN (decl_args_type);
1223 return compparms (decl_args_type, friend_args_type);
1227 /* DECL is a TYPE_DECL */
1229 tree decl_type = TREE_TYPE (decl);
1231 /* Make sure that both DECL and FRIEND_DECL are templates or
1234 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1235 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1237 if (need_template ^ is_template)
1239 else if (is_template)
1242 /* If both are templates, check the name of the two
1243 TEMPLATE_DECL's first because is_friend didn't. */
1244 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1245 != DECL_NAME (friend_decl))
1248 /* Now check template parameter list. */
1250 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1252 return comp_template_parms
1253 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1257 return (DECL_NAME (decl)
1258 == DECL_NAME (friend_decl));
1264 /* Register the specialization SPEC as a specialization of TMPL with
1265 the indicated ARGS. IS_FRIEND indicates whether the specialization
1266 is actually just a friend declaration. Returns SPEC, or an
1267 equivalent prior declaration, if available. */
1270 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1274 spec_entry **slot = NULL;
1277 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1279 if (TREE_CODE (spec) == FUNCTION_DECL
1280 && uses_template_parms (DECL_TI_ARGS (spec)))
1281 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1282 register it; we want the corresponding TEMPLATE_DECL instead.
1283 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1284 the more obvious `uses_template_parms (spec)' to avoid problems
1285 with default function arguments. In particular, given
1286 something like this:
1288 template <class T> void f(T t1, T t = T())
1290 the default argument expression is not substituted for in an
1291 instantiation unless and until it is actually needed. */
1294 if (optimize_specialization_lookup_p (tmpl))
1295 /* We don't put these specializations in the hash table, but we might
1296 want to give an error about a mismatch. */
1297 fn = retrieve_specialization (tmpl, args, 0);
1305 hash = hash_specialization (&elt);
1307 slot = (spec_entry **)
1308 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1315 /* We can sometimes try to re-register a specialization that we've
1316 already got. In particular, regenerate_decl_from_template calls
1317 duplicate_decls which will update the specialization list. But,
1318 we'll still get called again here anyhow. It's more convenient
1319 to simply allow this than to try to prevent it. */
1322 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1324 if (DECL_TEMPLATE_INSTANTIATION (fn))
1326 if (DECL_ODR_USED (fn)
1327 || DECL_EXPLICIT_INSTANTIATION (fn))
1329 error ("specialization of %qD after instantiation",
1331 return error_mark_node;
1336 /* This situation should occur only if the first
1337 specialization is an implicit instantiation, the
1338 second is an explicit specialization, and the
1339 implicit instantiation has not yet been used. That
1340 situation can occur if we have implicitly
1341 instantiated a member function and then specialized
1344 We can also wind up here if a friend declaration that
1345 looked like an instantiation turns out to be a
1348 template <class T> void foo(T);
1349 class S { friend void foo<>(int) };
1350 template <> void foo(int);
1352 We transform the existing DECL in place so that any
1353 pointers to it become pointers to the updated
1356 If there was a definition for the template, but not
1357 for the specialization, we want this to look as if
1358 there were no definition, and vice versa. */
1359 DECL_INITIAL (fn) = NULL_TREE;
1360 duplicate_decls (spec, fn, is_friend);
1361 /* The call to duplicate_decls will have applied
1364 An explicit specialization of a function template
1365 is inline only if it is explicitly declared to be,
1366 and independently of whether its function template
1369 to the primary function; now copy the inline bits to
1370 the various clones. */
1371 FOR_EACH_CLONE (clone, fn)
1373 DECL_DECLARED_INLINE_P (clone)
1374 = DECL_DECLARED_INLINE_P (fn);
1375 DECL_SOURCE_LOCATION (clone)
1376 = DECL_SOURCE_LOCATION (fn);
1378 check_specialization_namespace (fn);
1383 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1385 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1386 /* Dup decl failed, but this is a new definition. Set the
1387 line number so any errors match this new
1389 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1395 return duplicate_decls (spec, fn, is_friend);
1397 /* A specialization must be declared in the same namespace as the
1398 template it is specializing. */
1399 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1400 && !check_specialization_namespace (tmpl))
1401 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1403 if (!optimize_specialization_lookup_p (tmpl))
1405 gcc_assert (tmpl && args && spec);
1406 *slot = GGC_NEW (spec_entry);
1408 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1409 && PRIMARY_TEMPLATE_P (tmpl)
1410 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1411 /* TMPL is a forward declaration of a template function; keep a list
1412 of all specializations in case we need to reassign them to a friend
1413 template later in tsubst_friend_function. */
1414 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1415 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1421 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1422 TMPL and ARGS members, ignores SPEC. */
1425 eq_specializations (const void *p1, const void *p2)
1427 const spec_entry *e1 = (const spec_entry *)p1;
1428 const spec_entry *e2 = (const spec_entry *)p2;
1430 return (e1->tmpl == e2->tmpl
1431 && comp_template_args (e1->args, e2->args));
1434 /* Returns a hash for a template TMPL and template arguments ARGS. */
1437 hash_tmpl_and_args (tree tmpl, tree args)
1439 hashval_t val = DECL_UID (tmpl);
1440 return iterative_hash_template_arg (args, val);
1443 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1447 hash_specialization (const void *p)
1449 const spec_entry *e = (const spec_entry *)p;
1450 return hash_tmpl_and_args (e->tmpl, e->args);
1453 /* Recursively calculate a hash value for a template argument ARG, for use
1454 in the hash tables of template specializations. */
1457 iterative_hash_template_arg (tree arg, hashval_t val)
1459 unsigned HOST_WIDE_INT i;
1460 enum tree_code code;
1463 if (arg == NULL_TREE)
1464 return iterative_hash_object (arg, val);
1469 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1470 /* We can get one of these when re-hashing a previous entry in the middle
1471 of substituting into a pack expansion. Just look through it. */
1472 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1474 code = TREE_CODE (arg);
1475 tclass = TREE_CODE_CLASS (code);
1477 val = iterative_hash_object (code, val);
1484 case IDENTIFIER_NODE:
1485 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1489 int i, len = TREE_VEC_LENGTH (arg);
1490 for (i = 0; i < len; ++i)
1491 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1495 case TYPE_PACK_EXPANSION:
1496 case EXPR_PACK_EXPANSION:
1497 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1499 case TYPE_ARGUMENT_PACK:
1500 case NONTYPE_ARGUMENT_PACK:
1501 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1504 for (; arg; arg = TREE_CHAIN (arg))
1505 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1509 for (; arg; arg = OVL_CHAIN (arg))
1510 val = iterative_hash_template_arg (OVL_FUNCTION (arg), val);
1516 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1518 val = iterative_hash_template_arg (field, val);
1519 val = iterative_hash_template_arg (value, val);
1525 if (!DECL_ARTIFICIAL (arg))
1526 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1527 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1530 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1533 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1534 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1536 case TEMPLATE_PARM_INDEX:
1537 val = iterative_hash_template_arg
1538 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1539 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1540 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1543 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1544 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1545 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1548 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1550 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1554 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1555 code = TREE_CODE (TREE_OPERAND (arg, 1));
1556 val = iterative_hash_object (code, val);
1557 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1560 /* layout_type sets structural equality for arrays of
1561 incomplete type, so we can't rely on the canonical type
1563 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1564 return iterative_hash_template_arg (TYPE_DOMAIN (arg), val);
1570 if (TYPE_CANONICAL (arg))
1571 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1573 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1574 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1575 /* Otherwise just compare the types during lookup. */
1578 case tcc_declaration:
1580 return iterative_hash_expr (arg, val);
1583 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1585 unsigned n = TREE_OPERAND_LENGTH (arg);
1586 for (i = 0; i < n; ++i)
1587 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1596 /* Unregister the specialization SPEC as a specialization of TMPL.
1597 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1598 if the SPEC was listed as a specialization of TMPL.
1600 Note that SPEC has been ggc_freed, so we can't look inside it. */
1603 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1608 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1609 elt.args = TI_ARGS (tinfo);
1610 elt.spec = NULL_TREE;
1612 slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
1615 gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
1616 gcc_assert (new_spec != NULL_TREE);
1617 (*slot)->spec = new_spec;
1624 /* Compare an entry in the local specializations hash table P1 (which
1625 is really a pointer to a TREE_LIST) with P2 (which is really a
1629 eq_local_specializations (const void *p1, const void *p2)
1631 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1634 /* Hash P1, an entry in the local specializations table. */
1637 hash_local_specialization (const void* p1)
1639 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1642 /* Like register_specialization, but for local declarations. We are
1643 registering SPEC, an instantiation of TMPL. */
1646 register_local_specialization (tree spec, tree tmpl)
1650 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1651 htab_hash_pointer (tmpl), INSERT);
1652 *slot = build_tree_list (spec, tmpl);
1655 /* TYPE is a class type. Returns true if TYPE is an explicitly
1656 specialized class. */
1659 explicit_class_specialization_p (tree type)
1661 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1663 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1666 /* Print the list of functions at FNS, going through all the overloads
1667 for each element of the list. Alternatively, FNS can not be a
1668 TREE_LIST, in which case it will be printed together with all the
1671 MORE and *STR should respectively be FALSE and NULL when the function
1672 is called from the outside. They are used internally on recursive
1673 calls. print_candidates manages the two parameters and leaves NULL
1674 in *STR when it ends. */
1677 print_candidates_1 (tree fns, bool more, const char **str)
1680 char *spaces = NULL;
1682 for (fn = fns; fn; fn = OVL_NEXT (fn))
1683 if (TREE_CODE (fn) == TREE_LIST)
1685 gcc_assert (!OVL_NEXT (fn) && !is_overloaded_fn (fn));
1686 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1687 print_candidates_1 (TREE_VALUE (fn2),
1688 TREE_CHAIN (fn2) || more, str);
1694 /* Pick the prefix string. */
1695 if (!more && !OVL_NEXT (fns))
1697 error ("candidate is: %+#D", OVL_CURRENT (fn));
1701 *str = _("candidates are:");
1702 spaces = get_spaces (*str);
1704 error ("%s %+#D", *str, OVL_CURRENT (fn));
1705 *str = spaces ? spaces : *str;
1715 /* Print the list of candidate FNS in an error message. */
1718 print_candidates (tree fns)
1720 const char *str = NULL;
1721 print_candidates_1 (fns, false, &str);
1722 gcc_assert (str == NULL);
1725 /* Returns the template (one of the functions given by TEMPLATE_ID)
1726 which can be specialized to match the indicated DECL with the
1727 explicit template args given in TEMPLATE_ID. The DECL may be
1728 NULL_TREE if none is available. In that case, the functions in
1729 TEMPLATE_ID are non-members.
1731 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1732 specialization of a member template.
1734 The TEMPLATE_COUNT is the number of references to qualifying
1735 template classes that appeared in the name of the function. See
1736 check_explicit_specialization for a more accurate description.
1738 TSK indicates what kind of template declaration (if any) is being
1739 declared. TSK_TEMPLATE indicates that the declaration given by
1740 DECL, though a FUNCTION_DECL, has template parameters, and is
1741 therefore a template function.
1743 The template args (those explicitly specified and those deduced)
1744 are output in a newly created vector *TARGS_OUT.
1746 If it is impossible to determine the result, an error message is
1747 issued. The error_mark_node is returned to indicate failure. */
1750 determine_specialization (tree template_id,
1753 int need_member_template,
1759 tree explicit_targs;
1760 tree candidates = NULL_TREE;
1761 /* A TREE_LIST of templates of which DECL may be a specialization.
1762 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1763 corresponding TREE_PURPOSE is the set of template arguments that,
1764 when used to instantiate the template, would produce a function
1765 with the signature of DECL. */
1766 tree templates = NULL_TREE;
1768 struct cp_binding_level *b;
1770 *targs_out = NULL_TREE;
1772 if (template_id == error_mark_node || decl == error_mark_node)
1773 return error_mark_node;
1775 fns = TREE_OPERAND (template_id, 0);
1776 explicit_targs = TREE_OPERAND (template_id, 1);
1778 if (fns == error_mark_node)
1779 return error_mark_node;
1781 /* Check for baselinks. */
1782 if (BASELINK_P (fns))
1783 fns = BASELINK_FUNCTIONS (fns);
1785 if (!is_overloaded_fn (fns))
1787 error ("%qD is not a function template", fns);
1788 return error_mark_node;
1791 /* Count the number of template headers specified for this
1794 for (b = current_binding_level;
1795 b->kind == sk_template_parms;
1799 for (; fns; fns = OVL_NEXT (fns))
1801 tree fn = OVL_CURRENT (fns);
1803 if (TREE_CODE (fn) == TEMPLATE_DECL)
1805 tree decl_arg_types;
1808 /* In case of explicit specialization, we need to check if
1809 the number of template headers appearing in the specialization
1810 is correct. This is usually done in check_explicit_specialization,
1811 but the check done there cannot be exhaustive when specializing
1812 member functions. Consider the following code:
1814 template <> void A<int>::f(int);
1815 template <> template <> void A<int>::f(int);
1817 Assuming that A<int> is not itself an explicit specialization
1818 already, the first line specializes "f" which is a non-template
1819 member function, whilst the second line specializes "f" which
1820 is a template member function. So both lines are syntactically
1821 correct, and check_explicit_specialization does not reject
1824 Here, we can do better, as we are matching the specialization
1825 against the declarations. We count the number of template
1826 headers, and we check if they match TEMPLATE_COUNT + 1
1827 (TEMPLATE_COUNT is the number of qualifying template classes,
1828 plus there must be another header for the member template
1831 Notice that if header_count is zero, this is not a
1832 specialization but rather a template instantiation, so there
1833 is no check we can perform here. */
1834 if (header_count && header_count != template_count + 1)
1837 /* Check that the number of template arguments at the
1838 innermost level for DECL is the same as for FN. */
1839 if (current_binding_level->kind == sk_template_parms
1840 && !current_binding_level->explicit_spec_p
1841 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1842 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1843 (current_template_parms))))
1846 /* DECL might be a specialization of FN. */
1847 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1848 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1850 /* For a non-static member function, we need to make sure
1851 that the const qualification is the same. Since
1852 get_bindings does not try to merge the "this" parameter,
1853 we must do the comparison explicitly. */
1854 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1855 && !same_type_p (TREE_VALUE (fn_arg_types),
1856 TREE_VALUE (decl_arg_types)))
1859 /* Skip the "this" parameter and, for constructors of
1860 classes with virtual bases, the VTT parameter. A
1861 full specialization of a constructor will have a VTT
1862 parameter, but a template never will. */
1864 = skip_artificial_parms_for (decl, decl_arg_types);
1866 = skip_artificial_parms_for (fn, fn_arg_types);
1868 /* Check that the number of function parameters matches.
1870 template <class T> void f(int i = 0);
1871 template <> void f<int>();
1872 The specialization f<int> is invalid but is not caught
1873 by get_bindings below. */
1874 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1877 /* Function templates cannot be specializations; there are
1878 no partial specializations of functions. Therefore, if
1879 the type of DECL does not match FN, there is no
1881 if (tsk == tsk_template)
1883 if (compparms (fn_arg_types, decl_arg_types))
1884 candidates = tree_cons (NULL_TREE, fn, candidates);
1888 /* See whether this function might be a specialization of this
1890 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1893 /* We cannot deduce template arguments that when used to
1894 specialize TMPL will produce DECL. */
1897 /* Save this template, and the arguments deduced. */
1898 templates = tree_cons (targs, fn, templates);
1900 else if (need_member_template)
1901 /* FN is an ordinary member function, and we need a
1902 specialization of a member template. */
1904 else if (TREE_CODE (fn) != FUNCTION_DECL)
1905 /* We can get IDENTIFIER_NODEs here in certain erroneous
1908 else if (!DECL_FUNCTION_MEMBER_P (fn))
1909 /* This is just an ordinary non-member function. Nothing can
1910 be a specialization of that. */
1912 else if (DECL_ARTIFICIAL (fn))
1913 /* Cannot specialize functions that are created implicitly. */
1917 tree decl_arg_types;
1919 /* This is an ordinary member function. However, since
1920 we're here, we can assume it's enclosing class is a
1921 template class. For example,
1923 template <typename T> struct S { void f(); };
1924 template <> void S<int>::f() {}
1926 Here, S<int>::f is a non-template, but S<int> is a
1927 template class. If FN has the same type as DECL, we
1928 might be in business. */
1930 if (!DECL_TEMPLATE_INFO (fn))
1931 /* Its enclosing class is an explicit specialization
1932 of a template class. This is not a candidate. */
1935 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1936 TREE_TYPE (TREE_TYPE (fn))))
1937 /* The return types differ. */
1940 /* Adjust the type of DECL in case FN is a static member. */
1941 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1942 if (DECL_STATIC_FUNCTION_P (fn)
1943 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1944 decl_arg_types = TREE_CHAIN (decl_arg_types);
1946 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1949 candidates = tree_cons (NULL_TREE, fn, candidates);
1953 if (templates && TREE_CHAIN (templates))
1959 It is possible for a specialization with a given function
1960 signature to be instantiated from more than one function
1961 template. In such cases, explicit specification of the
1962 template arguments must be used to uniquely identify the
1963 function template specialization being specialized.
1965 Note that here, there's no suggestion that we're supposed to
1966 determine which of the candidate templates is most
1967 specialized. However, we, also have:
1971 Partial ordering of overloaded function template
1972 declarations is used in the following contexts to select
1973 the function template to which a function template
1974 specialization refers:
1976 -- when an explicit specialization refers to a function
1979 So, we do use the partial ordering rules, at least for now.
1980 This extension can only serve to make invalid programs valid,
1981 so it's safe. And, there is strong anecdotal evidence that
1982 the committee intended the partial ordering rules to apply;
1983 the EDG front end has that behavior, and John Spicer claims
1984 that the committee simply forgot to delete the wording in
1985 [temp.expl.spec]. */
1986 tree tmpl = most_specialized_instantiation (templates);
1987 if (tmpl != error_mark_node)
1990 TREE_CHAIN (templates) = NULL_TREE;
1994 if (templates == NULL_TREE && candidates == NULL_TREE)
1996 error ("template-id %qD for %q+D does not match any template "
1997 "declaration", template_id, decl);
1998 if (header_count && header_count != template_count + 1)
1999 inform (input_location, "saw %d %<template<>%>, need %d for "
2000 "specializing a member function template",
2001 header_count, template_count + 1);
2002 return error_mark_node;
2004 else if ((templates && TREE_CHAIN (templates))
2005 || (candidates && TREE_CHAIN (candidates))
2006 || (templates && candidates))
2008 error ("ambiguous template specialization %qD for %q+D",
2010 candidates = chainon (candidates, templates);
2011 print_candidates (candidates);
2012 return error_mark_node;
2015 /* We have one, and exactly one, match. */
2018 tree fn = TREE_VALUE (candidates);
2019 *targs_out = copy_node (DECL_TI_ARGS (fn));
2020 /* DECL is a re-declaration or partial instantiation of a template
2022 if (TREE_CODE (fn) == TEMPLATE_DECL)
2024 /* It was a specialization of an ordinary member function in a
2026 return DECL_TI_TEMPLATE (fn);
2029 /* It was a specialization of a template. */
2030 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2031 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2033 *targs_out = copy_node (targs);
2034 SET_TMPL_ARGS_LEVEL (*targs_out,
2035 TMPL_ARGS_DEPTH (*targs_out),
2036 TREE_PURPOSE (templates));
2039 *targs_out = TREE_PURPOSE (templates);
2040 return TREE_VALUE (templates);
2043 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2044 but with the default argument values filled in from those in the
2048 copy_default_args_to_explicit_spec_1 (tree spec_types,
2051 tree new_spec_types;
2056 if (spec_types == void_list_node)
2057 return void_list_node;
2059 /* Substitute into the rest of the list. */
2061 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2062 TREE_CHAIN (tmpl_types));
2064 /* Add the default argument for this parameter. */
2065 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2066 TREE_VALUE (spec_types),
2070 /* DECL is an explicit specialization. Replicate default arguments
2071 from the template it specializes. (That way, code like:
2073 template <class T> void f(T = 3);
2074 template <> void f(double);
2077 works, as required.) An alternative approach would be to look up
2078 the correct default arguments at the call-site, but this approach
2079 is consistent with how implicit instantiations are handled. */
2082 copy_default_args_to_explicit_spec (tree decl)
2087 tree new_spec_types;
2091 tree object_type = NULL_TREE;
2092 tree in_charge = NULL_TREE;
2093 tree vtt = NULL_TREE;
2095 /* See if there's anything we need to do. */
2096 tmpl = DECL_TI_TEMPLATE (decl);
2097 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2098 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2099 if (TREE_PURPOSE (t))
2104 old_type = TREE_TYPE (decl);
2105 spec_types = TYPE_ARG_TYPES (old_type);
2107 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2109 /* Remove the this pointer, but remember the object's type for
2111 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2112 spec_types = TREE_CHAIN (spec_types);
2113 tmpl_types = TREE_CHAIN (tmpl_types);
2115 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2117 /* DECL may contain more parameters than TMPL due to the extra
2118 in-charge parameter in constructors and destructors. */
2119 in_charge = spec_types;
2120 spec_types = TREE_CHAIN (spec_types);
2122 if (DECL_HAS_VTT_PARM_P (decl))
2125 spec_types = TREE_CHAIN (spec_types);
2129 /* Compute the merged default arguments. */
2131 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2133 /* Compute the new FUNCTION_TYPE. */
2137 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2142 /* Put the in-charge parameter back. */
2143 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2144 TREE_VALUE (in_charge),
2147 new_type = build_method_type_directly (object_type,
2148 TREE_TYPE (old_type),
2152 new_type = build_function_type (TREE_TYPE (old_type),
2154 new_type = cp_build_type_attribute_variant (new_type,
2155 TYPE_ATTRIBUTES (old_type));
2156 new_type = build_exception_variant (new_type,
2157 TYPE_RAISES_EXCEPTIONS (old_type));
2158 TREE_TYPE (decl) = new_type;
2161 /* Check to see if the function just declared, as indicated in
2162 DECLARATOR, and in DECL, is a specialization of a function
2163 template. We may also discover that the declaration is an explicit
2164 instantiation at this point.
2166 Returns DECL, or an equivalent declaration that should be used
2167 instead if all goes well. Issues an error message if something is
2168 amiss. Returns error_mark_node if the error is not easily
2171 FLAGS is a bitmask consisting of the following flags:
2173 2: The function has a definition.
2174 4: The function is a friend.
2176 The TEMPLATE_COUNT is the number of references to qualifying
2177 template classes that appeared in the name of the function. For
2180 template <class T> struct S { void f(); };
2183 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2184 classes are not counted in the TEMPLATE_COUNT, so that in
2186 template <class T> struct S {};
2187 template <> struct S<int> { void f(); }
2188 template <> void S<int>::f();
2190 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2191 invalid; there should be no template <>.)
2193 If the function is a specialization, it is marked as such via
2194 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2195 is set up correctly, and it is added to the list of specializations
2196 for that template. */
2199 check_explicit_specialization (tree declarator,
2204 int have_def = flags & 2;
2205 int is_friend = flags & 4;
2206 int specialization = 0;
2207 int explicit_instantiation = 0;
2208 int member_specialization = 0;
2209 tree ctype = DECL_CLASS_CONTEXT (decl);
2210 tree dname = DECL_NAME (decl);
2215 if (!processing_specialization)
2218 tsk = tsk_excessive_parms;
2221 tsk = current_tmpl_spec_kind (template_count);
2226 if (processing_specialization)
2229 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2231 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2234 /* This could be something like:
2236 template <class T> void f(T);
2237 class S { friend void f<>(int); } */
2241 /* This case handles bogus declarations like template <>
2242 template <class T> void f<int>(); */
2244 error ("template-id %qD in declaration of primary template",
2251 case tsk_invalid_member_spec:
2252 /* The error has already been reported in
2253 check_specialization_scope. */
2254 return error_mark_node;
2256 case tsk_invalid_expl_inst:
2257 error ("template parameter list used in explicit instantiation");
2263 error ("definition provided for explicit instantiation");
2265 explicit_instantiation = 1;
2268 case tsk_excessive_parms:
2269 case tsk_insufficient_parms:
2270 if (tsk == tsk_excessive_parms)
2271 error ("too many template parameter lists in declaration of %qD",
2273 else if (template_header_count)
2274 error("too few template parameter lists in declaration of %qD", decl);
2276 error("explicit specialization of %qD must be introduced by "
2277 "%<template <>%>", decl);
2281 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2283 member_specialization = 1;
2289 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2291 /* This case handles bogus declarations like template <>
2292 template <class T> void f<int>(); */
2294 if (uses_template_parms (declarator))
2295 error ("function template partial specialization %qD "
2296 "is not allowed", declarator);
2298 error ("template-id %qD in declaration of primary template",
2303 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2304 /* This is a specialization of a member template, without
2305 specialization the containing class. Something like:
2307 template <class T> struct S {
2308 template <class U> void f (U);
2310 template <> template <class U> void S<int>::f(U) {}
2312 That's a specialization -- but of the entire template. */
2320 if (specialization || member_specialization)
2322 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2323 for (; t; t = TREE_CHAIN (t))
2324 if (TREE_PURPOSE (t))
2326 permerror (input_location,
2327 "default argument specified in explicit specialization");
2332 if (specialization || member_specialization || explicit_instantiation)
2334 tree tmpl = NULL_TREE;
2335 tree targs = NULL_TREE;
2337 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2338 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2342 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2347 /* If there is no class context, the explicit instantiation
2348 must be at namespace scope. */
2349 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2351 /* Find the namespace binding, using the declaration
2353 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2355 if (fns == error_mark_node || !is_overloaded_fn (fns))
2357 error ("%qD is not a template function", dname);
2358 fns = error_mark_node;
2362 tree fn = OVL_CURRENT (fns);
2363 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2364 CP_DECL_CONTEXT (fn)))
2365 error ("%qD is not declared in %qD",
2366 decl, current_namespace);
2370 declarator = lookup_template_function (fns, NULL_TREE);
2373 if (declarator == error_mark_node)
2374 return error_mark_node;
2376 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2378 if (!explicit_instantiation)
2379 /* A specialization in class scope. This is invalid,
2380 but the error will already have been flagged by
2381 check_specialization_scope. */
2382 return error_mark_node;
2385 /* It's not valid to write an explicit instantiation in
2388 class C { template void f(); }
2390 This case is caught by the parser. However, on
2393 template class C { void f(); };
2395 (which is invalid) we can get here. The error will be
2402 else if (ctype != NULL_TREE
2403 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2406 /* Find the list of functions in ctype that have the same
2407 name as the declared function. */
2408 tree name = TREE_OPERAND (declarator, 0);
2409 tree fns = NULL_TREE;
2412 if (constructor_name_p (name, ctype))
2414 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2416 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2417 : !CLASSTYPE_DESTRUCTORS (ctype))
2419 /* From [temp.expl.spec]:
2421 If such an explicit specialization for the member
2422 of a class template names an implicitly-declared
2423 special member function (clause _special_), the
2424 program is ill-formed.
2426 Similar language is found in [temp.explicit]. */
2427 error ("specialization of implicitly-declared special member function");
2428 return error_mark_node;
2431 name = is_constructor ? ctor_identifier : dtor_identifier;
2434 if (!DECL_CONV_FN_P (decl))
2436 idx = lookup_fnfields_1 (ctype, name);
2438 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2442 VEC(tree,gc) *methods;
2445 /* For a type-conversion operator, we cannot do a
2446 name-based lookup. We might be looking for `operator
2447 int' which will be a specialization of `operator T'.
2448 So, we find *all* the conversion operators, and then
2449 select from them. */
2452 methods = CLASSTYPE_METHOD_VEC (ctype);
2454 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2455 VEC_iterate (tree, methods, idx, ovl);
2458 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2459 /* There are no more conversion functions. */
2462 /* Glue all these conversion functions together
2463 with those we already have. */
2464 for (; ovl; ovl = OVL_NEXT (ovl))
2465 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2469 if (fns == NULL_TREE)
2471 error ("no member function %qD declared in %qT", name, ctype);
2472 return error_mark_node;
2475 TREE_OPERAND (declarator, 0) = fns;
2478 /* Figure out what exactly is being specialized at this point.
2479 Note that for an explicit instantiation, even one for a
2480 member function, we cannot tell apriori whether the
2481 instantiation is for a member template, or just a member
2482 function of a template class. Even if a member template is
2483 being instantiated, the member template arguments may be
2484 elided if they can be deduced from the rest of the
2486 tmpl = determine_specialization (declarator, decl,
2488 member_specialization,
2492 if (!tmpl || tmpl == error_mark_node)
2493 /* We couldn't figure out what this declaration was
2495 return error_mark_node;
2498 tree gen_tmpl = most_general_template (tmpl);
2500 if (explicit_instantiation)
2502 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2503 is done by do_decl_instantiation later. */
2505 int arg_depth = TMPL_ARGS_DEPTH (targs);
2506 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2508 if (arg_depth > parm_depth)
2510 /* If TMPL is not the most general template (for
2511 example, if TMPL is a friend template that is
2512 injected into namespace scope), then there will
2513 be too many levels of TARGS. Remove some of them
2518 new_targs = make_tree_vec (parm_depth);
2519 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2520 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2521 = TREE_VEC_ELT (targs, i);
2525 return instantiate_template (tmpl, targs, tf_error);
2528 /* If we thought that the DECL was a member function, but it
2529 turns out to be specializing a static member function,
2530 make DECL a static member function as well. */
2531 if (DECL_STATIC_FUNCTION_P (tmpl)
2532 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2533 revert_static_member_fn (decl);
2535 /* If this is a specialization of a member template of a
2536 template class, we want to return the TEMPLATE_DECL, not
2537 the specialization of it. */
2538 if (tsk == tsk_template)
2540 tree result = DECL_TEMPLATE_RESULT (tmpl);
2541 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2542 DECL_INITIAL (result) = NULL_TREE;
2546 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2547 DECL_SOURCE_LOCATION (result)
2548 = DECL_SOURCE_LOCATION (decl);
2549 /* We want to use the argument list specified in the
2550 definition, not in the original declaration. */
2551 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2552 for (parm = DECL_ARGUMENTS (result); parm;
2553 parm = TREE_CHAIN (parm))
2554 DECL_CONTEXT (parm) = result;
2556 return register_specialization (tmpl, gen_tmpl, targs,
2560 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2561 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2563 /* Inherit default function arguments from the template
2564 DECL is specializing. */
2565 copy_default_args_to_explicit_spec (decl);
2567 /* This specialization has the same protection as the
2568 template it specializes. */
2569 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2570 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2572 /* 7.1.1-1 [dcl.stc]
2574 A storage-class-specifier shall not be specified in an
2575 explicit specialization...
2577 The parser rejects these, so unless action is taken here,
2578 explicit function specializations will always appear with
2581 The action recommended by the C++ CWG in response to C++
2582 defect report 605 is to make the storage class and linkage
2583 of the explicit specialization match the templated function:
2585 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2587 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2589 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2590 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2592 /* This specialization has the same linkage and visibility as
2593 the function template it specializes. */
2594 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2595 if (! TREE_PUBLIC (decl))
2597 DECL_INTERFACE_KNOWN (decl) = 1;
2598 DECL_NOT_REALLY_EXTERN (decl) = 1;
2600 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2601 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2603 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2604 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2608 /* If DECL is a friend declaration, declared using an
2609 unqualified name, the namespace associated with DECL may
2610 have been set incorrectly. For example, in:
2612 template <typename T> void f(T);
2614 struct S { friend void f<int>(int); }
2617 we will have set the DECL_CONTEXT for the friend
2618 declaration to N, rather than to the global namespace. */
2619 if (DECL_NAMESPACE_SCOPE_P (decl))
2620 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2622 if (is_friend && !have_def)
2623 /* This is not really a declaration of a specialization.
2624 It's just the name of an instantiation. But, it's not
2625 a request for an instantiation, either. */
2626 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2627 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2628 /* This is indeed a specialization. In case of constructors
2629 and destructors, we need in-charge and not-in-charge
2630 versions in V3 ABI. */
2631 clone_function_decl (decl, /*update_method_vec_p=*/0);
2633 /* Register this specialization so that we can find it
2635 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2642 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2643 parameters. These are represented in the same format used for
2644 DECL_TEMPLATE_PARMS. */
2647 comp_template_parms (const_tree parms1, const_tree parms2)
2652 if (parms1 == parms2)
2655 for (p1 = parms1, p2 = parms2;
2656 p1 != NULL_TREE && p2 != NULL_TREE;
2657 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2659 tree t1 = TREE_VALUE (p1);
2660 tree t2 = TREE_VALUE (p2);
2663 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2664 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2666 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2669 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2671 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2672 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2674 /* If either of the template parameters are invalid, assume
2675 they match for the sake of error recovery. */
2676 if (parm1 == error_mark_node || parm2 == error_mark_node)
2679 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2682 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2683 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2684 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2686 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2691 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2692 /* One set of parameters has more parameters lists than the
2699 /* Determine whether PARM is a parameter pack. */
2702 template_parameter_pack_p (const_tree parm)
2704 /* Determine if we have a non-type template parameter pack. */
2705 if (TREE_CODE (parm) == PARM_DECL)
2706 return (DECL_TEMPLATE_PARM_P (parm)
2707 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2709 /* If this is a list of template parameters, we could get a
2710 TYPE_DECL or a TEMPLATE_DECL. */
2711 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2712 parm = TREE_TYPE (parm);
2714 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2715 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2716 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2719 /* Determine if T is a function parameter pack. */
2722 function_parameter_pack_p (const_tree t)
2724 if (t && TREE_CODE (t) == PARM_DECL)
2725 return FUNCTION_PARAMETER_PACK_P (t);
2729 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2730 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2733 get_function_template_decl (const_tree primary_func_tmpl_inst)
2735 if (! primary_func_tmpl_inst
2736 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2737 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2740 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2743 /* Return true iff the function parameter PARAM_DECL was expanded
2744 from the function parameter pack PACK. */
2747 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2749 if (DECL_ARTIFICIAL (param_decl)
2750 || !function_parameter_pack_p (pack))
2753 /* The parameter pack and its pack arguments have the same
2755 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2758 /* Determine whether ARGS describes a variadic template args list,
2759 i.e., one that is terminated by a template argument pack. */
2762 template_args_variadic_p (tree args)
2767 if (args == NULL_TREE)
2770 args = INNERMOST_TEMPLATE_ARGS (args);
2771 nargs = TREE_VEC_LENGTH (args);
2776 last_parm = TREE_VEC_ELT (args, nargs - 1);
2778 return ARGUMENT_PACK_P (last_parm);
2781 /* Generate a new name for the parameter pack name NAME (an
2782 IDENTIFIER_NODE) that incorporates its */
2785 make_ith_pack_parameter_name (tree name, int i)
2787 /* Munge the name to include the parameter index. */
2788 #define NUMBUF_LEN 128
2789 char numbuf[NUMBUF_LEN];
2793 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2794 newname_len = IDENTIFIER_LENGTH (name)
2795 + strlen (numbuf) + 2;
2796 newname = (char*)alloca (newname_len);
2797 snprintf (newname, newname_len,
2798 "%s#%i", IDENTIFIER_POINTER (name), i);
2799 return get_identifier (newname);
2802 /* Return true if T is a primary function
2803 or class template instantiation. */
2806 primary_template_instantiation_p (const_tree t)
2811 if (TREE_CODE (t) == FUNCTION_DECL)
2812 return DECL_LANG_SPECIFIC (t)
2813 && DECL_TEMPLATE_INSTANTIATION (t)
2814 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2815 else if (CLASS_TYPE_P (t))
2816 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2817 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2821 /* Return true if PARM is a template template parameter. */
2824 template_template_parameter_p (const_tree parm)
2826 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2829 /* Return the template parameters of T if T is a
2830 primary template instantiation, NULL otherwise. */
2833 get_primary_template_innermost_parameters (const_tree t)
2835 tree parms = NULL, template_info = NULL;
2837 if ((template_info = get_template_info (t))
2838 && primary_template_instantiation_p (t))
2839 parms = INNERMOST_TEMPLATE_PARMS
2840 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2845 /* Return the template parameters of the LEVELth level from the full list
2846 of template parameters PARMS. */
2849 get_template_parms_at_level (tree parms, int level)
2853 || TREE_CODE (parms) != TREE_LIST
2854 || level > TMPL_PARMS_DEPTH (parms))
2857 for (p = parms; p; p = TREE_CHAIN (p))
2858 if (TMPL_PARMS_DEPTH (p) == level)
2864 /* Returns the template arguments of T if T is a template instantiation,
2868 get_template_innermost_arguments (const_tree t)
2870 tree args = NULL, template_info = NULL;
2872 if ((template_info = get_template_info (t))
2873 && TI_ARGS (template_info))
2874 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2879 /* Return the argument pack elements of T if T is a template argument pack,
2883 get_template_argument_pack_elems (const_tree t)
2885 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2886 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2889 return ARGUMENT_PACK_ARGS (t);
2892 /* Structure used to track the progress of find_parameter_packs_r. */
2893 struct find_parameter_pack_data
2895 /* TREE_LIST that will contain all of the parameter packs found by
2897 tree* parameter_packs;
2899 /* Set of AST nodes that have been visited by the traversal. */
2900 struct pointer_set_t *visited;
2903 /* Identifies all of the argument packs that occur in a template
2904 argument and appends them to the TREE_LIST inside DATA, which is a
2905 find_parameter_pack_data structure. This is a subroutine of
2906 make_pack_expansion and uses_parameter_packs. */
2908 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2911 struct find_parameter_pack_data* ppd =
2912 (struct find_parameter_pack_data*)data;
2913 bool parameter_pack_p = false;
2915 /* Identify whether this is a parameter pack or not. */
2916 switch (TREE_CODE (t))
2918 case TEMPLATE_PARM_INDEX:
2919 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2920 parameter_pack_p = true;
2923 case TEMPLATE_TYPE_PARM:
2924 case TEMPLATE_TEMPLATE_PARM:
2925 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2926 parameter_pack_p = true;
2930 if (FUNCTION_PARAMETER_PACK_P (t))
2932 /* We don't want to walk into the type of a PARM_DECL,
2933 because we don't want to see the type parameter pack. */
2935 parameter_pack_p = true;
2940 /* Not a parameter pack. */
2944 if (parameter_pack_p)
2946 /* Add this parameter pack to the list. */
2947 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2951 cp_walk_tree (&TYPE_CONTEXT (t),
2952 &find_parameter_packs_r, ppd, ppd->visited);
2954 /* This switch statement will return immediately if we don't find a
2956 switch (TREE_CODE (t))
2958 case TEMPLATE_PARM_INDEX:
2961 case BOUND_TEMPLATE_TEMPLATE_PARM:
2962 /* Check the template itself. */
2963 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
2964 &find_parameter_packs_r, ppd, ppd->visited);
2965 /* Check the template arguments. */
2966 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2971 case TEMPLATE_TYPE_PARM:
2972 case TEMPLATE_TEMPLATE_PARM:
2979 if (TYPE_PTRMEMFUNC_P (t))
2985 if (TYPE_TEMPLATE_INFO (t))
2986 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
2987 &find_parameter_packs_r, ppd, ppd->visited);
2993 cp_walk_tree (&TREE_TYPE (t),
2994 &find_parameter_packs_r, ppd, ppd->visited);
2998 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3003 case TYPE_PACK_EXPANSION:
3004 case EXPR_PACK_EXPANSION:
3009 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3014 case IDENTIFIER_NODE:
3015 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3027 /* Determines if the expression or type T uses any parameter packs. */
3029 uses_parameter_packs (tree t)
3031 tree parameter_packs = NULL_TREE;
3032 struct find_parameter_pack_data ppd;
3033 ppd.parameter_packs = ¶meter_packs;
3034 ppd.visited = pointer_set_create ();
3035 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3036 pointer_set_destroy (ppd.visited);
3037 return parameter_packs != NULL_TREE;
3040 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3041 representation a base-class initializer into a parameter pack
3042 expansion. If all goes well, the resulting node will be an
3043 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3046 make_pack_expansion (tree arg)
3049 tree parameter_packs = NULL_TREE;
3050 bool for_types = false;
3051 struct find_parameter_pack_data ppd;
3053 if (!arg || arg == error_mark_node)
3056 if (TREE_CODE (arg) == TREE_LIST)
3058 /* The only time we will see a TREE_LIST here is for a base
3059 class initializer. In this case, the TREE_PURPOSE will be a
3060 _TYPE node (representing the base class expansion we're
3061 initializing) and the TREE_VALUE will be a TREE_LIST
3062 containing the initialization arguments.
3064 The resulting expansion looks somewhat different from most
3065 expansions. Rather than returning just one _EXPANSION, we
3066 return a TREE_LIST whose TREE_PURPOSE is a
3067 TYPE_PACK_EXPANSION containing the bases that will be
3068 initialized. The TREE_VALUE will be identical to the
3069 original TREE_VALUE, which is a list of arguments that will
3070 be passed to each base. We do not introduce any new pack
3071 expansion nodes into the TREE_VALUE (although it is possible
3072 that some already exist), because the TREE_PURPOSE and
3073 TREE_VALUE all need to be expanded together with the same
3074 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3075 resulting TREE_PURPOSE will mention the parameter packs in
3076 both the bases and the arguments to the bases. */
3079 tree parameter_packs = NULL_TREE;
3081 /* Determine which parameter packs will be used by the base
3083 ppd.visited = pointer_set_create ();
3084 ppd.parameter_packs = ¶meter_packs;
3085 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3088 if (parameter_packs == NULL_TREE)
3090 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3091 pointer_set_destroy (ppd.visited);
3092 return error_mark_node;
3095 if (TREE_VALUE (arg) != void_type_node)
3097 /* Collect the sets of parameter packs used in each of the
3098 initialization arguments. */
3099 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3101 /* Determine which parameter packs will be expanded in this
3103 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3108 pointer_set_destroy (ppd.visited);
3110 /* Create the pack expansion type for the base type. */
3111 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3112 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3113 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3115 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3116 they will rarely be compared to anything. */
3117 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3119 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3122 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3125 /* Build the PACK_EXPANSION_* node. */
3127 ? cxx_make_type (TYPE_PACK_EXPANSION)
3128 : make_node (EXPR_PACK_EXPANSION);
3129 SET_PACK_EXPANSION_PATTERN (result, arg);
3130 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3132 /* Propagate type and const-expression information. */
3133 TREE_TYPE (result) = TREE_TYPE (arg);
3134 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3137 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3138 they will rarely be compared to anything. */
3139 SET_TYPE_STRUCTURAL_EQUALITY (result);
3141 /* Determine which parameter packs will be expanded. */
3142 ppd.parameter_packs = ¶meter_packs;
3143 ppd.visited = pointer_set_create ();
3144 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3145 pointer_set_destroy (ppd.visited);
3147 /* Make sure we found some parameter packs. */
3148 if (parameter_packs == NULL_TREE)
3151 error ("expansion pattern %<%T%> contains no argument packs", arg);
3153 error ("expansion pattern %<%E%> contains no argument packs", arg);
3154 return error_mark_node;
3156 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3161 /* Checks T for any "bare" parameter packs, which have not yet been
3162 expanded, and issues an error if any are found. This operation can
3163 only be done on full expressions or types (e.g., an expression
3164 statement, "if" condition, etc.), because we could have expressions like:
3166 foo(f(g(h(args)))...)
3168 where "args" is a parameter pack. check_for_bare_parameter_packs
3169 should not be called for the subexpressions args, h(args),
3170 g(h(args)), or f(g(h(args))), because we would produce erroneous
3173 Returns TRUE and emits an error if there were bare parameter packs,
3174 returns FALSE otherwise. */
3176 check_for_bare_parameter_packs (tree t)
3178 tree parameter_packs = NULL_TREE;
3179 struct find_parameter_pack_data ppd;
3181 if (!processing_template_decl || !t || t == error_mark_node)
3184 if (TREE_CODE (t) == TYPE_DECL)
3187 ppd.parameter_packs = ¶meter_packs;
3188 ppd.visited = pointer_set_create ();
3189 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3190 pointer_set_destroy (ppd.visited);
3192 if (parameter_packs)
3194 error ("parameter packs not expanded with %<...%>:");
3195 while (parameter_packs)
3197 tree pack = TREE_VALUE (parameter_packs);
3198 tree name = NULL_TREE;
3200 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3201 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3202 name = TYPE_NAME (pack);
3203 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3204 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3206 name = DECL_NAME (pack);
3209 inform (input_location, " %qD", name);
3211 inform (input_location, " <anonymous>");
3213 parameter_packs = TREE_CHAIN (parameter_packs);
3222 /* Expand any parameter packs that occur in the template arguments in
3225 expand_template_argument_pack (tree args)
3227 tree result_args = NULL_TREE;
3228 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3229 int num_result_args = -1;
3230 int non_default_args_count = -1;
3232 /* First, determine if we need to expand anything, and the number of
3233 slots we'll need. */
3234 for (in_arg = 0; in_arg < nargs; ++in_arg)
3236 tree arg = TREE_VEC_ELT (args, in_arg);
3237 if (arg == NULL_TREE)
3239 if (ARGUMENT_PACK_P (arg))
3241 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3242 if (num_result_args < 0)
3243 num_result_args = in_arg + num_packed;
3245 num_result_args += num_packed;
3249 if (num_result_args >= 0)
3254 /* If no expansion is necessary, we're done. */
3255 if (num_result_args < 0)
3258 /* Expand arguments. */
3259 result_args = make_tree_vec (num_result_args);
3260 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3261 non_default_args_count =
3262 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3263 for (in_arg = 0; in_arg < nargs; ++in_arg)
3265 tree arg = TREE_VEC_ELT (args, in_arg);
3266 if (ARGUMENT_PACK_P (arg))
3268 tree packed = ARGUMENT_PACK_ARGS (arg);
3269 int i, num_packed = TREE_VEC_LENGTH (packed);
3270 for (i = 0; i < num_packed; ++i, ++out_arg)
3271 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3272 if (non_default_args_count > 0)
3273 non_default_args_count += num_packed;
3277 TREE_VEC_ELT (result_args, out_arg) = arg;
3281 if (non_default_args_count >= 0)
3282 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3286 /* Checks if DECL shadows a template parameter.
3288 [temp.local]: A template-parameter shall not be redeclared within its
3289 scope (including nested scopes).
3291 Emits an error and returns TRUE if the DECL shadows a parameter,
3292 returns FALSE otherwise. */
3295 check_template_shadow (tree decl)
3299 /* If we're not in a template, we can't possibly shadow a template
3301 if (!current_template_parms)
3304 /* Figure out what we're shadowing. */
3305 if (TREE_CODE (decl) == OVERLOAD)
3306 decl = OVL_CURRENT (decl);
3307 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3309 /* If there's no previous binding for this name, we're not shadowing
3310 anything, let alone a template parameter. */
3314 /* If we're not shadowing a template parameter, we're done. Note
3315 that OLDDECL might be an OVERLOAD (or perhaps even an
3316 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3318 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3321 /* We check for decl != olddecl to avoid bogus errors for using a
3322 name inside a class. We check TPFI to avoid duplicate errors for
3323 inline member templates. */
3325 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3328 error ("declaration of %q+#D", decl);
3329 error (" shadows template parm %q+#D", olddecl);
3333 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3334 ORIG_LEVEL, DECL, and TYPE. */
3337 build_template_parm_index (int index,
3343 tree t = make_node (TEMPLATE_PARM_INDEX);
3344 TEMPLATE_PARM_IDX (t) = index;
3345 TEMPLATE_PARM_LEVEL (t) = level;
3346 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3347 TEMPLATE_PARM_DECL (t) = decl;
3348 TREE_TYPE (t) = type;
3349 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3350 TREE_READONLY (t) = TREE_READONLY (decl);
3355 /* Find the canonical type parameter for the given template type
3356 parameter. Returns the canonical type parameter, which may be TYPE
3357 if no such parameter existed. */
3359 canonical_type_parameter (tree type)
3362 int idx = TEMPLATE_TYPE_IDX (type);
3363 if (!canonical_template_parms)
3364 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3366 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3367 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3369 list = VEC_index (tree, canonical_template_parms, idx);
3370 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3371 list = TREE_CHAIN (list);
3374 return TREE_VALUE (list);
3377 VEC_replace(tree, canonical_template_parms, idx,
3378 tree_cons (NULL_TREE, type,
3379 VEC_index (tree, canonical_template_parms, idx)));
3384 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3385 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3386 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3387 new one is created. */
3390 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3391 tsubst_flags_t complain)
3393 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3394 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3395 != TEMPLATE_PARM_LEVEL (index) - levels)
3396 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3398 tree orig_decl = TEMPLATE_PARM_DECL (index);
3401 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3402 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3403 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3404 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3405 DECL_ARTIFICIAL (decl) = 1;
3406 SET_DECL_TEMPLATE_PARM_P (decl);
3408 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3409 TEMPLATE_PARM_LEVEL (index) - levels,
3410 TEMPLATE_PARM_ORIG_LEVEL (index),
3412 TEMPLATE_PARM_DESCENDANTS (index) = t;
3413 TEMPLATE_PARM_PARAMETER_PACK (t)
3414 = TEMPLATE_PARM_PARAMETER_PACK (index);
3416 /* Template template parameters need this. */
3417 if (TREE_CODE (decl) == TEMPLATE_DECL)
3418 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3419 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3423 return TEMPLATE_PARM_DESCENDANTS (index);
3426 /* Process information from new template parameter PARM and append it to the
3427 LIST being built. This new parameter is a non-type parameter iff
3428 IS_NON_TYPE is true. This new parameter is a parameter
3429 pack iff IS_PARAMETER_PACK is true. The location of PARM is in
3433 process_template_parm (tree list, location_t parm_loc, tree parm, bool is_non_type,
3434 bool is_parameter_pack)
3441 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3442 defval = TREE_PURPOSE (parm);
3446 tree p = tree_last (list);
3448 if (p && TREE_VALUE (p) != error_mark_node)
3451 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3452 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3454 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3464 parm = TREE_VALUE (parm);
3466 SET_DECL_TEMPLATE_PARM_P (parm);
3468 if (TREE_TYPE (parm) == error_mark_node)
3470 err_parm_list = build_tree_list (defval, parm);
3471 TREE_VALUE (err_parm_list) = error_mark_node;
3472 return chainon (list, err_parm_list);
3478 The top-level cv-qualifiers on the template-parameter are
3479 ignored when determining its type. */
3480 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3481 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3483 err_parm_list = build_tree_list (defval, parm);
3484 TREE_VALUE (err_parm_list) = error_mark_node;
3485 return chainon (list, err_parm_list);
3488 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3490 /* This template parameter is not a parameter pack, but it
3491 should be. Complain about "bare" parameter packs. */
3492 check_for_bare_parameter_packs (TREE_TYPE (parm));
3494 /* Recover by calling this a parameter pack. */
3495 is_parameter_pack = true;
3499 /* A template parameter is not modifiable. */
3500 TREE_CONSTANT (parm) = 1;
3501 TREE_READONLY (parm) = 1;
3502 decl = build_decl (parm_loc,
3503 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3504 TREE_CONSTANT (decl) = 1;
3505 TREE_READONLY (decl) = 1;
3506 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3507 = build_template_parm_index (idx, processing_template_decl,
3508 processing_template_decl,
3509 decl, TREE_TYPE (parm));
3511 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3512 = is_parameter_pack;
3517 parm = TREE_VALUE (TREE_VALUE (parm));
3519 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3521 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3522 /* This is for distinguishing between real templates and template
3523 template parameters */
3524 TREE_TYPE (parm) = t;
3525 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3530 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3531 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3532 decl = build_decl (parm_loc,
3533 TYPE_DECL, parm, t);
3536 TYPE_NAME (t) = decl;
3537 TYPE_STUB_DECL (t) = decl;
3539 TEMPLATE_TYPE_PARM_INDEX (t)
3540 = build_template_parm_index (idx, processing_template_decl,
3541 processing_template_decl,
3542 decl, TREE_TYPE (parm));
3543 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3544 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3546 DECL_ARTIFICIAL (decl) = 1;
3547 SET_DECL_TEMPLATE_PARM_P (decl);
3549 parm = build_tree_list (defval, parm);
3550 return chainon (list, parm);
3553 /* The end of a template parameter list has been reached. Process the
3554 tree list into a parameter vector, converting each parameter into a more
3555 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3559 end_template_parm_list (tree parms)
3563 tree saved_parmlist = make_tree_vec (list_length (parms));
3565 current_template_parms
3566 = tree_cons (size_int (processing_template_decl),
3567 saved_parmlist, current_template_parms);
3569 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3571 next = TREE_CHAIN (parm);
3572 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3573 TREE_CHAIN (parm) = NULL_TREE;
3574 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
3575 TEMPLATE_TYPE_PARM_SIBLING_PARMS (TREE_TYPE (TREE_VALUE (parm))) =
3576 current_template_parms;
3579 --processing_template_parmlist;
3581 return saved_parmlist;
3584 /* end_template_decl is called after a template declaration is seen. */
3587 end_template_decl (void)
3589 reset_specialization ();
3591 if (! processing_template_decl)
3594 /* This matches the pushlevel in begin_template_parm_list. */
3597 --processing_template_decl;
3598 current_template_parms = TREE_CHAIN (current_template_parms);
3601 /* Within the declaration of a template, return all levels of template
3602 parameters that apply. The template parameters are represented as
3603 a TREE_VEC, in the form documented in cp-tree.h for template
3607 current_template_args (void)
3610 tree args = NULL_TREE;
3611 int length = TMPL_PARMS_DEPTH (current_template_parms);
3614 /* If there is only one level of template parameters, we do not
3615 create a TREE_VEC of TREE_VECs. Instead, we return a single
3616 TREE_VEC containing the arguments. */
3618 args = make_tree_vec (length);
3620 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3622 tree a = copy_node (TREE_VALUE (header));
3625 TREE_TYPE (a) = NULL_TREE;
3626 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3628 tree t = TREE_VEC_ELT (a, i);
3630 /* T will be a list if we are called from within a
3631 begin/end_template_parm_list pair, but a vector directly
3632 if within a begin/end_member_template_processing pair. */
3633 if (TREE_CODE (t) == TREE_LIST)
3637 if (!error_operand_p (t))
3639 if (TREE_CODE (t) == TYPE_DECL
3640 || TREE_CODE (t) == TEMPLATE_DECL)
3644 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3646 /* Turn this argument into a TYPE_ARGUMENT_PACK
3647 with a single element, which expands T. */
3648 tree vec = make_tree_vec (1);
3649 #ifdef ENABLE_CHECKING
3650 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3651 (vec, TREE_VEC_LENGTH (vec));
3653 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3655 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3656 SET_ARGUMENT_PACK_ARGS (t, vec);
3661 t = DECL_INITIAL (t);
3663 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3665 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3666 with a single element, which expands T. */
3667 tree vec = make_tree_vec (1);
3668 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3669 #ifdef ENABLE_CHECKING
3670 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3671 (vec, TREE_VEC_LENGTH (vec));
3673 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3675 t = make_node (NONTYPE_ARGUMENT_PACK);
3676 SET_ARGUMENT_PACK_ARGS (t, vec);
3677 TREE_TYPE (t) = type;
3680 TREE_VEC_ELT (a, i) = t;
3685 #ifdef ENABLE_CHECKING
3686 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3690 TREE_VEC_ELT (args, --l) = a;
3698 /* Update the declared TYPE by doing any lookups which were thought to be
3699 dependent, but are not now that we know the SCOPE of the declarator. */
3702 maybe_update_decl_type (tree orig_type, tree scope)
3704 tree type = orig_type;
3706 if (type == NULL_TREE)
3709 if (TREE_CODE (orig_type) == TYPE_DECL)
3710 type = TREE_TYPE (type);
3712 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3713 && dependent_type_p (type)
3714 /* Don't bother building up the args in this case. */
3715 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3717 /* tsubst in the args corresponding to the template parameters,
3718 including auto if present. Most things will be unchanged, but
3719 make_typename_type and tsubst_qualified_id will resolve
3720 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3721 tree args = current_template_args ();
3722 tree auto_node = type_uses_auto (type);
3726 tree auto_vec = make_tree_vec (1);
3727 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3728 args = add_to_template_args (args, auto_vec);
3730 pushed = push_scope (scope);
3731 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3736 if (type == error_mark_node)
3739 if (TREE_CODE (orig_type) == TYPE_DECL)
3741 if (same_type_p (type, TREE_TYPE (orig_type)))
3744 type = TYPE_NAME (type);
3749 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3750 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3751 a member template. Used by push_template_decl below. */
3754 build_template_decl (tree decl, tree parms, bool member_template_p)
3756 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3757 DECL_TEMPLATE_PARMS (tmpl) = parms;
3758 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3759 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3764 struct template_parm_data
3766 /* The level of the template parameters we are currently
3770 /* The index of the specialization argument we are currently
3774 /* An array whose size is the number of template parameters. The
3775 elements are nonzero if the parameter has been used in any one
3776 of the arguments processed so far. */
3779 /* An array whose size is the number of template arguments. The
3780 elements are nonzero if the argument makes use of template
3781 parameters of this level. */
3782 int* arg_uses_template_parms;
3785 /* Subroutine of push_template_decl used to see if each template
3786 parameter in a partial specialization is used in the explicit
3787 argument list. If T is of the LEVEL given in DATA (which is
3788 treated as a template_parm_data*), then DATA->PARMS is marked
3792 mark_template_parm (tree t, void* data)
3796 struct template_parm_data* tpd = (struct template_parm_data*) data;
3798 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3800 level = TEMPLATE_PARM_LEVEL (t);
3801 idx = TEMPLATE_PARM_IDX (t);
3805 level = TEMPLATE_TYPE_LEVEL (t);
3806 idx = TEMPLATE_TYPE_IDX (t);
3809 if (level == tpd->level)
3811 tpd->parms[idx] = 1;
3812 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3815 /* Return zero so that for_each_template_parm will continue the
3816 traversal of the tree; we want to mark *every* template parm. */
3820 /* Process the partial specialization DECL. */
3823 process_partial_specialization (tree decl)
3825 tree type = TREE_TYPE (decl);
3826 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3827 tree specargs = CLASSTYPE_TI_ARGS (type);
3828 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3829 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3831 int nargs = TREE_VEC_LENGTH (inner_args);
3834 int did_error_intro = 0;
3835 struct template_parm_data tpd;
3836 struct template_parm_data tpd2;
3838 gcc_assert (current_template_parms);
3840 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3841 ntparms = TREE_VEC_LENGTH (inner_parms);
3843 /* We check that each of the template parameters given in the
3844 partial specialization is used in the argument list to the
3845 specialization. For example:
3847 template <class T> struct S;
3848 template <class T> struct S<T*>;
3850 The second declaration is OK because `T*' uses the template
3851 parameter T, whereas
3853 template <class T> struct S<int>;
3855 is no good. Even trickier is:
3866 The S2<T> declaration is actually invalid; it is a
3867 full-specialization. Of course,
3870 struct S2<T (*)(U)>;
3872 or some such would have been OK. */
3873 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3874 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
3875 memset (tpd.parms, 0, sizeof (int) * ntparms);
3877 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
3878 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3879 for (i = 0; i < nargs; ++i)
3881 tpd.current_arg = i;
3882 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3883 &mark_template_parm,
3886 /*include_nondeduced_p=*/false);
3888 for (i = 0; i < ntparms; ++i)
3889 if (tpd.parms[i] == 0)
3891 /* One of the template parms was not used in the
3893 if (!did_error_intro)
3895 error ("template parameters not used in partial specialization:");
3896 did_error_intro = 1;
3899 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3902 /* [temp.class.spec]
3904 The argument list of the specialization shall not be identical to
3905 the implicit argument list of the primary template. */
3906 if (comp_template_args
3908 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3910 error ("partial specialization %qT does not specialize any template arguments", type);
3912 /* [temp.class.spec]
3914 A partially specialized non-type argument expression shall not
3915 involve template parameters of the partial specialization except
3916 when the argument expression is a simple identifier.
3918 The type of a template parameter corresponding to a specialized
3919 non-type argument shall not be dependent on a parameter of the
3922 Also, we verify that pack expansions only occur at the
3923 end of the argument list. */
3924 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3926 for (i = 0; i < nargs; ++i)
3928 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3929 tree arg = TREE_VEC_ELT (inner_args, i);
3930 tree packed_args = NULL_TREE;
3933 if (ARGUMENT_PACK_P (arg))
3935 /* Extract the arguments from the argument pack. We'll be
3936 iterating over these in the following loop. */
3937 packed_args = ARGUMENT_PACK_ARGS (arg);
3938 len = TREE_VEC_LENGTH (packed_args);
3941 for (j = 0; j < len; j++)
3944 /* Get the Jth argument in the parameter pack. */
3945 arg = TREE_VEC_ELT (packed_args, j);
3947 if (PACK_EXPANSION_P (arg))
3949 /* Pack expansions must come at the end of the
3951 if ((packed_args && j < len - 1)
3952 || (!packed_args && i < nargs - 1))
3954 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3955 error ("parameter pack argument %qE must be at the "
3956 "end of the template argument list", arg);
3958 error ("parameter pack argument %qT must be at the "
3959 "end of the template argument list", arg);
3963 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3964 /* We only care about the pattern. */
3965 arg = PACK_EXPANSION_PATTERN (arg);
3967 if (/* These first two lines are the `non-type' bit. */
3969 && TREE_CODE (arg) != TEMPLATE_DECL
3970 /* This next line is the `argument expression is not just a
3971 simple identifier' condition and also the `specialized
3972 non-type argument' bit. */
3973 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3975 if ((!packed_args && tpd.arg_uses_template_parms[i])
3976 || (packed_args && uses_template_parms (arg)))
3977 error ("template argument %qE involves template parameter(s)",
3981 /* Look at the corresponding template parameter,
3982 marking which template parameters its type depends
3984 tree type = TREE_TYPE (parm);
3988 /* We haven't yet initialized TPD2. Do so now. */
3989 tpd2.arg_uses_template_parms
3990 = (int *) alloca (sizeof (int) * nargs);
3991 /* The number of parameters here is the number in the
3992 main template, which, as checked in the assertion
3994 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
3996 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
3999 /* Mark the template parameters. But this time, we're
4000 looking for the template parameters of the main
4001 template, not in the specialization. */
4002 tpd2.current_arg = i;
4003 tpd2.arg_uses_template_parms[i] = 0;
4004 memset (tpd2.parms, 0, sizeof (int) * nargs);
4005 for_each_template_parm (type,
4006 &mark_template_parm,
4009 /*include_nondeduced_p=*/false);
4011 if (tpd2.arg_uses_template_parms [i])
4013 /* The type depended on some template parameters.
4014 If they are fully specialized in the
4015 specialization, that's OK. */
4018 for (j = 0; j < nargs; ++j)
4019 if (tpd2.parms[j] != 0
4020 && tpd.arg_uses_template_parms [j])
4023 error_n (input_location, count,
4024 "type %qT of template argument %qE depends "
4025 "on a template parameter",
4026 "type %qT of template argument %qE depends "
4027 "on template parameters",
4036 /* We should only get here once. */
4037 gcc_assert (!COMPLETE_TYPE_P (type));
4039 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4040 = tree_cons (specargs, inner_parms,
4041 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4042 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4046 /* Check that a template declaration's use of default arguments and
4047 parameter packs is not invalid. Here, PARMS are the template
4048 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4049 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4053 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4054 declaration (but not a definition); 1 indicates a declaration, 2
4055 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4056 emitted for extraneous default arguments.
4058 Returns TRUE if there were no errors found, FALSE otherwise. */
4061 check_default_tmpl_args (tree decl, tree parms, int is_primary,
4062 int is_partial, int is_friend_decl)
4065 int last_level_to_check;
4067 bool no_errors = true;
4071 A default template-argument shall not be specified in a
4072 function template declaration or a function template definition, nor
4073 in the template-parameter-list of the definition of a member of a
4076 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4077 /* You can't have a function template declaration in a local
4078 scope, nor you can you define a member of a class template in a
4082 if (current_class_type
4083 && !TYPE_BEING_DEFINED (current_class_type)
4084 && DECL_LANG_SPECIFIC (decl)
4085 && DECL_DECLARES_FUNCTION_P (decl)
4086 /* If this is either a friend defined in the scope of the class
4087 or a member function. */
4088 && (DECL_FUNCTION_MEMBER_P (decl)
4089 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4090 : DECL_FRIEND_CONTEXT (decl)
4091 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4093 /* And, if it was a member function, it really was defined in
4094 the scope of the class. */
4095 && (!DECL_FUNCTION_MEMBER_P (decl)
4096 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4097 /* We already checked these parameters when the template was
4098 declared, so there's no need to do it again now. This function
4099 was defined in class scope, but we're processing it's body now
4100 that the class is complete. */
4103 /* Core issue 226 (C++0x only): the following only applies to class
4105 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
4109 If a template-parameter has a default template-argument, all
4110 subsequent template-parameters shall have a default
4111 template-argument supplied. */
4112 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4114 tree inner_parms = TREE_VALUE (parm_level);
4115 int ntparms = TREE_VEC_LENGTH (inner_parms);
4116 int seen_def_arg_p = 0;
4119 for (i = 0; i < ntparms; ++i)
4121 tree parm = TREE_VEC_ELT (inner_parms, i);
4123 if (parm == error_mark_node)
4126 if (TREE_PURPOSE (parm))
4128 else if (seen_def_arg_p
4129 && !template_parameter_pack_p (TREE_VALUE (parm)))
4131 error ("no default argument for %qD", TREE_VALUE (parm));
4132 /* For better subsequent error-recovery, we indicate that
4133 there should have been a default argument. */
4134 TREE_PURPOSE (parm) = error_mark_node;
4140 /* Don't complain about an enclosing partial
4142 && parm_level == parms
4143 && TREE_CODE (decl) == TYPE_DECL
4145 && template_parameter_pack_p (TREE_VALUE (parm)))
4147 /* A primary class template can only have one
4148 parameter pack, at the end of the template
4151 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4152 error ("parameter pack %qE must be at the end of the"
4153 " template parameter list", TREE_VALUE (parm));
4155 error ("parameter pack %qT must be at the end of the"
4156 " template parameter list",
4157 TREE_TYPE (TREE_VALUE (parm)));
4159 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4167 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4171 /* For an ordinary class template, default template arguments are
4172 allowed at the innermost level, e.g.:
4173 template <class T = int>
4175 but, in a partial specialization, they're not allowed even
4176 there, as we have in [temp.class.spec]:
4178 The template parameter list of a specialization shall not
4179 contain default template argument values.
4181 So, for a partial specialization, or for a function template
4182 (in C++98/C++03), we look at all of them. */
4185 /* But, for a primary class template that is not a partial
4186 specialization we look at all template parameters except the
4188 parms = TREE_CHAIN (parms);
4190 /* Figure out what error message to issue. */
4191 if (is_friend_decl == 2)
4192 msg = G_("default template arguments may not be used in function template "
4193 "friend re-declaration");
4194 else if (is_friend_decl)
4195 msg = G_("default template arguments may not be used in function template "
4196 "friend declarations");
4197 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4198 msg = G_("default template arguments may not be used in function templates "
4199 "without -std=c++0x or -std=gnu++0x");
4200 else if (is_partial)
4201 msg = G_("default template arguments may not be used in "
4202 "partial specializations");
4204 msg = G_("default argument for template parameter for class enclosing %qD");
4206 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4207 /* If we're inside a class definition, there's no need to
4208 examine the parameters to the class itself. On the one
4209 hand, they will be checked when the class is defined, and,
4210 on the other, default arguments are valid in things like:
4211 template <class T = double>
4212 struct S { template <class U> void f(U); };
4213 Here the default argument for `S' has no bearing on the
4214 declaration of `f'. */
4215 last_level_to_check = template_class_depth (current_class_type) + 1;
4217 /* Check everything. */
4218 last_level_to_check = 0;
4220 for (parm_level = parms;
4221 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4222 parm_level = TREE_CHAIN (parm_level))
4224 tree inner_parms = TREE_VALUE (parm_level);
4228 ntparms = TREE_VEC_LENGTH (inner_parms);
4229 for (i = 0; i < ntparms; ++i)
4231 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4234 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4239 if (is_friend_decl == 2)
4246 /* Clear out the default argument so that we are not
4248 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4252 /* At this point, if we're still interested in issuing messages,
4253 they must apply to classes surrounding the object declared. */
4255 msg = G_("default argument for template parameter for class "
4262 /* Worker for push_template_decl_real, called via
4263 for_each_template_parm. DATA is really an int, indicating the
4264 level of the parameters we are interested in. If T is a template
4265 parameter of that level, return nonzero. */
4268 template_parm_this_level_p (tree t, void* data)
4270 int this_level = *(int *)data;
4273 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4274 level = TEMPLATE_PARM_LEVEL (t);
4276 level = TEMPLATE_TYPE_LEVEL (t);
4277 return level == this_level;
4280 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4281 parameters given by current_template_args, or reuses a
4282 previously existing one, if appropriate. Returns the DECL, or an
4283 equivalent one, if it is replaced via a call to duplicate_decls.
4285 If IS_FRIEND is true, DECL is a friend declaration. */
4288 push_template_decl_real (tree decl, bool is_friend)
4296 int new_template_p = 0;
4297 /* True if the template is a member template, in the sense of
4299 bool member_template_p = false;
4301 if (decl == error_mark_node || !current_template_parms)
4302 return error_mark_node;
4304 /* See if this is a partial specialization. */
4305 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4306 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4307 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4309 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4313 /* For a friend, we want the context of the friend function, not
4314 the type of which it is a friend. */
4315 ctx = DECL_CONTEXT (decl);
4316 else if (CP_DECL_CONTEXT (decl)
4317 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4318 /* In the case of a virtual function, we want the class in which
4320 ctx = CP_DECL_CONTEXT (decl);
4322 /* Otherwise, if we're currently defining some class, the DECL
4323 is assumed to be a member of the class. */
4324 ctx = current_scope ();
4326 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4329 if (!DECL_CONTEXT (decl))
4330 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4332 /* See if this is a primary template. */
4333 if (is_friend && ctx)
4334 /* A friend template that specifies a class context, i.e.
4335 template <typename T> friend void A<T>::f();
4339 primary = template_parm_scope_p ();
4343 if (DECL_CLASS_SCOPE_P (decl))
4344 member_template_p = true;
4345 if (TREE_CODE (decl) == TYPE_DECL
4346 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4348 error ("template class without a name");
4349 return error_mark_node;
4351 else if (TREE_CODE (decl) == FUNCTION_DECL)
4353 if (DECL_DESTRUCTOR_P (decl))
4357 A destructor shall not be a member template. */
4358 error ("destructor %qD declared as member template", decl);
4359 return error_mark_node;
4361 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4362 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
4363 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4364 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4365 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4366 == void_list_node)))
4368 /* [basic.stc.dynamic.allocation]
4370 An allocation function can be a function
4371 template. ... Template allocation functions shall
4372 have two or more parameters. */
4373 error ("invalid template declaration of %qD", decl);
4374 return error_mark_node;
4377 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4378 && CLASS_TYPE_P (TREE_TYPE (decl)))
4382 error ("template declaration of %q#D", decl);
4383 return error_mark_node;
4387 /* Check to see that the rules regarding the use of default
4388 arguments are not being violated. */
4389 check_default_tmpl_args (decl, current_template_parms,
4390 primary, is_partial, /*is_friend_decl=*/0);
4392 /* Ensure that there are no parameter packs in the type of this
4393 declaration that have not been expanded. */
4394 if (TREE_CODE (decl) == FUNCTION_DECL)
4396 /* Check each of the arguments individually to see if there are
4397 any bare parameter packs. */
4398 tree type = TREE_TYPE (decl);
4399 tree arg = DECL_ARGUMENTS (decl);
4400 tree argtype = TYPE_ARG_TYPES (type);
4402 while (arg && argtype)
4404 if (!FUNCTION_PARAMETER_PACK_P (arg)
4405 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4407 /* This is a PARM_DECL that contains unexpanded parameter
4408 packs. We have already complained about this in the
4409 check_for_bare_parameter_packs call, so just replace
4410 these types with ERROR_MARK_NODE. */
4411 TREE_TYPE (arg) = error_mark_node;
4412 TREE_VALUE (argtype) = error_mark_node;
4415 arg = TREE_CHAIN (arg);
4416 argtype = TREE_CHAIN (argtype);
4419 /* Check for bare parameter packs in the return type and the
4420 exception specifiers. */
4421 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4422 /* Errors were already issued, set return type to int
4423 as the frontend doesn't expect error_mark_node as
4425 TREE_TYPE (type) = integer_type_node;
4426 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4427 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4429 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
4431 TREE_TYPE (decl) = error_mark_node;
4432 return error_mark_node;
4436 return process_partial_specialization (decl);
4438 args = current_template_args ();
4441 || TREE_CODE (ctx) == FUNCTION_DECL
4442 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4443 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4445 if (DECL_LANG_SPECIFIC (decl)
4446 && DECL_TEMPLATE_INFO (decl)
4447 && DECL_TI_TEMPLATE (decl))
4448 tmpl = DECL_TI_TEMPLATE (decl);
4449 /* If DECL is a TYPE_DECL for a class-template, then there won't
4450 be DECL_LANG_SPECIFIC. The information equivalent to
4451 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4452 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4453 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4454 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4456 /* Since a template declaration already existed for this
4457 class-type, we must be redeclaring it here. Make sure
4458 that the redeclaration is valid. */
4459 redeclare_class_template (TREE_TYPE (decl),
4460 current_template_parms);
4461 /* We don't need to create a new TEMPLATE_DECL; just use the
4462 one we already had. */
4463 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4467 tmpl = build_template_decl (decl, current_template_parms,
4471 if (DECL_LANG_SPECIFIC (decl)
4472 && DECL_TEMPLATE_SPECIALIZATION (decl))
4474 /* A specialization of a member template of a template
4476 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4477 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4478 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4484 tree a, t, current, parms;
4486 tree tinfo = get_template_info (decl);
4490 error ("template definition of non-template %q#D", decl);
4491 return error_mark_node;
4494 tmpl = TI_TEMPLATE (tinfo);
4496 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4497 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4498 && DECL_TEMPLATE_SPECIALIZATION (decl)
4499 && DECL_MEMBER_TEMPLATE_P (tmpl))
4503 /* The declaration is a specialization of a member
4504 template, declared outside the class. Therefore, the
4505 innermost template arguments will be NULL, so we
4506 replace them with the arguments determined by the
4507 earlier call to check_explicit_specialization. */
4508 args = DECL_TI_ARGS (decl);
4511 = build_template_decl (decl, current_template_parms,
4513 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4514 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4515 DECL_TI_TEMPLATE (decl) = new_tmpl;
4516 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4517 DECL_TEMPLATE_INFO (new_tmpl)
4518 = build_template_info (tmpl, args);
4520 register_specialization (new_tmpl,
4521 most_general_template (tmpl),
4527 /* Make sure the template headers we got make sense. */
4529 parms = DECL_TEMPLATE_PARMS (tmpl);
4530 i = TMPL_PARMS_DEPTH (parms);
4531 if (TMPL_ARGS_DEPTH (args) != i)
4533 error ("expected %d levels of template parms for %q#D, got %d",
4534 i, decl, TMPL_ARGS_DEPTH (args));
4537 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4539 a = TMPL_ARGS_LEVEL (args, i);
4540 t = INNERMOST_TEMPLATE_PARMS (parms);
4542 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4544 if (current == decl)
4545 error ("got %d template parameters for %q#D",
4546 TREE_VEC_LENGTH (a), decl);
4548 error ("got %d template parameters for %q#T",
4549 TREE_VEC_LENGTH (a), current);
4550 error (" but %d required", TREE_VEC_LENGTH (t));
4551 return error_mark_node;
4554 if (current == decl)
4556 else if (current == NULL_TREE)
4557 /* Can happen in erroneous input. */
4560 current = (TYPE_P (current)
4561 ? TYPE_CONTEXT (current)
4562 : DECL_CONTEXT (current));
4565 /* Check that the parms are used in the appropriate qualifying scopes
4566 in the declarator. */
4567 if (!comp_template_args
4569 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4572 template arguments to %qD do not match original template %qD",
4573 decl, DECL_TEMPLATE_RESULT (tmpl));
4574 if (!uses_template_parms (TI_ARGS (tinfo)))
4575 inform (input_location, "use template<> for an explicit specialization");
4576 /* Avoid crash in import_export_decl. */
4577 DECL_INTERFACE_KNOWN (decl) = 1;
4578 return error_mark_node;
4582 DECL_TEMPLATE_RESULT (tmpl) = decl;
4583 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4585 /* Push template declarations for global functions and types. Note
4586 that we do not try to push a global template friend declared in a
4587 template class; such a thing may well depend on the template
4588 parameters of the class. */
4589 if (new_template_p && !ctx
4590 && !(is_friend && template_class_depth (current_class_type) > 0))
4592 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4593 if (tmpl == error_mark_node)
4594 return error_mark_node;
4596 /* Hide template friend classes that haven't been declared yet. */
4597 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4599 DECL_ANTICIPATED (tmpl) = 1;
4600 DECL_FRIEND_P (tmpl) = 1;
4606 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4609 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4610 if (DECL_CONV_FN_P (tmpl))
4612 int depth = TMPL_PARMS_DEPTH (parms);
4614 /* It is a conversion operator. See if the type converted to
4615 depends on innermost template operands. */
4617 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4619 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4622 /* Give template template parms a DECL_CONTEXT of the template
4623 for which they are a parameter. */
4624 parms = INNERMOST_TEMPLATE_PARMS (parms);
4625 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4627 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4628 if (TREE_CODE (parm) == TEMPLATE_DECL)
4629 DECL_CONTEXT (parm) = tmpl;
4633 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4634 back to its most general template. If TMPL is a specialization,
4635 ARGS may only have the innermost set of arguments. Add the missing
4636 argument levels if necessary. */
4637 if (DECL_TEMPLATE_INFO (tmpl))
4638 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4640 info = build_template_info (tmpl, args);
4642 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4643 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4644 else if (DECL_LANG_SPECIFIC (decl))
4645 DECL_TEMPLATE_INFO (decl) = info;
4647 return DECL_TEMPLATE_RESULT (tmpl);
4651 push_template_decl (tree decl)
4653 return push_template_decl_real (decl, false);
4656 /* Called when a class template TYPE is redeclared with the indicated
4657 template PARMS, e.g.:
4659 template <class T> struct S;
4660 template <class T> struct S {}; */
4663 redeclare_class_template (tree type, tree parms)
4669 if (!TYPE_TEMPLATE_INFO (type))
4671 error ("%qT is not a template type", type);
4675 tmpl = TYPE_TI_TEMPLATE (type);
4676 if (!PRIMARY_TEMPLATE_P (tmpl))
4677 /* The type is nested in some template class. Nothing to worry
4678 about here; there are no new template parameters for the nested
4684 error ("template specifiers not specified in declaration of %qD",
4689 parms = INNERMOST_TEMPLATE_PARMS (parms);
4690 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4692 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4694 error_n (input_location, TREE_VEC_LENGTH (parms),
4695 "redeclared with %d template parameter",
4696 "redeclared with %d template parameters",
4697 TREE_VEC_LENGTH (parms));
4698 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
4699 "previous declaration %q+D used %d template parameter",
4700 "previous declaration %q+D used %d template parameters",
4701 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4705 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4712 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4713 || TREE_VEC_ELT (parms, i) == error_mark_node)
4716 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4717 if (tmpl_parm == error_mark_node)
4720 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4721 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4722 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4724 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4726 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4727 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4728 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4729 || (TREE_CODE (tmpl_parm) != PARM_DECL
4730 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4731 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4732 || (TREE_CODE (tmpl_parm) == PARM_DECL
4733 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4734 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
4736 error ("template parameter %q+#D", tmpl_parm);
4737 error ("redeclared here as %q#D", parm);
4741 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4743 /* We have in [temp.param]:
4745 A template-parameter may not be given default arguments
4746 by two different declarations in the same scope. */
4747 error_at (input_location, "redefinition of default argument for %q#D", parm);
4748 inform (DECL_SOURCE_LOCATION (tmpl_parm),
4749 "original definition appeared here");
4753 if (parm_default != NULL_TREE)
4754 /* Update the previous template parameters (which are the ones
4755 that will really count) with the new default value. */
4756 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4757 else if (tmpl_default != NULL_TREE)
4758 /* Update the new parameters, too; they'll be used as the
4759 parameters for any members. */
4760 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4766 /* Simplify EXPR if it is a non-dependent expression. Returns the
4767 (possibly simplified) expression. */
4770 fold_non_dependent_expr (tree expr)
4772 if (expr == NULL_TREE)
4775 /* If we're in a template, but EXPR isn't value dependent, simplify
4776 it. We're supposed to treat:
4778 template <typename T> void f(T[1 + 1]);
4779 template <typename T> void f(T[2]);
4781 as two declarations of the same function, for example. */
4782 if (processing_template_decl
4783 && !type_dependent_expression_p (expr)
4784 && !value_dependent_expression_p (expr))
4786 HOST_WIDE_INT saved_processing_template_decl;
4788 saved_processing_template_decl = processing_template_decl;
4789 processing_template_decl = 0;
4790 expr = tsubst_copy_and_build (expr,
4793 /*in_decl=*/NULL_TREE,
4794 /*function_p=*/false,
4795 /*integral_constant_expression_p=*/true);
4796 processing_template_decl = saved_processing_template_decl;
4801 /* EXPR is an expression which is used in a constant-expression context.
4802 For instance, it could be a VAR_DECL with a constant initializer.
4803 Extract the innermost constant expression.
4805 This is basically a more powerful version of
4806 integral_constant_value, which can be used also in templates where
4807 initializers can maintain a syntactic rather than semantic form
4808 (even if they are non-dependent, for access-checking purposes). */
4811 fold_decl_constant_value (tree expr)
4813 tree const_expr = expr;
4816 expr = fold_non_dependent_expr (const_expr);
4817 const_expr = integral_constant_value (expr);
4819 while (expr != const_expr);
4824 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4825 must be a function or a pointer-to-function type, as specified
4826 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4827 and check that the resulting function has external linkage. */
4830 convert_nontype_argument_function (tree type, tree expr)
4835 fn = instantiate_type (type, fns, tf_none);
4836 if (fn == error_mark_node)
4837 return error_mark_node;
4840 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4841 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4842 if (TREE_CODE (fn_no_ptr) == BASELINK)
4843 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4845 /* [temp.arg.nontype]/1
4847 A template-argument for a non-type, non-template template-parameter
4850 -- the address of an object or function with external linkage. */
4851 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4853 error ("%qE is not a valid template argument for type %qT "
4854 "because function %qD has not external linkage",
4855 expr, type, fn_no_ptr);
4862 /* Subroutine of convert_nontype_argument.
4863 Check if EXPR of type TYPE is a valid pointer-to-member constant.
4864 Emit an error otherwise. */
4867 check_valid_ptrmem_cst_expr (tree type, tree expr)
4870 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
4872 error ("%qE is not a valid template argument for type %qT",
4874 error ("it must be a pointer-to-member of the form `&X::Y'");
4878 /* Attempt to convert the non-type template parameter EXPR to the
4879 indicated TYPE. If the conversion is successful, return the
4880 converted value. If the conversion is unsuccessful, return
4881 NULL_TREE if we issued an error message, or error_mark_node if we
4882 did not. We issue error messages for out-and-out bad template
4883 parameters, but not simply because the conversion failed, since we
4884 might be just trying to do argument deduction. Both TYPE and EXPR
4885 must be non-dependent.
4887 The conversion follows the special rules described in
4888 [temp.arg.nontype], and it is much more strict than an implicit
4891 This function is called twice for each template argument (see
4892 lookup_template_class for a more accurate description of this
4893 problem). This means that we need to handle expressions which
4894 are not valid in a C++ source, but can be created from the
4895 first call (for instance, casts to perform conversions). These
4896 hacks can go away after we fix the double coercion problem. */
4899 convert_nontype_argument (tree type, tree expr)
4903 /* Detect immediately string literals as invalid non-type argument.
4904 This special-case is not needed for correctness (we would easily
4905 catch this later), but only to provide better diagnostic for this
4906 common user mistake. As suggested by DR 100, we do not mention
4907 linkage issues in the diagnostic as this is not the point. */
4908 if (TREE_CODE (expr) == STRING_CST)
4910 error ("%qE is not a valid template argument for type %qT "
4911 "because string literals can never be used in this context",
4916 /* If we are in a template, EXPR may be non-dependent, but still
4917 have a syntactic, rather than semantic, form. For example, EXPR
4918 might be a SCOPE_REF, rather than the VAR_DECL to which the
4919 SCOPE_REF refers. Preserving the qualifying scope is necessary
4920 so that access checking can be performed when the template is
4921 instantiated -- but here we need the resolved form so that we can
4922 convert the argument. */
4923 expr = fold_non_dependent_expr (expr);
4924 if (error_operand_p (expr))
4925 return error_mark_node;
4926 expr_type = TREE_TYPE (expr);
4928 /* HACK: Due to double coercion, we can get a
4929 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4930 which is the tree that we built on the first call (see
4931 below when coercing to reference to object or to reference to
4932 function). We just strip everything and get to the arg.
4933 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4935 if (TREE_CODE (expr) == NOP_EXPR)
4937 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
4939 /* ??? Maybe we could use convert_from_reference here, but we
4940 would need to relax its constraints because the NOP_EXPR
4941 could actually change the type to something more cv-qualified,
4942 and this is not folded by convert_from_reference. */
4943 tree addr = TREE_OPERAND (expr, 0);
4944 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
4945 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
4946 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
4947 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4948 (TREE_TYPE (expr_type),
4949 TREE_TYPE (TREE_TYPE (addr))));
4951 expr = TREE_OPERAND (addr, 0);
4952 expr_type = TREE_TYPE (expr);
4955 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4956 parameter is a pointer to object, through decay and
4957 qualification conversion. Let's strip everything. */
4958 else if (TYPE_PTROBV_P (type))
4961 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
4962 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
4963 /* Skip the ADDR_EXPR only if it is part of the decay for
4964 an array. Otherwise, it is part of the original argument
4965 in the source code. */
4966 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
4967 expr = TREE_OPERAND (expr, 0);
4968 expr_type = TREE_TYPE (expr);
4972 /* [temp.arg.nontype]/5, bullet 1
4974 For a non-type template-parameter of integral or enumeration type,
4975 integral promotions (_conv.prom_) and integral conversions
4976 (_conv.integral_) are applied. */
4977 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4979 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type))
4980 return error_mark_node;
4982 expr = fold_decl_constant_value (expr);
4983 /* Notice that there are constant expressions like '4 % 0' which
4984 do not fold into integer constants. */
4985 if (TREE_CODE (expr) != INTEGER_CST)
4987 error ("%qE is not a valid template argument for type %qT "
4988 "because it is a non-constant expression", expr, type);
4992 /* At this point, an implicit conversion does what we want,
4993 because we already know that the expression is of integral
4995 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
4996 if (expr == error_mark_node)
4997 return error_mark_node;
4999 /* Conversion was allowed: fold it to a bare integer constant. */
5002 /* [temp.arg.nontype]/5, bullet 2
5004 For a non-type template-parameter of type pointer to object,
5005 qualification conversions (_conv.qual_) and the array-to-pointer
5006 conversion (_conv.array_) are applied. */
5007 else if (TYPE_PTROBV_P (type))
5009 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5011 A template-argument for a non-type, non-template template-parameter
5012 shall be one of: [...]
5014 -- the name of a non-type template-parameter;
5015 -- the address of an object or function with external linkage, [...]
5016 expressed as "& id-expression" where the & is optional if the name
5017 refers to a function or array, or if the corresponding
5018 template-parameter is a reference.
5020 Here, we do not care about functions, as they are invalid anyway
5021 for a parameter of type pointer-to-object. */
5023 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5024 /* Non-type template parameters are OK. */
5026 else if (TREE_CODE (expr) != ADDR_EXPR
5027 && TREE_CODE (expr_type) != ARRAY_TYPE)
5029 if (TREE_CODE (expr) == VAR_DECL)
5031 error ("%qD is not a valid template argument "
5032 "because %qD is a variable, not the address of "
5037 /* Other values, like integer constants, might be valid
5038 non-type arguments of some other type. */
5039 return error_mark_node;
5045 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5046 ? TREE_OPERAND (expr, 0) : expr);
5047 if (TREE_CODE (decl) != VAR_DECL)
5049 error ("%qE is not a valid template argument of type %qT "
5050 "because %qE is not a variable",
5054 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
5056 error ("%qE is not a valid template argument of type %qT "
5057 "because %qD does not have external linkage",
5063 expr = decay_conversion (expr);
5064 if (expr == error_mark_node)
5065 return error_mark_node;
5067 expr = perform_qualification_conversions (type, expr);
5068 if (expr == error_mark_node)
5069 return error_mark_node;
5071 /* [temp.arg.nontype]/5, bullet 3
5073 For a non-type template-parameter of type reference to object, no
5074 conversions apply. The type referred to by the reference may be more
5075 cv-qualified than the (otherwise identical) type of the
5076 template-argument. The template-parameter is bound directly to the
5077 template-argument, which must be an lvalue. */
5078 else if (TYPE_REF_OBJ_P (type))
5080 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5082 return error_mark_node;
5084 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5086 error ("%qE is not a valid template argument for type %qT "
5087 "because of conflicts in cv-qualification", expr, type);
5091 if (!real_lvalue_p (expr))
5093 error ("%qE is not a valid template argument for type %qT "
5094 "because it is not an lvalue", expr, type);
5098 /* [temp.arg.nontype]/1
5100 A template-argument for a non-type, non-template template-parameter
5101 shall be one of: [...]
5103 -- the address of an object or function with external linkage. */
5104 if (TREE_CODE (expr) == INDIRECT_REF
5105 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5107 expr = TREE_OPERAND (expr, 0);
5110 error ("%q#D is not a valid template argument for type %qT "
5111 "because a reference variable does not have a constant "
5112 "address", expr, type);
5119 error ("%qE is not a valid template argument for type %qT "
5120 "because it is not an object with external linkage",
5125 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5127 error ("%qE is not a valid template argument for type %qT "
5128 "because object %qD has not external linkage",
5133 expr = build_nop (type, build_address (expr));
5135 /* [temp.arg.nontype]/5, bullet 4
5137 For a non-type template-parameter of type pointer to function, only
5138 the function-to-pointer conversion (_conv.func_) is applied. If the
5139 template-argument represents a set of overloaded functions (or a
5140 pointer to such), the matching function is selected from the set
5142 else if (TYPE_PTRFN_P (type))
5144 /* If the argument is a template-id, we might not have enough
5145 context information to decay the pointer. */
5146 if (!type_unknown_p (expr_type))
5148 expr = decay_conversion (expr);
5149 if (expr == error_mark_node)
5150 return error_mark_node;
5153 expr = convert_nontype_argument_function (type, expr);
5154 if (!expr || expr == error_mark_node)
5157 if (TREE_CODE (expr) != ADDR_EXPR)
5159 error ("%qE is not a valid template argument for type %qT", expr, type);
5160 error ("it must be the address of a function with external linkage");
5164 /* [temp.arg.nontype]/5, bullet 5
5166 For a non-type template-parameter of type reference to function, no
5167 conversions apply. If the template-argument represents a set of
5168 overloaded functions, the matching function is selected from the set
5170 else if (TYPE_REFFN_P (type))
5172 if (TREE_CODE (expr) == ADDR_EXPR)
5174 error ("%qE is not a valid template argument for type %qT "
5175 "because it is a pointer", expr, type);
5176 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5180 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5181 if (!expr || expr == error_mark_node)
5184 expr = build_nop (type, build_address (expr));
5186 /* [temp.arg.nontype]/5, bullet 6
5188 For a non-type template-parameter of type pointer to member function,
5189 no conversions apply. If the template-argument represents a set of
5190 overloaded member functions, the matching member function is selected
5191 from the set (_over.over_). */
5192 else if (TYPE_PTRMEMFUNC_P (type))
5194 expr = instantiate_type (type, expr, tf_none);
5195 if (expr == error_mark_node)
5196 return error_mark_node;
5198 /* [temp.arg.nontype] bullet 1 says the pointer to member
5199 expression must be a pointer-to-member constant. */
5200 if (!check_valid_ptrmem_cst_expr (type, expr))
5201 return error_mark_node;
5203 /* There is no way to disable standard conversions in
5204 resolve_address_of_overloaded_function (called by
5205 instantiate_type). It is possible that the call succeeded by
5206 converting &B::I to &D::I (where B is a base of D), so we need
5207 to reject this conversion here.
5209 Actually, even if there was a way to disable standard conversions,
5210 it would still be better to reject them here so that we can
5211 provide a superior diagnostic. */
5212 if (!same_type_p (TREE_TYPE (expr), type))
5214 error ("%qE is not a valid template argument for type %qT "
5215 "because it is of type %qT", expr, type,
5217 /* If we are just one standard conversion off, explain. */
5218 if (can_convert (type, TREE_TYPE (expr)))
5219 inform (input_location,
5220 "standard conversions are not allowed in this context");
5224 /* [temp.arg.nontype]/5, bullet 7
5226 For a non-type template-parameter of type pointer to data member,
5227 qualification conversions (_conv.qual_) are applied. */
5228 else if (TYPE_PTRMEM_P (type))
5230 /* [temp.arg.nontype] bullet 1 says the pointer to member
5231 expression must be a pointer-to-member constant. */
5232 if (!check_valid_ptrmem_cst_expr (type, expr))
5233 return error_mark_node;
5235 expr = perform_qualification_conversions (type, expr);
5236 if (expr == error_mark_node)
5239 /* A template non-type parameter must be one of the above. */
5243 /* Sanity check: did we actually convert the argument to the
5245 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
5249 /* Subroutine of coerce_template_template_parms, which returns 1 if
5250 PARM_PARM and ARG_PARM match using the rule for the template
5251 parameters of template template parameters. Both PARM and ARG are
5252 template parameters; the rest of the arguments are the same as for
5253 coerce_template_template_parms.
5256 coerce_template_template_parm (tree parm,
5258 tsubst_flags_t complain,
5262 if (arg == NULL_TREE || arg == error_mark_node
5263 || parm == NULL_TREE || parm == error_mark_node)
5266 if (TREE_CODE (arg) != TREE_CODE (parm))
5269 switch (TREE_CODE (parm))
5272 /* We encounter instantiations of templates like
5273 template <template <template <class> class> class TT>
5276 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5277 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5279 if (!coerce_template_template_parms
5280 (parmparm, argparm, complain, in_decl, outer_args))
5286 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5287 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5288 /* Argument is a parameter pack but parameter is not. */
5293 /* The tsubst call is used to handle cases such as
5295 template <int> class C {};
5296 template <class T, template <T> class TT> class D {};
5299 i.e. the parameter list of TT depends on earlier parameters. */
5300 if (!uses_template_parms (TREE_TYPE (arg))
5302 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5306 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5307 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5308 /* Argument is a parameter pack but parameter is not. */
5321 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5322 template template parameters. Both PARM_PARMS and ARG_PARMS are
5323 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5326 Consider the example:
5327 template <class T> class A;
5328 template<template <class U> class TT> class B;
5330 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5331 the parameters to A, and OUTER_ARGS contains A. */
5334 coerce_template_template_parms (tree parm_parms,
5336 tsubst_flags_t complain,
5340 int nparms, nargs, i;
5344 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5345 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5347 nparms = TREE_VEC_LENGTH (parm_parms);
5348 nargs = TREE_VEC_LENGTH (arg_parms);
5350 /* Determine whether we have a parameter pack at the end of the
5351 template template parameter's template parameter list. */
5352 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5354 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5356 if (parm == error_mark_node)
5359 switch (TREE_CODE (parm))
5363 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5368 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5378 && !(variadic_p && nargs >= nparms - 1))
5381 /* Check all of the template parameters except the parameter pack at
5382 the end (if any). */
5383 for (i = 0; i < nparms - variadic_p; ++i)
5385 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5386 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5389 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5390 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5392 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5400 /* Check each of the template parameters in the template
5401 argument against the template parameter pack at the end of
5402 the template template parameter. */
5403 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
5406 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5408 for (; i < nargs; ++i)
5410 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5413 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5415 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5424 /* Verifies that the deduced template arguments (in TARGS) for the
5425 template template parameters (in TPARMS) represent valid bindings,
5426 by comparing the template parameter list of each template argument
5427 to the template parameter list of its corresponding template
5428 template parameter, in accordance with DR150. This
5429 routine can only be called after all template arguments have been
5430 deduced. It will return TRUE if all of the template template
5431 parameter bindings are okay, FALSE otherwise. */
5433 template_template_parm_bindings_ok_p (tree tparms, tree targs)
5435 int i, ntparms = TREE_VEC_LENGTH (tparms);
5438 /* We're dealing with template parms in this process. */
5439 ++processing_template_decl;
5441 targs = INNERMOST_TEMPLATE_ARGS (targs);
5443 for (i = 0; i < ntparms; ++i)
5445 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
5446 tree targ = TREE_VEC_ELT (targs, i);
5448 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
5450 tree packed_args = NULL_TREE;
5453 if (ARGUMENT_PACK_P (targ))
5455 /* Look inside the argument pack. */
5456 packed_args = ARGUMENT_PACK_ARGS (targ);
5457 len = TREE_VEC_LENGTH (packed_args);
5460 for (idx = 0; idx < len; ++idx)
5462 tree targ_parms = NULL_TREE;
5465 /* Extract the next argument from the argument
5467 targ = TREE_VEC_ELT (packed_args, idx);
5469 if (PACK_EXPANSION_P (targ))
5470 /* Look at the pattern of the pack expansion. */
5471 targ = PACK_EXPANSION_PATTERN (targ);
5473 /* Extract the template parameters from the template
5475 if (TREE_CODE (targ) == TEMPLATE_DECL)
5476 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
5477 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
5478 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
5480 /* Verify that we can coerce the template template
5481 parameters from the template argument to the template
5482 parameter. This requires an exact match. */
5484 && !coerce_template_template_parms
5485 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
5500 --processing_template_decl;
5504 /* Convert the indicated template ARG as necessary to match the
5505 indicated template PARM. Returns the converted ARG, or
5506 error_mark_node if the conversion was unsuccessful. Error and
5507 warning messages are issued under control of COMPLAIN. This
5508 conversion is for the Ith parameter in the parameter list. ARGS is
5509 the full set of template arguments deduced so far. */
5512 convert_template_argument (tree parm,
5515 tsubst_flags_t complain,
5521 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
5523 if (TREE_CODE (arg) == TREE_LIST
5524 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
5526 /* The template argument was the name of some
5527 member function. That's usually
5528 invalid, but static members are OK. In any
5529 case, grab the underlying fields/functions
5530 and issue an error later if required. */
5531 orig_arg = TREE_VALUE (arg);
5532 TREE_TYPE (arg) = unknown_type_node;
5537 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
5538 requires_type = (TREE_CODE (parm) == TYPE_DECL
5539 || requires_tmpl_type);
5541 /* When determining whether an argument pack expansion is a template,
5542 look at the pattern. */
5543 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
5544 arg = PACK_EXPANSION_PATTERN (arg);
5546 /* Deal with an injected-class-name used as a template template arg. */
5547 if (requires_tmpl_type && CLASS_TYPE_P (arg))
5549 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
5550 if (TREE_CODE (t) == TEMPLATE_DECL)
5552 if (complain & tf_warning_or_error)
5553 pedwarn (input_location, OPT_pedantic, "injected-class-name %qD"
5554 " used as template template argument", TYPE_NAME (arg));
5555 else if (flag_pedantic_errors)
5563 ((TREE_CODE (arg) == TEMPLATE_DECL
5564 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
5565 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5566 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
5569 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5570 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
5571 arg = TYPE_STUB_DECL (arg);
5573 is_type = TYPE_P (arg) || is_tmpl_type;
5575 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
5576 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
5578 permerror (input_location, "to refer to a type member of a template parameter, "
5579 "use %<typename %E%>", orig_arg);
5581 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
5582 TREE_OPERAND (arg, 1),
5584 complain & tf_error);
5588 if (is_type != requires_type)
5592 if (complain & tf_error)
5594 error ("type/value mismatch at argument %d in template "
5595 "parameter list for %qD",
5598 error (" expected a constant of type %qT, got %qT",
5600 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
5601 else if (requires_tmpl_type)
5602 error (" expected a class template, got %qE", orig_arg);
5604 error (" expected a type, got %qE", orig_arg);
5607 return error_mark_node;
5609 if (is_tmpl_type ^ requires_tmpl_type)
5611 if (in_decl && (complain & tf_error))
5613 error ("type/value mismatch at argument %d in template "
5614 "parameter list for %qD",
5617 error (" expected a type, got %qT", DECL_NAME (arg));
5619 error (" expected a class template, got %qT", orig_arg);
5621 return error_mark_node;
5626 if (requires_tmpl_type)
5628 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
5629 /* The number of argument required is not known yet.
5630 Just accept it for now. */
5631 val = TREE_TYPE (arg);
5634 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5637 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5639 if (coerce_template_template_parms (parmparm, argparm,
5645 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5647 if (val != error_mark_node)
5649 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
5650 val = TREE_TYPE (val);
5651 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
5652 val = make_pack_expansion (val);
5657 if (in_decl && (complain & tf_error))
5659 error ("type/value mismatch at argument %d in "
5660 "template parameter list for %qD",
5662 error (" expected a template of type %qD, got %qT",
5666 val = error_mark_node;
5672 /* We only form one instance of each template specialization.
5673 Therefore, if we use a non-canonical variant (i.e., a
5674 typedef), any future messages referring to the type will use
5675 the typedef, which is confusing if those future uses do not
5676 themselves also use the typedef. */
5678 val = strip_typedefs (val);
5682 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
5684 if (invalid_nontype_parm_type_p (t, complain))
5685 return error_mark_node;
5687 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
5689 if (same_type_p (t, TREE_TYPE (orig_arg)))
5693 /* Not sure if this is reachable, but it doesn't hurt
5695 error ("type mismatch in nontype parameter pack");
5696 val = error_mark_node;
5699 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
5700 /* We used to call digest_init here. However, digest_init
5701 will report errors, which we don't want when complain
5702 is zero. More importantly, digest_init will try too
5703 hard to convert things: for example, `0' should not be
5704 converted to pointer type at this point according to
5705 the standard. Accepting this is not merely an
5706 extension, since deciding whether or not these
5707 conversions can occur is part of determining which
5708 function template to call, or whether a given explicit
5709 argument specification is valid. */
5710 val = convert_nontype_argument (t, orig_arg);
5714 if (val == NULL_TREE)
5715 val = error_mark_node;
5716 else if (val == error_mark_node && (complain & tf_error))
5717 error ("could not convert template argument %qE to %qT", orig_arg, t);
5719 if (TREE_CODE (val) == SCOPE_REF)
5721 /* Strip typedefs from the SCOPE_REF. */
5722 tree type = strip_typedefs (TREE_TYPE (val));
5723 tree scope = strip_typedefs (TREE_OPERAND (val, 0));
5724 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
5725 QUALIFIED_NAME_IS_TEMPLATE (val));
5732 /* Coerces the remaining template arguments in INNER_ARGS (from
5733 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5734 Returns the coerced argument pack. PARM_IDX is the position of this
5735 parameter in the template parameter list. ARGS is the original
5736 template argument list. */
5738 coerce_template_parameter_pack (tree parms,
5746 tsubst_flags_t complain)
5748 tree parm = TREE_VEC_ELT (parms, parm_idx);
5749 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5752 tree packed_types = NULL_TREE;
5754 if (arg_idx > nargs)
5757 packed_args = make_tree_vec (nargs - arg_idx);
5759 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
5760 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
5762 /* When the template parameter is a non-type template
5763 parameter pack whose type uses parameter packs, we need
5764 to look at each of the template arguments
5765 separately. Build a vector of the types for these
5766 non-type template parameters in PACKED_TYPES. */
5768 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
5769 packed_types = tsubst_pack_expansion (expansion, args,
5772 if (packed_types == error_mark_node)
5773 return error_mark_node;
5775 /* Check that we have the right number of arguments. */
5777 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
5778 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
5781 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
5782 error ("wrong number of template arguments (%d, should be %d)",
5783 nargs, needed_parms);
5784 return error_mark_node;
5787 /* If we aren't able to check the actual arguments now
5788 (because they haven't been expanded yet), we can at least
5789 verify that all of the types used for the non-type
5790 template parameter pack are, in fact, valid for non-type
5791 template parameters. */
5793 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
5795 int j, len = TREE_VEC_LENGTH (packed_types);
5796 for (j = 0; j < len; ++j)
5798 tree t = TREE_VEC_ELT (packed_types, j);
5799 if (invalid_nontype_parm_type_p (t, complain))
5800 return error_mark_node;
5805 /* Convert the remaining arguments, which will be a part of the
5806 parameter pack "parm". */
5807 for (; arg_idx < nargs; ++arg_idx)
5809 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
5810 tree actual_parm = TREE_VALUE (parm);
5812 if (packed_types && !PACK_EXPANSION_P (arg))
5814 /* When we have a vector of types (corresponding to the
5815 non-type template parameter pack that uses parameter
5816 packs in its type, as mention above), and the
5817 argument is not an expansion (which expands to a
5818 currently unknown number of arguments), clone the
5819 parm and give it the next type in PACKED_TYPES. */
5820 actual_parm = copy_node (actual_parm);
5821 TREE_TYPE (actual_parm) =
5822 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
5825 if (arg != error_mark_node)
5826 arg = convert_template_argument (actual_parm,
5827 arg, new_args, complain, parm_idx,
5829 if (arg == error_mark_node)
5831 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
5834 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
5835 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
5836 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
5839 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
5840 TREE_TYPE (argument_pack)
5841 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
5842 TREE_CONSTANT (argument_pack) = 1;
5845 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
5846 #ifdef ENABLE_CHECKING
5847 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
5848 TREE_VEC_LENGTH (packed_args));
5850 return argument_pack;
5853 /* Convert all template arguments to their appropriate types, and
5854 return a vector containing the innermost resulting template
5855 arguments. If any error occurs, return error_mark_node. Error and
5856 warning messages are issued under control of COMPLAIN.
5858 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5859 for arguments not specified in ARGS. Otherwise, if
5860 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5861 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5862 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5866 coerce_template_parms (tree parms,
5869 tsubst_flags_t complain,
5870 bool require_all_args,
5871 bool use_default_args)
5873 int nparms, nargs, parm_idx, arg_idx, lost = 0;
5876 tree new_inner_args;
5877 int saved_unevaluated_operand;
5878 int saved_inhibit_evaluation_warnings;
5880 /* When used as a boolean value, indicates whether this is a
5881 variadic template parameter list. Since it's an int, we can also
5882 subtract it from nparms to get the number of non-variadic
5886 nparms = TREE_VEC_LENGTH (parms);
5888 /* Determine if there are any parameter packs. */
5889 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
5891 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
5892 if (template_parameter_pack_p (tparm))
5896 inner_args = INNERMOST_TEMPLATE_ARGS (args);
5897 /* If there are 0 or 1 parameter packs, we need to expand any argument
5898 packs so that we can deduce a parameter pack from some non-packed args
5899 followed by an argument pack, as in variadic85.C. If there are more
5900 than that, we need to leave argument packs intact so the arguments are
5901 assigned to the right parameter packs. This should only happen when
5902 dealing with a nested class inside a partial specialization of a class
5903 template, as in variadic92.C. */
5904 if (variadic_p <= 1)
5905 inner_args = expand_template_argument_pack (inner_args);
5907 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5908 if ((nargs > nparms && !variadic_p)
5909 || (nargs < nparms - variadic_p
5911 && (!use_default_args
5912 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
5913 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
5915 if (complain & tf_error)
5917 const char *or_more = "";
5920 or_more = " or more";
5924 error ("wrong number of template arguments (%d, should be %d%s)",
5925 nargs, nparms, or_more);
5928 error ("provided for %q+D", in_decl);
5931 return error_mark_node;
5934 /* We need to evaluate the template arguments, even though this
5935 template-id may be nested within a "sizeof". */
5936 saved_unevaluated_operand = cp_unevaluated_operand;
5937 cp_unevaluated_operand = 0;
5938 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
5939 c_inhibit_evaluation_warnings = 0;
5940 new_inner_args = make_tree_vec (nparms);
5941 new_args = add_outermost_template_args (args, new_inner_args);
5942 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
5947 /* Get the Ith template parameter. */
5948 parm = TREE_VEC_ELT (parms, parm_idx);
5950 if (parm == error_mark_node)
5952 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
5956 /* Calculate the next argument. */
5957 if (arg_idx < nargs)
5958 arg = TREE_VEC_ELT (inner_args, arg_idx);
5962 if (template_parameter_pack_p (TREE_VALUE (parm))
5963 && !(arg && ARGUMENT_PACK_P (arg)))
5965 /* All remaining arguments will be placed in the
5966 template parameter pack PARM. */
5967 arg = coerce_template_parameter_pack (parms, parm_idx, args,
5968 inner_args, arg_idx,
5972 /* Store this argument. */
5973 if (arg == error_mark_node)
5975 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
5977 /* We are done with all of the arguments. */
5984 if (PACK_EXPANSION_P (arg))
5986 if (complain & tf_error)
5988 /* FIXME this restriction was removed by N2555; see
5990 /* If ARG is a pack expansion, but PARM is not a
5991 template parameter pack (if it were, we would have
5992 handled it above), we're trying to expand into a
5993 fixed-length argument list. */
5994 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5995 sorry ("cannot expand %<%E%> into a fixed-length "
5996 "argument list", arg);
5998 sorry ("cannot expand %<%T%> into a fixed-length "
5999 "argument list", arg);
6001 return error_mark_node;
6004 else if (require_all_args)
6006 /* There must be a default arg in this case. */
6007 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6009 /* The position of the first default template argument,
6010 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6012 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6013 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6018 if (arg == error_mark_node)
6020 if (complain & tf_error)
6021 error ("template argument %d is invalid", arg_idx + 1);
6024 /* This only occurs if there was an error in the template
6025 parameter list itself (which we would already have
6026 reported) that we are trying to recover from, e.g., a class
6027 template with a parameter list such as
6028 template<typename..., typename>. */
6029 return error_mark_node;
6031 arg = convert_template_argument (TREE_VALUE (parm),
6032 arg, new_args, complain,
6035 if (arg == error_mark_node)
6037 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6039 cp_unevaluated_operand = saved_unevaluated_operand;
6040 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6043 return error_mark_node;
6045 #ifdef ENABLE_CHECKING
6046 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6047 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6048 TREE_VEC_LENGTH (new_inner_args));
6051 return new_inner_args;
6054 /* Returns 1 if template args OT and NT are equivalent. */
6057 template_args_equal (tree ot, tree nt)
6062 if (TREE_CODE (nt) == TREE_VEC)
6063 /* For member templates */
6064 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6065 else if (PACK_EXPANSION_P (ot))
6066 return PACK_EXPANSION_P (nt)
6067 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6068 PACK_EXPANSION_PATTERN (nt));
6069 else if (ARGUMENT_PACK_P (ot))
6074 if (!ARGUMENT_PACK_P (nt))
6077 opack = ARGUMENT_PACK_ARGS (ot);
6078 npack = ARGUMENT_PACK_ARGS (nt);
6079 len = TREE_VEC_LENGTH (opack);
6080 if (TREE_VEC_LENGTH (npack) != len)
6082 for (i = 0; i < len; ++i)
6083 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6084 TREE_VEC_ELT (npack, i)))
6088 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6090 /* We get here probably because we are in the middle of substituting
6091 into the pattern of a pack expansion. In that case the
6092 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6093 interested in. So we want to use the initial pack argument for
6095 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6096 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6097 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6098 return template_args_equal (ot, nt);
6100 else if (TYPE_P (nt))
6101 return TYPE_P (ot) && same_type_p (ot, nt);
6102 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6105 return cp_tree_equal (ot, nt);
6108 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6109 of template arguments. Returns 0 otherwise. */
6112 comp_template_args (tree oldargs, tree newargs)
6116 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6119 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6121 tree nt = TREE_VEC_ELT (newargs, i);
6122 tree ot = TREE_VEC_ELT (oldargs, i);
6124 if (! template_args_equal (ot, nt))
6131 add_pending_template (tree d)
6133 tree ti = (TYPE_P (d)
6134 ? CLASSTYPE_TEMPLATE_INFO (d)
6135 : DECL_TEMPLATE_INFO (d));
6136 struct pending_template *pt;
6139 if (TI_PENDING_TEMPLATE_FLAG (ti))
6142 /* We are called both from instantiate_decl, where we've already had a
6143 tinst_level pushed, and instantiate_template, where we haven't.
6145 level = !current_tinst_level || current_tinst_level->decl != d;
6148 push_tinst_level (d);
6150 pt = GGC_NEW (struct pending_template);
6152 pt->tinst = current_tinst_level;
6153 if (last_pending_template)
6154 last_pending_template->next = pt;
6156 pending_templates = pt;
6158 last_pending_template = pt;
6160 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6167 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6168 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6169 documentation for TEMPLATE_ID_EXPR. */
6172 lookup_template_function (tree fns, tree arglist)
6176 if (fns == error_mark_node || arglist == error_mark_node)
6177 return error_mark_node;
6179 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6180 gcc_assert (fns && (is_overloaded_fn (fns)
6181 || TREE_CODE (fns) == IDENTIFIER_NODE));
6183 if (BASELINK_P (fns))
6185 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6187 BASELINK_FUNCTIONS (fns),
6192 type = TREE_TYPE (fns);
6193 if (TREE_CODE (fns) == OVERLOAD || !type)
6194 type = unknown_type_node;
6196 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6199 /* Within the scope of a template class S<T>, the name S gets bound
6200 (in build_self_reference) to a TYPE_DECL for the class, not a
6201 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6202 or one of its enclosing classes, and that type is a template,
6203 return the associated TEMPLATE_DECL. Otherwise, the original
6206 Also handle the case when DECL is a TREE_LIST of ambiguous
6207 injected-class-names from different bases. */
6210 maybe_get_template_decl_from_type_decl (tree decl)
6212 if (decl == NULL_TREE)
6215 /* DR 176: A lookup that finds an injected-class-name (10.2
6216 [class.member.lookup]) can result in an ambiguity in certain cases
6217 (for example, if it is found in more than one base class). If all of
6218 the injected-class-names that are found refer to specializations of
6219 the same class template, and if the name is followed by a
6220 template-argument-list, the reference refers to the class template
6221 itself and not a specialization thereof, and is not ambiguous. */
6222 if (TREE_CODE (decl) == TREE_LIST)
6224 tree t, tmpl = NULL_TREE;
6225 for (t = decl; t; t = TREE_CHAIN (t))
6227 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6230 else if (tmpl != elt)
6233 if (tmpl && t == NULL_TREE)
6239 return (decl != NULL_TREE
6240 && DECL_SELF_REFERENCE_P (decl)
6241 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6242 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
6245 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6246 parameters, find the desired type.
6248 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6250 IN_DECL, if non-NULL, is the template declaration we are trying to
6253 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6254 the class we are looking up.
6256 Issue error and warning messages under control of COMPLAIN.
6258 If the template class is really a local class in a template
6259 function, then the FUNCTION_CONTEXT is the function in which it is
6262 ??? Note that this function is currently called *twice* for each
6263 template-id: the first time from the parser, while creating the
6264 incomplete type (finish_template_type), and the second type during the
6265 real instantiation (instantiate_template_class). This is surely something
6266 that we want to avoid. It also causes some problems with argument
6267 coercion (see convert_nontype_argument for more information on this). */
6270 lookup_template_class (tree d1,
6275 tsubst_flags_t complain)
6277 tree templ = NULL_TREE, parmlist;
6284 timevar_push (TV_NAME_LOOKUP);
6286 if (TREE_CODE (d1) == IDENTIFIER_NODE)
6288 tree value = innermost_non_namespace_value (d1);
6289 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
6294 push_decl_namespace (context);
6295 templ = lookup_name (d1);
6296 templ = maybe_get_template_decl_from_type_decl (templ);
6298 pop_decl_namespace ();
6301 context = DECL_CONTEXT (templ);
6303 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
6305 tree type = TREE_TYPE (d1);
6307 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6308 an implicit typename for the second A. Deal with it. */
6309 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
6310 type = TREE_TYPE (type);
6312 if (CLASSTYPE_TEMPLATE_INFO (type))
6314 templ = CLASSTYPE_TI_TEMPLATE (type);
6315 d1 = DECL_NAME (templ);
6318 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6319 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
6321 templ = TYPE_TI_TEMPLATE (d1);
6322 d1 = DECL_NAME (templ);
6324 else if (TREE_CODE (d1) == TEMPLATE_DECL
6325 && DECL_TEMPLATE_RESULT (d1)
6326 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
6329 d1 = DECL_NAME (templ);
6330 context = DECL_CONTEXT (templ);
6333 /* Issue an error message if we didn't find a template. */
6336 if (complain & tf_error)
6337 error ("%qT is not a template", d1);
6338 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6341 if (TREE_CODE (templ) != TEMPLATE_DECL
6342 /* Make sure it's a user visible template, if it was named by
6344 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
6345 && !PRIMARY_TEMPLATE_P (templ)))
6347 if (complain & tf_error)
6349 error ("non-template type %qT used as a template", d1);
6351 error ("for template declaration %q+D", in_decl);
6353 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6356 complain &= ~tf_user;
6358 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
6360 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6361 template arguments */
6367 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6369 /* Consider an example where a template template parameter declared as
6371 template <class T, class U = std::allocator<T> > class TT
6373 The template parameter level of T and U are one level larger than
6374 of TT. To proper process the default argument of U, say when an
6375 instantiation `TT<int>' is seen, we need to build the full
6376 arguments containing {int} as the innermost level. Outer levels,
6377 available when not appearing as default template argument, can be
6378 obtained from the arguments of the enclosing template.
6380 Suppose that TT is later substituted with std::vector. The above
6381 instantiation is `TT<int, std::allocator<T> >' with TT at
6382 level 1, and T at level 2, while the template arguments at level 1
6383 becomes {std::vector} and the inner level 2 is {int}. */
6385 outer = DECL_CONTEXT (templ);
6387 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6388 else if (current_template_parms)
6389 /* This is an argument of the current template, so we haven't set
6390 DECL_CONTEXT yet. */
6391 outer = current_template_args ();
6394 arglist = add_to_template_args (outer, arglist);
6396 arglist2 = coerce_template_parms (parmlist, arglist, templ,
6398 /*require_all_args=*/true,
6399 /*use_default_args=*/true);
6400 if (arglist2 == error_mark_node
6401 || (!uses_template_parms (arglist2)
6402 && check_instantiated_args (templ, arglist2, complain)))
6403 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6405 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
6406 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
6410 tree template_type = TREE_TYPE (templ);
6413 tree found = NULL_TREE;
6416 int is_dependent_type;
6417 int use_partial_inst_tmpl = false;
6419 gen_tmpl = most_general_template (templ);
6420 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
6421 parm_depth = TMPL_PARMS_DEPTH (parmlist);
6422 arg_depth = TMPL_ARGS_DEPTH (arglist);
6424 if (arg_depth == 1 && parm_depth > 1)
6426 /* We've been given an incomplete set of template arguments.
6429 template <class T> struct S1 {
6430 template <class U> struct S2 {};
6431 template <class U> struct S2<U*> {};
6434 we will be called with an ARGLIST of `U*', but the
6435 TEMPLATE will be `template <class T> template
6436 <class U> struct S1<T>::S2'. We must fill in the missing
6439 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
6441 arg_depth = TMPL_ARGS_DEPTH (arglist);
6444 /* Now we should have enough arguments. */
6445 gcc_assert (parm_depth == arg_depth);
6447 /* From here on, we're only interested in the most general
6450 /* Calculate the BOUND_ARGS. These will be the args that are
6451 actually tsubst'd into the definition to create the
6455 /* We have multiple levels of arguments to coerce, at once. */
6457 int saved_depth = TMPL_ARGS_DEPTH (arglist);
6459 tree bound_args = make_tree_vec (parm_depth);
6461 for (i = saved_depth,
6462 t = DECL_TEMPLATE_PARMS (gen_tmpl);
6463 i > 0 && t != NULL_TREE;
6464 --i, t = TREE_CHAIN (t))
6466 tree a = coerce_template_parms (TREE_VALUE (t),
6469 /*require_all_args=*/true,
6470 /*use_default_args=*/true);
6472 /* Don't process further if one of the levels fails. */
6473 if (a == error_mark_node)
6475 /* Restore the ARGLIST to its full size. */
6476 TREE_VEC_LENGTH (arglist) = saved_depth;
6477 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6480 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
6482 /* We temporarily reduce the length of the ARGLIST so
6483 that coerce_template_parms will see only the arguments
6484 corresponding to the template parameters it is
6486 TREE_VEC_LENGTH (arglist)--;
6489 /* Restore the ARGLIST to its full size. */
6490 TREE_VEC_LENGTH (arglist) = saved_depth;
6492 arglist = bound_args;
6496 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
6497 INNERMOST_TEMPLATE_ARGS (arglist),
6500 /*require_all_args=*/true,
6501 /*use_default_args=*/true);
6503 if (arglist == error_mark_node)
6504 /* We were unable to bind the arguments. */
6505 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6507 /* In the scope of a template class, explicit references to the
6508 template class refer to the type of the template, not any
6509 instantiation of it. For example, in:
6511 template <class T> class C { void f(C<T>); }
6513 the `C<T>' is just the same as `C'. Outside of the
6514 class, however, such a reference is an instantiation. */
6516 || !PRIMARY_TEMPLATE_P (gen_tmpl)
6517 || currently_open_class (template_type))
6518 /* comp_template_args is expensive, check it last. */
6519 && comp_template_args (TYPE_TI_ARGS (template_type),
6521 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type);
6523 /* If we already have this specialization, return it. */
6524 elt.tmpl = gen_tmpl;
6526 hash = hash_specialization (&elt);
6527 entry = (spec_entry *) htab_find_with_hash (type_specializations,
6531 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec);
6533 is_dependent_type = uses_template_parms (arglist);
6535 /* If the deduced arguments are invalid, then the binding
6537 if (!is_dependent_type
6538 && check_instantiated_args (gen_tmpl,
6539 INNERMOST_TEMPLATE_ARGS (arglist),
6541 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6543 if (!is_dependent_type
6544 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6545 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
6546 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
6548 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
6549 DECL_NAME (gen_tmpl),
6550 /*tag_scope=*/ts_global);
6551 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
6554 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
6557 context = global_namespace;
6559 /* Create the type. */
6560 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
6562 if (!is_dependent_type)
6564 set_current_access_from_decl (TYPE_NAME (template_type));
6565 t = start_enum (TYPE_IDENTIFIER (template_type),
6566 tsubst (ENUM_UNDERLYING_TYPE (template_type),
6567 arglist, complain, in_decl),
6568 SCOPED_ENUM_P (template_type));
6572 /* We don't want to call start_enum for this type, since
6573 the values for the enumeration constants may involve
6574 template parameters. And, no one should be interested
6575 in the enumeration constants for such a type. */
6576 t = cxx_make_type (ENUMERAL_TYPE);
6577 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
6582 t = make_class_type (TREE_CODE (template_type));
6583 CLASSTYPE_DECLARED_CLASS (t)
6584 = CLASSTYPE_DECLARED_CLASS (template_type);
6585 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
6586 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
6588 /* A local class. Make sure the decl gets registered properly. */
6589 if (context == current_function_decl)
6590 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
6592 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
6593 /* This instantiation is another name for the primary
6594 template type. Set the TYPE_CANONICAL field
6596 TYPE_CANONICAL (t) = template_type;
6597 else if (any_template_arguments_need_structural_equality_p (arglist))
6598 /* Some of the template arguments require structural
6599 equality testing, so this template class requires
6600 structural equality testing. */
6601 SET_TYPE_STRUCTURAL_EQUALITY (t);
6604 /* If we called start_enum or pushtag above, this information
6605 will already be set up. */
6608 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
6610 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
6611 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
6612 DECL_SOURCE_LOCATION (type_decl)
6613 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
6616 type_decl = TYPE_NAME (t);
6618 TREE_PRIVATE (type_decl)
6619 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
6620 TREE_PROTECTED (type_decl)
6621 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
6622 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
6624 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
6625 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
6628 /* Let's consider the explicit specialization of a member
6629 of a class template specialization that is implicitely instantiated,
6634 template<class U> struct M {}; //#0
6639 struct S<int>::M<char> //#1
6643 [temp.expl.spec]/4 says this is valid.
6645 In this case, when we write:
6648 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
6651 When we encounter #1, we want to store the partial instantiation
6652 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
6654 For all cases other than this "explicit specialization of member of a
6655 class template", we just want to store the most general template into
6656 the CLASSTYPE_TI_TEMPLATE of M.
6658 This case of "explicit specialization of member of a class template"
6660 1/ the enclosing class is an instantiation of, and therefore not
6661 the same as, the context of the most general template, and
6662 2/ we aren't looking at the partial instantiation itself, i.e.
6663 the innermost arguments are not the same as the innermost parms of
6664 the most general template.
6666 So it's only when 1/ and 2/ happens that we want to use the partial
6667 instantiation of the member template in lieu of its most general
6670 if (PRIMARY_TEMPLATE_P (gen_tmpl)
6671 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
6672 /* the enclosing class must be an instantiation... */
6673 && CLASS_TYPE_P (context)
6674 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
6676 tree partial_inst_args;
6677 TREE_VEC_LENGTH (arglist)--;
6678 ++processing_template_decl;
6680 tsubst (INNERMOST_TEMPLATE_ARGS
6681 (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
6682 arglist, complain, NULL_TREE);
6683 --processing_template_decl;
6684 TREE_VEC_LENGTH (arglist)++;
6685 use_partial_inst_tmpl =
6686 /*...and we must not be looking at the partial instantiation
6688 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
6692 if (!use_partial_inst_tmpl)
6693 /* This case is easy; there are no member templates involved. */
6697 /* This is a full instantiation of a member template. Find
6698 the partial instantiation of which this is an instance. */
6700 /* Temporarily reduce by one the number of levels in the ARGLIST
6701 so as to avoid comparing the last set of arguments. */
6702 TREE_VEC_LENGTH (arglist)--;
6703 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
6704 TREE_VEC_LENGTH (arglist)++;
6705 found = CLASSTYPE_TI_TEMPLATE (found);
6708 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
6711 slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
6712 &elt, hash, INSERT);
6713 *slot = GGC_NEW (spec_entry);
6716 /* Note this use of the partial instantiation so we can check it
6717 later in maybe_process_partial_specialization. */
6718 DECL_TEMPLATE_INSTANTIATIONS (templ)
6719 = tree_cons (arglist, t,
6720 DECL_TEMPLATE_INSTANTIATIONS (templ));
6722 if (TREE_CODE (t) == ENUMERAL_TYPE && !is_dependent_type)
6723 /* Now that the type has been registered on the instantiations
6724 list, we set up the enumerators. Because the enumeration
6725 constants may involve the enumeration type itself, we make
6726 sure to register the type first, and then create the
6727 constants. That way, doing tsubst_expr for the enumeration
6728 constants won't result in recursive calls here; we'll find
6729 the instantiation and exit above. */
6730 tsubst_enum (template_type, t, arglist);
6732 if (is_dependent_type)
6733 /* If the type makes use of template parameters, the
6734 code that generates debugging information will crash. */
6735 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
6737 /* Possibly limit visibility based on template args. */
6738 TREE_PUBLIC (type_decl) = 1;
6739 determine_visibility (type_decl);
6741 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6743 timevar_pop (TV_NAME_LOOKUP);
6750 /* True when we should also visit template parameters that occur in
6751 non-deduced contexts. */
6752 bool include_nondeduced_p;
6753 struct pointer_set_t *visited;
6756 /* Called from for_each_template_parm via walk_tree. */
6759 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
6762 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
6763 tree_fn_t fn = pfd->fn;
6764 void *data = pfd->data;
6767 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
6768 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
6769 pfd->include_nondeduced_p))
6770 return error_mark_node;
6772 switch (TREE_CODE (t))
6775 if (TYPE_PTRMEMFUNC_P (t))
6781 if (!TYPE_TEMPLATE_INFO (t))
6783 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
6784 fn, data, pfd->visited,
6785 pfd->include_nondeduced_p))
6786 return error_mark_node;
6790 if (for_each_template_parm (TYPE_MIN_VALUE (t),
6791 fn, data, pfd->visited,
6792 pfd->include_nondeduced_p)
6793 || for_each_template_parm (TYPE_MAX_VALUE (t),
6794 fn, data, pfd->visited,
6795 pfd->include_nondeduced_p))
6796 return error_mark_node;
6800 /* Since we're not going to walk subtrees, we have to do this
6802 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
6803 pfd->visited, pfd->include_nondeduced_p))
6804 return error_mark_node;
6808 /* Check the return type. */
6809 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6810 pfd->include_nondeduced_p))
6811 return error_mark_node;
6813 /* Check the parameter types. Since default arguments are not
6814 instantiated until they are needed, the TYPE_ARG_TYPES may
6815 contain expressions that involve template parameters. But,
6816 no-one should be looking at them yet. And, once they're
6817 instantiated, they don't contain template parameters, so
6818 there's no point in looking at them then, either. */
6822 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
6823 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
6824 pfd->visited, pfd->include_nondeduced_p))
6825 return error_mark_node;
6827 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6828 want walk_tree walking into them itself. */
6834 if (pfd->include_nondeduced_p
6835 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
6837 pfd->include_nondeduced_p))
6838 return error_mark_node;
6843 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
6844 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
6845 pfd->visited, pfd->include_nondeduced_p))
6846 return error_mark_node;
6851 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
6852 && for_each_template_parm (DECL_INITIAL (t), fn, data,
6853 pfd->visited, pfd->include_nondeduced_p))
6854 return error_mark_node;
6855 if (DECL_CONTEXT (t)
6856 && pfd->include_nondeduced_p
6857 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
6858 pfd->visited, pfd->include_nondeduced_p))
6859 return error_mark_node;
6862 case BOUND_TEMPLATE_TEMPLATE_PARM:
6863 /* Record template parameters such as `T' inside `TT<T>'. */
6864 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
6865 pfd->include_nondeduced_p))
6866 return error_mark_node;
6869 case TEMPLATE_TEMPLATE_PARM:
6870 case TEMPLATE_TYPE_PARM:
6871 case TEMPLATE_PARM_INDEX:
6872 if (fn && (*fn)(t, data))
6873 return error_mark_node;
6875 return error_mark_node;
6879 /* A template template parameter is encountered. */
6880 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
6881 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6882 pfd->include_nondeduced_p))
6883 return error_mark_node;
6885 /* Already substituted template template parameter */
6891 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
6893 pfd->include_nondeduced_p))
6894 return error_mark_node;
6898 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
6899 && pfd->include_nondeduced_p
6900 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
6901 (TREE_TYPE (t)), fn, data,
6902 pfd->visited, pfd->include_nondeduced_p))
6903 return error_mark_node;
6908 /* If there's no type, then this thing must be some expression
6909 involving template parameters. */
6910 if (!fn && !TREE_TYPE (t))
6911 return error_mark_node;
6916 case REINTERPRET_CAST_EXPR:
6917 case CONST_CAST_EXPR:
6918 case STATIC_CAST_EXPR:
6919 case DYNAMIC_CAST_EXPR:
6923 case PSEUDO_DTOR_EXPR:
6925 return error_mark_node;
6932 /* We didn't find any template parameters we liked. */
6936 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
6937 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
6938 call FN with the parameter and the DATA.
6939 If FN returns nonzero, the iteration is terminated, and
6940 for_each_template_parm returns 1. Otherwise, the iteration
6941 continues. If FN never returns a nonzero value, the value
6942 returned by for_each_template_parm is 0. If FN is NULL, it is
6943 considered to be the function which always returns 1.
6945 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
6946 parameters that occur in non-deduced contexts. When false, only
6947 visits those template parameters that can be deduced. */
6950 for_each_template_parm (tree t, tree_fn_t fn, void* data,
6951 struct pointer_set_t *visited,
6952 bool include_nondeduced_p)
6954 struct pair_fn_data pfd;
6960 pfd.include_nondeduced_p = include_nondeduced_p;
6962 /* Walk the tree. (Conceptually, we would like to walk without
6963 duplicates, but for_each_template_parm_r recursively calls
6964 for_each_template_parm, so we would need to reorganize a fair
6965 bit to use walk_tree_without_duplicates, so we keep our own
6968 pfd.visited = visited;
6970 pfd.visited = pointer_set_create ();
6971 result = cp_walk_tree (&t,
6972 for_each_template_parm_r,
6974 pfd.visited) != NULL_TREE;
6979 pointer_set_destroy (pfd.visited);
6986 /* Returns true if T depends on any template parameter. */
6989 uses_template_parms (tree t)
6992 int saved_processing_template_decl;
6994 saved_processing_template_decl = processing_template_decl;
6995 if (!saved_processing_template_decl)
6996 processing_template_decl = 1;
6998 dependent_p = dependent_type_p (t);
6999 else if (TREE_CODE (t) == TREE_VEC)
7000 dependent_p = any_dependent_template_arguments_p (t);
7001 else if (TREE_CODE (t) == TREE_LIST)
7002 dependent_p = (uses_template_parms (TREE_VALUE (t))
7003 || uses_template_parms (TREE_CHAIN (t)));
7004 else if (TREE_CODE (t) == TYPE_DECL)
7005 dependent_p = dependent_type_p (TREE_TYPE (t));
7008 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7009 || TREE_CODE (t) == OVERLOAD
7010 || TREE_CODE (t) == BASELINK
7011 || TREE_CODE (t) == IDENTIFIER_NODE
7012 || TREE_CODE (t) == TRAIT_EXPR
7013 || TREE_CODE (t) == CONSTRUCTOR
7014 || CONSTANT_CLASS_P (t))
7015 dependent_p = (type_dependent_expression_p (t)
7016 || value_dependent_expression_p (t));
7019 gcc_assert (t == error_mark_node);
7020 dependent_p = false;
7023 processing_template_decl = saved_processing_template_decl;
7028 /* Returns true if T depends on any template parameter with level LEVEL. */
7031 uses_template_parms_level (tree t, int level)
7033 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
7034 /*include_nondeduced_p=*/true);
7037 static int tinst_depth;
7038 extern int max_tinst_depth;
7039 #ifdef GATHER_STATISTICS
7042 static int tinst_level_tick;
7043 static int last_template_error_tick;
7045 /* We're starting to instantiate D; record the template instantiation context
7046 for diagnostics and to restore it later. */
7049 push_tinst_level (tree d)
7051 struct tinst_level *new_level;
7053 if (tinst_depth >= max_tinst_depth)
7055 /* If the instantiation in question still has unbound template parms,
7056 we don't really care if we can't instantiate it, so just return.
7057 This happens with base instantiation for implicit `typename'. */
7058 if (uses_template_parms (d))
7061 last_template_error_tick = tinst_level_tick;
7062 error ("template instantiation depth exceeds maximum of %d (use "
7063 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7064 max_tinst_depth, d);
7066 print_instantiation_context ();
7071 new_level = GGC_NEW (struct tinst_level);
7072 new_level->decl = d;
7073 new_level->locus = input_location;
7074 new_level->in_system_header_p = in_system_header;
7075 new_level->next = current_tinst_level;
7076 current_tinst_level = new_level;
7079 #ifdef GATHER_STATISTICS
7080 if (tinst_depth > depth_reached)
7081 depth_reached = tinst_depth;
7088 /* We're done instantiating this template; return to the instantiation
7092 pop_tinst_level (void)
7094 /* Restore the filename and line number stashed away when we started
7095 this instantiation. */
7096 input_location = current_tinst_level->locus;
7097 current_tinst_level = current_tinst_level->next;
7102 /* We're instantiating a deferred template; restore the template
7103 instantiation context in which the instantiation was requested, which
7104 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7107 reopen_tinst_level (struct tinst_level *level)
7109 struct tinst_level *t;
7112 for (t = level; t; t = t->next)
7115 current_tinst_level = level;
7120 /* Returns the TINST_LEVEL which gives the original instantiation
7123 struct tinst_level *
7124 outermost_tinst_level (void)
7126 struct tinst_level *level = current_tinst_level;
7129 level = level->next;
7133 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
7136 parameter_of_template_p (tree parm, tree templ)
7141 if (!parm || !templ)
7144 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
7145 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7147 parms = DECL_TEMPLATE_PARMS (templ);
7148 parms = INNERMOST_TEMPLATE_PARMS (parms);
7150 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
7151 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
7157 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7158 vector of template arguments, as for tsubst.
7160 Returns an appropriate tsubst'd friend declaration. */
7163 tsubst_friend_function (tree decl, tree args)
7167 if (TREE_CODE (decl) == FUNCTION_DECL
7168 && DECL_TEMPLATE_INSTANTIATION (decl)
7169 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
7170 /* This was a friend declared with an explicit template
7171 argument list, e.g.:
7175 to indicate that f was a template instantiation, not a new
7176 function declaration. Now, we have to figure out what
7177 instantiation of what template. */
7179 tree template_id, arglist, fns;
7182 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
7184 /* Friend functions are looked up in the containing namespace scope.
7185 We must enter that scope, to avoid finding member functions of the
7186 current class with same name. */
7187 push_nested_namespace (ns);
7188 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
7189 tf_warning_or_error, NULL_TREE,
7190 /*integral_constant_expression_p=*/false);
7191 pop_nested_namespace (ns);
7192 arglist = tsubst (DECL_TI_ARGS (decl), args,
7193 tf_warning_or_error, NULL_TREE);
7194 template_id = lookup_template_function (fns, arglist);
7196 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7197 tmpl = determine_specialization (template_id, new_friend,
7199 /*need_member_template=*/0,
7200 TREE_VEC_LENGTH (args),
7202 return instantiate_template (tmpl, new_args, tf_error);
7205 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7207 /* The NEW_FRIEND will look like an instantiation, to the
7208 compiler, but is not an instantiation from the point of view of
7209 the language. For example, we might have had:
7211 template <class T> struct S {
7212 template <class U> friend void f(T, U);
7215 Then, in S<int>, template <class U> void f(int, U) is not an
7216 instantiation of anything. */
7217 if (new_friend == error_mark_node)
7218 return error_mark_node;
7220 DECL_USE_TEMPLATE (new_friend) = 0;
7221 if (TREE_CODE (decl) == TEMPLATE_DECL)
7223 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
7224 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
7225 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
7228 /* The mangled name for the NEW_FRIEND is incorrect. The function
7229 is not a template instantiation and should not be mangled like
7230 one. Therefore, we forget the mangling here; we'll recompute it
7231 later if we need it. */
7232 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
7234 SET_DECL_RTL (new_friend, NULL_RTX);
7235 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
7238 if (DECL_NAMESPACE_SCOPE_P (new_friend))
7241 tree new_friend_template_info;
7242 tree new_friend_result_template_info;
7244 int new_friend_is_defn;
7246 /* We must save some information from NEW_FRIEND before calling
7247 duplicate decls since that function will free NEW_FRIEND if
7249 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
7250 new_friend_is_defn =
7251 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7252 (template_for_substitution (new_friend)))
7254 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
7256 /* This declaration is a `primary' template. */
7257 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
7259 new_friend_result_template_info
7260 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
7263 new_friend_result_template_info = NULL_TREE;
7265 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7266 if (new_friend_is_defn)
7267 DECL_INITIAL (new_friend) = error_mark_node;
7269 /* Inside pushdecl_namespace_level, we will push into the
7270 current namespace. However, the friend function should go
7271 into the namespace of the template. */
7272 ns = decl_namespace_context (new_friend);
7273 push_nested_namespace (ns);
7274 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
7275 pop_nested_namespace (ns);
7277 if (old_decl == error_mark_node)
7278 return error_mark_node;
7280 if (old_decl != new_friend)
7282 /* This new friend declaration matched an existing
7283 declaration. For example, given:
7285 template <class T> void f(T);
7286 template <class U> class C {
7287 template <class T> friend void f(T) {}
7290 the friend declaration actually provides the definition
7291 of `f', once C has been instantiated for some type. So,
7292 old_decl will be the out-of-class template declaration,
7293 while new_friend is the in-class definition.
7295 But, if `f' was called before this point, the
7296 instantiation of `f' will have DECL_TI_ARGS corresponding
7297 to `T' but not to `U', references to which might appear
7298 in the definition of `f'. Previously, the most general
7299 template for an instantiation of `f' was the out-of-class
7300 version; now it is the in-class version. Therefore, we
7301 run through all specialization of `f', adding to their
7302 DECL_TI_ARGS appropriately. In particular, they need a
7303 new set of outer arguments, corresponding to the
7304 arguments for this class instantiation.
7306 The same situation can arise with something like this:
7309 template <class T> class C {
7313 when `C<int>' is instantiated. Now, `f(int)' is defined
7316 if (!new_friend_is_defn)
7317 /* On the other hand, if the in-class declaration does
7318 *not* provide a definition, then we don't want to alter
7319 existing definitions. We can just leave everything
7324 tree new_template = TI_TEMPLATE (new_friend_template_info);
7325 tree new_args = TI_ARGS (new_friend_template_info);
7327 /* Overwrite whatever template info was there before, if
7328 any, with the new template information pertaining to
7330 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
7332 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
7333 /* We should have called reregister_specialization in
7335 gcc_assert (retrieve_specialization (new_template,
7342 /* Indicate that the old function template is a partial
7344 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
7345 = new_friend_result_template_info;
7347 gcc_assert (new_template
7348 == most_general_template (new_template));
7349 gcc_assert (new_template != old_decl);
7351 /* Reassign any specializations already in the hash table
7352 to the new more general template, and add the
7353 additional template args. */
7354 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
7358 tree spec = TREE_VALUE (t);
7361 elt.tmpl = old_decl;
7362 elt.args = DECL_TI_ARGS (spec);
7363 elt.spec = NULL_TREE;
7365 htab_remove_elt (decl_specializations, &elt);
7368 = add_outermost_template_args (new_args,
7369 DECL_TI_ARGS (spec));
7371 register_specialization
7372 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
7375 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
7379 /* The information from NEW_FRIEND has been merged into OLD_DECL
7380 by duplicate_decls. */
7381 new_friend = old_decl;
7386 tree context = DECL_CONTEXT (new_friend);
7390 template <class T> class C {
7391 template <class U> friend void C1<U>::f (); // case 1
7392 friend void C2<T>::f (); // case 2
7394 we only need to make sure CONTEXT is a complete type for
7395 case 2. To distinguish between the two cases, we note that
7396 CONTEXT of case 1 remains dependent type after tsubst while
7397 this isn't true for case 2. */
7398 ++processing_template_decl;
7399 dependent_p = dependent_type_p (context);
7400 --processing_template_decl;
7403 && !complete_type_or_else (context, NULL_TREE))
7404 return error_mark_node;
7406 if (COMPLETE_TYPE_P (context))
7408 /* Check to see that the declaration is really present, and,
7409 possibly obtain an improved declaration. */
7410 tree fn = check_classfn (context,
7411 new_friend, NULL_TREE);
7421 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7422 template arguments, as for tsubst.
7424 Returns an appropriate tsubst'd friend type or error_mark_node on
7428 tsubst_friend_class (tree friend_tmpl, tree args)
7434 context = DECL_CONTEXT (friend_tmpl);
7438 if (TREE_CODE (context) == NAMESPACE_DECL)
7439 push_nested_namespace (context);
7441 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
7444 /* Look for a class template declaration. We look for hidden names
7445 because two friend declarations of the same template are the
7446 same. For example, in:
7449 template <typename> friend class F;
7451 template <typename> struct B {
7452 template <typename> friend class F;
7455 both F templates are the same. */
7456 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
7457 /*block_p=*/true, 0,
7458 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
7460 /* But, if we don't find one, it might be because we're in a
7461 situation like this:
7469 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7470 for `S<int>', not the TEMPLATE_DECL. */
7471 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
7473 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
7474 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
7477 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
7479 /* The friend template has already been declared. Just
7480 check to see that the declarations match, and install any new
7481 default parameters. We must tsubst the default parameters,
7482 of course. We only need the innermost template parameters
7483 because that is all that redeclare_class_template will look
7485 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
7486 > TMPL_ARGS_DEPTH (args))
7489 location_t saved_input_location;
7490 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
7491 args, tf_warning_or_error);
7493 saved_input_location = input_location;
7494 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
7495 redeclare_class_template (TREE_TYPE (tmpl), parms);
7496 input_location = saved_input_location;
7500 friend_type = TREE_TYPE (tmpl);
7504 /* The friend template has not already been declared. In this
7505 case, the instantiation of the template class will cause the
7506 injection of this template into the global scope. */
7507 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
7508 if (tmpl == error_mark_node)
7509 return error_mark_node;
7511 /* The new TMPL is not an instantiation of anything, so we
7512 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7513 the new type because that is supposed to be the corresponding
7514 template decl, i.e., TMPL. */
7515 DECL_USE_TEMPLATE (tmpl) = 0;
7516 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
7517 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
7518 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
7519 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
7521 /* Inject this template into the global scope. */
7522 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
7527 if (TREE_CODE (context) == NAMESPACE_DECL)
7528 pop_nested_namespace (context);
7530 pop_nested_class ();
7536 /* Returns zero if TYPE cannot be completed later due to circularity.
7537 Otherwise returns one. */
7540 can_complete_type_without_circularity (tree type)
7542 if (type == NULL_TREE || type == error_mark_node)
7544 else if (COMPLETE_TYPE_P (type))
7546 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
7547 return can_complete_type_without_circularity (TREE_TYPE (type));
7548 else if (CLASS_TYPE_P (type)
7549 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
7555 /* Apply any attributes which had to be deferred until instantiation
7556 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7557 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7560 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
7561 tree args, tsubst_flags_t complain, tree in_decl)
7563 tree last_dep = NULL_TREE;
7567 for (t = attributes; t; t = TREE_CHAIN (t))
7568 if (ATTR_IS_DEPENDENT (t))
7571 attributes = copy_list (attributes);
7575 if (DECL_P (*decl_p))
7577 if (TREE_TYPE (*decl_p) == error_mark_node)
7579 p = &DECL_ATTRIBUTES (*decl_p);
7582 p = &TYPE_ATTRIBUTES (*decl_p);
7586 tree late_attrs = NULL_TREE;
7587 tree *q = &late_attrs;
7589 for (*p = attributes; *p; )
7592 if (ATTR_IS_DEPENDENT (t))
7594 *p = TREE_CHAIN (t);
7595 TREE_CHAIN (t) = NULL_TREE;
7596 /* If the first attribute argument is an identifier, don't
7597 pass it through tsubst. Attributes like mode, format,
7598 cleanup and several target specific attributes expect it
7601 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
7602 && TREE_VALUE (TREE_VALUE (t))
7603 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
7604 == IDENTIFIER_NODE))
7607 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
7609 /*integral_constant_expression_p=*/false);
7610 if (chain != TREE_CHAIN (TREE_VALUE (t)))
7612 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
7617 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
7618 /*integral_constant_expression_p=*/false);
7620 q = &TREE_CHAIN (t);
7623 p = &TREE_CHAIN (t);
7626 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
7630 /* Perform (or defer) access check for typedefs that were referenced
7631 from within the template TMPL code.
7632 This is a subroutine of instantiate_template and instantiate_class_template.
7633 TMPL is the template to consider and TARGS is the list of arguments of
7637 perform_typedefs_access_check (tree tmpl, tree targs)
7639 location_t saved_location;
7641 qualified_typedef_usage_t *iter;
7644 || (!CLASS_TYPE_P (tmpl)
7645 && TREE_CODE (tmpl) != FUNCTION_DECL))
7648 saved_location = input_location;
7650 VEC_iterate (qualified_typedef_usage_t,
7651 get_types_needing_access_check (tmpl),
7655 tree type_decl = iter->typedef_decl;
7656 tree type_scope = iter->context;
7658 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
7661 if (uses_template_parms (type_decl))
7662 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
7663 if (uses_template_parms (type_scope))
7664 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
7666 /* Make access check error messages point to the location
7667 of the use of the typedef. */
7668 input_location = iter->locus;
7669 perform_or_defer_access_check (TYPE_BINFO (type_scope),
7670 type_decl, type_decl);
7672 input_location = saved_location;
7676 instantiate_class_template (tree type)
7678 tree templ, args, pattern, t, member;
7682 unsigned int saved_maximum_field_alignment;
7684 if (type == error_mark_node)
7685 return error_mark_node;
7687 if (TYPE_BEING_DEFINED (type)
7688 || COMPLETE_TYPE_P (type)
7689 || uses_template_parms (type))
7692 /* Figure out which template is being instantiated. */
7693 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
7694 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7696 /* Determine what specialization of the original template to
7698 t = most_specialized_class (type, templ);
7699 if (t == error_mark_node)
7701 TYPE_BEING_DEFINED (type) = 1;
7702 return error_mark_node;
7706 /* This TYPE is actually an instantiation of a partial
7707 specialization. We replace the innermost set of ARGS with
7708 the arguments appropriate for substitution. For example,
7711 template <class T> struct S {};
7712 template <class T> struct S<T*> {};
7714 and supposing that we are instantiating S<int*>, ARGS will
7715 presently be {int*} -- but we need {int}. */
7716 pattern = TREE_TYPE (t);
7717 args = TREE_PURPOSE (t);
7721 pattern = TREE_TYPE (templ);
7722 args = CLASSTYPE_TI_ARGS (type);
7725 /* If the template we're instantiating is incomplete, then clearly
7726 there's nothing we can do. */
7727 if (!COMPLETE_TYPE_P (pattern))
7730 /* If we've recursively instantiated too many templates, stop. */
7731 if (! push_tinst_level (type))
7734 /* Now we're really doing the instantiation. Mark the type as in
7735 the process of being defined. */
7736 TYPE_BEING_DEFINED (type) = 1;
7738 /* We may be in the middle of deferred access check. Disable
7740 push_deferring_access_checks (dk_no_deferred);
7742 push_to_top_level ();
7743 /* Use #pragma pack from the template context. */
7744 saved_maximum_field_alignment = maximum_field_alignment;
7745 maximum_field_alignment = TYPE_PRECISION (pattern);
7747 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
7749 /* Set the input location to the most specialized template definition.
7750 This is needed if tsubsting causes an error. */
7751 typedecl = TYPE_MAIN_DECL (pattern);
7752 input_location = DECL_SOURCE_LOCATION (typedecl);
7754 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
7755 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
7756 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
7757 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
7758 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
7759 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
7760 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
7761 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
7762 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
7763 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
7764 TYPE_PACKED (type) = TYPE_PACKED (pattern);
7765 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
7766 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
7767 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
7768 if (ANON_AGGR_TYPE_P (pattern))
7769 SET_ANON_AGGR_TYPE_P (type);
7770 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
7772 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
7773 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
7776 pbinfo = TYPE_BINFO (pattern);
7778 /* We should never instantiate a nested class before its enclosing
7779 class; we need to look up the nested class by name before we can
7780 instantiate it, and that lookup should instantiate the enclosing
7782 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
7783 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
7784 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
7786 base_list = NULL_TREE;
7787 if (BINFO_N_BASE_BINFOS (pbinfo))
7790 tree context = TYPE_CONTEXT (type);
7794 /* We must enter the scope containing the type, as that is where
7795 the accessibility of types named in dependent bases are
7797 pushed_scope = push_scope (context ? context : global_namespace);
7799 /* Substitute into each of the bases to determine the actual
7801 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
7804 tree access = BINFO_BASE_ACCESS (pbinfo, i);
7805 tree expanded_bases = NULL_TREE;
7808 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
7811 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
7812 args, tf_error, NULL_TREE);
7813 if (expanded_bases == error_mark_node)
7816 len = TREE_VEC_LENGTH (expanded_bases);
7819 for (idx = 0; idx < len; idx++)
7822 /* Extract the already-expanded base class. */
7823 base = TREE_VEC_ELT (expanded_bases, idx);
7825 /* Substitute to figure out the base class. */
7826 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
7829 if (base == error_mark_node)
7832 base_list = tree_cons (access, base, base_list);
7833 if (BINFO_VIRTUAL_P (pbase_binfo))
7834 TREE_TYPE (base_list) = integer_type_node;
7838 /* The list is now in reverse order; correct that. */
7839 base_list = nreverse (base_list);
7842 pop_scope (pushed_scope);
7844 /* Now call xref_basetypes to set up all the base-class
7846 xref_basetypes (type, base_list);
7848 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
7849 (int) ATTR_FLAG_TYPE_IN_PLACE,
7850 args, tf_error, NULL_TREE);
7852 /* Now that our base classes are set up, enter the scope of the
7853 class, so that name lookups into base classes, etc. will work
7854 correctly. This is precisely analogous to what we do in
7855 begin_class_definition when defining an ordinary non-template
7856 class, except we also need to push the enclosing classes. */
7857 push_nested_class (type);
7859 /* Now members are processed in the order of declaration. */
7860 for (member = CLASSTYPE_DECL_LIST (pattern);
7861 member; member = TREE_CHAIN (member))
7863 tree t = TREE_VALUE (member);
7865 if (TREE_PURPOSE (member))
7869 /* Build new CLASSTYPE_NESTED_UTDS. */
7872 bool class_template_p;
7874 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
7875 && TYPE_LANG_SPECIFIC (t)
7876 && CLASSTYPE_IS_TEMPLATE (t));
7877 /* If the member is a class template, then -- even after
7878 substitution -- there may be dependent types in the
7879 template argument list for the class. We increment
7880 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7881 that function will assume that no types are dependent
7882 when outside of a template. */
7883 if (class_template_p)
7884 ++processing_template_decl;
7885 newtag = tsubst (t, args, tf_error, NULL_TREE);
7886 if (class_template_p)
7887 --processing_template_decl;
7888 if (newtag == error_mark_node)
7891 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
7893 tree name = TYPE_IDENTIFIER (t);
7895 if (class_template_p)
7896 /* Unfortunately, lookup_template_class sets
7897 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
7898 instantiation (i.e., for the type of a member
7899 template class nested within a template class.)
7900 This behavior is required for
7901 maybe_process_partial_specialization to work
7902 correctly, but is not accurate in this case;
7903 the TAG is not an instantiation of anything.
7904 (The corresponding TEMPLATE_DECL is an
7905 instantiation, but the TYPE is not.) */
7906 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
7908 /* Now, we call pushtag to put this NEWTAG into the scope of
7909 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
7910 pushtag calling push_template_decl. We don't have to do
7911 this for enums because it will already have been done in
7914 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
7915 pushtag (name, newtag, /*tag_scope=*/ts_current);
7918 else if (TREE_CODE (t) == FUNCTION_DECL
7919 || DECL_FUNCTION_TEMPLATE_P (t))
7921 /* Build new TYPE_METHODS. */
7924 if (TREE_CODE (t) == TEMPLATE_DECL)
7925 ++processing_template_decl;
7926 r = tsubst (t, args, tf_error, NULL_TREE);
7927 if (TREE_CODE (t) == TEMPLATE_DECL)
7928 --processing_template_decl;
7929 set_current_access_from_decl (r);
7930 finish_member_declaration (r);
7934 /* Build new TYPE_FIELDS. */
7935 if (TREE_CODE (t) == STATIC_ASSERT)
7938 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
7939 tf_warning_or_error, NULL_TREE,
7940 /*integral_constant_expression_p=*/true);
7941 finish_static_assert (condition,
7942 STATIC_ASSERT_MESSAGE (t),
7943 STATIC_ASSERT_SOURCE_LOCATION (t),
7946 else if (TREE_CODE (t) != CONST_DECL)
7950 /* The file and line for this declaration, to
7951 assist in error message reporting. Since we
7952 called push_tinst_level above, we don't need to
7954 input_location = DECL_SOURCE_LOCATION (t);
7956 if (TREE_CODE (t) == TEMPLATE_DECL)
7957 ++processing_template_decl;
7958 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
7959 if (TREE_CODE (t) == TEMPLATE_DECL)
7960 --processing_template_decl;
7961 if (TREE_CODE (r) == VAR_DECL)
7965 [t]he initialization (and any associated
7966 side-effects) of a static data member does
7967 not occur unless the static data member is
7968 itself used in a way that requires the
7969 definition of the static data member to
7972 Therefore, we do not substitute into the
7973 initialized for the static data member here. */
7974 finish_static_data_member_decl
7977 /*init_const_expr_p=*/false,
7978 /*asmspec_tree=*/NULL_TREE,
7980 if (DECL_INITIALIZED_IN_CLASS_P (r))
7981 check_static_variable_definition (r, TREE_TYPE (r));
7983 else if (TREE_CODE (r) == FIELD_DECL)
7985 /* Determine whether R has a valid type and can be
7986 completed later. If R is invalid, then it is
7987 replaced by error_mark_node so that it will not be
7988 added to TYPE_FIELDS. */
7989 tree rtype = TREE_TYPE (r);
7990 if (can_complete_type_without_circularity (rtype))
7991 complete_type (rtype);
7993 if (!COMPLETE_TYPE_P (rtype))
7995 cxx_incomplete_type_error (r, rtype);
7996 r = error_mark_node;
8000 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8001 such a thing will already have been added to the field
8002 list by tsubst_enum in finish_member_declaration in the
8003 CLASSTYPE_NESTED_UTDS case above. */
8004 if (!(TREE_CODE (r) == TYPE_DECL
8005 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
8006 && DECL_ARTIFICIAL (r)))
8008 set_current_access_from_decl (r);
8009 finish_member_declaration (r);
8016 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
8018 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8020 tree friend_type = t;
8021 bool adjust_processing_template_decl = false;
8023 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8025 /* template <class T> friend class C; */
8026 friend_type = tsubst_friend_class (friend_type, args);
8027 adjust_processing_template_decl = true;
8029 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
8031 /* template <class T> friend class C::D; */
8032 friend_type = tsubst (friend_type, args,
8033 tf_warning_or_error, NULL_TREE);
8034 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8035 friend_type = TREE_TYPE (friend_type);
8036 adjust_processing_template_decl = true;
8038 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
8040 /* This could be either
8044 when dependent_type_p is false or
8046 template <class U> friend class T::C;
8049 friend_type = tsubst (friend_type, args,
8050 tf_warning_or_error, NULL_TREE);
8051 /* Bump processing_template_decl for correct
8052 dependent_type_p calculation. */
8053 ++processing_template_decl;
8054 if (dependent_type_p (friend_type))
8055 adjust_processing_template_decl = true;
8056 --processing_template_decl;
8058 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
8059 && hidden_name_p (TYPE_NAME (friend_type)))
8063 where C hasn't been declared yet. Let's lookup name
8064 from namespace scope directly, bypassing any name that
8065 come from dependent base class. */
8066 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
8068 /* The call to xref_tag_from_type does injection for friend
8070 push_nested_namespace (ns);
8072 xref_tag_from_type (friend_type, NULL_TREE,
8073 /*tag_scope=*/ts_current);
8074 pop_nested_namespace (ns);
8076 else if (uses_template_parms (friend_type))
8077 /* friend class C<T>; */
8078 friend_type = tsubst (friend_type, args,
8079 tf_warning_or_error, NULL_TREE);
8084 where C is already declared or
8086 friend class C<int>;
8088 We don't have to do anything in these cases. */
8090 if (adjust_processing_template_decl)
8091 /* Trick make_friend_class into realizing that the friend
8092 we're adding is a template, not an ordinary class. It's
8093 important that we use make_friend_class since it will
8094 perform some error-checking and output cross-reference
8096 ++processing_template_decl;
8098 if (friend_type != error_mark_node)
8099 make_friend_class (type, friend_type, /*complain=*/false);
8101 if (adjust_processing_template_decl)
8102 --processing_template_decl;
8106 /* Build new DECL_FRIENDLIST. */
8109 /* The file and line for this declaration, to
8110 assist in error message reporting. Since we
8111 called push_tinst_level above, we don't need to
8113 input_location = DECL_SOURCE_LOCATION (t);
8115 if (TREE_CODE (t) == TEMPLATE_DECL)
8117 ++processing_template_decl;
8118 push_deferring_access_checks (dk_no_check);
8121 r = tsubst_friend_function (t, args);
8122 add_friend (type, r, /*complain=*/false);
8123 if (TREE_CODE (t) == TEMPLATE_DECL)
8125 pop_deferring_access_checks ();
8126 --processing_template_decl;
8132 /* Set the file and line number information to whatever is given for
8133 the class itself. This puts error messages involving generated
8134 implicit functions at a predictable point, and the same point
8135 that would be used for non-template classes. */
8136 input_location = DECL_SOURCE_LOCATION (typedecl);
8138 unreverse_member_declarations (type);
8139 finish_struct_1 (type);
8140 TYPE_BEING_DEFINED (type) = 0;
8142 /* Now that the class is complete, instantiate default arguments for
8143 any member functions. We don't do this earlier because the
8144 default arguments may reference members of the class. */
8145 if (!PRIMARY_TEMPLATE_P (templ))
8146 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
8147 if (TREE_CODE (t) == FUNCTION_DECL
8148 /* Implicitly generated member functions will not have template
8149 information; they are not instantiations, but instead are
8150 created "fresh" for each instantiation. */
8151 && DECL_TEMPLATE_INFO (t))
8152 tsubst_default_arguments (t);
8154 /* Some typedefs referenced from within the template code need to be access
8155 checked at template instantiation time, i.e now. These types were
8156 added to the template at parsing time. Let's get those and perform
8157 the access checks then. */
8158 perform_typedefs_access_check (pattern, args);
8159 perform_deferred_access_checks ();
8160 pop_nested_class ();
8161 maximum_field_alignment = saved_maximum_field_alignment;
8162 pop_from_top_level ();
8163 pop_deferring_access_checks ();
8166 /* The vtable for a template class can be emitted in any translation
8167 unit in which the class is instantiated. When there is no key
8168 method, however, finish_struct_1 will already have added TYPE to
8169 the keyed_classes list. */
8170 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
8171 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
8177 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8183 else if (TYPE_P (t))
8184 r = tsubst (t, args, complain, in_decl);
8187 r = tsubst_expr (t, args, complain, in_decl,
8188 /*integral_constant_expression_p=*/true);
8189 r = fold_non_dependent_expr (r);
8194 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8195 NONTYPE_ARGUMENT_PACK. */
8198 make_fnparm_pack (tree spec_parm)
8200 /* Collect all of the extra "packed" parameters into an
8204 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
8205 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
8206 int i, len = list_length (spec_parm);
8208 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8209 parmvec = make_tree_vec (len);
8210 parmtypevec = make_tree_vec (len);
8211 for (i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
8213 TREE_VEC_ELT (parmvec, i) = spec_parm;
8214 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
8217 /* Build the argument packs. */
8218 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
8219 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
8220 TREE_TYPE (argpack) = argtypepack;
8225 /* Substitute ARGS into T, which is an pack expansion
8226 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8227 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8228 (if only a partial substitution could be performed) or
8229 ERROR_MARK_NODE if there was an error. */
8231 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
8235 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
8239 bool very_local_specializations = false;
8241 gcc_assert (PACK_EXPANSION_P (t));
8242 pattern = PACK_EXPANSION_PATTERN (t);
8244 /* Determine the argument packs that will instantiate the parameter
8245 packs used in the expansion expression. While we're at it,
8246 compute the number of arguments to be expanded and make sure it
8248 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
8249 pack = TREE_CHAIN (pack))
8251 tree parm_pack = TREE_VALUE (pack);
8252 tree arg_pack = NULL_TREE;
8253 tree orig_arg = NULL_TREE;
8255 if (TREE_CODE (parm_pack) == PARM_DECL)
8257 arg_pack = retrieve_local_specialization (parm_pack);
8258 if (arg_pack == NULL_TREE)
8260 /* This can happen for a parameter name used later in a function
8261 declaration (such as in a late-specified return type). Just
8262 make a dummy decl, since it's only used for its type. */
8263 gcc_assert (cp_unevaluated_operand != 0);
8264 arg_pack = tsubst_decl (parm_pack, args, complain);
8265 arg_pack = make_fnparm_pack (arg_pack);
8270 int level, idx, levels;
8271 template_parm_level_and_index (parm_pack, &level, &idx);
8273 levels = TMPL_ARGS_DEPTH (args);
8274 if (level <= levels)
8275 arg_pack = TMPL_ARG (args, level, idx);
8278 orig_arg = arg_pack;
8279 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
8280 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
8282 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
8283 /* This can only happen if we forget to expand an argument
8284 pack somewhere else. Just return an error, silently. */
8286 result = make_tree_vec (1);
8287 TREE_VEC_ELT (result, 0) = error_mark_node;
8292 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
8293 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
8295 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
8296 tree pattern = PACK_EXPANSION_PATTERN (expansion);
8297 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
8298 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
8299 /* The argument pack that the parameter maps to is just an
8300 expansion of the parameter itself, such as one would
8301 find in the implicit typedef of a class inside the
8302 class itself. Consider this parameter "unsubstituted",
8303 so that we will maintain the outer pack expansion. */
8304 arg_pack = NULL_TREE;
8310 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
8312 /* It's all-or-nothing with incomplete argument packs. */
8313 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8314 return error_mark_node;
8316 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8321 else if (len != my_len)
8324 /* We got explicit args for some packs but not others;
8325 do nothing now and try again after deduction. */
8327 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
8328 error ("mismatched argument pack lengths while expanding "
8332 error ("mismatched argument pack lengths while expanding "
8335 return error_mark_node;
8338 /* Keep track of the parameter packs and their corresponding
8340 packs = tree_cons (parm_pack, arg_pack, packs);
8341 TREE_TYPE (packs) = orig_arg;
8344 /* We can't substitute for this parameter pack. */
8345 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
8347 unsubstituted_packs);
8350 /* We cannot expand this expansion expression, because we don't have
8351 all of the argument packs we need. Substitute into the pattern
8352 and return a PACK_EXPANSION_*. The caller will need to deal with
8354 if (unsubstituted_packs)
8357 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8358 new_pat = tsubst_expr (pattern, args, complain, in_decl,
8359 /*integral_constant_expression_p=*/false);
8361 new_pat = tsubst (pattern, args, complain, in_decl);
8362 return make_pack_expansion (new_pat);
8365 /* We could not find any argument packs that work. */
8367 return error_mark_node;
8369 if (!local_specializations)
8371 /* We're in a late-specified return type, so we don't have a local
8372 specializations table. Create one for doing this expansion. */
8373 very_local_specializations = true;
8374 local_specializations = htab_create (37,
8375 hash_local_specialization,
8376 eq_local_specializations,
8380 /* For each argument in each argument pack, substitute into the
8382 result = make_tree_vec (len + incomplete);
8383 for (i = 0; i < len + incomplete; ++i)
8385 /* For parameter pack, change the substitution of the parameter
8386 pack to the ith argument in its argument pack, then expand
8388 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8390 tree parm = TREE_PURPOSE (pack);
8392 if (TREE_CODE (parm) == PARM_DECL)
8394 /* Select the Ith argument from the pack. */
8395 tree arg = make_node (ARGUMENT_PACK_SELECT);
8396 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
8397 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
8399 register_local_specialization (arg, parm);
8405 template_parm_level_and_index (parm, &level, &idx);
8409 /* Select the Ith argument from the pack. */
8410 value = make_node (ARGUMENT_PACK_SELECT);
8411 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
8412 ARGUMENT_PACK_SELECT_INDEX (value) = i;
8415 /* Update the corresponding argument. */
8416 TMPL_ARG (args, level, idx) = value;
8420 /* Substitute into the PATTERN with the altered arguments. */
8421 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8422 TREE_VEC_ELT (result, i) =
8423 tsubst_expr (pattern, args, complain, in_decl,
8424 /*integral_constant_expression_p=*/false);
8426 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
8429 /* When we have incomplete argument packs, the last "expanded"
8430 result is itself a pack expansion, which allows us
8431 to deduce more arguments. */
8432 TREE_VEC_ELT (result, i) =
8433 make_pack_expansion (TREE_VEC_ELT (result, i));
8435 if (TREE_VEC_ELT (result, i) == error_mark_node)
8437 result = error_mark_node;
8442 /* Update ARGS to restore the substitution from parameter packs to
8443 their argument packs. */
8444 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8446 tree parm = TREE_PURPOSE (pack);
8448 if (TREE_CODE (parm) == PARM_DECL)
8449 register_local_specialization (TREE_TYPE (pack), parm);
8453 template_parm_level_and_index (parm, &level, &idx);
8455 /* Update the corresponding argument. */
8456 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
8457 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
8460 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
8464 if (very_local_specializations)
8466 htab_delete (local_specializations);
8467 local_specializations = NULL;
8473 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8474 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8475 parameter packs; all parms generated from a function parameter pack will
8476 have the same DECL_PARM_INDEX. */
8479 get_pattern_parm (tree parm, tree tmpl)
8481 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
8484 if (DECL_ARTIFICIAL (parm))
8486 for (patparm = DECL_ARGUMENTS (pattern);
8487 patparm; patparm = TREE_CHAIN (patparm))
8488 if (DECL_ARTIFICIAL (patparm)
8489 && DECL_NAME (parm) == DECL_NAME (patparm))
8494 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
8495 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
8496 gcc_assert (DECL_PARM_INDEX (patparm)
8497 == DECL_PARM_INDEX (parm));
8503 /* Substitute ARGS into the vector or list of template arguments T. */
8506 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8509 int len = TREE_VEC_LENGTH (t);
8510 int need_new = 0, i, expanded_len_adjust = 0, out;
8511 tree *elts = (tree *) alloca (len * sizeof (tree));
8513 for (i = 0; i < len; i++)
8515 tree orig_arg = TREE_VEC_ELT (t, i);
8518 if (TREE_CODE (orig_arg) == TREE_VEC)
8519 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
8520 else if (PACK_EXPANSION_P (orig_arg))
8522 /* Substitute into an expansion expression. */
8523 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
8525 if (TREE_CODE (new_arg) == TREE_VEC)
8526 /* Add to the expanded length adjustment the number of
8527 expanded arguments. We subtract one from this
8528 measurement, because the argument pack expression
8529 itself is already counted as 1 in
8530 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
8531 the argument pack is empty. */
8532 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
8534 else if (ARGUMENT_PACK_P (orig_arg))
8536 /* Substitute into each of the arguments. */
8537 new_arg = TYPE_P (orig_arg)
8538 ? cxx_make_type (TREE_CODE (orig_arg))
8539 : make_node (TREE_CODE (orig_arg));
8541 SET_ARGUMENT_PACK_ARGS (
8543 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
8544 args, complain, in_decl));
8546 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
8547 new_arg = error_mark_node;
8549 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
8550 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
8552 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
8554 if (TREE_TYPE (new_arg) == error_mark_node)
8555 new_arg = error_mark_node;
8559 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
8561 if (new_arg == error_mark_node)
8562 return error_mark_node;
8565 if (new_arg != orig_arg)
8572 /* Make space for the expanded arguments coming from template
8574 t = make_tree_vec (len + expanded_len_adjust);
8575 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
8576 arguments for a member template.
8577 In that case each TREE_VEC in ORIG_T represents a level of template
8578 arguments, and ORIG_T won't carry any non defaulted argument count.
8579 It will rather be the nested TREE_VECs that will carry one.
8580 In other words, ORIG_T carries a non defaulted argument count only
8581 if it doesn't contain any nested TREE_VEC. */
8582 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
8584 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
8585 count += expanded_len_adjust;
8586 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
8588 for (i = 0, out = 0; i < len; i++)
8590 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
8591 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
8592 && TREE_CODE (elts[i]) == TREE_VEC)
8596 /* Now expand the template argument pack "in place". */
8597 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
8598 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
8602 TREE_VEC_ELT (t, out) = elts[i];
8610 /* Return the result of substituting ARGS into the template parameters
8611 given by PARMS. If there are m levels of ARGS and m + n levels of
8612 PARMS, then the result will contain n levels of PARMS. For
8613 example, if PARMS is `template <class T> template <class U>
8614 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
8615 result will be `template <int*, double, class V>'. */
8618 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
8623 /* When substituting into a template, we must set
8624 PROCESSING_TEMPLATE_DECL as the template parameters may be
8625 dependent if they are based on one-another, and the dependency
8626 predicates are short-circuit outside of templates. */
8627 ++processing_template_decl;
8629 for (new_parms = &r;
8630 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
8631 new_parms = &(TREE_CHAIN (*new_parms)),
8632 parms = TREE_CHAIN (parms))
8635 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
8638 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
8644 if (parms == error_mark_node)
8647 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
8649 if (tuple == error_mark_node)
8652 default_value = TREE_PURPOSE (tuple);
8653 parm_decl = TREE_VALUE (tuple);
8655 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
8656 if (TREE_CODE (parm_decl) == PARM_DECL
8657 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
8658 parm_decl = error_mark_node;
8659 default_value = tsubst_template_arg (default_value, args,
8660 complain, NULL_TREE);
8662 tuple = build_tree_list (default_value, parm_decl);
8663 TREE_VEC_ELT (new_vec, i) = tuple;
8667 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
8668 - TMPL_ARGS_DEPTH (args)),
8669 new_vec, NULL_TREE);
8672 --processing_template_decl;
8677 /* Substitute the ARGS into the indicated aggregate (or enumeration)
8678 type T. If T is not an aggregate or enumeration type, it is
8679 handled as if by tsubst. IN_DECL is as for tsubst. If
8680 ENTERING_SCOPE is nonzero, T is the context for a template which
8681 we are presently tsubst'ing. Return the substituted value. */
8684 tsubst_aggr_type (tree t,
8686 tsubst_flags_t complain,
8693 switch (TREE_CODE (t))
8696 if (TYPE_PTRMEMFUNC_P (t))
8697 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
8699 /* Else fall through. */
8702 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
8707 int saved_unevaluated_operand;
8708 int saved_inhibit_evaluation_warnings;
8710 /* In "sizeof(X<I>)" we need to evaluate "I". */
8711 saved_unevaluated_operand = cp_unevaluated_operand;
8712 cp_unevaluated_operand = 0;
8713 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8714 c_inhibit_evaluation_warnings = 0;
8716 /* First, determine the context for the type we are looking
8718 context = TYPE_CONTEXT (t);
8721 context = tsubst_aggr_type (context, args, complain,
8722 in_decl, /*entering_scope=*/1);
8723 /* If context is a nested class inside a class template,
8724 it may still need to be instantiated (c++/33959). */
8725 if (TYPE_P (context))
8726 context = complete_type (context);
8729 /* Then, figure out what arguments are appropriate for the
8730 type we are trying to find. For example, given:
8732 template <class T> struct S;
8733 template <class T, class U> void f(T, U) { S<U> su; }
8735 and supposing that we are instantiating f<int, double>,
8736 then our ARGS will be {int, double}, but, when looking up
8737 S we only want {double}. */
8738 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
8740 if (argvec == error_mark_node)
8741 r = error_mark_node;
8744 r = lookup_template_class (t, argvec, in_decl, context,
8745 entering_scope, complain);
8746 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
8749 cp_unevaluated_operand = saved_unevaluated_operand;
8750 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8755 /* This is not a template type, so there's nothing to do. */
8759 return tsubst (t, args, complain, in_decl);
8763 /* Substitute into the default argument ARG (a default argument for
8764 FN), which has the indicated TYPE. */
8767 tsubst_default_argument (tree fn, tree type, tree arg)
8769 tree saved_class_ptr = NULL_TREE;
8770 tree saved_class_ref = NULL_TREE;
8772 /* This default argument came from a template. Instantiate the
8773 default argument here, not in tsubst. In the case of
8782 we must be careful to do name lookup in the scope of S<T>,
8783 rather than in the current class. */
8784 push_access_scope (fn);
8785 /* The "this" pointer is not valid in a default argument. */
8788 saved_class_ptr = current_class_ptr;
8789 cp_function_chain->x_current_class_ptr = NULL_TREE;
8790 saved_class_ref = current_class_ref;
8791 cp_function_chain->x_current_class_ref = NULL_TREE;
8794 push_deferring_access_checks(dk_no_deferred);
8795 /* The default argument expression may cause implicitly defined
8796 member functions to be synthesized, which will result in garbage
8797 collection. We must treat this situation as if we were within
8798 the body of function so as to avoid collecting live data on the
8801 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
8802 tf_warning_or_error, NULL_TREE,
8803 /*integral_constant_expression_p=*/false);
8805 pop_deferring_access_checks();
8807 /* Restore the "this" pointer. */
8810 cp_function_chain->x_current_class_ptr = saved_class_ptr;
8811 cp_function_chain->x_current_class_ref = saved_class_ref;
8814 /* Make sure the default argument is reasonable. */
8815 arg = check_default_argument (type, arg);
8817 pop_access_scope (fn);
8822 /* Substitute into all the default arguments for FN. */
8825 tsubst_default_arguments (tree fn)
8830 tmpl_args = DECL_TI_ARGS (fn);
8832 /* If this function is not yet instantiated, we certainly don't need
8833 its default arguments. */
8834 if (uses_template_parms (tmpl_args))
8837 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
8839 arg = TREE_CHAIN (arg))
8840 if (TREE_PURPOSE (arg))
8841 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
8843 TREE_PURPOSE (arg));
8846 /* Substitute the ARGS into the T, which is a _DECL. Return the
8847 result of the substitution. Issue error and warning messages under
8848 control of COMPLAIN. */
8851 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
8853 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
8854 location_t saved_loc;
8859 /* Set the filename and linenumber to improve error-reporting. */
8860 saved_loc = input_location;
8861 input_location = DECL_SOURCE_LOCATION (t);
8863 switch (TREE_CODE (t))
8867 /* We can get here when processing a member function template,
8868 member class template, or template template parameter. */
8869 tree decl = DECL_TEMPLATE_RESULT (t);
8874 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8876 /* Template template parameter is treated here. */
8877 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8878 if (new_type == error_mark_node)
8879 RETURN (error_mark_node);
8882 TREE_CHAIN (r) = NULL_TREE;
8883 TREE_TYPE (r) = new_type;
8884 DECL_TEMPLATE_RESULT (r)
8885 = build_decl (DECL_SOURCE_LOCATION (decl),
8886 TYPE_DECL, DECL_NAME (decl), new_type);
8887 DECL_TEMPLATE_PARMS (r)
8888 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8890 TYPE_NAME (new_type) = r;
8894 /* We might already have an instance of this template.
8895 The ARGS are for the surrounding class type, so the
8896 full args contain the tsubst'd args for the context,
8897 plus the innermost args from the template decl. */
8898 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
8899 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
8900 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
8901 /* Because this is a template, the arguments will still be
8902 dependent, even after substitution. If
8903 PROCESSING_TEMPLATE_DECL is not set, the dependency
8904 predicates will short-circuit. */
8905 ++processing_template_decl;
8906 full_args = tsubst_template_args (tmpl_args, args,
8908 --processing_template_decl;
8909 if (full_args == error_mark_node)
8910 RETURN (error_mark_node);
8912 /* If this is a default template template argument,
8913 tsubst might not have changed anything. */
8914 if (full_args == tmpl_args)
8917 hash = hash_tmpl_and_args (t, full_args);
8918 spec = retrieve_specialization (t, full_args, hash);
8919 if (spec != NULL_TREE)
8925 /* Make a new template decl. It will be similar to the
8926 original, but will record the current template arguments.
8927 We also create a new function declaration, which is just
8928 like the old one, but points to this new template, rather
8929 than the old one. */
8931 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
8932 TREE_CHAIN (r) = NULL_TREE;
8934 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
8936 if (TREE_CODE (decl) == TYPE_DECL)
8939 ++processing_template_decl;
8940 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8941 --processing_template_decl;
8942 if (new_type == error_mark_node)
8943 RETURN (error_mark_node);
8945 TREE_TYPE (r) = new_type;
8946 CLASSTYPE_TI_TEMPLATE (new_type) = r;
8947 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
8948 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
8949 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
8954 ++processing_template_decl;
8955 new_decl = tsubst (decl, args, complain, in_decl);
8956 --processing_template_decl;
8957 if (new_decl == error_mark_node)
8958 RETURN (error_mark_node);
8960 DECL_TEMPLATE_RESULT (r) = new_decl;
8961 DECL_TI_TEMPLATE (new_decl) = r;
8962 TREE_TYPE (r) = TREE_TYPE (new_decl);
8963 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
8964 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
8967 SET_DECL_IMPLICIT_INSTANTIATION (r);
8968 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
8969 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
8971 /* The template parameters for this new template are all the
8972 template parameters for the old template, except the
8973 outermost level of parameters. */
8974 DECL_TEMPLATE_PARMS (r)
8975 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8978 if (PRIMARY_TEMPLATE_P (t))
8979 DECL_PRIMARY_TEMPLATE (r) = r;
8981 if (TREE_CODE (decl) != TYPE_DECL)
8982 /* Record this non-type partial instantiation. */
8983 register_specialization (r, t,
8984 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
8992 tree argvec = NULL_TREE;
9000 /* Nobody should be tsubst'ing into non-template functions. */
9001 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
9003 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
9008 /* If T is not dependent, just return it. We have to
9009 increment PROCESSING_TEMPLATE_DECL because
9010 value_dependent_expression_p assumes that nothing is
9011 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9012 ++processing_template_decl;
9013 dependent_p = value_dependent_expression_p (t);
9014 --processing_template_decl;
9018 /* Calculate the most general template of which R is a
9019 specialization, and the complete set of arguments used to
9021 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
9022 argvec = tsubst_template_args (DECL_TI_ARGS
9023 (DECL_TEMPLATE_RESULT (gen_tmpl)),
9024 args, complain, in_decl);
9026 /* Check to see if we already have this specialization. */
9027 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9028 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9036 /* We can see more levels of arguments than parameters if
9037 there was a specialization of a member template, like
9040 template <class T> struct S { template <class U> void f(); }
9041 template <> template <class U> void S<int>::f(U);
9043 Here, we'll be substituting into the specialization,
9044 because that's where we can find the code we actually
9045 want to generate, but we'll have enough arguments for
9046 the most general template.
9048 We also deal with the peculiar case:
9050 template <class T> struct S {
9051 template <class U> friend void f();
9053 template <class U> void f() {}
9055 template void f<double>();
9057 Here, the ARGS for the instantiation of will be {int,
9058 double}. But, we only need as many ARGS as there are
9059 levels of template parameters in CODE_PATTERN. We are
9060 careful not to get fooled into reducing the ARGS in
9063 template <class T> struct S { template <class U> void f(U); }
9064 template <class T> template <> void S<T>::f(int) {}
9066 which we can spot because the pattern will be a
9067 specialization in this case. */
9068 args_depth = TMPL_ARGS_DEPTH (args);
9070 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
9071 if (args_depth > parms_depth
9072 && !DECL_TEMPLATE_SPECIALIZATION (t))
9073 args = get_innermost_template_args (args, parms_depth);
9077 /* This special case arises when we have something like this:
9079 template <class T> struct S {
9080 friend void f<int>(int, double);
9083 Here, the DECL_TI_TEMPLATE for the friend declaration
9084 will be an IDENTIFIER_NODE. We are being called from
9085 tsubst_friend_function, and we want only to create a
9086 new decl (R) with appropriate types so that we can call
9087 determine_specialization. */
9088 gen_tmpl = NULL_TREE;
9091 if (DECL_CLASS_SCOPE_P (t))
9093 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
9097 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
9098 complain, t, /*entering_scope=*/1);
9103 ctx = DECL_CONTEXT (t);
9105 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9106 if (type == error_mark_node)
9107 RETURN (error_mark_node);
9109 /* We do NOT check for matching decls pushed separately at this
9110 point, as they may not represent instantiations of this
9111 template, and in any case are considered separate under the
9114 DECL_USE_TEMPLATE (r) = 0;
9115 TREE_TYPE (r) = type;
9116 /* Clear out the mangled name and RTL for the instantiation. */
9117 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9118 SET_DECL_RTL (r, NULL_RTX);
9119 /* Leave DECL_INITIAL set on deleted instantiations. */
9120 if (!DECL_DELETED_FN (r))
9121 DECL_INITIAL (r) = NULL_TREE;
9122 DECL_CONTEXT (r) = ctx;
9124 if (member && DECL_CONV_FN_P (r))
9125 /* Type-conversion operator. Reconstruct the name, in
9126 case it's the name of one of the template's parameters. */
9127 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
9129 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
9131 DECL_RESULT (r) = NULL_TREE;
9133 TREE_STATIC (r) = 0;
9134 TREE_PUBLIC (r) = TREE_PUBLIC (t);
9135 DECL_EXTERNAL (r) = 1;
9136 /* If this is an instantiation of a function with internal
9137 linkage, we already know what object file linkage will be
9138 assigned to the instantiation. */
9139 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
9140 DECL_DEFER_OUTPUT (r) = 0;
9141 TREE_CHAIN (r) = NULL_TREE;
9142 DECL_PENDING_INLINE_INFO (r) = 0;
9143 DECL_PENDING_INLINE_P (r) = 0;
9144 DECL_SAVED_TREE (r) = NULL_TREE;
9145 DECL_STRUCT_FUNCTION (r) = NULL;
9147 /* We'll re-clone as appropriate in instantiate_template. */
9148 DECL_CLONED_FUNCTION (r) = NULL_TREE;
9150 /* If we aren't complaining now, return on error before we register
9151 the specialization so that we'll complain eventually. */
9152 if ((complain & tf_error) == 0
9153 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9154 && !grok_op_properties (r, /*complain=*/false))
9155 RETURN (error_mark_node);
9157 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
9158 this in the special friend case mentioned above where
9159 GEN_TMPL is NULL. */
9162 DECL_TEMPLATE_INFO (r)
9163 = build_template_info (gen_tmpl, argvec);
9164 SET_DECL_IMPLICIT_INSTANTIATION (r);
9165 register_specialization (r, gen_tmpl, argvec, false, hash);
9167 /* We're not supposed to instantiate default arguments
9168 until they are called, for a template. But, for a
9171 template <class T> void f ()
9172 { extern void g(int i = T()); }
9174 we should do the substitution when the template is
9175 instantiated. We handle the member function case in
9176 instantiate_class_template since the default arguments
9177 might refer to other members of the class. */
9179 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9180 && !uses_template_parms (argvec))
9181 tsubst_default_arguments (r);
9184 DECL_TEMPLATE_INFO (r) = NULL_TREE;
9186 /* Copy the list of befriending classes. */
9187 for (friends = &DECL_BEFRIENDING_CLASSES (r);
9189 friends = &TREE_CHAIN (*friends))
9191 *friends = copy_node (*friends);
9192 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
9197 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
9199 maybe_retrofit_in_chrg (r);
9200 if (DECL_CONSTRUCTOR_P (r))
9201 grok_ctor_properties (ctx, r);
9202 /* If this is an instantiation of a member template, clone it.
9203 If it isn't, that'll be handled by
9204 clone_constructors_and_destructors. */
9205 if (PRIMARY_TEMPLATE_P (gen_tmpl))
9206 clone_function_decl (r, /*update_method_vec_p=*/0);
9208 else if ((complain & tf_error) != 0
9209 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9210 && !grok_op_properties (r, /*complain=*/true))
9211 RETURN (error_mark_node);
9213 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
9214 SET_DECL_FRIEND_CONTEXT (r,
9215 tsubst (DECL_FRIEND_CONTEXT (t),
9216 args, complain, in_decl));
9218 /* Possibly limit visibility based on template args. */
9219 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9220 if (DECL_VISIBILITY_SPECIFIED (t))
9222 DECL_VISIBILITY_SPECIFIED (r) = 0;
9224 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9226 determine_visibility (r);
9227 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
9228 && !processing_template_decl)
9229 defaulted_late_check (r);
9231 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9232 args, complain, in_decl);
9238 tree type = NULL_TREE;
9240 tree expanded_types = NULL_TREE;
9241 tree prev_r = NULL_TREE;
9242 tree first_r = NULL_TREE;
9244 if (FUNCTION_PARAMETER_PACK_P (t))
9246 /* If there is a local specialization that isn't a
9247 parameter pack, it means that we're doing a "simple"
9248 substitution from inside tsubst_pack_expansion. Just
9249 return the local specialization (which will be a single
9251 tree spec = retrieve_local_specialization (t);
9253 && TREE_CODE (spec) == PARM_DECL
9254 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
9257 /* Expand the TYPE_PACK_EXPANSION that provides the types for
9258 the parameters in this function parameter pack. */
9259 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
9261 if (TREE_CODE (expanded_types) == TREE_VEC)
9263 len = TREE_VEC_LENGTH (expanded_types);
9265 /* Zero-length parameter packs are boring. Just substitute
9268 RETURN (tsubst (TREE_CHAIN (t), args, complain,
9273 /* All we did was update the type. Make a note of that. */
9274 type = expanded_types;
9275 expanded_types = NULL_TREE;
9279 /* Loop through all of the parameter's we'll build. When T is
9280 a function parameter pack, LEN is the number of expanded
9281 types in EXPANDED_TYPES; otherwise, LEN is 1. */
9283 for (i = 0; i < len; ++i)
9287 if (DECL_TEMPLATE_PARM_P (t))
9288 SET_DECL_TEMPLATE_PARM_P (r);
9290 /* An argument of a function parameter pack is not a parameter
9292 FUNCTION_PARAMETER_PACK_P (r) = false;
9295 /* We're on the Ith parameter of the function parameter
9298 /* Get the Ith type. */
9299 type = TREE_VEC_ELT (expanded_types, i);
9302 /* Rename the parameter to include the index. */
9304 make_ith_pack_parameter_name (DECL_NAME (r), i);
9307 /* We're dealing with a normal parameter. */
9308 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9310 type = type_decays_to (type);
9311 TREE_TYPE (r) = type;
9312 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9314 if (DECL_INITIAL (r))
9316 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
9317 DECL_INITIAL (r) = TREE_TYPE (r);
9319 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
9323 DECL_CONTEXT (r) = NULL_TREE;
9325 if (!DECL_TEMPLATE_PARM_P (r))
9326 DECL_ARG_TYPE (r) = type_passed_as (type);
9328 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9329 args, complain, in_decl);
9331 /* Keep track of the first new parameter we
9332 generate. That's what will be returned to the
9337 /* Build a proper chain of parameters when substituting
9338 into a function parameter pack. */
9340 TREE_CHAIN (prev_r) = r;
9344 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
9345 complain, TREE_CHAIN (t));
9347 /* FIRST_R contains the start of the chain we've built. */
9357 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9358 if (type == error_mark_node)
9359 RETURN (error_mark_node);
9360 TREE_TYPE (r) = type;
9361 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9363 /* DECL_INITIAL gives the number of bits in a bit-field. */
9365 = tsubst_expr (DECL_INITIAL (t), args,
9367 /*integral_constant_expression_p=*/true);
9368 /* We don't have to set DECL_CONTEXT here; it is set by
9369 finish_member_declaration. */
9370 TREE_CHAIN (r) = NULL_TREE;
9371 if (VOID_TYPE_P (type))
9372 error ("instantiation of %q+D as type %qT", r, type);
9374 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9375 args, complain, in_decl);
9380 /* We reach here only for member using decls. */
9381 if (DECL_DEPENDENT_P (t))
9383 r = do_class_using_decl
9384 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
9385 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
9387 r = error_mark_node;
9390 TREE_PROTECTED (r) = TREE_PROTECTED (t);
9391 TREE_PRIVATE (r) = TREE_PRIVATE (t);
9397 TREE_CHAIN (r) = NULL_TREE;
9404 tree argvec = NULL_TREE;
9405 tree gen_tmpl = NULL_TREE;
9407 tree tmpl = NULL_TREE;
9409 tree type = NULL_TREE;
9412 if (TREE_CODE (t) == TYPE_DECL
9413 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
9415 /* If this is the canonical decl, we don't have to
9416 mess with instantiations, and often we can't (for
9417 typename, template type parms and such). Note that
9418 TYPE_NAME is not correct for the above test if
9419 we've copied the type for a typedef. */
9420 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9421 if (type == error_mark_node)
9422 RETURN (error_mark_node);
9423 r = TYPE_NAME (type);
9427 /* Check to see if we already have the specialization we
9430 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
9432 /* T is a static data member or namespace-scope entity.
9433 We have to substitute into namespace-scope variables
9434 (even though such entities are never templates) because
9437 template <class T> void f() { extern T t; }
9439 where the entity referenced is not known until
9440 instantiation time. */
9442 ctx = DECL_CONTEXT (t);
9443 if (DECL_CLASS_SCOPE_P (t))
9445 ctx = tsubst_aggr_type (ctx, args,
9447 in_decl, /*entering_scope=*/1);
9448 /* If CTX is unchanged, then T is in fact the
9449 specialization we want. That situation occurs when
9450 referencing a static data member within in its own
9451 class. We can use pointer equality, rather than
9452 same_type_p, because DECL_CONTEXT is always
9454 if (ctx == DECL_CONTEXT (t))
9460 tmpl = DECL_TI_TEMPLATE (t);
9461 gen_tmpl = most_general_template (tmpl);
9462 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
9463 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9464 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9469 /* A local variable. */
9471 /* Subsequent calls to pushdecl will fill this in. */
9473 spec = retrieve_local_specialization (t);
9475 /* If we already have the specialization we need, there is
9476 nothing more to do. */
9483 /* Create a new node for the specialization we need. */
9485 if (type == NULL_TREE)
9487 if (is_typedef_decl (t))
9488 type = DECL_ORIGINAL_TYPE (t);
9490 type = TREE_TYPE (t);
9491 type = tsubst (type, args, complain, in_decl);
9493 if (TREE_CODE (r) == VAR_DECL)
9495 /* Even if the original location is out of scope, the
9496 newly substituted one is not. */
9497 DECL_DEAD_FOR_LOCAL (r) = 0;
9498 DECL_INITIALIZED_P (r) = 0;
9499 DECL_TEMPLATE_INSTANTIATED (r) = 0;
9500 if (type == error_mark_node)
9501 RETURN (error_mark_node);
9502 if (TREE_CODE (type) == FUNCTION_TYPE)
9504 /* It may seem that this case cannot occur, since:
9509 declares a function, not a variable. However:
9512 template <typename T> void g() { T t; }
9513 template void g<f>();
9515 is an attempt to declare a variable with function
9517 error ("variable %qD has function type",
9518 /* R is not yet sufficiently initialized, so we
9519 just use its name. */
9521 RETURN (error_mark_node);
9523 type = complete_type (type);
9524 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
9525 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
9526 type = check_var_type (DECL_NAME (r), type);
9528 if (DECL_HAS_VALUE_EXPR_P (t))
9530 tree ve = DECL_VALUE_EXPR (t);
9531 ve = tsubst_expr (ve, args, complain, in_decl,
9532 /*constant_expression_p=*/false);
9533 SET_DECL_VALUE_EXPR (r, ve);
9536 else if (DECL_SELF_REFERENCE_P (t))
9537 SET_DECL_SELF_REFERENCE_P (r);
9538 TREE_TYPE (r) = type;
9539 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9540 DECL_CONTEXT (r) = ctx;
9541 /* Clear out the mangled name and RTL for the instantiation. */
9542 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9543 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9544 SET_DECL_RTL (r, NULL_RTX);
9545 /* The initializer must not be expanded until it is required;
9547 DECL_INITIAL (r) = NULL_TREE;
9548 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9549 SET_DECL_RTL (r, NULL_RTX);
9550 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
9551 if (TREE_CODE (r) == VAR_DECL)
9553 /* Possibly limit visibility based on template args. */
9554 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9555 if (DECL_VISIBILITY_SPECIFIED (t))
9557 DECL_VISIBILITY_SPECIFIED (r) = 0;
9559 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9561 determine_visibility (r);
9566 /* A static data member declaration is always marked
9567 external when it is declared in-class, even if an
9568 initializer is present. We mimic the non-template
9570 DECL_EXTERNAL (r) = 1;
9572 register_specialization (r, gen_tmpl, argvec, false, hash);
9573 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
9574 SET_DECL_IMPLICIT_INSTANTIATION (r);
9576 else if (cp_unevaluated_operand)
9578 /* We're substituting this var in a decltype outside of its
9579 scope, such as for a lambda return type. Don't add it to
9580 local_specializations, do perform auto deduction. */
9581 tree auto_node = type_uses_auto (type);
9583 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
9584 /*constant_expression_p=*/false);
9586 if (auto_node && init && describable_type (init))
9588 type = do_auto_deduction (type, init, auto_node);
9589 TREE_TYPE (r) = type;
9593 register_local_specialization (r, t);
9595 TREE_CHAIN (r) = NULL_TREE;
9597 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
9599 args, complain, in_decl);
9601 /* Preserve a typedef that names a type. */
9602 if (is_typedef_decl (r))
9604 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
9605 set_underlying_type (r);
9618 /* Restore the file and line information. */
9619 input_location = saved_loc;
9624 /* Substitute into the ARG_TYPES of a function type. */
9627 tsubst_arg_types (tree arg_types,
9629 tsubst_flags_t complain,
9632 tree remaining_arg_types;
9633 tree type = NULL_TREE;
9635 tree expanded_args = NULL_TREE;
9638 if (!arg_types || arg_types == void_list_node)
9641 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
9642 args, complain, in_decl);
9643 if (remaining_arg_types == error_mark_node)
9644 return error_mark_node;
9646 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
9648 /* For a pack expansion, perform substitution on the
9649 entire expression. Later on, we'll handle the arguments
9651 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
9652 args, complain, in_decl);
9654 if (TREE_CODE (expanded_args) == TREE_VEC)
9655 /* So that we'll spin through the parameters, one by one. */
9656 i = TREE_VEC_LENGTH (expanded_args);
9659 /* We only partially substituted into the parameter
9660 pack. Our type is TYPE_PACK_EXPANSION. */
9661 type = expanded_args;
9662 expanded_args = NULL_TREE;
9670 type = TREE_VEC_ELT (expanded_args, i);
9672 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
9674 if (type == error_mark_node)
9675 return error_mark_node;
9676 if (VOID_TYPE_P (type))
9678 if (complain & tf_error)
9680 error ("invalid parameter type %qT", type);
9682 error ("in declaration %q+D", in_decl);
9684 return error_mark_node;
9687 /* Do array-to-pointer, function-to-pointer conversion, and ignore
9688 top-level qualifiers as required. */
9689 type = TYPE_MAIN_VARIANT (type_decays_to (type));
9691 /* We do not substitute into default arguments here. The standard
9692 mandates that they be instantiated only when needed, which is
9693 done in build_over_call. */
9694 default_arg = TREE_PURPOSE (arg_types);
9696 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
9698 /* We've instantiated a template before its default arguments
9699 have been parsed. This can happen for a nested template
9700 class, and is not an error unless we require the default
9701 argument in a call of this function. */
9702 remaining_arg_types =
9703 tree_cons (default_arg, type, remaining_arg_types);
9704 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
9705 remaining_arg_types);
9708 remaining_arg_types =
9709 hash_tree_cons (default_arg, type, remaining_arg_types);
9712 return remaining_arg_types;
9715 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
9716 *not* handle the exception-specification for FNTYPE, because the
9717 initial substitution of explicitly provided template parameters
9718 during argument deduction forbids substitution into the
9719 exception-specification:
9723 All references in the function type of the function template to the
9724 corresponding template parameters are replaced by the specified tem-
9725 plate argument values. If a substitution in a template parameter or
9726 in the function type of the function template results in an invalid
9727 type, type deduction fails. [Note: The equivalent substitution in
9728 exception specifications is done only when the function is instanti-
9729 ated, at which point a program is ill-formed if the substitution
9730 results in an invalid type.] */
9733 tsubst_function_type (tree t,
9735 tsubst_flags_t complain,
9742 /* The TYPE_CONTEXT is not used for function/method types. */
9743 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
9745 /* Substitute the return type. */
9746 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9747 if (return_type == error_mark_node)
9748 return error_mark_node;
9749 /* The standard does not presently indicate that creation of a
9750 function type with an invalid return type is a deduction failure.
9751 However, that is clearly analogous to creating an array of "void"
9752 or a reference to a reference. This is core issue #486. */
9753 if (TREE_CODE (return_type) == ARRAY_TYPE
9754 || TREE_CODE (return_type) == FUNCTION_TYPE)
9756 if (complain & tf_error)
9758 if (TREE_CODE (return_type) == ARRAY_TYPE)
9759 error ("function returning an array");
9761 error ("function returning a function");
9763 return error_mark_node;
9766 /* Substitute the argument types. */
9767 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
9769 if (arg_types == error_mark_node)
9770 return error_mark_node;
9772 /* Construct a new type node and return it. */
9773 if (TREE_CODE (t) == FUNCTION_TYPE)
9774 fntype = build_function_type (return_type, arg_types);
9777 tree r = TREE_TYPE (TREE_VALUE (arg_types));
9778 if (! MAYBE_CLASS_TYPE_P (r))
9782 Type deduction may fail for any of the following
9785 -- Attempting to create "pointer to member of T" when T
9786 is not a class type. */
9787 if (complain & tf_error)
9788 error ("creating pointer to member function of non-class type %qT",
9790 return error_mark_node;
9793 fntype = build_method_type_directly (r, return_type,
9794 TREE_CHAIN (arg_types));
9796 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
9797 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
9802 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
9803 ARGS into that specification, and return the substituted
9804 specification. If there is no specification, return NULL_TREE. */
9807 tsubst_exception_specification (tree fntype,
9809 tsubst_flags_t complain,
9815 specs = TYPE_RAISES_EXCEPTIONS (fntype);
9816 new_specs = NULL_TREE;
9819 if (! TREE_VALUE (specs))
9826 tree expanded_specs = NULL_TREE;
9828 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
9830 /* Expand the pack expansion type. */
9831 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
9835 if (expanded_specs == error_mark_node)
9836 return error_mark_node;
9837 else if (TREE_CODE (expanded_specs) == TREE_VEC)
9838 len = TREE_VEC_LENGTH (expanded_specs);
9841 /* We're substituting into a member template, so
9842 we got a TYPE_PACK_EXPANSION back. Add that
9843 expansion and move on. */
9844 gcc_assert (TREE_CODE (expanded_specs)
9845 == TYPE_PACK_EXPANSION);
9846 new_specs = add_exception_specifier (new_specs,
9849 specs = TREE_CHAIN (specs);
9854 for (i = 0; i < len; ++i)
9857 spec = TREE_VEC_ELT (expanded_specs, i);
9859 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
9860 if (spec == error_mark_node)
9862 new_specs = add_exception_specifier (new_specs, spec,
9866 specs = TREE_CHAIN (specs);
9872 /* Take the tree structure T and replace template parameters used
9873 therein with the argument vector ARGS. IN_DECL is an associated
9874 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
9875 Issue error and warning messages under control of COMPLAIN. Note
9876 that we must be relatively non-tolerant of extensions here, in
9877 order to preserve conformance; if we allow substitutions that
9878 should not be allowed, we may allow argument deductions that should
9879 not succeed, and therefore report ambiguous overload situations
9880 where there are none. In theory, we could allow the substitution,
9881 but indicate that it should have failed, and allow our caller to
9882 make sure that the right thing happens, but we don't try to do this
9885 This function is used for dealing with types, decls and the like;
9886 for expressions, use tsubst_expr or tsubst_copy. */
9889 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9893 if (t == NULL_TREE || t == error_mark_node
9894 || t == integer_type_node
9895 || t == void_type_node
9896 || t == char_type_node
9897 || t == unknown_type_node
9898 || TREE_CODE (t) == NAMESPACE_DECL)
9902 return tsubst_decl (t, args, complain);
9904 if (args == NULL_TREE)
9907 if (TREE_CODE (t) == IDENTIFIER_NODE)
9908 type = IDENTIFIER_TYPE_VALUE (t);
9910 type = TREE_TYPE (t);
9912 gcc_assert (type != unknown_type_node);
9914 /* Reuse typedefs. We need to do this to handle dependent attributes,
9915 such as attribute aligned. */
9918 && TYPE_NAME (t) != TYPE_MAIN_DECL (t))
9920 tree decl = TYPE_NAME (t);
9922 if (DECL_CLASS_SCOPE_P (decl)
9923 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
9924 && uses_template_parms (DECL_CONTEXT (decl)))
9926 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
9927 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
9928 r = retrieve_specialization (tmpl, gen_args, 0);
9930 else if (DECL_FUNCTION_SCOPE_P (decl)
9931 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
9932 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
9933 r = retrieve_local_specialization (decl);
9935 /* The typedef is from a non-template context. */
9941 r = cp_build_qualified_type_real
9942 (r, cp_type_quals (t) | cp_type_quals (r),
9943 complain | tf_ignore_bad_quals);
9946 /* Else we must be instantiating the typedef, so fall through. */
9950 && TREE_CODE (t) != TYPENAME_TYPE
9951 && TREE_CODE (t) != TEMPLATE_TYPE_PARM
9952 && TREE_CODE (t) != IDENTIFIER_NODE
9953 && TREE_CODE (t) != FUNCTION_TYPE
9954 && TREE_CODE (t) != METHOD_TYPE)
9955 type = tsubst (type, args, complain, in_decl);
9956 if (type == error_mark_node)
9957 return error_mark_node;
9959 switch (TREE_CODE (t))
9964 return tsubst_aggr_type (t, args, complain, in_decl,
9965 /*entering_scope=*/0);
9968 case IDENTIFIER_NODE:
9980 if (t == integer_type_node)
9983 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
9984 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
9988 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
9990 max = tsubst_expr (omax, args, complain, in_decl,
9991 /*integral_constant_expression_p=*/false);
9993 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
9995 if (TREE_CODE (max) == NOP_EXPR
9996 && TREE_SIDE_EFFECTS (omax)
9997 && !TREE_TYPE (max))
9998 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
10000 max = fold_decl_constant_value (max);
10002 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
10003 with TREE_SIDE_EFFECTS that indicates this is not an integral
10004 constant expression. */
10005 if (processing_template_decl
10006 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
10008 gcc_assert (TREE_CODE (max) == NOP_EXPR);
10009 TREE_SIDE_EFFECTS (max) = 1;
10012 if (TREE_CODE (max) != INTEGER_CST
10013 && !at_function_scope_p ()
10014 && !TREE_SIDE_EFFECTS (max)
10015 && !value_dependent_expression_p (max))
10017 if (complain & tf_error)
10018 error ("array bound is not an integer constant");
10019 return error_mark_node;
10024 Type deduction may fail for any of the following
10027 Attempting to create an array with a size that is
10028 zero or negative. */
10029 if (integer_zerop (max) && !(complain & tf_error))
10030 /* We must fail if performing argument deduction (as
10031 indicated by the state of complain), so that
10032 another substitution can be found. */
10033 return error_mark_node;
10034 else if (TREE_CODE (max) == INTEGER_CST
10035 && INT_CST_LT (max, integer_zero_node))
10037 if (complain & tf_error)
10038 error ("creating array with negative size (%qE)", max);
10040 return error_mark_node;
10043 return compute_array_index_type (NULL_TREE, max);
10046 case TEMPLATE_TYPE_PARM:
10047 case TEMPLATE_TEMPLATE_PARM:
10048 case BOUND_TEMPLATE_TEMPLATE_PARM:
10049 case TEMPLATE_PARM_INDEX:
10054 tree arg = NULL_TREE;
10058 gcc_assert (TREE_VEC_LENGTH (args) > 0);
10059 template_parm_level_and_index (t, &level, &idx);
10061 levels = TMPL_ARGS_DEPTH (args);
10062 if (level <= levels)
10064 arg = TMPL_ARG (args, level, idx);
10066 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
10067 /* See through ARGUMENT_PACK_SELECT arguments. */
10068 arg = ARGUMENT_PACK_SELECT_ARG (arg);
10071 if (arg == error_mark_node)
10072 return error_mark_node;
10073 else if (arg != NULL_TREE)
10075 if (ARGUMENT_PACK_P (arg))
10076 /* If ARG is an argument pack, we don't actually want to
10077 perform a substitution here, because substitutions
10078 for argument packs are only done
10079 element-by-element. We can get to this point when
10080 substituting the type of a non-type template
10081 parameter pack, when that type actually contains
10082 template parameter packs from an outer template, e.g.,
10084 template<typename... Types> struct A {
10085 template<Types... Values> struct B { };
10089 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
10092 gcc_assert (TYPE_P (arg));
10094 /* cv-quals from the template are discarded when
10095 substituting in a function or reference type. */
10096 if (TREE_CODE (arg) == FUNCTION_TYPE
10097 || TREE_CODE (arg) == METHOD_TYPE
10098 || TREE_CODE (arg) == REFERENCE_TYPE)
10099 quals = cp_type_quals (arg);
10101 quals = cp_type_quals (arg) | cp_type_quals (t);
10103 return cp_build_qualified_type_real
10104 (arg, quals, complain | tf_ignore_bad_quals);
10106 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10108 /* We are processing a type constructed from a
10109 template template parameter. */
10110 tree argvec = tsubst (TYPE_TI_ARGS (t),
10111 args, complain, in_decl);
10112 if (argvec == error_mark_node)
10113 return error_mark_node;
10115 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
10116 are resolving nested-types in the signature of a
10117 member function templates. Otherwise ARG is a
10118 TEMPLATE_DECL and is the real template to be
10120 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
10121 arg = TYPE_NAME (arg);
10123 r = lookup_template_class (arg,
10125 DECL_CONTEXT (arg),
10126 /*entering_scope=*/0,
10128 return cp_build_qualified_type_real
10129 (r, TYPE_QUALS (t), complain);
10132 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
10137 /* This can happen during the attempted tsubst'ing in
10138 unify. This means that we don't yet have any information
10139 about the template parameter in question. */
10142 /* If we get here, we must have been looking at a parm for a
10143 more deeply nested template. Make a new version of this
10144 template parameter, but with a lower level. */
10145 switch (TREE_CODE (t))
10147 case TEMPLATE_TYPE_PARM:
10148 case TEMPLATE_TEMPLATE_PARM:
10149 case BOUND_TEMPLATE_TEMPLATE_PARM:
10150 if (cp_type_quals (t))
10152 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
10153 r = cp_build_qualified_type_real
10154 (r, cp_type_quals (t),
10155 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
10156 ? tf_ignore_bad_quals : 0));
10161 TEMPLATE_TYPE_PARM_INDEX (r)
10162 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
10163 r, levels, args, complain);
10164 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
10165 TYPE_MAIN_VARIANT (r) = r;
10166 TYPE_POINTER_TO (r) = NULL_TREE;
10167 TYPE_REFERENCE_TO (r) = NULL_TREE;
10169 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
10170 /* We have reduced the level of the template
10171 template parameter, but not the levels of its
10172 template parameters, so canonical_type_parameter
10173 will not be able to find the canonical template
10174 template parameter for this level. Thus, we
10175 require structural equality checking to compare
10176 TEMPLATE_TEMPLATE_PARMs. */
10177 SET_TYPE_STRUCTURAL_EQUALITY (r);
10178 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
10179 SET_TYPE_STRUCTURAL_EQUALITY (r);
10181 TYPE_CANONICAL (r) = canonical_type_parameter (r);
10183 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10185 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
10186 complain, in_decl);
10187 if (argvec == error_mark_node)
10188 return error_mark_node;
10190 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
10191 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
10196 case TEMPLATE_PARM_INDEX:
10197 r = reduce_template_parm_level (t, type, levels, args, complain);
10201 gcc_unreachable ();
10209 tree purpose, value, chain;
10211 if (t == void_list_node)
10214 purpose = TREE_PURPOSE (t);
10217 purpose = tsubst (purpose, args, complain, in_decl);
10218 if (purpose == error_mark_node)
10219 return error_mark_node;
10221 value = TREE_VALUE (t);
10224 value = tsubst (value, args, complain, in_decl);
10225 if (value == error_mark_node)
10226 return error_mark_node;
10228 chain = TREE_CHAIN (t);
10229 if (chain && chain != void_type_node)
10231 chain = tsubst (chain, args, complain, in_decl);
10232 if (chain == error_mark_node)
10233 return error_mark_node;
10235 if (purpose == TREE_PURPOSE (t)
10236 && value == TREE_VALUE (t)
10237 && chain == TREE_CHAIN (t))
10239 return hash_tree_cons (purpose, value, chain);
10243 /* We should never be tsubsting a binfo. */
10244 gcc_unreachable ();
10247 /* A vector of template arguments. */
10248 gcc_assert (!type);
10249 return tsubst_template_args (t, args, complain, in_decl);
10252 case REFERENCE_TYPE:
10254 enum tree_code code;
10256 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
10259 code = TREE_CODE (t);
10264 Type deduction may fail for any of the following
10267 -- Attempting to create a pointer to reference type.
10268 -- Attempting to create a reference to a reference type or
10269 a reference to void.
10271 Core issue 106 says that creating a reference to a reference
10272 during instantiation is no longer a cause for failure. We
10273 only enforce this check in strict C++98 mode. */
10274 if ((TREE_CODE (type) == REFERENCE_TYPE
10275 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
10276 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
10278 static location_t last_loc;
10280 /* We keep track of the last time we issued this error
10281 message to avoid spewing a ton of messages during a
10282 single bad template instantiation. */
10283 if (complain & tf_error
10284 && last_loc != input_location)
10286 if (TREE_CODE (type) == VOID_TYPE)
10287 error ("forming reference to void");
10288 else if (code == POINTER_TYPE)
10289 error ("forming pointer to reference type %qT", type);
10291 error ("forming reference to reference type %qT", type);
10292 last_loc = input_location;
10295 return error_mark_node;
10297 else if (code == POINTER_TYPE)
10299 r = build_pointer_type (type);
10300 if (TREE_CODE (type) == METHOD_TYPE)
10301 r = build_ptrmemfunc_type (r);
10303 else if (TREE_CODE (type) == REFERENCE_TYPE)
10304 /* In C++0x, during template argument substitution, when there is an
10305 attempt to create a reference to a reference type, reference
10306 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
10308 "If a template-argument for a template-parameter T names a type
10309 that is a reference to a type A, an attempt to create the type
10310 'lvalue reference to cv T' creates the type 'lvalue reference to
10311 A,' while an attempt to create the type type rvalue reference to
10312 cv T' creates the type T"
10314 r = cp_build_reference_type
10316 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
10318 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
10319 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
10321 if (r != error_mark_node)
10322 /* Will this ever be needed for TYPE_..._TO values? */
10329 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
10330 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
10334 Type deduction may fail for any of the following
10337 -- Attempting to create "pointer to member of T" when T
10338 is not a class type. */
10339 if (complain & tf_error)
10340 error ("creating pointer to member of non-class type %qT", r);
10341 return error_mark_node;
10343 if (TREE_CODE (type) == REFERENCE_TYPE)
10345 if (complain & tf_error)
10346 error ("creating pointer to member reference type %qT", type);
10347 return error_mark_node;
10349 if (TREE_CODE (type) == VOID_TYPE)
10351 if (complain & tf_error)
10352 error ("creating pointer to member of type void");
10353 return error_mark_node;
10355 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
10356 if (TREE_CODE (type) == FUNCTION_TYPE)
10358 /* The type of the implicit object parameter gets its
10359 cv-qualifiers from the FUNCTION_TYPE. */
10361 tree method_type = build_memfn_type (type, r, cp_type_quals (type));
10362 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
10363 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
10367 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
10371 case FUNCTION_TYPE:
10376 fntype = tsubst_function_type (t, args, complain, in_decl);
10377 if (fntype == error_mark_node)
10378 return error_mark_node;
10380 /* Substitute the exception specification. */
10381 specs = tsubst_exception_specification (t, args, complain,
10383 if (specs == error_mark_node)
10384 return error_mark_node;
10386 fntype = build_exception_variant (fntype, specs);
10391 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
10392 if (domain == error_mark_node)
10393 return error_mark_node;
10395 /* As an optimization, we avoid regenerating the array type if
10396 it will obviously be the same as T. */
10397 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
10400 /* These checks should match the ones in grokdeclarator.
10404 The deduction may fail for any of the following reasons:
10406 -- Attempting to create an array with an element type that
10407 is void, a function type, or a reference type, or [DR337]
10408 an abstract class type. */
10409 if (TREE_CODE (type) == VOID_TYPE
10410 || TREE_CODE (type) == FUNCTION_TYPE
10411 || TREE_CODE (type) == REFERENCE_TYPE)
10413 if (complain & tf_error)
10414 error ("creating array of %qT", type);
10415 return error_mark_node;
10417 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
10419 if (complain & tf_error)
10420 error ("creating array of %qT, which is an abstract class type",
10422 return error_mark_node;
10425 r = build_cplus_array_type (type, domain);
10427 if (TYPE_USER_ALIGN (t))
10429 TYPE_ALIGN (r) = TYPE_ALIGN (t);
10430 TYPE_USER_ALIGN (r) = 1;
10439 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10440 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10442 if (e1 == error_mark_node || e2 == error_mark_node)
10443 return error_mark_node;
10445 return fold_build2_loc (input_location,
10446 TREE_CODE (t), TREE_TYPE (t), e1, e2);
10452 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10453 if (e == error_mark_node)
10454 return error_mark_node;
10456 return fold_build1_loc (input_location, TREE_CODE (t), TREE_TYPE (t), e);
10459 case TYPENAME_TYPE:
10461 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10462 in_decl, /*entering_scope=*/1);
10463 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
10464 complain, in_decl);
10466 if (ctx == error_mark_node || f == error_mark_node)
10467 return error_mark_node;
10469 if (!MAYBE_CLASS_TYPE_P (ctx))
10471 if (complain & tf_error)
10472 error ("%qT is not a class, struct, or union type", ctx);
10473 return error_mark_node;
10475 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
10477 /* Normally, make_typename_type does not require that the CTX
10478 have complete type in order to allow things like:
10480 template <class T> struct S { typename S<T>::X Y; };
10482 But, such constructs have already been resolved by this
10483 point, so here CTX really should have complete type, unless
10484 it's a partial instantiation. */
10485 if (!(complain & tf_no_class_instantiations))
10486 ctx = complete_type (ctx);
10487 if (!COMPLETE_TYPE_P (ctx))
10489 if (complain & tf_error)
10490 cxx_incomplete_type_error (NULL_TREE, ctx);
10491 return error_mark_node;
10495 f = make_typename_type (ctx, f, typename_type,
10496 (complain & tf_error) | tf_keep_type_decl);
10497 if (f == error_mark_node)
10499 if (TREE_CODE (f) == TYPE_DECL)
10501 complain |= tf_ignore_bad_quals;
10505 if (TREE_CODE (f) != TYPENAME_TYPE)
10507 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
10508 error ("%qT resolves to %qT, which is not an enumeration type",
10510 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
10511 error ("%qT resolves to %qT, which is is not a class type",
10515 return cp_build_qualified_type_real
10516 (f, cp_type_quals (f) | cp_type_quals (t), complain);
10519 case UNBOUND_CLASS_TEMPLATE:
10521 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10522 in_decl, /*entering_scope=*/1);
10523 tree name = TYPE_IDENTIFIER (t);
10524 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
10526 if (ctx == error_mark_node || name == error_mark_node)
10527 return error_mark_node;
10530 parm_list = tsubst_template_parms (parm_list, args, complain);
10531 return make_unbound_class_template (ctx, name, parm_list, complain);
10537 gcc_unreachable ();
10541 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10542 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
10543 /*integral_constant_expression_p=*/false);
10544 if (e1 == error_mark_node || e2 == error_mark_node)
10545 return error_mark_node;
10547 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
10552 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10553 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10554 if (e1 == error_mark_node || e2 == error_mark_node)
10555 return error_mark_node;
10557 return build_qualified_name (/*type=*/NULL_TREE,
10558 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
10565 ++cp_unevaluated_operand;
10566 ++c_inhibit_evaluation_warnings;
10568 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
10570 /*integral_constant_expression_p=*/false);
10572 --cp_unevaluated_operand;
10573 --c_inhibit_evaluation_warnings;
10575 type = finish_typeof (type);
10576 return cp_build_qualified_type_real (type,
10578 | cp_type_quals (type),
10582 case DECLTYPE_TYPE:
10586 ++cp_unevaluated_operand;
10587 ++c_inhibit_evaluation_warnings;
10589 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
10591 /*integral_constant_expression_p=*/false);
10593 --cp_unevaluated_operand;
10594 --c_inhibit_evaluation_warnings;
10596 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
10597 type = lambda_capture_field_type (type);
10598 else if (DECLTYPE_FOR_LAMBDA_RETURN (t))
10599 type = lambda_return_type (type);
10601 type = finish_decltype_type
10602 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
10603 return cp_build_qualified_type_real (type,
10605 | cp_type_quals (type),
10609 case TYPE_ARGUMENT_PACK:
10610 case NONTYPE_ARGUMENT_PACK:
10612 tree r = TYPE_P (t)
10613 ? cxx_make_type (TREE_CODE (t))
10614 : make_node (TREE_CODE (t));
10616 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
10620 SET_ARGUMENT_PACK_ARGS (r, packed_out);
10622 /* For template nontype argument packs, also substitute into
10624 if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
10625 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
10632 sorry ("use of %qs in template",
10633 tree_code_name [(int) TREE_CODE (t)]);
10634 return error_mark_node;
10638 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
10639 type of the expression on the left-hand side of the "." or "->"
10643 tsubst_baselink (tree baselink, tree object_type,
10644 tree args, tsubst_flags_t complain, tree in_decl)
10647 tree qualifying_scope;
10650 tree template_args = 0;
10651 bool template_id_p = false;
10653 /* A baselink indicates a function from a base class. Both the
10654 BASELINK_ACCESS_BINFO and the base class referenced may
10655 indicate bases of the template class, rather than the
10656 instantiated class. In addition, lookups that were not
10657 ambiguous before may be ambiguous now. Therefore, we perform
10658 the lookup again. */
10659 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
10660 qualifying_scope = tsubst (qualifying_scope, args,
10661 complain, in_decl);
10662 fns = BASELINK_FUNCTIONS (baselink);
10663 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
10664 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
10666 template_id_p = true;
10667 template_args = TREE_OPERAND (fns, 1);
10668 fns = TREE_OPERAND (fns, 0);
10670 template_args = tsubst_template_args (template_args, args,
10671 complain, in_decl);
10673 name = DECL_NAME (get_first_fn (fns));
10674 if (IDENTIFIER_TYPENAME_P (name))
10675 name = mangle_conv_op_name_for_type (optype);
10676 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
10678 /* If lookup found a single function, mark it as used at this
10679 point. (If it lookup found multiple functions the one selected
10680 later by overload resolution will be marked as used at that
10682 if (BASELINK_P (baselink))
10683 fns = BASELINK_FUNCTIONS (baselink);
10684 if (!template_id_p && !really_overloaded_fn (fns))
10685 mark_used (OVL_CURRENT (fns));
10687 /* Add back the template arguments, if present. */
10688 if (BASELINK_P (baselink) && template_id_p)
10689 BASELINK_FUNCTIONS (baselink)
10690 = build_nt (TEMPLATE_ID_EXPR,
10691 BASELINK_FUNCTIONS (baselink),
10693 /* Update the conversion operator type. */
10694 BASELINK_OPTYPE (baselink) = optype;
10697 object_type = current_class_type;
10698 return adjust_result_of_qualified_name_lookup (baselink,
10703 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
10704 true if the qualified-id will be a postfix-expression in-and-of
10705 itself; false if more of the postfix-expression follows the
10706 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
10710 tsubst_qualified_id (tree qualified_id, tree args,
10711 tsubst_flags_t complain, tree in_decl,
10712 bool done, bool address_p)
10718 tree template_args;
10720 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
10722 /* Figure out what name to look up. */
10723 name = TREE_OPERAND (qualified_id, 1);
10724 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
10726 is_template = true;
10727 template_args = TREE_OPERAND (name, 1);
10729 template_args = tsubst_template_args (template_args, args,
10730 complain, in_decl);
10731 name = TREE_OPERAND (name, 0);
10735 is_template = false;
10736 template_args = NULL_TREE;
10739 /* Substitute into the qualifying scope. When there are no ARGS, we
10740 are just trying to simplify a non-dependent expression. In that
10741 case the qualifying scope may be dependent, and, in any case,
10742 substituting will not help. */
10743 scope = TREE_OPERAND (qualified_id, 0);
10746 scope = tsubst (scope, args, complain, in_decl);
10747 expr = tsubst_copy (name, args, complain, in_decl);
10752 if (dependent_scope_p (scope))
10753 return build_qualified_name (NULL_TREE, scope, expr,
10754 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
10756 if (!BASELINK_P (name) && !DECL_P (expr))
10758 if (TREE_CODE (expr) == BIT_NOT_EXPR)
10760 /* A BIT_NOT_EXPR is used to represent a destructor. */
10761 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
10763 error ("qualifying type %qT does not match destructor name ~%qT",
10764 scope, TREE_OPERAND (expr, 0));
10765 expr = error_mark_node;
10768 expr = lookup_qualified_name (scope, complete_dtor_identifier,
10769 /*is_type_p=*/0, false);
10772 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
10773 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
10774 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
10776 if (complain & tf_error)
10778 error ("dependent-name %qE is parsed as a non-type, but "
10779 "instantiation yields a type", qualified_id);
10780 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
10782 return error_mark_node;
10788 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
10790 /* Remember that there was a reference to this entity. */
10794 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
10796 if (complain & tf_error)
10797 qualified_name_lookup_error (scope,
10798 TREE_OPERAND (qualified_id, 1),
10799 expr, input_location);
10800 return error_mark_node;
10804 expr = lookup_template_function (expr, template_args);
10806 if (expr == error_mark_node && complain & tf_error)
10807 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
10808 expr, input_location);
10809 else if (TYPE_P (scope))
10811 expr = (adjust_result_of_qualified_name_lookup
10812 (expr, scope, current_class_type));
10813 expr = (finish_qualified_id_expr
10814 (scope, expr, done, address_p,
10815 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
10816 /*template_arg_p=*/false));
10819 /* Expressions do not generally have reference type. */
10820 if (TREE_CODE (expr) != SCOPE_REF
10821 /* However, if we're about to form a pointer-to-member, we just
10822 want the referenced member referenced. */
10823 && TREE_CODE (expr) != OFFSET_REF)
10824 expr = convert_from_reference (expr);
10829 /* Like tsubst, but deals with expressions. This function just replaces
10830 template parms; to finish processing the resultant expression, use
10834 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10836 enum tree_code code;
10839 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
10842 code = TREE_CODE (t);
10847 r = retrieve_local_specialization (t);
10852 /* This can happen for a parameter name used later in a function
10853 declaration (such as in a late-specified return type). Just
10854 make a dummy decl, since it's only used for its type. */
10855 gcc_assert (cp_unevaluated_operand != 0);
10856 /* We copy T because want to tsubst the PARM_DECL only,
10857 not the following PARM_DECLs that are chained to T. */
10859 r = tsubst_decl (c, args, complain);
10860 /* Give it the template pattern as its context; its true context
10861 hasn't been instantiated yet and this is good enough for
10863 DECL_CONTEXT (r) = DECL_CONTEXT (t);
10866 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
10867 r = ARGUMENT_PACK_SELECT_ARG (r);
10876 if (DECL_TEMPLATE_PARM_P (t))
10877 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
10878 /* There is no need to substitute into namespace-scope
10880 if (DECL_NAMESPACE_SCOPE_P (t))
10882 /* If ARGS is NULL, then T is known to be non-dependent. */
10883 if (args == NULL_TREE)
10884 return integral_constant_value (t);
10886 /* Unfortunately, we cannot just call lookup_name here.
10889 template <int I> int f() {
10891 struct S { void g() { E e = a; } };
10894 When we instantiate f<7>::S::g(), say, lookup_name is not
10895 clever enough to find f<7>::a. */
10897 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
10898 /*entering_scope=*/0);
10900 for (v = TYPE_VALUES (enum_type);
10902 v = TREE_CHAIN (v))
10903 if (TREE_PURPOSE (v) == DECL_NAME (t))
10904 return TREE_VALUE (v);
10906 /* We didn't find the name. That should never happen; if
10907 name-lookup found it during preliminary parsing, we
10908 should find it again here during instantiation. */
10909 gcc_unreachable ();
10914 if (DECL_CONTEXT (t))
10918 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
10919 /*entering_scope=*/1);
10920 if (ctx != DECL_CONTEXT (t))
10922 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
10925 if (complain & tf_error)
10926 error ("using invalid field %qD", t);
10927 return error_mark_node;
10936 case FUNCTION_DECL:
10937 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10938 || local_variable_p (t))
10939 t = tsubst (t, args, complain, in_decl);
10944 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
10946 case TEMPLATE_DECL:
10947 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10948 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
10949 args, complain, in_decl);
10950 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
10951 return tsubst (t, args, complain, in_decl);
10952 else if (DECL_CLASS_SCOPE_P (t)
10953 && uses_template_parms (DECL_CONTEXT (t)))
10955 /* Template template argument like the following example need
10958 template <template <class> class TT> struct C {};
10959 template <class T> struct D {
10960 template <class U> struct E {};
10965 We are processing the template argument `E' in #1 for
10966 the template instantiation #2. Originally, `E' is a
10967 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
10968 have to substitute this with one having context `D<int>'. */
10970 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
10971 return lookup_field (context, DECL_NAME(t), 0, false);
10974 /* Ordinary template template argument. */
10978 case REINTERPRET_CAST_EXPR:
10979 case CONST_CAST_EXPR:
10980 case STATIC_CAST_EXPR:
10981 case DYNAMIC_CAST_EXPR:
10984 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10985 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10988 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
10990 /* We only want to compute the number of arguments. */
10991 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
10992 complain, in_decl);
10995 if (TREE_CODE (expanded) == TREE_VEC)
10996 len = TREE_VEC_LENGTH (expanded);
10998 if (expanded == error_mark_node)
10999 return error_mark_node;
11000 else if (PACK_EXPANSION_P (expanded)
11001 || (TREE_CODE (expanded) == TREE_VEC
11003 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
11005 if (TREE_CODE (expanded) == TREE_VEC)
11006 expanded = TREE_VEC_ELT (expanded, len - 1);
11008 if (TYPE_P (expanded))
11009 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
11010 complain & tf_error);
11012 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
11013 complain & tf_error);
11016 return build_int_cst (size_type_node, len);
11022 case TRUTH_NOT_EXPR:
11025 case UNARY_PLUS_EXPR: /* Unary + */
11030 case REALPART_EXPR:
11031 case IMAGPART_EXPR:
11033 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11034 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11036 case COMPONENT_REF:
11041 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
11042 name = TREE_OPERAND (t, 1);
11043 if (TREE_CODE (name) == BIT_NOT_EXPR)
11045 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11046 complain, in_decl);
11047 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11049 else if (TREE_CODE (name) == SCOPE_REF
11050 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
11052 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
11053 complain, in_decl);
11054 name = TREE_OPERAND (name, 1);
11055 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11056 complain, in_decl);
11057 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11058 name = build_qualified_name (/*type=*/NULL_TREE,
11060 /*template_p=*/false);
11062 else if (TREE_CODE (name) == BASELINK)
11063 name = tsubst_baselink (name,
11064 non_reference (TREE_TYPE (object)),
11068 name = tsubst_copy (name, args, complain, in_decl);
11069 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
11075 case TRUNC_DIV_EXPR:
11076 case CEIL_DIV_EXPR:
11077 case FLOOR_DIV_EXPR:
11078 case ROUND_DIV_EXPR:
11079 case EXACT_DIV_EXPR:
11083 case TRUNC_MOD_EXPR:
11084 case FLOOR_MOD_EXPR:
11085 case TRUTH_ANDIF_EXPR:
11086 case TRUTH_ORIF_EXPR:
11087 case TRUTH_AND_EXPR:
11088 case TRUTH_OR_EXPR:
11101 case COMPOUND_EXPR:
11104 case PREDECREMENT_EXPR:
11105 case PREINCREMENT_EXPR:
11106 case POSTDECREMENT_EXPR:
11107 case POSTINCREMENT_EXPR:
11109 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11110 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11113 return build_qualified_name (/*type=*/NULL_TREE,
11114 tsubst_copy (TREE_OPERAND (t, 0),
11115 args, complain, in_decl),
11116 tsubst_copy (TREE_OPERAND (t, 1),
11117 args, complain, in_decl),
11118 QUALIFIED_NAME_IS_TEMPLATE (t));
11123 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11124 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11125 NULL_TREE, NULL_TREE);
11129 int n = VL_EXP_OPERAND_LENGTH (t);
11130 tree result = build_vl_exp (CALL_EXPR, n);
11132 for (i = 0; i < n; i++)
11133 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
11134 complain, in_decl);
11140 case PSEUDO_DTOR_EXPR:
11143 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11144 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11145 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
11146 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
11153 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11154 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11155 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
11156 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
11163 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11164 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11165 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
11166 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
11170 case TEMPLATE_ID_EXPR:
11172 /* Substituted template arguments */
11173 tree fn = TREE_OPERAND (t, 0);
11174 tree targs = TREE_OPERAND (t, 1);
11176 fn = tsubst_copy (fn, args, complain, in_decl);
11178 targs = tsubst_template_args (targs, args, complain, in_decl);
11180 return lookup_template_function (fn, targs);
11185 tree purpose, value, chain;
11187 if (t == void_list_node)
11190 purpose = TREE_PURPOSE (t);
11192 purpose = tsubst_copy (purpose, args, complain, in_decl);
11193 value = TREE_VALUE (t);
11195 value = tsubst_copy (value, args, complain, in_decl);
11196 chain = TREE_CHAIN (t);
11197 if (chain && chain != void_type_node)
11198 chain = tsubst_copy (chain, args, complain, in_decl);
11199 if (purpose == TREE_PURPOSE (t)
11200 && value == TREE_VALUE (t)
11201 && chain == TREE_CHAIN (t))
11203 return tree_cons (purpose, value, chain);
11208 case ENUMERAL_TYPE:
11210 case TEMPLATE_TYPE_PARM:
11211 case TEMPLATE_TEMPLATE_PARM:
11212 case BOUND_TEMPLATE_TEMPLATE_PARM:
11213 case TEMPLATE_PARM_INDEX:
11215 case REFERENCE_TYPE:
11217 case FUNCTION_TYPE:
11220 case TYPENAME_TYPE:
11221 case UNBOUND_CLASS_TEMPLATE:
11223 case DECLTYPE_TYPE:
11225 return tsubst (t, args, complain, in_decl);
11227 case IDENTIFIER_NODE:
11228 if (IDENTIFIER_TYPENAME_P (t))
11230 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11231 return mangle_conv_op_name_for_type (new_type);
11237 /* This is handled by tsubst_copy_and_build. */
11238 gcc_unreachable ();
11241 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
11243 tsubst (TREE_TYPE (t), args, complain, in_decl));
11245 case CLEANUP_POINT_EXPR:
11246 /* We shouldn't have built any of these during initial template
11247 generation. Instead, they should be built during instantiation
11248 in response to the saved STMT_IS_FULL_EXPR_P setting. */
11249 gcc_unreachable ();
11252 mark_used (TREE_OPERAND (t, 1));
11255 case EXPR_PACK_EXPANSION:
11256 error ("invalid use of pack expansion expression");
11257 return error_mark_node;
11259 case NONTYPE_ARGUMENT_PACK:
11260 error ("use %<...%> to expand argument pack");
11261 return error_mark_node;
11268 /* Like tsubst_copy, but specifically for OpenMP clauses. */
11271 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
11274 tree new_clauses = NULL, nc, oc;
11276 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
11278 nc = copy_node (oc);
11279 OMP_CLAUSE_CHAIN (nc) = new_clauses;
11282 switch (OMP_CLAUSE_CODE (nc))
11284 case OMP_CLAUSE_LASTPRIVATE:
11285 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
11287 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
11288 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
11289 in_decl, /*integral_constant_expression_p=*/false);
11290 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
11291 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
11294 case OMP_CLAUSE_PRIVATE:
11295 case OMP_CLAUSE_SHARED:
11296 case OMP_CLAUSE_FIRSTPRIVATE:
11297 case OMP_CLAUSE_REDUCTION:
11298 case OMP_CLAUSE_COPYIN:
11299 case OMP_CLAUSE_COPYPRIVATE:
11300 case OMP_CLAUSE_IF:
11301 case OMP_CLAUSE_NUM_THREADS:
11302 case OMP_CLAUSE_SCHEDULE:
11303 case OMP_CLAUSE_COLLAPSE:
11304 OMP_CLAUSE_OPERAND (nc, 0)
11305 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
11306 in_decl, /*integral_constant_expression_p=*/false);
11308 case OMP_CLAUSE_NOWAIT:
11309 case OMP_CLAUSE_ORDERED:
11310 case OMP_CLAUSE_DEFAULT:
11311 case OMP_CLAUSE_UNTIED:
11314 gcc_unreachable ();
11318 return finish_omp_clauses (nreverse (new_clauses));
11321 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
11324 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
11327 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
11329 tree purpose, value, chain;
11334 if (TREE_CODE (t) != TREE_LIST)
11335 return tsubst_copy_and_build (t, args, complain, in_decl,
11336 /*function_p=*/false,
11337 /*integral_constant_expression_p=*/false);
11339 if (t == void_list_node)
11342 purpose = TREE_PURPOSE (t);
11344 purpose = RECUR (purpose);
11345 value = TREE_VALUE (t);
11346 if (value && TREE_CODE (value) != LABEL_DECL)
11347 value = RECUR (value);
11348 chain = TREE_CHAIN (t);
11349 if (chain && chain != void_type_node)
11350 chain = RECUR (chain);
11351 return tree_cons (purpose, value, chain);
11355 /* Substitute one OMP_FOR iterator. */
11358 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
11359 tree condv, tree incrv, tree *clauses,
11360 tree args, tsubst_flags_t complain, tree in_decl,
11361 bool integral_constant_expression_p)
11363 #define RECUR(NODE) \
11364 tsubst_expr ((NODE), args, complain, in_decl, \
11365 integral_constant_expression_p)
11366 tree decl, init, cond, incr, auto_node;
11368 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
11369 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
11370 decl = RECUR (TREE_OPERAND (init, 0));
11371 init = TREE_OPERAND (init, 1);
11372 auto_node = type_uses_auto (TREE_TYPE (decl));
11373 if (auto_node && init)
11375 tree init_expr = init;
11376 if (TREE_CODE (init_expr) == DECL_EXPR)
11377 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
11378 init_expr = RECUR (init_expr);
11380 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
11382 gcc_assert (!type_dependent_expression_p (decl));
11384 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
11386 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
11387 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11388 if (TREE_CODE (incr) == MODIFY_EXPR)
11389 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
11390 RECUR (TREE_OPERAND (incr, 1)),
11393 incr = RECUR (incr);
11394 TREE_VEC_ELT (declv, i) = decl;
11395 TREE_VEC_ELT (initv, i) = init;
11396 TREE_VEC_ELT (condv, i) = cond;
11397 TREE_VEC_ELT (incrv, i) = incr;
11401 if (init && TREE_CODE (init) != DECL_EXPR)
11404 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11406 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
11407 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
11408 && OMP_CLAUSE_DECL (c) == decl)
11410 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
11411 && OMP_CLAUSE_DECL (c) == decl)
11412 error ("iteration variable %qD should not be firstprivate", decl);
11413 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
11414 && OMP_CLAUSE_DECL (c) == decl)
11415 error ("iteration variable %qD should not be reduction", decl);
11419 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
11420 OMP_CLAUSE_DECL (c) = decl;
11421 c = finish_omp_clauses (c);
11424 OMP_CLAUSE_CHAIN (c) = *clauses;
11429 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
11430 if (COMPARISON_CLASS_P (cond))
11431 cond = build2 (TREE_CODE (cond), boolean_type_node,
11432 RECUR (TREE_OPERAND (cond, 0)),
11433 RECUR (TREE_OPERAND (cond, 1)));
11435 cond = RECUR (cond);
11436 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11437 switch (TREE_CODE (incr))
11439 case PREINCREMENT_EXPR:
11440 case PREDECREMENT_EXPR:
11441 case POSTINCREMENT_EXPR:
11442 case POSTDECREMENT_EXPR:
11443 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
11444 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
11447 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11448 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11450 tree rhs = TREE_OPERAND (incr, 1);
11451 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11452 RECUR (TREE_OPERAND (incr, 0)),
11453 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11454 RECUR (TREE_OPERAND (rhs, 0)),
11455 RECUR (TREE_OPERAND (rhs, 1))));
11458 incr = RECUR (incr);
11461 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11462 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11464 tree lhs = RECUR (TREE_OPERAND (incr, 0));
11465 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
11466 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
11467 TREE_TYPE (decl), lhs,
11468 RECUR (TREE_OPERAND (incr, 2))));
11470 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
11471 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
11472 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
11474 tree rhs = TREE_OPERAND (incr, 2);
11475 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11476 RECUR (TREE_OPERAND (incr, 0)),
11477 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11478 RECUR (TREE_OPERAND (rhs, 0)),
11479 RECUR (TREE_OPERAND (rhs, 1))));
11482 incr = RECUR (incr);
11485 incr = RECUR (incr);
11489 TREE_VEC_ELT (declv, i) = decl;
11490 TREE_VEC_ELT (initv, i) = init;
11491 TREE_VEC_ELT (condv, i) = cond;
11492 TREE_VEC_ELT (incrv, i) = incr;
11496 /* Like tsubst_copy for expressions, etc. but also does semantic
11500 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
11501 bool integral_constant_expression_p)
11503 #define RECUR(NODE) \
11504 tsubst_expr ((NODE), args, complain, in_decl, \
11505 integral_constant_expression_p)
11509 if (t == NULL_TREE || t == error_mark_node)
11512 if (EXPR_HAS_LOCATION (t))
11513 input_location = EXPR_LOCATION (t);
11514 if (STATEMENT_CODE_P (TREE_CODE (t)))
11515 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
11517 switch (TREE_CODE (t))
11519 case STATEMENT_LIST:
11521 tree_stmt_iterator i;
11522 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
11523 RECUR (tsi_stmt (i));
11527 case CTOR_INITIALIZER:
11528 finish_mem_initializers (tsubst_initializer_list
11529 (TREE_OPERAND (t, 0), args));
11533 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
11537 tmp = RECUR (EXPR_STMT_EXPR (t));
11538 if (EXPR_STMT_STMT_EXPR_RESULT (t))
11539 finish_stmt_expr_expr (tmp, cur_stmt_expr);
11541 finish_expr_stmt (tmp);
11545 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
11553 decl = DECL_EXPR_DECL (t);
11554 if (TREE_CODE (decl) == LABEL_DECL)
11555 finish_label_decl (DECL_NAME (decl));
11556 else if (TREE_CODE (decl) == USING_DECL)
11558 tree scope = USING_DECL_SCOPE (decl);
11559 tree name = DECL_NAME (decl);
11562 scope = RECUR (scope);
11563 decl = lookup_qualified_name (scope, name,
11564 /*is_type_p=*/false,
11565 /*complain=*/false);
11566 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
11567 qualified_name_lookup_error (scope, name, decl, input_location);
11569 do_local_using_decl (decl, scope, name);
11573 init = DECL_INITIAL (decl);
11574 decl = tsubst (decl, args, complain, in_decl);
11575 if (decl != error_mark_node)
11577 /* By marking the declaration as instantiated, we avoid
11578 trying to instantiate it. Since instantiate_decl can't
11579 handle local variables, and since we've already done
11580 all that needs to be done, that's the right thing to
11582 if (TREE_CODE (decl) == VAR_DECL)
11583 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11584 if (TREE_CODE (decl) == VAR_DECL
11585 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
11586 /* Anonymous aggregates are a special case. */
11587 finish_anon_union (decl);
11590 maybe_push_decl (decl);
11591 if (TREE_CODE (decl) == VAR_DECL
11592 && DECL_PRETTY_FUNCTION_P (decl))
11594 /* For __PRETTY_FUNCTION__ we have to adjust the
11596 const char *const name
11597 = cxx_printable_name (current_function_decl, 2);
11598 init = cp_fname_init (name, &TREE_TYPE (decl));
11602 tree t = RECUR (init);
11605 /* If we had an initializer but it
11606 instantiated to nothing,
11607 value-initialize the object. This will
11608 only occur when the initializer was a
11609 pack expansion where the parameter packs
11610 used in that expansion were of length
11612 init = build_value_init (TREE_TYPE (decl));
11617 cp_finish_decl (decl, init, false, NULL_TREE, 0);
11622 /* A DECL_EXPR can also be used as an expression, in the condition
11623 clause of an if/for/while construct. */
11628 stmt = begin_for_stmt ();
11629 RECUR (FOR_INIT_STMT (t));
11630 finish_for_init_stmt (stmt);
11631 tmp = RECUR (FOR_COND (t));
11632 finish_for_cond (tmp, stmt);
11633 tmp = RECUR (FOR_EXPR (t));
11634 finish_for_expr (tmp, stmt);
11635 RECUR (FOR_BODY (t));
11636 finish_for_stmt (stmt);
11640 stmt = begin_while_stmt ();
11641 tmp = RECUR (WHILE_COND (t));
11642 finish_while_stmt_cond (tmp, stmt);
11643 RECUR (WHILE_BODY (t));
11644 finish_while_stmt (stmt);
11648 stmt = begin_do_stmt ();
11649 RECUR (DO_BODY (t));
11650 finish_do_body (stmt);
11651 tmp = RECUR (DO_COND (t));
11652 finish_do_stmt (tmp, stmt);
11656 stmt = begin_if_stmt ();
11657 tmp = RECUR (IF_COND (t));
11658 finish_if_stmt_cond (tmp, stmt);
11659 RECUR (THEN_CLAUSE (t));
11660 finish_then_clause (stmt);
11662 if (ELSE_CLAUSE (t))
11664 begin_else_clause (stmt);
11665 RECUR (ELSE_CLAUSE (t));
11666 finish_else_clause (stmt);
11669 finish_if_stmt (stmt);
11673 if (BIND_EXPR_BODY_BLOCK (t))
11674 stmt = begin_function_body ();
11676 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
11677 ? BCS_TRY_BLOCK : 0);
11679 RECUR (BIND_EXPR_BODY (t));
11681 if (BIND_EXPR_BODY_BLOCK (t))
11682 finish_function_body (stmt);
11684 finish_compound_stmt (stmt);
11688 finish_break_stmt ();
11691 case CONTINUE_STMT:
11692 finish_continue_stmt ();
11696 stmt = begin_switch_stmt ();
11697 tmp = RECUR (SWITCH_STMT_COND (t));
11698 finish_switch_cond (tmp, stmt);
11699 RECUR (SWITCH_STMT_BODY (t));
11700 finish_switch_stmt (stmt);
11703 case CASE_LABEL_EXPR:
11704 finish_case_label (EXPR_LOCATION (t),
11705 RECUR (CASE_LOW (t)),
11706 RECUR (CASE_HIGH (t)));
11711 tree decl = LABEL_EXPR_LABEL (t);
11714 label = finish_label_stmt (DECL_NAME (decl));
11715 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
11716 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
11721 tmp = GOTO_DESTINATION (t);
11722 if (TREE_CODE (tmp) != LABEL_DECL)
11723 /* Computed goto's must be tsubst'd into. On the other hand,
11724 non-computed gotos must not be; the identifier in question
11725 will have no binding. */
11728 tmp = DECL_NAME (tmp);
11729 finish_goto_stmt (tmp);
11733 tmp = finish_asm_stmt
11734 (ASM_VOLATILE_P (t),
11735 RECUR (ASM_STRING (t)),
11736 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
11737 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
11738 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
11739 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
11741 tree asm_expr = tmp;
11742 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
11743 asm_expr = TREE_OPERAND (asm_expr, 0);
11744 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
11751 stmt = begin_try_block ();
11752 RECUR (TRY_STMTS (t));
11753 finish_cleanup_try_block (stmt);
11754 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
11758 tree compound_stmt = NULL_TREE;
11760 if (FN_TRY_BLOCK_P (t))
11761 stmt = begin_function_try_block (&compound_stmt);
11763 stmt = begin_try_block ();
11765 RECUR (TRY_STMTS (t));
11767 if (FN_TRY_BLOCK_P (t))
11768 finish_function_try_block (stmt);
11770 finish_try_block (stmt);
11772 RECUR (TRY_HANDLERS (t));
11773 if (FN_TRY_BLOCK_P (t))
11774 finish_function_handler_sequence (stmt, compound_stmt);
11776 finish_handler_sequence (stmt);
11782 tree decl = HANDLER_PARMS (t);
11786 decl = tsubst (decl, args, complain, in_decl);
11787 /* Prevent instantiate_decl from trying to instantiate
11788 this variable. We've already done all that needs to be
11790 if (decl != error_mark_node)
11791 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11793 stmt = begin_handler ();
11794 finish_handler_parms (decl, stmt);
11795 RECUR (HANDLER_BODY (t));
11796 finish_handler (stmt);
11801 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
11804 case STATIC_ASSERT:
11807 tsubst_expr (STATIC_ASSERT_CONDITION (t),
11810 /*integral_constant_expression_p=*/true);
11811 finish_static_assert (condition,
11812 STATIC_ASSERT_MESSAGE (t),
11813 STATIC_ASSERT_SOURCE_LOCATION (t),
11814 /*member_p=*/false);
11819 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
11820 args, complain, in_decl);
11821 stmt = begin_omp_parallel ();
11822 RECUR (OMP_PARALLEL_BODY (t));
11823 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
11824 = OMP_PARALLEL_COMBINED (t);
11828 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
11829 args, complain, in_decl);
11830 stmt = begin_omp_task ();
11831 RECUR (OMP_TASK_BODY (t));
11832 finish_omp_task (tmp, stmt);
11837 tree clauses, body, pre_body;
11838 tree declv, initv, condv, incrv;
11841 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
11842 args, complain, in_decl);
11843 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11844 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11845 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11846 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11848 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
11849 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
11850 &clauses, args, complain, in_decl,
11851 integral_constant_expression_p);
11853 stmt = begin_omp_structured_block ();
11855 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
11856 if (TREE_VEC_ELT (initv, i) == NULL
11857 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
11858 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
11859 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
11861 tree init = RECUR (TREE_VEC_ELT (initv, i));
11862 gcc_assert (init == TREE_VEC_ELT (declv, i));
11863 TREE_VEC_ELT (initv, i) = NULL_TREE;
11867 tree decl_expr = TREE_VEC_ELT (initv, i);
11868 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
11869 gcc_assert (init != NULL);
11870 TREE_VEC_ELT (initv, i) = RECUR (init);
11871 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
11873 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
11876 pre_body = push_stmt_list ();
11877 RECUR (OMP_FOR_PRE_BODY (t));
11878 pre_body = pop_stmt_list (pre_body);
11880 body = push_stmt_list ();
11881 RECUR (OMP_FOR_BODY (t));
11882 body = pop_stmt_list (body);
11884 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
11885 body, pre_body, clauses);
11887 add_stmt (finish_omp_structured_block (stmt));
11893 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
11894 stmt = push_stmt_list ();
11895 RECUR (OMP_BODY (t));
11896 stmt = pop_stmt_list (stmt);
11899 OMP_BODY (t) = stmt;
11900 OMP_CLAUSES (t) = tmp;
11908 stmt = push_stmt_list ();
11909 RECUR (OMP_BODY (t));
11910 stmt = pop_stmt_list (stmt);
11913 OMP_BODY (t) = stmt;
11918 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
11920 tree op1 = TREE_OPERAND (t, 1);
11921 tree lhs = RECUR (TREE_OPERAND (op1, 0));
11922 tree rhs = RECUR (TREE_OPERAND (op1, 1));
11923 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
11927 case EXPR_PACK_EXPANSION:
11928 error ("invalid use of pack expansion expression");
11929 return error_mark_node;
11931 case NONTYPE_ARGUMENT_PACK:
11932 error ("use %<...%> to expand argument pack");
11933 return error_mark_node;
11936 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
11938 return tsubst_copy_and_build (t, args, complain, in_decl,
11939 /*function_p=*/false,
11940 integral_constant_expression_p);
11947 /* T is a postfix-expression that is not being used in a function
11948 call. Return the substituted version of T. */
11951 tsubst_non_call_postfix_expression (tree t, tree args,
11952 tsubst_flags_t complain,
11955 if (TREE_CODE (t) == SCOPE_REF)
11956 t = tsubst_qualified_id (t, args, complain, in_decl,
11957 /*done=*/false, /*address_p=*/false);
11959 t = tsubst_copy_and_build (t, args, complain, in_decl,
11960 /*function_p=*/false,
11961 /*integral_constant_expression_p=*/false);
11966 /* Like tsubst but deals with expressions and performs semantic
11967 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
11970 tsubst_copy_and_build (tree t,
11972 tsubst_flags_t complain,
11975 bool integral_constant_expression_p)
11977 #define RECUR(NODE) \
11978 tsubst_copy_and_build (NODE, args, complain, in_decl, \
11979 /*function_p=*/false, \
11980 integral_constant_expression_p)
11984 if (t == NULL_TREE || t == error_mark_node)
11987 switch (TREE_CODE (t))
11991 /* Fall through. */
11992 case IDENTIFIER_NODE:
11996 bool non_integral_constant_expression_p;
11997 const char *error_msg;
11999 if (IDENTIFIER_TYPENAME_P (t))
12001 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12002 t = mangle_conv_op_name_for_type (new_type);
12005 /* Look up the name. */
12006 decl = lookup_name (t);
12008 /* By convention, expressions use ERROR_MARK_NODE to indicate
12009 failure, not NULL_TREE. */
12010 if (decl == NULL_TREE)
12011 decl = error_mark_node;
12013 decl = finish_id_expression (t, decl, NULL_TREE,
12015 integral_constant_expression_p,
12016 /*allow_non_integral_constant_expression_p=*/false,
12017 &non_integral_constant_expression_p,
12018 /*template_p=*/false,
12020 /*address_p=*/false,
12021 /*template_arg_p=*/false,
12026 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
12027 decl = unqualified_name_lookup_error (decl);
12031 case TEMPLATE_ID_EXPR:
12034 tree templ = RECUR (TREE_OPERAND (t, 0));
12035 tree targs = TREE_OPERAND (t, 1);
12038 targs = tsubst_template_args (targs, args, complain, in_decl);
12040 if (TREE_CODE (templ) == COMPONENT_REF)
12042 object = TREE_OPERAND (templ, 0);
12043 templ = TREE_OPERAND (templ, 1);
12046 object = NULL_TREE;
12047 templ = lookup_template_function (templ, targs);
12050 return build3 (COMPONENT_REF, TREE_TYPE (templ),
12051 object, templ, NULL_TREE);
12053 return baselink_for_fns (templ);
12058 tree r = RECUR (TREE_OPERAND (t, 0));
12060 if (REFERENCE_REF_P (t))
12062 /* A type conversion to reference type will be enclosed in
12063 such an indirect ref, but the substitution of the cast
12064 will have also added such an indirect ref. */
12065 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
12066 r = convert_from_reference (r);
12069 r = build_x_indirect_ref (r, RO_UNARY_STAR, complain);
12075 (tsubst (TREE_TYPE (t), args, complain, in_decl),
12076 RECUR (TREE_OPERAND (t, 0)));
12079 case REINTERPRET_CAST_EXPR:
12080 case CONST_CAST_EXPR:
12081 case DYNAMIC_CAST_EXPR:
12082 case STATIC_CAST_EXPR:
12087 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12088 if (integral_constant_expression_p
12089 && !cast_valid_in_integral_constant_expression_p (type))
12091 if (complain & tf_error)
12092 error ("a cast to a type other than an integral or "
12093 "enumeration type cannot appear in a constant-expression");
12094 return error_mark_node;
12097 op = RECUR (TREE_OPERAND (t, 0));
12099 switch (TREE_CODE (t))
12102 return build_functional_cast (type, op, complain);
12103 case REINTERPRET_CAST_EXPR:
12104 return build_reinterpret_cast (type, op, complain);
12105 case CONST_CAST_EXPR:
12106 return build_const_cast (type, op, complain);
12107 case DYNAMIC_CAST_EXPR:
12108 return build_dynamic_cast (type, op, complain);
12109 case STATIC_CAST_EXPR:
12110 return build_static_cast (type, op, complain);
12112 gcc_unreachable ();
12116 case POSTDECREMENT_EXPR:
12117 case POSTINCREMENT_EXPR:
12118 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12119 args, complain, in_decl);
12120 return build_x_unary_op (TREE_CODE (t), op1, complain);
12122 case PREDECREMENT_EXPR:
12123 case PREINCREMENT_EXPR:
12127 case TRUTH_NOT_EXPR:
12128 case UNARY_PLUS_EXPR: /* Unary + */
12129 case REALPART_EXPR:
12130 case IMAGPART_EXPR:
12131 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
12135 op1 = TREE_OPERAND (t, 0);
12136 if (TREE_CODE (op1) == SCOPE_REF)
12137 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
12138 /*done=*/true, /*address_p=*/true);
12140 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
12142 if (TREE_CODE (op1) == LABEL_DECL)
12143 return finish_label_address_expr (DECL_NAME (op1),
12144 EXPR_LOCATION (op1));
12145 return build_x_unary_op (ADDR_EXPR, op1, complain);
12150 case TRUNC_DIV_EXPR:
12151 case CEIL_DIV_EXPR:
12152 case FLOOR_DIV_EXPR:
12153 case ROUND_DIV_EXPR:
12154 case EXACT_DIV_EXPR:
12158 case TRUNC_MOD_EXPR:
12159 case FLOOR_MOD_EXPR:
12160 case TRUTH_ANDIF_EXPR:
12161 case TRUTH_ORIF_EXPR:
12162 case TRUTH_AND_EXPR:
12163 case TRUTH_OR_EXPR:
12178 return build_x_binary_op
12180 RECUR (TREE_OPERAND (t, 0)),
12181 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
12183 : TREE_CODE (TREE_OPERAND (t, 0))),
12184 RECUR (TREE_OPERAND (t, 1)),
12185 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
12187 : TREE_CODE (TREE_OPERAND (t, 1))),
12188 /*overloaded_p=*/NULL,
12192 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
12193 /*address_p=*/false);
12195 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12196 args, complain, in_decl);
12197 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
12200 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12201 return tsubst_copy (t, args, complain, in_decl);
12205 op1 = TREE_OPERAND (t, 0);
12208 /* When there are no ARGS, we are trying to evaluate a
12209 non-dependent expression from the parser. Trying to do
12210 the substitutions may not work. */
12212 op1 = TREE_TYPE (op1);
12216 ++cp_unevaluated_operand;
12217 ++c_inhibit_evaluation_warnings;
12218 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12219 /*function_p=*/false,
12220 /*integral_constant_expression_p=*/false);
12221 --cp_unevaluated_operand;
12222 --c_inhibit_evaluation_warnings;
12225 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
12226 complain & tf_error);
12228 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
12229 complain & tf_error);
12233 tree r = build_x_modify_expr
12234 (RECUR (TREE_OPERAND (t, 0)),
12235 TREE_CODE (TREE_OPERAND (t, 1)),
12236 RECUR (TREE_OPERAND (t, 2)),
12238 /* TREE_NO_WARNING must be set if either the expression was
12239 parenthesized or it uses an operator such as >>= rather
12240 than plain assignment. In the former case, it was already
12241 set and must be copied. In the latter case,
12242 build_x_modify_expr sets it and it must not be reset
12244 if (TREE_NO_WARNING (t))
12245 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12250 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12251 args, complain, in_decl);
12252 /* Remember that there was a reference to this entity. */
12255 return build_x_arrow (op1);
12259 tree placement = RECUR (TREE_OPERAND (t, 0));
12260 tree init = RECUR (TREE_OPERAND (t, 3));
12261 VEC(tree,gc) *placement_vec;
12262 VEC(tree,gc) *init_vec;
12265 if (placement == NULL_TREE)
12266 placement_vec = NULL;
12269 placement_vec = make_tree_vector ();
12270 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
12271 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
12274 /* If there was an initializer in the original tree, but it
12275 instantiated to an empty list, then we should pass a
12276 non-NULL empty vector to tell build_new that it was an
12277 empty initializer() rather than no initializer. This can
12278 only happen when the initializer is a pack expansion whose
12279 parameter packs are of length zero. */
12280 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
12284 init_vec = make_tree_vector ();
12285 if (init == void_zero_node)
12286 gcc_assert (init_vec != NULL);
12289 for (; init != NULL_TREE; init = TREE_CHAIN (init))
12290 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
12294 ret = build_new (&placement_vec,
12295 RECUR (TREE_OPERAND (t, 1)),
12296 RECUR (TREE_OPERAND (t, 2)),
12298 NEW_EXPR_USE_GLOBAL (t),
12301 if (placement_vec != NULL)
12302 release_tree_vector (placement_vec);
12303 if (init_vec != NULL)
12304 release_tree_vector (init_vec);
12310 return delete_sanity
12311 (RECUR (TREE_OPERAND (t, 0)),
12312 RECUR (TREE_OPERAND (t, 1)),
12313 DELETE_EXPR_USE_VEC (t),
12314 DELETE_EXPR_USE_GLOBAL (t));
12316 case COMPOUND_EXPR:
12317 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
12318 RECUR (TREE_OPERAND (t, 1)),
12324 VEC(tree,gc) *call_args;
12325 unsigned int nargs, i;
12330 function = CALL_EXPR_FN (t);
12331 /* When we parsed the expression, we determined whether or
12332 not Koenig lookup should be performed. */
12333 koenig_p = KOENIG_LOOKUP_P (t);
12334 if (TREE_CODE (function) == SCOPE_REF)
12336 qualified_p = true;
12337 function = tsubst_qualified_id (function, args, complain, in_decl,
12339 /*address_p=*/false);
12343 if (TREE_CODE (function) == COMPONENT_REF)
12345 tree op = TREE_OPERAND (function, 1);
12347 qualified_p = (TREE_CODE (op) == SCOPE_REF
12348 || (BASELINK_P (op)
12349 && BASELINK_QUALIFIED_P (op)));
12352 qualified_p = false;
12354 function = tsubst_copy_and_build (function, args, complain,
12357 integral_constant_expression_p);
12359 if (BASELINK_P (function))
12360 qualified_p = true;
12363 nargs = call_expr_nargs (t);
12364 call_args = make_tree_vector ();
12365 for (i = 0; i < nargs; ++i)
12367 tree arg = CALL_EXPR_ARG (t, i);
12369 if (!PACK_EXPANSION_P (arg))
12370 VEC_safe_push (tree, gc, call_args,
12371 RECUR (CALL_EXPR_ARG (t, i)));
12374 /* Expand the pack expansion and push each entry onto
12376 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
12377 if (TREE_CODE (arg) == TREE_VEC)
12379 unsigned int len, j;
12381 len = TREE_VEC_LENGTH (arg);
12382 for (j = 0; j < len; ++j)
12384 tree value = TREE_VEC_ELT (arg, j);
12385 if (value != NULL_TREE)
12386 value = convert_from_reference (value);
12387 VEC_safe_push (tree, gc, call_args, value);
12392 /* A partial substitution. Add one entry. */
12393 VEC_safe_push (tree, gc, call_args, arg);
12398 /* We do not perform argument-dependent lookup if normal
12399 lookup finds a non-function, in accordance with the
12400 expected resolution of DR 218. */
12402 && ((is_overloaded_fn (function)
12403 /* If lookup found a member function, the Koenig lookup is
12404 not appropriate, even if an unqualified-name was used
12405 to denote the function. */
12406 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
12407 || TREE_CODE (function) == IDENTIFIER_NODE)
12408 /* Only do this when substitution turns a dependent call
12409 into a non-dependent call. */
12410 && type_dependent_expression_p_push (t)
12411 && !any_type_dependent_arguments_p (call_args))
12412 function = perform_koenig_lookup (function, call_args);
12414 if (TREE_CODE (function) == IDENTIFIER_NODE)
12416 unqualified_name_lookup_error (function);
12417 release_tree_vector (call_args);
12418 return error_mark_node;
12421 /* Remember that there was a reference to this entity. */
12422 if (DECL_P (function))
12423 mark_used (function);
12425 if (TREE_CODE (function) == OFFSET_REF)
12426 ret = build_offset_ref_call_from_tree (function, &call_args);
12427 else if (TREE_CODE (function) == COMPONENT_REF)
12429 if (!BASELINK_P (TREE_OPERAND (function, 1)))
12430 ret = finish_call_expr (function, &call_args,
12431 /*disallow_virtual=*/false,
12432 /*koenig_p=*/false,
12435 ret = (build_new_method_call
12436 (TREE_OPERAND (function, 0),
12437 TREE_OPERAND (function, 1),
12438 &call_args, NULL_TREE,
12439 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
12444 ret = finish_call_expr (function, &call_args,
12445 /*disallow_virtual=*/qualified_p,
12449 release_tree_vector (call_args);
12455 return build_x_conditional_expr
12456 (RECUR (TREE_OPERAND (t, 0)),
12457 RECUR (TREE_OPERAND (t, 1)),
12458 RECUR (TREE_OPERAND (t, 2)),
12461 case PSEUDO_DTOR_EXPR:
12462 return finish_pseudo_destructor_expr
12463 (RECUR (TREE_OPERAND (t, 0)),
12464 RECUR (TREE_OPERAND (t, 1)),
12465 RECUR (TREE_OPERAND (t, 2)));
12469 tree purpose, value, chain;
12471 if (t == void_list_node)
12474 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
12475 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
12477 /* We have pack expansions, so expand those and
12478 create a new list out of it. */
12479 tree purposevec = NULL_TREE;
12480 tree valuevec = NULL_TREE;
12484 /* Expand the argument expressions. */
12485 if (TREE_PURPOSE (t))
12486 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
12487 complain, in_decl);
12488 if (TREE_VALUE (t))
12489 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
12490 complain, in_decl);
12492 /* Build the rest of the list. */
12493 chain = TREE_CHAIN (t);
12494 if (chain && chain != void_type_node)
12495 chain = RECUR (chain);
12497 /* Determine the number of arguments. */
12498 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
12500 len = TREE_VEC_LENGTH (purposevec);
12501 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
12503 else if (TREE_CODE (valuevec) == TREE_VEC)
12504 len = TREE_VEC_LENGTH (valuevec);
12507 /* Since we only performed a partial substitution into
12508 the argument pack, we only return a single list
12510 if (purposevec == TREE_PURPOSE (t)
12511 && valuevec == TREE_VALUE (t)
12512 && chain == TREE_CHAIN (t))
12515 return tree_cons (purposevec, valuevec, chain);
12518 /* Convert the argument vectors into a TREE_LIST */
12522 /* Grab the Ith values. */
12524 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
12527 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
12530 /* Build the list (backwards). */
12531 chain = tree_cons (purpose, value, chain);
12537 purpose = TREE_PURPOSE (t);
12539 purpose = RECUR (purpose);
12540 value = TREE_VALUE (t);
12542 value = RECUR (value);
12543 chain = TREE_CHAIN (t);
12544 if (chain && chain != void_type_node)
12545 chain = RECUR (chain);
12546 if (purpose == TREE_PURPOSE (t)
12547 && value == TREE_VALUE (t)
12548 && chain == TREE_CHAIN (t))
12550 return tree_cons (purpose, value, chain);
12553 case COMPONENT_REF:
12559 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12560 args, complain, in_decl);
12561 /* Remember that there was a reference to this entity. */
12562 if (DECL_P (object))
12563 mark_used (object);
12564 object_type = TREE_TYPE (object);
12566 member = TREE_OPERAND (t, 1);
12567 if (BASELINK_P (member))
12568 member = tsubst_baselink (member,
12569 non_reference (TREE_TYPE (object)),
12570 args, complain, in_decl);
12572 member = tsubst_copy (member, args, complain, in_decl);
12573 if (member == error_mark_node)
12574 return error_mark_node;
12576 if (object_type && !CLASS_TYPE_P (object_type))
12578 if (SCALAR_TYPE_P (object_type))
12580 tree s = NULL_TREE;
12581 tree dtor = member;
12583 if (TREE_CODE (dtor) == SCOPE_REF)
12585 s = TREE_OPERAND (dtor, 0);
12586 dtor = TREE_OPERAND (dtor, 1);
12588 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
12590 dtor = TREE_OPERAND (dtor, 0);
12592 return finish_pseudo_destructor_expr (object, s, dtor);
12596 else if (TREE_CODE (member) == SCOPE_REF
12597 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
12602 /* Lookup the template functions now that we know what the
12604 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
12605 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
12606 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
12607 /*is_type_p=*/false,
12608 /*complain=*/false);
12609 if (BASELINK_P (member))
12611 BASELINK_FUNCTIONS (member)
12612 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
12614 member = (adjust_result_of_qualified_name_lookup
12615 (member, BINFO_TYPE (BASELINK_BINFO (member)),
12620 qualified_name_lookup_error (object_type, tmpl, member,
12622 return error_mark_node;
12625 else if (TREE_CODE (member) == SCOPE_REF
12626 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
12627 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
12629 if (complain & tf_error)
12631 if (TYPE_P (TREE_OPERAND (member, 0)))
12632 error ("%qT is not a class or namespace",
12633 TREE_OPERAND (member, 0));
12635 error ("%qD is not a class or namespace",
12636 TREE_OPERAND (member, 0));
12638 return error_mark_node;
12640 else if (TREE_CODE (member) == FIELD_DECL)
12641 return finish_non_static_data_member (member, object, NULL_TREE);
12643 return finish_class_member_access_expr (object, member,
12644 /*template_p=*/false,
12650 (RECUR (TREE_OPERAND (t, 0)));
12654 VEC(constructor_elt,gc) *n;
12655 constructor_elt *ce;
12656 unsigned HOST_WIDE_INT idx;
12657 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12658 bool process_index_p;
12660 bool need_copy_p = false;
12663 if (type == error_mark_node)
12664 return error_mark_node;
12666 /* digest_init will do the wrong thing if we let it. */
12667 if (type && TYPE_PTRMEMFUNC_P (type))
12670 /* We do not want to process the index of aggregate
12671 initializers as they are identifier nodes which will be
12672 looked up by digest_init. */
12673 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
12675 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
12676 newlen = VEC_length (constructor_elt, n);
12677 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
12679 if (ce->index && process_index_p)
12680 ce->index = RECUR (ce->index);
12682 if (PACK_EXPANSION_P (ce->value))
12684 /* Substitute into the pack expansion. */
12685 ce->value = tsubst_pack_expansion (ce->value, args, complain,
12688 if (ce->value == error_mark_node)
12690 else if (TREE_VEC_LENGTH (ce->value) == 1)
12691 /* Just move the argument into place. */
12692 ce->value = TREE_VEC_ELT (ce->value, 0);
12695 /* Update the length of the final CONSTRUCTOR
12696 arguments vector, and note that we will need to
12698 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
12699 need_copy_p = true;
12703 ce->value = RECUR (ce->value);
12708 VEC(constructor_elt,gc) *old_n = n;
12710 n = VEC_alloc (constructor_elt, gc, newlen);
12711 for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
12714 if (TREE_CODE (ce->value) == TREE_VEC)
12716 int i, len = TREE_VEC_LENGTH (ce->value);
12717 for (i = 0; i < len; ++i)
12718 CONSTRUCTOR_APPEND_ELT (n, 0,
12719 TREE_VEC_ELT (ce->value, i));
12722 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
12726 r = build_constructor (init_list_type_node, n);
12727 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
12729 if (TREE_HAS_CONSTRUCTOR (t))
12730 return finish_compound_literal (type, r);
12737 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
12738 if (TYPE_P (operand_0))
12739 return get_typeid (operand_0);
12740 return build_typeid (operand_0);
12750 tree r = tsubst_copy (t, args, complain, in_decl);
12752 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
12753 /* If the original type was a reference, we'll be wrapped in
12754 the appropriate INDIRECT_REF. */
12755 r = convert_from_reference (r);
12760 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
12761 tsubst_copy (TREE_TYPE (t), args, complain,
12764 case OFFSETOF_EXPR:
12765 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
12769 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
12770 complain, in_decl);
12772 tree type2 = TRAIT_EXPR_TYPE2 (t);
12774 type2 = tsubst_copy (type2, args, complain, in_decl);
12776 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
12781 tree old_stmt_expr = cur_stmt_expr;
12782 tree stmt_expr = begin_stmt_expr ();
12784 cur_stmt_expr = stmt_expr;
12785 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
12786 integral_constant_expression_p);
12787 stmt_expr = finish_stmt_expr (stmt_expr, false);
12788 cur_stmt_expr = old_stmt_expr;
12790 /* If the resulting list of expression statement is empty,
12791 fold it further into void_zero_node. */
12792 if (empty_expr_stmt_p (stmt_expr))
12793 stmt_expr = void_zero_node;
12799 t = tsubst_copy (t, args, complain, in_decl);
12800 /* As in finish_id_expression, we resolve enumeration constants
12801 to their underlying values. */
12802 if (TREE_CODE (t) == CONST_DECL)
12804 used_types_insert (TREE_TYPE (t));
12805 return DECL_INITIAL (t);
12811 tree r = build_lambda_expr ();
12813 tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12814 TREE_TYPE (r) = type;
12815 CLASSTYPE_LAMBDA_EXPR (type) = r;
12817 LAMBDA_EXPR_LOCATION (r)
12818 = LAMBDA_EXPR_LOCATION (t);
12819 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
12820 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
12821 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
12822 LAMBDA_EXPR_DISCRIMINATOR (r)
12823 = (LAMBDA_EXPR_DISCRIMINATOR (t));
12824 LAMBDA_EXPR_CAPTURE_LIST (r)
12825 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
12826 LAMBDA_EXPR_THIS_CAPTURE (r)
12827 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t));
12828 LAMBDA_EXPR_EXTRA_SCOPE (r)
12829 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
12831 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
12832 determine_visibility (TYPE_NAME (type));
12833 /* Now that we know visibility, instantiate the type so we have a
12834 declaration of the op() for later calls to lambda_function. */
12835 complete_type (type);
12837 type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
12839 apply_lambda_return_type (r, type);
12841 return build_lambda_object (r);
12845 /* Handle Objective-C++ constructs, if appropriate. */
12848 = objcp_tsubst_copy_and_build (t, args, complain,
12849 in_decl, /*function_p=*/false);
12853 return tsubst_copy (t, args, complain, in_decl);
12859 /* Verify that the instantiated ARGS are valid. For type arguments,
12860 make sure that the type's linkage is ok. For non-type arguments,
12861 make sure they are constants if they are integral or enumerations.
12862 Emit an error under control of COMPLAIN, and return TRUE on error. */
12865 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
12867 if (ARGUMENT_PACK_P (t))
12869 tree vec = ARGUMENT_PACK_ARGS (t);
12870 int len = TREE_VEC_LENGTH (vec);
12871 bool result = false;
12874 for (i = 0; i < len; ++i)
12875 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
12879 else if (TYPE_P (t))
12881 /* [basic.link]: A name with no linkage (notably, the name
12882 of a class or enumeration declared in a local scope)
12883 shall not be used to declare an entity with linkage.
12884 This implies that names with no linkage cannot be used as
12887 DR 757 relaxes this restriction for C++0x. */
12888 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
12889 : no_linkage_check (t, /*relaxed_p=*/false));
12893 /* DR 488 makes use of a type with no linkage cause
12894 type deduction to fail. */
12895 if (complain & tf_error)
12897 if (TYPE_ANONYMOUS_P (nt))
12898 error ("%qT is/uses anonymous type", t);
12900 error ("template argument for %qD uses local type %qT",
12905 /* In order to avoid all sorts of complications, we do not
12906 allow variably-modified types as template arguments. */
12907 else if (variably_modified_type_p (t, NULL_TREE))
12909 if (complain & tf_error)
12910 error ("%qT is a variably modified type", t);
12914 /* A non-type argument of integral or enumerated type must be a
12916 else if (TREE_TYPE (t)
12917 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
12918 && !TREE_CONSTANT (t))
12920 if (complain & tf_error)
12921 error ("integral expression %qE is not constant", t);
12928 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
12930 int ix, len = DECL_NTPARMS (tmpl);
12931 bool result = false;
12933 for (ix = 0; ix != len; ix++)
12935 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
12938 if (result && (complain & tf_error))
12939 error (" trying to instantiate %qD", tmpl);
12943 /* Instantiate the indicated variable or function template TMPL with
12944 the template arguments in TARG_PTR. */
12947 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
12949 tree targ_ptr = orig_args;
12953 HOST_WIDE_INT saved_processing_template_decl;
12955 if (tmpl == error_mark_node)
12956 return error_mark_node;
12958 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
12960 /* If this function is a clone, handle it specially. */
12961 if (DECL_CLONED_FUNCTION_P (tmpl))
12966 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
12967 DECL_CLONED_FUNCTION. */
12968 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
12969 targ_ptr, complain);
12970 if (spec == error_mark_node)
12971 return error_mark_node;
12973 /* Look for the clone. */
12974 FOR_EACH_CLONE (clone, spec)
12975 if (DECL_NAME (clone) == DECL_NAME (tmpl))
12977 /* We should always have found the clone by now. */
12978 gcc_unreachable ();
12982 /* Check to see if we already have this specialization. */
12983 gen_tmpl = most_general_template (tmpl);
12984 if (tmpl != gen_tmpl)
12985 /* The TMPL is a partial instantiation. To get a full set of
12986 arguments we must add the arguments used to perform the
12987 partial instantiation. */
12988 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
12991 /* It would be nice to avoid hashing here and then again in tsubst_decl,
12992 but it doesn't seem to be on the hot path. */
12993 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
12995 gcc_assert (tmpl == gen_tmpl
12996 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
12998 || fndecl == NULL_TREE);
13000 if (spec != NULL_TREE)
13003 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
13005 return error_mark_node;
13007 /* We are building a FUNCTION_DECL, during which the access of its
13008 parameters and return types have to be checked. However this
13009 FUNCTION_DECL which is the desired context for access checking
13010 is not built yet. We solve this chicken-and-egg problem by
13011 deferring all checks until we have the FUNCTION_DECL. */
13012 push_deferring_access_checks (dk_deferred);
13014 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
13015 (because, for example, we have encountered a non-dependent
13016 function call in the body of a template function and must now
13017 determine which of several overloaded functions will be called),
13018 within the instantiation itself we are not processing a
13020 saved_processing_template_decl = processing_template_decl;
13021 processing_template_decl = 0;
13022 /* Substitute template parameters to obtain the specialization. */
13023 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
13024 targ_ptr, complain, gen_tmpl);
13025 processing_template_decl = saved_processing_template_decl;
13026 if (fndecl == error_mark_node)
13027 return error_mark_node;
13029 /* Now we know the specialization, compute access previously
13031 push_access_scope (fndecl);
13033 /* Some typedefs referenced from within the template code need to be access
13034 checked at template instantiation time, i.e now. These types were
13035 added to the template at parsing time. Let's get those and perfom
13036 the acces checks then. */
13037 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
13038 perform_deferred_access_checks ();
13039 pop_access_scope (fndecl);
13040 pop_deferring_access_checks ();
13042 /* The DECL_TI_TEMPLATE should always be the immediate parent
13043 template, not the most general template. */
13044 DECL_TI_TEMPLATE (fndecl) = tmpl;
13046 /* If we've just instantiated the main entry point for a function,
13047 instantiate all the alternate entry points as well. We do this
13048 by cloning the instantiation of the main entry point, not by
13049 instantiating the template clones. */
13050 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
13051 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
13056 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
13057 NARGS elements of the arguments that are being used when calling
13058 it. TARGS is a vector into which the deduced template arguments
13061 Return zero for success, 2 for an incomplete match that doesn't resolve
13062 all the types, and 1 for complete failure. An error message will be
13063 printed only for an incomplete match.
13065 If FN is a conversion operator, or we are trying to produce a specific
13066 specialization, RETURN_TYPE is the return type desired.
13068 The EXPLICIT_TARGS are explicit template arguments provided via a
13071 The parameter STRICT is one of:
13074 We are deducing arguments for a function call, as in
13075 [temp.deduct.call].
13078 We are deducing arguments for a conversion function, as in
13079 [temp.deduct.conv].
13082 We are deducing arguments when doing an explicit instantiation
13083 as in [temp.explicit], when determining an explicit specialization
13084 as in [temp.expl.spec], or when taking the address of a function
13085 template, as in [temp.deduct.funcaddr]. */
13088 fn_type_unification (tree fn,
13089 tree explicit_targs,
13092 unsigned int nargs,
13094 unification_kind_t strict,
13100 bool incomplete_argument_packs_p = false;
13102 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
13104 fntype = TREE_TYPE (fn);
13105 if (explicit_targs)
13109 The specified template arguments must match the template
13110 parameters in kind (i.e., type, nontype, template), and there
13111 must not be more arguments than there are parameters;
13112 otherwise type deduction fails.
13114 Nontype arguments must match the types of the corresponding
13115 nontype template parameters, or must be convertible to the
13116 types of the corresponding nontype parameters as specified in
13117 _temp.arg.nontype_, otherwise type deduction fails.
13119 All references in the function type of the function template
13120 to the corresponding template parameters are replaced by the
13121 specified template argument values. If a substitution in a
13122 template parameter or in the function type of the function
13123 template results in an invalid type, type deduction fails. */
13124 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
13125 int i, len = TREE_VEC_LENGTH (tparms);
13126 tree converted_args;
13127 bool incomplete = false;
13129 if (explicit_targs == error_mark_node)
13133 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
13134 /*require_all_args=*/false,
13135 /*use_default_args=*/false));
13136 if (converted_args == error_mark_node)
13139 /* Substitute the explicit args into the function type. This is
13140 necessary so that, for instance, explicitly declared function
13141 arguments can match null pointed constants. If we were given
13142 an incomplete set of explicit args, we must not do semantic
13143 processing during substitution as we could create partial
13145 for (i = 0; i < len; i++)
13147 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13148 bool parameter_pack = false;
13150 /* Dig out the actual parm. */
13151 if (TREE_CODE (parm) == TYPE_DECL
13152 || TREE_CODE (parm) == TEMPLATE_DECL)
13154 parm = TREE_TYPE (parm);
13155 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
13157 else if (TREE_CODE (parm) == PARM_DECL)
13159 parm = DECL_INITIAL (parm);
13160 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
13163 if (parameter_pack)
13167 template_parm_level_and_index (parm, &level, &idx);
13169 /* Mark the argument pack as "incomplete". We could
13170 still deduce more arguments during unification. */
13171 targ = TMPL_ARG (converted_args, level, idx);
13174 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
13175 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
13176 = ARGUMENT_PACK_ARGS (targ);
13179 /* We have some incomplete argument packs. */
13180 incomplete_argument_packs_p = true;
13184 if (incomplete_argument_packs_p)
13185 /* Any substitution is guaranteed to be incomplete if there
13186 are incomplete argument packs, because we can still deduce
13190 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
13192 processing_template_decl += incomplete;
13193 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
13194 processing_template_decl -= incomplete;
13196 if (fntype == error_mark_node)
13199 /* Place the explicitly specified arguments in TARGS. */
13200 for (i = NUM_TMPL_ARGS (converted_args); i--;)
13201 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
13204 /* Never do unification on the 'this' parameter. */
13205 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
13211 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
13212 new_args = XALLOCAVEC (tree, nargs + 1);
13213 new_args[0] = return_type;
13214 memcpy (new_args + 1, args, nargs * sizeof (tree));
13219 /* We allow incomplete unification without an error message here
13220 because the standard doesn't seem to explicitly prohibit it. Our
13221 callers must be ready to deal with unification failures in any
13223 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
13224 targs, parms, args, nargs, /*subr=*/0,
13227 if (result == 0 && incomplete_argument_packs_p)
13229 int i, len = NUM_TMPL_ARGS (targs);
13231 /* Clear the "incomplete" flags on all argument packs. */
13232 for (i = 0; i < len; i++)
13234 tree arg = TREE_VEC_ELT (targs, i);
13235 if (ARGUMENT_PACK_P (arg))
13237 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
13238 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
13243 /* Now that we have bindings for all of the template arguments,
13244 ensure that the arguments deduced for the template template
13245 parameters have compatible template parameter lists. We cannot
13246 check this property before we have deduced all template
13247 arguments, because the template parameter types of a template
13248 template parameter might depend on prior template parameters
13249 deduced after the template template parameter. The following
13250 ill-formed example illustrates this issue:
13252 template<typename T, template<T> class C> void f(C<5>, T);
13254 template<int N> struct X {};
13257 f(X<5>(), 5l); // error: template argument deduction fails
13260 The template parameter list of 'C' depends on the template type
13261 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
13262 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
13263 time that we deduce 'C'. */
13265 && !template_template_parm_bindings_ok_p
13266 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
13270 /* All is well so far. Now, check:
13274 When all template arguments have been deduced, all uses of
13275 template parameters in nondeduced contexts are replaced with
13276 the corresponding deduced argument values. If the
13277 substitution results in an invalid type, as described above,
13278 type deduction fails. */
13280 tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
13281 if (substed == error_mark_node)
13284 /* If we're looking for an exact match, check that what we got
13285 is indeed an exact match. It might not be if some template
13286 parameters are used in non-deduced contexts. */
13287 if (strict == DEDUCE_EXACT)
13292 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
13294 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
13295 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
13296 if (!same_type_p (args[i], TREE_VALUE (sarg)))
13304 /* Adjust types before performing type deduction, as described in
13305 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
13306 sections are symmetric. PARM is the type of a function parameter
13307 or the return type of the conversion function. ARG is the type of
13308 the argument passed to the call, or the type of the value
13309 initialized with the result of the conversion function.
13310 ARG_EXPR is the original argument expression, which may be null. */
13313 maybe_adjust_types_for_deduction (unification_kind_t strict,
13327 /* Swap PARM and ARG throughout the remainder of this
13328 function; the handling is precisely symmetric since PARM
13329 will initialize ARG rather than vice versa. */
13337 /* Core issue #873: Do the DR606 thing (see below) for these cases,
13338 too, but here handle it by stripping the reference from PARM
13339 rather than by adding it to ARG. */
13340 if (TREE_CODE (*parm) == REFERENCE_TYPE
13341 && TYPE_REF_IS_RVALUE (*parm)
13342 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13343 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13344 && TREE_CODE (*arg) == REFERENCE_TYPE
13345 && !TYPE_REF_IS_RVALUE (*arg))
13346 *parm = TREE_TYPE (*parm);
13347 /* Nothing else to do in this case. */
13351 gcc_unreachable ();
13354 if (TREE_CODE (*parm) != REFERENCE_TYPE)
13356 /* [temp.deduct.call]
13358 If P is not a reference type:
13360 --If A is an array type, the pointer type produced by the
13361 array-to-pointer standard conversion (_conv.array_) is
13362 used in place of A for type deduction; otherwise,
13364 --If A is a function type, the pointer type produced by
13365 the function-to-pointer standard conversion
13366 (_conv.func_) is used in place of A for type deduction;
13369 --If A is a cv-qualified type, the top level
13370 cv-qualifiers of A's type are ignored for type
13372 if (TREE_CODE (*arg) == ARRAY_TYPE)
13373 *arg = build_pointer_type (TREE_TYPE (*arg));
13374 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
13375 *arg = build_pointer_type (*arg);
13377 *arg = TYPE_MAIN_VARIANT (*arg);
13380 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
13381 of the form T&&, where T is a template parameter, and the argument
13382 is an lvalue, T is deduced as A& */
13383 if (TREE_CODE (*parm) == REFERENCE_TYPE
13384 && TYPE_REF_IS_RVALUE (*parm)
13385 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13386 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13387 && arg_expr && real_lvalue_p (arg_expr))
13388 *arg = build_reference_type (*arg);
13390 /* [temp.deduct.call]
13392 If P is a cv-qualified type, the top level cv-qualifiers
13393 of P's type are ignored for type deduction. If P is a
13394 reference type, the type referred to by P is used for
13396 *parm = TYPE_MAIN_VARIANT (*parm);
13397 if (TREE_CODE (*parm) == REFERENCE_TYPE)
13399 *parm = TREE_TYPE (*parm);
13400 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
13403 /* DR 322. For conversion deduction, remove a reference type on parm
13404 too (which has been swapped into ARG). */
13405 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
13406 *arg = TREE_TYPE (*arg);
13411 /* Most parms like fn_type_unification.
13413 If SUBR is 1, we're being called recursively (to unify the
13414 arguments of a function or method parameter of a function
13418 type_unification_real (tree tparms,
13422 unsigned int xnargs,
13424 unification_kind_t strict,
13427 tree parm, arg, arg_expr;
13429 int ntparms = TREE_VEC_LENGTH (tparms);
13431 int saw_undeduced = 0;
13434 unsigned int nargs;
13437 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
13438 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
13439 gcc_assert (ntparms > 0);
13441 /* Reset the number of non-defaulted template arguments contained
13443 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
13448 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
13449 | UNIFY_ALLOW_DERIVED);
13453 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13457 sub_strict = UNIFY_ALLOW_NONE;
13461 gcc_unreachable ();
13470 while (parms && parms != void_list_node
13473 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13476 parm = TREE_VALUE (parms);
13477 parms = TREE_CHAIN (parms);
13482 if (arg == error_mark_node)
13484 if (arg == unknown_type_node)
13485 /* We can't deduce anything from this, but we might get all the
13486 template args from other function args. */
13489 /* Conversions will be performed on a function argument that
13490 corresponds with a function parameter that contains only
13491 non-deducible template parameters and explicitly specified
13492 template parameters. */
13493 if (!uses_template_parms (parm))
13498 type = TREE_TYPE (arg);
13502 if (same_type_p (parm, type))
13504 if (strict != DEDUCE_EXACT
13505 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
13514 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
13515 if (type_unknown_p (arg))
13517 /* [temp.deduct.type]
13519 A template-argument can be deduced from a pointer to
13520 function or pointer to member function argument if
13521 the set of overloaded functions does not contain
13522 function templates and at most one of a set of
13523 overloaded functions provides a unique match. */
13524 if (resolve_overloaded_unification
13525 (tparms, targs, parm, arg, strict, sub_strict))
13531 arg = unlowered_expr_type (arg);
13532 if (arg == error_mark_node)
13537 int arg_strict = sub_strict;
13540 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
13543 if (arg == init_list_type_node && arg_expr)
13545 if (unify (tparms, targs, parm, arg, arg_strict))
13552 && parms != void_list_node
13553 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13555 /* Unify the remaining arguments with the pack expansion type. */
13557 tree parmvec = make_tree_vec (1);
13559 /* Allocate a TREE_VEC and copy in all of the arguments */
13560 argvec = make_tree_vec (nargs - ia);
13561 for (i = 0; ia < nargs; ++ia, ++i)
13562 TREE_VEC_ELT (argvec, i) = args[ia];
13564 /* Copy the parameter into parmvec. */
13565 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
13566 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
13567 /*call_args_p=*/true, /*subr=*/subr))
13570 /* Advance to the end of the list of parameters. */
13571 parms = TREE_CHAIN (parms);
13574 /* Fail if we've reached the end of the parm list, and more args
13575 are present, and the parm list isn't variadic. */
13576 if (ia < nargs && parms == void_list_node)
13578 /* Fail if parms are left and they don't have default values. */
13579 if (parms && parms != void_list_node
13580 && TREE_PURPOSE (parms) == NULL_TREE)
13584 for (i = 0; i < ntparms; i++)
13585 if (!TREE_VEC_ELT (targs, i))
13589 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
13592 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13594 /* If this is an undeduced nontype parameter that depends on
13595 a type parameter, try another pass; its type may have been
13596 deduced from a later argument than the one from which
13597 this parameter can be deduced. */
13598 if (TREE_CODE (tparm) == PARM_DECL
13599 && uses_template_parms (TREE_TYPE (tparm))
13600 && !saw_undeduced++)
13603 /* Core issue #226 (C++0x) [temp.deduct]:
13605 If a template argument has not been deduced, its
13606 default template argument, if any, is used.
13608 When we are in C++98 mode, TREE_PURPOSE will either
13609 be NULL_TREE or ERROR_MARK_NODE, so we do not need
13610 to explicitly check cxx_dialect here. */
13611 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
13613 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13614 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
13615 arg = tsubst_template_arg (arg, targs, tf_none, NULL_TREE);
13616 arg = convert_template_argument (parm, arg, targs, tf_none,
13618 if (arg == error_mark_node)
13622 TREE_VEC_ELT (targs, i) = arg;
13623 /* The position of the first default template argument,
13624 is also the number of non-defaulted arguments in TARGS.
13626 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
13627 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
13632 /* If the type parameter is a parameter pack, then it will
13633 be deduced to an empty parameter pack. */
13634 if (template_parameter_pack_p (tparm))
13638 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
13640 arg = make_node (NONTYPE_ARGUMENT_PACK);
13641 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
13642 TREE_CONSTANT (arg) = 1;
13645 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
13647 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
13649 TREE_VEC_ELT (targs, i) = arg;
13655 #ifdef ENABLE_CHECKING
13656 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
13657 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
13663 /* Subroutine of type_unification_real. Args are like the variables
13664 at the call site. ARG is an overloaded function (or template-id);
13665 we try deducing template args from each of the overloads, and if
13666 only one succeeds, we go with that. Modifies TARGS and returns
13667 true on success. */
13670 resolve_overloaded_unification (tree tparms,
13674 unification_kind_t strict,
13677 tree tempargs = copy_node (targs);
13679 tree goodfn = NULL_TREE;
13682 if (TREE_CODE (arg) == ADDR_EXPR)
13684 arg = TREE_OPERAND (arg, 0);
13690 if (TREE_CODE (arg) == COMPONENT_REF)
13691 /* Handle `&x' where `x' is some static or non-static member
13693 arg = TREE_OPERAND (arg, 1);
13695 if (TREE_CODE (arg) == OFFSET_REF)
13696 arg = TREE_OPERAND (arg, 1);
13698 /* Strip baselink information. */
13699 if (BASELINK_P (arg))
13700 arg = BASELINK_FUNCTIONS (arg);
13702 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
13704 /* If we got some explicit template args, we need to plug them into
13705 the affected templates before we try to unify, in case the
13706 explicit args will completely resolve the templates in question. */
13708 tree expl_subargs = TREE_OPERAND (arg, 1);
13709 arg = TREE_OPERAND (arg, 0);
13711 for (; arg; arg = OVL_NEXT (arg))
13713 tree fn = OVL_CURRENT (arg);
13714 tree subargs, elem;
13716 if (TREE_CODE (fn) != TEMPLATE_DECL)
13719 ++processing_template_decl;
13720 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13721 expl_subargs, /*check_ret=*/false);
13724 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
13725 if (try_one_overload (tparms, targs, tempargs, parm,
13726 elem, strict, sub_strict, addr_p)
13727 && (!goodfn || !decls_match (goodfn, elem)))
13733 --processing_template_decl;
13736 else if (TREE_CODE (arg) != OVERLOAD
13737 && TREE_CODE (arg) != FUNCTION_DECL)
13738 /* If ARG is, for example, "(0, &f)" then its type will be unknown
13739 -- but the deduction does not succeed because the expression is
13740 not just the function on its own. */
13743 for (; arg; arg = OVL_NEXT (arg))
13744 if (try_one_overload (tparms, targs, tempargs, parm,
13745 TREE_TYPE (OVL_CURRENT (arg)),
13746 strict, sub_strict, addr_p)
13747 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
13749 goodfn = OVL_CURRENT (arg);
13753 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13754 to function or pointer to member function argument if the set of
13755 overloaded functions does not contain function templates and at most
13756 one of a set of overloaded functions provides a unique match.
13758 So if we found multiple possibilities, we return success but don't
13759 deduce anything. */
13763 int i = TREE_VEC_LENGTH (targs);
13765 if (TREE_VEC_ELT (tempargs, i))
13766 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
13774 /* Core DR 115: In contexts where deduction is done and fails, or in
13775 contexts where deduction is not done, if a template argument list is
13776 specified and it, along with any default template arguments, identifies
13777 a single function template specialization, then the template-id is an
13778 lvalue for the function template specialization. */
13781 resolve_nondeduced_context (tree orig_expr)
13783 tree expr, offset, baselink;
13786 if (!type_unknown_p (orig_expr))
13791 offset = NULL_TREE;
13792 baselink = NULL_TREE;
13794 if (TREE_CODE (expr) == ADDR_EXPR)
13796 expr = TREE_OPERAND (expr, 0);
13799 if (TREE_CODE (expr) == OFFSET_REF)
13802 expr = TREE_OPERAND (expr, 1);
13804 if (TREE_CODE (expr) == BASELINK)
13807 expr = BASELINK_FUNCTIONS (expr);
13810 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
13813 tree goodfn = NULL_TREE;
13815 /* If we got some explicit template args, we need to plug them into
13816 the affected templates before we try to unify, in case the
13817 explicit args will completely resolve the templates in question. */
13819 tree expl_subargs = TREE_OPERAND (expr, 1);
13820 tree arg = TREE_OPERAND (expr, 0);
13821 tree badfn = NULL_TREE;
13822 tree badargs = NULL_TREE;
13824 for (; arg; arg = OVL_NEXT (arg))
13826 tree fn = OVL_CURRENT (arg);
13827 tree subargs, elem;
13829 if (TREE_CODE (fn) != TEMPLATE_DECL)
13832 ++processing_template_decl;
13833 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13834 expl_subargs, /*check_ret=*/false);
13835 if (subargs && !any_dependent_template_arguments_p (subargs))
13837 elem = instantiate_template (fn, subargs, tf_none);
13838 if (elem == error_mark_node)
13843 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
13849 --processing_template_decl;
13855 expr = build_baselink (BASELINK_BINFO (baselink),
13856 BASELINK_ACCESS_BINFO (baselink),
13857 expr, BASELINK_OPTYPE (baselink));
13859 expr = build2 (OFFSET_REF, TREE_TYPE (expr),
13860 TREE_OPERAND (offset, 0), expr);
13862 expr = build_address (expr);
13865 else if (good == 0 && badargs)
13866 /* There were no good options and at least one bad one, so let the
13867 user know what the problem is. */
13868 instantiate_template (badfn, badargs, tf_warning_or_error);
13873 /* Subroutine of resolve_overloaded_unification; does deduction for a single
13874 overload. Fills TARGS with any deduced arguments, or error_mark_node if
13875 different overloads deduce different arguments for a given parm.
13876 ADDR_P is true if the expression for which deduction is being
13877 performed was of the form "& fn" rather than simply "fn".
13879 Returns 1 on success. */
13882 try_one_overload (tree tparms,
13887 unification_kind_t strict,
13895 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13896 to function or pointer to member function argument if the set of
13897 overloaded functions does not contain function templates and at most
13898 one of a set of overloaded functions provides a unique match.
13900 So if this is a template, just return success. */
13902 if (uses_template_parms (arg))
13905 if (TREE_CODE (arg) == METHOD_TYPE)
13906 arg = build_ptrmemfunc_type (build_pointer_type (arg));
13908 arg = build_pointer_type (arg);
13910 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
13912 /* We don't copy orig_targs for this because if we have already deduced
13913 some template args from previous args, unify would complain when we
13914 try to deduce a template parameter for the same argument, even though
13915 there isn't really a conflict. */
13916 nargs = TREE_VEC_LENGTH (targs);
13917 tempargs = make_tree_vec (nargs);
13919 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
13922 /* First make sure we didn't deduce anything that conflicts with
13923 explicitly specified args. */
13924 for (i = nargs; i--; )
13926 tree elt = TREE_VEC_ELT (tempargs, i);
13927 tree oldelt = TREE_VEC_ELT (orig_targs, i);
13931 else if (uses_template_parms (elt))
13932 /* Since we're unifying against ourselves, we will fill in
13933 template args used in the function parm list with our own
13934 template parms. Discard them. */
13935 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
13936 else if (oldelt && !template_args_equal (oldelt, elt))
13940 for (i = nargs; i--; )
13942 tree elt = TREE_VEC_ELT (tempargs, i);
13945 TREE_VEC_ELT (targs, i) = elt;
13951 /* PARM is a template class (perhaps with unbound template
13952 parameters). ARG is a fully instantiated type. If ARG can be
13953 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
13954 TARGS are as for unify. */
13957 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
13959 tree copy_of_targs;
13961 if (!CLASSTYPE_TEMPLATE_INFO (arg)
13962 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
13963 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
13966 /* We need to make a new template argument vector for the call to
13967 unify. If we used TARGS, we'd clutter it up with the result of
13968 the attempted unification, even if this class didn't work out.
13969 We also don't want to commit ourselves to all the unifications
13970 we've already done, since unification is supposed to be done on
13971 an argument-by-argument basis. In other words, consider the
13972 following pathological case:
13974 template <int I, int J, int K>
13977 template <int I, int J>
13978 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
13980 template <int I, int J, int K>
13981 void f(S<I, J, K>, S<I, I, I>);
13990 Now, by the time we consider the unification involving `s2', we
13991 already know that we must have `f<0, 0, 0>'. But, even though
13992 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
13993 because there are two ways to unify base classes of S<0, 1, 2>
13994 with S<I, I, I>. If we kept the already deduced knowledge, we
13995 would reject the possibility I=1. */
13996 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
13998 /* If unification failed, we're done. */
13999 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
14000 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
14006 /* Given a template type PARM and a class type ARG, find the unique
14007 base type in ARG that is an instance of PARM. We do not examine
14008 ARG itself; only its base-classes. If there is not exactly one
14009 appropriate base class, return NULL_TREE. PARM may be the type of
14010 a partial specialization, as well as a plain template type. Used
14014 get_template_base (tree tparms, tree targs, tree parm, tree arg)
14016 tree rval = NULL_TREE;
14019 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
14021 binfo = TYPE_BINFO (complete_type (arg));
14023 /* The type could not be completed. */
14026 /* Walk in inheritance graph order. The search order is not
14027 important, and this avoids multiple walks of virtual bases. */
14028 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
14030 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
14034 /* If there is more than one satisfactory baseclass, then:
14038 If they yield more than one possible deduced A, the type
14042 if (rval && !same_type_p (r, rval))
14052 /* Returns the level of DECL, which declares a template parameter. */
14055 template_decl_level (tree decl)
14057 switch (TREE_CODE (decl))
14060 case TEMPLATE_DECL:
14061 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
14064 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
14067 gcc_unreachable ();
14072 /* Decide whether ARG can be unified with PARM, considering only the
14073 cv-qualifiers of each type, given STRICT as documented for unify.
14074 Returns nonzero iff the unification is OK on that basis. */
14077 check_cv_quals_for_unify (int strict, tree arg, tree parm)
14079 int arg_quals = cp_type_quals (arg);
14080 int parm_quals = cp_type_quals (parm);
14082 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14083 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
14085 /* Although a CVR qualifier is ignored when being applied to a
14086 substituted template parameter ([8.3.2]/1 for example), that
14087 does not apply during deduction [14.8.2.4]/1, (even though
14088 that is not explicitly mentioned, [14.8.2.4]/9 indicates
14089 this). Except when we're allowing additional CV qualifiers
14090 at the outer level [14.8.2.1]/3,1st bullet. */
14091 if ((TREE_CODE (arg) == REFERENCE_TYPE
14092 || TREE_CODE (arg) == FUNCTION_TYPE
14093 || TREE_CODE (arg) == METHOD_TYPE)
14094 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
14097 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
14098 && (parm_quals & TYPE_QUAL_RESTRICT))
14102 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
14103 && (arg_quals & parm_quals) != parm_quals)
14106 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
14107 && (parm_quals & arg_quals) != arg_quals)
14113 /* Determines the LEVEL and INDEX for the template parameter PARM. */
14115 template_parm_level_and_index (tree parm, int* level, int* index)
14117 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14118 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14119 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14121 *index = TEMPLATE_TYPE_IDX (parm);
14122 *level = TEMPLATE_TYPE_LEVEL (parm);
14126 *index = TEMPLATE_PARM_IDX (parm);
14127 *level = TEMPLATE_PARM_LEVEL (parm);
14131 /* Unifies the remaining arguments in PACKED_ARGS with the pack
14132 expansion at the end of PACKED_PARMS. Returns 0 if the type
14133 deduction succeeds, 1 otherwise. STRICT is the same as in
14134 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
14135 call argument list. We'll need to adjust the arguments to make them
14136 types. SUBR tells us if this is from a recursive call to
14137 type_unification_real. */
14139 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
14140 tree packed_args, int strict, bool call_args_p,
14144 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
14145 tree pattern = PACK_EXPANSION_PATTERN (parm);
14146 tree pack, packs = NULL_TREE;
14147 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
14148 int len = TREE_VEC_LENGTH (packed_args);
14150 /* Determine the parameter packs we will be deducing from the
14151 pattern, and record their current deductions. */
14152 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
14153 pack; pack = TREE_CHAIN (pack))
14155 tree parm_pack = TREE_VALUE (pack);
14158 /* Determine the index and level of this parameter pack. */
14159 template_parm_level_and_index (parm_pack, &level, &idx);
14161 /* Keep track of the parameter packs and their corresponding
14163 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
14164 TREE_TYPE (packs) = make_tree_vec (len - start);
14167 /* Loop through all of the arguments that have not yet been
14168 unified and unify each with the pattern. */
14169 for (i = start; i < len; i++)
14171 tree parm = pattern;
14173 /* For each parameter pack, clear out the deduced value so that
14174 we can deduce it again. */
14175 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14178 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14180 TMPL_ARG (targs, level, idx) = NULL_TREE;
14183 /* Unify the pattern with the current argument. */
14185 tree arg = TREE_VEC_ELT (packed_args, i);
14186 tree arg_expr = NULL_TREE;
14187 int arg_strict = strict;
14188 bool skip_arg_p = false;
14194 /* This mirrors what we do in type_unification_real. */
14198 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
14199 | UNIFY_ALLOW_MORE_CV_QUAL
14200 | UNIFY_ALLOW_DERIVED);
14204 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
14208 sub_strict = UNIFY_ALLOW_NONE;
14212 gcc_unreachable ();
14217 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
14218 if (type_unknown_p (arg))
14220 /* [temp.deduct.type] A template-argument can be
14221 deduced from a pointer to function or pointer
14222 to member function argument if the set of
14223 overloaded functions does not contain function
14224 templates and at most one of a set of
14225 overloaded functions provides a unique
14228 if (resolve_overloaded_unification
14229 (tparms, targs, parm, arg,
14230 (unification_kind_t) strict,
14240 arg = unlowered_expr_type (arg);
14241 if (arg == error_mark_node)
14246 arg_strict = sub_strict;
14250 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
14251 &parm, &arg, arg_expr);
14256 /* For deduction from an init-list we need the actual list. */
14257 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
14259 if (unify (tparms, targs, parm, arg, arg_strict))
14264 /* For each parameter pack, collect the deduced value. */
14265 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14268 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14270 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
14271 TMPL_ARG (targs, level, idx);
14275 /* Verify that the results of unification with the parameter packs
14276 produce results consistent with what we've seen before, and make
14277 the deduced argument packs available. */
14278 for (pack = packs; pack; pack = TREE_CHAIN (pack))
14280 tree old_pack = TREE_VALUE (pack);
14281 tree new_args = TREE_TYPE (pack);
14282 int i, len = TREE_VEC_LENGTH (new_args);
14284 bool nondeduced_p = false;
14286 /* By default keep the original deduced argument pack.
14287 If necessary, more specific code is going to update the
14288 resulting deduced argument later down in this function. */
14289 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14290 TMPL_ARG (targs, level, idx) = old_pack;
14292 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
14293 actually deduce anything. */
14294 for (i = 0; i < len && !nondeduced_p; ++i)
14295 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
14296 nondeduced_p = true;
14300 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
14302 /* Prepend the explicit arguments onto NEW_ARGS. */
14303 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14304 tree old_args = new_args;
14305 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
14306 int len = explicit_len + TREE_VEC_LENGTH (old_args);
14308 /* Copy the explicit arguments. */
14309 new_args = make_tree_vec (len);
14310 for (i = 0; i < explicit_len; i++)
14311 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
14313 /* Copy the deduced arguments. */
14314 for (; i < len; i++)
14315 TREE_VEC_ELT (new_args, i) =
14316 TREE_VEC_ELT (old_args, i - explicit_len);
14322 /* Build the deduced *_ARGUMENT_PACK. */
14323 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
14325 result = make_node (NONTYPE_ARGUMENT_PACK);
14326 TREE_TYPE (result) =
14327 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
14328 TREE_CONSTANT (result) = 1;
14331 result = cxx_make_type (TYPE_ARGUMENT_PACK);
14333 SET_ARGUMENT_PACK_ARGS (result, new_args);
14335 /* Note the deduced argument packs for this parameter
14337 TMPL_ARG (targs, level, idx) = result;
14339 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
14340 && (ARGUMENT_PACK_ARGS (old_pack)
14341 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
14343 /* We only had the explicitly-provided arguments before, but
14344 now we have a complete set of arguments. */
14345 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14347 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
14348 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
14349 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
14351 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
14353 /* Inconsistent unification of this parameter pack. */
14360 /* Deduce the value of template parameters. TPARMS is the (innermost)
14361 set of template parameters to a template. TARGS is the bindings
14362 for those template parameters, as determined thus far; TARGS may
14363 include template arguments for outer levels of template parameters
14364 as well. PARM is a parameter to a template function, or a
14365 subcomponent of that parameter; ARG is the corresponding argument.
14366 This function attempts to match PARM with ARG in a manner
14367 consistent with the existing assignments in TARGS. If more values
14368 are deduced, then TARGS is updated.
14370 Returns 0 if the type deduction succeeds, 1 otherwise. The
14371 parameter STRICT is a bitwise or of the following flags:
14374 Require an exact match between PARM and ARG.
14375 UNIFY_ALLOW_MORE_CV_QUAL:
14376 Allow the deduced ARG to be more cv-qualified (by qualification
14377 conversion) than ARG.
14378 UNIFY_ALLOW_LESS_CV_QUAL:
14379 Allow the deduced ARG to be less cv-qualified than ARG.
14380 UNIFY_ALLOW_DERIVED:
14381 Allow the deduced ARG to be a template base class of ARG,
14382 or a pointer to a template base class of the type pointed to by
14384 UNIFY_ALLOW_INTEGER:
14385 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
14386 case for more information.
14387 UNIFY_ALLOW_OUTER_LEVEL:
14388 This is the outermost level of a deduction. Used to determine validity
14389 of qualification conversions. A valid qualification conversion must
14390 have const qualified pointers leading up to the inner type which
14391 requires additional CV quals, except at the outer level, where const
14392 is not required [conv.qual]. It would be normal to set this flag in
14393 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
14394 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
14395 This is the outermost level of a deduction, and PARM can be more CV
14396 qualified at this point.
14397 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
14398 This is the outermost level of a deduction, and PARM can be less CV
14399 qualified at this point. */
14402 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
14407 int strict_in = strict;
14409 /* I don't think this will do the right thing with respect to types.
14410 But the only case I've seen it in so far has been array bounds, where
14411 signedness is the only information lost, and I think that will be
14413 while (TREE_CODE (parm) == NOP_EXPR)
14414 parm = TREE_OPERAND (parm, 0);
14416 if (arg == error_mark_node)
14418 if (arg == unknown_type_node
14419 || arg == init_list_type_node)
14420 /* We can't deduce anything from this, but we might get all the
14421 template args from other function args. */
14424 /* If PARM uses template parameters, then we can't bail out here,
14425 even if ARG == PARM, since we won't record unifications for the
14426 template parameters. We might need them if we're trying to
14427 figure out which of two things is more specialized. */
14428 if (arg == parm && !uses_template_parms (parm))
14431 /* Handle init lists early, so the rest of the function can assume
14432 we're dealing with a type. */
14433 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
14437 tree orig_parm = parm;
14439 /* Replace T with std::initializer_list<T> for deduction. */
14440 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14441 && flag_deduce_init_list)
14442 parm = listify (parm);
14444 if (!is_std_init_list (parm))
14445 /* We can only deduce from an initializer list argument if the
14446 parameter is std::initializer_list; otherwise this is a
14447 non-deduced context. */
14450 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
14452 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
14454 int elt_strict = strict;
14455 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
14457 tree type = TREE_TYPE (elt);
14458 /* It should only be possible to get here for a call. */
14459 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
14460 elt_strict |= maybe_adjust_types_for_deduction
14461 (DEDUCE_CALL, &elttype, &type, elt);
14465 if (unify (tparms, targs, elttype, elt, elt_strict))
14469 /* If the std::initializer_list<T> deduction worked, replace the
14470 deduced A with std::initializer_list<A>. */
14471 if (orig_parm != parm)
14473 idx = TEMPLATE_TYPE_IDX (orig_parm);
14474 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14475 targ = listify (targ);
14476 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
14481 /* Immediately reject some pairs that won't unify because of
14482 cv-qualification mismatches. */
14483 if (TREE_CODE (arg) == TREE_CODE (parm)
14485 /* It is the elements of the array which hold the cv quals of an array
14486 type, and the elements might be template type parms. We'll check
14487 when we recurse. */
14488 && TREE_CODE (arg) != ARRAY_TYPE
14489 /* We check the cv-qualifiers when unifying with template type
14490 parameters below. We want to allow ARG `const T' to unify with
14491 PARM `T' for example, when computing which of two templates
14492 is more specialized, for example. */
14493 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
14494 && !check_cv_quals_for_unify (strict_in, arg, parm))
14497 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
14498 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
14499 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
14500 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
14501 strict &= ~UNIFY_ALLOW_DERIVED;
14502 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
14503 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
14505 switch (TREE_CODE (parm))
14507 case TYPENAME_TYPE:
14509 case UNBOUND_CLASS_TEMPLATE:
14510 /* In a type which contains a nested-name-specifier, template
14511 argument values cannot be deduced for template parameters used
14512 within the nested-name-specifier. */
14515 case TEMPLATE_TYPE_PARM:
14516 case TEMPLATE_TEMPLATE_PARM:
14517 case BOUND_TEMPLATE_TEMPLATE_PARM:
14518 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14519 if (tparm == error_mark_node)
14522 if (TEMPLATE_TYPE_LEVEL (parm)
14523 != template_decl_level (tparm))
14524 /* The PARM is not one we're trying to unify. Just check
14525 to see if it matches ARG. */
14526 return (TREE_CODE (arg) == TREE_CODE (parm)
14527 && same_type_p (parm, arg)) ? 0 : 1;
14528 idx = TEMPLATE_TYPE_IDX (parm);
14529 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14530 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
14532 /* Check for mixed types and values. */
14533 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14534 && TREE_CODE (tparm) != TYPE_DECL)
14535 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14536 && TREE_CODE (tparm) != TEMPLATE_DECL))
14539 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14541 /* ARG must be constructed from a template class or a template
14542 template parameter. */
14543 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
14544 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
14548 tree parmvec = TYPE_TI_ARGS (parm);
14549 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
14551 = DECL_INNERMOST_TEMPLATE_PARMS
14552 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
14554 int parm_variadic_p = 0;
14556 /* The resolution to DR150 makes clear that default
14557 arguments for an N-argument may not be used to bind T
14558 to a template template parameter with fewer than N
14559 parameters. It is not safe to permit the binding of
14560 default arguments as an extension, as that may change
14561 the meaning of a conforming program. Consider:
14563 struct Dense { static const unsigned int dim = 1; };
14565 template <template <typename> class View,
14567 void operator+(float, View<Block> const&);
14569 template <typename Block,
14570 unsigned int Dim = Block::dim>
14571 struct Lvalue_proxy { operator float() const; };
14575 Lvalue_proxy<Dense> p;
14580 Here, if Lvalue_proxy is permitted to bind to View, then
14581 the global operator+ will be used; if they are not, the
14582 Lvalue_proxy will be converted to float. */
14583 if (coerce_template_parms (parm_parms,
14585 TYPE_TI_TEMPLATE (parm),
14587 /*require_all_args=*/true,
14588 /*use_default_args=*/false)
14589 == error_mark_node)
14592 /* Deduce arguments T, i from TT<T> or TT<i>.
14593 We check each element of PARMVEC and ARGVEC individually
14594 rather than the whole TREE_VEC since they can have
14595 different number of elements. */
14597 parmvec = expand_template_argument_pack (parmvec);
14598 argvec = expand_template_argument_pack (argvec);
14600 len = TREE_VEC_LENGTH (parmvec);
14602 /* Check if the parameters end in a pack, making them
14605 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
14606 parm_variadic_p = 1;
14608 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
14611 for (i = 0; i < len - parm_variadic_p; ++i)
14613 if (unify (tparms, targs,
14614 TREE_VEC_ELT (parmvec, i),
14615 TREE_VEC_ELT (argvec, i),
14620 if (parm_variadic_p
14621 && unify_pack_expansion (tparms, targs,
14624 /*call_args_p=*/false,
14628 arg = TYPE_TI_TEMPLATE (arg);
14630 /* Fall through to deduce template name. */
14633 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14634 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14636 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
14638 /* Simple cases: Value already set, does match or doesn't. */
14639 if (targ != NULL_TREE && template_args_equal (targ, arg))
14646 /* If PARM is `const T' and ARG is only `int', we don't have
14647 a match unless we are allowing additional qualification.
14648 If ARG is `const int' and PARM is just `T' that's OK;
14649 that binds `const int' to `T'. */
14650 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
14654 /* Consider the case where ARG is `const volatile int' and
14655 PARM is `const T'. Then, T should be `volatile int'. */
14656 arg = cp_build_qualified_type_real
14657 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
14658 if (arg == error_mark_node)
14661 /* Simple cases: Value already set, does match or doesn't. */
14662 if (targ != NULL_TREE && same_type_p (targ, arg))
14667 /* Make sure that ARG is not a variable-sized array. (Note
14668 that were talking about variable-sized arrays (like
14669 `int[n]'), rather than arrays of unknown size (like
14670 `int[]').) We'll get very confused by such a type since
14671 the bound of the array will not be computable in an
14672 instantiation. Besides, such types are not allowed in
14673 ISO C++, so we can do as we please here. */
14674 if (variably_modified_type_p (arg, NULL_TREE))
14677 /* Strip typedefs as in convert_template_argument. */
14678 arg = strip_typedefs (arg);
14681 /* If ARG is a parameter pack or an expansion, we cannot unify
14682 against it unless PARM is also a parameter pack. */
14683 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14684 && !template_parameter_pack_p (parm))
14687 /* If the argument deduction results is a METHOD_TYPE,
14688 then there is a problem.
14689 METHOD_TYPE doesn't map to any real C++ type the result of
14690 the deduction can not be of that type. */
14691 if (TREE_CODE (arg) == METHOD_TYPE)
14694 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14697 case TEMPLATE_PARM_INDEX:
14698 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14699 if (tparm == error_mark_node)
14702 if (TEMPLATE_PARM_LEVEL (parm)
14703 != template_decl_level (tparm))
14704 /* The PARM is not one we're trying to unify. Just check
14705 to see if it matches ARG. */
14706 return !(TREE_CODE (arg) == TREE_CODE (parm)
14707 && cp_tree_equal (parm, arg));
14709 idx = TEMPLATE_PARM_IDX (parm);
14710 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14713 return !cp_tree_equal (targ, arg);
14715 /* [temp.deduct.type] If, in the declaration of a function template
14716 with a non-type template-parameter, the non-type
14717 template-parameter is used in an expression in the function
14718 parameter-list and, if the corresponding template-argument is
14719 deduced, the template-argument type shall match the type of the
14720 template-parameter exactly, except that a template-argument
14721 deduced from an array bound may be of any integral type.
14722 The non-type parameter might use already deduced type parameters. */
14723 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
14724 if (!TREE_TYPE (arg))
14725 /* Template-parameter dependent expression. Just accept it for now.
14726 It will later be processed in convert_template_argument. */
14728 else if (same_type_p (TREE_TYPE (arg), tparm))
14730 else if ((strict & UNIFY_ALLOW_INTEGER)
14731 && (TREE_CODE (tparm) == INTEGER_TYPE
14732 || TREE_CODE (tparm) == BOOLEAN_TYPE))
14733 /* Convert the ARG to the type of PARM; the deduced non-type
14734 template argument must exactly match the types of the
14735 corresponding parameter. */
14736 arg = fold (build_nop (tparm, arg));
14737 else if (uses_template_parms (tparm))
14738 /* We haven't deduced the type of this parameter yet. Try again
14744 /* If ARG is a parameter pack or an expansion, we cannot unify
14745 against it unless PARM is also a parameter pack. */
14746 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14747 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
14750 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14755 /* A pointer-to-member constant can be unified only with
14756 another constant. */
14757 if (TREE_CODE (arg) != PTRMEM_CST)
14760 /* Just unify the class member. It would be useless (and possibly
14761 wrong, depending on the strict flags) to unify also
14762 PTRMEM_CST_CLASS, because we want to be sure that both parm and
14763 arg refer to the same variable, even if through different
14764 classes. For instance:
14766 struct A { int x; };
14769 Unification of &A::x and &B::x must succeed. */
14770 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
14771 PTRMEM_CST_MEMBER (arg), strict);
14776 if (TREE_CODE (arg) != POINTER_TYPE)
14779 /* [temp.deduct.call]
14781 A can be another pointer or pointer to member type that can
14782 be converted to the deduced A via a qualification
14783 conversion (_conv.qual_).
14785 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
14786 This will allow for additional cv-qualification of the
14787 pointed-to types if appropriate. */
14789 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
14790 /* The derived-to-base conversion only persists through one
14791 level of pointers. */
14792 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
14794 return unify (tparms, targs, TREE_TYPE (parm),
14795 TREE_TYPE (arg), strict);
14798 case REFERENCE_TYPE:
14799 if (TREE_CODE (arg) != REFERENCE_TYPE)
14801 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14802 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14805 if (TREE_CODE (arg) != ARRAY_TYPE)
14807 if ((TYPE_DOMAIN (parm) == NULL_TREE)
14808 != (TYPE_DOMAIN (arg) == NULL_TREE))
14810 if (TYPE_DOMAIN (parm) != NULL_TREE)
14817 /* Our representation of array types uses "N - 1" as the
14818 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
14819 not an integer constant. We cannot unify arbitrarily
14820 complex expressions, so we eliminate the MINUS_EXPRs
14822 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
14823 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
14826 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
14827 parm_max = TREE_OPERAND (parm_max, 0);
14829 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
14830 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
14833 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
14834 trying to unify the type of a variable with the type
14835 of a template parameter. For example:
14837 template <unsigned int N>
14838 void f (char (&) [N]);
14845 Here, the type of the ARG will be "int [g(i)]", and
14846 may be a SAVE_EXPR, etc. */
14847 if (TREE_CODE (arg_max) != MINUS_EXPR)
14849 arg_max = TREE_OPERAND (arg_max, 0);
14852 /* If only one of the bounds used a MINUS_EXPR, compensate
14853 by adding one to the other bound. */
14854 if (parm_cst && !arg_cst)
14855 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
14859 else if (arg_cst && !parm_cst)
14860 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
14865 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
14868 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14869 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14876 case ENUMERAL_TYPE:
14878 if (TREE_CODE (arg) != TREE_CODE (parm))
14881 /* We have already checked cv-qualification at the top of the
14883 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
14886 /* As far as unification is concerned, this wins. Later checks
14887 will invalidate it if necessary. */
14890 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
14891 /* Type INTEGER_CST can come from ordinary constant template args. */
14893 while (TREE_CODE (arg) == NOP_EXPR)
14894 arg = TREE_OPERAND (arg, 0);
14896 if (TREE_CODE (arg) != INTEGER_CST)
14898 return !tree_int_cst_equal (parm, arg);
14903 if (TREE_CODE (arg) != TREE_VEC)
14905 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
14907 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
14908 if (unify (tparms, targs,
14909 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
14917 if (TREE_CODE (arg) != TREE_CODE (parm))
14920 if (TYPE_PTRMEMFUNC_P (parm))
14922 if (!TYPE_PTRMEMFUNC_P (arg))
14925 return unify (tparms, targs,
14926 TYPE_PTRMEMFUNC_FN_TYPE (parm),
14927 TYPE_PTRMEMFUNC_FN_TYPE (arg),
14931 if (CLASSTYPE_TEMPLATE_INFO (parm))
14933 tree t = NULL_TREE;
14935 if (strict_in & UNIFY_ALLOW_DERIVED)
14937 /* First, we try to unify the PARM and ARG directly. */
14938 t = try_class_unification (tparms, targs,
14943 /* Fallback to the special case allowed in
14944 [temp.deduct.call]:
14946 If P is a class, and P has the form
14947 template-id, then A can be a derived class of
14948 the deduced A. Likewise, if P is a pointer to
14949 a class of the form template-id, A can be a
14950 pointer to a derived class pointed to by the
14952 t = get_template_base (tparms, targs, parm, arg);
14958 else if (CLASSTYPE_TEMPLATE_INFO (arg)
14959 && (CLASSTYPE_TI_TEMPLATE (parm)
14960 == CLASSTYPE_TI_TEMPLATE (arg)))
14961 /* Perhaps PARM is something like S<U> and ARG is S<int>.
14962 Then, we should unify `int' and `U'. */
14965 /* There's no chance of unification succeeding. */
14968 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
14969 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
14971 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
14976 case FUNCTION_TYPE:
14978 unsigned int nargs;
14983 if (TREE_CODE (arg) != TREE_CODE (parm))
14986 /* CV qualifications for methods can never be deduced, they must
14987 match exactly. We need to check them explicitly here,
14988 because type_unification_real treats them as any other
14989 cv-qualified parameter. */
14990 if (TREE_CODE (parm) == METHOD_TYPE
14991 && (!check_cv_quals_for_unify
14993 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
14994 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
14997 if (unify (tparms, targs, TREE_TYPE (parm),
14998 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
15001 nargs = list_length (TYPE_ARG_TYPES (arg));
15002 args = XALLOCAVEC (tree, nargs);
15003 for (a = TYPE_ARG_TYPES (arg), i = 0;
15004 a != NULL_TREE && a != void_list_node;
15005 a = TREE_CHAIN (a), ++i)
15006 args[i] = TREE_VALUE (a);
15009 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
15010 args, nargs, 1, DEDUCE_EXACT,
15015 /* Unify a pointer to member with a pointer to member function, which
15016 deduces the type of the member as a function type. */
15017 if (TYPE_PTRMEMFUNC_P (arg))
15021 cp_cv_quals cv_quals;
15023 /* Check top-level cv qualifiers */
15024 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
15027 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
15028 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
15031 /* Determine the type of the function we are unifying against. */
15032 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
15034 build_function_type (TREE_TYPE (method_type),
15035 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
15037 /* Extract the cv-qualifiers of the member function from the
15038 implicit object parameter and place them on the function
15039 type to be restored later. */
15041 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
15042 fntype = build_qualified_type (fntype, cv_quals);
15043 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
15046 if (TREE_CODE (arg) != OFFSET_TYPE)
15048 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
15049 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
15051 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
15055 if (DECL_TEMPLATE_PARM_P (parm))
15056 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
15057 if (arg != integral_constant_value (parm))
15062 case TEMPLATE_DECL:
15063 /* Matched cases are handled by the ARG == PARM test above. */
15067 /* A non-type template parameter that is a variable should be a
15068 an integral constant, in which case, it whould have been
15069 folded into its (constant) value. So we should not be getting
15070 a variable here. */
15071 gcc_unreachable ();
15073 case TYPE_ARGUMENT_PACK:
15074 case NONTYPE_ARGUMENT_PACK:
15076 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
15077 tree packed_args = ARGUMENT_PACK_ARGS (arg);
15078 int i, len = TREE_VEC_LENGTH (packed_parms);
15079 int argslen = TREE_VEC_LENGTH (packed_args);
15080 int parm_variadic_p = 0;
15082 for (i = 0; i < len; ++i)
15084 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
15087 /* We can unify against something with a trailing
15089 parm_variadic_p = 1;
15091 /* Since there is something following the pack
15092 expansion, we cannot unify this template argument
15099 /* If we don't have enough arguments to satisfy the parameters
15100 (not counting the pack expression at the end), or we have
15101 too many arguments for a parameter list that doesn't end in
15102 a pack expression, we can't unify. */
15103 if (argslen < (len - parm_variadic_p)
15104 || (argslen > len && !parm_variadic_p))
15107 /* Unify all of the parameters that precede the (optional)
15108 pack expression. */
15109 for (i = 0; i < len - parm_variadic_p; ++i)
15111 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
15112 TREE_VEC_ELT (packed_args, i), strict))
15116 if (parm_variadic_p)
15117 return unify_pack_expansion (tparms, targs,
15118 packed_parms, packed_args,
15119 strict, /*call_args_p=*/false,
15127 case DECLTYPE_TYPE:
15128 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
15133 /* Unification fails if we hit an error node. */
15137 gcc_assert (EXPR_P (parm));
15139 /* We must be looking at an expression. This can happen with
15143 void foo(S<I>, S<I + 2>);
15145 This is a "nondeduced context":
15149 The nondeduced contexts are:
15151 --A type that is a template-id in which one or more of
15152 the template-arguments is an expression that references
15153 a template-parameter.
15155 In these cases, we assume deduction succeeded, but don't
15156 actually infer any unifications. */
15158 if (!uses_template_parms (parm)
15159 && !template_args_equal (parm, arg))
15166 /* Note that DECL can be defined in this translation unit, if
15170 mark_definable (tree decl)
15173 DECL_NOT_REALLY_EXTERN (decl) = 1;
15174 FOR_EACH_CLONE (clone, decl)
15175 DECL_NOT_REALLY_EXTERN (clone) = 1;
15178 /* Called if RESULT is explicitly instantiated, or is a member of an
15179 explicitly instantiated class. */
15182 mark_decl_instantiated (tree result, int extern_p)
15184 SET_DECL_EXPLICIT_INSTANTIATION (result);
15186 /* If this entity has already been written out, it's too late to
15187 make any modifications. */
15188 if (TREE_ASM_WRITTEN (result))
15191 if (TREE_CODE (result) != FUNCTION_DECL)
15192 /* The TREE_PUBLIC flag for function declarations will have been
15193 set correctly by tsubst. */
15194 TREE_PUBLIC (result) = 1;
15196 /* This might have been set by an earlier implicit instantiation. */
15197 DECL_COMDAT (result) = 0;
15200 DECL_NOT_REALLY_EXTERN (result) = 0;
15203 mark_definable (result);
15204 /* Always make artificials weak. */
15205 if (DECL_ARTIFICIAL (result) && flag_weak)
15206 comdat_linkage (result);
15207 /* For WIN32 we also want to put explicit instantiations in
15208 linkonce sections. */
15209 else if (TREE_PUBLIC (result))
15210 maybe_make_one_only (result);
15213 /* If EXTERN_P, then this function will not be emitted -- unless
15214 followed by an explicit instantiation, at which point its linkage
15215 will be adjusted. If !EXTERN_P, then this function will be
15216 emitted here. In neither circumstance do we want
15217 import_export_decl to adjust the linkage. */
15218 DECL_INTERFACE_KNOWN (result) = 1;
15221 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
15222 important template arguments. If any are missing, we check whether
15223 they're important by using error_mark_node for substituting into any
15224 args that were used for partial ordering (the ones between ARGS and END)
15225 and seeing if it bubbles up. */
15228 check_undeduced_parms (tree targs, tree args, tree end)
15230 bool found = false;
15232 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
15233 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
15236 TREE_VEC_ELT (targs, i) = error_mark_node;
15240 for (; args != end; args = TREE_CHAIN (args))
15242 tree substed = tsubst (TREE_VALUE (args), targs, tf_none, NULL_TREE);
15243 if (substed == error_mark_node)
15250 /* Given two function templates PAT1 and PAT2, return:
15252 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
15253 -1 if PAT2 is more specialized than PAT1.
15254 0 if neither is more specialized.
15256 LEN indicates the number of parameters we should consider
15257 (defaulted parameters should not be considered).
15259 The 1998 std underspecified function template partial ordering, and
15260 DR214 addresses the issue. We take pairs of arguments, one from
15261 each of the templates, and deduce them against each other. One of
15262 the templates will be more specialized if all the *other*
15263 template's arguments deduce against its arguments and at least one
15264 of its arguments *does* *not* deduce against the other template's
15265 corresponding argument. Deduction is done as for class templates.
15266 The arguments used in deduction have reference and top level cv
15267 qualifiers removed. Iff both arguments were originally reference
15268 types *and* deduction succeeds in both directions, the template
15269 with the more cv-qualified argument wins for that pairing (if
15270 neither is more cv-qualified, they both are equal). Unlike regular
15271 deduction, after all the arguments have been deduced in this way,
15272 we do *not* verify the deduced template argument values can be
15273 substituted into non-deduced contexts.
15275 The logic can be a bit confusing here, because we look at deduce1 and
15276 targs1 to see if pat2 is at least as specialized, and vice versa; if we
15277 can find template arguments for pat1 to make arg1 look like arg2, that
15278 means that arg2 is at least as specialized as arg1. */
15281 more_specialized_fn (tree pat1, tree pat2, int len)
15283 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
15284 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
15285 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
15286 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
15287 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
15288 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
15289 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
15290 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
15291 tree origs1, origs2;
15292 bool lose1 = false;
15293 bool lose2 = false;
15295 /* Remove the this parameter from non-static member functions. If
15296 one is a non-static member function and the other is not a static
15297 member function, remove the first parameter from that function
15298 also. This situation occurs for operator functions where we
15299 locate both a member function (with this pointer) and non-member
15300 operator (with explicit first operand). */
15301 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
15303 len--; /* LEN is the number of significant arguments for DECL1 */
15304 args1 = TREE_CHAIN (args1);
15305 if (!DECL_STATIC_FUNCTION_P (decl2))
15306 args2 = TREE_CHAIN (args2);
15308 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
15310 args2 = TREE_CHAIN (args2);
15311 if (!DECL_STATIC_FUNCTION_P (decl1))
15314 args1 = TREE_CHAIN (args1);
15318 /* If only one is a conversion operator, they are unordered. */
15319 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
15322 /* Consider the return type for a conversion function */
15323 if (DECL_CONV_FN_P (decl1))
15325 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
15326 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
15330 processing_template_decl++;
15336 /* Stop when an ellipsis is seen. */
15337 && args1 != NULL_TREE && args2 != NULL_TREE)
15339 tree arg1 = TREE_VALUE (args1);
15340 tree arg2 = TREE_VALUE (args2);
15341 int deduce1, deduce2;
15345 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15346 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15348 /* When both arguments are pack expansions, we need only
15349 unify the patterns themselves. */
15350 arg1 = PACK_EXPANSION_PATTERN (arg1);
15351 arg2 = PACK_EXPANSION_PATTERN (arg2);
15353 /* This is the last comparison we need to do. */
15357 if (TREE_CODE (arg1) == REFERENCE_TYPE)
15359 arg1 = TREE_TYPE (arg1);
15360 quals1 = cp_type_quals (arg1);
15363 if (TREE_CODE (arg2) == REFERENCE_TYPE)
15365 arg2 = TREE_TYPE (arg2);
15366 quals2 = cp_type_quals (arg2);
15369 if ((quals1 < 0) != (quals2 < 0))
15371 /* Only of the args is a reference, see if we should apply
15372 array/function pointer decay to it. This is not part of
15373 DR214, but is, IMHO, consistent with the deduction rules
15374 for the function call itself, and with our earlier
15375 implementation of the underspecified partial ordering
15376 rules. (nathan). */
15379 switch (TREE_CODE (arg1))
15382 arg1 = TREE_TYPE (arg1);
15384 case FUNCTION_TYPE:
15385 arg1 = build_pointer_type (arg1);
15394 switch (TREE_CODE (arg2))
15397 arg2 = TREE_TYPE (arg2);
15399 case FUNCTION_TYPE:
15400 arg2 = build_pointer_type (arg2);
15409 arg1 = TYPE_MAIN_VARIANT (arg1);
15410 arg2 = TYPE_MAIN_VARIANT (arg2);
15412 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
15414 int i, len2 = list_length (args2);
15415 tree parmvec = make_tree_vec (1);
15416 tree argvec = make_tree_vec (len2);
15419 /* Setup the parameter vector, which contains only ARG1. */
15420 TREE_VEC_ELT (parmvec, 0) = arg1;
15422 /* Setup the argument vector, which contains the remaining
15424 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
15425 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15427 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
15428 argvec, UNIFY_ALLOW_NONE,
15429 /*call_args_p=*/false,
15432 /* We cannot deduce in the other direction, because ARG1 is
15433 a pack expansion but ARG2 is not. */
15436 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15438 int i, len1 = list_length (args1);
15439 tree parmvec = make_tree_vec (1);
15440 tree argvec = make_tree_vec (len1);
15443 /* Setup the parameter vector, which contains only ARG1. */
15444 TREE_VEC_ELT (parmvec, 0) = arg2;
15446 /* Setup the argument vector, which contains the remaining
15448 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
15449 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15451 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
15452 argvec, UNIFY_ALLOW_NONE,
15453 /*call_args_p=*/false,
15456 /* We cannot deduce in the other direction, because ARG2 is
15457 a pack expansion but ARG1 is not.*/
15463 /* The normal case, where neither argument is a pack
15465 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
15466 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
15469 /* If we couldn't deduce arguments for tparms1 to make arg1 match
15470 arg2, then arg2 is not as specialized as arg1. */
15476 /* "If, for a given type, deduction succeeds in both directions
15477 (i.e., the types are identical after the transformations above)
15478 and if the type from the argument template is more cv-qualified
15479 than the type from the parameter template (as described above)
15480 that type is considered to be more specialized than the other. If
15481 neither type is more cv-qualified than the other then neither type
15482 is more specialized than the other." */
15484 if (deduce1 && deduce2
15485 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
15487 if ((quals1 & quals2) == quals2)
15489 if ((quals1 & quals2) == quals1)
15493 if (lose1 && lose2)
15494 /* We've failed to deduce something in either direction.
15495 These must be unordered. */
15498 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15499 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15500 /* We have already processed all of the arguments in our
15501 handing of the pack expansion type. */
15504 args1 = TREE_CHAIN (args1);
15505 args2 = TREE_CHAIN (args2);
15508 /* "In most cases, all template parameters must have values in order for
15509 deduction to succeed, but for partial ordering purposes a template
15510 parameter may remain without a value provided it is not used in the
15511 types being used for partial ordering."
15513 Thus, if we are missing any of the targs1 we need to substitute into
15514 origs1, then pat2 is not as specialized as pat1. This can happen when
15515 there is a nondeduced context. */
15516 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
15518 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
15521 processing_template_decl--;
15523 /* All things being equal, if the next argument is a pack expansion
15524 for one function but not for the other, prefer the
15525 non-variadic function. FIXME this is bogus; see c++/41958. */
15527 && args1 && TREE_VALUE (args1)
15528 && args2 && TREE_VALUE (args2))
15530 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
15531 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
15534 if (lose1 == lose2)
15542 /* Determine which of two partial specializations is more specialized.
15544 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
15545 to the first partial specialization. The TREE_VALUE is the
15546 innermost set of template parameters for the partial
15547 specialization. PAT2 is similar, but for the second template.
15549 Return 1 if the first partial specialization is more specialized;
15550 -1 if the second is more specialized; 0 if neither is more
15553 See [temp.class.order] for information about determining which of
15554 two templates is more specialized. */
15557 more_specialized_class (tree pat1, tree pat2)
15562 bool any_deductions = false;
15564 tmpl1 = TREE_TYPE (pat1);
15565 tmpl2 = TREE_TYPE (pat2);
15567 /* Just like what happens for functions, if we are ordering between
15568 different class template specializations, we may encounter dependent
15569 types in the arguments, and we need our dependency check functions
15570 to behave correctly. */
15571 ++processing_template_decl;
15572 targs = get_class_bindings (TREE_VALUE (pat1),
15573 CLASSTYPE_TI_ARGS (tmpl1),
15574 CLASSTYPE_TI_ARGS (tmpl2));
15578 any_deductions = true;
15581 targs = get_class_bindings (TREE_VALUE (pat2),
15582 CLASSTYPE_TI_ARGS (tmpl2),
15583 CLASSTYPE_TI_ARGS (tmpl1));
15587 any_deductions = true;
15589 --processing_template_decl;
15591 /* In the case of a tie where at least one of the class templates
15592 has a parameter pack at the end, the template with the most
15593 non-packed parameters wins. */
15596 && (template_args_variadic_p (TREE_PURPOSE (pat1))
15597 || template_args_variadic_p (TREE_PURPOSE (pat2))))
15599 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
15600 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
15601 int len1 = TREE_VEC_LENGTH (args1);
15602 int len2 = TREE_VEC_LENGTH (args2);
15604 /* We don't count the pack expansion at the end. */
15605 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
15607 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
15612 else if (len1 < len2)
15619 /* Return the template arguments that will produce the function signature
15620 DECL from the function template FN, with the explicit template
15621 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
15622 also match. Return NULL_TREE if no satisfactory arguments could be
15626 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
15628 int ntparms = DECL_NTPARMS (fn);
15629 tree targs = make_tree_vec (ntparms);
15631 tree decl_arg_types;
15633 unsigned int nargs, ix;
15636 /* Substitute the explicit template arguments into the type of DECL.
15637 The call to fn_type_unification will handle substitution into the
15639 decl_type = TREE_TYPE (decl);
15640 if (explicit_args && uses_template_parms (decl_type))
15643 tree converted_args;
15645 if (DECL_TEMPLATE_INFO (decl))
15646 tmpl = DECL_TI_TEMPLATE (decl);
15648 /* We can get here for some invalid specializations. */
15652 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
15653 explicit_args, NULL_TREE,
15655 /*require_all_args=*/false,
15656 /*use_default_args=*/false);
15657 if (converted_args == error_mark_node)
15660 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
15661 if (decl_type == error_mark_node)
15665 /* Never do unification on the 'this' parameter. */
15666 decl_arg_types = skip_artificial_parms_for (decl,
15667 TYPE_ARG_TYPES (decl_type));
15669 nargs = list_length (decl_arg_types);
15670 args = XALLOCAVEC (tree, nargs);
15671 for (arg = decl_arg_types, ix = 0;
15672 arg != NULL_TREE && arg != void_list_node;
15673 arg = TREE_CHAIN (arg), ++ix)
15674 args[ix] = TREE_VALUE (arg);
15676 if (fn_type_unification (fn, explicit_args, targs,
15678 (check_rettype || DECL_CONV_FN_P (fn)
15679 ? TREE_TYPE (decl_type) : NULL_TREE),
15680 DEDUCE_EXACT, LOOKUP_NORMAL))
15686 /* Return the innermost template arguments that, when applied to a
15687 template specialization whose innermost template parameters are
15688 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
15691 For example, suppose we have:
15693 template <class T, class U> struct S {};
15694 template <class T> struct S<T*, int> {};
15696 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
15697 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
15698 int}. The resulting vector will be {double}, indicating that `T'
15699 is bound to `double'. */
15702 get_class_bindings (tree tparms, tree spec_args, tree args)
15704 int i, ntparms = TREE_VEC_LENGTH (tparms);
15706 tree innermost_deduced_args;
15708 innermost_deduced_args = make_tree_vec (ntparms);
15709 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15711 deduced_args = copy_node (args);
15712 SET_TMPL_ARGS_LEVEL (deduced_args,
15713 TMPL_ARGS_DEPTH (deduced_args),
15714 innermost_deduced_args);
15717 deduced_args = innermost_deduced_args;
15719 if (unify (tparms, deduced_args,
15720 INNERMOST_TEMPLATE_ARGS (spec_args),
15721 INNERMOST_TEMPLATE_ARGS (args),
15725 for (i = 0; i < ntparms; ++i)
15726 if (! TREE_VEC_ELT (innermost_deduced_args, i))
15729 /* Verify that nondeduced template arguments agree with the type
15730 obtained from argument deduction.
15734 struct A { typedef int X; };
15735 template <class T, class U> struct C {};
15736 template <class T> struct C<T, typename T::X> {};
15738 Then with the instantiation `C<A, int>', we can deduce that
15739 `T' is `A' but unify () does not check whether `typename T::X'
15741 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
15742 if (spec_args == error_mark_node
15743 /* We only need to check the innermost arguments; the other
15744 arguments will always agree. */
15745 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
15746 INNERMOST_TEMPLATE_ARGS (args)))
15749 /* Now that we have bindings for all of the template arguments,
15750 ensure that the arguments deduced for the template template
15751 parameters have compatible template parameter lists. See the use
15752 of template_template_parm_bindings_ok_p in fn_type_unification
15753 for more information. */
15754 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
15757 return deduced_args;
15760 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
15761 Return the TREE_LIST node with the most specialized template, if
15762 any. If there is no most specialized template, the error_mark_node
15765 Note that this function does not look at, or modify, the
15766 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
15767 returned is one of the elements of INSTANTIATIONS, callers may
15768 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
15769 and retrieve it from the value returned. */
15772 most_specialized_instantiation (tree templates)
15776 ++processing_template_decl;
15779 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
15783 if (get_bindings (TREE_VALUE (champ),
15784 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15785 NULL_TREE, /*check_ret=*/false))
15788 if (get_bindings (TREE_VALUE (fn),
15789 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15790 NULL_TREE, /*check_ret=*/false))
15797 /* Equally specialized, move to next function. If there
15798 is no next function, nothing's most specialized. */
15799 fn = TREE_CHAIN (fn);
15807 /* Now verify that champ is better than everything earlier in the
15808 instantiation list. */
15809 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
15810 if (get_bindings (TREE_VALUE (champ),
15811 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15812 NULL_TREE, /*check_ret=*/false)
15813 || !get_bindings (TREE_VALUE (fn),
15814 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15815 NULL_TREE, /*check_ret=*/false))
15821 processing_template_decl--;
15824 return error_mark_node;
15829 /* If DECL is a specialization of some template, return the most
15830 general such template. Otherwise, returns NULL_TREE.
15832 For example, given:
15834 template <class T> struct S { template <class U> void f(U); };
15836 if TMPL is `template <class U> void S<int>::f(U)' this will return
15837 the full template. This function will not trace past partial
15838 specializations, however. For example, given in addition:
15840 template <class T> struct S<T*> { template <class U> void f(U); };
15842 if TMPL is `template <class U> void S<int*>::f(U)' this will return
15843 `template <class T> template <class U> S<T*>::f(U)'. */
15846 most_general_template (tree decl)
15848 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
15849 an immediate specialization. */
15850 if (TREE_CODE (decl) == FUNCTION_DECL)
15852 if (DECL_TEMPLATE_INFO (decl)) {
15853 decl = DECL_TI_TEMPLATE (decl);
15855 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
15856 template friend. */
15857 if (TREE_CODE (decl) != TEMPLATE_DECL)
15863 /* Look for more and more general templates. */
15864 while (DECL_TEMPLATE_INFO (decl))
15866 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
15867 (See cp-tree.h for details.) */
15868 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
15871 if (CLASS_TYPE_P (TREE_TYPE (decl))
15872 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
15875 /* Stop if we run into an explicitly specialized class template. */
15876 if (!DECL_NAMESPACE_SCOPE_P (decl)
15877 && DECL_CONTEXT (decl)
15878 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
15881 decl = DECL_TI_TEMPLATE (decl);
15887 /* Return the most specialized of the class template partial
15888 specializations of TMPL which can produce TYPE, a specialization of
15889 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
15890 a _TYPE node corresponding to the partial specialization, while the
15891 TREE_PURPOSE is the set of template arguments that must be
15892 substituted into the TREE_TYPE in order to generate TYPE.
15894 If the choice of partial specialization is ambiguous, a diagnostic
15895 is issued, and the error_mark_node is returned. If there are no
15896 partial specializations of TMPL matching TYPE, then NULL_TREE is
15900 most_specialized_class (tree type, tree tmpl)
15902 tree list = NULL_TREE;
15908 tree outer_args = NULL_TREE;
15910 tmpl = most_general_template (tmpl);
15911 args = CLASSTYPE_TI_ARGS (type);
15913 /* For determining which partial specialization to use, only the
15914 innermost args are interesting. */
15915 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15917 outer_args = strip_innermost_template_args (args, 1);
15918 args = INNERMOST_TEMPLATE_ARGS (args);
15921 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
15923 tree partial_spec_args;
15925 tree parms = TREE_VALUE (t);
15927 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
15932 ++processing_template_decl;
15934 /* Discard the outer levels of args, and then substitute in the
15935 template args from the enclosing class. */
15936 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
15937 partial_spec_args = tsubst_template_args
15938 (partial_spec_args, outer_args, tf_none, NULL_TREE);
15940 /* PARMS already refers to just the innermost parms, but the
15941 template parms in partial_spec_args had their levels lowered
15942 by tsubst, so we need to do the same for the parm list. We
15943 can't just tsubst the TREE_VEC itself, as tsubst wants to
15944 treat a TREE_VEC as an argument vector. */
15945 parms = copy_node (parms);
15946 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
15947 TREE_VEC_ELT (parms, i) =
15948 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
15950 --processing_template_decl;
15953 partial_spec_args =
15954 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
15955 add_to_template_args (outer_args,
15956 partial_spec_args),
15958 /*require_all_args=*/true,
15959 /*use_default_args=*/true);
15961 if (partial_spec_args == error_mark_node)
15962 return error_mark_node;
15964 spec_args = get_class_bindings (parms,
15970 spec_args = add_to_template_args (outer_args, spec_args);
15971 list = tree_cons (spec_args, TREE_VALUE (t), list);
15972 TREE_TYPE (list) = TREE_TYPE (t);
15979 ambiguous_p = false;
15982 t = TREE_CHAIN (t);
15983 for (; t; t = TREE_CHAIN (t))
15985 fate = more_specialized_class (champ, t);
15992 t = TREE_CHAIN (t);
15995 ambiguous_p = true;
16004 for (t = list; t && t != champ; t = TREE_CHAIN (t))
16006 fate = more_specialized_class (champ, t);
16009 ambiguous_p = true;
16017 char *spaces = NULL;
16018 error ("ambiguous class template instantiation for %q#T", type);
16019 str = TREE_CHAIN (list) ? _("candidates are:") : _("candidate is:");
16020 for (t = list; t; t = TREE_CHAIN (t))
16022 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
16023 spaces = spaces ? spaces : get_spaces (str);
16026 return error_mark_node;
16032 /* Explicitly instantiate DECL. */
16035 do_decl_instantiation (tree decl, tree storage)
16037 tree result = NULL_TREE;
16040 if (!decl || decl == error_mark_node)
16041 /* An error occurred, for which grokdeclarator has already issued
16042 an appropriate message. */
16044 else if (! DECL_LANG_SPECIFIC (decl))
16046 error ("explicit instantiation of non-template %q#D", decl);
16049 else if (TREE_CODE (decl) == VAR_DECL)
16051 /* There is an asymmetry here in the way VAR_DECLs and
16052 FUNCTION_DECLs are handled by grokdeclarator. In the case of
16053 the latter, the DECL we get back will be marked as a
16054 template instantiation, and the appropriate
16055 DECL_TEMPLATE_INFO will be set up. This does not happen for
16056 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
16057 should handle VAR_DECLs as it currently handles
16059 if (!DECL_CLASS_SCOPE_P (decl))
16061 error ("%qD is not a static data member of a class template", decl);
16064 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
16065 if (!result || TREE_CODE (result) != VAR_DECL)
16067 error ("no matching template for %qD found", decl);
16070 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
16072 error ("type %qT for explicit instantiation %qD does not match "
16073 "declared type %qT", TREE_TYPE (result), decl,
16078 else if (TREE_CODE (decl) != FUNCTION_DECL)
16080 error ("explicit instantiation of %q#D", decl);
16086 /* Check for various error cases. Note that if the explicit
16087 instantiation is valid the RESULT will currently be marked as an
16088 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
16089 until we get here. */
16091 if (DECL_TEMPLATE_SPECIALIZATION (result))
16093 /* DR 259 [temp.spec].
16095 Both an explicit instantiation and a declaration of an explicit
16096 specialization shall not appear in a program unless the explicit
16097 instantiation follows a declaration of the explicit specialization.
16099 For a given set of template parameters, if an explicit
16100 instantiation of a template appears after a declaration of an
16101 explicit specialization for that template, the explicit
16102 instantiation has no effect. */
16105 else if (DECL_EXPLICIT_INSTANTIATION (result))
16109 No program shall explicitly instantiate any template more
16112 We check DECL_NOT_REALLY_EXTERN so as not to complain when
16113 the first instantiation was `extern' and the second is not,
16114 and EXTERN_P for the opposite case. */
16115 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
16116 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
16117 /* If an "extern" explicit instantiation follows an ordinary
16118 explicit instantiation, the template is instantiated. */
16122 else if (!DECL_IMPLICIT_INSTANTIATION (result))
16124 error ("no matching template for %qD found", result);
16127 else if (!DECL_TEMPLATE_INFO (result))
16129 permerror (input_location, "explicit instantiation of non-template %q#D", result);
16133 if (storage == NULL_TREE)
16135 else if (storage == ridpointers[(int) RID_EXTERN])
16137 if (!in_system_header && (cxx_dialect == cxx98))
16138 pedwarn (input_location, OPT_pedantic,
16139 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
16144 error ("storage class %qD applied to template instantiation", storage);
16146 check_explicit_instantiation_namespace (result);
16147 mark_decl_instantiated (result, extern_p);
16149 instantiate_decl (result, /*defer_ok=*/1,
16150 /*expl_inst_class_mem_p=*/false);
16154 mark_class_instantiated (tree t, int extern_p)
16156 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
16157 SET_CLASSTYPE_INTERFACE_KNOWN (t);
16158 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
16159 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
16162 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
16163 rest_of_type_compilation (t, 1);
16167 /* Called from do_type_instantiation through binding_table_foreach to
16168 do recursive instantiation for the type bound in ENTRY. */
16170 bt_instantiate_type_proc (binding_entry entry, void *data)
16172 tree storage = *(tree *) data;
16174 if (MAYBE_CLASS_TYPE_P (entry->type)
16175 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
16176 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
16179 /* Called from do_type_instantiation to instantiate a member
16180 (a member function or a static member variable) of an
16181 explicitly instantiated class template. */
16183 instantiate_class_member (tree decl, int extern_p)
16185 mark_decl_instantiated (decl, extern_p);
16187 instantiate_decl (decl, /*defer_ok=*/1,
16188 /*expl_inst_class_mem_p=*/true);
16191 /* Perform an explicit instantiation of template class T. STORAGE, if
16192 non-null, is the RID for extern, inline or static. COMPLAIN is
16193 nonzero if this is called from the parser, zero if called recursively,
16194 since the standard is unclear (as detailed below). */
16197 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
16202 int previous_instantiation_extern_p = 0;
16204 if (TREE_CODE (t) == TYPE_DECL)
16207 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
16209 error ("explicit instantiation of non-template type %qT", t);
16215 if (!COMPLETE_TYPE_P (t))
16217 if (complain & tf_error)
16218 error ("explicit instantiation of %q#T before definition of template",
16223 if (storage != NULL_TREE)
16225 if (!in_system_header)
16227 if (storage == ridpointers[(int) RID_EXTERN])
16229 if (cxx_dialect == cxx98)
16230 pedwarn (input_location, OPT_pedantic,
16231 "ISO C++ 1998 forbids the use of %<extern%> on "
16232 "explicit instantiations");
16235 pedwarn (input_location, OPT_pedantic,
16236 "ISO C++ forbids the use of %qE"
16237 " on explicit instantiations", storage);
16240 if (storage == ridpointers[(int) RID_INLINE])
16242 else if (storage == ridpointers[(int) RID_EXTERN])
16244 else if (storage == ridpointers[(int) RID_STATIC])
16248 error ("storage class %qD applied to template instantiation",
16254 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
16256 /* DR 259 [temp.spec].
16258 Both an explicit instantiation and a declaration of an explicit
16259 specialization shall not appear in a program unless the explicit
16260 instantiation follows a declaration of the explicit specialization.
16262 For a given set of template parameters, if an explicit
16263 instantiation of a template appears after a declaration of an
16264 explicit specialization for that template, the explicit
16265 instantiation has no effect. */
16268 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
16272 No program shall explicitly instantiate any template more
16275 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
16276 instantiation was `extern'. If EXTERN_P then the second is.
16277 These cases are OK. */
16278 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
16280 if (!previous_instantiation_extern_p && !extern_p
16281 && (complain & tf_error))
16282 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
16284 /* If we've already instantiated the template, just return now. */
16285 if (!CLASSTYPE_INTERFACE_ONLY (t))
16289 check_explicit_instantiation_namespace (TYPE_NAME (t));
16290 mark_class_instantiated (t, extern_p);
16298 /* In contrast to implicit instantiation, where only the
16299 declarations, and not the definitions, of members are
16300 instantiated, we have here:
16304 The explicit instantiation of a class template specialization
16305 implies the instantiation of all of its members not
16306 previously explicitly specialized in the translation unit
16307 containing the explicit instantiation.
16309 Of course, we can't instantiate member template classes, since
16310 we don't have any arguments for them. Note that the standard
16311 is unclear on whether the instantiation of the members are
16312 *explicit* instantiations or not. However, the most natural
16313 interpretation is that it should be an explicit instantiation. */
16316 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
16317 if (TREE_CODE (tmp) == FUNCTION_DECL
16318 && DECL_TEMPLATE_INSTANTIATION (tmp))
16319 instantiate_class_member (tmp, extern_p);
16321 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
16322 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
16323 instantiate_class_member (tmp, extern_p);
16325 if (CLASSTYPE_NESTED_UTDS (t))
16326 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
16327 bt_instantiate_type_proc, &storage);
16331 /* Given a function DECL, which is a specialization of TMPL, modify
16332 DECL to be a re-instantiation of TMPL with the same template
16333 arguments. TMPL should be the template into which tsubst'ing
16334 should occur for DECL, not the most general template.
16336 One reason for doing this is a scenario like this:
16339 void f(const T&, int i);
16341 void g() { f(3, 7); }
16344 void f(const T& t, const int i) { }
16346 Note that when the template is first instantiated, with
16347 instantiate_template, the resulting DECL will have no name for the
16348 first parameter, and the wrong type for the second. So, when we go
16349 to instantiate the DECL, we regenerate it. */
16352 regenerate_decl_from_template (tree decl, tree tmpl)
16354 /* The arguments used to instantiate DECL, from the most general
16359 args = DECL_TI_ARGS (decl);
16360 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
16362 /* Make sure that we can see identifiers, and compute access
16364 push_access_scope (decl);
16366 if (TREE_CODE (decl) == FUNCTION_DECL)
16374 args_depth = TMPL_ARGS_DEPTH (args);
16375 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
16376 if (args_depth > parms_depth)
16377 args = get_innermost_template_args (args, parms_depth);
16379 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
16380 args, tf_error, NULL_TREE);
16382 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
16385 /* Merge parameter declarations. */
16386 decl_parm = skip_artificial_parms_for (decl,
16387 DECL_ARGUMENTS (decl));
16389 = skip_artificial_parms_for (code_pattern,
16390 DECL_ARGUMENTS (code_pattern));
16391 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
16396 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16397 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
16398 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
16400 parm_type = type_decays_to (parm_type);
16401 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16402 TREE_TYPE (decl_parm) = parm_type;
16403 attributes = DECL_ATTRIBUTES (pattern_parm);
16404 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16406 DECL_ATTRIBUTES (decl_parm) = attributes;
16407 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16409 decl_parm = TREE_CHAIN (decl_parm);
16410 pattern_parm = TREE_CHAIN (pattern_parm);
16412 /* Merge any parameters that match with the function parameter
16414 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
16417 tree expanded_types;
16418 /* Expand the TYPE_PACK_EXPANSION that provides the types for
16419 the parameters in this function parameter pack. */
16420 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
16421 args, tf_error, NULL_TREE);
16422 len = TREE_VEC_LENGTH (expanded_types);
16423 for (i = 0; i < len; i++)
16428 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16429 /* Rename the parameter to include the index. */
16430 DECL_NAME (decl_parm) =
16431 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
16432 parm_type = TREE_VEC_ELT (expanded_types, i);
16433 parm_type = type_decays_to (parm_type);
16434 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16435 TREE_TYPE (decl_parm) = parm_type;
16436 attributes = DECL_ATTRIBUTES (pattern_parm);
16437 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16439 DECL_ATTRIBUTES (decl_parm) = attributes;
16440 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16442 decl_parm = TREE_CHAIN (decl_parm);
16445 /* Merge additional specifiers from the CODE_PATTERN. */
16446 if (DECL_DECLARED_INLINE_P (code_pattern)
16447 && !DECL_DECLARED_INLINE_P (decl))
16448 DECL_DECLARED_INLINE_P (decl) = 1;
16450 else if (TREE_CODE (decl) == VAR_DECL)
16451 DECL_INITIAL (decl) =
16452 tsubst_expr (DECL_INITIAL (code_pattern), args,
16453 tf_error, DECL_TI_TEMPLATE (decl),
16454 /*integral_constant_expression_p=*/false);
16456 gcc_unreachable ();
16458 pop_access_scope (decl);
16461 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
16462 substituted to get DECL. */
16465 template_for_substitution (tree decl)
16467 tree tmpl = DECL_TI_TEMPLATE (decl);
16469 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
16470 for the instantiation. This is not always the most general
16471 template. Consider, for example:
16474 struct S { template <class U> void f();
16475 template <> void f<int>(); };
16477 and an instantiation of S<double>::f<int>. We want TD to be the
16478 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
16479 while (/* An instantiation cannot have a definition, so we need a
16480 more general template. */
16481 DECL_TEMPLATE_INSTANTIATION (tmpl)
16482 /* We must also deal with friend templates. Given:
16484 template <class T> struct S {
16485 template <class U> friend void f() {};
16488 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
16489 so far as the language is concerned, but that's still
16490 where we get the pattern for the instantiation from. On
16491 other hand, if the definition comes outside the class, say:
16493 template <class T> struct S {
16494 template <class U> friend void f();
16496 template <class U> friend void f() {}
16498 we don't need to look any further. That's what the check for
16499 DECL_INITIAL is for. */
16500 || (TREE_CODE (decl) == FUNCTION_DECL
16501 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
16502 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
16504 /* The present template, TD, should not be a definition. If it
16505 were a definition, we should be using it! Note that we
16506 cannot restructure the loop to just keep going until we find
16507 a template with a definition, since that might go too far if
16508 a specialization was declared, but not defined. */
16509 gcc_assert (TREE_CODE (decl) != VAR_DECL
16510 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
16512 /* Fetch the more general template. */
16513 tmpl = DECL_TI_TEMPLATE (tmpl);
16519 /* Returns true if we need to instantiate this template instance even if we
16520 know we aren't going to emit it.. */
16523 always_instantiate_p (tree decl)
16525 /* We always instantiate inline functions so that we can inline them. An
16526 explicit instantiation declaration prohibits implicit instantiation of
16527 non-inline functions. With high levels of optimization, we would
16528 normally inline non-inline functions -- but we're not allowed to do
16529 that for "extern template" functions. Therefore, we check
16530 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
16531 return ((TREE_CODE (decl) == FUNCTION_DECL
16532 && DECL_DECLARED_INLINE_P (decl))
16533 /* And we need to instantiate static data members so that
16534 their initializers are available in integral constant
16536 || (TREE_CODE (decl) == VAR_DECL
16537 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)));
16540 /* Produce the definition of D, a _DECL generated from a template. If
16541 DEFER_OK is nonzero, then we don't have to actually do the
16542 instantiation now; we just have to do it sometime. Normally it is
16543 an error if this is an explicit instantiation but D is undefined.
16544 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
16545 explicitly instantiated class template. */
16548 instantiate_decl (tree d, int defer_ok,
16549 bool expl_inst_class_mem_p)
16551 tree tmpl = DECL_TI_TEMPLATE (d);
16558 bool pattern_defined;
16560 location_t saved_loc = input_location;
16563 /* This function should only be used to instantiate templates for
16564 functions and static member variables. */
16565 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
16566 || TREE_CODE (d) == VAR_DECL);
16568 /* Variables are never deferred; if instantiation is required, they
16569 are instantiated right away. That allows for better code in the
16570 case that an expression refers to the value of the variable --
16571 if the variable has a constant value the referring expression can
16572 take advantage of that fact. */
16573 if (TREE_CODE (d) == VAR_DECL)
16576 /* Don't instantiate cloned functions. Instead, instantiate the
16577 functions they cloned. */
16578 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
16579 d = DECL_CLONED_FUNCTION (d);
16581 if (DECL_TEMPLATE_INSTANTIATED (d)
16582 || DECL_TEMPLATE_SPECIALIZATION (d))
16583 /* D has already been instantiated or explicitly specialized, so
16584 there's nothing for us to do here.
16586 It might seem reasonable to check whether or not D is an explicit
16587 instantiation, and, if so, stop here. But when an explicit
16588 instantiation is deferred until the end of the compilation,
16589 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
16590 the instantiation. */
16593 /* Check to see whether we know that this template will be
16594 instantiated in some other file, as with "extern template"
16596 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
16598 /* In general, we do not instantiate such templates. */
16599 if (external_p && !always_instantiate_p (d))
16602 gen_tmpl = most_general_template (tmpl);
16603 gen_args = DECL_TI_ARGS (d);
16605 if (tmpl != gen_tmpl)
16606 /* We should already have the extra args. */
16607 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
16608 == TMPL_ARGS_DEPTH (gen_args));
16609 /* And what's in the hash table should match D. */
16610 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
16611 || spec == NULL_TREE);
16613 /* This needs to happen before any tsubsting. */
16614 if (! push_tinst_level (d))
16617 timevar_push (TV_PARSE);
16619 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
16620 for the instantiation. */
16621 td = template_for_substitution (d);
16622 code_pattern = DECL_TEMPLATE_RESULT (td);
16624 /* We should never be trying to instantiate a member of a class
16625 template or partial specialization. */
16626 gcc_assert (d != code_pattern);
16628 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
16629 || DECL_TEMPLATE_SPECIALIZATION (td))
16630 /* In the case of a friend template whose definition is provided
16631 outside the class, we may have too many arguments. Drop the
16632 ones we don't need. The same is true for specializations. */
16633 args = get_innermost_template_args
16634 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
16638 if (TREE_CODE (d) == FUNCTION_DECL)
16639 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
16641 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
16643 /* We may be in the middle of deferred access check. Disable it now. */
16644 push_deferring_access_checks (dk_no_deferred);
16646 /* Unless an explicit instantiation directive has already determined
16647 the linkage of D, remember that a definition is available for
16649 if (pattern_defined
16650 && !DECL_INTERFACE_KNOWN (d)
16651 && !DECL_NOT_REALLY_EXTERN (d))
16652 mark_definable (d);
16654 input_location = DECL_SOURCE_LOCATION (d);
16656 /* If D is a member of an explicitly instantiated class template,
16657 and no definition is available, treat it like an implicit
16659 if (!pattern_defined && expl_inst_class_mem_p
16660 && DECL_EXPLICIT_INSTANTIATION (d))
16662 DECL_NOT_REALLY_EXTERN (d) = 0;
16663 DECL_INTERFACE_KNOWN (d) = 0;
16664 SET_DECL_IMPLICIT_INSTANTIATION (d);
16667 /* Recheck the substitutions to obtain any warning messages
16668 about ignoring cv qualifiers. Don't do this for artificial decls,
16669 as it breaks the context-sensitive substitution for lambda op(). */
16670 if (!defer_ok && !DECL_ARTIFICIAL (d))
16672 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
16673 tree type = TREE_TYPE (gen);
16675 /* Make sure that we can see identifiers, and compute access
16676 correctly. D is already the target FUNCTION_DECL with the
16678 push_access_scope (d);
16680 if (TREE_CODE (gen) == FUNCTION_DECL)
16682 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
16683 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
16685 /* Don't simply tsubst the function type, as that will give
16686 duplicate warnings about poor parameter qualifications.
16687 The function arguments are the same as the decl_arguments
16688 without the top level cv qualifiers. */
16689 type = TREE_TYPE (type);
16691 tsubst (type, gen_args, tf_warning_or_error, d);
16693 pop_access_scope (d);
16696 /* Defer all other templates, unless we have been explicitly
16697 forbidden from doing so. */
16698 if (/* If there is no definition, we cannot instantiate the
16701 /* If it's OK to postpone instantiation, do so. */
16703 /* If this is a static data member that will be defined
16704 elsewhere, we don't want to instantiate the entire data
16705 member, but we do want to instantiate the initializer so that
16706 we can substitute that elsewhere. */
16707 || (external_p && TREE_CODE (d) == VAR_DECL))
16709 /* The definition of the static data member is now required so
16710 we must substitute the initializer. */
16711 if (TREE_CODE (d) == VAR_DECL
16712 && !DECL_INITIAL (d)
16713 && DECL_INITIAL (code_pattern))
16718 ns = decl_namespace_context (d);
16719 push_nested_namespace (ns);
16720 push_nested_class (DECL_CONTEXT (d));
16721 init = tsubst_expr (DECL_INITIAL (code_pattern),
16723 tf_warning_or_error, NULL_TREE,
16724 /*integral_constant_expression_p=*/false);
16725 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
16726 /*asmspec_tree=*/NULL_TREE,
16727 LOOKUP_ONLYCONVERTING);
16728 pop_nested_class ();
16729 pop_nested_namespace (ns);
16732 /* We restore the source position here because it's used by
16733 add_pending_template. */
16734 input_location = saved_loc;
16736 if (at_eof && !pattern_defined
16737 && DECL_EXPLICIT_INSTANTIATION (d)
16738 && DECL_NOT_REALLY_EXTERN (d))
16741 The definition of a non-exported function template, a
16742 non-exported member function template, or a non-exported
16743 member function or static data member of a class template
16744 shall be present in every translation unit in which it is
16745 explicitly instantiated. */
16746 permerror (input_location, "explicit instantiation of %qD "
16747 "but no definition available", d);
16749 /* ??? Historically, we have instantiated inline functions, even
16750 when marked as "extern template". */
16751 if (!(external_p && TREE_CODE (d) == VAR_DECL))
16752 add_pending_template (d);
16755 /* Tell the repository that D is available in this translation unit
16756 -- and see if it is supposed to be instantiated here. */
16757 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
16759 /* In a PCH file, despite the fact that the repository hasn't
16760 requested instantiation in the PCH it is still possible that
16761 an instantiation will be required in a file that includes the
16764 add_pending_template (d);
16765 /* Instantiate inline functions so that the inliner can do its
16766 job, even though we'll not be emitting a copy of this
16768 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
16772 need_push = !cfun || !global_bindings_p ();
16774 push_to_top_level ();
16776 /* Mark D as instantiated so that recursive calls to
16777 instantiate_decl do not try to instantiate it again. */
16778 DECL_TEMPLATE_INSTANTIATED (d) = 1;
16780 /* Regenerate the declaration in case the template has been modified
16781 by a subsequent redeclaration. */
16782 regenerate_decl_from_template (d, td);
16784 /* We already set the file and line above. Reset them now in case
16785 they changed as a result of calling regenerate_decl_from_template. */
16786 input_location = DECL_SOURCE_LOCATION (d);
16788 if (TREE_CODE (d) == VAR_DECL)
16792 /* Clear out DECL_RTL; whatever was there before may not be right
16793 since we've reset the type of the declaration. */
16794 SET_DECL_RTL (d, NULL_RTX);
16795 DECL_IN_AGGR_P (d) = 0;
16797 /* The initializer is placed in DECL_INITIAL by
16798 regenerate_decl_from_template. Pull it out so that
16799 cp_finish_decl can process it. */
16800 init = DECL_INITIAL (d);
16801 DECL_INITIAL (d) = NULL_TREE;
16802 DECL_INITIALIZED_P (d) = 0;
16804 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
16805 initializer. That function will defer actual emission until
16806 we have a chance to determine linkage. */
16807 DECL_EXTERNAL (d) = 0;
16809 /* Enter the scope of D so that access-checking works correctly. */
16810 push_nested_class (DECL_CONTEXT (d));
16811 cp_finish_decl (d, init, false, NULL_TREE, 0);
16812 pop_nested_class ();
16814 else if (TREE_CODE (d) == FUNCTION_DECL)
16816 htab_t saved_local_specializations;
16821 /* Save away the current list, in case we are instantiating one
16822 template from within the body of another. */
16823 saved_local_specializations = local_specializations;
16825 /* Set up the list of local specializations. */
16826 local_specializations = htab_create (37,
16827 hash_local_specialization,
16828 eq_local_specializations,
16831 /* Set up context. */
16832 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
16834 /* Create substitution entries for the parameters. */
16835 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
16836 tmpl_parm = DECL_ARGUMENTS (subst_decl);
16837 spec_parm = DECL_ARGUMENTS (d);
16838 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
16840 register_local_specialization (spec_parm, tmpl_parm);
16841 spec_parm = skip_artificial_parms_for (d, spec_parm);
16842 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
16844 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
16846 register_local_specialization (spec_parm, tmpl_parm);
16847 tmpl_parm = TREE_CHAIN (tmpl_parm);
16848 spec_parm = TREE_CHAIN (spec_parm);
16850 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
16852 /* Register the (value) argument pack as a specialization of
16853 TMPL_PARM, then move on. */
16854 tree argpack = make_fnparm_pack (spec_parm);
16855 register_local_specialization (argpack, tmpl_parm);
16856 tmpl_parm = TREE_CHAIN (tmpl_parm);
16857 spec_parm = NULL_TREE;
16859 gcc_assert (!spec_parm);
16861 /* Substitute into the body of the function. */
16862 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
16863 tf_warning_or_error, tmpl,
16864 /*integral_constant_expression_p=*/false);
16866 /* Set the current input_location to the end of the function
16867 so that finish_function knows where we are. */
16868 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
16870 /* We don't need the local specializations any more. */
16871 htab_delete (local_specializations);
16872 local_specializations = saved_local_specializations;
16874 /* Finish the function. */
16875 d = finish_function (0);
16876 expand_or_defer_fn (d);
16879 /* We're not deferring instantiation any more. */
16880 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
16883 pop_from_top_level ();
16886 input_location = saved_loc;
16887 pop_deferring_access_checks ();
16888 pop_tinst_level ();
16890 timevar_pop (TV_PARSE);
16895 /* Run through the list of templates that we wish we could
16896 instantiate, and instantiate any we can. RETRIES is the
16897 number of times we retry pending template instantiation. */
16900 instantiate_pending_templates (int retries)
16903 location_t saved_loc = input_location;
16905 /* Instantiating templates may trigger vtable generation. This in turn
16906 may require further template instantiations. We place a limit here
16907 to avoid infinite loop. */
16908 if (pending_templates && retries >= max_tinst_depth)
16910 tree decl = pending_templates->tinst->decl;
16912 error ("template instantiation depth exceeds maximum of %d"
16913 " instantiating %q+D, possibly from virtual table generation"
16914 " (use -ftemplate-depth= to increase the maximum)",
16915 max_tinst_depth, decl);
16916 if (TREE_CODE (decl) == FUNCTION_DECL)
16917 /* Pretend that we defined it. */
16918 DECL_INITIAL (decl) = error_mark_node;
16924 struct pending_template **t = &pending_templates;
16925 struct pending_template *last = NULL;
16929 tree instantiation = reopen_tinst_level ((*t)->tinst);
16930 bool complete = false;
16932 if (TYPE_P (instantiation))
16936 if (!COMPLETE_TYPE_P (instantiation))
16938 instantiate_class_template (instantiation);
16939 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
16940 for (fn = TYPE_METHODS (instantiation);
16942 fn = TREE_CHAIN (fn))
16943 if (! DECL_ARTIFICIAL (fn))
16944 instantiate_decl (fn,
16946 /*expl_inst_class_mem_p=*/false);
16947 if (COMPLETE_TYPE_P (instantiation))
16951 complete = COMPLETE_TYPE_P (instantiation);
16955 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
16956 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
16959 = instantiate_decl (instantiation,
16961 /*expl_inst_class_mem_p=*/false);
16962 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
16966 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
16967 || DECL_TEMPLATE_INSTANTIATED (instantiation));
16971 /* If INSTANTIATION has been instantiated, then we don't
16972 need to consider it again in the future. */
16980 current_tinst_level = NULL;
16982 last_pending_template = last;
16984 while (reconsider);
16986 input_location = saved_loc;
16989 /* Substitute ARGVEC into T, which is a list of initializers for
16990 either base class or a non-static data member. The TREE_PURPOSEs
16991 are DECLs, and the TREE_VALUEs are the initializer values. Used by
16992 instantiate_decl. */
16995 tsubst_initializer_list (tree t, tree argvec)
16997 tree inits = NULL_TREE;
16999 for (; t; t = TREE_CHAIN (t))
17003 tree expanded_bases = NULL_TREE;
17004 tree expanded_arguments = NULL_TREE;
17007 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
17012 /* Expand the base class expansion type into separate base
17014 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
17015 tf_warning_or_error,
17017 if (expanded_bases == error_mark_node)
17020 /* We'll be building separate TREE_LISTs of arguments for
17022 len = TREE_VEC_LENGTH (expanded_bases);
17023 expanded_arguments = make_tree_vec (len);
17024 for (i = 0; i < len; i++)
17025 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
17027 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
17028 expand each argument in the TREE_VALUE of t. */
17029 expr = make_node (EXPR_PACK_EXPANSION);
17030 PACK_EXPANSION_PARAMETER_PACKS (expr) =
17031 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
17033 if (TREE_VALUE (t) == void_type_node)
17034 /* VOID_TYPE_NODE is used to indicate
17035 value-initialization. */
17037 for (i = 0; i < len; i++)
17038 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
17042 /* Substitute parameter packs into each argument in the
17044 in_base_initializer = 1;
17045 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
17047 tree expanded_exprs;
17049 /* Expand the argument. */
17050 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
17052 = tsubst_pack_expansion (expr, argvec,
17053 tf_warning_or_error,
17055 if (expanded_exprs == error_mark_node)
17058 /* Prepend each of the expanded expressions to the
17059 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
17060 for (i = 0; i < len; i++)
17062 TREE_VEC_ELT (expanded_arguments, i) =
17063 tree_cons (NULL_TREE,
17064 TREE_VEC_ELT (expanded_exprs, i),
17065 TREE_VEC_ELT (expanded_arguments, i));
17068 in_base_initializer = 0;
17070 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
17071 since we built them backwards. */
17072 for (i = 0; i < len; i++)
17074 TREE_VEC_ELT (expanded_arguments, i) =
17075 nreverse (TREE_VEC_ELT (expanded_arguments, i));
17080 for (i = 0; i < len; ++i)
17082 if (expanded_bases)
17084 decl = TREE_VEC_ELT (expanded_bases, i);
17085 decl = expand_member_init (decl);
17086 init = TREE_VEC_ELT (expanded_arguments, i);
17090 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
17091 tf_warning_or_error, NULL_TREE);
17093 decl = expand_member_init (decl);
17094 if (decl && !DECL_P (decl))
17095 in_base_initializer = 1;
17097 init = tsubst_expr (TREE_VALUE (t), argvec,
17098 tf_warning_or_error, NULL_TREE,
17099 /*integral_constant_expression_p=*/false);
17100 in_base_initializer = 0;
17105 init = build_tree_list (decl, init);
17106 TREE_CHAIN (init) = inits;
17114 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
17117 set_current_access_from_decl (tree decl)
17119 if (TREE_PRIVATE (decl))
17120 current_access_specifier = access_private_node;
17121 else if (TREE_PROTECTED (decl))
17122 current_access_specifier = access_protected_node;
17124 current_access_specifier = access_public_node;
17127 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
17128 is the instantiation (which should have been created with
17129 start_enum) and ARGS are the template arguments to use. */
17132 tsubst_enum (tree tag, tree newtag, tree args)
17136 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
17141 decl = TREE_VALUE (e);
17142 /* Note that in a template enum, the TREE_VALUE is the
17143 CONST_DECL, not the corresponding INTEGER_CST. */
17144 value = tsubst_expr (DECL_INITIAL (decl),
17145 args, tf_warning_or_error, NULL_TREE,
17146 /*integral_constant_expression_p=*/true);
17148 /* Give this enumeration constant the correct access. */
17149 set_current_access_from_decl (decl);
17151 /* Actually build the enumerator itself. */
17152 build_enumerator (DECL_NAME (decl), value, newtag);
17155 finish_enum (newtag);
17156 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
17157 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
17160 /* DECL is a FUNCTION_DECL that is a template specialization. Return
17161 its type -- but without substituting the innermost set of template
17162 arguments. So, innermost set of template parameters will appear in
17166 get_mostly_instantiated_function_type (tree decl)
17174 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
17175 targs = DECL_TI_ARGS (decl);
17176 tparms = DECL_TEMPLATE_PARMS (tmpl);
17177 parm_depth = TMPL_PARMS_DEPTH (tparms);
17179 /* There should be as many levels of arguments as there are levels
17181 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
17183 fn_type = TREE_TYPE (tmpl);
17185 if (parm_depth == 1)
17186 /* No substitution is necessary. */
17190 int i, save_access_control;
17193 /* Replace the innermost level of the TARGS with NULL_TREEs to
17194 let tsubst know not to substitute for those parameters. */
17195 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
17196 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
17197 SET_TMPL_ARGS_LEVEL (partial_args, i,
17198 TMPL_ARGS_LEVEL (targs, i));
17199 SET_TMPL_ARGS_LEVEL (partial_args,
17200 TMPL_ARGS_DEPTH (targs),
17201 make_tree_vec (DECL_NTPARMS (tmpl)));
17203 /* Disable access control as this function is used only during
17205 save_access_control = flag_access_control;
17206 flag_access_control = 0;
17208 ++processing_template_decl;
17209 /* Now, do the (partial) substitution to figure out the
17210 appropriate function type. */
17211 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
17212 --processing_template_decl;
17214 /* Substitute into the template parameters to obtain the real
17215 innermost set of parameters. This step is important if the
17216 innermost set of template parameters contains value
17217 parameters whose types depend on outer template parameters. */
17218 TREE_VEC_LENGTH (partial_args)--;
17219 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
17221 flag_access_control = save_access_control;
17227 /* Return truthvalue if we're processing a template different from
17228 the last one involved in diagnostics. */
17230 problematic_instantiation_changed (void)
17232 return last_template_error_tick != tinst_level_tick;
17235 /* Remember current template involved in diagnostics. */
17237 record_last_problematic_instantiation (void)
17239 last_template_error_tick = tinst_level_tick;
17242 struct tinst_level *
17243 current_instantiation (void)
17245 return current_tinst_level;
17248 /* [temp.param] Check that template non-type parm TYPE is of an allowable
17249 type. Return zero for ok, nonzero for disallowed. Issue error and
17250 warning messages under control of COMPLAIN. */
17253 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
17255 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
17257 else if (POINTER_TYPE_P (type))
17259 else if (TYPE_PTR_TO_MEMBER_P (type))
17261 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
17263 else if (TREE_CODE (type) == TYPENAME_TYPE)
17266 if (complain & tf_error)
17267 error ("%q#T is not a valid type for a template constant parameter", type);
17271 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
17272 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
17275 dependent_type_p_r (tree type)
17281 A type is dependent if it is:
17283 -- a template parameter. Template template parameters are types
17284 for us (since TYPE_P holds true for them) so we handle
17286 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17287 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
17289 /* -- a qualified-id with a nested-name-specifier which contains a
17290 class-name that names a dependent type or whose unqualified-id
17291 names a dependent type. */
17292 if (TREE_CODE (type) == TYPENAME_TYPE)
17294 /* -- a cv-qualified type where the cv-unqualified type is
17296 type = TYPE_MAIN_VARIANT (type);
17297 /* -- a compound type constructed from any dependent type. */
17298 if (TYPE_PTR_TO_MEMBER_P (type))
17299 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
17300 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
17302 else if (TREE_CODE (type) == POINTER_TYPE
17303 || TREE_CODE (type) == REFERENCE_TYPE)
17304 return dependent_type_p (TREE_TYPE (type));
17305 else if (TREE_CODE (type) == FUNCTION_TYPE
17306 || TREE_CODE (type) == METHOD_TYPE)
17310 if (dependent_type_p (TREE_TYPE (type)))
17312 for (arg_type = TYPE_ARG_TYPES (type);
17314 arg_type = TREE_CHAIN (arg_type))
17315 if (dependent_type_p (TREE_VALUE (arg_type)))
17319 /* -- an array type constructed from any dependent type or whose
17320 size is specified by a constant expression that is
17321 value-dependent. */
17322 if (TREE_CODE (type) == ARRAY_TYPE)
17324 if (TYPE_DOMAIN (type)
17325 && dependent_type_p (TYPE_DOMAIN (type)))
17327 return dependent_type_p (TREE_TYPE (type));
17329 else if (TREE_CODE (type) == INTEGER_TYPE
17330 && !TREE_CONSTANT (TYPE_MAX_VALUE (type)))
17332 /* If this is the TYPE_DOMAIN of an array type, consider it
17333 dependent. We already checked for value-dependence in
17334 compute_array_index_type. */
17335 return type_dependent_expression_p (TYPE_MAX_VALUE (type));
17338 /* -- a template-id in which either the template name is a template
17340 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
17342 /* ... or any of the template arguments is a dependent type or
17343 an expression that is type-dependent or value-dependent. */
17344 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
17345 && (any_dependent_template_arguments_p
17346 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
17349 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
17350 argument of the `typeof' expression is not type-dependent, then
17351 it should already been have resolved. */
17352 if (TREE_CODE (type) == TYPEOF_TYPE
17353 || TREE_CODE (type) == DECLTYPE_TYPE)
17356 /* A template argument pack is dependent if any of its packed
17358 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
17360 tree args = ARGUMENT_PACK_ARGS (type);
17361 int i, len = TREE_VEC_LENGTH (args);
17362 for (i = 0; i < len; ++i)
17363 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17367 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
17368 be template parameters. */
17369 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
17372 /* The standard does not specifically mention types that are local
17373 to template functions or local classes, but they should be
17374 considered dependent too. For example:
17376 template <int I> void f() {
17381 The size of `E' cannot be known until the value of `I' has been
17382 determined. Therefore, `E' must be considered dependent. */
17383 scope = TYPE_CONTEXT (type);
17384 if (scope && TYPE_P (scope))
17385 return dependent_type_p (scope);
17386 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
17387 return type_dependent_expression_p (scope);
17389 /* Other types are non-dependent. */
17393 /* Returns TRUE if TYPE is dependent, in the sense of
17394 [temp.dep.type]. */
17397 dependent_type_p (tree type)
17399 /* If there are no template parameters in scope, then there can't be
17400 any dependent types. */
17401 if (!processing_template_decl)
17403 /* If we are not processing a template, then nobody should be
17404 providing us with a dependent type. */
17406 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
17410 /* If the type is NULL, we have not computed a type for the entity
17411 in question; in that case, the type is dependent. */
17415 /* Erroneous types can be considered non-dependent. */
17416 if (type == error_mark_node)
17419 /* If we have not already computed the appropriate value for TYPE,
17421 if (!TYPE_DEPENDENT_P_VALID (type))
17423 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
17424 TYPE_DEPENDENT_P_VALID (type) = 1;
17427 return TYPE_DEPENDENT_P (type);
17430 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
17431 lookup. In other words, a dependent type that is not the current
17435 dependent_scope_p (tree scope)
17437 return (scope && TYPE_P (scope) && dependent_type_p (scope)
17438 && !currently_open_class (scope));
17441 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
17444 dependent_scope_ref_p (tree expression, bool criterion (tree))
17449 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
17451 if (!TYPE_P (TREE_OPERAND (expression, 0)))
17454 scope = TREE_OPERAND (expression, 0);
17455 name = TREE_OPERAND (expression, 1);
17459 An id-expression is type-dependent if it contains a
17460 nested-name-specifier that contains a class-name that names a
17462 /* The suggested resolution to Core Issue 224 implies that if the
17463 qualifying type is the current class, then we must peek
17466 && currently_open_class (scope)
17467 && !criterion (name))
17469 if (dependent_type_p (scope))
17475 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
17476 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
17480 value_dependent_expression_p (tree expression)
17482 if (!processing_template_decl)
17485 /* A name declared with a dependent type. */
17486 if (DECL_P (expression) && type_dependent_expression_p (expression))
17489 switch (TREE_CODE (expression))
17491 case IDENTIFIER_NODE:
17492 /* A name that has not been looked up -- must be dependent. */
17495 case TEMPLATE_PARM_INDEX:
17496 /* A non-type template parm. */
17500 /* A non-type template parm. */
17501 if (DECL_TEMPLATE_PARM_P (expression))
17503 return value_dependent_expression_p (DECL_INITIAL (expression));
17506 /* A constant with integral or enumeration type and is initialized
17507 with an expression that is value-dependent. */
17508 if (DECL_INITIAL (expression)
17509 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
17510 && value_dependent_expression_p (DECL_INITIAL (expression)))
17514 case DYNAMIC_CAST_EXPR:
17515 case STATIC_CAST_EXPR:
17516 case CONST_CAST_EXPR:
17517 case REINTERPRET_CAST_EXPR:
17519 /* These expressions are value-dependent if the type to which
17520 the cast occurs is dependent or the expression being casted
17521 is value-dependent. */
17523 tree type = TREE_TYPE (expression);
17525 if (dependent_type_p (type))
17528 /* A functional cast has a list of operands. */
17529 expression = TREE_OPERAND (expression, 0);
17532 /* If there are no operands, it must be an expression such
17533 as "int()". This should not happen for aggregate types
17534 because it would form non-constant expressions. */
17535 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
17540 if (TREE_CODE (expression) == TREE_LIST)
17541 return any_value_dependent_elements_p (expression);
17543 return value_dependent_expression_p (expression);
17548 /* A `sizeof' expression is value-dependent if the operand is
17549 type-dependent or is a pack expansion. */
17550 expression = TREE_OPERAND (expression, 0);
17551 if (PACK_EXPANSION_P (expression))
17553 else if (TYPE_P (expression))
17554 return dependent_type_p (expression);
17555 return type_dependent_expression_p (expression);
17558 return dependent_scope_ref_p (expression, value_dependent_expression_p);
17560 case COMPONENT_REF:
17561 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
17562 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
17565 /* A CALL_EXPR may appear in a constant expression if it is a
17566 call to a builtin function, e.g., __builtin_constant_p. All
17567 such calls are value-dependent. */
17570 case NONTYPE_ARGUMENT_PACK:
17571 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
17572 is value-dependent. */
17574 tree values = ARGUMENT_PACK_ARGS (expression);
17575 int i, len = TREE_VEC_LENGTH (values);
17577 for (i = 0; i < len; ++i)
17578 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
17586 tree type2 = TRAIT_EXPR_TYPE2 (expression);
17587 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
17588 || (type2 ? dependent_type_p (type2) : false));
17592 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
17593 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
17596 /* A constant expression is value-dependent if any subexpression is
17597 value-dependent. */
17598 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
17600 case tcc_reference:
17602 return (value_dependent_expression_p
17603 (TREE_OPERAND (expression, 0)));
17605 case tcc_comparison:
17607 return ((value_dependent_expression_p
17608 (TREE_OPERAND (expression, 0)))
17609 || (value_dependent_expression_p
17610 (TREE_OPERAND (expression, 1))));
17612 case tcc_expression:
17616 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
17617 /* In some cases, some of the operands may be missing.
17618 (For example, in the case of PREDECREMENT_EXPR, the
17619 amount to increment by may be missing.) That doesn't
17620 make the expression dependent. */
17621 if (TREE_OPERAND (expression, i)
17622 && (value_dependent_expression_p
17623 (TREE_OPERAND (expression, i))))
17633 /* The expression is not value-dependent. */
17637 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
17638 [temp.dep.expr]. */
17641 type_dependent_expression_p (tree expression)
17643 if (!processing_template_decl)
17646 if (expression == error_mark_node)
17649 /* An unresolved name is always dependent. */
17650 if (TREE_CODE (expression) == IDENTIFIER_NODE
17651 || TREE_CODE (expression) == USING_DECL)
17654 /* Some expression forms are never type-dependent. */
17655 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
17656 || TREE_CODE (expression) == SIZEOF_EXPR
17657 || TREE_CODE (expression) == ALIGNOF_EXPR
17658 || TREE_CODE (expression) == TRAIT_EXPR
17659 || TREE_CODE (expression) == TYPEID_EXPR
17660 || TREE_CODE (expression) == DELETE_EXPR
17661 || TREE_CODE (expression) == VEC_DELETE_EXPR
17662 || TREE_CODE (expression) == THROW_EXPR)
17665 /* The types of these expressions depends only on the type to which
17666 the cast occurs. */
17667 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
17668 || TREE_CODE (expression) == STATIC_CAST_EXPR
17669 || TREE_CODE (expression) == CONST_CAST_EXPR
17670 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
17671 || TREE_CODE (expression) == CAST_EXPR)
17672 return dependent_type_p (TREE_TYPE (expression));
17674 /* The types of these expressions depends only on the type created
17675 by the expression. */
17676 if (TREE_CODE (expression) == NEW_EXPR
17677 || TREE_CODE (expression) == VEC_NEW_EXPR)
17679 /* For NEW_EXPR tree nodes created inside a template, either
17680 the object type itself or a TREE_LIST may appear as the
17682 tree type = TREE_OPERAND (expression, 1);
17683 if (TREE_CODE (type) == TREE_LIST)
17684 /* This is an array type. We need to check array dimensions
17686 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
17687 || value_dependent_expression_p
17688 (TREE_OPERAND (TREE_VALUE (type), 1));
17690 return dependent_type_p (type);
17693 if (TREE_CODE (expression) == SCOPE_REF
17694 && dependent_scope_ref_p (expression,
17695 type_dependent_expression_p))
17698 if (TREE_CODE (expression) == FUNCTION_DECL
17699 && DECL_LANG_SPECIFIC (expression)
17700 && DECL_TEMPLATE_INFO (expression)
17701 && (any_dependent_template_arguments_p
17702 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
17705 if (TREE_CODE (expression) == TEMPLATE_DECL
17706 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
17709 if (TREE_CODE (expression) == STMT_EXPR)
17710 expression = stmt_expr_value_expr (expression);
17712 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
17717 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
17719 if (type_dependent_expression_p (elt))
17725 if (TREE_TYPE (expression) == unknown_type_node)
17727 if (TREE_CODE (expression) == ADDR_EXPR)
17728 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
17729 if (TREE_CODE (expression) == COMPONENT_REF
17730 || TREE_CODE (expression) == OFFSET_REF)
17732 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
17734 expression = TREE_OPERAND (expression, 1);
17735 if (TREE_CODE (expression) == IDENTIFIER_NODE)
17738 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
17739 if (TREE_CODE (expression) == SCOPE_REF)
17742 if (TREE_CODE (expression) == BASELINK)
17743 expression = BASELINK_FUNCTIONS (expression);
17745 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
17747 if (any_dependent_template_arguments_p
17748 (TREE_OPERAND (expression, 1)))
17750 expression = TREE_OPERAND (expression, 0);
17752 gcc_assert (TREE_CODE (expression) == OVERLOAD
17753 || TREE_CODE (expression) == FUNCTION_DECL);
17757 if (type_dependent_expression_p (OVL_CURRENT (expression)))
17759 expression = OVL_NEXT (expression);
17764 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
17766 return (dependent_type_p (TREE_TYPE (expression)));
17769 /* Like type_dependent_expression_p, but it also works while not processing
17770 a template definition, i.e. during substitution or mangling. */
17773 type_dependent_expression_p_push (tree expr)
17776 ++processing_template_decl;
17777 b = type_dependent_expression_p (expr);
17778 --processing_template_decl;
17782 /* Returns TRUE if ARGS contains a type-dependent expression. */
17785 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
17790 for (i = 0; VEC_iterate (tree, args, i, arg); ++i)
17792 if (type_dependent_expression_p (arg))
17798 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
17799 expressions) contains any value-dependent expressions. */
17802 any_value_dependent_elements_p (const_tree list)
17804 for (; list; list = TREE_CHAIN (list))
17805 if (value_dependent_expression_p (TREE_VALUE (list)))
17811 /* Returns TRUE if the ARG (a template argument) is dependent. */
17814 dependent_template_arg_p (tree arg)
17816 if (!processing_template_decl)
17819 if (TREE_CODE (arg) == TEMPLATE_DECL
17820 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17821 return dependent_template_p (arg);
17822 else if (ARGUMENT_PACK_P (arg))
17824 tree args = ARGUMENT_PACK_ARGS (arg);
17825 int i, len = TREE_VEC_LENGTH (args);
17826 for (i = 0; i < len; ++i)
17828 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17834 else if (TYPE_P (arg))
17835 return dependent_type_p (arg);
17837 return (type_dependent_expression_p (arg)
17838 || value_dependent_expression_p (arg));
17841 /* Returns true if ARGS (a collection of template arguments) contains
17842 any types that require structural equality testing. */
17845 any_template_arguments_need_structural_equality_p (tree args)
17852 if (args == error_mark_node)
17855 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17857 tree level = TMPL_ARGS_LEVEL (args, i + 1);
17858 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17860 tree arg = TREE_VEC_ELT (level, j);
17861 tree packed_args = NULL_TREE;
17864 if (ARGUMENT_PACK_P (arg))
17866 /* Look inside the argument pack. */
17867 packed_args = ARGUMENT_PACK_ARGS (arg);
17868 len = TREE_VEC_LENGTH (packed_args);
17871 for (k = 0; k < len; ++k)
17874 arg = TREE_VEC_ELT (packed_args, k);
17876 if (error_operand_p (arg))
17878 else if (TREE_CODE (arg) == TEMPLATE_DECL
17879 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17881 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
17883 else if (!TYPE_P (arg) && TREE_TYPE (arg)
17884 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
17893 /* Returns true if ARGS (a collection of template arguments) contains
17894 any dependent arguments. */
17897 any_dependent_template_arguments_p (const_tree args)
17904 if (args == error_mark_node)
17907 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17909 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
17910 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17911 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
17918 /* Returns TRUE if the template TMPL is dependent. */
17921 dependent_template_p (tree tmpl)
17923 if (TREE_CODE (tmpl) == OVERLOAD)
17927 if (dependent_template_p (OVL_FUNCTION (tmpl)))
17929 tmpl = OVL_CHAIN (tmpl);
17934 /* Template template parameters are dependent. */
17935 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
17936 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
17938 /* So are names that have not been looked up. */
17939 if (TREE_CODE (tmpl) == SCOPE_REF
17940 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
17942 /* So are member templates of dependent classes. */
17943 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
17944 return dependent_type_p (DECL_CONTEXT (tmpl));
17948 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
17951 dependent_template_id_p (tree tmpl, tree args)
17953 return (dependent_template_p (tmpl)
17954 || any_dependent_template_arguments_p (args));
17957 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
17961 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
17965 if (!processing_template_decl)
17968 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
17970 tree decl = TREE_VEC_ELT (declv, i);
17971 tree init = TREE_VEC_ELT (initv, i);
17972 tree cond = TREE_VEC_ELT (condv, i);
17973 tree incr = TREE_VEC_ELT (incrv, i);
17975 if (type_dependent_expression_p (decl))
17978 if (init && type_dependent_expression_p (init))
17981 if (type_dependent_expression_p (cond))
17984 if (COMPARISON_CLASS_P (cond)
17985 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
17986 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
17989 if (TREE_CODE (incr) == MODOP_EXPR)
17991 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
17992 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
17995 else if (type_dependent_expression_p (incr))
17997 else if (TREE_CODE (incr) == MODIFY_EXPR)
17999 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
18001 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
18003 tree t = TREE_OPERAND (incr, 1);
18004 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
18005 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
18014 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
18015 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
18016 no such TYPE can be found. Note that this function peers inside
18017 uninstantiated templates and therefore should be used only in
18018 extremely limited situations. ONLY_CURRENT_P restricts this
18019 peering to the currently open classes hierarchy (which is required
18020 when comparing types). */
18023 resolve_typename_type (tree type, bool only_current_p)
18032 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
18034 scope = TYPE_CONTEXT (type);
18035 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
18036 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
18037 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
18038 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
18039 identifier of the TYPENAME_TYPE anymore.
18040 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
18041 TYPENAME_TYPE instead, we avoid messing up with a possible
18042 typedef variant case. */
18043 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
18045 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
18046 it first before we can figure out what NAME refers to. */
18047 if (TREE_CODE (scope) == TYPENAME_TYPE)
18048 scope = resolve_typename_type (scope, only_current_p);
18049 /* If we don't know what SCOPE refers to, then we cannot resolve the
18051 if (TREE_CODE (scope) == TYPENAME_TYPE)
18053 /* If the SCOPE is a template type parameter, we have no way of
18054 resolving the name. */
18055 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
18057 /* If the SCOPE is not the current instantiation, there's no reason
18058 to look inside it. */
18059 if (only_current_p && !currently_open_class (scope))
18061 /* If this is a typedef, we don't want to look inside (c++/11987). */
18062 if (typedef_variant_p (type))
18064 /* If SCOPE isn't the template itself, it will not have a valid
18065 TYPE_FIELDS list. */
18066 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
18067 /* scope is either the template itself or a compatible instantiation
18068 like X<T>, so look up the name in the original template. */
18069 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
18071 /* scope is a partial instantiation, so we can't do the lookup or we
18072 will lose the template arguments. */
18074 /* Enter the SCOPE so that name lookup will be resolved as if we
18075 were in the class definition. In particular, SCOPE will no
18076 longer be considered a dependent type. */
18077 pushed_scope = push_scope (scope);
18078 /* Look up the declaration. */
18079 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
18081 result = NULL_TREE;
18083 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
18084 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
18087 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
18088 && TREE_CODE (decl) == TYPE_DECL)
18090 result = TREE_TYPE (decl);
18091 if (result == error_mark_node)
18092 result = NULL_TREE;
18094 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
18095 && DECL_CLASS_TEMPLATE_P (decl))
18099 /* Obtain the template and the arguments. */
18100 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
18101 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
18102 /* Instantiate the template. */
18103 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
18104 /*entering_scope=*/0,
18105 tf_error | tf_user);
18106 if (result == error_mark_node)
18107 result = NULL_TREE;
18110 /* Leave the SCOPE. */
18112 pop_scope (pushed_scope);
18114 /* If we failed to resolve it, return the original typename. */
18118 /* If lookup found a typename type, resolve that too. */
18119 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
18121 /* Ill-formed programs can cause infinite recursion here, so we
18122 must catch that. */
18123 TYPENAME_IS_RESOLVING_P (type) = 1;
18124 result = resolve_typename_type (result, only_current_p);
18125 TYPENAME_IS_RESOLVING_P (type) = 0;
18128 /* Qualify the resulting type. */
18129 quals = cp_type_quals (type);
18131 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
18136 /* EXPR is an expression which is not type-dependent. Return a proxy
18137 for EXPR that can be used to compute the types of larger
18138 expressions containing EXPR. */
18141 build_non_dependent_expr (tree expr)
18145 /* Preserve null pointer constants so that the type of things like
18146 "p == 0" where "p" is a pointer can be determined. */
18147 if (null_ptr_cst_p (expr))
18149 /* Preserve OVERLOADs; the functions must be available to resolve
18152 if (TREE_CODE (inner_expr) == STMT_EXPR)
18153 inner_expr = stmt_expr_value_expr (inner_expr);
18154 if (TREE_CODE (inner_expr) == ADDR_EXPR)
18155 inner_expr = TREE_OPERAND (inner_expr, 0);
18156 if (TREE_CODE (inner_expr) == COMPONENT_REF)
18157 inner_expr = TREE_OPERAND (inner_expr, 1);
18158 if (is_overloaded_fn (inner_expr)
18159 || TREE_CODE (inner_expr) == OFFSET_REF)
18161 /* There is no need to return a proxy for a variable. */
18162 if (TREE_CODE (expr) == VAR_DECL)
18164 /* Preserve string constants; conversions from string constants to
18165 "char *" are allowed, even though normally a "const char *"
18166 cannot be used to initialize a "char *". */
18167 if (TREE_CODE (expr) == STRING_CST)
18169 /* Preserve arithmetic constants, as an optimization -- there is no
18170 reason to create a new node. */
18171 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
18173 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
18174 There is at least one place where we want to know that a
18175 particular expression is a throw-expression: when checking a ?:
18176 expression, there are special rules if the second or third
18177 argument is a throw-expression. */
18178 if (TREE_CODE (expr) == THROW_EXPR)
18181 if (TREE_CODE (expr) == COND_EXPR)
18182 return build3 (COND_EXPR,
18184 TREE_OPERAND (expr, 0),
18185 (TREE_OPERAND (expr, 1)
18186 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
18187 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
18188 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
18189 if (TREE_CODE (expr) == COMPOUND_EXPR
18190 && !COMPOUND_EXPR_OVERLOADED (expr))
18191 return build2 (COMPOUND_EXPR,
18193 TREE_OPERAND (expr, 0),
18194 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
18196 /* If the type is unknown, it can't really be non-dependent */
18197 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
18199 /* Otherwise, build a NON_DEPENDENT_EXPR.
18201 REFERENCE_TYPEs are not stripped for expressions in templates
18202 because doing so would play havoc with mangling. Consider, for
18205 template <typename T> void f<T& g>() { g(); }
18207 In the body of "f", the expression for "g" will have
18208 REFERENCE_TYPE, even though the standard says that it should
18209 not. The reason is that we must preserve the syntactic form of
18210 the expression so that mangling (say) "f<g>" inside the body of
18211 "f" works out correctly. Therefore, the REFERENCE_TYPE is
18213 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
18216 /* ARGS is a vector of expressions as arguments to a function call.
18217 Replace the arguments with equivalent non-dependent expressions.
18218 This modifies ARGS in place. */
18221 make_args_non_dependent (VEC(tree,gc) *args)
18226 for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix)
18228 tree newarg = build_non_dependent_expr (arg);
18230 VEC_replace (tree, args, ix, newarg);
18234 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
18235 with a level one deeper than the actual template parms. */
18240 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
18241 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
18242 TYPE_DECL, get_identifier ("auto"), au);
18243 TYPE_STUB_DECL (au) = TYPE_NAME (au);
18244 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
18245 (0, processing_template_decl + 1, processing_template_decl + 1,
18246 TYPE_NAME (au), NULL_TREE);
18247 TYPE_CANONICAL (au) = canonical_type_parameter (au);
18248 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
18249 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
18254 /* Given type ARG, return std::initializer_list<ARG>. */
18259 tree std_init_list = namespace_binding
18260 (get_identifier ("initializer_list"), std_node);
18262 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
18264 error ("deducing from brace-enclosed initializer list requires "
18265 "#include <initializer_list>");
18266 return error_mark_node;
18268 argvec = make_tree_vec (1);
18269 TREE_VEC_ELT (argvec, 0) = arg;
18270 return lookup_template_class (std_init_list, argvec, NULL_TREE,
18271 NULL_TREE, 0, tf_warning_or_error);
18274 /* Replace auto in TYPE with std::initializer_list<auto>. */
18277 listify_autos (tree type, tree auto_node)
18279 tree init_auto = listify (auto_node);
18280 tree argvec = make_tree_vec (1);
18281 TREE_VEC_ELT (argvec, 0) = init_auto;
18282 if (processing_template_decl)
18283 argvec = add_to_template_args (current_template_args (), argvec);
18284 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18287 /* walk_tree helper for do_auto_deduction. */
18290 contains_auto_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
18293 /* Is this a variable with the type we're looking for? */
18295 && TREE_TYPE (*tp) == type)
18301 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
18302 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
18305 do_auto_deduction (tree type, tree init, tree auto_node)
18307 tree parms, tparms, targs;
18312 /* The name of the object being declared shall not appear in the
18313 initializer expression. */
18314 decl = cp_walk_tree_without_duplicates (&init, contains_auto_r, type);
18317 error ("variable %q#D with %<auto%> type used in its own "
18318 "initializer", decl);
18319 return error_mark_node;
18322 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
18323 with either a new invented type template parameter U or, if the
18324 initializer is a braced-init-list (8.5.4), with
18325 std::initializer_list<U>. */
18326 if (BRACE_ENCLOSED_INITIALIZER_P (init))
18327 type = listify_autos (type, auto_node);
18329 parms = build_tree_list (NULL_TREE, type);
18331 tparms = make_tree_vec (1);
18332 targs = make_tree_vec (1);
18333 TREE_VEC_ELT (tparms, 0)
18334 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
18335 val = type_unification_real (tparms, targs, parms, args, 1, 0,
18336 DEDUCE_CALL, LOOKUP_NORMAL);
18339 error ("unable to deduce %qT from %qE", type, init);
18340 return error_mark_node;
18343 /* If the list of declarators contains more than one declarator, the type
18344 of each declared variable is determined as described above. If the
18345 type deduced for the template parameter U is not the same in each
18346 deduction, the program is ill-formed. */
18347 if (TREE_TYPE (auto_node)
18348 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
18350 error ("inconsistent deduction for %qT: %qT and then %qT",
18351 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
18352 return error_mark_node;
18354 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
18356 if (processing_template_decl)
18357 targs = add_to_template_args (current_template_args (), targs);
18358 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
18361 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
18365 splice_late_return_type (tree type, tree late_return_type)
18369 if (late_return_type == NULL_TREE)
18371 argvec = make_tree_vec (1);
18372 TREE_VEC_ELT (argvec, 0) = late_return_type;
18373 if (processing_template_decl)
18374 argvec = add_to_template_args (current_template_args (), argvec);
18375 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18378 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
18381 is_auto (const_tree type)
18383 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18384 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
18390 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
18391 appear as a type-specifier for the declaration in question, we don't
18392 have to look through the whole type. */
18395 type_uses_auto (tree type)
18397 enum tree_code code;
18398 if (is_auto (type))
18401 code = TREE_CODE (type);
18403 if (code == POINTER_TYPE || code == REFERENCE_TYPE
18404 || code == OFFSET_TYPE || code == FUNCTION_TYPE
18405 || code == METHOD_TYPE || code == ARRAY_TYPE)
18406 return type_uses_auto (TREE_TYPE (type));
18408 if (TYPE_PTRMEMFUNC_P (type))
18409 return type_uses_auto (TREE_TYPE (TREE_TYPE
18410 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
18415 /* For a given template T, return the vector of typedefs referenced
18416 in T for which access check is needed at T instantiation time.
18417 T is either a FUNCTION_DECL or a RECORD_TYPE.
18418 Those typedefs were added to T by the function
18419 append_type_to_template_for_access_check. */
18421 VEC(qualified_typedef_usage_t,gc)*
18422 get_types_needing_access_check (tree t)
18425 VEC(qualified_typedef_usage_t,gc) *result = NULL;
18427 if (!t || t == error_mark_node)
18430 if (!(ti = get_template_info (t)))
18433 if (CLASS_TYPE_P (t)
18434 || TREE_CODE (t) == FUNCTION_DECL)
18436 if (!TI_TEMPLATE (ti))
18439 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
18445 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
18446 tied to T. That list of typedefs will be access checked at
18447 T instantiation time.
18448 T is either a FUNCTION_DECL or a RECORD_TYPE.
18449 TYPE_DECL is a TYPE_DECL node representing a typedef.
18450 SCOPE is the scope through which TYPE_DECL is accessed.
18451 LOCATION is the location of the usage point of TYPE_DECL.
18453 This function is a subroutine of
18454 append_type_to_template_for_access_check. */
18457 append_type_to_template_for_access_check_1 (tree t,
18460 location_t location)
18462 qualified_typedef_usage_t typedef_usage;
18465 if (!t || t == error_mark_node)
18468 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
18469 || CLASS_TYPE_P (t))
18471 && TREE_CODE (type_decl) == TYPE_DECL
18474 if (!(ti = get_template_info (t)))
18477 gcc_assert (TI_TEMPLATE (ti));
18479 typedef_usage.typedef_decl = type_decl;
18480 typedef_usage.context = scope;
18481 typedef_usage.locus = location;
18483 VEC_safe_push (qualified_typedef_usage_t, gc,
18484 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
18488 /* Append TYPE_DECL to the template TEMPL.
18489 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
18490 At TEMPL instanciation time, TYPE_DECL will be checked to see
18491 if it can be accessed through SCOPE.
18492 LOCATION is the location of the usage point of TYPE_DECL.
18494 e.g. consider the following code snippet:
18501 template<class U> struct S
18503 C::myint mi; // <-- usage point of the typedef C::myint
18508 At S<char> instantiation time, we need to check the access of C::myint
18509 In other words, we need to check the access of the myint typedef through
18510 the C scope. For that purpose, this function will add the myint typedef
18511 and the scope C through which its being accessed to a list of typedefs
18512 tied to the template S. That list will be walked at template instantiation
18513 time and access check performed on each typedefs it contains.
18514 Note that this particular code snippet should yield an error because
18515 myint is private to C. */
18518 append_type_to_template_for_access_check (tree templ,
18521 location_t location)
18523 qualified_typedef_usage_t *iter;
18526 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
18528 /* Make sure we don't append the type to the template twice. */
18530 VEC_iterate (qualified_typedef_usage_t,
18531 get_types_needing_access_check (templ),
18534 if (iter->typedef_decl == type_decl && scope == iter->context)
18537 append_type_to_template_for_access_check_1 (templ, type_decl,
18541 /* Set up the hash tables for template instantiations. */
18544 init_template_processing (void)
18546 decl_specializations = htab_create_ggc (37,
18547 hash_specialization,
18548 eq_specializations,
18550 type_specializations = htab_create_ggc (37,
18551 hash_specialization,
18552 eq_specializations,
18556 #include "gt-cp-pt.h"