1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
31 #include "coretypes.h"
35 #include "pointer-set.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
41 #include "tree-inline.h"
46 #include "tree-iterator.h"
49 /* The type of functions taking a tree, and some additional data, and
51 typedef int (*tree_fn_t) (tree, void*);
53 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
54 instantiations have been deferred, either because their definitions
55 were not yet available, or because we were putting off doing the work. */
56 struct GTY ((chain_next ("%h.next"))) pending_template {
57 struct pending_template *next;
58 struct tinst_level *tinst;
61 static GTY(()) struct pending_template *pending_templates;
62 static GTY(()) struct pending_template *last_pending_template;
64 int processing_template_parmlist;
65 static int template_header_count;
67 static GTY(()) tree saved_trees;
68 static VEC(int,heap) *inline_parm_levels;
70 static GTY(()) struct tinst_level *current_tinst_level;
72 static GTY(()) tree saved_access_scope;
74 /* Live only within one (recursive) call to tsubst_expr. We use
75 this to pass the statement expression node from the STMT_EXPR
76 to the EXPR_STMT that is its result. */
77 static tree cur_stmt_expr;
79 /* A map from local variable declarations in the body of the template
80 presently being instantiated to the corresponding instantiated
82 static htab_t local_specializations;
84 typedef struct GTY(()) spec_entry
91 static GTY ((param_is (spec_entry)))
92 htab_t decl_specializations;
94 static GTY ((param_is (spec_entry)))
95 htab_t type_specializations;
97 /* Contains canonical template parameter types. The vector is indexed by
98 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
99 TREE_LIST, whose TREE_VALUEs contain the canonical template
100 parameters of various types and levels. */
101 static GTY(()) VEC(tree,gc) *canonical_template_parms;
103 #define UNIFY_ALLOW_NONE 0
104 #define UNIFY_ALLOW_MORE_CV_QUAL 1
105 #define UNIFY_ALLOW_LESS_CV_QUAL 2
106 #define UNIFY_ALLOW_DERIVED 4
107 #define UNIFY_ALLOW_INTEGER 8
108 #define UNIFY_ALLOW_OUTER_LEVEL 16
109 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
110 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
112 enum template_base_result {
114 tbr_ambiguous_baseclass,
118 static void push_access_scope (tree);
119 static void pop_access_scope (tree);
120 static void push_deduction_access_scope (tree);
121 static void pop_deduction_access_scope (tree);
122 static bool resolve_overloaded_unification (tree, tree, tree, tree,
123 unification_kind_t, int,
125 static int try_one_overload (tree, tree, tree, tree, tree,
126 unification_kind_t, int, bool, bool);
127 static int unify (tree, tree, tree, tree, int, bool);
128 static void add_pending_template (tree);
129 static tree reopen_tinst_level (struct tinst_level *);
130 static tree tsubst_initializer_list (tree, tree);
131 static tree get_class_bindings (tree, tree, tree);
132 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
134 static void tsubst_enum (tree, tree, tree);
135 static tree add_to_template_args (tree, tree);
136 static tree add_outermost_template_args (tree, tree);
137 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
138 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
140 static int type_unification_real (tree, tree, tree, const tree *,
141 unsigned int, int, unification_kind_t, int,
143 static void note_template_header (int);
144 static tree convert_nontype_argument_function (tree, tree);
145 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
146 static tree convert_template_argument (tree, tree, tree,
147 tsubst_flags_t, int, tree);
148 static int for_each_template_parm (tree, tree_fn_t, void*,
149 struct pointer_set_t*, bool);
150 static tree expand_template_argument_pack (tree);
151 static tree build_template_parm_index (int, int, int, tree, tree);
152 static bool inline_needs_template_parms (tree);
153 static void push_inline_template_parms_recursive (tree, int);
154 static tree retrieve_local_specialization (tree);
155 static void register_local_specialization (tree, tree);
156 static hashval_t hash_specialization (const void *p);
157 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
158 static int mark_template_parm (tree, void *);
159 static int template_parm_this_level_p (tree, void *);
160 static tree tsubst_friend_function (tree, tree);
161 static tree tsubst_friend_class (tree, tree);
162 static int can_complete_type_without_circularity (tree);
163 static tree get_bindings (tree, tree, tree, bool);
164 static int template_decl_level (tree);
165 static int check_cv_quals_for_unify (int, tree, tree);
166 static void template_parm_level_and_index (tree, int*, int*);
167 static int unify_pack_expansion (tree, tree, tree,
168 tree, unification_kind_t, bool, bool);
169 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
170 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
171 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
172 static void regenerate_decl_from_template (tree, tree);
173 static tree most_specialized_class (tree, tree, tsubst_flags_t);
174 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
175 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
176 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
177 static bool check_specialization_scope (void);
178 static tree process_partial_specialization (tree);
179 static void set_current_access_from_decl (tree);
180 static enum template_base_result get_template_base (tree, tree, tree, tree,
182 static tree try_class_unification (tree, tree, tree, tree, bool);
183 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
185 static bool template_template_parm_bindings_ok_p (tree, tree);
186 static int template_args_equal (tree, tree);
187 static void tsubst_default_arguments (tree);
188 static tree for_each_template_parm_r (tree *, int *, void *);
189 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
190 static void copy_default_args_to_explicit_spec (tree);
191 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
192 static int eq_local_specializations (const void *, const void *);
193 static bool dependent_template_arg_p (tree);
194 static bool any_template_arguments_need_structural_equality_p (tree);
195 static bool dependent_type_p_r (tree);
196 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
197 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
198 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
199 static tree tsubst_decl (tree, tree, tsubst_flags_t);
200 static void perform_typedefs_access_check (tree tmpl, tree targs);
201 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
203 static tree listify (tree);
204 static tree listify_autos (tree, tree);
205 static tree template_parm_to_arg (tree t);
206 static bool arg_from_parm_pack_p (tree, tree);
207 static tree current_template_args (void);
208 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
210 /* Make the current scope suitable for access checking when we are
211 processing T. T can be FUNCTION_DECL for instantiated function
212 template, or VAR_DECL for static member variable (need by
213 instantiate_decl). */
216 push_access_scope (tree t)
218 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
219 || TREE_CODE (t) == VAR_DECL);
221 if (DECL_FRIEND_CONTEXT (t))
222 push_nested_class (DECL_FRIEND_CONTEXT (t));
223 else if (DECL_CLASS_SCOPE_P (t))
224 push_nested_class (DECL_CONTEXT (t));
226 push_to_top_level ();
228 if (TREE_CODE (t) == FUNCTION_DECL)
230 saved_access_scope = tree_cons
231 (NULL_TREE, current_function_decl, saved_access_scope);
232 current_function_decl = t;
236 /* Restore the scope set up by push_access_scope. T is the node we
240 pop_access_scope (tree t)
242 if (TREE_CODE (t) == FUNCTION_DECL)
244 current_function_decl = TREE_VALUE (saved_access_scope);
245 saved_access_scope = TREE_CHAIN (saved_access_scope);
248 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
251 pop_from_top_level ();
254 /* Do any processing required when DECL (a member template
255 declaration) is finished. Returns the TEMPLATE_DECL corresponding
256 to DECL, unless it is a specialization, in which case the DECL
257 itself is returned. */
260 finish_member_template_decl (tree decl)
262 if (decl == error_mark_node)
263 return error_mark_node;
265 gcc_assert (DECL_P (decl));
267 if (TREE_CODE (decl) == TYPE_DECL)
271 type = TREE_TYPE (decl);
272 if (type == error_mark_node)
273 return error_mark_node;
274 if (MAYBE_CLASS_TYPE_P (type)
275 && CLASSTYPE_TEMPLATE_INFO (type)
276 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
278 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
279 check_member_template (tmpl);
284 else if (TREE_CODE (decl) == FIELD_DECL)
285 error ("data member %qD cannot be a member template", decl);
286 else if (DECL_TEMPLATE_INFO (decl))
288 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
290 check_member_template (DECL_TI_TEMPLATE (decl));
291 return DECL_TI_TEMPLATE (decl);
297 error ("invalid member template declaration %qD", decl);
299 return error_mark_node;
302 /* Create a template info node. */
305 build_template_info (tree template_decl, tree template_args)
307 tree result = make_node (TEMPLATE_INFO);
308 TI_TEMPLATE (result) = template_decl;
309 TI_ARGS (result) = template_args;
313 /* Return the template info node corresponding to T, whatever T is. */
316 get_template_info (const_tree t)
318 tree tinfo = NULL_TREE;
320 if (!t || t == error_mark_node)
323 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
324 tinfo = DECL_TEMPLATE_INFO (t);
326 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
329 if (TAGGED_TYPE_P (t))
330 tinfo = TYPE_TEMPLATE_INFO (t);
331 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
332 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
337 /* Returns the template nesting level of the indicated class TYPE.
347 A<T>::B<U> has depth two, while A<T> has depth one.
348 Both A<T>::B<int> and A<int>::B<U> have depth one, if
349 they are instantiations, not specializations.
351 This function is guaranteed to return 0 if passed NULL_TREE so
352 that, for example, `template_class_depth (current_class_type)' is
356 template_class_depth (tree type)
361 type && TREE_CODE (type) != NAMESPACE_DECL;
362 type = (TREE_CODE (type) == FUNCTION_DECL)
363 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
365 tree tinfo = get_template_info (type);
367 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
368 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
375 /* Subroutine of maybe_begin_member_template_processing.
376 Returns true if processing DECL needs us to push template parms. */
379 inline_needs_template_parms (tree decl)
381 if (! DECL_TEMPLATE_INFO (decl))
384 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
385 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
388 /* Subroutine of maybe_begin_member_template_processing.
389 Push the template parms in PARMS, starting from LEVELS steps into the
390 chain, and ending at the beginning, since template parms are listed
394 push_inline_template_parms_recursive (tree parmlist, int levels)
396 tree parms = TREE_VALUE (parmlist);
400 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
402 ++processing_template_decl;
403 current_template_parms
404 = tree_cons (size_int (processing_template_decl),
405 parms, current_template_parms);
406 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
408 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
410 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
412 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
414 if (parm == error_mark_node)
417 gcc_assert (DECL_P (parm));
419 switch (TREE_CODE (parm))
428 /* Make a CONST_DECL as is done in process_template_parm.
429 It is ugly that we recreate this here; the original
430 version built in process_template_parm is no longer
432 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
433 CONST_DECL, DECL_NAME (parm),
435 DECL_ARTIFICIAL (decl) = 1;
436 TREE_CONSTANT (decl) = 1;
437 TREE_READONLY (decl) = 1;
438 DECL_INITIAL (decl) = DECL_INITIAL (parm);
439 SET_DECL_TEMPLATE_PARM_P (decl);
450 /* Restore the template parameter context for a member template or
451 a friend template defined in a class definition. */
454 maybe_begin_member_template_processing (tree decl)
459 if (inline_needs_template_parms (decl))
461 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
462 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
464 if (DECL_TEMPLATE_SPECIALIZATION (decl))
467 parms = TREE_CHAIN (parms);
470 push_inline_template_parms_recursive (parms, levels);
473 /* Remember how many levels of template parameters we pushed so that
474 we can pop them later. */
475 VEC_safe_push (int, heap, inline_parm_levels, levels);
478 /* Undo the effects of maybe_begin_member_template_processing. */
481 maybe_end_member_template_processing (void)
486 if (VEC_length (int, inline_parm_levels) == 0)
489 last = VEC_pop (int, inline_parm_levels);
490 for (i = 0; i < last; ++i)
492 --processing_template_decl;
493 current_template_parms = TREE_CHAIN (current_template_parms);
498 /* Return a new template argument vector which contains all of ARGS,
499 but has as its innermost set of arguments the EXTRA_ARGS. */
502 add_to_template_args (tree args, tree extra_args)
509 if (args == NULL_TREE || extra_args == error_mark_node)
512 extra_depth = TMPL_ARGS_DEPTH (extra_args);
513 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
515 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
516 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
518 for (j = 1; j <= extra_depth; ++j, ++i)
519 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
524 /* Like add_to_template_args, but only the outermost ARGS are added to
525 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
526 (EXTRA_ARGS) levels are added. This function is used to combine
527 the template arguments from a partial instantiation with the
528 template arguments used to attain the full instantiation from the
529 partial instantiation. */
532 add_outermost_template_args (tree args, tree extra_args)
536 /* If there are more levels of EXTRA_ARGS than there are ARGS,
537 something very fishy is going on. */
538 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
540 /* If *all* the new arguments will be the EXTRA_ARGS, just return
542 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
545 /* For the moment, we make ARGS look like it contains fewer levels. */
546 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
548 new_args = add_to_template_args (args, extra_args);
550 /* Now, we restore ARGS to its full dimensions. */
551 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
556 /* Return the N levels of innermost template arguments from the ARGS. */
559 get_innermost_template_args (tree args, int n)
567 /* If N is 1, just return the innermost set of template arguments. */
569 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
571 /* If we're not removing anything, just return the arguments we were
573 extra_levels = TMPL_ARGS_DEPTH (args) - n;
574 gcc_assert (extra_levels >= 0);
575 if (extra_levels == 0)
578 /* Make a new set of arguments, not containing the outer arguments. */
579 new_args = make_tree_vec (n);
580 for (i = 1; i <= n; ++i)
581 SET_TMPL_ARGS_LEVEL (new_args, i,
582 TMPL_ARGS_LEVEL (args, i + extra_levels));
587 /* The inverse of get_innermost_template_args: Return all but the innermost
588 EXTRA_LEVELS levels of template arguments from the ARGS. */
591 strip_innermost_template_args (tree args, int extra_levels)
594 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
599 /* If N is 1, just return the outermost set of template arguments. */
601 return TMPL_ARGS_LEVEL (args, 1);
603 /* If we're not removing anything, just return the arguments we were
605 gcc_assert (extra_levels >= 0);
606 if (extra_levels == 0)
609 /* Make a new set of arguments, not containing the inner arguments. */
610 new_args = make_tree_vec (n);
611 for (i = 1; i <= n; ++i)
612 SET_TMPL_ARGS_LEVEL (new_args, i,
613 TMPL_ARGS_LEVEL (args, i));
618 /* We've got a template header coming up; push to a new level for storing
622 begin_template_parm_list (void)
624 /* We use a non-tag-transparent scope here, which causes pushtag to
625 put tags in this scope, rather than in the enclosing class or
626 namespace scope. This is the right thing, since we want
627 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
628 global template class, push_template_decl handles putting the
629 TEMPLATE_DECL into top-level scope. For a nested template class,
632 template <class T> struct S1 {
633 template <class T> struct S2 {};
636 pushtag contains special code to call pushdecl_with_scope on the
637 TEMPLATE_DECL for S2. */
638 begin_scope (sk_template_parms, NULL);
639 ++processing_template_decl;
640 ++processing_template_parmlist;
641 note_template_header (0);
644 /* This routine is called when a specialization is declared. If it is
645 invalid to declare a specialization here, an error is reported and
646 false is returned, otherwise this routine will return true. */
649 check_specialization_scope (void)
651 tree scope = current_scope ();
655 An explicit specialization shall be declared in the namespace of
656 which the template is a member, or, for member templates, in the
657 namespace of which the enclosing class or enclosing class
658 template is a member. An explicit specialization of a member
659 function, member class or static data member of a class template
660 shall be declared in the namespace of which the class template
662 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
664 error ("explicit specialization in non-namespace scope %qD", scope);
670 In an explicit specialization declaration for a member of a class
671 template or a member template that appears in namespace scope,
672 the member template and some of its enclosing class templates may
673 remain unspecialized, except that the declaration shall not
674 explicitly specialize a class member template if its enclosing
675 class templates are not explicitly specialized as well. */
676 if (current_template_parms)
678 error ("enclosing class templates are not explicitly specialized");
685 /* We've just seen template <>. */
688 begin_specialization (void)
690 begin_scope (sk_template_spec, NULL);
691 note_template_header (1);
692 return check_specialization_scope ();
695 /* Called at then end of processing a declaration preceded by
699 end_specialization (void)
702 reset_specialization ();
705 /* Any template <>'s that we have seen thus far are not referring to a
706 function specialization. */
709 reset_specialization (void)
711 processing_specialization = 0;
712 template_header_count = 0;
715 /* We've just seen a template header. If SPECIALIZATION is nonzero,
716 it was of the form template <>. */
719 note_template_header (int specialization)
721 processing_specialization = specialization;
722 template_header_count++;
725 /* We're beginning an explicit instantiation. */
728 begin_explicit_instantiation (void)
730 gcc_assert (!processing_explicit_instantiation);
731 processing_explicit_instantiation = true;
736 end_explicit_instantiation (void)
738 gcc_assert (processing_explicit_instantiation);
739 processing_explicit_instantiation = false;
742 /* An explicit specialization or partial specialization TMPL is being
743 declared. Check that the namespace in which the specialization is
744 occurring is permissible. Returns false iff it is invalid to
745 specialize TMPL in the current namespace. */
748 check_specialization_namespace (tree tmpl)
750 tree tpl_ns = decl_namespace_context (tmpl);
754 An explicit specialization shall be declared in the namespace of
755 which the template is a member, or, for member templates, in the
756 namespace of which the enclosing class or enclosing class
757 template is a member. An explicit specialization of a member
758 function, member class or static data member of a class template
759 shall be declared in the namespace of which the class template is
761 if (current_scope() != DECL_CONTEXT (tmpl)
762 && !at_namespace_scope_p ())
764 error ("specialization of %qD must appear at namespace scope", tmpl);
767 if (is_associated_namespace (current_namespace, tpl_ns))
768 /* Same or super-using namespace. */
772 permerror (input_location, "specialization of %qD in different namespace", tmpl);
773 permerror (input_location, " from definition of %q+#D", tmpl);
778 /* SPEC is an explicit instantiation. Check that it is valid to
779 perform this explicit instantiation in the current namespace. */
782 check_explicit_instantiation_namespace (tree spec)
786 /* DR 275: An explicit instantiation shall appear in an enclosing
787 namespace of its template. */
788 ns = decl_namespace_context (spec);
789 if (!is_ancestor (current_namespace, ns))
790 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
791 "(which does not enclose namespace %qD)",
792 spec, current_namespace, ns);
795 /* The TYPE is being declared. If it is a template type, that means it
796 is a partial specialization. Do appropriate error-checking. */
799 maybe_process_partial_specialization (tree type)
803 if (type == error_mark_node)
804 return error_mark_node;
806 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
808 error ("name of class shadows template template parameter %qD",
810 return error_mark_node;
813 context = TYPE_CONTEXT (type);
815 if ((CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
816 /* Consider non-class instantiations of alias templates as
819 && TYPE_TEMPLATE_INFO (type)
820 && DECL_LANG_SPECIFIC (TYPE_NAME (type))
821 && DECL_USE_TEMPLATE (TYPE_NAME (type))))
823 /* This is for ordinary explicit specialization and partial
824 specialization of a template class such as:
826 template <> class C<int>;
830 template <class T> class C<T*>;
832 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
834 if (CLASS_TYPE_P (type)
835 && CLASSTYPE_IMPLICIT_INSTANTIATION (type)
836 && !COMPLETE_TYPE_P (type))
838 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
839 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
840 if (processing_template_decl)
842 if (push_template_decl (TYPE_MAIN_DECL (type))
844 return error_mark_node;
847 else if (CLASS_TYPE_P (type)
848 && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
849 error ("specialization of %qT after instantiation", type);
851 if (DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
853 error ("partial specialization of alias template %qD",
854 TYPE_TI_TEMPLATE (type));
855 return error_mark_node;
858 else if (CLASS_TYPE_P (type)
859 && !CLASSTYPE_USE_TEMPLATE (type)
860 && CLASSTYPE_TEMPLATE_INFO (type)
861 && context && CLASS_TYPE_P (context)
862 && CLASSTYPE_TEMPLATE_INFO (context))
864 /* This is for an explicit specialization of member class
865 template according to [temp.expl.spec/18]:
867 template <> template <class U> class C<int>::D;
869 The context `C<int>' must be an implicit instantiation.
870 Otherwise this is just a member class template declared
873 template <> class C<int> { template <class U> class D; };
874 template <> template <class U> class C<int>::D;
876 In the first case, `C<int>::D' is a specialization of `C<T>::D'
877 while in the second case, `C<int>::D' is a primary template
878 and `C<T>::D' may not exist. */
880 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
881 && !COMPLETE_TYPE_P (type))
884 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
886 if (current_namespace
887 != decl_namespace_context (tmpl))
889 permerror (input_location, "specializing %q#T in different namespace", type);
890 permerror (input_location, " from definition of %q+#D", tmpl);
893 /* Check for invalid specialization after instantiation:
895 template <> template <> class C<int>::D<int>;
896 template <> template <class U> class C<int>::D; */
898 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
899 t; t = TREE_CHAIN (t))
901 tree inst = TREE_VALUE (t);
902 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
904 /* We already have a full specialization of this partial
905 instantiation. Reassign it to the new member
906 specialization template. */
911 elt.tmpl = most_general_template (tmpl);
912 elt.args = CLASSTYPE_TI_ARGS (inst);
915 htab_remove_elt (type_specializations, &elt);
918 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
920 slot = htab_find_slot (type_specializations, &elt, INSERT);
921 entry = ggc_alloc_spec_entry ();
925 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
926 /* But if we've had an implicit instantiation, that's a
927 problem ([temp.expl.spec]/6). */
928 error ("specialization %qT after instantiation %qT",
932 /* Mark TYPE as a specialization. And as a result, we only
933 have one level of template argument for the innermost
935 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
936 CLASSTYPE_TI_ARGS (type)
937 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
940 else if (processing_specialization)
942 /* Someday C++0x may allow for enum template specialization. */
943 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
944 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
945 pedwarn (input_location, OPT_pedantic, "template specialization "
946 "of %qD not allowed by ISO C++", type);
949 error ("explicit specialization of non-template %qT", type);
950 return error_mark_node;
957 /* Returns nonzero if we can optimize the retrieval of specializations
958 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
959 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
962 optimize_specialization_lookup_p (tree tmpl)
964 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
965 && DECL_CLASS_SCOPE_P (tmpl)
966 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
968 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
969 /* The optimized lookup depends on the fact that the
970 template arguments for the member function template apply
971 purely to the containing class, which is not true if the
972 containing class is an explicit or partial
974 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
975 && !DECL_MEMBER_TEMPLATE_P (tmpl)
976 && !DECL_CONV_FN_P (tmpl)
977 /* It is possible to have a template that is not a member
978 template and is not a member of a template class:
980 template <typename T>
981 struct S { friend A::f(); };
983 Here, the friend function is a template, but the context does
984 not have template information. The optimized lookup relies
985 on having ARGS be the template arguments for both the class
986 and the function template. */
987 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
990 /* Retrieve the specialization (in the sense of [temp.spec] - a
991 specialization is either an instantiation or an explicit
992 specialization) of TMPL for the given template ARGS. If there is
993 no such specialization, return NULL_TREE. The ARGS are a vector of
994 arguments, or a vector of vectors of arguments, in the case of
995 templates with more than one level of parameters.
997 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
998 then we search for a partial specialization matching ARGS. This
999 parameter is ignored if TMPL is not a class template. */
1002 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1004 if (args == error_mark_node)
1007 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1009 /* There should be as many levels of arguments as there are
1010 levels of parameters. */
1011 gcc_assert (TMPL_ARGS_DEPTH (args)
1012 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
1014 if (optimize_specialization_lookup_p (tmpl))
1016 tree class_template;
1017 tree class_specialization;
1018 VEC(tree,gc) *methods;
1022 /* The template arguments actually apply to the containing
1023 class. Find the class specialization with those
1025 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1026 class_specialization
1027 = retrieve_specialization (class_template, args, 0);
1028 if (!class_specialization)
1030 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1031 for the specialization. */
1032 idx = class_method_index_for_fn (class_specialization, tmpl);
1035 /* Iterate through the methods with the indicated name, looking
1036 for the one that has an instance of TMPL. */
1037 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1038 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
1040 tree fn = OVL_CURRENT (fns);
1041 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1042 /* using-declarations can add base methods to the method vec,
1043 and we don't want those here. */
1044 && DECL_CONTEXT (fn) == class_specialization)
1053 htab_t specializations;
1057 elt.spec = NULL_TREE;
1059 if (DECL_CLASS_TEMPLATE_P (tmpl))
1060 specializations = type_specializations;
1062 specializations = decl_specializations;
1065 hash = hash_specialization (&elt);
1066 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1074 /* Like retrieve_specialization, but for local declarations. */
1077 retrieve_local_specialization (tree tmpl)
1081 if (local_specializations == NULL)
1084 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
1085 htab_hash_pointer (tmpl));
1086 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
1089 /* Returns nonzero iff DECL is a specialization of TMPL. */
1092 is_specialization_of (tree decl, tree tmpl)
1096 if (TREE_CODE (decl) == FUNCTION_DECL)
1100 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1106 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1108 for (t = TREE_TYPE (decl);
1110 t = CLASSTYPE_USE_TEMPLATE (t)
1111 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1112 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1119 /* Returns nonzero iff DECL is a specialization of friend declaration
1120 FRIEND_DECL according to [temp.friend]. */
1123 is_specialization_of_friend (tree decl, tree friend_decl)
1125 bool need_template = true;
1128 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1129 || TREE_CODE (decl) == TYPE_DECL);
1131 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1132 of a template class, we want to check if DECL is a specialization
1134 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1135 && DECL_TEMPLATE_INFO (friend_decl)
1136 && !DECL_USE_TEMPLATE (friend_decl))
1138 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1139 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1140 need_template = false;
1142 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1143 && !PRIMARY_TEMPLATE_P (friend_decl))
1144 need_template = false;
1146 /* There is nothing to do if this is not a template friend. */
1147 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1150 if (is_specialization_of (decl, friend_decl))
1154 A member of a class template may be declared to be a friend of a
1155 non-template class. In this case, the corresponding member of
1156 every specialization of the class template is a friend of the
1157 class granting friendship.
1159 For example, given a template friend declaration
1161 template <class T> friend void A<T>::f();
1163 the member function below is considered a friend
1165 template <> struct A<int> {
1169 For this type of template friend, TEMPLATE_DEPTH below will be
1170 nonzero. To determine if DECL is a friend of FRIEND, we first
1171 check if the enclosing class is a specialization of another. */
1173 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1175 && DECL_CLASS_SCOPE_P (decl)
1176 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1177 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1179 /* Next, we check the members themselves. In order to handle
1180 a few tricky cases, such as when FRIEND_DECL's are
1182 template <class T> friend void A<T>::g(T t);
1183 template <class T> template <T t> friend void A<T>::h();
1187 void A<int>::g(int);
1188 template <int> void A<int>::h();
1190 we need to figure out ARGS, the template arguments from
1191 the context of DECL. This is required for template substitution
1192 of `T' in the function parameter of `g' and template parameter
1193 of `h' in the above examples. Here ARGS corresponds to `int'. */
1195 tree context = DECL_CONTEXT (decl);
1196 tree args = NULL_TREE;
1197 int current_depth = 0;
1199 while (current_depth < template_depth)
1201 if (CLASSTYPE_TEMPLATE_INFO (context))
1203 if (current_depth == 0)
1204 args = TYPE_TI_ARGS (context);
1206 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1209 context = TYPE_CONTEXT (context);
1212 if (TREE_CODE (decl) == FUNCTION_DECL)
1217 tree friend_args_type;
1218 tree decl_args_type;
1220 /* Make sure that both DECL and FRIEND_DECL are templates or
1222 is_template = DECL_TEMPLATE_INFO (decl)
1223 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1224 if (need_template ^ is_template)
1226 else if (is_template)
1228 /* If both are templates, check template parameter list. */
1230 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1232 if (!comp_template_parms
1233 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1237 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1240 decl_type = TREE_TYPE (decl);
1242 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1243 tf_none, NULL_TREE);
1244 if (friend_type == error_mark_node)
1247 /* Check if return types match. */
1248 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1251 /* Check if function parameter types match, ignoring the
1252 `this' parameter. */
1253 friend_args_type = TYPE_ARG_TYPES (friend_type);
1254 decl_args_type = TYPE_ARG_TYPES (decl_type);
1255 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1256 friend_args_type = TREE_CHAIN (friend_args_type);
1257 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1258 decl_args_type = TREE_CHAIN (decl_args_type);
1260 return compparms (decl_args_type, friend_args_type);
1264 /* DECL is a TYPE_DECL */
1266 tree decl_type = TREE_TYPE (decl);
1268 /* Make sure that both DECL and FRIEND_DECL are templates or
1271 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1272 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1274 if (need_template ^ is_template)
1276 else if (is_template)
1279 /* If both are templates, check the name of the two
1280 TEMPLATE_DECL's first because is_friend didn't. */
1281 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1282 != DECL_NAME (friend_decl))
1285 /* Now check template parameter list. */
1287 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1289 return comp_template_parms
1290 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1294 return (DECL_NAME (decl)
1295 == DECL_NAME (friend_decl));
1301 /* Register the specialization SPEC as a specialization of TMPL with
1302 the indicated ARGS. IS_FRIEND indicates whether the specialization
1303 is actually just a friend declaration. Returns SPEC, or an
1304 equivalent prior declaration, if available. */
1307 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1314 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1316 if (TREE_CODE (spec) == FUNCTION_DECL
1317 && uses_template_parms (DECL_TI_ARGS (spec)))
1318 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1319 register it; we want the corresponding TEMPLATE_DECL instead.
1320 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1321 the more obvious `uses_template_parms (spec)' to avoid problems
1322 with default function arguments. In particular, given
1323 something like this:
1325 template <class T> void f(T t1, T t = T())
1327 the default argument expression is not substituted for in an
1328 instantiation unless and until it is actually needed. */
1331 if (optimize_specialization_lookup_p (tmpl))
1332 /* We don't put these specializations in the hash table, but we might
1333 want to give an error about a mismatch. */
1334 fn = retrieve_specialization (tmpl, args, 0);
1342 hash = hash_specialization (&elt);
1345 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1347 fn = ((spec_entry *) *slot)->spec;
1352 /* We can sometimes try to re-register a specialization that we've
1353 already got. In particular, regenerate_decl_from_template calls
1354 duplicate_decls which will update the specialization list. But,
1355 we'll still get called again here anyhow. It's more convenient
1356 to simply allow this than to try to prevent it. */
1359 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1361 if (DECL_TEMPLATE_INSTANTIATION (fn))
1363 if (DECL_ODR_USED (fn)
1364 || DECL_EXPLICIT_INSTANTIATION (fn))
1366 error ("specialization of %qD after instantiation",
1368 return error_mark_node;
1373 /* This situation should occur only if the first
1374 specialization is an implicit instantiation, the
1375 second is an explicit specialization, and the
1376 implicit instantiation has not yet been used. That
1377 situation can occur if we have implicitly
1378 instantiated a member function and then specialized
1381 We can also wind up here if a friend declaration that
1382 looked like an instantiation turns out to be a
1385 template <class T> void foo(T);
1386 class S { friend void foo<>(int) };
1387 template <> void foo(int);
1389 We transform the existing DECL in place so that any
1390 pointers to it become pointers to the updated
1393 If there was a definition for the template, but not
1394 for the specialization, we want this to look as if
1395 there were no definition, and vice versa. */
1396 DECL_INITIAL (fn) = NULL_TREE;
1397 duplicate_decls (spec, fn, is_friend);
1398 /* The call to duplicate_decls will have applied
1401 An explicit specialization of a function template
1402 is inline only if it is explicitly declared to be,
1403 and independently of whether its function template
1406 to the primary function; now copy the inline bits to
1407 the various clones. */
1408 FOR_EACH_CLONE (clone, fn)
1410 DECL_DECLARED_INLINE_P (clone)
1411 = DECL_DECLARED_INLINE_P (fn);
1412 DECL_SOURCE_LOCATION (clone)
1413 = DECL_SOURCE_LOCATION (fn);
1415 check_specialization_namespace (fn);
1420 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1422 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1423 /* Dup decl failed, but this is a new definition. Set the
1424 line number so any errors match this new
1426 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1432 return duplicate_decls (spec, fn, is_friend);
1434 /* A specialization must be declared in the same namespace as the
1435 template it is specializing. */
1436 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1437 && !check_specialization_namespace (tmpl))
1438 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1440 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1442 spec_entry *entry = ggc_alloc_spec_entry ();
1443 gcc_assert (tmpl && args && spec);
1446 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1447 && PRIMARY_TEMPLATE_P (tmpl)
1448 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1449 /* TMPL is a forward declaration of a template function; keep a list
1450 of all specializations in case we need to reassign them to a friend
1451 template later in tsubst_friend_function. */
1452 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1453 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1459 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1460 TMPL and ARGS members, ignores SPEC. */
1462 int comparing_specializations;
1465 eq_specializations (const void *p1, const void *p2)
1467 const spec_entry *e1 = (const spec_entry *)p1;
1468 const spec_entry *e2 = (const spec_entry *)p2;
1471 ++comparing_specializations;
1472 equal = (e1->tmpl == e2->tmpl
1473 && comp_template_args (e1->args, e2->args));
1474 --comparing_specializations;
1479 /* Returns a hash for a template TMPL and template arguments ARGS. */
1482 hash_tmpl_and_args (tree tmpl, tree args)
1484 hashval_t val = DECL_UID (tmpl);
1485 return iterative_hash_template_arg (args, val);
1488 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1492 hash_specialization (const void *p)
1494 const spec_entry *e = (const spec_entry *)p;
1495 return hash_tmpl_and_args (e->tmpl, e->args);
1498 /* Recursively calculate a hash value for a template argument ARG, for use
1499 in the hash tables of template specializations. */
1502 iterative_hash_template_arg (tree arg, hashval_t val)
1504 unsigned HOST_WIDE_INT i;
1505 enum tree_code code;
1508 if (arg == NULL_TREE)
1509 return iterative_hash_object (arg, val);
1514 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1515 /* We can get one of these when re-hashing a previous entry in the middle
1516 of substituting into a pack expansion. Just look through it. */
1517 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1519 code = TREE_CODE (arg);
1520 tclass = TREE_CODE_CLASS (code);
1522 val = iterative_hash_object (code, val);
1529 case IDENTIFIER_NODE:
1530 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1534 int i, len = TREE_VEC_LENGTH (arg);
1535 for (i = 0; i < len; ++i)
1536 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1540 case TYPE_PACK_EXPANSION:
1541 case EXPR_PACK_EXPANSION:
1542 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1543 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1545 case TYPE_ARGUMENT_PACK:
1546 case NONTYPE_ARGUMENT_PACK:
1547 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1550 for (; arg; arg = TREE_CHAIN (arg))
1551 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1555 for (; arg; arg = OVL_NEXT (arg))
1556 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1562 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1564 val = iterative_hash_template_arg (field, val);
1565 val = iterative_hash_template_arg (value, val);
1571 if (!DECL_ARTIFICIAL (arg))
1573 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1574 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1576 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1579 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1582 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1583 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1585 case TEMPLATE_PARM_INDEX:
1586 val = iterative_hash_template_arg
1587 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1588 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1589 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1592 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1593 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1594 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1597 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1599 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1603 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1604 code = TREE_CODE (TREE_OPERAND (arg, 1));
1605 val = iterative_hash_object (code, val);
1606 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1609 /* A lambda can't appear in a template arg, but don't crash on
1611 gcc_assert (seen_error ());
1615 case IMPLICIT_CONV_EXPR:
1616 case STATIC_CAST_EXPR:
1617 case REINTERPRET_CAST_EXPR:
1618 case CONST_CAST_EXPR:
1619 case DYNAMIC_CAST_EXPR:
1621 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1622 /* Now hash operands as usual. */
1632 if (TYPE_CANONICAL (arg))
1633 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1635 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1636 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1637 /* Otherwise just compare the types during lookup. */
1640 case tcc_declaration:
1642 return iterative_hash_expr (arg, val);
1645 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1647 unsigned n = cp_tree_operand_length (arg);
1648 for (i = 0; i < n; ++i)
1649 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1657 /* Unregister the specialization SPEC as a specialization of TMPL.
1658 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1659 if the SPEC was listed as a specialization of TMPL.
1661 Note that SPEC has been ggc_freed, so we can't look inside it. */
1664 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1669 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1670 elt.args = TI_ARGS (tinfo);
1671 elt.spec = NULL_TREE;
1673 entry = (spec_entry *) htab_find (decl_specializations, &elt);
1676 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1677 gcc_assert (new_spec != NULL_TREE);
1678 entry->spec = new_spec;
1685 /* Compare an entry in the local specializations hash table P1 (which
1686 is really a pointer to a TREE_LIST) with P2 (which is really a
1690 eq_local_specializations (const void *p1, const void *p2)
1692 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1695 /* Hash P1, an entry in the local specializations table. */
1698 hash_local_specialization (const void* p1)
1700 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1703 /* Like register_specialization, but for local declarations. We are
1704 registering SPEC, an instantiation of TMPL. */
1707 register_local_specialization (tree spec, tree tmpl)
1711 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1712 htab_hash_pointer (tmpl), INSERT);
1713 *slot = build_tree_list (spec, tmpl);
1716 /* TYPE is a class type. Returns true if TYPE is an explicitly
1717 specialized class. */
1720 explicit_class_specialization_p (tree type)
1722 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1724 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1727 /* Print the list of functions at FNS, going through all the overloads
1728 for each element of the list. Alternatively, FNS can not be a
1729 TREE_LIST, in which case it will be printed together with all the
1732 MORE and *STR should respectively be FALSE and NULL when the function
1733 is called from the outside. They are used internally on recursive
1734 calls. print_candidates manages the two parameters and leaves NULL
1735 in *STR when it ends. */
1738 print_candidates_1 (tree fns, bool more, const char **str)
1741 char *spaces = NULL;
1743 for (fn = fns; fn; fn = OVL_NEXT (fn))
1744 if (TREE_CODE (fn) == TREE_LIST)
1746 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1747 print_candidates_1 (TREE_VALUE (fn2),
1748 TREE_CHAIN (fn2) || more, str);
1754 /* Pick the prefix string. */
1755 if (!more && !OVL_NEXT (fns))
1757 error ("candidate is: %+#D", OVL_CURRENT (fn));
1761 *str = _("candidates are:");
1762 spaces = get_spaces (*str);
1764 error ("%s %+#D", *str, OVL_CURRENT (fn));
1765 *str = spaces ? spaces : *str;
1775 /* Print the list of candidate FNS in an error message. FNS can also
1776 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1779 print_candidates (tree fns)
1781 const char *str = NULL;
1782 print_candidates_1 (fns, false, &str);
1783 gcc_assert (str == NULL);
1786 /* Returns the template (one of the functions given by TEMPLATE_ID)
1787 which can be specialized to match the indicated DECL with the
1788 explicit template args given in TEMPLATE_ID. The DECL may be
1789 NULL_TREE if none is available. In that case, the functions in
1790 TEMPLATE_ID are non-members.
1792 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1793 specialization of a member template.
1795 The TEMPLATE_COUNT is the number of references to qualifying
1796 template classes that appeared in the name of the function. See
1797 check_explicit_specialization for a more accurate description.
1799 TSK indicates what kind of template declaration (if any) is being
1800 declared. TSK_TEMPLATE indicates that the declaration given by
1801 DECL, though a FUNCTION_DECL, has template parameters, and is
1802 therefore a template function.
1804 The template args (those explicitly specified and those deduced)
1805 are output in a newly created vector *TARGS_OUT.
1807 If it is impossible to determine the result, an error message is
1808 issued. The error_mark_node is returned to indicate failure. */
1811 determine_specialization (tree template_id,
1814 int need_member_template,
1820 tree explicit_targs;
1821 tree candidates = NULL_TREE;
1822 /* A TREE_LIST of templates of which DECL may be a specialization.
1823 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1824 corresponding TREE_PURPOSE is the set of template arguments that,
1825 when used to instantiate the template, would produce a function
1826 with the signature of DECL. */
1827 tree templates = NULL_TREE;
1829 cp_binding_level *b;
1831 *targs_out = NULL_TREE;
1833 if (template_id == error_mark_node || decl == error_mark_node)
1834 return error_mark_node;
1836 fns = TREE_OPERAND (template_id, 0);
1837 explicit_targs = TREE_OPERAND (template_id, 1);
1839 if (fns == error_mark_node)
1840 return error_mark_node;
1842 /* Check for baselinks. */
1843 if (BASELINK_P (fns))
1844 fns = BASELINK_FUNCTIONS (fns);
1846 if (!is_overloaded_fn (fns))
1848 error ("%qD is not a function template", fns);
1849 return error_mark_node;
1852 /* Count the number of template headers specified for this
1855 for (b = current_binding_level;
1856 b->kind == sk_template_parms;
1860 for (; fns; fns = OVL_NEXT (fns))
1862 tree fn = OVL_CURRENT (fns);
1864 if (TREE_CODE (fn) == TEMPLATE_DECL)
1866 tree decl_arg_types;
1870 /* In case of explicit specialization, we need to check if
1871 the number of template headers appearing in the specialization
1872 is correct. This is usually done in check_explicit_specialization,
1873 but the check done there cannot be exhaustive when specializing
1874 member functions. Consider the following code:
1876 template <> void A<int>::f(int);
1877 template <> template <> void A<int>::f(int);
1879 Assuming that A<int> is not itself an explicit specialization
1880 already, the first line specializes "f" which is a non-template
1881 member function, whilst the second line specializes "f" which
1882 is a template member function. So both lines are syntactically
1883 correct, and check_explicit_specialization does not reject
1886 Here, we can do better, as we are matching the specialization
1887 against the declarations. We count the number of template
1888 headers, and we check if they match TEMPLATE_COUNT + 1
1889 (TEMPLATE_COUNT is the number of qualifying template classes,
1890 plus there must be another header for the member template
1893 Notice that if header_count is zero, this is not a
1894 specialization but rather a template instantiation, so there
1895 is no check we can perform here. */
1896 if (header_count && header_count != template_count + 1)
1899 /* Check that the number of template arguments at the
1900 innermost level for DECL is the same as for FN. */
1901 if (current_binding_level->kind == sk_template_parms
1902 && !current_binding_level->explicit_spec_p
1903 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1904 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1905 (current_template_parms))))
1908 /* DECL might be a specialization of FN. */
1909 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1910 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1912 /* For a non-static member function, we need to make sure
1913 that the const qualification is the same. Since
1914 get_bindings does not try to merge the "this" parameter,
1915 we must do the comparison explicitly. */
1916 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1917 && !same_type_p (TREE_VALUE (fn_arg_types),
1918 TREE_VALUE (decl_arg_types)))
1921 /* Skip the "this" parameter and, for constructors of
1922 classes with virtual bases, the VTT parameter. A
1923 full specialization of a constructor will have a VTT
1924 parameter, but a template never will. */
1926 = skip_artificial_parms_for (decl, decl_arg_types);
1928 = skip_artificial_parms_for (fn, fn_arg_types);
1930 /* Check that the number of function parameters matches.
1932 template <class T> void f(int i = 0);
1933 template <> void f<int>();
1934 The specialization f<int> is invalid but is not caught
1935 by get_bindings below. */
1936 if (cxx_dialect < cxx11
1937 && list_length (fn_arg_types) != list_length (decl_arg_types))
1940 /* Function templates cannot be specializations; there are
1941 no partial specializations of functions. Therefore, if
1942 the type of DECL does not match FN, there is no
1944 if (tsk == tsk_template)
1946 if (compparms (fn_arg_types, decl_arg_types))
1947 candidates = tree_cons (NULL_TREE, fn, candidates);
1951 /* See whether this function might be a specialization of this
1953 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1956 /* We cannot deduce template arguments that when used to
1957 specialize TMPL will produce DECL. */
1960 if (cxx_dialect >= cxx11)
1962 /* Make sure that the deduced arguments actually work. */
1963 insttype = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
1964 if (insttype == error_mark_node)
1967 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
1968 if (!compparms (fn_arg_types, decl_arg_types))
1972 /* Save this template, and the arguments deduced. */
1973 templates = tree_cons (targs, fn, templates);
1975 else if (need_member_template)
1976 /* FN is an ordinary member function, and we need a
1977 specialization of a member template. */
1979 else if (TREE_CODE (fn) != FUNCTION_DECL)
1980 /* We can get IDENTIFIER_NODEs here in certain erroneous
1983 else if (!DECL_FUNCTION_MEMBER_P (fn))
1984 /* This is just an ordinary non-member function. Nothing can
1985 be a specialization of that. */
1987 else if (DECL_ARTIFICIAL (fn))
1988 /* Cannot specialize functions that are created implicitly. */
1992 tree decl_arg_types;
1994 /* This is an ordinary member function. However, since
1995 we're here, we can assume it's enclosing class is a
1996 template class. For example,
1998 template <typename T> struct S { void f(); };
1999 template <> void S<int>::f() {}
2001 Here, S<int>::f is a non-template, but S<int> is a
2002 template class. If FN has the same type as DECL, we
2003 might be in business. */
2005 if (!DECL_TEMPLATE_INFO (fn))
2006 /* Its enclosing class is an explicit specialization
2007 of a template class. This is not a candidate. */
2010 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2011 TREE_TYPE (TREE_TYPE (fn))))
2012 /* The return types differ. */
2015 /* Adjust the type of DECL in case FN is a static member. */
2016 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2017 if (DECL_STATIC_FUNCTION_P (fn)
2018 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2019 decl_arg_types = TREE_CHAIN (decl_arg_types);
2021 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2024 candidates = tree_cons (NULL_TREE, fn, candidates);
2028 if (templates && TREE_CHAIN (templates))
2034 It is possible for a specialization with a given function
2035 signature to be instantiated from more than one function
2036 template. In such cases, explicit specification of the
2037 template arguments must be used to uniquely identify the
2038 function template specialization being specialized.
2040 Note that here, there's no suggestion that we're supposed to
2041 determine which of the candidate templates is most
2042 specialized. However, we, also have:
2046 Partial ordering of overloaded function template
2047 declarations is used in the following contexts to select
2048 the function template to which a function template
2049 specialization refers:
2051 -- when an explicit specialization refers to a function
2054 So, we do use the partial ordering rules, at least for now.
2055 This extension can only serve to make invalid programs valid,
2056 so it's safe. And, there is strong anecdotal evidence that
2057 the committee intended the partial ordering rules to apply;
2058 the EDG front end has that behavior, and John Spicer claims
2059 that the committee simply forgot to delete the wording in
2060 [temp.expl.spec]. */
2061 tree tmpl = most_specialized_instantiation (templates);
2062 if (tmpl != error_mark_node)
2065 TREE_CHAIN (templates) = NULL_TREE;
2069 if (templates == NULL_TREE && candidates == NULL_TREE)
2071 error ("template-id %qD for %q+D does not match any template "
2072 "declaration", template_id, decl);
2073 if (header_count && header_count != template_count + 1)
2074 inform (input_location, "saw %d %<template<>%>, need %d for "
2075 "specializing a member function template",
2076 header_count, template_count + 1);
2077 return error_mark_node;
2079 else if ((templates && TREE_CHAIN (templates))
2080 || (candidates && TREE_CHAIN (candidates))
2081 || (templates && candidates))
2083 error ("ambiguous template specialization %qD for %q+D",
2085 candidates = chainon (candidates, templates);
2086 print_candidates (candidates);
2087 return error_mark_node;
2090 /* We have one, and exactly one, match. */
2093 tree fn = TREE_VALUE (candidates);
2094 *targs_out = copy_node (DECL_TI_ARGS (fn));
2095 /* DECL is a re-declaration or partial instantiation of a template
2097 if (TREE_CODE (fn) == TEMPLATE_DECL)
2099 /* It was a specialization of an ordinary member function in a
2101 return DECL_TI_TEMPLATE (fn);
2104 /* It was a specialization of a template. */
2105 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2106 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2108 *targs_out = copy_node (targs);
2109 SET_TMPL_ARGS_LEVEL (*targs_out,
2110 TMPL_ARGS_DEPTH (*targs_out),
2111 TREE_PURPOSE (templates));
2114 *targs_out = TREE_PURPOSE (templates);
2115 return TREE_VALUE (templates);
2118 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2119 but with the default argument values filled in from those in the
2123 copy_default_args_to_explicit_spec_1 (tree spec_types,
2126 tree new_spec_types;
2131 if (spec_types == void_list_node)
2132 return void_list_node;
2134 /* Substitute into the rest of the list. */
2136 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2137 TREE_CHAIN (tmpl_types));
2139 /* Add the default argument for this parameter. */
2140 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2141 TREE_VALUE (spec_types),
2145 /* DECL is an explicit specialization. Replicate default arguments
2146 from the template it specializes. (That way, code like:
2148 template <class T> void f(T = 3);
2149 template <> void f(double);
2152 works, as required.) An alternative approach would be to look up
2153 the correct default arguments at the call-site, but this approach
2154 is consistent with how implicit instantiations are handled. */
2157 copy_default_args_to_explicit_spec (tree decl)
2162 tree new_spec_types;
2166 tree object_type = NULL_TREE;
2167 tree in_charge = NULL_TREE;
2168 tree vtt = NULL_TREE;
2170 /* See if there's anything we need to do. */
2171 tmpl = DECL_TI_TEMPLATE (decl);
2172 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2173 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2174 if (TREE_PURPOSE (t))
2179 old_type = TREE_TYPE (decl);
2180 spec_types = TYPE_ARG_TYPES (old_type);
2182 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2184 /* Remove the this pointer, but remember the object's type for
2186 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2187 spec_types = TREE_CHAIN (spec_types);
2188 tmpl_types = TREE_CHAIN (tmpl_types);
2190 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2192 /* DECL may contain more parameters than TMPL due to the extra
2193 in-charge parameter in constructors and destructors. */
2194 in_charge = spec_types;
2195 spec_types = TREE_CHAIN (spec_types);
2197 if (DECL_HAS_VTT_PARM_P (decl))
2200 spec_types = TREE_CHAIN (spec_types);
2204 /* Compute the merged default arguments. */
2206 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2208 /* Compute the new FUNCTION_TYPE. */
2212 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2217 /* Put the in-charge parameter back. */
2218 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2219 TREE_VALUE (in_charge),
2222 new_type = build_method_type_directly (object_type,
2223 TREE_TYPE (old_type),
2227 new_type = build_function_type (TREE_TYPE (old_type),
2229 new_type = cp_build_type_attribute_variant (new_type,
2230 TYPE_ATTRIBUTES (old_type));
2231 new_type = build_exception_variant (new_type,
2232 TYPE_RAISES_EXCEPTIONS (old_type));
2233 TREE_TYPE (decl) = new_type;
2236 /* Check to see if the function just declared, as indicated in
2237 DECLARATOR, and in DECL, is a specialization of a function
2238 template. We may also discover that the declaration is an explicit
2239 instantiation at this point.
2241 Returns DECL, or an equivalent declaration that should be used
2242 instead if all goes well. Issues an error message if something is
2243 amiss. Returns error_mark_node if the error is not easily
2246 FLAGS is a bitmask consisting of the following flags:
2248 2: The function has a definition.
2249 4: The function is a friend.
2251 The TEMPLATE_COUNT is the number of references to qualifying
2252 template classes that appeared in the name of the function. For
2255 template <class T> struct S { void f(); };
2258 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2259 classes are not counted in the TEMPLATE_COUNT, so that in
2261 template <class T> struct S {};
2262 template <> struct S<int> { void f(); }
2263 template <> void S<int>::f();
2265 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2266 invalid; there should be no template <>.)
2268 If the function is a specialization, it is marked as such via
2269 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2270 is set up correctly, and it is added to the list of specializations
2271 for that template. */
2274 check_explicit_specialization (tree declarator,
2279 int have_def = flags & 2;
2280 int is_friend = flags & 4;
2281 int specialization = 0;
2282 int explicit_instantiation = 0;
2283 int member_specialization = 0;
2284 tree ctype = DECL_CLASS_CONTEXT (decl);
2285 tree dname = DECL_NAME (decl);
2290 if (!processing_specialization)
2293 tsk = tsk_excessive_parms;
2296 tsk = current_tmpl_spec_kind (template_count);
2301 if (processing_specialization)
2304 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2306 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2309 /* This could be something like:
2311 template <class T> void f(T);
2312 class S { friend void f<>(int); } */
2316 /* This case handles bogus declarations like template <>
2317 template <class T> void f<int>(); */
2319 error ("template-id %qD in declaration of primary template",
2326 case tsk_invalid_member_spec:
2327 /* The error has already been reported in
2328 check_specialization_scope. */
2329 return error_mark_node;
2331 case tsk_invalid_expl_inst:
2332 error ("template parameter list used in explicit instantiation");
2338 error ("definition provided for explicit instantiation");
2340 explicit_instantiation = 1;
2343 case tsk_excessive_parms:
2344 case tsk_insufficient_parms:
2345 if (tsk == tsk_excessive_parms)
2346 error ("too many template parameter lists in declaration of %qD",
2348 else if (template_header_count)
2349 error("too few template parameter lists in declaration of %qD", decl);
2351 error("explicit specialization of %qD must be introduced by "
2352 "%<template <>%>", decl);
2356 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2358 member_specialization = 1;
2364 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2366 /* This case handles bogus declarations like template <>
2367 template <class T> void f<int>(); */
2369 if (uses_template_parms (declarator))
2370 error ("function template partial specialization %qD "
2371 "is not allowed", declarator);
2373 error ("template-id %qD in declaration of primary template",
2378 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2379 /* This is a specialization of a member template, without
2380 specialization the containing class. Something like:
2382 template <class T> struct S {
2383 template <class U> void f (U);
2385 template <> template <class U> void S<int>::f(U) {}
2387 That's a specialization -- but of the entire template. */
2395 if (specialization || member_specialization)
2397 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2398 for (; t; t = TREE_CHAIN (t))
2399 if (TREE_PURPOSE (t))
2401 permerror (input_location,
2402 "default argument specified in explicit specialization");
2407 if (specialization || member_specialization || explicit_instantiation)
2409 tree tmpl = NULL_TREE;
2410 tree targs = NULL_TREE;
2412 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2413 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2417 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2422 /* If there is no class context, the explicit instantiation
2423 must be at namespace scope. */
2424 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2426 /* Find the namespace binding, using the declaration
2428 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2430 if (fns == error_mark_node || !is_overloaded_fn (fns))
2432 error ("%qD is not a template function", dname);
2433 fns = error_mark_node;
2437 tree fn = OVL_CURRENT (fns);
2438 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2439 CP_DECL_CONTEXT (fn)))
2440 error ("%qD is not declared in %qD",
2441 decl, current_namespace);
2445 declarator = lookup_template_function (fns, NULL_TREE);
2448 if (declarator == error_mark_node)
2449 return error_mark_node;
2451 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2453 if (!explicit_instantiation)
2454 /* A specialization in class scope. This is invalid,
2455 but the error will already have been flagged by
2456 check_specialization_scope. */
2457 return error_mark_node;
2460 /* It's not valid to write an explicit instantiation in
2463 class C { template void f(); }
2465 This case is caught by the parser. However, on
2468 template class C { void f(); };
2470 (which is invalid) we can get here. The error will be
2477 else if (ctype != NULL_TREE
2478 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2481 /* Find the list of functions in ctype that have the same
2482 name as the declared function. */
2483 tree name = TREE_OPERAND (declarator, 0);
2484 tree fns = NULL_TREE;
2487 if (constructor_name_p (name, ctype))
2489 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2491 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2492 : !CLASSTYPE_DESTRUCTORS (ctype))
2494 /* From [temp.expl.spec]:
2496 If such an explicit specialization for the member
2497 of a class template names an implicitly-declared
2498 special member function (clause _special_), the
2499 program is ill-formed.
2501 Similar language is found in [temp.explicit]. */
2502 error ("specialization of implicitly-declared special member function");
2503 return error_mark_node;
2506 name = is_constructor ? ctor_identifier : dtor_identifier;
2509 if (!DECL_CONV_FN_P (decl))
2511 idx = lookup_fnfields_1 (ctype, name);
2513 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2517 VEC(tree,gc) *methods;
2520 /* For a type-conversion operator, we cannot do a
2521 name-based lookup. We might be looking for `operator
2522 int' which will be a specialization of `operator T'.
2523 So, we find *all* the conversion operators, and then
2524 select from them. */
2527 methods = CLASSTYPE_METHOD_VEC (ctype);
2529 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2530 VEC_iterate (tree, methods, idx, ovl);
2533 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2534 /* There are no more conversion functions. */
2537 /* Glue all these conversion functions together
2538 with those we already have. */
2539 for (; ovl; ovl = OVL_NEXT (ovl))
2540 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2544 if (fns == NULL_TREE)
2546 error ("no member function %qD declared in %qT", name, ctype);
2547 return error_mark_node;
2550 TREE_OPERAND (declarator, 0) = fns;
2553 /* Figure out what exactly is being specialized at this point.
2554 Note that for an explicit instantiation, even one for a
2555 member function, we cannot tell apriori whether the
2556 instantiation is for a member template, or just a member
2557 function of a template class. Even if a member template is
2558 being instantiated, the member template arguments may be
2559 elided if they can be deduced from the rest of the
2561 tmpl = determine_specialization (declarator, decl,
2563 member_specialization,
2567 if (!tmpl || tmpl == error_mark_node)
2568 /* We couldn't figure out what this declaration was
2570 return error_mark_node;
2573 tree gen_tmpl = most_general_template (tmpl);
2575 if (explicit_instantiation)
2577 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2578 is done by do_decl_instantiation later. */
2580 int arg_depth = TMPL_ARGS_DEPTH (targs);
2581 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2583 if (arg_depth > parm_depth)
2585 /* If TMPL is not the most general template (for
2586 example, if TMPL is a friend template that is
2587 injected into namespace scope), then there will
2588 be too many levels of TARGS. Remove some of them
2593 new_targs = make_tree_vec (parm_depth);
2594 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2595 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2596 = TREE_VEC_ELT (targs, i);
2600 return instantiate_template (tmpl, targs, tf_error);
2603 /* If we thought that the DECL was a member function, but it
2604 turns out to be specializing a static member function,
2605 make DECL a static member function as well. */
2606 if (DECL_STATIC_FUNCTION_P (tmpl)
2607 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2608 revert_static_member_fn (decl);
2610 /* If this is a specialization of a member template of a
2611 template class, we want to return the TEMPLATE_DECL, not
2612 the specialization of it. */
2613 if (tsk == tsk_template)
2615 tree result = DECL_TEMPLATE_RESULT (tmpl);
2616 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2617 DECL_INITIAL (result) = NULL_TREE;
2621 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2622 DECL_SOURCE_LOCATION (result)
2623 = DECL_SOURCE_LOCATION (decl);
2624 /* We want to use the argument list specified in the
2625 definition, not in the original declaration. */
2626 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2627 for (parm = DECL_ARGUMENTS (result); parm;
2628 parm = DECL_CHAIN (parm))
2629 DECL_CONTEXT (parm) = result;
2631 return register_specialization (tmpl, gen_tmpl, targs,
2635 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2636 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2638 /* Inherit default function arguments from the template
2639 DECL is specializing. */
2640 copy_default_args_to_explicit_spec (decl);
2642 /* This specialization has the same protection as the
2643 template it specializes. */
2644 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2645 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2647 /* 7.1.1-1 [dcl.stc]
2649 A storage-class-specifier shall not be specified in an
2650 explicit specialization...
2652 The parser rejects these, so unless action is taken here,
2653 explicit function specializations will always appear with
2656 The action recommended by the C++ CWG in response to C++
2657 defect report 605 is to make the storage class and linkage
2658 of the explicit specialization match the templated function:
2660 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2662 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2664 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2665 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2667 /* This specialization has the same linkage and visibility as
2668 the function template it specializes. */
2669 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2670 if (! TREE_PUBLIC (decl))
2672 DECL_INTERFACE_KNOWN (decl) = 1;
2673 DECL_NOT_REALLY_EXTERN (decl) = 1;
2675 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2676 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2678 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2679 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2683 /* If DECL is a friend declaration, declared using an
2684 unqualified name, the namespace associated with DECL may
2685 have been set incorrectly. For example, in:
2687 template <typename T> void f(T);
2689 struct S { friend void f<int>(int); }
2692 we will have set the DECL_CONTEXT for the friend
2693 declaration to N, rather than to the global namespace. */
2694 if (DECL_NAMESPACE_SCOPE_P (decl))
2695 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2697 if (is_friend && !have_def)
2698 /* This is not really a declaration of a specialization.
2699 It's just the name of an instantiation. But, it's not
2700 a request for an instantiation, either. */
2701 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2702 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2703 /* This is indeed a specialization. In case of constructors
2704 and destructors, we need in-charge and not-in-charge
2705 versions in V3 ABI. */
2706 clone_function_decl (decl, /*update_method_vec_p=*/0);
2708 /* Register this specialization so that we can find it
2710 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2717 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2718 parameters. These are represented in the same format used for
2719 DECL_TEMPLATE_PARMS. */
2722 comp_template_parms (const_tree parms1, const_tree parms2)
2727 if (parms1 == parms2)
2730 for (p1 = parms1, p2 = parms2;
2731 p1 != NULL_TREE && p2 != NULL_TREE;
2732 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2734 tree t1 = TREE_VALUE (p1);
2735 tree t2 = TREE_VALUE (p2);
2738 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2739 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2741 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2744 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2746 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2747 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2749 /* If either of the template parameters are invalid, assume
2750 they match for the sake of error recovery. */
2751 if (parm1 == error_mark_node || parm2 == error_mark_node)
2754 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2757 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2758 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2759 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2761 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2766 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2767 /* One set of parameters has more parameters lists than the
2774 /* Determine whether PARM is a parameter pack. */
2777 template_parameter_pack_p (const_tree parm)
2779 /* Determine if we have a non-type template parameter pack. */
2780 if (TREE_CODE (parm) == PARM_DECL)
2781 return (DECL_TEMPLATE_PARM_P (parm)
2782 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2783 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
2784 return TEMPLATE_PARM_PARAMETER_PACK (parm);
2786 /* If this is a list of template parameters, we could get a
2787 TYPE_DECL or a TEMPLATE_DECL. */
2788 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2789 parm = TREE_TYPE (parm);
2791 /* Otherwise it must be a type template parameter. */
2792 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2793 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2794 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2797 /* Determine if T is a function parameter pack. */
2800 function_parameter_pack_p (const_tree t)
2802 if (t && TREE_CODE (t) == PARM_DECL)
2803 return FUNCTION_PARAMETER_PACK_P (t);
2807 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2808 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2811 get_function_template_decl (const_tree primary_func_tmpl_inst)
2813 if (! primary_func_tmpl_inst
2814 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2815 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2818 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2821 /* Return true iff the function parameter PARAM_DECL was expanded
2822 from the function parameter pack PACK. */
2825 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2827 if (DECL_ARTIFICIAL (param_decl)
2828 || !function_parameter_pack_p (pack))
2831 /* The parameter pack and its pack arguments have the same
2833 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2836 /* Determine whether ARGS describes a variadic template args list,
2837 i.e., one that is terminated by a template argument pack. */
2840 template_args_variadic_p (tree args)
2845 if (args == NULL_TREE)
2848 args = INNERMOST_TEMPLATE_ARGS (args);
2849 nargs = TREE_VEC_LENGTH (args);
2854 last_parm = TREE_VEC_ELT (args, nargs - 1);
2856 return ARGUMENT_PACK_P (last_parm);
2859 /* Generate a new name for the parameter pack name NAME (an
2860 IDENTIFIER_NODE) that incorporates its */
2863 make_ith_pack_parameter_name (tree name, int i)
2865 /* Munge the name to include the parameter index. */
2866 #define NUMBUF_LEN 128
2867 char numbuf[NUMBUF_LEN];
2871 if (name == NULL_TREE)
2873 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2874 newname_len = IDENTIFIER_LENGTH (name)
2875 + strlen (numbuf) + 2;
2876 newname = (char*)alloca (newname_len);
2877 snprintf (newname, newname_len,
2878 "%s#%i", IDENTIFIER_POINTER (name), i);
2879 return get_identifier (newname);
2882 /* Return true if T is a primary function, class or alias template
2886 primary_template_instantiation_p (const_tree t)
2891 if (TREE_CODE (t) == FUNCTION_DECL)
2892 return DECL_LANG_SPECIFIC (t)
2893 && DECL_TEMPLATE_INSTANTIATION (t)
2894 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2895 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
2896 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2897 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2899 && TYPE_TEMPLATE_INFO (t)
2900 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
2901 && DECL_TEMPLATE_INSTANTIATION (TYPE_NAME (t)))
2906 /* Return true if PARM is a template template parameter. */
2909 template_template_parameter_p (const_tree parm)
2911 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2914 /* Return the template parameters of T if T is a
2915 primary template instantiation, NULL otherwise. */
2918 get_primary_template_innermost_parameters (const_tree t)
2920 tree parms = NULL, template_info = NULL;
2922 if ((template_info = get_template_info (t))
2923 && primary_template_instantiation_p (t))
2924 parms = INNERMOST_TEMPLATE_PARMS
2925 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2930 /* Return the template parameters of the LEVELth level from the full list
2931 of template parameters PARMS. */
2934 get_template_parms_at_level (tree parms, int level)
2938 || TREE_CODE (parms) != TREE_LIST
2939 || level > TMPL_PARMS_DEPTH (parms))
2942 for (p = parms; p; p = TREE_CHAIN (p))
2943 if (TMPL_PARMS_DEPTH (p) == level)
2949 /* Returns the template arguments of T if T is a template instantiation,
2953 get_template_innermost_arguments (const_tree t)
2955 tree args = NULL, template_info = NULL;
2957 if ((template_info = get_template_info (t))
2958 && TI_ARGS (template_info))
2959 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2964 /* Return the argument pack elements of T if T is a template argument pack,
2968 get_template_argument_pack_elems (const_tree t)
2970 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2971 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2974 return ARGUMENT_PACK_ARGS (t);
2977 /* Structure used to track the progress of find_parameter_packs_r. */
2978 struct find_parameter_pack_data
2980 /* TREE_LIST that will contain all of the parameter packs found by
2982 tree* parameter_packs;
2984 /* Set of AST nodes that have been visited by the traversal. */
2985 struct pointer_set_t *visited;
2988 /* Identifies all of the argument packs that occur in a template
2989 argument and appends them to the TREE_LIST inside DATA, which is a
2990 find_parameter_pack_data structure. This is a subroutine of
2991 make_pack_expansion and uses_parameter_packs. */
2993 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2996 struct find_parameter_pack_data* ppd =
2997 (struct find_parameter_pack_data*)data;
2998 bool parameter_pack_p = false;
3000 /* Handle type aliases/typedefs. */
3003 && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
3004 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3006 if (TYPE_TEMPLATE_INFO (t))
3007 cp_walk_tree (&TYPE_TI_ARGS (t),
3008 &find_parameter_packs_r,
3014 /* Identify whether this is a parameter pack or not. */
3015 switch (TREE_CODE (t))
3017 case TEMPLATE_PARM_INDEX:
3018 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3019 parameter_pack_p = true;
3022 case TEMPLATE_TYPE_PARM:
3023 t = TYPE_MAIN_VARIANT (t);
3024 case TEMPLATE_TEMPLATE_PARM:
3025 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3026 parameter_pack_p = true;
3030 if (FUNCTION_PARAMETER_PACK_P (t))
3032 /* We don't want to walk into the type of a PARM_DECL,
3033 because we don't want to see the type parameter pack. */
3035 parameter_pack_p = true;
3040 parameter_pack_p = true;
3043 /* Not a parameter pack. */
3047 if (parameter_pack_p)
3049 /* Add this parameter pack to the list. */
3050 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3054 cp_walk_tree (&TYPE_CONTEXT (t),
3055 &find_parameter_packs_r, ppd, ppd->visited);
3057 /* This switch statement will return immediately if we don't find a
3059 switch (TREE_CODE (t))
3061 case TEMPLATE_PARM_INDEX:
3064 case BOUND_TEMPLATE_TEMPLATE_PARM:
3065 /* Check the template itself. */
3066 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3067 &find_parameter_packs_r, ppd, ppd->visited);
3068 /* Check the template arguments. */
3069 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3074 case TEMPLATE_TYPE_PARM:
3075 case TEMPLATE_TEMPLATE_PARM:
3082 if (TYPE_PTRMEMFUNC_P (t))
3088 if (TYPE_TEMPLATE_INFO (t))
3089 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
3090 &find_parameter_packs_r, ppd, ppd->visited);
3097 cp_walk_tree (&TREE_TYPE (t),
3098 &find_parameter_packs_r, ppd, ppd->visited);
3102 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3107 case TYPE_PACK_EXPANSION:
3108 case EXPR_PACK_EXPANSION:
3113 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3118 case IDENTIFIER_NODE:
3119 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3131 /* Determines if the expression or type T uses any parameter packs. */
3133 uses_parameter_packs (tree t)
3135 tree parameter_packs = NULL_TREE;
3136 struct find_parameter_pack_data ppd;
3137 ppd.parameter_packs = ¶meter_packs;
3138 ppd.visited = pointer_set_create ();
3139 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3140 pointer_set_destroy (ppd.visited);
3141 return parameter_packs != NULL_TREE;
3144 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3145 representation a base-class initializer into a parameter pack
3146 expansion. If all goes well, the resulting node will be an
3147 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3150 make_pack_expansion (tree arg)
3153 tree parameter_packs = NULL_TREE;
3154 bool for_types = false;
3155 struct find_parameter_pack_data ppd;
3157 if (!arg || arg == error_mark_node)
3160 if (TREE_CODE (arg) == TREE_LIST)
3162 /* The only time we will see a TREE_LIST here is for a base
3163 class initializer. In this case, the TREE_PURPOSE will be a
3164 _TYPE node (representing the base class expansion we're
3165 initializing) and the TREE_VALUE will be a TREE_LIST
3166 containing the initialization arguments.
3168 The resulting expansion looks somewhat different from most
3169 expansions. Rather than returning just one _EXPANSION, we
3170 return a TREE_LIST whose TREE_PURPOSE is a
3171 TYPE_PACK_EXPANSION containing the bases that will be
3172 initialized. The TREE_VALUE will be identical to the
3173 original TREE_VALUE, which is a list of arguments that will
3174 be passed to each base. We do not introduce any new pack
3175 expansion nodes into the TREE_VALUE (although it is possible
3176 that some already exist), because the TREE_PURPOSE and
3177 TREE_VALUE all need to be expanded together with the same
3178 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3179 resulting TREE_PURPOSE will mention the parameter packs in
3180 both the bases and the arguments to the bases. */
3183 tree parameter_packs = NULL_TREE;
3185 /* Determine which parameter packs will be used by the base
3187 ppd.visited = pointer_set_create ();
3188 ppd.parameter_packs = ¶meter_packs;
3189 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3192 if (parameter_packs == NULL_TREE)
3194 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3195 pointer_set_destroy (ppd.visited);
3196 return error_mark_node;
3199 if (TREE_VALUE (arg) != void_type_node)
3201 /* Collect the sets of parameter packs used in each of the
3202 initialization arguments. */
3203 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3205 /* Determine which parameter packs will be expanded in this
3207 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3212 pointer_set_destroy (ppd.visited);
3214 /* Create the pack expansion type for the base type. */
3215 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3216 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3217 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3219 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3220 they will rarely be compared to anything. */
3221 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3223 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3226 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3229 /* Build the PACK_EXPANSION_* node. */
3231 ? cxx_make_type (TYPE_PACK_EXPANSION)
3232 : make_node (EXPR_PACK_EXPANSION);
3233 SET_PACK_EXPANSION_PATTERN (result, arg);
3234 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3236 /* Propagate type and const-expression information. */
3237 TREE_TYPE (result) = TREE_TYPE (arg);
3238 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3241 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3242 they will rarely be compared to anything. */
3243 SET_TYPE_STRUCTURAL_EQUALITY (result);
3245 /* Determine which parameter packs will be expanded. */
3246 ppd.parameter_packs = ¶meter_packs;
3247 ppd.visited = pointer_set_create ();
3248 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3249 pointer_set_destroy (ppd.visited);
3251 /* Make sure we found some parameter packs. */
3252 if (parameter_packs == NULL_TREE)
3255 error ("expansion pattern %<%T%> contains no argument packs", arg);
3257 error ("expansion pattern %<%E%> contains no argument packs", arg);
3258 return error_mark_node;
3260 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3262 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3267 /* Checks T for any "bare" parameter packs, which have not yet been
3268 expanded, and issues an error if any are found. This operation can
3269 only be done on full expressions or types (e.g., an expression
3270 statement, "if" condition, etc.), because we could have expressions like:
3272 foo(f(g(h(args)))...)
3274 where "args" is a parameter pack. check_for_bare_parameter_packs
3275 should not be called for the subexpressions args, h(args),
3276 g(h(args)), or f(g(h(args))), because we would produce erroneous
3279 Returns TRUE and emits an error if there were bare parameter packs,
3280 returns FALSE otherwise. */
3282 check_for_bare_parameter_packs (tree t)
3284 tree parameter_packs = NULL_TREE;
3285 struct find_parameter_pack_data ppd;
3287 if (!processing_template_decl || !t || t == error_mark_node)
3290 if (TREE_CODE (t) == TYPE_DECL)
3293 ppd.parameter_packs = ¶meter_packs;
3294 ppd.visited = pointer_set_create ();
3295 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3296 pointer_set_destroy (ppd.visited);
3298 if (parameter_packs)
3300 error ("parameter packs not expanded with %<...%>:");
3301 while (parameter_packs)
3303 tree pack = TREE_VALUE (parameter_packs);
3304 tree name = NULL_TREE;
3306 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3307 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3308 name = TYPE_NAME (pack);
3309 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3310 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3312 name = DECL_NAME (pack);
3315 inform (input_location, " %qD", name);
3317 inform (input_location, " <anonymous>");
3319 parameter_packs = TREE_CHAIN (parameter_packs);
3328 /* Expand any parameter packs that occur in the template arguments in
3331 expand_template_argument_pack (tree args)
3333 tree result_args = NULL_TREE;
3334 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3335 int num_result_args = -1;
3336 int non_default_args_count = -1;
3338 /* First, determine if we need to expand anything, and the number of
3339 slots we'll need. */
3340 for (in_arg = 0; in_arg < nargs; ++in_arg)
3342 tree arg = TREE_VEC_ELT (args, in_arg);
3343 if (arg == NULL_TREE)
3345 if (ARGUMENT_PACK_P (arg))
3347 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3348 if (num_result_args < 0)
3349 num_result_args = in_arg + num_packed;
3351 num_result_args += num_packed;
3355 if (num_result_args >= 0)
3360 /* If no expansion is necessary, we're done. */
3361 if (num_result_args < 0)
3364 /* Expand arguments. */
3365 result_args = make_tree_vec (num_result_args);
3366 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3367 non_default_args_count =
3368 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3369 for (in_arg = 0; in_arg < nargs; ++in_arg)
3371 tree arg = TREE_VEC_ELT (args, in_arg);
3372 if (ARGUMENT_PACK_P (arg))
3374 tree packed = ARGUMENT_PACK_ARGS (arg);
3375 int i, num_packed = TREE_VEC_LENGTH (packed);
3376 for (i = 0; i < num_packed; ++i, ++out_arg)
3377 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3378 if (non_default_args_count > 0)
3379 non_default_args_count += num_packed;
3383 TREE_VEC_ELT (result_args, out_arg) = arg;
3387 if (non_default_args_count >= 0)
3388 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3392 /* Checks if DECL shadows a template parameter.
3394 [temp.local]: A template-parameter shall not be redeclared within its
3395 scope (including nested scopes).
3397 Emits an error and returns TRUE if the DECL shadows a parameter,
3398 returns FALSE otherwise. */
3401 check_template_shadow (tree decl)
3405 /* If we're not in a template, we can't possibly shadow a template
3407 if (!current_template_parms)
3410 /* Figure out what we're shadowing. */
3411 if (TREE_CODE (decl) == OVERLOAD)
3412 decl = OVL_CURRENT (decl);
3413 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3415 /* If there's no previous binding for this name, we're not shadowing
3416 anything, let alone a template parameter. */
3420 /* If we're not shadowing a template parameter, we're done. Note
3421 that OLDDECL might be an OVERLOAD (or perhaps even an
3422 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3424 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3427 /* We check for decl != olddecl to avoid bogus errors for using a
3428 name inside a class. We check TPFI to avoid duplicate errors for
3429 inline member templates. */
3431 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3434 error ("declaration of %q+#D", decl);
3435 error (" shadows template parm %q+#D", olddecl);
3439 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3440 ORIG_LEVEL, DECL, and TYPE. */
3443 build_template_parm_index (int index,
3449 tree t = make_node (TEMPLATE_PARM_INDEX);
3450 TEMPLATE_PARM_IDX (t) = index;
3451 TEMPLATE_PARM_LEVEL (t) = level;
3452 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3453 TEMPLATE_PARM_DECL (t) = decl;
3454 TREE_TYPE (t) = type;
3455 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3456 TREE_READONLY (t) = TREE_READONLY (decl);
3461 /* Find the canonical type parameter for the given template type
3462 parameter. Returns the canonical type parameter, which may be TYPE
3463 if no such parameter existed. */
3466 canonical_type_parameter (tree type)
3469 int idx = TEMPLATE_TYPE_IDX (type);
3470 if (!canonical_template_parms)
3471 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3473 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3474 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3476 list = VEC_index (tree, canonical_template_parms, idx);
3477 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3478 list = TREE_CHAIN (list);
3481 return TREE_VALUE (list);
3484 VEC_replace(tree, canonical_template_parms, idx,
3485 tree_cons (NULL_TREE, type,
3486 VEC_index (tree, canonical_template_parms, idx)));
3491 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3492 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3493 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3494 new one is created. */
3497 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3498 tsubst_flags_t complain)
3500 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3501 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3502 != TEMPLATE_PARM_LEVEL (index) - levels)
3503 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3505 tree orig_decl = TEMPLATE_PARM_DECL (index);
3508 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3509 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3510 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3511 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3512 DECL_ARTIFICIAL (decl) = 1;
3513 SET_DECL_TEMPLATE_PARM_P (decl);
3515 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3516 TEMPLATE_PARM_LEVEL (index) - levels,
3517 TEMPLATE_PARM_ORIG_LEVEL (index),
3519 TEMPLATE_PARM_DESCENDANTS (index) = t;
3520 TEMPLATE_PARM_PARAMETER_PACK (t)
3521 = TEMPLATE_PARM_PARAMETER_PACK (index);
3523 /* Template template parameters need this. */
3524 if (TREE_CODE (decl) == TEMPLATE_DECL)
3525 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3526 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3530 return TEMPLATE_PARM_DESCENDANTS (index);
3533 /* Process information from new template parameter PARM and append it
3534 to the LIST being built. This new parameter is a non-type
3535 parameter iff IS_NON_TYPE is true. This new parameter is a
3536 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3537 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3538 parameter list PARM belongs to. This is used used to create a
3539 proper canonical type for the type of PARM that is to be created,
3540 iff PARM is a type. If the size is not known, this parameter shall
3544 process_template_parm (tree list, location_t parm_loc, tree parm,
3545 bool is_non_type, bool is_parameter_pack)
3552 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3553 defval = TREE_PURPOSE (parm);
3557 tree p = tree_last (list);
3559 if (p && TREE_VALUE (p) != error_mark_node)
3562 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3563 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3565 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3575 parm = TREE_VALUE (parm);
3577 SET_DECL_TEMPLATE_PARM_P (parm);
3579 if (TREE_TYPE (parm) == error_mark_node)
3581 err_parm_list = build_tree_list (defval, parm);
3582 TREE_VALUE (err_parm_list) = error_mark_node;
3583 return chainon (list, err_parm_list);
3589 The top-level cv-qualifiers on the template-parameter are
3590 ignored when determining its type. */
3591 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3592 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3594 err_parm_list = build_tree_list (defval, parm);
3595 TREE_VALUE (err_parm_list) = error_mark_node;
3596 return chainon (list, err_parm_list);
3599 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3601 /* This template parameter is not a parameter pack, but it
3602 should be. Complain about "bare" parameter packs. */
3603 check_for_bare_parameter_packs (TREE_TYPE (parm));
3605 /* Recover by calling this a parameter pack. */
3606 is_parameter_pack = true;
3610 /* A template parameter is not modifiable. */
3611 TREE_CONSTANT (parm) = 1;
3612 TREE_READONLY (parm) = 1;
3613 decl = build_decl (parm_loc,
3614 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3615 TREE_CONSTANT (decl) = 1;
3616 TREE_READONLY (decl) = 1;
3617 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3618 = build_template_parm_index (idx, processing_template_decl,
3619 processing_template_decl,
3620 decl, TREE_TYPE (parm));
3622 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3623 = is_parameter_pack;
3628 parm = TREE_VALUE (TREE_VALUE (parm));
3630 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3632 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3633 /* This is for distinguishing between real templates and template
3634 template parameters */
3635 TREE_TYPE (parm) = t;
3636 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3641 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3642 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3643 decl = build_decl (parm_loc,
3644 TYPE_DECL, parm, t);
3647 TYPE_NAME (t) = decl;
3648 TYPE_STUB_DECL (t) = decl;
3650 TEMPLATE_TYPE_PARM_INDEX (t)
3651 = build_template_parm_index (idx, processing_template_decl,
3652 processing_template_decl,
3653 decl, TREE_TYPE (parm));
3654 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3655 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3657 DECL_ARTIFICIAL (decl) = 1;
3658 SET_DECL_TEMPLATE_PARM_P (decl);
3660 parm = build_tree_list (defval, parm);
3661 return chainon (list, parm);
3664 /* The end of a template parameter list has been reached. Process the
3665 tree list into a parameter vector, converting each parameter into a more
3666 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3670 end_template_parm_list (tree parms)
3674 tree saved_parmlist = make_tree_vec (list_length (parms));
3676 current_template_parms
3677 = tree_cons (size_int (processing_template_decl),
3678 saved_parmlist, current_template_parms);
3680 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3682 next = TREE_CHAIN (parm);
3683 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3684 TREE_CHAIN (parm) = NULL_TREE;
3687 --processing_template_parmlist;
3689 return saved_parmlist;
3692 /* end_template_decl is called after a template declaration is seen. */
3695 end_template_decl (void)
3697 reset_specialization ();
3699 if (! processing_template_decl)
3702 /* This matches the pushlevel in begin_template_parm_list. */
3705 --processing_template_decl;
3706 current_template_parms = TREE_CHAIN (current_template_parms);
3709 /* Takes a TREE_LIST representing a template parameter and convert it
3710 into an argument suitable to be passed to the type substitution
3711 functions. Note that If the TREE_LIST contains an error_mark
3712 node, the returned argument is error_mark_node. */
3715 template_parm_to_arg (tree t)
3719 || TREE_CODE (t) != TREE_LIST)
3722 if (error_operand_p (TREE_VALUE (t)))
3723 return error_mark_node;
3727 if (TREE_CODE (t) == TYPE_DECL
3728 || TREE_CODE (t) == TEMPLATE_DECL)
3732 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3734 /* Turn this argument into a TYPE_ARGUMENT_PACK
3735 with a single element, which expands T. */
3736 tree vec = make_tree_vec (1);
3737 #ifdef ENABLE_CHECKING
3738 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3739 (vec, TREE_VEC_LENGTH (vec));
3741 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3743 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3744 SET_ARGUMENT_PACK_ARGS (t, vec);
3749 t = DECL_INITIAL (t);
3751 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3753 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3754 with a single element, which expands T. */
3755 tree vec = make_tree_vec (1);
3756 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3757 #ifdef ENABLE_CHECKING
3758 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3759 (vec, TREE_VEC_LENGTH (vec));
3761 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3763 t = make_node (NONTYPE_ARGUMENT_PACK);
3764 SET_ARGUMENT_PACK_ARGS (t, vec);
3765 TREE_TYPE (t) = type;
3771 /* This function returns TRUE if PARM_PACK is a template parameter
3772 pack and if ARG_PACK is what template_parm_to_arg returned when
3773 passed PARM_PACK. */
3776 arg_from_parm_pack_p (tree arg_pack, tree parm_pack)
3778 /* For clarity in the comments below let's use the representation
3779 argument_pack<elements>' to denote an argument pack and its
3782 In the 'if' block below, we want to detect cases where
3783 ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to
3784 check if ARG_PACK is an argument pack which sole element is
3785 the expansion of PARM_PACK. That argument pack is typically
3786 created by template_parm_to_arg when passed a parameter
3790 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
3791 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
3793 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
3794 tree pattern = PACK_EXPANSION_PATTERN (expansion);
3795 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
3796 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
3797 /* The argument pack that the parameter maps to is just an
3798 expansion of the parameter itself, such as one would
3799 find in the implicit typedef of a class inside the
3800 class itself. Consider this parameter "unsubstituted",
3801 so that we will maintain the outer pack expansion. */
3807 /* Within the declaration of a template, return all levels of template
3808 parameters that apply. The template parameters are represented as
3809 a TREE_VEC, in the form documented in cp-tree.h for template
3813 current_template_args (void)
3816 tree args = NULL_TREE;
3817 int length = TMPL_PARMS_DEPTH (current_template_parms);
3820 /* If there is only one level of template parameters, we do not
3821 create a TREE_VEC of TREE_VECs. Instead, we return a single
3822 TREE_VEC containing the arguments. */
3824 args = make_tree_vec (length);
3826 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3828 tree a = copy_node (TREE_VALUE (header));
3831 TREE_TYPE (a) = NULL_TREE;
3832 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3833 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
3835 #ifdef ENABLE_CHECKING
3836 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
3840 TREE_VEC_ELT (args, --l) = a;
3845 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
3846 /* This can happen for template parms of a template template
3849 template<template<class T, class U> class TT> struct S;
3851 Consider the level of the parms of TT; T and U both have
3852 level 2; TT has no template parm of level 1. So in this case
3853 the first element of full_template_args is NULL_TREE. If we
3854 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
3855 of 2. This will make tsubst wrongly consider that T and U
3856 have level 1. Instead, let's create a dummy vector as the
3857 first element of full_template_args so that TMPL_ARG_DEPTH
3858 returns the correct depth for args. */
3859 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
3863 /* Update the declared TYPE by doing any lookups which were thought to be
3864 dependent, but are not now that we know the SCOPE of the declarator. */
3867 maybe_update_decl_type (tree orig_type, tree scope)
3869 tree type = orig_type;
3871 if (type == NULL_TREE)
3874 if (TREE_CODE (orig_type) == TYPE_DECL)
3875 type = TREE_TYPE (type);
3877 if (scope && TYPE_P (scope) && dependent_type_p (scope)
3878 && dependent_type_p (type)
3879 /* Don't bother building up the args in this case. */
3880 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
3882 /* tsubst in the args corresponding to the template parameters,
3883 including auto if present. Most things will be unchanged, but
3884 make_typename_type and tsubst_qualified_id will resolve
3885 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3886 tree args = current_template_args ();
3887 tree auto_node = type_uses_auto (type);
3891 tree auto_vec = make_tree_vec (1);
3892 TREE_VEC_ELT (auto_vec, 0) = auto_node;
3893 args = add_to_template_args (args, auto_vec);
3895 pushed = push_scope (scope);
3896 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
3901 if (type == error_mark_node)
3904 if (TREE_CODE (orig_type) == TYPE_DECL)
3906 if (same_type_p (type, TREE_TYPE (orig_type)))
3909 type = TYPE_NAME (type);
3914 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3915 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3916 a member template. Used by push_template_decl below. */
3919 build_template_decl (tree decl, tree parms, bool member_template_p)
3921 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3922 DECL_TEMPLATE_PARMS (tmpl) = parms;
3923 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3924 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3925 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3930 struct template_parm_data
3932 /* The level of the template parameters we are currently
3936 /* The index of the specialization argument we are currently
3940 /* An array whose size is the number of template parameters. The
3941 elements are nonzero if the parameter has been used in any one
3942 of the arguments processed so far. */
3945 /* An array whose size is the number of template arguments. The
3946 elements are nonzero if the argument makes use of template
3947 parameters of this level. */
3948 int* arg_uses_template_parms;
3951 /* Subroutine of push_template_decl used to see if each template
3952 parameter in a partial specialization is used in the explicit
3953 argument list. If T is of the LEVEL given in DATA (which is
3954 treated as a template_parm_data*), then DATA->PARMS is marked
3958 mark_template_parm (tree t, void* data)
3962 struct template_parm_data* tpd = (struct template_parm_data*) data;
3964 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3966 level = TEMPLATE_PARM_LEVEL (t);
3967 idx = TEMPLATE_PARM_IDX (t);
3971 level = TEMPLATE_TYPE_LEVEL (t);
3972 idx = TEMPLATE_TYPE_IDX (t);
3975 if (level == tpd->level)
3977 tpd->parms[idx] = 1;
3978 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3981 /* Return zero so that for_each_template_parm will continue the
3982 traversal of the tree; we want to mark *every* template parm. */
3986 /* Process the partial specialization DECL. */
3989 process_partial_specialization (tree decl)
3991 tree type = TREE_TYPE (decl);
3992 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3993 tree specargs = CLASSTYPE_TI_ARGS (type);
3994 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3995 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3998 int nargs = TREE_VEC_LENGTH (inner_args);
4001 bool did_error_intro = false;
4002 struct template_parm_data tpd;
4003 struct template_parm_data tpd2;
4005 gcc_assert (current_template_parms);
4007 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4008 ntparms = TREE_VEC_LENGTH (inner_parms);
4010 /* We check that each of the template parameters given in the
4011 partial specialization is used in the argument list to the
4012 specialization. For example:
4014 template <class T> struct S;
4015 template <class T> struct S<T*>;
4017 The second declaration is OK because `T*' uses the template
4018 parameter T, whereas
4020 template <class T> struct S<int>;
4022 is no good. Even trickier is:
4033 The S2<T> declaration is actually invalid; it is a
4034 full-specialization. Of course,
4037 struct S2<T (*)(U)>;
4039 or some such would have been OK. */
4040 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4041 tpd.parms = XALLOCAVEC (int, ntparms);
4042 memset (tpd.parms, 0, sizeof (int) * ntparms);
4044 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4045 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4046 for (i = 0; i < nargs; ++i)
4048 tpd.current_arg = i;
4049 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4050 &mark_template_parm,
4053 /*include_nondeduced_p=*/false);
4055 for (i = 0; i < ntparms; ++i)
4056 if (tpd.parms[i] == 0)
4058 /* One of the template parms was not used in the
4060 if (!did_error_intro)
4062 error ("template parameters not used in partial specialization:");
4063 did_error_intro = true;
4066 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4069 if (did_error_intro)
4070 return error_mark_node;
4072 /* [temp.class.spec]
4074 The argument list of the specialization shall not be identical to
4075 the implicit argument list of the primary template. */
4076 if (comp_template_args
4078 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4080 error ("partial specialization %qT does not specialize any template arguments", type);
4082 /* [temp.class.spec]
4084 A partially specialized non-type argument expression shall not
4085 involve template parameters of the partial specialization except
4086 when the argument expression is a simple identifier.
4088 The type of a template parameter corresponding to a specialized
4089 non-type argument shall not be dependent on a parameter of the
4092 Also, we verify that pack expansions only occur at the
4093 end of the argument list. */
4094 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4096 for (i = 0; i < nargs; ++i)
4098 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4099 tree arg = TREE_VEC_ELT (inner_args, i);
4100 tree packed_args = NULL_TREE;
4103 if (ARGUMENT_PACK_P (arg))
4105 /* Extract the arguments from the argument pack. We'll be
4106 iterating over these in the following loop. */
4107 packed_args = ARGUMENT_PACK_ARGS (arg);
4108 len = TREE_VEC_LENGTH (packed_args);
4111 for (j = 0; j < len; j++)
4114 /* Get the Jth argument in the parameter pack. */
4115 arg = TREE_VEC_ELT (packed_args, j);
4117 if (PACK_EXPANSION_P (arg))
4119 /* Pack expansions must come at the end of the
4121 if ((packed_args && j < len - 1)
4122 || (!packed_args && i < nargs - 1))
4124 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4125 error ("parameter pack argument %qE must be at the "
4126 "end of the template argument list", arg);
4128 error ("parameter pack argument %qT must be at the "
4129 "end of the template argument list", arg);
4133 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4134 /* We only care about the pattern. */
4135 arg = PACK_EXPANSION_PATTERN (arg);
4137 if (/* These first two lines are the `non-type' bit. */
4139 && TREE_CODE (arg) != TEMPLATE_DECL
4140 /* This next line is the `argument expression is not just a
4141 simple identifier' condition and also the `specialized
4142 non-type argument' bit. */
4143 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4145 if ((!packed_args && tpd.arg_uses_template_parms[i])
4146 || (packed_args && uses_template_parms (arg)))
4147 error ("template argument %qE involves template parameter(s)",
4151 /* Look at the corresponding template parameter,
4152 marking which template parameters its type depends
4154 tree type = TREE_TYPE (parm);
4158 /* We haven't yet initialized TPD2. Do so now. */
4159 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4160 /* The number of parameters here is the number in the
4161 main template, which, as checked in the assertion
4163 tpd2.parms = XALLOCAVEC (int, nargs);
4165 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4168 /* Mark the template parameters. But this time, we're
4169 looking for the template parameters of the main
4170 template, not in the specialization. */
4171 tpd2.current_arg = i;
4172 tpd2.arg_uses_template_parms[i] = 0;
4173 memset (tpd2.parms, 0, sizeof (int) * nargs);
4174 for_each_template_parm (type,
4175 &mark_template_parm,
4178 /*include_nondeduced_p=*/false);
4180 if (tpd2.arg_uses_template_parms [i])
4182 /* The type depended on some template parameters.
4183 If they are fully specialized in the
4184 specialization, that's OK. */
4187 for (j = 0; j < nargs; ++j)
4188 if (tpd2.parms[j] != 0
4189 && tpd.arg_uses_template_parms [j])
4192 error_n (input_location, count,
4193 "type %qT of template argument %qE depends "
4194 "on a template parameter",
4195 "type %qT of template argument %qE depends "
4196 "on template parameters",
4205 /* We should only get here once. */
4206 gcc_assert (!COMPLETE_TYPE_P (type));
4208 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4209 = tree_cons (specargs, inner_parms,
4210 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4211 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4213 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4214 inst = TREE_CHAIN (inst))
4216 tree inst_type = TREE_VALUE (inst);
4217 if (COMPLETE_TYPE_P (inst_type)
4218 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4220 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4221 if (spec && TREE_TYPE (spec) == type)
4222 permerror (input_location,
4223 "partial specialization of %qT after instantiation "
4224 "of %qT", type, inst_type);
4231 /* Check that a template declaration's use of default arguments and
4232 parameter packs is not invalid. Here, PARMS are the template
4233 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4234 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4238 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4239 declaration (but not a definition); 1 indicates a declaration, 2
4240 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4241 emitted for extraneous default arguments.
4243 Returns TRUE if there were no errors found, FALSE otherwise. */
4246 check_default_tmpl_args (tree decl, tree parms, int is_primary,
4247 int is_partial, int is_friend_decl)
4250 int last_level_to_check;
4252 bool no_errors = true;
4256 A default template-argument shall not be specified in a
4257 function template declaration or a function template definition, nor
4258 in the template-parameter-list of the definition of a member of a
4261 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4262 /* You can't have a function template declaration in a local
4263 scope, nor you can you define a member of a class template in a
4267 if (current_class_type
4268 && !TYPE_BEING_DEFINED (current_class_type)
4269 && DECL_LANG_SPECIFIC (decl)
4270 && DECL_DECLARES_FUNCTION_P (decl)
4271 /* If this is either a friend defined in the scope of the class
4272 or a member function. */
4273 && (DECL_FUNCTION_MEMBER_P (decl)
4274 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4275 : DECL_FRIEND_CONTEXT (decl)
4276 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4278 /* And, if it was a member function, it really was defined in
4279 the scope of the class. */
4280 && (!DECL_FUNCTION_MEMBER_P (decl)
4281 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4282 /* We already checked these parameters when the template was
4283 declared, so there's no need to do it again now. This function
4284 was defined in class scope, but we're processing it's body now
4285 that the class is complete. */
4288 /* Core issue 226 (C++0x only): the following only applies to class
4290 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
4294 If a template-parameter has a default template-argument, all
4295 subsequent template-parameters shall have a default
4296 template-argument supplied. */
4297 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4299 tree inner_parms = TREE_VALUE (parm_level);
4300 int ntparms = TREE_VEC_LENGTH (inner_parms);
4301 int seen_def_arg_p = 0;
4304 for (i = 0; i < ntparms; ++i)
4306 tree parm = TREE_VEC_ELT (inner_parms, i);
4308 if (parm == error_mark_node)
4311 if (TREE_PURPOSE (parm))
4313 else if (seen_def_arg_p
4314 && !template_parameter_pack_p (TREE_VALUE (parm)))
4316 error ("no default argument for %qD", TREE_VALUE (parm));
4317 /* For better subsequent error-recovery, we indicate that
4318 there should have been a default argument. */
4319 TREE_PURPOSE (parm) = error_mark_node;
4325 /* Don't complain about an enclosing partial
4327 && parm_level == parms
4328 && TREE_CODE (decl) == TYPE_DECL
4330 && template_parameter_pack_p (TREE_VALUE (parm)))
4332 /* A primary class template can only have one
4333 parameter pack, at the end of the template
4336 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4337 error ("parameter pack %qE must be at the end of the"
4338 " template parameter list", TREE_VALUE (parm));
4340 error ("parameter pack %qT must be at the end of the"
4341 " template parameter list",
4342 TREE_TYPE (TREE_VALUE (parm)));
4344 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4352 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4356 /* For an ordinary class template, default template arguments are
4357 allowed at the innermost level, e.g.:
4358 template <class T = int>
4360 but, in a partial specialization, they're not allowed even
4361 there, as we have in [temp.class.spec]:
4363 The template parameter list of a specialization shall not
4364 contain default template argument values.
4366 So, for a partial specialization, or for a function template
4367 (in C++98/C++03), we look at all of them. */
4370 /* But, for a primary class template that is not a partial
4371 specialization we look at all template parameters except the
4373 parms = TREE_CHAIN (parms);
4375 /* Figure out what error message to issue. */
4376 if (is_friend_decl == 2)
4377 msg = G_("default template arguments may not be used in function template "
4378 "friend re-declaration");
4379 else if (is_friend_decl)
4380 msg = G_("default template arguments may not be used in function template "
4381 "friend declarations");
4382 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4383 msg = G_("default template arguments may not be used in function templates "
4384 "without -std=c++11 or -std=gnu++11");
4385 else if (is_partial)
4386 msg = G_("default template arguments may not be used in "
4387 "partial specializations");
4389 msg = G_("default argument for template parameter for class enclosing %qD");
4391 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4392 /* If we're inside a class definition, there's no need to
4393 examine the parameters to the class itself. On the one
4394 hand, they will be checked when the class is defined, and,
4395 on the other, default arguments are valid in things like:
4396 template <class T = double>
4397 struct S { template <class U> void f(U); };
4398 Here the default argument for `S' has no bearing on the
4399 declaration of `f'. */
4400 last_level_to_check = template_class_depth (current_class_type) + 1;
4402 /* Check everything. */
4403 last_level_to_check = 0;
4405 for (parm_level = parms;
4406 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4407 parm_level = TREE_CHAIN (parm_level))
4409 tree inner_parms = TREE_VALUE (parm_level);
4413 ntparms = TREE_VEC_LENGTH (inner_parms);
4414 for (i = 0; i < ntparms; ++i)
4416 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4419 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4424 if (is_friend_decl == 2)
4431 /* Clear out the default argument so that we are not
4433 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4437 /* At this point, if we're still interested in issuing messages,
4438 they must apply to classes surrounding the object declared. */
4440 msg = G_("default argument for template parameter for class "
4447 /* Worker for push_template_decl_real, called via
4448 for_each_template_parm. DATA is really an int, indicating the
4449 level of the parameters we are interested in. If T is a template
4450 parameter of that level, return nonzero. */
4453 template_parm_this_level_p (tree t, void* data)
4455 int this_level = *(int *)data;
4458 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4459 level = TEMPLATE_PARM_LEVEL (t);
4461 level = TEMPLATE_TYPE_LEVEL (t);
4462 return level == this_level;
4465 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4466 parameters given by current_template_args, or reuses a
4467 previously existing one, if appropriate. Returns the DECL, or an
4468 equivalent one, if it is replaced via a call to duplicate_decls.
4470 If IS_FRIEND is true, DECL is a friend declaration. */
4473 push_template_decl_real (tree decl, bool is_friend)
4481 int new_template_p = 0;
4482 /* True if the template is a member template, in the sense of
4484 bool member_template_p = false;
4486 if (decl == error_mark_node || !current_template_parms)
4487 return error_mark_node;
4489 /* See if this is a partial specialization. */
4490 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4491 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4492 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4494 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4498 /* For a friend, we want the context of the friend function, not
4499 the type of which it is a friend. */
4500 ctx = CP_DECL_CONTEXT (decl);
4501 else if (CP_DECL_CONTEXT (decl)
4502 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4503 /* In the case of a virtual function, we want the class in which
4505 ctx = CP_DECL_CONTEXT (decl);
4507 /* Otherwise, if we're currently defining some class, the DECL
4508 is assumed to be a member of the class. */
4509 ctx = current_scope ();
4511 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4514 if (!DECL_CONTEXT (decl))
4515 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4517 /* See if this is a primary template. */
4518 if (is_friend && ctx)
4519 /* A friend template that specifies a class context, i.e.
4520 template <typename T> friend void A<T>::f();
4524 primary = template_parm_scope_p ();
4528 if (DECL_CLASS_SCOPE_P (decl))
4529 member_template_p = true;
4530 if (TREE_CODE (decl) == TYPE_DECL
4531 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4533 error ("template class without a name");
4534 return error_mark_node;
4536 else if (TREE_CODE (decl) == FUNCTION_DECL)
4538 if (DECL_DESTRUCTOR_P (decl))
4542 A destructor shall not be a member template. */
4543 error ("destructor %qD declared as member template", decl);
4544 return error_mark_node;
4546 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4547 && (!prototype_p (TREE_TYPE (decl))
4548 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4549 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4550 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4551 == void_list_node)))
4553 /* [basic.stc.dynamic.allocation]
4555 An allocation function can be a function
4556 template. ... Template allocation functions shall
4557 have two or more parameters. */
4558 error ("invalid template declaration of %qD", decl);
4559 return error_mark_node;
4562 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4563 && CLASS_TYPE_P (TREE_TYPE (decl)))
4565 else if (TREE_CODE (decl) == TYPE_DECL
4566 && TYPE_DECL_ALIAS_P (decl))
4567 /* alias-declaration */
4568 gcc_assert (!DECL_ARTIFICIAL (decl));
4571 error ("template declaration of %q#D", decl);
4572 return error_mark_node;
4576 /* Check to see that the rules regarding the use of default
4577 arguments are not being violated. */
4578 check_default_tmpl_args (decl, current_template_parms,
4579 primary, is_partial, /*is_friend_decl=*/0);
4581 /* Ensure that there are no parameter packs in the type of this
4582 declaration that have not been expanded. */
4583 if (TREE_CODE (decl) == FUNCTION_DECL)
4585 /* Check each of the arguments individually to see if there are
4586 any bare parameter packs. */
4587 tree type = TREE_TYPE (decl);
4588 tree arg = DECL_ARGUMENTS (decl);
4589 tree argtype = TYPE_ARG_TYPES (type);
4591 while (arg && argtype)
4593 if (!FUNCTION_PARAMETER_PACK_P (arg)
4594 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4596 /* This is a PARM_DECL that contains unexpanded parameter
4597 packs. We have already complained about this in the
4598 check_for_bare_parameter_packs call, so just replace
4599 these types with ERROR_MARK_NODE. */
4600 TREE_TYPE (arg) = error_mark_node;
4601 TREE_VALUE (argtype) = error_mark_node;
4604 arg = DECL_CHAIN (arg);
4605 argtype = TREE_CHAIN (argtype);
4608 /* Check for bare parameter packs in the return type and the
4609 exception specifiers. */
4610 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4611 /* Errors were already issued, set return type to int
4612 as the frontend doesn't expect error_mark_node as
4614 TREE_TYPE (type) = integer_type_node;
4615 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4616 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4618 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
4619 && TYPE_DECL_ALIAS_P (decl))
4620 ? DECL_ORIGINAL_TYPE (decl)
4621 : TREE_TYPE (decl)))
4623 TREE_TYPE (decl) = error_mark_node;
4624 return error_mark_node;
4628 return process_partial_specialization (decl);
4630 args = current_template_args ();
4633 || TREE_CODE (ctx) == FUNCTION_DECL
4634 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4635 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4637 if (DECL_LANG_SPECIFIC (decl)
4638 && DECL_TEMPLATE_INFO (decl)
4639 && DECL_TI_TEMPLATE (decl))
4640 tmpl = DECL_TI_TEMPLATE (decl);
4641 /* If DECL is a TYPE_DECL for a class-template, then there won't
4642 be DECL_LANG_SPECIFIC. The information equivalent to
4643 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4644 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4645 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4646 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4648 /* Since a template declaration already existed for this
4649 class-type, we must be redeclaring it here. Make sure
4650 that the redeclaration is valid. */
4651 redeclare_class_template (TREE_TYPE (decl),
4652 current_template_parms);
4653 /* We don't need to create a new TEMPLATE_DECL; just use the
4654 one we already had. */
4655 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4659 tmpl = build_template_decl (decl, current_template_parms,
4663 if (DECL_LANG_SPECIFIC (decl)
4664 && DECL_TEMPLATE_SPECIALIZATION (decl))
4666 /* A specialization of a member template of a template
4668 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4669 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4670 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4676 tree a, t, current, parms;
4678 tree tinfo = get_template_info (decl);
4682 error ("template definition of non-template %q#D", decl);
4683 return error_mark_node;
4686 tmpl = TI_TEMPLATE (tinfo);
4688 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4689 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4690 && DECL_TEMPLATE_SPECIALIZATION (decl)
4691 && DECL_MEMBER_TEMPLATE_P (tmpl))
4695 /* The declaration is a specialization of a member
4696 template, declared outside the class. Therefore, the
4697 innermost template arguments will be NULL, so we
4698 replace them with the arguments determined by the
4699 earlier call to check_explicit_specialization. */
4700 args = DECL_TI_ARGS (decl);
4703 = build_template_decl (decl, current_template_parms,
4705 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4706 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4707 DECL_TI_TEMPLATE (decl) = new_tmpl;
4708 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4709 DECL_TEMPLATE_INFO (new_tmpl)
4710 = build_template_info (tmpl, args);
4712 register_specialization (new_tmpl,
4713 most_general_template (tmpl),
4719 /* Make sure the template headers we got make sense. */
4721 parms = DECL_TEMPLATE_PARMS (tmpl);
4722 i = TMPL_PARMS_DEPTH (parms);
4723 if (TMPL_ARGS_DEPTH (args) != i)
4725 error ("expected %d levels of template parms for %q#D, got %d",
4726 i, decl, TMPL_ARGS_DEPTH (args));
4729 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4731 a = TMPL_ARGS_LEVEL (args, i);
4732 t = INNERMOST_TEMPLATE_PARMS (parms);
4734 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4736 if (current == decl)
4737 error ("got %d template parameters for %q#D",
4738 TREE_VEC_LENGTH (a), decl);
4740 error ("got %d template parameters for %q#T",
4741 TREE_VEC_LENGTH (a), current);
4742 error (" but %d required", TREE_VEC_LENGTH (t));
4743 /* Avoid crash in import_export_decl. */
4744 DECL_INTERFACE_KNOWN (decl) = 1;
4745 return error_mark_node;
4748 if (current == decl)
4750 else if (current == NULL_TREE)
4751 /* Can happen in erroneous input. */
4754 current = (TYPE_P (current)
4755 ? TYPE_CONTEXT (current)
4756 : DECL_CONTEXT (current));
4759 /* Check that the parms are used in the appropriate qualifying scopes
4760 in the declarator. */
4761 if (!comp_template_args
4763 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4766 template arguments to %qD do not match original template %qD",
4767 decl, DECL_TEMPLATE_RESULT (tmpl));
4768 if (!uses_template_parms (TI_ARGS (tinfo)))
4769 inform (input_location, "use template<> for an explicit specialization");
4770 /* Avoid crash in import_export_decl. */
4771 DECL_INTERFACE_KNOWN (decl) = 1;
4772 return error_mark_node;
4776 DECL_TEMPLATE_RESULT (tmpl) = decl;
4777 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4779 /* Push template declarations for global functions and types. Note
4780 that we do not try to push a global template friend declared in a
4781 template class; such a thing may well depend on the template
4782 parameters of the class. */
4783 if (new_template_p && !ctx
4784 && !(is_friend && template_class_depth (current_class_type) > 0))
4786 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4787 if (tmpl == error_mark_node)
4788 return error_mark_node;
4790 /* Hide template friend classes that haven't been declared yet. */
4791 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4793 DECL_ANTICIPATED (tmpl) = 1;
4794 DECL_FRIEND_P (tmpl) = 1;
4800 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4803 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4804 if (DECL_CONV_FN_P (tmpl))
4806 int depth = TMPL_PARMS_DEPTH (parms);
4808 /* It is a conversion operator. See if the type converted to
4809 depends on innermost template operands. */
4811 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4813 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4816 /* Give template template parms a DECL_CONTEXT of the template
4817 for which they are a parameter. */
4818 parms = INNERMOST_TEMPLATE_PARMS (parms);
4819 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4821 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4822 if (TREE_CODE (parm) == TEMPLATE_DECL)
4823 DECL_CONTEXT (parm) = tmpl;
4827 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4828 back to its most general template. If TMPL is a specialization,
4829 ARGS may only have the innermost set of arguments. Add the missing
4830 argument levels if necessary. */
4831 if (DECL_TEMPLATE_INFO (tmpl))
4832 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4834 info = build_template_info (tmpl, args);
4836 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4837 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4840 if (primary && !DECL_LANG_SPECIFIC (decl))
4841 retrofit_lang_decl (decl);
4842 if (DECL_LANG_SPECIFIC (decl))
4843 DECL_TEMPLATE_INFO (decl) = info;
4846 return DECL_TEMPLATE_RESULT (tmpl);
4850 push_template_decl (tree decl)
4852 return push_template_decl_real (decl, false);
4855 /* Called when a class template TYPE is redeclared with the indicated
4856 template PARMS, e.g.:
4858 template <class T> struct S;
4859 template <class T> struct S {}; */
4862 redeclare_class_template (tree type, tree parms)
4868 if (!TYPE_TEMPLATE_INFO (type))
4870 error ("%qT is not a template type", type);
4874 tmpl = TYPE_TI_TEMPLATE (type);
4875 if (!PRIMARY_TEMPLATE_P (tmpl))
4876 /* The type is nested in some template class. Nothing to worry
4877 about here; there are no new template parameters for the nested
4883 error ("template specifiers not specified in declaration of %qD",
4888 parms = INNERMOST_TEMPLATE_PARMS (parms);
4889 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4891 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4893 error_n (input_location, TREE_VEC_LENGTH (parms),
4894 "redeclared with %d template parameter",
4895 "redeclared with %d template parameters",
4896 TREE_VEC_LENGTH (parms));
4897 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
4898 "previous declaration %q+D used %d template parameter",
4899 "previous declaration %q+D used %d template parameters",
4900 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4904 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4911 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4912 || TREE_VEC_ELT (parms, i) == error_mark_node)
4915 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4916 if (tmpl_parm == error_mark_node)
4919 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4920 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4921 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4923 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4925 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4926 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4927 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4928 || (TREE_CODE (tmpl_parm) != PARM_DECL
4929 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4930 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4931 || (TREE_CODE (tmpl_parm) == PARM_DECL
4932 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4933 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
4935 error ("template parameter %q+#D", tmpl_parm);
4936 error ("redeclared here as %q#D", parm);
4940 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4942 /* We have in [temp.param]:
4944 A template-parameter may not be given default arguments
4945 by two different declarations in the same scope. */
4946 error_at (input_location, "redefinition of default argument for %q#D", parm);
4947 inform (DECL_SOURCE_LOCATION (tmpl_parm),
4948 "original definition appeared here");
4952 if (parm_default != NULL_TREE)
4953 /* Update the previous template parameters (which are the ones
4954 that will really count) with the new default value. */
4955 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4956 else if (tmpl_default != NULL_TREE)
4957 /* Update the new parameters, too; they'll be used as the
4958 parameters for any members. */
4959 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4965 /* Simplify EXPR if it is a non-dependent expression. Returns the
4966 (possibly simplified) expression. */
4969 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
4971 if (expr == NULL_TREE)
4974 /* If we're in a template, but EXPR isn't value dependent, simplify
4975 it. We're supposed to treat:
4977 template <typename T> void f(T[1 + 1]);
4978 template <typename T> void f(T[2]);
4980 as two declarations of the same function, for example. */
4981 if (processing_template_decl
4982 && !type_dependent_expression_p (expr)
4983 && potential_constant_expression (expr)
4984 && !value_dependent_expression_p (expr))
4986 HOST_WIDE_INT saved_processing_template_decl;
4988 saved_processing_template_decl = processing_template_decl;
4989 processing_template_decl = 0;
4990 expr = tsubst_copy_and_build (expr,
4993 /*in_decl=*/NULL_TREE,
4994 /*function_p=*/false,
4995 /*integral_constant_expression_p=*/true);
4996 processing_template_decl = saved_processing_template_decl;
5002 fold_non_dependent_expr (tree expr)
5004 return fold_non_dependent_expr_sfinae (expr, tf_error);
5007 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5008 template declaration, or a TYPE_DECL for an alias declaration. */
5011 alias_type_or_template_p (tree t)
5015 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5018 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5019 || DECL_ALIAS_TEMPLATE_P (t));
5022 /* Return TRUE iff is a specialization of an alias template. */
5025 alias_template_specialization_p (tree t)
5029 return (primary_template_instantiation_p (t)
5030 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
5033 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5034 must be a function or a pointer-to-function type, as specified
5035 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5036 and check that the resulting function has external linkage. */
5039 convert_nontype_argument_function (tree type, tree expr)
5043 linkage_kind linkage;
5045 fn = instantiate_type (type, fns, tf_none);
5046 if (fn == error_mark_node)
5047 return error_mark_node;
5050 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5051 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5052 if (BASELINK_P (fn_no_ptr))
5053 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5055 /* [temp.arg.nontype]/1
5057 A template-argument for a non-type, non-template template-parameter
5060 -- the address of an object or function with external [C++11: or
5061 internal] linkage. */
5062 linkage = decl_linkage (fn_no_ptr);
5063 if (cxx_dialect >= cxx0x ? linkage == lk_none : linkage != lk_external)
5065 if (cxx_dialect >= cxx0x)
5066 error ("%qE is not a valid template argument for type %qT "
5067 "because %qD has no linkage",
5068 expr, type, fn_no_ptr);
5070 error ("%qE is not a valid template argument for type %qT "
5071 "because %qD does not have external linkage",
5072 expr, type, fn_no_ptr);
5079 /* Subroutine of convert_nontype_argument.
5080 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5081 Emit an error otherwise. */
5084 check_valid_ptrmem_cst_expr (tree type, tree expr,
5085 tsubst_flags_t complain)
5088 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5090 if (cxx_dialect >= cxx0x && null_member_pointer_value_p (expr))
5092 if (complain & tf_error)
5094 error ("%qE is not a valid template argument for type %qT",
5096 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5101 /* Returns TRUE iff the address of OP is value-dependent.
5103 14.6.2.4 [temp.dep.temp]:
5104 A non-integral non-type template-argument is dependent if its type is
5105 dependent or it has either of the following forms
5108 and contains a nested-name-specifier which specifies a class-name that
5109 names a dependent type.
5111 We generalize this to just say that the address of a member of a
5112 dependent class is value-dependent; the above doesn't cover the
5113 address of a static data member named with an unqualified-id. */
5116 has_value_dependent_address (tree op)
5118 /* We could use get_inner_reference here, but there's no need;
5119 this is only relevant for template non-type arguments, which
5120 can only be expressed as &id-expression. */
5123 tree ctx = CP_DECL_CONTEXT (op);
5124 if (TYPE_P (ctx) && dependent_type_p (ctx))
5131 /* The next set of functions are used for providing helpful explanatory
5132 diagnostics for failed overload resolution. Their messages should be
5133 indented by two spaces for consistency with the messages in
5137 unify_success (bool explain_p ATTRIBUTE_UNUSED)
5143 unify_parameter_deduction_failure (bool explain_p, tree parm)
5146 inform (input_location,
5147 " couldn't deduce template parameter %qD", parm);
5152 unify_invalid (bool explain_p ATTRIBUTE_UNUSED)
5158 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
5161 inform (input_location,
5162 " types %qT and %qT have incompatible cv-qualifiers",
5168 unify_type_mismatch (bool explain_p, tree parm, tree arg)
5171 inform (input_location, " mismatched types %qT and %qT", parm, arg);
5176 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
5179 inform (input_location,
5180 " template parameter %qD is not a parameter pack, but "
5187 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
5190 inform (input_location,
5191 " template argument %qE does not match "
5192 "pointer-to-member constant %qE",
5198 unify_expression_unequal (bool explain_p, tree parm, tree arg)
5201 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
5206 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
5209 inform (input_location,
5210 " inconsistent parameter pack deduction with %qT and %qT",
5216 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
5221 inform (input_location,
5222 " deduced conflicting types for parameter %qT (%qT and %qT)",
5223 parm, first, second);
5225 inform (input_location,
5226 " deduced conflicting values for non-type parameter "
5227 "%qE (%qE and %qE)", parm, first, second);
5233 unify_vla_arg (bool explain_p, tree arg)
5236 inform (input_location,
5237 " variable-sized array type %qT is not "
5238 "a valid template argument",
5244 unify_method_type_error (bool explain_p, tree arg)
5247 inform (input_location,
5248 " member function type %qT is not a valid template argument",
5254 unify_arity (bool explain_p, int have, int wanted)
5257 inform_n (input_location, wanted,
5258 " candidate expects %d argument, %d provided",
5259 " candidate expects %d arguments, %d provided",
5265 unify_too_many_arguments (bool explain_p, int have, int wanted)
5267 return unify_arity (explain_p, have, wanted);
5271 unify_too_few_arguments (bool explain_p, int have, int wanted)
5273 return unify_arity (explain_p, have, wanted);
5277 unify_arg_conversion (bool explain_p, tree to_type,
5278 tree from_type, tree arg)
5281 inform (input_location, " cannot convert %qE (type %qT) to type %qT",
5282 arg, from_type, to_type);
5287 unify_no_common_base (bool explain_p, enum template_base_result r,
5288 tree parm, tree arg)
5293 case tbr_ambiguous_baseclass:
5294 inform (input_location, " %qT is an ambiguous base class of %qT",
5298 inform (input_location, " %qT is not derived from %qT", arg, parm);
5305 unify_inconsistent_template_template_parameters (bool explain_p)
5308 inform (input_location,
5309 " template parameters of a template template argument are "
5310 "inconsistent with other deduced template arguments");
5315 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
5318 inform (input_location,
5319 " can't deduce a template for %qT from non-template type %qT",
5325 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
5328 inform (input_location,
5329 " template argument %qE does not match %qD", arg, parm);
5334 unify_overload_resolution_failure (bool explain_p, tree arg)
5337 inform (input_location,
5338 " could not resolve address from overloaded function %qE",
5343 /* Attempt to convert the non-type template parameter EXPR to the
5344 indicated TYPE. If the conversion is successful, return the
5345 converted value. If the conversion is unsuccessful, return
5346 NULL_TREE if we issued an error message, or error_mark_node if we
5347 did not. We issue error messages for out-and-out bad template
5348 parameters, but not simply because the conversion failed, since we
5349 might be just trying to do argument deduction. Both TYPE and EXPR
5350 must be non-dependent.
5352 The conversion follows the special rules described in
5353 [temp.arg.nontype], and it is much more strict than an implicit
5356 This function is called twice for each template argument (see
5357 lookup_template_class for a more accurate description of this
5358 problem). This means that we need to handle expressions which
5359 are not valid in a C++ source, but can be created from the
5360 first call (for instance, casts to perform conversions). These
5361 hacks can go away after we fix the double coercion problem. */
5364 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5368 /* Detect immediately string literals as invalid non-type argument.
5369 This special-case is not needed for correctness (we would easily
5370 catch this later), but only to provide better diagnostic for this
5371 common user mistake. As suggested by DR 100, we do not mention
5372 linkage issues in the diagnostic as this is not the point. */
5373 /* FIXME we're making this OK. */
5374 if (TREE_CODE (expr) == STRING_CST)
5376 if (complain & tf_error)
5377 error ("%qE is not a valid template argument for type %qT "
5378 "because string literals can never be used in this context",
5383 /* Add the ADDR_EXPR now for the benefit of
5384 value_dependent_expression_p. */
5385 if (TYPE_PTROBV_P (type)
5386 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5387 expr = decay_conversion (expr);
5389 /* If we are in a template, EXPR may be non-dependent, but still
5390 have a syntactic, rather than semantic, form. For example, EXPR
5391 might be a SCOPE_REF, rather than the VAR_DECL to which the
5392 SCOPE_REF refers. Preserving the qualifying scope is necessary
5393 so that access checking can be performed when the template is
5394 instantiated -- but here we need the resolved form so that we can
5395 convert the argument. */
5396 if (TYPE_REF_OBJ_P (type)
5397 && has_value_dependent_address (expr))
5398 /* If we want the address and it's value-dependent, don't fold. */;
5399 else if (!type_unknown_p (expr))
5400 expr = fold_non_dependent_expr_sfinae (expr, complain);
5401 if (error_operand_p (expr))
5402 return error_mark_node;
5403 expr_type = TREE_TYPE (expr);
5404 if (TREE_CODE (type) == REFERENCE_TYPE)
5405 expr = mark_lvalue_use (expr);
5407 expr = mark_rvalue_use (expr);
5409 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5410 to a non-type argument of "nullptr". */
5411 if (expr == nullptr_node
5412 && (TYPE_PTR_P (type) || TYPE_PTR_TO_MEMBER_P (type)))
5413 expr = convert (type, expr);
5415 /* In C++11, integral or enumeration non-type template arguments can be
5416 arbitrary constant expressions. Pointer and pointer to
5417 member arguments can be general constant expressions that evaluate
5418 to a null value, but otherwise still need to be of a specific form. */
5419 if (cxx_dialect >= cxx0x)
5421 if (TREE_CODE (expr) == PTRMEM_CST)
5422 /* A PTRMEM_CST is already constant, and a valid template
5423 argument for a parameter of pointer to member type, we just want
5424 to leave it in that form rather than lower it to a
5426 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5427 expr = maybe_constant_value (expr);
5428 else if (TYPE_PTR_P (type)
5429 || TYPE_PTR_TO_MEMBER_P (type))
5431 tree folded = maybe_constant_value (expr);
5432 if (TYPE_PTR_P (type) ? integer_zerop (folded)
5433 : null_member_pointer_value_p (folded))
5438 /* HACK: Due to double coercion, we can get a
5439 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5440 which is the tree that we built on the first call (see
5441 below when coercing to reference to object or to reference to
5442 function). We just strip everything and get to the arg.
5443 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5445 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5447 tree probe_type, probe = expr;
5448 if (REFERENCE_REF_P (probe))
5449 probe = TREE_OPERAND (probe, 0);
5450 probe_type = TREE_TYPE (probe);
5451 if (TREE_CODE (probe) == NOP_EXPR)
5453 /* ??? Maybe we could use convert_from_reference here, but we
5454 would need to relax its constraints because the NOP_EXPR
5455 could actually change the type to something more cv-qualified,
5456 and this is not folded by convert_from_reference. */
5457 tree addr = TREE_OPERAND (probe, 0);
5458 gcc_assert (TREE_CODE (probe_type) == REFERENCE_TYPE);
5459 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
5460 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
5461 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5462 (TREE_TYPE (probe_type),
5463 TREE_TYPE (TREE_TYPE (addr))));
5465 expr = TREE_OPERAND (addr, 0);
5466 expr_type = TREE_TYPE (expr);
5470 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5471 parameter is a pointer to object, through decay and
5472 qualification conversion. Let's strip everything. */
5473 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
5476 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
5477 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
5478 /* Skip the ADDR_EXPR only if it is part of the decay for
5479 an array. Otherwise, it is part of the original argument
5480 in the source code. */
5481 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
5482 expr = TREE_OPERAND (expr, 0);
5483 expr_type = TREE_TYPE (expr);
5486 /* [temp.arg.nontype]/5, bullet 1
5488 For a non-type template-parameter of integral or enumeration type,
5489 integral promotions (_conv.prom_) and integral conversions
5490 (_conv.integral_) are applied. */
5491 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5493 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5494 t = maybe_constant_value (t);
5495 if (t != error_mark_node)
5498 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5499 return error_mark_node;
5501 /* Notice that there are constant expressions like '4 % 0' which
5502 do not fold into integer constants. */
5503 if (TREE_CODE (expr) != INTEGER_CST)
5505 if (complain & tf_error)
5507 int errs = errorcount, warns = warningcount;
5508 if (processing_template_decl
5509 && !require_potential_constant_expression (expr))
5511 expr = cxx_constant_value (expr);
5512 if (errorcount > errs || warningcount > warns)
5513 inform (EXPR_LOC_OR_HERE (expr),
5514 "in template argument for type %qT ", type);
5515 if (expr == error_mark_node)
5517 /* else cxx_constant_value complained but gave us
5518 a real constant, so go ahead. */
5519 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5525 /* Avoid typedef problems. */
5526 if (TREE_TYPE (expr) != type)
5527 expr = fold_convert (type, expr);
5529 /* [temp.arg.nontype]/5, bullet 2
5531 For a non-type template-parameter of type pointer to object,
5532 qualification conversions (_conv.qual_) and the array-to-pointer
5533 conversion (_conv.array_) are applied. */
5534 else if (TYPE_PTROBV_P (type))
5536 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5538 A template-argument for a non-type, non-template template-parameter
5539 shall be one of: [...]
5541 -- the name of a non-type template-parameter;
5542 -- the address of an object or function with external linkage, [...]
5543 expressed as "& id-expression" where the & is optional if the name
5544 refers to a function or array, or if the corresponding
5545 template-parameter is a reference.
5547 Here, we do not care about functions, as they are invalid anyway
5548 for a parameter of type pointer-to-object. */
5550 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5551 /* Non-type template parameters are OK. */
5553 else if (cxx_dialect >= cxx0x && integer_zerop (expr))
5554 /* Null pointer values are OK in C++11. */;
5555 else if (TREE_CODE (expr) != ADDR_EXPR
5556 && TREE_CODE (expr_type) != ARRAY_TYPE)
5558 if (TREE_CODE (expr) == VAR_DECL)
5560 error ("%qD is not a valid template argument "
5561 "because %qD is a variable, not the address of "
5566 /* Other values, like integer constants, might be valid
5567 non-type arguments of some other type. */
5568 return error_mark_node;
5574 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5575 ? TREE_OPERAND (expr, 0) : expr);
5576 if (TREE_CODE (decl) != VAR_DECL)
5578 error ("%qE is not a valid template argument of type %qT "
5579 "because %qE is not a variable",
5583 else if (cxx_dialect < cxx0x && !DECL_EXTERNAL_LINKAGE_P (decl))
5585 error ("%qE is not a valid template argument of type %qT "
5586 "because %qD does not have external linkage",
5590 else if (cxx_dialect >= cxx0x && decl_linkage (decl) == lk_none)
5592 error ("%qE is not a valid template argument of type %qT "
5593 "because %qD has no linkage",
5599 expr = decay_conversion (expr);
5600 if (expr == error_mark_node)
5601 return error_mark_node;
5603 expr = perform_qualification_conversions (type, expr);
5604 if (expr == error_mark_node)
5605 return error_mark_node;
5607 /* [temp.arg.nontype]/5, bullet 3
5609 For a non-type template-parameter of type reference to object, no
5610 conversions apply. The type referred to by the reference may be more
5611 cv-qualified than the (otherwise identical) type of the
5612 template-argument. The template-parameter is bound directly to the
5613 template-argument, which must be an lvalue. */
5614 else if (TYPE_REF_OBJ_P (type))
5616 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5618 return error_mark_node;
5620 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5622 error ("%qE is not a valid template argument for type %qT "
5623 "because of conflicts in cv-qualification", expr, type);
5627 if (!real_lvalue_p (expr))
5629 error ("%qE is not a valid template argument for type %qT "
5630 "because it is not an lvalue", expr, type);
5634 /* [temp.arg.nontype]/1
5636 A template-argument for a non-type, non-template template-parameter
5637 shall be one of: [...]
5639 -- the address of an object or function with external linkage. */
5640 if (TREE_CODE (expr) == INDIRECT_REF
5641 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5643 expr = TREE_OPERAND (expr, 0);
5646 error ("%q#D is not a valid template argument for type %qT "
5647 "because a reference variable does not have a constant "
5648 "address", expr, type);
5655 error ("%qE is not a valid template argument for type %qT "
5656 "because it is not an object with external linkage",
5661 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5663 error ("%qE is not a valid template argument for type %qT "
5664 "because object %qD has not external linkage",
5669 expr = build_nop (type, build_address (expr));
5671 /* [temp.arg.nontype]/5, bullet 4
5673 For a non-type template-parameter of type pointer to function, only
5674 the function-to-pointer conversion (_conv.func_) is applied. If the
5675 template-argument represents a set of overloaded functions (or a
5676 pointer to such), the matching function is selected from the set
5678 else if (TYPE_PTRFN_P (type))
5680 /* If the argument is a template-id, we might not have enough
5681 context information to decay the pointer. */
5682 if (!type_unknown_p (expr_type))
5684 expr = decay_conversion (expr);
5685 if (expr == error_mark_node)
5686 return error_mark_node;
5689 if (cxx_dialect >= cxx0x && integer_zerop (expr))
5690 /* Null pointer values are OK in C++11. */
5691 return perform_qualification_conversions (type, expr);
5693 expr = convert_nontype_argument_function (type, expr);
5694 if (!expr || expr == error_mark_node)
5697 if (TREE_CODE (expr) != ADDR_EXPR)
5699 error ("%qE is not a valid template argument for type %qT", expr, type);
5700 error ("it must be the address of a function with external linkage");
5704 /* [temp.arg.nontype]/5, bullet 5
5706 For a non-type template-parameter of type reference to function, no
5707 conversions apply. If the template-argument represents a set of
5708 overloaded functions, the matching function is selected from the set
5710 else if (TYPE_REFFN_P (type))
5712 if (TREE_CODE (expr) == ADDR_EXPR)
5714 error ("%qE is not a valid template argument for type %qT "
5715 "because it is a pointer", expr, type);
5716 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5720 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5721 if (!expr || expr == error_mark_node)
5724 expr = build_nop (type, build_address (expr));
5726 /* [temp.arg.nontype]/5, bullet 6
5728 For a non-type template-parameter of type pointer to member function,
5729 no conversions apply. If the template-argument represents a set of
5730 overloaded member functions, the matching member function is selected
5731 from the set (_over.over_). */
5732 else if (TYPE_PTRMEMFUNC_P (type))
5734 expr = instantiate_type (type, expr, tf_none);
5735 if (expr == error_mark_node)
5736 return error_mark_node;
5738 /* [temp.arg.nontype] bullet 1 says the pointer to member
5739 expression must be a pointer-to-member constant. */
5740 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5741 return error_mark_node;
5743 /* There is no way to disable standard conversions in
5744 resolve_address_of_overloaded_function (called by
5745 instantiate_type). It is possible that the call succeeded by
5746 converting &B::I to &D::I (where B is a base of D), so we need
5747 to reject this conversion here.
5749 Actually, even if there was a way to disable standard conversions,
5750 it would still be better to reject them here so that we can
5751 provide a superior diagnostic. */
5752 if (!same_type_p (TREE_TYPE (expr), type))
5754 error ("%qE is not a valid template argument for type %qT "
5755 "because it is of type %qT", expr, type,
5757 /* If we are just one standard conversion off, explain. */
5758 if (can_convert (type, TREE_TYPE (expr)))
5759 inform (input_location,
5760 "standard conversions are not allowed in this context");
5764 /* [temp.arg.nontype]/5, bullet 7
5766 For a non-type template-parameter of type pointer to data member,
5767 qualification conversions (_conv.qual_) are applied. */
5768 else if (TYPE_PTRMEM_P (type))
5770 /* [temp.arg.nontype] bullet 1 says the pointer to member
5771 expression must be a pointer-to-member constant. */
5772 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5773 return error_mark_node;
5775 expr = perform_qualification_conversions (type, expr);
5776 if (expr == error_mark_node)
5779 else if (NULLPTR_TYPE_P (type))
5781 if (expr != nullptr_node)
5783 error ("%qE is not a valid template argument for type %qT "
5784 "because it is of type %qT", expr, type, TREE_TYPE (expr));
5789 /* A template non-type parameter must be one of the above. */
5793 /* Sanity check: did we actually convert the argument to the
5795 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5796 (type, TREE_TYPE (expr)));
5800 /* Subroutine of coerce_template_template_parms, which returns 1 if
5801 PARM_PARM and ARG_PARM match using the rule for the template
5802 parameters of template template parameters. Both PARM and ARG are
5803 template parameters; the rest of the arguments are the same as for
5804 coerce_template_template_parms.
5807 coerce_template_template_parm (tree parm,
5809 tsubst_flags_t complain,
5813 if (arg == NULL_TREE || arg == error_mark_node
5814 || parm == NULL_TREE || parm == error_mark_node)
5817 if (TREE_CODE (arg) != TREE_CODE (parm))
5820 switch (TREE_CODE (parm))
5823 /* We encounter instantiations of templates like
5824 template <template <template <class> class> class TT>
5827 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5828 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5830 if (!coerce_template_template_parms
5831 (parmparm, argparm, complain, in_decl, outer_args))
5837 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5838 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5839 /* Argument is a parameter pack but parameter is not. */
5844 /* The tsubst call is used to handle cases such as
5846 template <int> class C {};
5847 template <class T, template <T> class TT> class D {};
5850 i.e. the parameter list of TT depends on earlier parameters. */
5851 if (!uses_template_parms (TREE_TYPE (arg))
5853 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5857 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5858 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5859 /* Argument is a parameter pack but parameter is not. */
5872 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5873 template template parameters. Both PARM_PARMS and ARG_PARMS are
5874 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5877 Consider the example:
5878 template <class T> class A;
5879 template<template <class U> class TT> class B;
5881 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5882 the parameters to A, and OUTER_ARGS contains A. */
5885 coerce_template_template_parms (tree parm_parms,
5887 tsubst_flags_t complain,
5891 int nparms, nargs, i;
5895 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5896 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5898 nparms = TREE_VEC_LENGTH (parm_parms);
5899 nargs = TREE_VEC_LENGTH (arg_parms);
5901 /* Determine whether we have a parameter pack at the end of the
5902 template template parameter's template parameter list. */
5903 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5905 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5907 if (parm == error_mark_node)
5910 switch (TREE_CODE (parm))
5914 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5919 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5929 && !(variadic_p && nargs >= nparms - 1))
5932 /* Check all of the template parameters except the parameter pack at
5933 the end (if any). */
5934 for (i = 0; i < nparms - variadic_p; ++i)
5936 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5937 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5940 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5941 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5943 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5951 /* Check each of the template parameters in the template
5952 argument against the template parameter pack at the end of
5953 the template template parameter. */
5954 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
5957 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5959 for (; i < nargs; ++i)
5961 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5964 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5966 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5975 /* Verifies that the deduced template arguments (in TARGS) for the
5976 template template parameters (in TPARMS) represent valid bindings,
5977 by comparing the template parameter list of each template argument
5978 to the template parameter list of its corresponding template
5979 template parameter, in accordance with DR150. This
5980 routine can only be called after all template arguments have been
5981 deduced. It will return TRUE if all of the template template
5982 parameter bindings are okay, FALSE otherwise. */
5984 template_template_parm_bindings_ok_p (tree tparms, tree targs)
5986 int i, ntparms = TREE_VEC_LENGTH (tparms);
5989 /* We're dealing with template parms in this process. */
5990 ++processing_template_decl;
5992 targs = INNERMOST_TEMPLATE_ARGS (targs);
5994 for (i = 0; i < ntparms; ++i)
5996 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
5997 tree targ = TREE_VEC_ELT (targs, i);
5999 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
6001 tree packed_args = NULL_TREE;
6004 if (ARGUMENT_PACK_P (targ))
6006 /* Look inside the argument pack. */
6007 packed_args = ARGUMENT_PACK_ARGS (targ);
6008 len = TREE_VEC_LENGTH (packed_args);
6011 for (idx = 0; idx < len; ++idx)
6013 tree targ_parms = NULL_TREE;
6016 /* Extract the next argument from the argument
6018 targ = TREE_VEC_ELT (packed_args, idx);
6020 if (PACK_EXPANSION_P (targ))
6021 /* Look at the pattern of the pack expansion. */
6022 targ = PACK_EXPANSION_PATTERN (targ);
6024 /* Extract the template parameters from the template
6026 if (TREE_CODE (targ) == TEMPLATE_DECL)
6027 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
6028 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
6029 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
6031 /* Verify that we can coerce the template template
6032 parameters from the template argument to the template
6033 parameter. This requires an exact match. */
6035 && !coerce_template_template_parms
6036 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
6051 --processing_template_decl;
6055 /* Since type attributes aren't mangled, we need to strip them from
6056 template type arguments. */
6059 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
6062 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
6064 mv = TYPE_MAIN_VARIANT (arg);
6065 arg = strip_typedefs (arg);
6066 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
6067 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
6069 if (complain & tf_warning)
6070 warning (0, "ignoring attributes on template argument %qT", arg);
6071 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
6072 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
6077 /* Convert the indicated template ARG as necessary to match the
6078 indicated template PARM. Returns the converted ARG, or
6079 error_mark_node if the conversion was unsuccessful. Error and
6080 warning messages are issued under control of COMPLAIN. This
6081 conversion is for the Ith parameter in the parameter list. ARGS is
6082 the full set of template arguments deduced so far. */
6085 convert_template_argument (tree parm,
6088 tsubst_flags_t complain,
6094 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
6096 if (TREE_CODE (arg) == TREE_LIST
6097 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
6099 /* The template argument was the name of some
6100 member function. That's usually
6101 invalid, but static members are OK. In any
6102 case, grab the underlying fields/functions
6103 and issue an error later if required. */
6104 orig_arg = TREE_VALUE (arg);
6105 TREE_TYPE (arg) = unknown_type_node;
6110 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
6111 requires_type = (TREE_CODE (parm) == TYPE_DECL
6112 || requires_tmpl_type);
6114 /* When determining whether an argument pack expansion is a template,
6115 look at the pattern. */
6116 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
6117 arg = PACK_EXPANSION_PATTERN (arg);
6119 /* Deal with an injected-class-name used as a template template arg. */
6120 if (requires_tmpl_type && CLASS_TYPE_P (arg))
6122 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
6123 if (TREE_CODE (t) == TEMPLATE_DECL)
6125 if (cxx_dialect >= cxx0x)
6126 /* OK under DR 1004. */;
6127 else if (complain & tf_warning_or_error)
6128 pedwarn (input_location, OPT_pedantic, "injected-class-name %qD"
6129 " used as template template argument", TYPE_NAME (arg));
6130 else if (flag_pedantic_errors)
6138 ((TREE_CODE (arg) == TEMPLATE_DECL
6139 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6140 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
6141 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6142 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6145 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6146 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6147 arg = TYPE_STUB_DECL (arg);
6149 is_type = TYPE_P (arg) || is_tmpl_type;
6151 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6152 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6154 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
6156 if (complain & tf_error)
6157 error ("invalid use of destructor %qE as a type", orig_arg);
6158 return error_mark_node;
6161 permerror (input_location,
6162 "to refer to a type member of a template parameter, "
6163 "use %<typename %E%>", orig_arg);
6165 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6166 TREE_OPERAND (arg, 1),
6168 complain & tf_error);
6172 if (is_type != requires_type)
6176 if (complain & tf_error)
6178 error ("type/value mismatch at argument %d in template "
6179 "parameter list for %qD",
6182 error (" expected a constant of type %qT, got %qT",
6184 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6185 else if (requires_tmpl_type)
6186 error (" expected a class template, got %qE", orig_arg);
6188 error (" expected a type, got %qE", orig_arg);
6191 return error_mark_node;
6193 if (is_tmpl_type ^ requires_tmpl_type)
6195 if (in_decl && (complain & tf_error))
6197 error ("type/value mismatch at argument %d in template "
6198 "parameter list for %qD",
6201 error (" expected a type, got %qT", DECL_NAME (arg));
6203 error (" expected a class template, got %qT", orig_arg);
6205 return error_mark_node;
6210 if (requires_tmpl_type)
6212 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6214 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6215 /* The number of argument required is not known yet.
6216 Just accept it for now. */
6217 val = TREE_TYPE (arg);
6220 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6223 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6225 if (coerce_template_template_parms (parmparm, argparm,
6231 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6233 if (val != error_mark_node)
6235 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6236 val = TREE_TYPE (val);
6237 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6238 val = make_pack_expansion (val);
6243 if (in_decl && (complain & tf_error))
6245 error ("type/value mismatch at argument %d in "
6246 "template parameter list for %qD",
6248 error (" expected a template of type %qD, got %qT",
6252 val = error_mark_node;
6258 /* We only form one instance of each template specialization.
6259 Therefore, if we use a non-canonical variant (i.e., a
6260 typedef), any future messages referring to the type will use
6261 the typedef, which is confusing if those future uses do not
6262 themselves also use the typedef. */
6264 val = canonicalize_type_argument (val, complain);
6268 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6270 if (invalid_nontype_parm_type_p (t, complain))
6271 return error_mark_node;
6273 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6275 if (same_type_p (t, TREE_TYPE (orig_arg)))
6279 /* Not sure if this is reachable, but it doesn't hurt
6281 error ("type mismatch in nontype parameter pack");
6282 val = error_mark_node;
6285 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
6286 /* We used to call digest_init here. However, digest_init
6287 will report errors, which we don't want when complain
6288 is zero. More importantly, digest_init will try too
6289 hard to convert things: for example, `0' should not be
6290 converted to pointer type at this point according to
6291 the standard. Accepting this is not merely an
6292 extension, since deciding whether or not these
6293 conversions can occur is part of determining which
6294 function template to call, or whether a given explicit
6295 argument specification is valid. */
6296 val = convert_nontype_argument (t, orig_arg, complain);
6298 val = strip_typedefs_expr (orig_arg);
6300 if (val == NULL_TREE)
6301 val = error_mark_node;
6302 else if (val == error_mark_node && (complain & tf_error))
6303 error ("could not convert template argument %qE to %qT", orig_arg, t);
6305 if (TREE_CODE (val) == SCOPE_REF)
6307 /* Strip typedefs from the SCOPE_REF. */
6308 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6309 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6311 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6312 QUALIFIED_NAME_IS_TEMPLATE (val));
6319 /* Coerces the remaining template arguments in INNER_ARGS (from
6320 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6321 Returns the coerced argument pack. PARM_IDX is the position of this
6322 parameter in the template parameter list. ARGS is the original
6323 template argument list. */
6325 coerce_template_parameter_pack (tree parms,
6333 tsubst_flags_t complain)
6335 tree parm = TREE_VEC_ELT (parms, parm_idx);
6336 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6339 tree packed_types = NULL_TREE;
6341 if (arg_idx > nargs)
6344 packed_args = make_tree_vec (nargs - arg_idx);
6346 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6347 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6349 /* When the template parameter is a non-type template
6350 parameter pack whose type uses parameter packs, we need
6351 to look at each of the template arguments
6352 separately. Build a vector of the types for these
6353 non-type template parameters in PACKED_TYPES. */
6355 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6356 packed_types = tsubst_pack_expansion (expansion, args,
6359 if (packed_types == error_mark_node)
6360 return error_mark_node;
6362 /* Check that we have the right number of arguments. */
6364 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6365 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6368 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6369 error ("wrong number of template arguments (%d, should be %d)",
6370 nargs, needed_parms);
6371 return error_mark_node;
6374 /* If we aren't able to check the actual arguments now
6375 (because they haven't been expanded yet), we can at least
6376 verify that all of the types used for the non-type
6377 template parameter pack are, in fact, valid for non-type
6378 template parameters. */
6380 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6382 int j, len = TREE_VEC_LENGTH (packed_types);
6383 for (j = 0; j < len; ++j)
6385 tree t = TREE_VEC_ELT (packed_types, j);
6386 if (invalid_nontype_parm_type_p (t, complain))
6387 return error_mark_node;
6392 /* Convert the remaining arguments, which will be a part of the
6393 parameter pack "parm". */
6394 for (; arg_idx < nargs; ++arg_idx)
6396 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6397 tree actual_parm = TREE_VALUE (parm);
6399 if (packed_types && !PACK_EXPANSION_P (arg))
6401 /* When we have a vector of types (corresponding to the
6402 non-type template parameter pack that uses parameter
6403 packs in its type, as mention above), and the
6404 argument is not an expansion (which expands to a
6405 currently unknown number of arguments), clone the
6406 parm and give it the next type in PACKED_TYPES. */
6407 actual_parm = copy_node (actual_parm);
6408 TREE_TYPE (actual_parm) =
6409 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6412 if (arg != error_mark_node)
6413 arg = convert_template_argument (actual_parm,
6414 arg, new_args, complain, parm_idx,
6416 if (arg == error_mark_node)
6418 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6421 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6422 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6423 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6426 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6427 TREE_TYPE (argument_pack)
6428 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6429 TREE_CONSTANT (argument_pack) = 1;
6432 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6433 #ifdef ENABLE_CHECKING
6434 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6435 TREE_VEC_LENGTH (packed_args));
6437 return argument_pack;
6440 /* Returns true if the template argument vector ARGS contains
6441 any pack expansions, false otherwise. */
6444 any_pack_expanson_args_p (tree args)
6448 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
6449 if (PACK_EXPANSION_P (TREE_VEC_ELT (args, i)))
6454 /* Convert all template arguments to their appropriate types, and
6455 return a vector containing the innermost resulting template
6456 arguments. If any error occurs, return error_mark_node. Error and
6457 warning messages are issued under control of COMPLAIN.
6459 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6460 for arguments not specified in ARGS. Otherwise, if
6461 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6462 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6463 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6467 coerce_template_parms (tree parms,
6470 tsubst_flags_t complain,
6471 bool require_all_args,
6472 bool use_default_args)
6474 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6477 tree new_inner_args;
6478 int saved_unevaluated_operand;
6479 int saved_inhibit_evaluation_warnings;
6481 /* When used as a boolean value, indicates whether this is a
6482 variadic template parameter list. Since it's an int, we can also
6483 subtract it from nparms to get the number of non-variadic
6486 int post_variadic_parms = 0;
6488 if (args == error_mark_node)
6489 return error_mark_node;
6491 nparms = TREE_VEC_LENGTH (parms);
6493 /* Determine if there are any parameter packs. */
6494 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6496 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6498 ++post_variadic_parms;
6499 if (template_parameter_pack_p (tparm))
6503 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6504 /* If there are no parameters that follow a parameter pack, we need to
6505 expand any argument packs so that we can deduce a parameter pack from
6506 some non-packed args followed by an argument pack, as in variadic85.C.
6507 If there are such parameters, we need to leave argument packs intact
6508 so the arguments are assigned properly. This can happen when dealing
6509 with a nested class inside a partial specialization of a class
6510 template, as in variadic92.C, or when deducing a template parameter pack
6511 from a sub-declarator, as in variadic114.C. */
6512 if (!post_variadic_parms)
6513 inner_args = expand_template_argument_pack (inner_args);
6515 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6516 if ((nargs > nparms && !variadic_p)
6517 || (nargs < nparms - variadic_p
6519 && !any_pack_expanson_args_p (inner_args)
6520 && (!use_default_args
6521 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6522 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6524 if (complain & tf_error)
6528 nparms -= variadic_p;
6529 error ("wrong number of template arguments "
6530 "(%d, should be %d or more)", nargs, nparms);
6533 error ("wrong number of template arguments "
6534 "(%d, should be %d)", nargs, nparms);
6537 error ("provided for %q+D", in_decl);
6540 return error_mark_node;
6543 /* We need to evaluate the template arguments, even though this
6544 template-id may be nested within a "sizeof". */
6545 saved_unevaluated_operand = cp_unevaluated_operand;
6546 cp_unevaluated_operand = 0;
6547 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6548 c_inhibit_evaluation_warnings = 0;
6549 new_inner_args = make_tree_vec (nparms);
6550 new_args = add_outermost_template_args (args, new_inner_args);
6551 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6556 /* Get the Ith template parameter. */
6557 parm = TREE_VEC_ELT (parms, parm_idx);
6559 if (parm == error_mark_node)
6561 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6565 /* Calculate the next argument. */
6566 if (arg_idx < nargs)
6567 arg = TREE_VEC_ELT (inner_args, arg_idx);
6571 if (template_parameter_pack_p (TREE_VALUE (parm))
6572 && !(arg && ARGUMENT_PACK_P (arg)))
6574 /* All remaining arguments will be placed in the
6575 template parameter pack PARM. */
6576 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6577 inner_args, arg_idx,
6581 /* Store this argument. */
6582 if (arg == error_mark_node)
6584 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6586 /* We are done with all of the arguments. */
6593 if (PACK_EXPANSION_P (arg))
6595 /* We don't know how many args we have yet, just
6596 use the unconverted ones for now. */
6597 new_inner_args = inner_args;
6601 else if (require_all_args)
6603 /* There must be a default arg in this case. */
6604 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6606 /* The position of the first default template argument,
6607 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6609 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6610 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6615 if (arg == error_mark_node)
6617 if (complain & tf_error)
6618 error ("template argument %d is invalid", arg_idx + 1);
6621 /* This only occurs if there was an error in the template
6622 parameter list itself (which we would already have
6623 reported) that we are trying to recover from, e.g., a class
6624 template with a parameter list such as
6625 template<typename..., typename>. */
6628 arg = convert_template_argument (TREE_VALUE (parm),
6629 arg, new_args, complain,
6632 if (arg == error_mark_node)
6634 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6636 cp_unevaluated_operand = saved_unevaluated_operand;
6637 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6640 return error_mark_node;
6642 #ifdef ENABLE_CHECKING
6643 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6644 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6645 TREE_VEC_LENGTH (new_inner_args));
6648 return new_inner_args;
6651 /* Returns 1 if template args OT and NT are equivalent. */
6654 template_args_equal (tree ot, tree nt)
6658 if (nt == NULL_TREE || ot == NULL_TREE)
6661 if (TREE_CODE (nt) == TREE_VEC)
6662 /* For member templates */
6663 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6664 else if (PACK_EXPANSION_P (ot))
6665 return (PACK_EXPANSION_P (nt)
6666 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6667 PACK_EXPANSION_PATTERN (nt))
6668 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
6669 PACK_EXPANSION_EXTRA_ARGS (nt)));
6670 else if (ARGUMENT_PACK_P (ot))
6675 if (!ARGUMENT_PACK_P (nt))
6678 opack = ARGUMENT_PACK_ARGS (ot);
6679 npack = ARGUMENT_PACK_ARGS (nt);
6680 len = TREE_VEC_LENGTH (opack);
6681 if (TREE_VEC_LENGTH (npack) != len)
6683 for (i = 0; i < len; ++i)
6684 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6685 TREE_VEC_ELT (npack, i)))
6689 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6691 /* We get here probably because we are in the middle of substituting
6692 into the pattern of a pack expansion. In that case the
6693 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6694 interested in. So we want to use the initial pack argument for
6696 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6697 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6698 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6699 return template_args_equal (ot, nt);
6701 else if (TYPE_P (nt))
6702 return TYPE_P (ot) && same_type_p (ot, nt);
6703 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6706 return cp_tree_equal (ot, nt);
6709 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6710 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6711 NEWARG_PTR with the offending arguments if they are non-NULL. */
6714 comp_template_args_with_info (tree oldargs, tree newargs,
6715 tree *oldarg_ptr, tree *newarg_ptr)
6719 if (oldargs == newargs)
6722 if (!oldargs || !newargs)
6725 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6728 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6730 tree nt = TREE_VEC_ELT (newargs, i);
6731 tree ot = TREE_VEC_ELT (oldargs, i);
6733 if (! template_args_equal (ot, nt))
6735 if (oldarg_ptr != NULL)
6737 if (newarg_ptr != NULL)
6745 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6746 of template arguments. Returns 0 otherwise. */
6749 comp_template_args (tree oldargs, tree newargs)
6751 return comp_template_args_with_info (oldargs, newargs, NULL, NULL);
6755 add_pending_template (tree d)
6757 tree ti = (TYPE_P (d)
6758 ? CLASSTYPE_TEMPLATE_INFO (d)
6759 : DECL_TEMPLATE_INFO (d));
6760 struct pending_template *pt;
6763 if (TI_PENDING_TEMPLATE_FLAG (ti))
6766 /* We are called both from instantiate_decl, where we've already had a
6767 tinst_level pushed, and instantiate_template, where we haven't.
6769 level = !current_tinst_level || current_tinst_level->decl != d;
6772 push_tinst_level (d);
6774 pt = ggc_alloc_pending_template ();
6776 pt->tinst = current_tinst_level;
6777 if (last_pending_template)
6778 last_pending_template->next = pt;
6780 pending_templates = pt;
6782 last_pending_template = pt;
6784 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6791 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6792 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6793 documentation for TEMPLATE_ID_EXPR. */
6796 lookup_template_function (tree fns, tree arglist)
6800 if (fns == error_mark_node || arglist == error_mark_node)
6801 return error_mark_node;
6803 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6805 if (!is_overloaded_fn (fns) && TREE_CODE (fns) != IDENTIFIER_NODE)
6807 error ("%q#D is not a function template", fns);
6808 return error_mark_node;
6811 if (BASELINK_P (fns))
6813 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6815 BASELINK_FUNCTIONS (fns),
6820 type = TREE_TYPE (fns);
6821 if (TREE_CODE (fns) == OVERLOAD || !type)
6822 type = unknown_type_node;
6824 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6827 /* Within the scope of a template class S<T>, the name S gets bound
6828 (in build_self_reference) to a TYPE_DECL for the class, not a
6829 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6830 or one of its enclosing classes, and that type is a template,
6831 return the associated TEMPLATE_DECL. Otherwise, the original
6834 Also handle the case when DECL is a TREE_LIST of ambiguous
6835 injected-class-names from different bases. */
6838 maybe_get_template_decl_from_type_decl (tree decl)
6840 if (decl == NULL_TREE)
6843 /* DR 176: A lookup that finds an injected-class-name (10.2
6844 [class.member.lookup]) can result in an ambiguity in certain cases
6845 (for example, if it is found in more than one base class). If all of
6846 the injected-class-names that are found refer to specializations of
6847 the same class template, and if the name is followed by a
6848 template-argument-list, the reference refers to the class template
6849 itself and not a specialization thereof, and is not ambiguous. */
6850 if (TREE_CODE (decl) == TREE_LIST)
6852 tree t, tmpl = NULL_TREE;
6853 for (t = decl; t; t = TREE_CHAIN (t))
6855 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6858 else if (tmpl != elt)
6861 if (tmpl && t == NULL_TREE)
6867 return (decl != NULL_TREE
6868 && DECL_SELF_REFERENCE_P (decl)
6869 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6870 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
6873 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6874 parameters, find the desired type.
6876 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6878 IN_DECL, if non-NULL, is the template declaration we are trying to
6881 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6882 the class we are looking up.
6884 Issue error and warning messages under control of COMPLAIN.
6886 If the template class is really a local class in a template
6887 function, then the FUNCTION_CONTEXT is the function in which it is
6890 ??? Note that this function is currently called *twice* for each
6891 template-id: the first time from the parser, while creating the
6892 incomplete type (finish_template_type), and the second type during the
6893 real instantiation (instantiate_template_class). This is surely something
6894 that we want to avoid. It also causes some problems with argument
6895 coercion (see convert_nontype_argument for more information on this). */
6898 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
6899 int entering_scope, tsubst_flags_t complain)
6901 tree templ = NULL_TREE, parmlist;
6908 if (TREE_CODE (d1) == IDENTIFIER_NODE)
6910 tree value = innermost_non_namespace_value (d1);
6911 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
6916 push_decl_namespace (context);
6917 templ = lookup_name (d1);
6918 templ = maybe_get_template_decl_from_type_decl (templ);
6920 pop_decl_namespace ();
6923 context = DECL_CONTEXT (templ);
6925 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
6927 tree type = TREE_TYPE (d1);
6929 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6930 an implicit typename for the second A. Deal with it. */
6931 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
6932 type = TREE_TYPE (type);
6934 if (CLASSTYPE_TEMPLATE_INFO (type))
6936 templ = CLASSTYPE_TI_TEMPLATE (type);
6937 d1 = DECL_NAME (templ);
6940 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6941 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
6943 templ = TYPE_TI_TEMPLATE (d1);
6944 d1 = DECL_NAME (templ);
6946 else if (TREE_CODE (d1) == TEMPLATE_DECL
6947 && DECL_TEMPLATE_RESULT (d1)
6948 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
6951 d1 = DECL_NAME (templ);
6952 context = DECL_CONTEXT (templ);
6955 /* Issue an error message if we didn't find a template. */
6958 if (complain & tf_error)
6959 error ("%qT is not a template", d1);
6960 return error_mark_node;
6963 if (TREE_CODE (templ) != TEMPLATE_DECL
6964 /* Make sure it's a user visible template, if it was named by
6966 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
6967 && !PRIMARY_TEMPLATE_P (templ)))
6969 if (complain & tf_error)
6971 error ("non-template type %qT used as a template", d1);
6973 error ("for template declaration %q+D", in_decl);
6975 return error_mark_node;
6978 complain &= ~tf_user;
6980 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
6982 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6983 template arguments */
6989 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6991 /* Consider an example where a template template parameter declared as
6993 template <class T, class U = std::allocator<T> > class TT
6995 The template parameter level of T and U are one level larger than
6996 of TT. To proper process the default argument of U, say when an
6997 instantiation `TT<int>' is seen, we need to build the full
6998 arguments containing {int} as the innermost level. Outer levels,
6999 available when not appearing as default template argument, can be
7000 obtained from the arguments of the enclosing template.
7002 Suppose that TT is later substituted with std::vector. The above
7003 instantiation is `TT<int, std::allocator<T> >' with TT at
7004 level 1, and T at level 2, while the template arguments at level 1
7005 becomes {std::vector} and the inner level 2 is {int}. */
7007 outer = DECL_CONTEXT (templ);
7009 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7010 else if (current_template_parms)
7011 /* This is an argument of the current template, so we haven't set
7012 DECL_CONTEXT yet. */
7013 outer = current_template_args ();
7016 arglist = add_to_template_args (outer, arglist);
7018 arglist2 = coerce_template_parms (parmlist, arglist, templ,
7020 /*require_all_args=*/true,
7021 /*use_default_args=*/true);
7022 if (arglist2 == error_mark_node
7023 || (!uses_template_parms (arglist2)
7024 && check_instantiated_args (templ, arglist2, complain)))
7025 return error_mark_node;
7027 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
7032 tree template_type = TREE_TYPE (templ);
7035 tree found = NULL_TREE;
7038 int is_dependent_type;
7039 int use_partial_inst_tmpl = false;
7041 if (template_type == error_mark_node)
7042 /* An error occured while building the template TEMPL, and a
7043 diagnostic has most certainly been emitted for that
7044 already. Let's propagate that error. */
7045 return error_mark_node;
7047 gen_tmpl = most_general_template (templ);
7048 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
7049 parm_depth = TMPL_PARMS_DEPTH (parmlist);
7050 arg_depth = TMPL_ARGS_DEPTH (arglist);
7052 if (arg_depth == 1 && parm_depth > 1)
7054 /* We've been given an incomplete set of template arguments.
7057 template <class T> struct S1 {
7058 template <class U> struct S2 {};
7059 template <class U> struct S2<U*> {};
7062 we will be called with an ARGLIST of `U*', but the
7063 TEMPLATE will be `template <class T> template
7064 <class U> struct S1<T>::S2'. We must fill in the missing
7067 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7069 arg_depth = TMPL_ARGS_DEPTH (arglist);
7072 /* Now we should have enough arguments. */
7073 gcc_assert (parm_depth == arg_depth);
7075 /* From here on, we're only interested in the most general
7078 /* Calculate the BOUND_ARGS. These will be the args that are
7079 actually tsubst'd into the definition to create the
7083 /* We have multiple levels of arguments to coerce, at once. */
7085 int saved_depth = TMPL_ARGS_DEPTH (arglist);
7087 tree bound_args = make_tree_vec (parm_depth);
7089 for (i = saved_depth,
7090 t = DECL_TEMPLATE_PARMS (gen_tmpl);
7091 i > 0 && t != NULL_TREE;
7092 --i, t = TREE_CHAIN (t))
7095 if (i == saved_depth)
7096 a = coerce_template_parms (TREE_VALUE (t),
7099 /*require_all_args=*/true,
7100 /*use_default_args=*/true);
7102 /* Outer levels should have already been coerced. */
7103 a = TMPL_ARGS_LEVEL (arglist, i);
7105 /* Don't process further if one of the levels fails. */
7106 if (a == error_mark_node)
7108 /* Restore the ARGLIST to its full size. */
7109 TREE_VEC_LENGTH (arglist) = saved_depth;
7110 return error_mark_node;
7113 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
7115 /* We temporarily reduce the length of the ARGLIST so
7116 that coerce_template_parms will see only the arguments
7117 corresponding to the template parameters it is
7119 TREE_VEC_LENGTH (arglist)--;
7122 /* Restore the ARGLIST to its full size. */
7123 TREE_VEC_LENGTH (arglist) = saved_depth;
7125 arglist = bound_args;
7129 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
7130 INNERMOST_TEMPLATE_ARGS (arglist),
7133 /*require_all_args=*/true,
7134 /*use_default_args=*/true);
7136 if (arglist == error_mark_node)
7137 /* We were unable to bind the arguments. */
7138 return error_mark_node;
7140 /* In the scope of a template class, explicit references to the
7141 template class refer to the type of the template, not any
7142 instantiation of it. For example, in:
7144 template <class T> class C { void f(C<T>); }
7146 the `C<T>' is just the same as `C'. Outside of the
7147 class, however, such a reference is an instantiation. */
7149 || !PRIMARY_TEMPLATE_P (gen_tmpl)
7150 || currently_open_class (template_type))
7151 /* comp_template_args is expensive, check it last. */
7152 && comp_template_args (TYPE_TI_ARGS (template_type),
7154 return template_type;
7156 /* If we already have this specialization, return it. */
7157 elt.tmpl = gen_tmpl;
7159 hash = hash_specialization (&elt);
7160 entry = (spec_entry *) htab_find_with_hash (type_specializations,
7166 is_dependent_type = uses_template_parms (arglist);
7168 /* If the deduced arguments are invalid, then the binding
7170 if (!is_dependent_type
7171 && check_instantiated_args (gen_tmpl,
7172 INNERMOST_TEMPLATE_ARGS (arglist),
7174 return error_mark_node;
7176 if (!is_dependent_type
7177 && !PRIMARY_TEMPLATE_P (gen_tmpl)
7178 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
7179 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
7181 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
7182 DECL_NAME (gen_tmpl),
7183 /*tag_scope=*/ts_global);
7187 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7189 if (context == error_mark_node)
7190 return error_mark_node;
7193 context = global_namespace;
7195 /* Create the type. */
7196 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7198 if (!is_dependent_type)
7200 set_current_access_from_decl (TYPE_NAME (template_type));
7201 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7202 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7203 arglist, complain, in_decl),
7204 SCOPED_ENUM_P (template_type), NULL);
7208 /* We don't want to call start_enum for this type, since
7209 the values for the enumeration constants may involve
7210 template parameters. And, no one should be interested
7211 in the enumeration constants for such a type. */
7212 t = cxx_make_type (ENUMERAL_TYPE);
7213 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7215 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7216 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7217 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7219 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
7221 /* The user referred to a specialization of an alias
7222 template represented by GEN_TMPL.
7224 [temp.alias]/2 says:
7226 When a template-id refers to the specialization of an
7227 alias template, it is equivalent to the associated
7228 type obtained by substitution of its
7229 template-arguments for the template-parameters in the
7230 type-id of the alias template. */
7232 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
7233 /* Note that the call above (by indirectly calling
7234 register_specialization in tsubst_decl) registers the
7235 TYPE_DECL representing the specialization of the alias
7236 template. So next time someone substitutes ARGLIST for
7237 the template parms into the alias template (GEN_TMPL),
7238 she'll get that TYPE_DECL back. */
7240 if (t == error_mark_node)
7243 else if (CLASS_TYPE_P (template_type))
7245 t = make_class_type (TREE_CODE (template_type));
7246 CLASSTYPE_DECLARED_CLASS (t)
7247 = CLASSTYPE_DECLARED_CLASS (template_type);
7248 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7249 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7251 /* A local class. Make sure the decl gets registered properly. */
7252 if (context == current_function_decl)
7253 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_global);
7255 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7256 /* This instantiation is another name for the primary
7257 template type. Set the TYPE_CANONICAL field
7259 TYPE_CANONICAL (t) = template_type;
7260 else if (any_template_arguments_need_structural_equality_p (arglist))
7261 /* Some of the template arguments require structural
7262 equality testing, so this template class requires
7263 structural equality testing. */
7264 SET_TYPE_STRUCTURAL_EQUALITY (t);
7269 /* If we called start_enum or pushtag above, this information
7270 will already be set up. */
7273 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7275 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7276 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7277 DECL_SOURCE_LOCATION (type_decl)
7278 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7281 type_decl = TYPE_NAME (t);
7283 if (CLASS_TYPE_P (template_type))
7285 TREE_PRIVATE (type_decl)
7286 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
7287 TREE_PROTECTED (type_decl)
7288 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
7289 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7291 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7292 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7296 /* Let's consider the explicit specialization of a member
7297 of a class template specialization that is implicitely instantiated,
7302 template<class U> struct M {}; //#0
7307 struct S<int>::M<char> //#1
7311 [temp.expl.spec]/4 says this is valid.
7313 In this case, when we write:
7316 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7319 When we encounter #1, we want to store the partial instantiation
7320 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7322 For all cases other than this "explicit specialization of member of a
7323 class template", we just want to store the most general template into
7324 the CLASSTYPE_TI_TEMPLATE of M.
7326 This case of "explicit specialization of member of a class template"
7328 1/ the enclosing class is an instantiation of, and therefore not
7329 the same as, the context of the most general template, and
7330 2/ we aren't looking at the partial instantiation itself, i.e.
7331 the innermost arguments are not the same as the innermost parms of
7332 the most general template.
7334 So it's only when 1/ and 2/ happens that we want to use the partial
7335 instantiation of the member template in lieu of its most general
7338 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7339 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7340 /* the enclosing class must be an instantiation... */
7341 && CLASS_TYPE_P (context)
7342 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7344 tree partial_inst_args;
7345 TREE_VEC_LENGTH (arglist)--;
7346 ++processing_template_decl;
7348 tsubst (INNERMOST_TEMPLATE_ARGS
7349 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7350 arglist, complain, NULL_TREE);
7351 --processing_template_decl;
7352 TREE_VEC_LENGTH (arglist)++;
7353 use_partial_inst_tmpl =
7354 /*...and we must not be looking at the partial instantiation
7356 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7360 if (!use_partial_inst_tmpl)
7361 /* This case is easy; there are no member templates involved. */
7365 /* This is a full instantiation of a member template. Find
7366 the partial instantiation of which this is an instance. */
7368 /* Temporarily reduce by one the number of levels in the ARGLIST
7369 so as to avoid comparing the last set of arguments. */
7370 TREE_VEC_LENGTH (arglist)--;
7371 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7372 TREE_VEC_LENGTH (arglist)++;
7373 /* FOUND is either a proper class type, or an alias
7374 template specialization. In the later case, it's a
7375 TYPE_DECL, resulting from the substituting of arguments
7376 for parameters in the TYPE_DECL of the alias template
7377 done earlier. So be careful while getting the template
7379 found = TREE_CODE (found) == TYPE_DECL
7380 ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
7381 : CLASSTYPE_TI_TEMPLATE (found);
7384 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7387 slot = htab_find_slot_with_hash (type_specializations,
7388 &elt, hash, INSERT);
7389 entry = ggc_alloc_spec_entry ();
7393 /* Note this use of the partial instantiation so we can check it
7394 later in maybe_process_partial_specialization. */
7395 DECL_TEMPLATE_INSTANTIATIONS (templ)
7396 = tree_cons (arglist, t,
7397 DECL_TEMPLATE_INSTANTIATIONS (templ));
7399 if (TREE_CODE (t) == ENUMERAL_TYPE && !is_dependent_type)
7400 /* Now that the type has been registered on the instantiations
7401 list, we set up the enumerators. Because the enumeration
7402 constants may involve the enumeration type itself, we make
7403 sure to register the type first, and then create the
7404 constants. That way, doing tsubst_expr for the enumeration
7405 constants won't result in recursive calls here; we'll find
7406 the instantiation and exit above. */
7407 tsubst_enum (template_type, t, arglist);
7409 if (CLASS_TYPE_P (template_type) && is_dependent_type)
7410 /* If the type makes use of template parameters, the
7411 code that generates debugging information will crash. */
7412 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
7414 /* Possibly limit visibility based on template args. */
7415 TREE_PUBLIC (type_decl) = 1;
7416 determine_visibility (type_decl);
7422 /* Wrapper for lookup_template_class_1. */
7425 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7426 int entering_scope, tsubst_flags_t complain)
7429 timevar_push (TV_TEMPLATE_INST);
7430 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7431 entering_scope, complain);
7432 timevar_pop (TV_TEMPLATE_INST);
7440 /* True when we should also visit template parameters that occur in
7441 non-deduced contexts. */
7442 bool include_nondeduced_p;
7443 struct pointer_set_t *visited;
7446 /* Called from for_each_template_parm via walk_tree. */
7449 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7452 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7453 tree_fn_t fn = pfd->fn;
7454 void *data = pfd->data;
7457 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7458 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7459 pfd->include_nondeduced_p))
7460 return error_mark_node;
7462 switch (TREE_CODE (t))
7465 if (TYPE_PTRMEMFUNC_P (t))
7471 if (!TYPE_TEMPLATE_INFO (t))
7473 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
7474 fn, data, pfd->visited,
7475 pfd->include_nondeduced_p))
7476 return error_mark_node;
7480 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7481 fn, data, pfd->visited,
7482 pfd->include_nondeduced_p)
7483 || for_each_template_parm (TYPE_MAX_VALUE (t),
7484 fn, data, pfd->visited,
7485 pfd->include_nondeduced_p))
7486 return error_mark_node;
7490 /* Since we're not going to walk subtrees, we have to do this
7492 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7493 pfd->visited, pfd->include_nondeduced_p))
7494 return error_mark_node;
7498 /* Check the return type. */
7499 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7500 pfd->include_nondeduced_p))
7501 return error_mark_node;
7503 /* Check the parameter types. Since default arguments are not
7504 instantiated until they are needed, the TYPE_ARG_TYPES may
7505 contain expressions that involve template parameters. But,
7506 no-one should be looking at them yet. And, once they're
7507 instantiated, they don't contain template parameters, so
7508 there's no point in looking at them then, either. */
7512 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7513 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7514 pfd->visited, pfd->include_nondeduced_p))
7515 return error_mark_node;
7517 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7518 want walk_tree walking into them itself. */
7524 case UNDERLYING_TYPE:
7525 if (pfd->include_nondeduced_p
7526 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7528 pfd->include_nondeduced_p))
7529 return error_mark_node;
7534 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7535 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7536 pfd->visited, pfd->include_nondeduced_p))
7537 return error_mark_node;
7542 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7543 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7544 pfd->visited, pfd->include_nondeduced_p))
7545 return error_mark_node;
7546 if (DECL_CONTEXT (t)
7547 && pfd->include_nondeduced_p
7548 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7549 pfd->visited, pfd->include_nondeduced_p))
7550 return error_mark_node;
7553 case BOUND_TEMPLATE_TEMPLATE_PARM:
7554 /* Record template parameters such as `T' inside `TT<T>'. */
7555 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7556 pfd->include_nondeduced_p))
7557 return error_mark_node;
7560 case TEMPLATE_TEMPLATE_PARM:
7561 case TEMPLATE_TYPE_PARM:
7562 case TEMPLATE_PARM_INDEX:
7563 if (fn && (*fn)(t, data))
7564 return error_mark_node;
7566 return error_mark_node;
7570 /* A template template parameter is encountered. */
7571 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7572 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7573 pfd->include_nondeduced_p))
7574 return error_mark_node;
7576 /* Already substituted template template parameter */
7582 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7584 pfd->include_nondeduced_p))
7585 return error_mark_node;
7589 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7590 && pfd->include_nondeduced_p
7591 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7592 (TREE_TYPE (t)), fn, data,
7593 pfd->visited, pfd->include_nondeduced_p))
7594 return error_mark_node;
7599 /* If there's no type, then this thing must be some expression
7600 involving template parameters. */
7601 if (!fn && !TREE_TYPE (t))
7602 return error_mark_node;
7607 case IMPLICIT_CONV_EXPR:
7608 case REINTERPRET_CAST_EXPR:
7609 case CONST_CAST_EXPR:
7610 case STATIC_CAST_EXPR:
7611 case DYNAMIC_CAST_EXPR:
7615 case PSEUDO_DTOR_EXPR:
7617 return error_mark_node;
7624 /* We didn't find any template parameters we liked. */
7628 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7629 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7630 call FN with the parameter and the DATA.
7631 If FN returns nonzero, the iteration is terminated, and
7632 for_each_template_parm returns 1. Otherwise, the iteration
7633 continues. If FN never returns a nonzero value, the value
7634 returned by for_each_template_parm is 0. If FN is NULL, it is
7635 considered to be the function which always returns 1.
7637 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7638 parameters that occur in non-deduced contexts. When false, only
7639 visits those template parameters that can be deduced. */
7642 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7643 struct pointer_set_t *visited,
7644 bool include_nondeduced_p)
7646 struct pair_fn_data pfd;
7652 pfd.include_nondeduced_p = include_nondeduced_p;
7654 /* Walk the tree. (Conceptually, we would like to walk without
7655 duplicates, but for_each_template_parm_r recursively calls
7656 for_each_template_parm, so we would need to reorganize a fair
7657 bit to use walk_tree_without_duplicates, so we keep our own
7660 pfd.visited = visited;
7662 pfd.visited = pointer_set_create ();
7663 result = cp_walk_tree (&t,
7664 for_each_template_parm_r,
7666 pfd.visited) != NULL_TREE;
7671 pointer_set_destroy (pfd.visited);
7678 /* Returns true if T depends on any template parameter. */
7681 uses_template_parms (tree t)
7684 int saved_processing_template_decl;
7686 saved_processing_template_decl = processing_template_decl;
7687 if (!saved_processing_template_decl)
7688 processing_template_decl = 1;
7690 dependent_p = dependent_type_p (t);
7691 else if (TREE_CODE (t) == TREE_VEC)
7692 dependent_p = any_dependent_template_arguments_p (t);
7693 else if (TREE_CODE (t) == TREE_LIST)
7694 dependent_p = (uses_template_parms (TREE_VALUE (t))
7695 || uses_template_parms (TREE_CHAIN (t)));
7696 else if (TREE_CODE (t) == TYPE_DECL)
7697 dependent_p = dependent_type_p (TREE_TYPE (t));
7700 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7701 || TREE_CODE (t) == OVERLOAD
7703 || TREE_CODE (t) == IDENTIFIER_NODE
7704 || TREE_CODE (t) == TRAIT_EXPR
7705 || TREE_CODE (t) == CONSTRUCTOR
7706 || CONSTANT_CLASS_P (t))
7707 dependent_p = (type_dependent_expression_p (t)
7708 || value_dependent_expression_p (t));
7711 gcc_assert (t == error_mark_node);
7712 dependent_p = false;
7715 processing_template_decl = saved_processing_template_decl;
7720 /* Returns true if T depends on any template parameter with level LEVEL. */
7723 uses_template_parms_level (tree t, int level)
7725 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
7726 /*include_nondeduced_p=*/true);
7729 /* Returns TRUE iff INST is an instantiation we don't need to do in an
7730 ill-formed translation unit, i.e. a variable or function that isn't
7731 usable in a constant expression. */
7734 neglectable_inst_p (tree d)
7737 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
7738 : decl_maybe_constant_var_p (d)));
7741 /* Returns TRUE iff we should refuse to instantiate DECL because it's
7742 neglectable and instantiated from within an erroneous instantiation. */
7745 limit_bad_template_recursion (tree decl)
7747 struct tinst_level *lev = current_tinst_level;
7748 int errs = errorcount + sorrycount;
7749 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
7752 for (; lev; lev = lev->next)
7753 if (neglectable_inst_p (lev->decl))
7756 return (lev && errs > lev->errors);
7759 static int tinst_depth;
7760 extern int max_tinst_depth;
7761 #ifdef GATHER_STATISTICS
7764 static GTY(()) struct tinst_level *last_error_tinst_level;
7766 /* We're starting to instantiate D; record the template instantiation context
7767 for diagnostics and to restore it later. */
7770 push_tinst_level (tree d)
7772 struct tinst_level *new_level;
7774 if (tinst_depth >= max_tinst_depth)
7776 last_error_tinst_level = current_tinst_level;
7777 if (TREE_CODE (d) == TREE_LIST)
7778 error ("template instantiation depth exceeds maximum of %d (use "
7779 "-ftemplate-depth= to increase the maximum) substituting %qS",
7780 max_tinst_depth, d);
7782 error ("template instantiation depth exceeds maximum of %d (use "
7783 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7784 max_tinst_depth, d);
7786 print_instantiation_context ();
7791 /* If the current instantiation caused problems, don't let it instantiate
7792 anything else. Do allow deduction substitution and decls usable in
7793 constant expressions. */
7794 if (limit_bad_template_recursion (d))
7797 new_level = ggc_alloc_tinst_level ();
7798 new_level->decl = d;
7799 new_level->locus = input_location;
7800 new_level->errors = errorcount+sorrycount;
7801 new_level->in_system_header_p = in_system_header;
7802 new_level->next = current_tinst_level;
7803 current_tinst_level = new_level;
7806 #ifdef GATHER_STATISTICS
7807 if (tinst_depth > depth_reached)
7808 depth_reached = tinst_depth;
7814 /* We're done instantiating this template; return to the instantiation
7818 pop_tinst_level (void)
7820 /* Restore the filename and line number stashed away when we started
7821 this instantiation. */
7822 input_location = current_tinst_level->locus;
7823 current_tinst_level = current_tinst_level->next;
7827 /* We're instantiating a deferred template; restore the template
7828 instantiation context in which the instantiation was requested, which
7829 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7832 reopen_tinst_level (struct tinst_level *level)
7834 struct tinst_level *t;
7837 for (t = level; t; t = t->next)
7840 current_tinst_level = level;
7842 if (current_tinst_level)
7843 current_tinst_level->errors = errorcount+sorrycount;
7847 /* Returns the TINST_LEVEL which gives the original instantiation
7850 struct tinst_level *
7851 outermost_tinst_level (void)
7853 struct tinst_level *level = current_tinst_level;
7856 level = level->next;
7860 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
7863 parameter_of_template_p (tree parm, tree templ)
7868 if (!parm || !templ)
7871 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
7872 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7874 parms = DECL_TEMPLATE_PARMS (templ);
7875 parms = INNERMOST_TEMPLATE_PARMS (parms);
7877 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
7879 tree p = TREE_VALUE (TREE_VEC_ELT (parms, i));
7880 if (p == error_mark_node)
7884 || (DECL_INITIAL (parm)
7885 && DECL_INITIAL (parm) == DECL_INITIAL (p)))
7892 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7893 vector of template arguments, as for tsubst.
7895 Returns an appropriate tsubst'd friend declaration. */
7898 tsubst_friend_function (tree decl, tree args)
7902 if (TREE_CODE (decl) == FUNCTION_DECL
7903 && DECL_TEMPLATE_INSTANTIATION (decl)
7904 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
7905 /* This was a friend declared with an explicit template
7906 argument list, e.g.:
7910 to indicate that f was a template instantiation, not a new
7911 function declaration. Now, we have to figure out what
7912 instantiation of what template. */
7914 tree template_id, arglist, fns;
7917 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
7919 /* Friend functions are looked up in the containing namespace scope.
7920 We must enter that scope, to avoid finding member functions of the
7921 current class with same name. */
7922 push_nested_namespace (ns);
7923 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
7924 tf_warning_or_error, NULL_TREE,
7925 /*integral_constant_expression_p=*/false);
7926 pop_nested_namespace (ns);
7927 arglist = tsubst (DECL_TI_ARGS (decl), args,
7928 tf_warning_or_error, NULL_TREE);
7929 template_id = lookup_template_function (fns, arglist);
7931 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7932 tmpl = determine_specialization (template_id, new_friend,
7934 /*need_member_template=*/0,
7935 TREE_VEC_LENGTH (args),
7937 return instantiate_template (tmpl, new_args, tf_error);
7940 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7942 /* The NEW_FRIEND will look like an instantiation, to the
7943 compiler, but is not an instantiation from the point of view of
7944 the language. For example, we might have had:
7946 template <class T> struct S {
7947 template <class U> friend void f(T, U);
7950 Then, in S<int>, template <class U> void f(int, U) is not an
7951 instantiation of anything. */
7952 if (new_friend == error_mark_node)
7953 return error_mark_node;
7955 DECL_USE_TEMPLATE (new_friend) = 0;
7956 if (TREE_CODE (decl) == TEMPLATE_DECL)
7958 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
7959 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
7960 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
7963 /* The mangled name for the NEW_FRIEND is incorrect. The function
7964 is not a template instantiation and should not be mangled like
7965 one. Therefore, we forget the mangling here; we'll recompute it
7966 later if we need it. */
7967 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
7969 SET_DECL_RTL (new_friend, NULL);
7970 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
7973 if (DECL_NAMESPACE_SCOPE_P (new_friend))
7976 tree new_friend_template_info;
7977 tree new_friend_result_template_info;
7979 int new_friend_is_defn;
7981 /* We must save some information from NEW_FRIEND before calling
7982 duplicate decls since that function will free NEW_FRIEND if
7984 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
7985 new_friend_is_defn =
7986 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7987 (template_for_substitution (new_friend)))
7989 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
7991 /* This declaration is a `primary' template. */
7992 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
7994 new_friend_result_template_info
7995 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
7998 new_friend_result_template_info = NULL_TREE;
8000 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8001 if (new_friend_is_defn)
8002 DECL_INITIAL (new_friend) = error_mark_node;
8004 /* Inside pushdecl_namespace_level, we will push into the
8005 current namespace. However, the friend function should go
8006 into the namespace of the template. */
8007 ns = decl_namespace_context (new_friend);
8008 push_nested_namespace (ns);
8009 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
8010 pop_nested_namespace (ns);
8012 if (old_decl == error_mark_node)
8013 return error_mark_node;
8015 if (old_decl != new_friend)
8017 /* This new friend declaration matched an existing
8018 declaration. For example, given:
8020 template <class T> void f(T);
8021 template <class U> class C {
8022 template <class T> friend void f(T) {}
8025 the friend declaration actually provides the definition
8026 of `f', once C has been instantiated for some type. So,
8027 old_decl will be the out-of-class template declaration,
8028 while new_friend is the in-class definition.
8030 But, if `f' was called before this point, the
8031 instantiation of `f' will have DECL_TI_ARGS corresponding
8032 to `T' but not to `U', references to which might appear
8033 in the definition of `f'. Previously, the most general
8034 template for an instantiation of `f' was the out-of-class
8035 version; now it is the in-class version. Therefore, we
8036 run through all specialization of `f', adding to their
8037 DECL_TI_ARGS appropriately. In particular, they need a
8038 new set of outer arguments, corresponding to the
8039 arguments for this class instantiation.
8041 The same situation can arise with something like this:
8044 template <class T> class C {
8048 when `C<int>' is instantiated. Now, `f(int)' is defined
8051 if (!new_friend_is_defn)
8052 /* On the other hand, if the in-class declaration does
8053 *not* provide a definition, then we don't want to alter
8054 existing definitions. We can just leave everything
8059 tree new_template = TI_TEMPLATE (new_friend_template_info);
8060 tree new_args = TI_ARGS (new_friend_template_info);
8062 /* Overwrite whatever template info was there before, if
8063 any, with the new template information pertaining to
8065 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
8067 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8069 /* We should have called reregister_specialization in
8071 gcc_assert (retrieve_specialization (new_template,
8075 /* Instantiate it if the global has already been used. */
8076 if (DECL_ODR_USED (old_decl))
8077 instantiate_decl (old_decl, /*defer_ok=*/true,
8078 /*expl_inst_class_mem_p=*/false);
8084 /* Indicate that the old function template is a partial
8086 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
8087 = new_friend_result_template_info;
8089 gcc_assert (new_template
8090 == most_general_template (new_template));
8091 gcc_assert (new_template != old_decl);
8093 /* Reassign any specializations already in the hash table
8094 to the new more general template, and add the
8095 additional template args. */
8096 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
8100 tree spec = TREE_VALUE (t);
8103 elt.tmpl = old_decl;
8104 elt.args = DECL_TI_ARGS (spec);
8105 elt.spec = NULL_TREE;
8107 htab_remove_elt (decl_specializations, &elt);
8110 = add_outermost_template_args (new_args,
8111 DECL_TI_ARGS (spec));
8113 register_specialization
8114 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
8117 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
8121 /* The information from NEW_FRIEND has been merged into OLD_DECL
8122 by duplicate_decls. */
8123 new_friend = old_decl;
8128 tree context = DECL_CONTEXT (new_friend);
8132 template <class T> class C {
8133 template <class U> friend void C1<U>::f (); // case 1
8134 friend void C2<T>::f (); // case 2
8136 we only need to make sure CONTEXT is a complete type for
8137 case 2. To distinguish between the two cases, we note that
8138 CONTEXT of case 1 remains dependent type after tsubst while
8139 this isn't true for case 2. */
8140 ++processing_template_decl;
8141 dependent_p = dependent_type_p (context);
8142 --processing_template_decl;
8145 && !complete_type_or_else (context, NULL_TREE))
8146 return error_mark_node;
8148 if (COMPLETE_TYPE_P (context))
8150 /* Check to see that the declaration is really present, and,
8151 possibly obtain an improved declaration. */
8152 tree fn = check_classfn (context,
8153 new_friend, NULL_TREE);
8163 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8164 template arguments, as for tsubst.
8166 Returns an appropriate tsubst'd friend type or error_mark_node on
8170 tsubst_friend_class (tree friend_tmpl, tree args)
8176 context = CP_DECL_CONTEXT (friend_tmpl);
8178 if (context != global_namespace)
8180 if (TREE_CODE (context) == NAMESPACE_DECL)
8181 push_nested_namespace (context);
8183 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
8186 /* Look for a class template declaration. We look for hidden names
8187 because two friend declarations of the same template are the
8188 same. For example, in:
8191 template <typename> friend class F;
8193 template <typename> struct B {
8194 template <typename> friend class F;
8197 both F templates are the same. */
8198 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
8199 /*block_p=*/true, 0,
8200 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
8202 /* But, if we don't find one, it might be because we're in a
8203 situation like this:
8211 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8212 for `S<int>', not the TEMPLATE_DECL. */
8213 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
8215 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
8216 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
8219 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
8221 /* The friend template has already been declared. Just
8222 check to see that the declarations match, and install any new
8223 default parameters. We must tsubst the default parameters,
8224 of course. We only need the innermost template parameters
8225 because that is all that redeclare_class_template will look
8227 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
8228 > TMPL_ARGS_DEPTH (args))
8231 location_t saved_input_location;
8232 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
8233 args, tf_warning_or_error);
8235 saved_input_location = input_location;
8236 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
8237 redeclare_class_template (TREE_TYPE (tmpl), parms);
8238 input_location = saved_input_location;
8242 friend_type = TREE_TYPE (tmpl);
8246 /* The friend template has not already been declared. In this
8247 case, the instantiation of the template class will cause the
8248 injection of this template into the global scope. */
8249 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
8250 if (tmpl == error_mark_node)
8251 return error_mark_node;
8253 /* The new TMPL is not an instantiation of anything, so we
8254 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8255 the new type because that is supposed to be the corresponding
8256 template decl, i.e., TMPL. */
8257 DECL_USE_TEMPLATE (tmpl) = 0;
8258 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
8259 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
8260 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
8261 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
8263 /* Inject this template into the global scope. */
8264 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
8267 if (context != global_namespace)
8269 if (TREE_CODE (context) == NAMESPACE_DECL)
8270 pop_nested_namespace (context);
8272 pop_nested_class ();
8278 /* Returns zero if TYPE cannot be completed later due to circularity.
8279 Otherwise returns one. */
8282 can_complete_type_without_circularity (tree type)
8284 if (type == NULL_TREE || type == error_mark_node)
8286 else if (COMPLETE_TYPE_P (type))
8288 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8289 return can_complete_type_without_circularity (TREE_TYPE (type));
8290 else if (CLASS_TYPE_P (type)
8291 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8297 /* Apply any attributes which had to be deferred until instantiation
8298 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8299 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8302 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8303 tree args, tsubst_flags_t complain, tree in_decl)
8305 tree last_dep = NULL_TREE;
8309 for (t = attributes; t; t = TREE_CHAIN (t))
8310 if (ATTR_IS_DEPENDENT (t))
8313 attributes = copy_list (attributes);
8317 if (DECL_P (*decl_p))
8319 if (TREE_TYPE (*decl_p) == error_mark_node)
8321 p = &DECL_ATTRIBUTES (*decl_p);
8324 p = &TYPE_ATTRIBUTES (*decl_p);
8328 tree late_attrs = NULL_TREE;
8329 tree *q = &late_attrs;
8331 for (*p = attributes; *p; )
8334 if (ATTR_IS_DEPENDENT (t))
8336 *p = TREE_CHAIN (t);
8337 TREE_CHAIN (t) = NULL_TREE;
8338 /* If the first attribute argument is an identifier, don't
8339 pass it through tsubst. Attributes like mode, format,
8340 cleanup and several target specific attributes expect it
8343 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
8344 && TREE_VALUE (TREE_VALUE (t))
8345 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
8346 == IDENTIFIER_NODE))
8349 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8351 /*integral_constant_expression_p=*/false);
8352 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8354 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8359 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8360 /*integral_constant_expression_p=*/false);
8362 q = &TREE_CHAIN (t);
8365 p = &TREE_CHAIN (t);
8368 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8372 /* Perform (or defer) access check for typedefs that were referenced
8373 from within the template TMPL code.
8374 This is a subroutine of instantiate_template and instantiate_class_template.
8375 TMPL is the template to consider and TARGS is the list of arguments of
8379 perform_typedefs_access_check (tree tmpl, tree targs)
8381 location_t saved_location;
8383 qualified_typedef_usage_t *iter;
8386 || (!CLASS_TYPE_P (tmpl)
8387 && TREE_CODE (tmpl) != FUNCTION_DECL))
8390 saved_location = input_location;
8391 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
8392 get_types_needing_access_check (tmpl),
8395 tree type_decl = iter->typedef_decl;
8396 tree type_scope = iter->context;
8398 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8401 if (uses_template_parms (type_decl))
8402 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8403 if (uses_template_parms (type_scope))
8404 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8406 /* Make access check error messages point to the location
8407 of the use of the typedef. */
8408 input_location = iter->locus;
8409 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8410 type_decl, type_decl);
8412 input_location = saved_location;
8416 instantiate_class_template_1 (tree type)
8418 tree templ, args, pattern, t, member;
8422 unsigned int saved_maximum_field_alignment;
8425 if (type == error_mark_node)
8426 return error_mark_node;
8428 if (COMPLETE_OR_OPEN_TYPE_P (type)
8429 || uses_template_parms (type))
8432 /* Figure out which template is being instantiated. */
8433 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8434 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8436 /* Determine what specialization of the original template to
8438 t = most_specialized_class (type, templ, tf_warning_or_error);
8439 if (t == error_mark_node)
8441 TYPE_BEING_DEFINED (type) = 1;
8442 return error_mark_node;
8446 /* This TYPE is actually an instantiation of a partial
8447 specialization. We replace the innermost set of ARGS with
8448 the arguments appropriate for substitution. For example,
8451 template <class T> struct S {};
8452 template <class T> struct S<T*> {};
8454 and supposing that we are instantiating S<int*>, ARGS will
8455 presently be {int*} -- but we need {int}. */
8456 pattern = TREE_TYPE (t);
8457 args = TREE_PURPOSE (t);
8461 pattern = TREE_TYPE (templ);
8462 args = CLASSTYPE_TI_ARGS (type);
8465 /* If the template we're instantiating is incomplete, then clearly
8466 there's nothing we can do. */
8467 if (!COMPLETE_TYPE_P (pattern))
8470 /* If we've recursively instantiated too many templates, stop. */
8471 if (! push_tinst_level (type))
8474 /* Now we're really doing the instantiation. Mark the type as in
8475 the process of being defined. */
8476 TYPE_BEING_DEFINED (type) = 1;
8478 /* We may be in the middle of deferred access check. Disable
8480 push_deferring_access_checks (dk_no_deferred);
8482 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
8484 push_to_top_level ();
8485 /* Use #pragma pack from the template context. */
8486 saved_maximum_field_alignment = maximum_field_alignment;
8487 maximum_field_alignment = TYPE_PRECISION (pattern);
8489 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8491 /* Set the input location to the most specialized template definition.
8492 This is needed if tsubsting causes an error. */
8493 typedecl = TYPE_MAIN_DECL (pattern);
8494 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8495 DECL_SOURCE_LOCATION (typedecl);
8497 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8498 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8499 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8500 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8501 if (ANON_AGGR_TYPE_P (pattern))
8502 SET_ANON_AGGR_TYPE_P (type);
8503 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8505 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8506 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8507 /* Adjust visibility for template arguments. */
8508 determine_visibility (TYPE_MAIN_DECL (type));
8510 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8512 pbinfo = TYPE_BINFO (pattern);
8514 /* We should never instantiate a nested class before its enclosing
8515 class; we need to look up the nested class by name before we can
8516 instantiate it, and that lookup should instantiate the enclosing
8518 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8519 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8521 base_list = NULL_TREE;
8522 if (BINFO_N_BASE_BINFOS (pbinfo))
8528 /* We must enter the scope containing the type, as that is where
8529 the accessibility of types named in dependent bases are
8531 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8533 /* Substitute into each of the bases to determine the actual
8535 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8538 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8539 tree expanded_bases = NULL_TREE;
8542 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8545 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8546 args, tf_error, NULL_TREE);
8547 if (expanded_bases == error_mark_node)
8550 len = TREE_VEC_LENGTH (expanded_bases);
8553 for (idx = 0; idx < len; idx++)
8556 /* Extract the already-expanded base class. */
8557 base = TREE_VEC_ELT (expanded_bases, idx);
8559 /* Substitute to figure out the base class. */
8560 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8563 if (base == error_mark_node)
8566 base_list = tree_cons (access, base, base_list);
8567 if (BINFO_VIRTUAL_P (pbase_binfo))
8568 TREE_TYPE (base_list) = integer_type_node;
8572 /* The list is now in reverse order; correct that. */
8573 base_list = nreverse (base_list);
8576 pop_scope (pushed_scope);
8578 /* Now call xref_basetypes to set up all the base-class
8580 xref_basetypes (type, base_list);
8582 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8583 (int) ATTR_FLAG_TYPE_IN_PLACE,
8584 args, tf_error, NULL_TREE);
8585 fixup_attribute_variants (type);
8587 /* Now that our base classes are set up, enter the scope of the
8588 class, so that name lookups into base classes, etc. will work
8589 correctly. This is precisely analogous to what we do in
8590 begin_class_definition when defining an ordinary non-template
8591 class, except we also need to push the enclosing classes. */
8592 push_nested_class (type);
8594 /* Now members are processed in the order of declaration. */
8595 for (member = CLASSTYPE_DECL_LIST (pattern);
8596 member; member = TREE_CHAIN (member))
8598 tree t = TREE_VALUE (member);
8600 if (TREE_PURPOSE (member))
8604 /* Build new CLASSTYPE_NESTED_UTDS. */
8607 bool class_template_p;
8609 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8610 && TYPE_LANG_SPECIFIC (t)
8611 && CLASSTYPE_IS_TEMPLATE (t));
8612 /* If the member is a class template, then -- even after
8613 substitution -- there may be dependent types in the
8614 template argument list for the class. We increment
8615 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8616 that function will assume that no types are dependent
8617 when outside of a template. */
8618 if (class_template_p)
8619 ++processing_template_decl;
8620 newtag = tsubst (t, args, tf_error, NULL_TREE);
8621 if (class_template_p)
8622 --processing_template_decl;
8623 if (newtag == error_mark_node)
8626 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8628 tree name = TYPE_IDENTIFIER (t);
8630 if (class_template_p)
8631 /* Unfortunately, lookup_template_class sets
8632 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8633 instantiation (i.e., for the type of a member
8634 template class nested within a template class.)
8635 This behavior is required for
8636 maybe_process_partial_specialization to work
8637 correctly, but is not accurate in this case;
8638 the TAG is not an instantiation of anything.
8639 (The corresponding TEMPLATE_DECL is an
8640 instantiation, but the TYPE is not.) */
8641 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8643 /* Now, we call pushtag to put this NEWTAG into the scope of
8644 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8645 pushtag calling push_template_decl. We don't have to do
8646 this for enums because it will already have been done in
8649 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8650 pushtag (name, newtag, /*tag_scope=*/ts_current);
8653 else if (TREE_CODE (t) == FUNCTION_DECL
8654 || DECL_FUNCTION_TEMPLATE_P (t))
8656 /* Build new TYPE_METHODS. */
8659 if (TREE_CODE (t) == TEMPLATE_DECL)
8660 ++processing_template_decl;
8661 r = tsubst (t, args, tf_error, NULL_TREE);
8662 if (TREE_CODE (t) == TEMPLATE_DECL)
8663 --processing_template_decl;
8664 set_current_access_from_decl (r);
8665 finish_member_declaration (r);
8666 /* Instantiate members marked with attribute used. */
8667 if (r != error_mark_node && DECL_PRESERVE_P (r))
8672 /* Build new TYPE_FIELDS. */
8673 if (TREE_CODE (t) == STATIC_ASSERT)
8676 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8677 tf_warning_or_error, NULL_TREE,
8678 /*integral_constant_expression_p=*/true);
8679 finish_static_assert (condition,
8680 STATIC_ASSERT_MESSAGE (t),
8681 STATIC_ASSERT_SOURCE_LOCATION (t),
8684 else if (TREE_CODE (t) != CONST_DECL)
8688 /* The file and line for this declaration, to
8689 assist in error message reporting. Since we
8690 called push_tinst_level above, we don't need to
8692 input_location = DECL_SOURCE_LOCATION (t);
8694 if (TREE_CODE (t) == TEMPLATE_DECL)
8695 ++processing_template_decl;
8696 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8697 if (TREE_CODE (t) == TEMPLATE_DECL)
8698 --processing_template_decl;
8699 if (TREE_CODE (r) == VAR_DECL)
8703 [t]he initialization (and any associated
8704 side-effects) of a static data member does
8705 not occur unless the static data member is
8706 itself used in a way that requires the
8707 definition of the static data member to
8710 Therefore, we do not substitute into the
8711 initialized for the static data member here. */
8712 finish_static_data_member_decl
8715 /*init_const_expr_p=*/false,
8716 /*asmspec_tree=*/NULL_TREE,
8718 /* Instantiate members marked with attribute used. */
8719 if (r != error_mark_node && DECL_PRESERVE_P (r))
8722 else if (TREE_CODE (r) == FIELD_DECL)
8724 /* Determine whether R has a valid type and can be
8725 completed later. If R is invalid, then it is
8726 replaced by error_mark_node so that it will not be
8727 added to TYPE_FIELDS. */
8728 tree rtype = TREE_TYPE (r);
8729 if (can_complete_type_without_circularity (rtype))
8730 complete_type (rtype);
8732 if (!COMPLETE_TYPE_P (rtype))
8734 cxx_incomplete_type_error (r, rtype);
8735 r = error_mark_node;
8739 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8740 such a thing will already have been added to the field
8741 list by tsubst_enum in finish_member_declaration in the
8742 CLASSTYPE_NESTED_UTDS case above. */
8743 if (!(TREE_CODE (r) == TYPE_DECL
8744 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
8745 && DECL_ARTIFICIAL (r)))
8747 set_current_access_from_decl (r);
8748 finish_member_declaration (r);
8755 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
8757 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8759 tree friend_type = t;
8760 bool adjust_processing_template_decl = false;
8762 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8764 /* template <class T> friend class C; */
8765 friend_type = tsubst_friend_class (friend_type, args);
8766 adjust_processing_template_decl = true;
8768 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
8770 /* template <class T> friend class C::D; */
8771 friend_type = tsubst (friend_type, args,
8772 tf_warning_or_error, NULL_TREE);
8773 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8774 friend_type = TREE_TYPE (friend_type);
8775 adjust_processing_template_decl = true;
8777 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
8778 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
8780 /* This could be either
8784 when dependent_type_p is false or
8786 template <class U> friend class T::C;
8789 friend_type = tsubst (friend_type, args,
8790 tf_warning_or_error, NULL_TREE);
8791 /* Bump processing_template_decl for correct
8792 dependent_type_p calculation. */
8793 ++processing_template_decl;
8794 if (dependent_type_p (friend_type))
8795 adjust_processing_template_decl = true;
8796 --processing_template_decl;
8798 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
8799 && hidden_name_p (TYPE_NAME (friend_type)))
8803 where C hasn't been declared yet. Let's lookup name
8804 from namespace scope directly, bypassing any name that
8805 come from dependent base class. */
8806 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
8808 /* The call to xref_tag_from_type does injection for friend
8810 push_nested_namespace (ns);
8812 xref_tag_from_type (friend_type, NULL_TREE,
8813 /*tag_scope=*/ts_current);
8814 pop_nested_namespace (ns);
8816 else if (uses_template_parms (friend_type))
8817 /* friend class C<T>; */
8818 friend_type = tsubst (friend_type, args,
8819 tf_warning_or_error, NULL_TREE);
8824 where C is already declared or
8826 friend class C<int>;
8828 We don't have to do anything in these cases. */
8830 if (adjust_processing_template_decl)
8831 /* Trick make_friend_class into realizing that the friend
8832 we're adding is a template, not an ordinary class. It's
8833 important that we use make_friend_class since it will
8834 perform some error-checking and output cross-reference
8836 ++processing_template_decl;
8838 if (friend_type != error_mark_node)
8839 make_friend_class (type, friend_type, /*complain=*/false);
8841 if (adjust_processing_template_decl)
8842 --processing_template_decl;
8846 /* Build new DECL_FRIENDLIST. */
8849 /* The file and line for this declaration, to
8850 assist in error message reporting. Since we
8851 called push_tinst_level above, we don't need to
8853 input_location = DECL_SOURCE_LOCATION (t);
8855 if (TREE_CODE (t) == TEMPLATE_DECL)
8857 ++processing_template_decl;
8858 push_deferring_access_checks (dk_no_check);
8861 r = tsubst_friend_function (t, args);
8862 add_friend (type, r, /*complain=*/false);
8863 if (TREE_CODE (t) == TEMPLATE_DECL)
8865 pop_deferring_access_checks ();
8866 --processing_template_decl;
8872 if (CLASSTYPE_LAMBDA_EXPR (type))
8874 tree decl = lambda_function (type);
8877 tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
8878 if (LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda))
8880 apply_lambda_return_type (lambda, void_type_node);
8881 LAMBDA_EXPR_RETURN_TYPE (lambda) = NULL_TREE;
8884 instantiate_decl (decl, false, false);
8885 maybe_add_lambda_conv_op (type);
8888 gcc_assert (errorcount);
8891 /* Set the file and line number information to whatever is given for
8892 the class itself. This puts error messages involving generated
8893 implicit functions at a predictable point, and the same point
8894 that would be used for non-template classes. */
8895 input_location = DECL_SOURCE_LOCATION (typedecl);
8897 unreverse_member_declarations (type);
8898 finish_struct_1 (type);
8899 TYPE_BEING_DEFINED (type) = 0;
8901 /* We don't instantiate default arguments for member functions. 14.7.1:
8903 The implicit instantiation of a class template specialization causes
8904 the implicit instantiation of the declarations, but not of the
8905 definitions or default arguments, of the class member functions,
8906 member classes, static data members and member templates.... */
8908 /* Some typedefs referenced from within the template code need to be access
8909 checked at template instantiation time, i.e now. These types were
8910 added to the template at parsing time. Let's get those and perform
8911 the access checks then. */
8912 perform_typedefs_access_check (pattern, args);
8913 perform_deferred_access_checks ();
8914 pop_nested_class ();
8915 maximum_field_alignment = saved_maximum_field_alignment;
8917 pop_from_top_level ();
8918 pop_deferring_access_checks ();
8921 /* The vtable for a template class can be emitted in any translation
8922 unit in which the class is instantiated. When there is no key
8923 method, however, finish_struct_1 will already have added TYPE to
8924 the keyed_classes list. */
8925 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
8926 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
8931 /* Wrapper for instantiate_class_template_1. */
8934 instantiate_class_template (tree type)
8937 timevar_push (TV_TEMPLATE_INST);
8938 ret = instantiate_class_template_1 (type);
8939 timevar_pop (TV_TEMPLATE_INST);
8944 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8950 else if (TYPE_P (t))
8951 r = tsubst (t, args, complain, in_decl);
8954 if (!(complain & tf_warning))
8955 ++c_inhibit_evaluation_warnings;
8956 r = tsubst_expr (t, args, complain, in_decl,
8957 /*integral_constant_expression_p=*/true);
8958 if (!(complain & tf_warning))
8959 --c_inhibit_evaluation_warnings;
8960 /* Preserve the raw-reference nature of T. */
8961 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE
8962 && REFERENCE_REF_P (r))
8963 r = TREE_OPERAND (r, 0);
8968 /* Given a function parameter pack TMPL_PARM and some function parameters
8969 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
8970 and set *SPEC_P to point at the next point in the list. */
8973 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
8975 /* Collect all of the extra "packed" parameters into an
8979 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
8980 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
8981 tree spec_parm = *spec_p;
8984 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
8986 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
8989 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8990 parmvec = make_tree_vec (len);
8991 parmtypevec = make_tree_vec (len);
8992 spec_parm = *spec_p;
8993 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
8995 TREE_VEC_ELT (parmvec, i) = spec_parm;
8996 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
8999 /* Build the argument packs. */
9000 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
9001 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
9002 TREE_TYPE (argpack) = argtypepack;
9003 *spec_p = spec_parm;
9008 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9009 NONTYPE_ARGUMENT_PACK. */
9012 make_fnparm_pack (tree spec_parm)
9014 return extract_fnparm_pack (NULL_TREE, &spec_parm);
9017 /* Substitute ARGS into T, which is an pack expansion
9018 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9019 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9020 (if only a partial substitution could be performed) or
9021 ERROR_MARK_NODE if there was an error. */
9023 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
9027 tree pack, packs = NULL_TREE;
9028 bool unsubstituted_packs = false;
9029 bool real_packs = false;
9030 int missing_level = 0;
9033 htab_t saved_local_specializations = NULL;
9034 bool need_local_specializations = false;
9037 gcc_assert (PACK_EXPANSION_P (t));
9038 pattern = PACK_EXPANSION_PATTERN (t);
9040 /* Add in any args remembered from an earlier partial instantiation. */
9041 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
9043 levels = TMPL_ARGS_DEPTH (args);
9045 /* Determine the argument packs that will instantiate the parameter
9046 packs used in the expansion expression. While we're at it,
9047 compute the number of arguments to be expanded and make sure it
9049 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
9050 pack = TREE_CHAIN (pack))
9052 tree parm_pack = TREE_VALUE (pack);
9053 tree arg_pack = NULL_TREE;
9054 tree orig_arg = NULL_TREE;
9057 if (TREE_CODE (parm_pack) == BASES)
9059 if (BASES_DIRECT (parm_pack))
9060 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
9061 args, complain, in_decl, false));
9063 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
9064 args, complain, in_decl, false));
9066 if (TREE_CODE (parm_pack) == PARM_DECL)
9068 if (PACK_EXPANSION_LOCAL_P (t))
9069 arg_pack = retrieve_local_specialization (parm_pack);
9072 /* We can't rely on local_specializations for a parameter
9073 name used later in a function declaration (such as in a
9074 late-specified return type). Even if it exists, it might
9075 have the wrong value for a recursive call. Just make a
9076 dummy decl, since it's only used for its type. */
9077 /* Copy before tsubsting so that we don't recurse into any
9078 later PARM_DECLs. */
9079 arg_pack = tsubst_decl (copy_node (parm_pack), args, complain);
9080 if (arg_pack && FUNCTION_PARAMETER_PACK_P (arg_pack))
9081 /* Partial instantiation of the parm_pack, we can't build
9082 up an argument pack yet. */
9083 arg_pack = NULL_TREE;
9085 arg_pack = make_fnparm_pack (arg_pack);
9086 need_local_specializations = true;
9092 template_parm_level_and_index (parm_pack, &level, &idx);
9094 if (level <= levels)
9095 arg_pack = TMPL_ARG (args, level, idx);
9098 orig_arg = arg_pack;
9099 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
9100 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
9102 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
9103 /* This can only happen if we forget to expand an argument
9104 pack somewhere else. Just return an error, silently. */
9106 result = make_tree_vec (1);
9107 TREE_VEC_ELT (result, 0) = error_mark_node;
9111 if (arg_from_parm_pack_p (arg_pack, parm_pack))
9112 /* The argument pack that the parameter maps to is just an
9113 expansion of the parameter itself, such as one would find
9114 in the implicit typedef of a class inside the class itself.
9115 Consider this parameter "unsubstituted", so that we will
9116 maintain the outer pack expansion. */
9117 arg_pack = NULL_TREE;
9122 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
9124 /* Don't bother trying to do a partial substitution with
9125 incomplete packs; we'll try again after deduction. */
9126 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
9131 else if (len != my_len)
9133 if (!(complain & tf_error))
9134 /* Fail quietly. */;
9135 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
9136 error ("mismatched argument pack lengths while expanding "
9140 error ("mismatched argument pack lengths while expanding "
9143 return error_mark_node;
9146 if (TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
9147 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack),
9149 /* This isn't a real argument pack yet. */;
9153 /* Keep track of the parameter packs and their corresponding
9155 packs = tree_cons (parm_pack, arg_pack, packs);
9156 TREE_TYPE (packs) = orig_arg;
9160 /* We can't substitute for this parameter pack. We use a flag as
9161 well as the missing_level counter because function parameter
9162 packs don't have a level. */
9163 unsubstituted_packs = true;
9164 if (!missing_level || missing_level > level)
9165 missing_level = level;
9169 /* We cannot expand this expansion expression, because we don't have
9170 all of the argument packs we need. */
9171 if (unsubstituted_packs)
9175 /* We got some full packs, but we can't substitute them in until we
9176 have values for all the packs. So remember these until then. */
9179 t = make_pack_expansion (pattern);
9181 /* The call to add_to_template_args above assumes no overlap
9182 between saved args and new args, so prune away any fake
9183 args, i.e. those that satisfied arg_from_parm_pack_p above. */
9184 if (missing_level && levels >= missing_level)
9186 gcc_assert (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)
9187 && missing_level > 1);
9188 TREE_VEC_LENGTH (args) = missing_level - 1;
9189 save_args = copy_node (args);
9190 TREE_VEC_LENGTH (args) = levels;
9195 PACK_EXPANSION_EXTRA_ARGS (t) = save_args;
9199 /* There were no real arguments, we're just replacing a parameter
9200 pack with another version of itself. Substitute into the
9201 pattern and return a PACK_EXPANSION_*. The caller will need to
9203 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
9204 t = tsubst_expr (pattern, args, complain, in_decl,
9205 /*integral_constant_expression_p=*/false);
9207 t = tsubst (pattern, args, complain, in_decl);
9208 t = make_pack_expansion (t);
9213 /* We could not find any argument packs that work. */
9215 return error_mark_node;
9217 if (need_local_specializations)
9219 /* We're in a late-specified return type, so create our own local
9220 specializations table; the current table is either NULL or (in the
9221 case of recursive unification) might have bindings that we don't
9222 want to use or alter. */
9223 saved_local_specializations = local_specializations;
9224 local_specializations = htab_create (37,
9225 hash_local_specialization,
9226 eq_local_specializations,
9230 /* For each argument in each argument pack, substitute into the
9232 result = make_tree_vec (len);
9233 for (i = 0; i < len; ++i)
9235 /* For parameter pack, change the substitution of the parameter
9236 pack to the ith argument in its argument pack, then expand
9238 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9240 tree parm = TREE_PURPOSE (pack);
9243 /* Select the Ith argument from the pack. */
9244 if (TREE_CODE (parm) == PARM_DECL)
9248 arg = make_node (ARGUMENT_PACK_SELECT);
9249 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
9251 register_local_specialization (arg, parm);
9254 arg = retrieve_local_specialization (parm);
9259 template_parm_level_and_index (parm, &level, &idx);
9263 arg = make_node (ARGUMENT_PACK_SELECT);
9264 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
9265 /* Update the corresponding argument. */
9266 TMPL_ARG (args, level, idx) = arg;
9269 /* Re-use the ARGUMENT_PACK_SELECT. */
9270 arg = TMPL_ARG (args, level, idx);
9272 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
9275 /* Substitute into the PATTERN with the altered arguments. */
9276 if (!TYPE_P (pattern))
9277 TREE_VEC_ELT (result, i) =
9278 tsubst_expr (pattern, args, complain, in_decl,
9279 /*integral_constant_expression_p=*/false);
9281 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
9283 if (TREE_VEC_ELT (result, i) == error_mark_node)
9285 result = error_mark_node;
9290 /* Update ARGS to restore the substitution from parameter packs to
9291 their argument packs. */
9292 for (pack = packs; pack; pack = TREE_CHAIN (pack))
9294 tree parm = TREE_PURPOSE (pack);
9296 if (TREE_CODE (parm) == PARM_DECL)
9297 register_local_specialization (TREE_TYPE (pack), parm);
9301 template_parm_level_and_index (parm, &level, &idx);
9303 /* Update the corresponding argument. */
9304 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
9305 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
9308 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
9312 if (need_local_specializations)
9314 htab_delete (local_specializations);
9315 local_specializations = saved_local_specializations;
9321 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9322 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9323 parameter packs; all parms generated from a function parameter pack will
9324 have the same DECL_PARM_INDEX. */
9327 get_pattern_parm (tree parm, tree tmpl)
9329 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9332 if (DECL_ARTIFICIAL (parm))
9334 for (patparm = DECL_ARGUMENTS (pattern);
9335 patparm; patparm = DECL_CHAIN (patparm))
9336 if (DECL_ARTIFICIAL (patparm)
9337 && DECL_NAME (parm) == DECL_NAME (patparm))
9342 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9343 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9344 gcc_assert (DECL_PARM_INDEX (patparm)
9345 == DECL_PARM_INDEX (parm));
9351 /* Substitute ARGS into the vector or list of template arguments T. */
9354 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9357 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9360 if (t == error_mark_node)
9361 return error_mark_node;
9363 len = TREE_VEC_LENGTH (t);
9364 elts = XALLOCAVEC (tree, len);
9366 for (i = 0; i < len; i++)
9368 tree orig_arg = TREE_VEC_ELT (t, i);
9371 if (TREE_CODE (orig_arg) == TREE_VEC)
9372 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9373 else if (PACK_EXPANSION_P (orig_arg))
9375 /* Substitute into an expansion expression. */
9376 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9378 if (TREE_CODE (new_arg) == TREE_VEC)
9379 /* Add to the expanded length adjustment the number of
9380 expanded arguments. We subtract one from this
9381 measurement, because the argument pack expression
9382 itself is already counted as 1 in
9383 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9384 the argument pack is empty. */
9385 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9387 else if (ARGUMENT_PACK_P (orig_arg))
9389 /* Substitute into each of the arguments. */
9390 new_arg = TYPE_P (orig_arg)
9391 ? cxx_make_type (TREE_CODE (orig_arg))
9392 : make_node (TREE_CODE (orig_arg));
9394 SET_ARGUMENT_PACK_ARGS (
9396 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9397 args, complain, in_decl));
9399 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9400 new_arg = error_mark_node;
9402 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9403 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9405 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9407 if (TREE_TYPE (new_arg) == error_mark_node)
9408 new_arg = error_mark_node;
9412 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9414 if (new_arg == error_mark_node)
9415 return error_mark_node;
9418 if (new_arg != orig_arg)
9425 /* Make space for the expanded arguments coming from template
9427 t = make_tree_vec (len + expanded_len_adjust);
9428 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9429 arguments for a member template.
9430 In that case each TREE_VEC in ORIG_T represents a level of template
9431 arguments, and ORIG_T won't carry any non defaulted argument count.
9432 It will rather be the nested TREE_VECs that will carry one.
9433 In other words, ORIG_T carries a non defaulted argument count only
9434 if it doesn't contain any nested TREE_VEC. */
9435 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9437 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9438 count += expanded_len_adjust;
9439 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9441 for (i = 0, out = 0; i < len; i++)
9443 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9444 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9445 && TREE_CODE (elts[i]) == TREE_VEC)
9449 /* Now expand the template argument pack "in place". */
9450 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9451 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9455 TREE_VEC_ELT (t, out) = elts[i];
9463 /* Return the result of substituting ARGS into the template parameters
9464 given by PARMS. If there are m levels of ARGS and m + n levels of
9465 PARMS, then the result will contain n levels of PARMS. For
9466 example, if PARMS is `template <class T> template <class U>
9467 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9468 result will be `template <int*, double, class V>'. */
9471 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9476 /* When substituting into a template, we must set
9477 PROCESSING_TEMPLATE_DECL as the template parameters may be
9478 dependent if they are based on one-another, and the dependency
9479 predicates are short-circuit outside of templates. */
9480 ++processing_template_decl;
9482 for (new_parms = &r;
9483 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9484 new_parms = &(TREE_CHAIN (*new_parms)),
9485 parms = TREE_CHAIN (parms))
9488 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9491 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9495 if (parms == error_mark_node)
9498 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9500 if (tuple == error_mark_node)
9503 TREE_VEC_ELT (new_vec, i) =
9504 tsubst_template_parm (tuple, args, complain);
9508 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9509 - TMPL_ARGS_DEPTH (args)),
9510 new_vec, NULL_TREE);
9513 --processing_template_decl;
9518 /* Return the result of substituting ARGS into one template parameter
9519 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9520 parameter and which TREE_PURPOSE is the default argument of the
9521 template parameter. */
9524 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9526 tree default_value, parm_decl;
9528 if (args == NULL_TREE
9530 || t == error_mark_node)
9533 gcc_assert (TREE_CODE (t) == TREE_LIST);
9535 default_value = TREE_PURPOSE (t);
9536 parm_decl = TREE_VALUE (t);
9538 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9539 if (TREE_CODE (parm_decl) == PARM_DECL
9540 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9541 parm_decl = error_mark_node;
9542 default_value = tsubst_template_arg (default_value, args,
9543 complain, NULL_TREE);
9545 return build_tree_list (default_value, parm_decl);
9548 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9549 type T. If T is not an aggregate or enumeration type, it is
9550 handled as if by tsubst. IN_DECL is as for tsubst. If
9551 ENTERING_SCOPE is nonzero, T is the context for a template which
9552 we are presently tsubst'ing. Return the substituted value. */
9555 tsubst_aggr_type (tree t,
9557 tsubst_flags_t complain,
9564 switch (TREE_CODE (t))
9567 if (TYPE_PTRMEMFUNC_P (t))
9568 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9570 /* Else fall through. */
9573 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9578 int saved_unevaluated_operand;
9579 int saved_inhibit_evaluation_warnings;
9581 /* In "sizeof(X<I>)" we need to evaluate "I". */
9582 saved_unevaluated_operand = cp_unevaluated_operand;
9583 cp_unevaluated_operand = 0;
9584 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9585 c_inhibit_evaluation_warnings = 0;
9587 /* First, determine the context for the type we are looking
9589 context = TYPE_CONTEXT (t);
9590 if (context && TYPE_P (context))
9592 context = tsubst_aggr_type (context, args, complain,
9593 in_decl, /*entering_scope=*/1);
9594 /* If context is a nested class inside a class template,
9595 it may still need to be instantiated (c++/33959). */
9596 context = complete_type (context);
9599 /* Then, figure out what arguments are appropriate for the
9600 type we are trying to find. For example, given:
9602 template <class T> struct S;
9603 template <class T, class U> void f(T, U) { S<U> su; }
9605 and supposing that we are instantiating f<int, double>,
9606 then our ARGS will be {int, double}, but, when looking up
9607 S we only want {double}. */
9608 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
9610 if (argvec == error_mark_node)
9611 r = error_mark_node;
9614 r = lookup_template_class (t, argvec, in_decl, context,
9615 entering_scope, complain);
9616 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
9619 cp_unevaluated_operand = saved_unevaluated_operand;
9620 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
9625 /* This is not a template type, so there's nothing to do. */
9629 return tsubst (t, args, complain, in_decl);
9633 /* Substitute into the default argument ARG (a default argument for
9634 FN), which has the indicated TYPE. */
9637 tsubst_default_argument (tree fn, tree type, tree arg)
9639 tree saved_class_ptr = NULL_TREE;
9640 tree saved_class_ref = NULL_TREE;
9642 /* This can happen in invalid code. */
9643 if (TREE_CODE (arg) == DEFAULT_ARG)
9646 /* This default argument came from a template. Instantiate the
9647 default argument here, not in tsubst. In the case of
9656 we must be careful to do name lookup in the scope of S<T>,
9657 rather than in the current class. */
9658 push_access_scope (fn);
9659 /* The "this" pointer is not valid in a default argument. */
9662 saved_class_ptr = current_class_ptr;
9663 cp_function_chain->x_current_class_ptr = NULL_TREE;
9664 saved_class_ref = current_class_ref;
9665 cp_function_chain->x_current_class_ref = NULL_TREE;
9668 push_deferring_access_checks(dk_no_deferred);
9669 /* The default argument expression may cause implicitly defined
9670 member functions to be synthesized, which will result in garbage
9671 collection. We must treat this situation as if we were within
9672 the body of function so as to avoid collecting live data on the
9675 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
9676 tf_warning_or_error, NULL_TREE,
9677 /*integral_constant_expression_p=*/false);
9679 pop_deferring_access_checks();
9681 /* Restore the "this" pointer. */
9684 cp_function_chain->x_current_class_ptr = saved_class_ptr;
9685 cp_function_chain->x_current_class_ref = saved_class_ref;
9688 /* Make sure the default argument is reasonable. */
9689 arg = check_default_argument (type, arg);
9691 pop_access_scope (fn);
9696 /* Substitute into all the default arguments for FN. */
9699 tsubst_default_arguments (tree fn)
9704 tmpl_args = DECL_TI_ARGS (fn);
9706 /* If this function is not yet instantiated, we certainly don't need
9707 its default arguments. */
9708 if (uses_template_parms (tmpl_args))
9710 /* Don't do this again for clones. */
9711 if (DECL_CLONED_FUNCTION_P (fn))
9714 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
9716 arg = TREE_CHAIN (arg))
9717 if (TREE_PURPOSE (arg))
9718 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
9720 TREE_PURPOSE (arg));
9723 /* Substitute the ARGS into the T, which is a _DECL. Return the
9724 result of the substitution. Issue error and warning messages under
9725 control of COMPLAIN. */
9728 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
9730 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9731 location_t saved_loc;
9736 /* Set the filename and linenumber to improve error-reporting. */
9737 saved_loc = input_location;
9738 input_location = DECL_SOURCE_LOCATION (t);
9740 switch (TREE_CODE (t))
9744 /* We can get here when processing a member function template,
9745 member class template, or template template parameter. */
9746 tree decl = DECL_TEMPLATE_RESULT (t);
9751 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9753 /* Template template parameter is treated here. */
9754 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9755 if (new_type == error_mark_node)
9756 RETURN (error_mark_node);
9759 DECL_CHAIN (r) = NULL_TREE;
9760 TREE_TYPE (r) = new_type;
9761 DECL_TEMPLATE_RESULT (r)
9762 = build_decl (DECL_SOURCE_LOCATION (decl),
9763 TYPE_DECL, DECL_NAME (decl), new_type);
9764 DECL_TEMPLATE_PARMS (r)
9765 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9767 TYPE_NAME (new_type) = r;
9771 /* We might already have an instance of this template.
9772 The ARGS are for the surrounding class type, so the
9773 full args contain the tsubst'd args for the context,
9774 plus the innermost args from the template decl. */
9775 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
9776 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
9777 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
9778 /* Because this is a template, the arguments will still be
9779 dependent, even after substitution. If
9780 PROCESSING_TEMPLATE_DECL is not set, the dependency
9781 predicates will short-circuit. */
9782 ++processing_template_decl;
9783 full_args = tsubst_template_args (tmpl_args, args,
9785 --processing_template_decl;
9786 if (full_args == error_mark_node)
9787 RETURN (error_mark_node);
9789 /* If this is a default template template argument,
9790 tsubst might not have changed anything. */
9791 if (full_args == tmpl_args)
9794 hash = hash_tmpl_and_args (t, full_args);
9795 spec = retrieve_specialization (t, full_args, hash);
9796 if (spec != NULL_TREE)
9802 /* Make a new template decl. It will be similar to the
9803 original, but will record the current template arguments.
9804 We also create a new function declaration, which is just
9805 like the old one, but points to this new template, rather
9806 than the old one. */
9808 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
9809 DECL_CHAIN (r) = NULL_TREE;
9811 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
9813 if (TREE_CODE (decl) == TYPE_DECL
9814 && !TYPE_DECL_ALIAS_P (decl))
9817 ++processing_template_decl;
9818 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9819 --processing_template_decl;
9820 if (new_type == error_mark_node)
9821 RETURN (error_mark_node);
9823 TREE_TYPE (r) = new_type;
9824 CLASSTYPE_TI_TEMPLATE (new_type) = r;
9825 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
9826 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
9827 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
9832 ++processing_template_decl;
9833 new_decl = tsubst (decl, args, complain, in_decl);
9834 --processing_template_decl;
9835 if (new_decl == error_mark_node)
9836 RETURN (error_mark_node);
9838 DECL_TEMPLATE_RESULT (r) = new_decl;
9839 DECL_TI_TEMPLATE (new_decl) = r;
9840 TREE_TYPE (r) = TREE_TYPE (new_decl);
9841 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
9842 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
9845 SET_DECL_IMPLICIT_INSTANTIATION (r);
9846 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
9847 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
9849 /* The template parameters for this new template are all the
9850 template parameters for the old template, except the
9851 outermost level of parameters. */
9852 DECL_TEMPLATE_PARMS (r)
9853 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9856 if (PRIMARY_TEMPLATE_P (t))
9857 DECL_PRIMARY_TEMPLATE (r) = r;
9859 if (TREE_CODE (decl) != TYPE_DECL)
9860 /* Record this non-type partial instantiation. */
9861 register_specialization (r, t,
9862 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
9870 tree argvec = NULL_TREE;
9878 /* Nobody should be tsubst'ing into non-template functions. */
9879 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
9881 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
9886 /* If T is not dependent, just return it. We have to
9887 increment PROCESSING_TEMPLATE_DECL because
9888 value_dependent_expression_p assumes that nothing is
9889 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9890 ++processing_template_decl;
9891 dependent_p = value_dependent_expression_p (t);
9892 --processing_template_decl;
9896 /* Calculate the most general template of which R is a
9897 specialization, and the complete set of arguments used to
9899 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
9900 argvec = tsubst_template_args (DECL_TI_ARGS
9901 (DECL_TEMPLATE_RESULT
9902 (DECL_TI_TEMPLATE (t))),
9903 args, complain, in_decl);
9904 if (argvec == error_mark_node)
9905 RETURN (error_mark_node);
9907 /* Check to see if we already have this specialization. */
9908 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9909 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9917 /* We can see more levels of arguments than parameters if
9918 there was a specialization of a member template, like
9921 template <class T> struct S { template <class U> void f(); }
9922 template <> template <class U> void S<int>::f(U);
9924 Here, we'll be substituting into the specialization,
9925 because that's where we can find the code we actually
9926 want to generate, but we'll have enough arguments for
9927 the most general template.
9929 We also deal with the peculiar case:
9931 template <class T> struct S {
9932 template <class U> friend void f();
9934 template <class U> void f() {}
9936 template void f<double>();
9938 Here, the ARGS for the instantiation of will be {int,
9939 double}. But, we only need as many ARGS as there are
9940 levels of template parameters in CODE_PATTERN. We are
9941 careful not to get fooled into reducing the ARGS in
9944 template <class T> struct S { template <class U> void f(U); }
9945 template <class T> template <> void S<T>::f(int) {}
9947 which we can spot because the pattern will be a
9948 specialization in this case. */
9949 args_depth = TMPL_ARGS_DEPTH (args);
9951 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
9952 if (args_depth > parms_depth
9953 && !DECL_TEMPLATE_SPECIALIZATION (t))
9954 args = get_innermost_template_args (args, parms_depth);
9958 /* This special case arises when we have something like this:
9960 template <class T> struct S {
9961 friend void f<int>(int, double);
9964 Here, the DECL_TI_TEMPLATE for the friend declaration
9965 will be an IDENTIFIER_NODE. We are being called from
9966 tsubst_friend_function, and we want only to create a
9967 new decl (R) with appropriate types so that we can call
9968 determine_specialization. */
9969 gen_tmpl = NULL_TREE;
9972 if (DECL_CLASS_SCOPE_P (t))
9974 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
9978 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
9979 complain, t, /*entering_scope=*/1);
9984 ctx = DECL_CONTEXT (t);
9986 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9987 if (type == error_mark_node)
9988 RETURN (error_mark_node);
9990 /* We do NOT check for matching decls pushed separately at this
9991 point, as they may not represent instantiations of this
9992 template, and in any case are considered separate under the
9995 DECL_USE_TEMPLATE (r) = 0;
9996 TREE_TYPE (r) = type;
9997 /* Clear out the mangled name and RTL for the instantiation. */
9998 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9999 SET_DECL_RTL (r, NULL);
10000 /* Leave DECL_INITIAL set on deleted instantiations. */
10001 if (!DECL_DELETED_FN (r))
10002 DECL_INITIAL (r) = NULL_TREE;
10003 DECL_CONTEXT (r) = ctx;
10005 if (member && DECL_CONV_FN_P (r))
10006 /* Type-conversion operator. Reconstruct the name, in
10007 case it's the name of one of the template's parameters. */
10008 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
10010 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
10012 DECL_RESULT (r) = NULL_TREE;
10014 TREE_STATIC (r) = 0;
10015 TREE_PUBLIC (r) = TREE_PUBLIC (t);
10016 DECL_EXTERNAL (r) = 1;
10017 /* If this is an instantiation of a function with internal
10018 linkage, we already know what object file linkage will be
10019 assigned to the instantiation. */
10020 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
10021 DECL_DEFER_OUTPUT (r) = 0;
10022 DECL_CHAIN (r) = NULL_TREE;
10023 DECL_PENDING_INLINE_INFO (r) = 0;
10024 DECL_PENDING_INLINE_P (r) = 0;
10025 DECL_SAVED_TREE (r) = NULL_TREE;
10026 DECL_STRUCT_FUNCTION (r) = NULL;
10028 /* We'll re-clone as appropriate in instantiate_template. */
10029 DECL_CLONED_FUNCTION (r) = NULL_TREE;
10031 /* If we aren't complaining now, return on error before we register
10032 the specialization so that we'll complain eventually. */
10033 if ((complain & tf_error) == 0
10034 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10035 && !grok_op_properties (r, /*complain=*/false))
10036 RETURN (error_mark_node);
10038 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10039 this in the special friend case mentioned above where
10040 GEN_TMPL is NULL. */
10043 DECL_TEMPLATE_INFO (r)
10044 = build_template_info (gen_tmpl, argvec);
10045 SET_DECL_IMPLICIT_INSTANTIATION (r);
10046 register_specialization (r, gen_tmpl, argvec, false, hash);
10048 /* We're not supposed to instantiate default arguments
10049 until they are called, for a template. But, for a
10052 template <class T> void f ()
10053 { extern void g(int i = T()); }
10055 we should do the substitution when the template is
10056 instantiated. We handle the member function case in
10057 instantiate_class_template since the default arguments
10058 might refer to other members of the class. */
10060 && !PRIMARY_TEMPLATE_P (gen_tmpl)
10061 && !uses_template_parms (argvec))
10062 tsubst_default_arguments (r);
10065 DECL_TEMPLATE_INFO (r) = NULL_TREE;
10067 /* Copy the list of befriending classes. */
10068 for (friends = &DECL_BEFRIENDING_CLASSES (r);
10070 friends = &TREE_CHAIN (*friends))
10072 *friends = copy_node (*friends);
10073 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
10078 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
10080 maybe_retrofit_in_chrg (r);
10081 if (DECL_CONSTRUCTOR_P (r))
10082 grok_ctor_properties (ctx, r);
10083 /* If this is an instantiation of a member template, clone it.
10084 If it isn't, that'll be handled by
10085 clone_constructors_and_destructors. */
10086 if (PRIMARY_TEMPLATE_P (gen_tmpl))
10087 clone_function_decl (r, /*update_method_vec_p=*/0);
10089 else if ((complain & tf_error) != 0
10090 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
10091 && !grok_op_properties (r, /*complain=*/true))
10092 RETURN (error_mark_node);
10094 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
10095 SET_DECL_FRIEND_CONTEXT (r,
10096 tsubst (DECL_FRIEND_CONTEXT (t),
10097 args, complain, in_decl));
10099 /* Possibly limit visibility based on template args. */
10100 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10101 if (DECL_VISIBILITY_SPECIFIED (t))
10103 DECL_VISIBILITY_SPECIFIED (r) = 0;
10104 DECL_ATTRIBUTES (r)
10105 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10107 determine_visibility (r);
10108 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
10109 && !processing_template_decl)
10110 defaulted_late_check (r);
10112 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10113 args, complain, in_decl);
10119 tree type = NULL_TREE;
10121 tree expanded_types = NULL_TREE;
10122 tree prev_r = NULL_TREE;
10123 tree first_r = NULL_TREE;
10125 if (FUNCTION_PARAMETER_PACK_P (t))
10127 /* If there is a local specialization that isn't a
10128 parameter pack, it means that we're doing a "simple"
10129 substitution from inside tsubst_pack_expansion. Just
10130 return the local specialization (which will be a single
10132 tree spec = retrieve_local_specialization (t);
10134 && TREE_CODE (spec) == PARM_DECL
10135 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
10138 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10139 the parameters in this function parameter pack. */
10140 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
10141 complain, in_decl);
10142 if (TREE_CODE (expanded_types) == TREE_VEC)
10144 len = TREE_VEC_LENGTH (expanded_types);
10146 /* Zero-length parameter packs are boring. Just substitute
10149 RETURN (tsubst (TREE_CHAIN (t), args, complain,
10154 /* All we did was update the type. Make a note of that. */
10155 type = expanded_types;
10156 expanded_types = NULL_TREE;
10160 /* Loop through all of the parameter's we'll build. When T is
10161 a function parameter pack, LEN is the number of expanded
10162 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10164 for (i = 0; i < len; ++i)
10168 if (DECL_TEMPLATE_PARM_P (t))
10169 SET_DECL_TEMPLATE_PARM_P (r);
10171 if (expanded_types)
10172 /* We're on the Ith parameter of the function parameter
10175 /* An argument of a function parameter pack is not a parameter
10177 FUNCTION_PARAMETER_PACK_P (r) = false;
10179 /* Get the Ith type. */
10180 type = TREE_VEC_ELT (expanded_types, i);
10182 /* Rename the parameter to include the index. */
10184 = make_ith_pack_parameter_name (DECL_NAME (r), i);
10187 /* We're dealing with a normal parameter. */
10188 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10190 type = type_decays_to (type);
10191 TREE_TYPE (r) = type;
10192 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10194 if (DECL_INITIAL (r))
10196 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
10197 DECL_INITIAL (r) = TREE_TYPE (r);
10199 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
10200 complain, in_decl);
10203 DECL_CONTEXT (r) = NULL_TREE;
10205 if (!DECL_TEMPLATE_PARM_P (r))
10206 DECL_ARG_TYPE (r) = type_passed_as (type);
10208 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10209 args, complain, in_decl);
10211 /* Keep track of the first new parameter we
10212 generate. That's what will be returned to the
10217 /* Build a proper chain of parameters when substituting
10218 into a function parameter pack. */
10220 DECL_CHAIN (prev_r) = r;
10223 if (DECL_CHAIN (t))
10224 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
10225 complain, DECL_CHAIN (t));
10227 /* FIRST_R contains the start of the chain we've built. */
10237 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10238 if (type == error_mark_node)
10239 RETURN (error_mark_node);
10240 TREE_TYPE (r) = type;
10241 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10243 if (DECL_C_BIT_FIELD (r))
10244 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10245 non-bit-fields DECL_INITIAL is a non-static data member
10246 initializer, which gets deferred instantiation. */
10248 = tsubst_expr (DECL_INITIAL (t), args,
10250 /*integral_constant_expression_p=*/true);
10251 else if (DECL_INITIAL (t))
10253 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10254 NSDMI in perform_member_init. Still set DECL_INITIAL
10255 so that we know there is one. */
10256 DECL_INITIAL (r) = void_zero_node;
10257 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
10258 retrofit_lang_decl (r);
10259 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
10261 /* We don't have to set DECL_CONTEXT here; it is set by
10262 finish_member_declaration. */
10263 DECL_CHAIN (r) = NULL_TREE;
10264 if (VOID_TYPE_P (type))
10265 error ("instantiation of %q+D as type %qT", r, type);
10267 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
10268 args, complain, in_decl);
10273 /* We reach here only for member using decls. We also need to check
10274 uses_template_parms because DECL_DEPENDENT_P is not set for a
10275 using-declaration that designates a member of the current
10276 instantiation (c++/53549). */
10277 if (DECL_DEPENDENT_P (t)
10278 || uses_template_parms (USING_DECL_SCOPE (t)))
10280 r = do_class_using_decl
10281 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
10282 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
10284 r = error_mark_node;
10287 TREE_PROTECTED (r) = TREE_PROTECTED (t);
10288 TREE_PRIVATE (r) = TREE_PRIVATE (t);
10294 DECL_CHAIN (r) = NULL_TREE;
10301 tree argvec = NULL_TREE;
10302 tree gen_tmpl = NULL_TREE;
10304 tree tmpl = NULL_TREE;
10306 tree type = NULL_TREE;
10309 if (TREE_CODE (t) == TYPE_DECL
10310 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
10312 /* If this is the canonical decl, we don't have to
10313 mess with instantiations, and often we can't (for
10314 typename, template type parms and such). Note that
10315 TYPE_NAME is not correct for the above test if
10316 we've copied the type for a typedef. */
10317 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10318 if (type == error_mark_node)
10319 RETURN (error_mark_node);
10320 r = TYPE_NAME (type);
10324 /* Check to see if we already have the specialization we
10327 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
10329 /* T is a static data member or namespace-scope entity.
10330 We have to substitute into namespace-scope variables
10331 (even though such entities are never templates) because
10334 template <class T> void f() { extern T t; }
10336 where the entity referenced is not known until
10337 instantiation time. */
10339 ctx = DECL_CONTEXT (t);
10340 if (DECL_CLASS_SCOPE_P (t))
10342 ctx = tsubst_aggr_type (ctx, args,
10344 in_decl, /*entering_scope=*/1);
10345 /* If CTX is unchanged, then T is in fact the
10346 specialization we want. That situation occurs when
10347 referencing a static data member within in its own
10348 class. We can use pointer equality, rather than
10349 same_type_p, because DECL_CONTEXT is always
10351 if (ctx == DECL_CONTEXT (t)
10352 && (TREE_CODE (t) != TYPE_DECL
10353 /* ... unless T is a member template; in which
10354 case our caller can be willing to create a
10355 specialization of that template represented
10357 || !(DECL_TI_TEMPLATE (t)
10358 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
10364 tmpl = DECL_TI_TEMPLATE (t);
10365 gen_tmpl = most_general_template (tmpl);
10366 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10367 if (argvec == error_mark_node)
10368 RETURN (error_mark_node);
10369 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10370 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10375 /* A local variable. */
10377 /* Subsequent calls to pushdecl will fill this in. */
10379 spec = retrieve_local_specialization (t);
10381 /* If we already have the specialization we need, there is
10382 nothing more to do. */
10389 if (TREE_CODE (t) == VAR_DECL && DECL_ANON_UNION_VAR_P (t))
10391 /* Just use name lookup to find a member alias for an anonymous
10392 union, but then add it to the hash table. */
10393 r = lookup_name (DECL_NAME (t));
10394 gcc_assert (DECL_ANON_UNION_VAR_P (r));
10395 register_local_specialization (r, t);
10399 /* Create a new node for the specialization we need. */
10401 if (type == NULL_TREE)
10403 if (is_typedef_decl (t))
10404 type = DECL_ORIGINAL_TYPE (t);
10406 type = TREE_TYPE (t);
10407 if (TREE_CODE (t) == VAR_DECL
10408 && VAR_HAD_UNKNOWN_BOUND (t)
10409 && type != error_mark_node)
10410 type = strip_array_domain (type);
10411 type = tsubst (type, args, complain, in_decl);
10413 if (TREE_CODE (r) == VAR_DECL)
10415 /* Even if the original location is out of scope, the
10416 newly substituted one is not. */
10417 DECL_DEAD_FOR_LOCAL (r) = 0;
10418 DECL_INITIALIZED_P (r) = 0;
10419 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10420 if (type == error_mark_node)
10421 RETURN (error_mark_node);
10422 if (TREE_CODE (type) == FUNCTION_TYPE)
10424 /* It may seem that this case cannot occur, since:
10429 declares a function, not a variable. However:
10432 template <typename T> void g() { T t; }
10433 template void g<f>();
10435 is an attempt to declare a variable with function
10437 error ("variable %qD has function type",
10438 /* R is not yet sufficiently initialized, so we
10439 just use its name. */
10441 RETURN (error_mark_node);
10443 type = complete_type (type);
10444 /* Wait until cp_finish_decl to set this again, to handle
10445 circular dependency (template/instantiate6.C). */
10446 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10447 type = check_var_type (DECL_NAME (r), type);
10449 if (DECL_HAS_VALUE_EXPR_P (t))
10451 tree ve = DECL_VALUE_EXPR (t);
10452 ve = tsubst_expr (ve, args, complain, in_decl,
10453 /*constant_expression_p=*/false);
10454 if (REFERENCE_REF_P (ve))
10456 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10457 ve = TREE_OPERAND (ve, 0);
10459 SET_DECL_VALUE_EXPR (r, ve);
10462 else if (DECL_SELF_REFERENCE_P (t))
10463 SET_DECL_SELF_REFERENCE_P (r);
10464 TREE_TYPE (r) = type;
10465 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10466 DECL_CONTEXT (r) = ctx;
10467 /* Clear out the mangled name and RTL for the instantiation. */
10468 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10469 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10470 SET_DECL_RTL (r, NULL);
10471 /* The initializer must not be expanded until it is required;
10472 see [temp.inst]. */
10473 DECL_INITIAL (r) = NULL_TREE;
10474 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10475 SET_DECL_RTL (r, NULL);
10476 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10477 if (TREE_CODE (r) == VAR_DECL)
10479 /* Possibly limit visibility based on template args. */
10480 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10481 if (DECL_VISIBILITY_SPECIFIED (t))
10483 DECL_VISIBILITY_SPECIFIED (r) = 0;
10484 DECL_ATTRIBUTES (r)
10485 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10487 determine_visibility (r);
10492 /* A static data member declaration is always marked
10493 external when it is declared in-class, even if an
10494 initializer is present. We mimic the non-template
10495 processing here. */
10496 DECL_EXTERNAL (r) = 1;
10498 register_specialization (r, gen_tmpl, argvec, false, hash);
10499 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10500 SET_DECL_IMPLICIT_INSTANTIATION (r);
10502 else if (cp_unevaluated_operand)
10504 /* We're substituting this var in a decltype outside of its
10505 scope, such as for a lambda return type. Don't add it to
10506 local_specializations, do perform auto deduction. */
10507 tree auto_node = type_uses_auto (type);
10511 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
10512 /*constant_expression_p=*/false);
10513 init = resolve_nondeduced_context (init);
10514 TREE_TYPE (r) = type
10515 = do_auto_deduction (type, init, auto_node);
10519 register_local_specialization (r, t);
10521 DECL_CHAIN (r) = NULL_TREE;
10523 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10525 args, complain, in_decl);
10527 /* Preserve a typedef that names a type. */
10528 if (is_typedef_decl (r))
10530 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
10531 set_underlying_type (r);
10534 layout_decl (r, 0);
10539 gcc_unreachable ();
10544 /* Restore the file and line information. */
10545 input_location = saved_loc;
10550 /* Substitute into the ARG_TYPES of a function type.
10551 If END is a TREE_CHAIN, leave it and any following types
10555 tsubst_arg_types (tree arg_types,
10558 tsubst_flags_t complain,
10561 tree remaining_arg_types;
10562 tree type = NULL_TREE;
10564 tree expanded_args = NULL_TREE;
10567 if (!arg_types || arg_types == void_list_node || arg_types == end)
10570 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
10571 args, end, complain, in_decl);
10572 if (remaining_arg_types == error_mark_node)
10573 return error_mark_node;
10575 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
10577 /* For a pack expansion, perform substitution on the
10578 entire expression. Later on, we'll handle the arguments
10580 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
10581 args, complain, in_decl);
10583 if (TREE_CODE (expanded_args) == TREE_VEC)
10584 /* So that we'll spin through the parameters, one by one. */
10585 i = TREE_VEC_LENGTH (expanded_args);
10588 /* We only partially substituted into the parameter
10589 pack. Our type is TYPE_PACK_EXPANSION. */
10590 type = expanded_args;
10591 expanded_args = NULL_TREE;
10599 type = TREE_VEC_ELT (expanded_args, i);
10601 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
10603 if (type == error_mark_node)
10604 return error_mark_node;
10605 if (VOID_TYPE_P (type))
10607 if (complain & tf_error)
10609 error ("invalid parameter type %qT", type);
10611 error ("in declaration %q+D", in_decl);
10613 return error_mark_node;
10616 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10617 top-level qualifiers as required. */
10618 type = cv_unqualified (type_decays_to (type));
10620 /* We do not substitute into default arguments here. The standard
10621 mandates that they be instantiated only when needed, which is
10622 done in build_over_call. */
10623 default_arg = TREE_PURPOSE (arg_types);
10625 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
10627 /* We've instantiated a template before its default arguments
10628 have been parsed. This can happen for a nested template
10629 class, and is not an error unless we require the default
10630 argument in a call of this function. */
10631 remaining_arg_types =
10632 tree_cons (default_arg, type, remaining_arg_types);
10633 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
10634 remaining_arg_types);
10637 remaining_arg_types =
10638 hash_tree_cons (default_arg, type, remaining_arg_types);
10641 return remaining_arg_types;
10644 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10645 *not* handle the exception-specification for FNTYPE, because the
10646 initial substitution of explicitly provided template parameters
10647 during argument deduction forbids substitution into the
10648 exception-specification:
10652 All references in the function type of the function template to the
10653 corresponding template parameters are replaced by the specified tem-
10654 plate argument values. If a substitution in a template parameter or
10655 in the function type of the function template results in an invalid
10656 type, type deduction fails. [Note: The equivalent substitution in
10657 exception specifications is done only when the function is instanti-
10658 ated, at which point a program is ill-formed if the substitution
10659 results in an invalid type.] */
10662 tsubst_function_type (tree t,
10664 tsubst_flags_t complain,
10671 /* The TYPE_CONTEXT is not used for function/method types. */
10672 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
10674 /* Substitute the return type. */
10675 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10676 if (return_type == error_mark_node)
10677 return error_mark_node;
10678 /* The standard does not presently indicate that creation of a
10679 function type with an invalid return type is a deduction failure.
10680 However, that is clearly analogous to creating an array of "void"
10681 or a reference to a reference. This is core issue #486. */
10682 if (TREE_CODE (return_type) == ARRAY_TYPE
10683 || TREE_CODE (return_type) == FUNCTION_TYPE)
10685 if (complain & tf_error)
10687 if (TREE_CODE (return_type) == ARRAY_TYPE)
10688 error ("function returning an array");
10690 error ("function returning a function");
10692 return error_mark_node;
10695 /* Substitute the argument types. */
10696 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
10697 complain, in_decl);
10698 if (arg_types == error_mark_node)
10699 return error_mark_node;
10701 /* Construct a new type node and return it. */
10702 if (TREE_CODE (t) == FUNCTION_TYPE)
10704 fntype = build_function_type (return_type, arg_types);
10705 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
10709 tree r = TREE_TYPE (TREE_VALUE (arg_types));
10710 if (! MAYBE_CLASS_TYPE_P (r))
10714 Type deduction may fail for any of the following
10717 -- Attempting to create "pointer to member of T" when T
10718 is not a class type. */
10719 if (complain & tf_error)
10720 error ("creating pointer to member function of non-class type %qT",
10722 return error_mark_node;
10725 fntype = build_method_type_directly (r, return_type,
10726 TREE_CHAIN (arg_types));
10728 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
10733 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10734 ARGS into that specification, and return the substituted
10735 specification. If there is no specification, return NULL_TREE. */
10738 tsubst_exception_specification (tree fntype,
10740 tsubst_flags_t complain,
10747 specs = TYPE_RAISES_EXCEPTIONS (fntype);
10748 new_specs = NULL_TREE;
10749 if (specs && TREE_PURPOSE (specs))
10751 /* A noexcept-specifier. */
10752 tree expr = TREE_PURPOSE (specs);
10753 if (TREE_CODE (expr) == INTEGER_CST)
10757 /* Defer instantiation of noexcept-specifiers to avoid
10758 excessive instantiations (c++/49107). */
10759 new_specs = make_node (DEFERRED_NOEXCEPT);
10760 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
10762 /* We already partially instantiated this member template,
10763 so combine the new args with the old. */
10764 DEFERRED_NOEXCEPT_PATTERN (new_specs)
10765 = DEFERRED_NOEXCEPT_PATTERN (expr);
10766 DEFERRED_NOEXCEPT_ARGS (new_specs)
10767 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
10771 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
10772 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
10776 new_specs = tsubst_copy_and_build
10777 (expr, args, complain, in_decl, /*function_p=*/false,
10778 /*integral_constant_expression_p=*/true);
10779 new_specs = build_noexcept_spec (new_specs, complain);
10783 if (! TREE_VALUE (specs))
10790 tree expanded_specs = NULL_TREE;
10792 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
10794 /* Expand the pack expansion type. */
10795 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
10799 if (expanded_specs == error_mark_node)
10800 return error_mark_node;
10801 else if (TREE_CODE (expanded_specs) == TREE_VEC)
10802 len = TREE_VEC_LENGTH (expanded_specs);
10805 /* We're substituting into a member template, so
10806 we got a TYPE_PACK_EXPANSION back. Add that
10807 expansion and move on. */
10808 gcc_assert (TREE_CODE (expanded_specs)
10809 == TYPE_PACK_EXPANSION);
10810 new_specs = add_exception_specifier (new_specs,
10813 specs = TREE_CHAIN (specs);
10818 for (i = 0; i < len; ++i)
10820 if (expanded_specs)
10821 spec = TREE_VEC_ELT (expanded_specs, i);
10823 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
10824 if (spec == error_mark_node)
10826 new_specs = add_exception_specifier (new_specs, spec,
10830 specs = TREE_CHAIN (specs);
10836 /* Take the tree structure T and replace template parameters used
10837 therein with the argument vector ARGS. IN_DECL is an associated
10838 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
10839 Issue error and warning messages under control of COMPLAIN. Note
10840 that we must be relatively non-tolerant of extensions here, in
10841 order to preserve conformance; if we allow substitutions that
10842 should not be allowed, we may allow argument deductions that should
10843 not succeed, and therefore report ambiguous overload situations
10844 where there are none. In theory, we could allow the substitution,
10845 but indicate that it should have failed, and allow our caller to
10846 make sure that the right thing happens, but we don't try to do this
10849 This function is used for dealing with types, decls and the like;
10850 for expressions, use tsubst_expr or tsubst_copy. */
10853 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10855 enum tree_code code;
10856 tree type, r = NULL_TREE;
10858 if (t == NULL_TREE || t == error_mark_node
10859 || t == integer_type_node
10860 || t == void_type_node
10861 || t == char_type_node
10862 || t == unknown_type_node
10863 || TREE_CODE (t) == NAMESPACE_DECL
10864 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
10868 return tsubst_decl (t, args, complain);
10870 if (args == NULL_TREE)
10873 code = TREE_CODE (t);
10875 if (code == IDENTIFIER_NODE)
10876 type = IDENTIFIER_TYPE_VALUE (t);
10878 type = TREE_TYPE (t);
10880 gcc_assert (type != unknown_type_node);
10882 /* Reuse typedefs. We need to do this to handle dependent attributes,
10883 such as attribute aligned. */
10885 && typedef_variant_p (t))
10887 tree decl = TYPE_NAME (t);
10889 if (TYPE_DECL_ALIAS_P (decl)
10890 && DECL_LANG_SPECIFIC (decl)
10891 && DECL_TEMPLATE_INFO (decl)
10892 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
10894 /* DECL represents an alias template and we want to
10895 instantiate it. Let's substitute our arguments for the
10896 template parameters into the declaration and get the
10898 r = tsubst (decl, args, complain, decl);
10900 else if (DECL_CLASS_SCOPE_P (decl)
10901 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
10902 && uses_template_parms (DECL_CONTEXT (decl)))
10904 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10905 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
10906 r = retrieve_specialization (tmpl, gen_args, 0);
10908 else if (DECL_FUNCTION_SCOPE_P (decl)
10909 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
10910 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
10911 r = retrieve_local_specialization (decl);
10913 /* The typedef is from a non-template context. */
10919 r = cp_build_qualified_type_real
10920 (r, cp_type_quals (t) | cp_type_quals (r),
10921 complain | tf_ignore_bad_quals);
10926 /* We don't have an instantiation yet, so drop the typedef. */
10927 int quals = cp_type_quals (t);
10928 t = DECL_ORIGINAL_TYPE (decl);
10929 t = cp_build_qualified_type_real (t, quals,
10930 complain | tf_ignore_bad_quals);
10935 && code != TYPENAME_TYPE
10936 && code != TEMPLATE_TYPE_PARM
10937 && code != IDENTIFIER_NODE
10938 && code != FUNCTION_TYPE
10939 && code != METHOD_TYPE)
10940 type = tsubst (type, args, complain, in_decl);
10941 if (type == error_mark_node)
10942 return error_mark_node;
10948 case ENUMERAL_TYPE:
10949 return tsubst_aggr_type (t, args, complain, in_decl,
10950 /*entering_scope=*/0);
10953 case IDENTIFIER_NODE:
10964 if (t == integer_type_node)
10967 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
10968 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
10972 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
10974 max = tsubst_expr (omax, args, complain, in_decl,
10975 /*integral_constant_expression_p=*/false);
10977 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
10979 if (TREE_CODE (max) == NOP_EXPR
10980 && TREE_SIDE_EFFECTS (omax)
10981 && !TREE_TYPE (max))
10982 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
10984 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
10985 with TREE_SIDE_EFFECTS that indicates this is not an integral
10986 constant expression. */
10987 if (processing_template_decl
10988 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
10990 gcc_assert (TREE_CODE (max) == NOP_EXPR);
10991 TREE_SIDE_EFFECTS (max) = 1;
10994 return compute_array_index_type (NULL_TREE, max, complain);
10997 case TEMPLATE_TYPE_PARM:
10998 case TEMPLATE_TEMPLATE_PARM:
10999 case BOUND_TEMPLATE_TEMPLATE_PARM:
11000 case TEMPLATE_PARM_INDEX:
11005 tree arg = NULL_TREE;
11009 gcc_assert (TREE_VEC_LENGTH (args) > 0);
11010 template_parm_level_and_index (t, &level, &idx);
11012 levels = TMPL_ARGS_DEPTH (args);
11013 if (level <= levels)
11015 arg = TMPL_ARG (args, level, idx);
11017 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
11018 /* See through ARGUMENT_PACK_SELECT arguments. */
11019 arg = ARGUMENT_PACK_SELECT_ARG (arg);
11022 if (arg == error_mark_node)
11023 return error_mark_node;
11024 else if (arg != NULL_TREE)
11026 if (ARGUMENT_PACK_P (arg))
11027 /* If ARG is an argument pack, we don't actually want to
11028 perform a substitution here, because substitutions
11029 for argument packs are only done
11030 element-by-element. We can get to this point when
11031 substituting the type of a non-type template
11032 parameter pack, when that type actually contains
11033 template parameter packs from an outer template, e.g.,
11035 template<typename... Types> struct A {
11036 template<Types... Values> struct B { };
11040 if (code == TEMPLATE_TYPE_PARM)
11043 gcc_assert (TYPE_P (arg));
11045 quals = cp_type_quals (arg) | cp_type_quals (t);
11047 return cp_build_qualified_type_real
11048 (arg, quals, complain | tf_ignore_bad_quals);
11050 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11052 /* We are processing a type constructed from a
11053 template template parameter. */
11054 tree argvec = tsubst (TYPE_TI_ARGS (t),
11055 args, complain, in_decl);
11056 if (argvec == error_mark_node)
11057 return error_mark_node;
11059 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
11060 || TREE_CODE (arg) == TEMPLATE_DECL
11061 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
11063 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
11064 /* Consider this code:
11066 template <template <class> class Template>
11068 template <class Arg> using Bind = Template<Arg>;
11071 template <template <class> class Template, class Arg>
11072 using Instantiate = Template<Arg>; //#0
11074 template <template <class> class Template,
11077 Instantiate<Internal<Template>::template Bind,
11080 When #1 is parsed, the
11081 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11082 parameter `Template' in #0 matches the
11083 UNBOUND_CLASS_TEMPLATE representing the argument
11084 `Internal<Template>::template Bind'; We then want
11085 to assemble the type `Bind<Argument>' that can't
11086 be fully created right now, because
11087 `Internal<Template>' not being complete, the Bind
11088 template cannot be looked up in that context. So
11089 we need to "store" `Bind<Argument>' for later
11090 when the context of Bind becomes complete. Let's
11091 store that in a TYPENAME_TYPE. */
11092 return make_typename_type (TYPE_CONTEXT (arg),
11093 build_nt (TEMPLATE_ID_EXPR,
11094 TYPE_IDENTIFIER (arg),
11099 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11100 are resolving nested-types in the signature of a
11101 member function templates. Otherwise ARG is a
11102 TEMPLATE_DECL and is the real template to be
11104 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11105 arg = TYPE_NAME (arg);
11107 r = lookup_template_class (arg,
11109 DECL_CONTEXT (arg),
11110 /*entering_scope=*/0,
11112 return cp_build_qualified_type_real
11113 (r, cp_type_quals (t), complain);
11116 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11117 return convert_from_reference (unshare_expr (arg));
11121 /* This can happen during the attempted tsubst'ing in
11122 unify. This means that we don't yet have any information
11123 about the template parameter in question. */
11126 /* If we get here, we must have been looking at a parm for a
11127 more deeply nested template. Make a new version of this
11128 template parameter, but with a lower level. */
11131 case TEMPLATE_TYPE_PARM:
11132 case TEMPLATE_TEMPLATE_PARM:
11133 case BOUND_TEMPLATE_TEMPLATE_PARM:
11134 if (cp_type_quals (t))
11136 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
11137 r = cp_build_qualified_type_real
11138 (r, cp_type_quals (t),
11139 complain | (code == TEMPLATE_TYPE_PARM
11140 ? tf_ignore_bad_quals : 0));
11145 TEMPLATE_TYPE_PARM_INDEX (r)
11146 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
11147 r, levels, args, complain);
11148 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
11149 TYPE_MAIN_VARIANT (r) = r;
11150 TYPE_POINTER_TO (r) = NULL_TREE;
11151 TYPE_REFERENCE_TO (r) = NULL_TREE;
11153 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
11154 /* We have reduced the level of the template
11155 template parameter, but not the levels of its
11156 template parameters, so canonical_type_parameter
11157 will not be able to find the canonical template
11158 template parameter for this level. Thus, we
11159 require structural equality checking to compare
11160 TEMPLATE_TEMPLATE_PARMs. */
11161 SET_TYPE_STRUCTURAL_EQUALITY (r);
11162 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
11163 SET_TYPE_STRUCTURAL_EQUALITY (r);
11165 TYPE_CANONICAL (r) = canonical_type_parameter (r);
11167 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
11169 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
11170 complain, in_decl);
11171 if (argvec == error_mark_node)
11172 return error_mark_node;
11174 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
11175 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
11180 case TEMPLATE_PARM_INDEX:
11181 r = reduce_template_parm_level (t, type, levels, args, complain);
11185 gcc_unreachable ();
11193 tree purpose, value, chain;
11195 if (t == void_list_node)
11198 purpose = TREE_PURPOSE (t);
11201 purpose = tsubst (purpose, args, complain, in_decl);
11202 if (purpose == error_mark_node)
11203 return error_mark_node;
11205 value = TREE_VALUE (t);
11208 value = tsubst (value, args, complain, in_decl);
11209 if (value == error_mark_node)
11210 return error_mark_node;
11212 chain = TREE_CHAIN (t);
11213 if (chain && chain != void_type_node)
11215 chain = tsubst (chain, args, complain, in_decl);
11216 if (chain == error_mark_node)
11217 return error_mark_node;
11219 if (purpose == TREE_PURPOSE (t)
11220 && value == TREE_VALUE (t)
11221 && chain == TREE_CHAIN (t))
11223 return hash_tree_cons (purpose, value, chain);
11227 /* We should never be tsubsting a binfo. */
11228 gcc_unreachable ();
11231 /* A vector of template arguments. */
11232 gcc_assert (!type);
11233 return tsubst_template_args (t, args, complain, in_decl);
11236 case REFERENCE_TYPE:
11238 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
11243 Type deduction may fail for any of the following
11246 -- Attempting to create a pointer to reference type.
11247 -- Attempting to create a reference to a reference type or
11248 a reference to void.
11250 Core issue 106 says that creating a reference to a reference
11251 during instantiation is no longer a cause for failure. We
11252 only enforce this check in strict C++98 mode. */
11253 if ((TREE_CODE (type) == REFERENCE_TYPE
11254 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
11255 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
11257 static location_t last_loc;
11259 /* We keep track of the last time we issued this error
11260 message to avoid spewing a ton of messages during a
11261 single bad template instantiation. */
11262 if (complain & tf_error
11263 && last_loc != input_location)
11265 if (TREE_CODE (type) == VOID_TYPE)
11266 error ("forming reference to void");
11267 else if (code == POINTER_TYPE)
11268 error ("forming pointer to reference type %qT", type);
11270 error ("forming reference to reference type %qT", type);
11271 last_loc = input_location;
11274 return error_mark_node;
11276 else if (code == POINTER_TYPE)
11278 r = build_pointer_type (type);
11279 if (TREE_CODE (type) == METHOD_TYPE)
11280 r = build_ptrmemfunc_type (r);
11282 else if (TREE_CODE (type) == REFERENCE_TYPE)
11283 /* In C++0x, during template argument substitution, when there is an
11284 attempt to create a reference to a reference type, reference
11285 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11287 "If a template-argument for a template-parameter T names a type
11288 that is a reference to a type A, an attempt to create the type
11289 'lvalue reference to cv T' creates the type 'lvalue reference to
11290 A,' while an attempt to create the type type rvalue reference to
11291 cv T' creates the type T"
11293 r = cp_build_reference_type
11295 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
11297 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
11298 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11300 if (r != error_mark_node)
11301 /* Will this ever be needed for TYPE_..._TO values? */
11308 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
11309 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
11313 Type deduction may fail for any of the following
11316 -- Attempting to create "pointer to member of T" when T
11317 is not a class type. */
11318 if (complain & tf_error)
11319 error ("creating pointer to member of non-class type %qT", r);
11320 return error_mark_node;
11322 if (TREE_CODE (type) == REFERENCE_TYPE)
11324 if (complain & tf_error)
11325 error ("creating pointer to member reference type %qT", type);
11326 return error_mark_node;
11328 if (TREE_CODE (type) == VOID_TYPE)
11330 if (complain & tf_error)
11331 error ("creating pointer to member of type void");
11332 return error_mark_node;
11334 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
11335 if (TREE_CODE (type) == FUNCTION_TYPE)
11337 /* The type of the implicit object parameter gets its
11338 cv-qualifiers from the FUNCTION_TYPE. */
11340 tree method_type = build_memfn_type (type, r, type_memfn_quals (type));
11341 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
11342 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
11346 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
11350 case FUNCTION_TYPE:
11355 fntype = tsubst_function_type (t, args, complain, in_decl);
11356 if (fntype == error_mark_node)
11357 return error_mark_node;
11359 /* Substitute the exception specification. */
11360 specs = tsubst_exception_specification (t, args, complain,
11361 in_decl, /*defer_ok*/true);
11362 if (specs == error_mark_node)
11363 return error_mark_node;
11365 fntype = build_exception_variant (fntype, specs);
11370 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
11371 if (domain == error_mark_node)
11372 return error_mark_node;
11374 /* As an optimization, we avoid regenerating the array type if
11375 it will obviously be the same as T. */
11376 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
11379 /* These checks should match the ones in grokdeclarator.
11383 The deduction may fail for any of the following reasons:
11385 -- Attempting to create an array with an element type that
11386 is void, a function type, or a reference type, or [DR337]
11387 an abstract class type. */
11388 if (TREE_CODE (type) == VOID_TYPE
11389 || TREE_CODE (type) == FUNCTION_TYPE
11390 || TREE_CODE (type) == REFERENCE_TYPE)
11392 if (complain & tf_error)
11393 error ("creating array of %qT", type);
11394 return error_mark_node;
11396 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
11398 if (complain & tf_error)
11399 error ("creating array of %qT, which is an abstract class type",
11401 return error_mark_node;
11404 r = build_cplus_array_type (type, domain);
11406 if (TYPE_USER_ALIGN (t))
11408 TYPE_ALIGN (r) = TYPE_ALIGN (t);
11409 TYPE_USER_ALIGN (r) = 1;
11415 case TYPENAME_TYPE:
11417 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11418 in_decl, /*entering_scope=*/1);
11419 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
11420 complain, in_decl);
11422 if (ctx == error_mark_node || f == error_mark_node)
11423 return error_mark_node;
11425 if (!MAYBE_CLASS_TYPE_P (ctx))
11427 if (complain & tf_error)
11428 error ("%qT is not a class, struct, or union type", ctx);
11429 return error_mark_node;
11431 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11433 /* Normally, make_typename_type does not require that the CTX
11434 have complete type in order to allow things like:
11436 template <class T> struct S { typename S<T>::X Y; };
11438 But, such constructs have already been resolved by this
11439 point, so here CTX really should have complete type, unless
11440 it's a partial instantiation. */
11441 ctx = complete_type (ctx);
11442 if (!COMPLETE_TYPE_P (ctx))
11444 if (complain & tf_error)
11445 cxx_incomplete_type_error (NULL_TREE, ctx);
11446 return error_mark_node;
11450 f = make_typename_type (ctx, f, typename_type,
11451 (complain & tf_error) | tf_keep_type_decl);
11452 if (f == error_mark_node)
11454 if (TREE_CODE (f) == TYPE_DECL)
11456 complain |= tf_ignore_bad_quals;
11460 if (TREE_CODE (f) != TYPENAME_TYPE)
11462 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11464 if (complain & tf_error)
11465 error ("%qT resolves to %qT, which is not an enumeration type",
11468 return error_mark_node;
11470 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
11472 if (complain & tf_error)
11473 error ("%qT resolves to %qT, which is is not a class type",
11476 return error_mark_node;
11480 return cp_build_qualified_type_real
11481 (f, cp_type_quals (f) | cp_type_quals (t), complain);
11484 case UNBOUND_CLASS_TEMPLATE:
11486 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11487 in_decl, /*entering_scope=*/1);
11488 tree name = TYPE_IDENTIFIER (t);
11489 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
11491 if (ctx == error_mark_node || name == error_mark_node)
11492 return error_mark_node;
11495 parm_list = tsubst_template_parms (parm_list, args, complain);
11496 return make_unbound_class_template (ctx, name, parm_list, complain);
11503 ++cp_unevaluated_operand;
11504 ++c_inhibit_evaluation_warnings;
11506 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
11508 /*integral_constant_expression_p=*/false);
11510 --cp_unevaluated_operand;
11511 --c_inhibit_evaluation_warnings;
11513 type = finish_typeof (type);
11514 return cp_build_qualified_type_real (type,
11516 | cp_type_quals (type),
11520 case DECLTYPE_TYPE:
11524 ++cp_unevaluated_operand;
11525 ++c_inhibit_evaluation_warnings;
11527 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
11529 /*integral_constant_expression_p=*/false);
11531 --cp_unevaluated_operand;
11532 --c_inhibit_evaluation_warnings;
11534 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
11535 type = lambda_capture_field_type (type);
11536 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
11537 type = lambda_proxy_type (type);
11539 type = finish_decltype_type
11540 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t), complain);
11541 return cp_build_qualified_type_real (type,
11543 | cp_type_quals (type),
11547 case UNDERLYING_TYPE:
11549 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
11550 complain, in_decl);
11551 return finish_underlying_type (type);
11554 case TYPE_ARGUMENT_PACK:
11555 case NONTYPE_ARGUMENT_PACK:
11557 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
11559 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
11563 SET_ARGUMENT_PACK_ARGS (r, packed_out);
11565 /* For template nontype argument packs, also substitute into
11567 if (code == NONTYPE_ARGUMENT_PACK)
11568 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
11586 /* We should use one of the expression tsubsts for these codes. */
11587 gcc_unreachable ();
11590 sorry ("use of %qs in template", tree_code_name [(int) code]);
11591 return error_mark_node;
11595 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11596 type of the expression on the left-hand side of the "." or "->"
11600 tsubst_baselink (tree baselink, tree object_type,
11601 tree args, tsubst_flags_t complain, tree in_decl)
11604 tree qualifying_scope;
11607 tree template_args = 0;
11608 bool template_id_p = false;
11609 bool qualified = BASELINK_QUALIFIED_P (baselink);
11611 /* A baselink indicates a function from a base class. Both the
11612 BASELINK_ACCESS_BINFO and the base class referenced may
11613 indicate bases of the template class, rather than the
11614 instantiated class. In addition, lookups that were not
11615 ambiguous before may be ambiguous now. Therefore, we perform
11616 the lookup again. */
11617 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
11618 qualifying_scope = tsubst (qualifying_scope, args,
11619 complain, in_decl);
11620 fns = BASELINK_FUNCTIONS (baselink);
11621 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
11622 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
11624 template_id_p = true;
11625 template_args = TREE_OPERAND (fns, 1);
11626 fns = TREE_OPERAND (fns, 0);
11628 template_args = tsubst_template_args (template_args, args,
11629 complain, in_decl);
11631 name = DECL_NAME (get_first_fn (fns));
11632 if (IDENTIFIER_TYPENAME_P (name))
11633 name = mangle_conv_op_name_for_type (optype);
11634 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
11636 return error_mark_node;
11638 /* If lookup found a single function, mark it as used at this
11639 point. (If it lookup found multiple functions the one selected
11640 later by overload resolution will be marked as used at that
11642 if (BASELINK_P (baselink))
11643 fns = BASELINK_FUNCTIONS (baselink);
11644 if (!template_id_p && !really_overloaded_fn (fns))
11645 mark_used (OVL_CURRENT (fns));
11647 /* Add back the template arguments, if present. */
11648 if (BASELINK_P (baselink) && template_id_p)
11649 BASELINK_FUNCTIONS (baselink)
11650 = build_nt (TEMPLATE_ID_EXPR,
11651 BASELINK_FUNCTIONS (baselink),
11653 /* Update the conversion operator type. */
11654 BASELINK_OPTYPE (baselink) = optype;
11657 object_type = current_class_type;
11660 baselink = adjust_result_of_qualified_name_lookup (baselink,
11666 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11667 true if the qualified-id will be a postfix-expression in-and-of
11668 itself; false if more of the postfix-expression follows the
11669 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11673 tsubst_qualified_id (tree qualified_id, tree args,
11674 tsubst_flags_t complain, tree in_decl,
11675 bool done, bool address_p)
11681 tree template_args;
11683 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
11685 /* Figure out what name to look up. */
11686 name = TREE_OPERAND (qualified_id, 1);
11687 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11689 is_template = true;
11690 template_args = TREE_OPERAND (name, 1);
11692 template_args = tsubst_template_args (template_args, args,
11693 complain, in_decl);
11694 name = TREE_OPERAND (name, 0);
11698 is_template = false;
11699 template_args = NULL_TREE;
11702 /* Substitute into the qualifying scope. When there are no ARGS, we
11703 are just trying to simplify a non-dependent expression. In that
11704 case the qualifying scope may be dependent, and, in any case,
11705 substituting will not help. */
11706 scope = TREE_OPERAND (qualified_id, 0);
11709 scope = tsubst (scope, args, complain, in_decl);
11710 expr = tsubst_copy (name, args, complain, in_decl);
11715 if (dependent_scope_p (scope))
11718 expr = build_min_nt (TEMPLATE_ID_EXPR, expr, template_args);
11719 return build_qualified_name (NULL_TREE, scope, expr,
11720 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
11723 if (!BASELINK_P (name) && !DECL_P (expr))
11725 if (TREE_CODE (expr) == BIT_NOT_EXPR)
11727 /* A BIT_NOT_EXPR is used to represent a destructor. */
11728 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
11730 error ("qualifying type %qT does not match destructor name ~%qT",
11731 scope, TREE_OPERAND (expr, 0));
11732 expr = error_mark_node;
11735 expr = lookup_qualified_name (scope, complete_dtor_identifier,
11736 /*is_type_p=*/0, false);
11739 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
11740 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
11741 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
11743 if (complain & tf_error)
11745 error ("dependent-name %qE is parsed as a non-type, but "
11746 "instantiation yields a type", qualified_id);
11747 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
11749 return error_mark_node;
11755 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
11757 /* Remember that there was a reference to this entity. */
11761 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
11763 if (complain & tf_error)
11764 qualified_name_lookup_error (scope,
11765 TREE_OPERAND (qualified_id, 1),
11766 expr, input_location);
11767 return error_mark_node;
11771 expr = lookup_template_function (expr, template_args);
11773 if (expr == error_mark_node && complain & tf_error)
11774 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
11775 expr, input_location);
11776 else if (TYPE_P (scope))
11778 expr = (adjust_result_of_qualified_name_lookup
11779 (expr, scope, current_class_type));
11780 expr = (finish_qualified_id_expr
11781 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
11782 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
11783 /*template_arg_p=*/false));
11786 /* Expressions do not generally have reference type. */
11787 if (TREE_CODE (expr) != SCOPE_REF
11788 /* However, if we're about to form a pointer-to-member, we just
11789 want the referenced member referenced. */
11790 && TREE_CODE (expr) != OFFSET_REF)
11791 expr = convert_from_reference (expr);
11796 /* Like tsubst, but deals with expressions. This function just replaces
11797 template parms; to finish processing the resultant expression, use
11798 tsubst_copy_and_build or tsubst_expr. */
11801 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11803 enum tree_code code;
11806 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
11809 code = TREE_CODE (t);
11814 r = retrieve_local_specialization (t);
11820 /* We get here for a use of 'this' in an NSDMI. */
11821 if (DECL_NAME (t) == this_identifier
11822 && at_function_scope_p ()
11823 && DECL_CONSTRUCTOR_P (current_function_decl))
11824 return current_class_ptr;
11826 /* This can happen for a parameter name used later in a function
11827 declaration (such as in a late-specified return type). Just
11828 make a dummy decl, since it's only used for its type. */
11829 gcc_assert (cp_unevaluated_operand != 0);
11830 /* We copy T because want to tsubst the PARM_DECL only,
11831 not the following PARM_DECLs that are chained to T. */
11833 r = tsubst_decl (c, args, complain);
11834 /* Give it the template pattern as its context; its true context
11835 hasn't been instantiated yet and this is good enough for
11837 DECL_CONTEXT (r) = DECL_CONTEXT (t);
11840 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
11841 r = ARGUMENT_PACK_SELECT_ARG (r);
11850 if (DECL_TEMPLATE_PARM_P (t))
11851 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
11852 /* There is no need to substitute into namespace-scope
11854 if (DECL_NAMESPACE_SCOPE_P (t))
11856 /* If ARGS is NULL, then T is known to be non-dependent. */
11857 if (args == NULL_TREE)
11858 return integral_constant_value (t);
11860 /* Unfortunately, we cannot just call lookup_name here.
11863 template <int I> int f() {
11865 struct S { void g() { E e = a; } };
11868 When we instantiate f<7>::S::g(), say, lookup_name is not
11869 clever enough to find f<7>::a. */
11871 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
11872 /*entering_scope=*/0);
11874 for (v = TYPE_VALUES (enum_type);
11876 v = TREE_CHAIN (v))
11877 if (TREE_PURPOSE (v) == DECL_NAME (t))
11878 return TREE_VALUE (v);
11880 /* We didn't find the name. That should never happen; if
11881 name-lookup found it during preliminary parsing, we
11882 should find it again here during instantiation. */
11883 gcc_unreachable ();
11888 if (DECL_CONTEXT (t))
11892 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
11893 /*entering_scope=*/1);
11894 if (ctx != DECL_CONTEXT (t))
11896 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
11899 if (complain & tf_error)
11900 error ("using invalid field %qD", t);
11901 return error_mark_node;
11910 case FUNCTION_DECL:
11911 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
11912 || local_variable_p (t))
11913 t = tsubst (t, args, complain, in_decl);
11917 case NAMESPACE_DECL:
11921 /* An OVERLOAD will always be a non-dependent overload set; an
11922 overload set from function scope will just be represented with an
11923 IDENTIFIER_NODE, and from class scope with a BASELINK. */
11924 gcc_assert (!uses_template_parms (t));
11928 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
11930 case TEMPLATE_DECL:
11931 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11932 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
11933 args, complain, in_decl);
11934 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
11935 return tsubst (t, args, complain, in_decl);
11936 else if (DECL_CLASS_SCOPE_P (t)
11937 && uses_template_parms (DECL_CONTEXT (t)))
11939 /* Template template argument like the following example need
11942 template <template <class> class TT> struct C {};
11943 template <class T> struct D {
11944 template <class U> struct E {};
11949 We are processing the template argument `E' in #1 for
11950 the template instantiation #2. Originally, `E' is a
11951 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
11952 have to substitute this with one having context `D<int>'. */
11954 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
11955 return lookup_field (context, DECL_NAME(t), 0, false);
11958 /* Ordinary template template argument. */
11962 case REINTERPRET_CAST_EXPR:
11963 case CONST_CAST_EXPR:
11964 case STATIC_CAST_EXPR:
11965 case DYNAMIC_CAST_EXPR:
11966 case IMPLICIT_CONV_EXPR:
11970 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11971 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11974 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
11980 ++cp_unevaluated_operand;
11981 ++c_inhibit_evaluation_warnings;
11982 /* We only want to compute the number of arguments. */
11983 expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
11984 complain, in_decl);
11985 --cp_unevaluated_operand;
11986 --c_inhibit_evaluation_warnings;
11988 if (TREE_CODE (expanded) == TREE_VEC)
11989 len = TREE_VEC_LENGTH (expanded);
11991 if (expanded == error_mark_node)
11992 return error_mark_node;
11993 else if (PACK_EXPANSION_P (expanded)
11994 || (TREE_CODE (expanded) == TREE_VEC
11996 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
11998 if (TREE_CODE (expanded) == TREE_VEC)
11999 expanded = TREE_VEC_ELT (expanded, len - 1);
12001 if (TYPE_P (expanded))
12002 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
12003 complain & tf_error);
12005 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
12006 complain & tf_error);
12009 return build_int_cst (size_type_node, len);
12015 case TRUTH_NOT_EXPR:
12018 case UNARY_PLUS_EXPR: /* Unary + */
12020 case AT_ENCODE_EXPR:
12024 case REALPART_EXPR:
12025 case IMAGPART_EXPR:
12027 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12028 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
12030 case COMPONENT_REF:
12035 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
12036 name = TREE_OPERAND (t, 1);
12037 if (TREE_CODE (name) == BIT_NOT_EXPR)
12039 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12040 complain, in_decl);
12041 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12043 else if (TREE_CODE (name) == SCOPE_REF
12044 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
12046 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
12047 complain, in_decl);
12048 name = TREE_OPERAND (name, 1);
12049 name = tsubst_copy (TREE_OPERAND (name, 0), args,
12050 complain, in_decl);
12051 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
12052 name = build_qualified_name (/*type=*/NULL_TREE,
12054 /*template_p=*/false);
12056 else if (BASELINK_P (name))
12057 name = tsubst_baselink (name,
12058 non_reference (TREE_TYPE (object)),
12062 name = tsubst_copy (name, args, complain, in_decl);
12063 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
12069 case TRUNC_DIV_EXPR:
12070 case CEIL_DIV_EXPR:
12071 case FLOOR_DIV_EXPR:
12072 case ROUND_DIV_EXPR:
12073 case EXACT_DIV_EXPR:
12077 case TRUNC_MOD_EXPR:
12078 case FLOOR_MOD_EXPR:
12079 case TRUTH_ANDIF_EXPR:
12080 case TRUTH_ORIF_EXPR:
12081 case TRUTH_AND_EXPR:
12082 case TRUTH_OR_EXPR:
12095 case COMPOUND_EXPR:
12098 case PREDECREMENT_EXPR:
12099 case PREINCREMENT_EXPR:
12100 case POSTDECREMENT_EXPR:
12101 case POSTINCREMENT_EXPR:
12103 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12104 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12107 return build_qualified_name (/*type=*/NULL_TREE,
12108 tsubst_copy (TREE_OPERAND (t, 0),
12109 args, complain, in_decl),
12110 tsubst_copy (TREE_OPERAND (t, 1),
12111 args, complain, in_decl),
12112 QUALIFIED_NAME_IS_TEMPLATE (t));
12117 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12118 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12119 NULL_TREE, NULL_TREE);
12123 int n = VL_EXP_OPERAND_LENGTH (t);
12124 tree result = build_vl_exp (CALL_EXPR, n);
12126 for (i = 0; i < n; i++)
12127 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
12128 complain, in_decl);
12134 case PSEUDO_DTOR_EXPR:
12137 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12138 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12139 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12140 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12147 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12148 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
12149 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
12150 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
12157 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12158 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12159 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
12160 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
12164 case TEMPLATE_ID_EXPR:
12166 /* Substituted template arguments */
12167 tree fn = TREE_OPERAND (t, 0);
12168 tree targs = TREE_OPERAND (t, 1);
12170 fn = tsubst_copy (fn, args, complain, in_decl);
12172 targs = tsubst_template_args (targs, args, complain, in_decl);
12174 return lookup_template_function (fn, targs);
12179 tree purpose, value, chain;
12181 if (t == void_list_node)
12184 purpose = TREE_PURPOSE (t);
12186 purpose = tsubst_copy (purpose, args, complain, in_decl);
12187 value = TREE_VALUE (t);
12189 value = tsubst_copy (value, args, complain, in_decl);
12190 chain = TREE_CHAIN (t);
12191 if (chain && chain != void_type_node)
12192 chain = tsubst_copy (chain, args, complain, in_decl);
12193 if (purpose == TREE_PURPOSE (t)
12194 && value == TREE_VALUE (t)
12195 && chain == TREE_CHAIN (t))
12197 return tree_cons (purpose, value, chain);
12202 case ENUMERAL_TYPE:
12204 case TEMPLATE_TYPE_PARM:
12205 case TEMPLATE_TEMPLATE_PARM:
12206 case BOUND_TEMPLATE_TEMPLATE_PARM:
12207 case TEMPLATE_PARM_INDEX:
12209 case REFERENCE_TYPE:
12211 case FUNCTION_TYPE:
12214 case TYPENAME_TYPE:
12215 case UNBOUND_CLASS_TEMPLATE:
12217 case DECLTYPE_TYPE:
12219 return tsubst (t, args, complain, in_decl);
12223 /* Fall through. */
12224 case IDENTIFIER_NODE:
12225 if (IDENTIFIER_TYPENAME_P (t))
12227 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12228 return mangle_conv_op_name_for_type (new_type);
12234 /* This is handled by tsubst_copy_and_build. */
12235 gcc_unreachable ();
12238 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
12240 tsubst (TREE_TYPE (t), args, complain, in_decl));
12242 case CLEANUP_POINT_EXPR:
12243 /* We shouldn't have built any of these during initial template
12244 generation. Instead, they should be built during instantiation
12245 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12246 gcc_unreachable ();
12250 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
12251 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
12252 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
12253 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
12254 mark_used (TREE_OPERAND (r, 1));
12257 case EXPR_PACK_EXPANSION:
12258 error ("invalid use of pack expansion expression");
12259 return error_mark_node;
12261 case NONTYPE_ARGUMENT_PACK:
12262 error ("use %<...%> to expand argument pack");
12263 return error_mark_node;
12270 /* Instantiate any typedefs in the type. */
12271 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12272 r = fold_convert (type, t);
12273 gcc_assert (TREE_CODE (r) == code);
12278 /* These can sometimes show up in a partial instantiation, but never
12279 involve template parms. */
12280 gcc_assert (!uses_template_parms (t));
12284 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12285 gcc_checking_assert (false);
12290 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12293 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
12296 tree new_clauses = NULL, nc, oc;
12298 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
12300 nc = copy_node (oc);
12301 OMP_CLAUSE_CHAIN (nc) = new_clauses;
12304 switch (OMP_CLAUSE_CODE (nc))
12306 case OMP_CLAUSE_LASTPRIVATE:
12307 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
12309 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
12310 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
12311 in_decl, /*integral_constant_expression_p=*/false);
12312 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
12313 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
12316 case OMP_CLAUSE_PRIVATE:
12317 case OMP_CLAUSE_SHARED:
12318 case OMP_CLAUSE_FIRSTPRIVATE:
12319 case OMP_CLAUSE_REDUCTION:
12320 case OMP_CLAUSE_COPYIN:
12321 case OMP_CLAUSE_COPYPRIVATE:
12322 case OMP_CLAUSE_IF:
12323 case OMP_CLAUSE_NUM_THREADS:
12324 case OMP_CLAUSE_SCHEDULE:
12325 case OMP_CLAUSE_COLLAPSE:
12326 case OMP_CLAUSE_FINAL:
12327 OMP_CLAUSE_OPERAND (nc, 0)
12328 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
12329 in_decl, /*integral_constant_expression_p=*/false);
12331 case OMP_CLAUSE_NOWAIT:
12332 case OMP_CLAUSE_ORDERED:
12333 case OMP_CLAUSE_DEFAULT:
12334 case OMP_CLAUSE_UNTIED:
12335 case OMP_CLAUSE_MERGEABLE:
12338 gcc_unreachable ();
12342 return finish_omp_clauses (nreverse (new_clauses));
12345 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12348 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
12351 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12353 tree purpose, value, chain;
12358 if (TREE_CODE (t) != TREE_LIST)
12359 return tsubst_copy_and_build (t, args, complain, in_decl,
12360 /*function_p=*/false,
12361 /*integral_constant_expression_p=*/false);
12363 if (t == void_list_node)
12366 purpose = TREE_PURPOSE (t);
12368 purpose = RECUR (purpose);
12369 value = TREE_VALUE (t);
12372 if (TREE_CODE (value) != LABEL_DECL)
12373 value = RECUR (value);
12376 value = lookup_label (DECL_NAME (value));
12377 gcc_assert (TREE_CODE (value) == LABEL_DECL);
12378 TREE_USED (value) = 1;
12381 chain = TREE_CHAIN (t);
12382 if (chain && chain != void_type_node)
12383 chain = RECUR (chain);
12384 return tree_cons (purpose, value, chain);
12388 /* Substitute one OMP_FOR iterator. */
12391 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
12392 tree condv, tree incrv, tree *clauses,
12393 tree args, tsubst_flags_t complain, tree in_decl,
12394 bool integral_constant_expression_p)
12396 #define RECUR(NODE) \
12397 tsubst_expr ((NODE), args, complain, in_decl, \
12398 integral_constant_expression_p)
12399 tree decl, init, cond, incr, auto_node;
12401 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
12402 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
12403 decl = RECUR (TREE_OPERAND (init, 0));
12404 init = TREE_OPERAND (init, 1);
12405 auto_node = type_uses_auto (TREE_TYPE (decl));
12406 if (auto_node && init)
12408 tree init_expr = init;
12409 if (TREE_CODE (init_expr) == DECL_EXPR)
12410 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
12411 init_expr = RECUR (init_expr);
12413 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
12415 gcc_assert (!type_dependent_expression_p (decl));
12417 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
12419 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
12420 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
12421 if (TREE_CODE (incr) == MODIFY_EXPR)
12422 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
12423 RECUR (TREE_OPERAND (incr, 1)),
12426 incr = RECUR (incr);
12427 TREE_VEC_ELT (declv, i) = decl;
12428 TREE_VEC_ELT (initv, i) = init;
12429 TREE_VEC_ELT (condv, i) = cond;
12430 TREE_VEC_ELT (incrv, i) = incr;
12434 if (init && TREE_CODE (init) != DECL_EXPR)
12437 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
12439 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
12440 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
12441 && OMP_CLAUSE_DECL (c) == decl)
12443 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12444 && OMP_CLAUSE_DECL (c) == decl)
12445 error ("iteration variable %qD should not be firstprivate", decl);
12446 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
12447 && OMP_CLAUSE_DECL (c) == decl)
12448 error ("iteration variable %qD should not be reduction", decl);
12452 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
12453 OMP_CLAUSE_DECL (c) = decl;
12454 c = finish_omp_clauses (c);
12457 OMP_CLAUSE_CHAIN (c) = *clauses;
12462 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
12463 if (COMPARISON_CLASS_P (cond))
12464 cond = build2 (TREE_CODE (cond), boolean_type_node,
12465 RECUR (TREE_OPERAND (cond, 0)),
12466 RECUR (TREE_OPERAND (cond, 1)));
12468 cond = RECUR (cond);
12469 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
12470 switch (TREE_CODE (incr))
12472 case PREINCREMENT_EXPR:
12473 case PREDECREMENT_EXPR:
12474 case POSTINCREMENT_EXPR:
12475 case POSTDECREMENT_EXPR:
12476 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
12477 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
12480 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12481 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12483 tree rhs = TREE_OPERAND (incr, 1);
12484 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12485 RECUR (TREE_OPERAND (incr, 0)),
12486 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12487 RECUR (TREE_OPERAND (rhs, 0)),
12488 RECUR (TREE_OPERAND (rhs, 1))));
12491 incr = RECUR (incr);
12494 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12495 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12497 tree lhs = RECUR (TREE_OPERAND (incr, 0));
12498 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
12499 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
12500 TREE_TYPE (decl), lhs,
12501 RECUR (TREE_OPERAND (incr, 2))));
12503 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
12504 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
12505 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
12507 tree rhs = TREE_OPERAND (incr, 2);
12508 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12509 RECUR (TREE_OPERAND (incr, 0)),
12510 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12511 RECUR (TREE_OPERAND (rhs, 0)),
12512 RECUR (TREE_OPERAND (rhs, 1))));
12515 incr = RECUR (incr);
12518 incr = RECUR (incr);
12522 TREE_VEC_ELT (declv, i) = decl;
12523 TREE_VEC_ELT (initv, i) = init;
12524 TREE_VEC_ELT (condv, i) = cond;
12525 TREE_VEC_ELT (incrv, i) = incr;
12529 /* Like tsubst_copy for expressions, etc. but also does semantic
12533 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
12534 bool integral_constant_expression_p)
12536 #define RECUR(NODE) \
12537 tsubst_expr ((NODE), args, complain, in_decl, \
12538 integral_constant_expression_p)
12542 if (t == NULL_TREE || t == error_mark_node)
12545 if (EXPR_HAS_LOCATION (t))
12546 input_location = EXPR_LOCATION (t);
12547 if (STATEMENT_CODE_P (TREE_CODE (t)))
12548 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
12550 switch (TREE_CODE (t))
12552 case STATEMENT_LIST:
12554 tree_stmt_iterator i;
12555 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
12556 RECUR (tsi_stmt (i));
12560 case CTOR_INITIALIZER:
12561 finish_mem_initializers (tsubst_initializer_list
12562 (TREE_OPERAND (t, 0), args));
12566 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
12570 tmp = RECUR (EXPR_STMT_EXPR (t));
12571 if (EXPR_STMT_STMT_EXPR_RESULT (t))
12572 finish_stmt_expr_expr (tmp, cur_stmt_expr);
12574 finish_expr_stmt (tmp);
12578 do_using_directive (USING_STMT_NAMESPACE (t));
12583 tree decl, pattern_decl;
12586 pattern_decl = decl = DECL_EXPR_DECL (t);
12587 if (TREE_CODE (decl) == LABEL_DECL)
12588 finish_label_decl (DECL_NAME (decl));
12589 else if (TREE_CODE (decl) == USING_DECL)
12591 tree scope = USING_DECL_SCOPE (decl);
12592 tree name = DECL_NAME (decl);
12595 scope = tsubst (scope, args, complain, in_decl);
12596 decl = lookup_qualified_name (scope, name,
12597 /*is_type_p=*/false,
12598 /*complain=*/false);
12599 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
12600 qualified_name_lookup_error (scope, name, decl, input_location);
12602 do_local_using_decl (decl, scope, name);
12606 init = DECL_INITIAL (decl);
12607 decl = tsubst (decl, args, complain, in_decl);
12608 if (decl != error_mark_node)
12610 /* By marking the declaration as instantiated, we avoid
12611 trying to instantiate it. Since instantiate_decl can't
12612 handle local variables, and since we've already done
12613 all that needs to be done, that's the right thing to
12615 if (TREE_CODE (decl) == VAR_DECL)
12616 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12617 if (TREE_CODE (decl) == VAR_DECL
12618 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
12619 /* Anonymous aggregates are a special case. */
12620 finish_anon_union (decl);
12621 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
12623 DECL_CONTEXT (decl) = current_function_decl;
12624 if (DECL_NAME (decl) == this_identifier)
12626 tree lam = DECL_CONTEXT (current_function_decl);
12627 lam = CLASSTYPE_LAMBDA_EXPR (lam);
12628 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
12630 insert_capture_proxy (decl);
12634 int const_init = false;
12635 maybe_push_decl (decl);
12636 if (TREE_CODE (decl) == VAR_DECL
12637 && DECL_PRETTY_FUNCTION_P (decl))
12639 /* For __PRETTY_FUNCTION__ we have to adjust the
12641 const char *const name
12642 = cxx_printable_name (current_function_decl, 2);
12643 init = cp_fname_init (name, &TREE_TYPE (decl));
12647 tree t = RECUR (init);
12651 /* If we had an initializer but it
12652 instantiated to nothing,
12653 value-initialize the object. This will
12654 only occur when the initializer was a
12655 pack expansion where the parameter packs
12656 used in that expansion were of length
12658 init = build_value_init (TREE_TYPE (decl),
12660 if (TREE_CODE (init) == AGGR_INIT_EXPR)
12661 init = get_target_expr_sfinae (init, complain);
12667 if (TREE_CODE (decl) == VAR_DECL)
12668 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12670 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
12675 /* A DECL_EXPR can also be used as an expression, in the condition
12676 clause of an if/for/while construct. */
12681 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12682 RECUR (FOR_INIT_STMT (t));
12683 finish_for_init_stmt (stmt);
12684 tmp = RECUR (FOR_COND (t));
12685 finish_for_cond (tmp, stmt);
12686 tmp = RECUR (FOR_EXPR (t));
12687 finish_for_expr (tmp, stmt);
12688 RECUR (FOR_BODY (t));
12689 finish_for_stmt (stmt);
12692 case RANGE_FOR_STMT:
12695 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12696 decl = RANGE_FOR_DECL (t);
12697 decl = tsubst (decl, args, complain, in_decl);
12698 maybe_push_decl (decl);
12699 expr = RECUR (RANGE_FOR_EXPR (t));
12700 stmt = cp_convert_range_for (stmt, decl, expr);
12701 RECUR (RANGE_FOR_BODY (t));
12702 finish_for_stmt (stmt);
12707 stmt = begin_while_stmt ();
12708 tmp = RECUR (WHILE_COND (t));
12709 finish_while_stmt_cond (tmp, stmt);
12710 RECUR (WHILE_BODY (t));
12711 finish_while_stmt (stmt);
12715 stmt = begin_do_stmt ();
12716 RECUR (DO_BODY (t));
12717 finish_do_body (stmt);
12718 tmp = RECUR (DO_COND (t));
12719 finish_do_stmt (tmp, stmt);
12723 stmt = begin_if_stmt ();
12724 tmp = RECUR (IF_COND (t));
12725 finish_if_stmt_cond (tmp, stmt);
12726 RECUR (THEN_CLAUSE (t));
12727 finish_then_clause (stmt);
12729 if (ELSE_CLAUSE (t))
12731 begin_else_clause (stmt);
12732 RECUR (ELSE_CLAUSE (t));
12733 finish_else_clause (stmt);
12736 finish_if_stmt (stmt);
12740 if (BIND_EXPR_BODY_BLOCK (t))
12741 stmt = begin_function_body ();
12743 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
12744 ? BCS_TRY_BLOCK : 0);
12746 RECUR (BIND_EXPR_BODY (t));
12748 if (BIND_EXPR_BODY_BLOCK (t))
12749 finish_function_body (stmt);
12751 finish_compound_stmt (stmt);
12755 finish_break_stmt ();
12758 case CONTINUE_STMT:
12759 finish_continue_stmt ();
12763 stmt = begin_switch_stmt ();
12764 tmp = RECUR (SWITCH_STMT_COND (t));
12765 finish_switch_cond (tmp, stmt);
12766 RECUR (SWITCH_STMT_BODY (t));
12767 finish_switch_stmt (stmt);
12770 case CASE_LABEL_EXPR:
12771 finish_case_label (EXPR_LOCATION (t),
12772 RECUR (CASE_LOW (t)),
12773 RECUR (CASE_HIGH (t)));
12778 tree decl = LABEL_EXPR_LABEL (t);
12781 label = finish_label_stmt (DECL_NAME (decl));
12782 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
12783 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
12788 tmp = GOTO_DESTINATION (t);
12789 if (TREE_CODE (tmp) != LABEL_DECL)
12790 /* Computed goto's must be tsubst'd into. On the other hand,
12791 non-computed gotos must not be; the identifier in question
12792 will have no binding. */
12795 tmp = DECL_NAME (tmp);
12796 finish_goto_stmt (tmp);
12800 tmp = finish_asm_stmt
12801 (ASM_VOLATILE_P (t),
12802 RECUR (ASM_STRING (t)),
12803 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
12804 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
12805 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
12806 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
12808 tree asm_expr = tmp;
12809 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
12810 asm_expr = TREE_OPERAND (asm_expr, 0);
12811 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
12818 stmt = begin_try_block ();
12819 RECUR (TRY_STMTS (t));
12820 finish_cleanup_try_block (stmt);
12821 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
12825 tree compound_stmt = NULL_TREE;
12827 if (FN_TRY_BLOCK_P (t))
12828 stmt = begin_function_try_block (&compound_stmt);
12830 stmt = begin_try_block ();
12832 RECUR (TRY_STMTS (t));
12834 if (FN_TRY_BLOCK_P (t))
12835 finish_function_try_block (stmt);
12837 finish_try_block (stmt);
12839 RECUR (TRY_HANDLERS (t));
12840 if (FN_TRY_BLOCK_P (t))
12841 finish_function_handler_sequence (stmt, compound_stmt);
12843 finish_handler_sequence (stmt);
12849 tree decl = HANDLER_PARMS (t);
12853 decl = tsubst (decl, args, complain, in_decl);
12854 /* Prevent instantiate_decl from trying to instantiate
12855 this variable. We've already done all that needs to be
12857 if (decl != error_mark_node)
12858 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12860 stmt = begin_handler ();
12861 finish_handler_parms (decl, stmt);
12862 RECUR (HANDLER_BODY (t));
12863 finish_handler (stmt);
12868 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12871 case STATIC_ASSERT:
12874 tsubst_expr (STATIC_ASSERT_CONDITION (t),
12877 /*integral_constant_expression_p=*/true);
12878 finish_static_assert (condition,
12879 STATIC_ASSERT_MESSAGE (t),
12880 STATIC_ASSERT_SOURCE_LOCATION (t),
12881 /*member_p=*/false);
12886 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
12887 args, complain, in_decl);
12888 stmt = begin_omp_parallel ();
12889 RECUR (OMP_PARALLEL_BODY (t));
12890 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
12891 = OMP_PARALLEL_COMBINED (t);
12895 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
12896 args, complain, in_decl);
12897 stmt = begin_omp_task ();
12898 RECUR (OMP_TASK_BODY (t));
12899 finish_omp_task (tmp, stmt);
12904 tree clauses, body, pre_body;
12905 tree declv, initv, condv, incrv;
12908 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
12909 args, complain, in_decl);
12910 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12911 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12912 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12913 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12915 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
12916 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
12917 &clauses, args, complain, in_decl,
12918 integral_constant_expression_p);
12920 stmt = begin_omp_structured_block ();
12922 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
12923 if (TREE_VEC_ELT (initv, i) == NULL
12924 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
12925 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
12926 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
12928 tree init = RECUR (TREE_VEC_ELT (initv, i));
12929 gcc_assert (init == TREE_VEC_ELT (declv, i));
12930 TREE_VEC_ELT (initv, i) = NULL_TREE;
12934 tree decl_expr = TREE_VEC_ELT (initv, i);
12935 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
12936 gcc_assert (init != NULL);
12937 TREE_VEC_ELT (initv, i) = RECUR (init);
12938 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
12940 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
12943 pre_body = push_stmt_list ();
12944 RECUR (OMP_FOR_PRE_BODY (t));
12945 pre_body = pop_stmt_list (pre_body);
12947 body = push_stmt_list ();
12948 RECUR (OMP_FOR_BODY (t));
12949 body = pop_stmt_list (body);
12951 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
12952 body, pre_body, clauses);
12954 add_stmt (finish_omp_structured_block (stmt));
12960 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
12961 stmt = push_stmt_list ();
12962 RECUR (OMP_BODY (t));
12963 stmt = pop_stmt_list (stmt);
12966 OMP_BODY (t) = stmt;
12967 OMP_CLAUSES (t) = tmp;
12975 stmt = push_stmt_list ();
12976 RECUR (OMP_BODY (t));
12977 stmt = pop_stmt_list (stmt);
12980 OMP_BODY (t) = stmt;
12985 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
12986 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
12988 tree op1 = TREE_OPERAND (t, 1);
12989 tree rhs1 = NULL_TREE;
12991 if (TREE_CODE (op1) == COMPOUND_EXPR)
12993 rhs1 = RECUR (TREE_OPERAND (op1, 0));
12994 op1 = TREE_OPERAND (op1, 1);
12996 lhs = RECUR (TREE_OPERAND (op1, 0));
12997 rhs = RECUR (TREE_OPERAND (op1, 1));
12998 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
12999 NULL_TREE, NULL_TREE, rhs1);
13003 tree op1 = TREE_OPERAND (t, 1);
13004 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
13005 tree rhs1 = NULL_TREE;
13006 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
13007 enum tree_code opcode = NOP_EXPR;
13008 if (code == OMP_ATOMIC_READ)
13010 v = RECUR (TREE_OPERAND (op1, 0));
13011 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13013 else if (code == OMP_ATOMIC_CAPTURE_OLD
13014 || code == OMP_ATOMIC_CAPTURE_NEW)
13016 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
13017 v = RECUR (TREE_OPERAND (op1, 0));
13018 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
13019 if (TREE_CODE (op11) == COMPOUND_EXPR)
13021 rhs1 = RECUR (TREE_OPERAND (op11, 0));
13022 op11 = TREE_OPERAND (op11, 1);
13024 lhs = RECUR (TREE_OPERAND (op11, 0));
13025 rhs = RECUR (TREE_OPERAND (op11, 1));
13026 opcode = TREE_CODE (op11);
13031 lhs = RECUR (TREE_OPERAND (op1, 0));
13032 rhs = RECUR (TREE_OPERAND (op1, 1));
13034 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
13038 case TRANSACTION_EXPR:
13041 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
13042 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
13044 if (TRANSACTION_EXPR_IS_STMT (t))
13046 tree body = TRANSACTION_EXPR_BODY (t);
13047 tree noex = NULL_TREE;
13048 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
13050 noex = MUST_NOT_THROW_COND (body);
13051 if (noex == NULL_TREE)
13052 noex = boolean_true_node;
13053 body = TREE_OPERAND (body, 0);
13055 stmt = begin_transaction_stmt (input_location, NULL, flags);
13057 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
13061 stmt = build_transaction_expr (EXPR_LOCATION (t),
13062 RECUR (TRANSACTION_EXPR_BODY (t)),
13069 case MUST_NOT_THROW_EXPR:
13070 return build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
13071 RECUR (MUST_NOT_THROW_COND (t)));
13073 case EXPR_PACK_EXPANSION:
13074 error ("invalid use of pack expansion expression");
13075 return error_mark_node;
13077 case NONTYPE_ARGUMENT_PACK:
13078 error ("use %<...%> to expand argument pack");
13079 return error_mark_node;
13082 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
13084 return tsubst_copy_and_build (t, args, complain, in_decl,
13085 /*function_p=*/false,
13086 integral_constant_expression_p);
13093 /* T is a postfix-expression that is not being used in a function
13094 call. Return the substituted version of T. */
13097 tsubst_non_call_postfix_expression (tree t, tree args,
13098 tsubst_flags_t complain,
13101 if (TREE_CODE (t) == SCOPE_REF)
13102 t = tsubst_qualified_id (t, args, complain, in_decl,
13103 /*done=*/false, /*address_p=*/false);
13105 t = tsubst_copy_and_build (t, args, complain, in_decl,
13106 /*function_p=*/false,
13107 /*integral_constant_expression_p=*/false);
13112 /* Like tsubst but deals with expressions and performs semantic
13113 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13116 tsubst_copy_and_build (tree t,
13118 tsubst_flags_t complain,
13121 bool integral_constant_expression_p)
13123 #define RECUR(NODE) \
13124 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13125 /*function_p=*/false, \
13126 integral_constant_expression_p)
13130 if (t == NULL_TREE || t == error_mark_node)
13133 switch (TREE_CODE (t))
13137 /* Fall through. */
13138 case IDENTIFIER_NODE:
13142 bool non_integral_constant_expression_p;
13143 const char *error_msg;
13145 if (IDENTIFIER_TYPENAME_P (t))
13147 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13148 t = mangle_conv_op_name_for_type (new_type);
13151 /* Look up the name. */
13152 decl = lookup_name (t);
13154 /* By convention, expressions use ERROR_MARK_NODE to indicate
13155 failure, not NULL_TREE. */
13156 if (decl == NULL_TREE)
13157 decl = error_mark_node;
13159 decl = finish_id_expression (t, decl, NULL_TREE,
13161 integral_constant_expression_p,
13162 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx0x),
13163 &non_integral_constant_expression_p,
13164 /*template_p=*/false,
13166 /*address_p=*/false,
13167 /*template_arg_p=*/false,
13172 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
13174 if (complain & tf_error)
13175 unqualified_name_lookup_error (decl);
13176 decl = error_mark_node;
13181 case TEMPLATE_ID_EXPR:
13184 tree templ = RECUR (TREE_OPERAND (t, 0));
13185 tree targs = TREE_OPERAND (t, 1);
13188 targs = tsubst_template_args (targs, args, complain, in_decl);
13190 if (TREE_CODE (templ) == COMPONENT_REF)
13192 object = TREE_OPERAND (templ, 0);
13193 templ = TREE_OPERAND (templ, 1);
13196 object = NULL_TREE;
13197 templ = lookup_template_function (templ, targs);
13200 return build3 (COMPONENT_REF, TREE_TYPE (templ),
13201 object, templ, NULL_TREE);
13203 return baselink_for_fns (templ);
13208 tree r = RECUR (TREE_OPERAND (t, 0));
13210 if (REFERENCE_REF_P (t))
13212 /* A type conversion to reference type will be enclosed in
13213 such an indirect ref, but the substitution of the cast
13214 will have also added such an indirect ref. */
13215 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
13216 r = convert_from_reference (r);
13219 r = build_x_indirect_ref (r, RO_UNARY_STAR, complain);
13225 (tsubst (TREE_TYPE (t), args, complain, in_decl),
13226 RECUR (TREE_OPERAND (t, 0)));
13228 case IMPLICIT_CONV_EXPR:
13230 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13231 tree expr = RECUR (TREE_OPERAND (t, 0));
13232 int flags = LOOKUP_IMPLICIT;
13233 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
13234 flags = LOOKUP_NORMAL;
13235 return perform_implicit_conversion_flags (type, expr, complain,
13242 tsubst (TREE_TYPE (t), args, complain, in_decl),
13243 RECUR (TREE_OPERAND (t, 0)));
13246 case REINTERPRET_CAST_EXPR:
13247 case CONST_CAST_EXPR:
13248 case DYNAMIC_CAST_EXPR:
13249 case STATIC_CAST_EXPR:
13254 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13255 if (integral_constant_expression_p
13256 && !cast_valid_in_integral_constant_expression_p (type))
13258 if (complain & tf_error)
13259 error ("a cast to a type other than an integral or "
13260 "enumeration type cannot appear in a constant-expression");
13261 return error_mark_node;
13264 op = RECUR (TREE_OPERAND (t, 0));
13266 switch (TREE_CODE (t))
13269 return build_functional_cast (type, op, complain);
13270 case REINTERPRET_CAST_EXPR:
13271 return build_reinterpret_cast (type, op, complain);
13272 case CONST_CAST_EXPR:
13273 return build_const_cast (type, op, complain);
13274 case DYNAMIC_CAST_EXPR:
13275 return build_dynamic_cast (type, op, complain);
13276 case STATIC_CAST_EXPR:
13277 return build_static_cast (type, op, complain);
13279 gcc_unreachable ();
13283 case POSTDECREMENT_EXPR:
13284 case POSTINCREMENT_EXPR:
13285 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13286 args, complain, in_decl);
13287 return build_x_unary_op (TREE_CODE (t), op1, complain);
13289 case PREDECREMENT_EXPR:
13290 case PREINCREMENT_EXPR:
13294 case TRUTH_NOT_EXPR:
13295 case UNARY_PLUS_EXPR: /* Unary + */
13296 case REALPART_EXPR:
13297 case IMAGPART_EXPR:
13298 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
13301 case FIX_TRUNC_EXPR:
13302 return cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
13306 op1 = TREE_OPERAND (t, 0);
13307 if (TREE_CODE (op1) == LABEL_DECL)
13308 return finish_label_address_expr (DECL_NAME (op1),
13309 EXPR_LOCATION (op1));
13310 if (TREE_CODE (op1) == SCOPE_REF)
13311 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
13312 /*done=*/true, /*address_p=*/true);
13314 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
13316 return build_x_unary_op (ADDR_EXPR, op1, complain);
13321 case TRUNC_DIV_EXPR:
13322 case CEIL_DIV_EXPR:
13323 case FLOOR_DIV_EXPR:
13324 case ROUND_DIV_EXPR:
13325 case EXACT_DIV_EXPR:
13329 case TRUNC_MOD_EXPR:
13330 case FLOOR_MOD_EXPR:
13331 case TRUTH_ANDIF_EXPR:
13332 case TRUTH_ORIF_EXPR:
13333 case TRUTH_AND_EXPR:
13334 case TRUTH_OR_EXPR:
13350 tree r = build_x_binary_op
13352 RECUR (TREE_OPERAND (t, 0)),
13353 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
13355 : TREE_CODE (TREE_OPERAND (t, 0))),
13356 RECUR (TREE_OPERAND (t, 1)),
13357 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
13359 : TREE_CODE (TREE_OPERAND (t, 1))),
13362 if (EXPR_P (r) && TREE_NO_WARNING (t))
13363 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13368 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
13369 /*address_p=*/false);
13371 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13372 args, complain, in_decl);
13373 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
13376 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
13377 return tsubst_copy (t, args, complain, in_decl);
13381 op1 = TREE_OPERAND (t, 0);
13384 /* When there are no ARGS, we are trying to evaluate a
13385 non-dependent expression from the parser. Trying to do
13386 the substitutions may not work. */
13388 op1 = TREE_TYPE (op1);
13392 ++cp_unevaluated_operand;
13393 ++c_inhibit_evaluation_warnings;
13394 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13395 /*function_p=*/false,
13396 /*integral_constant_expression_p=*/false);
13397 --cp_unevaluated_operand;
13398 --c_inhibit_evaluation_warnings;
13401 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
13402 complain & tf_error);
13404 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
13405 complain & tf_error);
13407 case AT_ENCODE_EXPR:
13409 op1 = TREE_OPERAND (t, 0);
13410 ++cp_unevaluated_operand;
13411 ++c_inhibit_evaluation_warnings;
13412 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13413 /*function_p=*/false,
13414 /*integral_constant_expression_p=*/false);
13415 --cp_unevaluated_operand;
13416 --c_inhibit_evaluation_warnings;
13417 return objc_build_encode_expr (op1);
13420 case NOEXCEPT_EXPR:
13421 op1 = TREE_OPERAND (t, 0);
13422 ++cp_unevaluated_operand;
13423 ++c_inhibit_evaluation_warnings;
13424 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
13425 /*function_p=*/false,
13426 /*integral_constant_expression_p=*/false);
13427 --cp_unevaluated_operand;
13428 --c_inhibit_evaluation_warnings;
13429 return finish_noexcept_expr (op1, complain);
13433 tree r = build_x_modify_expr
13434 (RECUR (TREE_OPERAND (t, 0)),
13435 TREE_CODE (TREE_OPERAND (t, 1)),
13436 RECUR (TREE_OPERAND (t, 2)),
13438 /* TREE_NO_WARNING must be set if either the expression was
13439 parenthesized or it uses an operator such as >>= rather
13440 than plain assignment. In the former case, it was already
13441 set and must be copied. In the latter case,
13442 build_x_modify_expr sets it and it must not be reset
13444 if (TREE_NO_WARNING (t))
13445 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
13450 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13451 args, complain, in_decl);
13452 /* Remember that there was a reference to this entity. */
13455 return build_x_arrow (op1);
13459 tree placement = RECUR (TREE_OPERAND (t, 0));
13460 tree init = RECUR (TREE_OPERAND (t, 3));
13461 VEC(tree,gc) *placement_vec;
13462 VEC(tree,gc) *init_vec;
13465 if (placement == NULL_TREE)
13466 placement_vec = NULL;
13469 placement_vec = make_tree_vector ();
13470 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
13471 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
13474 /* If there was an initializer in the original tree, but it
13475 instantiated to an empty list, then we should pass a
13476 non-NULL empty vector to tell build_new that it was an
13477 empty initializer() rather than no initializer. This can
13478 only happen when the initializer is a pack expansion whose
13479 parameter packs are of length zero. */
13480 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
13484 init_vec = make_tree_vector ();
13485 if (init == void_zero_node)
13486 gcc_assert (init_vec != NULL);
13489 for (; init != NULL_TREE; init = TREE_CHAIN (init))
13490 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
13494 ret = build_new (&placement_vec,
13495 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
13496 RECUR (TREE_OPERAND (t, 2)),
13498 NEW_EXPR_USE_GLOBAL (t),
13501 if (placement_vec != NULL)
13502 release_tree_vector (placement_vec);
13503 if (init_vec != NULL)
13504 release_tree_vector (init_vec);
13510 return delete_sanity
13511 (RECUR (TREE_OPERAND (t, 0)),
13512 RECUR (TREE_OPERAND (t, 1)),
13513 DELETE_EXPR_USE_VEC (t),
13514 DELETE_EXPR_USE_GLOBAL (t),
13517 case COMPOUND_EXPR:
13518 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
13519 RECUR (TREE_OPERAND (t, 1)),
13525 VEC(tree,gc) *call_args;
13526 unsigned int nargs, i;
13531 function = CALL_EXPR_FN (t);
13532 /* When we parsed the expression, we determined whether or
13533 not Koenig lookup should be performed. */
13534 koenig_p = KOENIG_LOOKUP_P (t);
13535 if (TREE_CODE (function) == SCOPE_REF)
13537 qualified_p = true;
13538 function = tsubst_qualified_id (function, args, complain, in_decl,
13540 /*address_p=*/false);
13542 else if (koenig_p && TREE_CODE (function) == IDENTIFIER_NODE)
13544 /* Do nothing; calling tsubst_copy_and_build on an identifier
13545 would incorrectly perform unqualified lookup again.
13547 Note that we can also have an IDENTIFIER_NODE if the earlier
13548 unqualified lookup found a member function; in that case
13549 koenig_p will be false and we do want to do the lookup
13550 again to find the instantiated member function.
13552 FIXME but doing that causes c++/15272, so we need to stop
13553 using IDENTIFIER_NODE in that situation. */
13554 qualified_p = false;
13558 if (TREE_CODE (function) == COMPONENT_REF)
13560 tree op = TREE_OPERAND (function, 1);
13562 qualified_p = (TREE_CODE (op) == SCOPE_REF
13563 || (BASELINK_P (op)
13564 && BASELINK_QUALIFIED_P (op)));
13567 qualified_p = false;
13569 function = tsubst_copy_and_build (function, args, complain,
13572 integral_constant_expression_p);
13574 if (BASELINK_P (function))
13575 qualified_p = true;
13578 nargs = call_expr_nargs (t);
13579 call_args = make_tree_vector ();
13580 for (i = 0; i < nargs; ++i)
13582 tree arg = CALL_EXPR_ARG (t, i);
13584 if (!PACK_EXPANSION_P (arg))
13585 VEC_safe_push (tree, gc, call_args,
13586 RECUR (CALL_EXPR_ARG (t, i)));
13589 /* Expand the pack expansion and push each entry onto
13591 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
13592 if (TREE_CODE (arg) == TREE_VEC)
13594 unsigned int len, j;
13596 len = TREE_VEC_LENGTH (arg);
13597 for (j = 0; j < len; ++j)
13599 tree value = TREE_VEC_ELT (arg, j);
13600 if (value != NULL_TREE)
13601 value = convert_from_reference (value);
13602 VEC_safe_push (tree, gc, call_args, value);
13607 /* A partial substitution. Add one entry. */
13608 VEC_safe_push (tree, gc, call_args, arg);
13613 /* We do not perform argument-dependent lookup if normal
13614 lookup finds a non-function, in accordance with the
13615 expected resolution of DR 218. */
13617 && ((is_overloaded_fn (function)
13618 /* If lookup found a member function, the Koenig lookup is
13619 not appropriate, even if an unqualified-name was used
13620 to denote the function. */
13621 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
13622 || TREE_CODE (function) == IDENTIFIER_NODE)
13623 /* Only do this when substitution turns a dependent call
13624 into a non-dependent call. */
13625 && type_dependent_expression_p_push (t)
13626 && !any_type_dependent_arguments_p (call_args))
13627 function = perform_koenig_lookup (function, call_args, false,
13630 if (TREE_CODE (function) == IDENTIFIER_NODE
13631 && !any_type_dependent_arguments_p (call_args))
13633 if (koenig_p && (complain & tf_warning_or_error))
13635 /* For backwards compatibility and good diagnostics, try
13636 the unqualified lookup again if we aren't in SFINAE
13638 tree unq = (tsubst_copy_and_build
13639 (function, args, complain, in_decl, true,
13640 integral_constant_expression_p));
13641 if (unq == error_mark_node)
13642 return error_mark_node;
13644 if (unq != function)
13647 if (TREE_CODE (fn) == INDIRECT_REF)
13648 fn = TREE_OPERAND (fn, 0);
13649 if (TREE_CODE (fn) == COMPONENT_REF)
13650 fn = TREE_OPERAND (fn, 1);
13651 if (is_overloaded_fn (fn))
13652 fn = get_first_fn (fn);
13653 permerror (EXPR_LOC_OR_HERE (t),
13654 "%qD was not declared in this scope, "
13655 "and no declarations were found by "
13656 "argument-dependent lookup at the point "
13657 "of instantiation", function);
13659 /* Can't say anything more. */;
13660 else if (DECL_CLASS_SCOPE_P (fn))
13662 inform (EXPR_LOC_OR_HERE (t),
13663 "declarations in dependent base %qT are "
13664 "not found by unqualified lookup",
13665 DECL_CLASS_CONTEXT (fn));
13666 if (current_class_ptr)
13667 inform (EXPR_LOC_OR_HERE (t),
13668 "use %<this->%D%> instead", function);
13670 inform (EXPR_LOC_OR_HERE (t),
13671 "use %<%T::%D%> instead",
13672 current_class_name, function);
13675 inform (0, "%q+D declared here, later in the "
13676 "translation unit", fn);
13680 if (TREE_CODE (function) == IDENTIFIER_NODE)
13682 unqualified_name_lookup_error (function);
13683 release_tree_vector (call_args);
13684 return error_mark_node;
13688 /* Remember that there was a reference to this entity. */
13689 if (DECL_P (function))
13690 mark_used (function);
13692 if (TREE_CODE (function) == OFFSET_REF)
13693 ret = build_offset_ref_call_from_tree (function, &call_args);
13694 else if (TREE_CODE (function) == COMPONENT_REF)
13696 tree instance = TREE_OPERAND (function, 0);
13697 tree fn = TREE_OPERAND (function, 1);
13699 if (processing_template_decl
13700 && (type_dependent_expression_p (instance)
13701 || (!BASELINK_P (fn)
13702 && TREE_CODE (fn) != FIELD_DECL)
13703 || type_dependent_expression_p (fn)
13704 || any_type_dependent_arguments_p (call_args)))
13705 ret = build_nt_call_vec (function, call_args);
13706 else if (!BASELINK_P (fn))
13707 ret = finish_call_expr (function, &call_args,
13708 /*disallow_virtual=*/false,
13709 /*koenig_p=*/false,
13712 ret = (build_new_method_call
13714 &call_args, NULL_TREE,
13715 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
13720 ret = finish_call_expr (function, &call_args,
13721 /*disallow_virtual=*/qualified_p,
13725 release_tree_vector (call_args);
13731 return build_x_conditional_expr
13732 (RECUR (TREE_OPERAND (t, 0)),
13733 RECUR (TREE_OPERAND (t, 1)),
13734 RECUR (TREE_OPERAND (t, 2)),
13737 case PSEUDO_DTOR_EXPR:
13738 return finish_pseudo_destructor_expr
13739 (RECUR (TREE_OPERAND (t, 0)),
13740 RECUR (TREE_OPERAND (t, 1)),
13741 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl));
13745 tree purpose, value, chain;
13747 if (t == void_list_node)
13750 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
13751 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
13753 /* We have pack expansions, so expand those and
13754 create a new list out of it. */
13755 tree purposevec = NULL_TREE;
13756 tree valuevec = NULL_TREE;
13760 /* Expand the argument expressions. */
13761 if (TREE_PURPOSE (t))
13762 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
13763 complain, in_decl);
13764 if (TREE_VALUE (t))
13765 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
13766 complain, in_decl);
13768 /* Build the rest of the list. */
13769 chain = TREE_CHAIN (t);
13770 if (chain && chain != void_type_node)
13771 chain = RECUR (chain);
13773 /* Determine the number of arguments. */
13774 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
13776 len = TREE_VEC_LENGTH (purposevec);
13777 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
13779 else if (TREE_CODE (valuevec) == TREE_VEC)
13780 len = TREE_VEC_LENGTH (valuevec);
13783 /* Since we only performed a partial substitution into
13784 the argument pack, we only return a single list
13786 if (purposevec == TREE_PURPOSE (t)
13787 && valuevec == TREE_VALUE (t)
13788 && chain == TREE_CHAIN (t))
13791 return tree_cons (purposevec, valuevec, chain);
13794 /* Convert the argument vectors into a TREE_LIST */
13798 /* Grab the Ith values. */
13800 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
13803 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
13806 /* Build the list (backwards). */
13807 chain = tree_cons (purpose, value, chain);
13813 purpose = TREE_PURPOSE (t);
13815 purpose = RECUR (purpose);
13816 value = TREE_VALUE (t);
13818 value = RECUR (value);
13819 chain = TREE_CHAIN (t);
13820 if (chain && chain != void_type_node)
13821 chain = RECUR (chain);
13822 if (purpose == TREE_PURPOSE (t)
13823 && value == TREE_VALUE (t)
13824 && chain == TREE_CHAIN (t))
13826 return tree_cons (purpose, value, chain);
13829 case COMPONENT_REF:
13835 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13836 args, complain, in_decl);
13837 /* Remember that there was a reference to this entity. */
13838 if (DECL_P (object))
13839 mark_used (object);
13840 object_type = TREE_TYPE (object);
13842 member = TREE_OPERAND (t, 1);
13843 if (BASELINK_P (member))
13844 member = tsubst_baselink (member,
13845 non_reference (TREE_TYPE (object)),
13846 args, complain, in_decl);
13848 member = tsubst_copy (member, args, complain, in_decl);
13849 if (member == error_mark_node)
13850 return error_mark_node;
13852 if (type_dependent_expression_p (object))
13853 /* We can't do much here. */;
13854 else if (!CLASS_TYPE_P (object_type))
13856 if (SCALAR_TYPE_P (object_type))
13858 tree s = NULL_TREE;
13859 tree dtor = member;
13861 if (TREE_CODE (dtor) == SCOPE_REF)
13863 s = TREE_OPERAND (dtor, 0);
13864 dtor = TREE_OPERAND (dtor, 1);
13866 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
13868 dtor = TREE_OPERAND (dtor, 0);
13870 return finish_pseudo_destructor_expr (object, s, dtor);
13874 else if (TREE_CODE (member) == SCOPE_REF
13875 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
13877 /* Lookup the template functions now that we know what the
13879 tree scope = TREE_OPERAND (member, 0);
13880 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
13881 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
13882 member = lookup_qualified_name (scope, tmpl,
13883 /*is_type_p=*/false,
13884 /*complain=*/false);
13885 if (BASELINK_P (member))
13887 BASELINK_FUNCTIONS (member)
13888 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
13890 member = (adjust_result_of_qualified_name_lookup
13891 (member, BINFO_TYPE (BASELINK_BINFO (member)),
13896 qualified_name_lookup_error (scope, tmpl, member,
13898 return error_mark_node;
13901 else if (TREE_CODE (member) == SCOPE_REF
13902 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
13903 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
13905 if (complain & tf_error)
13907 if (TYPE_P (TREE_OPERAND (member, 0)))
13908 error ("%qT is not a class or namespace",
13909 TREE_OPERAND (member, 0));
13911 error ("%qD is not a class or namespace",
13912 TREE_OPERAND (member, 0));
13914 return error_mark_node;
13916 else if (TREE_CODE (member) == FIELD_DECL)
13917 return finish_non_static_data_member (member, object, NULL_TREE);
13919 return finish_class_member_access_expr (object, member,
13920 /*template_p=*/false,
13926 (RECUR (TREE_OPERAND (t, 0)));
13930 VEC(constructor_elt,gc) *n;
13931 constructor_elt *ce;
13932 unsigned HOST_WIDE_INT idx;
13933 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13934 bool process_index_p;
13936 bool need_copy_p = false;
13939 if (type == error_mark_node)
13940 return error_mark_node;
13942 /* digest_init will do the wrong thing if we let it. */
13943 if (type && TYPE_PTRMEMFUNC_P (type))
13946 /* We do not want to process the index of aggregate
13947 initializers as they are identifier nodes which will be
13948 looked up by digest_init. */
13949 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
13951 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
13952 newlen = VEC_length (constructor_elt, n);
13953 FOR_EACH_VEC_ELT (constructor_elt, n, idx, ce)
13955 if (ce->index && process_index_p)
13956 ce->index = RECUR (ce->index);
13958 if (PACK_EXPANSION_P (ce->value))
13960 /* Substitute into the pack expansion. */
13961 ce->value = tsubst_pack_expansion (ce->value, args, complain,
13964 if (ce->value == error_mark_node
13965 || PACK_EXPANSION_P (ce->value))
13967 else if (TREE_VEC_LENGTH (ce->value) == 1)
13968 /* Just move the argument into place. */
13969 ce->value = TREE_VEC_ELT (ce->value, 0);
13972 /* Update the length of the final CONSTRUCTOR
13973 arguments vector, and note that we will need to
13975 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
13976 need_copy_p = true;
13980 ce->value = RECUR (ce->value);
13985 VEC(constructor_elt,gc) *old_n = n;
13987 n = VEC_alloc (constructor_elt, gc, newlen);
13988 FOR_EACH_VEC_ELT (constructor_elt, old_n, idx, ce)
13990 if (TREE_CODE (ce->value) == TREE_VEC)
13992 int i, len = TREE_VEC_LENGTH (ce->value);
13993 for (i = 0; i < len; ++i)
13994 CONSTRUCTOR_APPEND_ELT (n, 0,
13995 TREE_VEC_ELT (ce->value, i));
13998 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
14002 r = build_constructor (init_list_type_node, n);
14003 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
14005 if (TREE_HAS_CONSTRUCTOR (t))
14006 return finish_compound_literal (type, r, complain);
14008 TREE_TYPE (r) = type;
14014 tree operand_0 = TREE_OPERAND (t, 0);
14015 if (TYPE_P (operand_0))
14017 operand_0 = tsubst (operand_0, args, complain, in_decl);
14018 return get_typeid (operand_0);
14022 operand_0 = RECUR (operand_0);
14023 return build_typeid (operand_0);
14034 tree r = tsubst_copy (t, args, complain, in_decl);
14036 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
14037 /* If the original type was a reference, we'll be wrapped in
14038 the appropriate INDIRECT_REF. */
14039 r = convert_from_reference (r);
14044 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
14045 tsubst (TREE_TYPE (t), args, complain, in_decl));
14047 case OFFSETOF_EXPR:
14048 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
14052 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
14053 complain, in_decl);
14055 tree type2 = TRAIT_EXPR_TYPE2 (t);
14057 type2 = tsubst_copy (type2, args, complain, in_decl);
14059 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
14064 tree old_stmt_expr = cur_stmt_expr;
14065 tree stmt_expr = begin_stmt_expr ();
14067 cur_stmt_expr = stmt_expr;
14068 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
14069 integral_constant_expression_p);
14070 stmt_expr = finish_stmt_expr (stmt_expr, false);
14071 cur_stmt_expr = old_stmt_expr;
14073 /* If the resulting list of expression statement is empty,
14074 fold it further into void_zero_node. */
14075 if (empty_expr_stmt_p (stmt_expr))
14076 stmt_expr = void_zero_node;
14082 t = tsubst_copy (t, args, complain, in_decl);
14083 /* As in finish_id_expression, we resolve enumeration constants
14084 to their underlying values. */
14085 if (TREE_CODE (t) == CONST_DECL && !processing_template_decl)
14087 used_types_insert (TREE_TYPE (t));
14088 return DECL_INITIAL (t);
14094 tree r = build_lambda_expr ();
14096 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
14097 LAMBDA_EXPR_CLOSURE (r) = type;
14098 CLASSTYPE_LAMBDA_EXPR (type) = r;
14100 LAMBDA_EXPR_LOCATION (r)
14101 = LAMBDA_EXPR_LOCATION (t);
14102 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
14103 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
14104 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
14105 LAMBDA_EXPR_DISCRIMINATOR (r)
14106 = (LAMBDA_EXPR_DISCRIMINATOR (t));
14107 LAMBDA_EXPR_EXTRA_SCOPE (r)
14108 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
14109 if (LAMBDA_EXPR_RETURN_TYPE (t) == dependent_lambda_return_type_node)
14111 LAMBDA_EXPR_RETURN_TYPE (r) = dependent_lambda_return_type_node;
14112 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (r) = true;
14115 LAMBDA_EXPR_RETURN_TYPE (r)
14116 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
14118 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
14119 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
14121 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14122 determine_visibility (TYPE_NAME (type));
14123 /* Now that we know visibility, instantiate the type so we have a
14124 declaration of the op() for later calls to lambda_function. */
14125 complete_type (type);
14127 /* The capture list refers to closure members, so this needs to
14128 wait until after we finish instantiating the type. Also keep
14129 any captures that may have been added during instantiation. */
14130 LAMBDA_EXPR_CAPTURE_LIST (r)
14131 = chainon (RECUR (LAMBDA_EXPR_CAPTURE_LIST (t)),
14132 LAMBDA_EXPR_CAPTURE_LIST (r));
14133 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
14135 return build_lambda_object (r);
14139 /* We can get here for a constant initializer of non-dependent type.
14140 FIXME stop folding in cp_parser_initializer_clause. */
14142 tree r = get_target_expr (RECUR (TARGET_EXPR_INITIAL (t)));
14146 case TRANSACTION_EXPR:
14147 return tsubst_expr(t, args, complain, in_decl,
14148 integral_constant_expression_p);
14151 /* Handle Objective-C++ constructs, if appropriate. */
14154 = objcp_tsubst_copy_and_build (t, args, complain,
14155 in_decl, /*function_p=*/false);
14159 return tsubst_copy (t, args, complain, in_decl);
14165 /* Verify that the instantiated ARGS are valid. For type arguments,
14166 make sure that the type's linkage is ok. For non-type arguments,
14167 make sure they are constants if they are integral or enumerations.
14168 Emit an error under control of COMPLAIN, and return TRUE on error. */
14171 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
14173 if (ARGUMENT_PACK_P (t))
14175 tree vec = ARGUMENT_PACK_ARGS (t);
14176 int len = TREE_VEC_LENGTH (vec);
14177 bool result = false;
14180 for (i = 0; i < len; ++i)
14181 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
14185 else if (TYPE_P (t))
14187 /* [basic.link]: A name with no linkage (notably, the name
14188 of a class or enumeration declared in a local scope)
14189 shall not be used to declare an entity with linkage.
14190 This implies that names with no linkage cannot be used as
14193 DR 757 relaxes this restriction for C++0x. */
14194 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
14195 : no_linkage_check (t, /*relaxed_p=*/false));
14199 /* DR 488 makes use of a type with no linkage cause
14200 type deduction to fail. */
14201 if (complain & tf_error)
14203 if (TYPE_ANONYMOUS_P (nt))
14204 error ("%qT is/uses anonymous type", t);
14206 error ("template argument for %qD uses local type %qT",
14211 /* In order to avoid all sorts of complications, we do not
14212 allow variably-modified types as template arguments. */
14213 else if (variably_modified_type_p (t, NULL_TREE))
14215 if (complain & tf_error)
14216 error ("%qT is a variably modified type", t);
14220 /* A non-type argument of integral or enumerated type must be a
14222 else if (TREE_TYPE (t)
14223 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
14224 && !TREE_CONSTANT (t))
14226 if (complain & tf_error)
14227 error ("integral expression %qE is not constant", t);
14234 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
14236 int ix, len = DECL_NTPARMS (tmpl);
14237 bool result = false;
14239 for (ix = 0; ix != len; ix++)
14241 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
14244 if (result && (complain & tf_error))
14245 error (" trying to instantiate %qD", tmpl);
14249 /* In C++0x, it's possible to have a function template whose type depends
14250 on itself recursively. This is most obvious with decltype, but can also
14251 occur with enumeration scope (c++/48969). So we need to catch infinite
14252 recursion and reject the substitution at deduction time; this function
14253 will return error_mark_node for any repeated substitution.
14255 This also catches excessive recursion such as when f<N> depends on
14256 f<N-1> across all integers, and returns error_mark_node for all the
14257 substitutions back up to the initial one.
14259 This is, of course, not reentrant. */
14262 deduction_tsubst_fntype (tree fn, tree targs, tsubst_flags_t complain)
14264 static bool excessive_deduction_depth;
14265 static int deduction_depth;
14266 struct pending_template *old_last_pend = last_pending_template;
14267 struct tinst_level *old_error_tinst = last_error_tinst_level;
14269 tree fntype = TREE_TYPE (fn);
14273 if (excessive_deduction_depth)
14274 return error_mark_node;
14276 tinst = build_tree_list (fn, targs);
14277 if (!push_tinst_level (tinst))
14279 excessive_deduction_depth = true;
14281 return error_mark_node;
14284 input_location = DECL_SOURCE_LOCATION (fn);
14286 push_deduction_access_scope (fn);
14287 r = tsubst (fntype, targs, complain, NULL_TREE);
14288 pop_deduction_access_scope (fn);
14291 if (excessive_deduction_depth)
14293 r = error_mark_node;
14294 if (deduction_depth == 0)
14295 /* Reset once we're all the way out. */
14296 excessive_deduction_depth = false;
14299 pop_tinst_level ();
14300 /* We can't free this if a pending_template entry or last_error_tinst_level
14301 is pointing at it. */
14302 if (last_pending_template == old_last_pend
14303 && last_error_tinst_level == old_error_tinst)
14308 /* Instantiate the indicated variable or function template TMPL with
14309 the template arguments in TARG_PTR. */
14312 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
14314 tree targ_ptr = orig_args;
14319 if (tmpl == error_mark_node)
14320 return error_mark_node;
14322 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
14324 /* If this function is a clone, handle it specially. */
14325 if (DECL_CLONED_FUNCTION_P (tmpl))
14330 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14331 DECL_CLONED_FUNCTION. */
14332 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
14333 targ_ptr, complain);
14334 if (spec == error_mark_node)
14335 return error_mark_node;
14337 /* Look for the clone. */
14338 FOR_EACH_CLONE (clone, spec)
14339 if (DECL_NAME (clone) == DECL_NAME (tmpl))
14341 /* We should always have found the clone by now. */
14342 gcc_unreachable ();
14346 /* Check to see if we already have this specialization. */
14347 gen_tmpl = most_general_template (tmpl);
14348 if (tmpl != gen_tmpl)
14349 /* The TMPL is a partial instantiation. To get a full set of
14350 arguments we must add the arguments used to perform the
14351 partial instantiation. */
14352 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
14355 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14356 but it doesn't seem to be on the hot path. */
14357 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
14359 gcc_assert (tmpl == gen_tmpl
14360 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
14362 || fndecl == NULL_TREE);
14364 if (spec != NULL_TREE)
14367 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
14369 return error_mark_node;
14371 /* We are building a FUNCTION_DECL, during which the access of its
14372 parameters and return types have to be checked. However this
14373 FUNCTION_DECL which is the desired context for access checking
14374 is not built yet. We solve this chicken-and-egg problem by
14375 deferring all checks until we have the FUNCTION_DECL. */
14376 push_deferring_access_checks (dk_deferred);
14378 /* Instantiation of the function happens in the context of the function
14379 template, not the context of the overload resolution we're doing. */
14380 push_to_top_level ();
14381 if (DECL_CLASS_SCOPE_P (gen_tmpl))
14383 tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
14384 complain, gen_tmpl);
14385 push_nested_class (ctx);
14387 /* Substitute template parameters to obtain the specialization. */
14388 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
14389 targ_ptr, complain, gen_tmpl);
14390 if (DECL_CLASS_SCOPE_P (gen_tmpl))
14391 pop_nested_class ();
14392 pop_from_top_level ();
14394 if (fndecl == error_mark_node)
14395 return error_mark_node;
14397 /* Now we know the specialization, compute access previously
14399 push_access_scope (fndecl);
14401 /* Some typedefs referenced from within the template code need to be access
14402 checked at template instantiation time, i.e now. These types were
14403 added to the template at parsing time. Let's get those and perfom
14404 the acces checks then. */
14405 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
14406 perform_deferred_access_checks ();
14407 pop_access_scope (fndecl);
14408 pop_deferring_access_checks ();
14410 /* The DECL_TI_TEMPLATE should always be the immediate parent
14411 template, not the most general template. */
14412 DECL_TI_TEMPLATE (fndecl) = tmpl;
14414 /* If we've just instantiated the main entry point for a function,
14415 instantiate all the alternate entry points as well. We do this
14416 by cloning the instantiation of the main entry point, not by
14417 instantiating the template clones. */
14418 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
14419 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
14424 /* Wrapper for instantiate_template_1. */
14427 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
14430 timevar_push (TV_TEMPLATE_INST);
14431 ret = instantiate_template_1 (tmpl, orig_args, complain);
14432 timevar_pop (TV_TEMPLATE_INST);
14436 /* We're going to do deduction substitution on the type of TMPL, a function
14437 template. In C++11 mode, push into that access scope. In C++03 mode,
14438 disable access checking. */
14441 push_deduction_access_scope (tree tmpl)
14443 if (cxx_dialect >= cxx0x)
14445 int ptd = processing_template_decl;
14446 push_access_scope (DECL_TEMPLATE_RESULT (tmpl));
14447 /* Preserve processing_template_decl across push_to_top_level. */
14448 if (ptd && !processing_template_decl)
14449 ++processing_template_decl;
14452 push_deferring_access_checks (dk_no_check);
14455 /* And pop back out. */
14458 pop_deduction_access_scope (tree tmpl)
14460 if (cxx_dialect >= cxx0x)
14461 pop_access_scope (DECL_TEMPLATE_RESULT (tmpl));
14463 pop_deferring_access_checks ();
14466 /* PARM is a template parameter pack for FN. Returns true iff
14467 PARM is used in a deducible way in the argument list of FN. */
14470 pack_deducible_p (tree parm, tree fn)
14472 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
14473 for (; t; t = TREE_CHAIN (t))
14475 tree type = TREE_VALUE (t);
14477 if (!PACK_EXPANSION_P (type))
14479 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
14480 packs; packs = TREE_CHAIN (packs))
14481 if (TREE_VALUE (packs) == parm)
14483 /* The template parameter pack is used in a function parameter
14484 pack. If this is the end of the parameter list, the
14485 template parameter pack is deducible. */
14486 if (TREE_CHAIN (t) == void_list_node)
14489 /* Otherwise, not. Well, it could be deduced from
14490 a non-pack parameter, but doing so would end up with
14491 a deduction mismatch, so don't bother. */
14495 /* The template parameter pack isn't used in any function parameter
14496 packs, but it might be used deeper, e.g. tuple<Args...>. */
14500 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14501 NARGS elements of the arguments that are being used when calling
14502 it. TARGS is a vector into which the deduced template arguments
14505 Return zero for success, 2 for an incomplete match that doesn't resolve
14506 all the types, and 1 for complete failure. An error message will be
14507 printed only for an incomplete match.
14509 If FN is a conversion operator, or we are trying to produce a specific
14510 specialization, RETURN_TYPE is the return type desired.
14512 The EXPLICIT_TARGS are explicit template arguments provided via a
14515 The parameter STRICT is one of:
14518 We are deducing arguments for a function call, as in
14519 [temp.deduct.call].
14522 We are deducing arguments for a conversion function, as in
14523 [temp.deduct.conv].
14526 We are deducing arguments when doing an explicit instantiation
14527 as in [temp.explicit], when determining an explicit specialization
14528 as in [temp.expl.spec], or when taking the address of a function
14529 template, as in [temp.deduct.funcaddr]. */
14532 fn_type_unification (tree fn,
14533 tree explicit_targs,
14536 unsigned int nargs,
14538 unification_kind_t strict,
14546 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
14548 fntype = TREE_TYPE (fn);
14549 if (explicit_targs)
14553 The specified template arguments must match the template
14554 parameters in kind (i.e., type, nontype, template), and there
14555 must not be more arguments than there are parameters;
14556 otherwise type deduction fails.
14558 Nontype arguments must match the types of the corresponding
14559 nontype template parameters, or must be convertible to the
14560 types of the corresponding nontype parameters as specified in
14561 _temp.arg.nontype_, otherwise type deduction fails.
14563 All references in the function type of the function template
14564 to the corresponding template parameters are replaced by the
14565 specified template argument values. If a substitution in a
14566 template parameter or in the function type of the function
14567 template results in an invalid type, type deduction fails. */
14568 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
14569 int i, len = TREE_VEC_LENGTH (tparms);
14570 tree converted_args;
14571 bool incomplete = false;
14573 if (explicit_targs == error_mark_node)
14574 return unify_invalid (explain_p);
14577 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
14579 ? tf_warning_or_error
14581 /*require_all_args=*/false,
14582 /*use_default_args=*/false));
14583 if (converted_args == error_mark_node)
14586 /* Substitute the explicit args into the function type. This is
14587 necessary so that, for instance, explicitly declared function
14588 arguments can match null pointed constants. If we were given
14589 an incomplete set of explicit args, we must not do semantic
14590 processing during substitution as we could create partial
14592 for (i = 0; i < len; i++)
14594 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
14595 bool parameter_pack = false;
14596 tree targ = TREE_VEC_ELT (converted_args, i);
14598 /* Dig out the actual parm. */
14599 if (TREE_CODE (parm) == TYPE_DECL
14600 || TREE_CODE (parm) == TEMPLATE_DECL)
14602 parm = TREE_TYPE (parm);
14603 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
14605 else if (TREE_CODE (parm) == PARM_DECL)
14607 parm = DECL_INITIAL (parm);
14608 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
14611 if (!parameter_pack && targ == NULL_TREE)
14612 /* No explicit argument for this template parameter. */
14615 if (parameter_pack && pack_deducible_p (parm, fn))
14617 /* Mark the argument pack as "incomplete". We could
14618 still deduce more arguments during unification.
14619 We remove this mark in type_unification_real. */
14622 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
14623 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
14624 = ARGUMENT_PACK_ARGS (targ);
14627 /* We have some incomplete argument packs. */
14632 processing_template_decl += incomplete;
14633 fntype = deduction_tsubst_fntype (fn, converted_args,
14635 ? tf_warning_or_error
14637 processing_template_decl -= incomplete;
14639 if (fntype == error_mark_node)
14642 /* Place the explicitly specified arguments in TARGS. */
14643 for (i = NUM_TMPL_ARGS (converted_args); i--;)
14644 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
14647 /* Never do unification on the 'this' parameter. */
14648 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
14654 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
14655 new_args = XALLOCAVEC (tree, nargs + 1);
14656 new_args[0] = return_type;
14657 memcpy (new_args + 1, args, nargs * sizeof (tree));
14662 /* We allow incomplete unification without an error message here
14663 because the standard doesn't seem to explicitly prohibit it. Our
14664 callers must be ready to deal with unification failures in any
14666 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
14667 targs, parms, args, nargs, /*subr=*/0,
14668 strict, flags, explain_p);
14670 /* Now that we have bindings for all of the template arguments,
14671 ensure that the arguments deduced for the template template
14672 parameters have compatible template parameter lists. We cannot
14673 check this property before we have deduced all template
14674 arguments, because the template parameter types of a template
14675 template parameter might depend on prior template parameters
14676 deduced after the template template parameter. The following
14677 ill-formed example illustrates this issue:
14679 template<typename T, template<T> class C> void f(C<5>, T);
14681 template<int N> struct X {};
14684 f(X<5>(), 5l); // error: template argument deduction fails
14687 The template parameter list of 'C' depends on the template type
14688 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
14689 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
14690 time that we deduce 'C'. */
14692 && !template_template_parm_bindings_ok_p
14693 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
14694 return unify_inconsistent_template_template_parameters (explain_p);
14697 /* All is well so far. Now, check:
14701 When all template arguments have been deduced, all uses of
14702 template parameters in nondeduced contexts are replaced with
14703 the corresponding deduced argument values. If the
14704 substitution results in an invalid type, as described above,
14705 type deduction fails. */
14707 tree substed = deduction_tsubst_fntype (fn, targs,
14709 ? tf_warning_or_error
14711 if (substed == error_mark_node)
14714 /* If we're looking for an exact match, check that what we got
14715 is indeed an exact match. It might not be if some template
14716 parameters are used in non-deduced contexts. But don't check
14717 for an exact match if we have dependent template arguments;
14718 in that case we're doing partial ordering, and we already know
14719 that we have two candidates that will provide the actual type. */
14720 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
14725 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
14727 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
14728 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
14729 if (!same_type_p (args[i], TREE_VALUE (sarg)))
14730 return unify_type_mismatch (explain_p, args[i],
14731 TREE_VALUE (sarg));
14738 /* Adjust types before performing type deduction, as described in
14739 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
14740 sections are symmetric. PARM is the type of a function parameter
14741 or the return type of the conversion function. ARG is the type of
14742 the argument passed to the call, or the type of the value
14743 initialized with the result of the conversion function.
14744 ARG_EXPR is the original argument expression, which may be null. */
14747 maybe_adjust_types_for_deduction (unification_kind_t strict,
14761 /* Swap PARM and ARG throughout the remainder of this
14762 function; the handling is precisely symmetric since PARM
14763 will initialize ARG rather than vice versa. */
14771 /* Core issue #873: Do the DR606 thing (see below) for these cases,
14772 too, but here handle it by stripping the reference from PARM
14773 rather than by adding it to ARG. */
14774 if (TREE_CODE (*parm) == REFERENCE_TYPE
14775 && TYPE_REF_IS_RVALUE (*parm)
14776 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
14777 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
14778 && TREE_CODE (*arg) == REFERENCE_TYPE
14779 && !TYPE_REF_IS_RVALUE (*arg))
14780 *parm = TREE_TYPE (*parm);
14781 /* Nothing else to do in this case. */
14785 gcc_unreachable ();
14788 if (TREE_CODE (*parm) != REFERENCE_TYPE)
14790 /* [temp.deduct.call]
14792 If P is not a reference type:
14794 --If A is an array type, the pointer type produced by the
14795 array-to-pointer standard conversion (_conv.array_) is
14796 used in place of A for type deduction; otherwise,
14798 --If A is a function type, the pointer type produced by
14799 the function-to-pointer standard conversion
14800 (_conv.func_) is used in place of A for type deduction;
14803 --If A is a cv-qualified type, the top level
14804 cv-qualifiers of A's type are ignored for type
14806 if (TREE_CODE (*arg) == ARRAY_TYPE)
14807 *arg = build_pointer_type (TREE_TYPE (*arg));
14808 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
14809 *arg = build_pointer_type (*arg);
14811 *arg = TYPE_MAIN_VARIANT (*arg);
14814 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
14815 of the form T&&, where T is a template parameter, and the argument
14816 is an lvalue, T is deduced as A& */
14817 if (TREE_CODE (*parm) == REFERENCE_TYPE
14818 && TYPE_REF_IS_RVALUE (*parm)
14819 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
14820 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
14821 && (arg_expr ? real_lvalue_p (arg_expr)
14822 /* try_one_overload doesn't provide an arg_expr, but
14823 functions are always lvalues. */
14824 : TREE_CODE (*arg) == FUNCTION_TYPE))
14825 *arg = build_reference_type (*arg);
14827 /* [temp.deduct.call]
14829 If P is a cv-qualified type, the top level cv-qualifiers
14830 of P's type are ignored for type deduction. If P is a
14831 reference type, the type referred to by P is used for
14833 *parm = TYPE_MAIN_VARIANT (*parm);
14834 if (TREE_CODE (*parm) == REFERENCE_TYPE)
14836 *parm = TREE_TYPE (*parm);
14837 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
14840 /* DR 322. For conversion deduction, remove a reference type on parm
14841 too (which has been swapped into ARG). */
14842 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
14843 *arg = TREE_TYPE (*arg);
14848 /* Subroutine of unify_one_argument. PARM is a function parameter of a
14849 template which does contain any deducible template parameters; check if
14850 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
14851 unify_one_argument. */
14854 check_non_deducible_conversion (tree parm, tree arg, int strict,
14855 int flags, bool explain_p)
14860 type = TREE_TYPE (arg);
14864 if (same_type_p (parm, type))
14865 return unify_success (explain_p);
14867 if (strict == DEDUCE_CONV)
14869 if (can_convert_arg (type, parm, NULL_TREE, flags))
14870 return unify_success (explain_p);
14872 else if (strict != DEDUCE_EXACT)
14874 if (can_convert_arg (parm, type,
14875 TYPE_P (arg) ? NULL_TREE : arg,
14877 return unify_success (explain_p);
14880 if (strict == DEDUCE_EXACT)
14881 return unify_type_mismatch (explain_p, parm, arg);
14883 return unify_arg_conversion (explain_p, parm, type, arg);
14886 /* Subroutine of type_unification_real and unify_pack_expansion to
14887 handle unification of a single P/A pair. Parameters are as
14888 for those functions. */
14891 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
14892 int subr, unification_kind_t strict, int flags,
14895 tree arg_expr = NULL_TREE;
14898 if (arg == error_mark_node || parm == error_mark_node)
14899 return unify_invalid (explain_p);
14900 if (arg == unknown_type_node)
14901 /* We can't deduce anything from this, but we might get all the
14902 template args from other function args. */
14903 return unify_success (explain_p);
14905 /* FIXME uses_deducible_template_parms */
14906 if (TYPE_P (parm) && !uses_template_parms (parm))
14907 return check_non_deducible_conversion (parm, arg, strict, flags,
14913 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
14914 | UNIFY_ALLOW_MORE_CV_QUAL
14915 | UNIFY_ALLOW_DERIVED);
14919 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
14923 arg_strict = UNIFY_ALLOW_NONE;
14927 gcc_unreachable ();
14930 /* We only do these transformations if this is the top-level
14931 parameter_type_list in a call or declaration matching; in other
14932 situations (nested function declarators, template argument lists) we
14933 won't be comparing a type to an expression, and we don't do any type
14939 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
14940 if (type_unknown_p (arg))
14942 /* [temp.deduct.type] A template-argument can be
14943 deduced from a pointer to function or pointer
14944 to member function argument if the set of
14945 overloaded functions does not contain function
14946 templates and at most one of a set of
14947 overloaded functions provides a unique
14950 if (resolve_overloaded_unification
14951 (tparms, targs, parm, arg, strict,
14952 arg_strict, explain_p))
14953 return unify_success (explain_p);
14954 return unify_overload_resolution_failure (explain_p, arg);
14958 arg = unlowered_expr_type (arg);
14959 if (arg == error_mark_node)
14960 return unify_invalid (explain_p);
14964 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
14967 gcc_assert ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
14968 == (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL));
14970 /* For deduction from an init-list we need the actual list. */
14971 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
14973 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
14976 /* Most parms like fn_type_unification.
14978 If SUBR is 1, we're being called recursively (to unify the
14979 arguments of a function or method parameter of a function
14983 type_unification_real (tree tparms,
14987 unsigned int xnargs,
14989 unification_kind_t strict,
14995 int ntparms = TREE_VEC_LENGTH (tparms);
14996 int saw_undeduced = 0;
14999 unsigned int nargs;
15002 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
15003 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
15004 gcc_assert (ntparms > 0);
15006 /* Reset the number of non-defaulted template arguments contained
15008 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
15016 while (parms && parms != void_list_node
15019 parm = TREE_VALUE (parms);
15021 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
15022 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
15023 /* For a function parameter pack that occurs at the end of the
15024 parameter-declaration-list, the type A of each remaining
15025 argument of the call is compared with the type P of the
15026 declarator-id of the function parameter pack. */
15029 parms = TREE_CHAIN (parms);
15031 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
15032 /* For a function parameter pack that does not occur at the
15033 end of the parameter-declaration-list, the type of the
15034 parameter pack is a non-deduced context. */
15040 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
15046 && parms != void_list_node
15047 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
15049 /* Unify the remaining arguments with the pack expansion type. */
15051 tree parmvec = make_tree_vec (1);
15053 /* Allocate a TREE_VEC and copy in all of the arguments */
15054 argvec = make_tree_vec (nargs - ia);
15055 for (i = 0; ia < nargs; ++ia, ++i)
15056 TREE_VEC_ELT (argvec, i) = args[ia];
15058 /* Copy the parameter into parmvec. */
15059 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
15060 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
15061 /*subr=*/subr, explain_p))
15064 /* Advance to the end of the list of parameters. */
15065 parms = TREE_CHAIN (parms);
15068 /* Fail if we've reached the end of the parm list, and more args
15069 are present, and the parm list isn't variadic. */
15070 if (ia < nargs && parms == void_list_node)
15071 return unify_too_many_arguments (explain_p, nargs, ia);
15072 /* Fail if parms are left and they don't have default values. */
15073 if (parms && parms != void_list_node
15074 && TREE_PURPOSE (parms) == NULL_TREE)
15076 unsigned int count = nargs;
15078 while (p && p != void_list_node)
15081 p = TREE_CHAIN (p);
15083 return unify_too_few_arguments (explain_p, ia, count);
15088 tsubst_flags_t complain = (explain_p
15089 ? tf_warning_or_error
15092 for (i = 0; i < ntparms; i++)
15094 tree targ = TREE_VEC_ELT (targs, i);
15095 tree tparm = TREE_VEC_ELT (tparms, i);
15097 /* Clear the "incomplete" flags on all argument packs now so that
15098 substituting them into later default arguments works. */
15099 if (targ && ARGUMENT_PACK_P (targ))
15101 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
15102 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
15105 if (targ || tparm == error_mark_node)
15107 tparm = TREE_VALUE (tparm);
15109 /* If this is an undeduced nontype parameter that depends on
15110 a type parameter, try another pass; its type may have been
15111 deduced from a later argument than the one from which
15112 this parameter can be deduced. */
15113 if (TREE_CODE (tparm) == PARM_DECL
15114 && uses_template_parms (TREE_TYPE (tparm))
15115 && !saw_undeduced++)
15118 /* Core issue #226 (C++0x) [temp.deduct]:
15120 If a template argument has not been deduced, its
15121 default template argument, if any, is used.
15123 When we are in C++98 mode, TREE_PURPOSE will either
15124 be NULL_TREE or ERROR_MARK_NODE, so we do not need
15125 to explicitly check cxx_dialect here. */
15126 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
15128 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
15129 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
15130 location_t save_loc = input_location;
15132 input_location = DECL_SOURCE_LOCATION (parm);
15133 arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
15134 arg = convert_template_argument (parm, arg, targs, complain,
15136 input_location = save_loc;
15137 if (arg == error_mark_node)
15141 TREE_VEC_ELT (targs, i) = arg;
15142 /* The position of the first default template argument,
15143 is also the number of non-defaulted arguments in TARGS.
15145 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
15146 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
15151 /* If the type parameter is a parameter pack, then it will
15152 be deduced to an empty parameter pack. */
15153 if (template_parameter_pack_p (tparm))
15157 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
15159 arg = make_node (NONTYPE_ARGUMENT_PACK);
15160 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
15161 TREE_CONSTANT (arg) = 1;
15164 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
15166 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
15168 TREE_VEC_ELT (targs, i) = arg;
15172 return unify_parameter_deduction_failure (explain_p, tparm);
15175 #ifdef ENABLE_CHECKING
15176 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
15177 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
15180 return unify_success (explain_p);
15183 /* Subroutine of type_unification_real. Args are like the variables
15184 at the call site. ARG is an overloaded function (or template-id);
15185 we try deducing template args from each of the overloads, and if
15186 only one succeeds, we go with that. Modifies TARGS and returns
15187 true on success. */
15190 resolve_overloaded_unification (tree tparms,
15194 unification_kind_t strict,
15198 tree tempargs = copy_node (targs);
15200 tree goodfn = NULL_TREE;
15203 if (TREE_CODE (arg) == ADDR_EXPR)
15205 arg = TREE_OPERAND (arg, 0);
15211 if (TREE_CODE (arg) == COMPONENT_REF)
15212 /* Handle `&x' where `x' is some static or non-static member
15214 arg = TREE_OPERAND (arg, 1);
15216 if (TREE_CODE (arg) == OFFSET_REF)
15217 arg = TREE_OPERAND (arg, 1);
15219 /* Strip baselink information. */
15220 if (BASELINK_P (arg))
15221 arg = BASELINK_FUNCTIONS (arg);
15223 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
15225 /* If we got some explicit template args, we need to plug them into
15226 the affected templates before we try to unify, in case the
15227 explicit args will completely resolve the templates in question. */
15230 tree expl_subargs = TREE_OPERAND (arg, 1);
15231 arg = TREE_OPERAND (arg, 0);
15233 for (; arg; arg = OVL_NEXT (arg))
15235 tree fn = OVL_CURRENT (arg);
15236 tree subargs, elem;
15238 if (TREE_CODE (fn) != TEMPLATE_DECL)
15241 ++processing_template_decl;
15242 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
15243 expl_subargs, /*check_ret=*/false);
15244 if (subargs && !any_dependent_template_arguments_p (subargs))
15246 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
15247 if (try_one_overload (tparms, targs, tempargs, parm,
15248 elem, strict, sub_strict, addr_p, explain_p)
15249 && (!goodfn || !same_type_p (goodfn, elem)))
15257 --processing_template_decl;
15259 /* If no templates (or more than one) are fully resolved by the
15260 explicit arguments, this template-id is a non-deduced context; it
15261 could still be OK if we deduce all template arguments for the
15262 enclosing call through other arguments. */
15266 else if (TREE_CODE (arg) != OVERLOAD
15267 && TREE_CODE (arg) != FUNCTION_DECL)
15268 /* If ARG is, for example, "(0, &f)" then its type will be unknown
15269 -- but the deduction does not succeed because the expression is
15270 not just the function on its own. */
15273 for (; arg; arg = OVL_NEXT (arg))
15274 if (try_one_overload (tparms, targs, tempargs, parm,
15275 TREE_TYPE (OVL_CURRENT (arg)),
15276 strict, sub_strict, addr_p, explain_p)
15277 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
15279 goodfn = OVL_CURRENT (arg);
15283 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15284 to function or pointer to member function argument if the set of
15285 overloaded functions does not contain function templates and at most
15286 one of a set of overloaded functions provides a unique match.
15288 So if we found multiple possibilities, we return success but don't
15289 deduce anything. */
15293 int i = TREE_VEC_LENGTH (targs);
15295 if (TREE_VEC_ELT (tempargs, i))
15296 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
15304 /* Core DR 115: In contexts where deduction is done and fails, or in
15305 contexts where deduction is not done, if a template argument list is
15306 specified and it, along with any default template arguments, identifies
15307 a single function template specialization, then the template-id is an
15308 lvalue for the function template specialization. */
15311 resolve_nondeduced_context (tree orig_expr)
15313 tree expr, offset, baselink;
15316 if (!type_unknown_p (orig_expr))
15321 offset = NULL_TREE;
15322 baselink = NULL_TREE;
15324 if (TREE_CODE (expr) == ADDR_EXPR)
15326 expr = TREE_OPERAND (expr, 0);
15329 if (TREE_CODE (expr) == OFFSET_REF)
15332 expr = TREE_OPERAND (expr, 1);
15334 if (BASELINK_P (expr))
15337 expr = BASELINK_FUNCTIONS (expr);
15340 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
15343 tree goodfn = NULL_TREE;
15345 /* If we got some explicit template args, we need to plug them into
15346 the affected templates before we try to unify, in case the
15347 explicit args will completely resolve the templates in question. */
15349 tree expl_subargs = TREE_OPERAND (expr, 1);
15350 tree arg = TREE_OPERAND (expr, 0);
15351 tree badfn = NULL_TREE;
15352 tree badargs = NULL_TREE;
15354 for (; arg; arg = OVL_NEXT (arg))
15356 tree fn = OVL_CURRENT (arg);
15357 tree subargs, elem;
15359 if (TREE_CODE (fn) != TEMPLATE_DECL)
15362 ++processing_template_decl;
15363 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
15364 expl_subargs, /*check_ret=*/false);
15365 if (subargs && !any_dependent_template_arguments_p (subargs))
15367 elem = instantiate_template (fn, subargs, tf_none);
15368 if (elem == error_mark_node)
15373 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
15379 --processing_template_decl;
15383 mark_used (goodfn);
15386 expr = build_baselink (BASELINK_BINFO (baselink),
15387 BASELINK_ACCESS_BINFO (baselink),
15388 expr, BASELINK_OPTYPE (baselink));
15392 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
15393 expr = build_offset_ref (base, expr, addr);
15396 expr = cp_build_addr_expr (expr, tf_warning_or_error);
15399 else if (good == 0 && badargs)
15400 /* There were no good options and at least one bad one, so let the
15401 user know what the problem is. */
15402 instantiate_template (badfn, badargs, tf_warning_or_error);
15407 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15408 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15409 different overloads deduce different arguments for a given parm.
15410 ADDR_P is true if the expression for which deduction is being
15411 performed was of the form "& fn" rather than simply "fn".
15413 Returns 1 on success. */
15416 try_one_overload (tree tparms,
15421 unification_kind_t strict,
15430 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15431 to function or pointer to member function argument if the set of
15432 overloaded functions does not contain function templates and at most
15433 one of a set of overloaded functions provides a unique match.
15435 So if this is a template, just return success. */
15437 if (uses_template_parms (arg))
15440 if (TREE_CODE (arg) == METHOD_TYPE)
15441 arg = build_ptrmemfunc_type (build_pointer_type (arg));
15443 arg = build_pointer_type (arg);
15445 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
15447 /* We don't copy orig_targs for this because if we have already deduced
15448 some template args from previous args, unify would complain when we
15449 try to deduce a template parameter for the same argument, even though
15450 there isn't really a conflict. */
15451 nargs = TREE_VEC_LENGTH (targs);
15452 tempargs = make_tree_vec (nargs);
15454 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
15457 /* First make sure we didn't deduce anything that conflicts with
15458 explicitly specified args. */
15459 for (i = nargs; i--; )
15461 tree elt = TREE_VEC_ELT (tempargs, i);
15462 tree oldelt = TREE_VEC_ELT (orig_targs, i);
15466 else if (uses_template_parms (elt))
15467 /* Since we're unifying against ourselves, we will fill in
15468 template args used in the function parm list with our own
15469 template parms. Discard them. */
15470 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
15471 else if (oldelt && !template_args_equal (oldelt, elt))
15475 for (i = nargs; i--; )
15477 tree elt = TREE_VEC_ELT (tempargs, i);
15480 TREE_VEC_ELT (targs, i) = elt;
15486 /* PARM is a template class (perhaps with unbound template
15487 parameters). ARG is a fully instantiated type. If ARG can be
15488 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15489 TARGS are as for unify. */
15492 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
15495 tree copy_of_targs;
15497 if (!CLASSTYPE_TEMPLATE_INFO (arg)
15498 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
15499 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
15502 /* We need to make a new template argument vector for the call to
15503 unify. If we used TARGS, we'd clutter it up with the result of
15504 the attempted unification, even if this class didn't work out.
15505 We also don't want to commit ourselves to all the unifications
15506 we've already done, since unification is supposed to be done on
15507 an argument-by-argument basis. In other words, consider the
15508 following pathological case:
15510 template <int I, int J, int K>
15513 template <int I, int J>
15514 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15516 template <int I, int J, int K>
15517 void f(S<I, J, K>, S<I, I, I>);
15526 Now, by the time we consider the unification involving `s2', we
15527 already know that we must have `f<0, 0, 0>'. But, even though
15528 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
15529 because there are two ways to unify base classes of S<0, 1, 2>
15530 with S<I, I, I>. If we kept the already deduced knowledge, we
15531 would reject the possibility I=1. */
15532 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
15534 /* If unification failed, we're done. */
15535 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
15536 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
15542 /* Given a template type PARM and a class type ARG, find the unique
15543 base type in ARG that is an instance of PARM. We do not examine
15544 ARG itself; only its base-classes. If there is not exactly one
15545 appropriate base class, return NULL_TREE. PARM may be the type of
15546 a partial specialization, as well as a plain template type. Used
15549 static enum template_base_result
15550 get_template_base (tree tparms, tree targs, tree parm, tree arg,
15551 bool explain_p, tree *result)
15553 tree rval = NULL_TREE;
15556 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
15558 binfo = TYPE_BINFO (complete_type (arg));
15561 /* The type could not be completed. */
15562 *result = NULL_TREE;
15563 return tbr_incomplete_type;
15566 /* Walk in inheritance graph order. The search order is not
15567 important, and this avoids multiple walks of virtual bases. */
15568 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
15570 tree r = try_class_unification (tparms, targs, parm,
15571 BINFO_TYPE (binfo), explain_p);
15575 /* If there is more than one satisfactory baseclass, then:
15579 If they yield more than one possible deduced A, the type
15583 if (rval && !same_type_p (r, rval))
15585 *result = NULL_TREE;
15586 return tbr_ambiguous_baseclass;
15594 return tbr_success;
15597 /* Returns the level of DECL, which declares a template parameter. */
15600 template_decl_level (tree decl)
15602 switch (TREE_CODE (decl))
15605 case TEMPLATE_DECL:
15606 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
15609 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
15612 gcc_unreachable ();
15617 /* Decide whether ARG can be unified with PARM, considering only the
15618 cv-qualifiers of each type, given STRICT as documented for unify.
15619 Returns nonzero iff the unification is OK on that basis. */
15622 check_cv_quals_for_unify (int strict, tree arg, tree parm)
15624 int arg_quals = cp_type_quals (arg);
15625 int parm_quals = cp_type_quals (parm);
15627 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15628 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
15630 /* Although a CVR qualifier is ignored when being applied to a
15631 substituted template parameter ([8.3.2]/1 for example), that
15632 does not allow us to unify "const T" with "int&" because both
15633 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
15634 It is ok when we're allowing additional CV qualifiers
15635 at the outer level [14.8.2.1]/3,1st bullet. */
15636 if ((TREE_CODE (arg) == REFERENCE_TYPE
15637 || TREE_CODE (arg) == FUNCTION_TYPE
15638 || TREE_CODE (arg) == METHOD_TYPE)
15639 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
15642 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
15643 && (parm_quals & TYPE_QUAL_RESTRICT))
15647 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
15648 && (arg_quals & parm_quals) != parm_quals)
15651 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
15652 && (parm_quals & arg_quals) != arg_quals)
15658 /* Determines the LEVEL and INDEX for the template parameter PARM. */
15660 template_parm_level_and_index (tree parm, int* level, int* index)
15662 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15663 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
15664 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
15666 *index = TEMPLATE_TYPE_IDX (parm);
15667 *level = TEMPLATE_TYPE_LEVEL (parm);
15671 *index = TEMPLATE_PARM_IDX (parm);
15672 *level = TEMPLATE_PARM_LEVEL (parm);
15676 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
15678 if (unify (TP, TA, P, A, S, EP)) \
15682 /* Unifies the remaining arguments in PACKED_ARGS with the pack
15683 expansion at the end of PACKED_PARMS. Returns 0 if the type
15684 deduction succeeds, 1 otherwise. STRICT is the same as in
15685 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
15686 call argument list. We'll need to adjust the arguments to make them
15687 types. SUBR tells us if this is from a recursive call to
15688 type_unification_real, or for comparing two template argument
15692 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
15693 tree packed_args, unification_kind_t strict,
15694 bool subr, bool explain_p)
15697 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
15698 tree pattern = PACK_EXPANSION_PATTERN (parm);
15699 tree pack, packs = NULL_TREE;
15700 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
15701 int len = TREE_VEC_LENGTH (packed_args);
15703 /* Determine the parameter packs we will be deducing from the
15704 pattern, and record their current deductions. */
15705 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
15706 pack; pack = TREE_CHAIN (pack))
15708 tree parm_pack = TREE_VALUE (pack);
15711 /* Determine the index and level of this parameter pack. */
15712 template_parm_level_and_index (parm_pack, &level, &idx);
15714 /* Keep track of the parameter packs and their corresponding
15716 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
15717 TREE_TYPE (packs) = make_tree_vec (len - start);
15720 /* Loop through all of the arguments that have not yet been
15721 unified and unify each with the pattern. */
15722 for (i = start; i < len; i++)
15725 bool any_explicit = false;
15726 tree arg = TREE_VEC_ELT (packed_args, i);
15728 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
15729 or the element of its argument pack at the current index if
15730 this argument was explicitly specified. */
15731 for (pack = packs; pack; pack = TREE_CHAIN (pack))
15735 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
15738 if (TREE_VALUE (pack)
15739 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
15740 && (i - start < TREE_VEC_LENGTH (pargs)))
15742 any_explicit = true;
15743 arg = TREE_VEC_ELT (pargs, i - start);
15745 TMPL_ARG (targs, level, idx) = arg;
15748 /* If we had explicit template arguments, substitute them into the
15749 pattern before deduction. */
15752 /* Some arguments might still be unspecified or dependent. */
15754 ++processing_template_decl;
15755 dependent = any_dependent_template_arguments_p (targs);
15757 --processing_template_decl;
15758 parm = tsubst (pattern, targs,
15759 explain_p ? tf_warning_or_error : tf_none,
15762 --processing_template_decl;
15763 if (parm == error_mark_node)
15769 /* Unify the pattern with the current argument. */
15770 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
15771 LOOKUP_IMPLICIT, explain_p))
15774 /* For each parameter pack, collect the deduced value. */
15775 for (pack = packs; pack; pack = TREE_CHAIN (pack))
15778 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
15780 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
15781 TMPL_ARG (targs, level, idx);
15785 /* Verify that the results of unification with the parameter packs
15786 produce results consistent with what we've seen before, and make
15787 the deduced argument packs available. */
15788 for (pack = packs; pack; pack = TREE_CHAIN (pack))
15790 tree old_pack = TREE_VALUE (pack);
15791 tree new_args = TREE_TYPE (pack);
15792 int i, len = TREE_VEC_LENGTH (new_args);
15794 bool nondeduced_p = false;
15796 /* By default keep the original deduced argument pack.
15797 If necessary, more specific code is going to update the
15798 resulting deduced argument later down in this function. */
15799 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
15800 TMPL_ARG (targs, level, idx) = old_pack;
15802 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
15803 actually deduce anything. */
15804 for (i = 0; i < len && !nondeduced_p; ++i)
15805 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
15806 nondeduced_p = true;
15810 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
15812 /* If we had fewer function args than explicit template args,
15813 just use the explicits. */
15814 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
15815 int explicit_len = TREE_VEC_LENGTH (explicit_args);
15816 if (len < explicit_len)
15817 new_args = explicit_args;
15823 /* Build the deduced *_ARGUMENT_PACK. */
15824 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
15826 result = make_node (NONTYPE_ARGUMENT_PACK);
15827 TREE_TYPE (result) =
15828 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
15829 TREE_CONSTANT (result) = 1;
15832 result = cxx_make_type (TYPE_ARGUMENT_PACK);
15834 SET_ARGUMENT_PACK_ARGS (result, new_args);
15836 /* Note the deduced argument packs for this parameter
15838 TMPL_ARG (targs, level, idx) = result;
15840 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
15841 && (ARGUMENT_PACK_ARGS (old_pack)
15842 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
15844 /* We only had the explicitly-provided arguments before, but
15845 now we have a complete set of arguments. */
15846 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
15848 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
15849 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
15850 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
15854 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
15855 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
15857 if (!comp_template_args_with_info (old_args, new_args,
15858 &bad_old_arg, &bad_new_arg))
15859 /* Inconsistent unification of this parameter pack. */
15860 return unify_parameter_pack_inconsistent (explain_p,
15866 return unify_success (explain_p);
15869 /* Deduce the value of template parameters. TPARMS is the (innermost)
15870 set of template parameters to a template. TARGS is the bindings
15871 for those template parameters, as determined thus far; TARGS may
15872 include template arguments for outer levels of template parameters
15873 as well. PARM is a parameter to a template function, or a
15874 subcomponent of that parameter; ARG is the corresponding argument.
15875 This function attempts to match PARM with ARG in a manner
15876 consistent with the existing assignments in TARGS. If more values
15877 are deduced, then TARGS is updated.
15879 Returns 0 if the type deduction succeeds, 1 otherwise. The
15880 parameter STRICT is a bitwise or of the following flags:
15883 Require an exact match between PARM and ARG.
15884 UNIFY_ALLOW_MORE_CV_QUAL:
15885 Allow the deduced ARG to be more cv-qualified (by qualification
15886 conversion) than ARG.
15887 UNIFY_ALLOW_LESS_CV_QUAL:
15888 Allow the deduced ARG to be less cv-qualified than ARG.
15889 UNIFY_ALLOW_DERIVED:
15890 Allow the deduced ARG to be a template base class of ARG,
15891 or a pointer to a template base class of the type pointed to by
15893 UNIFY_ALLOW_INTEGER:
15894 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
15895 case for more information.
15896 UNIFY_ALLOW_OUTER_LEVEL:
15897 This is the outermost level of a deduction. Used to determine validity
15898 of qualification conversions. A valid qualification conversion must
15899 have const qualified pointers leading up to the inner type which
15900 requires additional CV quals, except at the outer level, where const
15901 is not required [conv.qual]. It would be normal to set this flag in
15902 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
15903 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
15904 This is the outermost level of a deduction, and PARM can be more CV
15905 qualified at this point.
15906 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
15907 This is the outermost level of a deduction, and PARM can be less CV
15908 qualified at this point. */
15911 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
15917 int strict_in = strict;
15919 /* I don't think this will do the right thing with respect to types.
15920 But the only case I've seen it in so far has been array bounds, where
15921 signedness is the only information lost, and I think that will be
15923 while (TREE_CODE (parm) == NOP_EXPR)
15924 parm = TREE_OPERAND (parm, 0);
15926 if (arg == error_mark_node)
15927 return unify_invalid (explain_p);
15928 if (arg == unknown_type_node
15929 || arg == init_list_type_node)
15930 /* We can't deduce anything from this, but we might get all the
15931 template args from other function args. */
15932 return unify_success (explain_p);
15934 /* If PARM uses template parameters, then we can't bail out here,
15935 even if ARG == PARM, since we won't record unifications for the
15936 template parameters. We might need them if we're trying to
15937 figure out which of two things is more specialized. */
15938 if (arg == parm && !uses_template_parms (parm))
15939 return unify_success (explain_p);
15941 /* Handle init lists early, so the rest of the function can assume
15942 we're dealing with a type. */
15943 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
15947 tree orig_parm = parm;
15949 /* Replace T with std::initializer_list<T> for deduction. */
15950 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15951 && flag_deduce_init_list)
15952 parm = listify (parm);
15954 if (!is_std_init_list (parm))
15955 /* We can only deduce from an initializer list argument if the
15956 parameter is std::initializer_list; otherwise this is a
15957 non-deduced context. */
15958 return unify_success (explain_p);
15960 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
15962 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
15964 int elt_strict = strict;
15966 if (elt == error_mark_node)
15967 return unify_invalid (explain_p);
15969 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
15971 tree type = TREE_TYPE (elt);
15972 /* It should only be possible to get here for a call. */
15973 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
15974 elt_strict |= maybe_adjust_types_for_deduction
15975 (DEDUCE_CALL, &elttype, &type, elt);
15979 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
15983 /* If the std::initializer_list<T> deduction worked, replace the
15984 deduced A with std::initializer_list<A>. */
15985 if (orig_parm != parm)
15987 idx = TEMPLATE_TYPE_IDX (orig_parm);
15988 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
15989 targ = listify (targ);
15990 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
15992 return unify_success (explain_p);
15995 /* Immediately reject some pairs that won't unify because of
15996 cv-qualification mismatches. */
15997 if (TREE_CODE (arg) == TREE_CODE (parm)
15999 /* It is the elements of the array which hold the cv quals of an array
16000 type, and the elements might be template type parms. We'll check
16001 when we recurse. */
16002 && TREE_CODE (arg) != ARRAY_TYPE
16003 /* We check the cv-qualifiers when unifying with template type
16004 parameters below. We want to allow ARG `const T' to unify with
16005 PARM `T' for example, when computing which of two templates
16006 is more specialized, for example. */
16007 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
16008 && !check_cv_quals_for_unify (strict_in, arg, parm))
16009 return unify_cv_qual_mismatch (explain_p, parm, arg);
16011 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
16012 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
16013 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
16014 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
16015 strict &= ~UNIFY_ALLOW_DERIVED;
16016 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
16017 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
16019 switch (TREE_CODE (parm))
16021 case TYPENAME_TYPE:
16023 case UNBOUND_CLASS_TEMPLATE:
16024 /* In a type which contains a nested-name-specifier, template
16025 argument values cannot be deduced for template parameters used
16026 within the nested-name-specifier. */
16027 return unify_success (explain_p);
16029 case TEMPLATE_TYPE_PARM:
16030 case TEMPLATE_TEMPLATE_PARM:
16031 case BOUND_TEMPLATE_TEMPLATE_PARM:
16032 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
16033 if (tparm == error_mark_node)
16034 return unify_invalid (explain_p);
16036 if (TEMPLATE_TYPE_LEVEL (parm)
16037 != template_decl_level (tparm))
16038 /* The PARM is not one we're trying to unify. Just check
16039 to see if it matches ARG. */
16041 if (TREE_CODE (arg) == TREE_CODE (parm)
16042 && same_type_p (parm, arg))
16043 return unify_success (explain_p);
16045 return unify_type_mismatch (explain_p, parm, arg);
16047 idx = TEMPLATE_TYPE_IDX (parm);
16048 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16049 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
16050 if (tparm == error_mark_node)
16051 return unify_invalid (explain_p);
16053 /* Check for mixed types and values. */
16054 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
16055 && TREE_CODE (tparm) != TYPE_DECL)
16056 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16057 && TREE_CODE (tparm) != TEMPLATE_DECL))
16058 gcc_unreachable ();
16060 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16062 /* ARG must be constructed from a template class or a template
16063 template parameter. */
16064 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
16065 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
16066 return unify_template_deduction_failure (explain_p, parm, arg);
16069 tree parmvec = TYPE_TI_ARGS (parm);
16070 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
16071 tree full_argvec = add_to_template_args (targs, argvec);
16073 = DECL_INNERMOST_TEMPLATE_PARMS
16074 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
16076 int parm_variadic_p = 0;
16078 /* The resolution to DR150 makes clear that default
16079 arguments for an N-argument may not be used to bind T
16080 to a template template parameter with fewer than N
16081 parameters. It is not safe to permit the binding of
16082 default arguments as an extension, as that may change
16083 the meaning of a conforming program. Consider:
16085 struct Dense { static const unsigned int dim = 1; };
16087 template <template <typename> class View,
16089 void operator+(float, View<Block> const&);
16091 template <typename Block,
16092 unsigned int Dim = Block::dim>
16093 struct Lvalue_proxy { operator float() const; };
16097 Lvalue_proxy<Dense> p;
16102 Here, if Lvalue_proxy is permitted to bind to View, then
16103 the global operator+ will be used; if they are not, the
16104 Lvalue_proxy will be converted to float. */
16105 if (coerce_template_parms (parm_parms,
16107 TYPE_TI_TEMPLATE (parm),
16109 ? tf_warning_or_error
16111 /*require_all_args=*/true,
16112 /*use_default_args=*/false)
16113 == error_mark_node)
16116 /* Deduce arguments T, i from TT<T> or TT<i>.
16117 We check each element of PARMVEC and ARGVEC individually
16118 rather than the whole TREE_VEC since they can have
16119 different number of elements. */
16121 parmvec = expand_template_argument_pack (parmvec);
16122 argvec = expand_template_argument_pack (argvec);
16124 len = TREE_VEC_LENGTH (parmvec);
16126 /* Check if the parameters end in a pack, making them
16129 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
16130 parm_variadic_p = 1;
16132 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
16133 return unify_too_few_arguments (explain_p,
16134 TREE_VEC_LENGTH (argvec), len);
16136 for (i = 0; i < len - parm_variadic_p; ++i)
16138 RECUR_AND_CHECK_FAILURE (tparms, targs,
16139 TREE_VEC_ELT (parmvec, i),
16140 TREE_VEC_ELT (argvec, i),
16141 UNIFY_ALLOW_NONE, explain_p);
16144 if (parm_variadic_p
16145 && unify_pack_expansion (tparms, targs,
16148 /*subr=*/true, explain_p))
16151 arg = TYPE_TI_TEMPLATE (arg);
16153 /* Fall through to deduce template name. */
16156 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
16157 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
16159 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
16161 /* Simple cases: Value already set, does match or doesn't. */
16162 if (targ != NULL_TREE && template_args_equal (targ, arg))
16163 return unify_success (explain_p);
16165 return unify_inconsistency (explain_p, parm, targ, arg);
16169 /* If PARM is `const T' and ARG is only `int', we don't have
16170 a match unless we are allowing additional qualification.
16171 If ARG is `const int' and PARM is just `T' that's OK;
16172 that binds `const int' to `T'. */
16173 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
16175 return unify_cv_qual_mismatch (explain_p, parm, arg);
16177 /* Consider the case where ARG is `const volatile int' and
16178 PARM is `const T'. Then, T should be `volatile int'. */
16179 arg = cp_build_qualified_type_real
16180 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
16181 if (arg == error_mark_node)
16182 return unify_invalid (explain_p);
16184 /* Simple cases: Value already set, does match or doesn't. */
16185 if (targ != NULL_TREE && same_type_p (targ, arg))
16186 return unify_success (explain_p);
16188 return unify_inconsistency (explain_p, parm, targ, arg);
16190 /* Make sure that ARG is not a variable-sized array. (Note
16191 that were talking about variable-sized arrays (like
16192 `int[n]'), rather than arrays of unknown size (like
16193 `int[]').) We'll get very confused by such a type since
16194 the bound of the array is not constant, and therefore
16195 not mangleable. Besides, such types are not allowed in
16196 ISO C++, so we can do as we please here. We do allow
16197 them for 'auto' deduction, since that isn't ABI-exposed. */
16198 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
16199 return unify_vla_arg (explain_p, arg);
16201 /* Strip typedefs as in convert_template_argument. */
16202 arg = canonicalize_type_argument (arg, tf_none);
16205 /* If ARG is a parameter pack or an expansion, we cannot unify
16206 against it unless PARM is also a parameter pack. */
16207 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
16208 && !template_parameter_pack_p (parm))
16209 return unify_parameter_pack_mismatch (explain_p, parm, arg);
16211 /* If the argument deduction results is a METHOD_TYPE,
16212 then there is a problem.
16213 METHOD_TYPE doesn't map to any real C++ type the result of
16214 the deduction can not be of that type. */
16215 if (TREE_CODE (arg) == METHOD_TYPE)
16216 return unify_method_type_error (explain_p, arg);
16218 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
16219 return unify_success (explain_p);
16221 case TEMPLATE_PARM_INDEX:
16222 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
16223 if (tparm == error_mark_node)
16224 return unify_invalid (explain_p);
16226 if (TEMPLATE_PARM_LEVEL (parm)
16227 != template_decl_level (tparm))
16229 /* The PARM is not one we're trying to unify. Just check
16230 to see if it matches ARG. */
16231 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
16232 && cp_tree_equal (parm, arg));
16234 unify_expression_unequal (explain_p, parm, arg);
16238 idx = TEMPLATE_PARM_IDX (parm);
16239 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
16243 int x = !cp_tree_equal (targ, arg);
16245 unify_inconsistency (explain_p, parm, targ, arg);
16249 /* [temp.deduct.type] If, in the declaration of a function template
16250 with a non-type template-parameter, the non-type
16251 template-parameter is used in an expression in the function
16252 parameter-list and, if the corresponding template-argument is
16253 deduced, the template-argument type shall match the type of the
16254 template-parameter exactly, except that a template-argument
16255 deduced from an array bound may be of any integral type.
16256 The non-type parameter might use already deduced type parameters. */
16257 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
16258 if (!TREE_TYPE (arg))
16259 /* Template-parameter dependent expression. Just accept it for now.
16260 It will later be processed in convert_template_argument. */
16262 else if (same_type_p (TREE_TYPE (arg), tparm))
16264 else if ((strict & UNIFY_ALLOW_INTEGER)
16265 && (TREE_CODE (tparm) == INTEGER_TYPE
16266 || TREE_CODE (tparm) == BOOLEAN_TYPE))
16267 /* Convert the ARG to the type of PARM; the deduced non-type
16268 template argument must exactly match the types of the
16269 corresponding parameter. */
16270 arg = fold (build_nop (tparm, arg));
16271 else if (uses_template_parms (tparm))
16272 /* We haven't deduced the type of this parameter yet. Try again
16274 return unify_success (explain_p);
16276 return unify_type_mismatch (explain_p, tparm, arg);
16278 /* If ARG is a parameter pack or an expansion, we cannot unify
16279 against it unless PARM is also a parameter pack. */
16280 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
16281 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
16282 return unify_parameter_pack_mismatch (explain_p, parm, arg);
16284 arg = strip_typedefs_expr (arg);
16285 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
16286 return unify_success (explain_p);
16290 /* A pointer-to-member constant can be unified only with
16291 another constant. */
16292 if (TREE_CODE (arg) != PTRMEM_CST)
16293 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
16295 /* Just unify the class member. It would be useless (and possibly
16296 wrong, depending on the strict flags) to unify also
16297 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16298 arg refer to the same variable, even if through different
16299 classes. For instance:
16301 struct A { int x; };
16304 Unification of &A::x and &B::x must succeed. */
16305 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
16306 PTRMEM_CST_MEMBER (arg), strict, explain_p);
16311 if (TREE_CODE (arg) != POINTER_TYPE)
16312 return unify_type_mismatch (explain_p, parm, arg);
16314 /* [temp.deduct.call]
16316 A can be another pointer or pointer to member type that can
16317 be converted to the deduced A via a qualification
16318 conversion (_conv.qual_).
16320 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16321 This will allow for additional cv-qualification of the
16322 pointed-to types if appropriate. */
16324 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
16325 /* The derived-to-base conversion only persists through one
16326 level of pointers. */
16327 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
16329 return unify (tparms, targs, TREE_TYPE (parm),
16330 TREE_TYPE (arg), strict, explain_p);
16333 case REFERENCE_TYPE:
16334 if (TREE_CODE (arg) != REFERENCE_TYPE)
16335 return unify_type_mismatch (explain_p, parm, arg);
16336 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16337 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
16340 if (TREE_CODE (arg) != ARRAY_TYPE)
16341 return unify_type_mismatch (explain_p, parm, arg);
16342 if ((TYPE_DOMAIN (parm) == NULL_TREE)
16343 != (TYPE_DOMAIN (arg) == NULL_TREE))
16344 return unify_type_mismatch (explain_p, parm, arg);
16345 if (TYPE_DOMAIN (parm) != NULL_TREE)
16352 /* Our representation of array types uses "N - 1" as the
16353 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16354 not an integer constant. We cannot unify arbitrarily
16355 complex expressions, so we eliminate the MINUS_EXPRs
16357 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
16358 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
16361 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
16362 parm_max = TREE_OPERAND (parm_max, 0);
16364 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
16365 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
16368 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16369 trying to unify the type of a variable with the type
16370 of a template parameter. For example:
16372 template <unsigned int N>
16373 void f (char (&) [N]);
16380 Here, the type of the ARG will be "int [g(i)]", and
16381 may be a SAVE_EXPR, etc. */
16382 if (TREE_CODE (arg_max) != MINUS_EXPR)
16383 return unify_vla_arg (explain_p, arg);
16384 arg_max = TREE_OPERAND (arg_max, 0);
16387 /* If only one of the bounds used a MINUS_EXPR, compensate
16388 by adding one to the other bound. */
16389 if (parm_cst && !arg_cst)
16390 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
16394 else if (arg_cst && !parm_cst)
16395 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
16400 RECUR_AND_CHECK_FAILURE (tparms, targs, parm_max, arg_max,
16401 UNIFY_ALLOW_INTEGER, explain_p);
16403 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16404 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
16411 case ENUMERAL_TYPE:
16414 if (TREE_CODE (arg) != TREE_CODE (parm))
16415 return unify_type_mismatch (explain_p, parm, arg);
16417 /* We have already checked cv-qualification at the top of the
16419 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
16420 return unify_type_mismatch (explain_p, parm, arg);
16422 /* As far as unification is concerned, this wins. Later checks
16423 will invalidate it if necessary. */
16424 return unify_success (explain_p);
16426 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16427 /* Type INTEGER_CST can come from ordinary constant template args. */
16429 while (TREE_CODE (arg) == NOP_EXPR)
16430 arg = TREE_OPERAND (arg, 0);
16432 if (TREE_CODE (arg) != INTEGER_CST)
16433 return unify_template_argument_mismatch (explain_p, parm, arg);
16434 return (tree_int_cst_equal (parm, arg)
16435 ? unify_success (explain_p)
16436 : unify_template_argument_mismatch (explain_p, parm, arg));
16440 int i, len, argslen;
16441 int parm_variadic_p = 0;
16443 if (TREE_CODE (arg) != TREE_VEC)
16444 return unify_template_argument_mismatch (explain_p, parm, arg);
16446 len = TREE_VEC_LENGTH (parm);
16447 argslen = TREE_VEC_LENGTH (arg);
16449 /* Check for pack expansions in the parameters. */
16450 for (i = 0; i < len; ++i)
16452 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
16455 /* We can unify against something with a trailing
16457 parm_variadic_p = 1;
16459 /* [temp.deduct.type]/9: If the template argument list of
16460 P contains a pack expansion that is not the last
16461 template argument, the entire template argument list
16462 is a non-deduced context. */
16463 return unify_success (explain_p);
16467 /* If we don't have enough arguments to satisfy the parameters
16468 (not counting the pack expression at the end), or we have
16469 too many arguments for a parameter list that doesn't end in
16470 a pack expression, we can't unify. */
16471 if (parm_variadic_p
16472 ? argslen < len - parm_variadic_p
16474 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
16476 /* Unify all of the parameters that precede the (optional)
16477 pack expression. */
16478 for (i = 0; i < len - parm_variadic_p; ++i)
16480 RECUR_AND_CHECK_FAILURE (tparms, targs,
16481 TREE_VEC_ELT (parm, i),
16482 TREE_VEC_ELT (arg, i),
16483 UNIFY_ALLOW_NONE, explain_p);
16485 if (parm_variadic_p)
16486 return unify_pack_expansion (tparms, targs, parm, arg,
16488 /*subr=*/true, explain_p);
16489 return unify_success (explain_p);
16494 if (TREE_CODE (arg) != TREE_CODE (parm))
16495 return unify_type_mismatch (explain_p, parm, arg);
16497 if (TYPE_PTRMEMFUNC_P (parm))
16499 if (!TYPE_PTRMEMFUNC_P (arg))
16500 return unify_type_mismatch (explain_p, parm, arg);
16502 return unify (tparms, targs,
16503 TYPE_PTRMEMFUNC_FN_TYPE (parm),
16504 TYPE_PTRMEMFUNC_FN_TYPE (arg),
16505 strict, explain_p);
16508 if (CLASSTYPE_TEMPLATE_INFO (parm))
16510 tree t = NULL_TREE;
16512 if (strict_in & UNIFY_ALLOW_DERIVED)
16514 /* First, we try to unify the PARM and ARG directly. */
16515 t = try_class_unification (tparms, targs,
16516 parm, arg, explain_p);
16520 /* Fallback to the special case allowed in
16521 [temp.deduct.call]:
16523 If P is a class, and P has the form
16524 template-id, then A can be a derived class of
16525 the deduced A. Likewise, if P is a pointer to
16526 a class of the form template-id, A can be a
16527 pointer to a derived class pointed to by the
16529 enum template_base_result r;
16530 r = get_template_base (tparms, targs, parm, arg,
16534 return unify_no_common_base (explain_p, r, parm, arg);
16537 else if (CLASSTYPE_TEMPLATE_INFO (arg)
16538 && (CLASSTYPE_TI_TEMPLATE (parm)
16539 == CLASSTYPE_TI_TEMPLATE (arg)))
16540 /* Perhaps PARM is something like S<U> and ARG is S<int>.
16541 Then, we should unify `int' and `U'. */
16544 /* There's no chance of unification succeeding. */
16545 return unify_type_mismatch (explain_p, parm, arg);
16547 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
16548 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
16550 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
16551 return unify_type_mismatch (explain_p, parm, arg);
16552 return unify_success (explain_p);
16555 case FUNCTION_TYPE:
16557 unsigned int nargs;
16562 if (TREE_CODE (arg) != TREE_CODE (parm))
16563 return unify_type_mismatch (explain_p, parm, arg);
16565 /* CV qualifications for methods can never be deduced, they must
16566 match exactly. We need to check them explicitly here,
16567 because type_unification_real treats them as any other
16568 cv-qualified parameter. */
16569 if (TREE_CODE (parm) == METHOD_TYPE
16570 && (!check_cv_quals_for_unify
16572 class_of_this_parm (arg),
16573 class_of_this_parm (parm))))
16574 return unify_cv_qual_mismatch (explain_p, parm, arg);
16576 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
16577 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
16579 nargs = list_length (TYPE_ARG_TYPES (arg));
16580 args = XALLOCAVEC (tree, nargs);
16581 for (a = TYPE_ARG_TYPES (arg), i = 0;
16582 a != NULL_TREE && a != void_list_node;
16583 a = TREE_CHAIN (a), ++i)
16584 args[i] = TREE_VALUE (a);
16587 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
16588 args, nargs, 1, DEDUCE_EXACT,
16589 LOOKUP_NORMAL, explain_p);
16593 /* Unify a pointer to member with a pointer to member function, which
16594 deduces the type of the member as a function type. */
16595 if (TYPE_PTRMEMFUNC_P (arg))
16600 /* Check top-level cv qualifiers */
16601 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
16602 return unify_cv_qual_mismatch (explain_p, parm, arg);
16604 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
16605 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
16606 UNIFY_ALLOW_NONE, explain_p);
16608 /* Determine the type of the function we are unifying against. */
16609 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
16611 build_function_type (TREE_TYPE (method_type),
16612 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
16614 /* Extract the cv-qualifiers of the member function from the
16615 implicit object parameter and place them on the function
16616 type to be restored later. */
16617 fntype = apply_memfn_quals (fntype, type_memfn_quals (method_type));
16618 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
16621 if (TREE_CODE (arg) != OFFSET_TYPE)
16622 return unify_type_mismatch (explain_p, parm, arg);
16623 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
16624 TYPE_OFFSET_BASETYPE (arg),
16625 UNIFY_ALLOW_NONE, explain_p);
16626 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
16627 strict, explain_p);
16630 if (DECL_TEMPLATE_PARM_P (parm))
16631 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
16632 if (arg != integral_constant_value (parm))
16633 return unify_template_argument_mismatch (explain_p, parm, arg);
16634 return unify_success (explain_p);
16637 case TEMPLATE_DECL:
16638 /* Matched cases are handled by the ARG == PARM test above. */
16639 return unify_template_argument_mismatch (explain_p, parm, arg);
16642 /* A non-type template parameter that is a variable should be a
16643 an integral constant, in which case, it whould have been
16644 folded into its (constant) value. So we should not be getting
16645 a variable here. */
16646 gcc_unreachable ();
16648 case TYPE_ARGUMENT_PACK:
16649 case NONTYPE_ARGUMENT_PACK:
16650 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
16651 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
16654 case DECLTYPE_TYPE:
16655 case UNDERLYING_TYPE:
16656 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
16657 or UNDERLYING_TYPE nodes. */
16658 return unify_success (explain_p);
16661 /* Unification fails if we hit an error node. */
16662 return unify_invalid (explain_p);
16665 /* An unresolved overload is a nondeduced context. */
16666 if (is_overloaded_fn (parm) || type_unknown_p (parm))
16667 return unify_success (explain_p);
16668 gcc_assert (EXPR_P (parm));
16670 /* We must be looking at an expression. This can happen with
16674 void foo(S<I>, S<I + 2>);
16676 This is a "nondeduced context":
16680 The nondeduced contexts are:
16682 --A type that is a template-id in which one or more of
16683 the template-arguments is an expression that references
16684 a template-parameter.
16686 In these cases, we assume deduction succeeded, but don't
16687 actually infer any unifications. */
16689 if (!uses_template_parms (parm)
16690 && !template_args_equal (parm, arg))
16691 return unify_expression_unequal (explain_p, parm, arg);
16693 return unify_success (explain_p);
16696 #undef RECUR_AND_CHECK_FAILURE
16698 /* Note that DECL can be defined in this translation unit, if
16702 mark_definable (tree decl)
16705 DECL_NOT_REALLY_EXTERN (decl) = 1;
16706 FOR_EACH_CLONE (clone, decl)
16707 DECL_NOT_REALLY_EXTERN (clone) = 1;
16710 /* Called if RESULT is explicitly instantiated, or is a member of an
16711 explicitly instantiated class. */
16714 mark_decl_instantiated (tree result, int extern_p)
16716 SET_DECL_EXPLICIT_INSTANTIATION (result);
16718 /* If this entity has already been written out, it's too late to
16719 make any modifications. */
16720 if (TREE_ASM_WRITTEN (result))
16723 if (TREE_CODE (result) != FUNCTION_DECL)
16724 /* The TREE_PUBLIC flag for function declarations will have been
16725 set correctly by tsubst. */
16726 TREE_PUBLIC (result) = 1;
16728 /* This might have been set by an earlier implicit instantiation. */
16729 DECL_COMDAT (result) = 0;
16732 DECL_NOT_REALLY_EXTERN (result) = 0;
16735 mark_definable (result);
16736 /* Always make artificials weak. */
16737 if (DECL_ARTIFICIAL (result) && flag_weak)
16738 comdat_linkage (result);
16739 /* For WIN32 we also want to put explicit instantiations in
16740 linkonce sections. */
16741 else if (TREE_PUBLIC (result))
16742 maybe_make_one_only (result);
16745 /* If EXTERN_P, then this function will not be emitted -- unless
16746 followed by an explicit instantiation, at which point its linkage
16747 will be adjusted. If !EXTERN_P, then this function will be
16748 emitted here. In neither circumstance do we want
16749 import_export_decl to adjust the linkage. */
16750 DECL_INTERFACE_KNOWN (result) = 1;
16753 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
16754 important template arguments. If any are missing, we check whether
16755 they're important by using error_mark_node for substituting into any
16756 args that were used for partial ordering (the ones between ARGS and END)
16757 and seeing if it bubbles up. */
16760 check_undeduced_parms (tree targs, tree args, tree end)
16762 bool found = false;
16764 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
16765 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
16768 TREE_VEC_ELT (targs, i) = error_mark_node;
16772 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
16773 if (substed == error_mark_node)
16779 /* Given two function templates PAT1 and PAT2, return:
16781 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
16782 -1 if PAT2 is more specialized than PAT1.
16783 0 if neither is more specialized.
16785 LEN indicates the number of parameters we should consider
16786 (defaulted parameters should not be considered).
16788 The 1998 std underspecified function template partial ordering, and
16789 DR214 addresses the issue. We take pairs of arguments, one from
16790 each of the templates, and deduce them against each other. One of
16791 the templates will be more specialized if all the *other*
16792 template's arguments deduce against its arguments and at least one
16793 of its arguments *does* *not* deduce against the other template's
16794 corresponding argument. Deduction is done as for class templates.
16795 The arguments used in deduction have reference and top level cv
16796 qualifiers removed. Iff both arguments were originally reference
16797 types *and* deduction succeeds in both directions, the template
16798 with the more cv-qualified argument wins for that pairing (if
16799 neither is more cv-qualified, they both are equal). Unlike regular
16800 deduction, after all the arguments have been deduced in this way,
16801 we do *not* verify the deduced template argument values can be
16802 substituted into non-deduced contexts.
16804 The logic can be a bit confusing here, because we look at deduce1 and
16805 targs1 to see if pat2 is at least as specialized, and vice versa; if we
16806 can find template arguments for pat1 to make arg1 look like arg2, that
16807 means that arg2 is at least as specialized as arg1. */
16810 more_specialized_fn (tree pat1, tree pat2, int len)
16812 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
16813 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
16814 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
16815 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
16816 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
16817 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
16818 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
16819 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
16820 tree origs1, origs2;
16821 bool lose1 = false;
16822 bool lose2 = false;
16824 /* Remove the this parameter from non-static member functions. If
16825 one is a non-static member function and the other is not a static
16826 member function, remove the first parameter from that function
16827 also. This situation occurs for operator functions where we
16828 locate both a member function (with this pointer) and non-member
16829 operator (with explicit first operand). */
16830 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
16832 len--; /* LEN is the number of significant arguments for DECL1 */
16833 args1 = TREE_CHAIN (args1);
16834 if (!DECL_STATIC_FUNCTION_P (decl2))
16835 args2 = TREE_CHAIN (args2);
16837 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
16839 args2 = TREE_CHAIN (args2);
16840 if (!DECL_STATIC_FUNCTION_P (decl1))
16843 args1 = TREE_CHAIN (args1);
16847 /* If only one is a conversion operator, they are unordered. */
16848 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
16851 /* Consider the return type for a conversion function */
16852 if (DECL_CONV_FN_P (decl1))
16854 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
16855 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
16859 processing_template_decl++;
16865 /* Stop when an ellipsis is seen. */
16866 && args1 != NULL_TREE && args2 != NULL_TREE)
16868 tree arg1 = TREE_VALUE (args1);
16869 tree arg2 = TREE_VALUE (args2);
16870 int deduce1, deduce2;
16874 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
16875 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
16877 /* When both arguments are pack expansions, we need only
16878 unify the patterns themselves. */
16879 arg1 = PACK_EXPANSION_PATTERN (arg1);
16880 arg2 = PACK_EXPANSION_PATTERN (arg2);
16882 /* This is the last comparison we need to do. */
16886 if (TREE_CODE (arg1) == REFERENCE_TYPE)
16888 arg1 = TREE_TYPE (arg1);
16889 quals1 = cp_type_quals (arg1);
16892 if (TREE_CODE (arg2) == REFERENCE_TYPE)
16894 arg2 = TREE_TYPE (arg2);
16895 quals2 = cp_type_quals (arg2);
16898 if ((quals1 < 0) != (quals2 < 0))
16900 /* Only of the args is a reference, see if we should apply
16901 array/function pointer decay to it. This is not part of
16902 DR214, but is, IMHO, consistent with the deduction rules
16903 for the function call itself, and with our earlier
16904 implementation of the underspecified partial ordering
16905 rules. (nathan). */
16908 switch (TREE_CODE (arg1))
16911 arg1 = TREE_TYPE (arg1);
16913 case FUNCTION_TYPE:
16914 arg1 = build_pointer_type (arg1);
16923 switch (TREE_CODE (arg2))
16926 arg2 = TREE_TYPE (arg2);
16928 case FUNCTION_TYPE:
16929 arg2 = build_pointer_type (arg2);
16938 arg1 = TYPE_MAIN_VARIANT (arg1);
16939 arg2 = TYPE_MAIN_VARIANT (arg2);
16941 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
16943 int i, len2 = list_length (args2);
16944 tree parmvec = make_tree_vec (1);
16945 tree argvec = make_tree_vec (len2);
16948 /* Setup the parameter vector, which contains only ARG1. */
16949 TREE_VEC_ELT (parmvec, 0) = arg1;
16951 /* Setup the argument vector, which contains the remaining
16953 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
16954 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
16956 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
16957 argvec, DEDUCE_EXACT,
16958 /*subr=*/true, /*explain_p=*/false)
16961 /* We cannot deduce in the other direction, because ARG1 is
16962 a pack expansion but ARG2 is not. */
16965 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
16967 int i, len1 = list_length (args1);
16968 tree parmvec = make_tree_vec (1);
16969 tree argvec = make_tree_vec (len1);
16972 /* Setup the parameter vector, which contains only ARG1. */
16973 TREE_VEC_ELT (parmvec, 0) = arg2;
16975 /* Setup the argument vector, which contains the remaining
16977 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
16978 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
16980 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
16981 argvec, DEDUCE_EXACT,
16982 /*subr=*/true, /*explain_p=*/false)
16985 /* We cannot deduce in the other direction, because ARG2 is
16986 a pack expansion but ARG1 is not.*/
16992 /* The normal case, where neither argument is a pack
16994 deduce1 = (unify (tparms1, targs1, arg1, arg2,
16995 UNIFY_ALLOW_NONE, /*explain_p=*/false)
16997 deduce2 = (unify (tparms2, targs2, arg2, arg1,
16998 UNIFY_ALLOW_NONE, /*explain_p=*/false)
17002 /* If we couldn't deduce arguments for tparms1 to make arg1 match
17003 arg2, then arg2 is not as specialized as arg1. */
17009 /* "If, for a given type, deduction succeeds in both directions
17010 (i.e., the types are identical after the transformations above)
17011 and if the type from the argument template is more cv-qualified
17012 than the type from the parameter template (as described above)
17013 that type is considered to be more specialized than the other. If
17014 neither type is more cv-qualified than the other then neither type
17015 is more specialized than the other." */
17017 if (deduce1 && deduce2
17018 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
17020 if ((quals1 & quals2) == quals2)
17022 if ((quals1 & quals2) == quals1)
17026 if (lose1 && lose2)
17027 /* We've failed to deduce something in either direction.
17028 These must be unordered. */
17031 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
17032 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
17033 /* We have already processed all of the arguments in our
17034 handing of the pack expansion type. */
17037 args1 = TREE_CHAIN (args1);
17038 args2 = TREE_CHAIN (args2);
17041 /* "In most cases, all template parameters must have values in order for
17042 deduction to succeed, but for partial ordering purposes a template
17043 parameter may remain without a value provided it is not used in the
17044 types being used for partial ordering."
17046 Thus, if we are missing any of the targs1 we need to substitute into
17047 origs1, then pat2 is not as specialized as pat1. This can happen when
17048 there is a nondeduced context. */
17049 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
17051 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
17054 processing_template_decl--;
17056 /* All things being equal, if the next argument is a pack expansion
17057 for one function but not for the other, prefer the
17058 non-variadic function. FIXME this is bogus; see c++/41958. */
17060 && args1 && TREE_VALUE (args1)
17061 && args2 && TREE_VALUE (args2))
17063 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
17064 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
17067 if (lose1 == lose2)
17075 /* Determine which of two partial specializations is more specialized.
17077 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
17078 to the first partial specialization. The TREE_VALUE is the
17079 innermost set of template parameters for the partial
17080 specialization. PAT2 is similar, but for the second template.
17082 Return 1 if the first partial specialization is more specialized;
17083 -1 if the second is more specialized; 0 if neither is more
17086 See [temp.class.order] for information about determining which of
17087 two templates is more specialized. */
17090 more_specialized_class (tree pat1, tree pat2)
17095 bool any_deductions = false;
17097 tmpl1 = TREE_TYPE (pat1);
17098 tmpl2 = TREE_TYPE (pat2);
17100 /* Just like what happens for functions, if we are ordering between
17101 different class template specializations, we may encounter dependent
17102 types in the arguments, and we need our dependency check functions
17103 to behave correctly. */
17104 ++processing_template_decl;
17105 targs = get_class_bindings (TREE_VALUE (pat1),
17106 CLASSTYPE_TI_ARGS (tmpl1),
17107 CLASSTYPE_TI_ARGS (tmpl2));
17111 any_deductions = true;
17114 targs = get_class_bindings (TREE_VALUE (pat2),
17115 CLASSTYPE_TI_ARGS (tmpl2),
17116 CLASSTYPE_TI_ARGS (tmpl1));
17120 any_deductions = true;
17122 --processing_template_decl;
17124 /* In the case of a tie where at least one of the class templates
17125 has a parameter pack at the end, the template with the most
17126 non-packed parameters wins. */
17129 && (template_args_variadic_p (TREE_PURPOSE (pat1))
17130 || template_args_variadic_p (TREE_PURPOSE (pat2))))
17132 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
17133 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
17134 int len1 = TREE_VEC_LENGTH (args1);
17135 int len2 = TREE_VEC_LENGTH (args2);
17137 /* We don't count the pack expansion at the end. */
17138 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
17140 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
17145 else if (len1 < len2)
17152 /* Return the template arguments that will produce the function signature
17153 DECL from the function template FN, with the explicit template
17154 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
17155 also match. Return NULL_TREE if no satisfactory arguments could be
17159 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
17161 int ntparms = DECL_NTPARMS (fn);
17162 tree targs = make_tree_vec (ntparms);
17164 tree decl_arg_types;
17166 unsigned int nargs, ix;
17169 /* Substitute the explicit template arguments into the type of DECL.
17170 The call to fn_type_unification will handle substitution into the
17172 decl_type = TREE_TYPE (decl);
17173 if (explicit_args && uses_template_parms (decl_type))
17176 tree converted_args;
17178 if (DECL_TEMPLATE_INFO (decl))
17179 tmpl = DECL_TI_TEMPLATE (decl);
17181 /* We can get here for some invalid specializations. */
17185 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
17186 explicit_args, NULL_TREE,
17188 /*require_all_args=*/false,
17189 /*use_default_args=*/false);
17190 if (converted_args == error_mark_node)
17193 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
17194 if (decl_type == error_mark_node)
17198 /* Never do unification on the 'this' parameter. */
17199 decl_arg_types = skip_artificial_parms_for (decl,
17200 TYPE_ARG_TYPES (decl_type));
17202 nargs = list_length (decl_arg_types);
17203 args = XALLOCAVEC (tree, nargs);
17204 for (arg = decl_arg_types, ix = 0;
17205 arg != NULL_TREE && arg != void_list_node;
17206 arg = TREE_CHAIN (arg), ++ix)
17207 args[ix] = TREE_VALUE (arg);
17209 if (fn_type_unification (fn, explicit_args, targs,
17211 (check_rettype || DECL_CONV_FN_P (fn)
17212 ? TREE_TYPE (decl_type) : NULL_TREE),
17213 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false))
17219 /* Return the innermost template arguments that, when applied to a
17220 template specialization whose innermost template parameters are
17221 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
17224 For example, suppose we have:
17226 template <class T, class U> struct S {};
17227 template <class T> struct S<T*, int> {};
17229 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
17230 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
17231 int}. The resulting vector will be {double}, indicating that `T'
17232 is bound to `double'. */
17235 get_class_bindings (tree tparms, tree spec_args, tree args)
17237 int i, ntparms = TREE_VEC_LENGTH (tparms);
17239 tree innermost_deduced_args;
17241 innermost_deduced_args = make_tree_vec (ntparms);
17242 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
17244 deduced_args = copy_node (args);
17245 SET_TMPL_ARGS_LEVEL (deduced_args,
17246 TMPL_ARGS_DEPTH (deduced_args),
17247 innermost_deduced_args);
17250 deduced_args = innermost_deduced_args;
17252 if (unify (tparms, deduced_args,
17253 INNERMOST_TEMPLATE_ARGS (spec_args),
17254 INNERMOST_TEMPLATE_ARGS (args),
17255 UNIFY_ALLOW_NONE, /*explain_p=*/false))
17258 for (i = 0; i < ntparms; ++i)
17259 if (! TREE_VEC_ELT (innermost_deduced_args, i))
17262 /* Verify that nondeduced template arguments agree with the type
17263 obtained from argument deduction.
17267 struct A { typedef int X; };
17268 template <class T, class U> struct C {};
17269 template <class T> struct C<T, typename T::X> {};
17271 Then with the instantiation `C<A, int>', we can deduce that
17272 `T' is `A' but unify () does not check whether `typename T::X'
17274 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
17275 if (spec_args == error_mark_node
17276 /* We only need to check the innermost arguments; the other
17277 arguments will always agree. */
17278 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
17279 INNERMOST_TEMPLATE_ARGS (args)))
17282 /* Now that we have bindings for all of the template arguments,
17283 ensure that the arguments deduced for the template template
17284 parameters have compatible template parameter lists. See the use
17285 of template_template_parm_bindings_ok_p in fn_type_unification
17286 for more information. */
17287 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
17290 return deduced_args;
17293 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17294 Return the TREE_LIST node with the most specialized template, if
17295 any. If there is no most specialized template, the error_mark_node
17298 Note that this function does not look at, or modify, the
17299 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17300 returned is one of the elements of INSTANTIATIONS, callers may
17301 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17302 and retrieve it from the value returned. */
17305 most_specialized_instantiation (tree templates)
17309 ++processing_template_decl;
17312 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
17316 if (get_bindings (TREE_VALUE (champ),
17317 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
17318 NULL_TREE, /*check_ret=*/true))
17321 if (get_bindings (TREE_VALUE (fn),
17322 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
17323 NULL_TREE, /*check_ret=*/true))
17330 /* Equally specialized, move to next function. If there
17331 is no next function, nothing's most specialized. */
17332 fn = TREE_CHAIN (fn);
17340 /* Now verify that champ is better than everything earlier in the
17341 instantiation list. */
17342 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
17343 if (get_bindings (TREE_VALUE (champ),
17344 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
17345 NULL_TREE, /*check_ret=*/true)
17346 || !get_bindings (TREE_VALUE (fn),
17347 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
17348 NULL_TREE, /*check_ret=*/true))
17354 processing_template_decl--;
17357 return error_mark_node;
17362 /* If DECL is a specialization of some template, return the most
17363 general such template. Otherwise, returns NULL_TREE.
17365 For example, given:
17367 template <class T> struct S { template <class U> void f(U); };
17369 if TMPL is `template <class U> void S<int>::f(U)' this will return
17370 the full template. This function will not trace past partial
17371 specializations, however. For example, given in addition:
17373 template <class T> struct S<T*> { template <class U> void f(U); };
17375 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17376 `template <class T> template <class U> S<T*>::f(U)'. */
17379 most_general_template (tree decl)
17381 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17382 an immediate specialization. */
17383 if (TREE_CODE (decl) == FUNCTION_DECL)
17385 if (DECL_TEMPLATE_INFO (decl)) {
17386 decl = DECL_TI_TEMPLATE (decl);
17388 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17389 template friend. */
17390 if (TREE_CODE (decl) != TEMPLATE_DECL)
17396 /* Look for more and more general templates. */
17397 while (DECL_TEMPLATE_INFO (decl))
17399 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17400 (See cp-tree.h for details.) */
17401 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
17404 if (CLASS_TYPE_P (TREE_TYPE (decl))
17405 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
17408 /* Stop if we run into an explicitly specialized class template. */
17409 if (!DECL_NAMESPACE_SCOPE_P (decl)
17410 && DECL_CONTEXT (decl)
17411 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
17414 decl = DECL_TI_TEMPLATE (decl);
17420 /* Return the most specialized of the class template partial
17421 specializations of TMPL which can produce TYPE, a specialization of
17422 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17423 a _TYPE node corresponding to the partial specialization, while the
17424 TREE_PURPOSE is the set of template arguments that must be
17425 substituted into the TREE_TYPE in order to generate TYPE.
17427 If the choice of partial specialization is ambiguous, a diagnostic
17428 is issued, and the error_mark_node is returned. If there are no
17429 partial specializations of TMPL matching TYPE, then NULL_TREE is
17433 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
17435 tree list = NULL_TREE;
17441 tree outer_args = NULL_TREE;
17443 tmpl = most_general_template (tmpl);
17444 args = CLASSTYPE_TI_ARGS (type);
17446 /* For determining which partial specialization to use, only the
17447 innermost args are interesting. */
17448 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
17450 outer_args = strip_innermost_template_args (args, 1);
17451 args = INNERMOST_TEMPLATE_ARGS (args);
17454 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
17456 tree partial_spec_args;
17458 tree parms = TREE_VALUE (t);
17460 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
17462 ++processing_template_decl;
17468 /* Discard the outer levels of args, and then substitute in the
17469 template args from the enclosing class. */
17470 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
17471 partial_spec_args = tsubst_template_args
17472 (partial_spec_args, outer_args, tf_none, NULL_TREE);
17474 /* PARMS already refers to just the innermost parms, but the
17475 template parms in partial_spec_args had their levels lowered
17476 by tsubst, so we need to do the same for the parm list. We
17477 can't just tsubst the TREE_VEC itself, as tsubst wants to
17478 treat a TREE_VEC as an argument vector. */
17479 parms = copy_node (parms);
17480 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
17481 TREE_VEC_ELT (parms, i) =
17482 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
17486 partial_spec_args =
17487 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
17488 add_to_template_args (outer_args,
17489 partial_spec_args),
17491 /*require_all_args=*/true,
17492 /*use_default_args=*/true);
17494 --processing_template_decl;
17496 if (partial_spec_args == error_mark_node)
17497 return error_mark_node;
17499 spec_args = get_class_bindings (parms,
17505 spec_args = add_to_template_args (outer_args, spec_args);
17506 list = tree_cons (spec_args, TREE_VALUE (t), list);
17507 TREE_TYPE (list) = TREE_TYPE (t);
17514 ambiguous_p = false;
17517 t = TREE_CHAIN (t);
17518 for (; t; t = TREE_CHAIN (t))
17520 fate = more_specialized_class (champ, t);
17527 t = TREE_CHAIN (t);
17530 ambiguous_p = true;
17539 for (t = list; t && t != champ; t = TREE_CHAIN (t))
17541 fate = more_specialized_class (champ, t);
17544 ambiguous_p = true;
17552 char *spaces = NULL;
17553 if (!(complain & tf_error))
17554 return error_mark_node;
17555 error ("ambiguous class template instantiation for %q#T", type);
17556 str = ngettext ("candidate is:", "candidates are:", list_length (list));
17557 for (t = list; t; t = TREE_CHAIN (t))
17559 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
17560 spaces = spaces ? spaces : get_spaces (str);
17563 return error_mark_node;
17569 /* Explicitly instantiate DECL. */
17572 do_decl_instantiation (tree decl, tree storage)
17574 tree result = NULL_TREE;
17577 if (!decl || decl == error_mark_node)
17578 /* An error occurred, for which grokdeclarator has already issued
17579 an appropriate message. */
17581 else if (! DECL_LANG_SPECIFIC (decl))
17583 error ("explicit instantiation of non-template %q#D", decl);
17586 else if (TREE_CODE (decl) == VAR_DECL)
17588 /* There is an asymmetry here in the way VAR_DECLs and
17589 FUNCTION_DECLs are handled by grokdeclarator. In the case of
17590 the latter, the DECL we get back will be marked as a
17591 template instantiation, and the appropriate
17592 DECL_TEMPLATE_INFO will be set up. This does not happen for
17593 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
17594 should handle VAR_DECLs as it currently handles
17596 if (!DECL_CLASS_SCOPE_P (decl))
17598 error ("%qD is not a static data member of a class template", decl);
17601 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
17602 if (!result || TREE_CODE (result) != VAR_DECL)
17604 error ("no matching template for %qD found", decl);
17607 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
17609 error ("type %qT for explicit instantiation %qD does not match "
17610 "declared type %qT", TREE_TYPE (result), decl,
17615 else if (TREE_CODE (decl) != FUNCTION_DECL)
17617 error ("explicit instantiation of %q#D", decl);
17623 /* Check for various error cases. Note that if the explicit
17624 instantiation is valid the RESULT will currently be marked as an
17625 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
17626 until we get here. */
17628 if (DECL_TEMPLATE_SPECIALIZATION (result))
17630 /* DR 259 [temp.spec].
17632 Both an explicit instantiation and a declaration of an explicit
17633 specialization shall not appear in a program unless the explicit
17634 instantiation follows a declaration of the explicit specialization.
17636 For a given set of template parameters, if an explicit
17637 instantiation of a template appears after a declaration of an
17638 explicit specialization for that template, the explicit
17639 instantiation has no effect. */
17642 else if (DECL_EXPLICIT_INSTANTIATION (result))
17646 No program shall explicitly instantiate any template more
17649 We check DECL_NOT_REALLY_EXTERN so as not to complain when
17650 the first instantiation was `extern' and the second is not,
17651 and EXTERN_P for the opposite case. */
17652 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
17653 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
17654 /* If an "extern" explicit instantiation follows an ordinary
17655 explicit instantiation, the template is instantiated. */
17659 else if (!DECL_IMPLICIT_INSTANTIATION (result))
17661 error ("no matching template for %qD found", result);
17664 else if (!DECL_TEMPLATE_INFO (result))
17666 permerror (input_location, "explicit instantiation of non-template %q#D", result);
17670 if (storage == NULL_TREE)
17672 else if (storage == ridpointers[(int) RID_EXTERN])
17674 if (!in_system_header && (cxx_dialect == cxx98))
17675 pedwarn (input_location, OPT_pedantic,
17676 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
17681 error ("storage class %qD applied to template instantiation", storage);
17683 check_explicit_instantiation_namespace (result);
17684 mark_decl_instantiated (result, extern_p);
17686 instantiate_decl (result, /*defer_ok=*/1,
17687 /*expl_inst_class_mem_p=*/false);
17691 mark_class_instantiated (tree t, int extern_p)
17693 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
17694 SET_CLASSTYPE_INTERFACE_KNOWN (t);
17695 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
17696 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
17699 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
17700 rest_of_type_compilation (t, 1);
17704 /* Called from do_type_instantiation through binding_table_foreach to
17705 do recursive instantiation for the type bound in ENTRY. */
17707 bt_instantiate_type_proc (binding_entry entry, void *data)
17709 tree storage = *(tree *) data;
17711 if (MAYBE_CLASS_TYPE_P (entry->type)
17712 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
17713 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
17716 /* Called from do_type_instantiation to instantiate a member
17717 (a member function or a static member variable) of an
17718 explicitly instantiated class template. */
17720 instantiate_class_member (tree decl, int extern_p)
17722 mark_decl_instantiated (decl, extern_p);
17724 instantiate_decl (decl, /*defer_ok=*/1,
17725 /*expl_inst_class_mem_p=*/true);
17728 /* Perform an explicit instantiation of template class T. STORAGE, if
17729 non-null, is the RID for extern, inline or static. COMPLAIN is
17730 nonzero if this is called from the parser, zero if called recursively,
17731 since the standard is unclear (as detailed below). */
17734 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
17739 int previous_instantiation_extern_p = 0;
17741 if (TREE_CODE (t) == TYPE_DECL)
17744 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
17747 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
17749 error ("explicit instantiation of non-class template %qD", tmpl);
17751 error ("explicit instantiation of non-template type %qT", t);
17757 if (!COMPLETE_TYPE_P (t))
17759 if (complain & tf_error)
17760 error ("explicit instantiation of %q#T before definition of template",
17765 if (storage != NULL_TREE)
17767 if (!in_system_header)
17769 if (storage == ridpointers[(int) RID_EXTERN])
17771 if (cxx_dialect == cxx98)
17772 pedwarn (input_location, OPT_pedantic,
17773 "ISO C++ 1998 forbids the use of %<extern%> on "
17774 "explicit instantiations");
17777 pedwarn (input_location, OPT_pedantic,
17778 "ISO C++ forbids the use of %qE"
17779 " on explicit instantiations", storage);
17782 if (storage == ridpointers[(int) RID_INLINE])
17784 else if (storage == ridpointers[(int) RID_EXTERN])
17786 else if (storage == ridpointers[(int) RID_STATIC])
17790 error ("storage class %qD applied to template instantiation",
17796 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
17798 /* DR 259 [temp.spec].
17800 Both an explicit instantiation and a declaration of an explicit
17801 specialization shall not appear in a program unless the explicit
17802 instantiation follows a declaration of the explicit specialization.
17804 For a given set of template parameters, if an explicit
17805 instantiation of a template appears after a declaration of an
17806 explicit specialization for that template, the explicit
17807 instantiation has no effect. */
17810 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
17814 No program shall explicitly instantiate any template more
17817 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
17818 instantiation was `extern'. If EXTERN_P then the second is.
17819 These cases are OK. */
17820 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
17822 if (!previous_instantiation_extern_p && !extern_p
17823 && (complain & tf_error))
17824 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
17826 /* If we've already instantiated the template, just return now. */
17827 if (!CLASSTYPE_INTERFACE_ONLY (t))
17831 check_explicit_instantiation_namespace (TYPE_NAME (t));
17832 mark_class_instantiated (t, extern_p);
17840 /* In contrast to implicit instantiation, where only the
17841 declarations, and not the definitions, of members are
17842 instantiated, we have here:
17846 The explicit instantiation of a class template specialization
17847 implies the instantiation of all of its members not
17848 previously explicitly specialized in the translation unit
17849 containing the explicit instantiation.
17851 Of course, we can't instantiate member template classes, since
17852 we don't have any arguments for them. Note that the standard
17853 is unclear on whether the instantiation of the members are
17854 *explicit* instantiations or not. However, the most natural
17855 interpretation is that it should be an explicit instantiation. */
17858 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
17859 if (TREE_CODE (tmp) == FUNCTION_DECL
17860 && DECL_TEMPLATE_INSTANTIATION (tmp))
17861 instantiate_class_member (tmp, extern_p);
17863 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
17864 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
17865 instantiate_class_member (tmp, extern_p);
17867 if (CLASSTYPE_NESTED_UTDS (t))
17868 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
17869 bt_instantiate_type_proc, &storage);
17873 /* Given a function DECL, which is a specialization of TMPL, modify
17874 DECL to be a re-instantiation of TMPL with the same template
17875 arguments. TMPL should be the template into which tsubst'ing
17876 should occur for DECL, not the most general template.
17878 One reason for doing this is a scenario like this:
17881 void f(const T&, int i);
17883 void g() { f(3, 7); }
17886 void f(const T& t, const int i) { }
17888 Note that when the template is first instantiated, with
17889 instantiate_template, the resulting DECL will have no name for the
17890 first parameter, and the wrong type for the second. So, when we go
17891 to instantiate the DECL, we regenerate it. */
17894 regenerate_decl_from_template (tree decl, tree tmpl)
17896 /* The arguments used to instantiate DECL, from the most general
17901 args = DECL_TI_ARGS (decl);
17902 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
17904 /* Make sure that we can see identifiers, and compute access
17906 push_access_scope (decl);
17908 if (TREE_CODE (decl) == FUNCTION_DECL)
17916 args_depth = TMPL_ARGS_DEPTH (args);
17917 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
17918 if (args_depth > parms_depth)
17919 args = get_innermost_template_args (args, parms_depth);
17921 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
17922 args, tf_error, NULL_TREE,
17923 /*defer_ok*/false);
17924 if (specs && specs != error_mark_node)
17925 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
17928 /* Merge parameter declarations. */
17929 decl_parm = skip_artificial_parms_for (decl,
17930 DECL_ARGUMENTS (decl));
17932 = skip_artificial_parms_for (code_pattern,
17933 DECL_ARGUMENTS (code_pattern));
17934 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
17939 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
17940 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
17941 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
17943 parm_type = type_decays_to (parm_type);
17944 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
17945 TREE_TYPE (decl_parm) = parm_type;
17946 attributes = DECL_ATTRIBUTES (pattern_parm);
17947 if (DECL_ATTRIBUTES (decl_parm) != attributes)
17949 DECL_ATTRIBUTES (decl_parm) = attributes;
17950 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
17952 decl_parm = DECL_CHAIN (decl_parm);
17953 pattern_parm = DECL_CHAIN (pattern_parm);
17955 /* Merge any parameters that match with the function parameter
17957 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
17960 tree expanded_types;
17961 /* Expand the TYPE_PACK_EXPANSION that provides the types for
17962 the parameters in this function parameter pack. */
17963 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
17964 args, tf_error, NULL_TREE);
17965 len = TREE_VEC_LENGTH (expanded_types);
17966 for (i = 0; i < len; i++)
17971 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
17972 /* Rename the parameter to include the index. */
17973 DECL_NAME (decl_parm) =
17974 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
17975 parm_type = TREE_VEC_ELT (expanded_types, i);
17976 parm_type = type_decays_to (parm_type);
17977 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
17978 TREE_TYPE (decl_parm) = parm_type;
17979 attributes = DECL_ATTRIBUTES (pattern_parm);
17980 if (DECL_ATTRIBUTES (decl_parm) != attributes)
17982 DECL_ATTRIBUTES (decl_parm) = attributes;
17983 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
17985 decl_parm = DECL_CHAIN (decl_parm);
17988 /* Merge additional specifiers from the CODE_PATTERN. */
17989 if (DECL_DECLARED_INLINE_P (code_pattern)
17990 && !DECL_DECLARED_INLINE_P (decl))
17991 DECL_DECLARED_INLINE_P (decl) = 1;
17993 else if (TREE_CODE (decl) == VAR_DECL)
17995 DECL_INITIAL (decl) =
17996 tsubst_expr (DECL_INITIAL (code_pattern), args,
17997 tf_error, DECL_TI_TEMPLATE (decl),
17998 /*integral_constant_expression_p=*/false);
17999 if (VAR_HAD_UNKNOWN_BOUND (decl))
18000 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
18001 tf_error, DECL_TI_TEMPLATE (decl));
18004 gcc_unreachable ();
18006 pop_access_scope (decl);
18009 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
18010 substituted to get DECL. */
18013 template_for_substitution (tree decl)
18015 tree tmpl = DECL_TI_TEMPLATE (decl);
18017 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
18018 for the instantiation. This is not always the most general
18019 template. Consider, for example:
18022 struct S { template <class U> void f();
18023 template <> void f<int>(); };
18025 and an instantiation of S<double>::f<int>. We want TD to be the
18026 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
18027 while (/* An instantiation cannot have a definition, so we need a
18028 more general template. */
18029 DECL_TEMPLATE_INSTANTIATION (tmpl)
18030 /* We must also deal with friend templates. Given:
18032 template <class T> struct S {
18033 template <class U> friend void f() {};
18036 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
18037 so far as the language is concerned, but that's still
18038 where we get the pattern for the instantiation from. On
18039 other hand, if the definition comes outside the class, say:
18041 template <class T> struct S {
18042 template <class U> friend void f();
18044 template <class U> friend void f() {}
18046 we don't need to look any further. That's what the check for
18047 DECL_INITIAL is for. */
18048 || (TREE_CODE (decl) == FUNCTION_DECL
18049 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
18050 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
18052 /* The present template, TD, should not be a definition. If it
18053 were a definition, we should be using it! Note that we
18054 cannot restructure the loop to just keep going until we find
18055 a template with a definition, since that might go too far if
18056 a specialization was declared, but not defined. */
18057 gcc_assert (TREE_CODE (decl) != VAR_DECL
18058 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
18060 /* Fetch the more general template. */
18061 tmpl = DECL_TI_TEMPLATE (tmpl);
18067 /* Returns true if we need to instantiate this template instance even if we
18068 know we aren't going to emit it.. */
18071 always_instantiate_p (tree decl)
18073 /* We always instantiate inline functions so that we can inline them. An
18074 explicit instantiation declaration prohibits implicit instantiation of
18075 non-inline functions. With high levels of optimization, we would
18076 normally inline non-inline functions -- but we're not allowed to do
18077 that for "extern template" functions. Therefore, we check
18078 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
18079 return ((TREE_CODE (decl) == FUNCTION_DECL
18080 && DECL_DECLARED_INLINE_P (decl))
18081 /* And we need to instantiate static data members so that
18082 their initializers are available in integral constant
18084 || (TREE_CODE (decl) == VAR_DECL
18085 && decl_maybe_constant_var_p (decl)));
18088 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18089 instantiate it now, modifying TREE_TYPE (fn). */
18092 maybe_instantiate_noexcept (tree fn)
18094 tree fntype, spec, noex, clone;
18096 /* Don't instantiate a noexcept-specification from template context. */
18097 if (processing_template_decl)
18100 if (DECL_CLONED_FUNCTION_P (fn))
18101 fn = DECL_CLONED_FUNCTION (fn);
18102 fntype = TREE_TYPE (fn);
18103 spec = TYPE_RAISES_EXCEPTIONS (fntype);
18105 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
18108 noex = TREE_PURPOSE (spec);
18110 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
18112 if (push_tinst_level (fn))
18114 push_access_scope (fn);
18115 push_deferring_access_checks (dk_no_deferred);
18116 input_location = DECL_SOURCE_LOCATION (fn);
18117 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
18118 DEFERRED_NOEXCEPT_ARGS (noex),
18119 tf_warning_or_error, fn,
18120 /*function_p=*/false,
18121 /*integral_constant_expression_p=*/true);
18122 pop_deferring_access_checks ();
18123 pop_access_scope (fn);
18124 pop_tinst_level ();
18125 spec = build_noexcept_spec (noex, tf_warning_or_error);
18126 if (spec == error_mark_node)
18127 spec = noexcept_false_spec;
18130 spec = noexcept_false_spec;
18134 /* This is an implicitly declared function, so NOEX is a list of
18135 other functions to evaluate and merge. */
18137 spec = noexcept_true_spec;
18138 for (elt = noex; elt; elt = OVL_NEXT (elt))
18140 tree fn = OVL_CURRENT (elt);
18142 maybe_instantiate_noexcept (fn);
18143 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
18144 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
18148 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
18150 FOR_EACH_CLONE (clone, fn)
18152 if (TREE_TYPE (clone) == fntype)
18153 TREE_TYPE (clone) = TREE_TYPE (fn);
18155 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
18159 /* Produce the definition of D, a _DECL generated from a template. If
18160 DEFER_OK is nonzero, then we don't have to actually do the
18161 instantiation now; we just have to do it sometime. Normally it is
18162 an error if this is an explicit instantiation but D is undefined.
18163 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
18164 explicitly instantiated class template. */
18167 instantiate_decl (tree d, int defer_ok,
18168 bool expl_inst_class_mem_p)
18170 tree tmpl = DECL_TI_TEMPLATE (d);
18177 bool pattern_defined;
18178 location_t saved_loc = input_location;
18183 /* This function should only be used to instantiate templates for
18184 functions and static member variables. */
18185 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
18186 || TREE_CODE (d) == VAR_DECL);
18188 /* Variables are never deferred; if instantiation is required, they
18189 are instantiated right away. That allows for better code in the
18190 case that an expression refers to the value of the variable --
18191 if the variable has a constant value the referring expression can
18192 take advantage of that fact. */
18193 if (TREE_CODE (d) == VAR_DECL
18194 || decl_function_context (d)
18195 || DECL_DECLARED_CONSTEXPR_P (d))
18198 /* Don't instantiate cloned functions. Instead, instantiate the
18199 functions they cloned. */
18200 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
18201 d = DECL_CLONED_FUNCTION (d);
18203 if (DECL_TEMPLATE_INSTANTIATED (d)
18204 || (TREE_CODE (d) == FUNCTION_DECL
18205 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
18206 || DECL_TEMPLATE_SPECIALIZATION (d))
18207 /* D has already been instantiated or explicitly specialized, so
18208 there's nothing for us to do here.
18210 It might seem reasonable to check whether or not D is an explicit
18211 instantiation, and, if so, stop here. But when an explicit
18212 instantiation is deferred until the end of the compilation,
18213 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
18214 the instantiation. */
18217 /* Check to see whether we know that this template will be
18218 instantiated in some other file, as with "extern template"
18220 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
18222 /* In general, we do not instantiate such templates. */
18223 if (external_p && !always_instantiate_p (d))
18226 gen_tmpl = most_general_template (tmpl);
18227 gen_args = DECL_TI_ARGS (d);
18229 if (tmpl != gen_tmpl)
18230 /* We should already have the extra args. */
18231 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
18232 == TMPL_ARGS_DEPTH (gen_args));
18233 /* And what's in the hash table should match D. */
18234 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
18235 || spec == NULL_TREE);
18237 /* This needs to happen before any tsubsting. */
18238 if (! push_tinst_level (d))
18241 timevar_push (TV_TEMPLATE_INST);
18243 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
18244 for the instantiation. */
18245 td = template_for_substitution (d);
18246 code_pattern = DECL_TEMPLATE_RESULT (td);
18248 /* We should never be trying to instantiate a member of a class
18249 template or partial specialization. */
18250 gcc_assert (d != code_pattern);
18252 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
18253 || DECL_TEMPLATE_SPECIALIZATION (td))
18254 /* In the case of a friend template whose definition is provided
18255 outside the class, we may have too many arguments. Drop the
18256 ones we don't need. The same is true for specializations. */
18257 args = get_innermost_template_args
18258 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
18262 if (TREE_CODE (d) == FUNCTION_DECL)
18263 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
18264 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
18266 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
18268 /* We may be in the middle of deferred access check. Disable it now. */
18269 push_deferring_access_checks (dk_no_deferred);
18271 /* Unless an explicit instantiation directive has already determined
18272 the linkage of D, remember that a definition is available for
18274 if (pattern_defined
18275 && !DECL_INTERFACE_KNOWN (d)
18276 && !DECL_NOT_REALLY_EXTERN (d))
18277 mark_definable (d);
18279 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
18280 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
18281 input_location = DECL_SOURCE_LOCATION (d);
18283 /* If D is a member of an explicitly instantiated class template,
18284 and no definition is available, treat it like an implicit
18286 if (!pattern_defined && expl_inst_class_mem_p
18287 && DECL_EXPLICIT_INSTANTIATION (d))
18289 /* Leave linkage flags alone on instantiations with anonymous
18291 if (TREE_PUBLIC (d))
18293 DECL_NOT_REALLY_EXTERN (d) = 0;
18294 DECL_INTERFACE_KNOWN (d) = 0;
18296 SET_DECL_IMPLICIT_INSTANTIATION (d);
18299 if (TREE_CODE (d) == FUNCTION_DECL)
18300 maybe_instantiate_noexcept (d);
18302 /* Recheck the substitutions to obtain any warning messages
18303 about ignoring cv qualifiers. Don't do this for artificial decls,
18304 as it breaks the context-sensitive substitution for lambda op(). */
18305 if (!defer_ok && !DECL_ARTIFICIAL (d))
18307 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
18308 tree type = TREE_TYPE (gen);
18310 /* Make sure that we can see identifiers, and compute access
18311 correctly. D is already the target FUNCTION_DECL with the
18313 push_access_scope (d);
18315 if (TREE_CODE (gen) == FUNCTION_DECL)
18317 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
18318 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
18319 d, /*defer_ok*/true);
18320 /* Don't simply tsubst the function type, as that will give
18321 duplicate warnings about poor parameter qualifications.
18322 The function arguments are the same as the decl_arguments
18323 without the top level cv qualifiers. */
18324 type = TREE_TYPE (type);
18326 tsubst (type, gen_args, tf_warning_or_error, d);
18328 pop_access_scope (d);
18331 /* Defer all other templates, unless we have been explicitly
18332 forbidden from doing so. */
18333 if (/* If there is no definition, we cannot instantiate the
18336 /* If it's OK to postpone instantiation, do so. */
18338 /* If this is a static data member that will be defined
18339 elsewhere, we don't want to instantiate the entire data
18340 member, but we do want to instantiate the initializer so that
18341 we can substitute that elsewhere. */
18342 || (external_p && TREE_CODE (d) == VAR_DECL))
18344 /* The definition of the static data member is now required so
18345 we must substitute the initializer. */
18346 if (TREE_CODE (d) == VAR_DECL
18347 && !DECL_INITIAL (d)
18348 && DECL_INITIAL (code_pattern))
18352 bool const_init = false;
18354 ns = decl_namespace_context (d);
18355 push_nested_namespace (ns);
18356 push_nested_class (DECL_CONTEXT (d));
18357 init = tsubst_expr (DECL_INITIAL (code_pattern),
18359 tf_warning_or_error, NULL_TREE,
18360 /*integral_constant_expression_p=*/false);
18361 /* Make sure the initializer is still constant, in case of
18362 circular dependency (template/instantiate6.C). */
18364 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
18365 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
18366 /*asmspec_tree=*/NULL_TREE,
18367 LOOKUP_ONLYCONVERTING);
18368 pop_nested_class ();
18369 pop_nested_namespace (ns);
18372 /* We restore the source position here because it's used by
18373 add_pending_template. */
18374 input_location = saved_loc;
18376 if (at_eof && !pattern_defined
18377 && DECL_EXPLICIT_INSTANTIATION (d)
18378 && DECL_NOT_REALLY_EXTERN (d))
18381 The definition of a non-exported function template, a
18382 non-exported member function template, or a non-exported
18383 member function or static data member of a class template
18384 shall be present in every translation unit in which it is
18385 explicitly instantiated. */
18386 permerror (input_location, "explicit instantiation of %qD "
18387 "but no definition available", d);
18389 /* If we're in unevaluated context, we just wanted to get the
18390 constant value; this isn't an odr use, so don't queue
18391 a full instantiation. */
18392 if (cp_unevaluated_operand != 0)
18394 /* ??? Historically, we have instantiated inline functions, even
18395 when marked as "extern template". */
18396 if (!(external_p && TREE_CODE (d) == VAR_DECL))
18397 add_pending_template (d);
18400 /* Tell the repository that D is available in this translation unit
18401 -- and see if it is supposed to be instantiated here. */
18402 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
18404 /* In a PCH file, despite the fact that the repository hasn't
18405 requested instantiation in the PCH it is still possible that
18406 an instantiation will be required in a file that includes the
18409 add_pending_template (d);
18410 /* Instantiate inline functions so that the inliner can do its
18411 job, even though we'll not be emitting a copy of this
18413 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
18417 fn_context = decl_function_context (d);
18418 nested = (current_function_decl != NULL_TREE);
18420 push_to_top_level ();
18422 push_function_context ();
18424 /* Mark D as instantiated so that recursive calls to
18425 instantiate_decl do not try to instantiate it again. */
18426 DECL_TEMPLATE_INSTANTIATED (d) = 1;
18428 /* Regenerate the declaration in case the template has been modified
18429 by a subsequent redeclaration. */
18430 regenerate_decl_from_template (d, td);
18432 /* We already set the file and line above. Reset them now in case
18433 they changed as a result of calling regenerate_decl_from_template. */
18434 input_location = DECL_SOURCE_LOCATION (d);
18436 if (TREE_CODE (d) == VAR_DECL)
18439 bool const_init = false;
18441 /* Clear out DECL_RTL; whatever was there before may not be right
18442 since we've reset the type of the declaration. */
18443 SET_DECL_RTL (d, NULL);
18444 DECL_IN_AGGR_P (d) = 0;
18446 /* The initializer is placed in DECL_INITIAL by
18447 regenerate_decl_from_template so we don't need to
18448 push/pop_access_scope again here. Pull it out so that
18449 cp_finish_decl can process it. */
18450 init = DECL_INITIAL (d);
18451 DECL_INITIAL (d) = NULL_TREE;
18452 DECL_INITIALIZED_P (d) = 0;
18454 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18455 initializer. That function will defer actual emission until
18456 we have a chance to determine linkage. */
18457 DECL_EXTERNAL (d) = 0;
18459 /* Enter the scope of D so that access-checking works correctly. */
18460 push_nested_class (DECL_CONTEXT (d));
18461 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
18462 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
18463 pop_nested_class ();
18465 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
18466 synthesize_method (d);
18467 else if (TREE_CODE (d) == FUNCTION_DECL)
18469 htab_t saved_local_specializations;
18474 /* Save away the current list, in case we are instantiating one
18475 template from within the body of another. */
18476 saved_local_specializations = local_specializations;
18478 /* Set up the list of local specializations. */
18479 local_specializations = htab_create (37,
18480 hash_local_specialization,
18481 eq_local_specializations,
18484 /* Set up context. */
18485 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
18487 /* Create substitution entries for the parameters. */
18488 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
18489 tmpl_parm = DECL_ARGUMENTS (subst_decl);
18490 spec_parm = DECL_ARGUMENTS (d);
18491 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
18493 register_local_specialization (spec_parm, tmpl_parm);
18494 spec_parm = skip_artificial_parms_for (d, spec_parm);
18495 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
18497 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
18499 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm))
18501 register_local_specialization (spec_parm, tmpl_parm);
18502 spec_parm = DECL_CHAIN (spec_parm);
18506 /* Register the (value) argument pack as a specialization of
18507 TMPL_PARM, then move on. */
18508 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
18509 register_local_specialization (argpack, tmpl_parm);
18512 gcc_assert (!spec_parm);
18514 /* Substitute into the body of the function. */
18515 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
18516 tf_warning_or_error, tmpl,
18517 /*integral_constant_expression_p=*/false);
18519 /* Set the current input_location to the end of the function
18520 so that finish_function knows where we are. */
18521 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
18523 /* We don't need the local specializations any more. */
18524 htab_delete (local_specializations);
18525 local_specializations = saved_local_specializations;
18527 /* Finish the function. */
18528 d = finish_function (0);
18529 expand_or_defer_fn (d);
18532 /* We're not deferring instantiation any more. */
18533 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
18536 pop_from_top_level ();
18538 pop_function_context ();
18541 input_location = saved_loc;
18542 pop_deferring_access_checks ();
18543 pop_tinst_level ();
18545 timevar_pop (TV_TEMPLATE_INST);
18550 /* Run through the list of templates that we wish we could
18551 instantiate, and instantiate any we can. RETRIES is the
18552 number of times we retry pending template instantiation. */
18555 instantiate_pending_templates (int retries)
18558 location_t saved_loc = input_location;
18560 /* Instantiating templates may trigger vtable generation. This in turn
18561 may require further template instantiations. We place a limit here
18562 to avoid infinite loop. */
18563 if (pending_templates && retries >= max_tinst_depth)
18565 tree decl = pending_templates->tinst->decl;
18567 error ("template instantiation depth exceeds maximum of %d"
18568 " instantiating %q+D, possibly from virtual table generation"
18569 " (use -ftemplate-depth= to increase the maximum)",
18570 max_tinst_depth, decl);
18571 if (TREE_CODE (decl) == FUNCTION_DECL)
18572 /* Pretend that we defined it. */
18573 DECL_INITIAL (decl) = error_mark_node;
18579 struct pending_template **t = &pending_templates;
18580 struct pending_template *last = NULL;
18584 tree instantiation = reopen_tinst_level ((*t)->tinst);
18585 bool complete = false;
18587 if (TYPE_P (instantiation))
18591 if (!COMPLETE_TYPE_P (instantiation))
18593 instantiate_class_template (instantiation);
18594 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
18595 for (fn = TYPE_METHODS (instantiation);
18597 fn = TREE_CHAIN (fn))
18598 if (! DECL_ARTIFICIAL (fn))
18599 instantiate_decl (fn,
18601 /*expl_inst_class_mem_p=*/false);
18602 if (COMPLETE_TYPE_P (instantiation))
18606 complete = COMPLETE_TYPE_P (instantiation);
18610 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
18611 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
18614 = instantiate_decl (instantiation,
18616 /*expl_inst_class_mem_p=*/false);
18617 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
18621 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
18622 || DECL_TEMPLATE_INSTANTIATED (instantiation));
18626 /* If INSTANTIATION has been instantiated, then we don't
18627 need to consider it again in the future. */
18635 current_tinst_level = NULL;
18637 last_pending_template = last;
18639 while (reconsider);
18641 input_location = saved_loc;
18644 /* Substitute ARGVEC into T, which is a list of initializers for
18645 either base class or a non-static data member. The TREE_PURPOSEs
18646 are DECLs, and the TREE_VALUEs are the initializer values. Used by
18647 instantiate_decl. */
18650 tsubst_initializer_list (tree t, tree argvec)
18652 tree inits = NULL_TREE;
18654 for (; t; t = TREE_CHAIN (t))
18658 tree expanded_bases = NULL_TREE;
18659 tree expanded_arguments = NULL_TREE;
18662 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
18667 /* Expand the base class expansion type into separate base
18669 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
18670 tf_warning_or_error,
18672 if (expanded_bases == error_mark_node)
18675 /* We'll be building separate TREE_LISTs of arguments for
18677 len = TREE_VEC_LENGTH (expanded_bases);
18678 expanded_arguments = make_tree_vec (len);
18679 for (i = 0; i < len; i++)
18680 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
18682 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
18683 expand each argument in the TREE_VALUE of t. */
18684 expr = make_node (EXPR_PACK_EXPANSION);
18685 PACK_EXPANSION_LOCAL_P (expr) = true;
18686 PACK_EXPANSION_PARAMETER_PACKS (expr) =
18687 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
18689 if (TREE_VALUE (t) == void_type_node)
18690 /* VOID_TYPE_NODE is used to indicate
18691 value-initialization. */
18693 for (i = 0; i < len; i++)
18694 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
18698 /* Substitute parameter packs into each argument in the
18700 in_base_initializer = 1;
18701 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
18703 tree expanded_exprs;
18705 /* Expand the argument. */
18706 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
18708 = tsubst_pack_expansion (expr, argvec,
18709 tf_warning_or_error,
18711 if (expanded_exprs == error_mark_node)
18714 /* Prepend each of the expanded expressions to the
18715 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
18716 for (i = 0; i < len; i++)
18718 TREE_VEC_ELT (expanded_arguments, i) =
18719 tree_cons (NULL_TREE,
18720 TREE_VEC_ELT (expanded_exprs, i),
18721 TREE_VEC_ELT (expanded_arguments, i));
18724 in_base_initializer = 0;
18726 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
18727 since we built them backwards. */
18728 for (i = 0; i < len; i++)
18730 TREE_VEC_ELT (expanded_arguments, i) =
18731 nreverse (TREE_VEC_ELT (expanded_arguments, i));
18736 for (i = 0; i < len; ++i)
18738 if (expanded_bases)
18740 decl = TREE_VEC_ELT (expanded_bases, i);
18741 decl = expand_member_init (decl);
18742 init = TREE_VEC_ELT (expanded_arguments, i);
18747 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
18748 tf_warning_or_error, NULL_TREE);
18750 decl = expand_member_init (decl);
18751 if (decl && !DECL_P (decl))
18752 in_base_initializer = 1;
18754 init = TREE_VALUE (t);
18756 if (init != void_type_node)
18757 init = tsubst_expr (init, argvec,
18758 tf_warning_or_error, NULL_TREE,
18759 /*integral_constant_expression_p=*/false);
18760 if (init == NULL_TREE && tmp != NULL_TREE)
18761 /* If we had an initializer but it instantiated to nothing,
18762 value-initialize the object. This will only occur when
18763 the initializer was a pack expansion where the parameter
18764 packs used in that expansion were of length zero. */
18765 init = void_type_node;
18766 in_base_initializer = 0;
18771 init = build_tree_list (decl, init);
18772 TREE_CHAIN (init) = inits;
18780 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
18783 set_current_access_from_decl (tree decl)
18785 if (TREE_PRIVATE (decl))
18786 current_access_specifier = access_private_node;
18787 else if (TREE_PROTECTED (decl))
18788 current_access_specifier = access_protected_node;
18790 current_access_specifier = access_public_node;
18793 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
18794 is the instantiation (which should have been created with
18795 start_enum) and ARGS are the template arguments to use. */
18798 tsubst_enum (tree tag, tree newtag, tree args)
18802 if (SCOPED_ENUM_P (newtag))
18803 begin_scope (sk_scoped_enum, newtag);
18805 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
18810 decl = TREE_VALUE (e);
18811 /* Note that in a template enum, the TREE_VALUE is the
18812 CONST_DECL, not the corresponding INTEGER_CST. */
18813 value = tsubst_expr (DECL_INITIAL (decl),
18814 args, tf_warning_or_error, NULL_TREE,
18815 /*integral_constant_expression_p=*/true);
18817 /* Give this enumeration constant the correct access. */
18818 set_current_access_from_decl (decl);
18820 /* Actually build the enumerator itself. */
18822 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
18825 if (SCOPED_ENUM_P (newtag))
18828 finish_enum_value_list (newtag);
18829 finish_enum (newtag);
18831 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
18832 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
18835 /* DECL is a FUNCTION_DECL that is a template specialization. Return
18836 its type -- but without substituting the innermost set of template
18837 arguments. So, innermost set of template parameters will appear in
18841 get_mostly_instantiated_function_type (tree decl)
18849 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
18850 targs = DECL_TI_ARGS (decl);
18851 tparms = DECL_TEMPLATE_PARMS (tmpl);
18852 parm_depth = TMPL_PARMS_DEPTH (tparms);
18854 /* There should be as many levels of arguments as there are levels
18856 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
18858 fn_type = TREE_TYPE (tmpl);
18860 if (parm_depth == 1)
18861 /* No substitution is necessary. */
18868 /* Replace the innermost level of the TARGS with NULL_TREEs to
18869 let tsubst know not to substitute for those parameters. */
18870 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
18871 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
18872 SET_TMPL_ARGS_LEVEL (partial_args, i,
18873 TMPL_ARGS_LEVEL (targs, i));
18874 SET_TMPL_ARGS_LEVEL (partial_args,
18875 TMPL_ARGS_DEPTH (targs),
18876 make_tree_vec (DECL_NTPARMS (tmpl)));
18878 /* Make sure that we can see identifiers, and compute access
18880 push_access_scope (decl);
18882 ++processing_template_decl;
18883 /* Now, do the (partial) substitution to figure out the
18884 appropriate function type. */
18885 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
18886 --processing_template_decl;
18888 /* Substitute into the template parameters to obtain the real
18889 innermost set of parameters. This step is important if the
18890 innermost set of template parameters contains value
18891 parameters whose types depend on outer template parameters. */
18892 TREE_VEC_LENGTH (partial_args)--;
18893 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
18895 pop_access_scope (decl);
18901 /* Return truthvalue if we're processing a template different from
18902 the last one involved in diagnostics. */
18904 problematic_instantiation_changed (void)
18906 return current_tinst_level != last_error_tinst_level;
18909 /* Remember current template involved in diagnostics. */
18911 record_last_problematic_instantiation (void)
18913 last_error_tinst_level = current_tinst_level;
18916 struct tinst_level *
18917 current_instantiation (void)
18919 return current_tinst_level;
18922 /* [temp.param] Check that template non-type parm TYPE is of an allowable
18923 type. Return zero for ok, nonzero for disallowed. Issue error and
18924 warning messages under control of COMPLAIN. */
18927 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
18929 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
18931 else if (POINTER_TYPE_P (type))
18933 else if (TYPE_PTR_TO_MEMBER_P (type))
18935 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
18937 else if (TREE_CODE (type) == TYPENAME_TYPE)
18939 else if (TREE_CODE (type) == DECLTYPE_TYPE)
18941 else if (TREE_CODE (type) == NULLPTR_TYPE)
18944 if (complain & tf_error)
18946 if (type == error_mark_node)
18947 inform (input_location, "invalid template non-type parameter");
18949 error ("%q#T is not a valid type for a template non-type parameter",
18955 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
18956 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
18959 dependent_type_p_r (tree type)
18965 A type is dependent if it is:
18967 -- a template parameter. Template template parameters are types
18968 for us (since TYPE_P holds true for them) so we handle
18970 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18971 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
18973 /* -- a qualified-id with a nested-name-specifier which contains a
18974 class-name that names a dependent type or whose unqualified-id
18975 names a dependent type. */
18976 if (TREE_CODE (type) == TYPENAME_TYPE)
18978 /* -- a cv-qualified type where the cv-unqualified type is
18980 type = TYPE_MAIN_VARIANT (type);
18981 /* -- a compound type constructed from any dependent type. */
18982 if (TYPE_PTR_TO_MEMBER_P (type))
18983 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
18984 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
18986 else if (TREE_CODE (type) == POINTER_TYPE
18987 || TREE_CODE (type) == REFERENCE_TYPE)
18988 return dependent_type_p (TREE_TYPE (type));
18989 else if (TREE_CODE (type) == FUNCTION_TYPE
18990 || TREE_CODE (type) == METHOD_TYPE)
18994 if (dependent_type_p (TREE_TYPE (type)))
18996 for (arg_type = TYPE_ARG_TYPES (type);
18998 arg_type = TREE_CHAIN (arg_type))
18999 if (dependent_type_p (TREE_VALUE (arg_type)))
19003 /* -- an array type constructed from any dependent type or whose
19004 size is specified by a constant expression that is
19007 We checked for type- and value-dependence of the bounds in
19008 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
19009 if (TREE_CODE (type) == ARRAY_TYPE)
19011 if (TYPE_DOMAIN (type)
19012 && dependent_type_p (TYPE_DOMAIN (type)))
19014 return dependent_type_p (TREE_TYPE (type));
19017 /* -- a template-id in which either the template name is a template
19019 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19021 /* ... or any of the template arguments is a dependent type or
19022 an expression that is type-dependent or value-dependent. */
19023 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
19024 && (any_dependent_template_arguments_p
19025 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
19028 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
19029 dependent; if the argument of the `typeof' expression is not
19030 type-dependent, then it should already been have resolved. */
19031 if (TREE_CODE (type) == TYPEOF_TYPE
19032 || TREE_CODE (type) == DECLTYPE_TYPE
19033 || TREE_CODE (type) == UNDERLYING_TYPE)
19036 /* A template argument pack is dependent if any of its packed
19038 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
19040 tree args = ARGUMENT_PACK_ARGS (type);
19041 int i, len = TREE_VEC_LENGTH (args);
19042 for (i = 0; i < len; ++i)
19043 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
19047 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
19048 be template parameters. */
19049 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
19052 /* The standard does not specifically mention types that are local
19053 to template functions or local classes, but they should be
19054 considered dependent too. For example:
19056 template <int I> void f() {
19061 The size of `E' cannot be known until the value of `I' has been
19062 determined. Therefore, `E' must be considered dependent. */
19063 scope = TYPE_CONTEXT (type);
19064 if (scope && TYPE_P (scope))
19065 return dependent_type_p (scope);
19066 /* Don't use type_dependent_expression_p here, as it can lead
19067 to infinite recursion trying to determine whether a lambda
19068 nested in a lambda is dependent (c++/47687). */
19069 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
19070 && DECL_LANG_SPECIFIC (scope)
19071 && DECL_TEMPLATE_INFO (scope)
19072 && (any_dependent_template_arguments_p
19073 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
19076 /* Other types are non-dependent. */
19080 /* Returns TRUE if TYPE is dependent, in the sense of
19081 [temp.dep.type]. Note that a NULL type is considered dependent. */
19084 dependent_type_p (tree type)
19086 /* If there are no template parameters in scope, then there can't be
19087 any dependent types. */
19088 if (!processing_template_decl)
19090 /* If we are not processing a template, then nobody should be
19091 providing us with a dependent type. */
19093 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
19097 /* If the type is NULL, we have not computed a type for the entity
19098 in question; in that case, the type is dependent. */
19102 /* Erroneous types can be considered non-dependent. */
19103 if (type == error_mark_node)
19106 /* If we have not already computed the appropriate value for TYPE,
19108 if (!TYPE_DEPENDENT_P_VALID (type))
19110 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
19111 TYPE_DEPENDENT_P_VALID (type) = 1;
19114 return TYPE_DEPENDENT_P (type);
19117 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19118 lookup. In other words, a dependent type that is not the current
19122 dependent_scope_p (tree scope)
19124 return (scope && TYPE_P (scope) && dependent_type_p (scope)
19125 && !currently_open_class (scope));
19128 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
19129 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
19132 /* Note that this predicate is not appropriate for general expressions;
19133 only constant expressions (that satisfy potential_constant_expression)
19134 can be tested for value dependence.
19136 We should really also have a predicate for "instantiation-dependent".
19138 fold_non_dependent_expr: fold if constant and not type-dependent and not value-dependent.
19139 (what about instantiation-dependent constant-expressions?)
19140 is_late_template_attribute: defer if instantiation-dependent.
19141 compute_array_index_type: proceed if constant and not t- or v-dependent
19142 if instantiation-dependent, need to remember full expression
19143 uses_template_parms: FIXME - need to audit callers
19144 tsubst_decl [function_decl]: Why is this using value_dependent_expression_p?
19145 dependent_type_p [array_type]: dependent if index type is dependent
19147 static_assert - instantiation-dependent */
19150 value_dependent_expression_p (tree expression)
19152 if (!processing_template_decl)
19155 /* A name declared with a dependent type. */
19156 if (DECL_P (expression) && type_dependent_expression_p (expression))
19159 switch (TREE_CODE (expression))
19161 case IDENTIFIER_NODE:
19162 /* A name that has not been looked up -- must be dependent. */
19165 case TEMPLATE_PARM_INDEX:
19166 /* A non-type template parm. */
19170 /* A non-type template parm. */
19171 if (DECL_TEMPLATE_PARM_P (expression))
19173 return value_dependent_expression_p (DECL_INITIAL (expression));
19176 /* A constant with literal type and is initialized
19177 with an expression that is value-dependent.
19179 Note that a non-dependent parenthesized initializer will have
19180 already been replaced with its constant value, so if we see
19181 a TREE_LIST it must be dependent. */
19182 if (DECL_INITIAL (expression)
19183 && decl_constant_var_p (expression)
19184 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
19185 || value_dependent_expression_p (DECL_INITIAL (expression))))
19189 case DYNAMIC_CAST_EXPR:
19190 case STATIC_CAST_EXPR:
19191 case CONST_CAST_EXPR:
19192 case REINTERPRET_CAST_EXPR:
19194 /* These expressions are value-dependent if the type to which
19195 the cast occurs is dependent or the expression being casted
19196 is value-dependent. */
19198 tree type = TREE_TYPE (expression);
19200 if (dependent_type_p (type))
19203 /* A functional cast has a list of operands. */
19204 expression = TREE_OPERAND (expression, 0);
19207 /* If there are no operands, it must be an expression such
19208 as "int()". This should not happen for aggregate types
19209 because it would form non-constant expressions. */
19210 gcc_assert (cxx_dialect >= cxx0x
19211 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
19216 if (TREE_CODE (expression) == TREE_LIST)
19217 return any_value_dependent_elements_p (expression);
19219 return value_dependent_expression_p (expression);
19225 /* A `sizeof' expression is value-dependent if the operand is
19226 type-dependent or is a pack expansion. */
19227 expression = TREE_OPERAND (expression, 0);
19228 if (PACK_EXPANSION_P (expression))
19230 else if (TYPE_P (expression))
19231 return dependent_type_p (expression);
19232 return type_dependent_expression_p (expression);
19234 case AT_ENCODE_EXPR:
19235 /* An 'encode' expression is value-dependent if the operand is
19237 expression = TREE_OPERAND (expression, 0);
19238 return dependent_type_p (expression);
19240 case NOEXCEPT_EXPR:
19241 expression = TREE_OPERAND (expression, 0);
19242 return type_dependent_expression_p (expression);
19246 tree name = TREE_OPERAND (expression, 1);
19247 return value_dependent_expression_p (name);
19250 case COMPONENT_REF:
19251 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
19252 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
19254 case NONTYPE_ARGUMENT_PACK:
19255 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
19256 is value-dependent. */
19258 tree values = ARGUMENT_PACK_ARGS (expression);
19259 int i, len = TREE_VEC_LENGTH (values);
19261 for (i = 0; i < len; ++i)
19262 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
19270 tree type2 = TRAIT_EXPR_TYPE2 (expression);
19271 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
19272 || (type2 ? dependent_type_p (type2) : false));
19276 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
19277 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
19280 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
19281 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
19285 tree op = TREE_OPERAND (expression, 0);
19286 return (value_dependent_expression_p (op)
19287 || has_value_dependent_address (op));
19292 tree fn = get_callee_fndecl (expression);
19294 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
19296 nargs = call_expr_nargs (expression);
19297 for (i = 0; i < nargs; ++i)
19299 tree op = CALL_EXPR_ARG (expression, i);
19300 /* In a call to a constexpr member function, look through the
19301 implicit ADDR_EXPR on the object argument so that it doesn't
19302 cause the call to be considered value-dependent. We also
19303 look through it in potential_constant_expression. */
19304 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
19305 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
19306 && TREE_CODE (op) == ADDR_EXPR)
19307 op = TREE_OPERAND (op, 0);
19308 if (value_dependent_expression_p (op))
19314 case TEMPLATE_ID_EXPR:
19315 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19317 return type_dependent_expression_p (expression);
19323 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
19324 if (value_dependent_expression_p (val))
19330 /* Treat a GNU statement expression as dependent to avoid crashing
19331 under fold_non_dependent_expr; it can't be constant. */
19335 /* A constant expression is value-dependent if any subexpression is
19336 value-dependent. */
19337 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
19339 case tcc_reference:
19341 case tcc_comparison:
19343 case tcc_expression:
19346 int i, len = cp_tree_operand_length (expression);
19348 for (i = 0; i < len; i++)
19350 tree t = TREE_OPERAND (expression, i);
19352 /* In some cases, some of the operands may be missing.l
19353 (For example, in the case of PREDECREMENT_EXPR, the
19354 amount to increment by may be missing.) That doesn't
19355 make the expression dependent. */
19356 if (t && value_dependent_expression_p (t))
19367 /* The expression is not value-dependent. */
19371 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19372 [temp.dep.expr]. Note that an expression with no type is
19373 considered dependent. Other parts of the compiler arrange for an
19374 expression with type-dependent subexpressions to have no type, so
19375 this function doesn't have to be fully recursive. */
19378 type_dependent_expression_p (tree expression)
19380 if (!processing_template_decl)
19383 if (expression == error_mark_node)
19386 /* An unresolved name is always dependent. */
19387 if (TREE_CODE (expression) == IDENTIFIER_NODE
19388 || TREE_CODE (expression) == USING_DECL)
19391 /* Some expression forms are never type-dependent. */
19392 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
19393 || TREE_CODE (expression) == SIZEOF_EXPR
19394 || TREE_CODE (expression) == ALIGNOF_EXPR
19395 || TREE_CODE (expression) == AT_ENCODE_EXPR
19396 || TREE_CODE (expression) == NOEXCEPT_EXPR
19397 || TREE_CODE (expression) == TRAIT_EXPR
19398 || TREE_CODE (expression) == TYPEID_EXPR
19399 || TREE_CODE (expression) == DELETE_EXPR
19400 || TREE_CODE (expression) == VEC_DELETE_EXPR
19401 || TREE_CODE (expression) == THROW_EXPR)
19404 /* The types of these expressions depends only on the type to which
19405 the cast occurs. */
19406 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
19407 || TREE_CODE (expression) == STATIC_CAST_EXPR
19408 || TREE_CODE (expression) == CONST_CAST_EXPR
19409 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
19410 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
19411 || TREE_CODE (expression) == CAST_EXPR)
19412 return dependent_type_p (TREE_TYPE (expression));
19414 /* The types of these expressions depends only on the type created
19415 by the expression. */
19416 if (TREE_CODE (expression) == NEW_EXPR
19417 || TREE_CODE (expression) == VEC_NEW_EXPR)
19419 /* For NEW_EXPR tree nodes created inside a template, either
19420 the object type itself or a TREE_LIST may appear as the
19422 tree type = TREE_OPERAND (expression, 1);
19423 if (TREE_CODE (type) == TREE_LIST)
19424 /* This is an array type. We need to check array dimensions
19426 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
19427 || value_dependent_expression_p
19428 (TREE_OPERAND (TREE_VALUE (type), 1));
19430 return dependent_type_p (type);
19433 if (TREE_CODE (expression) == SCOPE_REF)
19435 tree scope = TREE_OPERAND (expression, 0);
19436 tree name = TREE_OPERAND (expression, 1);
19438 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19439 contains an identifier associated by name lookup with one or more
19440 declarations declared with a dependent type, or...a
19441 nested-name-specifier or qualified-id that names a member of an
19442 unknown specialization. */
19443 return (type_dependent_expression_p (name)
19444 || dependent_scope_p (scope));
19447 if (TREE_CODE (expression) == FUNCTION_DECL
19448 && DECL_LANG_SPECIFIC (expression)
19449 && DECL_TEMPLATE_INFO (expression)
19450 && (any_dependent_template_arguments_p
19451 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
19454 if (TREE_CODE (expression) == TEMPLATE_DECL
19455 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
19458 if (TREE_CODE (expression) == STMT_EXPR)
19459 expression = stmt_expr_value_expr (expression);
19461 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
19466 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
19468 if (type_dependent_expression_p (elt))
19474 /* A static data member of the current instantiation with incomplete
19475 array type is type-dependent, as the definition and specializations
19476 can have different bounds. */
19477 if (TREE_CODE (expression) == VAR_DECL
19478 && DECL_CLASS_SCOPE_P (expression)
19479 && dependent_type_p (DECL_CONTEXT (expression))
19480 && VAR_HAD_UNKNOWN_BOUND (expression))
19483 if (TREE_TYPE (expression) == unknown_type_node)
19485 if (TREE_CODE (expression) == ADDR_EXPR)
19486 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
19487 if (TREE_CODE (expression) == COMPONENT_REF
19488 || TREE_CODE (expression) == OFFSET_REF)
19490 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
19492 expression = TREE_OPERAND (expression, 1);
19493 if (TREE_CODE (expression) == IDENTIFIER_NODE)
19496 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19497 if (TREE_CODE (expression) == SCOPE_REF)
19500 if (BASELINK_P (expression))
19501 expression = BASELINK_FUNCTIONS (expression);
19503 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
19505 if (any_dependent_template_arguments_p
19506 (TREE_OPERAND (expression, 1)))
19508 expression = TREE_OPERAND (expression, 0);
19510 gcc_assert (TREE_CODE (expression) == OVERLOAD
19511 || TREE_CODE (expression) == FUNCTION_DECL);
19515 if (type_dependent_expression_p (OVL_CURRENT (expression)))
19517 expression = OVL_NEXT (expression);
19522 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
19524 return (dependent_type_p (TREE_TYPE (expression)));
19527 /* Like type_dependent_expression_p, but it also works while not processing
19528 a template definition, i.e. during substitution or mangling. */
19531 type_dependent_expression_p_push (tree expr)
19534 ++processing_template_decl;
19535 b = type_dependent_expression_p (expr);
19536 --processing_template_decl;
19540 /* Returns TRUE if ARGS contains a type-dependent expression. */
19543 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
19548 FOR_EACH_VEC_ELT (tree, args, i, arg)
19550 if (type_dependent_expression_p (arg))
19556 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19557 expressions) contains any type-dependent expressions. */
19560 any_type_dependent_elements_p (const_tree list)
19562 for (; list; list = TREE_CHAIN (list))
19563 if (type_dependent_expression_p (TREE_VALUE (list)))
19569 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19570 expressions) contains any value-dependent expressions. */
19573 any_value_dependent_elements_p (const_tree list)
19575 for (; list; list = TREE_CHAIN (list))
19576 if (value_dependent_expression_p (TREE_VALUE (list)))
19582 /* Returns TRUE if the ARG (a template argument) is dependent. */
19585 dependent_template_arg_p (tree arg)
19587 if (!processing_template_decl)
19590 /* Assume a template argument that was wrongly written by the user
19591 is dependent. This is consistent with what
19592 any_dependent_template_arguments_p [that calls this function]
19594 if (!arg || arg == error_mark_node)
19597 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
19598 arg = ARGUMENT_PACK_SELECT_ARG (arg);
19600 if (TREE_CODE (arg) == TEMPLATE_DECL
19601 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
19602 return dependent_template_p (arg);
19603 else if (ARGUMENT_PACK_P (arg))
19605 tree args = ARGUMENT_PACK_ARGS (arg);
19606 int i, len = TREE_VEC_LENGTH (args);
19607 for (i = 0; i < len; ++i)
19609 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
19615 else if (TYPE_P (arg))
19616 return dependent_type_p (arg);
19618 return (type_dependent_expression_p (arg)
19619 || value_dependent_expression_p (arg));
19622 /* Returns true if ARGS (a collection of template arguments) contains
19623 any types that require structural equality testing. */
19626 any_template_arguments_need_structural_equality_p (tree args)
19633 if (args == error_mark_node)
19636 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
19638 tree level = TMPL_ARGS_LEVEL (args, i + 1);
19639 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
19641 tree arg = TREE_VEC_ELT (level, j);
19642 tree packed_args = NULL_TREE;
19645 if (ARGUMENT_PACK_P (arg))
19647 /* Look inside the argument pack. */
19648 packed_args = ARGUMENT_PACK_ARGS (arg);
19649 len = TREE_VEC_LENGTH (packed_args);
19652 for (k = 0; k < len; ++k)
19655 arg = TREE_VEC_ELT (packed_args, k);
19657 if (error_operand_p (arg))
19659 else if (TREE_CODE (arg) == TEMPLATE_DECL
19660 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
19662 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
19664 else if (!TYPE_P (arg) && TREE_TYPE (arg)
19665 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
19674 /* Returns true if ARGS (a collection of template arguments) contains
19675 any dependent arguments. */
19678 any_dependent_template_arguments_p (const_tree args)
19685 if (args == error_mark_node)
19688 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
19690 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
19691 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
19692 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
19699 /* Returns TRUE if the template TMPL is dependent. */
19702 dependent_template_p (tree tmpl)
19704 if (TREE_CODE (tmpl) == OVERLOAD)
19708 if (dependent_template_p (OVL_CURRENT (tmpl)))
19710 tmpl = OVL_NEXT (tmpl);
19715 /* Template template parameters are dependent. */
19716 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
19717 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
19719 /* So are names that have not been looked up. */
19720 if (TREE_CODE (tmpl) == SCOPE_REF
19721 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
19723 /* So are member templates of dependent classes. */
19724 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
19725 return dependent_type_p (DECL_CONTEXT (tmpl));
19729 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
19732 dependent_template_id_p (tree tmpl, tree args)
19734 return (dependent_template_p (tmpl)
19735 || any_dependent_template_arguments_p (args));
19738 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
19742 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
19746 if (!processing_template_decl)
19749 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
19751 tree decl = TREE_VEC_ELT (declv, i);
19752 tree init = TREE_VEC_ELT (initv, i);
19753 tree cond = TREE_VEC_ELT (condv, i);
19754 tree incr = TREE_VEC_ELT (incrv, i);
19756 if (type_dependent_expression_p (decl))
19759 if (init && type_dependent_expression_p (init))
19762 if (type_dependent_expression_p (cond))
19765 if (COMPARISON_CLASS_P (cond)
19766 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
19767 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
19770 if (TREE_CODE (incr) == MODOP_EXPR)
19772 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
19773 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
19776 else if (type_dependent_expression_p (incr))
19778 else if (TREE_CODE (incr) == MODIFY_EXPR)
19780 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
19782 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
19784 tree t = TREE_OPERAND (incr, 1);
19785 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
19786 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
19795 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
19796 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
19797 no such TYPE can be found. Note that this function peers inside
19798 uninstantiated templates and therefore should be used only in
19799 extremely limited situations. ONLY_CURRENT_P restricts this
19800 peering to the currently open classes hierarchy (which is required
19801 when comparing types). */
19804 resolve_typename_type (tree type, bool only_current_p)
19813 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
19815 scope = TYPE_CONTEXT (type);
19816 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
19817 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
19818 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
19819 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
19820 identifier of the TYPENAME_TYPE anymore.
19821 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
19822 TYPENAME_TYPE instead, we avoid messing up with a possible
19823 typedef variant case. */
19824 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
19826 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
19827 it first before we can figure out what NAME refers to. */
19828 if (TREE_CODE (scope) == TYPENAME_TYPE)
19830 if (TYPENAME_IS_RESOLVING_P (scope))
19831 /* Given a class template A with a dependent base with nested type C,
19832 typedef typename A::C::C C will land us here, as trying to resolve
19833 the initial A::C leads to the local C typedef, which leads back to
19834 A::C::C. So we break the recursion now. */
19837 scope = resolve_typename_type (scope, only_current_p);
19839 /* If we don't know what SCOPE refers to, then we cannot resolve the
19841 if (TREE_CODE (scope) == TYPENAME_TYPE)
19843 /* If the SCOPE is a template type parameter, we have no way of
19844 resolving the name. */
19845 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
19847 /* If the SCOPE is not the current instantiation, there's no reason
19848 to look inside it. */
19849 if (only_current_p && !currently_open_class (scope))
19851 /* If this is a typedef, we don't want to look inside (c++/11987). */
19852 if (typedef_variant_p (type))
19854 /* If SCOPE isn't the template itself, it will not have a valid
19855 TYPE_FIELDS list. */
19856 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
19857 /* scope is either the template itself or a compatible instantiation
19858 like X<T>, so look up the name in the original template. */
19859 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
19861 /* scope is a partial instantiation, so we can't do the lookup or we
19862 will lose the template arguments. */
19864 /* Enter the SCOPE so that name lookup will be resolved as if we
19865 were in the class definition. In particular, SCOPE will no
19866 longer be considered a dependent type. */
19867 pushed_scope = push_scope (scope);
19868 /* Look up the declaration. */
19869 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
19870 tf_warning_or_error);
19872 result = NULL_TREE;
19874 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
19875 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
19878 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
19879 && TREE_CODE (decl) == TYPE_DECL)
19881 result = TREE_TYPE (decl);
19882 if (result == error_mark_node)
19883 result = NULL_TREE;
19885 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
19886 && DECL_CLASS_TEMPLATE_P (decl))
19890 /* Obtain the template and the arguments. */
19891 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
19892 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
19893 /* Instantiate the template. */
19894 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
19895 /*entering_scope=*/0,
19896 tf_error | tf_user);
19897 if (result == error_mark_node)
19898 result = NULL_TREE;
19901 /* Leave the SCOPE. */
19903 pop_scope (pushed_scope);
19905 /* If we failed to resolve it, return the original typename. */
19909 /* If lookup found a typename type, resolve that too. */
19910 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
19912 /* Ill-formed programs can cause infinite recursion here, so we
19913 must catch that. */
19914 TYPENAME_IS_RESOLVING_P (type) = 1;
19915 result = resolve_typename_type (result, only_current_p);
19916 TYPENAME_IS_RESOLVING_P (type) = 0;
19919 /* Qualify the resulting type. */
19920 quals = cp_type_quals (type);
19922 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
19927 /* EXPR is an expression which is not type-dependent. Return a proxy
19928 for EXPR that can be used to compute the types of larger
19929 expressions containing EXPR. */
19932 build_non_dependent_expr (tree expr)
19936 #ifdef ENABLE_CHECKING
19937 /* Try to get a constant value for all non-type-dependent expressions in
19938 order to expose bugs in *_dependent_expression_p and constexpr. */
19939 if (cxx_dialect >= cxx0x)
19940 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
19943 /* Preserve OVERLOADs; the functions must be available to resolve
19946 if (TREE_CODE (inner_expr) == STMT_EXPR)
19947 inner_expr = stmt_expr_value_expr (inner_expr);
19948 if (TREE_CODE (inner_expr) == ADDR_EXPR)
19949 inner_expr = TREE_OPERAND (inner_expr, 0);
19950 if (TREE_CODE (inner_expr) == COMPONENT_REF)
19951 inner_expr = TREE_OPERAND (inner_expr, 1);
19952 if (is_overloaded_fn (inner_expr)
19953 || TREE_CODE (inner_expr) == OFFSET_REF)
19955 /* There is no need to return a proxy for a variable. */
19956 if (TREE_CODE (expr) == VAR_DECL)
19958 /* Preserve string constants; conversions from string constants to
19959 "char *" are allowed, even though normally a "const char *"
19960 cannot be used to initialize a "char *". */
19961 if (TREE_CODE (expr) == STRING_CST)
19963 /* Preserve arithmetic constants, as an optimization -- there is no
19964 reason to create a new node. */
19965 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
19967 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
19968 There is at least one place where we want to know that a
19969 particular expression is a throw-expression: when checking a ?:
19970 expression, there are special rules if the second or third
19971 argument is a throw-expression. */
19972 if (TREE_CODE (expr) == THROW_EXPR)
19975 /* Don't wrap an initializer list, we need to be able to look inside. */
19976 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
19979 if (TREE_CODE (expr) == COND_EXPR)
19980 return build3 (COND_EXPR,
19982 TREE_OPERAND (expr, 0),
19983 (TREE_OPERAND (expr, 1)
19984 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
19985 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
19986 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
19987 if (TREE_CODE (expr) == COMPOUND_EXPR
19988 && !COMPOUND_EXPR_OVERLOADED (expr))
19989 return build2 (COMPOUND_EXPR,
19991 TREE_OPERAND (expr, 0),
19992 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
19994 /* If the type is unknown, it can't really be non-dependent */
19995 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
19997 /* Otherwise, build a NON_DEPENDENT_EXPR. */
19998 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
20001 /* ARGS is a vector of expressions as arguments to a function call.
20002 Replace the arguments with equivalent non-dependent expressions.
20003 This modifies ARGS in place. */
20006 make_args_non_dependent (VEC(tree,gc) *args)
20011 FOR_EACH_VEC_ELT (tree, args, ix, arg)
20013 tree newarg = build_non_dependent_expr (arg);
20015 VEC_replace (tree, args, ix, newarg);
20019 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
20020 with a level one deeper than the actual template parms. */
20025 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
20026 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
20027 TYPE_DECL, get_identifier ("auto"), au);
20028 TYPE_STUB_DECL (au) = TYPE_NAME (au);
20029 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
20030 (0, processing_template_decl + 1, processing_template_decl + 1,
20031 TYPE_NAME (au), NULL_TREE);
20032 TYPE_CANONICAL (au) = canonical_type_parameter (au);
20033 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
20034 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
20039 /* Given type ARG, return std::initializer_list<ARG>. */
20044 tree std_init_list = namespace_binding
20045 (get_identifier ("initializer_list"), std_node);
20047 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
20049 error ("deducing from brace-enclosed initializer list requires "
20050 "#include <initializer_list>");
20051 return error_mark_node;
20053 argvec = make_tree_vec (1);
20054 TREE_VEC_ELT (argvec, 0) = arg;
20055 return lookup_template_class (std_init_list, argvec, NULL_TREE,
20056 NULL_TREE, 0, tf_warning_or_error);
20059 /* Replace auto in TYPE with std::initializer_list<auto>. */
20062 listify_autos (tree type, tree auto_node)
20064 tree init_auto = listify (auto_node);
20065 tree argvec = make_tree_vec (1);
20066 TREE_VEC_ELT (argvec, 0) = init_auto;
20067 if (processing_template_decl)
20068 argvec = add_to_template_args (current_template_args (), argvec);
20069 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
20072 /* walk_tree helper for do_auto_deduction. */
20075 contains_auto_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
20078 /* Is this a variable with the type we're looking for? */
20080 && TREE_TYPE (*tp) == type)
20086 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
20087 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
20090 do_auto_deduction (tree type, tree init, tree auto_node)
20092 tree parms, tparms, targs;
20097 if (type_dependent_expression_p (init))
20098 /* Defining a subset of type-dependent expressions that we can deduce
20099 from ahead of time isn't worth the trouble. */
20102 /* The name of the object being declared shall not appear in the
20103 initializer expression. */
20104 decl = cp_walk_tree_without_duplicates (&init, contains_auto_r, type);
20107 error ("variable %q#D with %<auto%> type used in its own "
20108 "initializer", decl);
20109 return error_mark_node;
20112 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
20113 with either a new invented type template parameter U or, if the
20114 initializer is a braced-init-list (8.5.4), with
20115 std::initializer_list<U>. */
20116 if (BRACE_ENCLOSED_INITIALIZER_P (init))
20117 type = listify_autos (type, auto_node);
20119 init = resolve_nondeduced_context (init);
20121 parms = build_tree_list (NULL_TREE, type);
20123 tparms = make_tree_vec (1);
20124 targs = make_tree_vec (1);
20125 TREE_VEC_ELT (tparms, 0)
20126 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
20127 val = type_unification_real (tparms, targs, parms, args, 1, 0,
20128 DEDUCE_CALL, LOOKUP_NORMAL,
20129 /*explain_p=*/false);
20132 if (processing_template_decl)
20133 /* Try again at instantiation time. */
20135 if (type && type != error_mark_node)
20136 /* If type is error_mark_node a diagnostic must have been
20137 emitted by now. Also, having a mention to '<type error>'
20138 in the diagnostic is not really useful to the user. */
20139 error ("unable to deduce %qT from %qE", type, init);
20140 return error_mark_node;
20143 /* If the list of declarators contains more than one declarator, the type
20144 of each declared variable is determined as described above. If the
20145 type deduced for the template parameter U is not the same in each
20146 deduction, the program is ill-formed. */
20147 if (TREE_TYPE (auto_node)
20148 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
20150 error ("inconsistent deduction for %qT: %qT and then %qT",
20151 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
20152 return error_mark_node;
20154 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
20156 if (processing_template_decl)
20157 targs = add_to_template_args (current_template_args (), targs);
20158 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
20161 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
20165 splice_late_return_type (tree type, tree late_return_type)
20169 if (late_return_type == NULL_TREE)
20171 argvec = make_tree_vec (1);
20172 TREE_VEC_ELT (argvec, 0) = late_return_type;
20173 if (processing_template_parmlist)
20174 /* For a late-specified return type in a template type-parameter, we
20175 need to add a dummy argument level for its parmlist. */
20176 argvec = add_to_template_args
20177 (make_tree_vec (processing_template_parmlist), argvec);
20178 if (current_template_parms)
20179 argvec = add_to_template_args (current_template_args (), argvec);
20180 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
20183 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
20186 is_auto (const_tree type)
20188 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20189 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
20195 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
20196 appear as a type-specifier for the declaration in question, we don't
20197 have to look through the whole type. */
20200 type_uses_auto (tree type)
20202 enum tree_code code;
20203 if (is_auto (type))
20206 code = TREE_CODE (type);
20208 if (code == POINTER_TYPE || code == REFERENCE_TYPE
20209 || code == OFFSET_TYPE || code == FUNCTION_TYPE
20210 || code == METHOD_TYPE || code == ARRAY_TYPE)
20211 return type_uses_auto (TREE_TYPE (type));
20213 if (TYPE_PTRMEMFUNC_P (type))
20214 return type_uses_auto (TREE_TYPE (TREE_TYPE
20215 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
20220 /* For a given template T, return the vector of typedefs referenced
20221 in T for which access check is needed at T instantiation time.
20222 T is either a FUNCTION_DECL or a RECORD_TYPE.
20223 Those typedefs were added to T by the function
20224 append_type_to_template_for_access_check. */
20226 VEC(qualified_typedef_usage_t,gc)*
20227 get_types_needing_access_check (tree t)
20230 VEC(qualified_typedef_usage_t,gc) *result = NULL;
20232 if (!t || t == error_mark_node)
20235 if (!(ti = get_template_info (t)))
20238 if (CLASS_TYPE_P (t)
20239 || TREE_CODE (t) == FUNCTION_DECL)
20241 if (!TI_TEMPLATE (ti))
20244 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
20250 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
20251 tied to T. That list of typedefs will be access checked at
20252 T instantiation time.
20253 T is either a FUNCTION_DECL or a RECORD_TYPE.
20254 TYPE_DECL is a TYPE_DECL node representing a typedef.
20255 SCOPE is the scope through which TYPE_DECL is accessed.
20256 LOCATION is the location of the usage point of TYPE_DECL.
20258 This function is a subroutine of
20259 append_type_to_template_for_access_check. */
20262 append_type_to_template_for_access_check_1 (tree t,
20265 location_t location)
20267 qualified_typedef_usage_t typedef_usage;
20270 if (!t || t == error_mark_node)
20273 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
20274 || CLASS_TYPE_P (t))
20276 && TREE_CODE (type_decl) == TYPE_DECL
20279 if (!(ti = get_template_info (t)))
20282 gcc_assert (TI_TEMPLATE (ti));
20284 typedef_usage.typedef_decl = type_decl;
20285 typedef_usage.context = scope;
20286 typedef_usage.locus = location;
20288 VEC_safe_push (qualified_typedef_usage_t, gc,
20289 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
20293 /* Append TYPE_DECL to the template TEMPL.
20294 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
20295 At TEMPL instanciation time, TYPE_DECL will be checked to see
20296 if it can be accessed through SCOPE.
20297 LOCATION is the location of the usage point of TYPE_DECL.
20299 e.g. consider the following code snippet:
20306 template<class U> struct S
20308 C::myint mi; // <-- usage point of the typedef C::myint
20313 At S<char> instantiation time, we need to check the access of C::myint
20314 In other words, we need to check the access of the myint typedef through
20315 the C scope. For that purpose, this function will add the myint typedef
20316 and the scope C through which its being accessed to a list of typedefs
20317 tied to the template S. That list will be walked at template instantiation
20318 time and access check performed on each typedefs it contains.
20319 Note that this particular code snippet should yield an error because
20320 myint is private to C. */
20323 append_type_to_template_for_access_check (tree templ,
20326 location_t location)
20328 qualified_typedef_usage_t *iter;
20331 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
20333 /* Make sure we don't append the type to the template twice. */
20334 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
20335 get_types_needing_access_check (templ),
20337 if (iter->typedef_decl == type_decl && scope == iter->context)
20340 append_type_to_template_for_access_check_1 (templ, type_decl,
20344 /* Set up the hash tables for template instantiations. */
20347 init_template_processing (void)
20349 decl_specializations = htab_create_ggc (37,
20350 hash_specialization,
20351 eq_specializations,
20353 type_specializations = htab_create_ggc (37,
20354 hash_specialization,
20355 eq_specializations,
20359 /* Print stats about the template hash tables for -fstats. */
20362 print_template_statistics (void)
20364 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
20365 "%f collisions\n", (long) htab_size (decl_specializations),
20366 (long) htab_elements (decl_specializations),
20367 htab_collisions (decl_specializations));
20368 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
20369 "%f collisions\n", (long) htab_size (type_specializations),
20370 (long) htab_elements (type_specializations),
20371 htab_collisions (type_specializations));
20374 #include "gt-cp-pt.h"