1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
31 #include "coretypes.h"
35 #include "pointer-set.h"
39 #include "cp-objcp-common.h"
40 #include "tree-inline.h"
47 #include "tree-iterator.h"
50 /* The type of functions taking a tree, and some additional data, and
52 typedef int (*tree_fn_t) (tree, void*);
54 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
55 instantiations have been deferred, either because their definitions
56 were not yet available, or because we were putting off doing the work. */
57 struct GTY (()) pending_template {
58 struct pending_template *next;
59 struct tinst_level *tinst;
62 static GTY(()) struct pending_template *pending_templates;
63 static GTY(()) struct pending_template *last_pending_template;
65 int processing_template_parmlist;
66 static int template_header_count;
68 static GTY(()) tree saved_trees;
69 static VEC(int,heap) *inline_parm_levels;
71 static GTY(()) struct tinst_level *current_tinst_level;
73 static GTY(()) tree saved_access_scope;
75 /* Live only within one (recursive) call to tsubst_expr. We use
76 this to pass the statement expression node from the STMT_EXPR
77 to the EXPR_STMT that is its result. */
78 static tree cur_stmt_expr;
80 /* A map from local variable declarations in the body of the template
81 presently being instantiated to the corresponding instantiated
83 static htab_t local_specializations;
85 typedef struct GTY(()) spec_entry
92 static GTY ((param_is (spec_entry)))
93 htab_t decl_specializations;
95 static GTY ((param_is (spec_entry)))
96 htab_t type_specializations;
98 /* Contains canonical template parameter types. The vector is indexed by
99 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
100 TREE_LIST, whose TREE_VALUEs contain the canonical template
101 parameters of various types and levels. */
102 static GTY(()) VEC(tree,gc) *canonical_template_parms;
104 #define UNIFY_ALLOW_NONE 0
105 #define UNIFY_ALLOW_MORE_CV_QUAL 1
106 #define UNIFY_ALLOW_LESS_CV_QUAL 2
107 #define UNIFY_ALLOW_DERIVED 4
108 #define UNIFY_ALLOW_INTEGER 8
109 #define UNIFY_ALLOW_OUTER_LEVEL 16
110 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
111 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
113 static void push_access_scope (tree);
114 static void pop_access_scope (tree);
115 static bool resolve_overloaded_unification (tree, tree, tree, tree,
116 unification_kind_t, int);
117 static int try_one_overload (tree, tree, tree, tree, tree,
118 unification_kind_t, int, bool);
119 static int unify (tree, tree, tree, tree, int);
120 static void add_pending_template (tree);
121 static int push_tinst_level (tree);
122 static void pop_tinst_level (void);
123 static tree reopen_tinst_level (struct tinst_level *);
124 static tree tsubst_initializer_list (tree, tree);
125 static tree get_class_bindings (tree, tree, tree);
126 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
128 static void tsubst_enum (tree, tree, tree);
129 static tree add_to_template_args (tree, tree);
130 static tree add_outermost_template_args (tree, tree);
131 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
132 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
134 static int type_unification_real (tree, tree, tree, const tree *,
135 unsigned int, int, unification_kind_t, int);
136 static void note_template_header (int);
137 static tree convert_nontype_argument_function (tree, tree);
138 static tree convert_nontype_argument (tree, tree);
139 static tree convert_template_argument (tree, tree, tree,
140 tsubst_flags_t, int, tree);
141 static int for_each_template_parm (tree, tree_fn_t, void*,
142 struct pointer_set_t*, bool);
143 static tree expand_template_argument_pack (tree);
144 static tree build_template_parm_index (int, int, int, tree, tree);
145 static bool inline_needs_template_parms (tree);
146 static void push_inline_template_parms_recursive (tree, int);
147 static tree retrieve_local_specialization (tree);
148 static void register_local_specialization (tree, tree);
149 static hashval_t hash_specialization (const void *p);
150 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
151 static int mark_template_parm (tree, void *);
152 static int template_parm_this_level_p (tree, void *);
153 static tree tsubst_friend_function (tree, tree);
154 static tree tsubst_friend_class (tree, tree);
155 static int can_complete_type_without_circularity (tree);
156 static tree get_bindings (tree, tree, tree, bool);
157 static int template_decl_level (tree);
158 static int check_cv_quals_for_unify (int, tree, tree);
159 static void template_parm_level_and_index (tree, int*, int*);
160 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
161 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
162 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
163 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
164 static void regenerate_decl_from_template (tree, tree);
165 static tree most_specialized_class (tree, tree);
166 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
167 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
168 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
169 static bool check_specialization_scope (void);
170 static tree process_partial_specialization (tree);
171 static void set_current_access_from_decl (tree);
172 static tree get_template_base (tree, tree, tree, tree);
173 static tree try_class_unification (tree, tree, tree, tree);
174 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
176 static bool template_template_parm_bindings_ok_p (tree, tree);
177 static int template_args_equal (tree, tree);
178 static void tsubst_default_arguments (tree);
179 static tree for_each_template_parm_r (tree *, int *, void *);
180 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
181 static void copy_default_args_to_explicit_spec (tree);
182 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
183 static int eq_local_specializations (const void *, const void *);
184 static bool dependent_template_arg_p (tree);
185 static bool any_template_arguments_need_structural_equality_p (tree);
186 static bool dependent_type_p_r (tree);
187 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
188 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
189 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
190 static tree tsubst_decl (tree, tree, tsubst_flags_t);
191 static void perform_typedefs_access_check (tree tmpl, tree targs);
192 static void append_type_to_template_for_access_check_1 (tree, tree, tree);
193 static hashval_t iterative_hash_template_arg (tree arg, hashval_t val);
194 static bool primary_template_instantiation_p (const_tree);
195 static tree listify (tree);
196 static tree listify_autos (tree, tree);
198 /* Make the current scope suitable for access checking when we are
199 processing T. T can be FUNCTION_DECL for instantiated function
200 template, or VAR_DECL for static member variable (need by
201 instantiate_decl). */
204 push_access_scope (tree t)
206 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
207 || TREE_CODE (t) == VAR_DECL);
209 if (DECL_FRIEND_CONTEXT (t))
210 push_nested_class (DECL_FRIEND_CONTEXT (t));
211 else if (DECL_CLASS_SCOPE_P (t))
212 push_nested_class (DECL_CONTEXT (t));
214 push_to_top_level ();
216 if (TREE_CODE (t) == FUNCTION_DECL)
218 saved_access_scope = tree_cons
219 (NULL_TREE, current_function_decl, saved_access_scope);
220 current_function_decl = t;
224 /* Restore the scope set up by push_access_scope. T is the node we
228 pop_access_scope (tree t)
230 if (TREE_CODE (t) == FUNCTION_DECL)
232 current_function_decl = TREE_VALUE (saved_access_scope);
233 saved_access_scope = TREE_CHAIN (saved_access_scope);
236 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
239 pop_from_top_level ();
242 /* Do any processing required when DECL (a member template
243 declaration) is finished. Returns the TEMPLATE_DECL corresponding
244 to DECL, unless it is a specialization, in which case the DECL
245 itself is returned. */
248 finish_member_template_decl (tree decl)
250 if (decl == error_mark_node)
251 return error_mark_node;
253 gcc_assert (DECL_P (decl));
255 if (TREE_CODE (decl) == TYPE_DECL)
259 type = TREE_TYPE (decl);
260 if (type == error_mark_node)
261 return error_mark_node;
262 if (MAYBE_CLASS_TYPE_P (type)
263 && CLASSTYPE_TEMPLATE_INFO (type)
264 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
266 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
267 check_member_template (tmpl);
272 else if (TREE_CODE (decl) == FIELD_DECL)
273 error ("data member %qD cannot be a member template", decl);
274 else if (DECL_TEMPLATE_INFO (decl))
276 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
278 check_member_template (DECL_TI_TEMPLATE (decl));
279 return DECL_TI_TEMPLATE (decl);
285 error ("invalid member template declaration %qD", decl);
287 return error_mark_node;
290 /* Return the template info node corresponding to T, whatever T is. */
293 get_template_info (const_tree t)
295 tree tinfo = NULL_TREE;
297 if (!t || t == error_mark_node)
300 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
301 tinfo = DECL_TEMPLATE_INFO (t);
303 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
306 if (TAGGED_TYPE_P (t))
307 tinfo = TYPE_TEMPLATE_INFO (t);
312 /* Returns the template nesting level of the indicated class TYPE.
322 A<T>::B<U> has depth two, while A<T> has depth one.
323 Both A<T>::B<int> and A<int>::B<U> have depth one, if
324 they are instantiations, not specializations.
326 This function is guaranteed to return 0 if passed NULL_TREE so
327 that, for example, `template_class_depth (current_class_type)' is
331 template_class_depth (tree type)
336 type && TREE_CODE (type) != NAMESPACE_DECL;
337 type = (TREE_CODE (type) == FUNCTION_DECL)
338 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
340 tree tinfo = get_template_info (type);
342 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
343 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
350 /* Subroutine of maybe_begin_member_template_processing.
351 Returns true if processing DECL needs us to push template parms. */
354 inline_needs_template_parms (tree decl)
356 if (! DECL_TEMPLATE_INFO (decl))
359 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
360 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
363 /* Subroutine of maybe_begin_member_template_processing.
364 Push the template parms in PARMS, starting from LEVELS steps into the
365 chain, and ending at the beginning, since template parms are listed
369 push_inline_template_parms_recursive (tree parmlist, int levels)
371 tree parms = TREE_VALUE (parmlist);
375 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
377 ++processing_template_decl;
378 current_template_parms
379 = tree_cons (size_int (processing_template_decl),
380 parms, current_template_parms);
381 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
383 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
385 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
387 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
389 if (parm == error_mark_node)
392 gcc_assert (DECL_P (parm));
394 switch (TREE_CODE (parm))
403 /* Make a CONST_DECL as is done in process_template_parm.
404 It is ugly that we recreate this here; the original
405 version built in process_template_parm is no longer
407 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
408 CONST_DECL, DECL_NAME (parm),
410 DECL_ARTIFICIAL (decl) = 1;
411 TREE_CONSTANT (decl) = 1;
412 TREE_READONLY (decl) = 1;
413 DECL_INITIAL (decl) = DECL_INITIAL (parm);
414 SET_DECL_TEMPLATE_PARM_P (decl);
425 /* Restore the template parameter context for a member template or
426 a friend template defined in a class definition. */
429 maybe_begin_member_template_processing (tree decl)
434 if (inline_needs_template_parms (decl))
436 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
437 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
439 if (DECL_TEMPLATE_SPECIALIZATION (decl))
442 parms = TREE_CHAIN (parms);
445 push_inline_template_parms_recursive (parms, levels);
448 /* Remember how many levels of template parameters we pushed so that
449 we can pop them later. */
450 VEC_safe_push (int, heap, inline_parm_levels, levels);
453 /* Undo the effects of maybe_begin_member_template_processing. */
456 maybe_end_member_template_processing (void)
461 if (VEC_length (int, inline_parm_levels) == 0)
464 last = VEC_pop (int, inline_parm_levels);
465 for (i = 0; i < last; ++i)
467 --processing_template_decl;
468 current_template_parms = TREE_CHAIN (current_template_parms);
473 /* Return a new template argument vector which contains all of ARGS,
474 but has as its innermost set of arguments the EXTRA_ARGS. */
477 add_to_template_args (tree args, tree extra_args)
484 extra_depth = TMPL_ARGS_DEPTH (extra_args);
485 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
487 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
488 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
490 for (j = 1; j <= extra_depth; ++j, ++i)
491 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
496 /* Like add_to_template_args, but only the outermost ARGS are added to
497 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
498 (EXTRA_ARGS) levels are added. This function is used to combine
499 the template arguments from a partial instantiation with the
500 template arguments used to attain the full instantiation from the
501 partial instantiation. */
504 add_outermost_template_args (tree args, tree extra_args)
508 /* If there are more levels of EXTRA_ARGS than there are ARGS,
509 something very fishy is going on. */
510 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
512 /* If *all* the new arguments will be the EXTRA_ARGS, just return
514 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
517 /* For the moment, we make ARGS look like it contains fewer levels. */
518 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
520 new_args = add_to_template_args (args, extra_args);
522 /* Now, we restore ARGS to its full dimensions. */
523 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
528 /* Return the N levels of innermost template arguments from the ARGS. */
531 get_innermost_template_args (tree args, int n)
539 /* If N is 1, just return the innermost set of template arguments. */
541 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
543 /* If we're not removing anything, just return the arguments we were
545 extra_levels = TMPL_ARGS_DEPTH (args) - n;
546 gcc_assert (extra_levels >= 0);
547 if (extra_levels == 0)
550 /* Make a new set of arguments, not containing the outer arguments. */
551 new_args = make_tree_vec (n);
552 for (i = 1; i <= n; ++i)
553 SET_TMPL_ARGS_LEVEL (new_args, i,
554 TMPL_ARGS_LEVEL (args, i + extra_levels));
559 /* The inverse of get_innermost_template_args: Return all but the innermost
560 EXTRA_LEVELS levels of template arguments from the ARGS. */
563 strip_innermost_template_args (tree args, int extra_levels)
566 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
571 /* If N is 1, just return the outermost set of template arguments. */
573 return TMPL_ARGS_LEVEL (args, 1);
575 /* If we're not removing anything, just return the arguments we were
577 gcc_assert (extra_levels >= 0);
578 if (extra_levels == 0)
581 /* Make a new set of arguments, not containing the inner arguments. */
582 new_args = make_tree_vec (n);
583 for (i = 1; i <= n; ++i)
584 SET_TMPL_ARGS_LEVEL (new_args, i,
585 TMPL_ARGS_LEVEL (args, i));
590 /* We've got a template header coming up; push to a new level for storing
594 begin_template_parm_list (void)
596 /* We use a non-tag-transparent scope here, which causes pushtag to
597 put tags in this scope, rather than in the enclosing class or
598 namespace scope. This is the right thing, since we want
599 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
600 global template class, push_template_decl handles putting the
601 TEMPLATE_DECL into top-level scope. For a nested template class,
604 template <class T> struct S1 {
605 template <class T> struct S2 {};
608 pushtag contains special code to call pushdecl_with_scope on the
609 TEMPLATE_DECL for S2. */
610 begin_scope (sk_template_parms, NULL);
611 ++processing_template_decl;
612 ++processing_template_parmlist;
613 note_template_header (0);
616 /* This routine is called when a specialization is declared. If it is
617 invalid to declare a specialization here, an error is reported and
618 false is returned, otherwise this routine will return true. */
621 check_specialization_scope (void)
623 tree scope = current_scope ();
627 An explicit specialization shall be declared in the namespace of
628 which the template is a member, or, for member templates, in the
629 namespace of which the enclosing class or enclosing class
630 template is a member. An explicit specialization of a member
631 function, member class or static data member of a class template
632 shall be declared in the namespace of which the class template
634 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
636 error ("explicit specialization in non-namespace scope %qD", scope);
642 In an explicit specialization declaration for a member of a class
643 template or a member template that appears in namespace scope,
644 the member template and some of its enclosing class templates may
645 remain unspecialized, except that the declaration shall not
646 explicitly specialize a class member template if its enclosing
647 class templates are not explicitly specialized as well. */
648 if (current_template_parms)
650 error ("enclosing class templates are not explicitly specialized");
657 /* We've just seen template <>. */
660 begin_specialization (void)
662 begin_scope (sk_template_spec, NULL);
663 note_template_header (1);
664 return check_specialization_scope ();
667 /* Called at then end of processing a declaration preceded by
671 end_specialization (void)
674 reset_specialization ();
677 /* Any template <>'s that we have seen thus far are not referring to a
678 function specialization. */
681 reset_specialization (void)
683 processing_specialization = 0;
684 template_header_count = 0;
687 /* We've just seen a template header. If SPECIALIZATION is nonzero,
688 it was of the form template <>. */
691 note_template_header (int specialization)
693 processing_specialization = specialization;
694 template_header_count++;
697 /* We're beginning an explicit instantiation. */
700 begin_explicit_instantiation (void)
702 gcc_assert (!processing_explicit_instantiation);
703 processing_explicit_instantiation = true;
708 end_explicit_instantiation (void)
710 gcc_assert (processing_explicit_instantiation);
711 processing_explicit_instantiation = false;
714 /* An explicit specialization or partial specialization TMPL is being
715 declared. Check that the namespace in which the specialization is
716 occurring is permissible. Returns false iff it is invalid to
717 specialize TMPL in the current namespace. */
720 check_specialization_namespace (tree tmpl)
722 tree tpl_ns = decl_namespace_context (tmpl);
726 An explicit specialization shall be declared in the namespace of
727 which the template is a member, or, for member templates, in the
728 namespace of which the enclosing class or enclosing class
729 template is a member. An explicit specialization of a member
730 function, member class or static data member of a class template
731 shall be declared in the namespace of which the class template is
733 if (is_associated_namespace (current_namespace, tpl_ns))
734 /* Same or super-using namespace. */
738 permerror (input_location, "specialization of %qD in different namespace", tmpl);
739 permerror (input_location, " from definition of %q+#D", tmpl);
744 /* SPEC is an explicit instantiation. Check that it is valid to
745 perform this explicit instantiation in the current namespace. */
748 check_explicit_instantiation_namespace (tree spec)
752 /* DR 275: An explicit instantiation shall appear in an enclosing
753 namespace of its template. */
754 ns = decl_namespace_context (spec);
755 if (!is_ancestor (current_namespace, ns))
756 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
757 "(which does not enclose namespace %qD)",
758 spec, current_namespace, ns);
761 /* The TYPE is being declared. If it is a template type, that means it
762 is a partial specialization. Do appropriate error-checking. */
765 maybe_process_partial_specialization (tree type)
769 if (type == error_mark_node)
770 return error_mark_node;
772 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
774 error ("name of class shadows template template parameter %qD",
776 return error_mark_node;
779 context = TYPE_CONTEXT (type);
781 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
783 /* This is for ordinary explicit specialization and partial
784 specialization of a template class such as:
786 template <> class C<int>;
790 template <class T> class C<T*>;
792 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
794 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
795 && !COMPLETE_TYPE_P (type))
797 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
798 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
799 if (processing_template_decl)
801 if (push_template_decl (TYPE_MAIN_DECL (type))
803 return error_mark_node;
806 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
807 error ("specialization of %qT after instantiation", type);
809 else if (CLASS_TYPE_P (type)
810 && !CLASSTYPE_USE_TEMPLATE (type)
811 && CLASSTYPE_TEMPLATE_INFO (type)
812 && context && CLASS_TYPE_P (context)
813 && CLASSTYPE_TEMPLATE_INFO (context))
815 /* This is for an explicit specialization of member class
816 template according to [temp.expl.spec/18]:
818 template <> template <class U> class C<int>::D;
820 The context `C<int>' must be an implicit instantiation.
821 Otherwise this is just a member class template declared
824 template <> class C<int> { template <class U> class D; };
825 template <> template <class U> class C<int>::D;
827 In the first case, `C<int>::D' is a specialization of `C<T>::D'
828 while in the second case, `C<int>::D' is a primary template
829 and `C<T>::D' may not exist. */
831 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
832 && !COMPLETE_TYPE_P (type))
835 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
837 if (current_namespace
838 != decl_namespace_context (tmpl))
840 permerror (input_location, "specializing %q#T in different namespace", type);
841 permerror (input_location, " from definition of %q+#D", tmpl);
844 /* Check for invalid specialization after instantiation:
846 template <> template <> class C<int>::D<int>;
847 template <> template <class U> class C<int>::D; */
849 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
850 t; t = TREE_CHAIN (t))
852 tree inst = TREE_VALUE (t);
853 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
855 /* We already have a full specialization of this partial
856 instantiation. Reassign it to the new member
857 specialization template. */
861 elt.tmpl = most_general_template (tmpl);
862 elt.args = CLASSTYPE_TI_ARGS (inst);
865 htab_remove_elt (type_specializations, &elt);
868 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
870 slot = (spec_entry **)
871 htab_find_slot (type_specializations, &elt, INSERT);
872 *slot = GGC_NEW (spec_entry);
875 else if (COMPLETE_TYPE_P (inst) || TYPE_BEING_DEFINED (inst))
876 /* But if we've had an implicit instantiation, that's a
877 problem ([temp.expl.spec]/6). */
878 error ("specialization %qT after instantiation %qT",
882 /* Mark TYPE as a specialization. And as a result, we only
883 have one level of template argument for the innermost
885 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
886 CLASSTYPE_TI_ARGS (type)
887 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
890 else if (processing_specialization)
892 error ("explicit specialization of non-template %qT", type);
893 return error_mark_node;
899 /* Returns nonzero if we can optimize the retrieval of specializations
900 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
901 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
904 optimize_specialization_lookup_p (tree tmpl)
906 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
907 && DECL_CLASS_SCOPE_P (tmpl)
908 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
910 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
911 /* The optimized lookup depends on the fact that the
912 template arguments for the member function template apply
913 purely to the containing class, which is not true if the
914 containing class is an explicit or partial
916 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
917 && !DECL_MEMBER_TEMPLATE_P (tmpl)
918 && !DECL_CONV_FN_P (tmpl)
919 /* It is possible to have a template that is not a member
920 template and is not a member of a template class:
922 template <typename T>
923 struct S { friend A::f(); };
925 Here, the friend function is a template, but the context does
926 not have template information. The optimized lookup relies
927 on having ARGS be the template arguments for both the class
928 and the function template. */
929 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
932 /* Retrieve the specialization (in the sense of [temp.spec] - a
933 specialization is either an instantiation or an explicit
934 specialization) of TMPL for the given template ARGS. If there is
935 no such specialization, return NULL_TREE. The ARGS are a vector of
936 arguments, or a vector of vectors of arguments, in the case of
937 templates with more than one level of parameters.
939 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
940 then we search for a partial specialization matching ARGS. This
941 parameter is ignored if TMPL is not a class template. */
944 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
946 if (args == error_mark_node)
949 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
951 /* There should be as many levels of arguments as there are
952 levels of parameters. */
953 gcc_assert (TMPL_ARGS_DEPTH (args)
954 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
956 if (optimize_specialization_lookup_p (tmpl))
959 tree class_specialization;
960 VEC(tree,gc) *methods;
964 /* The template arguments actually apply to the containing
965 class. Find the class specialization with those
967 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
969 = retrieve_specialization (class_template, args, 0);
970 if (!class_specialization)
972 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
973 for the specialization. */
974 idx = class_method_index_for_fn (class_specialization, tmpl);
977 /* Iterate through the methods with the indicated name, looking
978 for the one that has an instance of TMPL. */
979 methods = CLASSTYPE_METHOD_VEC (class_specialization);
980 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
982 tree fn = OVL_CURRENT (fns);
983 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
984 /* using-declarations can add base methods to the method vec,
985 and we don't want those here. */
986 && DECL_CONTEXT (fn) == class_specialization)
995 htab_t specializations;
999 elt.spec = NULL_TREE;
1001 if (DECL_CLASS_TEMPLATE_P (tmpl))
1002 specializations = type_specializations;
1004 specializations = decl_specializations;
1007 hash = hash_specialization (&elt);
1008 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1016 /* Like retrieve_specialization, but for local declarations. */
1019 retrieve_local_specialization (tree tmpl)
1023 if (local_specializations == NULL)
1026 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
1027 htab_hash_pointer (tmpl));
1028 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
1031 /* Returns nonzero iff DECL is a specialization of TMPL. */
1034 is_specialization_of (tree decl, tree tmpl)
1038 if (TREE_CODE (decl) == FUNCTION_DECL)
1042 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1048 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1050 for (t = TREE_TYPE (decl);
1052 t = CLASSTYPE_USE_TEMPLATE (t)
1053 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1054 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1061 /* Returns nonzero iff DECL is a specialization of friend declaration
1062 FRIEND_DECL according to [temp.friend]. */
1065 is_specialization_of_friend (tree decl, tree friend_decl)
1067 bool need_template = true;
1070 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1071 || TREE_CODE (decl) == TYPE_DECL);
1073 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1074 of a template class, we want to check if DECL is a specialization
1076 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1077 && DECL_TEMPLATE_INFO (friend_decl)
1078 && !DECL_USE_TEMPLATE (friend_decl))
1080 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1081 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1082 need_template = false;
1084 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1085 && !PRIMARY_TEMPLATE_P (friend_decl))
1086 need_template = false;
1088 /* There is nothing to do if this is not a template friend. */
1089 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1092 if (is_specialization_of (decl, friend_decl))
1096 A member of a class template may be declared to be a friend of a
1097 non-template class. In this case, the corresponding member of
1098 every specialization of the class template is a friend of the
1099 class granting friendship.
1101 For example, given a template friend declaration
1103 template <class T> friend void A<T>::f();
1105 the member function below is considered a friend
1107 template <> struct A<int> {
1111 For this type of template friend, TEMPLATE_DEPTH below will be
1112 nonzero. To determine if DECL is a friend of FRIEND, we first
1113 check if the enclosing class is a specialization of another. */
1115 template_depth = template_class_depth (DECL_CONTEXT (friend_decl));
1117 && DECL_CLASS_SCOPE_P (decl)
1118 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1119 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1121 /* Next, we check the members themselves. In order to handle
1122 a few tricky cases, such as when FRIEND_DECL's are
1124 template <class T> friend void A<T>::g(T t);
1125 template <class T> template <T t> friend void A<T>::h();
1129 void A<int>::g(int);
1130 template <int> void A<int>::h();
1132 we need to figure out ARGS, the template arguments from
1133 the context of DECL. This is required for template substitution
1134 of `T' in the function parameter of `g' and template parameter
1135 of `h' in the above examples. Here ARGS corresponds to `int'. */
1137 tree context = DECL_CONTEXT (decl);
1138 tree args = NULL_TREE;
1139 int current_depth = 0;
1141 while (current_depth < template_depth)
1143 if (CLASSTYPE_TEMPLATE_INFO (context))
1145 if (current_depth == 0)
1146 args = TYPE_TI_ARGS (context);
1148 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1151 context = TYPE_CONTEXT (context);
1154 if (TREE_CODE (decl) == FUNCTION_DECL)
1159 tree friend_args_type;
1160 tree decl_args_type;
1162 /* Make sure that both DECL and FRIEND_DECL are templates or
1164 is_template = DECL_TEMPLATE_INFO (decl)
1165 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1166 if (need_template ^ is_template)
1168 else if (is_template)
1170 /* If both are templates, check template parameter list. */
1172 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1174 if (!comp_template_parms
1175 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1179 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1182 decl_type = TREE_TYPE (decl);
1184 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1185 tf_none, NULL_TREE);
1186 if (friend_type == error_mark_node)
1189 /* Check if return types match. */
1190 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1193 /* Check if function parameter types match, ignoring the
1194 `this' parameter. */
1195 friend_args_type = TYPE_ARG_TYPES (friend_type);
1196 decl_args_type = TYPE_ARG_TYPES (decl_type);
1197 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1198 friend_args_type = TREE_CHAIN (friend_args_type);
1199 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1200 decl_args_type = TREE_CHAIN (decl_args_type);
1202 return compparms (decl_args_type, friend_args_type);
1206 /* DECL is a TYPE_DECL */
1208 tree decl_type = TREE_TYPE (decl);
1210 /* Make sure that both DECL and FRIEND_DECL are templates or
1213 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1214 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1216 if (need_template ^ is_template)
1218 else if (is_template)
1221 /* If both are templates, check the name of the two
1222 TEMPLATE_DECL's first because is_friend didn't. */
1223 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1224 != DECL_NAME (friend_decl))
1227 /* Now check template parameter list. */
1229 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1231 return comp_template_parms
1232 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1236 return (DECL_NAME (decl)
1237 == DECL_NAME (friend_decl));
1243 /* Register the specialization SPEC as a specialization of TMPL with
1244 the indicated ARGS. IS_FRIEND indicates whether the specialization
1245 is actually just a friend declaration. Returns SPEC, or an
1246 equivalent prior declaration, if available. */
1249 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1253 spec_entry **slot = NULL;
1256 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1258 if (TREE_CODE (spec) == FUNCTION_DECL
1259 && uses_template_parms (DECL_TI_ARGS (spec)))
1260 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1261 register it; we want the corresponding TEMPLATE_DECL instead.
1262 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1263 the more obvious `uses_template_parms (spec)' to avoid problems
1264 with default function arguments. In particular, given
1265 something like this:
1267 template <class T> void f(T t1, T t = T())
1269 the default argument expression is not substituted for in an
1270 instantiation unless and until it is actually needed. */
1273 if (optimize_specialization_lookup_p (tmpl))
1274 /* We don't put these specializations in the hash table, but we might
1275 want to give an error about a mismatch. */
1276 fn = retrieve_specialization (tmpl, args, 0);
1284 hash = hash_specialization (&elt);
1286 slot = (spec_entry **)
1287 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1294 /* We can sometimes try to re-register a specialization that we've
1295 already got. In particular, regenerate_decl_from_template calls
1296 duplicate_decls which will update the specialization list. But,
1297 we'll still get called again here anyhow. It's more convenient
1298 to simply allow this than to try to prevent it. */
1301 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1303 if (DECL_TEMPLATE_INSTANTIATION (fn))
1305 if (DECL_ODR_USED (fn)
1306 || DECL_EXPLICIT_INSTANTIATION (fn))
1308 error ("specialization of %qD after instantiation",
1310 return error_mark_node;
1315 /* This situation should occur only if the first
1316 specialization is an implicit instantiation, the
1317 second is an explicit specialization, and the
1318 implicit instantiation has not yet been used. That
1319 situation can occur if we have implicitly
1320 instantiated a member function and then specialized
1323 We can also wind up here if a friend declaration that
1324 looked like an instantiation turns out to be a
1327 template <class T> void foo(T);
1328 class S { friend void foo<>(int) };
1329 template <> void foo(int);
1331 We transform the existing DECL in place so that any
1332 pointers to it become pointers to the updated
1335 If there was a definition for the template, but not
1336 for the specialization, we want this to look as if
1337 there were no definition, and vice versa. */
1338 DECL_INITIAL (fn) = NULL_TREE;
1339 duplicate_decls (spec, fn, is_friend);
1340 /* The call to duplicate_decls will have applied
1343 An explicit specialization of a function template
1344 is inline only if it is explicitly declared to be,
1345 and independently of whether its function template
1348 to the primary function; now copy the inline bits to
1349 the various clones. */
1350 FOR_EACH_CLONE (clone, fn)
1352 DECL_DECLARED_INLINE_P (clone)
1353 = DECL_DECLARED_INLINE_P (fn);
1354 DECL_SOURCE_LOCATION (clone)
1355 = DECL_SOURCE_LOCATION (fn);
1357 check_specialization_namespace (fn);
1362 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1364 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1365 /* Dup decl failed, but this is a new definition. Set the
1366 line number so any errors match this new
1368 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1374 return duplicate_decls (spec, fn, is_friend);
1376 /* A specialization must be declared in the same namespace as the
1377 template it is specializing. */
1378 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1379 && !check_specialization_namespace (tmpl))
1380 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1382 if (!optimize_specialization_lookup_p (tmpl))
1384 gcc_assert (tmpl && args && spec);
1385 *slot = GGC_NEW (spec_entry);
1387 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1388 && PRIMARY_TEMPLATE_P (tmpl)
1389 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1390 /* TMPL is a forward declaration of a template function; keep a list
1391 of all specializations in case we need to reassign them to a friend
1392 template later in tsubst_friend_function. */
1393 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1394 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1400 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1401 TMPL and ARGS members, ignores SPEC. */
1404 eq_specializations (const void *p1, const void *p2)
1406 const spec_entry *e1 = (const spec_entry *)p1;
1407 const spec_entry *e2 = (const spec_entry *)p2;
1409 return (e1->tmpl == e2->tmpl
1410 && comp_template_args (e1->args, e2->args));
1413 /* Returns a hash for a template TMPL and template arguments ARGS. */
1416 hash_tmpl_and_args (tree tmpl, tree args)
1418 hashval_t val = DECL_UID (tmpl);
1419 return iterative_hash_template_arg (args, val);
1422 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1426 hash_specialization (const void *p)
1428 const spec_entry *e = (const spec_entry *)p;
1429 return hash_tmpl_and_args (e->tmpl, e->args);
1432 /* Recursively calculate a hash value for a template argument ARG, for use
1433 in the hash tables of template specializations. */
1436 iterative_hash_template_arg (tree arg, hashval_t val)
1438 unsigned HOST_WIDE_INT i;
1439 enum tree_code code;
1442 if (arg == NULL_TREE)
1443 return iterative_hash_object (arg, val);
1448 code = TREE_CODE (arg);
1449 tclass = TREE_CODE_CLASS (code);
1451 val = iterative_hash_object (code, val);
1458 case IDENTIFIER_NODE:
1459 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1463 int i, len = TREE_VEC_LENGTH (arg);
1464 for (i = 0; i < len; ++i)
1465 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1469 case TYPE_PACK_EXPANSION:
1470 case EXPR_PACK_EXPANSION:
1471 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1473 case ARGUMENT_PACK_SELECT:
1474 /* We can get one of these when re-hashing a previous entry in the middle
1475 of substituting into a pack expansion. Just look through it... */
1476 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1477 /* ...and fall through. */
1478 case TYPE_ARGUMENT_PACK:
1479 case NONTYPE_ARGUMENT_PACK:
1480 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1483 for (; arg; arg = TREE_CHAIN (arg))
1484 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1488 for (; arg; arg = OVL_CHAIN (arg))
1489 val = iterative_hash_template_arg (OVL_FUNCTION (arg), val);
1495 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1497 val = iterative_hash_template_arg (field, val);
1498 val = iterative_hash_template_arg (value, val);
1504 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1505 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1508 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1511 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1512 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1514 case TEMPLATE_PARM_INDEX:
1515 val = iterative_hash_template_arg
1516 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1517 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1518 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1521 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1522 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1523 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1526 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1528 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1532 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1533 code = TREE_CODE (TREE_OPERAND (arg, 1));
1534 val = iterative_hash_object (code, val);
1535 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1541 if (TYPE_CANONICAL (arg))
1542 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1544 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1545 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1546 /* Otherwise just compare the types during lookup. */
1549 case tcc_declaration:
1551 return iterative_hash_expr (arg, val);
1554 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1556 unsigned n = TREE_OPERAND_LENGTH (arg);
1557 for (i = 0; i < n; ++i)
1558 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1567 /* Unregister the specialization SPEC as a specialization of TMPL.
1568 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1569 if the SPEC was listed as a specialization of TMPL.
1571 Note that SPEC has been ggc_freed, so we can't look inside it. */
1574 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1579 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1580 elt.args = TI_ARGS (tinfo);
1581 elt.spec = NULL_TREE;
1583 slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
1586 gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
1587 gcc_assert (new_spec != NULL_TREE);
1588 (*slot)->spec = new_spec;
1595 /* Compare an entry in the local specializations hash table P1 (which
1596 is really a pointer to a TREE_LIST) with P2 (which is really a
1600 eq_local_specializations (const void *p1, const void *p2)
1602 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1605 /* Hash P1, an entry in the local specializations table. */
1608 hash_local_specialization (const void* p1)
1610 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1613 /* Like register_specialization, but for local declarations. We are
1614 registering SPEC, an instantiation of TMPL. */
1617 register_local_specialization (tree spec, tree tmpl)
1621 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1622 htab_hash_pointer (tmpl), INSERT);
1623 *slot = build_tree_list (spec, tmpl);
1626 /* TYPE is a class type. Returns true if TYPE is an explicitly
1627 specialized class. */
1630 explicit_class_specialization_p (tree type)
1632 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1634 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1637 /* Print the list of candidate FNS in an error message. */
1640 print_candidates (tree fns)
1644 const char *str = "candidates are:";
1646 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1650 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1651 error ("%s %+#D", str, OVL_CURRENT (f));
1656 /* Returns the template (one of the functions given by TEMPLATE_ID)
1657 which can be specialized to match the indicated DECL with the
1658 explicit template args given in TEMPLATE_ID. The DECL may be
1659 NULL_TREE if none is available. In that case, the functions in
1660 TEMPLATE_ID are non-members.
1662 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1663 specialization of a member template.
1665 The TEMPLATE_COUNT is the number of references to qualifying
1666 template classes that appeared in the name of the function. See
1667 check_explicit_specialization for a more accurate description.
1669 TSK indicates what kind of template declaration (if any) is being
1670 declared. TSK_TEMPLATE indicates that the declaration given by
1671 DECL, though a FUNCTION_DECL, has template parameters, and is
1672 therefore a template function.
1674 The template args (those explicitly specified and those deduced)
1675 are output in a newly created vector *TARGS_OUT.
1677 If it is impossible to determine the result, an error message is
1678 issued. The error_mark_node is returned to indicate failure. */
1681 determine_specialization (tree template_id,
1684 int need_member_template,
1690 tree explicit_targs;
1691 tree candidates = NULL_TREE;
1692 /* A TREE_LIST of templates of which DECL may be a specialization.
1693 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1694 corresponding TREE_PURPOSE is the set of template arguments that,
1695 when used to instantiate the template, would produce a function
1696 with the signature of DECL. */
1697 tree templates = NULL_TREE;
1699 struct cp_binding_level *b;
1701 *targs_out = NULL_TREE;
1703 if (template_id == error_mark_node || decl == error_mark_node)
1704 return error_mark_node;
1706 fns = TREE_OPERAND (template_id, 0);
1707 explicit_targs = TREE_OPERAND (template_id, 1);
1709 if (fns == error_mark_node)
1710 return error_mark_node;
1712 /* Check for baselinks. */
1713 if (BASELINK_P (fns))
1714 fns = BASELINK_FUNCTIONS (fns);
1716 if (!is_overloaded_fn (fns))
1718 error ("%qD is not a function template", fns);
1719 return error_mark_node;
1722 /* Count the number of template headers specified for this
1725 for (b = current_binding_level;
1726 b->kind == sk_template_parms;
1730 for (; fns; fns = OVL_NEXT (fns))
1732 tree fn = OVL_CURRENT (fns);
1734 if (TREE_CODE (fn) == TEMPLATE_DECL)
1736 tree decl_arg_types;
1739 /* In case of explicit specialization, we need to check if
1740 the number of template headers appearing in the specialization
1741 is correct. This is usually done in check_explicit_specialization,
1742 but the check done there cannot be exhaustive when specializing
1743 member functions. Consider the following code:
1745 template <> void A<int>::f(int);
1746 template <> template <> void A<int>::f(int);
1748 Assuming that A<int> is not itself an explicit specialization
1749 already, the first line specializes "f" which is a non-template
1750 member function, whilst the second line specializes "f" which
1751 is a template member function. So both lines are syntactically
1752 correct, and check_explicit_specialization does not reject
1755 Here, we can do better, as we are matching the specialization
1756 against the declarations. We count the number of template
1757 headers, and we check if they match TEMPLATE_COUNT + 1
1758 (TEMPLATE_COUNT is the number of qualifying template classes,
1759 plus there must be another header for the member template
1762 Notice that if header_count is zero, this is not a
1763 specialization but rather a template instantiation, so there
1764 is no check we can perform here. */
1765 if (header_count && header_count != template_count + 1)
1768 /* Check that the number of template arguments at the
1769 innermost level for DECL is the same as for FN. */
1770 if (current_binding_level->kind == sk_template_parms
1771 && !current_binding_level->explicit_spec_p
1772 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1773 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1774 (current_template_parms))))
1777 /* DECL might be a specialization of FN. */
1778 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1779 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1781 /* For a non-static member function, we need to make sure
1782 that the const qualification is the same. Since
1783 get_bindings does not try to merge the "this" parameter,
1784 we must do the comparison explicitly. */
1785 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1786 && !same_type_p (TREE_VALUE (fn_arg_types),
1787 TREE_VALUE (decl_arg_types)))
1790 /* Skip the "this" parameter and, for constructors of
1791 classes with virtual bases, the VTT parameter. A
1792 full specialization of a constructor will have a VTT
1793 parameter, but a template never will. */
1795 = skip_artificial_parms_for (decl, decl_arg_types);
1797 = skip_artificial_parms_for (fn, fn_arg_types);
1799 /* Check that the number of function parameters matches.
1801 template <class T> void f(int i = 0);
1802 template <> void f<int>();
1803 The specialization f<int> is invalid but is not caught
1804 by get_bindings below. */
1805 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1808 /* Function templates cannot be specializations; there are
1809 no partial specializations of functions. Therefore, if
1810 the type of DECL does not match FN, there is no
1812 if (tsk == tsk_template)
1814 if (compparms (fn_arg_types, decl_arg_types))
1815 candidates = tree_cons (NULL_TREE, fn, candidates);
1819 /* See whether this function might be a specialization of this
1821 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1824 /* We cannot deduce template arguments that when used to
1825 specialize TMPL will produce DECL. */
1828 /* Save this template, and the arguments deduced. */
1829 templates = tree_cons (targs, fn, templates);
1831 else if (need_member_template)
1832 /* FN is an ordinary member function, and we need a
1833 specialization of a member template. */
1835 else if (TREE_CODE (fn) != FUNCTION_DECL)
1836 /* We can get IDENTIFIER_NODEs here in certain erroneous
1839 else if (!DECL_FUNCTION_MEMBER_P (fn))
1840 /* This is just an ordinary non-member function. Nothing can
1841 be a specialization of that. */
1843 else if (DECL_ARTIFICIAL (fn))
1844 /* Cannot specialize functions that are created implicitly. */
1848 tree decl_arg_types;
1850 /* This is an ordinary member function. However, since
1851 we're here, we can assume it's enclosing class is a
1852 template class. For example,
1854 template <typename T> struct S { void f(); };
1855 template <> void S<int>::f() {}
1857 Here, S<int>::f is a non-template, but S<int> is a
1858 template class. If FN has the same type as DECL, we
1859 might be in business. */
1861 if (!DECL_TEMPLATE_INFO (fn))
1862 /* Its enclosing class is an explicit specialization
1863 of a template class. This is not a candidate. */
1866 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1867 TREE_TYPE (TREE_TYPE (fn))))
1868 /* The return types differ. */
1871 /* Adjust the type of DECL in case FN is a static member. */
1872 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1873 if (DECL_STATIC_FUNCTION_P (fn)
1874 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1875 decl_arg_types = TREE_CHAIN (decl_arg_types);
1877 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1880 candidates = tree_cons (NULL_TREE, fn, candidates);
1884 if (templates && TREE_CHAIN (templates))
1890 It is possible for a specialization with a given function
1891 signature to be instantiated from more than one function
1892 template. In such cases, explicit specification of the
1893 template arguments must be used to uniquely identify the
1894 function template specialization being specialized.
1896 Note that here, there's no suggestion that we're supposed to
1897 determine which of the candidate templates is most
1898 specialized. However, we, also have:
1902 Partial ordering of overloaded function template
1903 declarations is used in the following contexts to select
1904 the function template to which a function template
1905 specialization refers:
1907 -- when an explicit specialization refers to a function
1910 So, we do use the partial ordering rules, at least for now.
1911 This extension can only serve to make invalid programs valid,
1912 so it's safe. And, there is strong anecdotal evidence that
1913 the committee intended the partial ordering rules to apply;
1914 the EDG front end has that behavior, and John Spicer claims
1915 that the committee simply forgot to delete the wording in
1916 [temp.expl.spec]. */
1917 tree tmpl = most_specialized_instantiation (templates);
1918 if (tmpl != error_mark_node)
1921 TREE_CHAIN (templates) = NULL_TREE;
1925 if (templates == NULL_TREE && candidates == NULL_TREE)
1927 error ("template-id %qD for %q+D does not match any template "
1928 "declaration", template_id, decl);
1929 return error_mark_node;
1931 else if ((templates && TREE_CHAIN (templates))
1932 || (candidates && TREE_CHAIN (candidates))
1933 || (templates && candidates))
1935 error ("ambiguous template specialization %qD for %q+D",
1937 chainon (candidates, templates);
1938 print_candidates (candidates);
1939 return error_mark_node;
1942 /* We have one, and exactly one, match. */
1945 tree fn = TREE_VALUE (candidates);
1946 *targs_out = copy_node (DECL_TI_ARGS (fn));
1947 /* DECL is a re-declaration or partial instantiation of a template
1949 if (TREE_CODE (fn) == TEMPLATE_DECL)
1951 /* It was a specialization of an ordinary member function in a
1953 return DECL_TI_TEMPLATE (fn);
1956 /* It was a specialization of a template. */
1957 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1958 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1960 *targs_out = copy_node (targs);
1961 SET_TMPL_ARGS_LEVEL (*targs_out,
1962 TMPL_ARGS_DEPTH (*targs_out),
1963 TREE_PURPOSE (templates));
1966 *targs_out = TREE_PURPOSE (templates);
1967 return TREE_VALUE (templates);
1970 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1971 but with the default argument values filled in from those in the
1975 copy_default_args_to_explicit_spec_1 (tree spec_types,
1978 tree new_spec_types;
1983 if (spec_types == void_list_node)
1984 return void_list_node;
1986 /* Substitute into the rest of the list. */
1988 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1989 TREE_CHAIN (tmpl_types));
1991 /* Add the default argument for this parameter. */
1992 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1993 TREE_VALUE (spec_types),
1997 /* DECL is an explicit specialization. Replicate default arguments
1998 from the template it specializes. (That way, code like:
2000 template <class T> void f(T = 3);
2001 template <> void f(double);
2004 works, as required.) An alternative approach would be to look up
2005 the correct default arguments at the call-site, but this approach
2006 is consistent with how implicit instantiations are handled. */
2009 copy_default_args_to_explicit_spec (tree decl)
2014 tree new_spec_types;
2018 tree object_type = NULL_TREE;
2019 tree in_charge = NULL_TREE;
2020 tree vtt = NULL_TREE;
2022 /* See if there's anything we need to do. */
2023 tmpl = DECL_TI_TEMPLATE (decl);
2024 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2025 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2026 if (TREE_PURPOSE (t))
2031 old_type = TREE_TYPE (decl);
2032 spec_types = TYPE_ARG_TYPES (old_type);
2034 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2036 /* Remove the this pointer, but remember the object's type for
2038 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2039 spec_types = TREE_CHAIN (spec_types);
2040 tmpl_types = TREE_CHAIN (tmpl_types);
2042 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2044 /* DECL may contain more parameters than TMPL due to the extra
2045 in-charge parameter in constructors and destructors. */
2046 in_charge = spec_types;
2047 spec_types = TREE_CHAIN (spec_types);
2049 if (DECL_HAS_VTT_PARM_P (decl))
2052 spec_types = TREE_CHAIN (spec_types);
2056 /* Compute the merged default arguments. */
2058 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2060 /* Compute the new FUNCTION_TYPE. */
2064 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2069 /* Put the in-charge parameter back. */
2070 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2071 TREE_VALUE (in_charge),
2074 new_type = build_method_type_directly (object_type,
2075 TREE_TYPE (old_type),
2079 new_type = build_function_type (TREE_TYPE (old_type),
2081 new_type = cp_build_type_attribute_variant (new_type,
2082 TYPE_ATTRIBUTES (old_type));
2083 new_type = build_exception_variant (new_type,
2084 TYPE_RAISES_EXCEPTIONS (old_type));
2085 TREE_TYPE (decl) = new_type;
2088 /* Check to see if the function just declared, as indicated in
2089 DECLARATOR, and in DECL, is a specialization of a function
2090 template. We may also discover that the declaration is an explicit
2091 instantiation at this point.
2093 Returns DECL, or an equivalent declaration that should be used
2094 instead if all goes well. Issues an error message if something is
2095 amiss. Returns error_mark_node if the error is not easily
2098 FLAGS is a bitmask consisting of the following flags:
2100 2: The function has a definition.
2101 4: The function is a friend.
2103 The TEMPLATE_COUNT is the number of references to qualifying
2104 template classes that appeared in the name of the function. For
2107 template <class T> struct S { void f(); };
2110 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2111 classes are not counted in the TEMPLATE_COUNT, so that in
2113 template <class T> struct S {};
2114 template <> struct S<int> { void f(); }
2115 template <> void S<int>::f();
2117 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2118 invalid; there should be no template <>.)
2120 If the function is a specialization, it is marked as such via
2121 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2122 is set up correctly, and it is added to the list of specializations
2123 for that template. */
2126 check_explicit_specialization (tree declarator,
2131 int have_def = flags & 2;
2132 int is_friend = flags & 4;
2133 int specialization = 0;
2134 int explicit_instantiation = 0;
2135 int member_specialization = 0;
2136 tree ctype = DECL_CLASS_CONTEXT (decl);
2137 tree dname = DECL_NAME (decl);
2142 if (!processing_specialization)
2145 tsk = tsk_excessive_parms;
2148 tsk = current_tmpl_spec_kind (template_count);
2153 if (processing_specialization)
2156 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2158 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2161 /* This could be something like:
2163 template <class T> void f(T);
2164 class S { friend void f<>(int); } */
2168 /* This case handles bogus declarations like template <>
2169 template <class T> void f<int>(); */
2171 error ("template-id %qD in declaration of primary template",
2178 case tsk_invalid_member_spec:
2179 /* The error has already been reported in
2180 check_specialization_scope. */
2181 return error_mark_node;
2183 case tsk_invalid_expl_inst:
2184 error ("template parameter list used in explicit instantiation");
2190 error ("definition provided for explicit instantiation");
2192 explicit_instantiation = 1;
2195 case tsk_excessive_parms:
2196 case tsk_insufficient_parms:
2197 if (tsk == tsk_excessive_parms)
2198 error ("too many template parameter lists in declaration of %qD",
2200 else if (template_header_count)
2201 error("too few template parameter lists in declaration of %qD", decl);
2203 error("explicit specialization of %qD must be introduced by "
2204 "%<template <>%>", decl);
2208 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2210 member_specialization = 1;
2216 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2218 /* This case handles bogus declarations like template <>
2219 template <class T> void f<int>(); */
2221 if (uses_template_parms (declarator))
2222 error ("function template partial specialization %qD "
2223 "is not allowed", declarator);
2225 error ("template-id %qD in declaration of primary template",
2230 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2231 /* This is a specialization of a member template, without
2232 specialization the containing class. Something like:
2234 template <class T> struct S {
2235 template <class U> void f (U);
2237 template <> template <class U> void S<int>::f(U) {}
2239 That's a specialization -- but of the entire template. */
2247 if (specialization || member_specialization)
2249 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2250 for (; t; t = TREE_CHAIN (t))
2251 if (TREE_PURPOSE (t))
2253 permerror (input_location,
2254 "default argument specified in explicit specialization");
2259 if (specialization || member_specialization || explicit_instantiation)
2261 tree tmpl = NULL_TREE;
2262 tree targs = NULL_TREE;
2264 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2265 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2269 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2274 /* If there is no class context, the explicit instantiation
2275 must be at namespace scope. */
2276 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2278 /* Find the namespace binding, using the declaration
2280 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2282 if (fns == error_mark_node || !is_overloaded_fn (fns))
2284 error ("%qD is not a template function", dname);
2285 fns = error_mark_node;
2289 tree fn = OVL_CURRENT (fns);
2290 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2291 CP_DECL_CONTEXT (fn)))
2292 error ("%qD is not declared in %qD",
2293 decl, current_namespace);
2297 declarator = lookup_template_function (fns, NULL_TREE);
2300 if (declarator == error_mark_node)
2301 return error_mark_node;
2303 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2305 if (!explicit_instantiation)
2306 /* A specialization in class scope. This is invalid,
2307 but the error will already have been flagged by
2308 check_specialization_scope. */
2309 return error_mark_node;
2312 /* It's not valid to write an explicit instantiation in
2315 class C { template void f(); }
2317 This case is caught by the parser. However, on
2320 template class C { void f(); };
2322 (which is invalid) we can get here. The error will be
2329 else if (ctype != NULL_TREE
2330 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2333 /* Find the list of functions in ctype that have the same
2334 name as the declared function. */
2335 tree name = TREE_OPERAND (declarator, 0);
2336 tree fns = NULL_TREE;
2339 if (constructor_name_p (name, ctype))
2341 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2343 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2344 : !CLASSTYPE_DESTRUCTORS (ctype))
2346 /* From [temp.expl.spec]:
2348 If such an explicit specialization for the member
2349 of a class template names an implicitly-declared
2350 special member function (clause _special_), the
2351 program is ill-formed.
2353 Similar language is found in [temp.explicit]. */
2354 error ("specialization of implicitly-declared special member function");
2355 return error_mark_node;
2358 name = is_constructor ? ctor_identifier : dtor_identifier;
2361 if (!DECL_CONV_FN_P (decl))
2363 idx = lookup_fnfields_1 (ctype, name);
2365 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2369 VEC(tree,gc) *methods;
2372 /* For a type-conversion operator, we cannot do a
2373 name-based lookup. We might be looking for `operator
2374 int' which will be a specialization of `operator T'.
2375 So, we find *all* the conversion operators, and then
2376 select from them. */
2379 methods = CLASSTYPE_METHOD_VEC (ctype);
2381 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2382 VEC_iterate (tree, methods, idx, ovl);
2385 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2386 /* There are no more conversion functions. */
2389 /* Glue all these conversion functions together
2390 with those we already have. */
2391 for (; ovl; ovl = OVL_NEXT (ovl))
2392 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2396 if (fns == NULL_TREE)
2398 error ("no member function %qD declared in %qT", name, ctype);
2399 return error_mark_node;
2402 TREE_OPERAND (declarator, 0) = fns;
2405 /* Figure out what exactly is being specialized at this point.
2406 Note that for an explicit instantiation, even one for a
2407 member function, we cannot tell apriori whether the
2408 instantiation is for a member template, or just a member
2409 function of a template class. Even if a member template is
2410 being instantiated, the member template arguments may be
2411 elided if they can be deduced from the rest of the
2413 tmpl = determine_specialization (declarator, decl,
2415 member_specialization,
2419 if (!tmpl || tmpl == error_mark_node)
2420 /* We couldn't figure out what this declaration was
2422 return error_mark_node;
2425 tree gen_tmpl = most_general_template (tmpl);
2427 if (explicit_instantiation)
2429 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2430 is done by do_decl_instantiation later. */
2432 int arg_depth = TMPL_ARGS_DEPTH (targs);
2433 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2435 if (arg_depth > parm_depth)
2437 /* If TMPL is not the most general template (for
2438 example, if TMPL is a friend template that is
2439 injected into namespace scope), then there will
2440 be too many levels of TARGS. Remove some of them
2445 new_targs = make_tree_vec (parm_depth);
2446 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2447 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2448 = TREE_VEC_ELT (targs, i);
2452 return instantiate_template (tmpl, targs, tf_error);
2455 /* If we thought that the DECL was a member function, but it
2456 turns out to be specializing a static member function,
2457 make DECL a static member function as well. */
2458 if (DECL_STATIC_FUNCTION_P (tmpl)
2459 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2460 revert_static_member_fn (decl);
2462 /* If this is a specialization of a member template of a
2463 template class, we want to return the TEMPLATE_DECL, not
2464 the specialization of it. */
2465 if (tsk == tsk_template)
2467 tree result = DECL_TEMPLATE_RESULT (tmpl);
2468 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2469 DECL_INITIAL (result) = NULL_TREE;
2473 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2474 DECL_SOURCE_LOCATION (result)
2475 = DECL_SOURCE_LOCATION (decl);
2476 /* We want to use the argument list specified in the
2477 definition, not in the original declaration. */
2478 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2479 for (parm = DECL_ARGUMENTS (result); parm;
2480 parm = TREE_CHAIN (parm))
2481 DECL_CONTEXT (parm) = result;
2483 return register_specialization (tmpl, gen_tmpl, targs,
2487 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2488 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2490 /* Inherit default function arguments from the template
2491 DECL is specializing. */
2492 copy_default_args_to_explicit_spec (decl);
2494 /* This specialization has the same protection as the
2495 template it specializes. */
2496 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2497 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2499 /* 7.1.1-1 [dcl.stc]
2501 A storage-class-specifier shall not be specified in an
2502 explicit specialization...
2504 The parser rejects these, so unless action is taken here,
2505 explicit function specializations will always appear with
2508 The action recommended by the C++ CWG in response to C++
2509 defect report 605 is to make the storage class and linkage
2510 of the explicit specialization match the templated function:
2512 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2514 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2516 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2517 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2519 /* This specialization has the same linkage and visibility as
2520 the function template it specializes. */
2521 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2522 if (! TREE_PUBLIC (decl))
2524 DECL_INTERFACE_KNOWN (decl) = 1;
2525 DECL_NOT_REALLY_EXTERN (decl) = 1;
2527 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2528 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2530 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2531 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2535 /* If DECL is a friend declaration, declared using an
2536 unqualified name, the namespace associated with DECL may
2537 have been set incorrectly. For example, in:
2539 template <typename T> void f(T);
2541 struct S { friend void f<int>(int); }
2544 we will have set the DECL_CONTEXT for the friend
2545 declaration to N, rather than to the global namespace. */
2546 if (DECL_NAMESPACE_SCOPE_P (decl))
2547 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2549 if (is_friend && !have_def)
2550 /* This is not really a declaration of a specialization.
2551 It's just the name of an instantiation. But, it's not
2552 a request for an instantiation, either. */
2553 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2554 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2555 /* This is indeed a specialization. In case of constructors
2556 and destructors, we need in-charge and not-in-charge
2557 versions in V3 ABI. */
2558 clone_function_decl (decl, /*update_method_vec_p=*/0);
2560 /* Register this specialization so that we can find it
2562 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2569 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2570 parameters. These are represented in the same format used for
2571 DECL_TEMPLATE_PARMS. */
2574 comp_template_parms (const_tree parms1, const_tree parms2)
2579 if (parms1 == parms2)
2582 for (p1 = parms1, p2 = parms2;
2583 p1 != NULL_TREE && p2 != NULL_TREE;
2584 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2586 tree t1 = TREE_VALUE (p1);
2587 tree t2 = TREE_VALUE (p2);
2590 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2591 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2593 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2596 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2598 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2599 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2601 /* If either of the template parameters are invalid, assume
2602 they match for the sake of error recovery. */
2603 if (parm1 == error_mark_node || parm2 == error_mark_node)
2606 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2609 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2610 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2611 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2613 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2618 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2619 /* One set of parameters has more parameters lists than the
2626 /* Determine whether PARM is a parameter pack. */
2629 template_parameter_pack_p (const_tree parm)
2631 /* Determine if we have a non-type template parameter pack. */
2632 if (TREE_CODE (parm) == PARM_DECL)
2633 return (DECL_TEMPLATE_PARM_P (parm)
2634 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2636 /* If this is a list of template parameters, we could get a
2637 TYPE_DECL or a TEMPLATE_DECL. */
2638 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2639 parm = TREE_TYPE (parm);
2641 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2642 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2643 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2646 /* Determine if T is a function parameter pack. */
2649 function_parameter_pack_p (const_tree t)
2651 if (t && TREE_CODE (t) == PARM_DECL)
2652 return FUNCTION_PARAMETER_PACK_P (t);
2656 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2657 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2660 get_function_template_decl (const_tree primary_func_tmpl_inst)
2662 if (! primary_func_tmpl_inst
2663 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2664 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2667 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2670 /* Return true iff the function parameter PARAM_DECL was expanded
2671 from the function parameter pack PACK. */
2674 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2676 if (DECL_ARTIFICIAL (param_decl)
2677 || !function_parameter_pack_p (pack))
2680 gcc_assert (DECL_NAME (param_decl) && DECL_NAME (pack));
2682 /* The parameter pack and its pack arguments have the same
2684 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2687 /* Determine whether ARGS describes a variadic template args list,
2688 i.e., one that is terminated by a template argument pack. */
2691 template_args_variadic_p (tree args)
2696 if (args == NULL_TREE)
2699 args = INNERMOST_TEMPLATE_ARGS (args);
2700 nargs = TREE_VEC_LENGTH (args);
2705 last_parm = TREE_VEC_ELT (args, nargs - 1);
2707 return ARGUMENT_PACK_P (last_parm);
2710 /* Generate a new name for the parameter pack name NAME (an
2711 IDENTIFIER_NODE) that incorporates its */
2714 make_ith_pack_parameter_name (tree name, int i)
2716 /* Munge the name to include the parameter index. */
2717 #define NUMBUF_LEN 128
2718 char numbuf[NUMBUF_LEN];
2722 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2723 newname_len = IDENTIFIER_LENGTH (name)
2724 + strlen (numbuf) + 2;
2725 newname = (char*)alloca (newname_len);
2726 snprintf (newname, newname_len,
2727 "%s#%i", IDENTIFIER_POINTER (name), i);
2728 return get_identifier (newname);
2731 /* Return true if T is a primary function
2732 or class template instantiation. */
2735 primary_template_instantiation_p (const_tree t)
2740 if (TREE_CODE (t) == FUNCTION_DECL)
2741 return DECL_LANG_SPECIFIC (t)
2742 && DECL_TEMPLATE_INSTANTIATION (t)
2743 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2744 else if (CLASS_TYPE_P (t))
2745 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2746 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2750 /* Return true if PARM is a template template parameter. */
2753 template_template_parameter_p (const_tree parm)
2755 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2758 /* Return the template parameters of T if T is a
2759 primary template instantiation, NULL otherwise. */
2762 get_primary_template_innermost_parameters (const_tree t)
2764 tree parms = NULL, template_info = NULL;
2766 if ((template_info = get_template_info (t))
2767 && primary_template_instantiation_p (t))
2768 parms = INNERMOST_TEMPLATE_PARMS
2769 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2774 /* Returns the template arguments of T if T is a template instantiation,
2778 get_template_innermost_arguments (const_tree t)
2780 tree args = NULL, template_info = NULL;
2782 if ((template_info = get_template_info (t))
2783 && TI_ARGS (template_info))
2784 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2789 /* Return the argument pack elements of T if T is a template argument pack,
2793 get_template_argument_pack_elems (const_tree t)
2795 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2796 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2799 return ARGUMENT_PACK_ARGS (t);
2802 /* Structure used to track the progress of find_parameter_packs_r. */
2803 struct find_parameter_pack_data
2805 /* TREE_LIST that will contain all of the parameter packs found by
2807 tree* parameter_packs;
2809 /* Set of AST nodes that have been visited by the traversal. */
2810 struct pointer_set_t *visited;
2813 /* Identifies all of the argument packs that occur in a template
2814 argument and appends them to the TREE_LIST inside DATA, which is a
2815 find_parameter_pack_data structure. This is a subroutine of
2816 make_pack_expansion and uses_parameter_packs. */
2818 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2821 struct find_parameter_pack_data* ppd =
2822 (struct find_parameter_pack_data*)data;
2823 bool parameter_pack_p = false;
2825 /* Identify whether this is a parameter pack or not. */
2826 switch (TREE_CODE (t))
2828 case TEMPLATE_PARM_INDEX:
2829 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2830 parameter_pack_p = true;
2833 case TEMPLATE_TYPE_PARM:
2834 case TEMPLATE_TEMPLATE_PARM:
2835 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2836 parameter_pack_p = true;
2840 if (FUNCTION_PARAMETER_PACK_P (t))
2842 /* We don't want to walk into the type of a PARM_DECL,
2843 because we don't want to see the type parameter pack. */
2845 parameter_pack_p = true;
2850 /* Not a parameter pack. */
2854 if (parameter_pack_p)
2856 /* Add this parameter pack to the list. */
2857 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2861 cp_walk_tree (&TYPE_CONTEXT (t),
2862 &find_parameter_packs_r, ppd, ppd->visited);
2864 /* This switch statement will return immediately if we don't find a
2866 switch (TREE_CODE (t))
2868 case TEMPLATE_PARM_INDEX:
2871 case BOUND_TEMPLATE_TEMPLATE_PARM:
2872 /* Check the template itself. */
2873 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
2874 &find_parameter_packs_r, ppd, ppd->visited);
2875 /* Check the template arguments. */
2876 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2881 case TEMPLATE_TYPE_PARM:
2882 case TEMPLATE_TEMPLATE_PARM:
2889 if (TYPE_PTRMEMFUNC_P (t))
2895 if (TYPE_TEMPLATE_INFO (t))
2896 cp_walk_tree (&TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
2897 &find_parameter_packs_r, ppd, ppd->visited);
2903 cp_walk_tree (&TREE_TYPE (t),
2904 &find_parameter_packs_r, ppd, ppd->visited);
2908 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
2913 case TYPE_PACK_EXPANSION:
2914 case EXPR_PACK_EXPANSION:
2919 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
2924 case IDENTIFIER_NODE:
2925 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
2937 /* Determines if the expression or type T uses any parameter packs. */
2939 uses_parameter_packs (tree t)
2941 tree parameter_packs = NULL_TREE;
2942 struct find_parameter_pack_data ppd;
2943 ppd.parameter_packs = ¶meter_packs;
2944 ppd.visited = pointer_set_create ();
2945 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
2946 pointer_set_destroy (ppd.visited);
2947 return parameter_packs != NULL_TREE;
2950 /* Turn ARG, which may be an expression, type, or a TREE_LIST
2951 representation a base-class initializer into a parameter pack
2952 expansion. If all goes well, the resulting node will be an
2953 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
2956 make_pack_expansion (tree arg)
2959 tree parameter_packs = NULL_TREE;
2960 bool for_types = false;
2961 struct find_parameter_pack_data ppd;
2963 if (!arg || arg == error_mark_node)
2966 if (TREE_CODE (arg) == TREE_LIST)
2968 /* The only time we will see a TREE_LIST here is for a base
2969 class initializer. In this case, the TREE_PURPOSE will be a
2970 _TYPE node (representing the base class expansion we're
2971 initializing) and the TREE_VALUE will be a TREE_LIST
2972 containing the initialization arguments.
2974 The resulting expansion looks somewhat different from most
2975 expansions. Rather than returning just one _EXPANSION, we
2976 return a TREE_LIST whose TREE_PURPOSE is a
2977 TYPE_PACK_EXPANSION containing the bases that will be
2978 initialized. The TREE_VALUE will be identical to the
2979 original TREE_VALUE, which is a list of arguments that will
2980 be passed to each base. We do not introduce any new pack
2981 expansion nodes into the TREE_VALUE (although it is possible
2982 that some already exist), because the TREE_PURPOSE and
2983 TREE_VALUE all need to be expanded together with the same
2984 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
2985 resulting TREE_PURPOSE will mention the parameter packs in
2986 both the bases and the arguments to the bases. */
2989 tree parameter_packs = NULL_TREE;
2991 /* Determine which parameter packs will be used by the base
2993 ppd.visited = pointer_set_create ();
2994 ppd.parameter_packs = ¶meter_packs;
2995 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
2998 if (parameter_packs == NULL_TREE)
3000 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3001 pointer_set_destroy (ppd.visited);
3002 return error_mark_node;
3005 if (TREE_VALUE (arg) != void_type_node)
3007 /* Collect the sets of parameter packs used in each of the
3008 initialization arguments. */
3009 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3011 /* Determine which parameter packs will be expanded in this
3013 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3018 pointer_set_destroy (ppd.visited);
3020 /* Create the pack expansion type for the base type. */
3021 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3022 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3023 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3025 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3026 they will rarely be compared to anything. */
3027 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3029 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3032 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3035 /* Build the PACK_EXPANSION_* node. */
3037 ? cxx_make_type (TYPE_PACK_EXPANSION)
3038 : make_node (EXPR_PACK_EXPANSION);
3039 SET_PACK_EXPANSION_PATTERN (result, arg);
3040 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3042 /* Propagate type and const-expression information. */
3043 TREE_TYPE (result) = TREE_TYPE (arg);
3044 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3047 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3048 they will rarely be compared to anything. */
3049 SET_TYPE_STRUCTURAL_EQUALITY (result);
3051 /* Determine which parameter packs will be expanded. */
3052 ppd.parameter_packs = ¶meter_packs;
3053 ppd.visited = pointer_set_create ();
3054 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3055 pointer_set_destroy (ppd.visited);
3057 /* Make sure we found some parameter packs. */
3058 if (parameter_packs == NULL_TREE)
3061 error ("expansion pattern %<%T%> contains no argument packs", arg);
3063 error ("expansion pattern %<%E%> contains no argument packs", arg);
3064 return error_mark_node;
3066 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3071 /* Checks T for any "bare" parameter packs, which have not yet been
3072 expanded, and issues an error if any are found. This operation can
3073 only be done on full expressions or types (e.g., an expression
3074 statement, "if" condition, etc.), because we could have expressions like:
3076 foo(f(g(h(args)))...)
3078 where "args" is a parameter pack. check_for_bare_parameter_packs
3079 should not be called for the subexpressions args, h(args),
3080 g(h(args)), or f(g(h(args))), because we would produce erroneous
3083 Returns TRUE and emits an error if there were bare parameter packs,
3084 returns FALSE otherwise. */
3086 check_for_bare_parameter_packs (tree t)
3088 tree parameter_packs = NULL_TREE;
3089 struct find_parameter_pack_data ppd;
3091 if (!processing_template_decl || !t || t == error_mark_node)
3094 if (TREE_CODE (t) == TYPE_DECL)
3097 ppd.parameter_packs = ¶meter_packs;
3098 ppd.visited = pointer_set_create ();
3099 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3100 pointer_set_destroy (ppd.visited);
3102 if (parameter_packs)
3104 error ("parameter packs not expanded with %<...%>:");
3105 while (parameter_packs)
3107 tree pack = TREE_VALUE (parameter_packs);
3108 tree name = NULL_TREE;
3110 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3111 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3112 name = TYPE_NAME (pack);
3113 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3114 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3116 name = DECL_NAME (pack);
3119 inform (input_location, " %qD", name);
3121 inform (input_location, " <anonymous>");
3123 parameter_packs = TREE_CHAIN (parameter_packs);
3132 /* Expand any parameter packs that occur in the template arguments in
3135 expand_template_argument_pack (tree args)
3137 tree result_args = NULL_TREE;
3138 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3139 int num_result_args = -1;
3141 /* First, determine if we need to expand anything, and the number of
3142 slots we'll need. */
3143 for (in_arg = 0; in_arg < nargs; ++in_arg)
3145 tree arg = TREE_VEC_ELT (args, in_arg);
3146 if (ARGUMENT_PACK_P (arg))
3148 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3149 if (num_result_args < 0)
3150 num_result_args = in_arg + num_packed;
3152 num_result_args += num_packed;
3156 if (num_result_args >= 0)
3161 /* If no expansion is necessary, we're done. */
3162 if (num_result_args < 0)
3165 /* Expand arguments. */
3166 result_args = make_tree_vec (num_result_args);
3167 for (in_arg = 0; in_arg < nargs; ++in_arg)
3169 tree arg = TREE_VEC_ELT (args, in_arg);
3170 if (ARGUMENT_PACK_P (arg))
3172 tree packed = ARGUMENT_PACK_ARGS (arg);
3173 int i, num_packed = TREE_VEC_LENGTH (packed);
3174 for (i = 0; i < num_packed; ++i, ++out_arg)
3175 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3179 TREE_VEC_ELT (result_args, out_arg) = arg;
3187 /* Checks if DECL shadows a template parameter.
3189 [temp.local]: A template-parameter shall not be redeclared within its
3190 scope (including nested scopes).
3192 Emits an error and returns TRUE if the DECL shadows a parameter,
3193 returns FALSE otherwise. */
3196 check_template_shadow (tree decl)
3200 /* If we're not in a template, we can't possibly shadow a template
3202 if (!current_template_parms)
3205 /* Figure out what we're shadowing. */
3206 if (TREE_CODE (decl) == OVERLOAD)
3207 decl = OVL_CURRENT (decl);
3208 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3210 /* If there's no previous binding for this name, we're not shadowing
3211 anything, let alone a template parameter. */
3215 /* If we're not shadowing a template parameter, we're done. Note
3216 that OLDDECL might be an OVERLOAD (or perhaps even an
3217 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3219 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3222 /* We check for decl != olddecl to avoid bogus errors for using a
3223 name inside a class. We check TPFI to avoid duplicate errors for
3224 inline member templates. */
3226 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3229 error ("declaration of %q+#D", decl);
3230 error (" shadows template parm %q+#D", olddecl);
3234 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3235 ORIG_LEVEL, DECL, and TYPE. */
3238 build_template_parm_index (int index,
3244 tree t = make_node (TEMPLATE_PARM_INDEX);
3245 TEMPLATE_PARM_IDX (t) = index;
3246 TEMPLATE_PARM_LEVEL (t) = level;
3247 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3248 TEMPLATE_PARM_DECL (t) = decl;
3249 TREE_TYPE (t) = type;
3250 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3251 TREE_READONLY (t) = TREE_READONLY (decl);
3256 /* Find the canonical type parameter for the given template type
3257 parameter. Returns the canonical type parameter, which may be TYPE
3258 if no such parameter existed. */
3260 canonical_type_parameter (tree type)
3263 int idx = TEMPLATE_TYPE_IDX (type);
3264 if (!canonical_template_parms)
3265 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3267 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3268 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3270 list = VEC_index (tree, canonical_template_parms, idx);
3271 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3272 list = TREE_CHAIN (list);
3275 return TREE_VALUE (list);
3278 VEC_replace(tree, canonical_template_parms, idx,
3279 tree_cons (NULL_TREE, type,
3280 VEC_index (tree, canonical_template_parms, idx)));
3285 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3286 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3287 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3288 new one is created. */
3291 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3292 tsubst_flags_t complain)
3294 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3295 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3296 != TEMPLATE_PARM_LEVEL (index) - levels))
3298 tree orig_decl = TEMPLATE_PARM_DECL (index);
3301 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3302 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3303 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3304 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3305 DECL_ARTIFICIAL (decl) = 1;
3306 SET_DECL_TEMPLATE_PARM_P (decl);
3308 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3309 TEMPLATE_PARM_LEVEL (index) - levels,
3310 TEMPLATE_PARM_ORIG_LEVEL (index),
3312 TEMPLATE_PARM_DESCENDANTS (index) = t;
3313 TEMPLATE_PARM_PARAMETER_PACK (t)
3314 = TEMPLATE_PARM_PARAMETER_PACK (index);
3316 /* Template template parameters need this. */
3317 if (TREE_CODE (decl) == TEMPLATE_DECL)
3318 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3319 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3323 return TEMPLATE_PARM_DESCENDANTS (index);
3326 /* Process information from new template parameter PARM and append it to the
3327 LIST being built. This new parameter is a non-type parameter iff
3328 IS_NON_TYPE is true. This new parameter is a parameter
3329 pack iff IS_PARAMETER_PACK is true. The location of PARM is in
3333 process_template_parm (tree list, location_t parm_loc, tree parm, bool is_non_type,
3334 bool is_parameter_pack)
3341 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3342 defval = TREE_PURPOSE (parm);
3346 tree p = tree_last (list);
3348 if (p && TREE_VALUE (p) != error_mark_node)
3351 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3352 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3354 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3364 parm = TREE_VALUE (parm);
3366 SET_DECL_TEMPLATE_PARM_P (parm);
3368 if (TREE_TYPE (parm) == error_mark_node)
3370 err_parm_list = build_tree_list (defval, parm);
3371 TREE_VALUE (err_parm_list) = error_mark_node;
3372 return chainon (list, err_parm_list);
3378 The top-level cv-qualifiers on the template-parameter are
3379 ignored when determining its type. */
3380 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3381 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3383 err_parm_list = build_tree_list (defval, parm);
3384 TREE_VALUE (err_parm_list) = error_mark_node;
3385 return chainon (list, err_parm_list);
3388 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3390 /* This template parameter is not a parameter pack, but it
3391 should be. Complain about "bare" parameter packs. */
3392 check_for_bare_parameter_packs (TREE_TYPE (parm));
3394 /* Recover by calling this a parameter pack. */
3395 is_parameter_pack = true;
3399 /* A template parameter is not modifiable. */
3400 TREE_CONSTANT (parm) = 1;
3401 TREE_READONLY (parm) = 1;
3402 decl = build_decl (parm_loc,
3403 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3404 TREE_CONSTANT (decl) = 1;
3405 TREE_READONLY (decl) = 1;
3406 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3407 = build_template_parm_index (idx, processing_template_decl,
3408 processing_template_decl,
3409 decl, TREE_TYPE (parm));
3411 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3412 = is_parameter_pack;
3417 parm = TREE_VALUE (TREE_VALUE (parm));
3419 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3421 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3422 /* This is for distinguishing between real templates and template
3423 template parameters */
3424 TREE_TYPE (parm) = t;
3425 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3430 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3431 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3432 decl = build_decl (parm_loc,
3433 TYPE_DECL, parm, t);
3436 TYPE_NAME (t) = decl;
3437 TYPE_STUB_DECL (t) = decl;
3439 TEMPLATE_TYPE_PARM_INDEX (t)
3440 = build_template_parm_index (idx, processing_template_decl,
3441 processing_template_decl,
3442 decl, TREE_TYPE (parm));
3443 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3444 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3446 DECL_ARTIFICIAL (decl) = 1;
3447 SET_DECL_TEMPLATE_PARM_P (decl);
3449 parm = build_tree_list (defval, parm);
3450 return chainon (list, parm);
3453 /* The end of a template parameter list has been reached. Process the
3454 tree list into a parameter vector, converting each parameter into a more
3455 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3459 end_template_parm_list (tree parms)
3463 tree saved_parmlist = make_tree_vec (list_length (parms));
3465 current_template_parms
3466 = tree_cons (size_int (processing_template_decl),
3467 saved_parmlist, current_template_parms);
3469 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3471 next = TREE_CHAIN (parm);
3472 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3473 TREE_CHAIN (parm) = NULL_TREE;
3476 --processing_template_parmlist;
3478 return saved_parmlist;
3481 /* end_template_decl is called after a template declaration is seen. */
3484 end_template_decl (void)
3486 reset_specialization ();
3488 if (! processing_template_decl)
3491 /* This matches the pushlevel in begin_template_parm_list. */
3494 --processing_template_decl;
3495 current_template_parms = TREE_CHAIN (current_template_parms);
3498 /* Within the declaration of a template, return all levels of template
3499 parameters that apply. The template parameters are represented as
3500 a TREE_VEC, in the form documented in cp-tree.h for template
3504 current_template_args (void)
3507 tree args = NULL_TREE;
3508 int length = TMPL_PARMS_DEPTH (current_template_parms);
3511 /* If there is only one level of template parameters, we do not
3512 create a TREE_VEC of TREE_VECs. Instead, we return a single
3513 TREE_VEC containing the arguments. */
3515 args = make_tree_vec (length);
3517 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3519 tree a = copy_node (TREE_VALUE (header));
3522 TREE_TYPE (a) = NULL_TREE;
3523 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3525 tree t = TREE_VEC_ELT (a, i);
3527 /* T will be a list if we are called from within a
3528 begin/end_template_parm_list pair, but a vector directly
3529 if within a begin/end_member_template_processing pair. */
3530 if (TREE_CODE (t) == TREE_LIST)
3534 if (!error_operand_p (t))
3536 if (TREE_CODE (t) == TYPE_DECL
3537 || TREE_CODE (t) == TEMPLATE_DECL)
3541 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3543 /* Turn this argument into a TYPE_ARGUMENT_PACK
3544 with a single element, which expands T. */
3545 tree vec = make_tree_vec (1);
3546 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3548 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3549 SET_ARGUMENT_PACK_ARGS (t, vec);
3554 t = DECL_INITIAL (t);
3556 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3558 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3559 with a single element, which expands T. */
3560 tree vec = make_tree_vec (1);
3561 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3562 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3564 t = make_node (NONTYPE_ARGUMENT_PACK);
3565 SET_ARGUMENT_PACK_ARGS (t, vec);
3566 TREE_TYPE (t) = type;
3569 TREE_VEC_ELT (a, i) = t;
3575 TREE_VEC_ELT (args, --l) = a;
3583 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3584 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3585 a member template. Used by push_template_decl below. */
3588 build_template_decl (tree decl, tree parms, bool member_template_p)
3590 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3591 DECL_TEMPLATE_PARMS (tmpl) = parms;
3592 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3593 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3598 struct template_parm_data
3600 /* The level of the template parameters we are currently
3604 /* The index of the specialization argument we are currently
3608 /* An array whose size is the number of template parameters. The
3609 elements are nonzero if the parameter has been used in any one
3610 of the arguments processed so far. */
3613 /* An array whose size is the number of template arguments. The
3614 elements are nonzero if the argument makes use of template
3615 parameters of this level. */
3616 int* arg_uses_template_parms;
3619 /* Subroutine of push_template_decl used to see if each template
3620 parameter in a partial specialization is used in the explicit
3621 argument list. If T is of the LEVEL given in DATA (which is
3622 treated as a template_parm_data*), then DATA->PARMS is marked
3626 mark_template_parm (tree t, void* data)
3630 struct template_parm_data* tpd = (struct template_parm_data*) data;
3632 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3634 level = TEMPLATE_PARM_LEVEL (t);
3635 idx = TEMPLATE_PARM_IDX (t);
3639 level = TEMPLATE_TYPE_LEVEL (t);
3640 idx = TEMPLATE_TYPE_IDX (t);
3643 if (level == tpd->level)
3645 tpd->parms[idx] = 1;
3646 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3649 /* Return zero so that for_each_template_parm will continue the
3650 traversal of the tree; we want to mark *every* template parm. */
3654 /* Process the partial specialization DECL. */
3657 process_partial_specialization (tree decl)
3659 tree type = TREE_TYPE (decl);
3660 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3661 tree specargs = CLASSTYPE_TI_ARGS (type);
3662 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3663 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3665 int nargs = TREE_VEC_LENGTH (inner_args);
3668 int did_error_intro = 0;
3669 struct template_parm_data tpd;
3670 struct template_parm_data tpd2;
3672 gcc_assert (current_template_parms);
3674 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3675 ntparms = TREE_VEC_LENGTH (inner_parms);
3677 /* We check that each of the template parameters given in the
3678 partial specialization is used in the argument list to the
3679 specialization. For example:
3681 template <class T> struct S;
3682 template <class T> struct S<T*>;
3684 The second declaration is OK because `T*' uses the template
3685 parameter T, whereas
3687 template <class T> struct S<int>;
3689 is no good. Even trickier is:
3700 The S2<T> declaration is actually invalid; it is a
3701 full-specialization. Of course,
3704 struct S2<T (*)(U)>;
3706 or some such would have been OK. */
3707 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3708 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
3709 memset (tpd.parms, 0, sizeof (int) * ntparms);
3711 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
3712 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3713 for (i = 0; i < nargs; ++i)
3715 tpd.current_arg = i;
3716 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3717 &mark_template_parm,
3720 /*include_nondeduced_p=*/false);
3722 for (i = 0; i < ntparms; ++i)
3723 if (tpd.parms[i] == 0)
3725 /* One of the template parms was not used in the
3727 if (!did_error_intro)
3729 error ("template parameters not used in partial specialization:");
3730 did_error_intro = 1;
3733 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3736 /* [temp.class.spec]
3738 The argument list of the specialization shall not be identical to
3739 the implicit argument list of the primary template. */
3740 if (comp_template_args
3742 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3744 error ("partial specialization %qT does not specialize any template arguments", type);
3746 /* [temp.class.spec]
3748 A partially specialized non-type argument expression shall not
3749 involve template parameters of the partial specialization except
3750 when the argument expression is a simple identifier.
3752 The type of a template parameter corresponding to a specialized
3753 non-type argument shall not be dependent on a parameter of the
3756 Also, we verify that pack expansions only occur at the
3757 end of the argument list. */
3758 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3760 for (i = 0; i < nargs; ++i)
3762 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3763 tree arg = TREE_VEC_ELT (inner_args, i);
3764 tree packed_args = NULL_TREE;
3767 if (ARGUMENT_PACK_P (arg))
3769 /* Extract the arguments from the argument pack. We'll be
3770 iterating over these in the following loop. */
3771 packed_args = ARGUMENT_PACK_ARGS (arg);
3772 len = TREE_VEC_LENGTH (packed_args);
3775 for (j = 0; j < len; j++)
3778 /* Get the Jth argument in the parameter pack. */
3779 arg = TREE_VEC_ELT (packed_args, j);
3781 if (PACK_EXPANSION_P (arg))
3783 /* Pack expansions must come at the end of the
3785 if ((packed_args && j < len - 1)
3786 || (!packed_args && i < nargs - 1))
3788 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3789 error ("parameter pack argument %qE must be at the end of the template argument list", arg);
3791 error ("parameter pack argument %qT must be at the end of the template argument list", arg);
3794 TREE_VEC_ELT (packed_args, j) = error_mark_node;
3798 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3799 /* We only care about the pattern. */
3800 arg = PACK_EXPANSION_PATTERN (arg);
3802 if (/* These first two lines are the `non-type' bit. */
3804 && TREE_CODE (arg) != TEMPLATE_DECL
3805 /* This next line is the `argument expression is not just a
3806 simple identifier' condition and also the `specialized
3807 non-type argument' bit. */
3808 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3810 if ((!packed_args && tpd.arg_uses_template_parms[i])
3811 || (packed_args && uses_template_parms (arg)))
3812 error ("template argument %qE involves template parameter(s)",
3816 /* Look at the corresponding template parameter,
3817 marking which template parameters its type depends
3819 tree type = TREE_TYPE (parm);
3823 /* We haven't yet initialized TPD2. Do so now. */
3824 tpd2.arg_uses_template_parms
3825 = (int *) alloca (sizeof (int) * nargs);
3826 /* The number of parameters here is the number in the
3827 main template, which, as checked in the assertion
3829 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
3831 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
3834 /* Mark the template parameters. But this time, we're
3835 looking for the template parameters of the main
3836 template, not in the specialization. */
3837 tpd2.current_arg = i;
3838 tpd2.arg_uses_template_parms[i] = 0;
3839 memset (tpd2.parms, 0, sizeof (int) * nargs);
3840 for_each_template_parm (type,
3841 &mark_template_parm,
3844 /*include_nondeduced_p=*/false);
3846 if (tpd2.arg_uses_template_parms [i])
3848 /* The type depended on some template parameters.
3849 If they are fully specialized in the
3850 specialization, that's OK. */
3852 for (j = 0; j < nargs; ++j)
3853 if (tpd2.parms[j] != 0
3854 && tpd.arg_uses_template_parms [j])
3856 error ("type %qT of template argument %qE depends "
3857 "on template parameter(s)",
3868 /* We should only get here once. */
3869 gcc_assert (!COMPLETE_TYPE_P (type));
3871 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
3872 = tree_cons (specargs, inner_parms,
3873 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
3874 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
3878 /* Check that a template declaration's use of default arguments and
3879 parameter packs is not invalid. Here, PARMS are the template
3880 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
3881 a primary template. IS_PARTIAL is nonzero if DECL is a partial
3885 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3886 declaration (but not a definition); 1 indicates a declaration, 2
3887 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3888 emitted for extraneous default arguments.
3890 Returns TRUE if there were no errors found, FALSE otherwise. */
3893 check_default_tmpl_args (tree decl, tree parms, int is_primary,
3894 int is_partial, int is_friend_decl)
3897 int last_level_to_check;
3899 bool no_errors = true;
3903 A default template-argument shall not be specified in a
3904 function template declaration or a function template definition, nor
3905 in the template-parameter-list of the definition of a member of a
3908 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
3909 /* You can't have a function template declaration in a local
3910 scope, nor you can you define a member of a class template in a
3914 if (current_class_type
3915 && !TYPE_BEING_DEFINED (current_class_type)
3916 && DECL_LANG_SPECIFIC (decl)
3917 && DECL_DECLARES_FUNCTION_P (decl)
3918 /* If this is either a friend defined in the scope of the class
3919 or a member function. */
3920 && (DECL_FUNCTION_MEMBER_P (decl)
3921 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
3922 : DECL_FRIEND_CONTEXT (decl)
3923 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
3925 /* And, if it was a member function, it really was defined in
3926 the scope of the class. */
3927 && (!DECL_FUNCTION_MEMBER_P (decl)
3928 || DECL_INITIALIZED_IN_CLASS_P (decl)))
3929 /* We already checked these parameters when the template was
3930 declared, so there's no need to do it again now. This function
3931 was defined in class scope, but we're processing it's body now
3932 that the class is complete. */
3935 /* Core issue 226 (C++0x only): the following only applies to class
3937 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
3941 If a template-parameter has a default template-argument, all
3942 subsequent template-parameters shall have a default
3943 template-argument supplied. */
3944 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
3946 tree inner_parms = TREE_VALUE (parm_level);
3947 int ntparms = TREE_VEC_LENGTH (inner_parms);
3948 int seen_def_arg_p = 0;
3951 for (i = 0; i < ntparms; ++i)
3953 tree parm = TREE_VEC_ELT (inner_parms, i);
3955 if (parm == error_mark_node)
3958 if (TREE_PURPOSE (parm))
3960 else if (seen_def_arg_p
3961 && !template_parameter_pack_p (TREE_VALUE (parm)))
3963 error ("no default argument for %qD", TREE_VALUE (parm));
3964 /* For better subsequent error-recovery, we indicate that
3965 there should have been a default argument. */
3966 TREE_PURPOSE (parm) = error_mark_node;
3972 /* Don't complain about an enclosing partial
3974 && parm_level == parms
3975 && TREE_CODE (decl) == TYPE_DECL
3977 && template_parameter_pack_p (TREE_VALUE (parm)))
3979 /* A primary class template can only have one
3980 parameter pack, at the end of the template
3983 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
3984 error ("parameter pack %qE must be at the end of the"
3985 " template parameter list", TREE_VALUE (parm));
3987 error ("parameter pack %qT must be at the end of the"
3988 " template parameter list",
3989 TREE_TYPE (TREE_VALUE (parm)));
3991 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
3999 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4003 /* For an ordinary class template, default template arguments are
4004 allowed at the innermost level, e.g.:
4005 template <class T = int>
4007 but, in a partial specialization, they're not allowed even
4008 there, as we have in [temp.class.spec]:
4010 The template parameter list of a specialization shall not
4011 contain default template argument values.
4013 So, for a partial specialization, or for a function template
4014 (in C++98/C++03), we look at all of them. */
4017 /* But, for a primary class template that is not a partial
4018 specialization we look at all template parameters except the
4020 parms = TREE_CHAIN (parms);
4022 /* Figure out what error message to issue. */
4023 if (is_friend_decl == 2)
4024 msg = "default template arguments may not be used in function template friend re-declaration";
4025 else if (is_friend_decl)
4026 msg = "default template arguments may not be used in function template friend declarations";
4027 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4028 msg = "default template arguments may not be used in function templates";
4029 else if (is_partial)
4030 msg = "default template arguments may not be used in partial specializations";
4032 msg = "default argument for template parameter for class enclosing %qD";
4034 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4035 /* If we're inside a class definition, there's no need to
4036 examine the parameters to the class itself. On the one
4037 hand, they will be checked when the class is defined, and,
4038 on the other, default arguments are valid in things like:
4039 template <class T = double>
4040 struct S { template <class U> void f(U); };
4041 Here the default argument for `S' has no bearing on the
4042 declaration of `f'. */
4043 last_level_to_check = template_class_depth (current_class_type) + 1;
4045 /* Check everything. */
4046 last_level_to_check = 0;
4048 for (parm_level = parms;
4049 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4050 parm_level = TREE_CHAIN (parm_level))
4052 tree inner_parms = TREE_VALUE (parm_level);
4056 ntparms = TREE_VEC_LENGTH (inner_parms);
4057 for (i = 0; i < ntparms; ++i)
4059 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4062 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4067 if (is_friend_decl == 2)
4074 /* Clear out the default argument so that we are not
4076 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4080 /* At this point, if we're still interested in issuing messages,
4081 they must apply to classes surrounding the object declared. */
4083 msg = "default argument for template parameter for class enclosing %qD";
4089 /* Worker for push_template_decl_real, called via
4090 for_each_template_parm. DATA is really an int, indicating the
4091 level of the parameters we are interested in. If T is a template
4092 parameter of that level, return nonzero. */
4095 template_parm_this_level_p (tree t, void* data)
4097 int this_level = *(int *)data;
4100 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4101 level = TEMPLATE_PARM_LEVEL (t);
4103 level = TEMPLATE_TYPE_LEVEL (t);
4104 return level == this_level;
4107 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4108 parameters given by current_template_args, or reuses a
4109 previously existing one, if appropriate. Returns the DECL, or an
4110 equivalent one, if it is replaced via a call to duplicate_decls.
4112 If IS_FRIEND is true, DECL is a friend declaration. */
4115 push_template_decl_real (tree decl, bool is_friend)
4123 int new_template_p = 0;
4124 /* True if the template is a member template, in the sense of
4126 bool member_template_p = false;
4128 if (decl == error_mark_node || !current_template_parms)
4129 return error_mark_node;
4131 /* See if this is a partial specialization. */
4132 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4133 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4134 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4136 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4140 /* For a friend, we want the context of the friend function, not
4141 the type of which it is a friend. */
4142 ctx = DECL_CONTEXT (decl);
4143 else if (CP_DECL_CONTEXT (decl)
4144 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4145 /* In the case of a virtual function, we want the class in which
4147 ctx = CP_DECL_CONTEXT (decl);
4149 /* Otherwise, if we're currently defining some class, the DECL
4150 is assumed to be a member of the class. */
4151 ctx = current_scope ();
4153 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4156 if (!DECL_CONTEXT (decl))
4157 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4159 /* See if this is a primary template. */
4160 if (is_friend && ctx)
4161 /* A friend template that specifies a class context, i.e.
4162 template <typename T> friend void A<T>::f();
4166 primary = template_parm_scope_p ();
4170 if (DECL_CLASS_SCOPE_P (decl))
4171 member_template_p = true;
4172 if (TREE_CODE (decl) == TYPE_DECL
4173 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4175 error ("template class without a name");
4176 return error_mark_node;
4178 else if (TREE_CODE (decl) == FUNCTION_DECL)
4180 if (DECL_DESTRUCTOR_P (decl))
4184 A destructor shall not be a member template. */
4185 error ("destructor %qD declared as member template", decl);
4186 return error_mark_node;
4188 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4189 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
4190 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4191 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4192 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4193 == void_list_node)))
4195 /* [basic.stc.dynamic.allocation]
4197 An allocation function can be a function
4198 template. ... Template allocation functions shall
4199 have two or more parameters. */
4200 error ("invalid template declaration of %qD", decl);
4201 return error_mark_node;
4204 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4205 && CLASS_TYPE_P (TREE_TYPE (decl)))
4209 error ("template declaration of %q#D", decl);
4210 return error_mark_node;
4214 /* Check to see that the rules regarding the use of default
4215 arguments are not being violated. */
4216 check_default_tmpl_args (decl, current_template_parms,
4217 primary, is_partial, /*is_friend_decl=*/0);
4219 /* Ensure that there are no parameter packs in the type of this
4220 declaration that have not been expanded. */
4221 if (TREE_CODE (decl) == FUNCTION_DECL)
4223 /* Check each of the arguments individually to see if there are
4224 any bare parameter packs. */
4225 tree type = TREE_TYPE (decl);
4226 tree arg = DECL_ARGUMENTS (decl);
4227 tree argtype = TYPE_ARG_TYPES (type);
4229 while (arg && argtype)
4231 if (!FUNCTION_PARAMETER_PACK_P (arg)
4232 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4234 /* This is a PARM_DECL that contains unexpanded parameter
4235 packs. We have already complained about this in the
4236 check_for_bare_parameter_packs call, so just replace
4237 these types with ERROR_MARK_NODE. */
4238 TREE_TYPE (arg) = error_mark_node;
4239 TREE_VALUE (argtype) = error_mark_node;
4242 arg = TREE_CHAIN (arg);
4243 argtype = TREE_CHAIN (argtype);
4246 /* Check for bare parameter packs in the return type and the
4247 exception specifiers. */
4248 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4249 /* Errors were already issued, set return type to int
4250 as the frontend doesn't expect error_mark_node as
4252 TREE_TYPE (type) = integer_type_node;
4253 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4254 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4256 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
4258 TREE_TYPE (decl) = error_mark_node;
4259 return error_mark_node;
4263 return process_partial_specialization (decl);
4265 args = current_template_args ();
4268 || TREE_CODE (ctx) == FUNCTION_DECL
4269 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4270 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4272 if (DECL_LANG_SPECIFIC (decl)
4273 && DECL_TEMPLATE_INFO (decl)
4274 && DECL_TI_TEMPLATE (decl))
4275 tmpl = DECL_TI_TEMPLATE (decl);
4276 /* If DECL is a TYPE_DECL for a class-template, then there won't
4277 be DECL_LANG_SPECIFIC. The information equivalent to
4278 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4279 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4280 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4281 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4283 /* Since a template declaration already existed for this
4284 class-type, we must be redeclaring it here. Make sure
4285 that the redeclaration is valid. */
4286 redeclare_class_template (TREE_TYPE (decl),
4287 current_template_parms);
4288 /* We don't need to create a new TEMPLATE_DECL; just use the
4289 one we already had. */
4290 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4294 tmpl = build_template_decl (decl, current_template_parms,
4298 if (DECL_LANG_SPECIFIC (decl)
4299 && DECL_TEMPLATE_SPECIALIZATION (decl))
4301 /* A specialization of a member template of a template
4303 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4304 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4305 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4311 tree a, t, current, parms;
4313 tree tinfo = get_template_info (decl);
4317 error ("template definition of non-template %q#D", decl);
4318 return error_mark_node;
4321 tmpl = TI_TEMPLATE (tinfo);
4323 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4324 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4325 && DECL_TEMPLATE_SPECIALIZATION (decl)
4326 && DECL_MEMBER_TEMPLATE_P (tmpl))
4330 /* The declaration is a specialization of a member
4331 template, declared outside the class. Therefore, the
4332 innermost template arguments will be NULL, so we
4333 replace them with the arguments determined by the
4334 earlier call to check_explicit_specialization. */
4335 args = DECL_TI_ARGS (decl);
4338 = build_template_decl (decl, current_template_parms,
4340 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4341 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4342 DECL_TI_TEMPLATE (decl) = new_tmpl;
4343 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4344 DECL_TEMPLATE_INFO (new_tmpl)
4345 = tree_cons (tmpl, args, NULL_TREE);
4347 register_specialization (new_tmpl,
4348 most_general_template (tmpl),
4354 /* Make sure the template headers we got make sense. */
4356 parms = DECL_TEMPLATE_PARMS (tmpl);
4357 i = TMPL_PARMS_DEPTH (parms);
4358 if (TMPL_ARGS_DEPTH (args) != i)
4360 error ("expected %d levels of template parms for %q#D, got %d",
4361 i, decl, TMPL_ARGS_DEPTH (args));
4364 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4366 a = TMPL_ARGS_LEVEL (args, i);
4367 t = INNERMOST_TEMPLATE_PARMS (parms);
4369 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4371 if (current == decl)
4372 error ("got %d template parameters for %q#D",
4373 TREE_VEC_LENGTH (a), decl);
4375 error ("got %d template parameters for %q#T",
4376 TREE_VEC_LENGTH (a), current);
4377 error (" but %d required", TREE_VEC_LENGTH (t));
4378 return error_mark_node;
4381 if (current == decl)
4384 current = (TYPE_P (current)
4385 ? TYPE_CONTEXT (current)
4386 : DECL_CONTEXT (current));
4389 /* Check that the parms are used in the appropriate qualifying scopes
4390 in the declarator. */
4391 if (!comp_template_args
4393 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4396 template arguments to %qD do not match original template %qD",
4397 decl, DECL_TEMPLATE_RESULT (tmpl));
4398 if (!uses_template_parms (TI_ARGS (tinfo)))
4399 inform (input_location, "use template<> for an explicit specialization");
4400 /* Avoid crash in import_export_decl. */
4401 DECL_INTERFACE_KNOWN (decl) = 1;
4402 return error_mark_node;
4406 DECL_TEMPLATE_RESULT (tmpl) = decl;
4407 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4409 /* Push template declarations for global functions and types. Note
4410 that we do not try to push a global template friend declared in a
4411 template class; such a thing may well depend on the template
4412 parameters of the class. */
4413 if (new_template_p && !ctx
4414 && !(is_friend && template_class_depth (current_class_type) > 0))
4416 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4417 if (tmpl == error_mark_node)
4418 return error_mark_node;
4420 /* Hide template friend classes that haven't been declared yet. */
4421 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4423 DECL_ANTICIPATED (tmpl) = 1;
4424 DECL_FRIEND_P (tmpl) = 1;
4430 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4433 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4434 if (DECL_CONV_FN_P (tmpl))
4436 int depth = TMPL_PARMS_DEPTH (parms);
4438 /* It is a conversion operator. See if the type converted to
4439 depends on innermost template operands. */
4441 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4443 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4446 /* Give template template parms a DECL_CONTEXT of the template
4447 for which they are a parameter. */
4448 parms = INNERMOST_TEMPLATE_PARMS (parms);
4449 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4451 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4452 if (TREE_CODE (parm) == TEMPLATE_DECL)
4453 DECL_CONTEXT (parm) = tmpl;
4457 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4458 back to its most general template. If TMPL is a specialization,
4459 ARGS may only have the innermost set of arguments. Add the missing
4460 argument levels if necessary. */
4461 if (DECL_TEMPLATE_INFO (tmpl))
4462 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4464 info = tree_cons (tmpl, args, NULL_TREE);
4466 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4467 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4468 else if (DECL_LANG_SPECIFIC (decl))
4469 DECL_TEMPLATE_INFO (decl) = info;
4471 return DECL_TEMPLATE_RESULT (tmpl);
4475 push_template_decl (tree decl)
4477 return push_template_decl_real (decl, false);
4480 /* Called when a class template TYPE is redeclared with the indicated
4481 template PARMS, e.g.:
4483 template <class T> struct S;
4484 template <class T> struct S {}; */
4487 redeclare_class_template (tree type, tree parms)
4493 if (!TYPE_TEMPLATE_INFO (type))
4495 error ("%qT is not a template type", type);
4499 tmpl = TYPE_TI_TEMPLATE (type);
4500 if (!PRIMARY_TEMPLATE_P (tmpl))
4501 /* The type is nested in some template class. Nothing to worry
4502 about here; there are no new template parameters for the nested
4508 error ("template specifiers not specified in declaration of %qD",
4513 parms = INNERMOST_TEMPLATE_PARMS (parms);
4514 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4516 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4518 error ("redeclared with %d template parameter(s)",
4519 TREE_VEC_LENGTH (parms));
4520 inform (input_location, "previous declaration %q+D used %d template parameter(s)",
4521 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4525 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4532 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4533 || TREE_VEC_ELT (parms, i) == error_mark_node)
4536 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4537 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4538 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4539 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4541 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4543 if (tmpl_parm != error_mark_node
4544 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4545 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4546 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4547 || (TREE_CODE (tmpl_parm) != PARM_DECL
4548 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4549 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4550 || (TREE_CODE (tmpl_parm) == PARM_DECL
4551 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4552 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))))))
4554 error ("template parameter %q+#D", tmpl_parm);
4555 error ("redeclared here as %q#D", parm);
4559 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4561 /* We have in [temp.param]:
4563 A template-parameter may not be given default arguments
4564 by two different declarations in the same scope. */
4565 error_at (input_location, "redefinition of default argument for %q#D", parm);
4566 inform (DECL_SOURCE_LOCATION (tmpl_parm),
4567 "original definition appeared here");
4571 if (parm_default != NULL_TREE)
4572 /* Update the previous template parameters (which are the ones
4573 that will really count) with the new default value. */
4574 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4575 else if (tmpl_default != NULL_TREE)
4576 /* Update the new parameters, too; they'll be used as the
4577 parameters for any members. */
4578 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4584 /* Simplify EXPR if it is a non-dependent expression. Returns the
4585 (possibly simplified) expression. */
4588 fold_non_dependent_expr (tree expr)
4590 if (expr == NULL_TREE)
4593 /* If we're in a template, but EXPR isn't value dependent, simplify
4594 it. We're supposed to treat:
4596 template <typename T> void f(T[1 + 1]);
4597 template <typename T> void f(T[2]);
4599 as two declarations of the same function, for example. */
4600 if (processing_template_decl
4601 && !type_dependent_expression_p (expr)
4602 && !value_dependent_expression_p (expr))
4604 HOST_WIDE_INT saved_processing_template_decl;
4606 saved_processing_template_decl = processing_template_decl;
4607 processing_template_decl = 0;
4608 expr = tsubst_copy_and_build (expr,
4611 /*in_decl=*/NULL_TREE,
4612 /*function_p=*/false,
4613 /*integral_constant_expression_p=*/true);
4614 processing_template_decl = saved_processing_template_decl;
4619 /* EXPR is an expression which is used in a constant-expression context.
4620 For instance, it could be a VAR_DECL with a constant initializer.
4621 Extract the innermost constant expression.
4623 This is basically a more powerful version of
4624 integral_constant_value, which can be used also in templates where
4625 initializers can maintain a syntactic rather than semantic form
4626 (even if they are non-dependent, for access-checking purposes). */
4629 fold_decl_constant_value (tree expr)
4631 tree const_expr = expr;
4634 expr = fold_non_dependent_expr (const_expr);
4635 const_expr = integral_constant_value (expr);
4637 while (expr != const_expr);
4642 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4643 must be a function or a pointer-to-function type, as specified
4644 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4645 and check that the resulting function has external linkage. */
4648 convert_nontype_argument_function (tree type, tree expr)
4653 fn = instantiate_type (type, fns, tf_none);
4654 if (fn == error_mark_node)
4655 return error_mark_node;
4658 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4659 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4660 if (TREE_CODE (fn_no_ptr) == BASELINK)
4661 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4663 /* [temp.arg.nontype]/1
4665 A template-argument for a non-type, non-template template-parameter
4668 -- the address of an object or function with external linkage. */
4669 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4671 error ("%qE is not a valid template argument for type %qT "
4672 "because function %qD has not external linkage",
4673 expr, type, fn_no_ptr);
4680 /* Attempt to convert the non-type template parameter EXPR to the
4681 indicated TYPE. If the conversion is successful, return the
4682 converted value. If the conversion is unsuccessful, return
4683 NULL_TREE if we issued an error message, or error_mark_node if we
4684 did not. We issue error messages for out-and-out bad template
4685 parameters, but not simply because the conversion failed, since we
4686 might be just trying to do argument deduction. Both TYPE and EXPR
4687 must be non-dependent.
4689 The conversion follows the special rules described in
4690 [temp.arg.nontype], and it is much more strict than an implicit
4693 This function is called twice for each template argument (see
4694 lookup_template_class for a more accurate description of this
4695 problem). This means that we need to handle expressions which
4696 are not valid in a C++ source, but can be created from the
4697 first call (for instance, casts to perform conversions). These
4698 hacks can go away after we fix the double coercion problem. */
4701 convert_nontype_argument (tree type, tree expr)
4705 /* Detect immediately string literals as invalid non-type argument.
4706 This special-case is not needed for correctness (we would easily
4707 catch this later), but only to provide better diagnostic for this
4708 common user mistake. As suggested by DR 100, we do not mention
4709 linkage issues in the diagnostic as this is not the point. */
4710 if (TREE_CODE (expr) == STRING_CST)
4712 error ("%qE is not a valid template argument for type %qT "
4713 "because string literals can never be used in this context",
4718 /* If we are in a template, EXPR may be non-dependent, but still
4719 have a syntactic, rather than semantic, form. For example, EXPR
4720 might be a SCOPE_REF, rather than the VAR_DECL to which the
4721 SCOPE_REF refers. Preserving the qualifying scope is necessary
4722 so that access checking can be performed when the template is
4723 instantiated -- but here we need the resolved form so that we can
4724 convert the argument. */
4725 expr = fold_non_dependent_expr (expr);
4726 if (error_operand_p (expr))
4727 return error_mark_node;
4728 expr_type = TREE_TYPE (expr);
4730 /* HACK: Due to double coercion, we can get a
4731 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4732 which is the tree that we built on the first call (see
4733 below when coercing to reference to object or to reference to
4734 function). We just strip everything and get to the arg.
4735 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4737 if (TREE_CODE (expr) == NOP_EXPR)
4739 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
4741 /* ??? Maybe we could use convert_from_reference here, but we
4742 would need to relax its constraints because the NOP_EXPR
4743 could actually change the type to something more cv-qualified,
4744 and this is not folded by convert_from_reference. */
4745 tree addr = TREE_OPERAND (expr, 0);
4746 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
4747 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
4748 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
4749 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4750 (TREE_TYPE (expr_type),
4751 TREE_TYPE (TREE_TYPE (addr))));
4753 expr = TREE_OPERAND (addr, 0);
4754 expr_type = TREE_TYPE (expr);
4757 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4758 parameter is a pointer to object, through decay and
4759 qualification conversion. Let's strip everything. */
4760 else if (TYPE_PTROBV_P (type))
4763 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
4764 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
4765 /* Skip the ADDR_EXPR only if it is part of the decay for
4766 an array. Otherwise, it is part of the original argument
4767 in the source code. */
4768 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
4769 expr = TREE_OPERAND (expr, 0);
4770 expr_type = TREE_TYPE (expr);
4774 /* [temp.arg.nontype]/5, bullet 1
4776 For a non-type template-parameter of integral or enumeration type,
4777 integral promotions (_conv.prom_) and integral conversions
4778 (_conv.integral_) are applied. */
4779 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4781 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type))
4782 return error_mark_node;
4784 expr = fold_decl_constant_value (expr);
4785 /* Notice that there are constant expressions like '4 % 0' which
4786 do not fold into integer constants. */
4787 if (TREE_CODE (expr) != INTEGER_CST)
4789 error ("%qE is not a valid template argument for type %qT "
4790 "because it is a non-constant expression", expr, type);
4794 /* At this point, an implicit conversion does what we want,
4795 because we already know that the expression is of integral
4797 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
4798 if (expr == error_mark_node)
4799 return error_mark_node;
4801 /* Conversion was allowed: fold it to a bare integer constant. */
4804 /* [temp.arg.nontype]/5, bullet 2
4806 For a non-type template-parameter of type pointer to object,
4807 qualification conversions (_conv.qual_) and the array-to-pointer
4808 conversion (_conv.array_) are applied. */
4809 else if (TYPE_PTROBV_P (type))
4811 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
4813 A template-argument for a non-type, non-template template-parameter
4814 shall be one of: [...]
4816 -- the name of a non-type template-parameter;
4817 -- the address of an object or function with external linkage, [...]
4818 expressed as "& id-expression" where the & is optional if the name
4819 refers to a function or array, or if the corresponding
4820 template-parameter is a reference.
4822 Here, we do not care about functions, as they are invalid anyway
4823 for a parameter of type pointer-to-object. */
4825 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
4826 /* Non-type template parameters are OK. */
4828 else if (TREE_CODE (expr) != ADDR_EXPR
4829 && TREE_CODE (expr_type) != ARRAY_TYPE)
4831 if (TREE_CODE (expr) == VAR_DECL)
4833 error ("%qD is not a valid template argument "
4834 "because %qD is a variable, not the address of "
4839 /* Other values, like integer constants, might be valid
4840 non-type arguments of some other type. */
4841 return error_mark_node;
4847 decl = ((TREE_CODE (expr) == ADDR_EXPR)
4848 ? TREE_OPERAND (expr, 0) : expr);
4849 if (TREE_CODE (decl) != VAR_DECL)
4851 error ("%qE is not a valid template argument of type %qT "
4852 "because %qE is not a variable",
4856 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
4858 error ("%qE is not a valid template argument of type %qT "
4859 "because %qD does not have external linkage",
4865 expr = decay_conversion (expr);
4866 if (expr == error_mark_node)
4867 return error_mark_node;
4869 expr = perform_qualification_conversions (type, expr);
4870 if (expr == error_mark_node)
4871 return error_mark_node;
4873 /* [temp.arg.nontype]/5, bullet 3
4875 For a non-type template-parameter of type reference to object, no
4876 conversions apply. The type referred to by the reference may be more
4877 cv-qualified than the (otherwise identical) type of the
4878 template-argument. The template-parameter is bound directly to the
4879 template-argument, which must be an lvalue. */
4880 else if (TYPE_REF_OBJ_P (type))
4882 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
4884 return error_mark_node;
4886 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
4888 error ("%qE is not a valid template argument for type %qT "
4889 "because of conflicts in cv-qualification", expr, type);
4893 if (!real_lvalue_p (expr))
4895 error ("%qE is not a valid template argument for type %qT "
4896 "because it is not an lvalue", expr, type);
4900 /* [temp.arg.nontype]/1
4902 A template-argument for a non-type, non-template template-parameter
4903 shall be one of: [...]
4905 -- the address of an object or function with external linkage. */
4906 if (!DECL_EXTERNAL_LINKAGE_P (expr))
4908 error ("%qE is not a valid template argument for type %qT "
4909 "because object %qD has not external linkage",
4914 expr = build_nop (type, build_address (expr));
4916 /* [temp.arg.nontype]/5, bullet 4
4918 For a non-type template-parameter of type pointer to function, only
4919 the function-to-pointer conversion (_conv.func_) is applied. If the
4920 template-argument represents a set of overloaded functions (or a
4921 pointer to such), the matching function is selected from the set
4923 else if (TYPE_PTRFN_P (type))
4925 /* If the argument is a template-id, we might not have enough
4926 context information to decay the pointer. */
4927 if (!type_unknown_p (expr_type))
4929 expr = decay_conversion (expr);
4930 if (expr == error_mark_node)
4931 return error_mark_node;
4934 expr = convert_nontype_argument_function (type, expr);
4935 if (!expr || expr == error_mark_node)
4938 if (TREE_CODE (expr) != ADDR_EXPR)
4940 error ("%qE is not a valid template argument for type %qT", expr, type);
4941 error ("it must be the address of a function with external linkage");
4945 /* [temp.arg.nontype]/5, bullet 5
4947 For a non-type template-parameter of type reference to function, no
4948 conversions apply. If the template-argument represents a set of
4949 overloaded functions, the matching function is selected from the set
4951 else if (TYPE_REFFN_P (type))
4953 if (TREE_CODE (expr) == ADDR_EXPR)
4955 error ("%qE is not a valid template argument for type %qT "
4956 "because it is a pointer", expr, type);
4957 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
4961 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
4962 if (!expr || expr == error_mark_node)
4965 expr = build_nop (type, build_address (expr));
4967 /* [temp.arg.nontype]/5, bullet 6
4969 For a non-type template-parameter of type pointer to member function,
4970 no conversions apply. If the template-argument represents a set of
4971 overloaded member functions, the matching member function is selected
4972 from the set (_over.over_). */
4973 else if (TYPE_PTRMEMFUNC_P (type))
4975 expr = instantiate_type (type, expr, tf_none);
4976 if (expr == error_mark_node)
4977 return error_mark_node;
4979 /* There is no way to disable standard conversions in
4980 resolve_address_of_overloaded_function (called by
4981 instantiate_type). It is possible that the call succeeded by
4982 converting &B::I to &D::I (where B is a base of D), so we need
4983 to reject this conversion here.
4985 Actually, even if there was a way to disable standard conversions,
4986 it would still be better to reject them here so that we can
4987 provide a superior diagnostic. */
4988 if (!same_type_p (TREE_TYPE (expr), type))
4990 /* Make sure we are just one standard conversion off. */
4991 gcc_assert (can_convert (type, TREE_TYPE (expr)));
4992 error ("%qE is not a valid template argument for type %qT "
4993 "because it is of type %qT", expr, type,
4995 inform (input_location, "standard conversions are not allowed in this context");
4999 /* [temp.arg.nontype]/5, bullet 7
5001 For a non-type template-parameter of type pointer to data member,
5002 qualification conversions (_conv.qual_) are applied. */
5003 else if (TYPE_PTRMEM_P (type))
5005 expr = perform_qualification_conversions (type, expr);
5006 if (expr == error_mark_node)
5009 /* A template non-type parameter must be one of the above. */
5013 /* Sanity check: did we actually convert the argument to the
5015 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
5019 /* Subroutine of coerce_template_template_parms, which returns 1 if
5020 PARM_PARM and ARG_PARM match using the rule for the template
5021 parameters of template template parameters. Both PARM and ARG are
5022 template parameters; the rest of the arguments are the same as for
5023 coerce_template_template_parms.
5026 coerce_template_template_parm (tree parm,
5028 tsubst_flags_t complain,
5032 if (arg == NULL_TREE || arg == error_mark_node
5033 || parm == NULL_TREE || parm == error_mark_node)
5036 if (TREE_CODE (arg) != TREE_CODE (parm))
5039 switch (TREE_CODE (parm))
5042 /* We encounter instantiations of templates like
5043 template <template <template <class> class> class TT>
5046 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5047 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5049 if (!coerce_template_template_parms
5050 (parmparm, argparm, complain, in_decl, outer_args))
5056 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5057 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5058 /* Argument is a parameter pack but parameter is not. */
5063 /* The tsubst call is used to handle cases such as
5065 template <int> class C {};
5066 template <class T, template <T> class TT> class D {};
5069 i.e. the parameter list of TT depends on earlier parameters. */
5070 if (!uses_template_parms (TREE_TYPE (arg))
5072 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5076 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5077 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5078 /* Argument is a parameter pack but parameter is not. */
5091 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5092 template template parameters. Both PARM_PARMS and ARG_PARMS are
5093 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5096 Consider the example:
5097 template <class T> class A;
5098 template<template <class U> class TT> class B;
5100 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5101 the parameters to A, and OUTER_ARGS contains A. */
5104 coerce_template_template_parms (tree parm_parms,
5106 tsubst_flags_t complain,
5110 int nparms, nargs, i;
5114 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5115 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5117 nparms = TREE_VEC_LENGTH (parm_parms);
5118 nargs = TREE_VEC_LENGTH (arg_parms);
5120 /* Determine whether we have a parameter pack at the end of the
5121 template template parameter's template parameter list. */
5122 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5124 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5126 if (parm == error_mark_node)
5129 switch (TREE_CODE (parm))
5133 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5138 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5148 && !(variadic_p && nargs >= nparms - 1))
5151 /* Check all of the template parameters except the parameter pack at
5152 the end (if any). */
5153 for (i = 0; i < nparms - variadic_p; ++i)
5155 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5156 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5159 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5160 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5162 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5170 /* Check each of the template parameters in the template
5171 argument against the template parameter pack at the end of
5172 the template template parameter. */
5173 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
5176 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5178 for (; i < nargs; ++i)
5180 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5183 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5185 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5194 /* Verifies that the deduced template arguments (in TARGS) for the
5195 template template parameters (in TPARMS) represent valid bindings,
5196 by comparing the template parameter list of each template argument
5197 to the template parameter list of its corresponding template
5198 template parameter, in accordance with DR150. This
5199 routine can only be called after all template arguments have been
5200 deduced. It will return TRUE if all of the template template
5201 parameter bindings are okay, FALSE otherwise. */
5203 template_template_parm_bindings_ok_p (tree tparms, tree targs)
5205 int i, ntparms = TREE_VEC_LENGTH (tparms);
5208 /* We're dealing with template parms in this process. */
5209 ++processing_template_decl;
5211 targs = INNERMOST_TEMPLATE_ARGS (targs);
5213 for (i = 0; i < ntparms; ++i)
5215 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
5216 tree targ = TREE_VEC_ELT (targs, i);
5218 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
5220 tree packed_args = NULL_TREE;
5223 if (ARGUMENT_PACK_P (targ))
5225 /* Look inside the argument pack. */
5226 packed_args = ARGUMENT_PACK_ARGS (targ);
5227 len = TREE_VEC_LENGTH (packed_args);
5230 for (idx = 0; idx < len; ++idx)
5232 tree targ_parms = NULL_TREE;
5235 /* Extract the next argument from the argument
5237 targ = TREE_VEC_ELT (packed_args, idx);
5239 if (PACK_EXPANSION_P (targ))
5240 /* Look at the pattern of the pack expansion. */
5241 targ = PACK_EXPANSION_PATTERN (targ);
5243 /* Extract the template parameters from the template
5245 if (TREE_CODE (targ) == TEMPLATE_DECL)
5246 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
5247 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
5248 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
5250 /* Verify that we can coerce the template template
5251 parameters from the template argument to the template
5252 parameter. This requires an exact match. */
5254 && !coerce_template_template_parms
5255 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
5270 --processing_template_decl;
5274 /* Convert the indicated template ARG as necessary to match the
5275 indicated template PARM. Returns the converted ARG, or
5276 error_mark_node if the conversion was unsuccessful. Error and
5277 warning messages are issued under control of COMPLAIN. This
5278 conversion is for the Ith parameter in the parameter list. ARGS is
5279 the full set of template arguments deduced so far. */
5282 convert_template_argument (tree parm,
5285 tsubst_flags_t complain,
5291 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
5293 if (TREE_CODE (arg) == TREE_LIST
5294 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
5296 /* The template argument was the name of some
5297 member function. That's usually
5298 invalid, but static members are OK. In any
5299 case, grab the underlying fields/functions
5300 and issue an error later if required. */
5301 orig_arg = TREE_VALUE (arg);
5302 TREE_TYPE (arg) = unknown_type_node;
5307 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
5308 requires_type = (TREE_CODE (parm) == TYPE_DECL
5309 || requires_tmpl_type);
5311 /* When determining whether an argument pack expansion is a template,
5312 look at the pattern. */
5313 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
5314 arg = PACK_EXPANSION_PATTERN (arg);
5317 ((TREE_CODE (arg) == TEMPLATE_DECL
5318 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
5319 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5320 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
5323 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5324 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
5325 arg = TYPE_STUB_DECL (arg);
5327 is_type = TYPE_P (arg) || is_tmpl_type;
5329 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
5330 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
5332 permerror (input_location, "to refer to a type member of a template parameter, "
5333 "use %<typename %E%>", orig_arg);
5335 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
5336 TREE_OPERAND (arg, 1),
5338 complain & tf_error);
5342 if (is_type != requires_type)
5346 if (complain & tf_error)
5348 error ("type/value mismatch at argument %d in template "
5349 "parameter list for %qD",
5352 error (" expected a constant of type %qT, got %qT",
5354 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
5355 else if (requires_tmpl_type)
5356 error (" expected a class template, got %qE", orig_arg);
5358 error (" expected a type, got %qE", orig_arg);
5361 return error_mark_node;
5363 if (is_tmpl_type ^ requires_tmpl_type)
5365 if (in_decl && (complain & tf_error))
5367 error ("type/value mismatch at argument %d in template "
5368 "parameter list for %qD",
5371 error (" expected a type, got %qT", DECL_NAME (arg));
5373 error (" expected a class template, got %qT", orig_arg);
5375 return error_mark_node;
5380 if (requires_tmpl_type)
5382 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
5383 /* The number of argument required is not known yet.
5384 Just accept it for now. */
5385 val = TREE_TYPE (arg);
5388 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5391 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5393 if (coerce_template_template_parms (parmparm, argparm,
5399 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5401 if (val != error_mark_node)
5403 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
5404 val = TREE_TYPE (val);
5405 else if (TREE_CODE (val) == TYPE_PACK_EXPANSION
5406 && DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
5408 val = TREE_TYPE (arg);
5409 val = make_pack_expansion (val);
5415 if (in_decl && (complain & tf_error))
5417 error ("type/value mismatch at argument %d in "
5418 "template parameter list for %qD",
5420 error (" expected a template of type %qD, got %qD",
5424 val = error_mark_node;
5430 /* We only form one instance of each template specialization.
5431 Therefore, if we use a non-canonical variant (i.e., a
5432 typedef), any future messages referring to the type will use
5433 the typedef, which is confusing if those future uses do not
5434 themselves also use the typedef. */
5436 val = strip_typedefs (val);
5440 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
5442 if (invalid_nontype_parm_type_p (t, complain))
5443 return error_mark_node;
5445 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
5447 if (same_type_p (t, TREE_TYPE (orig_arg)))
5451 /* Not sure if this is reachable, but it doesn't hurt
5453 error ("type mismatch in nontype parameter pack");
5454 val = error_mark_node;
5457 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
5458 /* We used to call digest_init here. However, digest_init
5459 will report errors, which we don't want when complain
5460 is zero. More importantly, digest_init will try too
5461 hard to convert things: for example, `0' should not be
5462 converted to pointer type at this point according to
5463 the standard. Accepting this is not merely an
5464 extension, since deciding whether or not these
5465 conversions can occur is part of determining which
5466 function template to call, or whether a given explicit
5467 argument specification is valid. */
5468 val = convert_nontype_argument (t, orig_arg);
5472 if (val == NULL_TREE)
5473 val = error_mark_node;
5474 else if (val == error_mark_node && (complain & tf_error))
5475 error ("could not convert template argument %qE to %qT", orig_arg, t);
5481 /* Coerces the remaining template arguments in INNER_ARGS (from
5482 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5483 Returns the coerced argument pack. PARM_IDX is the position of this
5484 parameter in the template parameter list. ARGS is the original
5485 template argument list. */
5487 coerce_template_parameter_pack (tree parms,
5495 tsubst_flags_t complain)
5497 tree parm = TREE_VEC_ELT (parms, parm_idx);
5498 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5501 tree packed_types = NULL_TREE;
5503 if (arg_idx > nargs)
5506 packed_args = make_tree_vec (nargs - arg_idx);
5508 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
5509 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
5511 /* When the template parameter is a non-type template
5512 parameter pack whose type uses parameter packs, we need
5513 to look at each of the template arguments
5514 separately. Build a vector of the types for these
5515 non-type template parameters in PACKED_TYPES. */
5517 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
5518 packed_types = tsubst_pack_expansion (expansion, args,
5521 if (packed_types == error_mark_node)
5522 return error_mark_node;
5524 /* Check that we have the right number of arguments. */
5526 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
5527 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
5530 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
5531 error ("wrong number of template arguments (%d, should be %d)",
5532 nargs, needed_parms);
5533 return error_mark_node;
5536 /* If we aren't able to check the actual arguments now
5537 (because they haven't been expanded yet), we can at least
5538 verify that all of the types used for the non-type
5539 template parameter pack are, in fact, valid for non-type
5540 template parameters. */
5542 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
5544 int j, len = TREE_VEC_LENGTH (packed_types);
5545 for (j = 0; j < len; ++j)
5547 tree t = TREE_VEC_ELT (packed_types, j);
5548 if (invalid_nontype_parm_type_p (t, complain))
5549 return error_mark_node;
5554 /* Convert the remaining arguments, which will be a part of the
5555 parameter pack "parm". */
5556 for (; arg_idx < nargs; ++arg_idx)
5558 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
5559 tree actual_parm = TREE_VALUE (parm);
5561 if (packed_types && !PACK_EXPANSION_P (arg))
5563 /* When we have a vector of types (corresponding to the
5564 non-type template parameter pack that uses parameter
5565 packs in its type, as mention above), and the
5566 argument is not an expansion (which expands to a
5567 currently unknown number of arguments), clone the
5568 parm and give it the next type in PACKED_TYPES. */
5569 actual_parm = copy_node (actual_parm);
5570 TREE_TYPE (actual_parm) =
5571 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
5574 if (arg != error_mark_node)
5575 arg = convert_template_argument (actual_parm,
5576 arg, new_args, complain, parm_idx,
5578 if (arg == error_mark_node)
5580 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
5583 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
5584 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
5585 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
5588 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
5589 TREE_TYPE (argument_pack)
5590 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
5591 TREE_CONSTANT (argument_pack) = 1;
5594 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
5595 return argument_pack;
5598 /* Convert all template arguments to their appropriate types, and
5599 return a vector containing the innermost resulting template
5600 arguments. If any error occurs, return error_mark_node. Error and
5601 warning messages are issued under control of COMPLAIN.
5603 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5604 for arguments not specified in ARGS. Otherwise, if
5605 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5606 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5607 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5611 coerce_template_parms (tree parms,
5614 tsubst_flags_t complain,
5615 bool require_all_args,
5616 bool use_default_args)
5618 int nparms, nargs, parm_idx, arg_idx, lost = 0;
5621 tree new_inner_args;
5622 int saved_unevaluated_operand;
5623 int saved_inhibit_evaluation_warnings;
5625 /* When used as a boolean value, indicates whether this is a
5626 variadic template parameter list. Since it's an int, we can also
5627 subtract it from nparms to get the number of non-variadic
5631 nparms = TREE_VEC_LENGTH (parms);
5633 /* Determine if there are any parameter packs. */
5634 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
5636 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
5637 if (template_parameter_pack_p (tparm))
5641 inner_args = INNERMOST_TEMPLATE_ARGS (args);
5642 /* If there are 0 or 1 parameter packs, we need to expand any argument
5643 packs so that we can deduce a parameter pack from some non-packed args
5644 followed by an argument pack, as in variadic85.C. If there are more
5645 than that, we need to leave argument packs intact so the arguments are
5646 assigned to the right parameter packs. This should only happen when
5647 dealing with a nested class inside a partial specialization of a class
5648 template, as in variadic92.C. */
5649 if (variadic_p <= 1)
5650 inner_args = expand_template_argument_pack (inner_args);
5652 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5653 if ((nargs > nparms && !variadic_p)
5654 || (nargs < nparms - variadic_p
5656 && (!use_default_args
5657 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
5658 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
5660 if (complain & tf_error)
5662 const char *or_more = "";
5665 or_more = " or more";
5669 error ("wrong number of template arguments (%d, should be %d%s)",
5670 nargs, nparms, or_more);
5673 error ("provided for %q+D", in_decl);
5676 return error_mark_node;
5679 /* We need to evaluate the template arguments, even though this
5680 template-id may be nested within a "sizeof". */
5681 saved_unevaluated_operand = cp_unevaluated_operand;
5682 cp_unevaluated_operand = 0;
5683 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
5684 c_inhibit_evaluation_warnings = 0;
5685 new_inner_args = make_tree_vec (nparms);
5686 new_args = add_outermost_template_args (args, new_inner_args);
5687 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
5692 /* Get the Ith template parameter. */
5693 parm = TREE_VEC_ELT (parms, parm_idx);
5695 if (parm == error_mark_node)
5697 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
5701 /* Calculate the next argument. */
5702 if (arg_idx < nargs)
5703 arg = TREE_VEC_ELT (inner_args, arg_idx);
5707 if (template_parameter_pack_p (TREE_VALUE (parm))
5708 && !(arg && ARGUMENT_PACK_P (arg)))
5710 /* All remaining arguments will be placed in the
5711 template parameter pack PARM. */
5712 arg = coerce_template_parameter_pack (parms, parm_idx, args,
5713 inner_args, arg_idx,
5717 /* Store this argument. */
5718 if (arg == error_mark_node)
5720 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
5722 /* We are done with all of the arguments. */
5729 if (PACK_EXPANSION_P (arg))
5731 if (complain & tf_error)
5733 /* FIXME this restriction was removed by N2555; see
5735 /* If ARG is a pack expansion, but PARM is not a
5736 template parameter pack (if it were, we would have
5737 handled it above), we're trying to expand into a
5738 fixed-length argument list. */
5739 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5740 sorry ("cannot expand %<%E%> into a fixed-length "
5741 "argument list", arg);
5743 sorry ("cannot expand %<%T%> into a fixed-length "
5744 "argument list", arg);
5746 return error_mark_node;
5749 else if (require_all_args)
5750 /* There must be a default arg in this case. */
5751 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
5756 if (arg == error_mark_node)
5758 if (complain & tf_error)
5759 error ("template argument %d is invalid", arg_idx + 1);
5762 /* This only occurs if there was an error in the template
5763 parameter list itself (which we would already have
5764 reported) that we are trying to recover from, e.g., a class
5765 template with a parameter list such as
5766 template<typename..., typename>. */
5767 return error_mark_node;
5769 arg = convert_template_argument (TREE_VALUE (parm),
5770 arg, new_args, complain,
5773 if (arg == error_mark_node)
5775 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
5777 cp_unevaluated_operand = saved_unevaluated_operand;
5778 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
5781 return error_mark_node;
5783 return new_inner_args;
5786 /* Returns 1 if template args OT and NT are equivalent. */
5789 template_args_equal (tree ot, tree nt)
5794 if (TREE_CODE (nt) == TREE_VEC)
5795 /* For member templates */
5796 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
5797 else if (PACK_EXPANSION_P (ot))
5798 return PACK_EXPANSION_P (nt)
5799 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
5800 PACK_EXPANSION_PATTERN (nt));
5801 else if (ARGUMENT_PACK_P (ot))
5806 if (!ARGUMENT_PACK_P (nt))
5809 opack = ARGUMENT_PACK_ARGS (ot);
5810 npack = ARGUMENT_PACK_ARGS (nt);
5811 len = TREE_VEC_LENGTH (opack);
5812 if (TREE_VEC_LENGTH (npack) != len)
5814 for (i = 0; i < len; ++i)
5815 if (!template_args_equal (TREE_VEC_ELT (opack, i),
5816 TREE_VEC_ELT (npack, i)))
5820 else if (TYPE_P (nt))
5821 return TYPE_P (ot) && same_type_p (ot, nt);
5822 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
5825 return cp_tree_equal (ot, nt);
5828 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
5829 of template arguments. Returns 0 otherwise. */
5832 comp_template_args (tree oldargs, tree newargs)
5836 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
5839 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
5841 tree nt = TREE_VEC_ELT (newargs, i);
5842 tree ot = TREE_VEC_ELT (oldargs, i);
5844 if (! template_args_equal (ot, nt))
5851 add_pending_template (tree d)
5853 tree ti = (TYPE_P (d)
5854 ? CLASSTYPE_TEMPLATE_INFO (d)
5855 : DECL_TEMPLATE_INFO (d));
5856 struct pending_template *pt;
5859 if (TI_PENDING_TEMPLATE_FLAG (ti))
5862 /* We are called both from instantiate_decl, where we've already had a
5863 tinst_level pushed, and instantiate_template, where we haven't.
5865 level = !current_tinst_level || current_tinst_level->decl != d;
5868 push_tinst_level (d);
5870 pt = GGC_NEW (struct pending_template);
5872 pt->tinst = current_tinst_level;
5873 if (last_pending_template)
5874 last_pending_template->next = pt;
5876 pending_templates = pt;
5878 last_pending_template = pt;
5880 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
5887 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
5888 ARGLIST. Valid choices for FNS are given in the cp-tree.def
5889 documentation for TEMPLATE_ID_EXPR. */
5892 lookup_template_function (tree fns, tree arglist)
5896 if (fns == error_mark_node || arglist == error_mark_node)
5897 return error_mark_node;
5899 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
5900 gcc_assert (fns && (is_overloaded_fn (fns)
5901 || TREE_CODE (fns) == IDENTIFIER_NODE));
5903 if (BASELINK_P (fns))
5905 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
5907 BASELINK_FUNCTIONS (fns),
5912 type = TREE_TYPE (fns);
5913 if (TREE_CODE (fns) == OVERLOAD || !type)
5914 type = unknown_type_node;
5916 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
5919 /* Within the scope of a template class S<T>, the name S gets bound
5920 (in build_self_reference) to a TYPE_DECL for the class, not a
5921 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
5922 or one of its enclosing classes, and that type is a template,
5923 return the associated TEMPLATE_DECL. Otherwise, the original
5924 DECL is returned. */
5927 maybe_get_template_decl_from_type_decl (tree decl)
5929 return (decl != NULL_TREE
5930 && TREE_CODE (decl) == TYPE_DECL
5931 && DECL_ARTIFICIAL (decl)
5932 && CLASS_TYPE_P (TREE_TYPE (decl))
5933 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
5934 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
5937 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
5938 parameters, find the desired type.
5940 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
5942 IN_DECL, if non-NULL, is the template declaration we are trying to
5945 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
5946 the class we are looking up.
5948 Issue error and warning messages under control of COMPLAIN.
5950 If the template class is really a local class in a template
5951 function, then the FUNCTION_CONTEXT is the function in which it is
5954 ??? Note that this function is currently called *twice* for each
5955 template-id: the first time from the parser, while creating the
5956 incomplete type (finish_template_type), and the second type during the
5957 real instantiation (instantiate_template_class). This is surely something
5958 that we want to avoid. It also causes some problems with argument
5959 coercion (see convert_nontype_argument for more information on this). */
5962 lookup_template_class (tree d1,
5967 tsubst_flags_t complain)
5969 tree templ = NULL_TREE, parmlist;
5976 timevar_push (TV_NAME_LOOKUP);
5978 if (TREE_CODE (d1) == IDENTIFIER_NODE)
5980 tree value = innermost_non_namespace_value (d1);
5981 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
5986 push_decl_namespace (context);
5987 templ = lookup_name (d1);
5988 templ = maybe_get_template_decl_from_type_decl (templ);
5990 pop_decl_namespace ();
5993 context = DECL_CONTEXT (templ);
5995 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
5997 tree type = TREE_TYPE (d1);
5999 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6000 an implicit typename for the second A. Deal with it. */
6001 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
6002 type = TREE_TYPE (type);
6004 if (CLASSTYPE_TEMPLATE_INFO (type))
6006 templ = CLASSTYPE_TI_TEMPLATE (type);
6007 d1 = DECL_NAME (templ);
6010 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6011 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
6013 templ = TYPE_TI_TEMPLATE (d1);
6014 d1 = DECL_NAME (templ);
6016 else if (TREE_CODE (d1) == TEMPLATE_DECL
6017 && DECL_TEMPLATE_RESULT (d1)
6018 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
6021 d1 = DECL_NAME (templ);
6022 context = DECL_CONTEXT (templ);
6025 /* Issue an error message if we didn't find a template. */
6028 if (complain & tf_error)
6029 error ("%qT is not a template", d1);
6030 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6033 if (TREE_CODE (templ) != TEMPLATE_DECL
6034 /* Make sure it's a user visible template, if it was named by
6036 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
6037 && !PRIMARY_TEMPLATE_P (templ)))
6039 if (complain & tf_error)
6041 error ("non-template type %qT used as a template", d1);
6043 error ("for template declaration %q+D", in_decl);
6045 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6048 complain &= ~tf_user;
6050 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
6052 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6053 template arguments */
6059 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6061 /* Consider an example where a template template parameter declared as
6063 template <class T, class U = std::allocator<T> > class TT
6065 The template parameter level of T and U are one level larger than
6066 of TT. To proper process the default argument of U, say when an
6067 instantiation `TT<int>' is seen, we need to build the full
6068 arguments containing {int} as the innermost level. Outer levels,
6069 available when not appearing as default template argument, can be
6070 obtained from the arguments of the enclosing template.
6072 Suppose that TT is later substituted with std::vector. The above
6073 instantiation is `TT<int, std::allocator<T> >' with TT at
6074 level 1, and T at level 2, while the template arguments at level 1
6075 becomes {std::vector} and the inner level 2 is {int}. */
6077 outer = DECL_CONTEXT (templ);
6079 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6080 else if (current_template_parms)
6081 /* This is an argument of the current template, so we haven't set
6082 DECL_CONTEXT yet. */
6083 outer = current_template_args ();
6086 arglist = add_to_template_args (outer, arglist);
6088 arglist2 = coerce_template_parms (parmlist, arglist, templ,
6090 /*require_all_args=*/true,
6091 /*use_default_args=*/true);
6092 if (arglist2 == error_mark_node
6093 || (!uses_template_parms (arglist2)
6094 && check_instantiated_args (templ, arglist2, complain)))
6095 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6097 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
6098 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
6102 tree template_type = TREE_TYPE (templ);
6105 tree found = NULL_TREE;
6108 int is_partial_instantiation;
6110 gen_tmpl = most_general_template (templ);
6111 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
6112 parm_depth = TMPL_PARMS_DEPTH (parmlist);
6113 arg_depth = TMPL_ARGS_DEPTH (arglist);
6115 if (arg_depth == 1 && parm_depth > 1)
6117 /* We've been given an incomplete set of template arguments.
6120 template <class T> struct S1 {
6121 template <class U> struct S2 {};
6122 template <class U> struct S2<U*> {};
6125 we will be called with an ARGLIST of `U*', but the
6126 TEMPLATE will be `template <class T> template
6127 <class U> struct S1<T>::S2'. We must fill in the missing
6130 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
6132 arg_depth = TMPL_ARGS_DEPTH (arglist);
6135 /* Now we should have enough arguments. */
6136 gcc_assert (parm_depth == arg_depth);
6138 /* From here on, we're only interested in the most general
6141 /* Calculate the BOUND_ARGS. These will be the args that are
6142 actually tsubst'd into the definition to create the
6146 /* We have multiple levels of arguments to coerce, at once. */
6148 int saved_depth = TMPL_ARGS_DEPTH (arglist);
6150 tree bound_args = make_tree_vec (parm_depth);
6152 for (i = saved_depth,
6153 t = DECL_TEMPLATE_PARMS (gen_tmpl);
6154 i > 0 && t != NULL_TREE;
6155 --i, t = TREE_CHAIN (t))
6157 tree a = coerce_template_parms (TREE_VALUE (t),
6160 /*require_all_args=*/true,
6161 /*use_default_args=*/true);
6163 /* Don't process further if one of the levels fails. */
6164 if (a == error_mark_node)
6166 /* Restore the ARGLIST to its full size. */
6167 TREE_VEC_LENGTH (arglist) = saved_depth;
6168 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6171 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
6173 /* We temporarily reduce the length of the ARGLIST so
6174 that coerce_template_parms will see only the arguments
6175 corresponding to the template parameters it is
6177 TREE_VEC_LENGTH (arglist)--;
6180 /* Restore the ARGLIST to its full size. */
6181 TREE_VEC_LENGTH (arglist) = saved_depth;
6183 arglist = bound_args;
6187 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
6188 INNERMOST_TEMPLATE_ARGS (arglist),
6191 /*require_all_args=*/true,
6192 /*use_default_args=*/true);
6194 if (arglist == error_mark_node)
6195 /* We were unable to bind the arguments. */
6196 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6198 /* In the scope of a template class, explicit references to the
6199 template class refer to the type of the template, not any
6200 instantiation of it. For example, in:
6202 template <class T> class C { void f(C<T>); }
6204 the `C<T>' is just the same as `C'. Outside of the
6205 class, however, such a reference is an instantiation. */
6207 || !PRIMARY_TEMPLATE_P (gen_tmpl)
6208 || currently_open_class (template_type))
6209 /* comp_template_args is expensive, check it last. */
6210 && comp_template_args (TYPE_TI_ARGS (template_type),
6212 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type);
6214 /* If we already have this specialization, return it. */
6215 elt.tmpl = gen_tmpl;
6217 hash = hash_specialization (&elt);
6218 entry = (spec_entry *) htab_find_with_hash (type_specializations,
6222 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec);
6224 /* This type is a "partial instantiation" if any of the template
6225 arguments still involve template parameters. Note that we set
6226 IS_PARTIAL_INSTANTIATION for partial specializations as
6228 is_partial_instantiation = uses_template_parms (arglist);
6230 /* If the deduced arguments are invalid, then the binding
6232 if (!is_partial_instantiation
6233 && check_instantiated_args (gen_tmpl,
6234 INNERMOST_TEMPLATE_ARGS (arglist),
6236 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6238 if (!is_partial_instantiation
6239 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6240 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
6241 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
6243 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
6244 DECL_NAME (gen_tmpl),
6245 /*tag_scope=*/ts_global);
6246 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
6249 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
6252 context = global_namespace;
6254 /* Create the type. */
6255 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
6257 if (!is_partial_instantiation)
6259 set_current_access_from_decl (TYPE_NAME (template_type));
6260 t = start_enum (TYPE_IDENTIFIER (template_type),
6261 tsubst (ENUM_UNDERLYING_TYPE (template_type),
6262 arglist, complain, in_decl),
6263 SCOPED_ENUM_P (template_type));
6267 /* We don't want to call start_enum for this type, since
6268 the values for the enumeration constants may involve
6269 template parameters. And, no one should be interested
6270 in the enumeration constants for such a type. */
6271 t = cxx_make_type (ENUMERAL_TYPE);
6272 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
6277 t = make_class_type (TREE_CODE (template_type));
6278 CLASSTYPE_DECLARED_CLASS (t)
6279 = CLASSTYPE_DECLARED_CLASS (template_type);
6280 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
6281 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
6283 /* A local class. Make sure the decl gets registered properly. */
6284 if (context == current_function_decl)
6285 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
6287 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
6288 /* This instantiation is another name for the primary
6289 template type. Set the TYPE_CANONICAL field
6291 TYPE_CANONICAL (t) = template_type;
6292 else if (any_template_arguments_need_structural_equality_p (arglist))
6293 /* Some of the template arguments require structural
6294 equality testing, so this template class requires
6295 structural equality testing. */
6296 SET_TYPE_STRUCTURAL_EQUALITY (t);
6299 /* If we called start_enum or pushtag above, this information
6300 will already be set up. */
6303 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
6305 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
6306 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
6307 TYPE_STUB_DECL (t) = type_decl;
6308 DECL_SOURCE_LOCATION (type_decl)
6309 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
6312 type_decl = TYPE_NAME (t);
6314 TREE_PRIVATE (type_decl)
6315 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
6316 TREE_PROTECTED (type_decl)
6317 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
6318 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
6320 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
6321 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
6324 /* Set up the template information. We have to figure out which
6325 template is the immediate parent if this is a full
6327 if (parm_depth == 1 || is_partial_instantiation
6328 || !PRIMARY_TEMPLATE_P (gen_tmpl))
6329 /* This case is easy; there are no member templates involved. */
6333 /* This is a full instantiation of a member template. Find
6334 the partial instantiation of which this is an instance. */
6336 /* Temporarily reduce by one the number of levels in the ARGLIST
6337 so as to avoid comparing the last set of arguments. */
6338 TREE_VEC_LENGTH (arglist)--;
6339 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
6340 TREE_VEC_LENGTH (arglist)++;
6341 found = CLASSTYPE_TI_TEMPLATE (found);
6344 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
6347 slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
6348 &elt, hash, INSERT);
6349 *slot = GGC_NEW (spec_entry);
6352 /* Note this use of the partial instantiation so we can check it
6353 later in maybe_process_partial_specialization. */
6354 DECL_TEMPLATE_INSTANTIATIONS (templ)
6355 = tree_cons (arglist, t,
6356 DECL_TEMPLATE_INSTANTIATIONS (templ));
6358 if (TREE_CODE (t) == ENUMERAL_TYPE
6359 && !is_partial_instantiation)
6360 /* Now that the type has been registered on the instantiations
6361 list, we set up the enumerators. Because the enumeration
6362 constants may involve the enumeration type itself, we make
6363 sure to register the type first, and then create the
6364 constants. That way, doing tsubst_expr for the enumeration
6365 constants won't result in recursive calls here; we'll find
6366 the instantiation and exit above. */
6367 tsubst_enum (template_type, t, arglist);
6369 if (is_partial_instantiation)
6370 /* If the type makes use of template parameters, the
6371 code that generates debugging information will crash. */
6372 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
6374 /* Possibly limit visibility based on template args. */
6375 TREE_PUBLIC (type_decl) = 1;
6376 determine_visibility (type_decl);
6378 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6380 timevar_pop (TV_NAME_LOOKUP);
6387 /* True when we should also visit template parameters that occur in
6388 non-deduced contexts. */
6389 bool include_nondeduced_p;
6390 struct pointer_set_t *visited;
6393 /* Called from for_each_template_parm via walk_tree. */
6396 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
6399 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
6400 tree_fn_t fn = pfd->fn;
6401 void *data = pfd->data;
6404 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
6405 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
6406 pfd->include_nondeduced_p))
6407 return error_mark_node;
6409 switch (TREE_CODE (t))
6412 if (TYPE_PTRMEMFUNC_P (t))
6418 if (!TYPE_TEMPLATE_INFO (t))
6420 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
6421 fn, data, pfd->visited,
6422 pfd->include_nondeduced_p))
6423 return error_mark_node;
6427 if (for_each_template_parm (TYPE_MIN_VALUE (t),
6428 fn, data, pfd->visited,
6429 pfd->include_nondeduced_p)
6430 || for_each_template_parm (TYPE_MAX_VALUE (t),
6431 fn, data, pfd->visited,
6432 pfd->include_nondeduced_p))
6433 return error_mark_node;
6437 /* Since we're not going to walk subtrees, we have to do this
6439 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
6440 pfd->visited, pfd->include_nondeduced_p))
6441 return error_mark_node;
6445 /* Check the return type. */
6446 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6447 pfd->include_nondeduced_p))
6448 return error_mark_node;
6450 /* Check the parameter types. Since default arguments are not
6451 instantiated until they are needed, the TYPE_ARG_TYPES may
6452 contain expressions that involve template parameters. But,
6453 no-one should be looking at them yet. And, once they're
6454 instantiated, they don't contain template parameters, so
6455 there's no point in looking at them then, either. */
6459 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
6460 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
6461 pfd->visited, pfd->include_nondeduced_p))
6462 return error_mark_node;
6464 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6465 want walk_tree walking into them itself. */
6471 if (pfd->include_nondeduced_p
6472 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
6474 pfd->include_nondeduced_p))
6475 return error_mark_node;
6480 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
6481 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
6482 pfd->visited, pfd->include_nondeduced_p))
6483 return error_mark_node;
6488 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
6489 && for_each_template_parm (DECL_INITIAL (t), fn, data,
6490 pfd->visited, pfd->include_nondeduced_p))
6491 return error_mark_node;
6492 if (DECL_CONTEXT (t)
6493 && pfd->include_nondeduced_p
6494 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
6495 pfd->visited, pfd->include_nondeduced_p))
6496 return error_mark_node;
6499 case BOUND_TEMPLATE_TEMPLATE_PARM:
6500 /* Record template parameters such as `T' inside `TT<T>'. */
6501 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
6502 pfd->include_nondeduced_p))
6503 return error_mark_node;
6506 case TEMPLATE_TEMPLATE_PARM:
6507 case TEMPLATE_TYPE_PARM:
6508 case TEMPLATE_PARM_INDEX:
6509 if (fn && (*fn)(t, data))
6510 return error_mark_node;
6512 return error_mark_node;
6516 /* A template template parameter is encountered. */
6517 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
6518 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6519 pfd->include_nondeduced_p))
6520 return error_mark_node;
6522 /* Already substituted template template parameter */
6528 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
6530 pfd->include_nondeduced_p))
6531 return error_mark_node;
6535 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
6536 && pfd->include_nondeduced_p
6537 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
6538 (TREE_TYPE (t)), fn, data,
6539 pfd->visited, pfd->include_nondeduced_p))
6540 return error_mark_node;
6545 /* If there's no type, then this thing must be some expression
6546 involving template parameters. */
6547 if (!fn && !TREE_TYPE (t))
6548 return error_mark_node;
6553 case REINTERPRET_CAST_EXPR:
6554 case CONST_CAST_EXPR:
6555 case STATIC_CAST_EXPR:
6556 case DYNAMIC_CAST_EXPR:
6560 case PSEUDO_DTOR_EXPR:
6562 return error_mark_node;
6569 /* We didn't find any template parameters we liked. */
6573 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
6574 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
6575 call FN with the parameter and the DATA.
6576 If FN returns nonzero, the iteration is terminated, and
6577 for_each_template_parm returns 1. Otherwise, the iteration
6578 continues. If FN never returns a nonzero value, the value
6579 returned by for_each_template_parm is 0. If FN is NULL, it is
6580 considered to be the function which always returns 1.
6582 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
6583 parameters that occur in non-deduced contexts. When false, only
6584 visits those template parameters that can be deduced. */
6587 for_each_template_parm (tree t, tree_fn_t fn, void* data,
6588 struct pointer_set_t *visited,
6589 bool include_nondeduced_p)
6591 struct pair_fn_data pfd;
6597 pfd.include_nondeduced_p = include_nondeduced_p;
6599 /* Walk the tree. (Conceptually, we would like to walk without
6600 duplicates, but for_each_template_parm_r recursively calls
6601 for_each_template_parm, so we would need to reorganize a fair
6602 bit to use walk_tree_without_duplicates, so we keep our own
6605 pfd.visited = visited;
6607 pfd.visited = pointer_set_create ();
6608 result = cp_walk_tree (&t,
6609 for_each_template_parm_r,
6611 pfd.visited) != NULL_TREE;
6616 pointer_set_destroy (pfd.visited);
6623 /* Returns true if T depends on any template parameter. */
6626 uses_template_parms (tree t)
6629 int saved_processing_template_decl;
6631 saved_processing_template_decl = processing_template_decl;
6632 if (!saved_processing_template_decl)
6633 processing_template_decl = 1;
6635 dependent_p = dependent_type_p (t);
6636 else if (TREE_CODE (t) == TREE_VEC)
6637 dependent_p = any_dependent_template_arguments_p (t);
6638 else if (TREE_CODE (t) == TREE_LIST)
6639 dependent_p = (uses_template_parms (TREE_VALUE (t))
6640 || uses_template_parms (TREE_CHAIN (t)));
6641 else if (TREE_CODE (t) == TYPE_DECL)
6642 dependent_p = dependent_type_p (TREE_TYPE (t));
6645 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
6646 || TREE_CODE (t) == OVERLOAD
6647 || TREE_CODE (t) == BASELINK
6648 || TREE_CODE (t) == IDENTIFIER_NODE
6649 || TREE_CODE (t) == TRAIT_EXPR
6650 || TREE_CODE (t) == CONSTRUCTOR
6651 || CONSTANT_CLASS_P (t))
6652 dependent_p = (type_dependent_expression_p (t)
6653 || value_dependent_expression_p (t));
6656 gcc_assert (t == error_mark_node);
6657 dependent_p = false;
6660 processing_template_decl = saved_processing_template_decl;
6665 /* Returns true if T depends on any template parameter with level LEVEL. */
6668 uses_template_parms_level (tree t, int level)
6670 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
6671 /*include_nondeduced_p=*/true);
6674 static int tinst_depth;
6675 extern int max_tinst_depth;
6676 #ifdef GATHER_STATISTICS
6679 static int tinst_level_tick;
6680 static int last_template_error_tick;
6682 /* We're starting to instantiate D; record the template instantiation context
6683 for diagnostics and to restore it later. */
6686 push_tinst_level (tree d)
6688 struct tinst_level *new_level;
6690 if (tinst_depth >= max_tinst_depth)
6692 /* If the instantiation in question still has unbound template parms,
6693 we don't really care if we can't instantiate it, so just return.
6694 This happens with base instantiation for implicit `typename'. */
6695 if (uses_template_parms (d))
6698 last_template_error_tick = tinst_level_tick;
6699 error ("template instantiation depth exceeds maximum of %d (use "
6700 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
6701 max_tinst_depth, d);
6703 print_instantiation_context ();
6708 new_level = GGC_NEW (struct tinst_level);
6709 new_level->decl = d;
6710 new_level->locus = input_location;
6711 new_level->in_system_header_p = in_system_header;
6712 new_level->next = current_tinst_level;
6713 current_tinst_level = new_level;
6716 #ifdef GATHER_STATISTICS
6717 if (tinst_depth > depth_reached)
6718 depth_reached = tinst_depth;
6725 /* We're done instantiating this template; return to the instantiation
6729 pop_tinst_level (void)
6731 /* Restore the filename and line number stashed away when we started
6732 this instantiation. */
6733 input_location = current_tinst_level->locus;
6734 current_tinst_level = current_tinst_level->next;
6739 /* We're instantiating a deferred template; restore the template
6740 instantiation context in which the instantiation was requested, which
6741 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
6744 reopen_tinst_level (struct tinst_level *level)
6746 struct tinst_level *t;
6749 for (t = level; t; t = t->next)
6752 current_tinst_level = level;
6757 /* Returns the TINST_LEVEL which gives the original instantiation
6760 struct tinst_level *
6761 outermost_tinst_level (void)
6763 struct tinst_level *level = current_tinst_level;
6766 level = level->next;
6770 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
6773 parameter_of_template_p (tree parm, tree templ)
6778 if (!parm || !templ)
6781 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
6782 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
6784 parms = DECL_TEMPLATE_PARMS (templ);
6785 parms = INNERMOST_TEMPLATE_PARMS (parms);
6787 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
6788 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
6794 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
6795 vector of template arguments, as for tsubst.
6797 Returns an appropriate tsubst'd friend declaration. */
6800 tsubst_friend_function (tree decl, tree args)
6804 if (TREE_CODE (decl) == FUNCTION_DECL
6805 && DECL_TEMPLATE_INSTANTIATION (decl)
6806 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
6807 /* This was a friend declared with an explicit template
6808 argument list, e.g.:
6812 to indicate that f was a template instantiation, not a new
6813 function declaration. Now, we have to figure out what
6814 instantiation of what template. */
6816 tree template_id, arglist, fns;
6819 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
6821 /* Friend functions are looked up in the containing namespace scope.
6822 We must enter that scope, to avoid finding member functions of the
6823 current class with same name. */
6824 push_nested_namespace (ns);
6825 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
6826 tf_warning_or_error, NULL_TREE,
6827 /*integral_constant_expression_p=*/false);
6828 pop_nested_namespace (ns);
6829 arglist = tsubst (DECL_TI_ARGS (decl), args,
6830 tf_warning_or_error, NULL_TREE);
6831 template_id = lookup_template_function (fns, arglist);
6833 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6834 tmpl = determine_specialization (template_id, new_friend,
6836 /*need_member_template=*/0,
6837 TREE_VEC_LENGTH (args),
6839 return instantiate_template (tmpl, new_args, tf_error);
6842 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6844 /* The NEW_FRIEND will look like an instantiation, to the
6845 compiler, but is not an instantiation from the point of view of
6846 the language. For example, we might have had:
6848 template <class T> struct S {
6849 template <class U> friend void f(T, U);
6852 Then, in S<int>, template <class U> void f(int, U) is not an
6853 instantiation of anything. */
6854 if (new_friend == error_mark_node)
6855 return error_mark_node;
6857 DECL_USE_TEMPLATE (new_friend) = 0;
6858 if (TREE_CODE (decl) == TEMPLATE_DECL)
6860 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
6861 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
6862 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
6865 /* The mangled name for the NEW_FRIEND is incorrect. The function
6866 is not a template instantiation and should not be mangled like
6867 one. Therefore, we forget the mangling here; we'll recompute it
6868 later if we need it. */
6869 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
6871 SET_DECL_RTL (new_friend, NULL_RTX);
6872 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
6875 if (DECL_NAMESPACE_SCOPE_P (new_friend))
6878 tree new_friend_template_info;
6879 tree new_friend_result_template_info;
6881 int new_friend_is_defn;
6883 /* We must save some information from NEW_FRIEND before calling
6884 duplicate decls since that function will free NEW_FRIEND if
6886 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
6887 new_friend_is_defn =
6888 (DECL_INITIAL (DECL_TEMPLATE_RESULT
6889 (template_for_substitution (new_friend)))
6891 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
6893 /* This declaration is a `primary' template. */
6894 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
6896 new_friend_result_template_info
6897 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
6900 new_friend_result_template_info = NULL_TREE;
6902 /* Make the init_value nonzero so pushdecl knows this is a defn. */
6903 if (new_friend_is_defn)
6904 DECL_INITIAL (new_friend) = error_mark_node;
6906 /* Inside pushdecl_namespace_level, we will push into the
6907 current namespace. However, the friend function should go
6908 into the namespace of the template. */
6909 ns = decl_namespace_context (new_friend);
6910 push_nested_namespace (ns);
6911 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
6912 pop_nested_namespace (ns);
6914 if (old_decl == error_mark_node)
6915 return error_mark_node;
6917 if (old_decl != new_friend)
6919 /* This new friend declaration matched an existing
6920 declaration. For example, given:
6922 template <class T> void f(T);
6923 template <class U> class C {
6924 template <class T> friend void f(T) {}
6927 the friend declaration actually provides the definition
6928 of `f', once C has been instantiated for some type. So,
6929 old_decl will be the out-of-class template declaration,
6930 while new_friend is the in-class definition.
6932 But, if `f' was called before this point, the
6933 instantiation of `f' will have DECL_TI_ARGS corresponding
6934 to `T' but not to `U', references to which might appear
6935 in the definition of `f'. Previously, the most general
6936 template for an instantiation of `f' was the out-of-class
6937 version; now it is the in-class version. Therefore, we
6938 run through all specialization of `f', adding to their
6939 DECL_TI_ARGS appropriately. In particular, they need a
6940 new set of outer arguments, corresponding to the
6941 arguments for this class instantiation.
6943 The same situation can arise with something like this:
6946 template <class T> class C {
6950 when `C<int>' is instantiated. Now, `f(int)' is defined
6953 if (!new_friend_is_defn)
6954 /* On the other hand, if the in-class declaration does
6955 *not* provide a definition, then we don't want to alter
6956 existing definitions. We can just leave everything
6961 tree new_template = TI_TEMPLATE (new_friend_template_info);
6962 tree new_args = TI_ARGS (new_friend_template_info);
6964 /* Overwrite whatever template info was there before, if
6965 any, with the new template information pertaining to
6967 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
6969 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
6970 /* We should have called reregister_specialization in
6972 gcc_assert (retrieve_specialization (new_template,
6979 /* Indicate that the old function template is a partial
6981 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
6982 = new_friend_result_template_info;
6984 gcc_assert (new_template
6985 == most_general_template (new_template));
6986 gcc_assert (new_template != old_decl);
6988 /* Reassign any specializations already in the hash table
6989 to the new more general template, and add the
6990 additional template args. */
6991 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
6995 tree spec = TREE_VALUE (t);
6998 elt.tmpl = old_decl;
6999 elt.args = DECL_TI_ARGS (spec);
7000 elt.spec = NULL_TREE;
7002 htab_remove_elt (decl_specializations, &elt);
7005 = add_outermost_template_args (new_args,
7006 DECL_TI_ARGS (spec));
7008 register_specialization
7009 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
7012 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
7016 /* The information from NEW_FRIEND has been merged into OLD_DECL
7017 by duplicate_decls. */
7018 new_friend = old_decl;
7023 tree context = DECL_CONTEXT (new_friend);
7027 template <class T> class C {
7028 template <class U> friend void C1<U>::f (); // case 1
7029 friend void C2<T>::f (); // case 2
7031 we only need to make sure CONTEXT is a complete type for
7032 case 2. To distinguish between the two cases, we note that
7033 CONTEXT of case 1 remains dependent type after tsubst while
7034 this isn't true for case 2. */
7035 ++processing_template_decl;
7036 dependent_p = dependent_type_p (context);
7037 --processing_template_decl;
7040 && !complete_type_or_else (context, NULL_TREE))
7041 return error_mark_node;
7043 if (COMPLETE_TYPE_P (context))
7045 /* Check to see that the declaration is really present, and,
7046 possibly obtain an improved declaration. */
7047 tree fn = check_classfn (context,
7048 new_friend, NULL_TREE);
7058 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7059 template arguments, as for tsubst.
7061 Returns an appropriate tsubst'd friend type or error_mark_node on
7065 tsubst_friend_class (tree friend_tmpl, tree args)
7071 context = DECL_CONTEXT (friend_tmpl);
7075 if (TREE_CODE (context) == NAMESPACE_DECL)
7076 push_nested_namespace (context);
7078 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
7081 /* Look for a class template declaration. We look for hidden names
7082 because two friend declarations of the same template are the
7083 same. For example, in:
7086 template <typename> friend class F;
7088 template <typename> struct B {
7089 template <typename> friend class F;
7092 both F templates are the same. */
7093 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
7094 /*block_p=*/true, 0,
7095 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
7097 /* But, if we don't find one, it might be because we're in a
7098 situation like this:
7106 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7107 for `S<int>', not the TEMPLATE_DECL. */
7108 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
7110 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
7111 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
7114 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
7116 /* The friend template has already been declared. Just
7117 check to see that the declarations match, and install any new
7118 default parameters. We must tsubst the default parameters,
7119 of course. We only need the innermost template parameters
7120 because that is all that redeclare_class_template will look
7122 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
7123 > TMPL_ARGS_DEPTH (args))
7126 location_t saved_input_location;
7127 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
7128 args, tf_warning_or_error);
7130 saved_input_location = input_location;
7131 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
7132 redeclare_class_template (TREE_TYPE (tmpl), parms);
7133 input_location = saved_input_location;
7137 friend_type = TREE_TYPE (tmpl);
7141 /* The friend template has not already been declared. In this
7142 case, the instantiation of the template class will cause the
7143 injection of this template into the global scope. */
7144 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
7145 if (tmpl == error_mark_node)
7146 return error_mark_node;
7148 /* The new TMPL is not an instantiation of anything, so we
7149 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7150 the new type because that is supposed to be the corresponding
7151 template decl, i.e., TMPL. */
7152 DECL_USE_TEMPLATE (tmpl) = 0;
7153 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
7154 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
7155 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
7156 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
7158 /* Inject this template into the global scope. */
7159 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
7164 if (TREE_CODE (context) == NAMESPACE_DECL)
7165 pop_nested_namespace (context);
7167 pop_nested_class ();
7173 /* Returns zero if TYPE cannot be completed later due to circularity.
7174 Otherwise returns one. */
7177 can_complete_type_without_circularity (tree type)
7179 if (type == NULL_TREE || type == error_mark_node)
7181 else if (COMPLETE_TYPE_P (type))
7183 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
7184 return can_complete_type_without_circularity (TREE_TYPE (type));
7185 else if (CLASS_TYPE_P (type)
7186 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
7192 /* Apply any attributes which had to be deferred until instantiation
7193 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7194 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7197 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
7198 tree args, tsubst_flags_t complain, tree in_decl)
7200 tree last_dep = NULL_TREE;
7204 for (t = attributes; t; t = TREE_CHAIN (t))
7205 if (ATTR_IS_DEPENDENT (t))
7208 attributes = copy_list (attributes);
7212 if (DECL_P (*decl_p))
7214 if (TREE_TYPE (*decl_p) == error_mark_node)
7216 p = &DECL_ATTRIBUTES (*decl_p);
7219 p = &TYPE_ATTRIBUTES (*decl_p);
7223 tree late_attrs = NULL_TREE;
7224 tree *q = &late_attrs;
7226 for (*p = attributes; *p; )
7229 if (ATTR_IS_DEPENDENT (t))
7231 *p = TREE_CHAIN (t);
7232 TREE_CHAIN (t) = NULL_TREE;
7233 /* If the first attribute argument is an identifier, don't
7234 pass it through tsubst. Attributes like mode, format,
7235 cleanup and several target specific attributes expect it
7238 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
7239 && TREE_VALUE (TREE_VALUE (t))
7240 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
7241 == IDENTIFIER_NODE))
7244 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
7246 /*integral_constant_expression_p=*/false);
7247 if (chain != TREE_CHAIN (TREE_VALUE (t)))
7249 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
7254 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
7255 /*integral_constant_expression_p=*/false);
7257 q = &TREE_CHAIN (t);
7260 p = &TREE_CHAIN (t);
7263 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
7267 /* Perform (or defer) access check for typedefs that were referenced
7268 from within the template TMPL code.
7269 This is a subroutine of instantiate_template and instantiate_class_template.
7270 TMPL is the template to consider and TARGS is the list of arguments of
7274 perform_typedefs_access_check (tree tmpl, tree targs)
7279 || (!CLASS_TYPE_P (tmpl)
7280 && TREE_CODE (tmpl) != FUNCTION_DECL))
7283 for (t = get_types_needing_access_check (tmpl); t; t = TREE_CHAIN (t))
7285 tree type_decl = TREE_PURPOSE (t);
7286 tree type_scope = TREE_VALUE (t);
7288 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
7291 if (uses_template_parms (type_decl))
7292 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
7293 if (uses_template_parms (type_scope))
7294 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
7296 perform_or_defer_access_check (TYPE_BINFO (type_scope),
7297 type_decl, type_decl);
7302 instantiate_class_template (tree type)
7304 tree templ, args, pattern, t, member;
7309 if (type == error_mark_node)
7310 return error_mark_node;
7312 if (TYPE_BEING_DEFINED (type)
7313 || COMPLETE_TYPE_P (type)
7314 || dependent_type_p (type))
7317 /* Figure out which template is being instantiated. */
7318 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
7319 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7321 /* Determine what specialization of the original template to
7323 t = most_specialized_class (type, templ);
7324 if (t == error_mark_node)
7326 TYPE_BEING_DEFINED (type) = 1;
7327 return error_mark_node;
7331 /* This TYPE is actually an instantiation of a partial
7332 specialization. We replace the innermost set of ARGS with
7333 the arguments appropriate for substitution. For example,
7336 template <class T> struct S {};
7337 template <class T> struct S<T*> {};
7339 and supposing that we are instantiating S<int*>, ARGS will
7340 presently be {int*} -- but we need {int}. */
7341 pattern = TREE_TYPE (t);
7342 args = TREE_PURPOSE (t);
7346 pattern = TREE_TYPE (templ);
7347 args = CLASSTYPE_TI_ARGS (type);
7350 /* If the template we're instantiating is incomplete, then clearly
7351 there's nothing we can do. */
7352 if (!COMPLETE_TYPE_P (pattern))
7355 /* If we've recursively instantiated too many templates, stop. */
7356 if (! push_tinst_level (type))
7359 /* Now we're really doing the instantiation. Mark the type as in
7360 the process of being defined. */
7361 TYPE_BEING_DEFINED (type) = 1;
7363 /* We may be in the middle of deferred access check. Disable
7365 push_deferring_access_checks (dk_no_deferred);
7367 push_to_top_level ();
7369 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
7371 /* Set the input location to the most specialized template definition.
7372 This is needed if tsubsting causes an error. */
7373 typedecl = TYPE_MAIN_DECL (pattern);
7374 input_location = DECL_SOURCE_LOCATION (typedecl);
7376 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
7377 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
7378 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
7379 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
7380 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
7381 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
7382 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
7383 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
7384 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
7385 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
7386 TYPE_PACKED (type) = TYPE_PACKED (pattern);
7387 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
7388 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
7389 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
7390 if (ANON_AGGR_TYPE_P (pattern))
7391 SET_ANON_AGGR_TYPE_P (type);
7392 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
7394 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
7395 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
7398 pbinfo = TYPE_BINFO (pattern);
7400 /* We should never instantiate a nested class before its enclosing
7401 class; we need to look up the nested class by name before we can
7402 instantiate it, and that lookup should instantiate the enclosing
7404 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
7405 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
7406 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
7408 base_list = NULL_TREE;
7409 if (BINFO_N_BASE_BINFOS (pbinfo))
7412 tree context = TYPE_CONTEXT (type);
7416 /* We must enter the scope containing the type, as that is where
7417 the accessibility of types named in dependent bases are
7419 pushed_scope = push_scope (context ? context : global_namespace);
7421 /* Substitute into each of the bases to determine the actual
7423 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
7426 tree access = BINFO_BASE_ACCESS (pbinfo, i);
7427 tree expanded_bases = NULL_TREE;
7430 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
7433 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
7434 args, tf_error, NULL_TREE);
7435 if (expanded_bases == error_mark_node)
7438 len = TREE_VEC_LENGTH (expanded_bases);
7441 for (idx = 0; idx < len; idx++)
7444 /* Extract the already-expanded base class. */
7445 base = TREE_VEC_ELT (expanded_bases, idx);
7447 /* Substitute to figure out the base class. */
7448 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
7451 if (base == error_mark_node)
7454 base_list = tree_cons (access, base, base_list);
7455 if (BINFO_VIRTUAL_P (pbase_binfo))
7456 TREE_TYPE (base_list) = integer_type_node;
7460 /* The list is now in reverse order; correct that. */
7461 base_list = nreverse (base_list);
7464 pop_scope (pushed_scope);
7466 /* Now call xref_basetypes to set up all the base-class
7468 xref_basetypes (type, base_list);
7470 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
7471 (int) ATTR_FLAG_TYPE_IN_PLACE,
7472 args, tf_error, NULL_TREE);
7474 /* Now that our base classes are set up, enter the scope of the
7475 class, so that name lookups into base classes, etc. will work
7476 correctly. This is precisely analogous to what we do in
7477 begin_class_definition when defining an ordinary non-template
7478 class, except we also need to push the enclosing classes. */
7479 push_nested_class (type);
7481 /* Now members are processed in the order of declaration. */
7482 for (member = CLASSTYPE_DECL_LIST (pattern);
7483 member; member = TREE_CHAIN (member))
7485 tree t = TREE_VALUE (member);
7487 if (TREE_PURPOSE (member))
7491 /* Build new CLASSTYPE_NESTED_UTDS. */
7494 bool class_template_p;
7496 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
7497 && TYPE_LANG_SPECIFIC (t)
7498 && CLASSTYPE_IS_TEMPLATE (t));
7499 /* If the member is a class template, then -- even after
7500 substitution -- there may be dependent types in the
7501 template argument list for the class. We increment
7502 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7503 that function will assume that no types are dependent
7504 when outside of a template. */
7505 if (class_template_p)
7506 ++processing_template_decl;
7507 newtag = tsubst (t, args, tf_error, NULL_TREE);
7508 if (class_template_p)
7509 --processing_template_decl;
7510 if (newtag == error_mark_node)
7513 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
7515 tree name = TYPE_IDENTIFIER (t);
7517 if (class_template_p)
7518 /* Unfortunately, lookup_template_class sets
7519 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
7520 instantiation (i.e., for the type of a member
7521 template class nested within a template class.)
7522 This behavior is required for
7523 maybe_process_partial_specialization to work
7524 correctly, but is not accurate in this case;
7525 the TAG is not an instantiation of anything.
7526 (The corresponding TEMPLATE_DECL is an
7527 instantiation, but the TYPE is not.) */
7528 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
7530 /* Now, we call pushtag to put this NEWTAG into the scope of
7531 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
7532 pushtag calling push_template_decl. We don't have to do
7533 this for enums because it will already have been done in
7536 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
7537 pushtag (name, newtag, /*tag_scope=*/ts_current);
7540 else if (TREE_CODE (t) == FUNCTION_DECL
7541 || DECL_FUNCTION_TEMPLATE_P (t))
7543 /* Build new TYPE_METHODS. */
7546 if (TREE_CODE (t) == TEMPLATE_DECL)
7547 ++processing_template_decl;
7548 r = tsubst (t, args, tf_error, NULL_TREE);
7549 if (TREE_CODE (t) == TEMPLATE_DECL)
7550 --processing_template_decl;
7551 set_current_access_from_decl (r);
7552 finish_member_declaration (r);
7556 /* Build new TYPE_FIELDS. */
7557 if (TREE_CODE (t) == STATIC_ASSERT)
7560 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
7561 tf_warning_or_error, NULL_TREE,
7562 /*integral_constant_expression_p=*/true);
7563 finish_static_assert (condition,
7564 STATIC_ASSERT_MESSAGE (t),
7565 STATIC_ASSERT_SOURCE_LOCATION (t),
7568 else if (TREE_CODE (t) != CONST_DECL)
7572 /* The file and line for this declaration, to
7573 assist in error message reporting. Since we
7574 called push_tinst_level above, we don't need to
7576 input_location = DECL_SOURCE_LOCATION (t);
7578 if (TREE_CODE (t) == TEMPLATE_DECL)
7579 ++processing_template_decl;
7580 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
7581 if (TREE_CODE (t) == TEMPLATE_DECL)
7582 --processing_template_decl;
7583 if (TREE_CODE (r) == VAR_DECL)
7587 [t]he initialization (and any associated
7588 side-effects) of a static data member does
7589 not occur unless the static data member is
7590 itself used in a way that requires the
7591 definition of the static data member to
7594 Therefore, we do not substitute into the
7595 initialized for the static data member here. */
7596 finish_static_data_member_decl
7599 /*init_const_expr_p=*/false,
7600 /*asmspec_tree=*/NULL_TREE,
7602 if (DECL_INITIALIZED_IN_CLASS_P (r))
7603 check_static_variable_definition (r, TREE_TYPE (r));
7605 else if (TREE_CODE (r) == FIELD_DECL)
7607 /* Determine whether R has a valid type and can be
7608 completed later. If R is invalid, then it is
7609 replaced by error_mark_node so that it will not be
7610 added to TYPE_FIELDS. */
7611 tree rtype = TREE_TYPE (r);
7612 if (can_complete_type_without_circularity (rtype))
7613 complete_type (rtype);
7615 if (!COMPLETE_TYPE_P (rtype))
7617 cxx_incomplete_type_error (r, rtype);
7618 r = error_mark_node;
7622 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
7623 such a thing will already have been added to the field
7624 list by tsubst_enum in finish_member_declaration in the
7625 CLASSTYPE_NESTED_UTDS case above. */
7626 if (!(TREE_CODE (r) == TYPE_DECL
7627 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
7628 && DECL_ARTIFICIAL (r)))
7630 set_current_access_from_decl (r);
7631 finish_member_declaration (r);
7638 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
7640 /* Build new CLASSTYPE_FRIEND_CLASSES. */
7642 tree friend_type = t;
7643 bool adjust_processing_template_decl = false;
7645 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7647 /* template <class T> friend class C; */
7648 friend_type = tsubst_friend_class (friend_type, args);
7649 adjust_processing_template_decl = true;
7651 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
7653 /* template <class T> friend class C::D; */
7654 friend_type = tsubst (friend_type, args,
7655 tf_warning_or_error, NULL_TREE);
7656 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7657 friend_type = TREE_TYPE (friend_type);
7658 adjust_processing_template_decl = true;
7660 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
7662 /* This could be either
7666 when dependent_type_p is false or
7668 template <class U> friend class T::C;
7671 friend_type = tsubst (friend_type, args,
7672 tf_warning_or_error, NULL_TREE);
7673 /* Bump processing_template_decl for correct
7674 dependent_type_p calculation. */
7675 ++processing_template_decl;
7676 if (dependent_type_p (friend_type))
7677 adjust_processing_template_decl = true;
7678 --processing_template_decl;
7680 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
7681 && hidden_name_p (TYPE_NAME (friend_type)))
7685 where C hasn't been declared yet. Let's lookup name
7686 from namespace scope directly, bypassing any name that
7687 come from dependent base class. */
7688 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
7690 /* The call to xref_tag_from_type does injection for friend
7692 push_nested_namespace (ns);
7694 xref_tag_from_type (friend_type, NULL_TREE,
7695 /*tag_scope=*/ts_current);
7696 pop_nested_namespace (ns);
7698 else if (uses_template_parms (friend_type))
7699 /* friend class C<T>; */
7700 friend_type = tsubst (friend_type, args,
7701 tf_warning_or_error, NULL_TREE);
7706 where C is already declared or
7708 friend class C<int>;
7710 We don't have to do anything in these cases. */
7712 if (adjust_processing_template_decl)
7713 /* Trick make_friend_class into realizing that the friend
7714 we're adding is a template, not an ordinary class. It's
7715 important that we use make_friend_class since it will
7716 perform some error-checking and output cross-reference
7718 ++processing_template_decl;
7720 if (friend_type != error_mark_node)
7721 make_friend_class (type, friend_type, /*complain=*/false);
7723 if (adjust_processing_template_decl)
7724 --processing_template_decl;
7728 /* Build new DECL_FRIENDLIST. */
7731 /* The file and line for this declaration, to
7732 assist in error message reporting. Since we
7733 called push_tinst_level above, we don't need to
7735 input_location = DECL_SOURCE_LOCATION (t);
7737 if (TREE_CODE (t) == TEMPLATE_DECL)
7739 ++processing_template_decl;
7740 push_deferring_access_checks (dk_no_check);
7743 r = tsubst_friend_function (t, args);
7744 add_friend (type, r, /*complain=*/false);
7745 if (TREE_CODE (t) == TEMPLATE_DECL)
7747 pop_deferring_access_checks ();
7748 --processing_template_decl;
7754 /* Set the file and line number information to whatever is given for
7755 the class itself. This puts error messages involving generated
7756 implicit functions at a predictable point, and the same point
7757 that would be used for non-template classes. */
7758 input_location = DECL_SOURCE_LOCATION (typedecl);
7760 unreverse_member_declarations (type);
7761 finish_struct_1 (type);
7762 TYPE_BEING_DEFINED (type) = 0;
7764 /* Now that the class is complete, instantiate default arguments for
7765 any member functions. We don't do this earlier because the
7766 default arguments may reference members of the class. */
7767 if (!PRIMARY_TEMPLATE_P (templ))
7768 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
7769 if (TREE_CODE (t) == FUNCTION_DECL
7770 /* Implicitly generated member functions will not have template
7771 information; they are not instantiations, but instead are
7772 created "fresh" for each instantiation. */
7773 && DECL_TEMPLATE_INFO (t))
7774 tsubst_default_arguments (t);
7776 /* Some typedefs referenced from within the template code need to be access
7777 checked at template instantiation time, i.e now. These types were
7778 added to the template at parsing time. Let's get those and perform
7779 the access checks then. */
7780 perform_typedefs_access_check (pattern, args);
7781 perform_deferred_access_checks ();
7782 pop_nested_class ();
7783 pop_from_top_level ();
7784 pop_deferring_access_checks ();
7787 /* The vtable for a template class can be emitted in any translation
7788 unit in which the class is instantiated. When there is no key
7789 method, however, finish_struct_1 will already have added TYPE to
7790 the keyed_classes list. */
7791 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
7792 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
7798 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7804 else if (TYPE_P (t))
7805 r = tsubst (t, args, complain, in_decl);
7808 r = tsubst_expr (t, args, complain, in_decl,
7809 /*integral_constant_expression_p=*/true);
7810 r = fold_non_dependent_expr (r);
7815 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
7816 NONTYPE_ARGUMENT_PACK. */
7819 make_fnparm_pack (tree spec_parm)
7821 /* Collect all of the extra "packed" parameters into an
7825 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
7826 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
7827 int i, len = list_length (spec_parm);
7829 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
7830 parmvec = make_tree_vec (len);
7831 parmtypevec = make_tree_vec (len);
7832 for (i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
7834 TREE_VEC_ELT (parmvec, i) = spec_parm;
7835 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
7838 /* Build the argument packs. */
7839 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
7840 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
7841 TREE_TYPE (argpack) = argtypepack;
7846 /* Substitute ARGS into T, which is an pack expansion
7847 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
7848 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
7849 (if only a partial substitution could be performed) or
7850 ERROR_MARK_NODE if there was an error. */
7852 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
7856 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
7857 tree first_arg_pack; int i, len = -1;
7860 bool very_local_specializations = false;
7862 gcc_assert (PACK_EXPANSION_P (t));
7863 pattern = PACK_EXPANSION_PATTERN (t);
7865 /* Determine the argument packs that will instantiate the parameter
7866 packs used in the expansion expression. While we're at it,
7867 compute the number of arguments to be expanded and make sure it
7869 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
7870 pack = TREE_CHAIN (pack))
7872 tree parm_pack = TREE_VALUE (pack);
7873 tree arg_pack = NULL_TREE;
7874 tree orig_arg = NULL_TREE;
7876 if (TREE_CODE (parm_pack) == PARM_DECL)
7878 arg_pack = retrieve_local_specialization (parm_pack);
7879 if (arg_pack == NULL_TREE)
7881 /* This can happen for a parameter name used later in a function
7882 declaration (such as in a late-specified return type). Just
7883 make a dummy decl, since it's only used for its type. */
7884 gcc_assert (cp_unevaluated_operand != 0);
7885 arg_pack = tsubst_decl (parm_pack, args, complain);
7886 arg_pack = make_fnparm_pack (arg_pack);
7891 int level, idx, levels;
7892 template_parm_level_and_index (parm_pack, &level, &idx);
7894 levels = TMPL_ARGS_DEPTH (args);
7895 if (level <= levels)
7896 arg_pack = TMPL_ARG (args, level, idx);
7899 orig_arg = arg_pack;
7900 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
7901 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
7903 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
7904 /* This can only happen if we forget to expand an argument
7905 pack somewhere else. Just return an error, silently. */
7907 result = make_tree_vec (1);
7908 TREE_VEC_ELT (result, 0) = error_mark_node;
7913 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
7914 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
7916 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
7917 tree pattern = PACK_EXPANSION_PATTERN (expansion);
7918 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
7919 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
7920 /* The argument pack that the parameter maps to is just an
7921 expansion of the parameter itself, such as one would
7922 find in the implicit typedef of a class inside the
7923 class itself. Consider this parameter "unsubstituted",
7924 so that we will maintain the outer pack expansion. */
7925 arg_pack = NULL_TREE;
7931 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
7933 /* It's all-or-nothing with incomplete argument packs. */
7934 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7935 return error_mark_node;
7937 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7943 first_arg_pack = arg_pack;
7945 else if (len != my_len)
7948 /* We got explicit args for some packs but not others;
7949 do nothing now and try again after deduction. */
7951 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
7952 error ("mismatched argument pack lengths while expanding "
7956 error ("mismatched argument pack lengths while expanding "
7959 return error_mark_node;
7962 /* Keep track of the parameter packs and their corresponding
7964 packs = tree_cons (parm_pack, arg_pack, packs);
7965 TREE_TYPE (packs) = orig_arg;
7968 /* We can't substitute for this parameter pack. */
7969 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
7971 unsubstituted_packs);
7974 /* We cannot expand this expansion expression, because we don't have
7975 all of the argument packs we need. Substitute into the pattern
7976 and return a PACK_EXPANSION_*. The caller will need to deal with
7978 if (unsubstituted_packs)
7981 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
7982 new_pat = tsubst_expr (pattern, args, complain, in_decl,
7983 /*integral_constant_expression_p=*/false);
7985 new_pat = tsubst (pattern, args, complain, in_decl);
7986 return make_pack_expansion (new_pat);
7989 /* We could not find any argument packs that work. */
7991 return error_mark_node;
7993 if (!local_specializations)
7995 /* We're in a late-specified return type, so we don't have a local
7996 specializations table. Create one for doing this expansion. */
7997 very_local_specializations = true;
7998 local_specializations = htab_create (37,
7999 hash_local_specialization,
8000 eq_local_specializations,
8004 /* For each argument in each argument pack, substitute into the
8006 result = make_tree_vec (len + incomplete);
8007 for (i = 0; i < len + incomplete; ++i)
8009 /* For parameter pack, change the substitution of the parameter
8010 pack to the ith argument in its argument pack, then expand
8012 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8014 tree parm = TREE_PURPOSE (pack);
8016 if (TREE_CODE (parm) == PARM_DECL)
8018 /* Select the Ith argument from the pack. */
8019 tree arg = make_node (ARGUMENT_PACK_SELECT);
8020 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
8021 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
8023 register_local_specialization (arg, parm);
8029 template_parm_level_and_index (parm, &level, &idx);
8033 /* Select the Ith argument from the pack. */
8034 value = make_node (ARGUMENT_PACK_SELECT);
8035 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
8036 ARGUMENT_PACK_SELECT_INDEX (value) = i;
8039 /* Update the corresponding argument. */
8040 TMPL_ARG (args, level, idx) = value;
8044 /* Substitute into the PATTERN with the altered arguments. */
8045 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8046 TREE_VEC_ELT (result, i) =
8047 tsubst_expr (pattern, args, complain, in_decl,
8048 /*integral_constant_expression_p=*/false);
8050 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
8053 /* When we have incomplete argument packs, the last "expanded"
8054 result is itself a pack expansion, which allows us
8055 to deduce more arguments. */
8056 TREE_VEC_ELT (result, i) =
8057 make_pack_expansion (TREE_VEC_ELT (result, i));
8059 if (TREE_VEC_ELT (result, i) == error_mark_node)
8061 result = error_mark_node;
8066 /* Update ARGS to restore the substitution from parameter packs to
8067 their argument packs. */
8068 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8070 tree parm = TREE_PURPOSE (pack);
8072 if (TREE_CODE (parm) == PARM_DECL)
8073 register_local_specialization (TREE_TYPE (pack), parm);
8077 template_parm_level_and_index (parm, &level, &idx);
8079 /* Update the corresponding argument. */
8080 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
8081 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
8084 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
8088 if (very_local_specializations)
8090 htab_delete (local_specializations);
8091 local_specializations = NULL;
8097 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8098 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8099 parameter packs; all parms generated from a function parameter pack will
8100 have the same DECL_PARM_INDEX. */
8103 get_pattern_parm (tree parm, tree tmpl)
8105 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
8108 if (DECL_ARTIFICIAL (parm))
8110 for (patparm = DECL_ARGUMENTS (pattern);
8111 patparm; patparm = TREE_CHAIN (patparm))
8112 if (DECL_ARTIFICIAL (patparm)
8113 && DECL_NAME (parm) == DECL_NAME (patparm))
8118 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
8119 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
8120 gcc_assert (DECL_PARM_INDEX (patparm)
8121 == DECL_PARM_INDEX (parm));
8127 /* Substitute ARGS into the vector or list of template arguments T. */
8130 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8133 int len = TREE_VEC_LENGTH (t);
8134 int need_new = 0, i, expanded_len_adjust = 0, out;
8135 tree *elts = (tree *) alloca (len * sizeof (tree));
8137 for (i = 0; i < len; i++)
8139 tree orig_arg = TREE_VEC_ELT (t, i);
8142 if (TREE_CODE (orig_arg) == TREE_VEC)
8143 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
8144 else if (PACK_EXPANSION_P (orig_arg))
8146 /* Substitute into an expansion expression. */
8147 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
8149 if (TREE_CODE (new_arg) == TREE_VEC)
8150 /* Add to the expanded length adjustment the number of
8151 expanded arguments. We subtract one from this
8152 measurement, because the argument pack expression
8153 itself is already counted as 1 in
8154 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
8155 the argument pack is empty. */
8156 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
8158 else if (ARGUMENT_PACK_P (orig_arg))
8160 /* Substitute into each of the arguments. */
8161 new_arg = TYPE_P (orig_arg)
8162 ? cxx_make_type (TREE_CODE (orig_arg))
8163 : make_node (TREE_CODE (orig_arg));
8165 SET_ARGUMENT_PACK_ARGS (
8167 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
8168 args, complain, in_decl));
8170 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
8171 new_arg = error_mark_node;
8173 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
8174 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
8176 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
8178 if (TREE_TYPE (new_arg) == error_mark_node)
8179 new_arg = error_mark_node;
8183 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
8185 if (new_arg == error_mark_node)
8186 return error_mark_node;
8189 if (new_arg != orig_arg)
8196 /* Make space for the expanded arguments coming from template
8198 t = make_tree_vec (len + expanded_len_adjust);
8199 for (i = 0, out = 0; i < len; i++)
8201 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
8202 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
8203 && TREE_CODE (elts[i]) == TREE_VEC)
8207 /* Now expand the template argument pack "in place". */
8208 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
8209 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
8213 TREE_VEC_ELT (t, out) = elts[i];
8221 /* Return the result of substituting ARGS into the template parameters
8222 given by PARMS. If there are m levels of ARGS and m + n levels of
8223 PARMS, then the result will contain n levels of PARMS. For
8224 example, if PARMS is `template <class T> template <class U>
8225 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
8226 result will be `template <int*, double, class V>'. */
8229 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
8234 /* When substituting into a template, we must set
8235 PROCESSING_TEMPLATE_DECL as the template parameters may be
8236 dependent if they are based on one-another, and the dependency
8237 predicates are short-circuit outside of templates. */
8238 ++processing_template_decl;
8240 for (new_parms = &r;
8241 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
8242 new_parms = &(TREE_CHAIN (*new_parms)),
8243 parms = TREE_CHAIN (parms))
8246 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
8249 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
8255 if (parms == error_mark_node)
8258 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
8260 if (tuple == error_mark_node)
8263 default_value = TREE_PURPOSE (tuple);
8264 parm_decl = TREE_VALUE (tuple);
8266 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
8267 if (TREE_CODE (parm_decl) == PARM_DECL
8268 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
8269 parm_decl = error_mark_node;
8270 default_value = tsubst_template_arg (default_value, args,
8271 complain, NULL_TREE);
8273 tuple = build_tree_list (default_value, parm_decl);
8274 TREE_VEC_ELT (new_vec, i) = tuple;
8278 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
8279 - TMPL_ARGS_DEPTH (args)),
8280 new_vec, NULL_TREE);
8283 --processing_template_decl;
8288 /* Substitute the ARGS into the indicated aggregate (or enumeration)
8289 type T. If T is not an aggregate or enumeration type, it is
8290 handled as if by tsubst. IN_DECL is as for tsubst. If
8291 ENTERING_SCOPE is nonzero, T is the context for a template which
8292 we are presently tsubst'ing. Return the substituted value. */
8295 tsubst_aggr_type (tree t,
8297 tsubst_flags_t complain,
8304 switch (TREE_CODE (t))
8307 if (TYPE_PTRMEMFUNC_P (t))
8308 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
8310 /* Else fall through. */
8313 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
8318 int saved_unevaluated_operand;
8319 int saved_inhibit_evaluation_warnings;
8321 /* In "sizeof(X<I>)" we need to evaluate "I". */
8322 saved_unevaluated_operand = cp_unevaluated_operand;
8323 cp_unevaluated_operand = 0;
8324 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8325 c_inhibit_evaluation_warnings = 0;
8327 /* First, determine the context for the type we are looking
8329 context = TYPE_CONTEXT (t);
8332 context = tsubst_aggr_type (context, args, complain,
8333 in_decl, /*entering_scope=*/1);
8334 /* If context is a nested class inside a class template,
8335 it may still need to be instantiated (c++/33959). */
8336 if (TYPE_P (context))
8337 context = complete_type (context);
8340 /* Then, figure out what arguments are appropriate for the
8341 type we are trying to find. For example, given:
8343 template <class T> struct S;
8344 template <class T, class U> void f(T, U) { S<U> su; }
8346 and supposing that we are instantiating f<int, double>,
8347 then our ARGS will be {int, double}, but, when looking up
8348 S we only want {double}. */
8349 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
8351 if (argvec == error_mark_node)
8352 r = error_mark_node;
8355 r = lookup_template_class (t, argvec, in_decl, context,
8356 entering_scope, complain);
8357 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
8360 cp_unevaluated_operand = saved_unevaluated_operand;
8361 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8366 /* This is not a template type, so there's nothing to do. */
8370 return tsubst (t, args, complain, in_decl);
8374 /* Substitute into the default argument ARG (a default argument for
8375 FN), which has the indicated TYPE. */
8378 tsubst_default_argument (tree fn, tree type, tree arg)
8380 tree saved_class_ptr = NULL_TREE;
8381 tree saved_class_ref = NULL_TREE;
8383 /* This default argument came from a template. Instantiate the
8384 default argument here, not in tsubst. In the case of
8393 we must be careful to do name lookup in the scope of S<T>,
8394 rather than in the current class. */
8395 push_access_scope (fn);
8396 /* The "this" pointer is not valid in a default argument. */
8399 saved_class_ptr = current_class_ptr;
8400 cp_function_chain->x_current_class_ptr = NULL_TREE;
8401 saved_class_ref = current_class_ref;
8402 cp_function_chain->x_current_class_ref = NULL_TREE;
8405 push_deferring_access_checks(dk_no_deferred);
8406 /* The default argument expression may cause implicitly defined
8407 member functions to be synthesized, which will result in garbage
8408 collection. We must treat this situation as if we were within
8409 the body of function so as to avoid collecting live data on the
8412 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
8413 tf_warning_or_error, NULL_TREE,
8414 /*integral_constant_expression_p=*/false);
8416 pop_deferring_access_checks();
8418 /* Restore the "this" pointer. */
8421 cp_function_chain->x_current_class_ptr = saved_class_ptr;
8422 cp_function_chain->x_current_class_ref = saved_class_ref;
8425 /* Make sure the default argument is reasonable. */
8426 arg = check_default_argument (type, arg);
8428 pop_access_scope (fn);
8433 /* Substitute into all the default arguments for FN. */
8436 tsubst_default_arguments (tree fn)
8441 tmpl_args = DECL_TI_ARGS (fn);
8443 /* If this function is not yet instantiated, we certainly don't need
8444 its default arguments. */
8445 if (uses_template_parms (tmpl_args))
8448 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
8450 arg = TREE_CHAIN (arg))
8451 if (TREE_PURPOSE (arg))
8452 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
8454 TREE_PURPOSE (arg));
8457 /* Substitute the ARGS into the T, which is a _DECL. Return the
8458 result of the substitution. Issue error and warning messages under
8459 control of COMPLAIN. */
8462 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
8464 location_t saved_loc;
8469 /* Set the filename and linenumber to improve error-reporting. */
8470 saved_loc = input_location;
8471 input_location = DECL_SOURCE_LOCATION (t);
8473 switch (TREE_CODE (t))
8477 /* We can get here when processing a member function template,
8478 member class template, or template template parameter. */
8479 tree decl = DECL_TEMPLATE_RESULT (t);
8484 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8486 /* Template template parameter is treated here. */
8487 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8488 if (new_type == error_mark_node)
8489 return error_mark_node;
8492 TREE_CHAIN (r) = NULL_TREE;
8493 TREE_TYPE (r) = new_type;
8494 DECL_TEMPLATE_RESULT (r)
8495 = build_decl (DECL_SOURCE_LOCATION (decl),
8496 TYPE_DECL, DECL_NAME (decl), new_type);
8497 DECL_TEMPLATE_PARMS (r)
8498 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8500 TYPE_NAME (new_type) = r;
8504 /* We might already have an instance of this template.
8505 The ARGS are for the surrounding class type, so the
8506 full args contain the tsubst'd args for the context,
8507 plus the innermost args from the template decl. */
8508 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
8509 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
8510 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
8511 /* Because this is a template, the arguments will still be
8512 dependent, even after substitution. If
8513 PROCESSING_TEMPLATE_DECL is not set, the dependency
8514 predicates will short-circuit. */
8515 ++processing_template_decl;
8516 full_args = tsubst_template_args (tmpl_args, args,
8518 --processing_template_decl;
8519 if (full_args == error_mark_node)
8520 return error_mark_node;
8522 /* If this is a default template template argument,
8523 tsubst might not have changed anything. */
8524 if (full_args == tmpl_args)
8527 hash = hash_tmpl_and_args (t, full_args);
8528 spec = retrieve_specialization (t, full_args, hash);
8529 if (spec != NULL_TREE)
8535 /* Make a new template decl. It will be similar to the
8536 original, but will record the current template arguments.
8537 We also create a new function declaration, which is just
8538 like the old one, but points to this new template, rather
8539 than the old one. */
8541 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
8542 TREE_CHAIN (r) = NULL_TREE;
8544 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
8546 if (TREE_CODE (decl) == TYPE_DECL)
8549 ++processing_template_decl;
8550 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8551 --processing_template_decl;
8552 if (new_type == error_mark_node)
8553 return error_mark_node;
8555 TREE_TYPE (r) = new_type;
8556 CLASSTYPE_TI_TEMPLATE (new_type) = r;
8557 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
8558 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
8559 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
8564 ++processing_template_decl;
8565 new_decl = tsubst (decl, args, complain, in_decl);
8566 --processing_template_decl;
8567 if (new_decl == error_mark_node)
8568 return error_mark_node;
8570 DECL_TEMPLATE_RESULT (r) = new_decl;
8571 DECL_TI_TEMPLATE (new_decl) = r;
8572 TREE_TYPE (r) = TREE_TYPE (new_decl);
8573 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
8574 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
8577 SET_DECL_IMPLICIT_INSTANTIATION (r);
8578 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
8579 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
8581 /* The template parameters for this new template are all the
8582 template parameters for the old template, except the
8583 outermost level of parameters. */
8584 DECL_TEMPLATE_PARMS (r)
8585 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8588 if (PRIMARY_TEMPLATE_P (t))
8589 DECL_PRIMARY_TEMPLATE (r) = r;
8591 if (TREE_CODE (decl) != TYPE_DECL)
8592 /* Record this non-type partial instantiation. */
8593 register_specialization (r, t,
8594 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
8602 tree argvec = NULL_TREE;
8610 /* Nobody should be tsubst'ing into non-template functions. */
8611 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
8613 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
8618 /* If T is not dependent, just return it. We have to
8619 increment PROCESSING_TEMPLATE_DECL because
8620 value_dependent_expression_p assumes that nothing is
8621 dependent when PROCESSING_TEMPLATE_DECL is zero. */
8622 ++processing_template_decl;
8623 dependent_p = value_dependent_expression_p (t);
8624 --processing_template_decl;
8628 /* Calculate the most general template of which R is a
8629 specialization, and the complete set of arguments used to
8631 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
8632 argvec = tsubst_template_args (DECL_TI_ARGS
8633 (DECL_TEMPLATE_RESULT (gen_tmpl)),
8634 args, complain, in_decl);
8636 /* Check to see if we already have this specialization. */
8637 hash = hash_tmpl_and_args (gen_tmpl, argvec);
8638 spec = retrieve_specialization (gen_tmpl, argvec, hash);
8646 /* We can see more levels of arguments than parameters if
8647 there was a specialization of a member template, like
8650 template <class T> struct S { template <class U> void f(); }
8651 template <> template <class U> void S<int>::f(U);
8653 Here, we'll be substituting into the specialization,
8654 because that's where we can find the code we actually
8655 want to generate, but we'll have enough arguments for
8656 the most general template.
8658 We also deal with the peculiar case:
8660 template <class T> struct S {
8661 template <class U> friend void f();
8663 template <class U> void f() {}
8665 template void f<double>();
8667 Here, the ARGS for the instantiation of will be {int,
8668 double}. But, we only need as many ARGS as there are
8669 levels of template parameters in CODE_PATTERN. We are
8670 careful not to get fooled into reducing the ARGS in
8673 template <class T> struct S { template <class U> void f(U); }
8674 template <class T> template <> void S<T>::f(int) {}
8676 which we can spot because the pattern will be a
8677 specialization in this case. */
8678 args_depth = TMPL_ARGS_DEPTH (args);
8680 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
8681 if (args_depth > parms_depth
8682 && !DECL_TEMPLATE_SPECIALIZATION (t))
8683 args = get_innermost_template_args (args, parms_depth);
8687 /* This special case arises when we have something like this:
8689 template <class T> struct S {
8690 friend void f<int>(int, double);
8693 Here, the DECL_TI_TEMPLATE for the friend declaration
8694 will be an IDENTIFIER_NODE. We are being called from
8695 tsubst_friend_function, and we want only to create a
8696 new decl (R) with appropriate types so that we can call
8697 determine_specialization. */
8698 gen_tmpl = NULL_TREE;
8701 if (DECL_CLASS_SCOPE_P (t))
8703 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
8707 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
8708 complain, t, /*entering_scope=*/1);
8713 ctx = DECL_CONTEXT (t);
8715 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8716 if (type == error_mark_node)
8717 return error_mark_node;
8719 /* We do NOT check for matching decls pushed separately at this
8720 point, as they may not represent instantiations of this
8721 template, and in any case are considered separate under the
8724 DECL_USE_TEMPLATE (r) = 0;
8725 TREE_TYPE (r) = type;
8726 /* Clear out the mangled name and RTL for the instantiation. */
8727 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
8728 SET_DECL_RTL (r, NULL_RTX);
8729 /* Leave DECL_INITIAL set on deleted instantiations. */
8730 if (!DECL_DELETED_FN (r))
8731 DECL_INITIAL (r) = NULL_TREE;
8732 DECL_CONTEXT (r) = ctx;
8734 if (member && DECL_CONV_FN_P (r))
8735 /* Type-conversion operator. Reconstruct the name, in
8736 case it's the name of one of the template's parameters. */
8737 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
8739 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
8741 DECL_RESULT (r) = NULL_TREE;
8743 TREE_STATIC (r) = 0;
8744 TREE_PUBLIC (r) = TREE_PUBLIC (t);
8745 DECL_EXTERNAL (r) = 1;
8746 /* If this is an instantiation of a function with internal
8747 linkage, we already know what object file linkage will be
8748 assigned to the instantiation. */
8749 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
8750 DECL_DEFER_OUTPUT (r) = 0;
8751 TREE_CHAIN (r) = NULL_TREE;
8752 DECL_PENDING_INLINE_INFO (r) = 0;
8753 DECL_PENDING_INLINE_P (r) = 0;
8754 DECL_SAVED_TREE (r) = NULL_TREE;
8755 DECL_STRUCT_FUNCTION (r) = NULL;
8757 /* We'll re-clone as appropriate in instantiate_template. */
8758 DECL_CLONED_FUNCTION (r) = NULL_TREE;
8760 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
8761 this in the special friend case mentioned above where
8762 GEN_TMPL is NULL. */
8765 DECL_TEMPLATE_INFO (r)
8766 = tree_cons (gen_tmpl, argvec, NULL_TREE);
8767 SET_DECL_IMPLICIT_INSTANTIATION (r);
8768 register_specialization (r, gen_tmpl, argvec, false, hash);
8770 /* We're not supposed to instantiate default arguments
8771 until they are called, for a template. But, for a
8774 template <class T> void f ()
8775 { extern void g(int i = T()); }
8777 we should do the substitution when the template is
8778 instantiated. We handle the member function case in
8779 instantiate_class_template since the default arguments
8780 might refer to other members of the class. */
8782 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8783 && !uses_template_parms (argvec))
8784 tsubst_default_arguments (r);
8787 DECL_TEMPLATE_INFO (r) = NULL_TREE;
8789 /* Copy the list of befriending classes. */
8790 for (friends = &DECL_BEFRIENDING_CLASSES (r);
8792 friends = &TREE_CHAIN (*friends))
8794 *friends = copy_node (*friends);
8795 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
8800 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
8802 maybe_retrofit_in_chrg (r);
8803 if (DECL_CONSTRUCTOR_P (r))
8804 grok_ctor_properties (ctx, r);
8805 /* If this is an instantiation of a member template, clone it.
8806 If it isn't, that'll be handled by
8807 clone_constructors_and_destructors. */
8808 if (PRIMARY_TEMPLATE_P (gen_tmpl))
8809 clone_function_decl (r, /*update_method_vec_p=*/0);
8811 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))
8812 && !grok_op_properties (r, (complain & tf_error) != 0))
8813 return error_mark_node;
8815 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
8816 SET_DECL_FRIEND_CONTEXT (r,
8817 tsubst (DECL_FRIEND_CONTEXT (t),
8818 args, complain, in_decl));
8820 /* Possibly limit visibility based on template args. */
8821 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
8822 if (DECL_VISIBILITY_SPECIFIED (t))
8824 DECL_VISIBILITY_SPECIFIED (r) = 0;
8826 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
8828 determine_visibility (r);
8830 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8831 args, complain, in_decl);
8837 tree type = NULL_TREE;
8839 tree expanded_types = NULL_TREE;
8840 tree prev_r = NULL_TREE;
8841 tree first_r = NULL_TREE;
8843 if (FUNCTION_PARAMETER_PACK_P (t))
8845 /* If there is a local specialization that isn't a
8846 parameter pack, it means that we're doing a "simple"
8847 substitution from inside tsubst_pack_expansion. Just
8848 return the local specialization (which will be a single
8850 tree spec = retrieve_local_specialization (t);
8852 && TREE_CODE (spec) == PARM_DECL
8853 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
8856 /* Expand the TYPE_PACK_EXPANSION that provides the types for
8857 the parameters in this function parameter pack. */
8858 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
8860 if (TREE_CODE (expanded_types) == TREE_VEC)
8862 len = TREE_VEC_LENGTH (expanded_types);
8864 /* Zero-length parameter packs are boring. Just substitute
8867 return tsubst (TREE_CHAIN (t), args, complain,
8872 /* All we did was update the type. Make a note of that. */
8873 type = expanded_types;
8874 expanded_types = NULL_TREE;
8878 /* Loop through all of the parameter's we'll build. When T is
8879 a function parameter pack, LEN is the number of expanded
8880 types in EXPANDED_TYPES; otherwise, LEN is 1. */
8882 for (i = 0; i < len; ++i)
8886 if (DECL_TEMPLATE_PARM_P (t))
8887 SET_DECL_TEMPLATE_PARM_P (r);
8889 /* An argument of a function parameter pack is not a parameter
8891 FUNCTION_PARAMETER_PACK_P (r) = false;
8894 /* We're on the Ith parameter of the function parameter
8897 /* Get the Ith type. */
8898 type = TREE_VEC_ELT (expanded_types, i);
8901 /* Rename the parameter to include the index. */
8903 make_ith_pack_parameter_name (DECL_NAME (r), i);
8906 /* We're dealing with a normal parameter. */
8907 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8909 type = type_decays_to (type);
8910 TREE_TYPE (r) = type;
8911 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8913 if (DECL_INITIAL (r))
8915 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
8916 DECL_INITIAL (r) = TREE_TYPE (r);
8918 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
8922 DECL_CONTEXT (r) = NULL_TREE;
8924 if (!DECL_TEMPLATE_PARM_P (r))
8925 DECL_ARG_TYPE (r) = type_passed_as (type);
8927 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8928 args, complain, in_decl);
8930 /* Keep track of the first new parameter we
8931 generate. That's what will be returned to the
8936 /* Build a proper chain of parameters when substituting
8937 into a function parameter pack. */
8939 TREE_CHAIN (prev_r) = r;
8943 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
8944 complain, TREE_CHAIN (t));
8946 /* FIRST_R contains the start of the chain we've built. */
8956 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8957 if (type == error_mark_node)
8958 return error_mark_node;
8959 TREE_TYPE (r) = type;
8960 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8962 /* DECL_INITIAL gives the number of bits in a bit-field. */
8964 = tsubst_expr (DECL_INITIAL (t), args,
8966 /*integral_constant_expression_p=*/true);
8967 /* We don't have to set DECL_CONTEXT here; it is set by
8968 finish_member_declaration. */
8969 TREE_CHAIN (r) = NULL_TREE;
8970 if (VOID_TYPE_P (type))
8971 error ("instantiation of %q+D as type %qT", r, type);
8973 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8974 args, complain, in_decl);
8979 /* We reach here only for member using decls. */
8980 if (DECL_DEPENDENT_P (t))
8982 r = do_class_using_decl
8983 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
8984 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
8986 r = error_mark_node;
8989 TREE_PROTECTED (r) = TREE_PROTECTED (t);
8990 TREE_PRIVATE (r) = TREE_PRIVATE (t);
8996 TREE_CHAIN (r) = NULL_TREE;
9003 tree argvec = NULL_TREE;
9004 tree gen_tmpl = NULL_TREE;
9006 tree tmpl = NULL_TREE;
9008 tree type = NULL_TREE;
9011 if (TREE_CODE (t) == TYPE_DECL
9012 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
9014 /* If this is the canonical decl, we don't have to
9015 mess with instantiations, and often we can't (for
9016 typename, template type parms and such). Note that
9017 TYPE_NAME is not correct for the above test if
9018 we've copied the type for a typedef. */
9019 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9020 if (type == error_mark_node)
9021 return error_mark_node;
9022 r = TYPE_NAME (type);
9026 /* Check to see if we already have the specialization we
9029 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
9031 /* T is a static data member or namespace-scope entity.
9032 We have to substitute into namespace-scope variables
9033 (even though such entities are never templates) because
9036 template <class T> void f() { extern T t; }
9038 where the entity referenced is not known until
9039 instantiation time. */
9041 ctx = DECL_CONTEXT (t);
9042 if (DECL_CLASS_SCOPE_P (t))
9044 ctx = tsubst_aggr_type (ctx, args,
9046 in_decl, /*entering_scope=*/1);
9047 /* If CTX is unchanged, then T is in fact the
9048 specialization we want. That situation occurs when
9049 referencing a static data member within in its own
9050 class. We can use pointer equality, rather than
9051 same_type_p, because DECL_CONTEXT is always
9053 if (ctx == DECL_CONTEXT (t))
9059 tmpl = DECL_TI_TEMPLATE (t);
9060 gen_tmpl = most_general_template (tmpl);
9061 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
9062 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9063 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9068 /* A local variable. */
9070 /* Subsequent calls to pushdecl will fill this in. */
9072 spec = retrieve_local_specialization (t);
9074 /* If we already have the specialization we need, there is
9075 nothing more to do. */
9082 /* Create a new node for the specialization we need. */
9084 if (type == NULL_TREE)
9085 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9086 if (TREE_CODE (r) == VAR_DECL)
9088 /* Even if the original location is out of scope, the
9089 newly substituted one is not. */
9090 DECL_DEAD_FOR_LOCAL (r) = 0;
9091 DECL_INITIALIZED_P (r) = 0;
9092 DECL_TEMPLATE_INSTANTIATED (r) = 0;
9093 if (type == error_mark_node)
9094 return error_mark_node;
9095 if (TREE_CODE (type) == FUNCTION_TYPE)
9097 /* It may seem that this case cannot occur, since:
9102 declares a function, not a variable. However:
9105 template <typename T> void g() { T t; }
9106 template void g<f>();
9108 is an attempt to declare a variable with function
9110 error ("variable %qD has function type",
9111 /* R is not yet sufficiently initialized, so we
9112 just use its name. */
9114 return error_mark_node;
9116 type = complete_type (type);
9117 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
9118 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
9119 type = check_var_type (DECL_NAME (r), type);
9121 if (DECL_HAS_VALUE_EXPR_P (t))
9123 tree ve = DECL_VALUE_EXPR (t);
9124 ve = tsubst_expr (ve, args, complain, in_decl,
9125 /*constant_expression_p=*/false);
9126 SET_DECL_VALUE_EXPR (r, ve);
9129 else if (DECL_SELF_REFERENCE_P (t))
9130 SET_DECL_SELF_REFERENCE_P (r);
9131 TREE_TYPE (r) = type;
9132 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9133 DECL_CONTEXT (r) = ctx;
9134 /* Clear out the mangled name and RTL for the instantiation. */
9135 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9136 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9137 SET_DECL_RTL (r, NULL_RTX);
9138 /* The initializer must not be expanded until it is required;
9140 DECL_INITIAL (r) = NULL_TREE;
9141 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9142 SET_DECL_RTL (r, NULL_RTX);
9143 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
9144 if (TREE_CODE (r) == VAR_DECL)
9146 /* Possibly limit visibility based on template args. */
9147 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9148 if (DECL_VISIBILITY_SPECIFIED (t))
9150 DECL_VISIBILITY_SPECIFIED (r) = 0;
9152 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9154 determine_visibility (r);
9156 /* Preserve a typedef that names a type. */
9157 else if (TREE_CODE (r) == TYPE_DECL
9158 && DECL_ORIGINAL_TYPE (t)
9159 && type != error_mark_node)
9161 DECL_ORIGINAL_TYPE (r) = tsubst (DECL_ORIGINAL_TYPE (t),
9162 args, complain, in_decl);
9163 TREE_TYPE (r) = type = build_variant_type_copy (type);
9164 TYPE_NAME (type) = r;
9169 /* A static data member declaration is always marked
9170 external when it is declared in-class, even if an
9171 initializer is present. We mimic the non-template
9173 DECL_EXTERNAL (r) = 1;
9175 register_specialization (r, gen_tmpl, argvec, false, hash);
9176 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
9177 SET_DECL_IMPLICIT_INSTANTIATION (r);
9179 else if (cp_unevaluated_operand)
9181 /* We're substituting this var in a decltype outside of its
9182 scope, such as for a lambda return type. Don't add it to
9183 local_specializations, do perform auto deduction. */
9184 tree auto_node = type_uses_auto (type);
9186 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
9187 /*constant_expression_p=*/false);
9189 if (auto_node && init && describable_type (init))
9191 type = do_auto_deduction (type, init, auto_node);
9192 TREE_TYPE (r) = type;
9196 register_local_specialization (r, t);
9198 TREE_CHAIN (r) = NULL_TREE;
9200 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
9201 (int) ATTR_FLAG_TYPE_IN_PLACE,
9202 args, complain, in_decl);
9211 /* Restore the file and line information. */
9212 input_location = saved_loc;
9217 /* Substitute into the ARG_TYPES of a function type. */
9220 tsubst_arg_types (tree arg_types,
9222 tsubst_flags_t complain,
9225 tree remaining_arg_types;
9226 tree type = NULL_TREE;
9228 tree expanded_args = NULL_TREE;
9231 if (!arg_types || arg_types == void_list_node)
9234 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
9235 args, complain, in_decl);
9236 if (remaining_arg_types == error_mark_node)
9237 return error_mark_node;
9239 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
9241 /* For a pack expansion, perform substitution on the
9242 entire expression. Later on, we'll handle the arguments
9244 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
9245 args, complain, in_decl);
9247 if (TREE_CODE (expanded_args) == TREE_VEC)
9248 /* So that we'll spin through the parameters, one by one. */
9249 i = TREE_VEC_LENGTH (expanded_args);
9252 /* We only partially substituted into the parameter
9253 pack. Our type is TYPE_PACK_EXPANSION. */
9254 type = expanded_args;
9255 expanded_args = NULL_TREE;
9263 type = TREE_VEC_ELT (expanded_args, i);
9265 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
9267 if (type == error_mark_node)
9268 return error_mark_node;
9269 if (VOID_TYPE_P (type))
9271 if (complain & tf_error)
9273 error ("invalid parameter type %qT", type);
9275 error ("in declaration %q+D", in_decl);
9277 return error_mark_node;
9280 /* Do array-to-pointer, function-to-pointer conversion, and ignore
9281 top-level qualifiers as required. */
9282 type = TYPE_MAIN_VARIANT (type_decays_to (type));
9284 /* We do not substitute into default arguments here. The standard
9285 mandates that they be instantiated only when needed, which is
9286 done in build_over_call. */
9287 default_arg = TREE_PURPOSE (arg_types);
9289 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
9291 /* We've instantiated a template before its default arguments
9292 have been parsed. This can happen for a nested template
9293 class, and is not an error unless we require the default
9294 argument in a call of this function. */
9295 remaining_arg_types =
9296 tree_cons (default_arg, type, remaining_arg_types);
9297 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
9298 remaining_arg_types);
9301 remaining_arg_types =
9302 hash_tree_cons (default_arg, type, remaining_arg_types);
9305 return remaining_arg_types;
9308 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
9309 *not* handle the exception-specification for FNTYPE, because the
9310 initial substitution of explicitly provided template parameters
9311 during argument deduction forbids substitution into the
9312 exception-specification:
9316 All references in the function type of the function template to the
9317 corresponding template parameters are replaced by the specified tem-
9318 plate argument values. If a substitution in a template parameter or
9319 in the function type of the function template results in an invalid
9320 type, type deduction fails. [Note: The equivalent substitution in
9321 exception specifications is done only when the function is instanti-
9322 ated, at which point a program is ill-formed if the substitution
9323 results in an invalid type.] */
9326 tsubst_function_type (tree t,
9328 tsubst_flags_t complain,
9335 /* The TYPE_CONTEXT is not used for function/method types. */
9336 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
9338 /* Substitute the return type. */
9339 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9340 if (return_type == error_mark_node)
9341 return error_mark_node;
9342 /* The standard does not presently indicate that creation of a
9343 function type with an invalid return type is a deduction failure.
9344 However, that is clearly analogous to creating an array of "void"
9345 or a reference to a reference. This is core issue #486. */
9346 if (TREE_CODE (return_type) == ARRAY_TYPE
9347 || TREE_CODE (return_type) == FUNCTION_TYPE)
9349 if (complain & tf_error)
9351 if (TREE_CODE (return_type) == ARRAY_TYPE)
9352 error ("function returning an array");
9354 error ("function returning a function");
9356 return error_mark_node;
9359 /* Substitute the argument types. */
9360 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
9362 if (arg_types == error_mark_node)
9363 return error_mark_node;
9365 /* Construct a new type node and return it. */
9366 if (TREE_CODE (t) == FUNCTION_TYPE)
9367 fntype = build_function_type (return_type, arg_types);
9370 tree r = TREE_TYPE (TREE_VALUE (arg_types));
9371 if (! MAYBE_CLASS_TYPE_P (r))
9375 Type deduction may fail for any of the following
9378 -- Attempting to create "pointer to member of T" when T
9379 is not a class type. */
9380 if (complain & tf_error)
9381 error ("creating pointer to member function of non-class type %qT",
9383 return error_mark_node;
9386 fntype = build_method_type_directly (r, return_type,
9387 TREE_CHAIN (arg_types));
9389 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
9390 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
9395 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
9396 ARGS into that specification, and return the substituted
9397 specification. If there is no specification, return NULL_TREE. */
9400 tsubst_exception_specification (tree fntype,
9402 tsubst_flags_t complain,
9408 specs = TYPE_RAISES_EXCEPTIONS (fntype);
9409 new_specs = NULL_TREE;
9412 if (! TREE_VALUE (specs))
9419 tree expanded_specs = NULL_TREE;
9421 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
9423 /* Expand the pack expansion type. */
9424 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
9428 if (expanded_specs == error_mark_node)
9429 return error_mark_node;
9430 else if (TREE_CODE (expanded_specs) == TREE_VEC)
9431 len = TREE_VEC_LENGTH (expanded_specs);
9434 /* We're substituting into a member template, so
9435 we got a TYPE_PACK_EXPANSION back. Add that
9436 expansion and move on. */
9437 gcc_assert (TREE_CODE (expanded_specs)
9438 == TYPE_PACK_EXPANSION);
9439 new_specs = add_exception_specifier (new_specs,
9442 specs = TREE_CHAIN (specs);
9447 for (i = 0; i < len; ++i)
9450 spec = TREE_VEC_ELT (expanded_specs, i);
9452 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
9453 if (spec == error_mark_node)
9455 new_specs = add_exception_specifier (new_specs, spec,
9459 specs = TREE_CHAIN (specs);
9465 /* Take the tree structure T and replace template parameters used
9466 therein with the argument vector ARGS. IN_DECL is an associated
9467 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
9468 Issue error and warning messages under control of COMPLAIN. Note
9469 that we must be relatively non-tolerant of extensions here, in
9470 order to preserve conformance; if we allow substitutions that
9471 should not be allowed, we may allow argument deductions that should
9472 not succeed, and therefore report ambiguous overload situations
9473 where there are none. In theory, we could allow the substitution,
9474 but indicate that it should have failed, and allow our caller to
9475 make sure that the right thing happens, but we don't try to do this
9478 This function is used for dealing with types, decls and the like;
9479 for expressions, use tsubst_expr or tsubst_copy. */
9482 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9486 if (t == NULL_TREE || t == error_mark_node
9487 || t == integer_type_node
9488 || t == void_type_node
9489 || t == char_type_node
9490 || t == unknown_type_node
9491 || TREE_CODE (t) == NAMESPACE_DECL)
9495 return tsubst_decl (t, args, complain);
9497 if (args == NULL_TREE)
9500 if (TREE_CODE (t) == IDENTIFIER_NODE)
9501 type = IDENTIFIER_TYPE_VALUE (t);
9503 type = TREE_TYPE (t);
9505 gcc_assert (type != unknown_type_node);
9507 /* Reuse typedefs. We need to do this to handle dependent attributes,
9508 such as attribute aligned. */
9511 && TYPE_NAME (t) != TYPE_MAIN_DECL (t))
9513 tree decl = TYPE_NAME (t);
9515 if (DECL_CLASS_SCOPE_P (decl)
9516 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
9517 && uses_template_parms (DECL_CONTEXT (decl)))
9519 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
9520 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
9521 r = retrieve_specialization (tmpl, gen_args, 0);
9523 else if (DECL_FUNCTION_SCOPE_P (decl)
9524 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
9525 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
9526 r = retrieve_local_specialization (decl);
9528 /* The typedef is from a non-template context. */
9534 r = cp_build_qualified_type_real
9535 (r, cp_type_quals (t) | cp_type_quals (r),
9536 complain | tf_ignore_bad_quals);
9539 /* Else we must be instantiating the typedef, so fall through. */
9543 && TREE_CODE (t) != TYPENAME_TYPE
9544 && TREE_CODE (t) != IDENTIFIER_NODE
9545 && TREE_CODE (t) != FUNCTION_TYPE
9546 && TREE_CODE (t) != METHOD_TYPE)
9547 type = tsubst (type, args, complain, in_decl);
9548 if (type == error_mark_node)
9549 return error_mark_node;
9551 switch (TREE_CODE (t))
9556 return tsubst_aggr_type (t, args, complain, in_decl,
9557 /*entering_scope=*/0);
9560 case IDENTIFIER_NODE:
9572 if (t == integer_type_node)
9575 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
9576 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
9580 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
9582 max = tsubst_expr (omax, args, complain, in_decl,
9583 /*integral_constant_expression_p=*/false);
9585 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
9587 if (TREE_CODE (max) == NOP_EXPR
9588 && TREE_SIDE_EFFECTS (omax)
9589 && !TREE_TYPE (max))
9590 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
9592 max = fold_decl_constant_value (max);
9594 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
9595 with TREE_SIDE_EFFECTS that indicates this is not an integral
9596 constant expression. */
9597 if (processing_template_decl
9598 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
9600 gcc_assert (TREE_CODE (max) == NOP_EXPR);
9601 TREE_SIDE_EFFECTS (max) = 1;
9604 if (TREE_CODE (max) != INTEGER_CST
9605 && !at_function_scope_p ()
9606 && !TREE_SIDE_EFFECTS (max)
9607 && !value_dependent_expression_p (max))
9609 if (complain & tf_error)
9610 error ("array bound is not an integer constant");
9611 return error_mark_node;
9616 Type deduction may fail for any of the following
9619 Attempting to create an array with a size that is
9620 zero or negative. */
9621 if (integer_zerop (max) && !(complain & tf_error))
9622 /* We must fail if performing argument deduction (as
9623 indicated by the state of complain), so that
9624 another substitution can be found. */
9625 return error_mark_node;
9626 else if (TREE_CODE (max) == INTEGER_CST
9627 && INT_CST_LT (max, integer_zero_node))
9629 if (complain & tf_error)
9630 error ("creating array with negative size (%qE)", max);
9632 return error_mark_node;
9635 return compute_array_index_type (NULL_TREE, max);
9638 case TEMPLATE_TYPE_PARM:
9639 case TEMPLATE_TEMPLATE_PARM:
9640 case BOUND_TEMPLATE_TEMPLATE_PARM:
9641 case TEMPLATE_PARM_INDEX:
9646 tree arg = NULL_TREE;
9650 gcc_assert (TREE_VEC_LENGTH (args) > 0);
9651 template_parm_level_and_index (t, &level, &idx);
9653 levels = TMPL_ARGS_DEPTH (args);
9654 if (level <= levels)
9656 arg = TMPL_ARG (args, level, idx);
9658 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
9659 /* See through ARGUMENT_PACK_SELECT arguments. */
9660 arg = ARGUMENT_PACK_SELECT_ARG (arg);
9663 if (arg == error_mark_node)
9664 return error_mark_node;
9665 else if (arg != NULL_TREE)
9667 if (ARGUMENT_PACK_P (arg))
9668 /* If ARG is an argument pack, we don't actually want to
9669 perform a substitution here, because substitutions
9670 for argument packs are only done
9671 element-by-element. We can get to this point when
9672 substituting the type of a non-type template
9673 parameter pack, when that type actually contains
9674 template parameter packs from an outer template, e.g.,
9676 template<typename... Types> struct A {
9677 template<Types... Values> struct B { };
9681 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
9684 gcc_assert (TYPE_P (arg));
9686 /* cv-quals from the template are discarded when
9687 substituting in a function or reference type. */
9688 if (TREE_CODE (arg) == FUNCTION_TYPE
9689 || TREE_CODE (arg) == METHOD_TYPE
9690 || TREE_CODE (arg) == REFERENCE_TYPE)
9691 quals = cp_type_quals (arg);
9693 quals = cp_type_quals (arg) | cp_type_quals (t);
9695 return cp_build_qualified_type_real
9696 (arg, quals, complain | tf_ignore_bad_quals);
9698 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9700 /* We are processing a type constructed from a
9701 template template parameter. */
9702 tree argvec = tsubst (TYPE_TI_ARGS (t),
9703 args, complain, in_decl);
9704 if (argvec == error_mark_node)
9705 return error_mark_node;
9707 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
9708 are resolving nested-types in the signature of a
9709 member function templates. Otherwise ARG is a
9710 TEMPLATE_DECL and is the real template to be
9712 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
9713 arg = TYPE_NAME (arg);
9715 r = lookup_template_class (arg,
9718 /*entering_scope=*/0,
9720 return cp_build_qualified_type_real
9721 (r, TYPE_QUALS (t), complain);
9724 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
9729 /* This can happen during the attempted tsubst'ing in
9730 unify. This means that we don't yet have any information
9731 about the template parameter in question. */
9734 /* If we get here, we must have been looking at a parm for a
9735 more deeply nested template. Make a new version of this
9736 template parameter, but with a lower level. */
9737 switch (TREE_CODE (t))
9739 case TEMPLATE_TYPE_PARM:
9740 case TEMPLATE_TEMPLATE_PARM:
9741 case BOUND_TEMPLATE_TEMPLATE_PARM:
9742 if (cp_type_quals (t))
9744 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
9745 r = cp_build_qualified_type_real
9746 (r, cp_type_quals (t),
9747 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
9748 ? tf_ignore_bad_quals : 0));
9753 TEMPLATE_TYPE_PARM_INDEX (r)
9754 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
9755 r, levels, args, complain);
9756 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
9757 TYPE_MAIN_VARIANT (r) = r;
9758 TYPE_POINTER_TO (r) = NULL_TREE;
9759 TYPE_REFERENCE_TO (r) = NULL_TREE;
9761 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
9762 /* We have reduced the level of the template
9763 template parameter, but not the levels of its
9764 template parameters, so canonical_type_parameter
9765 will not be able to find the canonical template
9766 template parameter for this level. Thus, we
9767 require structural equality checking to compare
9768 TEMPLATE_TEMPLATE_PARMs. */
9769 SET_TYPE_STRUCTURAL_EQUALITY (r);
9770 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
9771 SET_TYPE_STRUCTURAL_EQUALITY (r);
9773 TYPE_CANONICAL (r) = canonical_type_parameter (r);
9775 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9777 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
9779 if (argvec == error_mark_node)
9780 return error_mark_node;
9782 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
9783 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
9788 case TEMPLATE_PARM_INDEX:
9789 r = reduce_template_parm_level (t, type, levels, args, complain);
9801 tree purpose, value, chain;
9803 if (t == void_list_node)
9806 purpose = TREE_PURPOSE (t);
9809 purpose = tsubst (purpose, args, complain, in_decl);
9810 if (purpose == error_mark_node)
9811 return error_mark_node;
9813 value = TREE_VALUE (t);
9816 value = tsubst (value, args, complain, in_decl);
9817 if (value == error_mark_node)
9818 return error_mark_node;
9820 chain = TREE_CHAIN (t);
9821 if (chain && chain != void_type_node)
9823 chain = tsubst (chain, args, complain, in_decl);
9824 if (chain == error_mark_node)
9825 return error_mark_node;
9827 if (purpose == TREE_PURPOSE (t)
9828 && value == TREE_VALUE (t)
9829 && chain == TREE_CHAIN (t))
9831 return hash_tree_cons (purpose, value, chain);
9835 /* We should never be tsubsting a binfo. */
9839 /* A vector of template arguments. */
9841 return tsubst_template_args (t, args, complain, in_decl);
9844 case REFERENCE_TYPE:
9846 enum tree_code code;
9848 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
9851 code = TREE_CODE (t);
9856 Type deduction may fail for any of the following
9859 -- Attempting to create a pointer to reference type.
9860 -- Attempting to create a reference to a reference type or
9861 a reference to void.
9863 Core issue 106 says that creating a reference to a reference
9864 during instantiation is no longer a cause for failure. We
9865 only enforce this check in strict C++98 mode. */
9866 if ((TREE_CODE (type) == REFERENCE_TYPE
9867 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
9868 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
9870 static location_t last_loc;
9872 /* We keep track of the last time we issued this error
9873 message to avoid spewing a ton of messages during a
9874 single bad template instantiation. */
9875 if (complain & tf_error
9876 && last_loc != input_location)
9878 if (TREE_CODE (type) == VOID_TYPE)
9879 error ("forming reference to void");
9881 error ("forming %s to reference type %qT",
9882 (code == POINTER_TYPE) ? "pointer" : "reference",
9884 last_loc = input_location;
9887 return error_mark_node;
9889 else if (code == POINTER_TYPE)
9891 r = build_pointer_type (type);
9892 if (TREE_CODE (type) == METHOD_TYPE)
9893 r = build_ptrmemfunc_type (r);
9895 else if (TREE_CODE (type) == REFERENCE_TYPE)
9896 /* In C++0x, during template argument substitution, when there is an
9897 attempt to create a reference to a reference type, reference
9898 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
9900 "If a template-argument for a template-parameter T names a type
9901 that is a reference to a type A, an attempt to create the type
9902 'lvalue reference to cv T' creates the type 'lvalue reference to
9903 A,' while an attempt to create the type type rvalue reference to
9904 cv T' creates the type T"
9906 r = cp_build_reference_type
9908 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
9910 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
9911 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
9913 if (r != error_mark_node)
9914 /* Will this ever be needed for TYPE_..._TO values? */
9921 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
9922 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
9926 Type deduction may fail for any of the following
9929 -- Attempting to create "pointer to member of T" when T
9930 is not a class type. */
9931 if (complain & tf_error)
9932 error ("creating pointer to member of non-class type %qT", r);
9933 return error_mark_node;
9935 if (TREE_CODE (type) == REFERENCE_TYPE)
9937 if (complain & tf_error)
9938 error ("creating pointer to member reference type %qT", type);
9939 return error_mark_node;
9941 if (TREE_CODE (type) == VOID_TYPE)
9943 if (complain & tf_error)
9944 error ("creating pointer to member of type void");
9945 return error_mark_node;
9947 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
9948 if (TREE_CODE (type) == FUNCTION_TYPE)
9950 /* The type of the implicit object parameter gets its
9951 cv-qualifiers from the FUNCTION_TYPE. */
9953 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
9954 cp_type_quals (type));
9956 method_type = build_method_type_directly (this_type,
9958 TYPE_ARG_TYPES (type));
9959 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
9960 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
9964 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
9973 fntype = tsubst_function_type (t, args, complain, in_decl);
9974 if (fntype == error_mark_node)
9975 return error_mark_node;
9977 /* Substitute the exception specification. */
9978 specs = tsubst_exception_specification (t, args, complain,
9980 if (specs == error_mark_node)
9981 return error_mark_node;
9983 fntype = build_exception_variant (fntype, specs);
9988 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
9989 if (domain == error_mark_node)
9990 return error_mark_node;
9992 /* As an optimization, we avoid regenerating the array type if
9993 it will obviously be the same as T. */
9994 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
9997 /* These checks should match the ones in grokdeclarator.
10001 The deduction may fail for any of the following reasons:
10003 -- Attempting to create an array with an element type that
10004 is void, a function type, or a reference type, or [DR337]
10005 an abstract class type. */
10006 if (TREE_CODE (type) == VOID_TYPE
10007 || TREE_CODE (type) == FUNCTION_TYPE
10008 || TREE_CODE (type) == REFERENCE_TYPE)
10010 if (complain & tf_error)
10011 error ("creating array of %qT", type);
10012 return error_mark_node;
10014 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
10016 if (complain & tf_error)
10017 error ("creating array of %qT, which is an abstract class type",
10019 return error_mark_node;
10022 r = build_cplus_array_type (type, domain);
10024 if (TYPE_USER_ALIGN (t))
10026 TYPE_ALIGN (r) = TYPE_ALIGN (t);
10027 TYPE_USER_ALIGN (r) = 1;
10036 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10037 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10039 if (e1 == error_mark_node || e2 == error_mark_node)
10040 return error_mark_node;
10042 return fold_build2_loc (input_location,
10043 TREE_CODE (t), TREE_TYPE (t), e1, e2);
10049 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10050 if (e == error_mark_node)
10051 return error_mark_node;
10053 return fold_build1_loc (input_location, TREE_CODE (t), TREE_TYPE (t), e);
10056 case TYPENAME_TYPE:
10058 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10059 in_decl, /*entering_scope=*/1);
10060 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
10061 complain, in_decl);
10063 if (ctx == error_mark_node || f == error_mark_node)
10064 return error_mark_node;
10066 if (!MAYBE_CLASS_TYPE_P (ctx))
10068 if (complain & tf_error)
10069 error ("%qT is not a class, struct, or union type", ctx);
10070 return error_mark_node;
10072 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
10074 /* Normally, make_typename_type does not require that the CTX
10075 have complete type in order to allow things like:
10077 template <class T> struct S { typename S<T>::X Y; };
10079 But, such constructs have already been resolved by this
10080 point, so here CTX really should have complete type, unless
10081 it's a partial instantiation. */
10082 if (!(complain & tf_no_class_instantiations))
10083 ctx = complete_type (ctx);
10084 if (!COMPLETE_TYPE_P (ctx))
10086 if (complain & tf_error)
10087 cxx_incomplete_type_error (NULL_TREE, ctx);
10088 return error_mark_node;
10092 f = make_typename_type (ctx, f, typename_type,
10093 (complain & tf_error) | tf_keep_type_decl);
10094 if (f == error_mark_node)
10096 if (TREE_CODE (f) == TYPE_DECL)
10098 complain |= tf_ignore_bad_quals;
10102 if (TREE_CODE (f) != TYPENAME_TYPE)
10104 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
10105 error ("%qT resolves to %qT, which is not an enumeration type",
10107 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
10108 error ("%qT resolves to %qT, which is is not a class type",
10112 return cp_build_qualified_type_real
10113 (f, cp_type_quals (f) | cp_type_quals (t), complain);
10116 case UNBOUND_CLASS_TEMPLATE:
10118 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10119 in_decl, /*entering_scope=*/1);
10120 tree name = TYPE_IDENTIFIER (t);
10121 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
10123 if (ctx == error_mark_node || name == error_mark_node)
10124 return error_mark_node;
10127 parm_list = tsubst_template_parms (parm_list, args, complain);
10128 return make_unbound_class_template (ctx, name, parm_list, complain);
10134 gcc_unreachable ();
10138 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10139 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
10140 /*integral_constant_expression_p=*/false);
10141 if (e1 == error_mark_node || e2 == error_mark_node)
10142 return error_mark_node;
10144 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
10149 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10150 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10151 if (e1 == error_mark_node || e2 == error_mark_node)
10152 return error_mark_node;
10154 return build_qualified_name (/*type=*/NULL_TREE,
10155 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
10162 type = finish_typeof (tsubst_expr
10163 (TYPEOF_TYPE_EXPR (t), args,
10165 /*integral_constant_expression_p=*/false));
10166 return cp_build_qualified_type_real (type,
10168 | cp_type_quals (type),
10172 case DECLTYPE_TYPE:
10176 ++cp_unevaluated_operand;
10177 ++c_inhibit_evaluation_warnings;
10179 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
10181 /*integral_constant_expression_p=*/false);
10183 --cp_unevaluated_operand;
10184 --c_inhibit_evaluation_warnings;
10186 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
10187 type = lambda_capture_field_type (type);
10188 else if (DECLTYPE_FOR_LAMBDA_RETURN (t))
10189 type = lambda_return_type (type);
10191 type = finish_decltype_type
10192 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
10193 return cp_build_qualified_type_real (type,
10195 | cp_type_quals (type),
10199 case TYPE_ARGUMENT_PACK:
10200 case NONTYPE_ARGUMENT_PACK:
10202 tree r = TYPE_P (t)
10203 ? cxx_make_type (TREE_CODE (t))
10204 : make_node (TREE_CODE (t));
10206 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
10210 SET_ARGUMENT_PACK_ARGS (r, packed_out);
10212 /* For template nontype argument packs, also substitute into
10214 if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
10215 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
10222 sorry ("use of %qs in template",
10223 tree_code_name [(int) TREE_CODE (t)]);
10224 return error_mark_node;
10228 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
10229 type of the expression on the left-hand side of the "." or "->"
10233 tsubst_baselink (tree baselink, tree object_type,
10234 tree args, tsubst_flags_t complain, tree in_decl)
10237 tree qualifying_scope;
10240 tree template_args = 0;
10241 bool template_id_p = false;
10243 /* A baselink indicates a function from a base class. Both the
10244 BASELINK_ACCESS_BINFO and the base class referenced may
10245 indicate bases of the template class, rather than the
10246 instantiated class. In addition, lookups that were not
10247 ambiguous before may be ambiguous now. Therefore, we perform
10248 the lookup again. */
10249 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
10250 qualifying_scope = tsubst (qualifying_scope, args,
10251 complain, in_decl);
10252 fns = BASELINK_FUNCTIONS (baselink);
10253 optype = BASELINK_OPTYPE (baselink);
10254 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
10256 template_id_p = true;
10257 template_args = TREE_OPERAND (fns, 1);
10258 fns = TREE_OPERAND (fns, 0);
10260 template_args = tsubst_template_args (template_args, args,
10261 complain, in_decl);
10263 name = DECL_NAME (get_first_fn (fns));
10264 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
10266 /* If lookup found a single function, mark it as used at this
10267 point. (If it lookup found multiple functions the one selected
10268 later by overload resolution will be marked as used at that
10270 if (BASELINK_P (baselink))
10271 fns = BASELINK_FUNCTIONS (baselink);
10272 if (!template_id_p && !really_overloaded_fn (fns))
10273 mark_used (OVL_CURRENT (fns));
10275 /* Add back the template arguments, if present. */
10276 if (BASELINK_P (baselink) && template_id_p)
10277 BASELINK_FUNCTIONS (baselink)
10278 = build_nt (TEMPLATE_ID_EXPR,
10279 BASELINK_FUNCTIONS (baselink),
10281 /* Update the conversion operator type. */
10282 BASELINK_OPTYPE (baselink)
10283 = tsubst (optype, args, complain, in_decl);
10286 object_type = current_class_type;
10287 return adjust_result_of_qualified_name_lookup (baselink,
10292 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
10293 true if the qualified-id will be a postfix-expression in-and-of
10294 itself; false if more of the postfix-expression follows the
10295 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
10299 tsubst_qualified_id (tree qualified_id, tree args,
10300 tsubst_flags_t complain, tree in_decl,
10301 bool done, bool address_p)
10307 tree template_args;
10309 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
10311 /* Figure out what name to look up. */
10312 name = TREE_OPERAND (qualified_id, 1);
10313 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
10315 is_template = true;
10316 template_args = TREE_OPERAND (name, 1);
10318 template_args = tsubst_template_args (template_args, args,
10319 complain, in_decl);
10320 name = TREE_OPERAND (name, 0);
10324 is_template = false;
10325 template_args = NULL_TREE;
10328 /* Substitute into the qualifying scope. When there are no ARGS, we
10329 are just trying to simplify a non-dependent expression. In that
10330 case the qualifying scope may be dependent, and, in any case,
10331 substituting will not help. */
10332 scope = TREE_OPERAND (qualified_id, 0);
10335 scope = tsubst (scope, args, complain, in_decl);
10336 expr = tsubst_copy (name, args, complain, in_decl);
10341 if (dependent_type_p (scope))
10343 tree type = NULL_TREE;
10344 if (DECL_P (expr) && !dependent_scope_p (scope))
10345 type = TREE_TYPE (expr);
10346 return build_qualified_name (type, scope, expr,
10347 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
10350 if (!BASELINK_P (name) && !DECL_P (expr))
10352 if (TREE_CODE (expr) == BIT_NOT_EXPR)
10354 /* A BIT_NOT_EXPR is used to represent a destructor. */
10355 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
10357 error ("qualifying type %qT does not match destructor name ~%qT",
10358 scope, TREE_OPERAND (expr, 0));
10359 expr = error_mark_node;
10362 expr = lookup_qualified_name (scope, complete_dtor_identifier,
10363 /*is_type_p=*/0, false);
10366 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
10367 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
10368 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
10370 if (complain & tf_error)
10372 error ("dependent-name %qE is parsed as a non-type, but "
10373 "instantiation yields a type", qualified_id);
10374 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
10376 return error_mark_node;
10382 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
10384 /* Remember that there was a reference to this entity. */
10388 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
10390 if (complain & tf_error)
10391 qualified_name_lookup_error (scope,
10392 TREE_OPERAND (qualified_id, 1),
10393 expr, input_location);
10394 return error_mark_node;
10398 expr = lookup_template_function (expr, template_args);
10400 if (expr == error_mark_node && complain & tf_error)
10401 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
10402 expr, input_location);
10403 else if (TYPE_P (scope))
10405 expr = (adjust_result_of_qualified_name_lookup
10406 (expr, scope, current_class_type));
10407 expr = (finish_qualified_id_expr
10408 (scope, expr, done, address_p,
10409 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
10410 /*template_arg_p=*/false));
10413 /* Expressions do not generally have reference type. */
10414 if (TREE_CODE (expr) != SCOPE_REF
10415 /* However, if we're about to form a pointer-to-member, we just
10416 want the referenced member referenced. */
10417 && TREE_CODE (expr) != OFFSET_REF)
10418 expr = convert_from_reference (expr);
10423 /* Like tsubst, but deals with expressions. This function just replaces
10424 template parms; to finish processing the resultant expression, use
10428 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10430 enum tree_code code;
10433 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
10436 code = TREE_CODE (t);
10441 r = retrieve_local_specialization (t);
10446 /* This can happen for a parameter name used later in a function
10447 declaration (such as in a late-specified return type). Just
10448 make a dummy decl, since it's only used for its type. */
10449 gcc_assert (cp_unevaluated_operand != 0);
10450 /* We copy T because want to tsubst the PARM_DECL only,
10451 not the following PARM_DECLs that are chained to T. */
10453 r = tsubst_decl (c, args, complain);
10454 /* Give it the template pattern as its context; its true context
10455 hasn't been instantiated yet and this is good enough for
10457 DECL_CONTEXT (r) = DECL_CONTEXT (t);
10460 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
10461 r = ARGUMENT_PACK_SELECT_ARG (r);
10470 if (DECL_TEMPLATE_PARM_P (t))
10471 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
10472 /* There is no need to substitute into namespace-scope
10474 if (DECL_NAMESPACE_SCOPE_P (t))
10476 /* If ARGS is NULL, then T is known to be non-dependent. */
10477 if (args == NULL_TREE)
10478 return integral_constant_value (t);
10480 /* Unfortunately, we cannot just call lookup_name here.
10483 template <int I> int f() {
10485 struct S { void g() { E e = a; } };
10488 When we instantiate f<7>::S::g(), say, lookup_name is not
10489 clever enough to find f<7>::a. */
10491 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
10492 /*entering_scope=*/0);
10494 for (v = TYPE_VALUES (enum_type);
10496 v = TREE_CHAIN (v))
10497 if (TREE_PURPOSE (v) == DECL_NAME (t))
10498 return TREE_VALUE (v);
10500 /* We didn't find the name. That should never happen; if
10501 name-lookup found it during preliminary parsing, we
10502 should find it again here during instantiation. */
10503 gcc_unreachable ();
10508 if (DECL_CONTEXT (t))
10512 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
10513 /*entering_scope=*/1);
10514 if (ctx != DECL_CONTEXT (t))
10516 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
10519 if (complain & tf_error)
10520 error ("using invalid field %qD", t);
10521 return error_mark_node;
10530 case FUNCTION_DECL:
10531 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10532 || local_variable_p (t))
10533 t = tsubst (t, args, complain, in_decl);
10538 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
10540 case TEMPLATE_DECL:
10541 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10542 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
10543 args, complain, in_decl);
10544 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
10545 return tsubst (t, args, complain, in_decl);
10546 else if (DECL_CLASS_SCOPE_P (t)
10547 && uses_template_parms (DECL_CONTEXT (t)))
10549 /* Template template argument like the following example need
10552 template <template <class> class TT> struct C {};
10553 template <class T> struct D {
10554 template <class U> struct E {};
10559 We are processing the template argument `E' in #1 for
10560 the template instantiation #2. Originally, `E' is a
10561 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
10562 have to substitute this with one having context `D<int>'. */
10564 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
10565 return lookup_field (context, DECL_NAME(t), 0, false);
10568 /* Ordinary template template argument. */
10572 case REINTERPRET_CAST_EXPR:
10573 case CONST_CAST_EXPR:
10574 case STATIC_CAST_EXPR:
10575 case DYNAMIC_CAST_EXPR:
10578 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10579 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10582 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
10584 /* We only want to compute the number of arguments. */
10585 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
10586 complain, in_decl);
10589 if (TREE_CODE (expanded) == TREE_VEC)
10590 len = TREE_VEC_LENGTH (expanded);
10592 if (expanded == error_mark_node)
10593 return error_mark_node;
10594 else if (PACK_EXPANSION_P (expanded)
10595 || (TREE_CODE (expanded) == TREE_VEC
10597 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
10599 if (TREE_CODE (expanded) == TREE_VEC)
10600 expanded = TREE_VEC_ELT (expanded, len - 1);
10602 if (TYPE_P (expanded))
10603 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
10604 complain & tf_error);
10606 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
10607 complain & tf_error);
10610 return build_int_cst (size_type_node, len);
10616 case TRUTH_NOT_EXPR:
10619 case UNARY_PLUS_EXPR: /* Unary + */
10624 case REALPART_EXPR:
10625 case IMAGPART_EXPR:
10627 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10628 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10630 case COMPONENT_REF:
10635 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
10636 name = TREE_OPERAND (t, 1);
10637 if (TREE_CODE (name) == BIT_NOT_EXPR)
10639 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10640 complain, in_decl);
10641 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10643 else if (TREE_CODE (name) == SCOPE_REF
10644 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
10646 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
10647 complain, in_decl);
10648 name = TREE_OPERAND (name, 1);
10649 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10650 complain, in_decl);
10651 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10652 name = build_qualified_name (/*type=*/NULL_TREE,
10654 /*template_p=*/false);
10656 else if (TREE_CODE (name) == BASELINK)
10657 name = tsubst_baselink (name,
10658 non_reference (TREE_TYPE (object)),
10662 name = tsubst_copy (name, args, complain, in_decl);
10663 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
10669 case TRUNC_DIV_EXPR:
10670 case CEIL_DIV_EXPR:
10671 case FLOOR_DIV_EXPR:
10672 case ROUND_DIV_EXPR:
10673 case EXACT_DIV_EXPR:
10677 case TRUNC_MOD_EXPR:
10678 case FLOOR_MOD_EXPR:
10679 case TRUTH_ANDIF_EXPR:
10680 case TRUTH_ORIF_EXPR:
10681 case TRUTH_AND_EXPR:
10682 case TRUTH_OR_EXPR:
10695 case COMPOUND_EXPR:
10698 case PREDECREMENT_EXPR:
10699 case PREINCREMENT_EXPR:
10700 case POSTDECREMENT_EXPR:
10701 case POSTINCREMENT_EXPR:
10703 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10704 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10707 return build_qualified_name (/*type=*/NULL_TREE,
10708 tsubst_copy (TREE_OPERAND (t, 0),
10709 args, complain, in_decl),
10710 tsubst_copy (TREE_OPERAND (t, 1),
10711 args, complain, in_decl),
10712 QUALIFIED_NAME_IS_TEMPLATE (t));
10717 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10718 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10719 NULL_TREE, NULL_TREE);
10723 int n = VL_EXP_OPERAND_LENGTH (t);
10724 tree result = build_vl_exp (CALL_EXPR, n);
10726 for (i = 0; i < n; i++)
10727 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
10728 complain, in_decl);
10734 case PSEUDO_DTOR_EXPR:
10737 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10738 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10739 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10740 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
10747 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10748 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10749 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10750 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
10757 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10758 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10759 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
10760 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
10764 case TEMPLATE_ID_EXPR:
10766 /* Substituted template arguments */
10767 tree fn = TREE_OPERAND (t, 0);
10768 tree targs = TREE_OPERAND (t, 1);
10770 fn = tsubst_copy (fn, args, complain, in_decl);
10772 targs = tsubst_template_args (targs, args, complain, in_decl);
10774 return lookup_template_function (fn, targs);
10779 tree purpose, value, chain;
10781 if (t == void_list_node)
10784 purpose = TREE_PURPOSE (t);
10786 purpose = tsubst_copy (purpose, args, complain, in_decl);
10787 value = TREE_VALUE (t);
10789 value = tsubst_copy (value, args, complain, in_decl);
10790 chain = TREE_CHAIN (t);
10791 if (chain && chain != void_type_node)
10792 chain = tsubst_copy (chain, args, complain, in_decl);
10793 if (purpose == TREE_PURPOSE (t)
10794 && value == TREE_VALUE (t)
10795 && chain == TREE_CHAIN (t))
10797 return tree_cons (purpose, value, chain);
10802 case ENUMERAL_TYPE:
10804 case TEMPLATE_TYPE_PARM:
10805 case TEMPLATE_TEMPLATE_PARM:
10806 case BOUND_TEMPLATE_TEMPLATE_PARM:
10807 case TEMPLATE_PARM_INDEX:
10809 case REFERENCE_TYPE:
10811 case FUNCTION_TYPE:
10814 case TYPENAME_TYPE:
10815 case UNBOUND_CLASS_TEMPLATE:
10817 case DECLTYPE_TYPE:
10819 return tsubst (t, args, complain, in_decl);
10821 case IDENTIFIER_NODE:
10822 if (IDENTIFIER_TYPENAME_P (t))
10824 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10825 return mangle_conv_op_name_for_type (new_type);
10831 /* This is handled by tsubst_copy_and_build. */
10832 gcc_unreachable ();
10835 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
10837 tsubst (TREE_TYPE (t), args, complain, in_decl));
10839 case CLEANUP_POINT_EXPR:
10840 /* We shouldn't have built any of these during initial template
10841 generation. Instead, they should be built during instantiation
10842 in response to the saved STMT_IS_FULL_EXPR_P setting. */
10843 gcc_unreachable ();
10846 mark_used (TREE_OPERAND (t, 1));
10849 case EXPR_PACK_EXPANSION:
10850 error ("invalid use of pack expansion expression");
10851 return error_mark_node;
10853 case NONTYPE_ARGUMENT_PACK:
10854 error ("use %<...%> to expand argument pack");
10855 return error_mark_node;
10862 /* Like tsubst_copy, but specifically for OpenMP clauses. */
10865 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
10868 tree new_clauses = NULL, nc, oc;
10870 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
10872 nc = copy_node (oc);
10873 OMP_CLAUSE_CHAIN (nc) = new_clauses;
10876 switch (OMP_CLAUSE_CODE (nc))
10878 case OMP_CLAUSE_LASTPRIVATE:
10879 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
10881 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
10882 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
10883 in_decl, /*integral_constant_expression_p=*/false);
10884 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
10885 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
10888 case OMP_CLAUSE_PRIVATE:
10889 case OMP_CLAUSE_SHARED:
10890 case OMP_CLAUSE_FIRSTPRIVATE:
10891 case OMP_CLAUSE_REDUCTION:
10892 case OMP_CLAUSE_COPYIN:
10893 case OMP_CLAUSE_COPYPRIVATE:
10894 case OMP_CLAUSE_IF:
10895 case OMP_CLAUSE_NUM_THREADS:
10896 case OMP_CLAUSE_SCHEDULE:
10897 case OMP_CLAUSE_COLLAPSE:
10898 OMP_CLAUSE_OPERAND (nc, 0)
10899 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
10900 in_decl, /*integral_constant_expression_p=*/false);
10902 case OMP_CLAUSE_NOWAIT:
10903 case OMP_CLAUSE_ORDERED:
10904 case OMP_CLAUSE_DEFAULT:
10905 case OMP_CLAUSE_UNTIED:
10908 gcc_unreachable ();
10912 return finish_omp_clauses (nreverse (new_clauses));
10915 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
10918 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
10921 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
10923 tree purpose, value, chain;
10928 if (TREE_CODE (t) != TREE_LIST)
10929 return tsubst_copy_and_build (t, args, complain, in_decl,
10930 /*function_p=*/false,
10931 /*integral_constant_expression_p=*/false);
10933 if (t == void_list_node)
10936 purpose = TREE_PURPOSE (t);
10938 purpose = RECUR (purpose);
10939 value = TREE_VALUE (t);
10940 if (value && TREE_CODE (value) != LABEL_DECL)
10941 value = RECUR (value);
10942 chain = TREE_CHAIN (t);
10943 if (chain && chain != void_type_node)
10944 chain = RECUR (chain);
10945 return tree_cons (purpose, value, chain);
10949 /* Substitute one OMP_FOR iterator. */
10952 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
10953 tree condv, tree incrv, tree *clauses,
10954 tree args, tsubst_flags_t complain, tree in_decl,
10955 bool integral_constant_expression_p)
10957 #define RECUR(NODE) \
10958 tsubst_expr ((NODE), args, complain, in_decl, \
10959 integral_constant_expression_p)
10960 tree decl, init, cond, incr, auto_node;
10962 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
10963 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
10964 decl = RECUR (TREE_OPERAND (init, 0));
10965 init = TREE_OPERAND (init, 1);
10966 auto_node = type_uses_auto (TREE_TYPE (decl));
10967 if (auto_node && init)
10969 tree init_expr = init;
10970 if (TREE_CODE (init_expr) == DECL_EXPR)
10971 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
10972 init_expr = RECUR (init_expr);
10974 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
10976 gcc_assert (!type_dependent_expression_p (decl));
10978 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
10980 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
10981 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
10982 if (TREE_CODE (incr) == MODIFY_EXPR)
10983 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
10984 RECUR (TREE_OPERAND (incr, 1)),
10987 incr = RECUR (incr);
10988 TREE_VEC_ELT (declv, i) = decl;
10989 TREE_VEC_ELT (initv, i) = init;
10990 TREE_VEC_ELT (condv, i) = cond;
10991 TREE_VEC_ELT (incrv, i) = incr;
10995 if (init && TREE_CODE (init) != DECL_EXPR)
10998 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11000 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
11001 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
11002 && OMP_CLAUSE_DECL (c) == decl)
11004 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
11005 && OMP_CLAUSE_DECL (c) == decl)
11006 error ("iteration variable %qD should not be firstprivate", decl);
11007 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
11008 && OMP_CLAUSE_DECL (c) == decl)
11009 error ("iteration variable %qD should not be reduction", decl);
11013 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
11014 OMP_CLAUSE_DECL (c) = decl;
11015 c = finish_omp_clauses (c);
11018 OMP_CLAUSE_CHAIN (c) = *clauses;
11023 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
11024 if (COMPARISON_CLASS_P (cond))
11025 cond = build2 (TREE_CODE (cond), boolean_type_node,
11026 RECUR (TREE_OPERAND (cond, 0)),
11027 RECUR (TREE_OPERAND (cond, 1)));
11029 cond = RECUR (cond);
11030 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11031 switch (TREE_CODE (incr))
11033 case PREINCREMENT_EXPR:
11034 case PREDECREMENT_EXPR:
11035 case POSTINCREMENT_EXPR:
11036 case POSTDECREMENT_EXPR:
11037 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
11038 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
11041 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11042 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11044 tree rhs = TREE_OPERAND (incr, 1);
11045 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11046 RECUR (TREE_OPERAND (incr, 0)),
11047 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11048 RECUR (TREE_OPERAND (rhs, 0)),
11049 RECUR (TREE_OPERAND (rhs, 1))));
11052 incr = RECUR (incr);
11055 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11056 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11058 tree lhs = RECUR (TREE_OPERAND (incr, 0));
11059 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
11060 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
11061 TREE_TYPE (decl), lhs,
11062 RECUR (TREE_OPERAND (incr, 2))));
11064 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
11065 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
11066 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
11068 tree rhs = TREE_OPERAND (incr, 2);
11069 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11070 RECUR (TREE_OPERAND (incr, 0)),
11071 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11072 RECUR (TREE_OPERAND (rhs, 0)),
11073 RECUR (TREE_OPERAND (rhs, 1))));
11076 incr = RECUR (incr);
11079 incr = RECUR (incr);
11083 TREE_VEC_ELT (declv, i) = decl;
11084 TREE_VEC_ELT (initv, i) = init;
11085 TREE_VEC_ELT (condv, i) = cond;
11086 TREE_VEC_ELT (incrv, i) = incr;
11090 /* Like tsubst_copy for expressions, etc. but also does semantic
11094 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
11095 bool integral_constant_expression_p)
11097 #define RECUR(NODE) \
11098 tsubst_expr ((NODE), args, complain, in_decl, \
11099 integral_constant_expression_p)
11103 if (t == NULL_TREE || t == error_mark_node)
11106 if (EXPR_HAS_LOCATION (t))
11107 input_location = EXPR_LOCATION (t);
11108 if (STATEMENT_CODE_P (TREE_CODE (t)))
11109 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
11111 switch (TREE_CODE (t))
11113 case STATEMENT_LIST:
11115 tree_stmt_iterator i;
11116 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
11117 RECUR (tsi_stmt (i));
11121 case CTOR_INITIALIZER:
11122 finish_mem_initializers (tsubst_initializer_list
11123 (TREE_OPERAND (t, 0), args));
11127 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
11131 tmp = RECUR (EXPR_STMT_EXPR (t));
11132 if (EXPR_STMT_STMT_EXPR_RESULT (t))
11133 finish_stmt_expr_expr (tmp, cur_stmt_expr);
11135 finish_expr_stmt (tmp);
11139 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
11147 decl = DECL_EXPR_DECL (t);
11148 if (TREE_CODE (decl) == LABEL_DECL)
11149 finish_label_decl (DECL_NAME (decl));
11150 else if (TREE_CODE (decl) == USING_DECL)
11152 tree scope = USING_DECL_SCOPE (decl);
11153 tree name = DECL_NAME (decl);
11156 scope = RECUR (scope);
11157 decl = lookup_qualified_name (scope, name,
11158 /*is_type_p=*/false,
11159 /*complain=*/false);
11160 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
11161 qualified_name_lookup_error (scope, name, decl, input_location);
11163 do_local_using_decl (decl, scope, name);
11167 init = DECL_INITIAL (decl);
11168 decl = tsubst (decl, args, complain, in_decl);
11169 if (decl != error_mark_node)
11171 /* By marking the declaration as instantiated, we avoid
11172 trying to instantiate it. Since instantiate_decl can't
11173 handle local variables, and since we've already done
11174 all that needs to be done, that's the right thing to
11176 if (TREE_CODE (decl) == VAR_DECL)
11177 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11178 if (TREE_CODE (decl) == VAR_DECL
11179 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
11180 /* Anonymous aggregates are a special case. */
11181 finish_anon_union (decl);
11184 maybe_push_decl (decl);
11185 if (TREE_CODE (decl) == VAR_DECL
11186 && DECL_PRETTY_FUNCTION_P (decl))
11188 /* For __PRETTY_FUNCTION__ we have to adjust the
11190 const char *const name
11191 = cxx_printable_name (current_function_decl, 2);
11192 init = cp_fname_init (name, &TREE_TYPE (decl));
11196 tree t = RECUR (init);
11199 /* If we had an initializer but it
11200 instantiated to nothing,
11201 value-initialize the object. This will
11202 only occur when the initializer was a
11203 pack expansion where the parameter packs
11204 used in that expansion were of length
11206 init = build_value_init (TREE_TYPE (decl));
11211 cp_finish_decl (decl, init, false, NULL_TREE, 0);
11216 /* A DECL_EXPR can also be used as an expression, in the condition
11217 clause of an if/for/while construct. */
11222 stmt = begin_for_stmt ();
11223 RECUR (FOR_INIT_STMT (t));
11224 finish_for_init_stmt (stmt);
11225 tmp = RECUR (FOR_COND (t));
11226 finish_for_cond (tmp, stmt);
11227 tmp = RECUR (FOR_EXPR (t));
11228 finish_for_expr (tmp, stmt);
11229 RECUR (FOR_BODY (t));
11230 finish_for_stmt (stmt);
11234 stmt = begin_while_stmt ();
11235 tmp = RECUR (WHILE_COND (t));
11236 finish_while_stmt_cond (tmp, stmt);
11237 RECUR (WHILE_BODY (t));
11238 finish_while_stmt (stmt);
11242 stmt = begin_do_stmt ();
11243 RECUR (DO_BODY (t));
11244 finish_do_body (stmt);
11245 tmp = RECUR (DO_COND (t));
11246 finish_do_stmt (tmp, stmt);
11250 stmt = begin_if_stmt ();
11251 tmp = RECUR (IF_COND (t));
11252 finish_if_stmt_cond (tmp, stmt);
11253 RECUR (THEN_CLAUSE (t));
11254 finish_then_clause (stmt);
11256 if (ELSE_CLAUSE (t))
11258 begin_else_clause (stmt);
11259 RECUR (ELSE_CLAUSE (t));
11260 finish_else_clause (stmt);
11263 finish_if_stmt (stmt);
11267 if (BIND_EXPR_BODY_BLOCK (t))
11268 stmt = begin_function_body ();
11270 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
11271 ? BCS_TRY_BLOCK : 0);
11273 RECUR (BIND_EXPR_BODY (t));
11275 if (BIND_EXPR_BODY_BLOCK (t))
11276 finish_function_body (stmt);
11278 finish_compound_stmt (stmt);
11282 finish_break_stmt ();
11285 case CONTINUE_STMT:
11286 finish_continue_stmt ();
11290 stmt = begin_switch_stmt ();
11291 tmp = RECUR (SWITCH_STMT_COND (t));
11292 finish_switch_cond (tmp, stmt);
11293 RECUR (SWITCH_STMT_BODY (t));
11294 finish_switch_stmt (stmt);
11297 case CASE_LABEL_EXPR:
11298 finish_case_label (EXPR_LOCATION (t),
11299 RECUR (CASE_LOW (t)),
11300 RECUR (CASE_HIGH (t)));
11305 tree decl = LABEL_EXPR_LABEL (t);
11308 label = finish_label_stmt (DECL_NAME (decl));
11309 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
11310 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
11315 tmp = GOTO_DESTINATION (t);
11316 if (TREE_CODE (tmp) != LABEL_DECL)
11317 /* Computed goto's must be tsubst'd into. On the other hand,
11318 non-computed gotos must not be; the identifier in question
11319 will have no binding. */
11322 tmp = DECL_NAME (tmp);
11323 finish_goto_stmt (tmp);
11327 tmp = finish_asm_stmt
11328 (ASM_VOLATILE_P (t),
11329 RECUR (ASM_STRING (t)),
11330 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
11331 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
11332 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
11333 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
11335 tree asm_expr = tmp;
11336 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
11337 asm_expr = TREE_OPERAND (asm_expr, 0);
11338 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
11345 stmt = begin_try_block ();
11346 RECUR (TRY_STMTS (t));
11347 finish_cleanup_try_block (stmt);
11348 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
11352 tree compound_stmt = NULL_TREE;
11354 if (FN_TRY_BLOCK_P (t))
11355 stmt = begin_function_try_block (&compound_stmt);
11357 stmt = begin_try_block ();
11359 RECUR (TRY_STMTS (t));
11361 if (FN_TRY_BLOCK_P (t))
11362 finish_function_try_block (stmt);
11364 finish_try_block (stmt);
11366 RECUR (TRY_HANDLERS (t));
11367 if (FN_TRY_BLOCK_P (t))
11368 finish_function_handler_sequence (stmt, compound_stmt);
11370 finish_handler_sequence (stmt);
11376 tree decl = HANDLER_PARMS (t);
11380 decl = tsubst (decl, args, complain, in_decl);
11381 /* Prevent instantiate_decl from trying to instantiate
11382 this variable. We've already done all that needs to be
11384 if (decl != error_mark_node)
11385 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11387 stmt = begin_handler ();
11388 finish_handler_parms (decl, stmt);
11389 RECUR (HANDLER_BODY (t));
11390 finish_handler (stmt);
11395 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
11398 case STATIC_ASSERT:
11401 tsubst_expr (STATIC_ASSERT_CONDITION (t),
11404 /*integral_constant_expression_p=*/true);
11405 finish_static_assert (condition,
11406 STATIC_ASSERT_MESSAGE (t),
11407 STATIC_ASSERT_SOURCE_LOCATION (t),
11408 /*member_p=*/false);
11413 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
11414 args, complain, in_decl);
11415 stmt = begin_omp_parallel ();
11416 RECUR (OMP_PARALLEL_BODY (t));
11417 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
11418 = OMP_PARALLEL_COMBINED (t);
11422 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
11423 args, complain, in_decl);
11424 stmt = begin_omp_task ();
11425 RECUR (OMP_TASK_BODY (t));
11426 finish_omp_task (tmp, stmt);
11431 tree clauses, body, pre_body;
11432 tree declv, initv, condv, incrv;
11435 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
11436 args, complain, in_decl);
11437 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11438 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11439 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11440 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11442 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
11443 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
11444 &clauses, args, complain, in_decl,
11445 integral_constant_expression_p);
11447 stmt = begin_omp_structured_block ();
11449 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
11450 if (TREE_VEC_ELT (initv, i) == NULL
11451 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
11452 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
11453 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
11455 tree init = RECUR (TREE_VEC_ELT (initv, i));
11456 gcc_assert (init == TREE_VEC_ELT (declv, i));
11457 TREE_VEC_ELT (initv, i) = NULL_TREE;
11461 tree decl_expr = TREE_VEC_ELT (initv, i);
11462 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
11463 gcc_assert (init != NULL);
11464 TREE_VEC_ELT (initv, i) = RECUR (init);
11465 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
11467 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
11470 pre_body = push_stmt_list ();
11471 RECUR (OMP_FOR_PRE_BODY (t));
11472 pre_body = pop_stmt_list (pre_body);
11474 body = push_stmt_list ();
11475 RECUR (OMP_FOR_BODY (t));
11476 body = pop_stmt_list (body);
11478 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
11479 body, pre_body, clauses);
11481 add_stmt (finish_omp_structured_block (stmt));
11487 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
11488 stmt = push_stmt_list ();
11489 RECUR (OMP_BODY (t));
11490 stmt = pop_stmt_list (stmt);
11493 OMP_BODY (t) = stmt;
11494 OMP_CLAUSES (t) = tmp;
11502 stmt = push_stmt_list ();
11503 RECUR (OMP_BODY (t));
11504 stmt = pop_stmt_list (stmt);
11507 OMP_BODY (t) = stmt;
11512 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
11514 tree op1 = TREE_OPERAND (t, 1);
11515 tree lhs = RECUR (TREE_OPERAND (op1, 0));
11516 tree rhs = RECUR (TREE_OPERAND (op1, 1));
11517 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
11521 case EXPR_PACK_EXPANSION:
11522 error ("invalid use of pack expansion expression");
11523 return error_mark_node;
11525 case NONTYPE_ARGUMENT_PACK:
11526 error ("use %<...%> to expand argument pack");
11527 return error_mark_node;
11530 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
11532 return tsubst_copy_and_build (t, args, complain, in_decl,
11533 /*function_p=*/false,
11534 integral_constant_expression_p);
11541 /* T is a postfix-expression that is not being used in a function
11542 call. Return the substituted version of T. */
11545 tsubst_non_call_postfix_expression (tree t, tree args,
11546 tsubst_flags_t complain,
11549 if (TREE_CODE (t) == SCOPE_REF)
11550 t = tsubst_qualified_id (t, args, complain, in_decl,
11551 /*done=*/false, /*address_p=*/false);
11553 t = tsubst_copy_and_build (t, args, complain, in_decl,
11554 /*function_p=*/false,
11555 /*integral_constant_expression_p=*/false);
11560 /* Like tsubst but deals with expressions and performs semantic
11561 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
11564 tsubst_copy_and_build (tree t,
11566 tsubst_flags_t complain,
11569 bool integral_constant_expression_p)
11571 #define RECUR(NODE) \
11572 tsubst_copy_and_build (NODE, args, complain, in_decl, \
11573 /*function_p=*/false, \
11574 integral_constant_expression_p)
11578 if (t == NULL_TREE || t == error_mark_node)
11581 switch (TREE_CODE (t))
11585 /* Fall through. */
11586 case IDENTIFIER_NODE:
11590 bool non_integral_constant_expression_p;
11591 const char *error_msg;
11593 if (IDENTIFIER_TYPENAME_P (t))
11595 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11596 t = mangle_conv_op_name_for_type (new_type);
11599 /* Look up the name. */
11600 decl = lookup_name (t);
11602 /* By convention, expressions use ERROR_MARK_NODE to indicate
11603 failure, not NULL_TREE. */
11604 if (decl == NULL_TREE)
11605 decl = error_mark_node;
11607 decl = finish_id_expression (t, decl, NULL_TREE,
11609 integral_constant_expression_p,
11610 /*allow_non_integral_constant_expression_p=*/false,
11611 &non_integral_constant_expression_p,
11612 /*template_p=*/false,
11614 /*address_p=*/false,
11615 /*template_arg_p=*/false,
11620 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
11621 decl = unqualified_name_lookup_error (decl);
11625 case TEMPLATE_ID_EXPR:
11628 tree templ = RECUR (TREE_OPERAND (t, 0));
11629 tree targs = TREE_OPERAND (t, 1);
11632 targs = tsubst_template_args (targs, args, complain, in_decl);
11634 if (TREE_CODE (templ) == COMPONENT_REF)
11636 object = TREE_OPERAND (templ, 0);
11637 templ = TREE_OPERAND (templ, 1);
11640 object = NULL_TREE;
11641 templ = lookup_template_function (templ, targs);
11644 return build3 (COMPONENT_REF, TREE_TYPE (templ),
11645 object, templ, NULL_TREE);
11647 return baselink_for_fns (templ);
11652 tree r = RECUR (TREE_OPERAND (t, 0));
11654 if (REFERENCE_REF_P (t))
11656 /* A type conversion to reference type will be enclosed in
11657 such an indirect ref, but the substitution of the cast
11658 will have also added such an indirect ref. */
11659 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
11660 r = convert_from_reference (r);
11663 r = build_x_indirect_ref (r, "unary *", complain);
11669 (tsubst (TREE_TYPE (t), args, complain, in_decl),
11670 RECUR (TREE_OPERAND (t, 0)));
11673 case REINTERPRET_CAST_EXPR:
11674 case CONST_CAST_EXPR:
11675 case DYNAMIC_CAST_EXPR:
11676 case STATIC_CAST_EXPR:
11681 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11682 if (integral_constant_expression_p
11683 && !cast_valid_in_integral_constant_expression_p (type))
11685 if (complain & tf_error)
11686 error ("a cast to a type other than an integral or "
11687 "enumeration type cannot appear in a constant-expression");
11688 return error_mark_node;
11691 op = RECUR (TREE_OPERAND (t, 0));
11693 switch (TREE_CODE (t))
11696 return build_functional_cast (type, op, complain);
11697 case REINTERPRET_CAST_EXPR:
11698 return build_reinterpret_cast (type, op, complain);
11699 case CONST_CAST_EXPR:
11700 return build_const_cast (type, op, complain);
11701 case DYNAMIC_CAST_EXPR:
11702 return build_dynamic_cast (type, op, complain);
11703 case STATIC_CAST_EXPR:
11704 return build_static_cast (type, op, complain);
11706 gcc_unreachable ();
11710 case POSTDECREMENT_EXPR:
11711 case POSTINCREMENT_EXPR:
11712 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11713 args, complain, in_decl);
11714 return build_x_unary_op (TREE_CODE (t), op1, complain);
11716 case PREDECREMENT_EXPR:
11717 case PREINCREMENT_EXPR:
11721 case TRUTH_NOT_EXPR:
11722 case UNARY_PLUS_EXPR: /* Unary + */
11723 case REALPART_EXPR:
11724 case IMAGPART_EXPR:
11725 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
11729 op1 = TREE_OPERAND (t, 0);
11730 if (TREE_CODE (op1) == SCOPE_REF)
11731 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
11732 /*done=*/true, /*address_p=*/true);
11734 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
11736 if (TREE_CODE (op1) == LABEL_DECL)
11737 return finish_label_address_expr (DECL_NAME (op1),
11738 EXPR_LOCATION (op1));
11739 return build_x_unary_op (ADDR_EXPR, op1, complain);
11744 case TRUNC_DIV_EXPR:
11745 case CEIL_DIV_EXPR:
11746 case FLOOR_DIV_EXPR:
11747 case ROUND_DIV_EXPR:
11748 case EXACT_DIV_EXPR:
11752 case TRUNC_MOD_EXPR:
11753 case FLOOR_MOD_EXPR:
11754 case TRUTH_ANDIF_EXPR:
11755 case TRUTH_ORIF_EXPR:
11756 case TRUTH_AND_EXPR:
11757 case TRUTH_OR_EXPR:
11772 return build_x_binary_op
11774 RECUR (TREE_OPERAND (t, 0)),
11775 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
11777 : TREE_CODE (TREE_OPERAND (t, 0))),
11778 RECUR (TREE_OPERAND (t, 1)),
11779 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
11781 : TREE_CODE (TREE_OPERAND (t, 1))),
11782 /*overloaded_p=*/NULL,
11786 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
11787 /*address_p=*/false);
11789 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11790 args, complain, in_decl);
11791 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
11794 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
11795 return tsubst_copy (t, args, complain, in_decl);
11799 op1 = TREE_OPERAND (t, 0);
11802 /* When there are no ARGS, we are trying to evaluate a
11803 non-dependent expression from the parser. Trying to do
11804 the substitutions may not work. */
11806 op1 = TREE_TYPE (op1);
11810 ++cp_unevaluated_operand;
11811 ++c_inhibit_evaluation_warnings;
11812 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
11813 /*function_p=*/false,
11814 /*integral_constant_expression_p=*/false);
11815 --cp_unevaluated_operand;
11816 --c_inhibit_evaluation_warnings;
11819 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
11820 complain & tf_error);
11822 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
11823 complain & tf_error);
11827 tree r = build_x_modify_expr
11828 (RECUR (TREE_OPERAND (t, 0)),
11829 TREE_CODE (TREE_OPERAND (t, 1)),
11830 RECUR (TREE_OPERAND (t, 2)),
11832 /* TREE_NO_WARNING must be set if either the expression was
11833 parenthesized or it uses an operator such as >>= rather
11834 than plain assignment. In the former case, it was already
11835 set and must be copied. In the latter case,
11836 build_x_modify_expr sets it and it must not be reset
11838 if (TREE_NO_WARNING (t))
11839 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
11844 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11845 args, complain, in_decl);
11846 /* Remember that there was a reference to this entity. */
11849 return build_x_arrow (op1);
11853 tree placement = RECUR (TREE_OPERAND (t, 0));
11854 tree init = RECUR (TREE_OPERAND (t, 3));
11855 VEC(tree,gc) *placement_vec;
11856 VEC(tree,gc) *init_vec;
11859 if (placement == NULL_TREE)
11860 placement_vec = NULL;
11863 placement_vec = make_tree_vector ();
11864 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
11865 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
11868 /* If there was an initializer in the original tree, but it
11869 instantiated to an empty list, then we should pass a
11870 non-NULL empty vector to tell build_new that it was an
11871 empty initializer() rather than no initializer. This can
11872 only happen when the initializer is a pack expansion whose
11873 parameter packs are of length zero. */
11874 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
11878 init_vec = make_tree_vector ();
11879 if (init == void_zero_node)
11880 gcc_assert (init_vec != NULL);
11883 for (; init != NULL_TREE; init = TREE_CHAIN (init))
11884 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
11888 ret = build_new (&placement_vec,
11889 RECUR (TREE_OPERAND (t, 1)),
11890 RECUR (TREE_OPERAND (t, 2)),
11892 NEW_EXPR_USE_GLOBAL (t),
11895 if (placement_vec != NULL)
11896 release_tree_vector (placement_vec);
11897 if (init_vec != NULL)
11898 release_tree_vector (init_vec);
11904 return delete_sanity
11905 (RECUR (TREE_OPERAND (t, 0)),
11906 RECUR (TREE_OPERAND (t, 1)),
11907 DELETE_EXPR_USE_VEC (t),
11908 DELETE_EXPR_USE_GLOBAL (t));
11910 case COMPOUND_EXPR:
11911 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
11912 RECUR (TREE_OPERAND (t, 1)),
11918 VEC(tree,gc) *call_args;
11919 unsigned int nargs, i;
11924 function = CALL_EXPR_FN (t);
11925 /* When we parsed the expression, we determined whether or
11926 not Koenig lookup should be performed. */
11927 koenig_p = KOENIG_LOOKUP_P (t);
11928 if (TREE_CODE (function) == SCOPE_REF)
11930 qualified_p = true;
11931 function = tsubst_qualified_id (function, args, complain, in_decl,
11933 /*address_p=*/false);
11937 if (TREE_CODE (function) == COMPONENT_REF)
11939 tree op = TREE_OPERAND (function, 1);
11941 qualified_p = (TREE_CODE (op) == SCOPE_REF
11942 || (BASELINK_P (op)
11943 && BASELINK_QUALIFIED_P (op)));
11946 qualified_p = false;
11948 function = tsubst_copy_and_build (function, args, complain,
11951 integral_constant_expression_p);
11953 if (BASELINK_P (function))
11954 qualified_p = true;
11957 nargs = call_expr_nargs (t);
11958 call_args = make_tree_vector ();
11959 for (i = 0; i < nargs; ++i)
11961 tree arg = CALL_EXPR_ARG (t, i);
11963 if (!PACK_EXPANSION_P (arg))
11964 VEC_safe_push (tree, gc, call_args,
11965 RECUR (CALL_EXPR_ARG (t, i)));
11968 /* Expand the pack expansion and push each entry onto
11970 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
11971 if (TREE_CODE (arg) == TREE_VEC)
11973 unsigned int len, j;
11975 len = TREE_VEC_LENGTH (arg);
11976 for (j = 0; j < len; ++j)
11978 tree value = TREE_VEC_ELT (arg, j);
11979 if (value != NULL_TREE)
11980 value = convert_from_reference (value);
11981 VEC_safe_push (tree, gc, call_args, value);
11986 /* A partial substitution. Add one entry. */
11987 VEC_safe_push (tree, gc, call_args, arg);
11992 /* We do not perform argument-dependent lookup if normal
11993 lookup finds a non-function, in accordance with the
11994 expected resolution of DR 218. */
11996 && ((is_overloaded_fn (function)
11997 /* If lookup found a member function, the Koenig lookup is
11998 not appropriate, even if an unqualified-name was used
11999 to denote the function. */
12000 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
12001 || TREE_CODE (function) == IDENTIFIER_NODE)
12002 /* Only do this when substitution turns a dependent call
12003 into a non-dependent call. */
12004 && type_dependent_expression_p_push (t)
12005 && !any_type_dependent_arguments_p (call_args))
12006 function = perform_koenig_lookup (function, call_args);
12008 if (TREE_CODE (function) == IDENTIFIER_NODE)
12010 unqualified_name_lookup_error (function);
12011 release_tree_vector (call_args);
12012 return error_mark_node;
12015 /* Remember that there was a reference to this entity. */
12016 if (DECL_P (function))
12017 mark_used (function);
12019 if (TREE_CODE (function) == OFFSET_REF)
12020 ret = build_offset_ref_call_from_tree (function, &call_args);
12021 else if (TREE_CODE (function) == COMPONENT_REF)
12023 if (!BASELINK_P (TREE_OPERAND (function, 1)))
12024 ret = finish_call_expr (function, &call_args,
12025 /*disallow_virtual=*/false,
12026 /*koenig_p=*/false,
12029 ret = (build_new_method_call
12030 (TREE_OPERAND (function, 0),
12031 TREE_OPERAND (function, 1),
12032 &call_args, NULL_TREE,
12033 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
12038 ret = finish_call_expr (function, &call_args,
12039 /*disallow_virtual=*/qualified_p,
12043 release_tree_vector (call_args);
12049 return build_x_conditional_expr
12050 (RECUR (TREE_OPERAND (t, 0)),
12051 RECUR (TREE_OPERAND (t, 1)),
12052 RECUR (TREE_OPERAND (t, 2)),
12055 case PSEUDO_DTOR_EXPR:
12056 return finish_pseudo_destructor_expr
12057 (RECUR (TREE_OPERAND (t, 0)),
12058 RECUR (TREE_OPERAND (t, 1)),
12059 RECUR (TREE_OPERAND (t, 2)));
12063 tree purpose, value, chain;
12065 if (t == void_list_node)
12068 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
12069 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
12071 /* We have pack expansions, so expand those and
12072 create a new list out of it. */
12073 tree purposevec = NULL_TREE;
12074 tree valuevec = NULL_TREE;
12078 /* Expand the argument expressions. */
12079 if (TREE_PURPOSE (t))
12080 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
12081 complain, in_decl);
12082 if (TREE_VALUE (t))
12083 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
12084 complain, in_decl);
12086 /* Build the rest of the list. */
12087 chain = TREE_CHAIN (t);
12088 if (chain && chain != void_type_node)
12089 chain = RECUR (chain);
12091 /* Determine the number of arguments. */
12092 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
12094 len = TREE_VEC_LENGTH (purposevec);
12095 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
12097 else if (TREE_CODE (valuevec) == TREE_VEC)
12098 len = TREE_VEC_LENGTH (valuevec);
12101 /* Since we only performed a partial substitution into
12102 the argument pack, we only return a single list
12104 if (purposevec == TREE_PURPOSE (t)
12105 && valuevec == TREE_VALUE (t)
12106 && chain == TREE_CHAIN (t))
12109 return tree_cons (purposevec, valuevec, chain);
12112 /* Convert the argument vectors into a TREE_LIST */
12116 /* Grab the Ith values. */
12118 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
12121 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
12124 /* Build the list (backwards). */
12125 chain = tree_cons (purpose, value, chain);
12131 purpose = TREE_PURPOSE (t);
12133 purpose = RECUR (purpose);
12134 value = TREE_VALUE (t);
12136 value = RECUR (value);
12137 chain = TREE_CHAIN (t);
12138 if (chain && chain != void_type_node)
12139 chain = RECUR (chain);
12140 if (purpose == TREE_PURPOSE (t)
12141 && value == TREE_VALUE (t)
12142 && chain == TREE_CHAIN (t))
12144 return tree_cons (purpose, value, chain);
12147 case COMPONENT_REF:
12153 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12154 args, complain, in_decl);
12155 /* Remember that there was a reference to this entity. */
12156 if (DECL_P (object))
12157 mark_used (object);
12158 object_type = TREE_TYPE (object);
12160 member = TREE_OPERAND (t, 1);
12161 if (BASELINK_P (member))
12162 member = tsubst_baselink (member,
12163 non_reference (TREE_TYPE (object)),
12164 args, complain, in_decl);
12166 member = tsubst_copy (member, args, complain, in_decl);
12167 if (member == error_mark_node)
12168 return error_mark_node;
12170 if (object_type && !CLASS_TYPE_P (object_type))
12172 if (SCALAR_TYPE_P (object_type))
12174 tree s = NULL_TREE;
12175 tree dtor = member;
12177 if (TREE_CODE (dtor) == SCOPE_REF)
12179 s = TREE_OPERAND (dtor, 0);
12180 dtor = TREE_OPERAND (dtor, 1);
12182 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
12184 dtor = TREE_OPERAND (dtor, 0);
12186 return finish_pseudo_destructor_expr (object, s, dtor);
12190 else if (TREE_CODE (member) == SCOPE_REF
12191 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
12196 /* Lookup the template functions now that we know what the
12198 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
12199 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
12200 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
12201 /*is_type_p=*/false,
12202 /*complain=*/false);
12203 if (BASELINK_P (member))
12205 BASELINK_FUNCTIONS (member)
12206 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
12208 member = (adjust_result_of_qualified_name_lookup
12209 (member, BINFO_TYPE (BASELINK_BINFO (member)),
12214 qualified_name_lookup_error (object_type, tmpl, member,
12216 return error_mark_node;
12219 else if (TREE_CODE (member) == SCOPE_REF
12220 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
12221 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
12223 if (complain & tf_error)
12225 if (TYPE_P (TREE_OPERAND (member, 0)))
12226 error ("%qT is not a class or namespace",
12227 TREE_OPERAND (member, 0));
12229 error ("%qD is not a class or namespace",
12230 TREE_OPERAND (member, 0));
12232 return error_mark_node;
12234 else if (TREE_CODE (member) == FIELD_DECL)
12235 return finish_non_static_data_member (member, object, NULL_TREE);
12237 return finish_class_member_access_expr (object, member,
12238 /*template_p=*/false,
12244 (RECUR (TREE_OPERAND (t, 0)));
12248 VEC(constructor_elt,gc) *n;
12249 constructor_elt *ce;
12250 unsigned HOST_WIDE_INT idx;
12251 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12252 bool process_index_p;
12254 bool need_copy_p = false;
12257 if (type == error_mark_node)
12258 return error_mark_node;
12260 /* digest_init will do the wrong thing if we let it. */
12261 if (type && TYPE_PTRMEMFUNC_P (type))
12264 /* We do not want to process the index of aggregate
12265 initializers as they are identifier nodes which will be
12266 looked up by digest_init. */
12267 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
12269 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
12270 newlen = VEC_length (constructor_elt, n);
12271 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
12273 if (ce->index && process_index_p)
12274 ce->index = RECUR (ce->index);
12276 if (PACK_EXPANSION_P (ce->value))
12278 /* Substitute into the pack expansion. */
12279 ce->value = tsubst_pack_expansion (ce->value, args, complain,
12282 if (ce->value == error_mark_node)
12284 else if (TREE_VEC_LENGTH (ce->value) == 1)
12285 /* Just move the argument into place. */
12286 ce->value = TREE_VEC_ELT (ce->value, 0);
12289 /* Update the length of the final CONSTRUCTOR
12290 arguments vector, and note that we will need to
12292 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
12293 need_copy_p = true;
12297 ce->value = RECUR (ce->value);
12302 VEC(constructor_elt,gc) *old_n = n;
12304 n = VEC_alloc (constructor_elt, gc, newlen);
12305 for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
12308 if (TREE_CODE (ce->value) == TREE_VEC)
12310 int i, len = TREE_VEC_LENGTH (ce->value);
12311 for (i = 0; i < len; ++i)
12312 CONSTRUCTOR_APPEND_ELT (n, 0,
12313 TREE_VEC_ELT (ce->value, i));
12316 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
12320 r = build_constructor (init_list_type_node, n);
12321 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
12323 if (TREE_HAS_CONSTRUCTOR (t))
12324 return finish_compound_literal (type, r);
12331 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
12332 if (TYPE_P (operand_0))
12333 return get_typeid (operand_0);
12334 return build_typeid (operand_0);
12344 tree r = tsubst_copy (t, args, complain, in_decl);
12346 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
12347 /* If the original type was a reference, we'll be wrapped in
12348 the appropriate INDIRECT_REF. */
12349 r = convert_from_reference (r);
12354 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
12355 tsubst_copy (TREE_TYPE (t), args, complain,
12358 case OFFSETOF_EXPR:
12359 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
12363 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
12364 complain, in_decl);
12366 tree type2 = TRAIT_EXPR_TYPE2 (t);
12368 type2 = tsubst_copy (type2, args, complain, in_decl);
12370 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
12375 tree old_stmt_expr = cur_stmt_expr;
12376 tree stmt_expr = begin_stmt_expr ();
12378 cur_stmt_expr = stmt_expr;
12379 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
12380 integral_constant_expression_p);
12381 stmt_expr = finish_stmt_expr (stmt_expr, false);
12382 cur_stmt_expr = old_stmt_expr;
12388 t = tsubst_copy (t, args, complain, in_decl);
12389 /* As in finish_id_expression, we resolve enumeration constants
12390 to their underlying values. */
12391 if (TREE_CODE (t) == CONST_DECL)
12393 used_types_insert (TREE_TYPE (t));
12394 return DECL_INITIAL (t);
12400 tree r = build_lambda_expr ();
12402 tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12403 TREE_TYPE (r) = type;
12404 CLASSTYPE_LAMBDA_EXPR (type) = r;
12406 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
12407 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
12408 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
12409 LAMBDA_EXPR_DISCRIMINATOR (r)
12410 = (LAMBDA_EXPR_DISCRIMINATOR (t));
12411 LAMBDA_EXPR_CAPTURE_LIST (r)
12412 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
12413 LAMBDA_EXPR_THIS_CAPTURE (r)
12414 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t));
12415 LAMBDA_EXPR_EXTRA_SCOPE (r)
12416 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
12418 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
12419 determine_visibility (TYPE_NAME (type));
12420 /* Now that we know visibility, instantiate the type so we have a
12421 declaration of the op() for later calls to lambda_function. */
12422 complete_type (type);
12424 type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
12426 apply_lambda_return_type (r, type);
12428 return build_lambda_object (r);
12432 /* Handle Objective-C++ constructs, if appropriate. */
12435 = objcp_tsubst_copy_and_build (t, args, complain,
12436 in_decl, /*function_p=*/false);
12440 return tsubst_copy (t, args, complain, in_decl);
12446 /* Verify that the instantiated ARGS are valid. For type arguments,
12447 make sure that the type is not variably modified. For non-type arguments,
12448 make sure they are constants if they are integral or enumerations.
12449 Emit an error under control of COMPLAIN, and return TRUE on error. */
12452 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
12454 if (ARGUMENT_PACK_P (t))
12456 tree vec = ARGUMENT_PACK_ARGS (t);
12457 int len = TREE_VEC_LENGTH (vec);
12458 bool result = false;
12461 for (i = 0; i < len; ++i)
12462 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
12466 else if (TYPE_P (t))
12468 if (variably_modified_type_p (t, NULL_TREE))
12470 if (complain & tf_error)
12471 error ("%qT is a variably modified type", t);
12475 /* A non-type argument of integral or enumerated type must be a
12477 else if (TREE_TYPE (t)
12478 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
12479 && !TREE_CONSTANT (t))
12481 if (complain & tf_error)
12482 error ("integral expression %qE is not constant", t);
12489 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
12491 int ix, len = DECL_NTPARMS (tmpl);
12492 bool result = false;
12494 for (ix = 0; ix != len; ix++)
12496 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
12499 if (result && (complain & tf_error))
12500 error (" trying to instantiate %qD", tmpl);
12504 /* Instantiate the indicated variable or function template TMPL with
12505 the template arguments in TARG_PTR. */
12508 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
12510 tree targ_ptr = orig_args;
12514 HOST_WIDE_INT saved_processing_template_decl;
12516 if (tmpl == error_mark_node)
12517 return error_mark_node;
12519 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
12521 /* If this function is a clone, handle it specially. */
12522 if (DECL_CLONED_FUNCTION_P (tmpl))
12527 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
12528 DECL_CLONED_FUNCTION. */
12529 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
12530 targ_ptr, complain);
12531 if (spec == error_mark_node)
12532 return error_mark_node;
12534 /* Look for the clone. */
12535 FOR_EACH_CLONE (clone, spec)
12536 if (DECL_NAME (clone) == DECL_NAME (tmpl))
12538 /* We should always have found the clone by now. */
12539 gcc_unreachable ();
12543 /* Check to see if we already have this specialization. */
12544 gen_tmpl = most_general_template (tmpl);
12545 if (tmpl != gen_tmpl)
12546 /* The TMPL is a partial instantiation. To get a full set of
12547 arguments we must add the arguments used to perform the
12548 partial instantiation. */
12549 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
12552 /* It would be nice to avoid hashing here and then again in tsubst_decl,
12553 but it doesn't seem to be on the hot path. */
12554 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
12556 gcc_assert (tmpl == gen_tmpl
12557 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
12559 || fndecl == NULL_TREE);
12561 if (spec != NULL_TREE)
12564 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
12566 return error_mark_node;
12568 /* We are building a FUNCTION_DECL, during which the access of its
12569 parameters and return types have to be checked. However this
12570 FUNCTION_DECL which is the desired context for access checking
12571 is not built yet. We solve this chicken-and-egg problem by
12572 deferring all checks until we have the FUNCTION_DECL. */
12573 push_deferring_access_checks (dk_deferred);
12575 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
12576 (because, for example, we have encountered a non-dependent
12577 function call in the body of a template function and must now
12578 determine which of several overloaded functions will be called),
12579 within the instantiation itself we are not processing a
12581 saved_processing_template_decl = processing_template_decl;
12582 processing_template_decl = 0;
12583 /* Substitute template parameters to obtain the specialization. */
12584 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
12585 targ_ptr, complain, gen_tmpl);
12586 processing_template_decl = saved_processing_template_decl;
12587 if (fndecl == error_mark_node)
12588 return error_mark_node;
12590 /* Now we know the specialization, compute access previously
12592 push_access_scope (fndecl);
12594 /* Some typedefs referenced from within the template code need to be access
12595 checked at template instantiation time, i.e now. These types were
12596 added to the template at parsing time. Let's get those and perfom
12597 the acces checks then. */
12598 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
12599 perform_deferred_access_checks ();
12600 pop_access_scope (fndecl);
12601 pop_deferring_access_checks ();
12603 /* The DECL_TI_TEMPLATE should always be the immediate parent
12604 template, not the most general template. */
12605 DECL_TI_TEMPLATE (fndecl) = tmpl;
12607 /* If we've just instantiated the main entry point for a function,
12608 instantiate all the alternate entry points as well. We do this
12609 by cloning the instantiation of the main entry point, not by
12610 instantiating the template clones. */
12611 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
12612 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
12617 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
12618 NARGS elements of the arguments that are being used when calling
12619 it. TARGS is a vector into which the deduced template arguments
12622 Return zero for success, 2 for an incomplete match that doesn't resolve
12623 all the types, and 1 for complete failure. An error message will be
12624 printed only for an incomplete match.
12626 If FN is a conversion operator, or we are trying to produce a specific
12627 specialization, RETURN_TYPE is the return type desired.
12629 The EXPLICIT_TARGS are explicit template arguments provided via a
12632 The parameter STRICT is one of:
12635 We are deducing arguments for a function call, as in
12636 [temp.deduct.call].
12639 We are deducing arguments for a conversion function, as in
12640 [temp.deduct.conv].
12643 We are deducing arguments when doing an explicit instantiation
12644 as in [temp.explicit], when determining an explicit specialization
12645 as in [temp.expl.spec], or when taking the address of a function
12646 template, as in [temp.deduct.funcaddr]. */
12649 fn_type_unification (tree fn,
12650 tree explicit_targs,
12653 unsigned int nargs,
12655 unification_kind_t strict,
12661 bool incomplete_argument_packs_p = false;
12663 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
12665 fntype = TREE_TYPE (fn);
12666 if (explicit_targs)
12670 The specified template arguments must match the template
12671 parameters in kind (i.e., type, nontype, template), and there
12672 must not be more arguments than there are parameters;
12673 otherwise type deduction fails.
12675 Nontype arguments must match the types of the corresponding
12676 nontype template parameters, or must be convertible to the
12677 types of the corresponding nontype parameters as specified in
12678 _temp.arg.nontype_, otherwise type deduction fails.
12680 All references in the function type of the function template
12681 to the corresponding template parameters are replaced by the
12682 specified template argument values. If a substitution in a
12683 template parameter or in the function type of the function
12684 template results in an invalid type, type deduction fails. */
12685 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
12686 int i, len = TREE_VEC_LENGTH (tparms);
12687 tree converted_args;
12688 bool incomplete = false;
12690 if (explicit_targs == error_mark_node)
12694 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
12695 /*require_all_args=*/false,
12696 /*use_default_args=*/false));
12697 if (converted_args == error_mark_node)
12700 /* Substitute the explicit args into the function type. This is
12701 necessary so that, for instance, explicitly declared function
12702 arguments can match null pointed constants. If we were given
12703 an incomplete set of explicit args, we must not do semantic
12704 processing during substitution as we could create partial
12706 for (i = 0; i < len; i++)
12708 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
12709 bool parameter_pack = false;
12711 /* Dig out the actual parm. */
12712 if (TREE_CODE (parm) == TYPE_DECL
12713 || TREE_CODE (parm) == TEMPLATE_DECL)
12715 parm = TREE_TYPE (parm);
12716 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
12718 else if (TREE_CODE (parm) == PARM_DECL)
12720 parm = DECL_INITIAL (parm);
12721 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
12724 if (parameter_pack)
12728 template_parm_level_and_index (parm, &level, &idx);
12730 /* Mark the argument pack as "incomplete". We could
12731 still deduce more arguments during unification. */
12732 targ = TMPL_ARG (converted_args, level, idx);
12735 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
12736 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
12737 = ARGUMENT_PACK_ARGS (targ);
12740 /* We have some incomplete argument packs. */
12741 incomplete_argument_packs_p = true;
12745 if (incomplete_argument_packs_p)
12746 /* Any substitution is guaranteed to be incomplete if there
12747 are incomplete argument packs, because we can still deduce
12751 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
12753 processing_template_decl += incomplete;
12754 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
12755 processing_template_decl -= incomplete;
12757 if (fntype == error_mark_node)
12760 /* Place the explicitly specified arguments in TARGS. */
12761 for (i = NUM_TMPL_ARGS (converted_args); i--;)
12762 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
12765 /* Never do unification on the 'this' parameter. */
12766 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
12772 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
12773 new_args = XALLOCAVEC (tree, nargs + 1);
12774 new_args[0] = return_type;
12775 memcpy (new_args + 1, args, nargs * sizeof (tree));
12780 /* We allow incomplete unification without an error message here
12781 because the standard doesn't seem to explicitly prohibit it. Our
12782 callers must be ready to deal with unification failures in any
12784 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
12785 targs, parms, args, nargs, /*subr=*/0,
12788 if (result == 0 && incomplete_argument_packs_p)
12790 int i, len = NUM_TMPL_ARGS (targs);
12792 /* Clear the "incomplete" flags on all argument packs. */
12793 for (i = 0; i < len; i++)
12795 tree arg = TREE_VEC_ELT (targs, i);
12796 if (ARGUMENT_PACK_P (arg))
12798 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
12799 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
12804 /* Now that we have bindings for all of the template arguments,
12805 ensure that the arguments deduced for the template template
12806 parameters have compatible template parameter lists. We cannot
12807 check this property before we have deduced all template
12808 arguments, because the template parameter types of a template
12809 template parameter might depend on prior template parameters
12810 deduced after the template template parameter. The following
12811 ill-formed example illustrates this issue:
12813 template<typename T, template<T> class C> void f(C<5>, T);
12815 template<int N> struct X {};
12818 f(X<5>(), 5l); // error: template argument deduction fails
12821 The template parameter list of 'C' depends on the template type
12822 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
12823 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
12824 time that we deduce 'C'. */
12826 && !template_template_parm_bindings_ok_p
12827 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
12831 /* All is well so far. Now, check:
12835 When all template arguments have been deduced, all uses of
12836 template parameters in nondeduced contexts are replaced with
12837 the corresponding deduced argument values. If the
12838 substitution results in an invalid type, as described above,
12839 type deduction fails. */
12841 tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
12842 if (substed == error_mark_node)
12845 /* If we're looking for an exact match, check that what we got
12846 is indeed an exact match. It might not be if some template
12847 parameters are used in non-deduced contexts. */
12848 if (strict == DEDUCE_EXACT)
12853 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
12855 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
12856 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
12857 if (!same_type_p (args[i], TREE_VALUE (sarg)))
12865 /* Adjust types before performing type deduction, as described in
12866 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
12867 sections are symmetric. PARM is the type of a function parameter
12868 or the return type of the conversion function. ARG is the type of
12869 the argument passed to the call, or the type of the value
12870 initialized with the result of the conversion function.
12871 ARG_EXPR is the original argument expression, which may be null. */
12874 maybe_adjust_types_for_deduction (unification_kind_t strict,
12888 /* Swap PARM and ARG throughout the remainder of this
12889 function; the handling is precisely symmetric since PARM
12890 will initialize ARG rather than vice versa. */
12898 /* There is nothing to do in this case. */
12902 gcc_unreachable ();
12905 if (TREE_CODE (*parm) != REFERENCE_TYPE)
12907 /* [temp.deduct.call]
12909 If P is not a reference type:
12911 --If A is an array type, the pointer type produced by the
12912 array-to-pointer standard conversion (_conv.array_) is
12913 used in place of A for type deduction; otherwise,
12915 --If A is a function type, the pointer type produced by
12916 the function-to-pointer standard conversion
12917 (_conv.func_) is used in place of A for type deduction;
12920 --If A is a cv-qualified type, the top level
12921 cv-qualifiers of A's type are ignored for type
12923 if (TREE_CODE (*arg) == ARRAY_TYPE)
12924 *arg = build_pointer_type (TREE_TYPE (*arg));
12925 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
12926 *arg = build_pointer_type (*arg);
12928 *arg = TYPE_MAIN_VARIANT (*arg);
12931 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
12932 of the form T&&, where T is a template parameter, and the argument
12933 is an lvalue, T is deduced as A& */
12934 if (TREE_CODE (*parm) == REFERENCE_TYPE
12935 && TYPE_REF_IS_RVALUE (*parm)
12936 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
12937 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
12938 && arg_expr && real_lvalue_p (arg_expr))
12939 *arg = build_reference_type (*arg);
12941 /* [temp.deduct.call]
12943 If P is a cv-qualified type, the top level cv-qualifiers
12944 of P's type are ignored for type deduction. If P is a
12945 reference type, the type referred to by P is used for
12947 *parm = TYPE_MAIN_VARIANT (*parm);
12948 if (TREE_CODE (*parm) == REFERENCE_TYPE)
12950 *parm = TREE_TYPE (*parm);
12951 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
12954 /* DR 322. For conversion deduction, remove a reference type on parm
12955 too (which has been swapped into ARG). */
12956 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
12957 *arg = TREE_TYPE (*arg);
12962 /* Most parms like fn_type_unification.
12964 If SUBR is 1, we're being called recursively (to unify the
12965 arguments of a function or method parameter of a function
12969 type_unification_real (tree tparms,
12973 unsigned int xnargs,
12975 unification_kind_t strict,
12978 tree parm, arg, arg_expr;
12980 int ntparms = TREE_VEC_LENGTH (tparms);
12982 int saw_undeduced = 0;
12985 unsigned int nargs;
12988 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
12989 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
12990 gcc_assert (ntparms > 0);
12995 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
12996 | UNIFY_ALLOW_DERIVED);
13000 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13004 sub_strict = UNIFY_ALLOW_NONE;
13008 gcc_unreachable ();
13017 while (parms && parms != void_list_node
13020 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13023 parm = TREE_VALUE (parms);
13024 parms = TREE_CHAIN (parms);
13029 if (arg == error_mark_node)
13031 if (arg == unknown_type_node)
13032 /* We can't deduce anything from this, but we might get all the
13033 template args from other function args. */
13036 /* Conversions will be performed on a function argument that
13037 corresponds with a function parameter that contains only
13038 non-deducible template parameters and explicitly specified
13039 template parameters. */
13040 if (!uses_template_parms (parm))
13045 type = TREE_TYPE (arg);
13049 if (same_type_p (parm, type))
13051 if (strict != DEDUCE_EXACT
13052 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
13061 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
13062 if (type_unknown_p (arg))
13064 /* [temp.deduct.type]
13066 A template-argument can be deduced from a pointer to
13067 function or pointer to member function argument if
13068 the set of overloaded functions does not contain
13069 function templates and at most one of a set of
13070 overloaded functions provides a unique match. */
13071 if (resolve_overloaded_unification
13072 (tparms, targs, parm, arg, strict, sub_strict))
13078 arg = unlowered_expr_type (arg);
13079 if (arg == error_mark_node)
13084 int arg_strict = sub_strict;
13087 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
13090 if (arg == init_list_type_node && arg_expr)
13092 if (unify (tparms, targs, parm, arg, arg_strict))
13099 && parms != void_list_node
13100 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13102 /* Unify the remaining arguments with the pack expansion type. */
13104 tree parmvec = make_tree_vec (1);
13106 /* Allocate a TREE_VEC and copy in all of the arguments */
13107 argvec = make_tree_vec (nargs - ia);
13108 for (i = 0; ia < nargs; ++ia, ++i)
13109 TREE_VEC_ELT (argvec, i) = args[ia];
13111 /* Copy the parameter into parmvec. */
13112 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
13113 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
13114 /*call_args_p=*/true, /*subr=*/subr))
13117 /* Advance to the end of the list of parameters. */
13118 parms = TREE_CHAIN (parms);
13121 /* Fail if we've reached the end of the parm list, and more args
13122 are present, and the parm list isn't variadic. */
13123 if (ia < nargs && parms == void_list_node)
13125 /* Fail if parms are left and they don't have default values. */
13126 if (parms && parms != void_list_node
13127 && TREE_PURPOSE (parms) == NULL_TREE)
13131 for (i = 0; i < ntparms; i++)
13132 if (!TREE_VEC_ELT (targs, i))
13136 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
13139 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13141 /* If this is an undeduced nontype parameter that depends on
13142 a type parameter, try another pass; its type may have been
13143 deduced from a later argument than the one from which
13144 this parameter can be deduced. */
13145 if (TREE_CODE (tparm) == PARM_DECL
13146 && uses_template_parms (TREE_TYPE (tparm))
13147 && !saw_undeduced++)
13150 /* Core issue #226 (C++0x) [temp.deduct]:
13152 If a template argument has not been deduced, its
13153 default template argument, if any, is used.
13155 When we are in C++98 mode, TREE_PURPOSE will either
13156 be NULL_TREE or ERROR_MARK_NODE, so we do not need
13157 to explicitly check cxx_dialect here. */
13158 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
13160 tree arg = tsubst_template_arg
13161 (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)),
13162 targs, tf_none, NULL_TREE);
13163 if (arg == error_mark_node)
13167 TREE_VEC_ELT (targs, i) = arg;
13172 /* If the type parameter is a parameter pack, then it will
13173 be deduced to an empty parameter pack. */
13174 if (template_parameter_pack_p (tparm))
13178 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
13180 arg = make_node (NONTYPE_ARGUMENT_PACK);
13181 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
13182 TREE_CONSTANT (arg) = 1;
13185 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
13187 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
13189 TREE_VEC_ELT (targs, i) = arg;
13199 /* Subroutine of type_unification_real. Args are like the variables
13200 at the call site. ARG is an overloaded function (or template-id);
13201 we try deducing template args from each of the overloads, and if
13202 only one succeeds, we go with that. Modifies TARGS and returns
13203 true on success. */
13206 resolve_overloaded_unification (tree tparms,
13210 unification_kind_t strict,
13213 tree tempargs = copy_node (targs);
13215 tree goodfn = NULL_TREE;
13218 if (TREE_CODE (arg) == ADDR_EXPR)
13220 arg = TREE_OPERAND (arg, 0);
13226 if (TREE_CODE (arg) == COMPONENT_REF)
13227 /* Handle `&x' where `x' is some static or non-static member
13229 arg = TREE_OPERAND (arg, 1);
13231 if (TREE_CODE (arg) == OFFSET_REF)
13232 arg = TREE_OPERAND (arg, 1);
13234 /* Strip baselink information. */
13235 if (BASELINK_P (arg))
13236 arg = BASELINK_FUNCTIONS (arg);
13238 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
13240 /* If we got some explicit template args, we need to plug them into
13241 the affected templates before we try to unify, in case the
13242 explicit args will completely resolve the templates in question. */
13244 tree expl_subargs = TREE_OPERAND (arg, 1);
13245 arg = TREE_OPERAND (arg, 0);
13247 for (; arg; arg = OVL_NEXT (arg))
13249 tree fn = OVL_CURRENT (arg);
13250 tree subargs, elem;
13252 if (TREE_CODE (fn) != TEMPLATE_DECL)
13255 ++processing_template_decl;
13256 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13257 expl_subargs, /*check_ret=*/false);
13260 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
13261 if (try_one_overload (tparms, targs, tempargs, parm,
13262 elem, strict, sub_strict, addr_p)
13263 && (!goodfn || !decls_match (goodfn, elem)))
13269 --processing_template_decl;
13272 else if (TREE_CODE (arg) != OVERLOAD
13273 && TREE_CODE (arg) != FUNCTION_DECL)
13274 /* If ARG is, for example, "(0, &f)" then its type will be unknown
13275 -- but the deduction does not succeed because the expression is
13276 not just the function on its own. */
13279 for (; arg; arg = OVL_NEXT (arg))
13280 if (try_one_overload (tparms, targs, tempargs, parm,
13281 TREE_TYPE (OVL_CURRENT (arg)),
13282 strict, sub_strict, addr_p)
13283 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
13285 goodfn = OVL_CURRENT (arg);
13289 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13290 to function or pointer to member function argument if the set of
13291 overloaded functions does not contain function templates and at most
13292 one of a set of overloaded functions provides a unique match.
13294 So if we found multiple possibilities, we return success but don't
13295 deduce anything. */
13299 int i = TREE_VEC_LENGTH (targs);
13301 if (TREE_VEC_ELT (tempargs, i))
13302 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
13310 /* Subroutine of resolve_overloaded_unification; does deduction for a single
13311 overload. Fills TARGS with any deduced arguments, or error_mark_node if
13312 different overloads deduce different arguments for a given parm.
13313 ADDR_P is true if the expression for which deduction is being
13314 performed was of the form "& fn" rather than simply "fn".
13316 Returns 1 on success. */
13319 try_one_overload (tree tparms,
13324 unification_kind_t strict,
13332 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13333 to function or pointer to member function argument if the set of
13334 overloaded functions does not contain function templates and at most
13335 one of a set of overloaded functions provides a unique match.
13337 So if this is a template, just return success. */
13339 if (uses_template_parms (arg))
13342 if (TREE_CODE (arg) == METHOD_TYPE)
13343 arg = build_ptrmemfunc_type (build_pointer_type (arg));
13345 arg = build_pointer_type (arg);
13347 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
13349 /* We don't copy orig_targs for this because if we have already deduced
13350 some template args from previous args, unify would complain when we
13351 try to deduce a template parameter for the same argument, even though
13352 there isn't really a conflict. */
13353 nargs = TREE_VEC_LENGTH (targs);
13354 tempargs = make_tree_vec (nargs);
13356 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
13359 /* First make sure we didn't deduce anything that conflicts with
13360 explicitly specified args. */
13361 for (i = nargs; i--; )
13363 tree elt = TREE_VEC_ELT (tempargs, i);
13364 tree oldelt = TREE_VEC_ELT (orig_targs, i);
13368 else if (uses_template_parms (elt))
13369 /* Since we're unifying against ourselves, we will fill in
13370 template args used in the function parm list with our own
13371 template parms. Discard them. */
13372 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
13373 else if (oldelt && !template_args_equal (oldelt, elt))
13377 for (i = nargs; i--; )
13379 tree elt = TREE_VEC_ELT (tempargs, i);
13382 TREE_VEC_ELT (targs, i) = elt;
13388 /* PARM is a template class (perhaps with unbound template
13389 parameters). ARG is a fully instantiated type. If ARG can be
13390 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
13391 TARGS are as for unify. */
13394 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
13396 tree copy_of_targs;
13398 if (!CLASSTYPE_TEMPLATE_INFO (arg)
13399 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
13400 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
13403 /* We need to make a new template argument vector for the call to
13404 unify. If we used TARGS, we'd clutter it up with the result of
13405 the attempted unification, even if this class didn't work out.
13406 We also don't want to commit ourselves to all the unifications
13407 we've already done, since unification is supposed to be done on
13408 an argument-by-argument basis. In other words, consider the
13409 following pathological case:
13411 template <int I, int J, int K>
13414 template <int I, int J>
13415 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
13417 template <int I, int J, int K>
13418 void f(S<I, J, K>, S<I, I, I>);
13427 Now, by the time we consider the unification involving `s2', we
13428 already know that we must have `f<0, 0, 0>'. But, even though
13429 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
13430 because there are two ways to unify base classes of S<0, 1, 2>
13431 with S<I, I, I>. If we kept the already deduced knowledge, we
13432 would reject the possibility I=1. */
13433 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
13435 /* If unification failed, we're done. */
13436 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
13437 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
13443 /* Given a template type PARM and a class type ARG, find the unique
13444 base type in ARG that is an instance of PARM. We do not examine
13445 ARG itself; only its base-classes. If there is not exactly one
13446 appropriate base class, return NULL_TREE. PARM may be the type of
13447 a partial specialization, as well as a plain template type. Used
13451 get_template_base (tree tparms, tree targs, tree parm, tree arg)
13453 tree rval = NULL_TREE;
13456 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
13458 binfo = TYPE_BINFO (complete_type (arg));
13460 /* The type could not be completed. */
13463 /* Walk in inheritance graph order. The search order is not
13464 important, and this avoids multiple walks of virtual bases. */
13465 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
13467 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
13471 /* If there is more than one satisfactory baseclass, then:
13475 If they yield more than one possible deduced A, the type
13479 if (rval && !same_type_p (r, rval))
13489 /* Returns the level of DECL, which declares a template parameter. */
13492 template_decl_level (tree decl)
13494 switch (TREE_CODE (decl))
13497 case TEMPLATE_DECL:
13498 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
13501 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
13504 gcc_unreachable ();
13509 /* Decide whether ARG can be unified with PARM, considering only the
13510 cv-qualifiers of each type, given STRICT as documented for unify.
13511 Returns nonzero iff the unification is OK on that basis. */
13514 check_cv_quals_for_unify (int strict, tree arg, tree parm)
13516 int arg_quals = cp_type_quals (arg);
13517 int parm_quals = cp_type_quals (parm);
13519 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13520 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
13522 /* Although a CVR qualifier is ignored when being applied to a
13523 substituted template parameter ([8.3.2]/1 for example), that
13524 does not apply during deduction [14.8.2.4]/1, (even though
13525 that is not explicitly mentioned, [14.8.2.4]/9 indicates
13526 this). Except when we're allowing additional CV qualifiers
13527 at the outer level [14.8.2.1]/3,1st bullet. */
13528 if ((TREE_CODE (arg) == REFERENCE_TYPE
13529 || TREE_CODE (arg) == FUNCTION_TYPE
13530 || TREE_CODE (arg) == METHOD_TYPE)
13531 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
13534 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
13535 && (parm_quals & TYPE_QUAL_RESTRICT))
13539 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
13540 && (arg_quals & parm_quals) != parm_quals)
13543 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
13544 && (parm_quals & arg_quals) != arg_quals)
13550 /* Determines the LEVEL and INDEX for the template parameter PARM. */
13552 template_parm_level_and_index (tree parm, int* level, int* index)
13554 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13555 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13556 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13558 *index = TEMPLATE_TYPE_IDX (parm);
13559 *level = TEMPLATE_TYPE_LEVEL (parm);
13563 *index = TEMPLATE_PARM_IDX (parm);
13564 *level = TEMPLATE_PARM_LEVEL (parm);
13568 /* Unifies the remaining arguments in PACKED_ARGS with the pack
13569 expansion at the end of PACKED_PARMS. Returns 0 if the type
13570 deduction succeeds, 1 otherwise. STRICT is the same as in
13571 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
13572 call argument list. We'll need to adjust the arguments to make them
13573 types. SUBR tells us if this is from a recursive call to
13574 type_unification_real. */
13576 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
13577 tree packed_args, int strict, bool call_args_p,
13581 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
13582 tree pattern = PACK_EXPANSION_PATTERN (parm);
13583 tree pack, packs = NULL_TREE;
13584 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
13585 int len = TREE_VEC_LENGTH (packed_args);
13587 /* Determine the parameter packs we will be deducing from the
13588 pattern, and record their current deductions. */
13589 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
13590 pack; pack = TREE_CHAIN (pack))
13592 tree parm_pack = TREE_VALUE (pack);
13595 /* Determine the index and level of this parameter pack. */
13596 template_parm_level_and_index (parm_pack, &level, &idx);
13598 /* Keep track of the parameter packs and their corresponding
13600 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
13601 TREE_TYPE (packs) = make_tree_vec (len - start);
13604 /* Loop through all of the arguments that have not yet been
13605 unified and unify each with the pattern. */
13606 for (i = start; i < len; i++)
13608 tree parm = pattern;
13610 /* For each parameter pack, clear out the deduced value so that
13611 we can deduce it again. */
13612 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13615 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13617 TMPL_ARG (targs, level, idx) = NULL_TREE;
13620 /* Unify the pattern with the current argument. */
13622 tree arg = TREE_VEC_ELT (packed_args, i);
13623 tree arg_expr = NULL_TREE;
13624 int arg_strict = strict;
13625 bool skip_arg_p = false;
13631 /* This mirrors what we do in type_unification_real. */
13635 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
13636 | UNIFY_ALLOW_MORE_CV_QUAL
13637 | UNIFY_ALLOW_DERIVED);
13641 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13645 sub_strict = UNIFY_ALLOW_NONE;
13649 gcc_unreachable ();
13654 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
13655 if (type_unknown_p (arg))
13657 /* [temp.deduct.type] A template-argument can be
13658 deduced from a pointer to function or pointer
13659 to member function argument if the set of
13660 overloaded functions does not contain function
13661 templates and at most one of a set of
13662 overloaded functions provides a unique
13665 if (resolve_overloaded_unification
13666 (tparms, targs, parm, arg,
13667 (unification_kind_t) strict,
13677 arg = unlowered_expr_type (arg);
13678 if (arg == error_mark_node)
13683 arg_strict = sub_strict;
13687 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
13688 &parm, &arg, arg_expr);
13693 if (unify (tparms, targs, parm, arg, arg_strict))
13698 /* For each parameter pack, collect the deduced value. */
13699 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13702 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13704 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
13705 TMPL_ARG (targs, level, idx);
13709 /* Verify that the results of unification with the parameter packs
13710 produce results consistent with what we've seen before, and make
13711 the deduced argument packs available. */
13712 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13714 tree old_pack = TREE_VALUE (pack);
13715 tree new_args = TREE_TYPE (pack);
13716 int i, len = TREE_VEC_LENGTH (new_args);
13717 bool nondeduced_p = false;
13719 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
13720 actually deduce anything. */
13721 for (i = 0; i < len && !nondeduced_p; ++i)
13722 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
13723 nondeduced_p = true;
13727 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
13729 /* Prepend the explicit arguments onto NEW_ARGS. */
13730 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
13731 tree old_args = new_args;
13732 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
13733 int len = explicit_len + TREE_VEC_LENGTH (old_args);
13735 /* Copy the explicit arguments. */
13736 new_args = make_tree_vec (len);
13737 for (i = 0; i < explicit_len; i++)
13738 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
13740 /* Copy the deduced arguments. */
13741 for (; i < len; i++)
13742 TREE_VEC_ELT (new_args, i) =
13743 TREE_VEC_ELT (old_args, i - explicit_len);
13751 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13753 /* Build the deduced *_ARGUMENT_PACK. */
13754 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
13756 result = make_node (NONTYPE_ARGUMENT_PACK);
13757 TREE_TYPE (result) =
13758 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
13759 TREE_CONSTANT (result) = 1;
13762 result = cxx_make_type (TYPE_ARGUMENT_PACK);
13764 SET_ARGUMENT_PACK_ARGS (result, new_args);
13766 /* Note the deduced argument packs for this parameter
13768 TMPL_ARG (targs, level, idx) = result;
13770 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
13771 && (ARGUMENT_PACK_ARGS (old_pack)
13772 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
13774 /* We only had the explicitly-provided arguments before, but
13775 now we have a complete set of arguments. */
13777 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
13778 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13780 /* Keep the original deduced argument pack. */
13781 TMPL_ARG (targs, level, idx) = old_pack;
13783 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
13784 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
13785 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
13787 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
13789 /* Inconsistent unification of this parameter pack. */
13795 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13797 /* Keep the original deduced argument pack. */
13798 TMPL_ARG (targs, level, idx) = old_pack;
13805 /* Deduce the value of template parameters. TPARMS is the (innermost)
13806 set of template parameters to a template. TARGS is the bindings
13807 for those template parameters, as determined thus far; TARGS may
13808 include template arguments for outer levels of template parameters
13809 as well. PARM is a parameter to a template function, or a
13810 subcomponent of that parameter; ARG is the corresponding argument.
13811 This function attempts to match PARM with ARG in a manner
13812 consistent with the existing assignments in TARGS. If more values
13813 are deduced, then TARGS is updated.
13815 Returns 0 if the type deduction succeeds, 1 otherwise. The
13816 parameter STRICT is a bitwise or of the following flags:
13819 Require an exact match between PARM and ARG.
13820 UNIFY_ALLOW_MORE_CV_QUAL:
13821 Allow the deduced ARG to be more cv-qualified (by qualification
13822 conversion) than ARG.
13823 UNIFY_ALLOW_LESS_CV_QUAL:
13824 Allow the deduced ARG to be less cv-qualified than ARG.
13825 UNIFY_ALLOW_DERIVED:
13826 Allow the deduced ARG to be a template base class of ARG,
13827 or a pointer to a template base class of the type pointed to by
13829 UNIFY_ALLOW_INTEGER:
13830 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
13831 case for more information.
13832 UNIFY_ALLOW_OUTER_LEVEL:
13833 This is the outermost level of a deduction. Used to determine validity
13834 of qualification conversions. A valid qualification conversion must
13835 have const qualified pointers leading up to the inner type which
13836 requires additional CV quals, except at the outer level, where const
13837 is not required [conv.qual]. It would be normal to set this flag in
13838 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
13839 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
13840 This is the outermost level of a deduction, and PARM can be more CV
13841 qualified at this point.
13842 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
13843 This is the outermost level of a deduction, and PARM can be less CV
13844 qualified at this point. */
13847 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
13852 int strict_in = strict;
13854 /* I don't think this will do the right thing with respect to types.
13855 But the only case I've seen it in so far has been array bounds, where
13856 signedness is the only information lost, and I think that will be
13858 while (TREE_CODE (parm) == NOP_EXPR)
13859 parm = TREE_OPERAND (parm, 0);
13861 if (arg == error_mark_node)
13863 if (arg == unknown_type_node
13864 || arg == init_list_type_node)
13865 /* We can't deduce anything from this, but we might get all the
13866 template args from other function args. */
13869 /* If PARM uses template parameters, then we can't bail out here,
13870 even if ARG == PARM, since we won't record unifications for the
13871 template parameters. We might need them if we're trying to
13872 figure out which of two things is more specialized. */
13873 if (arg == parm && !uses_template_parms (parm))
13876 /* Handle init lists early, so the rest of the function can assume
13877 we're dealing with a type. */
13878 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
13882 tree orig_parm = parm;
13884 /* Replace T with std::initializer_list<T> for deduction. */
13885 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13886 && flag_deduce_init_list)
13887 parm = listify (parm);
13889 if (!is_std_init_list (parm))
13890 /* We can only deduce from an initializer list argument if the
13891 parameter is std::initializer_list; otherwise this is a
13892 non-deduced context. */
13895 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
13897 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
13899 int elt_strict = strict;
13900 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
13902 tree type = TREE_TYPE (elt);
13903 /* It should only be possible to get here for a call. */
13904 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
13905 elt_strict |= maybe_adjust_types_for_deduction
13906 (DEDUCE_CALL, &elttype, &type, elt);
13910 if (unify (tparms, targs, elttype, elt, elt_strict))
13914 /* If the std::initializer_list<T> deduction worked, replace the
13915 deduced A with std::initializer_list<A>. */
13916 if (orig_parm != parm)
13918 idx = TEMPLATE_TYPE_IDX (orig_parm);
13919 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
13920 targ = listify (targ);
13921 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
13926 /* Immediately reject some pairs that won't unify because of
13927 cv-qualification mismatches. */
13928 if (TREE_CODE (arg) == TREE_CODE (parm)
13930 /* It is the elements of the array which hold the cv quals of an array
13931 type, and the elements might be template type parms. We'll check
13932 when we recurse. */
13933 && TREE_CODE (arg) != ARRAY_TYPE
13934 /* We check the cv-qualifiers when unifying with template type
13935 parameters below. We want to allow ARG `const T' to unify with
13936 PARM `T' for example, when computing which of two templates
13937 is more specialized, for example. */
13938 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
13939 && !check_cv_quals_for_unify (strict_in, arg, parm))
13942 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
13943 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
13944 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
13945 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
13946 strict &= ~UNIFY_ALLOW_DERIVED;
13947 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
13948 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
13950 switch (TREE_CODE (parm))
13952 case TYPENAME_TYPE:
13954 case UNBOUND_CLASS_TEMPLATE:
13955 /* In a type which contains a nested-name-specifier, template
13956 argument values cannot be deduced for template parameters used
13957 within the nested-name-specifier. */
13960 case TEMPLATE_TYPE_PARM:
13961 case TEMPLATE_TEMPLATE_PARM:
13962 case BOUND_TEMPLATE_TEMPLATE_PARM:
13963 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
13964 if (tparm == error_mark_node)
13967 if (TEMPLATE_TYPE_LEVEL (parm)
13968 != template_decl_level (tparm))
13969 /* The PARM is not one we're trying to unify. Just check
13970 to see if it matches ARG. */
13971 return (TREE_CODE (arg) == TREE_CODE (parm)
13972 && same_type_p (parm, arg)) ? 0 : 1;
13973 idx = TEMPLATE_TYPE_IDX (parm);
13974 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
13975 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
13977 /* Check for mixed types and values. */
13978 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13979 && TREE_CODE (tparm) != TYPE_DECL)
13980 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13981 && TREE_CODE (tparm) != TEMPLATE_DECL))
13984 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13986 /* ARG must be constructed from a template class or a template
13987 template parameter. */
13988 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
13989 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
13993 tree parmvec = TYPE_TI_ARGS (parm);
13994 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
13996 = DECL_INNERMOST_TEMPLATE_PARMS
13997 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
13999 int parm_variadic_p = 0;
14001 /* The resolution to DR150 makes clear that default
14002 arguments for an N-argument may not be used to bind T
14003 to a template template parameter with fewer than N
14004 parameters. It is not safe to permit the binding of
14005 default arguments as an extension, as that may change
14006 the meaning of a conforming program. Consider:
14008 struct Dense { static const unsigned int dim = 1; };
14010 template <template <typename> class View,
14012 void operator+(float, View<Block> const&);
14014 template <typename Block,
14015 unsigned int Dim = Block::dim>
14016 struct Lvalue_proxy { operator float() const; };
14020 Lvalue_proxy<Dense> p;
14025 Here, if Lvalue_proxy is permitted to bind to View, then
14026 the global operator+ will be used; if they are not, the
14027 Lvalue_proxy will be converted to float. */
14028 if (coerce_template_parms (parm_parms,
14030 TYPE_TI_TEMPLATE (parm),
14032 /*require_all_args=*/true,
14033 /*use_default_args=*/false)
14034 == error_mark_node)
14037 /* Deduce arguments T, i from TT<T> or TT<i>.
14038 We check each element of PARMVEC and ARGVEC individually
14039 rather than the whole TREE_VEC since they can have
14040 different number of elements. */
14042 parmvec = expand_template_argument_pack (parmvec);
14043 argvec = expand_template_argument_pack (argvec);
14045 len = TREE_VEC_LENGTH (parmvec);
14047 /* Check if the parameters end in a pack, making them
14050 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
14051 parm_variadic_p = 1;
14053 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
14056 for (i = 0; i < len - parm_variadic_p; ++i)
14058 if (unify (tparms, targs,
14059 TREE_VEC_ELT (parmvec, i),
14060 TREE_VEC_ELT (argvec, i),
14065 if (parm_variadic_p
14066 && unify_pack_expansion (tparms, targs,
14069 /*call_args_p=*/false,
14073 arg = TYPE_TI_TEMPLATE (arg);
14075 /* Fall through to deduce template name. */
14078 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14079 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14081 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
14083 /* Simple cases: Value already set, does match or doesn't. */
14084 if (targ != NULL_TREE && template_args_equal (targ, arg))
14091 /* If PARM is `const T' and ARG is only `int', we don't have
14092 a match unless we are allowing additional qualification.
14093 If ARG is `const int' and PARM is just `T' that's OK;
14094 that binds `const int' to `T'. */
14095 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
14099 /* Consider the case where ARG is `const volatile int' and
14100 PARM is `const T'. Then, T should be `volatile int'. */
14101 arg = cp_build_qualified_type_real
14102 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
14103 if (arg == error_mark_node)
14106 /* Simple cases: Value already set, does match or doesn't. */
14107 if (targ != NULL_TREE && same_type_p (targ, arg))
14112 /* Make sure that ARG is not a variable-sized array. (Note
14113 that were talking about variable-sized arrays (like
14114 `int[n]'), rather than arrays of unknown size (like
14115 `int[]').) We'll get very confused by such a type since
14116 the bound of the array will not be computable in an
14117 instantiation. Besides, such types are not allowed in
14118 ISO C++, so we can do as we please here. */
14119 if (variably_modified_type_p (arg, NULL_TREE))
14122 /* Strip typedefs as in convert_template_argument. */
14123 arg = strip_typedefs (arg);
14126 /* If ARG is a parameter pack or an expansion, we cannot unify
14127 against it unless PARM is also a parameter pack. */
14128 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14129 && !template_parameter_pack_p (parm))
14132 /* If the argument deduction results is a METHOD_TYPE,
14133 then there is a problem.
14134 METHOD_TYPE doesn't map to any real C++ type the result of
14135 the deduction can not be of that type. */
14136 if (TREE_CODE (arg) == METHOD_TYPE)
14139 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14142 case TEMPLATE_PARM_INDEX:
14143 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14144 if (tparm == error_mark_node)
14147 if (TEMPLATE_PARM_LEVEL (parm)
14148 != template_decl_level (tparm))
14149 /* The PARM is not one we're trying to unify. Just check
14150 to see if it matches ARG. */
14151 return !(TREE_CODE (arg) == TREE_CODE (parm)
14152 && cp_tree_equal (parm, arg));
14154 idx = TEMPLATE_PARM_IDX (parm);
14155 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14158 return !cp_tree_equal (targ, arg);
14160 /* [temp.deduct.type] If, in the declaration of a function template
14161 with a non-type template-parameter, the non-type
14162 template-parameter is used in an expression in the function
14163 parameter-list and, if the corresponding template-argument is
14164 deduced, the template-argument type shall match the type of the
14165 template-parameter exactly, except that a template-argument
14166 deduced from an array bound may be of any integral type.
14167 The non-type parameter might use already deduced type parameters. */
14168 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
14169 if (!TREE_TYPE (arg))
14170 /* Template-parameter dependent expression. Just accept it for now.
14171 It will later be processed in convert_template_argument. */
14173 else if (same_type_p (TREE_TYPE (arg), tparm))
14175 else if ((strict & UNIFY_ALLOW_INTEGER)
14176 && (TREE_CODE (tparm) == INTEGER_TYPE
14177 || TREE_CODE (tparm) == BOOLEAN_TYPE))
14178 /* Convert the ARG to the type of PARM; the deduced non-type
14179 template argument must exactly match the types of the
14180 corresponding parameter. */
14181 arg = fold (build_nop (tparm, arg));
14182 else if (uses_template_parms (tparm))
14183 /* We haven't deduced the type of this parameter yet. Try again
14189 /* If ARG is a parameter pack or an expansion, we cannot unify
14190 against it unless PARM is also a parameter pack. */
14191 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14192 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
14195 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14200 /* A pointer-to-member constant can be unified only with
14201 another constant. */
14202 if (TREE_CODE (arg) != PTRMEM_CST)
14205 /* Just unify the class member. It would be useless (and possibly
14206 wrong, depending on the strict flags) to unify also
14207 PTRMEM_CST_CLASS, because we want to be sure that both parm and
14208 arg refer to the same variable, even if through different
14209 classes. For instance:
14211 struct A { int x; };
14214 Unification of &A::x and &B::x must succeed. */
14215 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
14216 PTRMEM_CST_MEMBER (arg), strict);
14221 if (TREE_CODE (arg) != POINTER_TYPE)
14224 /* [temp.deduct.call]
14226 A can be another pointer or pointer to member type that can
14227 be converted to the deduced A via a qualification
14228 conversion (_conv.qual_).
14230 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
14231 This will allow for additional cv-qualification of the
14232 pointed-to types if appropriate. */
14234 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
14235 /* The derived-to-base conversion only persists through one
14236 level of pointers. */
14237 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
14239 return unify (tparms, targs, TREE_TYPE (parm),
14240 TREE_TYPE (arg), strict);
14243 case REFERENCE_TYPE:
14244 if (TREE_CODE (arg) != REFERENCE_TYPE)
14246 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14247 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14250 if (TREE_CODE (arg) != ARRAY_TYPE)
14252 if ((TYPE_DOMAIN (parm) == NULL_TREE)
14253 != (TYPE_DOMAIN (arg) == NULL_TREE))
14255 if (TYPE_DOMAIN (parm) != NULL_TREE)
14262 /* Our representation of array types uses "N - 1" as the
14263 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
14264 not an integer constant. We cannot unify arbitrarily
14265 complex expressions, so we eliminate the MINUS_EXPRs
14267 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
14268 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
14271 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
14272 parm_max = TREE_OPERAND (parm_max, 0);
14274 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
14275 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
14278 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
14279 trying to unify the type of a variable with the type
14280 of a template parameter. For example:
14282 template <unsigned int N>
14283 void f (char (&) [N]);
14290 Here, the type of the ARG will be "int [g(i)]", and
14291 may be a SAVE_EXPR, etc. */
14292 if (TREE_CODE (arg_max) != MINUS_EXPR)
14294 arg_max = TREE_OPERAND (arg_max, 0);
14297 /* If only one of the bounds used a MINUS_EXPR, compensate
14298 by adding one to the other bound. */
14299 if (parm_cst && !arg_cst)
14300 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
14304 else if (arg_cst && !parm_cst)
14305 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
14310 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
14313 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14314 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14321 case ENUMERAL_TYPE:
14323 if (TREE_CODE (arg) != TREE_CODE (parm))
14326 /* We have already checked cv-qualification at the top of the
14328 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
14331 /* As far as unification is concerned, this wins. Later checks
14332 will invalidate it if necessary. */
14335 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
14336 /* Type INTEGER_CST can come from ordinary constant template args. */
14338 while (TREE_CODE (arg) == NOP_EXPR)
14339 arg = TREE_OPERAND (arg, 0);
14341 if (TREE_CODE (arg) != INTEGER_CST)
14343 return !tree_int_cst_equal (parm, arg);
14348 if (TREE_CODE (arg) != TREE_VEC)
14350 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
14352 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
14353 if (unify (tparms, targs,
14354 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
14362 if (TREE_CODE (arg) != TREE_CODE (parm))
14365 if (TYPE_PTRMEMFUNC_P (parm))
14367 if (!TYPE_PTRMEMFUNC_P (arg))
14370 return unify (tparms, targs,
14371 TYPE_PTRMEMFUNC_FN_TYPE (parm),
14372 TYPE_PTRMEMFUNC_FN_TYPE (arg),
14376 if (CLASSTYPE_TEMPLATE_INFO (parm))
14378 tree t = NULL_TREE;
14380 if (strict_in & UNIFY_ALLOW_DERIVED)
14382 /* First, we try to unify the PARM and ARG directly. */
14383 t = try_class_unification (tparms, targs,
14388 /* Fallback to the special case allowed in
14389 [temp.deduct.call]:
14391 If P is a class, and P has the form
14392 template-id, then A can be a derived class of
14393 the deduced A. Likewise, if P is a pointer to
14394 a class of the form template-id, A can be a
14395 pointer to a derived class pointed to by the
14397 t = get_template_base (tparms, targs, parm, arg);
14403 else if (CLASSTYPE_TEMPLATE_INFO (arg)
14404 && (CLASSTYPE_TI_TEMPLATE (parm)
14405 == CLASSTYPE_TI_TEMPLATE (arg)))
14406 /* Perhaps PARM is something like S<U> and ARG is S<int>.
14407 Then, we should unify `int' and `U'. */
14410 /* There's no chance of unification succeeding. */
14413 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
14414 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
14416 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
14421 case FUNCTION_TYPE:
14423 unsigned int nargs;
14428 if (TREE_CODE (arg) != TREE_CODE (parm))
14431 /* CV qualifications for methods can never be deduced, they must
14432 match exactly. We need to check them explicitly here,
14433 because type_unification_real treats them as any other
14434 cv-qualified parameter. */
14435 if (TREE_CODE (parm) == METHOD_TYPE
14436 && (!check_cv_quals_for_unify
14438 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
14439 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
14442 if (unify (tparms, targs, TREE_TYPE (parm),
14443 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
14446 nargs = list_length (TYPE_ARG_TYPES (arg));
14447 args = XALLOCAVEC (tree, nargs);
14448 for (a = TYPE_ARG_TYPES (arg), i = 0;
14449 a != NULL_TREE && a != void_list_node;
14450 a = TREE_CHAIN (a), ++i)
14451 args[i] = TREE_VALUE (a);
14454 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
14455 args, nargs, 1, DEDUCE_EXACT,
14460 /* Unify a pointer to member with a pointer to member function, which
14461 deduces the type of the member as a function type. */
14462 if (TYPE_PTRMEMFUNC_P (arg))
14466 cp_cv_quals cv_quals;
14468 /* Check top-level cv qualifiers */
14469 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
14472 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
14473 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
14476 /* Determine the type of the function we are unifying against. */
14477 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
14479 build_function_type (TREE_TYPE (method_type),
14480 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
14482 /* Extract the cv-qualifiers of the member function from the
14483 implicit object parameter and place them on the function
14484 type to be restored later. */
14486 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
14487 fntype = build_qualified_type (fntype, cv_quals);
14488 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
14491 if (TREE_CODE (arg) != OFFSET_TYPE)
14493 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
14494 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
14496 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14500 if (DECL_TEMPLATE_PARM_P (parm))
14501 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
14502 if (arg != integral_constant_value (parm))
14507 case TEMPLATE_DECL:
14508 /* Matched cases are handled by the ARG == PARM test above. */
14511 case TYPE_ARGUMENT_PACK:
14512 case NONTYPE_ARGUMENT_PACK:
14514 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
14515 tree packed_args = ARGUMENT_PACK_ARGS (arg);
14516 int i, len = TREE_VEC_LENGTH (packed_parms);
14517 int argslen = TREE_VEC_LENGTH (packed_args);
14518 int parm_variadic_p = 0;
14520 for (i = 0; i < len; ++i)
14522 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
14525 /* We can unify against something with a trailing
14527 parm_variadic_p = 1;
14529 /* Since there is something following the pack
14530 expansion, we cannot unify this template argument
14537 /* If we don't have enough arguments to satisfy the parameters
14538 (not counting the pack expression at the end), or we have
14539 too many arguments for a parameter list that doesn't end in
14540 a pack expression, we can't unify. */
14541 if (argslen < (len - parm_variadic_p)
14542 || (argslen > len && !parm_variadic_p))
14545 /* Unify all of the parameters that precede the (optional)
14546 pack expression. */
14547 for (i = 0; i < len - parm_variadic_p; ++i)
14549 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
14550 TREE_VEC_ELT (packed_args, i), strict))
14554 if (parm_variadic_p)
14555 return unify_pack_expansion (tparms, targs,
14556 packed_parms, packed_args,
14557 strict, /*call_args_p=*/false,
14565 case DECLTYPE_TYPE:
14566 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
14571 /* Unification fails if we hit an error node. */
14575 gcc_assert (EXPR_P (parm));
14577 /* We must be looking at an expression. This can happen with
14581 void foo(S<I>, S<I + 2>);
14583 This is a "nondeduced context":
14587 The nondeduced contexts are:
14589 --A type that is a template-id in which one or more of
14590 the template-arguments is an expression that references
14591 a template-parameter.
14593 In these cases, we assume deduction succeeded, but don't
14594 actually infer any unifications. */
14596 if (!uses_template_parms (parm)
14597 && !template_args_equal (parm, arg))
14604 /* Note that DECL can be defined in this translation unit, if
14608 mark_definable (tree decl)
14611 DECL_NOT_REALLY_EXTERN (decl) = 1;
14612 FOR_EACH_CLONE (clone, decl)
14613 DECL_NOT_REALLY_EXTERN (clone) = 1;
14616 /* Called if RESULT is explicitly instantiated, or is a member of an
14617 explicitly instantiated class. */
14620 mark_decl_instantiated (tree result, int extern_p)
14622 SET_DECL_EXPLICIT_INSTANTIATION (result);
14624 /* If this entity has already been written out, it's too late to
14625 make any modifications. */
14626 if (TREE_ASM_WRITTEN (result))
14629 if (TREE_CODE (result) != FUNCTION_DECL)
14630 /* The TREE_PUBLIC flag for function declarations will have been
14631 set correctly by tsubst. */
14632 TREE_PUBLIC (result) = 1;
14634 /* This might have been set by an earlier implicit instantiation. */
14635 DECL_COMDAT (result) = 0;
14638 DECL_NOT_REALLY_EXTERN (result) = 0;
14641 mark_definable (result);
14642 /* Always make artificials weak. */
14643 if (DECL_ARTIFICIAL (result) && flag_weak)
14644 comdat_linkage (result);
14645 /* For WIN32 we also want to put explicit instantiations in
14646 linkonce sections. */
14647 else if (TREE_PUBLIC (result))
14648 maybe_make_one_only (result);
14651 /* If EXTERN_P, then this function will not be emitted -- unless
14652 followed by an explicit instantiation, at which point its linkage
14653 will be adjusted. If !EXTERN_P, then this function will be
14654 emitted here. In neither circumstance do we want
14655 import_export_decl to adjust the linkage. */
14656 DECL_INTERFACE_KNOWN (result) = 1;
14659 /* Given two function templates PAT1 and PAT2, return:
14661 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
14662 -1 if PAT2 is more specialized than PAT1.
14663 0 if neither is more specialized.
14665 LEN indicates the number of parameters we should consider
14666 (defaulted parameters should not be considered).
14668 The 1998 std underspecified function template partial ordering, and
14669 DR214 addresses the issue. We take pairs of arguments, one from
14670 each of the templates, and deduce them against each other. One of
14671 the templates will be more specialized if all the *other*
14672 template's arguments deduce against its arguments and at least one
14673 of its arguments *does* *not* deduce against the other template's
14674 corresponding argument. Deduction is done as for class templates.
14675 The arguments used in deduction have reference and top level cv
14676 qualifiers removed. Iff both arguments were originally reference
14677 types *and* deduction succeeds in both directions, the template
14678 with the more cv-qualified argument wins for that pairing (if
14679 neither is more cv-qualified, they both are equal). Unlike regular
14680 deduction, after all the arguments have been deduced in this way,
14681 we do *not* verify the deduced template argument values can be
14682 substituted into non-deduced contexts, nor do we have to verify
14683 that all template arguments have been deduced. */
14686 more_specialized_fn (tree pat1, tree pat2, int len)
14688 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
14689 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
14690 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
14691 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
14692 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
14693 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
14694 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
14695 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
14699 /* Remove the this parameter from non-static member functions. If
14700 one is a non-static member function and the other is not a static
14701 member function, remove the first parameter from that function
14702 also. This situation occurs for operator functions where we
14703 locate both a member function (with this pointer) and non-member
14704 operator (with explicit first operand). */
14705 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
14707 len--; /* LEN is the number of significant arguments for DECL1 */
14708 args1 = TREE_CHAIN (args1);
14709 if (!DECL_STATIC_FUNCTION_P (decl2))
14710 args2 = TREE_CHAIN (args2);
14712 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
14714 args2 = TREE_CHAIN (args2);
14715 if (!DECL_STATIC_FUNCTION_P (decl1))
14718 args1 = TREE_CHAIN (args1);
14722 /* If only one is a conversion operator, they are unordered. */
14723 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
14726 /* Consider the return type for a conversion function */
14727 if (DECL_CONV_FN_P (decl1))
14729 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
14730 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
14734 processing_template_decl++;
14737 /* Stop when an ellipsis is seen. */
14738 && args1 != NULL_TREE && args2 != NULL_TREE)
14740 tree arg1 = TREE_VALUE (args1);
14741 tree arg2 = TREE_VALUE (args2);
14742 int deduce1, deduce2;
14746 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
14747 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14749 /* When both arguments are pack expansions, we need only
14750 unify the patterns themselves. */
14751 arg1 = PACK_EXPANSION_PATTERN (arg1);
14752 arg2 = PACK_EXPANSION_PATTERN (arg2);
14754 /* This is the last comparison we need to do. */
14758 if (TREE_CODE (arg1) == REFERENCE_TYPE)
14760 arg1 = TREE_TYPE (arg1);
14761 quals1 = cp_type_quals (arg1);
14764 if (TREE_CODE (arg2) == REFERENCE_TYPE)
14766 arg2 = TREE_TYPE (arg2);
14767 quals2 = cp_type_quals (arg2);
14770 if ((quals1 < 0) != (quals2 < 0))
14772 /* Only of the args is a reference, see if we should apply
14773 array/function pointer decay to it. This is not part of
14774 DR214, but is, IMHO, consistent with the deduction rules
14775 for the function call itself, and with our earlier
14776 implementation of the underspecified partial ordering
14777 rules. (nathan). */
14780 switch (TREE_CODE (arg1))
14783 arg1 = TREE_TYPE (arg1);
14785 case FUNCTION_TYPE:
14786 arg1 = build_pointer_type (arg1);
14795 switch (TREE_CODE (arg2))
14798 arg2 = TREE_TYPE (arg2);
14800 case FUNCTION_TYPE:
14801 arg2 = build_pointer_type (arg2);
14810 arg1 = TYPE_MAIN_VARIANT (arg1);
14811 arg2 = TYPE_MAIN_VARIANT (arg2);
14813 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
14815 int i, len2 = list_length (args2);
14816 tree parmvec = make_tree_vec (1);
14817 tree argvec = make_tree_vec (len2);
14820 /* Setup the parameter vector, which contains only ARG1. */
14821 TREE_VEC_ELT (parmvec, 0) = arg1;
14823 /* Setup the argument vector, which contains the remaining
14825 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
14826 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
14828 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
14829 argvec, UNIFY_ALLOW_NONE,
14830 /*call_args_p=*/false,
14833 /* We cannot deduce in the other direction, because ARG1 is
14834 a pack expansion but ARG2 is not. */
14837 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14839 int i, len1 = list_length (args1);
14840 tree parmvec = make_tree_vec (1);
14841 tree argvec = make_tree_vec (len1);
14844 /* Setup the parameter vector, which contains only ARG1. */
14845 TREE_VEC_ELT (parmvec, 0) = arg2;
14847 /* Setup the argument vector, which contains the remaining
14849 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
14850 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
14852 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
14853 argvec, UNIFY_ALLOW_NONE,
14854 /*call_args_p=*/false,
14857 /* We cannot deduce in the other direction, because ARG2 is
14858 a pack expansion but ARG1 is not.*/
14864 /* The normal case, where neither argument is a pack
14866 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
14867 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
14874 if (better1 < 0 && better2 < 0)
14875 /* We've failed to deduce something in either direction.
14876 These must be unordered. */
14879 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
14881 /* Deduces in both directions, see if quals can
14882 disambiguate. Pretend the worse one failed to deduce. */
14883 if ((quals1 & quals2) == quals2)
14885 if ((quals1 & quals2) == quals1)
14888 if (deduce1 && !deduce2 && !better2)
14890 if (deduce2 && !deduce1 && !better1)
14893 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
14894 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14895 /* We have already processed all of the arguments in our
14896 handing of the pack expansion type. */
14899 args1 = TREE_CHAIN (args1);
14900 args2 = TREE_CHAIN (args2);
14903 processing_template_decl--;
14905 /* All things being equal, if the next argument is a pack expansion
14906 for one function but not for the other, prefer the
14907 non-variadic function. */
14908 if ((better1 > 0) - (better2 > 0) == 0
14909 && args1 && TREE_VALUE (args1)
14910 && args2 && TREE_VALUE (args2))
14912 if (TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION)
14913 return TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION ? 0 : -1;
14914 else if (TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION)
14918 return (better1 > 0) - (better2 > 0);
14921 /* Determine which of two partial specializations is more specialized.
14923 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
14924 to the first partial specialization. The TREE_VALUE is the
14925 innermost set of template parameters for the partial
14926 specialization. PAT2 is similar, but for the second template.
14928 Return 1 if the first partial specialization is more specialized;
14929 -1 if the second is more specialized; 0 if neither is more
14932 See [temp.class.order] for information about determining which of
14933 two templates is more specialized. */
14936 more_specialized_class (tree pat1, tree pat2)
14941 bool any_deductions = false;
14943 tmpl1 = TREE_TYPE (pat1);
14944 tmpl2 = TREE_TYPE (pat2);
14946 /* Just like what happens for functions, if we are ordering between
14947 different class template specializations, we may encounter dependent
14948 types in the arguments, and we need our dependency check functions
14949 to behave correctly. */
14950 ++processing_template_decl;
14951 targs = get_class_bindings (TREE_VALUE (pat1),
14952 CLASSTYPE_TI_ARGS (tmpl1),
14953 CLASSTYPE_TI_ARGS (tmpl2));
14957 any_deductions = true;
14960 targs = get_class_bindings (TREE_VALUE (pat2),
14961 CLASSTYPE_TI_ARGS (tmpl2),
14962 CLASSTYPE_TI_ARGS (tmpl1));
14966 any_deductions = true;
14968 --processing_template_decl;
14970 /* In the case of a tie where at least one of the class templates
14971 has a parameter pack at the end, the template with the most
14972 non-packed parameters wins. */
14975 && (template_args_variadic_p (TREE_PURPOSE (pat1))
14976 || template_args_variadic_p (TREE_PURPOSE (pat2))))
14978 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
14979 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
14980 int len1 = TREE_VEC_LENGTH (args1);
14981 int len2 = TREE_VEC_LENGTH (args2);
14983 /* We don't count the pack expansion at the end. */
14984 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
14986 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
14991 else if (len1 < len2)
14998 /* Return the template arguments that will produce the function signature
14999 DECL from the function template FN, with the explicit template
15000 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
15001 also match. Return NULL_TREE if no satisfactory arguments could be
15005 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
15007 int ntparms = DECL_NTPARMS (fn);
15008 tree targs = make_tree_vec (ntparms);
15010 tree decl_arg_types;
15012 unsigned int nargs, ix;
15015 /* Substitute the explicit template arguments into the type of DECL.
15016 The call to fn_type_unification will handle substitution into the
15018 decl_type = TREE_TYPE (decl);
15019 if (explicit_args && uses_template_parms (decl_type))
15022 tree converted_args;
15024 if (DECL_TEMPLATE_INFO (decl))
15025 tmpl = DECL_TI_TEMPLATE (decl);
15027 /* We can get here for some invalid specializations. */
15031 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
15032 explicit_args, NULL_TREE,
15034 /*require_all_args=*/false,
15035 /*use_default_args=*/false);
15036 if (converted_args == error_mark_node)
15039 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
15040 if (decl_type == error_mark_node)
15044 /* Never do unification on the 'this' parameter. */
15045 decl_arg_types = skip_artificial_parms_for (decl,
15046 TYPE_ARG_TYPES (decl_type));
15048 nargs = list_length (decl_arg_types);
15049 args = XALLOCAVEC (tree, nargs);
15050 for (arg = decl_arg_types, ix = 0;
15051 arg != NULL_TREE && arg != void_list_node;
15052 arg = TREE_CHAIN (arg), ++ix)
15053 args[ix] = TREE_VALUE (arg);
15055 if (fn_type_unification (fn, explicit_args, targs,
15057 (check_rettype || DECL_CONV_FN_P (fn)
15058 ? TREE_TYPE (decl_type) : NULL_TREE),
15059 DEDUCE_EXACT, LOOKUP_NORMAL))
15065 /* Return the innermost template arguments that, when applied to a
15066 template specialization whose innermost template parameters are
15067 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
15070 For example, suppose we have:
15072 template <class T, class U> struct S {};
15073 template <class T> struct S<T*, int> {};
15075 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
15076 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
15077 int}. The resulting vector will be {double}, indicating that `T'
15078 is bound to `double'. */
15081 get_class_bindings (tree tparms, tree spec_args, tree args)
15083 int i, ntparms = TREE_VEC_LENGTH (tparms);
15085 tree innermost_deduced_args;
15087 innermost_deduced_args = make_tree_vec (ntparms);
15088 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15090 deduced_args = copy_node (args);
15091 SET_TMPL_ARGS_LEVEL (deduced_args,
15092 TMPL_ARGS_DEPTH (deduced_args),
15093 innermost_deduced_args);
15096 deduced_args = innermost_deduced_args;
15098 if (unify (tparms, deduced_args,
15099 INNERMOST_TEMPLATE_ARGS (spec_args),
15100 INNERMOST_TEMPLATE_ARGS (args),
15104 for (i = 0; i < ntparms; ++i)
15105 if (! TREE_VEC_ELT (innermost_deduced_args, i))
15108 /* Verify that nondeduced template arguments agree with the type
15109 obtained from argument deduction.
15113 struct A { typedef int X; };
15114 template <class T, class U> struct C {};
15115 template <class T> struct C<T, typename T::X> {};
15117 Then with the instantiation `C<A, int>', we can deduce that
15118 `T' is `A' but unify () does not check whether `typename T::X'
15120 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
15121 if (spec_args == error_mark_node
15122 /* We only need to check the innermost arguments; the other
15123 arguments will always agree. */
15124 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
15125 INNERMOST_TEMPLATE_ARGS (args)))
15128 /* Now that we have bindings for all of the template arguments,
15129 ensure that the arguments deduced for the template template
15130 parameters have compatible template parameter lists. See the use
15131 of template_template_parm_bindings_ok_p in fn_type_unification
15132 for more information. */
15133 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
15136 return deduced_args;
15139 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
15140 Return the TREE_LIST node with the most specialized template, if
15141 any. If there is no most specialized template, the error_mark_node
15144 Note that this function does not look at, or modify, the
15145 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
15146 returned is one of the elements of INSTANTIATIONS, callers may
15147 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
15148 and retrieve it from the value returned. */
15151 most_specialized_instantiation (tree templates)
15155 ++processing_template_decl;
15158 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
15162 if (get_bindings (TREE_VALUE (champ),
15163 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15164 NULL_TREE, /*check_ret=*/false))
15167 if (get_bindings (TREE_VALUE (fn),
15168 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15169 NULL_TREE, /*check_ret=*/false))
15176 /* Equally specialized, move to next function. If there
15177 is no next function, nothing's most specialized. */
15178 fn = TREE_CHAIN (fn);
15186 /* Now verify that champ is better than everything earlier in the
15187 instantiation list. */
15188 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
15189 if (get_bindings (TREE_VALUE (champ),
15190 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15191 NULL_TREE, /*check_ret=*/false)
15192 || !get_bindings (TREE_VALUE (fn),
15193 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15194 NULL_TREE, /*check_ret=*/false))
15200 processing_template_decl--;
15203 return error_mark_node;
15208 /* If DECL is a specialization of some template, return the most
15209 general such template. Otherwise, returns NULL_TREE.
15211 For example, given:
15213 template <class T> struct S { template <class U> void f(U); };
15215 if TMPL is `template <class U> void S<int>::f(U)' this will return
15216 the full template. This function will not trace past partial
15217 specializations, however. For example, given in addition:
15219 template <class T> struct S<T*> { template <class U> void f(U); };
15221 if TMPL is `template <class U> void S<int*>::f(U)' this will return
15222 `template <class T> template <class U> S<T*>::f(U)'. */
15225 most_general_template (tree decl)
15227 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
15228 an immediate specialization. */
15229 if (TREE_CODE (decl) == FUNCTION_DECL)
15231 if (DECL_TEMPLATE_INFO (decl)) {
15232 decl = DECL_TI_TEMPLATE (decl);
15234 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
15235 template friend. */
15236 if (TREE_CODE (decl) != TEMPLATE_DECL)
15242 /* Look for more and more general templates. */
15243 while (DECL_TEMPLATE_INFO (decl))
15245 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
15246 (See cp-tree.h for details.) */
15247 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
15250 if (CLASS_TYPE_P (TREE_TYPE (decl))
15251 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
15254 /* Stop if we run into an explicitly specialized class template. */
15255 if (!DECL_NAMESPACE_SCOPE_P (decl)
15256 && DECL_CONTEXT (decl)
15257 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
15260 decl = DECL_TI_TEMPLATE (decl);
15266 /* Return the most specialized of the class template partial
15267 specializations of TMPL which can produce TYPE, a specialization of
15268 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
15269 a _TYPE node corresponding to the partial specialization, while the
15270 TREE_PURPOSE is the set of template arguments that must be
15271 substituted into the TREE_TYPE in order to generate TYPE.
15273 If the choice of partial specialization is ambiguous, a diagnostic
15274 is issued, and the error_mark_node is returned. If there are no
15275 partial specializations of TMPL matching TYPE, then NULL_TREE is
15279 most_specialized_class (tree type, tree tmpl)
15281 tree list = NULL_TREE;
15287 tree outer_args = NULL_TREE;
15289 tmpl = most_general_template (tmpl);
15290 args = CLASSTYPE_TI_ARGS (type);
15292 /* For determining which partial specialization to use, only the
15293 innermost args are interesting. */
15294 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15296 outer_args = strip_innermost_template_args (args, 1);
15297 args = INNERMOST_TEMPLATE_ARGS (args);
15300 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
15302 tree partial_spec_args;
15304 tree parms = TREE_VALUE (t);
15306 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
15311 ++processing_template_decl;
15313 /* Discard the outer levels of args, and then substitute in the
15314 template args from the enclosing class. */
15315 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
15316 partial_spec_args = tsubst_template_args
15317 (partial_spec_args, outer_args, tf_none, NULL_TREE);
15319 /* PARMS already refers to just the innermost parms, but the
15320 template parms in partial_spec_args had their levels lowered
15321 by tsubst, so we need to do the same for the parm list. We
15322 can't just tsubst the TREE_VEC itself, as tsubst wants to
15323 treat a TREE_VEC as an argument vector. */
15324 parms = copy_node (parms);
15325 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
15326 TREE_VEC_ELT (parms, i) =
15327 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
15329 --processing_template_decl;
15331 spec_args = get_class_bindings (parms,
15337 spec_args = add_to_template_args (outer_args, spec_args);
15338 list = tree_cons (spec_args, TREE_VALUE (t), list);
15339 TREE_TYPE (list) = TREE_TYPE (t);
15346 ambiguous_p = false;
15349 t = TREE_CHAIN (t);
15350 for (; t; t = TREE_CHAIN (t))
15352 fate = more_specialized_class (champ, t);
15359 t = TREE_CHAIN (t);
15362 ambiguous_p = true;
15371 for (t = list; t && t != champ; t = TREE_CHAIN (t))
15373 fate = more_specialized_class (champ, t);
15376 ambiguous_p = true;
15383 const char *str = "candidates are:";
15384 error ("ambiguous class template instantiation for %q#T", type);
15385 for (t = list; t; t = TREE_CHAIN (t))
15387 error ("%s %+#T", str, TREE_TYPE (t));
15390 return error_mark_node;
15396 /* Explicitly instantiate DECL. */
15399 do_decl_instantiation (tree decl, tree storage)
15401 tree result = NULL_TREE;
15404 if (!decl || decl == error_mark_node)
15405 /* An error occurred, for which grokdeclarator has already issued
15406 an appropriate message. */
15408 else if (! DECL_LANG_SPECIFIC (decl))
15410 error ("explicit instantiation of non-template %q#D", decl);
15413 else if (TREE_CODE (decl) == VAR_DECL)
15415 /* There is an asymmetry here in the way VAR_DECLs and
15416 FUNCTION_DECLs are handled by grokdeclarator. In the case of
15417 the latter, the DECL we get back will be marked as a
15418 template instantiation, and the appropriate
15419 DECL_TEMPLATE_INFO will be set up. This does not happen for
15420 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
15421 should handle VAR_DECLs as it currently handles
15423 if (!DECL_CLASS_SCOPE_P (decl))
15425 error ("%qD is not a static data member of a class template", decl);
15428 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
15429 if (!result || TREE_CODE (result) != VAR_DECL)
15431 error ("no matching template for %qD found", decl);
15434 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
15436 error ("type %qT for explicit instantiation %qD does not match "
15437 "declared type %qT", TREE_TYPE (result), decl,
15442 else if (TREE_CODE (decl) != FUNCTION_DECL)
15444 error ("explicit instantiation of %q#D", decl);
15450 /* Check for various error cases. Note that if the explicit
15451 instantiation is valid the RESULT will currently be marked as an
15452 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
15453 until we get here. */
15455 if (DECL_TEMPLATE_SPECIALIZATION (result))
15457 /* DR 259 [temp.spec].
15459 Both an explicit instantiation and a declaration of an explicit
15460 specialization shall not appear in a program unless the explicit
15461 instantiation follows a declaration of the explicit specialization.
15463 For a given set of template parameters, if an explicit
15464 instantiation of a template appears after a declaration of an
15465 explicit specialization for that template, the explicit
15466 instantiation has no effect. */
15469 else if (DECL_EXPLICIT_INSTANTIATION (result))
15473 No program shall explicitly instantiate any template more
15476 We check DECL_NOT_REALLY_EXTERN so as not to complain when
15477 the first instantiation was `extern' and the second is not,
15478 and EXTERN_P for the opposite case. */
15479 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
15480 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
15481 /* If an "extern" explicit instantiation follows an ordinary
15482 explicit instantiation, the template is instantiated. */
15486 else if (!DECL_IMPLICIT_INSTANTIATION (result))
15488 error ("no matching template for %qD found", result);
15491 else if (!DECL_TEMPLATE_INFO (result))
15493 permerror (input_location, "explicit instantiation of non-template %q#D", result);
15497 if (storage == NULL_TREE)
15499 else if (storage == ridpointers[(int) RID_EXTERN])
15501 if (!in_system_header && (cxx_dialect == cxx98))
15502 pedwarn (input_location, OPT_pedantic,
15503 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
15508 error ("storage class %qD applied to template instantiation", storage);
15510 check_explicit_instantiation_namespace (result);
15511 mark_decl_instantiated (result, extern_p);
15513 instantiate_decl (result, /*defer_ok=*/1,
15514 /*expl_inst_class_mem_p=*/false);
15518 mark_class_instantiated (tree t, int extern_p)
15520 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
15521 SET_CLASSTYPE_INTERFACE_KNOWN (t);
15522 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
15523 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
15526 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
15527 rest_of_type_compilation (t, 1);
15531 /* Called from do_type_instantiation through binding_table_foreach to
15532 do recursive instantiation for the type bound in ENTRY. */
15534 bt_instantiate_type_proc (binding_entry entry, void *data)
15536 tree storage = *(tree *) data;
15538 if (MAYBE_CLASS_TYPE_P (entry->type)
15539 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
15540 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
15543 /* Called from do_type_instantiation to instantiate a member
15544 (a member function or a static member variable) of an
15545 explicitly instantiated class template. */
15547 instantiate_class_member (tree decl, int extern_p)
15549 mark_decl_instantiated (decl, extern_p);
15551 instantiate_decl (decl, /*defer_ok=*/1,
15552 /*expl_inst_class_mem_p=*/true);
15555 /* Perform an explicit instantiation of template class T. STORAGE, if
15556 non-null, is the RID for extern, inline or static. COMPLAIN is
15557 nonzero if this is called from the parser, zero if called recursively,
15558 since the standard is unclear (as detailed below). */
15561 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
15566 int previous_instantiation_extern_p = 0;
15568 if (TREE_CODE (t) == TYPE_DECL)
15571 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
15573 error ("explicit instantiation of non-template type %qT", t);
15579 if (!COMPLETE_TYPE_P (t))
15581 if (complain & tf_error)
15582 error ("explicit instantiation of %q#T before definition of template",
15587 if (storage != NULL_TREE)
15589 if (!in_system_header)
15591 if (storage == ridpointers[(int) RID_EXTERN])
15593 if (cxx_dialect == cxx98)
15594 pedwarn (input_location, OPT_pedantic,
15595 "ISO C++ 1998 forbids the use of %<extern%> on "
15596 "explicit instantiations");
15599 pedwarn (input_location, OPT_pedantic,
15600 "ISO C++ forbids the use of %qE"
15601 " on explicit instantiations", storage);
15604 if (storage == ridpointers[(int) RID_INLINE])
15606 else if (storage == ridpointers[(int) RID_EXTERN])
15608 else if (storage == ridpointers[(int) RID_STATIC])
15612 error ("storage class %qD applied to template instantiation",
15618 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
15620 /* DR 259 [temp.spec].
15622 Both an explicit instantiation and a declaration of an explicit
15623 specialization shall not appear in a program unless the explicit
15624 instantiation follows a declaration of the explicit specialization.
15626 For a given set of template parameters, if an explicit
15627 instantiation of a template appears after a declaration of an
15628 explicit specialization for that template, the explicit
15629 instantiation has no effect. */
15632 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
15636 No program shall explicitly instantiate any template more
15639 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
15640 instantiation was `extern'. If EXTERN_P then the second is.
15641 These cases are OK. */
15642 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
15644 if (!previous_instantiation_extern_p && !extern_p
15645 && (complain & tf_error))
15646 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
15648 /* If we've already instantiated the template, just return now. */
15649 if (!CLASSTYPE_INTERFACE_ONLY (t))
15653 check_explicit_instantiation_namespace (TYPE_NAME (t));
15654 mark_class_instantiated (t, extern_p);
15662 /* In contrast to implicit instantiation, where only the
15663 declarations, and not the definitions, of members are
15664 instantiated, we have here:
15668 The explicit instantiation of a class template specialization
15669 implies the instantiation of all of its members not
15670 previously explicitly specialized in the translation unit
15671 containing the explicit instantiation.
15673 Of course, we can't instantiate member template classes, since
15674 we don't have any arguments for them. Note that the standard
15675 is unclear on whether the instantiation of the members are
15676 *explicit* instantiations or not. However, the most natural
15677 interpretation is that it should be an explicit instantiation. */
15680 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
15681 if (TREE_CODE (tmp) == FUNCTION_DECL
15682 && DECL_TEMPLATE_INSTANTIATION (tmp))
15683 instantiate_class_member (tmp, extern_p);
15685 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
15686 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
15687 instantiate_class_member (tmp, extern_p);
15689 if (CLASSTYPE_NESTED_UTDS (t))
15690 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
15691 bt_instantiate_type_proc, &storage);
15695 /* Given a function DECL, which is a specialization of TMPL, modify
15696 DECL to be a re-instantiation of TMPL with the same template
15697 arguments. TMPL should be the template into which tsubst'ing
15698 should occur for DECL, not the most general template.
15700 One reason for doing this is a scenario like this:
15703 void f(const T&, int i);
15705 void g() { f(3, 7); }
15708 void f(const T& t, const int i) { }
15710 Note that when the template is first instantiated, with
15711 instantiate_template, the resulting DECL will have no name for the
15712 first parameter, and the wrong type for the second. So, when we go
15713 to instantiate the DECL, we regenerate it. */
15716 regenerate_decl_from_template (tree decl, tree tmpl)
15718 /* The arguments used to instantiate DECL, from the most general
15723 args = DECL_TI_ARGS (decl);
15724 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
15726 /* Make sure that we can see identifiers, and compute access
15728 push_access_scope (decl);
15730 if (TREE_CODE (decl) == FUNCTION_DECL)
15738 args_depth = TMPL_ARGS_DEPTH (args);
15739 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
15740 if (args_depth > parms_depth)
15741 args = get_innermost_template_args (args, parms_depth);
15743 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
15744 args, tf_error, NULL_TREE);
15746 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
15749 /* Merge parameter declarations. */
15750 decl_parm = skip_artificial_parms_for (decl,
15751 DECL_ARGUMENTS (decl));
15753 = skip_artificial_parms_for (code_pattern,
15754 DECL_ARGUMENTS (code_pattern));
15755 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
15760 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
15761 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
15762 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
15764 parm_type = type_decays_to (parm_type);
15765 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
15766 TREE_TYPE (decl_parm) = parm_type;
15767 attributes = DECL_ATTRIBUTES (pattern_parm);
15768 if (DECL_ATTRIBUTES (decl_parm) != attributes)
15770 DECL_ATTRIBUTES (decl_parm) = attributes;
15771 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
15773 decl_parm = TREE_CHAIN (decl_parm);
15774 pattern_parm = TREE_CHAIN (pattern_parm);
15776 /* Merge any parameters that match with the function parameter
15778 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
15781 tree expanded_types;
15782 /* Expand the TYPE_PACK_EXPANSION that provides the types for
15783 the parameters in this function parameter pack. */
15784 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
15785 args, tf_error, NULL_TREE);
15786 len = TREE_VEC_LENGTH (expanded_types);
15787 for (i = 0; i < len; i++)
15792 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
15793 /* Rename the parameter to include the index. */
15794 DECL_NAME (decl_parm) =
15795 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
15796 parm_type = TREE_VEC_ELT (expanded_types, i);
15797 parm_type = type_decays_to (parm_type);
15798 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
15799 TREE_TYPE (decl_parm) = parm_type;
15800 attributes = DECL_ATTRIBUTES (pattern_parm);
15801 if (DECL_ATTRIBUTES (decl_parm) != attributes)
15803 DECL_ATTRIBUTES (decl_parm) = attributes;
15804 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
15806 decl_parm = TREE_CHAIN (decl_parm);
15809 /* Merge additional specifiers from the CODE_PATTERN. */
15810 if (DECL_DECLARED_INLINE_P (code_pattern)
15811 && !DECL_DECLARED_INLINE_P (decl))
15812 DECL_DECLARED_INLINE_P (decl) = 1;
15814 else if (TREE_CODE (decl) == VAR_DECL)
15815 DECL_INITIAL (decl) =
15816 tsubst_expr (DECL_INITIAL (code_pattern), args,
15817 tf_error, DECL_TI_TEMPLATE (decl),
15818 /*integral_constant_expression_p=*/false);
15820 gcc_unreachable ();
15822 pop_access_scope (decl);
15825 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
15826 substituted to get DECL. */
15829 template_for_substitution (tree decl)
15831 tree tmpl = DECL_TI_TEMPLATE (decl);
15833 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
15834 for the instantiation. This is not always the most general
15835 template. Consider, for example:
15838 struct S { template <class U> void f();
15839 template <> void f<int>(); };
15841 and an instantiation of S<double>::f<int>. We want TD to be the
15842 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
15843 while (/* An instantiation cannot have a definition, so we need a
15844 more general template. */
15845 DECL_TEMPLATE_INSTANTIATION (tmpl)
15846 /* We must also deal with friend templates. Given:
15848 template <class T> struct S {
15849 template <class U> friend void f() {};
15852 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
15853 so far as the language is concerned, but that's still
15854 where we get the pattern for the instantiation from. On
15855 other hand, if the definition comes outside the class, say:
15857 template <class T> struct S {
15858 template <class U> friend void f();
15860 template <class U> friend void f() {}
15862 we don't need to look any further. That's what the check for
15863 DECL_INITIAL is for. */
15864 || (TREE_CODE (decl) == FUNCTION_DECL
15865 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
15866 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
15868 /* The present template, TD, should not be a definition. If it
15869 were a definition, we should be using it! Note that we
15870 cannot restructure the loop to just keep going until we find
15871 a template with a definition, since that might go too far if
15872 a specialization was declared, but not defined. */
15873 gcc_assert (TREE_CODE (decl) != VAR_DECL
15874 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
15876 /* Fetch the more general template. */
15877 tmpl = DECL_TI_TEMPLATE (tmpl);
15883 /* Returns true if we need to instantiate this template instance even if we
15884 know we aren't going to emit it.. */
15887 always_instantiate_p (tree decl)
15889 /* We always instantiate inline functions so that we can inline them. An
15890 explicit instantiation declaration prohibits implicit instantiation of
15891 non-inline functions. With high levels of optimization, we would
15892 normally inline non-inline functions -- but we're not allowed to do
15893 that for "extern template" functions. Therefore, we check
15894 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
15895 return ((TREE_CODE (decl) == FUNCTION_DECL
15896 && DECL_DECLARED_INLINE_P (decl))
15897 /* And we need to instantiate static data members so that
15898 their initializers are available in integral constant
15900 || (TREE_CODE (decl) == VAR_DECL
15901 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)));
15904 /* Produce the definition of D, a _DECL generated from a template. If
15905 DEFER_OK is nonzero, then we don't have to actually do the
15906 instantiation now; we just have to do it sometime. Normally it is
15907 an error if this is an explicit instantiation but D is undefined.
15908 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
15909 explicitly instantiated class template. */
15912 instantiate_decl (tree d, int defer_ok,
15913 bool expl_inst_class_mem_p)
15915 tree tmpl = DECL_TI_TEMPLATE (d);
15922 bool pattern_defined;
15924 location_t saved_loc = input_location;
15927 /* This function should only be used to instantiate templates for
15928 functions and static member variables. */
15929 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
15930 || TREE_CODE (d) == VAR_DECL);
15932 /* Variables are never deferred; if instantiation is required, they
15933 are instantiated right away. That allows for better code in the
15934 case that an expression refers to the value of the variable --
15935 if the variable has a constant value the referring expression can
15936 take advantage of that fact. */
15937 if (TREE_CODE (d) == VAR_DECL)
15940 /* Don't instantiate cloned functions. Instead, instantiate the
15941 functions they cloned. */
15942 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
15943 d = DECL_CLONED_FUNCTION (d);
15945 if (DECL_TEMPLATE_INSTANTIATED (d)
15946 || DECL_TEMPLATE_SPECIALIZATION (d))
15947 /* D has already been instantiated or explicitly specialized, so
15948 there's nothing for us to do here.
15950 It might seem reasonable to check whether or not D is an explicit
15951 instantiation, and, if so, stop here. But when an explicit
15952 instantiation is deferred until the end of the compilation,
15953 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
15954 the instantiation. */
15957 /* Check to see whether we know that this template will be
15958 instantiated in some other file, as with "extern template"
15960 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
15962 /* In general, we do not instantiate such templates. */
15963 if (external_p && !always_instantiate_p (d))
15966 gen_tmpl = most_general_template (tmpl);
15967 gen_args = DECL_TI_ARGS (d);
15969 if (tmpl != gen_tmpl)
15970 /* We should already have the extra args. */
15971 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
15972 == TMPL_ARGS_DEPTH (gen_args));
15973 /* And what's in the hash table should match D. */
15974 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
15975 || spec == NULL_TREE);
15977 /* This needs to happen before any tsubsting. */
15978 if (! push_tinst_level (d))
15981 timevar_push (TV_PARSE);
15983 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
15984 for the instantiation. */
15985 td = template_for_substitution (d);
15986 code_pattern = DECL_TEMPLATE_RESULT (td);
15988 /* We should never be trying to instantiate a member of a class
15989 template or partial specialization. */
15990 gcc_assert (d != code_pattern);
15992 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
15993 || DECL_TEMPLATE_SPECIALIZATION (td))
15994 /* In the case of a friend template whose definition is provided
15995 outside the class, we may have too many arguments. Drop the
15996 ones we don't need. The same is true for specializations. */
15997 args = get_innermost_template_args
15998 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
16002 if (TREE_CODE (d) == FUNCTION_DECL)
16003 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
16005 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
16007 /* We may be in the middle of deferred access check. Disable it now. */
16008 push_deferring_access_checks (dk_no_deferred);
16010 /* Unless an explicit instantiation directive has already determined
16011 the linkage of D, remember that a definition is available for
16013 if (pattern_defined
16014 && !DECL_INTERFACE_KNOWN (d)
16015 && !DECL_NOT_REALLY_EXTERN (d))
16016 mark_definable (d);
16018 input_location = DECL_SOURCE_LOCATION (d);
16020 /* If D is a member of an explicitly instantiated class template,
16021 and no definition is available, treat it like an implicit
16023 if (!pattern_defined && expl_inst_class_mem_p
16024 && DECL_EXPLICIT_INSTANTIATION (d))
16026 DECL_NOT_REALLY_EXTERN (d) = 0;
16027 DECL_INTERFACE_KNOWN (d) = 0;
16028 SET_DECL_IMPLICIT_INSTANTIATION (d);
16031 /* Recheck the substitutions to obtain any warning messages
16032 about ignoring cv qualifiers. Don't do this for artificial decls,
16033 as it breaks the context-sensitive substitution for lambda op(). */
16034 if (!defer_ok && !DECL_ARTIFICIAL (d))
16036 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
16037 tree type = TREE_TYPE (gen);
16039 /* Make sure that we can see identifiers, and compute access
16040 correctly. D is already the target FUNCTION_DECL with the
16042 push_access_scope (d);
16044 if (TREE_CODE (gen) == FUNCTION_DECL)
16046 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
16047 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
16049 /* Don't simply tsubst the function type, as that will give
16050 duplicate warnings about poor parameter qualifications.
16051 The function arguments are the same as the decl_arguments
16052 without the top level cv qualifiers. */
16053 type = TREE_TYPE (type);
16055 tsubst (type, gen_args, tf_warning_or_error, d);
16057 pop_access_scope (d);
16060 /* Defer all other templates, unless we have been explicitly
16061 forbidden from doing so. */
16062 if (/* If there is no definition, we cannot instantiate the
16065 /* If it's OK to postpone instantiation, do so. */
16067 /* If this is a static data member that will be defined
16068 elsewhere, we don't want to instantiate the entire data
16069 member, but we do want to instantiate the initializer so that
16070 we can substitute that elsewhere. */
16071 || (external_p && TREE_CODE (d) == VAR_DECL))
16073 /* The definition of the static data member is now required so
16074 we must substitute the initializer. */
16075 if (TREE_CODE (d) == VAR_DECL
16076 && !DECL_INITIAL (d)
16077 && DECL_INITIAL (code_pattern))
16082 ns = decl_namespace_context (d);
16083 push_nested_namespace (ns);
16084 push_nested_class (DECL_CONTEXT (d));
16085 init = tsubst_expr (DECL_INITIAL (code_pattern),
16087 tf_warning_or_error, NULL_TREE,
16088 /*integral_constant_expression_p=*/false);
16089 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
16090 /*asmspec_tree=*/NULL_TREE,
16091 LOOKUP_ONLYCONVERTING);
16092 pop_nested_class ();
16093 pop_nested_namespace (ns);
16096 /* We restore the source position here because it's used by
16097 add_pending_template. */
16098 input_location = saved_loc;
16100 if (at_eof && !pattern_defined
16101 && DECL_EXPLICIT_INSTANTIATION (d)
16102 && DECL_NOT_REALLY_EXTERN (d))
16105 The definition of a non-exported function template, a
16106 non-exported member function template, or a non-exported
16107 member function or static data member of a class template
16108 shall be present in every translation unit in which it is
16109 explicitly instantiated. */
16110 permerror (input_location, "explicit instantiation of %qD "
16111 "but no definition available", d);
16113 /* ??? Historically, we have instantiated inline functions, even
16114 when marked as "extern template". */
16115 if (!(external_p && TREE_CODE (d) == VAR_DECL))
16116 add_pending_template (d);
16119 /* Tell the repository that D is available in this translation unit
16120 -- and see if it is supposed to be instantiated here. */
16121 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
16123 /* In a PCH file, despite the fact that the repository hasn't
16124 requested instantiation in the PCH it is still possible that
16125 an instantiation will be required in a file that includes the
16128 add_pending_template (d);
16129 /* Instantiate inline functions so that the inliner can do its
16130 job, even though we'll not be emitting a copy of this
16132 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
16136 need_push = !cfun || !global_bindings_p ();
16138 push_to_top_level ();
16140 /* Mark D as instantiated so that recursive calls to
16141 instantiate_decl do not try to instantiate it again. */
16142 DECL_TEMPLATE_INSTANTIATED (d) = 1;
16144 /* Regenerate the declaration in case the template has been modified
16145 by a subsequent redeclaration. */
16146 regenerate_decl_from_template (d, td);
16148 /* We already set the file and line above. Reset them now in case
16149 they changed as a result of calling regenerate_decl_from_template. */
16150 input_location = DECL_SOURCE_LOCATION (d);
16152 if (TREE_CODE (d) == VAR_DECL)
16156 /* Clear out DECL_RTL; whatever was there before may not be right
16157 since we've reset the type of the declaration. */
16158 SET_DECL_RTL (d, NULL_RTX);
16159 DECL_IN_AGGR_P (d) = 0;
16161 /* The initializer is placed in DECL_INITIAL by
16162 regenerate_decl_from_template. Pull it out so that
16163 cp_finish_decl can process it. */
16164 init = DECL_INITIAL (d);
16165 DECL_INITIAL (d) = NULL_TREE;
16166 DECL_INITIALIZED_P (d) = 0;
16168 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
16169 initializer. That function will defer actual emission until
16170 we have a chance to determine linkage. */
16171 DECL_EXTERNAL (d) = 0;
16173 /* Enter the scope of D so that access-checking works correctly. */
16174 push_nested_class (DECL_CONTEXT (d));
16175 cp_finish_decl (d, init, false, NULL_TREE, 0);
16176 pop_nested_class ();
16178 else if (TREE_CODE (d) == FUNCTION_DECL)
16180 htab_t saved_local_specializations;
16185 /* Save away the current list, in case we are instantiating one
16186 template from within the body of another. */
16187 saved_local_specializations = local_specializations;
16189 /* Set up the list of local specializations. */
16190 local_specializations = htab_create (37,
16191 hash_local_specialization,
16192 eq_local_specializations,
16195 /* Set up context. */
16196 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
16198 /* Create substitution entries for the parameters. */
16199 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
16200 tmpl_parm = DECL_ARGUMENTS (subst_decl);
16201 spec_parm = DECL_ARGUMENTS (d);
16202 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
16204 register_local_specialization (spec_parm, tmpl_parm);
16205 spec_parm = skip_artificial_parms_for (d, spec_parm);
16206 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
16208 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
16210 register_local_specialization (spec_parm, tmpl_parm);
16211 tmpl_parm = TREE_CHAIN (tmpl_parm);
16212 spec_parm = TREE_CHAIN (spec_parm);
16214 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
16216 /* Register the (value) argument pack as a specialization of
16217 TMPL_PARM, then move on. */
16218 tree argpack = make_fnparm_pack (spec_parm);
16219 register_local_specialization (argpack, tmpl_parm);
16220 tmpl_parm = TREE_CHAIN (tmpl_parm);
16221 spec_parm = NULL_TREE;
16223 gcc_assert (!spec_parm);
16225 /* Substitute into the body of the function. */
16226 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
16227 tf_warning_or_error, tmpl,
16228 /*integral_constant_expression_p=*/false);
16230 /* Set the current input_location to the end of the function
16231 so that finish_function knows where we are. */
16232 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
16234 /* We don't need the local specializations any more. */
16235 htab_delete (local_specializations);
16236 local_specializations = saved_local_specializations;
16238 /* Finish the function. */
16239 d = finish_function (0);
16240 expand_or_defer_fn (d);
16243 /* We're not deferring instantiation any more. */
16244 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
16247 pop_from_top_level ();
16250 input_location = saved_loc;
16251 pop_deferring_access_checks ();
16252 pop_tinst_level ();
16254 timevar_pop (TV_PARSE);
16259 /* Run through the list of templates that we wish we could
16260 instantiate, and instantiate any we can. RETRIES is the
16261 number of times we retry pending template instantiation. */
16264 instantiate_pending_templates (int retries)
16267 location_t saved_loc = input_location;
16269 /* Instantiating templates may trigger vtable generation. This in turn
16270 may require further template instantiations. We place a limit here
16271 to avoid infinite loop. */
16272 if (pending_templates && retries >= max_tinst_depth)
16274 tree decl = pending_templates->tinst->decl;
16276 error ("template instantiation depth exceeds maximum of %d"
16277 " instantiating %q+D, possibly from virtual table generation"
16278 " (use -ftemplate-depth-NN to increase the maximum)",
16279 max_tinst_depth, decl);
16280 if (TREE_CODE (decl) == FUNCTION_DECL)
16281 /* Pretend that we defined it. */
16282 DECL_INITIAL (decl) = error_mark_node;
16288 struct pending_template **t = &pending_templates;
16289 struct pending_template *last = NULL;
16293 tree instantiation = reopen_tinst_level ((*t)->tinst);
16294 bool complete = false;
16296 if (TYPE_P (instantiation))
16300 if (!COMPLETE_TYPE_P (instantiation))
16302 instantiate_class_template (instantiation);
16303 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
16304 for (fn = TYPE_METHODS (instantiation);
16306 fn = TREE_CHAIN (fn))
16307 if (! DECL_ARTIFICIAL (fn))
16308 instantiate_decl (fn,
16310 /*expl_inst_class_mem_p=*/false);
16311 if (COMPLETE_TYPE_P (instantiation))
16315 complete = COMPLETE_TYPE_P (instantiation);
16319 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
16320 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
16323 = instantiate_decl (instantiation,
16325 /*expl_inst_class_mem_p=*/false);
16326 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
16330 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
16331 || DECL_TEMPLATE_INSTANTIATED (instantiation));
16335 /* If INSTANTIATION has been instantiated, then we don't
16336 need to consider it again in the future. */
16344 current_tinst_level = NULL;
16346 last_pending_template = last;
16348 while (reconsider);
16350 input_location = saved_loc;
16353 /* Substitute ARGVEC into T, which is a list of initializers for
16354 either base class or a non-static data member. The TREE_PURPOSEs
16355 are DECLs, and the TREE_VALUEs are the initializer values. Used by
16356 instantiate_decl. */
16359 tsubst_initializer_list (tree t, tree argvec)
16361 tree inits = NULL_TREE;
16363 for (; t; t = TREE_CHAIN (t))
16367 tree expanded_bases = NULL_TREE;
16368 tree expanded_arguments = NULL_TREE;
16371 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
16376 /* Expand the base class expansion type into separate base
16378 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
16379 tf_warning_or_error,
16381 if (expanded_bases == error_mark_node)
16384 /* We'll be building separate TREE_LISTs of arguments for
16386 len = TREE_VEC_LENGTH (expanded_bases);
16387 expanded_arguments = make_tree_vec (len);
16388 for (i = 0; i < len; i++)
16389 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
16391 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
16392 expand each argument in the TREE_VALUE of t. */
16393 expr = make_node (EXPR_PACK_EXPANSION);
16394 PACK_EXPANSION_PARAMETER_PACKS (expr) =
16395 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
16397 if (TREE_VALUE (t) == void_type_node)
16398 /* VOID_TYPE_NODE is used to indicate
16399 value-initialization. */
16401 for (i = 0; i < len; i++)
16402 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
16406 /* Substitute parameter packs into each argument in the
16408 in_base_initializer = 1;
16409 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
16411 tree expanded_exprs;
16413 /* Expand the argument. */
16414 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
16416 = tsubst_pack_expansion (expr, argvec,
16417 tf_warning_or_error,
16419 if (expanded_exprs == error_mark_node)
16422 /* Prepend each of the expanded expressions to the
16423 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
16424 for (i = 0; i < len; i++)
16426 TREE_VEC_ELT (expanded_arguments, i) =
16427 tree_cons (NULL_TREE,
16428 TREE_VEC_ELT (expanded_exprs, i),
16429 TREE_VEC_ELT (expanded_arguments, i));
16432 in_base_initializer = 0;
16434 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
16435 since we built them backwards. */
16436 for (i = 0; i < len; i++)
16438 TREE_VEC_ELT (expanded_arguments, i) =
16439 nreverse (TREE_VEC_ELT (expanded_arguments, i));
16444 for (i = 0; i < len; ++i)
16446 if (expanded_bases)
16448 decl = TREE_VEC_ELT (expanded_bases, i);
16449 decl = expand_member_init (decl);
16450 init = TREE_VEC_ELT (expanded_arguments, i);
16454 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
16455 tf_warning_or_error, NULL_TREE);
16457 decl = expand_member_init (decl);
16458 if (decl && !DECL_P (decl))
16459 in_base_initializer = 1;
16461 init = tsubst_expr (TREE_VALUE (t), argvec,
16462 tf_warning_or_error, NULL_TREE,
16463 /*integral_constant_expression_p=*/false);
16464 in_base_initializer = 0;
16469 init = build_tree_list (decl, init);
16470 TREE_CHAIN (init) = inits;
16478 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
16481 set_current_access_from_decl (tree decl)
16483 if (TREE_PRIVATE (decl))
16484 current_access_specifier = access_private_node;
16485 else if (TREE_PROTECTED (decl))
16486 current_access_specifier = access_protected_node;
16488 current_access_specifier = access_public_node;
16491 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
16492 is the instantiation (which should have been created with
16493 start_enum) and ARGS are the template arguments to use. */
16496 tsubst_enum (tree tag, tree newtag, tree args)
16500 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
16505 decl = TREE_VALUE (e);
16506 /* Note that in a template enum, the TREE_VALUE is the
16507 CONST_DECL, not the corresponding INTEGER_CST. */
16508 value = tsubst_expr (DECL_INITIAL (decl),
16509 args, tf_warning_or_error, NULL_TREE,
16510 /*integral_constant_expression_p=*/true);
16512 /* Give this enumeration constant the correct access. */
16513 set_current_access_from_decl (decl);
16515 /* Actually build the enumerator itself. */
16516 build_enumerator (DECL_NAME (decl), value, newtag);
16519 finish_enum (newtag);
16520 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
16521 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
16524 /* DECL is a FUNCTION_DECL that is a template specialization. Return
16525 its type -- but without substituting the innermost set of template
16526 arguments. So, innermost set of template parameters will appear in
16530 get_mostly_instantiated_function_type (tree decl)
16538 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
16539 targs = DECL_TI_ARGS (decl);
16540 tparms = DECL_TEMPLATE_PARMS (tmpl);
16541 parm_depth = TMPL_PARMS_DEPTH (tparms);
16543 /* There should be as many levels of arguments as there are levels
16545 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
16547 fn_type = TREE_TYPE (tmpl);
16549 if (parm_depth == 1)
16550 /* No substitution is necessary. */
16554 int i, save_access_control;
16557 /* Replace the innermost level of the TARGS with NULL_TREEs to
16558 let tsubst know not to substitute for those parameters. */
16559 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
16560 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
16561 SET_TMPL_ARGS_LEVEL (partial_args, i,
16562 TMPL_ARGS_LEVEL (targs, i));
16563 SET_TMPL_ARGS_LEVEL (partial_args,
16564 TMPL_ARGS_DEPTH (targs),
16565 make_tree_vec (DECL_NTPARMS (tmpl)));
16567 /* Disable access control as this function is used only during
16569 save_access_control = flag_access_control;
16570 flag_access_control = 0;
16572 ++processing_template_decl;
16573 /* Now, do the (partial) substitution to figure out the
16574 appropriate function type. */
16575 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
16576 --processing_template_decl;
16578 /* Substitute into the template parameters to obtain the real
16579 innermost set of parameters. This step is important if the
16580 innermost set of template parameters contains value
16581 parameters whose types depend on outer template parameters. */
16582 TREE_VEC_LENGTH (partial_args)--;
16583 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
16585 flag_access_control = save_access_control;
16591 /* Return truthvalue if we're processing a template different from
16592 the last one involved in diagnostics. */
16594 problematic_instantiation_changed (void)
16596 return last_template_error_tick != tinst_level_tick;
16599 /* Remember current template involved in diagnostics. */
16601 record_last_problematic_instantiation (void)
16603 last_template_error_tick = tinst_level_tick;
16606 struct tinst_level *
16607 current_instantiation (void)
16609 return current_tinst_level;
16612 /* [temp.param] Check that template non-type parm TYPE is of an allowable
16613 type. Return zero for ok, nonzero for disallowed. Issue error and
16614 warning messages under control of COMPLAIN. */
16617 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
16619 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
16621 else if (POINTER_TYPE_P (type))
16623 else if (TYPE_PTR_TO_MEMBER_P (type))
16625 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
16627 else if (TREE_CODE (type) == TYPENAME_TYPE)
16630 if (complain & tf_error)
16631 error ("%q#T is not a valid type for a template constant parameter", type);
16635 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
16636 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
16639 dependent_type_p_r (tree type)
16645 A type is dependent if it is:
16647 -- a template parameter. Template template parameters are types
16648 for us (since TYPE_P holds true for them) so we handle
16650 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16651 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
16653 /* -- a qualified-id with a nested-name-specifier which contains a
16654 class-name that names a dependent type or whose unqualified-id
16655 names a dependent type. */
16656 if (TREE_CODE (type) == TYPENAME_TYPE)
16658 /* -- a cv-qualified type where the cv-unqualified type is
16660 type = TYPE_MAIN_VARIANT (type);
16661 /* -- a compound type constructed from any dependent type. */
16662 if (TYPE_PTR_TO_MEMBER_P (type))
16663 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
16664 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
16666 else if (TREE_CODE (type) == POINTER_TYPE
16667 || TREE_CODE (type) == REFERENCE_TYPE)
16668 return dependent_type_p (TREE_TYPE (type));
16669 else if (TREE_CODE (type) == FUNCTION_TYPE
16670 || TREE_CODE (type) == METHOD_TYPE)
16674 if (dependent_type_p (TREE_TYPE (type)))
16676 for (arg_type = TYPE_ARG_TYPES (type);
16678 arg_type = TREE_CHAIN (arg_type))
16679 if (dependent_type_p (TREE_VALUE (arg_type)))
16683 /* -- an array type constructed from any dependent type or whose
16684 size is specified by a constant expression that is
16685 value-dependent. */
16686 if (TREE_CODE (type) == ARRAY_TYPE)
16688 if (TYPE_DOMAIN (type)
16689 && dependent_type_p (TYPE_DOMAIN (type)))
16691 return dependent_type_p (TREE_TYPE (type));
16693 else if (TREE_CODE (type) == INTEGER_TYPE
16694 && !TREE_CONSTANT (TYPE_MAX_VALUE (type)))
16696 /* If this is the TYPE_DOMAIN of an array type, consider it
16697 dependent. We already checked for value-dependence in
16698 compute_array_index_type. */
16699 return type_dependent_expression_p (TYPE_MAX_VALUE (type));
16702 /* -- a template-id in which either the template name is a template
16704 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
16706 /* ... or any of the template arguments is a dependent type or
16707 an expression that is type-dependent or value-dependent. */
16708 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
16709 && (any_dependent_template_arguments_p
16710 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
16713 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
16714 argument of the `typeof' expression is not type-dependent, then
16715 it should already been have resolved. */
16716 if (TREE_CODE (type) == TYPEOF_TYPE
16717 || TREE_CODE (type) == DECLTYPE_TYPE)
16720 /* A template argument pack is dependent if any of its packed
16722 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
16724 tree args = ARGUMENT_PACK_ARGS (type);
16725 int i, len = TREE_VEC_LENGTH (args);
16726 for (i = 0; i < len; ++i)
16727 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
16731 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
16732 be template parameters. */
16733 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
16736 /* The standard does not specifically mention types that are local
16737 to template functions or local classes, but they should be
16738 considered dependent too. For example:
16740 template <int I> void f() {
16745 The size of `E' cannot be known until the value of `I' has been
16746 determined. Therefore, `E' must be considered dependent. */
16747 scope = TYPE_CONTEXT (type);
16748 if (scope && TYPE_P (scope))
16749 return dependent_type_p (scope);
16750 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
16751 return type_dependent_expression_p (scope);
16753 /* Other types are non-dependent. */
16757 /* Returns TRUE if TYPE is dependent, in the sense of
16758 [temp.dep.type]. */
16761 dependent_type_p (tree type)
16763 /* If there are no template parameters in scope, then there can't be
16764 any dependent types. */
16765 if (!processing_template_decl)
16767 /* If we are not processing a template, then nobody should be
16768 providing us with a dependent type. */
16770 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
16774 /* If the type is NULL, we have not computed a type for the entity
16775 in question; in that case, the type is dependent. */
16779 /* Erroneous types can be considered non-dependent. */
16780 if (type == error_mark_node)
16783 /* If we have not already computed the appropriate value for TYPE,
16785 if (!TYPE_DEPENDENT_P_VALID (type))
16787 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
16788 TYPE_DEPENDENT_P_VALID (type) = 1;
16791 return TYPE_DEPENDENT_P (type);
16794 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
16795 lookup. In other words, a dependent type that is not the current
16799 dependent_scope_p (tree scope)
16801 return (scope && TYPE_P (scope) && dependent_type_p (scope)
16802 && !currently_open_class (scope));
16805 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
16808 dependent_scope_ref_p (tree expression, bool criterion (tree))
16813 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
16815 if (!TYPE_P (TREE_OPERAND (expression, 0)))
16818 scope = TREE_OPERAND (expression, 0);
16819 name = TREE_OPERAND (expression, 1);
16823 An id-expression is type-dependent if it contains a
16824 nested-name-specifier that contains a class-name that names a
16826 /* The suggested resolution to Core Issue 224 implies that if the
16827 qualifying type is the current class, then we must peek
16830 && currently_open_class (scope)
16831 && !criterion (name))
16833 if (dependent_type_p (scope))
16839 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
16840 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
16844 value_dependent_expression_p (tree expression)
16846 if (!processing_template_decl)
16849 /* A name declared with a dependent type. */
16850 if (DECL_P (expression) && type_dependent_expression_p (expression))
16853 switch (TREE_CODE (expression))
16855 case IDENTIFIER_NODE:
16856 /* A name that has not been looked up -- must be dependent. */
16859 case TEMPLATE_PARM_INDEX:
16860 /* A non-type template parm. */
16864 /* A non-type template parm. */
16865 if (DECL_TEMPLATE_PARM_P (expression))
16867 return value_dependent_expression_p (DECL_INITIAL (expression));
16870 /* A constant with integral or enumeration type and is initialized
16871 with an expression that is value-dependent. */
16872 if (DECL_INITIAL (expression)
16873 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
16874 && value_dependent_expression_p (DECL_INITIAL (expression)))
16878 case DYNAMIC_CAST_EXPR:
16879 case STATIC_CAST_EXPR:
16880 case CONST_CAST_EXPR:
16881 case REINTERPRET_CAST_EXPR:
16883 /* These expressions are value-dependent if the type to which
16884 the cast occurs is dependent or the expression being casted
16885 is value-dependent. */
16887 tree type = TREE_TYPE (expression);
16889 if (dependent_type_p (type))
16892 /* A functional cast has a list of operands. */
16893 expression = TREE_OPERAND (expression, 0);
16896 /* If there are no operands, it must be an expression such
16897 as "int()". This should not happen for aggregate types
16898 because it would form non-constant expressions. */
16899 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
16904 if (TREE_CODE (expression) == TREE_LIST)
16905 return any_value_dependent_elements_p (expression);
16907 return value_dependent_expression_p (expression);
16912 /* A `sizeof' expression is value-dependent if the operand is
16913 type-dependent or is a pack expansion. */
16914 expression = TREE_OPERAND (expression, 0);
16915 if (PACK_EXPANSION_P (expression))
16917 else if (TYPE_P (expression))
16918 return dependent_type_p (expression);
16919 return type_dependent_expression_p (expression);
16922 return dependent_scope_ref_p (expression, value_dependent_expression_p);
16924 case COMPONENT_REF:
16925 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
16926 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
16929 /* A CALL_EXPR may appear in a constant expression if it is a
16930 call to a builtin function, e.g., __builtin_constant_p. All
16931 such calls are value-dependent. */
16934 case NONTYPE_ARGUMENT_PACK:
16935 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
16936 is value-dependent. */
16938 tree values = ARGUMENT_PACK_ARGS (expression);
16939 int i, len = TREE_VEC_LENGTH (values);
16941 for (i = 0; i < len; ++i)
16942 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
16950 tree type2 = TRAIT_EXPR_TYPE2 (expression);
16951 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
16952 || (type2 ? dependent_type_p (type2) : false));
16956 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
16957 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
16960 /* A constant expression is value-dependent if any subexpression is
16961 value-dependent. */
16962 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
16964 case tcc_reference:
16966 return (value_dependent_expression_p
16967 (TREE_OPERAND (expression, 0)));
16969 case tcc_comparison:
16971 return ((value_dependent_expression_p
16972 (TREE_OPERAND (expression, 0)))
16973 || (value_dependent_expression_p
16974 (TREE_OPERAND (expression, 1))));
16976 case tcc_expression:
16980 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
16981 /* In some cases, some of the operands may be missing.
16982 (For example, in the case of PREDECREMENT_EXPR, the
16983 amount to increment by may be missing.) That doesn't
16984 make the expression dependent. */
16985 if (TREE_OPERAND (expression, i)
16986 && (value_dependent_expression_p
16987 (TREE_OPERAND (expression, i))))
16997 /* The expression is not value-dependent. */
17001 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
17002 [temp.dep.expr]. */
17005 type_dependent_expression_p (tree expression)
17007 if (!processing_template_decl)
17010 if (expression == error_mark_node)
17013 /* An unresolved name is always dependent. */
17014 if (TREE_CODE (expression) == IDENTIFIER_NODE
17015 || TREE_CODE (expression) == USING_DECL)
17018 /* Some expression forms are never type-dependent. */
17019 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
17020 || TREE_CODE (expression) == SIZEOF_EXPR
17021 || TREE_CODE (expression) == ALIGNOF_EXPR
17022 || TREE_CODE (expression) == TRAIT_EXPR
17023 || TREE_CODE (expression) == TYPEID_EXPR
17024 || TREE_CODE (expression) == DELETE_EXPR
17025 || TREE_CODE (expression) == VEC_DELETE_EXPR
17026 || TREE_CODE (expression) == THROW_EXPR)
17029 /* The types of these expressions depends only on the type to which
17030 the cast occurs. */
17031 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
17032 || TREE_CODE (expression) == STATIC_CAST_EXPR
17033 || TREE_CODE (expression) == CONST_CAST_EXPR
17034 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
17035 || TREE_CODE (expression) == CAST_EXPR)
17036 return dependent_type_p (TREE_TYPE (expression));
17038 /* The types of these expressions depends only on the type created
17039 by the expression. */
17040 if (TREE_CODE (expression) == NEW_EXPR
17041 || TREE_CODE (expression) == VEC_NEW_EXPR)
17043 /* For NEW_EXPR tree nodes created inside a template, either
17044 the object type itself or a TREE_LIST may appear as the
17046 tree type = TREE_OPERAND (expression, 1);
17047 if (TREE_CODE (type) == TREE_LIST)
17048 /* This is an array type. We need to check array dimensions
17050 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
17051 || value_dependent_expression_p
17052 (TREE_OPERAND (TREE_VALUE (type), 1));
17054 return dependent_type_p (type);
17057 if (TREE_CODE (expression) == SCOPE_REF
17058 && dependent_scope_ref_p (expression,
17059 type_dependent_expression_p))
17062 if (TREE_CODE (expression) == FUNCTION_DECL
17063 && DECL_LANG_SPECIFIC (expression)
17064 && DECL_TEMPLATE_INFO (expression)
17065 && (any_dependent_template_arguments_p
17066 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
17069 if (TREE_CODE (expression) == TEMPLATE_DECL
17070 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
17073 if (TREE_CODE (expression) == STMT_EXPR)
17074 expression = stmt_expr_value_expr (expression);
17076 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
17081 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
17083 if (type_dependent_expression_p (elt))
17089 if (TREE_TYPE (expression) == unknown_type_node)
17091 if (TREE_CODE (expression) == ADDR_EXPR)
17092 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
17093 if (TREE_CODE (expression) == COMPONENT_REF
17094 || TREE_CODE (expression) == OFFSET_REF)
17096 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
17098 expression = TREE_OPERAND (expression, 1);
17099 if (TREE_CODE (expression) == IDENTIFIER_NODE)
17102 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
17103 if (TREE_CODE (expression) == SCOPE_REF)
17106 if (TREE_CODE (expression) == BASELINK)
17107 expression = BASELINK_FUNCTIONS (expression);
17109 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
17111 if (any_dependent_template_arguments_p
17112 (TREE_OPERAND (expression, 1)))
17114 expression = TREE_OPERAND (expression, 0);
17116 gcc_assert (TREE_CODE (expression) == OVERLOAD
17117 || TREE_CODE (expression) == FUNCTION_DECL);
17121 if (type_dependent_expression_p (OVL_CURRENT (expression)))
17123 expression = OVL_NEXT (expression);
17128 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
17130 return (dependent_type_p (TREE_TYPE (expression)));
17133 /* Like type_dependent_expression_p, but it also works while not processing
17134 a template definition, i.e. during substitution or mangling. */
17137 type_dependent_expression_p_push (tree expr)
17140 ++processing_template_decl;
17141 b = type_dependent_expression_p (expr);
17142 --processing_template_decl;
17146 /* Returns TRUE if ARGS contains a type-dependent expression. */
17149 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
17154 for (i = 0; VEC_iterate (tree, args, i, arg); ++i)
17156 if (type_dependent_expression_p (arg))
17162 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
17163 expressions) contains any value-dependent expressions. */
17166 any_value_dependent_elements_p (const_tree list)
17168 for (; list; list = TREE_CHAIN (list))
17169 if (value_dependent_expression_p (TREE_VALUE (list)))
17175 /* Returns TRUE if the ARG (a template argument) is dependent. */
17178 dependent_template_arg_p (tree arg)
17180 if (!processing_template_decl)
17183 if (TREE_CODE (arg) == TEMPLATE_DECL
17184 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17185 return dependent_template_p (arg);
17186 else if (ARGUMENT_PACK_P (arg))
17188 tree args = ARGUMENT_PACK_ARGS (arg);
17189 int i, len = TREE_VEC_LENGTH (args);
17190 for (i = 0; i < len; ++i)
17192 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17198 else if (TYPE_P (arg))
17199 return dependent_type_p (arg);
17201 return (type_dependent_expression_p (arg)
17202 || value_dependent_expression_p (arg));
17205 /* Returns true if ARGS (a collection of template arguments) contains
17206 any types that require structural equality testing. */
17209 any_template_arguments_need_structural_equality_p (tree args)
17216 if (args == error_mark_node)
17219 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17221 tree level = TMPL_ARGS_LEVEL (args, i + 1);
17222 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17224 tree arg = TREE_VEC_ELT (level, j);
17225 tree packed_args = NULL_TREE;
17228 if (ARGUMENT_PACK_P (arg))
17230 /* Look inside the argument pack. */
17231 packed_args = ARGUMENT_PACK_ARGS (arg);
17232 len = TREE_VEC_LENGTH (packed_args);
17235 for (k = 0; k < len; ++k)
17238 arg = TREE_VEC_ELT (packed_args, k);
17240 if (error_operand_p (arg))
17242 else if (TREE_CODE (arg) == TEMPLATE_DECL
17243 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17245 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
17247 else if (!TYPE_P (arg) && TREE_TYPE (arg)
17248 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
17257 /* Returns true if ARGS (a collection of template arguments) contains
17258 any dependent arguments. */
17261 any_dependent_template_arguments_p (const_tree args)
17268 if (args == error_mark_node)
17271 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17273 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
17274 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17275 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
17282 /* Returns TRUE if the template TMPL is dependent. */
17285 dependent_template_p (tree tmpl)
17287 if (TREE_CODE (tmpl) == OVERLOAD)
17291 if (dependent_template_p (OVL_FUNCTION (tmpl)))
17293 tmpl = OVL_CHAIN (tmpl);
17298 /* Template template parameters are dependent. */
17299 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
17300 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
17302 /* So are names that have not been looked up. */
17303 if (TREE_CODE (tmpl) == SCOPE_REF
17304 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
17306 /* So are member templates of dependent classes. */
17307 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
17308 return dependent_type_p (DECL_CONTEXT (tmpl));
17312 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
17315 dependent_template_id_p (tree tmpl, tree args)
17317 return (dependent_template_p (tmpl)
17318 || any_dependent_template_arguments_p (args));
17321 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
17325 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
17329 if (!processing_template_decl)
17332 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
17334 tree decl = TREE_VEC_ELT (declv, i);
17335 tree init = TREE_VEC_ELT (initv, i);
17336 tree cond = TREE_VEC_ELT (condv, i);
17337 tree incr = TREE_VEC_ELT (incrv, i);
17339 if (type_dependent_expression_p (decl))
17342 if (init && type_dependent_expression_p (init))
17345 if (type_dependent_expression_p (cond))
17348 if (COMPARISON_CLASS_P (cond)
17349 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
17350 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
17353 if (TREE_CODE (incr) == MODOP_EXPR)
17355 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
17356 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
17359 else if (type_dependent_expression_p (incr))
17361 else if (TREE_CODE (incr) == MODIFY_EXPR)
17363 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
17365 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
17367 tree t = TREE_OPERAND (incr, 1);
17368 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
17369 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
17378 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
17379 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
17380 no such TYPE can be found. Note that this function peers inside
17381 uninstantiated templates and therefore should be used only in
17382 extremely limited situations. ONLY_CURRENT_P restricts this
17383 peering to the currently open classes hierarchy (which is required
17384 when comparing types). */
17387 resolve_typename_type (tree type, bool only_current_p)
17396 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
17398 scope = TYPE_CONTEXT (type);
17399 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
17400 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
17401 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
17402 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
17403 identifier of the TYPENAME_TYPE anymore.
17404 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
17405 TYPENAME_TYPE instead, we avoid messing up with a possible
17406 typedef variant case. */
17407 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
17409 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
17410 it first before we can figure out what NAME refers to. */
17411 if (TREE_CODE (scope) == TYPENAME_TYPE)
17412 scope = resolve_typename_type (scope, only_current_p);
17413 /* If we don't know what SCOPE refers to, then we cannot resolve the
17415 if (TREE_CODE (scope) == TYPENAME_TYPE)
17417 /* If the SCOPE is a template type parameter, we have no way of
17418 resolving the name. */
17419 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
17421 /* If the SCOPE is not the current instantiation, there's no reason
17422 to look inside it. */
17423 if (only_current_p && !currently_open_class (scope))
17425 /* If SCOPE isn't the template itself, it will not have a valid
17426 TYPE_FIELDS list. */
17427 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
17428 /* scope is either the template itself or a compatible instantiation
17429 like X<T>, so look up the name in the original template. */
17430 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
17432 /* scope is a partial instantiation, so we can't do the lookup or we
17433 will lose the template arguments. */
17435 /* Enter the SCOPE so that name lookup will be resolved as if we
17436 were in the class definition. In particular, SCOPE will no
17437 longer be considered a dependent type. */
17438 pushed_scope = push_scope (scope);
17439 /* Look up the declaration. */
17440 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
17442 result = NULL_TREE;
17444 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
17445 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
17448 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
17449 && TREE_CODE (decl) == TYPE_DECL)
17451 result = TREE_TYPE (decl);
17452 if (result == error_mark_node)
17453 result = NULL_TREE;
17455 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
17456 && DECL_CLASS_TEMPLATE_P (decl))
17460 /* Obtain the template and the arguments. */
17461 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
17462 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
17463 /* Instantiate the template. */
17464 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
17465 /*entering_scope=*/0,
17466 tf_error | tf_user);
17467 if (result == error_mark_node)
17468 result = NULL_TREE;
17471 /* Leave the SCOPE. */
17473 pop_scope (pushed_scope);
17475 /* If we failed to resolve it, return the original typename. */
17479 /* If lookup found a typename type, resolve that too. */
17480 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
17482 /* Ill-formed programs can cause infinite recursion here, so we
17483 must catch that. */
17484 TYPENAME_IS_RESOLVING_P (type) = 1;
17485 result = resolve_typename_type (result, only_current_p);
17486 TYPENAME_IS_RESOLVING_P (type) = 0;
17489 /* Qualify the resulting type. */
17490 quals = cp_type_quals (type);
17492 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
17497 /* EXPR is an expression which is not type-dependent. Return a proxy
17498 for EXPR that can be used to compute the types of larger
17499 expressions containing EXPR. */
17502 build_non_dependent_expr (tree expr)
17506 /* Preserve null pointer constants so that the type of things like
17507 "p == 0" where "p" is a pointer can be determined. */
17508 if (null_ptr_cst_p (expr))
17510 /* Preserve OVERLOADs; the functions must be available to resolve
17513 if (TREE_CODE (inner_expr) == STMT_EXPR)
17514 inner_expr = stmt_expr_value_expr (inner_expr);
17515 if (TREE_CODE (inner_expr) == ADDR_EXPR)
17516 inner_expr = TREE_OPERAND (inner_expr, 0);
17517 if (TREE_CODE (inner_expr) == COMPONENT_REF)
17518 inner_expr = TREE_OPERAND (inner_expr, 1);
17519 if (is_overloaded_fn (inner_expr)
17520 || TREE_CODE (inner_expr) == OFFSET_REF)
17522 /* There is no need to return a proxy for a variable. */
17523 if (TREE_CODE (expr) == VAR_DECL)
17525 /* Preserve string constants; conversions from string constants to
17526 "char *" are allowed, even though normally a "const char *"
17527 cannot be used to initialize a "char *". */
17528 if (TREE_CODE (expr) == STRING_CST)
17530 /* Preserve arithmetic constants, as an optimization -- there is no
17531 reason to create a new node. */
17532 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
17534 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
17535 There is at least one place where we want to know that a
17536 particular expression is a throw-expression: when checking a ?:
17537 expression, there are special rules if the second or third
17538 argument is a throw-expression. */
17539 if (TREE_CODE (expr) == THROW_EXPR)
17542 if (TREE_CODE (expr) == COND_EXPR)
17543 return build3 (COND_EXPR,
17545 TREE_OPERAND (expr, 0),
17546 (TREE_OPERAND (expr, 1)
17547 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
17548 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
17549 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
17550 if (TREE_CODE (expr) == COMPOUND_EXPR
17551 && !COMPOUND_EXPR_OVERLOADED (expr))
17552 return build2 (COMPOUND_EXPR,
17554 TREE_OPERAND (expr, 0),
17555 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
17557 /* If the type is unknown, it can't really be non-dependent */
17558 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
17560 /* Otherwise, build a NON_DEPENDENT_EXPR.
17562 REFERENCE_TYPEs are not stripped for expressions in templates
17563 because doing so would play havoc with mangling. Consider, for
17566 template <typename T> void f<T& g>() { g(); }
17568 In the body of "f", the expression for "g" will have
17569 REFERENCE_TYPE, even though the standard says that it should
17570 not. The reason is that we must preserve the syntactic form of
17571 the expression so that mangling (say) "f<g>" inside the body of
17572 "f" works out correctly. Therefore, the REFERENCE_TYPE is
17574 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
17577 /* ARGS is a vector of expressions as arguments to a function call.
17578 Replace the arguments with equivalent non-dependent expressions.
17579 This modifies ARGS in place. */
17582 make_args_non_dependent (VEC(tree,gc) *args)
17587 for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix)
17589 tree newarg = build_non_dependent_expr (arg);
17591 VEC_replace (tree, args, ix, newarg);
17595 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
17596 with a level one deeper than the actual template parms. */
17603 /* ??? Is it worth caching this for multiple autos at the same level? */
17604 au = cxx_make_type (TEMPLATE_TYPE_PARM);
17605 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
17606 TYPE_DECL, get_identifier ("auto"), au);
17607 TYPE_STUB_DECL (au) = TYPE_NAME (au);
17608 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
17609 (0, processing_template_decl + 1, processing_template_decl + 1,
17610 TYPE_NAME (au), NULL_TREE);
17611 TYPE_CANONICAL (au) = canonical_type_parameter (au);
17612 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
17613 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
17618 /* Given type ARG, return std::initializer_list<ARG>. */
17623 tree std_init_list = namespace_binding
17624 (get_identifier ("initializer_list"), std_node);
17626 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
17628 error ("deducing from brace-enclosed initializer list requires "
17629 "#include <initializer_list>");
17630 return error_mark_node;
17632 argvec = make_tree_vec (1);
17633 TREE_VEC_ELT (argvec, 0) = arg;
17634 return lookup_template_class (std_init_list, argvec, NULL_TREE,
17635 NULL_TREE, 0, tf_warning_or_error);
17638 /* Replace auto in TYPE with std::initializer_list<auto>. */
17641 listify_autos (tree type, tree auto_node)
17643 tree init_auto = listify (auto_node);
17644 tree argvec = make_tree_vec (1);
17645 TREE_VEC_ELT (argvec, 0) = init_auto;
17646 if (processing_template_decl)
17647 argvec = add_to_template_args (current_template_args (), argvec);
17648 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
17651 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
17652 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
17655 do_auto_deduction (tree type, tree init, tree auto_node)
17657 tree parms, tparms, targs;
17661 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
17662 with either a new invented type template parameter U or, if the
17663 initializer is a braced-init-list (8.5.4), with
17664 std::initializer_list<U>. */
17665 if (BRACE_ENCLOSED_INITIALIZER_P (init))
17666 type = listify_autos (type, auto_node);
17668 parms = build_tree_list (NULL_TREE, type);
17670 tparms = make_tree_vec (1);
17671 targs = make_tree_vec (1);
17672 TREE_VEC_ELT (tparms, 0)
17673 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
17674 val = type_unification_real (tparms, targs, parms, args, 1, 0,
17675 DEDUCE_CALL, LOOKUP_NORMAL);
17678 error ("unable to deduce %qT from %qE", type, init);
17679 return error_mark_node;
17682 if (processing_template_decl)
17683 targs = add_to_template_args (current_template_args (), targs);
17684 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
17687 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
17691 splice_late_return_type (tree type, tree late_return_type)
17695 if (late_return_type == NULL_TREE)
17697 argvec = make_tree_vec (1);
17698 TREE_VEC_ELT (argvec, 0) = late_return_type;
17699 if (processing_template_decl)
17700 argvec = add_to_template_args (current_template_args (), argvec);
17701 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
17704 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
17707 is_auto (const_tree type)
17709 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17710 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
17716 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
17717 appear as a type-specifier for the declaration in question, we don't
17718 have to look through the whole type. */
17721 type_uses_auto (tree type)
17723 enum tree_code code;
17724 if (is_auto (type))
17727 code = TREE_CODE (type);
17729 if (code == POINTER_TYPE || code == REFERENCE_TYPE
17730 || code == OFFSET_TYPE || code == FUNCTION_TYPE
17731 || code == METHOD_TYPE || code == ARRAY_TYPE)
17732 return type_uses_auto (TREE_TYPE (type));
17734 if (TYPE_PTRMEMFUNC_P (type))
17735 return type_uses_auto (TREE_TYPE (TREE_TYPE
17736 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17741 /* For a given template T, return the list of typedefs referenced
17742 in T for which access check is needed at T instantiation time.
17743 T is either a FUNCTION_DECL or a RECORD_TYPE.
17744 Those typedefs were added to T by the function
17745 append_type_to_template_for_access_check. */
17748 get_types_needing_access_check (tree t)
17750 tree ti, result = NULL_TREE;
17752 if (!t || t == error_mark_node)
17755 if (!(ti = get_template_info (t)))
17758 if (CLASS_TYPE_P (t)
17759 || TREE_CODE (t) == FUNCTION_DECL)
17761 if (!TI_TEMPLATE (ti))
17764 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
17770 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
17771 tied to T. That list of typedefs will be access checked at
17772 T instantiation time.
17773 T is either a FUNCTION_DECL or a RECORD_TYPE.
17774 TYPE_DECL is a TYPE_DECL node representing a typedef.
17775 SCOPE is the scope through which TYPE_DECL is accessed.
17777 This function is a subroutine of
17778 append_type_to_template_for_access_check. */
17781 append_type_to_template_for_access_check_1 (tree t,
17787 if (!t || t == error_mark_node)
17790 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
17791 || CLASS_TYPE_P (t))
17793 && TREE_CODE (type_decl) == TYPE_DECL
17796 if (!(ti = get_template_info (t)))
17799 gcc_assert (TI_TEMPLATE (ti));
17801 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti) =
17802 tree_cons (type_decl, scope, TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti));
17805 /* Append TYPE_DECL to the template TEMPL.
17806 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
17807 At TEMPL instanciation time, TYPE_DECL will be checked to see
17808 if it can be accessed through SCOPE.
17810 e.g. consider the following code snippet:
17817 template<class U> struct S
17824 At S<char> instantiation time, we need to check the access of C::myint
17825 In other words, we need to check the access of the myint typedef through
17826 the C scope. For that purpose, this function will add the myint typedef
17827 and the scope C through which its being accessed to a list of typedefs
17828 tied to the template S. That list will be walked at template instantiation
17829 time and access check performed on each typedefs it contains.
17830 Note that this particular code snippet should yield an error because
17831 myint is private to C. */
17834 append_type_to_template_for_access_check (tree templ,
17840 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
17842 /* Make sure we don't append the type to the template twice. */
17843 for (node = get_types_needing_access_check (templ);
17845 node = TREE_CHAIN (node))
17847 tree decl = TREE_PURPOSE (node);
17848 tree type_scope = TREE_VALUE (node);
17850 if (decl == type_decl && type_scope == scope)
17854 append_type_to_template_for_access_check_1 (templ, type_decl, scope);
17857 /* Set up the hash tables for template instantiations. */
17860 init_template_processing (void)
17862 decl_specializations = htab_create_ggc (37,
17863 hash_specialization,
17864 eq_specializations,
17866 type_specializations = htab_create_ggc (37,
17867 hash_specialization,
17868 eq_specializations,
17872 #include "gt-cp-pt.h"